Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 更新 Web Search 完成态参数
DavidShen86ba7a44c859
diff --git a/apps/desktop/src/main/__tests__/codexTranslator.test.ts b/apps/desktop/src/main/__tests__/codexTranslator.test.tsindex f53ada4fc5..bdfa045c03 100644--- a/apps/desktop/src/main/__tests__/codexTranslator.test.ts+++ b/apps/desktop/src/main/__tests__/codexTranslator.test.ts@@ -337,6 +337,46 @@ describe('webSearch', () => {});});+ it('completed 的权威 action 变化时用同一 id 补发 tool_use 更新', () => {+ feedItem('started', {+ type: 'webSearch',+ id: 'ws-final-action',+ query: '',+ action: { type: 'search', query: 'early query', queries: null },+ });+ feedItem('completed', {+ type: 'webSearch',+ id: 'ws-final-action',+ query: '',+ action: { type: 'openPage', url: 'https://example.com/final' },+ });++ expect(coll.events.map((event) => event.type)).toEqual([+ 'tool_use',+ 'tool_use',+ 'tool_result_full',+ 'tool_result',+ ]);+ expect(coll.events.slice(0, 2).map((event) => event.data)).toEqual([+ {+ toolUseId: 'ws-final-action',+ toolName: 'web_search',+ input: {+ query: 'early query',+ action: { type: 'search', query: 'early query', queries: null },+ },+ },+ {+ toolUseId: 'ws-final-action',+ toolName: 'web_search',+ input: {+ query: 'https://example.com/final',+ action: { type: 'openPage', url: 'https://example.com/final' },+ },+ },+ ]);+ });+it('completed 快照丢失字段时仍用 started 的 legacy query 补发调用记录', () => {feedItem('started', {type: 'webSearch',diff --git a/apps/desktop/src/main/__tests__/messagePersistBroadcaster.test.ts b/apps/desktop/src/main/__tests__/messagePersistBroadcaster.test.tsindex 980c0e4fd2..eb43d41d85 100644--- a/apps/desktop/src/main/__tests__/messagePersistBroadcaster.test.ts+++ b/apps/desktop/src/main/__tests__/messagePersistBroadcaster.test.ts@@ -121,6 +121,44 @@ describe('update_plan tool_use persistence', () => {);});+ it('updates the existing web_search row when completed carries authoritative input', async () => {+ const firstPersistId = onToolUseEvent(+ SESSION,+ {+ toolUseId: 'search-1',+ toolName: 'web_search',+ input: { query: 'early query', action: { type: 'search', query: 'early query' } },+ },+ null,+ );+ const secondPersistId = onToolUseEvent(+ SESSION,+ {+ toolUseId: 'search-1',+ toolName: 'web_search',+ input: { query: 'https://example.com/final', action: { type: 'openPage', url: 'https://example.com/final' } },+ },+ null,+ );++ expect(secondPersistId).toBe(firstPersistId);++ await flushWrites();+ expect(createMessage).toHaveBeenCalledTimes(1);+ expect(updateMessageContent).toHaveBeenCalledWith(+ SESSION,+ firstPersistId,+ {+ toolUseId: 'search-1',+ toolName: 'web_search',+ input: {+ query: 'https://example.com/final',+ action: { type: 'openPage', url: 'https://example.com/final' },+ },+ },+ );+ });+it('does not dedupe ordinary repeated tool_use ids', async () => {const firstPersistId = onToolUseEvent(SESSION,diff --git a/apps/desktop/src/main/messagePersistBroadcaster.ts b/apps/desktop/src/main/messagePersistBroadcaster.tsindex b844862a57..f123131d5a 100644--- a/apps/desktop/src/main/messagePersistBroadcaster.ts+++ b/apps/desktop/src/main/messagePersistBroadcaster.ts@@ -346,7 +346,7 @@ const toolUseCreatedAtBySession = new Map<string, Map<string, number>>();* 需要按 tool_use 的 input.args 去 mediaToolResultFallback 池里认领结果。*/const toolUseInfoBySession = new Map<string, Map<string, { toolName: string; input: unknown }>>();-const planToolUsePersistIdBySession = new Map<string, Map<string, string>>();+const updatableToolUsePersistIdBySession = new Map<string, Map<string, string>>();function rememberToolUseId(sessionId: string, toolUseId: string, createdAt: number): void {let set = knownToolUseIdsBySession.get(sessionId);@@ -384,11 +384,15 @@ function clampAfterLatestToolUse(sessionId: string, toolUseIds: string[], createreturn latestToolUseCreatedAt + 1;}-function rememberPlanToolUsePersistId(sessionId: string, toolUseId: string, persistId: string): void {- let idMap = planToolUsePersistIdBySession.get(sessionId);+function isUpdatableToolUse(toolName: string): boolean {+ return toolName === 'update_plan' || toolName === 'web_search';+}++function rememberUpdatableToolUsePersistId(sessionId: string, toolUseId: string, persistId: string): void {+ let idMap = updatableToolUsePersistIdBySession.get(sessionId);if (!idMap) {idMap = new Map();- planToolUsePersistIdBySession.set(sessionId, idMap);+ updatableToolUsePersistIdBySession.set(sessionId, idMap);}idMap.set(toolUseId, persistId);}@@ -414,16 +418,16 @@ export function onToolUseEvent(input: data.input,});}- const existingPlanPersistId = toolName === 'update_plan' && toolUseId- ? planToolUsePersistIdBySession.get(sessionId)?.get(toolUseId)+ const existingPersistId = isUpdatableToolUse(toolName) && toolUseId+ ? updatableToolUsePersistIdBySession.get(sessionId)?.get(toolUseId): undefined;- if (existingPlanPersistId) {+ if (existingPersistId) {const content = { toolUseId, toolName, input: data.input };- enqueueWrite(`tool_use_update:${sessionId}:${existingPlanPersistId}`, () =>- updateDbMessageContent(sessionId, existingPlanPersistId, content),+ enqueueWrite(`tool_use_update:${sessionId}:${existingPersistId}`, () =>+ updateDbMessageContent(sessionId, existingPersistId, content),);- notePersistedMessage(sessionId, 'tool_use', existingPlanPersistId);- return existingPlanPersistId;+ notePersistedMessage(sessionId, 'tool_use', existingPersistId);+ return existingPersistId;}const persistId = createId();const meta = agentMeta ?? lastAgentMetaBySession.get(sessionId) ?? null;@@ -436,8 +440,8 @@ export function onToolUseEvent(agentMeta: meta,createdAt,});- if (toolName === 'update_plan' && toolUseId) {- rememberPlanToolUsePersistId(sessionId, toolUseId, persistId);+ if (isUpdatableToolUse(toolName) && toolUseId) {+ rememberUpdatableToolUsePersistId(sessionId, toolUseId, persistId);}notePersistedMessage(sessionId, 'tool_use', persistId);return persistId;@@ -875,7 +879,7 @@ export function resetTurnPersistState(sessionId: string): void {knownToolUseIdsBySession.delete(sessionId);toolUseCreatedAtBySession.delete(sessionId);toolUseInfoBySession.delete(sessionId);- planToolUsePersistIdBySession.delete(sessionId);+ updatableToolUsePersistIdBySession.delete(sessionId);lastAgentMetaBySession.delete(sessionId);_turnStartedAtBySession.delete(sessionId);_turnDedupIdBySession.delete(sessionId);@@ -1148,7 +1152,7 @@ export function clearSessionPersistState(sessionId: string): void {knownToolUseIdsBySession.delete(sessionId);toolUseCreatedAtBySession.delete(sessionId);toolUseInfoBySession.delete(sessionId);- planToolUsePersistIdBySession.delete(sessionId);+ updatableToolUsePersistIdBySession.delete(sessionId);toolResultIdByToolUseId.delete(sessionId);pendingFullTextByToolUseId.delete(sessionId);toolResultContentByClientId.delete(sessionId);diff --git a/apps/desktop/src/renderer/__tests__/makerChatStoreTextDeltaBatching.test.ts b/apps/desktop/src/renderer/__tests__/makerChatStoreTextDeltaBatching.test.tsindex 93a86ea747..67191fb0f8 100644--- a/apps/desktop/src/renderer/__tests__/makerChatStoreTextDeltaBatching.test.ts+++ b/apps/desktop/src/renderer/__tests__/makerChatStoreTextDeltaBatching.test.ts@@ -418,6 +418,51 @@ describe('makerChatStore text delta batching', () => {expect(messages[0]?.clientId).toBe('assistant-1');});+ it('updates a repeated web_search tool_use row in place', () => {+ onEvent?.({+ sessionId: SESSION_ID,+ event: {+ type: 'tool_use',+ source: 'codex',+ data: {+ toolUseId: 'search-1',+ toolName: 'web_search',+ input: { query: 'early query' },+ },+ },+ persistId: 'search-message-1',+ });+ onEvent?.({+ sessionId: SESSION_ID,+ event: {+ type: 'tool_use',+ source: 'codex',+ data: {+ toolUseId: 'search-1',+ toolName: 'web_search',+ input: {+ query: 'https://example.com/final',+ action: { type: 'openPage', url: 'https://example.com/final' },+ },+ },+ },+ persistId: 'search-message-1',+ });++ const messages = makerChatStore.getSnapshot(SESSION_ID).messages;+ expect(messages).toHaveLength(1);+ expect(messages[0]).toMatchObject({+ clientId: 'search-message-1',+ role: 'tool_use',+ toolUseId: 'search-1',+ toolName: 'web_search',+ toolInput: {+ query: 'https://example.com/final',+ action: { type: 'openPage', url: 'https://example.com/final' },+ },+ });+ });+it('flushes pending text before a permission interaction request on the separate IPC channel', () => {const snapshots: Array<{ roles: string[]; pendingPermission: string | null }> = [];const unsubscribe = makerChatStore.subscribe(SESSION_ID, () => {diff --git a/apps/desktop/src/renderer/lib/makerChatStore.ts b/apps/desktop/src/renderer/lib/makerChatStore.tsindex 49cf10737e..49f56afd9d 100644--- a/apps/desktop/src/renderer/lib/makerChatStore.ts+++ b/apps/desktop/src/renderer/lib/makerChatStore.ts@@ -2305,17 +2305,17 @@ export function handleStreamEvent(const finalized = finalizeStreamingInState(state);const clientId = event.persistId ?? crypto.randomUUID();- const existingUpdatePlanIdx =- toolName === 'update_plan'+ const existingUpdatableToolIdx =+ toolName === 'update_plan' || toolName === 'web_search'? finalized.messages.findIndex((m) =>- m.role === 'tool_use' && m.toolName === 'update_plan' && m.toolUseId === toolUseId,+ m.role === 'tool_use' && m.toolName === toolName && m.toolUseId === toolUseId,): -1;- if (existingUpdatePlanIdx >= 0) {+ if (existingUpdatableToolIdx >= 0) {const messages = finalized.messages.slice();- messages[existingUpdatePlanIdx] = {- ...messages[existingUpdatePlanIdx],+ messages[existingUpdatableToolIdx] = {+ ...messages[existingUpdatableToolIdx],content: formatToolUseSummary(toolName, input),toolInput: input,};diff --git a/packages/maker-core/src/agents/codex/translator.ts b/packages/maker-core/src/agents/codex/translator.tsindex bb933734e3..9663092d60 100644--- a/packages/maker-core/src/agents/codex/translator.ts+++ b/packages/maker-core/src/agents/codex/translator.ts@@ -61,6 +61,8 @@ export interface CodexRuntimeState {emittedToolUse: Set<string>;/** 尚未 emit 的 Web Search 候选输入,供跨 started/updated/completed 快照补全。 */pendingWebSearchInput: Map<string, WebSearchInput>;+ /** 已 emit 的 Web Search 输入,用于判断 completed 是否带来权威参数更新。 */+ emittedWebSearchInput: Map<string, WebSearchInput>;/*** Auth retry-loop dedupe key (`<threadId>|<turnId>`)。daemon 撞 401 时* willRetry=true 通知会按 retry 频率 (~每秒) 持续发, 不 dedupe 会让@@ -87,6 +89,7 @@ export function newCodexRuntimeState(): CodexRuntimeState {itemTextLen: new Map(),emittedToolUse: new Set(),pendingWebSearchInput: new Map(),+ emittedWebSearchInput: new Map(),lastAuthErrorKey: null,networkRetryNotice: null,};@@ -949,6 +952,7 @@ function emitWebSearchToolUse(ctx: CodexTranslateContext,): void {ctx.rt.emittedToolUse.add(item.id);+ ctx.rt.emittedWebSearchInput.set(item.id, input);queue.push({type: 'tool_use',data: {@@ -986,14 +990,23 @@ function handleWebSearch(// completedconst toolUseEmitted = ctx.rt.emittedToolUse.has(item.id);+ const emittedInput = ctx.rt.emittedWebSearchInput.get(item.id);ctx.rt.emittedToolUse.delete(item.id);ctx.rt.pendingWebSearchInput.delete(item.id);+ ctx.rt.emittedWebSearchInput.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);+ ctx.rt.emittedWebSearchInput.delete(item.id);+ } else if (input.query && JSON.stringify(input) !== JSON.stringify(emittedInput)) {+ // started/updated 用于实时展示;completed 可能补充权威 URL、pattern 或修正后的+ // query。沿用同一 toolUseId 补发,由 Desktop 持久层与 renderer 原位更新。+ emitWebSearchToolUse(item, input, queue, ctx);+ ctx.rt.emittedToolUse.delete(item.id);+ ctx.rt.emittedWebSearchInput.delete(item.id);}queue.push({type: 'tool_result_full',