@meshagent/meshagent-tailwind 0.43.2 → 0.43.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.
Files changed (37) hide show
  1. package/CHANGELOG.md +3 -0
  2. package/dist/cjs/chat/agent-thread.d.ts +15 -0
  3. package/dist/cjs/chat/agent-thread.js +613 -0
  4. package/dist/cjs/chat/chat-bot-view.d.ts +3 -2
  5. package/dist/cjs/chat/chat-bot-view.js +34 -34
  6. package/dist/cjs/chat/chat-bot-view.test.js +325 -0
  7. package/dist/cjs/chat/live-chat-widgets.test.js +256 -0
  8. package/dist/cjs/chat/multi-thread-view.d.ts +3 -2
  9. package/dist/cjs/chat/multi-thread-view.js +4 -2
  10. package/dist/cjs/chat/new-chat-thread.d.ts +3 -2
  11. package/dist/cjs/chat/new-chat-thread.js +5 -2
  12. package/dist/cjs/file-preview/file-preview.js +2 -2
  13. package/dist/cjs/index.d.ts +1 -2
  14. package/dist/cjs/index.js +1 -2
  15. package/dist/esm/chat/agent-thread.d.ts +15 -0
  16. package/dist/esm/chat/agent-thread.js +606 -0
  17. package/dist/esm/chat/chat-bot-view.d.ts +3 -2
  18. package/dist/esm/chat/chat-bot-view.js +34 -34
  19. package/dist/esm/chat/chat-bot-view.test.js +336 -0
  20. package/dist/esm/chat/live-chat-widgets.test.js +262 -0
  21. package/dist/esm/chat/multi-thread-view.d.ts +3 -2
  22. package/dist/esm/chat/multi-thread-view.js +4 -2
  23. package/dist/esm/chat/new-chat-thread.d.ts +3 -2
  24. package/dist/esm/chat/new-chat-thread.js +5 -2
  25. package/dist/esm/file-preview/file-preview.js +2 -2
  26. package/dist/esm/index.d.ts +1 -2
  27. package/dist/esm/index.js +1 -2
  28. package/dist/index.css +1 -1
  29. package/package.json +10 -5
  30. package/dist/cjs/chat/chat-thread.d.ts +0 -12
  31. package/dist/cjs/chat/chat-thread.js +0 -815
  32. package/dist/cjs/chat/dataset-chat-thread.d.ts +0 -16
  33. package/dist/cjs/chat/dataset-chat-thread.js +0 -1813
  34. package/dist/esm/chat/chat-thread.d.ts +0 -12
  35. package/dist/esm/chat/chat-thread.js +0 -785
  36. package/dist/esm/chat/dataset-chat-thread.d.ts +0 -16
  37. package/dist/esm/chat/dataset-chat-thread.js +0 -1811
