devnonla-ui 0.4.2 → 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.
@@ -1,66 +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 { AgentHistoryMessage, AgentMessage, AgentPanelEndpoint, AgentToolAction, AgentToolCallEvent, AgentToolHook, AgentToolResultEvent } from "./types";
4
- import { formatToolName, matchesToolHook } from "./utils";
5
+ import type { AgentMessage, AgentPanelEndpoint, AgentToolCallEvent, AgentToolHook, AgentToolResultEvent } from "./types";
6
+ import { matchesToolHook } from "./utils";
5
7
 
6
- let _id = 0;
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
- }
8
+ export { buildAgentHistory } from "./agentStreamHelpers";
64
9
 
65
10
  export type UseAgentStreamOptions = {
66
11
  endpoint: AgentPanelEndpoint;
@@ -68,20 +13,24 @@ export type UseAgentStreamOptions = {
68
13
  headers?: Record<string, string>;
69
14
  fetcher?: typeof fetch;
70
15
  initialMessages?: AgentMessage[];
71
- onToolAction?: (event: AgentToolAction) => void;
72
16
  toolHooks?: AgentToolHook[];
73
17
  };
74
18
 
75
19
  type OpenTool = AgentToolCallEvent;
76
20
 
77
- export function useAgentStream({ endpoint, extraBody, headers, fetcher = fetch, initialMessages, onToolAction, toolHooks }: UseAgentStreamOptions) {
21
+ export function useAgentStream({
22
+ endpoint,
23
+ extraBody,
24
+ headers,
25
+ fetcher = fetch,
26
+ initialMessages,
27
+ toolHooks,
28
+ }: UseAgentStreamOptions) {
78
29
  const [messages, setMessages] = useState<AgentMessage[]>(() => initialMessages ?? []);
79
30
  const [generating, setGenerating] = useState(false);
80
31
  const thinkingRef = useRef("");
81
32
  const thinkingStartRef = useRef(0);
82
33
  const abortRef = useRef<AbortController | null>(null);
83
- const onToolActionRef = useRef(onToolAction);
84
- onToolActionRef.current = onToolAction;
85
34
  const toolHooksRef = useRef(toolHooks);
86
35
  toolHooksRef.current = toolHooks;
87
36
  const extraBodyRef = useRef(extraBody);
@@ -94,14 +43,12 @@ export function useAgentStream({ endpoint, extraBody, headers, fetcher = fetch,
94
43
  for (const hook of toolHooksRef.current ?? []) {
95
44
  if (matchesToolHook(hook.name, event.toolName)) hook.onCall?.(event);
96
45
  }
97
- onToolActionRef.current?.({ type: "tool-call", ...event });
98
46
  }, []);
99
47
 
100
48
  const emitResult = useCallback((event: AgentToolResultEvent) => {
101
49
  for (const hook of toolHooksRef.current ?? []) {
102
50
  if (matchesToolHook(hook.name, event.toolName)) hook.onResult?.(event);
103
51
  }
104
- onToolActionRef.current?.({ type: "tool-result", ...event });
105
52
  }, []);
106
53
 
107
54
  const flushOpenTools = useCallback(
@@ -169,83 +116,17 @@ export function useAgentStream({ endpoint, extraBody, headers, fetcher = fetch,
169
116
  thinkingStartRef.current = 0;
170
117
 
171
118
  const history = buildAgentHistory(historySnapshot);
172
- let assistantText = "";
173
- let currentId = assistantId;
174
- let needsNewBubble = false;
175
- const seenToolCallIds = new Set<string>();
176
-
177
- const freezeOpen = () => {
178
- const freezeId = currentId;
179
- if (freezeId) {
180
- const thinkingSnapshot = thinkingRef.current;
181
- const liveDuration = thinkingStartRef.current > 0 ? thinkingDurationSec(thinkingStartRef.current) : undefined;
182
- if (assistantText.trim()) {
183
- setMessages((prev) =>
184
- prev.map((m) => {
185
- if (m.id !== freezeId) return m;
186
- const existing = typeof m.meta?.thinkingDuration === "number" ? m.meta.thinkingDuration : undefined;
187
- return {
188
- ...m,
189
- content: assistantText,
190
- streaming: false,
191
- meta: thinkingSnapshot ? { ...m.meta, thinking: thinkingSnapshot, thinkingDuration: liveDuration ?? existing ?? 0 } : m.meta,
192
- };
193
- }),
194
- );
195
- } else if (thinkingSnapshot) {
196
- setMessages((prev) =>
197
- prev.map((m) =>
198
- m.id === freezeId
199
- ? {
200
- id: freezeId,
201
- role: "thinking" as const,
202
- content: thinkingSnapshot,
203
- streaming: false,
204
- timestamp: m.timestamp,
205
- meta: { thinking: thinkingSnapshot, thinkingDuration: liveDuration ?? 0 },
206
- }
207
- : m,
208
- ),
209
- );
210
- } else {
211
- setMessages((prev) => prev.filter((m) => m.id !== freezeId));
212
- }
213
- }
214
- thinkingRef.current = "";
215
- thinkingStartRef.current = 0;
216
- assistantText = "";
217
- currentId = "";
218
- needsNewBubble = true;
219
- };
220
-
221
- const ensureBubble = (seed?: { content?: string; thinking?: string }): string => {
222
- if (!needsNewBubble && currentId) return currentId;
223
- const newId = nextId("a");
224
- currentId = newId;
225
- needsNewBubble = false;
226
- setMessages((prev) => [
227
- ...prev,
228
- {
229
- id: newId,
230
- role: "assistant",
231
- content: seed?.content ?? "",
232
- streaming: true,
233
- timestamp: new Date(),
234
- meta: seed?.thinking ? { thinking: seed.thinking } : undefined,
235
- },
236
- ]);
237
- return newId;
238
- };
239
-
240
- const stampThinkingDuration = () => {
241
- if (!thinkingRef.current || !thinkingStartRef.current) return;
242
- const duration = thinkingDurationSec(thinkingStartRef.current);
243
- const targetId = currentId;
244
- if (targetId) {
245
- setMessages((prev) => prev.map((m) => (m.id === targetId ? { ...m, meta: { ...m.meta, thinkingDuration: duration } } : m)));
246
- }
247
- thinkingStartRef.current = 0;
248
- };
119
+ const session = createAgentSseSession({
120
+ setMessages,
121
+ setGenerating,
122
+ thinkingRef,
123
+ thinkingStartRef,
124
+ openToolsRef,
125
+ emitCall,
126
+ emitResult,
127
+ flushOpenTools,
128
+ assistantId,
129
+ });
249
130
 
250
131
  try {
251
132
  const extra = typeof extraBodyRef.current === "function" ? extraBodyRef.current() : extraBodyRef.current;
@@ -267,174 +148,15 @@ export function useAgentStream({ endpoint, extraBody, headers, fetcher = fetch,
267
148
  throw new Error(errText || `HTTP ${response.status}`);
268
149
  }
269
150
 
270
- const result = await parseSseStream(
271
- response.body,
272
- {
273
- onTextDelta: (delta) => {
274
- stampThinkingDuration();
275
- if (needsNewBubble || !currentId) {
276
- assistantText = delta;
277
- thinkingRef.current = "";
278
- thinkingStartRef.current = 0;
279
- ensureBubble({ content: delta });
280
- } else {
281
- assistantText += delta;
282
- const targetId = currentId;
283
- setMessages((prev) => prev.map((m) => (m.id === targetId ? { ...m, content: assistantText, streaming: true } : m)));
284
- }
285
- },
286
- onThinkingDelta: (delta) => {
287
- if (needsNewBubble || !currentId) {
288
- thinkingRef.current = delta;
289
- thinkingStartRef.current = Date.now();
290
- assistantText = "";
291
- ensureBubble({ thinking: delta });
292
- return;
293
- }
294
- if (!thinkingRef.current) thinkingStartRef.current = Date.now();
295
- thinkingRef.current += delta;
296
- const thinking = thinkingRef.current;
297
- const targetId = currentId;
298
- setMessages((prev) => prev.map((m) => (m.id === targetId ? { ...m, meta: { ...m.meta, thinking } } : m)));
299
- },
300
- onToolCall: (event) => {
301
- 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
- if (event.toolCallId) seenToolCallIds.add(event.toolCallId);
304
-
305
- if (alreadySeen) {
306
- setMessages((prev) =>
307
- prev.map((m) =>
308
- m.role === "tool-call" && m.toolCallId === event.toolCallId
309
- ? {
310
- ...m,
311
- content: event.toolName,
312
- toolName: event.toolName,
313
- toolLabel: label,
314
- toolInput: event.input !== undefined ? event.input : m.toolInput,
315
- }
316
- : m,
317
- ),
318
- );
319
- if (event.input !== undefined) {
320
- emitCall({
321
- toolCallId: event.toolCallId,
322
- toolName: event.toolName,
323
- toolLabel: label,
324
- input: event.input,
325
- });
326
- }
327
- return;
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);
353
- },
354
- onToolResult: (event) => {
355
- const raw = event.result;
356
- const resultStr = typeof raw === "string" ? raw : JSON.stringify(raw);
357
- if (event.toolCallId) {
358
- openToolsRef.current.delete(event.toolCallId);
359
- } else {
360
- for (const [key, tool] of [...openToolsRef.current.entries()].reverse()) {
361
- if (tool.toolName === event.toolName) {
362
- openToolsRef.current.delete(key);
363
- break;
364
- }
365
- }
366
- }
367
- setMessages((prev) => {
368
- let matchIdx = -1;
369
- if (event.toolCallId) {
370
- matchIdx = prev.findIndex((m) => m.role === "tool-call" && m.toolCallId === event.toolCallId && m.toolOutput == null);
371
- }
372
- if (matchIdx === -1) {
373
- const rev = [...prev].reverse().findIndex((m) => m.role === "tool-call" && m.toolName === event.toolName && !m.toolOutput);
374
- matchIdx = rev === -1 ? -1 : prev.length - 1 - rev;
375
- }
376
- if (matchIdx === -1) return prev;
377
- return prev.map((m, i) => (i === matchIdx ? { ...m, toolOutput: resultStr } : m));
378
- });
379
- emitResult({ toolCallId: event.toolCallId, toolName: event.toolName, output: raw });
380
- },
381
- onDone: () => {
382
- flushOpenTools("Tool did not return a result");
383
- setMessages((prev) => finalizeStreaming(failOpenTools(prev, "Tool did not return a result")));
384
- setGenerating(false);
385
- thinkingRef.current = "";
386
- thinkingStartRef.current = 0;
387
- },
388
- onError: (error) => {
389
- const reason = error === "cancelled" ? "Cancelled" : error;
390
- flushOpenTools(reason);
391
- if (error === "Connection lost") {
392
- setMessages((prev) => finalizeStreaming(failOpenTools(prev, "Connection lost")));
393
- setGenerating(false);
394
- thinkingRef.current = "";
395
- thinkingStartRef.current = 0;
396
- return;
397
- }
398
- setMessages((prev) => [
399
- ...finalizeStreaming(failOpenTools(prev, reason)),
400
- ...(error === "cancelled" ? [] : [{ id: nextId("err"), role: "error" as const, content: error, timestamp: new Date() }]),
401
- ]);
402
- setGenerating(false);
403
- thinkingRef.current = "";
404
- thinkingStartRef.current = 0;
405
- },
406
- },
407
- { signal: controller.signal },
408
- );
409
-
410
- if (result === "aborted") {
411
- flushOpenTools("Cancelled");
412
- setMessages((prev) => finalizeStreaming(failOpenTools(prev, "Cancelled")));
413
- setGenerating(false);
414
- thinkingRef.current = "";
415
- thinkingStartRef.current = 0;
416
- return;
417
- }
418
-
419
- flushOpenTools("Tool did not return a result");
420
- setMessages((prev) => finalizeStreaming(failOpenTools(prev, "Tool did not return a result")));
421
- setGenerating(false);
151
+ const result = await parseSseStream(response.body, session.callbacks, { signal: controller.signal });
152
+ session.finishAfterParse(result);
422
153
  } catch (err: unknown) {
423
154
  if ((err as Error)?.name === "AbortError") {
424
- flushOpenTools("Cancelled");
425
- setMessages((prev) => finalizeStreaming(failOpenTools(prev, "Cancelled")));
426
- setGenerating(false);
427
- thinkingRef.current = "";
428
- thinkingStartRef.current = 0;
155
+ session.finishAbortError();
429
156
  return;
430
157
  }
431
158
  const message = err instanceof Error ? err.message : String(err);
432
- flushOpenTools(message);
433
- setMessages((prev) => [
434
- ...finalizeStreaming(failOpenTools(prev, message)),
435
- { id: nextId("err"), role: "error", content: message, timestamp: new Date() },
436
- ]);
437
- setGenerating(false);
159
+ session.finishThrown(message);
438
160
  } finally {
439
161
  abortRef.current = null;
440
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, AgentToolAction, AgentToolCallEvent, AgentToolHook, AgentToolNameMatch, AgentToolResultEvent } from "./common/types";
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={16} aria-hidden />
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
- <Shimmer active={running} className={cn("min-w-0 flex-1 truncate text-left text-[14px] font-medium", !running && "text-muted-foreground", expandable && !running && "transition-colors group-hover:text-foreground")}>{displayLabel}</Shimmer>
37
- <ToolUiTrailing failed={hasError} chevron={expandable} chevronClassName={cn("group-hover:opacity-100", open && "opacity-100 rotate-90")} />
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-0.5">
44
- <div className={cn("mb-1 overflow-hidden rounded-md", open && "bg-card")}>
45
- {expandable ? (
46
- <button type="button" onClick={() => setOpen((v) => !v)} className="group flex w-full cursor-pointer items-center gap-2 border-0 bg-transparent px-2.5 py-1 outline-none">
47
- {header}
48
- </button>
49
- ) : (
50
- <div className="flex w-full items-center gap-2 bg-transparent px-2.5 py-1">{header}</div>
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
- {open && expandable ? (
54
- <div className="font-mono text-[13px]">
55
- {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}
56
- {running ? (
57
- <div className="bg-well-strong px-3 py-1.5 text-muted-foreground">
58
- <Shimmer className="italic">Running…</Shimmer>
59
- </div>
60
- ) : null}
61
- {!isPending ? (
62
- <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")}>
63
- {hasOutput ? prettyJson(toolOutput) : typeof toolError === "string" ? toolError : "Tool execution failed"}
64
- </pre>
65
- ) : null}
66
- </div>
67
- ) : null}
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-md border border-solid border-transparent px-2.5 py-1.5 text-sm outline-none data-[disabled]:opacity-40";
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";