@jazzmind/busibox-app 3.0.38 → 3.0.39
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/components/chat/ChatContainer.d.ts.map +1 -1
- package/dist/components/chat/ChatContainer.js +122 -602
- package/dist/components/chat/ChatContainer.js.map +1 -1
- package/dist/components/chat/ChatInterface.d.ts +0 -1
- package/dist/components/chat/ChatInterface.d.ts.map +1 -1
- package/dist/components/chat/ChatInterface.js +159 -302
- package/dist/components/chat/ChatInterface.js.map +1 -1
- package/dist/components/chat/MessageList.d.ts +2 -1
- package/dist/components/chat/MessageList.d.ts.map +1 -1
- package/dist/components/chat/MessageList.js +33 -17
- package/dist/components/chat/MessageList.js.map +1 -1
- package/dist/components/chat/ThinkingToggle.d.ts.map +1 -1
- package/dist/components/chat/ThinkingToggle.js +15 -1
- package/dist/components/chat/ThinkingToggle.js.map +1 -1
- package/dist/lib/agent/agent-api-base.d.ts.map +1 -1
- package/dist/lib/agent/agent-api-base.js +0 -1
- package/dist/lib/agent/agent-api-base.js.map +1 -1
- package/dist/lib/agent/chat-client.d.ts.map +1 -1
- package/dist/lib/agent/chat-client.js +0 -1
- package/dist/lib/agent/chat-client.js.map +1 -1
- package/dist/lib/agent/index.d.ts +2 -0
- package/dist/lib/agent/index.d.ts.map +1 -1
- package/dist/lib/agent/index.js +2 -0
- package/dist/lib/agent/index.js.map +1 -1
- package/dist/lib/agent/stream-event-processor.d.ts +37 -0
- package/dist/lib/agent/stream-event-processor.d.ts.map +1 -0
- package/dist/lib/agent/stream-event-processor.js +204 -0
- package/dist/lib/agent/stream-event-processor.js.map +1 -0
- package/dist/lib/hooks/useChatStream.d.ts.map +1 -1
- package/dist/lib/hooks/useChatStream.js +27 -173
- package/dist/lib/hooks/useChatStream.js.map +1 -1
- package/package.json +1 -1
package/dist/lib/agent/index.js
CHANGED
|
@@ -7,6 +7,8 @@ export { getAgentApiUrl, buildAuthHeaders, getAgentApiToken, agentApiFetch, agen
|
|
|
7
7
|
export { createAgentClient } from './agent-service-client';
|
|
8
8
|
// Standalone agent sync helpers (no client factory needed)
|
|
9
9
|
export { syncAgentDefinitions, getAgentSyncStatus } from './sync';
|
|
10
|
+
// Stream event processor (shared between ChatInterface and useChatStream)
|
|
11
|
+
export { createAccumulator, processStreamEvent, } from './stream-event-processor';
|
|
10
12
|
// Enhanced chat client (browser-side)
|
|
11
13
|
export { sendChatMessage, streamChatMessage, getAvailableModels, getConversationHistory, generateInsights, getConversations, createConversation, updateConversation, deleteConversation, searchInsights, insertInsights, deleteConversationInsights, getInsightStats, initializeInsights, } from './chat-client';
|
|
12
14
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/agent/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,kEAAkE;AAClE,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,gBAAgB,EAChB,aAAa,EACb,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAM1B,qDAAqD;AACrD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAY3D,2DAA2D;AAC3D,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,MAAM,QAAQ,CAAC;AAElE,sCAAsC;AACtC,OAAO,EACL,eAAe,EACf,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,gBAAgB,EAChB,gBAAgB,EAChB,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,cAAc,EACd,cAAc,EACd,0BAA0B,EAC1B,eAAe,EACf,kBAAkB,GACnB,MAAM,eAAe,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/agent/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,kEAAkE;AAClE,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,gBAAgB,EAChB,aAAa,EACb,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAM1B,qDAAqD;AACrD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAY3D,2DAA2D;AAC3D,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,MAAM,QAAQ,CAAC;AAElE,0EAA0E;AAC1E,OAAO,EACL,iBAAiB,EACjB,kBAAkB,GACnB,MAAM,0BAA0B,CAAC;AAOlC,sCAAsC;AACtC,OAAO,EACL,eAAe,EACf,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,gBAAgB,EAChB,gBAAgB,EAChB,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,cAAc,EACd,cAAc,EACd,0BAA0B,EAC1B,eAAe,EACf,kBAAkB,GACnB,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { ThoughtEvent } from '../../components/chat/ThinkingToggle';
|
|
2
|
+
import type { MessagePart } from '../../types/chat';
|
|
3
|
+
export interface StreamAccumulator {
|
|
4
|
+
fullContent: string;
|
|
5
|
+
thoughts: ThoughtEvent[];
|
|
6
|
+
parts: MessagePart[];
|
|
7
|
+
pendingTools: Map<string, number>;
|
|
8
|
+
agentName?: string;
|
|
9
|
+
interimMessages: string[];
|
|
10
|
+
}
|
|
11
|
+
export interface StreamEventResult {
|
|
12
|
+
content?: string;
|
|
13
|
+
thoughts?: ThoughtEvent[];
|
|
14
|
+
parts?: MessagePart[];
|
|
15
|
+
agentName?: string;
|
|
16
|
+
interimMessages?: string[];
|
|
17
|
+
quickReplies?: string[];
|
|
18
|
+
promptActive?: boolean;
|
|
19
|
+
conversationId?: string;
|
|
20
|
+
title?: string;
|
|
21
|
+
titleUpdate?: {
|
|
22
|
+
id: string;
|
|
23
|
+
title: string;
|
|
24
|
+
};
|
|
25
|
+
complete?: boolean;
|
|
26
|
+
error?: string;
|
|
27
|
+
}
|
|
28
|
+
export declare function createAccumulator(): StreamAccumulator;
|
|
29
|
+
/**
|
|
30
|
+
* Pure function that processes a single SSE stream event, mutating the
|
|
31
|
+
* accumulator in place and returning a result object describing what changed.
|
|
32
|
+
*
|
|
33
|
+
* Both ChatInterface and useChatStream call this so streaming behaviour
|
|
34
|
+
* (including live thinking extraction) is defined in exactly one place.
|
|
35
|
+
*/
|
|
36
|
+
export declare function processStreamEvent(eventType: string, parsed: any, accumulated: StreamAccumulator): StreamEventResult;
|
|
37
|
+
//# sourceMappingURL=stream-event-processor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-event-processor.d.ts","sourceRoot":"","sources":["../../../src/lib/agent/stream-event-processor.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sCAAsC,CAAC;AACzE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAEpD,MAAM,WAAW,iBAAiB;IAChC,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,YAAY,EAAE,CAAC;IACzB,KAAK,EAAE,WAAW,EAAE,CAAC;IACrB,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAClC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,eAAe,EAAE,MAAM,EAAE,CAAC;CAC3B;AAED,MAAM,WAAW,iBAAiB;IAChC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,YAAY,EAAE,CAAC;IAC1B,KAAK,CAAC,EAAE,WAAW,EAAE,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;IAC3B,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IAC5C,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,wBAAgB,iBAAiB,IAAI,iBAAiB,CASrD;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,GAAG,EACX,WAAW,EAAE,iBAAiB,GAC7B,iBAAiB,CA6MnB"}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { stripThinkTags, extractThinkContent } from '../../components/chat/chat-utils';
|
|
2
|
+
export function createAccumulator() {
|
|
3
|
+
return {
|
|
4
|
+
fullContent: '',
|
|
5
|
+
thoughts: [],
|
|
6
|
+
parts: [],
|
|
7
|
+
pendingTools: new Map(),
|
|
8
|
+
agentName: undefined,
|
|
9
|
+
interimMessages: [],
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Pure function that processes a single SSE stream event, mutating the
|
|
14
|
+
* accumulator in place and returning a result object describing what changed.
|
|
15
|
+
*
|
|
16
|
+
* Both ChatInterface and useChatStream call this so streaming behaviour
|
|
17
|
+
* (including live thinking extraction) is defined in exactly one place.
|
|
18
|
+
*/
|
|
19
|
+
export function processStreamEvent(eventType, parsed, accumulated) {
|
|
20
|
+
const newThought = {
|
|
21
|
+
type: eventType,
|
|
22
|
+
source: parsed.source,
|
|
23
|
+
message: parsed.message,
|
|
24
|
+
data: parsed.data || parsed,
|
|
25
|
+
timestamp: new Date(),
|
|
26
|
+
};
|
|
27
|
+
if (parsed.source && !parsed.source.includes('dispatcher')) {
|
|
28
|
+
accumulated.agentName = parsed.source;
|
|
29
|
+
}
|
|
30
|
+
switch (eventType) {
|
|
31
|
+
case 'conversation_created':
|
|
32
|
+
return { conversationId: parsed.conversation_id, title: parsed.title };
|
|
33
|
+
case 'title_update':
|
|
34
|
+
return { titleUpdate: { id: parsed.conversation_id, title: parsed.title } };
|
|
35
|
+
case 'thought':
|
|
36
|
+
case 'plan':
|
|
37
|
+
case 'progress':
|
|
38
|
+
accumulated.thoughts = [...accumulated.thoughts, newThought];
|
|
39
|
+
return { thoughts: accumulated.thoughts };
|
|
40
|
+
case 'tool_start': {
|
|
41
|
+
accumulated.thoughts = [...accumulated.thoughts, newThought];
|
|
42
|
+
const toolSource = parsed.source || 'tool';
|
|
43
|
+
const toolName = String(parsed.data?.tool_name || parsed.data?.display_name || toolSource);
|
|
44
|
+
const toolPart = {
|
|
45
|
+
type: 'tool_call',
|
|
46
|
+
id: `tool-${Date.now()}-${toolName}`,
|
|
47
|
+
name: toolName,
|
|
48
|
+
displayName: String(parsed.data?.display_name || parsed.message || toolName),
|
|
49
|
+
status: 'running',
|
|
50
|
+
input: (parsed.data || undefined),
|
|
51
|
+
startedAt: new Date(),
|
|
52
|
+
};
|
|
53
|
+
accumulated.pendingTools.set(toolSource, accumulated.parts.length);
|
|
54
|
+
accumulated.parts = [...accumulated.parts, toolPart];
|
|
55
|
+
return { thoughts: accumulated.thoughts, parts: accumulated.parts };
|
|
56
|
+
}
|
|
57
|
+
case 'tool_result': {
|
|
58
|
+
accumulated.thoughts = [...accumulated.thoughts, newThought];
|
|
59
|
+
const resultSource = parsed.source || 'tool';
|
|
60
|
+
const idx = accumulated.pendingTools.get(resultSource);
|
|
61
|
+
if (idx !== undefined && accumulated.parts[idx]?.type === 'tool_call') {
|
|
62
|
+
const existing = accumulated.parts[idx];
|
|
63
|
+
accumulated.parts = [...accumulated.parts];
|
|
64
|
+
accumulated.parts[idx] = {
|
|
65
|
+
...existing,
|
|
66
|
+
status: parsed.data?.success === false ? 'error' : 'completed',
|
|
67
|
+
output: parsed.message || undefined,
|
|
68
|
+
error: parsed.data?.success === false ? String(parsed.message || 'Failed') : undefined,
|
|
69
|
+
completedAt: new Date(),
|
|
70
|
+
};
|
|
71
|
+
accumulated.pendingTools.delete(resultSource);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
const toolName = String(parsed.data?.tool_name || parsed.data?.display_name || resultSource);
|
|
75
|
+
accumulated.parts = [...accumulated.parts, {
|
|
76
|
+
type: 'tool_call',
|
|
77
|
+
id: `tool-${Date.now()}-${toolName}`,
|
|
78
|
+
name: toolName,
|
|
79
|
+
displayName: String(parsed.data?.display_name || toolName),
|
|
80
|
+
status: parsed.data?.success === false ? 'error' : 'completed',
|
|
81
|
+
output: parsed.message || undefined,
|
|
82
|
+
completedAt: new Date(),
|
|
83
|
+
}];
|
|
84
|
+
}
|
|
85
|
+
return { thoughts: accumulated.thoughts, parts: accumulated.parts };
|
|
86
|
+
}
|
|
87
|
+
case 'content': {
|
|
88
|
+
const contentData = parsed.data || {};
|
|
89
|
+
const msgText = parsed.message || '';
|
|
90
|
+
if (contentData.streaming && contentData.partial) {
|
|
91
|
+
accumulated.fullContent += msgText;
|
|
92
|
+
}
|
|
93
|
+
else if (contentData.complete) {
|
|
94
|
+
// Final marker — no content change
|
|
95
|
+
}
|
|
96
|
+
else if (msgText) {
|
|
97
|
+
accumulated.fullContent = msgText;
|
|
98
|
+
}
|
|
99
|
+
const thinkTexts = extractThinkContent(accumulated.fullContent);
|
|
100
|
+
if (thinkTexts.length > 0) {
|
|
101
|
+
const thinkThought = {
|
|
102
|
+
type: 'thought',
|
|
103
|
+
source: 'model',
|
|
104
|
+
message: thinkTexts.join('\n\n'),
|
|
105
|
+
data: parsed.data || parsed,
|
|
106
|
+
timestamp: new Date(),
|
|
107
|
+
};
|
|
108
|
+
const hasModelThought = accumulated.thoughts.some(t => t.source === 'model' && t.type === 'thought');
|
|
109
|
+
if (hasModelThought) {
|
|
110
|
+
accumulated.thoughts = accumulated.thoughts.map(t => t.source === 'model' && t.type === 'thought' ? thinkThought : t);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
accumulated.thoughts = [...accumulated.thoughts, thinkThought];
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
content: stripThinkTags(accumulated.fullContent),
|
|
118
|
+
thoughts: accumulated.thoughts,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
case 'content_chunk': {
|
|
122
|
+
accumulated.fullContent += parsed.chunk || parsed.content || '';
|
|
123
|
+
const thinkTexts = extractThinkContent(accumulated.fullContent);
|
|
124
|
+
if (thinkTexts.length > 0) {
|
|
125
|
+
const thinkThought = {
|
|
126
|
+
type: 'thought',
|
|
127
|
+
source: 'model',
|
|
128
|
+
message: thinkTexts.join('\n\n'),
|
|
129
|
+
data: parsed.data || parsed,
|
|
130
|
+
timestamp: new Date(),
|
|
131
|
+
};
|
|
132
|
+
const hasModelThought = accumulated.thoughts.some(t => t.source === 'model' && t.type === 'thought');
|
|
133
|
+
if (hasModelThought) {
|
|
134
|
+
accumulated.thoughts = accumulated.thoughts.map(t => t.source === 'model' && t.type === 'thought' ? thinkThought : t);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
accumulated.thoughts = [...accumulated.thoughts, thinkThought];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
content: stripThinkTags(accumulated.fullContent),
|
|
142
|
+
thoughts: accumulated.thoughts,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
case 'interim': {
|
|
146
|
+
const nested = parsed.data || {};
|
|
147
|
+
const interimMsg = String(parsed.message || '').trim();
|
|
148
|
+
const audioUrl = typeof nested.audio_url === 'string' ? nested.audio_url : '';
|
|
149
|
+
const rendered = audioUrl
|
|
150
|
+
? `${interimMsg || 'Voice output ready'} (${audioUrl})`
|
|
151
|
+
: interimMsg;
|
|
152
|
+
if (rendered) {
|
|
153
|
+
accumulated.interimMessages = [...accumulated.interimMessages, rendered];
|
|
154
|
+
return { interimMessages: accumulated.interimMessages };
|
|
155
|
+
}
|
|
156
|
+
return {};
|
|
157
|
+
}
|
|
158
|
+
case 'clarify_parallel': {
|
|
159
|
+
const bgStatus = parsed.data?.background_status;
|
|
160
|
+
const clarifyMsg = parsed.message || '';
|
|
161
|
+
if (clarifyMsg) {
|
|
162
|
+
accumulated.fullContent += clarifyMsg + '\n\n';
|
|
163
|
+
}
|
|
164
|
+
if (bgStatus) {
|
|
165
|
+
accumulated.thoughts = [...accumulated.thoughts, {
|
|
166
|
+
type: 'progress',
|
|
167
|
+
source: parsed.source,
|
|
168
|
+
message: String(bgStatus),
|
|
169
|
+
data: { phase: 'background_work' },
|
|
170
|
+
timestamp: new Date(),
|
|
171
|
+
}];
|
|
172
|
+
}
|
|
173
|
+
const clarifyOptions = parsed.data?.options || parsed.options;
|
|
174
|
+
return {
|
|
175
|
+
content: stripThinkTags(accumulated.fullContent),
|
|
176
|
+
thoughts: accumulated.thoughts,
|
|
177
|
+
...(clarifyOptions && Array.isArray(clarifyOptions) && clarifyOptions.length > 0
|
|
178
|
+
? { quickReplies: clarifyOptions, promptActive: true }
|
|
179
|
+
: {}),
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
case 'prompt': {
|
|
183
|
+
const promptOptions = parsed.options || parsed.data?.options;
|
|
184
|
+
if (promptOptions && Array.isArray(promptOptions)) {
|
|
185
|
+
const promptType = (parsed.data?.prompt_type || 'choice');
|
|
186
|
+
accumulated.parts = [...accumulated.parts, { type: 'prompt', options: promptOptions, promptType }];
|
|
187
|
+
return {
|
|
188
|
+
parts: accumulated.parts,
|
|
189
|
+
quickReplies: promptOptions,
|
|
190
|
+
promptActive: true,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
return {};
|
|
194
|
+
}
|
|
195
|
+
case 'complete':
|
|
196
|
+
case 'message_complete':
|
|
197
|
+
return { complete: true, conversationId: parsed.conversation_id };
|
|
198
|
+
case 'error':
|
|
199
|
+
return { error: parsed.error || parsed.message || 'An error occurred' };
|
|
200
|
+
default:
|
|
201
|
+
return {};
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
//# sourceMappingURL=stream-event-processor.js.map
|
|
@@ -0,0 +1 @@
|
|
|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D,MAAM,UAAU,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,WAAW,IAAI,QAAQ,CAAkC,CAAC;gBAC3F,WAAW,CAAC,KAAK,GAAG,CAAC,GAAG,WAAW,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,CAAC,CAAC;gBACnG,OAAO;oBACL,KAAK,EAAE,WAAW,CAAC,KAAK;oBACxB,YAAY,EAAE,aAAa;oBAC3B,YAAY,EAAE,IAAI;iBACnB,CAAC;YACJ,CAAC;YACD,OAAO,EAAE,CAAC;QACZ,CAAC;QAED,KAAK,UAAU,CAAC;QAChB,KAAK,kBAAkB;YACrB,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,CAAC,eAAe,EAAE,CAAC;QAEpE,KAAK,OAAO;YACV,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,OAAO,IAAI,mBAAmB,EAAE,CAAC;QAE1E;YACE,OAAO,EAAE,CAAC;IACd,CAAC;AACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useChatStream.d.ts","sourceRoot":"","sources":["../../../src/lib/hooks/useChatStream.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useChatStream.d.ts","sourceRoot":"","sources":["../../../src/lib/hooks/useChatStream.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,kBAAkB,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAEtF,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,YAAY,EAAE,CAAC;IACzB,KAAK,EAAE,WAAW,EAAE,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,eAAe,EAAE,MAAM,EAAE,CAAC;IAC1B,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,YAAY,EAAE,OAAO,CAAC;IACtB,WAAW,EAAE,OAAO,CAAC;IACrB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,YAAY,EAAE,CAAC;IACzB,KAAK,EAAE,WAAW,EAAE,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,eAAe,EAAE,MAAM,EAAE,CAAC;IAC1B,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,UAAU,oBAAoB;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,qBAAqB,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IAC7D,aAAa,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACrD;AAcD,wBAAgB,aAAa,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,qBAAqB,EAAE,aAAa,EAAE,EAAE,oBAAoB;;2BAKhG,kBAAkB,KAC1B,OAAO,CAAC,YAAY,CAAC;;;EAgGzB"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { useState, useRef, useCallback } from 'react';
|
|
3
|
-
import { stripThinkTags
|
|
3
|
+
import { stripThinkTags } from '../../components/chat/chat-utils';
|
|
4
4
|
import { streamChatMessageAgentic } from '../agent/chat-client';
|
|
5
|
+
import { createAccumulator, processStreamEvent } from '../agent/stream-event-processor';
|
|
5
6
|
const INITIAL_STATE = {
|
|
6
7
|
content: '',
|
|
7
8
|
thoughts: [],
|
|
@@ -16,166 +17,25 @@ const INITIAL_STATE = {
|
|
|
16
17
|
export function useChatStream({ token, agentUrl, onConversationCreated, onTitleUpdate }) {
|
|
17
18
|
const [state, setState] = useState(INITIAL_STATE);
|
|
18
19
|
const abortControllerRef = useRef(null);
|
|
19
|
-
const processEvent = useCallback((eventType, parsed, accumulated) => {
|
|
20
|
-
const newThought = {
|
|
21
|
-
type: eventType,
|
|
22
|
-
source: parsed.source,
|
|
23
|
-
message: parsed.message,
|
|
24
|
-
data: parsed.data || parsed,
|
|
25
|
-
timestamp: new Date(),
|
|
26
|
-
};
|
|
27
|
-
if (parsed.source && !parsed.source.includes('dispatcher')) {
|
|
28
|
-
accumulated.agentName = parsed.source;
|
|
29
|
-
}
|
|
30
|
-
switch (eventType) {
|
|
31
|
-
case 'conversation_created':
|
|
32
|
-
return { conversationId: parsed.conversation_id, title: parsed.title };
|
|
33
|
-
case 'title_update':
|
|
34
|
-
return { titleUpdate: { id: parsed.conversation_id, title: parsed.title } };
|
|
35
|
-
case 'thought':
|
|
36
|
-
case 'plan':
|
|
37
|
-
case 'progress':
|
|
38
|
-
accumulated.thoughts = [...accumulated.thoughts, newThought];
|
|
39
|
-
return { thoughts: accumulated.thoughts };
|
|
40
|
-
case 'tool_start': {
|
|
41
|
-
accumulated.thoughts = [...accumulated.thoughts, newThought];
|
|
42
|
-
const toolSource = parsed.source || 'tool';
|
|
43
|
-
const toolName = String(parsed.data?.tool_name || parsed.data?.display_name || toolSource);
|
|
44
|
-
const toolPart = {
|
|
45
|
-
type: 'tool_call',
|
|
46
|
-
id: `tool-${Date.now()}-${toolName}`,
|
|
47
|
-
name: toolName,
|
|
48
|
-
displayName: String(parsed.data?.display_name || parsed.message || toolName),
|
|
49
|
-
status: 'running',
|
|
50
|
-
input: (parsed.data || undefined),
|
|
51
|
-
startedAt: new Date(),
|
|
52
|
-
};
|
|
53
|
-
accumulated.pendingTools.set(toolSource, accumulated.parts.length);
|
|
54
|
-
accumulated.parts = [...accumulated.parts, toolPart];
|
|
55
|
-
return { thoughts: accumulated.thoughts, parts: accumulated.parts };
|
|
56
|
-
}
|
|
57
|
-
case 'tool_result': {
|
|
58
|
-
accumulated.thoughts = [...accumulated.thoughts, newThought];
|
|
59
|
-
const resultSource = parsed.source || 'tool';
|
|
60
|
-
const idx = accumulated.pendingTools.get(resultSource);
|
|
61
|
-
if (idx !== undefined && accumulated.parts[idx]?.type === 'tool_call') {
|
|
62
|
-
const existing = accumulated.parts[idx];
|
|
63
|
-
accumulated.parts = [...accumulated.parts];
|
|
64
|
-
accumulated.parts[idx] = {
|
|
65
|
-
...existing,
|
|
66
|
-
status: parsed.data?.success === false ? 'error' : 'completed',
|
|
67
|
-
output: parsed.message || undefined,
|
|
68
|
-
error: parsed.data?.success === false ? String(parsed.message || 'Failed') : undefined,
|
|
69
|
-
completedAt: new Date(),
|
|
70
|
-
};
|
|
71
|
-
accumulated.pendingTools.delete(resultSource);
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
const toolName = String(parsed.data?.tool_name || parsed.data?.display_name || resultSource);
|
|
75
|
-
accumulated.parts = [...accumulated.parts, {
|
|
76
|
-
type: 'tool_call',
|
|
77
|
-
id: `tool-${Date.now()}-${toolName}`,
|
|
78
|
-
name: toolName,
|
|
79
|
-
displayName: String(parsed.data?.display_name || toolName),
|
|
80
|
-
status: parsed.data?.success === false ? 'error' : 'completed',
|
|
81
|
-
output: parsed.message || undefined,
|
|
82
|
-
completedAt: new Date(),
|
|
83
|
-
}];
|
|
84
|
-
}
|
|
85
|
-
return { thoughts: accumulated.thoughts, parts: accumulated.parts };
|
|
86
|
-
}
|
|
87
|
-
case 'content': {
|
|
88
|
-
const contentData = parsed.data || {};
|
|
89
|
-
const msgText = parsed.message || '';
|
|
90
|
-
if (contentData.streaming && contentData.partial) {
|
|
91
|
-
accumulated.fullContent += msgText;
|
|
92
|
-
}
|
|
93
|
-
else if (contentData.complete) {
|
|
94
|
-
// Final marker
|
|
95
|
-
}
|
|
96
|
-
else if (msgText) {
|
|
97
|
-
accumulated.fullContent = msgText;
|
|
98
|
-
}
|
|
99
|
-
return { content: stripThinkTags(accumulated.fullContent) };
|
|
100
|
-
}
|
|
101
|
-
case 'content_chunk':
|
|
102
|
-
accumulated.fullContent += parsed.chunk || parsed.content || '';
|
|
103
|
-
return { content: stripThinkTags(accumulated.fullContent) };
|
|
104
|
-
case 'interim': {
|
|
105
|
-
const nested = parsed.data || {};
|
|
106
|
-
const interimMsg = String(parsed.message || '').trim();
|
|
107
|
-
const audioUrl = typeof nested.audio_url === 'string' ? nested.audio_url : '';
|
|
108
|
-
const rendered = audioUrl
|
|
109
|
-
? `${interimMsg || 'Voice output ready'} (${audioUrl})`
|
|
110
|
-
: interimMsg;
|
|
111
|
-
if (rendered) {
|
|
112
|
-
accumulated.interimMessages = [...accumulated.interimMessages, rendered];
|
|
113
|
-
return { interimMessages: accumulated.interimMessages };
|
|
114
|
-
}
|
|
115
|
-
return {};
|
|
116
|
-
}
|
|
117
|
-
case 'clarify_parallel': {
|
|
118
|
-
const bgStatus = parsed.data?.background_status;
|
|
119
|
-
const clarifyMsg = parsed.message || '';
|
|
120
|
-
if (clarifyMsg) {
|
|
121
|
-
accumulated.fullContent += clarifyMsg + '\n\n';
|
|
122
|
-
}
|
|
123
|
-
if (bgStatus) {
|
|
124
|
-
accumulated.thoughts = [...accumulated.thoughts, {
|
|
125
|
-
type: 'progress',
|
|
126
|
-
source: parsed.source,
|
|
127
|
-
message: String(bgStatus),
|
|
128
|
-
data: { phase: 'background_work' },
|
|
129
|
-
timestamp: new Date(),
|
|
130
|
-
}];
|
|
131
|
-
}
|
|
132
|
-
const clarifyOptions = parsed.data?.options || parsed.options;
|
|
133
|
-
return {
|
|
134
|
-
content: stripThinkTags(accumulated.fullContent),
|
|
135
|
-
thoughts: accumulated.thoughts,
|
|
136
|
-
...(clarifyOptions && Array.isArray(clarifyOptions) && clarifyOptions.length > 0
|
|
137
|
-
? { quickReplies: clarifyOptions, promptActive: true }
|
|
138
|
-
: {}),
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
case 'prompt': {
|
|
142
|
-
const promptOptions = parsed.options || parsed.data?.options;
|
|
143
|
-
if (promptOptions && Array.isArray(promptOptions)) {
|
|
144
|
-
const promptType = (parsed.data?.prompt_type || 'choice');
|
|
145
|
-
accumulated.parts = [...accumulated.parts, { type: 'prompt', options: promptOptions, promptType }];
|
|
146
|
-
return {
|
|
147
|
-
parts: accumulated.parts,
|
|
148
|
-
quickReplies: promptOptions,
|
|
149
|
-
promptActive: true,
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
return {};
|
|
153
|
-
}
|
|
154
|
-
case 'complete':
|
|
155
|
-
case 'message_complete':
|
|
156
|
-
return { complete: true, conversationId: parsed.conversation_id };
|
|
157
|
-
case 'error':
|
|
158
|
-
return { error: parsed.error || parsed.message || 'An error occurred' };
|
|
159
|
-
default:
|
|
160
|
-
return {};
|
|
161
|
-
}
|
|
162
|
-
}, []);
|
|
163
20
|
const sendMessage = useCallback(async (request) => {
|
|
21
|
+
if (abortControllerRef.current) {
|
|
22
|
+
abortControllerRef.current.abort();
|
|
23
|
+
abortControllerRef.current = null;
|
|
24
|
+
}
|
|
164
25
|
const controller = new AbortController();
|
|
165
26
|
abortControllerRef.current = controller;
|
|
166
|
-
const accumulated =
|
|
167
|
-
|
|
27
|
+
const accumulated = createAccumulator();
|
|
28
|
+
setState(prev => ({
|
|
29
|
+
...prev,
|
|
30
|
+
content: '',
|
|
168
31
|
thoughts: [],
|
|
169
32
|
parts: [],
|
|
170
|
-
pendingTools: new Map(),
|
|
171
|
-
agentName: undefined,
|
|
172
33
|
interimMessages: [],
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
...INITIAL_STATE,
|
|
34
|
+
quickReplies: [],
|
|
35
|
+
promptActive: false,
|
|
176
36
|
isStreaming: true,
|
|
177
|
-
conversationId: request.conversation_id,
|
|
178
|
-
});
|
|
37
|
+
conversationId: request.conversation_id || prev.conversationId,
|
|
38
|
+
}));
|
|
179
39
|
let resultConversationId = request.conversation_id;
|
|
180
40
|
try {
|
|
181
41
|
for await (const event of streamChatMessageAgentic(request, {
|
|
@@ -183,8 +43,8 @@ export function useChatStream({ token, agentUrl, onConversationCreated, onTitleU
|
|
|
183
43
|
agentUrl,
|
|
184
44
|
signal: controller.signal,
|
|
185
45
|
})) {
|
|
186
|
-
|
|
187
|
-
const result =
|
|
46
|
+
const parsed = event.data;
|
|
47
|
+
const result = processStreamEvent(event.type, parsed, accumulated);
|
|
188
48
|
if (result.conversationId && !result.titleUpdate) {
|
|
189
49
|
resultConversationId = result.conversationId;
|
|
190
50
|
onConversationCreated?.(result.conversationId, result.title);
|
|
@@ -209,32 +69,26 @@ export function useChatStream({ token, agentUrl, onConversationCreated, onTitleU
|
|
|
209
69
|
}
|
|
210
70
|
}
|
|
211
71
|
finally {
|
|
212
|
-
setState(prev => ({
|
|
72
|
+
setState(prev => ({
|
|
73
|
+
...prev,
|
|
74
|
+
isStreaming: false,
|
|
75
|
+
content: '',
|
|
76
|
+
thoughts: [],
|
|
77
|
+
parts: [],
|
|
78
|
+
agentName: undefined,
|
|
79
|
+
interimMessages: [],
|
|
80
|
+
}));
|
|
213
81
|
abortControllerRef.current = null;
|
|
214
82
|
}
|
|
215
|
-
const finalContent = stripThinkTags(accumulated.fullContent);
|
|
216
|
-
// Extract any thinking content from the raw text that wasn't caught as events
|
|
217
|
-
const textThoughts = extractThinkContent(accumulated.fullContent);
|
|
218
|
-
if (textThoughts.length > 0) {
|
|
219
|
-
for (const t of textThoughts) {
|
|
220
|
-
accumulated.thoughts.push({
|
|
221
|
-
type: 'thought',
|
|
222
|
-
source: accumulated.agentName,
|
|
223
|
-
message: t,
|
|
224
|
-
data: { phase: 'model_reasoning' },
|
|
225
|
-
timestamp: new Date(),
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
83
|
return {
|
|
230
|
-
content:
|
|
84
|
+
content: stripThinkTags(accumulated.fullContent),
|
|
231
85
|
thoughts: accumulated.thoughts,
|
|
232
86
|
parts: accumulated.parts,
|
|
233
87
|
agentName: accumulated.agentName,
|
|
234
88
|
interimMessages: accumulated.interimMessages,
|
|
235
89
|
conversationId: resultConversationId,
|
|
236
90
|
};
|
|
237
|
-
}, [token, agentUrl,
|
|
91
|
+
}, [token, agentUrl, onConversationCreated, onTitleUpdate]);
|
|
238
92
|
const cancel = useCallback(() => {
|
|
239
93
|
abortControllerRef.current?.abort();
|
|
240
94
|
}, []);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useChatStream.js","sourceRoot":"","sources":["../../../src/lib/hooks/useChatStream.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,
|
|
1
|
+
{"version":3,"file":"useChatStream.js","sourceRoot":"","sources":["../../../src/lib/hooks/useChatStream.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,kCAAkC,CAAC;AAClE,OAAO,EAAE,wBAAwB,EAAE,MAAM,sBAAsB,CAAC;AAChE,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AA+BxF,MAAM,aAAa,GAAgB;IACjC,OAAO,EAAE,EAAE;IACX,QAAQ,EAAE,EAAE;IACZ,KAAK,EAAE,EAAE;IACT,SAAS,EAAE,SAAS;IACpB,eAAe,EAAE,EAAE;IACnB,YAAY,EAAE,EAAE;IAChB,YAAY,EAAE,KAAK;IACnB,WAAW,EAAE,KAAK;IAClB,cAAc,EAAE,SAAS;CAC1B,CAAC;AAEF,MAAM,UAAU,aAAa,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,qBAAqB,EAAE,aAAa,EAAwB;IAC3G,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAc,aAAa,CAAC,CAAC;IAC/D,MAAM,kBAAkB,GAAG,MAAM,CA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