@quidgest/chatbot 0.5.7 → 0.5.9
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 +1261 -1216
- package/package.json +1 -1
- package/src/components/ChatBot/ChatBot.vue +44 -0
- package/src/composables/__tests__/useSSE.spec.ts +96 -0
- package/src/composables/useChatApi.ts +4 -0
- package/src/composables/useSSE.ts +13 -1
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)
|
|
@@ -129,4 +129,100 @@ describe('useSSE', () => {
|
|
|
129
129
|
expect(consoleWarnSpy).toHaveBeenCalledWith('Unknown event type: unknown_event')
|
|
130
130
|
consoleWarnSpy.mockRestore()
|
|
131
131
|
})
|
|
132
|
+
|
|
133
|
+
// Tests for handling events split across chunks (bug reproduction)
|
|
134
|
+
describe('Events split across multiple chunks', () => {
|
|
135
|
+
it('should handle event split in the middle of data field', async () => {
|
|
136
|
+
// Simulate network splitting an event across two chunks
|
|
137
|
+
const stream = createMockStream(['event: message\ndata: {"val', 'ue":"hello"}\n\n'])
|
|
138
|
+
mockedAxios.mockResolvedValue({ data: stream })
|
|
139
|
+
|
|
140
|
+
await useSSE({ url: '/sse' }, handlers)
|
|
141
|
+
|
|
142
|
+
expect(handlers.onMessage).toHaveBeenCalledWith('hello')
|
|
143
|
+
expect(handlers.onDone).toHaveBeenCalled()
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
it('should handle multiple events split across multiple chunks', async () => {
|
|
147
|
+
// Simulate the real scenario from the bug report
|
|
148
|
+
const stream = createMockStream([
|
|
149
|
+
'event: message\ndata: {"value":"The"}\n\nevent: message\ndata: {"val',
|
|
150
|
+
'ue":" capital"}\n\nevent: message\ndata: {"value":" of"}\n\n',
|
|
151
|
+
'event: message\ndata: {"value":" France"}\n\n'
|
|
152
|
+
])
|
|
153
|
+
mockedAxios.mockResolvedValue({ data: stream })
|
|
154
|
+
|
|
155
|
+
await useSSE({ url: '/sse' }, handlers)
|
|
156
|
+
|
|
157
|
+
// Should receive all 4 messages
|
|
158
|
+
expect(handlers.onMessage).toHaveBeenCalledTimes(4)
|
|
159
|
+
expect(handlers.onMessage).toHaveBeenNthCalledWith(1, 'The')
|
|
160
|
+
expect(handlers.onMessage).toHaveBeenNthCalledWith(2, ' capital')
|
|
161
|
+
expect(handlers.onMessage).toHaveBeenNthCalledWith(3, ' of')
|
|
162
|
+
expect(handlers.onMessage).toHaveBeenNthCalledWith(4, ' France')
|
|
163
|
+
expect(handlers.onDone).toHaveBeenCalled()
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('should handle event split between event name and data', async () => {
|
|
167
|
+
const stream = createMockStream([
|
|
168
|
+
'event: message\n',
|
|
169
|
+
'data: {"value":"split event"}\n\n'
|
|
170
|
+
])
|
|
171
|
+
mockedAxios.mockResolvedValue({ data: stream })
|
|
172
|
+
|
|
173
|
+
await useSSE({ url: '/sse' }, handlers)
|
|
174
|
+
|
|
175
|
+
expect(handlers.onMessage).toHaveBeenCalledWith('split event')
|
|
176
|
+
expect(handlers.onDone).toHaveBeenCalled()
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
it('should handle event split right after event separator', async () => {
|
|
180
|
+
const stream = createMockStream([
|
|
181
|
+
'event: message\ndata: {"value":"first"}\n\n',
|
|
182
|
+
'event: message\ndata: {"value":"second"}\n\n'
|
|
183
|
+
])
|
|
184
|
+
mockedAxios.mockResolvedValue({ data: stream })
|
|
185
|
+
|
|
186
|
+
await useSSE({ url: '/sse' }, handlers)
|
|
187
|
+
|
|
188
|
+
expect(handlers.onMessage).toHaveBeenCalledTimes(2)
|
|
189
|
+
expect(handlers.onMessage).toHaveBeenNthCalledWith(1, 'first')
|
|
190
|
+
expect(handlers.onMessage).toHaveBeenNthCalledWith(2, 'second')
|
|
191
|
+
expect(handlers.onDone).toHaveBeenCalled()
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
it('should handle complex JSON split across chunks', async () => {
|
|
195
|
+
const stream = createMockStream([
|
|
196
|
+
'event: field_metadata\ndata: {"foo":"bar","nested":{"key',
|
|
197
|
+
'":"value"}}\n\n'
|
|
198
|
+
])
|
|
199
|
+
mockedAxios.mockResolvedValue({ data: stream })
|
|
200
|
+
|
|
201
|
+
await useSSE({ url: '/sse' }, handlers)
|
|
202
|
+
|
|
203
|
+
expect(handlers.onFieldMetadata).toHaveBeenCalledWith({
|
|
204
|
+
foo: 'bar',
|
|
205
|
+
nested: { key: 'value' }
|
|
206
|
+
})
|
|
207
|
+
expect(handlers.onDone).toHaveBeenCalled()
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
it('should handle event split in very small chunks', async () => {
|
|
211
|
+
// Extreme case: each character in a separate chunk
|
|
212
|
+
const stream = createMockStream([
|
|
213
|
+
'event: ',
|
|
214
|
+
'message\n',
|
|
215
|
+
'data: ',
|
|
216
|
+
'{"value":',
|
|
217
|
+
'"test"}',
|
|
218
|
+
'\n\n'
|
|
219
|
+
])
|
|
220
|
+
mockedAxios.mockResolvedValue({ data: stream })
|
|
221
|
+
|
|
222
|
+
await useSSE({ url: '/sse' }, handlers)
|
|
223
|
+
|
|
224
|
+
expect(handlers.onMessage).toHaveBeenCalledWith('test')
|
|
225
|
+
expect(handlers.onDone).toHaveBeenCalled()
|
|
226
|
+
})
|
|
227
|
+
})
|
|
132
228
|
})
|
|
@@ -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
|
|
@@ -29,6 +30,7 @@ export async function useSSE(config: AxiosRequestConfig, handlers: SSEvents) {
|
|
|
29
30
|
|
|
30
31
|
const reader = stream.getReader()
|
|
31
32
|
const decoder = new TextDecoder()
|
|
33
|
+
let buffer = '' // Buffer to accumulate incomplete events across chunks
|
|
32
34
|
|
|
33
35
|
while (true) {
|
|
34
36
|
const { done, value } = await reader.read()
|
|
@@ -38,7 +40,12 @@ export async function useSSE(config: AxiosRequestConfig, handlers: SSEvents) {
|
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
const chunk = decoder.decode(value, { stream: true })
|
|
41
|
-
|
|
43
|
+
buffer += chunk // Append chunk to buffer
|
|
44
|
+
const events = buffer.split(/\n\n+/)
|
|
45
|
+
|
|
46
|
+
// Keep the last element in buffer (might be incomplete)
|
|
47
|
+
// Process all complete events (all except the last one)
|
|
48
|
+
buffer = events.pop() || ''
|
|
42
49
|
|
|
43
50
|
for (const eventBlock of events) {
|
|
44
51
|
const lines = eventBlock.trim().split('\n')
|
|
@@ -64,6 +71,11 @@ export async function useSSE(config: AxiosRequestConfig, handlers: SSEvents) {
|
|
|
64
71
|
case 'message':
|
|
65
72
|
handlers.onMessage?.(parsedData.value)
|
|
66
73
|
break
|
|
74
|
+
case 'tool-status':
|
|
75
|
+
handlers.onToolStatus?.(
|
|
76
|
+
parsedData as { event: string; data: { id: string; html: string } }
|
|
77
|
+
)
|
|
78
|
+
break
|
|
67
79
|
case 'error':
|
|
68
80
|
handlers.onError?.(new Error(parsedData.value))
|
|
69
81
|
break
|