@meshagent/meshagent-tailwind 0.55.1 → 0.56.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.
Files changed (38) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +34 -3
  3. package/dist/cjs/chat/agent-thread.d.ts +42 -7
  4. package/dist/cjs/chat/agent-thread.js +422 -183
  5. package/dist/cjs/chat/chat-input-state.d.ts +2 -0
  6. package/dist/cjs/chat/chat-input-state.js +41 -0
  7. package/dist/cjs/chat/chat-input.d.ts +14 -2
  8. package/dist/cjs/chat/chat-input.js +41 -76
  9. package/dist/cjs/chat/chat-scroller.js +45 -43
  10. package/dist/cjs/chat/chat-thread-timeline.d.ts +2 -0
  11. package/dist/cjs/chat/chat-thread-timeline.js +83 -10
  12. package/dist/cjs/chat/composer-action-button.d.ts +11 -0
  13. package/dist/cjs/chat/composer-action-button.js +68 -0
  14. package/dist/cjs/chat/use-chat-virtualizer.d.ts +14 -17
  15. package/dist/cjs/chat/use-chat-virtualizer.js +288 -222
  16. package/dist/cjs/chat/virtualization.d.ts +0 -5
  17. package/dist/cjs/chat/virtualization.js +4 -6
  18. package/dist/cjs/index.d.ts +1 -0
  19. package/dist/cjs/index.js +1 -0
  20. package/dist/esm/chat/agent-thread.d.ts +42 -7
  21. package/dist/esm/chat/agent-thread.js +424 -186
  22. package/dist/esm/chat/chat-input-state.d.ts +2 -0
  23. package/dist/esm/chat/chat-input-state.js +21 -0
  24. package/dist/esm/chat/chat-input.d.ts +14 -2
  25. package/dist/esm/chat/chat-input.js +41 -76
  26. package/dist/esm/chat/chat-scroller.js +46 -44
  27. package/dist/esm/chat/chat-thread-timeline.d.ts +2 -0
  28. package/dist/esm/chat/chat-thread-timeline.js +83 -10
  29. package/dist/esm/chat/composer-action-button.d.ts +11 -0
  30. package/dist/esm/chat/composer-action-button.js +48 -0
  31. package/dist/esm/chat/use-chat-virtualizer.d.ts +14 -17
  32. package/dist/esm/chat/use-chat-virtualizer.js +290 -226
  33. package/dist/esm/chat/virtualization.d.ts +0 -5
  34. package/dist/esm/chat/virtualization.js +4 -6
  35. package/dist/esm/index.d.ts +1 -0
  36. package/dist/esm/index.js +1 -0
  37. package/dist/index.css +1 -1
  38. package/package.json +6 -5
@@ -0,0 +1,2 @@
1
+ import type { FileUpload } from "./file-attachment.js";
2
+ export declare function useAttachmentStatusVersion(attachments: readonly FileUpload[]): number;
@@ -0,0 +1,21 @@
1
+ import { useEffect, useState } from "react";
2
+ function useAttachmentStatusVersion(attachments) {
3
+ const [version, setVersion] = useState(0);
4
+ useEffect(() => {
5
+ const handleChange = () => {
6
+ setVersion((currentVersion) => currentVersion + 1);
7
+ };
8
+ for (const attachment of attachments) {
9
+ attachment.on("change", handleChange);
10
+ }
11
+ return () => {
12
+ for (const attachment of attachments) {
13
+ attachment.off("change", handleChange);
14
+ }
15
+ };
16
+ }, [attachments]);
17
+ return version;
18
+ }
19
+ export {
20
+ useAttachmentStatusVersion
21
+ };
@@ -1,7 +1,16 @@
1
- import type { ReactElement } from "react";
1
+ import type { ComponentType, ReactElement, ReactNode, RefObject } from "react";
2
2
  import { ChatMessage } from "./chat-message.js";
3
3
  import { type FileUpload } from "./file-attachment.js";
