@quidgest/chatbot 0.5.7 → 0.5.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/composables/useChatApi.d.ts +7 -1
- package/dist/composables/useSSE.d.ts +7 -0
- package/dist/index.js +18 -12
- package/dist/index.mjs +1239 -1198
- package/package.json +1 -1
- package/src/components/ChatBot/ChatBot.vue +44 -0
- package/src/composables/useChatApi.ts +4 -0
- package/src/composables/useSSE.ts +6 -0
package/package.json
CHANGED
|
@@ -256,11 +256,55 @@
|
|
|
256
256
|
const msg = getLastMessage()
|
|
257
257
|
if (!msg) return
|
|
258
258
|
|
|
259
|
+
const pending = new Map<string, { html: string; startedAt: number }>()
|
|
260
|
+
const MIN_VISIBLE_MS = 1000
|
|
261
|
+
|
|
259
262
|
await sendPrompt(
|
|
260
263
|
formData,
|
|
261
264
|
(chunk) => {
|
|
262
265
|
if (msg) msg.message += chunk
|
|
263
266
|
},
|
|
267
|
+
(payload) => {
|
|
268
|
+
if (!msg || !payload) return
|
|
269
|
+
const { event: kind, data } = payload
|
|
270
|
+
if (!data || !data.id) return
|
|
271
|
+
|
|
272
|
+
const appendToolMessage = (html: string) => {
|
|
273
|
+
if (msg.message && !msg.message.endsWith('\n\n')) msg.message += '\n\n'
|
|
274
|
+
msg.message += `${html}\n\n`
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const applyReplace = (start: { html: string; startedAt: number }) => {
|
|
278
|
+
msg.message = msg.message.replace(start.html, data.html)
|
|
279
|
+
pending.delete(data.id)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
switch (kind) {
|
|
283
|
+
case 'tool_start': {
|
|
284
|
+
pending.set(data.id, { html: data.html, startedAt: Date.now() })
|
|
285
|
+
appendToolMessage(data.html)
|
|
286
|
+
break
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
case 'tool_end': {
|
|
290
|
+
const start = pending.get(data.id)
|
|
291
|
+
|
|
292
|
+
if (start) {
|
|
293
|
+
const elapsed = Date.now() - start.startedAt
|
|
294
|
+
const wait = Math.max(0, MIN_VISIBLE_MS - elapsed)
|
|
295
|
+
if (wait > 0) setTimeout(() => applyReplace(start), wait)
|
|
296
|
+
else applyReplace(start)
|
|
297
|
+
} else {
|
|
298
|
+
appendToolMessage(data.html)
|
|
299
|
+
}
|
|
300
|
+
break
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
default:
|
|
304
|
+
// Ignore unknown events
|
|
305
|
+
break
|
|
306
|
+
}
|
|
307
|
+
},
|
|
264
308
|
(error) => {
|
|
265
309
|
setDisabledState(true)
|
|
266
310
|
addChatMessage(texts.botIsSick)
|
|
@@ -59,6 +59,7 @@ export function useChatApi(apiEndpoint: string) {
|
|
|
59
59
|
async function sendPrompt(
|
|
60
60
|
formData: FormData,
|
|
61
61
|
onChunk: (chunk: string) => void,
|
|
62
|
+
onStatus?: (payload: { event: string; data: { id: string; html: string } }) => void,
|
|
62
63
|
onError?: (error: Error) => void
|
|
63
64
|
) {
|
|
64
65
|
isLoading.value = true
|
|
@@ -73,6 +74,9 @@ export function useChatApi(apiEndpoint: string) {
|
|
|
73
74
|
onMessage: (data) => {
|
|
74
75
|
onChunk(data)
|
|
75
76
|
},
|
|
77
|
+
onToolStatus: (payload) => {
|
|
78
|
+
onStatus?.(payload)
|
|
79
|
+
},
|
|
76
80
|
onDone: () => (isLoading.value = false)
|
|
77
81
|
}
|
|
78
82
|
)
|
|
@@ -2,6 +2,7 @@ import axios, { AxiosRequestConfig } from 'axios'
|
|
|
2
2
|
|
|
3
3
|
export type SSEvents = {
|
|
4
4
|
onMessage?: (message: string) => void
|
|
5
|
+
onToolStatus?: (payload: { event: string; data: { id: string; html: string } }) => void
|
|
5
6
|
onError?: (error: Error) => void
|
|
6
7
|
onFieldMetadata?: (metadata: Record<string, unknown>) => void
|
|
7
8
|
onDone?: () => void
|
|
@@ -64,6 +65,11 @@ export async function useSSE(config: AxiosRequestConfig, handlers: SSEvents) {
|
|
|
64
65
|
case 'message':
|
|
65
66
|
handlers.onMessage?.(parsedData.value)
|
|
66
67
|
break
|
|
68
|
+
case 'tool-status':
|
|
69
|
+
handlers.onToolStatus?.(
|
|
70
|
+
parsedData as { event: string; data: { id: string; html: string } }
|
|
71
|
+
)
|
|
72
|
+
break
|
|
67
73
|
case 'error':
|
|
68
74
|
handlers.onError?.(new Error(parsedData.value))
|
|
69
75
|
break
|