Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/kit/src/vue/conversation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export interface UseConversationOptions {
* When provided, conversation list and messages can be loaded and persisted.
*/
storage?: ConversationStorageStrategy
/**
* Called when the initial conversation list has been loaded from storage.
* Only fired when storage.loadConversations is available and has completed.
*/
onLoad?: (conversations: ConversationInfo[]) => void
}

export interface UseConversationReturn {
Expand Down
12 changes: 9 additions & 3 deletions packages/kit/src/vue/conversation/useConversation.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { computed, ref, watch, WatchStopHandle } from 'vue'
import { localStorageStrategyFactory } from '../../storage/factories'
import { ChatMessage } from '../../types'
import { UseMessageOptions, UseMessageReturn } from '../message/types'
import { useMessage } from '../message/useMessage'
import { Conversation, ConversationInfo, UseConversationOptions, UseConversationReturn } from './types'
import { useThrottleFn } from './useThrottleFn'
import { localStorageStrategyFactory } from '../../storage/factories'

export const useConversation = (options: UseConversationOptions): UseConversationReturn => {
// 如果没有提供存储策略,使用默认的 LocalStorage 策略
Expand Down Expand Up @@ -117,12 +117,14 @@ export const useConversation = (options: UseConversationOptions): UseConversatio
Promise.resolve(storage.loadConversations())
.then((list) => {
// 如果加载的列表为空,直接返回
if (!list?.length) return
if (!list?.length) {
return []
}

// 如果当前内存中的会话列表为空,直接使用加载的列表
if (conversations.value.length === 0) {
conversations.value = list
return
return conversations.value
}

// 合并策略:内存数据优先于存储数据
Expand All @@ -140,6 +142,10 @@ export const useConversation = (options: UseConversationOptions): UseConversatio
// 确保 activeConversation 对应的会话在合并后的列表中
// 如果 activeConversationId 存在但对应的会话不在列表中,说明可能被意外删除
// 这种情况下,activeConversation 会自动变为 null(通过 computed 属性)
return conversations.value
})
.then((loadedList) => {
options.onLoad?.(loadedList)
})
.catch((error) => {
console.error('[useConversation] loadConversations failed:', error)
Expand Down
Loading