Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): utility 直连 Anthropic 时剥掉 SDK 专用的 [1m] 模型后缀 (#2510)

fico-huba8991ae9df52
diff --git a/apps/desktop/src/main/utility-model/__tests__/oneShotCandidates.test.ts b/apps/desktop/src/main/utility-model/__tests__/oneShotCandidates.test.ts
index 5462d7e991..7bfac0bfed 100644
--- a/apps/desktop/src/main/utility-model/__tests__/oneShotCandidates.test.ts
+++ b/apps/desktop/src/main/utility-model/__tests__/oneShotCandidates.test.ts
@@ -80,7 +80,7 @@ import { readModelDisableOverrides } from '../../maker-host/model-disable-store.
import { isProviderRouteMutationInProgress } from '../../maker-host/provider-route.js';
import { readCustomProviderKey } from '../../secrets/providerSecretStore.js';
import { getUtilityModelChainProfiles } from '../UtilityModelSelection.js';
-import { getUtilityTextCandidates, requestUtilityText } from '../oneShotCandidates.js';
+import { getUtilityTextCandidates, requestUtilityText, toAnthropicApiModelId } from '../oneShotCandidates.js';
const getProfiles = vi.mocked(getUtilityModelChainProfiles);
const readKey = vi.mocked(readClaudeApiKey);
@@ -1033,6 +1033,77 @@ describe('utility one-shot candidates', () => {
expect(body.max_tokens).toBe(64_000);
});
+ it('内置 Anthropic 直连:1M 目录模型的 body.model 用裸目录 id,不带 SDK 专用 [1m] 后缀(#2429)', async () => {
+ activeCatalog.mockReturnValue({
+ providers: [{
+ id: 'anthropic',
+ name: 'Anthropic',
+ source: 'builtin',
+ agents: ['claude-code'],
+ auth: { method: 'oauth' },
+ routing: {
+ 'claude-code': { upstream: 'https://anthropic.example/api/v1', authStrategy: 'oauth-passthrough' },
+ },
+ models: {
+ 'claude-code': [{ id: 'claude-fable-5', name: 'Fable', contextWindow: 1_000_000, maxOutput: 64_000 }],
+ },
+ }],
+ } as never);
+ readClaudeOAuth.mockResolvedValue({ accessToken: 'anthropic-token' });
+ fetchMock.mockResolvedValueOnce({
+ ok: true,
+ text: async () => JSON.stringify({ content: [{ type: 'text', text: 'verdict' }] }),
+ } as never);
+
+ const result = await requestUtilityText(makerMock(false), 'review this', {
+ providerId: 'anthropic',
+ agentKind: 'claude-code',
+ model: 'claude-fable-5',
+ });
+
+ expect(result).toMatchObject({ ok: true, providerId: 'anthropic' });
+ const body = JSON.parse(String(fetchMock.mock.calls[0]?.[1]?.body));
+ expect(body.model).toBe('claude-fable-5');
+ });
+
+ it('toAnthropicApiModelId:剥掉尾部 [1m],其余 id 原样返回(防御显示 id 泄入)', () => {
+ expect(toAnthropicApiModelId('claude-opus-5[1m]')).toBe('claude-opus-5');
+ expect(toAnthropicApiModelId('claude-fable-5')).toBe('claude-fable-5');
+ expect(toAnthropicApiModelId('claude-haiku-4-5-20251001')).toBe('claude-haiku-4-5-20251001');
+ });
+
+ it('内置 Anthropic 直连:非 1M 模型的 body.model 保持目录 id 不变(不回归)', async () => {
+ activeCatalog.mockReturnValue({
+ providers: [{
+ id: 'anthropic',
+ name: 'Anthropic',
+ source: 'builtin',
+ agents: ['claude-code'],
+ auth: { method: 'oauth' },
+ routing: {
+ 'claude-code': { upstream: 'https://anthropic.example/api/v1', authStrategy: 'oauth-passthrough' },
+ },
+ models: {
+ 'claude-code': [{ id: 'claude-haiku-4-5-20251001', name: 'Haiku', contextWindow: 200_000, maxOutput: 64_000 }],
+ },
+ }],
+ } as never);
+ readClaudeOAuth.mockResolvedValue({ accessToken: 'anthropic-token' });
+ fetchMock.mockResolvedValueOnce({
+ ok: true,
+ text: async () => JSON.stringify({ content: [{ type: 'text', text: 'verdict' }] }),
+ } as never);
+
+ await requestUtilityText(makerMock(false), 'review this', {
+ providerId: 'anthropic',
+ agentKind: 'claude-code',
+ model: 'claude-haiku-4-5-20251001',
+ });
+
+ const body = JSON.parse(String(fetchMock.mock.calls[0]?.[1]?.body));
+ expect(body.model).toBe('claude-haiku-4-5-20251001');
+ });
+
it('缺省不传 maxTokens 时,xd wire 不发送输出上限(模型自然输出)', async () => {
activeCatalog.mockReturnValue({
providers: [{
@@ -1443,7 +1514,9 @@ describe('utility one-shot candidates', () => {
expect(fetchMock).toHaveBeenCalledWith('https://anthropic.example/api/v1/messages', expect.anything());
const init = fetchMock.mock.calls[0]?.[1] as { headers: Record<string, string>; body: string };
expect(init.headers.Authorization).toBe('Bearer anthropic-token');
- expect(JSON.parse(init.body).model).toBe('claude-sonnet-4-6[1m]');
+ // [1m] 是 Claude Code SDK 的 beta 通道后缀,直连 /v1/messages 会 404(#2429):
+ // 直连请求体必须用目录裸 id。
+ expect(JSON.parse(init.body).model).toBe('claude-sonnet-4-6');
});
it('uses OpenAI Codex OAuth and strips the bridge model prefix on the selected route', async () => {
diff --git a/apps/desktop/src/main/utility-model/oneShotCandidates.ts b/apps/desktop/src/main/utility-model/oneShotCandidates.ts
index ddbf86328f..3462bcc59b 100644
--- a/apps/desktop/src/main/utility-model/oneShotCandidates.ts
+++ b/apps/desktop/src/main/utility-model/oneShotCandidates.ts
@@ -1,6 +1,6 @@
import { randomUUID } from 'node:crypto';
-import { toSdkModelString, type AgentKind, type Maker } from '@cindy/maker-core';
+import { type AgentKind, type Maker } from '@cindy/maker-core';
import { appendProviderRequestPath } from '@cindy/model-providers';
import { createLogger } from '../logger.js';
@@ -624,7 +624,9 @@ async function requestBuiltinProviderText(
'anthropic-version': '2023-06-01',
'anthropic-beta': 'oauth-2025-04-20',
},
- model: toSdkModelString(input.model, findProviderModel(input.provider, input.agentKind, input.model)?.contextWindow),
+ // 直连 Anthropic API 用目录裸 id;不要复用 toSdkModelString——它会给 1M
+ // 目录模型追加 SDK 专用的 [1m] 后缀,/v1/messages 对该串返回 404(#2429)。
+ model: toAnthropicApiModelId(input.model),
prompt: text,
// Anthropic API 协议必填 max_tokens:缺省时以模型目录声明的 maxOutput
// (模型自身输出能力)兜底,没有目录条目才回退 81920——宿主不设政策上限。
@@ -905,6 +907,19 @@ function appendProviderPath(baseUrl: string, suffix: string): string {
return url.toString();
}
+/**
+ * catalog model id → 内置 Anthropic `/v1/messages` 直连请求体的 API model id。
+ *
+ * `[1m]` 是 Claude Code SDK 的 beta 通道后缀(由 toSdkModelString 按目录
+ * contextWindow 追加),不是 Anthropic API 模型名;直连 Messages API 时携带它
+ * 会被上游以 404 not_found 拒绝,进而让 Auto-review 持续 fail-closed(#2429)。
+ * 该转换仅用于内置 anthropic 直连分支——不对自定义供应商做全局字符串剥离,
+ * 部分兼容网关可能把 `[1m]` 作为自己的路由 id。
+ */
+export function toAnthropicApiModelId(model: string): string {
+ return model.endsWith('[1m]') ? model.slice(0, -'[1m]'.length) : model;
+}
+
/** Claude providers may configure either the host root or an existing `/v1` base. */
function joinAnthropicMessagesPath(baseUrl: string): string {
const url = new URL(baseUrl);