From 94b0e2b7ecf1bdcbffe86099d4ab26283284c12e Mon Sep 17 00:00:00 2001 From: gene9831 Date: Fri, 19 Sep 2025 11:19:05 +0800 Subject: [PATCH] feat: enhance conversation loading with onLoaded callback and message sending logic - Added onLoaded callback to handle conversation loading events in useConversation. - Improved sendMessage function to create a conversation if none exists before sending a message. - Updated Assistant demo to log loaded conversations for better debugging. --- docs/demos/examples/Assistant.vue | 12 +++++++++- docs/src/tools/conversation.md | 17 ++++++++++---- .../src/vue/conversation/useConversation.ts | 22 +++++++++++++++++-- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/docs/demos/examples/Assistant.vue b/docs/demos/examples/Assistant.vue index bb6f66b9..0351bf57 100644 --- a/docs/demos/examples/Assistant.vue +++ b/docs/demos/examples/Assistant.vue @@ -448,10 +448,20 @@ const { messageManager, state, createConversation, updateTitle, switchConversati // preventDefault() console.log(data) }, + onLoaded: (conversations) => { + console.log(conversations) + }, }, }) -const { messages, messageState, inputMessage, sendMessage, abortRequest } = messageManager +const { messages, messageState, inputMessage, sendMessage: _sendMessage, abortRequest } = messageManager + +const sendMessage = (...args: Parameters) => { + if (!state.currentId) { + createConversation() + } + _sendMessage(...args) +} const handlePromptItemClick = (ev: unknown, item: { description?: string }) => { sendMessage(item.description) diff --git a/docs/src/tools/conversation.md b/docs/src/tools/conversation.md index 08c548c2..c608ba92 100644 --- a/docs/src/tools/conversation.md +++ b/docs/src/tools/conversation.md @@ -27,10 +27,19 @@ interface UseConversationOptions { useStreamByDefault?: boolean /** 错误消息模板 */ errorMessage?: string - events?: UseMessageOptions['events'] + /** 事件回调 */ + events?: UseConversationEvents } ``` +### 事件类型 + +```typescript +type UseConversationEvents = UseMessageOptions['events'] & { + /** 会话加载完成回调 */ + onLoaded?: (conversations: Conversation[]) => void +} +``` ### 返回值 @@ -41,7 +50,7 @@ interface UseConversationReturn { /** 消息管理 */ messageManager: UseMessageReturn; /** 创建新会话 */ - createConversation: (title?: string, metadata?: Record) => string; + createConversation: (title?: string, metadata?: Record) => string; /** 切换会话 */ switchConversation: (id: string) => void; /** 删除会话 */ @@ -49,7 +58,7 @@ interface UseConversationReturn { /** 更新会话标题 */ updateTitle: (id: string, title: string) => void; /** 更新会话元数据 */ - updateMetadata: (id: string, metadata: Record) => void; + updateMetadata: (id: string, metadata: Record) => void; /** 保存会话 */ saveConversations: () => Promise; /** 加载会话 */ @@ -88,7 +97,7 @@ interface Conversation { /** 更新时间 */ updatedAt: number; /** 自定义元数据 */ - metadata?: Record; + metadata?: Record; /** 消息 */ messages: ChatMessage[]; } diff --git a/packages/kit/src/vue/conversation/useConversation.ts b/packages/kit/src/vue/conversation/useConversation.ts index 1415b8dd..9a5e8f3a 100644 --- a/packages/kit/src/vue/conversation/useConversation.ts +++ b/packages/kit/src/vue/conversation/useConversation.ts @@ -77,6 +77,10 @@ export interface ConversationState { loading: boolean } +export type UseConversationEvents = UseMessageOptions['events'] & { + onLoaded?: (conversations: Conversation[]) => void +} + /** * useConversation选项接口 */ @@ -93,7 +97,8 @@ export interface UseConversationOptions { useStreamByDefault?: boolean /** 错误消息模板 */ errorMessage?: string - events?: UseMessageOptions['events'] + /** 事件回调 */ + events?: UseConversationEvents } /** @@ -146,6 +151,7 @@ export function useConversation(options: UseConversationOptions): UseConversatio allowEmpty = false, useStreamByDefault = true, errorMessage = '请求失败,请稍后重试', + events, } = options // 会话状态 @@ -155,13 +161,19 @@ export function useConversation(options: UseConversationOptions): UseConversatio loading: false, }) + // 标记是否已经触发过 onLoaded 回调 + let hasTriggeredOnLoaded = false + // 消息管理 const messageManager = useMessage({ client, useStreamByDefault, errorMessage, initialMessages: [], - events: options.events, + events: { + onReceiveData: events?.onReceiveData, + onFinish: events?.onFinish, + }, }) // 监听消息变化,自动更新会话 @@ -303,6 +315,12 @@ export function useConversation(options: UseConversationOptions): UseConversatio if (conversations.length > 0 && !state.currentId) { switchConversation(conversations[0].id) } + + // 仅在第一次加载完成后触发 onLoaded 回调 + if (!hasTriggeredOnLoaded && events?.onLoaded) { + hasTriggeredOnLoaded = true + events.onLoaded(conversations) + } } catch (error) { console.error('加载会话失败:', error) } finally {