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(responses-chat-bridge): drop Codex tool_search replay items instead of failing (#749)

xxxccb2c81aad4017
diff --git a/packages/responses-chat-bridge/src/__tests__/translate-request.test.ts b/packages/responses-chat-bridge/src/__tests__/translate-request.test.ts
index a5cf6017c6..1e6a68a9d8 100644
--- a/packages/responses-chat-bridge/src/__tests__/translate-request.test.ts
+++ b/packages/responses-chat-bridge/src/__tests__/translate-request.test.ts
@@ -299,6 +299,86 @@ describe('translateResponsesRequest', () => {
}))).toThrowError(UnsupportedResponsesFeatureError);
});
+ it('drops replayed Codex tool_search items without breaking tool-call merging', () => {
+ const dropped: Array<[string, number]> = [];
+ const out = translateResponsesRequest(base({
+ input: [
+ { type: 'message', role: 'user', content: 'hi' },
+ { type: 'tool_search_call', id: 'ts_1' },
+ {
+ type: 'function_call',
+ call_id: 'call_1',
+ name: 'shell',
+ arguments: '{"cmd":"ls"}',
+ },
+ { type: 'tool_search_output', id: 'ts_1', output: {} },
+ { type: 'tool_search_call_output', call_id: 'ts_1', output: {} },
+ { type: 'function_call_output', call_id: 'call_1', output: 'ok' },
+ ],
+ }), { onDroppedInputItem: (type, index) => dropped.push([type, index]) });
+
+ expect(out.messages).toEqual([
+ { role: 'user', content: 'hi' },
+ {
+ role: 'assistant',
+ content: null,
+ tool_calls: [
+ { id: 'call_1', type: 'function', function: { name: 'shell', arguments: '{"cmd":"ls"}' } },
+ ],
+ },
+ { role: 'tool', tool_call_id: 'call_1', content: 'ok' },
+ ]);
+ expect(dropped).toEqual([
+ ['tool_search_call', 1],
+ ['tool_search_output', 3],
+ ['tool_search_call_output', 4],
+ ]);
+ });
+
+ it('does not let dropped tool_search items split assistant merging', () => {
+ const dropped: string[] = [];
+ const out = translateResponsesRequest(base({
+ input: [
+ {
+ type: 'message',
+ role: 'assistant',
+ content: 'planning',
+ },
+ { type: 'tool_search_call', id: 'ts_1' },
+ { type: 'tool_search_output', id: 'ts_1', output: {} },
+ {
+ type: 'function_call',
+ call_id: 'call_1',
+ name: 'shell',
+ arguments: '{"cmd":"ls"}',
+ },
+ { type: 'tool_search_call_output', call_id: 'ts_1', output: {} },
+ {
+ type: 'function_call',
+ call_id: 'call_2',
+ name: 'shell',
+ arguments: '{"cmd":"pwd"}',
+ },
+ { type: 'function_call_output', call_id: 'call_1', output: 'a' },
+ { type: 'function_call_output', call_id: 'call_2', output: 'b' },
+ ],
+ }), { onDroppedInputItem: (type) => dropped.push(type) });
+
+ expect(out.messages).toEqual([
+ {
+ role: 'assistant',
+ content: 'planning',
+ tool_calls: [
+ { id: 'call_1', type: 'function', function: { name: 'shell', arguments: '{"cmd":"ls"}' } },
+ { id: 'call_2', type: 'function', function: { name: 'shell', arguments: '{"cmd":"pwd"}' } },
+ ],
+ },
+ { role: 'tool', tool_call_id: 'call_1', content: 'a' },
+ { role: 'tool', tool_call_id: 'call_2', content: 'b' },
+ ]);
+ expect(dropped).toEqual(['tool_search_call', 'tool_search_output', 'tool_search_call_output']);
+ });
+
it('translates capability-gated Kimi user images and preserves replayed history order', () => {
const imageUrl = 'data:image/png;base64,aW1hZ2U=';
const out = translateResponsesRequest(base({
diff --git a/packages/responses-chat-bridge/src/handler.ts b/packages/responses-chat-bridge/src/handler.ts
index ea956ada9e..220f07994a 100644
--- a/packages/responses-chat-bridge/src/handler.ts
+++ b/packages/responses-chat-bridge/src/handler.ts
@@ -130,6 +130,11 @@ export function createResponsesChatHandler(
onDroppedTool: (type, index) => {
log.warn?.('responses-chat bridge dropped non-function tool', { type, index });
},
+ onDroppedInputItem: (type, index) => {
+ // tool_search_* 是 Codex 每轮重放的历史内建 item,属于预期兼容路径,
+ // 不发 warn 避免随会话长度二次增长日志。
+ log.debug?.('responses-chat bridge dropped built-in input item', { type, index });
+ },
});
} catch (error) {
if (error instanceof UnsupportedResponsesFeatureError) {
diff --git a/packages/responses-chat-bridge/src/translate-request.ts b/packages/responses-chat-bridge/src/translate-request.ts
index e23bcec165..543a033e56 100644
--- a/packages/responses-chat-bridge/src/translate-request.ts
+++ b/packages/responses-chat-bridge/src/translate-request.ts
@@ -123,6 +123,8 @@ interface TranslateInputOptions {
toolCallReasoningPlaceholder: boolean;
/** Gemini 3 OpenAI 兼容层:为每步首个回放 tool call 注入官方允许的签名跳过值。 */
googleThoughtSignaturePlaceholder: boolean;
+ /** 丢弃的无上下文内建 input item(Codex tool_search 等)回调,供 handler 记 log。 */
+ onDroppedInputItem?: (type: string, index: number) => void;
}
/** cc-switch 的占位口径:kimi/DeepSeek 要求 tool_call assistant 消息带非空 reasoning_content。 */
@@ -182,6 +184,18 @@ function translateInput(input: ResponsesRequest['input'], opts: TranslateInputOp
for (let index = 0; index < input.length; index += 1) {
const item = input[index];
if (!isPlainObject(item)) throw new UnsupportedResponsesFeatureError(`input[${index}]`);
+ const itemType = typeof item.type === 'string' ? item.type : undefined;
+ if (
+ itemType === 'tool_search_call'
+ || itemType === 'tool_search_output'
+ || itemType === 'tool_search_call_output'
+ ) {
+ // Codex 内建 tool_search 的回放 item:与 namespace/web_search 工具声明同口径,
+ // 对第三方 Chat 上游无意义且不承载用户上下文,必须作为“非边界”item 剥掉降级
+ // —— 在 flushAssistant 之前处理,避免拆分本应合并的 assistant message。
+ opts.onDroppedInputItem?.(itemType, index);
+ continue;
+ }
if ('role' in item && typeof item.role === 'string') {
const content = messageContent(
@@ -336,6 +350,8 @@ export interface TranslateResponsesRequestOptions {
capabilities?: ChatBridgeCapabilities;
/** 被剥掉的非 function 工具(Codex 内建 namespace / local_shell 等)回调,供 handler 记 log。 */
onDroppedTool?: (type: string, index: number) => void;
+ /** 被剥掉的无上下文内建 input item(Codex tool_search 等)回调,供 handler 记 log。 */
+ onDroppedInputItem?: (type: string, index: number) => void;
}
/** Convert a Codex Responses request to a streaming Chat Completions request. */
@@ -353,6 +369,7 @@ export function translateResponsesRequest(
imageInput: capabilities.imageInput,
toolCallReasoningPlaceholder: capabilities.toolCallReasoningPlaceholder === true,
googleThoughtSignaturePlaceholder: capabilities.googleThoughtSignaturePlaceholder === true,
+ onDroppedInputItem: opts.onDroppedInputItem,
});
if (input.instructions) {
messages.unshift({ role: developerRole, content: input.instructions });