Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(cc-proxy): 自定义 Anthropic 兼容网关拒绝推理档位时省略 output_config.effort 重试一次(#5032)
ficowange83f6db5dc07
diff --git a/apps/desktop/src/main/maker-host/anthropic-compat-proxy-host.ts b/apps/desktop/src/main/maker-host/anthropic-compat-proxy-host.tsindex 8d558afbf9..47fcd0a53a 100644--- a/apps/desktop/src/main/maker-host/anthropic-compat-proxy-host.ts+++ b/apps/desktop/src/main/maker-host/anthropic-compat-proxy-host.ts@@ -29,6 +29,7 @@ import {createToolUseProviderSpecificFieldsRecoveryRule,createXaiModelInputRecoveryRule,createXaiModelInputSanitizeTransform,+ createAnthropicEffortCompatibilityRule,dedupeDuplicateToolUseIds,repairToolExchangeAdjacency,sanitizeXaiModelInputFromBody,@@ -1141,6 +1142,10 @@ export async function ensureAnthropicCompatProxyReady(): Promise<void> {createXaiModelInputRecoveryRule({onRetry: (threadId, model) => xaiModelInputStripController.markActive(threadId, model),}),+ // 自定义 Anthropic 兼容网关不认 Claude Code 默认档位的精确 400(#5032)→ 省略+ // output_config.effort 重发一次。只在该文本命中时生效,官方 Claude / 订阅直连+ // 不会返回它;能力未声明的模型本就不该收到客户端偏好(#4860 出站不变量)。+ createAnthropicEffortCompatibilityRule(),],logger: log,// 请求体 dump 默认关(dev trace 级别 + agent 高并发会刷爆 main event loopdiff --git a/packages/anthropic-compat-proxy/src/anthropic-effort-compatibility.test.ts b/packages/anthropic-compat-proxy/src/anthropic-effort-compatibility.test.tsnew file mode 100644index 0000000000..48e87eb97e--- /dev/null+++ b/packages/anthropic-compat-proxy/src/anthropic-effort-compatibility.test.ts@@ -0,0 +1,79 @@+import { describe, expect, it } from 'vitest';++import {+ createAnthropicEffortCompatibilityRule,+ omitUnsupportedAnthropicEffort,+} from './anthropic-effort-compatibility.js';++const REJECTION = '{"type":"error","error":{"type":"invalid_request_error","message":"Unexpected reasoning effort high. Supported types are xhigh (default), medium, and low."}}';++describe('Anthropic Messages effort compatibility (#5032)', () => {+ it('omits output_config.effort and keeps the rest of the request', () => {+ const request = {+ model: 'Qwen3.8-Flash-Next',+ max_tokens: 4096,+ output_config: { effort: 'high', format: { type: 'text' } },+ messages: [{ role: 'user', content: 'hello' }],+ stream: true,+ };+ expect(omitUnsupportedAnthropicEffort(request)).toEqual({+ model: 'Qwen3.8-Flash-Next',+ max_tokens: 4096,+ output_config: { format: { type: 'text' } },+ messages: [{ role: 'user', content: 'hello' }],+ stream: true,+ });+ });++ it('drops an output_config that only carried the rejected effort', () => {+ const next = omitUnsupportedAnthropicEffort({+ model: 'Qwen3.8-Flash-Next',+ output_config: { effort: 'high' },+ messages: [{ role: 'user', content: 'hello' }],+ });+ expect(next).toEqual({+ model: 'Qwen3.8-Flash-Next',+ messages: [{ role: 'user', content: 'hello' }],+ });+ expect(next).not.toHaveProperty('output_config');+ });++ it('leaves requests without an effort preference and non-Messages bodies alone', () => {+ expect(omitUnsupportedAnthropicEffort({+ model: 'Qwen3.8-Flash-Next',+ messages: [{ role: 'user', content: 'hello' }],+ })).toBeNull();+ // Responses 形态由 vLLM 规则处理,这里不能碰。+ expect(omitUnsupportedAnthropicEffort({+ model: 'qwen3.8-27b-fp8',+ reasoning: { effort: 'high' },+ input: [{ type: 'message', role: 'user', content: 'hello' }],+ })).toBeNull();+ expect(omitUnsupportedAnthropicEffort('not-json-object')).toBeNull();+ });++ it('matches only the unsupported-effort rejection and never joins unrelated retries', () => {+ const rule = createAnthropicEffortCompatibilityRule();+ expect(rule.matches(REJECTION)).toBe(true);+ expect(rule.matches('{"error":{"message":"Unexpected reasoning effort medium. Supported types are xhigh, and low."}}')).toBe(true);+ expect(rule.matches('{"error":{"message":"Unexpected message role."}}')).toBe(false);+ expect(rule.matches('{"error":{"message":"messages.0.output_config: Extra inputs are not permitted"}}')).toBe(false);+ expect(rule.applyOnUnmatchedRetry).toBe(false);+ });++ it('strips the effort from a matched body and declines bodies with nothing to change', () => {+ const rule = createAnthropicEffortCompatibilityRule();+ const stripped = rule.strip(Buffer.from(JSON.stringify({+ model: 'Qwen3.8-Flash-Next',+ output_config: { effort: 'high' },+ messages: [{ role: 'user', content: 'hello' }],+ })));+ expect(stripped).not.toBeNull();+ expect(JSON.parse(stripped!.toString('utf8'))).not.toHaveProperty('output_config');+ expect(rule.strip(Buffer.from(JSON.stringify({+ model: 'Qwen3.8-Flash-Next',+ messages: [{ role: 'user', content: 'hello' }],+ })))).toBeNull();+ expect(rule.strip(Buffer.from('{not json'))).toBeNull();+ });+});diff --git a/packages/anthropic-compat-proxy/src/anthropic-effort-compatibility.ts b/packages/anthropic-compat-proxy/src/anthropic-effort-compatibility.tsnew file mode 100644index 0000000000..0493a1a292--- /dev/null+++ b/packages/anthropic-compat-proxy/src/anthropic-effort-compatibility.ts@@ -0,0 +1,5 @@+/** Compatibility implementation is owned by the shared package; preserve this import contract. */+export {+ omitUnsupportedAnthropicEffort,+ createAnthropicEffortCompatibilityRule+} from '@cindy/model-compat';diff --git a/packages/anthropic-compat-proxy/src/index.ts b/packages/anthropic-compat-proxy/src/index.tsindex c56197eaef..7996713192 100644--- a/packages/anthropic-compat-proxy/src/index.ts+++ b/packages/anthropic-compat-proxy/src/index.ts@@ -77,6 +77,7 @@ export {xaiBareModelId,} from './xai-model-input.js';export { createVllmResponsesCompatibilityRule } from './vllm-responses-compatibility.js';+export { createAnthropicEffortCompatibilityRule } from './anthropic-effort-compatibility.js';export { createVisionBridgeTransform } from './vision-bridge-transform.js';export { createThreadStripController } from './thread-strip-controller.js';export type { ThreadStripController } from './thread-strip-controller.js';diff --git a/packages/anthropic-compat-proxy/src/server.test.ts b/packages/anthropic-compat-proxy/src/server.test.tsindex 854f291cc4..905a0c4384 100644--- a/packages/anthropic-compat-proxy/src/server.test.ts+++ b/packages/anthropic-compat-proxy/src/server.test.ts@@ -29,6 +29,7 @@ import {stripEncryptedContentFromBody,} from './transform.js';import { createXaiModelInputRecoveryRule } from './xai-model-input.js';+import { createAnthropicEffortCompatibilityRule } from './anthropic-effort-compatibility.js';import { listenOnAvailableLoopbackPort } from './test-loopback-server.js';import { createThreadStripController } from './thread-strip-controller.js';import type { ProxyHandle, RequestTransform } from './types.js';@@ -1028,6 +1029,80 @@ describe('anthropic-compat-proxy encrypted content retry', () => {expect(upstream.bodies).toHaveLength(2);});+ it('omits output_config.effort and retries once when a custom Anthropic gateway rejects the effort (#5032)', async () => {+ const upstream = await startFakeUpstream((idx, rawBody, res) => {+ const body = JSON.parse(rawBody) as { output_config?: { effort?: string } };+ if (idx === 0) {+ expect(body.output_config?.effort).toBe('high');+ res.writeHead(400, { 'content-type': 'application/json' });+ res.end(JSON.stringify({+ type: 'error',+ error: { type: 'invalid_request_error', message: 'Unexpected reasoning effort high. Supported types are xhigh (default), medium, and low.' },+ }));+ return;+ }+ expect(body).not.toHaveProperty('output_config');+ res.writeHead(200, { 'content-type': 'application/json' });+ res.end(JSON.stringify({ ok: true }));+ });+ upstreamClose = upstream.close;+ proxy = await createAnthropicCompatProxy({+ upstream: upstream.url,+ transformRequest: [],+ recoveryRules: [createAnthropicEffortCompatibilityRule()],+ });++ const result = await post(proxy.url, {+ model: 'Qwen3.8-Flash-Next',+ max_tokens: 1024,+ output_config: { effort: 'high' },+ messages: [{ role: 'user', content: 'hello' }],+ });++ expect(result.status).toBe(200);+ expect(upstream.bodies).toHaveLength(2);+ });++ it('surfaces the gateway effort rejection as-is when the rule is not registered (#5032 baseline)', async () => {+ const upstream = await startFakeUpstream((_idx, _rawBody, res) => {+ res.writeHead(400, { 'content-type': 'application/json' });+ res.end(JSON.stringify({ type: 'error', error: { type: 'invalid_request_error', message: 'Unexpected reasoning effort high. Supported types are xhigh (default), medium, and low.' } }));+ });+ upstreamClose = upstream.close;+ proxy = await createAnthropicCompatProxy({ upstream: upstream.url, transformRequest: [], recoveryRules: [] });++ const result = await post(proxy.url, {+ model: 'Qwen3.8-Flash-Next',+ output_config: { effort: 'high' },+ messages: [{ role: 'user', content: 'hello' }],+ });++ expect(result.status).toBe(400);+ expect(upstream.bodies).toHaveLength(1);+ });++ it('does not retry an unrelated 400 through the effort rule (#5032)', async () => {+ const upstream = await startFakeUpstream((_idx, _rawBody, res) => {+ res.writeHead(400, { 'content-type': 'application/json' });+ res.end(JSON.stringify({ type: 'error', error: { type: 'invalid_request_error', message: 'messages: at least one message is required' } }));+ });+ upstreamClose = upstream.close;+ proxy = await createAnthropicCompatProxy({+ upstream: upstream.url,+ transformRequest: [],+ recoveryRules: [createAnthropicEffortCompatibilityRule()],+ });++ const result = await post(proxy.url, {+ model: 'Qwen3.8-Flash-Next',+ output_config: { effort: 'high' },+ messages: [],+ });++ expect(result.status).toBe(400);+ expect(upstream.bodies).toHaveLength(1);+ });+it('preserves readable agent progress when foreign reasoning ciphertext triggers recovery', async () => {const upstream = await startFakeUpstream((_idx, rawBody, res) => {const body = JSON.parse(rawBody) as {diff --git a/packages/model-compat/src/cindy/proxy/anthropic-effort-compatibility.ts b/packages/model-compat/src/cindy/proxy/anthropic-effort-compatibility.tsnew file mode 100644index 0000000000..03237dfaca--- /dev/null+++ b/packages/model-compat/src/cindy/proxy/anthropic-effort-compatibility.ts@@ -0,0 +1,51 @@+import type { RecoveryRule } from './types.js';++/**+ * 自定义 Anthropic 兼容网关拒绝当前推理档位时的 400 文本(#5032 实测,与 Codex 侧+ * vLLM Responses 规则命中的是同一族网关;那边改写的是 Responses 的 `reasoning.effort`,+ * 这里只处理 Anthropic Messages 的 `output_config.effort`)。只匹配这一精确形态,+ * 官方 Claude / 订阅直连不会返回该文本,字节保持不变。+ */+const UNSUPPORTED_REASONING_EFFORT_RE =+ /Unexpected reasoning effort [\w-]+\. Supported types are /i;++function isRecord(value: unknown): value is Record<string, unknown> {+ return typeof value === 'object' && value !== null && !Array.isArray(value);+}++/**+ * 从 Anthropic Messages 请求体里省略 `output_config.effort`,其它字段原样保留。+ *+ * 能力未声明的自定义模型不该收到客户端偏好(#4860 出站不变量):Claude Code 子进程在+ * Cindy 不下发档位时会沿用自身默认 `high`,兼容网关只认 xhigh/medium/low 之类集合就会+ * 400。重试时把偏好省掉,让网关按自己的默认档执行,而不是猜一个它可能也不认的值。+ * 非 Messages 形态(无 `messages` 数组,例如 Responses)不处理,交给对应规则。+ */+export function omitUnsupportedAnthropicEffort(body: unknown): object | null {+ if (!isRecord(body) || !Array.isArray(body.messages)) return null;+ const outputConfig = body.output_config;+ if (!isRecord(outputConfig) || typeof outputConfig.effort !== 'string') return null;+ const { effort: _rejected, ...rest } = outputConfig;+ const next: Record<string, unknown> = { ...body };+ if (Object.keys(rest).length > 0) next.output_config = rest;+ else delete next.output_config;+ return next;+}++export function createAnthropicEffortCompatibilityRule(): RecoveryRule {+ return {+ id: 'anthropic_effort_compat',+ enabled: () => true,+ matches: (errorText) => UNSUPPORTED_REASONING_EFFORT_RE.test(errorText),+ strip: (body) => {+ try {+ const next = omitUnsupportedAnthropicEffort(JSON.parse(body.toString('utf8')));+ return next ? Buffer.from(JSON.stringify(next), 'utf8') : null;+ } catch {+ return null;+ }+ },+ // 语义绑在特定网关的拒绝文本上,别的 400 重试时不能顺手删掉用户档位。+ applyOnUnmatchedRetry: false,+ };+}diff --git a/packages/model-compat/src/index.ts b/packages/model-compat/src/index.tsindex 229e33248a..93dd8036c8 100644--- a/packages/model-compat/src/index.ts+++ b/packages/model-compat/src/index.ts@@ -15,6 +15,7 @@ export { createCodexResponsesCompatibilityAdapter } from './codex';export * from './cindy/proxy/transform';export * from './cindy/proxy/xai-model-input';export * from './cindy/proxy/vllm-responses-compatibility';+export * from './cindy/proxy/anthropic-effort-compatibility';export * from './cindy/proxy/headers';export * from './cindy/proxy/thread-strip-controller';export type * from './cindy/proxy/types';