devnonla-ui 0.4.3 → 0.5.1
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/button/Button.tsx +1 -1
- package/src/chat/AgentChatbox.tsx +112 -0
- package/src/chat/AgentPanel.tsx +7 -100
- 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 +20 -5
- package/src/chat/tool-ui/ChatToolCall.tsx +28 -28
- package/src/colorpicker/ColorPicker.tsx +1 -1
- package/src/datepicker/DatePicker.tsx +2 -2
- package/src/dropdown/ContextMenu.tsx +15 -3
- package/src/dropdown/Dropdown.tsx +16 -4
- package/src/dropdown/menuClasses.ts +5 -1
- package/src/index.ts +3 -1
- package/src/input/Input.tsx +2 -2
- package/src/menu/Menu.tsx +1 -1
- package/src/pagination/Pagination.tsx +2 -2
- package/src/segmented/Segmented.tsx +53 -4
- package/src/select/Select.tsx +5 -3
- package/src/splitter/Splitter.tsx +6 -12
- package/src/styles.css +19 -2
- package/src/tag/Tag.tsx +1 -1
- package/src/timepicker/TimePicker.tsx +5 -5
|
@@ -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";
|
|
@@ -23,10 +23,15 @@ export type ChatInputProps = {
|
|
|
23
23
|
|
|
24
24
|
function resize(el: HTMLTextAreaElement | null, maxRows = 10) {
|
|
25
25
|
if (!el) return;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
// Collapse first — `height: auto` can keep a stretched/flex height so scrollHeight
|
|
27
|
+
// stays inflated until a later text change remeasures.
|
|
28
|
+
el.style.height = "0px";
|
|
29
|
+
const styles = getComputedStyle(el);
|
|
30
|
+
const line = Number.parseFloat(styles.lineHeight) || 22;
|
|
31
|
+
const padY = (Number.parseFloat(styles.paddingTop) || 0) + (Number.parseFloat(styles.paddingBottom) || 0);
|
|
32
|
+
const min = line + padY;
|
|
33
|
+
const max = line * maxRows + padY;
|
|
34
|
+
el.style.height = `${Math.min(Math.max(el.scrollHeight, min), max)}px`;
|
|
30
35
|
}
|
|
31
36
|
|
|
32
37
|
function isEditableTarget(el: EventTarget | null): boolean {
|
|
@@ -46,6 +51,16 @@ export function ChatInput({ generating = false, placeholder = "Message…", disa
|
|
|
46
51
|
resize(textareaRef.current);
|
|
47
52
|
}, [text]);
|
|
48
53
|
|
|
54
|
+
// Remeasure when the composer width settles (first paint / panel resize).
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
const el = textareaRef.current;
|
|
57
|
+
const parent = el?.parentElement;
|
|
58
|
+
if (!el || !parent) return;
|
|
59
|
+
const ro = new ResizeObserver(() => resize(el));
|
|
60
|
+
ro.observe(parent);
|
|
61
|
+
return () => ro.disconnect();
|
|
62
|
+
}, []);
|
|
63
|
+
|
|
49
64
|
useEffect(() => {
|
|
50
65
|
const onSignal = focusSignal !== undefined;
|
|
51
66
|
if (!autoFocus && !onSignal) return;
|
|
@@ -129,7 +144,7 @@ export function ChatInput({ generating = false, placeholder = "Message…", disa
|
|
|
129
144
|
<div className="flex items-center gap-1.5 pb-2 px-2">
|
|
130
145
|
{onClear ? (
|
|
131
146
|
<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={
|
|
147
|
+
<Eraser size={14} strokeWidth={1.5} aria-hidden />
|
|
133
148
|
</button>
|
|
134
149
|
) : null}
|
|
135
150
|
{toolbar}
|
|
@@ -28,44 +28,44 @@ export function ChatToolCall({ toolName = "Tool", label, toolInput, toolOutput,
|
|
|
28
28
|
const [open, setOpen] = useState(defaultOpen);
|
|
29
29
|
const displayLabel = label ?? formatToolName(toolName);
|
|
30
30
|
|
|
31
|
-
const identityIcon = icon ?? <FluentIcon name="
|
|
31
|
+
const identityIcon = icon ?? <FluentIcon name="code-block-24" size={13} className="shrink-0" />;
|
|
32
32
|
|
|
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
|
);
|
|
@@ -606,7 +606,7 @@ export function ColorPicker({
|
|
|
606
606
|
<button
|
|
607
607
|
type="button"
|
|
608
608
|
aria-label="Clear"
|
|
609
|
-
className="inline-flex size-6 shrink-0 cursor-pointer items-center justify-center rounded-md text-muted-foreground hover:bg-
|
|
609
|
+
className="inline-flex size-6 shrink-0 cursor-pointer items-center justify-center rounded-md text-muted-foreground hover:bg-ink-hover hover:text-foreground"
|
|
610
610
|
onClick={() => clear()}
|
|
611
611
|
>
|
|
612
612
|
<ClearIcon />
|
|
@@ -269,7 +269,7 @@ const DatePickerRoot = forwardRef<HTMLButtonElement, DatePickerProps>(function D
|
|
|
269
269
|
role="button"
|
|
270
270
|
tabIndex={-1}
|
|
271
271
|
aria-label="Clear"
|
|
272
|
-
className="inline-flex size-4 shrink-0 cursor-pointer items-center justify-center rounded-full text-muted-foreground opacity-0 transition-opacity hover:bg-
|
|
272
|
+
className="inline-flex size-4 shrink-0 cursor-pointer items-center justify-center rounded-full text-muted-foreground opacity-0 transition-opacity hover:bg-ink-hover hover:text-foreground group-hover/datepicker:opacity-100"
|
|
273
273
|
onClick={clear}
|
|
274
274
|
onPointerDown={(e) => e.preventDefault()}
|
|
275
275
|
>
|
|
@@ -408,7 +408,7 @@ const RangePicker = forwardRef<HTMLButtonElement, RangePickerProps>(function Ran
|
|
|
408
408
|
role="button"
|
|
409
409
|
tabIndex={-1}
|
|
410
410
|
aria-label="Clear"
|
|
411
|
-
className="inline-flex size-4 shrink-0 cursor-pointer items-center justify-center rounded-full text-muted-foreground opacity-0 transition-opacity hover:bg-
|
|
411
|
+
className="inline-flex size-4 shrink-0 cursor-pointer items-center justify-center rounded-full text-muted-foreground opacity-0 transition-opacity hover:bg-ink-hover hover:text-foreground group-hover/datepicker:opacity-100"
|
|
412
412
|
onClick={clear}
|
|
413
413
|
onPointerDown={(e) => e.preventDefault()}
|
|
414
414
|
>
|
|
@@ -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,10 +44,22 @@ 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}>
|
|
50
|
-
<ContextMenuPrimitive.SubTrigger disabled={item.disabled} className={cn(menuItemClass, "data-[state=open]:bg-
|
|
62
|
+
<ContextMenuPrimitive.SubTrigger disabled={item.disabled} className={cn(menuItemClass, "data-[state=open]:bg-ink-active", item.danger && "text-destructive", item.className)} style={item.style}>
|
|
51
63
|
{item.icon ? <span className={menuIconClass}>{item.icon}</span> : null}
|
|
52
64
|
<span className="min-w-0 flex-1">{item.label}</span>
|
|
53
65
|
<ChevronRight />
|