fix(small-model): don't double up /v1 in Anthropic baseURL override
Match @ai-sdk/anthropic exactly: baseURL is the full API prefix, so /messages is appended as-is instead of unconditionally inserting /v1, which broke configs where the baseURL already ends in /v1 (the common form OpenCode itself passes to @ai-sdk/anthropic). Addresses review feedback from @btriapitsyn on PR #3000.
This commit is contained in:
@@ -103,7 +103,10 @@ other runtime API.
|
|||||||
`https://chatgpt.com/backend-api/codex/responses` with
|
`https://chatgpt.com/backend-api/codex/responses` with
|
||||||
`ChatGPT-Account-Id`; expired tokens are refreshed against
|
`ChatGPT-Account-Id`; expired tokens are refreshed against
|
||||||
`auth.openai.com` (single-flight) and written back to `auth.json`.
|
`auth.openai.com` (single-flight) and written back to `auth.json`.
|
||||||
- **Anthropic** (`type: api`): `/v1/messages` with `x-api-key`.
|
- **Anthropic** (`type: api`): `/messages` with `x-api-key`, against
|
||||||
|
`provider.anthropic.options.baseURL` when configured (used as-is, matching
|
||||||
|
`@ai-sdk/anthropic` — no `/v1` is inserted) or `https://api.anthropic.com/v1`
|
||||||
|
otherwise.
|
||||||
- **Google** (`type: api`): `generateContent` with `x-goog-api-key`; Gemini 3
|
- **Google** (`type: api`): `generateContent` with `x-goog-api-key`; Gemini 3
|
||||||
uses `thinkingLevel` while older Flash models use `thinkingBudget: 0`.
|
uses `thinkingLevel` while older Flash models use `thinkingBudget: 0`.
|
||||||
- Everything else: OpenAI-compatible `/chat/completions` against the
|
- Everything else: OpenAI-compatible `/chat/completions` against the
|
||||||
|
|||||||
@@ -341,7 +341,10 @@ const callMessages = async ({ url, headers, modelID, prompt, system, maxOutputTo
|
|||||||
};
|
};
|
||||||
|
|
||||||
const callAnthropic = async ({ apiKey, baseURL, modelID, prompt, system, maxOutputTokens, responseSchema, timeoutMs, signal }) => callMessages({
|
const callAnthropic = async ({ apiKey, baseURL, modelID, prompt, system, maxOutputTokens, responseSchema, timeoutMs, signal }) => callMessages({
|
||||||
url: `${(baseURL || 'https://api.anthropic.com').replace(/\/+$/, '')}/v1/messages`,
|
// Matches @ai-sdk/anthropic: baseURL is the full API prefix (commonly
|
||||||
|
// already ending in /v1), so it gets /messages appended as-is rather than
|
||||||
|
// having /v1/messages appended, which would double up a configured /v1.
|
||||||
|
url: `${(baseURL || 'https://api.anthropic.com/v1').replace(/\/+$/, '')}/messages`,
|
||||||
headers: {
|
headers: {
|
||||||
'x-api-key': apiKey,
|
'x-api-key': apiKey,
|
||||||
'anthropic-version': '2023-06-01',
|
'anthropic-version': '2023-06-01',
|
||||||
|
|||||||
@@ -325,7 +325,7 @@ describe('callSmallModel — custom provider config', () => {
|
|||||||
|
|
||||||
it('respects provider.anthropic.options.baseURL over the hardcoded Anthropic endpoint', async () => {
|
it('respects provider.anthropic.options.baseURL over the hardcoded Anthropic endpoint', async () => {
|
||||||
readConfig.mockReturnValue({
|
readConfig.mockReturnValue({
|
||||||
provider: { anthropic: { options: { baseURL: 'http://127.0.0.1:3456' } } },
|
provider: { anthropic: { options: { baseURL: 'http://127.0.0.1:3456/v1' } } },
|
||||||
});
|
});
|
||||||
fetchMock.mockResolvedValue(anthropicOk('ok'));
|
fetchMock.mockResolvedValue(anthropicOk('ok'));
|
||||||
|
|
||||||
@@ -344,6 +344,24 @@ describe('callSmallModel — custom provider config', () => {
|
|||||||
expect(init.headers['x-api-key']).toBe('dummy');
|
expect(init.headers['x-api-key']).toBe('dummy');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('uses a bare-host baseURL as-is without inserting /v1, matching @ai-sdk/anthropic', async () => {
|
||||||
|
readConfig.mockReturnValue({
|
||||||
|
provider: { anthropic: { options: { baseURL: 'http://127.0.0.1:3456' } } },
|
||||||
|
});
|
||||||
|
fetchMock.mockResolvedValue(anthropicOk('ok'));
|
||||||
|
|
||||||
|
await callSmallModel({
|
||||||
|
auth: { anthropic: { type: 'api', key: 'dummy' } },
|
||||||
|
catalog: {},
|
||||||
|
workingDirectory: '/proj',
|
||||||
|
providerID: 'anthropic',
|
||||||
|
modelID: 'claude-haiku-4-5',
|
||||||
|
prompt: 'hi',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(lastCall(fetchMock).url).toBe('http://127.0.0.1:3456/messages');
|
||||||
|
});
|
||||||
|
|
||||||
it('falls back to https://api.anthropic.com when no anthropic baseURL override is configured', async () => {
|
it('falls back to https://api.anthropic.com when no anthropic baseURL override is configured', async () => {
|
||||||
readConfig.mockReturnValue({});
|
readConfig.mockReturnValue({});
|
||||||
fetchMock.mockResolvedValue(anthropicOk('ok'));
|
fetchMock.mockResolvedValue(anthropicOk('ok'));
|
||||||
|
|||||||
Reference in New Issue
Block a user