fix: race condition in topic auto renaming

This commit is contained in:
one 2025-03-29 22:31:31 +08:00 committed by 亢奋猫
parent 464634d051
commit e5aaec2129

View File

@ -11,6 +11,8 @@ import { useEffect, useState } from 'react'
import { useAssistant } from './useAssistant' import { useAssistant } from './useAssistant'
import { getStoreSetting } from './useSettings' import { getStoreSetting } from './useSettings'
const renamingTopics = new Set<string>()
let _activeTopic: Topic let _activeTopic: Topic
let _setActiveTopic: (topic: Topic) => void let _setActiveTopic: (topic: Topic) => void
@ -54,6 +56,13 @@ export async function getTopicById(topicId: string) {
} }
export const autoRenameTopic = async (assistant: Assistant, topicId: string) => { export const autoRenameTopic = async (assistant: Assistant, topicId: string) => {
if (renamingTopics.has(topicId)) {
return
}
try {
renamingTopics.add(topicId)
const topic = await getTopicById(topicId) const topic = await getTopicById(topicId)
const enableTopicNaming = getStoreSetting('enableTopicNaming') const enableTopicNaming = getStoreSetting('enableTopicNaming')
@ -84,6 +93,9 @@ export const autoRenameTopic = async (assistant: Assistant, topicId: string) =>
store.dispatch(updateTopic({ assistantId: assistant.id, topic: data })) store.dispatch(updateTopic({ assistantId: assistant.id, topic: data }))
} }
} }
} finally {
renamingTopics.delete(topicId)
}
} }
// Convert class to object with functions since class only has static methods // Convert class to object with functions since class only has static methods