Skip to content

Commit ea0fdc2

Browse files
authored
feat: enhance conversation loading with onLoaded callback and message sending logic (#232)
1 parent 10278cd commit ea0fdc2

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

docs/demos/examples/Assistant.vue

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,20 @@ const { messageManager, state, createConversation, updateTitle, switchConversati
448448
// preventDefault()
449449
console.log(data)
450450
},
451+
onLoaded: (conversations) => {
452+
console.log(conversations)
453+
},
451454
},
452455
})
453456
454-
const { messages, messageState, inputMessage, sendMessage, abortRequest } = messageManager
457+
const { messages, messageState, inputMessage, sendMessage: _sendMessage, abortRequest } = messageManager
458+
459+
const sendMessage = (...args: Parameters<typeof _sendMessage>) => {
460+
if (!state.currentId) {
461+
createConversation()
462+
}
463+
_sendMessage(...args)
464+
}
455465
456466
const handlePromptItemClick = (ev: unknown, item: { description?: string }) => {
457467
sendMessage(item.description)

docs/src/tools/conversation.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,19 @@ interface UseConversationOptions {
2727
useStreamByDefault?: boolean
2828
/** 错误消息模板 */
2929
errorMessage?: string
30-
events?: UseMessageOptions['events']
30+
/** 事件回调 */
31+
events?: UseConversationEvents
3132
}
3233
```
3334

35+
### 事件类型
36+
37+
```typescript
38+
type UseConversationEvents = UseMessageOptions['events'] & {
39+
/** 会话加载完成回调 */
40+
onLoaded?: (conversations: Conversation[]) => void
41+
}
42+
```
3443
3544
### 返回值
3645
@@ -41,15 +50,15 @@ interface UseConversationReturn {
4150
/** 消息管理 */
4251
messageManager: UseMessageReturn;
4352
/** 创建新会话 */
44-
createConversation: (title?: string, metadata?: Record<string, any>) => string;
53+
createConversation: (title?: string, metadata?: Record<string, unknown>) => string;
4554
/** 切换会话 */
4655
switchConversation: (id: string) => void;
4756
/** 删除会话 */
4857
deleteConversation: (id: string) => void;
4958
/** 更新会话标题 */
5059
updateTitle: (id: string, title: string) => void;
5160
/** 更新会话元数据 */
52-
updateMetadata: (id: string, metadata: Record<string, any>) => void;
61+
updateMetadata: (id: string, metadata: Record<string, unknown>) => void;
5362
/** 保存会话 */
5463
saveConversations: () => Promise<void>;
5564
/** 加载会话 */
@@ -88,7 +97,7 @@ interface Conversation {
8897
/** 更新时间 */
8998
updatedAt: number;
9099
/** 自定义元数据 */
91-
metadata?: Record<string, any>;
100+
metadata?: Record<string, unknown>;
92101
/** 消息 */
93102
messages: ChatMessage[];
94103
}

packages/kit/src/vue/conversation/useConversation.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ export interface ConversationState {
7777
loading: boolean
7878
}
7979

80+
export type UseConversationEvents = UseMessageOptions['events'] & {
81+
onLoaded?: (conversations: Conversation[]) => void
82+
}
83+
8084
/**
8185
* useConversation选项接口
8286
*/
@@ -93,7 +97,8 @@ export interface UseConversationOptions {
9397
useStreamByDefault?: boolean
9498
/** 错误消息模板 */
9599
errorMessage?: string
96-
events?: UseMessageOptions['events']
100+
/** 事件回调 */
101+
events?: UseConversationEvents
97102
}
98103

99104
/**
@@ -146,6 +151,7 @@ export function useConversation(options: UseConversationOptions): UseConversatio
146151
allowEmpty = false,
147152
useStreamByDefault = true,
148153
errorMessage = '请求失败,请稍后重试',
154+
events,
149155
} = options
150156

151157
// 会话状态
@@ -155,13 +161,19 @@ export function useConversation(options: UseConversationOptions): UseConversatio
155161
loading: false,
156162
})
157163

164+
// 标记是否已经触发过 onLoaded 回调
165+
let hasTriggeredOnLoaded = false
166+
158167
// 消息管理
159168
const messageManager = useMessage({
160169
client,
161170
useStreamByDefault,
162171
errorMessage,
163172
initialMessages: [],
164-
events: options.events,
173+
events: {
174+
onReceiveData: events?.onReceiveData,
175+
onFinish: events?.onFinish,
176+
},
165177
})
166178

167179
// 监听消息变化,自动更新会话
@@ -303,6 +315,12 @@ export function useConversation(options: UseConversationOptions): UseConversatio
303315
if (conversations.length > 0 && !state.currentId) {
304316
switchConversation(conversations[0].id)
305317
}
318+
319+
// 仅在第一次加载完成后触发 onLoaded 回调
320+
if (!hasTriggeredOnLoaded && events?.onLoaded) {
321+
hasTriggeredOnLoaded = true
322+
events.onLoaded(conversations)
323+
}
306324
} catch (error) {
307325
console.error('加载会话失败:', error)
308326
} finally {

0 commit comments

Comments
 (0)