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): drop search tools for Chat bridge (#2439)

许君山 Junshan77d017e73f76
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 b6dcb67c15..bf2fd81be0 100644
--- a/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts
@@ -2953,6 +2953,113 @@ describe('codex proxy host', () => {
setCustomProviders([]);
});
+ it('drops Responses-native search tools before forwarding an ordinary Chat bridge turn', 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: 'qwen-chat-provider',
+ name: 'Qwen Chat Provider',
+ runtimes: {
+ codex: {
+ baseUrl: 'https://chat-provider.example/v1',
+ wireProtocol: 'openai-chat',
+ models: [{ id: 'qwen3.8-max', name: 'Qwen 3.8 Max' }],
+ },
+ },
+ }),
+ ]);
+ setCustomProviderKeyReader(() => 'chat-provider-key');
+ host.registerComposed('session-qwen-chat', 'thread-qwen-chat', 'PRODUCT_PROMPT');
+ setSessionProvider('session-qwen-chat', 'qwen-chat-provider');
+
+ const parsedBody = {
+ model: 'qwen3.8-max',
+ tools: [{ type: 'function', name: 'shell' }, { type: 'web_search' }],
+ input: [{ role: 'user', content: 'hello' }],
+ };
+ const ctx = { reqId: 1, method: 'POST', url: '/responses', headers: { 'thread-id': 'thread-qwen-chat' } };
+ 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',
+ tools: [{ type: 'function', name: 'shell' }],
+ },
+ res,
+ });
+
+ clearSessionProvider('session-qwen-chat');
+ setCustomProviderKeyReader(() => null);
+ setCustomProviders([]);
+ });
+
+ it('resets a removed search tool choice for Gemini Chat bridge turns', 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: 'gemini-chat-provider',
+ name: 'Gemini Chat Provider',
+ runtimes: {
+ codex: {
+ baseUrl: 'https://generativelanguage.googleapis.com/v1beta/openai',
+ wireProtocol: 'openai-chat',
+ models: [{ id: 'gemini-2.5-pro', name: 'Gemini 2.5 Pro' }],
+ },
+ },
+ }),
+ ]);
+ setCustomProviderKeyReader(() => 'gemini-chat-key');
+ host.registerComposed('session-gemini-chat', 'thread-gemini-chat', 'PRODUCT_PROMPT');
+ setSessionProvider('session-gemini-chat', 'gemini-chat-provider');
+
+ const parsedBody = {
+ model: 'gemini-2.5-pro',
+ tools: [{ type: 'function', name: 'shell' }, { type: 'web_search' }],
+ tool_choice: { type: 'web_search' },
+ parallel_tool_calls: true,
+ input: [{ role: 'user', content: 'hello' }],
+ };
+ const ctx = { reqId: 1, method: 'POST', url: '/responses', headers: { 'thread-id': 'thread-gemini-chat' } };
+ 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 Gemini 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',
+ tools: [{ type: 'function', name: 'shell' }],
+ tool_choice: 'auto',
+ },
+ res,
+ });
+
+ clearSessionProvider('session-gemini-chat');
+ setCustomProviderKeyReader(() => null);
+ setCustomProviders([]);
+ });
+
it('passes the parent session model into a Guardian request handled by the Anthropic bridge', 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 64b878b070..d1e7eda645 100644
--- a/apps/desktop/src/main/maker-host/codex-proxy-host.ts
+++ b/apps/desktop/src/main/maker-host/codex-proxy-host.ts
@@ -547,12 +547,21 @@ function providerAwareGuardianReviewerModel(
const GUARDIAN_PROVIDER_SEARCH_TOOL_TYPES = new Set(['web_search', 'x_search']);
+function providerSearchToolChoiceReferencesRemovedTool(
+ toolChoice: unknown,
+ tools: readonly unknown[],
+): boolean {
+ if (!isPlainObject(toolChoice) || typeof toolChoice.type !== 'string') return false;
+ if (!GUARDIAN_PROVIDER_SEARCH_TOOL_TYPES.has(toolChoice.type)) return false;
+ return !tools.some((tool) => isPlainObject(tool) && tool.type === toolChoice.type);
+}
+
/**
* Guardian decides whether another action may run. Provider-hosted search
* tools must not let that reviewer initiate an unrelated upstream network
* action with the approval context.
*/
-function stripGuardianProviderSearchTools(
+function stripProviderSearchTools(
body: Record<string, unknown>,
): Record<string, unknown> {
if (!Array.isArray(body.tools)) return body;
@@ -565,8 +574,16 @@ function stripGuardianProviderSearchTools(
if (tools.length === body.tools.length) return body;
const next = { ...body };
- if (tools.length > 0) next.tools = tools;
- else delete next.tools;
+ if (tools.length > 0) {
+ next.tools = tools;
+ if (providerSearchToolChoiceReferencesRemovedTool(next.tool_choice, tools)) {
+ next.tool_choice = 'auto';
+ }
+ } else {
+ delete next.tools;
+ delete next.tool_choice;
+ delete next.parallel_tool_calls;
+ }
return next;
}
@@ -591,7 +608,7 @@ function createProviderAwareGuardianReviewerTransform(
toModel: mainModel,
providerId: getSessionProvider(sessionId),
});
- return stripGuardianProviderSearchTools({ ...body, model: mainModel });
+ return stripProviderSearchTools({ ...body, model: mainModel });
};
}
@@ -878,10 +895,13 @@ function prepareLocalBridgeBody(opts: PrepareLocalBridgeBodyOptions): unknown {
// Keep the already parsed body if the defensive strip result cannot be parsed.
}
}
- if (opts.requestModelOverride && isPlainObject(body)) {
- body = stripGuardianProviderSearchTools({
+ // Chat Completions has no Responses-native search tool. Codex can attach it
+ // automatically even for an ordinary turn, so remove it before translating
+ // a provider-routed chat request instead of failing the entire request.
+ if (isPlainObject(body) && (opts.requestModelOverride || opts.bridge === 'chat')) {
+ body = stripProviderSearchTools({
...body,
- model: opts.requestModelOverride,
+ ...(opts.requestModelOverride ? { model: opts.requestModelOverride } : {}),
});
}
if (isPlainObject(body)) {