Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions apps/daemon/src/runtimes/byok-opencode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ function isExactOrigin(value: string, origin: string): boolean {
}
}

function isRealOpenAIHost(baseUrl: string): boolean {
if (!baseUrl) return true;
try {
return new URL(baseUrl).hostname === 'api.openai.com';
} catch {
return true;
}
}

function buildProviderEntry(
protocol: ByokChatProviderConfig['protocol'],
baseUrl: string,
Expand Down Expand Up @@ -207,11 +216,23 @@ function buildProviderEntry(
},
};
case 'openai':
// Real OpenAI speaks the Responses API via @ai-sdk/openai. Every other
// host under the "openai" protocol (DeepSeek, vLLM, etc.) only serves
// /chat/completions, so route it through @ai-sdk/openai-compatible.
if (isRealOpenAIHost(baseUrl)) {
return {
npm: '@ai-sdk/openai',
options: {
...apiKeyOption,
...(baseUrl ? { baseURL: baseUrl } : {}),
},
};
}
return {
npm: '@ai-sdk/openai',
npm: '@ai-sdk/openai-compatible',
options: {
baseURL: baseUrl,
...apiKeyOption,
...(baseUrl ? { baseURL: baseUrl } : {}),
},
};
case 'senseaudio':
Expand Down
25 changes: 24 additions & 1 deletion apps/daemon/tests/runtimes/byok-opencode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ describe('byok-opencode runtime config', () => {
});
});

it('routes OpenAI-protocol BYOK with a non-OpenAI base URL to the OpenAI-compatible provider package', () => {
expect(buildOpenCodeByokProviderConfig(
{ protocol: 'openai', apiKey: 'sk-deepseek', baseUrl: 'https://api.deepseek.com' },
'deepseek-v4-pro',
)?.config).toMatchObject({
provider: {
[BYOK_OPENCODE_PROVIDER_ID]: {
npm: '@ai-sdk/openai-compatible',
options: {
baseURL: 'https://api.deepseek.com',
apiKey: `{env:${BYOK_OPENCODE_API_KEY_ENV}}`,
},
},
},
});
expect(buildOpenCodeByokProviderConfig(
{ protocol: 'openai', apiKey: 'sk-openai', baseUrl: 'https://api.openai.com' },
'deepseek-v4-pro',
)?.config).toMatchObject({
provider: { [BYOK_OPENCODE_PROVIDER_ID]: { npm: '@ai-sdk/openai' } },
});
});

it('normalizes origin-only native provider base URLs for OpenCode provider packages', () => {
expect(buildOpenCodeByokProviderConfig(
{ protocol: 'anthropic', apiKey: 'sk-ant', baseUrl: 'https://api.anthropic.com' },
Expand Down Expand Up @@ -272,7 +295,7 @@ describe('byok-opencode runtime config', () => {
expect(out?.config).toMatchObject({
provider: {
[BYOK_OPENCODE_PROVIDER_ID]: {
npm: '@ai-sdk/openai',
npm: '@ai-sdk/openai-compatible',
options: {
baseURL: 'http://127.0.0.1:8000/v1',
},
Expand Down
Loading