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): recover cross-provider Codex streams (#2321)

Chris Zhang1f3f04a1c70a
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 8be9ebca75..e9068b29ee 100644
--- a/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts
@@ -375,24 +375,74 @@ describe('createCrossProviderCompactionCompatTransform', () => {
const compactionItem = { type: 'compaction', encrypted_content: 'ENC' };
const contextCompactionItem = { type: 'context_compaction', id: 'cc_1', encrypted_content: 'ENC2' };
const userMessage = { type: 'message', role: 'user', content: [{ type: 'input_text', text: 'hi' }] };
+ const agentMessage = {
+ type: 'agent_message',
+ author: 'researcher',
+ recipient: 'parent',
+ content: [
+ { type: 'input_text', text: 'readable agent result' },
+ { type: 'encrypted_content', encrypted_content: 'AGENT_ENC' },
+ ],
+ };
+ const reasoningItem = {
+ type: 'reasoning',
+ content: null,
+ summary: [],
+ encrypted_content: 'REASONING_ENC',
+ };
- it('把加密压缩块替换为明文占位 message(非 ChatGPT 上游)', async () => {
+ it('同时降级 compaction 与 agent 消息密文,保留可读正文和 reasoning(非 ChatGPT 上游)', async () => {
const { createCrossProviderCompactionCompatTransform } = await import('../codex-proxy-host.js');
const transform = createCrossProviderCompactionCompatTransform();
const out = transform(
- { model: 'gpt-5.5', input: [compactionItem, contextCompactionItem, userMessage] },
+ {
+ model: 'gpt-5.5',
+ input: [compactionItem, contextCompactionItem, agentMessage, reasoningItem, userMessage],
+ },
{ ...CTX_BASE, upstreamBase: 'https://gateway.example.com/v1' },
) as { input: Array<Record<string, unknown>> };
expect(out).not.toBeNull();
- expect(out.input).toHaveLength(3);
+ expect(out.input).toHaveLength(5);
expect(out.input[0].type).toBe('message');
expect(out.input[1].type).toBe('message');
- expect(JSON.stringify(out.input)).not.toContain('ENC');
expect(JSON.stringify(out.input[0])).toContain('compacted into an encrypted snapshot');
- // 压缩点之后的原有消息原样保留
- expect(out.input[2]).toEqual(userMessage);
+ expect(out.input[2]).toEqual({
+ type: 'agent_message',
+ author: 'researcher',
+ recipient: 'parent',
+ content: [{ type: 'input_text', text: 'readable agent result' }],
+ });
+ expect(out.input[3]).toEqual(reasoningItem);
+ expect(out.input[4]).toEqual(userMessage);
+ expect(JSON.stringify(out.input)).not.toContain('AGENT_ENC');
+ expect(JSON.stringify(out.input)).toContain('REASONING_ENC');
+ // transform 不得原地污染 Codex 持有的历史对象。
+ expect(agentMessage.content).toHaveLength(2);
+ });
+
+ it('agent_message 只有密文时整条丢弃', async () => {
+ const { createCrossProviderCompactionCompatTransform } = await import('../codex-proxy-host.js');
+ const transform = createCrossProviderCompactionCompatTransform();
+
+ const out = transform(
+ {
+ model: 'gpt-5.5',
+ input: [
+ {
+ type: 'agent_message',
+ author: 'researcher',
+ recipient: 'parent',
+ content: [{ type: 'encrypted_content', encrypted_content: 'ONLY_ENC' }],
+ },
+ userMessage,
+ ],
+ },
+ { ...CTX_BASE, upstreamBase: 'https://gateway.example.com/v1' },
+ ) as { input: Array<Record<string, unknown>> };
+
+ expect(out.input).toEqual([userMessage]);
});
it('ChatGPT 上游原样透传(远端压缩语义不受影响)', async () => {
@@ -400,7 +450,7 @@ describe('createCrossProviderCompactionCompatTransform', () => {
const transform = createCrossProviderCompactionCompatTransform();
expect(transform(
- { model: 'gpt-5.5', input: [compactionItem, userMessage] },
+ { model: 'gpt-5.5', input: [compactionItem, agentMessage, reasoningItem, userMessage] },
{ ...CTX_BASE, upstreamBase: 'https://chatgpt.com/backend-api/codex' },
)).toBeNull();
});
@@ -616,6 +666,104 @@ describe('chatBridgeCapabilitiesForRoute', () => {
setCustomProviders([]);
});
+ it('strips agent message ciphertext before a cross-provider Chat bridge request', async () => {
+ const host = await freshCodexProxyHost();
+ const { buildUserProvider } = await import('@cindy/model-providers');
+ const { setCustomProviders } = await import('../active-catalog.js');
+ const { setCustomProviderKeyReader } = await import('../provider-route.js');
+ const { setSessionProvider, clearSessionProvider } = await import('../session-provider-store.js');
+ setCustomProviders([
+ buildUserProvider({
+ id: 'history-chat-provider',
+ name: 'History Chat Provider',
+ runtimes: {
+ codex: {
+ baseUrl: 'https://chat-provider.example/v1',
+ wireProtocol: 'openai-chat',
+ models: [{ id: 'history-model', name: 'History Model' }],
+ },
+ },
+ }),
+ ]);
+ setCustomProviderKeyReader(() => 'history-provider-key');
+ host.registerComposed('session-history-chat', 'thread-history-chat', 'PRODUCT_PROMPT');
+ setSessionProvider('session-history-chat', 'history-chat-provider');
+ host.setCodexProxyAuthInjection('env-key');
+
+ const parsedBody = {
+ model: 'history-model',
+ input: [
+ {
+ type: 'agent_message',
+ author: 'researcher',
+ recipient: 'parent',
+ content: [
+ { type: 'input_text', text: 'readable result' },
+ { type: 'encrypted_content', encrypted_content: 'AGENT_ENC' },
+ ],
+ },
+ {
+ type: 'agent_message',
+ author: 'researcher',
+ recipient: 'parent',
+ content: [{ type: 'encrypted_content', encrypted_content: 'ONLY_ENC' }],
+ },
+ {
+ type: 'reasoning',
+ content: null,
+ summary: [],
+ encrypted_content: 'REASONING_ENC',
+ },
+ ],
+ };
+ const ctx = {
+ reqId: 1,
+ method: 'POST',
+ url: '/responses',
+ headers: { 'thread-id': 'thread-history-chat' },
+ };
+
+ try {
+ const decision = await Promise.resolve(host.createModelRoutingTransform()(
+ parsedBody,
+ ctx,
+ ));
+ expect(decision).toEqual(expect.objectContaining({ localHandler: expect.any(Function) }));
+ if (!decision?.localHandler) throw new Error('expected Chat bridge local handler');
+
+ const res = {} as never;
+ await decision.localHandler({
+ rawBody: Buffer.from(JSON.stringify(parsedBody)),
+ parsedBody,
+ ctx,
+ res,
+ });
+ const bridge = mockState.createResponsesChatHandler.mock.results.at(-1)?.value as
+ | { handle: ReturnType<typeof vi.fn> }
+ | undefined;
+ expect(bridge?.handle).toHaveBeenCalledWith({
+ parsedBody: {
+ ...parsedBody,
+ instructions: 'PRODUCT_PROMPT',
+ input: [
+ {
+ type: 'agent_message',
+ author: 'researcher',
+ recipient: 'parent',
+ content: [{ type: 'input_text', text: 'readable result' }],
+ },
+ parsedBody.input[2],
+ ],
+ },
+ res,
+ });
+ } finally {
+ clearSessionProvider('session-history-chat');
+ setCustomProviderKeyReader(() => null);
+ setCustomProviders([]);
+ }
+ });
+
it('routes a custom Codex Anthropic Messages runtime to the local bridge with provider-owned auth', async () => {
const host = await freshCodexProxyHost();
const { buildUserProvider } = await import('@cindy/model-providers');
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 aa1ffecca6..c63c7415b4 100644
--- a/apps/desktop/src/main/maker-host/codex-proxy-host.ts
+++ b/apps/desktop/src/main/maker-host/codex-proxy-host.ts
@@ -683,7 +683,7 @@ interface PrepareLocalBridgeBodyOptions {
/**
* localHandler runs before the shared request transform chain. Keep the preprocessing
* needed by every Responses bridge in one place so adding another wire adapter cannot
- * accidentally lose product instructions or cross-provider compaction recovery.
+ * accidentally lose product instructions or cross-provider encrypted-history recovery.
*/
function prepareLocalBridgeBody(opts: PrepareLocalBridgeBodyOptions): unknown {
let body = opts.parsedBody;
@@ -727,14 +727,16 @@ function prepareLocalBridgeBody(opts: PrepareLocalBridgeBodyOptions): unknown {
: [existingText, opts.instructions].filter(Boolean).join('\n\n'),
};
}
- const compactionSafe = replaceEncryptedCompactionItems(body);
- if (compactionSafe) {
- log.info('replaced encrypted compaction history for local bridge upstream', {
+ const historySafe = isChatGptUpstreamBase(opts.upstreamBase)
+ ? null
+ : rewriteCrossProviderHistoryItems(body);
+ if (historySafe) {
+ log.info('rewrote incompatible Codex history for local bridge upstream', {
bridge: opts.bridge,
providerId: opts.providerId,
upstreamBase: opts.upstreamBase,
});
- body = compactionSafe;
+ body = historySafe;
}
return body;
}
@@ -1812,18 +1814,21 @@ function isChatGptUpstreamBase(upstreamBase: string | undefined): boolean {
}
/**
- * 把 body.input 里**确实携带加密内容**的压缩项替换为明文占位 user message。
- * 返回 null = 无压缩项,零改写。透明转发路径(transform 链)与 Chat bridge
- * localHandler 路径共用——两条路都可能重放跨来源恢复的加密压缩历史。
+ * 把 body.input 里无法跨供应商重放的 Codex 历史降级成目标上游可接受的形态。
+ * 返回 null = 无需改写。透明转发路径(transform 链)与 localHandler 路径共用。
*
- * 只替换 encrypted_content 非空的项(codex wire 上 Compaction.encrypted_content
- * 必填、ContextCompaction 可选):未来若出现可读/非加密的 compaction 变体,
- * 不在「上游解不开」的问题域内,原样透传交给目标上游/翻译层自行处理。
+ * - 加密 compaction 仍替换成明文上下文缺失提示。
+ * - 多 Agent 历史会在 agent_message.content 里夹带仅原供应商可解的 encrypted_content;
+ * 非 ChatGPT 上游会直接拒绝整次请求。只删除这些嵌套密文,保留可读正文与路由元数据;
+ * 若消息只剩密文则整条丢弃。
+ * - reasoning.encrypted_content 不属于本故障;继续交给后续供应商兼容层判断,
+ * 不在这里扩大删除面。
*/
-function replaceEncryptedCompactionItems(body: unknown): Record<string, unknown> | null {
+function rewriteCrossProviderHistoryItems(body: unknown): Record<string, unknown> | null {
if (!isPlainObject(body) || !Array.isArray(body.input)) return null;
let changed = false;
- const input = body.input.map((item) => {
+ const input: unknown[] = [];
+ for (const item of body.input) {
if (
isPlainObject(item) &&
(item.type === 'compaction' || item.type === 'context_compaction') &&
@@ -1831,14 +1836,25 @@ function replaceEncryptedCompactionItems(body: unknown): Record<string, unknown>
item.encrypted_content.length > 0
) {
changed = true;
- return {
+ input.push({
type: 'message',
role: 'user',
content: [{ type: 'input_text', text: COMPACTION_UNAVAILABLE_NOTE }],
- };
+ });
+ continue;
}
- return item;
- });
+ if (isPlainObject(item) && item.type === 'agent_message' && Array.isArray(item.content)) {
+ const content = item.content.filter(
+ (part) => !(isPlainObject(part) && part.type === 'encrypted_content'),
+ );
+ if (content.length !== item.content.length) {
+ changed = true;
+ if (content.length > 0) input.push({ ...item, content });
+ continue;
+ }
+ }
+ input.push(item);
+ }
return changed ? { ...body, input } : null;
}
@@ -1847,9 +1863,9 @@ export function createCrossProviderCompactionCompatTransform(): RequestTransform
// upstreamBase 未注入(理论不发生)按非 ChatGPT 保守处理?否——保守方向是不改写:
// 改写会丢加密块,误伤真 ChatGPT 请求的代价(远端压缩语义被破坏)高于维持现状。
if (!ctx.upstreamBase || isChatGptUpstreamBase(ctx.upstreamBase)) return null;
- const replaced = replaceEncryptedCompactionItems(body);
+ const replaced = rewriteCrossProviderHistoryItems(body);
if (!replaced) return null;
- log.info('replaced encrypted compaction history for non-ChatGPT upstream', {
+ log.info('rewrote incompatible Codex history for non-ChatGPT upstream', {
reqId: ctx.reqId,
upstreamBase: ctx.upstreamBase,
threadId: selectedThreadIdFromHeaders(ctx.headers),
@@ -2272,8 +2288,8 @@ function createTransformRequestChain(
// compatibility transforms inspect the request.
createProviderAwareGuardianReviewerTransform(frozenAuthInjection),
createGatewayNativeWebSearchTransform(),
- // 必须先于 xAI/MiniMax 兼容改写:加密压缩块换成明文占位 message 后,后续
- // 针对具体供应商的 input 归一化才能按标准 message 处理它。
+ // 必须先于 xAI/MiniMax 兼容改写:先把供应商绑定的历史项降级成标准 message,
+ // 后续针对具体供应商的 input 归一化才能稳定处理。
createCrossProviderCompactionCompatTransform(),
createStrictGatewayHistoryCompatTransform(),
createXaiResponsesCompatTransform(),