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 事件归一化

DavidShen3dc29e151049
diff --git a/apps/desktop/src/main/__tests__/codexTranslator.test.ts b/apps/desktop/src/main/__tests__/codexTranslator.test.ts
index 8a12214b18..259d097547 100644
--- a/apps/desktop/src/main/__tests__/codexTranslator.test.ts
+++ b/apps/desktop/src/main/__tests__/codexTranslator.test.ts
@@ -287,6 +287,12 @@ describe('webSearch', () => {
query: '',
action: { type: 'findInPage', url: 'https://example.com/page', pattern: 'needle' },
});
+ feedItem('started', {
+ type: 'webSearch',
+ id: 'ws-find-escaped',
+ query: '',
+ action: { type: 'findInPage', url: 'https://example.com/page', pattern: "foo'\\bar" },
+ });
expect(coll.events.map((event) => (
event.data as { input: { query: string } }
@@ -294,11 +300,17 @@ describe('webSearch', () => {
'first query ...',
'https://example.com/page',
"'needle' in https://example.com/page",
+ "'foo\\'\\\\bar' in https://example.com/page",
]);
});
- it('started 参数为空时等 updated 补齐,且只落一条 tool_use', () => {
- feedItem('started', { type: 'webSearch', id: 'ws-late', query: '', action: null });
+ it('started 只有 legacy query 时等 updated action 补齐,且只落一条 tool_use', () => {
+ feedItem('started', {
+ type: 'webSearch',
+ id: 'ws-late',
+ query: 'placeholder query',
+ action: null,
+ });
expect(coll.events).toHaveLength(0);
feedItem('updated', {
@@ -319,7 +331,34 @@ describe('webSearch', () => {
'tool_result_full',
'tool_result',
]);
- expect((coll.events[0].data as { input: { query: string } }).input.query).toBe('late query');
+ expect((coll.events[0].data as { input: unknown }).input).toEqual({
+ query: 'late query',
+ action: { type: 'search', query: 'late query', queries: null },
+ });
+ });
+
+ it('只有 legacy query 时在 completed 补发调用记录', () => {
+ feedItem('started', {
+ type: 'webSearch',
+ id: 'ws-legacy-only',
+ query: 'legacy query',
+ action: null,
+ });
+ expect(coll.events).toHaveLength(0);
+
+ feedItem('completed', {
+ type: 'webSearch',
+ id: 'ws-legacy-only',
+ query: 'legacy query',
+ action: 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('legacy query');
});
it('缺失 started/updated 时 completed 仍先补 tool_use', () => {
@@ -337,6 +376,14 @@ describe('webSearch', () => {
]);
expect((coll.events[0].data as { input: { query: string } }).input.query).toBe('completed query');
});
+
+ it('completed 仍无有效参数时不生成空白调用记录', () => {
+ feedItem('started', { type: 'webSearch', id: 'ws-empty', query: '', action: null });
+ feedItem('updated', { type: 'webSearch', id: 'ws-empty', query: '', action: null });
+ feedItem('completed', { type: 'webSearch', id: 'ws-empty', query: '', action: null });
+
+ expect(coll.events).toHaveLength(0);
+ });
});
// ── fileChange ──────────────────────────────────────────────────────────────
diff --git a/packages/maker-core/src/agents/codex/translator.ts b/packages/maker-core/src/agents/codex/translator.ts
index c5aeba09df..ba764f5709 100644
--- a/packages/maker-core/src/agents/codex/translator.ts
+++ b/packages/maker-core/src/agents/codex/translator.ts
@@ -878,6 +878,10 @@ function nonEmptyWebSearchText(value: unknown): string {
return typeof value === 'string' ? value.trim() : '';
}
+function quoteWebSearchText(value: string): string {
+ return `'${value.replace(/['\\]/g, (char) => `\\${char}`)}'`;
+}
+
function webSearchActionDetail(action: WebSearchAction | null | undefined): string {
if (!action) return '';
switch (action.type) {
@@ -895,8 +899,8 @@ function webSearchActionDetail(action: WebSearchAction | null | undefined): stri
case 'findInPage': {
const url = nonEmptyWebSearchText(action.url);
const pattern = nonEmptyWebSearchText(action.pattern);
- if (pattern && url) return `'${pattern}' in ${url}`;
- if (pattern) return `'${pattern}'`;
+ if (pattern && url) return `${quoteWebSearchText(pattern)} in ${url}`;
+ if (pattern) return quoteWebSearchText(pattern);
return url;
}
default:
@@ -904,11 +908,14 @@ function webSearchActionDetail(action: WebSearchAction | null | undefined): stri
}
}
-function webSearchInput(item: WebSearchItem): {
+function webSearchInput(
+ item: WebSearchItem,
+ actionDetail = webSearchActionDetail(item.action),
+): {
query: string;
action?: WebSearchAction;
} {
- const query = webSearchActionDetail(item.action) || nonEmptyWebSearchText(item.query);
+ const query = actionDetail || nonEmptyWebSearchText(item.query);
return {
query,
...(item.action ? { action: item.action } : {}),
@@ -939,18 +946,19 @@ function handleWebSearch(
queue: AsyncQueue<AgentEvent>,
ctx: CodexTranslateContext,
): void {
- const input = webSearchInput(item);
+ const actionDetail = webSearchActionDetail(item.action);
+ const input = webSearchInput(item, actionDetail);
if (phase === 'started') {
if (ctx.rt.emittedToolUse.has(item.id)) return;
- // 某些 Codex 版本在 started 只给空 legacy query,updated 才补 action;
- // 延迟这一条展示事件,避免空参数先落库后无法无损更新。
- if (!input.query) return;
+ // started 的 legacy query 可能为空或只是占位符,updated 才补真实 action;
+ // 延迟到 action 到达或 completed,避免旧参数先落库后无法无损更新。
+ if (!actionDetail) return;
emitWebSearchToolUse(item, input, queue, ctx);
return;
}
if (phase === 'updated') {
- if (ctx.rt.emittedToolUse.has(item.id) || !input.query) return;
+ if (ctx.rt.emittedToolUse.has(item.id) || !actionDetail) return;
emitWebSearchToolUse(item, input, queue, ctx);
return;
}
@@ -960,6 +968,8 @@ function handleWebSearch(
ctx.rt.emittedToolUse.delete(item.id);
// 防御缺失 started/updated 的历史或异常事件序列,保持 tool_use → result 顺序。
if (!toolUseEmitted) {
+ // completed 仍无可展示参数时整条忽略,避免补发空白 tool_use 和孤立 result。
+ if (!input.query) return;
emitWebSearchToolUse(item, input, queue, ctx);
ctx.rt.emittedToolUse.delete(item.id);
}