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(codex): xAI 跨源回放时把 collab agent_message 归一成 message (#1521)

wang2dea16237f329
diff --git a/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts b/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts
index 472c2129ea..06b0838812 100644
--- a/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts
@@ -2553,6 +2553,95 @@ describe('codex proxy host', () => {
});
});
+ // OpenAI/Codex collab 历史里的 agent_message 不是 xAI ModelInput 变体;跨源 resume
+ // 到 grok 时原样转发 → 422 "data did not match any variant of untagged enum ModelInput"。
+ // (2026-08-03 实测: gpt-5.6-sol collab 会话切 xai/grok-4.5 必挂;新建 grok 会话正常。)
+ describe('xAI collab agent_message 跨源回放', () => {
+ async function runXaiInputTransforms(
+ suffix: string,
+ body: Record<string, unknown>,
+ ): Promise<Record<string, unknown>> {
+ const host = await freshCodexProxyHost();
+ const { setSessionProvider, clearSessionProvider } = await import('../session-provider-store.js');
+ mockState.createAnthropicCompatProxy.mockResolvedValueOnce({
+ url: 'http://127.0.0.1:43210',
+ dispose: vi.fn(async () => undefined),
+ });
+ await host.ensureCodexProxyReady();
+ const sessionId = `session-agent-message-${suffix}`;
+ const threadId = `thread-agent-message-${suffix}`;
+ host.registerComposed(sessionId, threadId, 'PRODUCT_PROMPT');
+ setSessionProvider(sessionId, 'xai');
+
+ const transforms = mockState.createAnthropicCompatProxy.mock.calls[0]?.[0]?.transformRequest ?? [];
+ const ctx = { method: 'POST', url: '/responses', headers: { 'thread-id': threadId } };
+ let current: unknown = body;
+ for (const transform of transforms) {
+ const next = transform(current, ctx);
+ if (next !== null && next !== undefined) current = next;
+ }
+ clearSessionProvider(sessionId);
+ return current as Record<string, unknown>;
+ }
+
+ it('把 agent_message 降级成 assistant message,丢掉 content 里的 encrypted_content', async () => {
+ const out = await runXaiInputTransforms('collab-to-message', {
+ model: 'xai/grok-4.5',
+ input: [
+ { type: 'message', role: 'user', content: [{ type: 'input_text', text: 'go' }] },
+ {
+ type: 'agent_message',
+ author: '/root/official_pr_rules',
+ recipient: '/root',
+ content: [
+ {
+ type: 'input_text',
+ text: 'Message Type: FINAL_ANSWER\nTask name: /root\nPayload:\n已完成只读审查',
+ },
+ { type: 'encrypted_content', encrypted_content: 'gAAAAA-openai-collab-blob' },
+ ],
+ internal_chat_message_metadata_passthrough: { turn_id: 't1' },
+ },
+ ],
+ });
+
+ const input = out.input as Array<Record<string, unknown>>;
+ expect(input).toHaveLength(2);
+ expect(input[0]).toMatchObject({ type: 'message', role: 'user' });
+ expect(input[1]).toEqual({
+ type: 'message',
+ role: 'assistant',
+ content: [
+ {
+ type: 'output_text',
+ text:
+ '[collab /root/official_pr_rules]\n'
+ + 'Message Type: FINAL_ANSWER\nTask name: /root\nPayload:\n已完成只读审查',
+ },
+ ],
+ });
+ // 不得残留 collab 专有键或 OpenAI 密文 part。
+ expect(JSON.stringify(input[1])).not.toContain('agent_message');
+ expect(JSON.stringify(input[1])).not.toContain('encrypted_content');
+ expect(JSON.stringify(input[1])).not.toContain('gAAAAA-openai-collab-blob');
+ });
+
+ it('未知 input type 直接丢掉,不原样透传给 xAI', async () => {
+ const out = await runXaiInputTransforms('drop-unknown', {
+ model: 'xai/grok-4.5',
+ input: [
+ { type: 'message', role: 'user', content: 'hi' },
+ { type: 'web_search_end', call_id: 'c1', query: 'x' },
+ { type: 'mcp_tool_call_end', call_id: 'c2' },
+ ],
+ });
+
+ const input = out.input as Array<Record<string, unknown>>;
+ expect(input).toHaveLength(1);
+ expect(input[0]).toMatchObject({ type: 'message', role: 'user' });
+ });
+ });
+
it('leaves custom_tool_call history untouched for non-xAI requests', async () => {
const host = await freshCodexProxyHost();
const { setSessionProvider, clearSessionProvider } = await import('../session-provider-store.js');
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 0ff38ea53f..b185afb200 100644
--- a/apps/desktop/src/main/maker-host/codex-proxy-host.ts
+++ b/apps/desktop/src/main/maker-host/codex-proxy-host.ts
@@ -1041,12 +1041,22 @@ function isXaiUnsupportedInputItem(item: unknown, opts: { supportsReasoning: boo
}
/**
- * Codex code-mode / app-server 历史里会回放 `custom_tool_call*`,且 tool output 常是
- * `[{type:"input_text",text}]` 数组。xAI Responses 的 untagged `ModelInput` 只认
- * message / function_call / function_call_output / reasoning 等标准变体;原样转发会 422:
+ * Codex code-mode / app-server 历史里会回放 `custom_tool_call*` / `agent_message` 等
+ * 非 Responses 标准 item,且 tool output 常是 `[{type:"input_text",text}]` 数组。
+ * xAI Responses 的 untagged `ModelInput` 只认 message / function_call /
+ * function_call_output / reasoning 等标准变体;原样转发会 422:
* "data did not match any variant of untagged enum ModelInput"。
- * 仅在 xAI 路由里把这些历史 item 归一成 xAI 可反序列化的 function_call 形态。
+ * 仅在 xAI 路由里把这些历史 item 归一成 xAI 可反序列化的形态;未知 type 直接丢掉,
+ * 避免跨源 resume(如 OpenAI collab → Grok)整轮卡死。
*/
+/** xAI ModelInput 可反序列化的 input item type(归一化后的白名单)。 */
+const XAI_MODEL_INPUT_TYPES = new Set([
+ 'message',
+ 'function_call',
+ 'function_call_output',
+ 'reasoning',
+]);
+
function textFromResponsesContent(value: unknown): string {
if (typeof value === 'string') return value;
if (typeof value === 'number' || typeof value === 'boolean') return String(value);
@@ -1132,6 +1142,28 @@ function normalizeXaiInputItem(item: unknown): { item: unknown; changed: boolean
};
}
+ // Codex multi-agent collab 历史项。OpenAI 会话里的 agent_message 带着 author/
+ // recipient 和 content 里的 encrypted_content part;xAI ModelInput 不认这个 type,
+ // 跨源 resume 到 grok 时整轮 422。降级成 assistant message,只保留可读文本
+ // (collab 密文 part 解不开,丢掉)。
+ if (type === 'agent_message') {
+ const bodyText = textFromResponsesContent(base.content).trim();
+ const author = typeof base.author === 'string' && base.author.length > 0
+ ? base.author
+ : 'agent';
+ const text = bodyText.length > 0
+ ? `[collab ${author}]\n${bodyText}`
+ : `[collab message from ${author}; encrypted payload omitted]`;
+ return {
+ item: {
+ type: 'message',
+ role: 'assistant',
+ content: [{ type: 'output_text', text }],
+ },
+ changed: true,
+ };
+ }
+
if (type === 'function_call') {
const normalizedArguments = argumentsFromCustomToolInput(base.arguments);
const next: Record<string, unknown> = {
@@ -1244,14 +1276,16 @@ function normalizeXaiInputItem(item: unknown): { item: unknown; changed: boolean
return changed ? { item: next, changed: true } : { item: base, changed: false };
}
- return { item: base, changed: typedFromEasy };
+ // 未知 type 不透传:xAI untagged ModelInput 任一 item 对不上就整包 422。
+ // 调用方按 changed=true + 非白名单 type 丢弃(见 normalizeXaiInputItems)。
+ return { item: base, changed: typedFromEasy || (typeof type === 'string' && !XAI_MODEL_INPUT_TYPES.has(type)) };
}
function normalizeXaiInputItems(body: Record<string, unknown>): Record<string, unknown> | null {
if (!Array.isArray(body.input)) return null;
// xAI supports encrypted reasoning replay, but not Codex/OpenAI image replay items in `input[]`.
- // Codex custom_tool_call* must also be rewritten before xAI's ModelInput deserialize.
+ // Codex custom_tool_call* / agent_message 等必须在 ModelInput deserialize 前洗掉或改写。
const supportsReasoning = supportsXaiReasoning(xaiRealModelId(body.model));
let changed = false;
const input: unknown[] = [];
@@ -1262,6 +1296,15 @@ function normalizeXaiInputItems(body: Record<string, unknown>): Record<string, u
}
const normalized = normalizeXaiInputItem(raw);
if (normalized.changed) changed = true;
+ // 归一化后仍不在白名单(或未知 type 原样返回)→ 丢掉,别把 422 留给上游。
+ if (
+ !isPlainObject(normalized.item)
+ || typeof normalized.item.type !== 'string'
+ || !XAI_MODEL_INPUT_TYPES.has(normalized.item.type)
+ ) {
+ changed = true;
+ continue;
+ }
input.push(normalized.item);
}
if (!changed) return null;