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): 显示 Web Search 实际查询内容

DavidShen143e7d2426c0
diff --git a/apps/desktop/src/main/__tests__/codexTranslator.test.ts b/apps/desktop/src/main/__tests__/codexTranslator.test.ts
index 7a5ac50260..8a12214b18 100644
--- a/apps/desktop/src/main/__tests__/codexTranslator.test.ts
+++ b/apps/desktop/src/main/__tests__/codexTranslator.test.ts
@@ -252,6 +252,91 @@ describe('webSearch', () => {
feedItem('updated', { type: 'webSearch', id: 'ws2', query: 'x' });
expect(coll.events).toHaveLength(0);
});
+
+ it('优先从 action 归一实际查询内容并保留完整参数', () => {
+ const action = {
+ type: 'search',
+ query: 'actual query',
+ queries: ['actual query', 'backup query'],
+ };
+ feedItem('started', { type: 'webSearch', id: 'ws-action', query: '', action });
+
+ expect(coll.events).toHaveLength(1);
+ expect((coll.events[0].data as { input: unknown }).input).toEqual({
+ query: 'actual query',
+ action,
+ });
+ });
+
+ it('多查询、打开页面和页内查找都生成可读摘要', () => {
+ feedItem('started', {
+ type: 'webSearch',
+ id: 'ws-many',
+ query: '',
+ action: { type: 'search', query: null, queries: ['', 'first query', 'second query'] },
+ });
+ feedItem('started', {
+ type: 'webSearch',
+ id: 'ws-open',
+ query: '',
+ action: { type: 'openPage', url: 'https://example.com/page' },
+ });
+ feedItem('started', {
+ type: 'webSearch',
+ id: 'ws-find',
+ query: '',
+ action: { type: 'findInPage', url: 'https://example.com/page', pattern: 'needle' },
+ });
+
+ expect(coll.events.map((event) => (
+ event.data as { input: { query: string } }
+ ).input.query)).toEqual([
+ 'first query ...',
+ 'https://example.com/page',
+ "'needle' in https://example.com/page",
+ ]);
+ });
+
+ it('started 参数为空时等 updated 补齐,且只落一条 tool_use', () => {
+ feedItem('started', { type: 'webSearch', id: 'ws-late', query: '', action: null });
+ expect(coll.events).toHaveLength(0);
+
+ feedItem('updated', {
+ type: 'webSearch',
+ id: 'ws-late',
+ query: '',
+ action: { type: 'search', query: 'late query', queries: null },
+ });
+ feedItem('completed', {
+ type: 'webSearch',
+ id: 'ws-late',
+ query: '',
+ action: { type: 'search', query: 'late query', queries: null },
+ });
+
+ expect(coll.events.map((event) => event.type)).toEqual([
+ 'tool_use',
+ 'tool_result_full',
+ 'tool_result',
+ ]);
+ expect((coll.events[0].data as { input: { query: string } }).input.query).toBe('late query');
+ });
+
+ it('缺失 started/updated 时 completed 仍先补 tool_use', () => {
+ feedItem('completed', {
+ type: 'webSearch',
+ id: 'ws-completed-only',
+ query: '',
+ action: { type: 'search', query: 'completed query', queries: null },
+ });
+
+ expect(coll.events.map((event) => event.type)).toEqual([
+ 'tool_use',
+ 'tool_result_full',
+ 'tool_result',
+ ]);
+ expect((coll.events[0].data as { input: { query: string } }).input.query).toBe('completed query');
+ });
});
// ── fileChange ──────────────────────────────────────────────────────────────
diff --git a/packages/maker-core/src/agents/codex/translator.ts b/packages/maker-core/src/agents/codex/translator.ts
index e365d1641f..c5aeba09df 100644
--- a/packages/maker-core/src/agents/codex/translator.ts
+++ b/packages/maker-core/src/agents/codex/translator.ts
@@ -871,7 +871,67 @@ function mcpContentTextParts(content: unknown[] | undefined): string[] {
}
// ── webSearch → tool_use + tool_result_full + tool_result ────────────────────
-// v2 加了 action 字段 (Search/OpenPage/FindInPage), 用 query 兜底显示。
+// v2 的 legacy query 可能为空,真实动作优先在 action 中;归一成 query 供现有
+// maker-shared / Desktop / Mobile 展示链路消费,同时保留 action 供展开详情核验。
+
+function nonEmptyWebSearchText(value: unknown): string {
+ return typeof value === 'string' ? value.trim() : '';
+}
+
+function webSearchActionDetail(action: WebSearchAction | null | undefined): string {
+ if (!action) return '';
+ switch (action.type) {
+ case 'search': {
+ const query = nonEmptyWebSearchText(action.query);
+ if (query) return query;
+ const queries = Array.isArray(action.queries)
+ ? action.queries.map(nonEmptyWebSearchText).filter(Boolean)
+ : [];
+ if (queries.length === 0) return '';
+ return queries.length > 1 ? `${queries[0]} ...` : queries[0];
+ }
+ case 'openPage':
+ return nonEmptyWebSearchText(action.url);
+ case 'findInPage': {
+ const url = nonEmptyWebSearchText(action.url);
+ const pattern = nonEmptyWebSearchText(action.pattern);
+ if (pattern && url) return `'${pattern}' in ${url}`;
+ if (pattern) return `'${pattern}'`;
+ return url;
+ }
+ default:
+ return '';
+ }
+}
+
+function webSearchInput(item: WebSearchItem): {
+ query: string;
+ action?: WebSearchAction;
+} {
+ const query = webSearchActionDetail(item.action) || nonEmptyWebSearchText(item.query);
+ return {
+ query,
+ ...(item.action ? { action: item.action } : {}),
+ };
+}
+
+function emitWebSearchToolUse(
+ item: WebSearchItem,
+ input: ReturnType<typeof webSearchInput>,
+ queue: AsyncQueue<AgentEvent>,
+ ctx: CodexTranslateContext,
+): void {
+ ctx.rt.emittedToolUse.add(item.id);
+ queue.push({
+ type: 'tool_use',
+ data: {
+ toolUseId: item.id,
+ toolName: 'web_search',
+ input,
+ },
+ source: 'codex',
+ });
+}
function handleWebSearch(
phase: ItemPhase,
@@ -879,24 +939,30 @@ function handleWebSearch(
queue: AsyncQueue<AgentEvent>,
ctx: CodexTranslateContext,
): void {
+ const input = webSearchInput(item);
+
if (phase === 'started') {
if (ctx.rt.emittedToolUse.has(item.id)) return;
- ctx.rt.emittedToolUse.add(item.id);
- queue.push({
- type: 'tool_use',
- data: {
- toolUseId: item.id,
- toolName: 'web_search',
- input: { query: item.query, action: item.action ?? undefined },
- },
- source: 'codex',
- });
+ // 某些 Codex 版本在 started 只给空 legacy query,updated 才补 action;
+ // 延迟这一条展示事件,避免空参数先落库后无法无损更新。
+ if (!input.query) return;
+ emitWebSearchToolUse(item, input, queue, ctx);
+ return;
+ }
+ if (phase === 'updated') {
+ if (ctx.rt.emittedToolUse.has(item.id) || !input.query) return;
+ emitWebSearchToolUse(item, input, queue, ctx);
return;
}
- if (phase === 'updated') return;
// completed
+ const toolUseEmitted = ctx.rt.emittedToolUse.has(item.id);
ctx.rt.emittedToolUse.delete(item.id);
+ // 防御缺失 started/updated 的历史或异常事件序列,保持 tool_use → result 顺序。
+ if (!toolUseEmitted) {
+ emitWebSearchToolUse(item, input, queue, ctx);
+ ctx.rt.emittedToolUse.delete(item.id);
+ }
queue.push({
type: 'tool_result_full',
data: { toolUseId: item.id, fullText: '', isError: false },