Fix chat provider routing for OpenCode Zen/Go
All checks were successful
CI / lint-and-test (push) Successful in 38s
Deploy Production / deploy (push) Successful in 1m30s
CI / build (push) Successful in 2m16s

OpenCode Zen and Go models were being routed to api.openai.com because
their base URLs weren't in the provider map. Add correct base URLs
(opencode.ai/zen and opencode.ai/zen/go) and error on unknown providers
instead of silently falling back to OpenAI.
This commit is contained in:
Julia McGhee
2026-03-22 10:55:32 +00:00
parent 8f06467880
commit e0c6d3940a
2 changed files with 12 additions and 4 deletions

View File

@@ -68,8 +68,14 @@ export async function POST(request: NextRequest) {
} }
const apiKey = creds[0].token; const apiKey = creds[0].token;
const baseUrl = getBaseUrl(provider, creds[0].baseUrl);
const useAnthropic = isAnthropicProvider(provider); const useAnthropic = isAnthropicProvider(provider);
const baseUrl = useAnthropic ? null : getBaseUrl(provider, creds[0].baseUrl);
if (!useAnthropic && !baseUrl) {
return Response.json(
{ error: `No API base URL known for provider: ${provider}. Set a baseUrl on the credential.` },
{ status: 400 },
);
}
// Build conversation history in provider format // Build conversation history in provider format
const providerMessages: ProviderMessages[] = []; const providerMessages: ProviderMessages[] = [];
@@ -125,7 +131,7 @@ export async function POST(request: NextRequest) {
// Stream from provider // Stream from provider
const events = useAnthropic const events = useAnthropic
? streamAnthropic(apiKey, model, SYSTEM_PROMPT, currentMessages, toolsAsAnthropic()) ? streamAnthropic(apiKey, model, SYSTEM_PROMPT, currentMessages, toolsAsAnthropic())
: streamOpenAI(apiKey, baseUrl, model, SYSTEM_PROMPT, currentMessages, toolsAsOpenAI()); : streamOpenAI(apiKey, baseUrl!, model, SYSTEM_PROMPT, currentMessages, toolsAsOpenAI());
for await (const event of events) { for await (const event of events) {
if (event.type === "text_delta") { if (event.type === "text_delta") {

View File

@@ -327,6 +327,8 @@ const PROVIDER_BASE_URLS: Record<string, string> = {
openai: "https://api.openai.com", openai: "https://api.openai.com",
openrouter: "https://openrouter.ai/api", openrouter: "https://openrouter.ai/api",
google: "https://generativelanguage.googleapis.com", google: "https://generativelanguage.googleapis.com",
"opencode-zen": "https://opencode.ai/zen",
"opencode-go": "https://opencode.ai/zen/go",
}; };
export type ChatProvider = "anthropic" | "openai" | "openrouter" | "google"; export type ChatProvider = "anthropic" | "openai" | "openrouter" | "google";
@@ -335,6 +337,6 @@ export function isAnthropicProvider(provider: string): boolean {
return provider === "anthropic"; return provider === "anthropic";
} }
export function getBaseUrl(provider: string, credentialBaseUrl?: string): string { export function getBaseUrl(provider: string, credentialBaseUrl?: string): string | null {
return credentialBaseUrl || PROVIDER_BASE_URLS[provider] || "https://api.openai.com"; return credentialBaseUrl || PROVIDER_BASE_URLS[provider] || null;
} }