Fix chat provider routing for OpenCode Zen/Go
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:
@@ -68,8 +68,14 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
const apiKey = creds[0].token;
|
||||
const baseUrl = getBaseUrl(provider, creds[0].baseUrl);
|
||||
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
|
||||
const providerMessages: ProviderMessages[] = [];
|
||||
@@ -125,7 +131,7 @@ export async function POST(request: NextRequest) {
|
||||
// Stream from provider
|
||||
const events = useAnthropic
|
||||
? 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) {
|
||||
if (event.type === "text_delta") {
|
||||
|
||||
@@ -327,6 +327,8 @@ const PROVIDER_BASE_URLS: Record<string, string> = {
|
||||
openai: "https://api.openai.com",
|
||||
openrouter: "https://openrouter.ai/api",
|
||||
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";
|
||||
@@ -335,6 +337,6 @@ export function isAnthropicProvider(provider: string): boolean {
|
||||
return provider === "anthropic";
|
||||
}
|
||||
|
||||
export function getBaseUrl(provider: string, credentialBaseUrl?: string): string {
|
||||
return credentialBaseUrl || PROVIDER_BASE_URLS[provider] || "https://api.openai.com";
|
||||
export function getBaseUrl(provider: string, credentialBaseUrl?: string): string | null {
|
||||
return credentialBaseUrl || PROVIDER_BASE_URLS[provider] || null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user