Skip to content

[Windows] @ai-sdk/openai-compatible cannot connect to localhost:9877 — Bun fetch fails for 127.0.0.1 while Node fetch works (Unable to connect. Is the computer able to access the url?) #38140

Description

@Zhuchen00123

Summary

On Windows, opencode's embedded Bun runtime cannot connect to a localhost @ai-sdk/openai-compatible provider at http://127.0.0.1:9877/v1. Every request fails with AI_APICallError: Cannot connect to API: Unable to connect. Is the computer able to access the url?. The same URL is reachable from Node.js fetch(), PowerShell Invoke-WebRequest, and curl on the same machine — so this is an opencode/Bun runtime issue, not a network or proxy issue.

This appears related to #28418 (@ai-sdk/openai-compatible fails to connect to LAN addresses) and #37043 (opencode ignores NO_PROXY), but this reproduces on plain 127.0.0.1 with no HTTP_PROXY/HTTPS_PROXY env vars set.

Environment

  • opencode: v1.18.4 (installed via npm i -g opencode-ai, binary at D:\node_global\opencode.ps1)
  • OS: Windows 11, PowerShell 5.1
  • Node: v22.19.0
  • Bun: installed at D:\node_global\bun.ps1 (bundled runtime used by opencode)
  • Provider: @ai-sdk/openai-compatible pointing at a local reverse proxy (commandcode-api-proxy v0.3.0) on http://127.0.0.1:9877/v1

Reproduction

Config (~/.config/opencode/opencode.jsonc)

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "commandcode": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Command Code",
      "options": {
        "baseURL": "http://127.0.0.1:9877/v1",
        "apiKey": "proxy-managed"
      },
      "models": {
        "deepseek-v4-pro": { "name": "DeepSeek V4 Pro" }
      }
    }
  }
}

Steps

  1. Start a local OpenAI-compatible server on 127.0.0.1:9877 (any will do — I used commandcode-api-proxy).
  2. Launch opencode, select the commandcode provider / deepseek-v4-pro model.
  3. Send any message.

Actual

opencode retries 3× then fails:

AI_APICallError: Cannot connect to API: Unable to connect. Is the computer able to access the url?
(cause: Error: Unable to connect. Is the computer able to access the url?)

Full log (~/.local/share/opencode/log/opencode.log):

timestamp=2026-07-21T15:34:23.959Z level=ERROR message="stream error" providerID=commandcode modelID=deepseek-v4-pro error.error="AI_APICallError: Cannot connect to API: Unable to connect. Is the computer able to access the url? (cause: Error: Unable to connect. Is the computer able to access the url?)"
timestamp=2026-07-21T15:34:29.384Z level=ERROR message="stream error" providerID=commandcode modelID=deepseek-v4-pro error.error="AI_RetryError: Failed after 3 attempts. Last error: Cannot connect to API: Unable to connect. Is the computer able to access the url?"

Notably, https://models.dev/api.json also fails with the exact same error — so the Bun runtime cannot reach any URL, not just localhost:

timestamp=2026-07-21T15:34:05.945Z level=ERROR message="Failed to fetch models.dev" cause="Cause([Fail(HttpClientError: Transport error (GET https://models.dev/api.json) (cause: Error: Unable to connect. Is the computer able to access the url?))])"

Expected

opencode connects to http://127.0.0.1:9877/v1/chat/completions and returns a response.

Proof the endpoint is reachable (same machine, same moment)

Node.js fetch() — works

const r = await fetch("http://127.0.0.1:9877/health");
console.log(r.status, await r.text());
// → 200 {"status":"ok","version":"0.1.0"}

PowerShell Invoke-WebRequest — works

Invoke-WebRequest "http://127.0.0.1:9877/health" -UseBasicParsing
# → 200 {"status":"ok","version":"0.1.0"}

$body = '{"model":"deepseek/deepseek-v4-flash","messages":[{"role":"user","content":"hi"}],"stream":false,"max_tokens":20}'
Invoke-WebRequest "http://127.0.0.1:9877/v1/chat/completions" -Method POST -Headers @{Authorization="Bearer proxy-managed";"Content-Type"="application/json"} -Body $body
# → 200, valid chat completion returned

Port is listening

Get-NetTCPConnection -LocalPort 9877
# → 127.0.0.1:9877  Listen  (OwningProcess confirmed)

What I've already tried

  • No proxy env vars set: HTTP_PROXY, HTTPS_PROXY, NO_PROXY are all unset (verified via Get-ChildItem env:).
  • Plugin that sets init.proxy for non-local URLs only: I have a proxy.ts plugin that routes non-local fetches through a local HTTP proxy at 127.0.0.1:60230, but it explicitly skips localhost/127.0.0.1/::1. Removing the plugin entirely does not fix the issue.
  • Both opencode.json and opencode.jsonc: consolidated to a single opencode.jsonc — no change.
  • Different localhost ports: tried 8787 (got EACCES — Windows reserved range), 9877, 9876 — all fail the same way in opencode.
  • 127.0.0.1 vs localhost: not yet tested swapping, but @ai-sdk/openai-compatible provider fails to connect to LAN addresses — FailedToOpenSocket errno:0 #28418 reports hostname also fails.

Hypothesis

opencode's embedded Bun runtime fetch() is broken for outbound HTTP on this Windows environment — it fails for both 127.0.0.1 (local proxy) and https://models.dev (public HTTPS). Node.js fetch() on the same machine works for both. This smells like a Bun-on-Windows socket/transport bug rather than a provider config issue, possibly the same class as #28418 but broader (affects public URLs too, not just LAN).

Questions / Workarounds sought

  1. Is there a way to force opencode to use Node's fetch instead of Bun's for provider calls?
  2. Any known Windows-specific Bun fetch workaround (env var, config flag)?
  3. Could this be related to the bundled Bun version? If so, is there a way to override it?

Happy to run any additional diagnostics the maintainers need.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions