Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(proxy): 修复密文恢复留下消息空壳 (#2258)
DavidShenXD2b667c00b83f
diff --git a/packages/anthropic-compat-proxy/src/server.test.ts b/packages/anthropic-compat-proxy/src/server.test.tsindex 1fea4f5e62..a3d48a8620 100644--- a/packages/anthropic-compat-proxy/src/server.test.ts+++ b/packages/anthropic-compat-proxy/src/server.test.ts@@ -302,6 +302,112 @@ describe('anthropic-compat-proxy tool_use provider field compatibility', () => {});describe('anthropic-compat-proxy encrypted content retry', () => {+ 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 {+ input?: Array<{ type?: string; content?: unknown[]; encrypted_content?: unknown }>;+ };+ const encryptedParts = (body.input ?? [])+ .filter((item) => item.type === 'agent_message' && Array.isArray(item.content))+ .flatMap((item) => item.content ?? [])+ .filter((part): part is Record<string, unknown> => (+ typeof part === 'object'+ && part !== null+ && 'type' in part+ && part.type === 'encrypted_content'+ ));+ const foreignReasoning = (body.input ?? []).some((item) => (+ item.type === 'reasoning'+ && item.encrypted_content === 'gAAAAA-foreign-reasoning'+ ));+ if (encryptedParts.some((part) => typeof part.encrypted_content !== 'string')) {+ res.writeHead(400, { 'content-type': 'application/json' });+ res.end(JSON.stringify({+ error: {+ message: "Missing required parameter: 'input[2].content[1].encrypted_content'.",+ code: 'missing_required_parameter',+ },+ }));+ } else if (foreignReasoning) {+ res.writeHead(400, { 'content-type': 'application/json' });+ res.end(ENC_ERROR_BODY);+ } else {+ res.writeHead(200, { 'content-type': 'application/json' });+ res.end(JSON.stringify({ ok: true }));+ }+ });+ upstreamClose = upstream.close;+ const controller = createThreadStripController();+ proxy = await createAnthropicCompatProxy({+ upstream: upstream.url,+ transformRequest: [createActiveStripTransform({+ controller,+ enabled: () => true,+ strip: stripEncryptedContentFromBody,+ })],+ recoveryRules: [createEncryptedContentRecoveryRule({+ enabled: () => true,+ onRetry: (threadId, model) => controller.markActive(threadId, model),+ })],+ });+ const request = {+ model: 'gpt-5.6-sol',+ input: [+ { type: 'message', role: 'user', content: 'go' },+ {+ type: 'reasoning',+ id: 'foreign-reasoning',+ summary: [],+ encrypted_content: 'gAAAAA-foreign-reasoning',+ },+ {+ type: 'agent_message',+ author: '/root/progress_test',+ recipient: '/root',+ content: [+ { type: 'input_text', text: 'progress' },+ { type: 'encrypted_content', encrypted_content: 'gAAAAA-progress' },+ ],+ internal_chat_message_metadata_passthrough: { source: 'send_message' },+ },+ {+ type: 'agent_message',+ author: '/root/progress_test',+ recipient: '/root',+ content: [{ type: 'input_text', text: 'complete' }],+ },+ {+ type: 'agent_message',+ author: '/root/opaque',+ content: [{ type: 'encrypted_content', encrypted_content: 'gAAAAA-only' }],+ },+ ],+ };++ expect((await post(proxy.url, request)).status).toBe(200);+ expect(upstream.bodies).toHaveLength(2);+ expect(JSON.parse(upstream.bodies[1]).input).toEqual([+ { type: 'message', role: 'user', content: 'go' },+ {+ type: 'agent_message',+ author: '/root/progress_test',+ recipient: '/root',+ content: [{ type: 'input_text', text: 'progress' }],+ internal_chat_message_metadata_passthrough: { source: 'send_message' },+ },+ {+ type: 'agent_message',+ author: '/root/progress_test',+ recipient: '/root',+ content: [{ type: 'input_text', text: 'complete' }],+ },+ ]);++ expect((await post(proxy.url, request)).status).toBe(200);+ expect(upstream.bodies).toHaveLength(3);+ expect(upstream.bodies[2]).toBe(upstream.bodies[1]);+ });+it('retries invalid_encrypted_content once when enabled and marks the thread active', async () => {const upstream = await startFakeUpstream((idx, _body, res) => {if (idx === 0) {diff --git a/packages/anthropic-compat-proxy/src/transform.test.ts b/packages/anthropic-compat-proxy/src/transform.test.tsindex 0625c02998..2b24254588 100644--- a/packages/anthropic-compat-proxy/src/transform.test.ts+++ b/packages/anthropic-compat-proxy/src/transform.test.ts@@ -39,6 +39,54 @@ const ctx: RequestTransformCtx = {};describe('stripEncryptedContentFromBody', () => {+ it('drops ciphertext from intermediate agent messages while preserving readable progress and completion', () => {+ const body = buf({+ model: 'gpt-5.6-sol',+ input: [+ {+ type: 'agent_message',+ author: '/root/progress_test',+ recipient: '/root',+ content: [+ { type: 'input_text', text: 'progress' },+ { type: 'encrypted_content', encrypted_content: 'gAAAAA-progress' },+ ],+ internal_chat_message_metadata_passthrough: { source: 'send_message' },+ },+ {+ type: 'agent_message',+ author: '/root/progress_test',+ recipient: '/root',+ content: [{ type: 'input_text', text: 'complete' }],+ },+ {+ type: 'agent_message',+ author: '/root/opaque',+ content: [{ type: 'encrypted_content', encrypted_content: 'gAAAAA-only' }],+ },+ ],+ });++ const out = stripEncryptedContentFromBody(body);++ expect(out).not.toBeNull();+ expect(JSON.parse(out!.toString('utf8')).input).toEqual([+ {+ type: 'agent_message',+ author: '/root/progress_test',+ recipient: '/root',+ content: [{ type: 'input_text', text: 'progress' }],+ internal_chat_message_metadata_passthrough: { source: 'send_message' },+ },+ {+ type: 'agent_message',+ author: '/root/progress_test',+ recipient: '/root',+ content: [{ type: 'input_text', text: 'complete' }],+ },+ ]);+ });+it('removes encrypted_content nested in Responses-style input items', () => {const body = buf({model: 'gpt-5.5',diff --git a/packages/anthropic-compat-proxy/src/transform.ts b/packages/anthropic-compat-proxy/src/transform.tsindex 10618147d1..a8c21e45ff 100644--- a/packages/anthropic-compat-proxy/src/transform.ts+++ b/packages/anthropic-compat-proxy/src/transform.ts@@ -116,6 +116,47 @@ function isCompactionItem(node: Record<string, unknown>): boolean {return node.type === 'compaction' || node.type === 'context_compaction';}+/**+ * Drop Codex agent-to-agent ciphertext at its protocol-defined location.+ *+ * A spawned Codex agent can call `send_message` from an otherwise ordinary+ * session; the resulting `agent_message` carries readable text and an optional+ * `{ type: 'encrypted_content', encrypted_content: '...' }` part. Deleting only+ * the field leaves a schema-invalid encrypted-content shell, so remove the+ * whole part before the generic recursive key strip. If the message contained+ * only ciphertext, the message itself has no replayable content and is dropped.+ *+ * This intentionally inspects only top-level Responses `input[]`; nested+ * business objects with a similarly shaped `content` array are not protocol+ * history and must not be interpreted by shape.+ */+function dropEncryptedAgentMessageContentParts(body: unknown): number {+ if (!isPlainObject(body) || !Array.isArray(body.input)) return 0;++ let removed = 0;+ const input: unknown[] = [];+ for (const item of body.input) {+ if (!isPlainObject(item) || item.type !== 'agent_message' || !Array.isArray(item.content)) {+ input.push(item);+ continue;+ }++ const content = item.content.filter((part) => {+ if (!isPlainObject(part) || part.type !== 'encrypted_content') return true;+ removed += 1;+ return false;+ });+ if (content.length === item.content.length) {+ input.push(item);+ } else if (content.length > 0) {+ input.push({ ...item, content });+ }+ }++ if (removed > 0) body.input = input;+ return removed;+}+/*** 递归删除 body 里所有 `encrypted_content` 键, 返回删掉的个数。* 用于 invalid_encrypted_content 报错后的透明重试 (见 stripEncryptedContentFromBody)。@@ -224,12 +265,15 @@ export function stripEncryptedContentFromBody(rawBody: Buffer): Buffer | null {} catch {return null;}+ // agent_message 的密文是 content part;必须在通用递归删键之前整块移除,否则会留下+ // `{ type: 'encrypted_content' }` 并被 OpenAI 拒绝为 missing_required_parameter。+ const removedAgentMessageParts = dropEncryptedAgentMessageContentParts(parsed);const removedKeys = deepDeleteEncryptedContent(parsed);// 空壳清理独立计数: 请求体里可能只带着一个缺 blob 的 compaction 空壳 (没有任何// encrypted_content 键可删), 那一样是必须处理的坏 payload, 不能因 removedKeys=0 放行。// 这一步只扫顶层 input[] (单次线性扫描), 不是第二遍深度遍历。const removedShells = dropEncryptedShellInputItems(parsed, removedKeys > 0);- if (removedKeys === 0 && removedShells === 0) return null;+ if (removedAgentMessageParts === 0 && removedKeys === 0 && removedShells === 0) return null;try {return Buffer.from(JSON.stringify(parsed), 'utf8');} catch {