devnonla-ui 0.4.1 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "devnonla-ui",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "type": "module",
5
5
  "description": "NonlaUI — React controls for Nonla Agents",
6
6
  "author": "devlangla <devlangla0x01@gmail.com>",
@@ -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-(length:--chat-body-size) leading-5.5 font-medium">{status}</Shimmer>
196
+ <Shimmer className="text-sm font-medium text-tertiary-foreground">{status}</Shimmer>
197
197
  </div>
198
198
  ) : null}
199
199
  </div>
@@ -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.toolCallId as string | undefined,
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.toolCallId as string | undefined,
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>);
@@ -172,6 +214,7 @@ export function useAgentStream({ endpoint, extraBody, headers, fetcher = fetch,
172
214
  let assistantText = "";
173
215
  let currentId = assistantId;
174
216
  let needsNewBubble = false;
217
+ const seenToolCallIds = new Set<string>();
175
218
 
176
219
  const freezeOpen = () => {
177
220
  const freezeId = currentId;
@@ -298,33 +341,62 @@ export function useAgentStream({ endpoint, extraBody, headers, fetcher = fetch,
298
341
  },
299
342
  onToolCall: (event) => {
300
343
  const label = event.toolLabel ? (event.toolLabel.includes(" ") ? event.toolLabel : formatToolName(event.toolLabel)) : formatToolName(event.toolName);
301
- freezeOpen();
302
- const id = nextId("tc");
344
+ if (event.toolCallId) seenToolCallIds.add(event.toolCallId);
345
+
303
346
  const call: AgentToolCallEvent = {
304
347
  toolCallId: event.toolCallId,
305
348
  toolName: event.toolName,
306
349
  toolLabel: label,
307
350
  input: event.input,
308
351
  };
309
- openToolsRef.current.set(event.toolCallId || id, call);
310
- setMessages((prev) => [
311
- ...prev,
312
- {
313
- id,
314
- role: "tool-call",
315
- content: event.toolName,
316
- toolCallId: event.toolCallId,
317
- toolName: event.toolName,
318
- toolLabel: label,
319
- toolInput: event.input,
320
- timestamp: new Date(),
321
- },
322
- ]);
323
- emitCall(call);
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);
324
395
  },
325
396
  onToolResult: (event) => {
326
397
  const raw = event.result;
327
398
  const resultStr = typeof raw === "string" ? raw : JSON.stringify(raw);
399
+ const resultLabel = formatToolName(event.toolName);
328
400
  if (event.toolCallId) {
329
401
  openToolsRef.current.delete(event.toolCallId);
330
402
  } else {
@@ -335,17 +407,35 @@ export function useAgentStream({ endpoint, extraBody, headers, fetcher = fetch,
335
407
  }
336
408
  }
337
409
  }
410
+ freezeOpen();
338
411
  setMessages((prev) => {
339
412
  let matchIdx = -1;
340
413
  if (event.toolCallId) {
341
- matchIdx = prev.findIndex((m) => m.role === "tool-call" && m.toolCallId === event.toolCallId && m.toolOutput == null);
414
+ matchIdx = prev.findIndex((m) => m.role === "tool-call" && m.toolCallId === event.toolCallId);
342
415
  }
343
416
  if (matchIdx === -1) {
344
- const rev = [...prev].reverse().findIndex((m) => m.role === "tool-call" && m.toolName === event.toolName && !m.toolOutput);
345
- matchIdx = rev === -1 ? -1 : prev.length - 1 - rev;
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));
346
424
  }
347
- if (matchIdx === -1) return prev;
348
- return prev.map((m, i) => (i === matchIdx ? { ...m, toolOutput: resultStr } : m));
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
+ ];
349
439
  });
350
440
  emitResult({ toolCallId: event.toolCallId, toolName: event.toolName, output: raw });
351
441
  },