From 7ae7f13ad1f66620a24289b24bb676d920a29b96 Mon Sep 17 00:00:00 2001 From: SuYao Date: Thu, 31 Jul 2025 09:50:24 +0800 Subject: [PATCH] refactor(ApiService): optimize memory search handling and improve error logging (#8671) * refactor(ApiService): optimize memory search handling and improve error logging - Updated fetchExternalTool to handle memory search in a separate promise, ensuring better management of asynchronous operations. - Enhanced error logging for memory search failures to improve debugging. - Removed redundant memory search result storage logic from the main result handling section. * refactor(ApiService): streamline external tool fetching and memory search handling - Simplified the fetchExternalTool function by separating web and knowledge search calls into individual await statements. - Improved memory search handling by awaiting the searchMemory function directly, ensuring better asynchronous flow. - Updated result storage logic to ensure memory search results are stored correctly after fetching. --- src/renderer/src/services/ApiService.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/renderer/src/services/ApiService.ts b/src/renderer/src/services/ApiService.ts index 841d9de934..8976780449 100644 --- a/src/renderer/src/services/ApiService.ts +++ b/src/renderer/src/services/ApiService.ts @@ -312,16 +312,18 @@ async function fetchExternalTool( let memorySearchReferences: MemoryItem[] | undefined const parentSpanId = currentSpan(lastUserMessage.topicId, assistant.model?.name)?.spanContext().spanId - // 并行执行搜索 - if (shouldWebSearch || shouldKnowledgeSearch || shouldSearchMemory) { - ;[webSearchResponseFromSearch, knowledgeReferencesFromSearch, memorySearchReferences] = await Promise.all([ - searchTheWeb(extractResults, parentSpanId), - searchKnowledgeBase(extractResults, parentSpanId, assistant.model?.name), - searchMemory() - ]) + if (shouldWebSearch) { + webSearchResponseFromSearch = await searchTheWeb(extractResults, parentSpanId) + } + + if (shouldKnowledgeSearch) { + knowledgeReferencesFromSearch = await searchKnowledgeBase(extractResults, parentSpanId, assistant.model?.name) + } + + if (shouldSearchMemory) { + memorySearchReferences = await searchMemory() } - // 存储搜索结果 if (lastUserMessage) { if (webSearchResponseFromSearch) { window.keyv.set(`web-search-${lastUserMessage.id}`, webSearchResponseFromSearch) @@ -492,7 +494,7 @@ export async function fetchChatCompletion({ // Post-conversation memory processing const globalMemoryEnabled = selectGlobalMemoryEnabled(store.getState()) if (globalMemoryEnabled && assistant.enableMemory) { - await processConversationMemory(messages, assistant) + processConversationMemory(messages, assistant) } return await AI.completionsForTrace(completionsParams, requestOptions)