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): add vLLM responses compatibility retry for custom endpoints (#3741)

betterkite68b46040edb1
diff --git a/apps/desktop/src/main/maker-host/codex-proxy-host.ts b/apps/desktop/src/main/maker-host/codex-proxy-host.ts
index a14068e6d0..7d71a12dd4 100644
--- a/apps/desktop/src/main/maker-host/codex-proxy-host.ts
+++ b/apps/desktop/src/main/maker-host/codex-proxy-host.ts
@@ -22,6 +22,7 @@ import {
createInstructionsRegistry,
createXaiModelInputRecoveryRule,
createXaiModelInputSanitizeTransform,
+ createVllmResponsesCompatibilityRule,
sanitizeXaiModelInputBody,
sanitizeXaiModelInputFromBody,
stripEncryptedContentFromBody,
@@ -138,10 +139,12 @@ const imageGenerationIdRecoveryRule = createImageGenerationIdRecoveryRule({
const xaiModelInputRecoveryRule = createXaiModelInputRecoveryRule({
onRetry: (threadId, model) => xaiModelInputStripController.markActive(threadId, model),
});
+const vllmResponsesCompatibilityRule = createVllmResponsesCompatibilityRule();
const CODEX_BODY_RECOVERY_RULES = [
encryptedContentRecoveryRule,
imageGenerationIdRecoveryRule,
xaiModelInputRecoveryRule,
+ vllmResponsesCompatibilityRule,
] as const;
/**
diff --git a/packages/anthropic-compat-proxy/src/index.ts b/packages/anthropic-compat-proxy/src/index.ts
index 6089a64293..d50001e45c 100644
--- a/packages/anthropic-compat-proxy/src/index.ts
+++ b/packages/anthropic-compat-proxy/src/index.ts
@@ -68,6 +68,7 @@ export {
supportsXaiReasoningModel,
xaiBareModelId,
} from './xai-model-input.js';
+export { createVllmResponsesCompatibilityRule } from './vllm-responses-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.ts
index 47d01df018..630a5d0dfe 100644
--- a/packages/anthropic-compat-proxy/src/server.test.ts
+++ b/packages/anthropic-compat-proxy/src/server.test.ts
@@ -1,4 +1,4 @@
-import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http';
+import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http';
import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process';
import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
@@ -23,6 +23,7 @@ import {
createImageGenerationIdRecoveryRule,
createToolExchangeAdjacencyRecoveryRule,
createToolUseProviderSpecificFieldsRecoveryRule,
+ createVllmResponsesCompatibilityRule,
dedupeDuplicateToolUseIds,
repairToolExchangeAdjacency,
stripEncryptedContentFromBody,
@@ -452,6 +453,59 @@ describe('anthropic-compat-proxy tool_use provider field compatibility', () => {
});
describe('anthropic-compat-proxy encrypted content retry', () => {
+ it('normalizes and retries the vLLM Qwen Responses incompatibility once', async () => {
+ const upstream = await startFakeUpstream((idx, body, res) => {
+ if (idx === 0) {
+ expect(JSON.parse(body)).toMatchObject({
+ instructions: 'base instructions',
+ reasoning: { effort: 'high' },
+ input: [
+ { type: 'message', role: 'developer', content: 'permissions' },
+ { type: 'message', role: 'user', content: 'hello' },
+ ],
+ });
+ res.writeHead(400, { 'content-type': 'application/json' });
+ res.end(JSON.stringify({
+ error: {
+ message: 'Unexpected reasoning effort high. Supported types are xhigh (default), medium, and low.',
+ type: 'BadRequestError',
+ },
+ }));
+ return;
+ }
+ expect(JSON.parse(body)).toEqual({
+ model: 'qwen3.8-27b-fp8',
+ reasoning: { effort: 'xhigh' },
+ input: [
+ { type: 'message', role: 'system', content: 'base instructions' },
+ { type: 'message', role: 'system', content: 'permissions' },
+ { type: 'message', role: 'user', content: 'hello' },
+ ],
+ });
+ res.writeHead(200, { 'content-type': 'application/json' });
+ res.end(JSON.stringify({ ok: true }));
+ });
+ upstreamClose = upstream.close;
+ proxy = await createAnthropicCompatProxy({
+ upstream: upstream.url,
+ transformRequest: [],
+ recoveryRules: [createVllmResponsesCompatibilityRule()],
+ });
+
+ const result = await post(proxy.url, {
+ model: 'qwen3.8-27b-fp8',
+ instructions: 'base instructions',
+ reasoning: { effort: 'high' },
+ input: [
+ { type: 'message', role: 'developer', content: 'permissions' },
+ { type: 'message', role: 'user', content: 'hello' },
+ ],
+ });
+
+ expect(result.status).toBe(200);
+ expect(upstream.bodies).toHaveLength(2);
+ });
+
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/anthropic-compat-proxy/src/transform.ts b/packages/anthropic-compat-proxy/src/transform.ts
index bb37b4eb96..1619528a70 100644
--- a/packages/anthropic-compat-proxy/src/transform.ts
+++ b/packages/anthropic-compat-proxy/src/transform.ts
@@ -17,6 +17,8 @@ import { DEFAULT_THREAD_ID_HEADERS, selectedHeaderValue } from './headers.js';
import type { ThreadStripController } from './thread-strip-controller.js';
import type { RecoveryRule, RequestTransform, RequestTransformCtx } from './types.js';
+export { createVllmResponsesCompatibilityRule } from './vllm-responses-compatibility.js';
+
/**
* 单个 model 的请求改写 handler。
*
diff --git a/packages/anthropic-compat-proxy/src/vllm-responses-compatibility.test.ts b/packages/anthropic-compat-proxy/src/vllm-responses-compatibility.test.ts
new file mode 100644
index 0000000000..90cacca3e2
--- /dev/null
+++ b/packages/anthropic-compat-proxy/src/vllm-responses-compatibility.test.ts
@@ -0,0 +1,78 @@
+import { describe, expect, it } from 'vitest';
+
+import {
+ createVllmResponsesCompatibilityRule,
+ normalizeVllmResponsesRequest,
+} from './vllm-responses-compatibility.js';
+
+describe('vLLM Responses compatibility', () => {
+ it('maps the rejected effort and standardizes rejected message roles', () => {
+ const request = {
+ model: 'qwen3.8-27b-fp8',
+ instructions: 'base instructions',
+ reasoning: { effort: 'high', summary: 'auto' },
+ input: [
+ { type: 'message', role: 'developer', content: 'permissions' },
+ { type: 'message', role: 'user', content: 'hello' },
+ ],
+ stream: true,
+ };
+
+ expect(normalizeVllmResponsesRequest(request)).toEqual({
+ model: 'qwen3.8-27b-fp8',
+ reasoning: { effort: 'xhigh', summary: 'auto' },
+ input: [
+ { type: 'message', role: 'system', content: 'base instructions' },
+ { type: 'message', role: 'system', content: 'permissions' },
+ { type: 'message', role: 'user', content: 'hello' },
+ ],
+ stream: true,
+ });
+ });
+
+ it('keeps string input as an explicit user message when instructions move into the list', () => {
+ // Greptile P1: 合法 Responses 请求的字符串 input 在重试中被
+ // instructions 的 system 消息覆盖,用户提示词会被静默丢弃。
+ const request = {
+ model: 'qwen3.8-27b-fp8',
+ instructions: 'base instructions',
+ reasoning: { effort: 'high' },
+ input: '帮我总结这份文档',
+ };
+
+ expect(normalizeVllmResponsesRequest(request)).toEqual({
+ model: 'qwen3.8-27b-fp8',
+ reasoning: { effort: 'xhigh' },
+ input: [
+ { type: 'message', role: 'system', content: 'base instructions' },
+ { type: 'message', role: 'user', content: '帮我总结这份文档' },
+ ],
+ });
+ });
+
+ it('leaves accepted requests unchanged', () => {
+ const request = {
+ model: 'qwen3.8-27b-fp8',
+ reasoning: { effort: 'xhigh' },
+ input: [{ type: 'message', role: 'user', content: 'hello' }],
+ };
+ expect(normalizeVllmResponsesRequest(request)).toBeNull();
+ });
+
+ it('matches only the two reported upstream rejections', () => {
+ const rule = createVllmResponsesCompatibilityRule();
+ expect(rule.matches('{"error":{"message":"Unexpected reasoning effort high. Supported types are xhigh (default), medium, and low."}}')).toBe(true);
+ expect(rule.matches('{"error":{"message":"Unexpected message role."}}')).toBe(true);
+ expect(rule.matches('{"error":{"message":"System message must be at the beginning."}}')).toBe(false);
+ });
+
+ it('does not retry a role error when no incompatible role is present', () => {
+ const rule = createVllmResponsesCompatibilityRule();
+ const body = Buffer.from(JSON.stringify({
+ model: 'qwen3.8-27b-fp8',
+ reasoning: { effort: 'medium' },
+ input: [{ type: 'message', role: 'user', content: 'hello' }],
+ }));
+ expect(rule.strip(body)).toBeNull();
+ });
+});
diff --git a/packages/anthropic-compat-proxy/src/vllm-responses-compatibility.ts b/packages/anthropic-compat-proxy/src/vllm-responses-compatibility.ts
new file mode 100644
index 0000000000..c42b50a9e3
--- /dev/null
+++ b/packages/anthropic-compat-proxy/src/vllm-responses-compatibility.ts
@@ -0,0 +1,97 @@
+import type { RecoveryRule } from './types.js';
+
+interface ResponsesCompatibilityBody {
+ instructions?: unknown;
+ input?: unknown;
+ reasoning?: { effort?: unknown };
+ [key: string]: unknown;
+}
+
+interface ResponsesCompatibilityMessage {
+ type?: unknown;
+ role?: unknown;
+ content?: unknown;
+ [key: string]: unknown;
+}
+
+const REASONING_EFFORT_ERROR_RE =
+ /Unexpected reasoning effort high\. Supported types are xhigh \(default\), medium, and low\./i;
+const UNEXPECTED_MESSAGE_ROLE_RE = /Unexpected message role\./i;
+
+function isRecord(value: unknown): value is Record<string, unknown> {
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
+}
+
+function isMessage(value: unknown): value is ResponsesCompatibilityMessage {
+ return isRecord(value) && typeof value.role === 'string';
+}
+
+function normalizeRoles(input: readonly unknown[]): { input: unknown[]; changed: boolean } {
+ let changed = false;
+ const normalized = input.map((item) => {
+ if (!isMessage(item) || item.role !== 'developer') return item;
+ changed = true;
+ return { ...item, role: 'system' };
+ });
+ return { input: normalized, changed };
+}
+
+/**
+ * Convert only the Responses fields that Qwen's vLLM chat template rejected.
+ * This runs after an exact 400, so native Responses endpoints keep byte-for-byte
+ * requests; normal endpoints never see the rewritten shape.
+ */
+export function normalizeVllmResponsesRequest(body: unknown): object | null {
+ if (!isRecord(body)) return null;
+ const source = body as ResponsesCompatibilityBody;
+ let changed = false;
+ const next: ResponsesCompatibilityBody = { ...source };
+
+ if (source.reasoning?.effort === 'high') {
+ next.reasoning = { ...source.reasoning, effort: 'xhigh' };
+ changed = true;
+ }
+
+ let input: unknown = source.input;
+ if (Array.isArray(source.input)) {
+ const roles = normalizeRoles(source.input);
+ input = roles.input;
+ changed ||= roles.changed;
+ }
+
+ if (source.instructions !== undefined && source.instructions !== null && source.instructions !== '') {
+ const systemMessage = { type: 'message', role: 'system', content: source.instructions };
+ // Responses 允许 input 为纯字符串(等价单条 user 消息)。重试时必须把它转成
+ // 显式 user 消息追加在 system 之后,绝不能被 instructions 的 system 消息覆盖。
+ if (typeof input === 'string') {
+ input = [systemMessage, { type: 'message', role: 'user', content: input }];
+ } else if (Array.isArray(input)) {
+ input = [systemMessage, ...input];
+ } else {
+ input = [systemMessage];
+ }
+ delete next.instructions;
+ changed = true;
+ }
+ next.input = input;
+
+ return changed ? next : null;
+}
+
+export function createVllmResponsesCompatibilityRule(): RecoveryRule {
+ return {
+ id: 'vllm_responses_compat',
+ enabled: () => true,
+ matches: (errorText) =>
+ REASONING_EFFORT_ERROR_RE.test(errorText) || UNEXPECTED_MESSAGE_ROLE_RE.test(errorText),
+ strip: (body) => {
+ try {
+ const normalized = normalizeVllmResponsesRequest(JSON.parse(body.toString('utf8')));
+ return normalized ? Buffer.from(JSON.stringify(normalized), 'utf8') : null;
+ } catch {
+ return null;
+ }
+ },
+ applyOnUnmatchedRetry: false,
+ };
+}