@@ -1,785 +0,0 @@
1
- import { jsx, jsxs } from "react/jsx-runtime";
2
- import { useMemo, useState, useEffect, useRef, useCallback } from "react";
3
- import { FileText } from "lucide-react";
4
- import ReactMarkdown from "react-markdown";
5
- import rehypeHighlight from "rehype-highlight";
6
- import rehypeSanitize from "rehype-sanitize";
7
- import remarkGfm from "remark-gfm";
8
- import { Button } from "../components/ui/button.js";
9
- import { Spinner } from "../components/ui/spinner.js";
10
- import { ChatTypingIndicator } from "./chat-typing-indicator.js";
11
- import { ChatInput } from "./chat-input.js";
12
- import { useChatThread, useThreadStatus } from "./chat-hooks.js";
13
- import { FilePreviewDialog, isImagePath } from "../file-preview/file-preview.js";
14
- import { cn } from "../lib/utils.js";
15
- const supportedEventKinds = /* @__PURE__ */ new Set([
16
- "exec",
17
- "tool",
18
- "web",
19
- "search",
20
- "diff",
21
- "image",
22
- "approval",
23
- "collab",
24
- "plan",
25
- "thread",
26
- "file"
27
- ]);
28
- const stickyBottomThresholdPx = 24;
29
- function getStringAttribute(element, name) {
30
- const value = element.getAttribute(name);
31
- return typeof value === "string" ? value : null;
32
- }
33
- function getTrimmedStringAttribute(element, name) {
34
- return getStringAttribute(element, name)?.trim() ?? "";
35
- }
36
- function getElementChildren(element) {
37
- return element.getChildren() ?? [];
38
- }
39
- function formatDateTime(iso) {
40
- const date = new Date(iso);
41
- return new Intl.DateTimeFormat(void 0, {
42
- year: "numeric",
43
- month: "long",
44
- day: "numeric",
45
- hour: "numeric",
46
- minute: "2-digit",
47
- hour12: true,
48
- timeZoneName: "short"
49
- }).format(date);
50
- }
51
- function timeAgo(iso) {
52
- const date = new Date(iso);
53
- if (Number.isNaN(date.getTime())) {
54
- return "";
55
- }
56
- const relativeTime = new Intl.RelativeTimeFormat(void 0, { numeric: "auto" });
57
- const now = /* @__PURE__ */ new Date();
58
- const seconds = Math.round((date.getTime() - now.getTime()) / 1e3);
59
- const minutes = Math.round(seconds / 60);
60
- const hours = Math.round(minutes / 60);
61
- const days = Math.round(hours / 24);
62
- const months = Math.round(days / 30);
63
- if (Math.abs(months) >= 1) {
64
- return formatDateTime(iso);
65
- }
66
- if (Math.abs(days) >= 1) {
67
- return relativeTime.format(days, "day");
68
- }
69
- if (Math.abs(hours) >= 1) {
70
- return relativeTime.format(hours, "hour");
71
- }
72
- if (Math.abs(minutes) >= 1) {
73
- return relativeTime.format(minutes, "minute");
74
- }
75
- return relativeTime.format(seconds, "second");
76
- }
77
- function displayParticipantName(name) {
78
- return name.split("@")[0]?.trim() ?? name.trim();
79
- }
80
- function isThreadAttachmentElement(element) {
81
- return element.tagName === "file" || element.tagName === "image";
82
- }
83
- function isImageAttachmentElement(element) {
84
- if (!isThreadAttachmentElement(element)) {
85
- return false;
86
- }
87
- const path = getTrimmedStringAttribute(element, "path");
88
- return element.tagName === "image" || path !== "" && isImagePath(path);
89
- }
90
- function getAttachmentTrackingId(attachment) {
91
- const path = getTrimmedStringAttribute(attachment, "path");
92
- return attachment.id || `${attachment.tagName}:${path}`;
93
- }
94
- function collectImageAttachmentIds(messages) {
95
- const ids = [];
96
- for (const message of messages) {
97
- if (message.tagName !== "message") {
98
- continue;
99
- }
100
- for (const attachment of getElementChildren(message)) {
101
- if (!isImageAttachmentElement(attachment)) {
102
- continue;
103
- }
104
- ids.push(getAttachmentTrackingId(attachment));
105
- }
106
- }
107
- return ids;
108
- }
109
- function parseEventDetailLines(raw) {
110
- const value = raw.trim();
111
- if (value === "") {
112
- return [];
113
- }
114
- if (value.startsWith("[") && value.endsWith("]")) {
115
- try {
116
- const decoded = JSON.parse(value);
117
- if (Array.isArray(decoded)) {
118
- return decoded.filter((item) => typeof item === "string").map((line) => line.trim()).filter((line) => line !== "");
119
- }
120
- } catch {
121
- }
122
- }
123
- return value.split(/\r?\n/u).map((line) => line.trim()).filter((line) => line !== "");
124
- }
125
- function isCompletedToolCallEvent(message) {
126
- if (message.tagName !== "event") {
127
- return false;
128
- }
129
- const kind = getTrimmedStringAttribute(message, "kind").toLowerCase();
130
- if (kind !== "tool") {
131
- return false;
132
- }
133
- const state = (getTrimmedStringAttribute(message, "state") || "info").toLowerCase();
134
- if (state !== "completed") {
135
- return false;
136
- }
137
- const itemType = getTrimmedStringAttribute(message, "item_type").toLowerCase();
138
- if (itemType === "tool_call") {
139
- return true;
140
- }
141
- const method = getTrimmedStringAttribute(message, "method") || "agent/event";
142
- const summary = getTrimmedStringAttribute(message, "summary") || method;
143
- const headline = getTrimmedStringAttribute(message, "headline");
144
- const detailLines = parseEventDetailLines(getTrimmedStringAttribute(message, "details"));
145
- const filterHeadline = (headline !== "" ? headline : summary).trim().toLowerCase();
146
- return filterHeadline === "called tool" && detailLines.length > 0 && detailLines.every((line) => line.trimStart().toLowerCase().startsWith("tool:"));
147
- }
148
- function shouldHideCompletedToolCallEvent(message, showCompletedToolCalls) {
149
- return !showCompletedToolCalls && isCompletedToolCallEvent(message);
150
- }
151
- function hasRenderableStandardThreadMessageContent(message) {
152
- if (message.tagName !== "message") {
153
- return true;
154
- }
155
- const text = getTrimmedStringAttribute(message, "text");
156
- if (text !== "") {
157
- return true;
158
- }
159
- return getElementChildren(message).some(isThreadAttachmentElement);
160
- }
161
- function shouldRenderThreadElement(message, showCompletedToolCalls) {
162
- if (message.tagName === "reasoning") {
163
- return getTrimmedStringAttribute(message, "summary") !== "";
164
- }
165
- if (message.tagName === "message") {
166
- return hasRenderableStandardThreadMessageContent(message);
167
- }
168
- if (message.tagName === "exec") {
169
- return true;
170
- }
171
- if (message.tagName !== "event") {
172
- return false;
173
- }
174
- const kind = getTrimmedStringAttribute(message, "kind").toLowerCase();
175
- if (!supportedEventKinds.has(kind)) {
176
- return false;
177
- }
178
- return !shouldHideCompletedToolCallEvent(message, showCompletedToolCalls);
179
- }
180
- function isCancellingThreadStatusText(statusText) {
181
- const normalized = statusText?.trim().toLowerCase();
182
- return normalized === "cancelling" || normalized === "canceling";
183
- }
184
- function distanceFromBottom(element) {
185
- return Math.max(element.scrollHeight - element.clientHeight - element.scrollTop, 0);
186
- }
187
- function isNearBottom(element) {
188
- return distanceFromBottom(element) <= stickyBottomThresholdPx;
189
- }
190
- function useDownloadUrl(room, path) {
191
- const [url, setUrl] = useState(null);
192
- useEffect(() => {
193
- let cancelled = false;
194
- if (path.trim() === "") {
195
- setUrl(null);
196
- return;
197
- }
198
- void room.storage.downloadUrl(path).then((nextUrl) => {
199
- if (!cancelled) {
200
- setUrl(nextUrl);
201
- }
202
- }).catch(() => {
203
- if (!cancelled) {
204
- setUrl(null);
205
- }
206
- });
207
- return () => {
208
- cancelled = true;
209
- };
210
- }, [path, room]);
211
- return url;
212
- }
213
- function MarkdownBlock({ text }) {
214
- return /* @__PURE__ */ jsx(
215
- ReactMarkdown,
216
- {
217
- remarkPlugins: [remarkGfm],
218
- rehypePlugins: [rehypeSanitize, rehypeHighlight],
219
- components: {
220
- pre: ({ className, children, ...props }) => /* @__PURE__ */ jsx(
221
- "pre",
222
- {
223
- ...props,
224
- className: cn("overflow-x-auto rounded-md border bg-background/80 p-3", className),
225
- children
226
- }
227
- ),
228
- p: ({ children, ...props }) => /* @__PURE__ */ jsx("p", { ...props, className: "mb-2 last:mb-0 overflow-hidden max-w-full", children }),
229
- table: ({ children, ...props }) => /* @__PURE__ */ jsx("div", { className: "my-4 w-full overflow-x-auto", children: /* @__PURE__ */ jsx(
230
- "table",
231
- {
232
- ...props,
233
- className: "w-full border-collapse border-spacing-0",
234
- children
235
- }
236
- ) }),
237
- th: ({ children, ...props }) => /* @__PURE__ */ jsx(
238
- "th",
239
- {
240
- ...props,
241
- className: "border bg-muted/50 px-3 py-2 text-left text-sm font-semibold",
242
- children
243
- }
244
- ),
245
- td: ({ children, ...props }) => /* @__PURE__ */ jsx(
246
- "td",
247
- {
248
- ...props,
249
- className: "border px-3 py-2 align-top text-sm bg-background",
250
- children
251
- }
252
- ),
253
- ul: ({ children, ...props }) => /* @__PURE__ */ jsx(
254
- "ul",
255
- {
256
- ...props,
257
- className: "mb-2 ml-6 list-disc last:mb-0",
258
- children
259
- }
260
- ),
261
- ol: ({ children, ...props }) => /* @__PURE__ */ jsx(
262
- "ol",
263
- {
264
- ...props,
265
- className: "mb-2 ml-6 list-decimal last:mb-0",
266
- children
267
- }
268
- ),
269
- a: ({ children, href, ...props }) => /* @__PURE__ */ jsx(
270
- "a",
271
- {
272
- ...props,
273
- href,
274
- target: "_blank",
275
- rel: "noopener noreferrer",
276
- className: "text-primary underline overflow-ellipsis inline-block max-w-full overflow-x-hidden",
277
- children
278
- }
279
- )
280
- },
281
- children: text
282
- }
283
- );
284
- }
285
- function ChatImage({
286
- room,
287
- path,
288
- alt,
289
- onSettled
290
- }) {
291
- const url = useDownloadUrl(room, path);
292
- const imageRef = useRef(null);
293
- const settledRef = useRef(false);
294
- const markSettled = useCallback(() => {
295
- if (settledRef.current) {
296
- return;
297
- }
298
- settledRef.current = true;
299
- onSettled?.();
300
- }, [onSettled]);
301
- useEffect(() => {
302
- settledRef.current = false;
303
- }, [path, url]);
304
- useEffect(() => {
305
- if (!url) {
306
- return;
307
- }
308
- if (imageRef.current?.complete) {
309
- markSettled();
310
- }
311
- }, [markSettled, url]);
312
- if (!url) {
313
- return null;
314
- }
315
- return /* @__PURE__ */ jsx(FilePreviewDialog, { room, path, children: /* @__PURE__ */ jsx(
316
- "button",
317
- {
318
- type: "button",
319
- className: "block overflow-hidden rounded-md bg-muted/20 shadow-xs transition-opacity hover:opacity-95",
320
- children: /* @__PURE__ */ jsx(
321
- "img",
322
- {
323
- ref: imageRef,
324
- src: url,
325
- alt,
326
- className: "max-h-[312px] w-auto max-w-full object-cover",
327
- onLoad: () => {
328
- markSettled();
329
- },
330
- onError: () => {
331
- markSettled();
332
- }
333
- }
334
- )
335
- }
336
- ) });
337
- }
338
- function FileAttachment({ room, path }) {
339
- const filename = path.split("/").pop() ?? path;
340
- return /* @__PURE__ */ jsx(FilePreviewDialog, { room, path, children: /* @__PURE__ */ jsxs(
341
- "button",
342
- {
343
- type: "button",
344
- className: "inline-flex max-w-full items-center gap-2 rounded-md bg-muted/60 px-3 py-2 text-left shadow-xs transition-colors hover:bg-muted/80",
345
- children: [
346
- /* @__PURE__ */ jsx(FileText, { className: "h-4 w-4 shrink-0 text-muted-foreground" }),
347
- /* @__PURE__ */ jsx("span", { className: "truncate text-sm font-medium", children: filename })
348
- ]
349
- }
350
- ) });
351
- }
352
- function ThreadAttachment({ room, attachment, onImageSettled }) {
353
- const path = getTrimmedStringAttribute(attachment, "path");
354
- if (path === "") {
355
- return null;
356
- }
357
- const filename = path.split("/").pop() ?? "Attachment";
358
- if (attachment.tagName === "image" || isImagePath(path)) {
359
- const attachmentId = getAttachmentTrackingId(attachment);
360
- return /* @__PURE__ */ jsx(
361
- ChatImage,
362
- {
363
- room,
364
- path,
365
- alt: filename,
366
- onSettled: () => {
367
- onImageSettled?.(attachmentId);
368
- }
369
- }
370
- );
371
- }
372
- return /* @__PURE__ */ jsx(FileAttachment, { room, path });
373
- }
374
- function ChatBubble({ text, mine }) {
375
- if (text.trim() === "") {
376
- return null;
377
- }
378
- return /* @__PURE__ */ jsx(
379
- "div",
380
- {
381
- className: cn(
382
- "w-fit max-w-[85%] rounded-md px-4 py-3 text-sm leading-6 shadow-xs sm:w-2xl",
383
- mine ? "bg-secondary/85 text-foreground" : "bg-muted/70 text-foreground"
384
- ),
385
- children: /* @__PURE__ */ jsx(MarkdownBlock, { text })
386
- }
387
- );
388
- }
389
- function ThreadReasoning({ message }) {
390
- const summary = getTrimmedStringAttribute(message, "summary");
391
- if (summary === "") {
392
- return null;
393
- }
394
- return /* @__PURE__ */ jsx("div", { className: "max-w-2xl border-l-2 border-primary/30 pl-4 text-sm text-muted-foreground", children: /* @__PURE__ */ jsx(MarkdownBlock, { text: summary }) });
395
- }
396
- function ThreadExec({ message }) {
397
- const command = getTrimmedStringAttribute(message, "command");
398
- const result = getTrimmedStringAttribute(message, "result");
399
- const stdout = getTrimmedStringAttribute(message, "stdout");
400
- const stderr = getTrimmedStringAttribute(message, "stderr");
401
- const sections = [
402
- command !== "" ? { label: "Command", value: command } : null,
403
- result !== "" ? { label: "Result", value: result } : null,
404
- stdout !== "" ? { label: "Stdout", value: stdout } : null,
405
- stderr !== "" ? { label: "Stderr", value: stderr } : null
406
- ].filter((section) => section !== null);
407
- return /* @__PURE__ */ jsxs("div", { className: "max-w-3xl rounded-2xl border bg-background/70 p-4", children: [
408
- /* @__PURE__ */ jsx("div", { className: "mb-3 text-xs font-semibold uppercase tracking-[0.16em] text-muted-foreground", children: "Terminal" }),
409
- /* @__PURE__ */ jsx("div", { className: "space-y-3 font-mono text-xs leading-6 text-foreground", children: sections.length === 0 ? /* @__PURE__ */ jsx("div", { className: "text-muted-foreground", children: "No command output." }) : sections.map((section) => /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
410
- /* @__PURE__ */ jsx("div", { className: "text-[11px] uppercase tracking-[0.16em] text-muted-foreground", children: section.label }),
411
- /* @__PURE__ */ jsx("pre", { className: "overflow-x-auto whitespace-pre-wrap rounded-xl border bg-muted/30 p-3", children: section.value })
412
- ] }, section.label)) })
413
- ] });
414
- }
415
- function defaultEventHeadline(kind, state, eventName) {
416
- if (kind === "approval") {
417
- return state === "completed" ? "Approval resolved" : "Approval required";
418
- }
419
- if (kind === "tool") {
420
- if (state === "failed") {
421
- return "Tool failed";
422
- }
423
- if (state === "completed") {
424
- return "Tool completed";
425
- }
426
- return "Calling tool";
427
- }
428
- if (kind === "web") {
429
- return "Opened webpage";
430
- }
431
- if (kind === "search") {
432
- return "Ran search";
433
- }
434
- if (kind === "diff") {
435
- return "Updated diff";
436
- }
437
- if (kind === "file") {
438
- return "File event";
439
- }
440
- if (kind === "thread") {
441
- return "Thread event";
442
- }
443
- if (kind === "plan") {
444
- return "Updated plan";
445
- }
446
- if (kind === "exec") {
447
- return "Ran command";
448
- }
449
- return eventName.replace(/[._]/gu, " ");
450
- }
451
- function EventRow({ room, message }) {
452
- const method = getTrimmedStringAttribute(message, "method") || "agent/event";
453
- const eventName = getTrimmedStringAttribute(message, "name") || getTrimmedStringAttribute(message, "event_type") || method.replace(/\//gu, ".");
454
- const kind = getTrimmedStringAttribute(message, "kind").toLowerCase();
455
- const state = (getTrimmedStringAttribute(message, "state") || "info").toLowerCase();
456
- const summary = getTrimmedStringAttribute(message, "summary");
457
- const headline = getTrimmedStringAttribute(message, "headline") || (summary !== "" ? summary : defaultEventHeadline(kind, state, eventName));
458
- const detailLines = parseEventDetailLines(getTrimmedStringAttribute(message, "details"));
459
- const eventPath = getTrimmedStringAttribute(message, "path");
460
- const inProgress = state === "in_progress" || state === "running" || state === "queued";
461
- const textColorClass = state === "failed" ? "text-destructive" : state === "cancelled" ? "text-muted-foreground" : inProgress ? "text-primary" : "text-foreground";
462
- if (headline === "") {
463
- return null;
464
- }
465
- return /* @__PURE__ */ jsx("div", { className: "max-w-3xl rounded-2xl border bg-background/70 px-4 py-3 text-xs", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-wrap items-start gap-2", children: [
466
- /* @__PURE__ */ jsx("span", { className: "rounded-full border px-2 py-0.5 text-[10px] uppercase tracking-[0.16em] text-muted-foreground", children: kind }),
467
- /* @__PURE__ */ jsxs("div", { className: "min-w-0 flex-1 space-y-2", children: [
468
- /* @__PURE__ */ jsx("div", { className: cn("text-sm font-semibold leading-5", textColorClass), children: headline }),
469
- summary !== "" && summary !== headline ? /* @__PURE__ */ jsx("div", { className: "text-muted-foreground", children: summary }) : null,
470
- detailLines.length > 0 ? /* @__PURE__ */ jsx("div", { className: "space-y-1 text-muted-foreground", children: detailLines.map((line, index) => /* @__PURE__ */ jsx("div", { className: "leading-5", children: line }, `${line}-${index}`)) }) : null,
471
- eventPath !== "" ? /* @__PURE__ */ jsxs(
472
- "button",
473
- {
474
- type: "button",
475
- className: "inline-flex items-center gap-2 rounded-full border px-3 py-1.5 text-left text-[11px] font-medium text-foreground transition-colors hover:bg-muted",
476
- onClick: () => {
477
- void room.storage.downloadUrl(eventPath).then((url) => {
478
- window.open(url, "_blank", "noopener,noreferrer");
479
- });
480
- },
481
- children: [
482
- /* @__PURE__ */ jsx(FileText, { className: "h-3.5 w-3.5 shrink-0" }),
483
- /* @__PURE__ */ jsx("span", { className: "truncate", children: eventPath })
484
- ]
485
- }
486
- ) : null
487
- ] })
488
- ] }) });
489
- }
490
- function ThreadMessage({
491
- room,
492
- message,
493
- previous,
494
- localParticipantName,
495
- onImageSettled
496
- }) {
497
- const authorName = getTrimmedStringAttribute(message, "author_name");
498
- const mine = authorName !== "" && authorName === localParticipantName.trim();
499
- const createdAt = getTrimmedStringAttribute(message, "created_at");
500
- const text = getStringAttribute(message, "text") ?? "";
501
- const attachments = getElementChildren(message).filter(isThreadAttachmentElement);
502
- const previousAuthor = previous ? getTrimmedStringAttribute(previous, "author_name") : "";
503
- const shouldShowHeader = previousAuthor !== authorName;
504
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-2", children: [
505
- shouldShowHeader && (authorName !== "" || createdAt !== "") ? /* @__PURE__ */ jsx("div", { className: cn("flex w-full", mine ? "justify-end" : "justify-start"), children: /* @__PURE__ */ jsx("div", { className: cn("max-w-[85%] px-1 sm:max-w-2xl", mine ? "text-right" : "text-left"), children: /* @__PURE__ */ jsxs(
506
- "div",
507
- {
508
- className: cn(
509
- "flex flex-wrap items-center gap-x-3 gap-y-1 text-xs text-muted-foreground",
510
- mine ? "justify-end" : "justify-start"
511
- ),
512
- children: [
513
- authorName !== "" ? /* @__PURE__ */ jsx("span", { className: "font-semibold text-foreground", children: displayParticipantName(authorName) }) : null,
514
- createdAt !== "" ? /* @__PURE__ */ jsx("span", { children: timeAgo(createdAt) }) : null
515
- ]
516
- }
517
- ) }) }) : null,
518
- text.trim() !== "" ? /* @__PURE__ */ jsx("div", { className: cn("flex w-full", mine ? "justify-end" : "justify-start"), children: /* @__PURE__ */ jsx(ChatBubble, { text, mine }) }) : null,
519
- attachments.length > 0 ? /* @__PURE__ */ jsx("div", { className: cn("flex w-full", mine ? "justify-end" : "justify-start"), children: /* @__PURE__ */ jsx(
520
- "div",
521
- {
522
- className: cn(
523
- "flex max-w-[85%] flex-wrap gap-3 px-1 sm:max-w-2xl",
524
- mine ? "justify-end" : "justify-start"
525
- ),
526
- children: attachments.map((attachment) => /* @__PURE__ */ jsx(
527
- ThreadAttachment,
528
- {
529
- room,
530
- attachment,
531
- onImageSettled
532
- },
533
- attachment.id
534
- ))
535
- }
536
- ) }) : null
537
- ] });
538
- }
539
- function EmptyState({
540
- title,
541
- description
542
- }) {
543
- return /* @__PURE__ */ jsxs("div", { className: "h-full mx-auto flex max-w-2xl flex-col items-center justify-center px-6 py-20 text-center", children: [
544
- /* @__PURE__ */ jsx("h2", { className: "text-4xl font-semibold tracking-tight text-foreground sm:text-5xl", children: title }),
545
- description?.trim() ? /* @__PURE__ */ jsx("p", { className: "mt-3 max-w-xl text-sm leading-6 text-muted-foreground sm:text-base", children: description }) : null
546
- ] });
547
- }
548
- function ChatThread({
549
- room,
550
- path,
551
- participants,
552
- agentName,
553
- emptyStateTitle,
554
- emptyStateDescription
555
- }) {
556
- const status = useThreadStatus({ room, path, agentName });
557
- const {
558
- document,
559
- messages,
560
- sendMessage,
561
- selectAttachments,
562
- attachments,
563
- setAttachments,
564
- localParticipantName,
565
- cancelRequest
566
- } = useChatThread({
567
- room,
568
- path,
569
- participants,
570
- agentName,
571
- useAgentMessages: agentName?.trim() !== "",
572
- messageType: status.mode === "steerable" && status.turnId != null ? "steer" : "chat",
573
- turnId: status.turnId
574
- });
575
- return /* @__PURE__ */ jsxs("div", { className: "flex min-h-0 flex-1 flex-col", children: [
576
- /* @__PURE__ */ jsx(
577
- ChatThreadView,
578
- {
579
- room,
580
- messages,
581
- isLoading: document === null,
582
- localParticipantName,
583
- path,
584
- threadStatusText: status.text,
585
- threadStatusStartedAt: status.startedAt,
586
- threadStatusMode: status.mode,
587
- onCancelRequest: cancelRequest,
588
- emptyStateTitle,
589
- emptyStateDescription
590
- }
591
- ),
592
- /* @__PURE__ */ jsx(
593
- ChatInput,
594
- {
595
- onSubmit: sendMessage,
596
- attachments,
597
- onFilesSelected: selectAttachments,
598
- setAttachments,
599
- disabled: document === null,
600
- placeholder: agentName?.trim() ? `Type a message or @${displayParticipantName(agentName)}` : "Type a message"
601
- }
602
- )
603
- ] });
604
- }
605
- function ChatThreadView({
606
- room,
607
- messages,
608
- isLoading = false,
609
- localParticipantName,
610
- path,
611
- showCompletedToolCalls = false,
612
- onShowCompletedToolCallsChanged,
613
- typing = false,
614
- thinking = false,
615
- threadStatusText,
616
- threadStatusStartedAt,
617
- threadStatusMode,
618
- onCancelRequest,
619
- emptyStateTitle,
620
- emptyStateDescription
621
- }) {
622
- const visibleMessages = useMemo(
623
- () => messages.filter((message) => shouldRenderThreadElement(message, showCompletedToolCalls)),
624
- [messages, showCompletedToolCalls]
625
- );
626
- const hiddenCompletedToolCallCount = useMemo(
627
- () => messages.filter(isCompletedToolCallEvent).length,
628
- [messages]
629
- );
630
- const [trackedInitialImageThreadKey, setTrackedInitialImageThreadKey] = useState(null);
631
- const [pendingInitialImageIds, setPendingInitialImageIds] = useState([]);
632
- const scrollContainerRef = useRef(null);
633
- const contentRef = useRef(null);
634
- const stickToBottomRef = useRef(true);
635
- const forceBottomRef = useRef(false);
636
- const lastVisibleMessageId = visibleMessages.length > 0 ? visibleMessages[visibleMessages.length - 1]?.id : null;
637
- const threadKey = path?.trim() || "__default__";
638
- const hasOverlay = threadStatusText?.trim() || thinking || typing;
639
- const waitingForInitialImages = trackedInitialImageThreadKey === threadKey && pendingInitialImageIds.length > 0;
640
- useEffect(() => {
641
- forceBottomRef.current = waitingForInitialImages;
642
- }, [waitingForInitialImages]);
643
- useEffect(() => {
644
- setTrackedInitialImageThreadKey(null);
645
- setPendingInitialImageIds([]);
646
- }, [threadKey]);
647
- useEffect(() => {
648
- if (trackedInitialImageThreadKey === threadKey || visibleMessages.length === 0) {
649
- return;
650
- }
651
- setTrackedInitialImageThreadKey(threadKey);
652
- setPendingInitialImageIds(collectImageAttachmentIds(visibleMessages));
653
- }, [threadKey, trackedInitialImageThreadKey, visibleMessages]);
654
- const handleImageSettled = useCallback((attachmentId) => {
655
- setPendingInitialImageIds((currentIds) => {
656
- if (!currentIds.includes(attachmentId)) {
657
- return currentIds;
658
- }
659
- return currentIds.filter((currentId) => currentId !== attachmentId);
660
- });
661
- }, []);
662
- useEffect(() => {
663
- const container = scrollContainerRef.current;
664
- if (!container) {
665
- return;
666
- }
667
- stickToBottomRef.current = true;
668
- container.scrollTop = container.scrollHeight;
669
- }, [path]);
670
- useEffect(() => {
671
- if (!waitingForInitialImages) {
672
- return;
673
- }
674
- const container = scrollContainerRef.current;
675
- if (!container) {
676
- return;
677
- }
678
- container.scrollTop = container.scrollHeight;
679
- }, [pendingInitialImageIds, waitingForInitialImages]);
680
- useEffect(() => {
681
- const container = scrollContainerRef.current;
682
- if (!container || !stickToBottomRef.current) {
683
- return;
684
- }
685
- container.scrollTop = container.scrollHeight;
686
- }, [hasOverlay, lastVisibleMessageId, threadStatusText, thinking, typing]);
687
- useEffect(() => {
688
- const container = scrollContainerRef.current;
689
- const content = contentRef.current;
690
- if (!container || !content || typeof ResizeObserver === "undefined") {
691
- return;
692
- }
693
- const observer = new ResizeObserver(() => {
694
- if (!stickToBottomRef.current && !forceBottomRef.current) {
695
- return;
696
- }
697
- container.scrollTop = container.scrollHeight;
698
- });
699
- observer.observe(content);
700
- return () => {
701
- observer.disconnect();
702
- };
703
- }, []);
704
- return /* @__PURE__ */ jsxs("div", { className: "relative flex min-h-0 flex-1 flex-col", children: [
705
- onShowCompletedToolCallsChanged && hiddenCompletedToolCallCount > 0 ? /* @__PURE__ */ jsx("div", { className: "pointer-events-none absolute inset-x-0 top-0 z-10 flex justify-center px-4 pt-3", children: /* @__PURE__ */ jsx("div", { className: "pointer-events-auto flex w-full max-w-[912px] justify-end", children: /* @__PURE__ */ jsx(
706
- Button,
707
- {
708
- type: "button",
709
- variant: "ghost",
710
- size: "sm",
711
- className: "rounded-full border bg-background/90 backdrop-blur",
712
- onClick: () => {
713
- onShowCompletedToolCallsChanged(!showCompletedToolCalls);
714
- },
715
- children: showCompletedToolCalls ? "Hide tool calls" : "Show tool calls"
716
- }
717
- ) }) }) : null,
718
- /* @__PURE__ */ jsx(
719
- "div",
720
- {
721
- ref: scrollContainerRef,
722
- className: "min-h-0 flex-1 overflow-y-auto overflow-x-hidden [overflow-anchor:none]",
723
- onScroll: (event) => {
724
- stickToBottomRef.current = isNearBottom(event.currentTarget);
725
- },
726
- children: /* @__PURE__ */ jsxs(
727
- "div",
728
- {
729
- ref: contentRef,
730
- className: cn(
731
- "mx-auto flex min-h-full w-full max-w-[912px] flex-col gap-8 px-4 pt-6",
732
- visibleMessages.length > 0 ? "justify-end" : null,
733
- hasOverlay ? "pb-24" : "pb-6"
734
- ),
735
- children: [
736
- isLoading ? /* @__PURE__ */ jsx("div", { className: "flex flex-1 items-center justify-center py-20", children: /* @__PURE__ */ jsx(Spinner, { size: "lg", className: "text-muted-foreground" }) }) : visibleMessages.length === 0 && emptyStateTitle ? /* @__PURE__ */ jsx(EmptyState, { title: emptyStateTitle, description: emptyStateDescription }) : null,
737
- visibleMessages.map((message, index) => {
738
- const previous = index > 0 ? visibleMessages[index - 1] : null;
739
- if (message.tagName === "message") {
740
- return /* @__PURE__ */ jsx(
741
- ThreadMessage,
742
- {
743
- room,
744
- message,
745
- previous,
746
- localParticipantName,
747
- onImageSettled: handleImageSettled
748
- },
749
- message.id
750
- );
751
- }
752
- if (message.tagName === "reasoning") {
753
- return /* @__PURE__ */ jsx(ThreadReasoning, { message }, message.id);
754
- }
755
- if (message.tagName === "exec") {
756
- return /* @__PURE__ */ jsx(ThreadExec, { message }, message.id);
757
- }
758
- if (message.tagName === "event") {
759
- return /* @__PURE__ */ jsx(EventRow, { room, message }, message.id);
760
- }
761
- return null;
762
- })
763
- ]
764
- }
765
- )
766
- }
767
- ),
768
- hasOverlay ? /* @__PURE__ */ jsx("div", { className: "pointer-events-none absolute inset-x-0 bottom-0 flex justify-center px-4 pb-4", children: /* @__PURE__ */ jsx("div", { className: "pointer-events-auto w-full max-w-[912px]", children: /* @__PURE__ */ jsx(
769
- ChatTypingIndicator,
770
- {
771
- typing,
772
- thinking,
773
- statusText: threadStatusText,
774
- startedAt: threadStatusStartedAt,
775
- onCancel: onCancelRequest,
776
- showCancelButton: threadStatusMode != null,
777
- cancelEnabled: !isCancellingThreadStatusText(threadStatusText)
778
- }
779
- ) }) }) : null
780
- ] });
781
- }
782
- export {
783
- ChatThread,
784
- timeAgo
785
- };