Skip to content

Commit 57e34b9

Browse files
deepracticexsclaude
andcommitted
fix: normalize baseURL per protocol — auto-append /v1 if missing
@ai-sdk/anthropic expects baseURL to include /v1, but users often configure without it. Now normalizeBaseUrl() handles all cases: - https://xxx/api/coding → https://xxx/api/coding/v1 (appended) - https://xxx/api/coding/v1 → unchanged - https://xxx/api/coding/v1/ → trailing slash removed Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1bcbce9 commit 57e34b9

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

packages/routerx/src/app.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,49 @@ export interface RouterXConfig {
3232
apiKey?: string;
3333
}
3434

35+
// ============================================================================
36+
// BaseURL normalization — each SDK expects a specific suffix
37+
// ============================================================================
38+
39+
const PROTOCOL_BASE_SUFFIX: Record<string, string> = {
40+
"openai-compatible": "/v1",
41+
anthropic: "/v1",
42+
};
43+
44+
const PROTOCOL_DEFAULT_BASE: Record<string, string> = {
45+
"openai-compatible": "https://api.openai.com/v1",
46+
anthropic: "https://api.anthropic.com/v1",
47+
};
48+
49+
function normalizeBaseUrl(baseUrl: string, protocol: string): string {
50+
const suffix = PROTOCOL_BASE_SUFFIX[protocol];
51+
if (!suffix) return baseUrl;
52+
const cleaned = baseUrl.replace(/\/+$/, "");
53+
if (cleaned.endsWith(suffix)) return cleaned;
54+
return cleaned + suffix;
55+
}
56+
3557
// ============================================================================
3658
// Vercel AI SDK model factory
3759
// ============================================================================
3860

3961
function createModel(provider: RegisteredProvider, modelId: string): LanguageModel {
62+
const baseURL = provider.baseUrl
63+
? normalizeBaseUrl(provider.baseUrl, provider.protocol)
64+
: PROTOCOL_DEFAULT_BASE[provider.protocol];
65+
4066
switch (provider.protocol) {
4167
case "openai-compatible": {
4268
const p = createOpenAICompatible({
4369
name: provider.id,
44-
baseURL: provider.baseUrl ?? "https://api.openai.com/v1",
70+
baseURL,
4571
apiKey: provider.apiKey,
4672
});
4773
return p(modelId);
4874
}
4975
case "anthropic": {
5076
const p = createAnthropic({
51-
baseURL: provider.baseUrl ?? "https://api.anthropic.com",
77+
baseURL,
5278
apiKey: provider.apiKey,
5379
});
5480
return p(modelId);

0 commit comments

Comments
 (0)