This commit is contained in:
duanfuxiang
2025-06-15 19:02:22 +08:00
parent d4776405ba
commit 47e0962f4b
5 changed files with 347 additions and 63 deletions

View File

@@ -26,10 +26,7 @@ export function useChatHistory(): UseChatHistory {
const [chatList, setChatList] = useState<ChatConversationMeta[]>([])
const fetchChatList = useCallback(async () => {
console.log('useChatHistory - fetching chat list...')
const conversations = await chatManager.listChats()
console.log('useChatHistory - fetched conversations:', conversations)
console.log('useChatHistory - conversations length:', conversations.length)
setChatList(conversations)
}, [chatManager])
@@ -41,21 +38,17 @@ export function useChatHistory(): UseChatHistory {
() =>
debounce(
async (id: string, messages: ChatMessage[]): Promise<void> => {
console.log('useChatHistory - createOrUpdateConversation called with id:', id, 'messages length:', messages.length)
const serializedMessages = messages.map(serializeChatMessage)
const existingConversation = await chatManager.findById(id)
if (existingConversation) {
console.log('useChatHistory - updating existing conversation:', existingConversation.id)
if (isEqual(existingConversation.messages, serializedMessages)) {
console.log('useChatHistory - messages are identical, skipping update')
return
}
await chatManager.updateChat(existingConversation.id, {
messages: serializedMessages,
})
} else {
console.log('useChatHistory - creating new conversation')
const firstUserMessage = messages.find((v) => v.role === 'user') as ChatUserMessage
const newChat = await chatManager.createChat({
@@ -68,10 +61,8 @@ export function useChatHistory(): UseChatHistory {
: 'New chat',
messages: serializedMessages,
})
console.log('useChatHistory - created new chat:', newChat)
}
console.log('useChatHistory - refreshing chat list after create/update')
await fetchChatList()
},
300,