4
+ import { type ComposerActionButtonProps } from "./composer-action-button.js";
4
5
  export interface ChatInputProps {
6
+ /** Classes for the outer composer wrapper. */
7
+ className?: string;
8
+ /** Classes for the row containing the buttons and textarea. */
9
+ inputRowClassName?: string;
10
+ /** Classes for the textarea. */
11
+ textareaClassName?: string;
12
+ /** Optional content before the file uploader and textarea. */
13
+ leadingButton?: ReactNode;
5
14
  onSubmit: (message: ChatMessage) => void | Promise<void>;
6
15
  onFilesSelected: (files: File[]) => void;
7
16
  attachments: FileUpload[];
@@ -10,6 +19,8 @@ export interface ChatInputProps {
10
19
  onTextChange?: (text: string) => void;
11
20
  onCancelRequest?: () => void;
12
21
  showCancelButton?: boolean;
22
+ showSubmitButton?: boolean;
23
+ submitButtonComponent?: ComponentType<ComposerActionButtonProps>;
13
24
  placeholder?: string;
14
25
  disabled?: boolean;
15
26
  clearOnSubmit?: boolean;
@@ -18,4 +29,5 @@ export interface ChatInputProps {
18
29
  defaultValue?: string;
19
30
  onValueChange?: (value: string) => void;
20
31
  }
21
- export declare function ChatInput({ onSubmit, onFilesSelected, attachments, setAttachments, enableFileUpload, onTextChange, onCancelRequest, showCancelButton, placeholder, disabled, clearOnSubmit, autoFocus, value: controlledValue, defaultValue, onValueChange, }: ChatInputProps): ReactElement;
32
+ export declare function useAutoResizingTextarea(textareaRef: RefObject<HTMLTextAreaElement | null>, value: string): void;
33
+ export declare function ChatInput({ className, inputRowClassName, textareaClassName, leadingButton, onSubmit, onFilesSelected, attachments, setAttachments, enableFileUpload, onTextChange, onCancelRequest, showCancelButton, showSubmitButton, submitButtonComponent: SubmitButton, placeholder, disabled, clearOnSubmit, autoFocus, value: controlledValue, defaultValue, onValueChange, }: ChatInputProps): ReactElement;
@@ -1,90 +1,42 @@
1
1
  import { jsx, jsxs } from "react/jsx-runtime";
2
- import { useRef, useState, useEffect, useMemo, useCallback } from "react";
3
- import { ArrowUp, LoaderCircle, X } from "lucide-react";
2
+ import { useRef, useState, useLayoutEffect, useMemo, useCallback } from "react";
4
3
  import { v4 as uuidV4 } from "uuid";
5
4
  import { ChatMessage } from "./chat-message.js";
6
- import { Button } from "../components/ui/button.js";
7
5
  import { FileUploader } from "./file-uploader.js";
8
6
  import { UploadPill } from "./upload-pill.js";
9
7
  import { UploadStatus } from "./file-attachment.js";
10
8
  import { cn } from "../lib/utils.js";
9
+ import { ComposerActionButton } from "./composer-action-button.js";
10
+ import { useAttachmentStatusVersion } from "./chat-input-state.js";
11
11
  const MIN_TEXTAREA_HEIGHT = 20;
12
12
  const MAX_TEXTAREA_HEIGHT = 160;
13
- function useAttachmentStatusVersion(attachments) {
14
- const [version, setVersion] = useState(0);
15
- useEffect(() => {
16
- const handleChange = () => {
17
- setVersion((currentVersion) => currentVersion + 1);
18
- };
19
- for (const attachment of attachments) {
20
- attachment.on("change", handleChange);
21
- }
22
- return () => {
23
- for (const attachment of attachments) {
24
- attachment.off("change", handleChange);
25
- }
26
- };
27
- }, [attachments]);
28
- return version;
13
+ function resizeTextarea(element) {
14
+ if (element.value === "") {
15
+ element.style.height = `${MIN_TEXTAREA_HEIGHT}px`;
16
+ element.style.overflowY = "hidden";
17
+ } else {
18
+ element.style.height = "0px";
19
+ element.style.overflowY = "hidden";
20
+ const contentHeight = element.scrollHeight;
21
+ const height = Math.max(MIN_TEXTAREA_HEIGHT, Math.min(contentHeight, MAX_TEXTAREA_HEIGHT));
22
+ element.style.height = `${height}px`;
23
+ element.style.overflowY = contentHeight > MAX_TEXTAREA_HEIGHT ? "auto" : "hidden";
24
+ }
29
25
  }
30
26
  function useAutoResizingTextarea(textareaRef, value) {
31
- useEffect(() => {
27
+ useLayoutEffect(() => {
32
28
  const element = textareaRef.current;
33
29
  if (!element) {
34
30
  return;
35
31
  }
36
- if (value === "") {
37
- element.style.height = `${MIN_TEXTAREA_HEIGHT}px`;
38
- element.style.overflowY = "hidden";
39
- } else {
40
- element.style.height = "0px";
41
- element.style.overflowY = "hidden";
42
- const contentHeight = element.scrollHeight;
43
- element.style.height = `${Math.max(
44
- MIN_TEXTAREA_HEIGHT,
45
- Math.min(contentHeight, MAX_TEXTAREA_HEIGHT)
46
- )}px`;
47
- element.style.overflowY = contentHeight > MAX_TEXTAREA_HEIGHT ? "auto" : "hidden";
48
- }
32
+ resizeTextarea(element);
49
33
  }, [textareaRef, value]);
50
34
  }
51
- function ComposerActionButton({
52
- onClick,
53
- disabled = false,
54
- showCancelButton = false
55
- }) {
56
- if (showCancelButton) {
57
- return /* @__PURE__ */ jsxs(
58
- Button,
59
- {
60
- type: "button",
61
- size: "icon",
62
- variant: "ghost",
63
- className: cn("relative size-9 rounded-full", disabled ? "opacity-55" : "cursor-pointer"),
64
- disabled,
65
- onClick,
66
- title: disabled ? "Cancelling" : "Stop",
67
- children: [
68
- /* @__PURE__ */ jsx(LoaderCircle, { className: "absolute h-9 w-9 animate-spin text-muted-foreground" }),
69
- /* @__PURE__ */ jsx("span", { className: "relative inline-flex h-5 w-5 items-center justify-center rounded-full bg-foreground text-background", children: /* @__PURE__ */ jsx(X, { className: "h-3 w-3" }) })
70
- ]
71
- }
72
- );
73
- }
74
- return /* @__PURE__ */ jsx(
75
- Button,
76
- {
77
- type: "button",
78
- size: "icon",
79
- className: cn("size-9 rounded-full shadow-xs", !disabled && "cursor-pointer"),
80
- disabled,
81
- onClick,
82
- title: "Send",
83
- children: /* @__PURE__ */ jsx(ArrowUp, { className: "h-4 w-4" })
84
- }
85
- );
86
- }
87
35
  function ChatInput({
36
+ className,
37
+ inputRowClassName,
38
+ textareaClassName,
39
+ leadingButton,
88
40
  onSubmit,
89
41
  onFilesSelected,
90
42
  attachments,
@@ -93,6 +45,8 @@ function ChatInput({
93
45
  onTextChange,
94
46
  onCancelRequest,
95
47
  showCancelButton = false,
48
+ showSubmitButton = true,
49
+ submitButtonComponent: SubmitButton = ComposerActionButton,
96
50
  placeholder = "Message the room",
97
51
  disabled = false,
98
52
  clearOnSubmit = true,
@@ -150,6 +104,7 @@ function ChatInput({
150
104
  }, [disabled, handleSend, onCancelRequest, showCancelButton]);
151
105
  const handleChange = useCallback((event) => {
152
106
  const nextValue = event.currentTarget.value;
107
+ resizeTextarea(event.currentTarget);
153
108
  setValue(nextValue);
154
109
  }, [setValue]);
155
110
  const cancelAttachment = useCallback((attachment) => {
@@ -158,8 +113,13 @@ function ChatInput({
158
113
  }
159
114
  setAttachments(attachments.filter((currentAttachment) => currentAttachment !== attachment));
160
115
  }, [attachments, disabled, setAttachments]);
161
- const trailingButton = showCancelButton ? /* @__PURE__ */ jsx(ComposerActionButton, { onClick: onCancelRequest, showCancelButton: true }) : /* @__PURE__ */ jsx(ComposerActionButton, { onClick: handleSend, disabled: !canSend });
162
- return /* @__PURE__ */ jsx("div", { className: "px-4 pt-2", children: /* @__PURE__ */ jsxs("div", { className: "mx-auto flex w-full max-w-4xl flex-col gap-2 rounded-md border border-input/70 bg-background px-2 py-1 shadow-xs focus-within:border-primary focus-within:[outline:1px_solid_var(--color-primary)]", children: [
116
+ const trailingButton = showCancelButton ? /* @__PURE__ */ jsx(SubmitButton, { onClick: onCancelRequest, showCancelButton: true, title: "Stop", ariaLabel: "Stop" }) : showSubmitButton ? /* @__PURE__ */ jsx(SubmitButton, { onClick: handleSend, disabled: !canSend, title: "Send", ariaLabel: "Send" }) : null;
117
+ return /* @__PURE__ */ jsxs("div", { className: cn(
118
+ "flex flex-col mx-auto w-full max-w-4xl gap-2",
119
+ "border border-input/70 rounded-md bg-background p-1 shadow-xs",
120
+ "focus-within:border-primary focus-within:[outline:1px_solid_var(--color-primary)]",
121
+ className
122
+ ), children: [
163
123
  attachments.length > 0 ? /* @__PURE__ */ jsx("div", { className: "flex max-w-full flex-wrap gap-2 px-1 pt-1", children: attachments.map((attachment, index) => /* @__PURE__ */ jsx(
164
124
  UploadPill,
165
125
  {
@@ -168,19 +128,23 @@ function ChatInput({
168
128
  },
169
129
  `${attachment.path}-${index}`
170
130
  )) }) : null,
171
- /* @__PURE__ */ jsxs("div", { className: cn("flex items-center gap-2", !enableFileUpload && "pl-1"), children: [
131
+ /* @__PURE__ */ jsxs("div", { className: cn("flex items-center gap-2", !enableFileUpload && "pl-1", inputRowClassName), children: [
132
+ leadingButton,
172
133
  enableFileUpload ? /* @__PURE__ */ jsx(FileUploader, { onFilesSelected, disabled }) : null,
173
134
  /* @__PURE__ */ jsx(
174
135
  "textarea",
175
136
  {
176
137
  ref: textareaRef,
138
+ rows: 1,
177
139
  autoFocus,
178
140
  placeholder,
179
141
  className: cn(
180
142
  "min-h-5 max-h-40",
181
143
  "flex-1 resize-none border-0 bg-transparent p-0 leading-5",
182
- "shadow-none outline-none ring-0 focus:ring-0 focus-visible:ring-0 disabled:cursor-not-allowed disabled:opacity-50",
183
- "md:text-sm"
144
+ "shadow-none outline-none ring-0 focus:ring-0 focus-visible:ring-0",
145
+ "disabled:cursor-not-allowed disabled:opacity-50",
146
+ "md:text-sm",
147
+ textareaClassName
184
148
  ),
185
149
  readOnly: disabled,
186
150
  value,
@@ -190,8 +154,9 @@ function ChatInput({
190
154
  ),
191
155
  trailingButton
192
156
  ] })
193
- ] }) });
157
+ ] });
194
158
  }
195
159
  export {
196
- ChatInput
160
+ ChatInput,
161
+ useAutoResizingTextarea
197
162
  };
@@ -1,4 +1,4 @@
1
- import { jsx } from "react/jsx-runtime";
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
2
  import {
3
3
  useLayoutEffect,
4
4
  useRef
@@ -12,29 +12,17 @@ function MeasuredPage({
12
12
  renderMessage,
13
13
  getMessageId,
14
14
  getMessageAuthorId,
15
- onHeightChange
15
+ registerPage
16
16
  }) {
17
17
  const pageRef = useRef(null);
18
18
  useLayoutEffect(() => {
19
- const pageElement = pageRef.current;
20
- if (!pageElement) {
21
- return void 0;
22
- }
23
- const reportHeight = () => {
24
- const height = pageElement.getBoundingClientRect().height;
25
- onHeightChange(pageIndex, height);
26
- };
27
- reportHeight();
28
- const resizeObserver = new ResizeObserver(reportHeight);
29
- resizeObserver.observe(pageElement);
30
- return () => {
31
- resizeObserver.disconnect();
32
- };
33
- }, [onHeightChange, pageIndex]);
19
+ const element = pageRef.current;
20
+ return element ? registerPage(pageIndex, element) : void 0;
21
+ }, [registerPage, pageIndex]);
34
22
  return /* @__PURE__ */ jsx(
35
23
  "div",
36
24
  {
37
- className: "chat-scroll__page flex flex-col gap-2 px-3 py-1",
25
+ className: "chat-scroll__page flex shrink-0 flex-col gap-2 px-3 py-1",
38
26
  "data-page-index": pageIndex,
39
27
  ref: pageRef,
40
28
  children: messages.map((message) => {
@@ -68,10 +56,10 @@ function ChatScroller({
68
56
  const {
69
57
  scrollRef,
70
58
  visiblePages,
71
- spacerHeight,
72
- windowBottom,
59
+ spacerHeights,
73
60
  handleScroll,
74
- handlePageHeightChange
61
+ handleScrollIntent,
62
+ registerPage
75
63
  } = useChatVirtualizer({
76
64
  messages,
77
65
  pageSize,
@@ -84,34 +72,48 @@ function ChatScroller({
84
72
  {
85
73
  className: className ? "chat-scroll relative box-border h-full min-h-0 overflow-y-auto overscroll-contain [overflow-anchor:none] " + className : "chat-scroll relative box-border h-full min-h-0 overflow-y-auto overscroll-contain [overflow-anchor:none]",
86
74
  onScroll: handleScroll,
75
+ onWheelCapture: handleScrollIntent,
76
+ onTouchMoveCapture: handleScrollIntent,
77
+ onPointerDownCapture: (event) => {
78
+ if (event.target === event.currentTarget) {
79
+ handleScrollIntent();
80
+ }
81
+ },
82
+ onKeyDownCapture: (event) => {
83
+ if (["ArrowUp", "ArrowDown", "PageUp", "PageDown", "Home", "End", " "].includes(event.key)) {
84
+ handleScrollIntent();
85
+ }
86
+ },
87
87
  ref: scrollRef,
88
88
  style,
89
- children: /* @__PURE__ */ jsx(
89
+ children: /* @__PURE__ */ jsxs(
90
90
  "div",
91
91
  {
92
92
  "aria-hidden": visiblePages.length === 0 ? "true" : void 0,
93
- className: "chat-scroll__spacer relative min-h-full",
94
- style: { height: spacerHeight },
95
- children: /* @__PURE__ */ jsx(
96
- "div",
97
- {
98
- className: "chat-scroll__window absolute right-0 left-0 flex flex-col",
99
- style: { bottom: windowBottom },
100
- children: visiblePages.map((page) => /* @__PURE__ */ jsx(
101
- MeasuredPage,
102
- {
103
- currentUserId,
104
- getMessageAuthorId,
105
- getMessageId,
106
- messages: page.items,
107
- onHeightChange: handlePageHeightChange,
108
- pageIndex: page.index,
109
- renderMessage
110
- },
111
- page.index
112
- ))
113
- }
114
- )
93
+ className: "chat-scroll__spacer flex min-h-full flex-col justify-end",
94
+ children: [
95
+ /* @__PURE__ */ jsx("div", { "aria-hidden": "true", className: "shrink-0", style: { height: spacerHeights.top } }),
96
+ /* @__PURE__ */ jsx(
97
+ "div",
98
+ {
99
+ className: "chat-scroll__window flex shrink-0 flex-col",
100
+ children: visiblePages.map((page) => /* @__PURE__ */ jsx(
101
+ MeasuredPage,
102
+ {
103
+ currentUserId,
104
+ getMessageAuthorId,
105
+ getMessageId,
106
+ messages: page.items,
107
+ registerPage,
108
+ pageIndex: page.index,
109
+ renderMessage
110
+ },
111
+ page.index
112
+ ))
113
+ }
114
+ ),
115
+ /* @__PURE__ */ jsx("div", { "aria-hidden": "true", className: "shrink-0", style: { height: spacerHeights.bottom } })
116
+ ]
115
117
  }
116
118
  )
117
119
  }
@@ -18,6 +18,8 @@ export interface ChatThreadItem {
18
18
  turnId?: string;
19
19
  toolkit?: string;
20
20
  tool?: string;
21
+ /** Execution request associated with this lifecycle row. */
22
+ clientToolRequestId?: string;
21
23
  command?: string;
22
24
  argumentsText?: string;
23
25
  input?: Record<string, unknown>;
@@ -46,7 +46,10 @@ function inputContent(message) {
46
46
  attachments.push(item.url);
47
47
  }
48
48
  }
49
- return { text: textParts.join("\n"), attachments };
49
+ return {
50
+ text: textParts.join("\n"),
51
+ attachments
52
+ };
50
53
  }
51
54
  function inputItemFromMessage(message, createdAt) {
52
55
  if (isTypedMessage(message, StartThread) || isTypedMessage(message, TurnStart) || isTypedMessage(message, TurnSteer)) {
@@ -237,7 +240,7 @@ function turnEndedErrorItem(message, createdAt) {
237
240
  turnId
238
241
  };
239
242
  }
240
- function buildChatThreadTimeline(events, pendingInputs = []) {
243
+ function buildRawChatThreadTimeline(events, pendingInputs = []) {
241
244
  const items = /* @__PURE__ */ new Map();
242
245
  let usage = null;
243
246
  for (const event of events) {
@@ -376,11 +379,12 @@ function buildChatThreadTimeline(events, pendingInputs = []) {
376
379
  failed: existing?.failed
377
380
  });
378
381
  } else if (isTypedMessage(message, AgentClientToolCallRequested)) {
382
+ const existing = items.get(message.requestId);
379
383
  upsertItem(items, {
380
384
  id: message.requestId,
381
385
  kind: "tool_call",
382
386
  role: "agent",
383
- text: `Waiting for client tool ${toolCallLabel(message)}`,
387
+ text: existing?.text ?? `Waiting for client tool ${toolCallLabel(message)}`,
384
388
  attachments: [],
385
389
  createdAt,
386
390
  turnId: message.turnId,
@@ -388,7 +392,7 @@ function buildChatThreadTimeline(events, pendingInputs = []) {
388
392
  tool: message.tool,
389
393
  argumentsText: formatJsonValue(message.arguments),
390
394
  input: message.arguments,
391
- state: "queued"
395
+ state: existing?.state ?? "queued"
392
396
  });
393
397
  } else if (isTypedMessage(message, AgentSecretRequested)) {
394
398
  const prompt = message.prompt?.trim();
@@ -434,9 +438,69 @@ function buildChatThreadTimeline(events, pendingInputs = []) {
434
438
  usage
435
439
  };
436
440
  }
441
+ function clientToolInputKey(input) {
442
+ return JSON.stringify(input, (_key, value) => {
443
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
444
+ return value;
445
+ }
446
+ return Object.fromEntries(Object.entries(value).sort(([left], [right]) => left.localeCompare(right)));
447
+ });
448
+ }
449
+ function mergeClientToolRequests(items, events) {
450
+ const requests = /* @__PURE__ */ new Map();
451
+ const lifecycleIds = /* @__PURE__ */ new Set();
452
+ const replacements = /* @__PURE__ */ new Map();
453
+ const consumedRequests = /* @__PURE__ */ new Set();
454
+ const matchesScope = (item, request) => item.turnId === request.turnId && item.toolkit === request.toolkit && item.tool === request.tool;
455
+ const pair = (item, request) => {
456
+ replacements.set(item.id, {
457
+ ...item,
458
+ clientToolRequestId: request.requestId,
459
+ input: item.input ?? request.arguments,
460
+ argumentsText: item.argumentsText ?? formatJsonValue(request.arguments)
461
+ });
462
+ consumedRequests.add(request.requestId);
463
+ };
464
+ for (const { message } of events) {
465
+ if (isTypedMessage(message, AgentClientToolCallRequested) && message.toolkit === "client") {
466
+ requests.set(message.requestId, message);
467
+ }
468
+ if (isTypedMessage(message, AgentToolCallPending) || isTypedMessage(message, AgentToolCallInProgress) || isTypedMessage(message, AgentToolCallStarted) || isTypedMessage(message, AgentToolCallEnded)) {
469
+ lifecycleIds.add(message.itemId);
470
+ }
471
+ }
472
+ const lifecycles = items.filter((item) => item.toolkit === "client" && lifecycleIds.has(item.id));
473
+ for (const item of lifecycles) {
474
+ const request = requests.get(item.id);
475
+ if (request && matchesScope(item, request)) {
476
+ pair(item, request);
477
+ }
478
+ }
479
+ for (const request of requests.values()) {
480
+ const item = lifecycles.find(
481
+ (candidate) => !replacements.has(candidate.id) && matchesScope(candidate, request) && candidate.input != null && clientToolInputKey(candidate.input) === clientToolInputKey(request.arguments)
482
+ );
483
+ if (!consumedRequests.has(request.requestId) && item) {
484
+ pair(item, request);
485
+ }
486
+ }
487
+ for (const request of requests.values()) {
488
+ const candidates = lifecycles.filter((item2) => !replacements.has(item2.id) && matchesScope(item2, request));
489
+ const peers = [...requests.values()].filter((other) => !consumedRequests.has(other.requestId) && other.turnId === request.turnId && other.toolkit === request.toolkit && other.tool === request.tool);
490
+ const item = candidates[0];
491
+ if (!consumedRequests.has(request.requestId) && candidates.length === 1 && peers.length === 1 && item.input == null) {
492
+ pair(item, request);
493
+ }
494
+ }
495
+ return items.filter((item) => !consumedRequests.has(item.id) || replacements.has(item.id)).map((item) => replacements.get(item.id) ?? item);
496
+ }
497
+ function buildChatThreadTimeline(events, pendingInputs = []) {
498
+ const timeline = buildRawChatThreadTimeline(events, pendingInputs);
499
+ return { ...timeline, items: mergeClientToolRequests(timeline.items, events) };
500
+ }
437
501
  function buildMergedChatThreadTimeline(persistedEvents, liveEvents, pendingInputs = []) {
438
- const persisted = buildChatThreadTimeline(persistedEvents);
439
- const live = buildChatThreadTimeline(liveEvents, pendingInputs);
502
+ const persisted = buildRawChatThreadTimeline(persistedEvents);
503
+ const live = buildRawChatThreadTimeline(liveEvents, pendingInputs);
440
504
  const items = persisted.items.map((item) => ({ ...item }));
441
505
  const indexes = new Map(items.map((item, index) => [item.id, index]));
442
506
  for (const liveItem of live.items) {
@@ -452,7 +516,10 @@ function buildMergedChatThreadTimeline(persistedEvents, liveEvents, pendingInput
452
516
  if (live.usage != null && shouldReplaceAgentUsageSnapshot(usage, live.usage)) {
453
517
  usage = live.usage;
454
518
  }
455
- return { items, usage };
519
+ return {
520
+ items: mergeClientToolRequests(items, [...persistedEvents, ...liveEvents]),
521
+ usage
522
+ };
456
523
  }
457
524
  function mergeChatThreadItem(persisted, live) {
458
525
  if (persisted.kind !== live.kind || persisted.role !== live.role) {
@@ -527,9 +594,13 @@ function sumUsageKeys(rawUsage, names) {
527
594
  let total = 0;
528
595
  let found = false;
529
596
  for (const [key, value] of Object.entries(rawUsage)) {
530
- if (![...names].some((name) => key === name || key.endsWith(`.${name}`))) continue;
597
+ if (![...names].some((name) => key === name || key.endsWith(`.${name}`))) {
598
+ continue;
599
+ }
531
600
  const numeric = finiteNumber(value);
532
- if (numeric == null) continue;
601
+ if (numeric == null) {
602
+ continue;
603
+ }
533
604
  total += numeric;
534
605
  found = true;
535
606
  }
@@ -537,7 +608,9 @@ function sumUsageKeys(rawUsage, names) {
537
608
  }
538
609
  function usageTotalTokens(rawUsage) {
539
610
  const explicit = sumUsageKeys(rawUsage, /* @__PURE__ */ new Set(["total_tokens"]));
540
- if (explicit != null) return explicit;
611
+ if (explicit != null) {
612
+ return explicit;
613
+ }
541
614
  const input = sumUsageKeys(rawUsage, /* @__PURE__ */ new Set(["input_tokens", "audio_input_tokens", "image_input_tokens", "cache_creation_input_tokens", "cache_read_input_tokens"]));
542
615
  const output = sumUsageKeys(rawUsage, /* @__PURE__ */ new Set(["output_tokens", "audio_output_tokens", "image_output_tokens"]));
543
616
  const cached = sumUsageKeys(rawUsage, /* @__PURE__ */ new Set(["cached_tokens", "audio_cached_tokens", "image_cached_tokens"]));
@@ -0,0 +1,11 @@
1
+ import type { ReactElement, ReactNode } from "react";
2
+ export interface ComposerActionButtonProps {
3
+ onClick?: () => void;
4
+ disabled?: boolean;
5
+ showCancelButton?: boolean;
6
+ className?: string;
7
+ icon?: ReactNode;
8
+ title?: string;
9
+ ariaLabel?: string;
10
+ }
11
+ export declare function ComposerActionButton({ onClick, disabled, showCancelButton, className, icon, title, ariaLabel, }: ComposerActionButtonProps): ReactElement;
@@ -0,0 +1,48 @@
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
+ import { ArrowUp, LoaderCircle, X } from "lucide-react";
3
+ import { Button } from "../components/ui/button.js";
4
+ import { cn } from "../lib/utils.js";
5
+ function ComposerActionButton({
6
+ onClick,
7
+ disabled = false,
8
+ showCancelButton = false,
9
+ className,
10
+ icon,
11
+ title = "Send",
12
+ ariaLabel = title
13
+ }) {
14
+ if (showCancelButton) {
15
+ return /* @__PURE__ */ jsxs(
16
+ Button,
17
+ {
18
+ type: "button",
19
+ size: "icon",
20
+ variant: "ghost",
21
+ className: cn("relative size-9 rounded-full", disabled ? "opacity-55" : "cursor-pointer", className),
22
+ disabled,
23
+ onClick,
24
+ title: disabled ? "Cancelling" : "Stop",
25
+ children: [
26
+ /* @__PURE__ */ jsx(LoaderCircle, { className: "absolute h-9 w-9 animate-spin text-muted-foreground" }),
27
+ /* @__PURE__ */ jsx("span", { className: "relative inline-flex h-5 w-5 items-center justify-center rounded-full bg-foreground text-background", children: /* @__PURE__ */ jsx(X, { className: "h-3 w-3" }) })
28
+ ]
29
+ }
30
+ );
31
+ }
32
+ return /* @__PURE__ */ jsx(
33
+ Button,
34
+ {
35
+ type: "button",
36
+ size: "icon",
37
+ className: cn("size-9 rounded-full shadow-xs", !disabled && "cursor-pointer", className),
38
+ disabled,
39
+ onClick,
40
+ title,
41
+ "aria-label": ariaLabel,
42
+ children: icon ?? /* @__PURE__ */ jsx(ArrowUp, { className: "h-4 w-4" })
43
+ }
44
+ );
45
+ }
46
+ export {
47
+ ComposerActionButton
48
+ };
@@ -1,9 +1,7 @@
1
- import { type RefObject } from "react";
2
- import { type Page, type VisiblePageRange } from "./virtualization.js";
1
+ import { type VisiblePageRange } from "./virtualization.js";
3
2
  export type ChatVirtualizerState = {
4
3
  visibleRange: VisiblePageRange;
5
4
  measuredHeights: ReadonlyMap<number, number>;
6
- bottomAdjustment: number;
7
5
  };
8
6
  export type ChatVirtualizerAction = {
9
7
  type: "setVisibleRange";
@@ -13,12 +11,12 @@ export type ChatVirtualizerAction = {
13
11
  pageCount: number;
14
12
  overscanPages: number;
15
13
  } | {
16
- type: "measurePage";
17
- pageIndex: number;
18
- height: number;
19
- shouldAdjustBottom: boolean;
14
+ type: "measurePages";
15
+ heights: ReadonlyMap<number, number>;
20
16
  } | {
21
- type: "clearBottomAdjustment";
17
+ type: "reset";
18
+ pageCount: number;
19
+ overscanPages: number;
22
20
  };
23
21
  export type CreateChatVirtualizerStateArgs = {
24
22
  pageCount: number;
@@ -31,17 +29,16 @@ type UseChatVirtualizerOptions<TMessage> = {
31
29
  bottomThresholdPx: number;
32
30
  getMessageId: (message: TMessage) => string;
33
31
  };
34
- type UseChatVirtualizerResult<TMessage> = {
35
- scrollRef: RefObject<HTMLDivElement | null>;
36
- visiblePages: Page<TMessage>[];
37
- spacerHeight: number;
38
- windowBottom: number;
39
- handleScroll: () => void;
40
- handlePageHeightChange: (pageIndex: number, height: number) => void;
41
- };
42
32
  export declare function getInitialVisibleRange(pageCount: number, overscanPages: number): VisiblePageRange;
43
33
  export declare function clampVisibleRange(range: VisiblePageRange, pageCount: number): VisiblePageRange;
44
34
  export declare function createChatVirtualizerState({ pageCount, overscanPages, }: CreateChatVirtualizerStateArgs): ChatVirtualizerState;
45
35
  export declare function chatVirtualizerReducer(state: ChatVirtualizerState, action: ChatVirtualizerAction): ChatVirtualizerState;
46
- export declare function useChatVirtualizer<TMessage>({ messages, pageSize, overscanPages, bottomThresholdPx, getMessageId, }: UseChatVirtualizerOptions<TMessage>): UseChatVirtualizerResult<TMessage>;
36
+ export declare function useChatVirtualizer<TMessage>({ messages, pageSize, overscanPages, bottomThresholdPx, getMessageId, }: UseChatVirtualizerOptions<TMessage>): {
37
+ scrollRef: import("react").RefObject<HTMLDivElement | null>;
38
+ visiblePages: import("./virtualization.js").Page<TMessage>[];
39
+ spacerHeights: import("./virtualization.js").SpacerHeights;
40
+ handleScroll: () => void;
41
+ handleScrollIntent: () => void;
42
+ registerPage: (index: number, element: HTMLDivElement) => () => void;
43
+ };
47
44
  export {};