Skip to content

fix(provider): parse URL-based provider IDs - #40071

Open
obadakhalili wants to merge 3 commits into
anomalyco:devfrom
obadakhalili:parse-gateway-model
Open

fix(provider): parse URL-based provider IDs#40071
obadakhalili wants to merge 3 commits into
anomalyco:devfrom
obadakhalili:parse-gateway-model

Conversation

@obadakhalili

@obadakhalili obadakhalili commented Aug 1, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #39926

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

parseModel does not account for models connected through a well-known gateway. It expects providerID/modelID, but fails when the provider ID is itself a URL. Normal prompts work because they send the provider and model IDs separately, while commands combine them into a single string that the server must parse.

the PR fixes parseModel to account for gateway-connected models.

How did you verify your code works?

  • create a mock gateway server to connect to it:
const host = "127.0.0.1"
const port = 8787
const origin = `http://${host}:${port}`
const model = process.env.ANTHROPIC_MODEL ?? "claude-sonnet-4-5"

const server = Bun.serve({
  hostname: host,
  port,
  async fetch(request) {
    const url = new URL(request.url)

    if (url.pathname === "/.well-known/opencode") {
      return Response.json({
        auth: {
          command: ["sh", "-c", 'printf %s "$ANTHROPIC_API_KEY"'],
          env: "ANTHROPIC_API_KEY",
        },
        config: {
          provider: {
            [origin]: {
              name: "Local Anthropic Gateway",
              npm: "@ai-sdk/anthropic",
              options: {
                apiKey: "{env:ANTHROPIC_API_KEY}",
                baseURL: `${origin}/v1`,
              },
              models: {
                [model]: {
                  name: model,
                  attachment: true,
                  reasoning: true,
                  tool_call: true,
                  limit: {
                    context: 200000,
                    output: 64000,
                  },
                  modalities: {
                    input: ["text", "image", "pdf"],
                    output: ["text"],
                  },
                },
              },
            },
          },
        },
      })
    }

    if (!url.pathname.startsWith("/v1/")) return new Response("Not found", { status: 404 })

    const headers = new Headers(request.headers)
    headers.delete("host")
    headers.delete("content-length")
    headers.delete("accept-encoding")

    const response = await fetch(`https://api.anthropic.com${url.pathname}${url.search}`, {
      method: request.method,
      headers,
      body: request.body,
      redirect: "manual",
    })
    const responseHeaders = new Headers(response.headers)
    responseHeaders.delete("content-encoding")
    responseHeaders.delete("content-length")
    return new Response(response.body, {
      status: response.status,
      statusText: response.statusText,
      headers: responseHeaders,
    })
  },
})

console.log(`Local Anthropic well-known gateway: ${server.url}`)
console.log(`Model: ${model}`)
  • register the gateway:
export ANTHROPIC_API_KEY="your-key"
bun dev auth login http://127.0.0.1:8787
  • start opencode with a test command:
export OPENCODE_CONFIG_CONTENT='{"command":{"gateway-test":{"template":"Reply with exactly COMMAND_OK"}}}'
bun dev
  • select the gateway model in /models

  • sending a prompt works

  • before fix: /gateway-test fails with "Model not found"

  • after fix: /gateway-test succeeds

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Aug 1, 2026
@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Aug 1, 2026
@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@simcop2387

Copy link
Copy Markdown

Can confirm as the original reporter that this does seem to fix the issue for me. Still trying to learn how to properly build and install from the branch to do more extensive testing but with npm run dev from within at least it's doing the right things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] parseModel breaks URL-based provider IDs used by opencode auth login

2 participants