devnonla-ui 0.4.3 → 0.5.0
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/README.md +12 -4
- package/package.json +1 -1
- package/src/chat/AgentChatbox.tsx +112 -0
- package/src/chat/AgentPanel.tsx +5 -98
- package/src/chat/chatMessageList.tsx +114 -0
- package/src/chat/common/agentStreamHelpers.ts +104 -0
- package/src/chat/common/createAgentSseSession.ts +304 -0
- package/src/chat/common/types.ts +0 -4
- package/src/chat/common/useAgentChatStream.ts +224 -0
- package/src/chat/common/useAgentStream.ts +28 -367
- package/src/chat/index.ts +5 -1
- package/src/chat/message-ui/ChatInput.tsx +1 -1
- package/src/chat/tool-ui/ChatToolCall.tsx +27 -27
- package/src/dropdown/ContextMenu.tsx +14 -2
- package/src/dropdown/Dropdown.tsx +15 -3
- package/src/dropdown/menuClasses.ts +5 -1
- package/src/index.ts +3 -1
|
@@ -1,108 +1,11 @@
|
|
|
1
1
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
+
import { buildAgentHistory, failOpenTools, finalizeStreaming, nextId } from "./agentStreamHelpers";
|
|
3
|
+
import { createAgentSseSession } from "./createAgentSseSession";
|
|
2
4
|
import { parseSseStream } from "./sse";
|
|
3
|
-
import type {
|
|
4
|
-
import {
|
|
5
|
+
import type { AgentMessage, AgentPanelEndpoint, AgentToolCallEvent, AgentToolHook, AgentToolResultEvent } from "./types";
|
|
6
|
+
import { matchesToolHook } from "./utils";
|
|
5
7
|
|
|
6
|
-
|
|
7
|
-
function nextId(prefix: string) {
|
|
8
|
-
return `${prefix}-${Date.now()}-${++_id}`;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function thinkingDurationSec(startedAt: number): number {
|
|
12
|
-
if (!startedAt) return 0;
|
|
13
|
-
return Math.round((Date.now() - startedAt) / 1000);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function buildAgentHistory(messages: AgentMessage[]): AgentHistoryMessage[] {
|
|
17
|
-
return messages
|
|
18
|
-
.filter((m) => {
|
|
19
|
-
if (m.role === "user") return m.content.trim() !== "";
|
|
20
|
-
if (m.role === "assistant") return m.content.trim() !== "";
|
|
21
|
-
if (m.role === "tool-call") return m.toolOutput != null;
|
|
22
|
-
return false;
|
|
23
|
-
})
|
|
24
|
-
.map((m) => {
|
|
25
|
-
if (m.role === "tool-call") {
|
|
26
|
-
return {
|
|
27
|
-
role: "tool-call" as const,
|
|
28
|
-
content: "" as const,
|
|
29
|
-
toolCallId: m.toolCallId,
|
|
30
|
-
toolName: m.toolName,
|
|
31
|
-
toolInput: m.toolInput,
|
|
32
|
-
toolOutput: m.toolOutput,
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
return { role: m.role as "user" | "assistant", content: m.content };
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function finalizeStreaming(prev: AgentMessage[]): AgentMessage[] {
|
|
40
|
-
return prev
|
|
41
|
-
.map((m) => {
|
|
42
|
-
if (m.role === "assistant" && m.streaming) {
|
|
43
|
-
if (!m.content.trim() && m.meta?.thinking) {
|
|
44
|
-
return {
|
|
45
|
-
...m,
|
|
46
|
-
role: "thinking" as const,
|
|
47
|
-
content: String(m.meta.thinking),
|
|
48
|
-
streaming: false,
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
return { ...m, streaming: false };
|
|
52
|
-
}
|
|
53
|
-
return m;
|
|
54
|
-
})
|
|
55
|
-
.filter((m) => !(m.role === "assistant" && !m.content.trim() && !m.meta?.thinking));
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function failOpenTools(prev: AgentMessage[], error: string): AgentMessage[] {
|
|
59
|
-
return prev.map((m) => {
|
|
60
|
-
if (m.role !== "tool-call" || m.toolOutput != null || m.toolError) return m;
|
|
61
|
-
return { ...m, toolError: error, toolOutput: JSON.stringify({ success: false, error }) };
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function sameStreamTool(m: AgentMessage, toolName: string, toolLabel: string) {
|
|
66
|
-
if (m.role !== "tool-call") return false;
|
|
67
|
-
if (m.toolName === toolName) return true;
|
|
68
|
-
if (m.toolLabel && (m.toolLabel === toolLabel || m.toolLabel === toolName)) return true;
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function lastToolIndex(prev: AgentMessage[], pred: (m: AgentMessage) => boolean): number {
|
|
73
|
-
for (let i = prev.length - 1; i >= 0; i--) {
|
|
74
|
-
if (pred(prev[i])) return i;
|
|
75
|
-
}
|
|
76
|
-
return -1;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** Merge a streamed tool-call into an existing card (same id, still running, or result-before-params). */
|
|
80
|
-
function findToolCallMergeIndex(
|
|
81
|
-
prev: AgentMessage[],
|
|
82
|
-
event: { toolCallId?: string; toolName: string; toolLabel: string; input: unknown },
|
|
83
|
-
): number {
|
|
84
|
-
if (event.toolCallId) {
|
|
85
|
-
const byId = prev.findIndex((m) => m.role === "tool-call" && m.toolCallId === event.toolCallId);
|
|
86
|
-
if (byId !== -1) return byId;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const pending = lastToolIndex(
|
|
90
|
-
prev,
|
|
91
|
-
(m) => sameStreamTool(m, event.toolName, event.toolLabel) && m.toolOutput == null && !m.toolError,
|
|
92
|
-
);
|
|
93
|
-
if (pending !== -1) {
|
|
94
|
-
const m = prev[pending];
|
|
95
|
-
if (!event.toolCallId || !m.toolCallId || m.toolCallId === event.toolCallId) return pending;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (!hasMeaningfulInput(event.input)) return -1;
|
|
99
|
-
|
|
100
|
-
const sparse = lastToolIndex(prev, (m) => sameStreamTool(m, event.toolName, event.toolLabel) && !hasMeaningfulInput(m.toolInput));
|
|
101
|
-
if (sparse === -1) return -1;
|
|
102
|
-
const m = prev[sparse];
|
|
103
|
-
if (!event.toolCallId || !m.toolCallId || m.toolCallId === event.toolCallId) return sparse;
|
|
104
|
-
return -1;
|
|
105
|
-
}
|
|
8
|
+
export { buildAgentHistory } from "./agentStreamHelpers";
|
|
106
9
|
|
|
107
10
|
export type UseAgentStreamOptions = {
|
|
108
11
|
endpoint: AgentPanelEndpoint;
|
|
@@ -110,20 +13,24 @@ export type UseAgentStreamOptions = {
|
|
|
110
13
|
headers?: Record<string, string>;
|
|
111
14
|
fetcher?: typeof fetch;
|
|
112
15
|
initialMessages?: AgentMessage[];
|
|
113
|
-
onToolAction?: (event: AgentToolAction) => void;
|
|
114
16
|
toolHooks?: AgentToolHook[];
|
|
115
17
|
};
|
|
116
18
|
|
|
117
19
|
type OpenTool = AgentToolCallEvent;
|
|
118
20
|
|
|
119
|
-
export function useAgentStream({
|
|
21
|
+
export function useAgentStream({
|
|
22
|
+
endpoint,
|
|
23
|
+
extraBody,
|
|
24
|
+
headers,
|
|
25
|
+
fetcher = fetch,
|
|
26
|
+
initialMessages,
|
|
27
|
+
toolHooks,
|
|
28
|
+
}: UseAgentStreamOptions) {
|
|
120
29
|
const [messages, setMessages] = useState<AgentMessage[]>(() => initialMessages ?? []);
|
|
121
30
|
const [generating, setGenerating] = useState(false);
|
|
122
31
|
const thinkingRef = useRef("");
|
|
123
32
|
const thinkingStartRef = useRef(0);
|
|
124
33
|
const abortRef = useRef<AbortController | null>(null);
|
|
125
|
-
const onToolActionRef = useRef(onToolAction);
|
|
126
|
-
onToolActionRef.current = onToolAction;
|
|
127
34
|
const toolHooksRef = useRef(toolHooks);
|
|
128
35
|
toolHooksRef.current = toolHooks;
|
|
129
36
|
const extraBodyRef = useRef(extraBody);
|
|
@@ -136,14 +43,12 @@ export function useAgentStream({ endpoint, extraBody, headers, fetcher = fetch,
|
|
|
136
43
|
for (const hook of toolHooksRef.current ?? []) {
|
|
137
44
|
if (matchesToolHook(hook.name, event.toolName)) hook.onCall?.(event);
|
|
138
45
|
}
|
|
139
|
-
onToolActionRef.current?.({ type: "tool-call", ...event });
|
|
140
46
|
}, []);
|
|
141
47
|
|
|
142
48
|
const emitResult = useCallback((event: AgentToolResultEvent) => {
|
|
143
49
|
for (const hook of toolHooksRef.current ?? []) {
|
|
144
50
|
if (matchesToolHook(hook.name, event.toolName)) hook.onResult?.(event);
|
|
145
51
|
}
|
|
146
|
-
onToolActionRef.current?.({ type: "tool-result", ...event });
|
|
147
52
|
}, []);
|
|
148
53
|
|
|
149
54
|
const flushOpenTools = useCallback(
|
|
@@ -211,83 +116,17 @@ export function useAgentStream({ endpoint, extraBody, headers, fetcher = fetch,
|
|
|
211
116
|
thinkingStartRef.current = 0;
|
|
212
117
|
|
|
213
118
|
const history = buildAgentHistory(historySnapshot);
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
setMessages((prev) =>
|
|
226
|
-
prev.map((m) => {
|
|
227
|
-
if (m.id !== freezeId) return m;
|
|
228
|
-
const existing = typeof m.meta?.thinkingDuration === "number" ? m.meta.thinkingDuration : undefined;
|
|
229
|
-
return {
|
|
230
|
-
...m,
|
|
231
|
-
content: assistantText,
|
|
232
|
-
streaming: false,
|
|
233
|
-
meta: thinkingSnapshot ? { ...m.meta, thinking: thinkingSnapshot, thinkingDuration: liveDuration ?? existing ?? 0 } : m.meta,
|
|
234
|
-
};
|
|
235
|
-
}),
|
|
236
|
-
);
|
|
237
|
-
} else if (thinkingSnapshot) {
|
|
238
|
-
setMessages((prev) =>
|
|
239
|
-
prev.map((m) =>
|
|
240
|
-
m.id === freezeId
|
|
241
|
-
? {
|
|
242
|
-
id: freezeId,
|
|
243
|
-
role: "thinking" as const,
|
|
244
|
-
content: thinkingSnapshot,
|
|
245
|
-
streaming: false,
|
|
246
|
-
timestamp: m.timestamp,
|
|
247
|
-
meta: { thinking: thinkingSnapshot, thinkingDuration: liveDuration ?? 0 },
|
|
248
|
-
}
|
|
249
|
-
: m,
|
|
250
|
-
),
|
|
251
|
-
);
|
|
252
|
-
} else {
|
|
253
|
-
setMessages((prev) => prev.filter((m) => m.id !== freezeId));
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
thinkingRef.current = "";
|
|
257
|
-
thinkingStartRef.current = 0;
|
|
258
|
-
assistantText = "";
|
|
259
|
-
currentId = "";
|
|
260
|
-
needsNewBubble = true;
|
|
261
|
-
};
|
|
262
|
-
|
|
263
|
-
const ensureBubble = (seed?: { content?: string; thinking?: string }): string => {
|
|
264
|
-
if (!needsNewBubble && currentId) return currentId;
|
|
265
|
-
const newId = nextId("a");
|
|
266
|
-
currentId = newId;
|
|
267
|
-
needsNewBubble = false;
|
|
268
|
-
setMessages((prev) => [
|
|
269
|
-
...prev,
|
|
270
|
-
{
|
|
271
|
-
id: newId,
|
|
272
|
-
role: "assistant",
|
|
273
|
-
content: seed?.content ?? "",
|
|
274
|
-
streaming: true,
|
|
275
|
-
timestamp: new Date(),
|
|
276
|
-
meta: seed?.thinking ? { thinking: seed.thinking } : undefined,
|
|
277
|
-
},
|
|
278
|
-
]);
|
|
279
|
-
return newId;
|
|
280
|
-
};
|
|
281
|
-
|
|
282
|
-
const stampThinkingDuration = () => {
|
|
283
|
-
if (!thinkingRef.current || !thinkingStartRef.current) return;
|
|
284
|
-
const duration = thinkingDurationSec(thinkingStartRef.current);
|
|
285
|
-
const targetId = currentId;
|
|
286
|
-
if (targetId) {
|
|
287
|
-
setMessages((prev) => prev.map((m) => (m.id === targetId ? { ...m, meta: { ...m.meta, thinkingDuration: duration } } : m)));
|
|
288
|
-
}
|
|
289
|
-
thinkingStartRef.current = 0;
|
|
290
|
-
};
|
|
119
|
+
const session = createAgentSseSession({
|
|
120
|
+
setMessages,
|
|
121
|
+
setGenerating,
|
|
122
|
+
thinkingRef,
|
|
123
|
+
thinkingStartRef,
|
|
124
|
+
openToolsRef,
|
|
125
|
+
emitCall,
|
|
126
|
+
emitResult,
|
|
127
|
+
flushOpenTools,
|
|
128
|
+
assistantId,
|
|
129
|
+
});
|
|
291
130
|
|
|
292
131
|
try {
|
|
293
132
|
const extra = typeof extraBodyRef.current === "function" ? extraBodyRef.current() : extraBodyRef.current;
|
|
@@ -309,193 +148,15 @@ export function useAgentStream({ endpoint, extraBody, headers, fetcher = fetch,
|
|
|
309
148
|
throw new Error(errText || `HTTP ${response.status}`);
|
|
310
149
|
}
|
|
311
150
|
|
|
312
|
-
const result = await parseSseStream(
|
|
313
|
-
|
|
314
|
-
{
|
|
315
|
-
onTextDelta: (delta) => {
|
|
316
|
-
stampThinkingDuration();
|
|
317
|
-
if (needsNewBubble || !currentId) {
|
|
318
|
-
assistantText = delta;
|
|
319
|
-
thinkingRef.current = "";
|
|
320
|
-
thinkingStartRef.current = 0;
|
|
321
|
-
ensureBubble({ content: delta });
|
|
322
|
-
} else {
|
|
323
|
-
assistantText += delta;
|
|
324
|
-
const targetId = currentId;
|
|
325
|
-
setMessages((prev) => prev.map((m) => (m.id === targetId ? { ...m, content: assistantText, streaming: true } : m)));
|
|
326
|
-
}
|
|
327
|
-
},
|
|
328
|
-
onThinkingDelta: (delta) => {
|
|
329
|
-
if (needsNewBubble || !currentId) {
|
|
330
|
-
thinkingRef.current = delta;
|
|
331
|
-
thinkingStartRef.current = Date.now();
|
|
332
|
-
assistantText = "";
|
|
333
|
-
ensureBubble({ thinking: delta });
|
|
334
|
-
return;
|
|
335
|
-
}
|
|
336
|
-
if (!thinkingRef.current) thinkingStartRef.current = Date.now();
|
|
337
|
-
thinkingRef.current += delta;
|
|
338
|
-
const thinking = thinkingRef.current;
|
|
339
|
-
const targetId = currentId;
|
|
340
|
-
setMessages((prev) => prev.map((m) => (m.id === targetId ? { ...m, meta: { ...m.meta, thinking } } : m)));
|
|
341
|
-
},
|
|
342
|
-
onToolCall: (event) => {
|
|
343
|
-
const label = event.toolLabel ? (event.toolLabel.includes(" ") ? event.toolLabel : formatToolName(event.toolLabel)) : formatToolName(event.toolName);
|
|
344
|
-
if (event.toolCallId) seenToolCallIds.add(event.toolCallId);
|
|
345
|
-
|
|
346
|
-
const call: AgentToolCallEvent = {
|
|
347
|
-
toolCallId: event.toolCallId,
|
|
348
|
-
toolName: event.toolName,
|
|
349
|
-
toolLabel: label,
|
|
350
|
-
input: event.input,
|
|
351
|
-
};
|
|
352
|
-
|
|
353
|
-
freezeOpen();
|
|
354
|
-
setMessages((prev) => {
|
|
355
|
-
const idx = findToolCallMergeIndex(prev, { ...event, toolLabel: label });
|
|
356
|
-
if (idx !== -1) {
|
|
357
|
-
const target = prev[idx];
|
|
358
|
-
const nextInput = hasMeaningfulInput(event.input) ? event.input : target.toolInput;
|
|
359
|
-
const nextId = event.toolCallId ?? target.toolCallId;
|
|
360
|
-
if (target.toolOutput == null) {
|
|
361
|
-
const mapKey = [...openToolsRef.current.entries()].find(([, t]) => t.toolCallId === target.toolCallId || t.toolName === target.toolName)?.[0] ?? nextId ?? target.id;
|
|
362
|
-
openToolsRef.current.set(mapKey, { ...call, toolCallId: nextId, input: nextInput });
|
|
363
|
-
}
|
|
364
|
-
return prev.map((m, i) =>
|
|
365
|
-
i === idx
|
|
366
|
-
? {
|
|
367
|
-
...m,
|
|
368
|
-
content: event.toolName,
|
|
369
|
-
toolCallId: nextId,
|
|
370
|
-
toolName: event.toolName,
|
|
371
|
-
toolLabel: label,
|
|
372
|
-
toolInput: nextInput,
|
|
373
|
-
}
|
|
374
|
-
: m,
|
|
375
|
-
);
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
const id = nextId("tc");
|
|
379
|
-
openToolsRef.current.set(event.toolCallId || id, call);
|
|
380
|
-
return [
|
|
381
|
-
...prev,
|
|
382
|
-
{
|
|
383
|
-
id,
|
|
384
|
-
role: "tool-call" as const,
|
|
385
|
-
content: event.toolName,
|
|
386
|
-
toolCallId: event.toolCallId,
|
|
387
|
-
toolName: event.toolName,
|
|
388
|
-
toolLabel: label,
|
|
389
|
-
toolInput: event.input,
|
|
390
|
-
timestamp: new Date(),
|
|
391
|
-
},
|
|
392
|
-
];
|
|
393
|
-
});
|
|
394
|
-
if (event.input !== undefined) emitCall(call);
|
|
395
|
-
},
|
|
396
|
-
onToolResult: (event) => {
|
|
397
|
-
const raw = event.result;
|
|
398
|
-
const resultStr = typeof raw === "string" ? raw : JSON.stringify(raw);
|
|
399
|
-
const resultLabel = formatToolName(event.toolName);
|
|
400
|
-
if (event.toolCallId) {
|
|
401
|
-
openToolsRef.current.delete(event.toolCallId);
|
|
402
|
-
} else {
|
|
403
|
-
for (const [key, tool] of [...openToolsRef.current.entries()].reverse()) {
|
|
404
|
-
if (tool.toolName === event.toolName) {
|
|
405
|
-
openToolsRef.current.delete(key);
|
|
406
|
-
break;
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
freezeOpen();
|
|
411
|
-
setMessages((prev) => {
|
|
412
|
-
let matchIdx = -1;
|
|
413
|
-
if (event.toolCallId) {
|
|
414
|
-
matchIdx = prev.findIndex((m) => m.role === "tool-call" && m.toolCallId === event.toolCallId);
|
|
415
|
-
}
|
|
416
|
-
if (matchIdx === -1) {
|
|
417
|
-
matchIdx = lastToolIndex(
|
|
418
|
-
prev,
|
|
419
|
-
(m) => sameStreamTool(m, event.toolName, resultLabel) && m.toolOutput == null,
|
|
420
|
-
);
|
|
421
|
-
}
|
|
422
|
-
if (matchIdx !== -1) {
|
|
423
|
-
return prev.map((m, i) => (i === matchIdx ? { ...m, toolOutput: resultStr, toolCallId: event.toolCallId ?? m.toolCallId } : m));
|
|
424
|
-
}
|
|
425
|
-
const id = nextId("tc");
|
|
426
|
-
return [
|
|
427
|
-
...prev,
|
|
428
|
-
{
|
|
429
|
-
id,
|
|
430
|
-
role: "tool-call" as const,
|
|
431
|
-
content: event.toolName,
|
|
432
|
-
toolCallId: event.toolCallId,
|
|
433
|
-
toolName: event.toolName,
|
|
434
|
-
toolLabel: resultLabel,
|
|
435
|
-
toolOutput: resultStr,
|
|
436
|
-
timestamp: new Date(),
|
|
437
|
-
},
|
|
438
|
-
];
|
|
439
|
-
});
|
|
440
|
-
emitResult({ toolCallId: event.toolCallId, toolName: event.toolName, output: raw });
|
|
441
|
-
},
|
|
442
|
-
onDone: () => {
|
|
443
|
-
flushOpenTools("Tool did not return a result");
|
|
444
|
-
setMessages((prev) => finalizeStreaming(failOpenTools(prev, "Tool did not return a result")));
|
|
445
|
-
setGenerating(false);
|
|
446
|
-
thinkingRef.current = "";
|
|
447
|
-
thinkingStartRef.current = 0;
|
|
448
|
-
},
|
|
449
|
-
onError: (error) => {
|
|
450
|
-
const reason = error === "cancelled" ? "Cancelled" : error;
|
|
451
|
-
flushOpenTools(reason);
|
|
452
|
-
if (error === "Connection lost") {
|
|
453
|
-
setMessages((prev) => finalizeStreaming(failOpenTools(prev, "Connection lost")));
|
|
454
|
-
setGenerating(false);
|
|
455
|
-
thinkingRef.current = "";
|
|
456
|
-
thinkingStartRef.current = 0;
|
|
457
|
-
return;
|
|
458
|
-
}
|
|
459
|
-
setMessages((prev) => [
|
|
460
|
-
...finalizeStreaming(failOpenTools(prev, reason)),
|
|
461
|
-
...(error === "cancelled" ? [] : [{ id: nextId("err"), role: "error" as const, content: error, timestamp: new Date() }]),
|
|
462
|
-
]);
|
|
463
|
-
setGenerating(false);
|
|
464
|
-
thinkingRef.current = "";
|
|
465
|
-
thinkingStartRef.current = 0;
|
|
466
|
-
},
|
|
467
|
-
},
|
|
468
|
-
{ signal: controller.signal },
|
|
469
|
-
);
|
|
470
|
-
|
|
471
|
-
if (result === "aborted") {
|
|
472
|
-
flushOpenTools("Cancelled");
|
|
473
|
-
setMessages((prev) => finalizeStreaming(failOpenTools(prev, "Cancelled")));
|
|
474
|
-
setGenerating(false);
|
|
475
|
-
thinkingRef.current = "";
|
|
476
|
-
thinkingStartRef.current = 0;
|
|
477
|
-
return;
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
flushOpenTools("Tool did not return a result");
|
|
481
|
-
setMessages((prev) => finalizeStreaming(failOpenTools(prev, "Tool did not return a result")));
|
|
482
|
-
setGenerating(false);
|
|
151
|
+
const result = await parseSseStream(response.body, session.callbacks, { signal: controller.signal });
|
|
152
|
+
session.finishAfterParse(result);
|
|
483
153
|
} catch (err: unknown) {
|
|
484
154
|
if ((err as Error)?.name === "AbortError") {
|
|
485
|
-
|
|
486
|
-
setMessages((prev) => finalizeStreaming(failOpenTools(prev, "Cancelled")));
|
|
487
|
-
setGenerating(false);
|
|
488
|
-
thinkingRef.current = "";
|
|
489
|
-
thinkingStartRef.current = 0;
|
|
155
|
+
session.finishAbortError();
|
|
490
156
|
return;
|
|
491
157
|
}
|
|
492
158
|
const message = err instanceof Error ? err.message : String(err);
|
|
493
|
-
|
|
494
|
-
setMessages((prev) => [
|
|
495
|
-
...finalizeStreaming(failOpenTools(prev, message)),
|
|
496
|
-
{ id: nextId("err"), role: "error", content: message, timestamp: new Date() },
|
|
497
|
-
]);
|
|
498
|
-
setGenerating(false);
|
|
159
|
+
session.finishThrown(message);
|
|
499
160
|
} finally {
|
|
500
161
|
abortRef.current = null;
|
|
501
162
|
}
|
package/src/chat/index.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
+
export type { AgentChatboxProps } from "./AgentChatbox";
|
|
2
|
+
export { AgentChatbox } from "./AgentChatbox";
|
|
1
3
|
export type { AgentPanelProps } from "./AgentPanel";
|
|
2
4
|
export { AgentPanel } from "./AgentPanel";
|
|
3
5
|
export type { ChatBgTask, ChatBgTaskStatus } from "./common/bgTasks";
|
|
4
6
|
export { formatBgElapsed, parseBgTaskRef } from "./common/bgTasks";
|
|
5
7
|
export type { AgentSseCallbacks, AgentSseEvent, ParseSseResult } from "./common/sse";
|
|
6
8
|
export { normalizeSseEvent, parseSseStream } from "./common/sse";
|
|
7
|
-
export type { AgentHistoryMessage, AgentMessage, AgentMessageRole, AgentPanelEndpoint, AgentStreamRequest,
|
|
9
|
+
export type { AgentHistoryMessage, AgentMessage, AgentMessageRole, AgentPanelEndpoint, AgentStreamRequest, AgentToolCallEvent, AgentToolHook, AgentToolNameMatch, AgentToolResultEvent } from "./common/types";
|
|
10
|
+
export type { AgentChatResumeRequest, AgentChatSendRequest, UseAgentChatStreamOptions } from "./common/useAgentChatStream";
|
|
11
|
+
export { useAgentChatStream } from "./common/useAgentChatStream";
|
|
8
12
|
export type { UseAgentStreamOptions } from "./common/useAgentStream";
|
|
9
13
|
export { buildAgentHistory, useAgentStream } from "./common/useAgentStream";
|
|
10
14
|
export { formatToolName, isCallAgentToolName, matchesToolHook, matchesToolName, parseCallAgentToolTargetId, prettyJson } from "./common/utils";
|
|
@@ -129,7 +129,7 @@ export function ChatInput({ generating = false, placeholder = "Message…", disa
|
|
|
129
129
|
<div className="flex items-center gap-1.5 pb-2 px-2">
|
|
130
130
|
{onClear ? (
|
|
131
131
|
<button type="button" onClick={onClear} title="Clear chat" aria-label="Clear chat" className="inline-flex size-6 shrink-0 items-center justify-center rounded-lg border-0 bg-transparent cursor-pointer hover:bg-ink-hover">
|
|
132
|
-
<Eraser size={
|
|
132
|
+
<Eraser size={14} strokeWidth={1.5} aria-hidden />
|
|
133
133
|
</button>
|
|
134
134
|
) : null}
|
|
135
135
|
{toolbar}
|
|
@@ -33,39 +33,39 @@ export function ChatToolCall({ toolName = "Tool", label, toolInput, toolOutput,
|
|
|
33
33
|
const header = (
|
|
34
34
|
<>
|
|
35
35
|
{identityIcon}
|
|
36
|
-
<
|
|
37
|
-
|
|
36
|
+
<span className="flex min-w-0 items-center gap-1">
|
|
37
|
+
<Shimmer active={running} className={cn("min-w-0 truncate text-left text-[14px] font-medium", !running && "text-muted-foreground", expandable && !running && "transition-colors group-hover:text-foreground")}>{displayLabel}</Shimmer>
|
|
38
|
+
<ToolUiTrailing failed={hasError} chevron={expandable} chevronClassName={cn("group-hover:opacity-100", open && "opacity-100 rotate-90")} />
|
|
39
|
+
</span>
|
|
38
40
|
</>
|
|
39
41
|
);
|
|
40
42
|
|
|
41
43
|
return (
|
|
42
44
|
<div className={cn("nonla-chat-tool mt-1", className)}>
|
|
43
|
-
<div className="px-4 pb-
|
|
44
|
-
|
|
45
|
-
{
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
)}
|
|
45
|
+
<div className="px-4 pb-2">
|
|
46
|
+
{expandable ? (
|
|
47
|
+
<button type="button" onClick={() => setOpen((v) => !v)} className="group flex w-full cursor-pointer items-center gap-2 border-0 bg-transparent py-0.5 text-[14px] leading-5.5 outline-none">
|
|
48
|
+
{header}
|
|
49
|
+
</button>
|
|
50
|
+
) : (
|
|
51
|
+
<div className="flex w-full items-center gap-2 py-0.5 text-[14px] leading-5.5">{header}</div>
|
|
52
|
+
)}
|
|
52
53
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
</div>
|
|
54
|
+
{open && expandable ? (
|
|
55
|
+
<div className="mt-1.5 mb-1 overflow-hidden rounded-lg font-mono text-[13px]">
|
|
56
|
+
{hasInput ? <pre className="m-0 max-h-27.5 overflow-y-auto bg-well px-3 py-1.5 break-all whitespace-pre-wrap font-normal leading-[1.65] text-muted-foreground">{prettyJson(toolInput)}</pre> : null}
|
|
57
|
+
{running ? (
|
|
58
|
+
<div className="bg-well-strong px-3 py-1.5 text-muted-foreground">
|
|
59
|
+
<Shimmer className="italic">Running…</Shimmer>
|
|
60
|
+
</div>
|
|
61
|
+
) : null}
|
|
62
|
+
{!isPending ? (
|
|
63
|
+
<pre className={cn("m-0 max-h-75 overflow-y-auto px-3 py-1.5 break-all whitespace-pre-wrap font-normal leading-[1.65] text-muted-foreground", hasError ? "bg-destructive/6" : "nonla-chat-tool-result-ok")}>
|
|
64
|
+
{hasOutput ? prettyJson(toolOutput) : typeof toolError === "string" ? toolError : "Tool execution failed"}
|
|
65
|
+
</pre>
|
|
66
|
+
) : null}
|
|
67
|
+
</div>
|
|
68
|
+
) : null}
|
|
69
69
|
</div>
|
|
70
70
|
</div>
|
|
71
71
|
);
|
|
@@ -3,7 +3,7 @@ import type { ReactNode } from "react";
|
|
|
3
3
|
import { usePopupContainer } from "../app/context";
|
|
4
4
|
import { cn } from "../lib/cn";
|
|
5
5
|
import type { MenuItemType, MenuProps } from "./Dropdown";
|
|
6
|
-
import { menuContentClass, menuIconClass, menuItemClass } from "./menuClasses";
|
|
6
|
+
import { menuContentClass, menuGroupClass, menuGroupLabelClass, menuIconClass, menuItemClass } from "./menuClasses";
|
|
7
7
|
|
|
8
8
|
export type ContextMenuProps = {
|
|
9
9
|
menu?: MenuProps;
|
|
@@ -24,7 +24,7 @@ function ChevronRight() {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
function itemKey(item: MenuItemType, i: number) {
|
|
27
|
-
return item.key ?? (item.type === "divider" ? `divider-${i}` : `item-${i}`);
|
|
27
|
+
return item.key ?? (item.type === "divider" ? `divider-${i}` : item.type === "group" ? `group-${i}` : `item-${i}`);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
function MenuItems({
|
|
@@ -44,6 +44,18 @@ function MenuItems({
|
|
|
44
44
|
if (item.type === "divider") {
|
|
45
45
|
return <ContextMenuPrimitive.Separator key={key} className="my-1 h-px bg-border" />;
|
|
46
46
|
}
|
|
47
|
+
if (item.type === "group") {
|
|
48
|
+
return (
|
|
49
|
+
<ContextMenuPrimitive.Group key={key} className={menuGroupClass}>
|
|
50
|
+
{item.label != null && item.label !== "" ? (
|
|
51
|
+
<ContextMenuPrimitive.Label className={cn(menuGroupLabelClass, item.className)} style={item.style}>
|
|
52
|
+
{item.label}
|
|
53
|
+
</ContextMenuPrimitive.Label>
|
|
54
|
+
) : null}
|
|
55
|
+
<MenuItems items={item.children ?? []} onClick={onClick} contentClassName={contentClassName} />
|
|
56
|
+
</ContextMenuPrimitive.Group>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
47
59
|
if (item.children?.length) {
|
|
48
60
|
return (
|
|
49
61
|
<ContextMenuPrimitive.Sub key={key}>
|
|
@@ -3,7 +3,7 @@ import { type CSSProperties, type MouseEvent, type ReactNode, useState } from "r
|
|
|
3
3
|
import { usePopupContainer } from "../app/context";
|
|
4
4
|
import { cn } from "../lib/cn";
|
|
5
5
|
import { type PopperPlacement, placementToRadix } from "../lib/placement";
|
|
6
|
-
import { menuContentClass, menuIconClass, menuItemClass } from "./menuClasses";
|
|
6
|
+
import { menuContentClass, menuGroupClass, menuGroupLabelClass, menuIconClass, menuItemClass } from "./menuClasses";
|
|
7
7
|
|
|
8
8
|
export type MenuItemType = {
|
|
9
9
|
key?: string;
|
|
@@ -11,7 +11,7 @@ export type MenuItemType = {
|
|
|
11
11
|
icon?: ReactNode;
|
|
12
12
|
disabled?: boolean;
|
|
13
13
|
danger?: boolean;
|
|
14
|
-
type?: "item" | "divider";
|
|
14
|
+
type?: "item" | "divider" | "group";
|
|
15
15
|
children?: (MenuItemType | null | undefined)[];
|
|
16
16
|
onClick?: (info?: { key: string }) => void;
|
|
17
17
|
style?: CSSProperties;
|
|
@@ -49,7 +49,7 @@ function ChevronRight() {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
function itemKey(item: MenuItemType, i: number) {
|
|
52
|
-
return item.key ?? (item.type === "divider" ? `divider-${i}` : `item-${i}`);
|
|
52
|
+
return item.key ?? (item.type === "divider" ? `divider-${i}` : item.type === "group" ? `group-${i}` : `item-${i}`);
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
function MenuItems({ items, onClick, portal }: { items: (MenuItemType | null | undefined)[]; onClick?: MenuProps["onClick"]; portal?: HTMLElement }) {
|
|
@@ -61,6 +61,18 @@ function MenuItems({ items, onClick, portal }: { items: (MenuItemType | null | u
|
|
|
61
61
|
if (item.type === "divider") {
|
|
62
62
|
return <DropdownMenu.Separator key={key} className="my-1 h-px bg-border" />;
|
|
63
63
|
}
|
|
64
|
+
if (item.type === "group") {
|
|
65
|
+
return (
|
|
66
|
+
<DropdownMenu.Group key={key} className={menuGroupClass}>
|
|
67
|
+
{item.label != null && item.label !== "" ? (
|
|
68
|
+
<DropdownMenu.Label className={cn(menuGroupLabelClass, item.className)} style={item.style}>
|
|
69
|
+
{item.label}
|
|
70
|
+
</DropdownMenu.Label>
|
|
71
|
+
) : null}
|
|
72
|
+
<MenuItems items={item.children ?? []} onClick={onClick} portal={portal} />
|
|
73
|
+
</DropdownMenu.Group>
|
|
74
|
+
);
|
|
75
|
+
}
|
|
64
76
|
if (item.children?.length) {
|
|
65
77
|
return (
|
|
66
78
|
<DropdownMenu.Sub key={key}>
|
|
@@ -2,8 +2,12 @@ import { cn } from "../lib/cn";
|
|
|
2
2
|
import { glassOverlayClass } from "../lib/surface";
|
|
3
3
|
|
|
4
4
|
export const menuItemClass =
|
|
5
|
-
"nonla-menu-item relative flex cursor-default select-none items-center gap-2 rounded-
|
|
5
|
+
"nonla-menu-item relative flex cursor-default select-none items-center gap-2 rounded-lg border border-solid border-transparent px-2.5 py-1 text-sm leading-5 outline-none data-[disabled]:opacity-40";
|
|
6
6
|
|
|
7
7
|
export const menuContentClass = cn(glassOverlayClass, "min-w-40 overflow-hidden p-1");
|
|
8
8
|
|
|
9
9
|
export const menuIconClass = "inline-flex size-4 shrink-0 items-center justify-center [&_img]:size-4 [&_svg]:size-4";
|
|
10
|
+
|
|
11
|
+
export const menuGroupClass = "not-first:pt-2.5";
|
|
12
|
+
|
|
13
|
+
export const menuGroupLabelClass = "px-2.5 pb-1 pt-1 text-xs font-medium leading-4 text-tertiary-foreground";
|
package/src/index.ts
CHANGED
|
@@ -16,13 +16,13 @@ export { Alert } from "./alert/Alert";
|
|
|
16
16
|
export type { CalendarProps } from "./calendar/Calendar";
|
|
17
17
|
export { Calendar } from "./calendar/Calendar";
|
|
18
18
|
export type {
|
|
19
|
+
AgentChatboxProps,
|
|
19
20
|
AgentHistoryMessage,
|
|
20
21
|
AgentMessage,
|
|
21
22
|
AgentMessageRole,
|
|
22
23
|
AgentPanelEndpoint,
|
|
23
24
|
AgentPanelProps,
|
|
24
25
|
AgentStreamRequest,
|
|
25
|
-
AgentToolAction,
|
|
26
26
|
AgentToolCallEvent,
|
|
27
27
|
AgentToolHook,
|
|
28
28
|
AgentToolNameMatch,
|
|
@@ -45,6 +45,7 @@ export type {
|
|
|
45
45
|
ToolUIProps,
|
|
46
46
|
} from "./chat";
|
|
47
47
|
export {
|
|
48
|
+
AgentChatbox,
|
|
48
49
|
AgentPanel,
|
|
49
50
|
BackgroundTasksBar,
|
|
50
51
|
BackgroundTaskToolUI,
|
|
@@ -81,6 +82,7 @@ export {
|
|
|
81
82
|
RunJsToolUI,
|
|
82
83
|
resolveToolUI,
|
|
83
84
|
ToolUiTrailing,
|
|
85
|
+
useAgentChatStream,
|
|
84
86
|
useAgentStream,
|
|
85
87
|
WebFetchToolUI,
|
|
86
88
|
} from "./chat";
|