devnonla-ui 0.4.2 → 0.4.3
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/package.json +1 -1
- package/src/chat/AgentPanel.tsx +1 -1
- package/src/chat/common/sse.ts +13 -7
- package/src/chat/common/useAgentStream.ts +106 -45
package/package.json
CHANGED
package/src/chat/AgentPanel.tsx
CHANGED
|
@@ -193,7 +193,7 @@ export function AgentPanel({
|
|
|
193
193
|
))}
|
|
194
194
|
{showFooter ? (
|
|
195
195
|
<div className="mt-1 px-4 pb-0.5">
|
|
196
|
-
<Shimmer className="text-
|
|
196
|
+
<Shimmer className="text-sm font-medium text-tertiary-foreground">{status}</Shimmer>
|
|
197
197
|
</div>
|
|
198
198
|
) : null}
|
|
199
199
|
</div>
|
package/src/chat/common/sse.ts
CHANGED
|
@@ -26,6 +26,12 @@ export interface AgentSseCallbacks {
|
|
|
26
26
|
|
|
27
27
|
export type ParseSseResult = "done" | "error" | "aborted" | "connection-lost";
|
|
28
28
|
|
|
29
|
+
function readToolCallId(raw: Record<string, unknown>): string | undefined {
|
|
30
|
+
const v = raw.toolCallId ?? raw.tool_call_id ?? raw.callId ?? raw.call_id;
|
|
31
|
+
if (typeof v === "string" && v.trim()) return v;
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
29
35
|
export function normalizeSseEvent(raw: Record<string, unknown>): AgentSseEvent | null {
|
|
30
36
|
const type = raw.type;
|
|
31
37
|
if (type === "ping") return null;
|
|
@@ -38,18 +44,18 @@ export function normalizeSseEvent(raw: Record<string, unknown>): AgentSseEvent |
|
|
|
38
44
|
if (type === "tool-call") {
|
|
39
45
|
return {
|
|
40
46
|
type: "tool-call",
|
|
41
|
-
toolCallId: raw
|
|
42
|
-
toolName: String(raw.toolName ?? "unknown"),
|
|
43
|
-
toolLabel: raw.toolLabel as string | undefined,
|
|
44
|
-
input: raw.input,
|
|
47
|
+
toolCallId: readToolCallId(raw),
|
|
48
|
+
toolName: String(raw.toolName ?? raw.name ?? "unknown"),
|
|
49
|
+
toolLabel: (raw.toolLabel as string | undefined) ?? (raw.label as string | undefined),
|
|
50
|
+
input: raw.input ?? raw.arguments ?? raw.args,
|
|
45
51
|
};
|
|
46
52
|
}
|
|
47
53
|
if (type === "tool-result") {
|
|
48
54
|
return {
|
|
49
55
|
type: "tool-result",
|
|
50
|
-
toolCallId: raw
|
|
51
|
-
toolName: String(raw.toolName ?? "unknown"),
|
|
52
|
-
result: raw.result,
|
|
56
|
+
toolCallId: readToolCallId(raw),
|
|
57
|
+
toolName: String(raw.toolName ?? raw.name ?? "unknown"),
|
|
58
|
+
result: raw.result ?? raw.output,
|
|
53
59
|
};
|
|
54
60
|
}
|
|
55
61
|
if (type === "context-usage" || type === "token-usage") return null;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
2
|
import { parseSseStream } from "./sse";
|
|
3
3
|
import type { AgentHistoryMessage, AgentMessage, AgentPanelEndpoint, AgentToolAction, AgentToolCallEvent, AgentToolHook, AgentToolResultEvent } from "./types";
|
|
4
|
-
import { formatToolName, matchesToolHook } from "./utils";
|
|
4
|
+
import { formatToolName, hasMeaningfulInput, matchesToolHook } from "./utils";
|
|
5
5
|
|
|
6
6
|
let _id = 0;
|
|
7
7
|
function nextId(prefix: string) {
|
|
@@ -62,6 +62,48 @@ function failOpenTools(prev: AgentMessage[], error: string): AgentMessage[] {
|
|
|
62
62
|
});
|
|
63
63
|
}
|
|
64
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
|
+
}
|
|
106
|
+
|
|
65
107
|
export type UseAgentStreamOptions = {
|
|
66
108
|
endpoint: AgentPanelEndpoint;
|
|
67
109
|
extraBody?: Record<string, unknown> | (() => Record<string, unknown>);
|
|
@@ -299,61 +341,62 @@ export function useAgentStream({ endpoint, extraBody, headers, fetcher = fetch,
|
|
|
299
341
|
},
|
|
300
342
|
onToolCall: (event) => {
|
|
301
343
|
const label = event.toolLabel ? (event.toolLabel.includes(" ") ? event.toolLabel : formatToolName(event.toolLabel)) : formatToolName(event.toolName);
|
|
302
|
-
const alreadySeen = event.toolCallId ? seenToolCallIds.has(event.toolCallId) : false;
|
|
303
344
|
if (event.toolCallId) seenToolCallIds.add(event.toolCallId);
|
|
304
345
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
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
|
|
309
366
|
? {
|
|
310
367
|
...m,
|
|
311
368
|
content: event.toolName,
|
|
369
|
+
toolCallId: nextId,
|
|
312
370
|
toolName: event.toolName,
|
|
313
371
|
toolLabel: label,
|
|
314
|
-
toolInput:
|
|
372
|
+
toolInput: nextInput,
|
|
315
373
|
}
|
|
316
374
|
: m,
|
|
317
|
-
)
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
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,
|
|
321
386
|
toolCallId: event.toolCallId,
|
|
322
387
|
toolName: event.toolName,
|
|
323
388
|
toolLabel: label,
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
freezeOpen();
|
|
331
|
-
const id = nextId("tc");
|
|
332
|
-
const call: AgentToolCallEvent = {
|
|
333
|
-
toolCallId: event.toolCallId,
|
|
334
|
-
toolName: event.toolName,
|
|
335
|
-
toolLabel: label,
|
|
336
|
-
input: event.input,
|
|
337
|
-
};
|
|
338
|
-
openToolsRef.current.set(event.toolCallId || id, call);
|
|
339
|
-
setMessages((prev) => [
|
|
340
|
-
...prev,
|
|
341
|
-
{
|
|
342
|
-
id,
|
|
343
|
-
role: "tool-call",
|
|
344
|
-
content: event.toolName,
|
|
345
|
-
toolCallId: event.toolCallId,
|
|
346
|
-
toolName: event.toolName,
|
|
347
|
-
toolLabel: label,
|
|
348
|
-
toolInput: event.input,
|
|
349
|
-
timestamp: new Date(),
|
|
350
|
-
},
|
|
351
|
-
]);
|
|
352
|
-
emitCall(call);
|
|
389
|
+
toolInput: event.input,
|
|
390
|
+
timestamp: new Date(),
|
|
391
|
+
},
|
|
392
|
+
];
|
|
393
|
+
});
|
|
394
|
+
if (event.input !== undefined) emitCall(call);
|
|
353
395
|
},
|
|
354
396
|
onToolResult: (event) => {
|
|
355
397
|
const raw = event.result;
|
|
356
398
|
const resultStr = typeof raw === "string" ? raw : JSON.stringify(raw);
|
|
399
|
+
const resultLabel = formatToolName(event.toolName);
|
|
357
400
|
if (event.toolCallId) {
|
|
358
401
|
openToolsRef.current.delete(event.toolCallId);
|
|
359
402
|
} else {
|
|
@@ -364,17 +407,35 @@ export function useAgentStream({ endpoint, extraBody, headers, fetcher = fetch,
|
|
|
364
407
|
}
|
|
365
408
|
}
|
|
366
409
|
}
|
|
410
|
+
freezeOpen();
|
|
367
411
|
setMessages((prev) => {
|
|
368
412
|
let matchIdx = -1;
|
|
369
413
|
if (event.toolCallId) {
|
|
370
|
-
matchIdx = prev.findIndex((m) => m.role === "tool-call" && m.toolCallId === event.toolCallId
|
|
414
|
+
matchIdx = prev.findIndex((m) => m.role === "tool-call" && m.toolCallId === event.toolCallId);
|
|
371
415
|
}
|
|
372
416
|
if (matchIdx === -1) {
|
|
373
|
-
|
|
374
|
-
|
|
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));
|
|
375
424
|
}
|
|
376
|
-
|
|
377
|
-
return
|
|
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
|
+
];
|
|
378
439
|
});
|
|
379
440
|
emitResult({ toolCallId: event.toolCallId, toolName: event.toolName, output: raw });
|
|
380
441
|
},
|