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.
This commit is contained in:
SuYao 2025-07-31 09:50:24 +08:00 committed by GitHub
parent 80409cd94e
commit 7ae7f13ad1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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)