@plaisolutions/react 0.1.1 → 0.3.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.
@@ -0,0 +1,2452 @@
1
+ import { createContext, forwardRef, useCallback, useContext, useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
2
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
3
+ import { Bot, CheckCircle2, Database, File, FileText, Globe, LoaderCircle, MessageSquare, Minus, Plug, Plus, Workflow, XCircle } from "lucide-react";
4
+
5
+ //#region src/components/internal/join-classes.ts
6
+ function joinClasses(...classes) {
7
+ return classes.filter(Boolean).join(" ");
8
+ }
9
+
10
+ //#endregion
11
+ //#region src/components/datasource-tool-result-card.tsx
12
+ function DatasourceIcon() {
13
+ return /* @__PURE__ */ jsxs("svg", {
14
+ xmlns: "http://www.w3.org/2000/svg",
15
+ width: "20",
16
+ height: "20",
17
+ viewBox: "0 0 20 20",
18
+ fill: "none",
19
+ stroke: "currentColor",
20
+ strokeWidth: "1.5",
21
+ strokeLinecap: "round",
22
+ strokeLinejoin: "round",
23
+ "aria-hidden": "true",
24
+ children: [
25
+ /* @__PURE__ */ jsx("rect", {
26
+ x: "2.75",
27
+ y: "3.25",
28
+ width: "13",
29
+ height: "11",
30
+ rx: "1.5"
31
+ }),
32
+ /* @__PURE__ */ jsx("path", { d: "M2.75 7h13" }),
33
+ /* @__PURE__ */ jsx("rect", {
34
+ x: "10.25",
35
+ y: "10.25",
36
+ width: "7",
37
+ height: "6.5",
38
+ rx: "1.25",
39
+ fill: "#f5f5f5"
40
+ }),
41
+ /* @__PURE__ */ jsx("path", { d: "M10.25 13.25h7" })
42
+ ]
43
+ });
44
+ }
45
+ function DatasourceToolResultCardContent({ title, description, type, resourceCount }) {
46
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
47
+ /* @__PURE__ */ jsxs("header", {
48
+ className: "flex items-center gap-3",
49
+ children: [/* @__PURE__ */ jsx(DatasourceIcon, {}), /* @__PURE__ */ jsx("p", {
50
+ className: "min-w-0 truncate text-base leading-5",
51
+ children: type
52
+ })]
53
+ }),
54
+ /* @__PURE__ */ jsx("h3", {
55
+ className: "mt-3 line-clamp-2 text-sm font-normal leading-5",
56
+ children: title
57
+ }),
58
+ /* @__PURE__ */ jsx("p", {
59
+ className: "mt-2 line-clamp-2 text-xs leading-4",
60
+ children: description
61
+ }),
62
+ resourceCount === void 0 ? null : /* @__PURE__ */ jsxs("p", {
63
+ className: "mt-2 text-xs leading-4 text-neutral-600",
64
+ children: [
65
+ resourceCount,
66
+ " ",
67
+ resourceCount === 1 ? "Recurso" : "Recursos"
68
+ ]
69
+ })
70
+ ] });
71
+ }
72
+ function DatasourceToolResultCard(props) {
73
+ return /* @__PURE__ */ jsx("article", {
74
+ "aria-label": `${props.type}: ${props.title}`,
75
+ className: "w-[186px] rounded-lg bg-neutral-100 p-4 text-neutral-950",
76
+ children: /* @__PURE__ */ jsx(DatasourceToolResultCardContent, { ...props })
77
+ });
78
+ }
79
+
80
+ //#endregion
81
+ //#region src/components/resource-card.tsx
82
+ function ResourceCardContent({ icon, title, description }) {
83
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("header", {
84
+ className: "flex min-w-0 items-center gap-3",
85
+ children: [icon ? /* @__PURE__ */ jsx("img", {
86
+ src: icon,
87
+ alt: "",
88
+ width: "20",
89
+ height: "20",
90
+ className: "h-5 w-5 shrink-0 object-contain"
91
+ }) : /* @__PURE__ */ jsx(File, {
92
+ className: "h-5 w-5 shrink-0",
93
+ "aria-hidden": "true"
94
+ }), /* @__PURE__ */ jsx("p", {
95
+ className: "min-w-0 truncate text-base leading-5",
96
+ children: title
97
+ })]
98
+ }), /* @__PURE__ */ jsx("p", {
99
+ className: "mt-2 line-clamp-2 text-sm leading-5",
100
+ children: description
101
+ })] });
102
+ }
103
+ function ResourceCard(props) {
104
+ const className = "block w-full rounded-lg bg-neutral-100 p-4 text-left text-neutral-950";
105
+ if (!props.url) return /* @__PURE__ */ jsx("article", {
106
+ "aria-label": `${props.title}: ${props.description}`,
107
+ className,
108
+ children: /* @__PURE__ */ jsx(ResourceCardContent, { ...props })
109
+ });
110
+ return /* @__PURE__ */ jsx("a", {
111
+ href: props.url,
112
+ target: "_blank",
113
+ rel: "noreferrer noopener",
114
+ className: `${className} transition-colors hover:bg-neutral-200 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950`,
115
+ children: /* @__PURE__ */ jsx(ResourceCardContent, { ...props })
116
+ });
117
+ }
118
+
119
+ //#endregion
120
+ //#region src/components/icons/arrow.tsx
121
+ function ArrowUp(props) {
122
+ return /* @__PURE__ */ jsxs("svg", {
123
+ ...props,
124
+ xmlns: "http://www.w3.org/2000/svg",
125
+ width: "24",
126
+ height: "24",
127
+ viewBox: "0 0 24 24",
128
+ fill: "none",
129
+ stroke: "currentColor",
130
+ strokeWidth: "2",
131
+ strokeLinecap: "round",
132
+ strokeLinejoin: "round",
133
+ "aria-hidden": "true",
134
+ children: [/* @__PURE__ */ jsx("path", { d: "m5 12 7-7 7 7" }), /* @__PURE__ */ jsx("path", { d: "M12 19V5" })]
135
+ });
136
+ }
137
+
138
+ //#endregion
139
+ //#region src/components/icons/loader.tsx
140
+ function Loader(props) {
141
+ return /* @__PURE__ */ jsx("svg", {
142
+ ...props,
143
+ xmlns: "http://www.w3.org/2000/svg",
144
+ width: "24",
145
+ height: "24",
146
+ viewBox: "0 0 24 24",
147
+ fill: "none",
148
+ stroke: "currentColor",
149
+ strokeWidth: "2",
150
+ strokeLinecap: "round",
151
+ strokeLinejoin: "round",
152
+ "aria-hidden": "true",
153
+ children: /* @__PURE__ */ jsx("path", { d: "M21 12a9 9 0 1 1-6.219-8.56" })
154
+ });
155
+ }
156
+
157
+ //#endregion
158
+ //#region src/components/icons/microphone.tsx
159
+ function Microphone(props) {
160
+ return /* @__PURE__ */ jsxs("svg", {
161
+ ...props,
162
+ xmlns: "http://www.w3.org/2000/svg",
163
+ width: "24",
164
+ height: "24",
165
+ viewBox: "0 0 24 24",
166
+ fill: "none",
167
+ stroke: "currentColor",
168
+ strokeWidth: "2",
169
+ strokeLinecap: "round",
170
+ strokeLinejoin: "round",
171
+ "aria-hidden": "true",
172
+ children: [
173
+ /* @__PURE__ */ jsx("path", { d: "M12 2a3 3 0 0 0-3 3v7a3 3 0 0 0 6 0V5a3 3 0 0 0-3-3Z" }),
174
+ /* @__PURE__ */ jsx("path", { d: "M19 10v2a7 7 0 0 1-14 0v-2" }),
175
+ /* @__PURE__ */ jsx("path", { d: "M12 19v3" })
176
+ ]
177
+ });
178
+ }
179
+
180
+ //#endregion
181
+ //#region src/components/icons/paperclip.tsx
182
+ function Paperclip(props) {
183
+ return /* @__PURE__ */ jsx("svg", {
184
+ ...props,
185
+ xmlns: "http://www.w3.org/2000/svg",
186
+ width: "24",
187
+ height: "24",
188
+ viewBox: "0 0 24 24",
189
+ fill: "none",
190
+ stroke: "currentColor",
191
+ strokeWidth: "2",
192
+ strokeLinecap: "round",
193
+ strokeLinejoin: "round",
194
+ "aria-hidden": "true",
195
+ children: /* @__PURE__ */ jsx("path", { d: "m21.44 11.05-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48" })
196
+ });
197
+ }
198
+
199
+ //#endregion
200
+ //#region src/components/icons/stop.tsx
201
+ function Stop(props) {
202
+ return /* @__PURE__ */ jsx("svg", {
203
+ ...props,
204
+ xmlns: "http://www.w3.org/2000/svg",
205
+ width: "24",
206
+ height: "24",
207
+ viewBox: "0 0 24 24",
208
+ fill: "none",
209
+ stroke: "currentColor",
210
+ strokeWidth: "2",
211
+ strokeLinecap: "round",
212
+ strokeLinejoin: "round",
213
+ "aria-hidden": "true",
214
+ children: /* @__PURE__ */ jsx("rect", {
215
+ x: "6",
216
+ y: "6",
217
+ width: "12",
218
+ height: "12",
219
+ rx: "1"
220
+ })
221
+ });
222
+ }
223
+
224
+ //#endregion
225
+ //#region src/components/icons/x.tsx
226
+ function X(props) {
227
+ return /* @__PURE__ */ jsxs("svg", {
228
+ ...props,
229
+ xmlns: "http://www.w3.org/2000/svg",
230
+ width: "24",
231
+ height: "24",
232
+ viewBox: "0 0 24 24",
233
+ fill: "none",
234
+ stroke: "currentColor",
235
+ strokeWidth: "2",
236
+ strokeLinecap: "round",
237
+ strokeLinejoin: "round",
238
+ "aria-hidden": "true",
239
+ children: [/* @__PURE__ */ jsx("path", { d: "M18 6 6 18" }), /* @__PURE__ */ jsx("path", { d: "m6 6 12 12" })]
240
+ });
241
+ }
242
+
243
+ //#endregion
244
+ //#region src/components/sheet/sheet.tsx
245
+ const SheetContext = createContext(null);
246
+ let bodyScrollLockCount = 0;
247
+ let bodyOverflowBeforeLock = "";
248
+ function lockBodyScroll() {
249
+ if (bodyScrollLockCount === 0) {
250
+ bodyOverflowBeforeLock = document.body.style.overflow;
251
+ document.body.style.overflow = "hidden";
252
+ }
253
+ bodyScrollLockCount += 1;
254
+ }
255
+ function unlockBodyScroll() {
256
+ bodyScrollLockCount = Math.max(0, bodyScrollLockCount - 1);
257
+ if (bodyScrollLockCount === 0) document.body.style.overflow = bodyOverflowBeforeLock;
258
+ }
259
+ function useSheetContext(componentName) {
260
+ const context = useContext(SheetContext);
261
+ if (!context) throw new Error(`${componentName} must be used within Sheet.`);
262
+ return context;
263
+ }
264
+ function assignRef(ref, value) {
265
+ if (typeof ref === "function") ref(value);
266
+ else if (ref) ref.current = value;
267
+ }
268
+ function Sheet({ children, open: controlledOpen, defaultOpen = false, onOpenChange }) {
269
+ const [uncontrolledOpen, setUncontrolledOpen] = useState(defaultOpen);
270
+ const [labelledBy, setLabelledBy] = useState();
271
+ const [describedBy, setDescribedBy] = useState();
272
+ const triggerRef = useRef(null);
273
+ const contentId = useId();
274
+ const titleId = useId();
275
+ const descriptionId = useId();
276
+ const open = controlledOpen ?? uncontrolledOpen;
277
+ const setOpen = useCallback((nextOpen) => {
278
+ if (controlledOpen === void 0) setUncontrolledOpen(nextOpen);
279
+ onOpenChange?.(nextOpen);
280
+ }, [controlledOpen, onOpenChange]);
281
+ const context = useMemo(() => ({
282
+ open,
283
+ setOpen,
284
+ triggerRef,
285
+ contentId,
286
+ titleId,
287
+ descriptionId,
288
+ labelledBy,
289
+ describedBy,
290
+ setLabelledBy,
291
+ setDescribedBy
292
+ }), [
293
+ open,
294
+ setOpen,
295
+ contentId,
296
+ titleId,
297
+ descriptionId,
298
+ labelledBy,
299
+ describedBy
300
+ ]);
301
+ return /* @__PURE__ */ jsx(SheetContext.Provider, {
302
+ value: context,
303
+ children
304
+ });
305
+ }
306
+ const SheetTrigger = forwardRef(function SheetTrigger$1({ onClick, type = "button",...props }, forwardedRef) {
307
+ const { open, setOpen, triggerRef, contentId } = useSheetContext("SheetTrigger");
308
+ const setTriggerRef = useCallback((element) => {
309
+ triggerRef.current = element;
310
+ assignRef(forwardedRef, element);
311
+ }, [forwardedRef, triggerRef]);
312
+ return /* @__PURE__ */ jsx("button", {
313
+ ...props,
314
+ ref: setTriggerRef,
315
+ type,
316
+ "data-slot": "sheet-trigger",
317
+ "aria-haspopup": "dialog",
318
+ "aria-expanded": open,
319
+ "aria-controls": contentId,
320
+ onClick: (event) => {
321
+ onClick?.(event);
322
+ if (!event.defaultPrevented) setOpen(true);
323
+ }
324
+ });
325
+ });
326
+ const SheetClose = forwardRef(function SheetClose$1({ onClick, type = "button",...props }, forwardedRef) {
327
+ const { setOpen } = useSheetContext("SheetClose");
328
+ return /* @__PURE__ */ jsx("button", {
329
+ ...props,
330
+ ref: forwardedRef,
331
+ type,
332
+ "data-slot": "sheet-close",
333
+ onClick: (event) => {
334
+ onClick?.(event);
335
+ if (!event.defaultPrevented) setOpen(false);
336
+ }
337
+ });
338
+ });
339
+ function getSideClasses(side) {
340
+ if (side === "left") return "inset-y-0 left-0 h-full w-full max-w-lg border-r";
341
+ if (side === "top") return "inset-x-0 top-0 max-h-[85vh] w-full border-b";
342
+ if (side === "bottom") return "inset-x-0 bottom-0 max-h-[85vh] w-full border-t";
343
+ return "inset-y-0 right-0 h-full w-full max-w-lg border-l";
344
+ }
345
+ const SheetContent = forwardRef(function SheetContent$1({ children, className, side = "right", showCloseButton = true, closeLabel = "Close", onCancel, onClick, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy,...props }, forwardedRef) {
346
+ const { open, setOpen, triggerRef, contentId, labelledBy, describedBy } = useSheetContext("SheetContent");
347
+ const [present, setPresent] = useState(open);
348
+ const dialogRef = useRef(null);
349
+ const setDialogRef = useCallback((element) => {
350
+ dialogRef.current = element;
351
+ assignRef(forwardedRef, element);
352
+ }, [forwardedRef]);
353
+ useEffect(() => {
354
+ if (open) setPresent(true);
355
+ }, [open]);
356
+ useEffect(() => {
357
+ if (!present) return;
358
+ const dialog = dialogRef.current;
359
+ if (!dialog) return;
360
+ if (!dialog.open) if (typeof dialog.showModal === "function") dialog.showModal();
361
+ else dialog.setAttribute("open", "");
362
+ lockBodyScroll();
363
+ (dialog.querySelector("[data-sheet-initial-focus], [autofocus]") ?? dialog.querySelector("[data-slot=\"sheet-title\"]") ?? dialog.querySelector("button, [href], [tabindex=\"0\"]"))?.focus({ preventScroll: true });
364
+ return () => {
365
+ unlockBodyScroll();
366
+ if (dialog.open) if (typeof dialog.close === "function") dialog.close();
367
+ else dialog.removeAttribute("open");
368
+ triggerRef.current?.focus({ preventScroll: true });
369
+ };
370
+ }, [present, triggerRef]);
371
+ useEffect(() => {
372
+ if (open || !present) return;
373
+ const fallbackTimer = window.setTimeout(() => setPresent(false), 250);
374
+ return () => window.clearTimeout(fallbackTimer);
375
+ }, [open, present]);
376
+ if (!present) return null;
377
+ const state = open ? "open" : "closed";
378
+ return /* @__PURE__ */ jsx("dialog", {
379
+ ...props,
380
+ ref: setDialogRef,
381
+ id: contentId,
382
+ "data-slot": "sheet-overlay",
383
+ "data-state": state,
384
+ "aria-label": ariaLabel,
385
+ "aria-labelledby": ariaLabelledBy ?? labelledBy,
386
+ "aria-describedby": ariaDescribedBy ?? describedBy,
387
+ className: "plai-sheet-overlay fixed inset-0 m-0 h-full max-h-none w-full max-w-none border-0 bg-black/80 p-0",
388
+ onCancel: (event) => {
389
+ onCancel?.(event);
390
+ if (!event.defaultPrevented) {
391
+ event.preventDefault();
392
+ setOpen(false);
393
+ }
394
+ },
395
+ onClick: (event) => {
396
+ onClick?.(event);
397
+ if (!event.defaultPrevented && event.target === event.currentTarget) setOpen(false);
398
+ },
399
+ children: /* @__PURE__ */ jsxs("div", {
400
+ "data-slot": "sheet-content",
401
+ "data-state": state,
402
+ "data-side": side,
403
+ className: joinClasses("plai-sheet-content fixed overflow-y-auto border-neutral-200 bg-white px-6 py-5 text-neutral-950 shadow-2xl outline-none", getSideClasses(side), className),
404
+ onAnimationEnd: (event) => {
405
+ if (event.currentTarget === event.target && !open) setPresent(false);
406
+ },
407
+ children: [showCloseButton ? /* @__PURE__ */ jsx(SheetClose, {
408
+ "aria-label": closeLabel,
409
+ className: "absolute right-4 top-2 rounded-sm p-1 text-neutral-500 transition-colors hover:bg-neutral-100 hover:text-neutral-950 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950",
410
+ children: /* @__PURE__ */ jsx(X, { className: "h-5 w-5" })
411
+ }) : null, children]
412
+ })
413
+ });
414
+ });
415
+ const SheetHeader = forwardRef(function SheetHeader$1({ className,...props }, forwardedRef) {
416
+ return /* @__PURE__ */ jsx("div", {
417
+ ...props,
418
+ ref: forwardedRef,
419
+ "data-slot": "sheet-header",
420
+ className: joinClasses("flex flex-col gap-4", className)
421
+ });
422
+ });
423
+ const SheetFooter = forwardRef(function SheetFooter$1({ className,...props }, forwardedRef) {
424
+ return /* @__PURE__ */ jsx("div", {
425
+ ...props,
426
+ ref: forwardedRef,
427
+ "data-slot": "sheet-footer",
428
+ className: joinClasses("mt-auto flex flex-col gap-2", className)
429
+ });
430
+ });
431
+ const SheetTitle = forwardRef(function SheetTitle$1({ className, id, tabIndex = -1,...props }, forwardedRef) {
432
+ const { titleId, setLabelledBy } = useSheetContext("SheetTitle");
433
+ const resolvedId = id ?? titleId;
434
+ useEffect(() => {
435
+ setLabelledBy(resolvedId);
436
+ return () => {
437
+ setLabelledBy((current) => current === resolvedId ? void 0 : current);
438
+ };
439
+ }, [resolvedId, setLabelledBy]);
440
+ return /* @__PURE__ */ jsx("h2", {
441
+ ...props,
442
+ ref: forwardedRef,
443
+ id: resolvedId,
444
+ tabIndex,
445
+ "data-slot": "sheet-title",
446
+ className: joinClasses("text-lg font-semibold leading-6 outline-none", className)
447
+ });
448
+ });
449
+ const SheetDescription = forwardRef(function SheetDescription$1({ className, id,...props }, forwardedRef) {
450
+ const { descriptionId, setDescribedBy } = useSheetContext("SheetDescription");
451
+ const resolvedId = id ?? descriptionId;
452
+ useEffect(() => {
453
+ setDescribedBy(resolvedId);
454
+ return () => {
455
+ setDescribedBy((current) => current === resolvedId ? void 0 : current);
456
+ };
457
+ }, [resolvedId, setDescribedBy]);
458
+ return /* @__PURE__ */ jsx("p", {
459
+ ...props,
460
+ ref: forwardedRef,
461
+ id: resolvedId,
462
+ "data-slot": "sheet-description",
463
+ className: joinClasses("text-sm text-neutral-600", className)
464
+ });
465
+ });
466
+
467
+ //#endregion
468
+ //#region src/components/datasource-folder-card.tsx
469
+ function DatasourceFolderCard({ title, description, type, resources }) {
470
+ return /* @__PURE__ */ jsxs(Sheet, { children: [/* @__PURE__ */ jsx(SheetTrigger, {
471
+ "aria-label": `${type}: ${title}`,
472
+ className: "min-h-[183px] w-[186px] rounded-lg bg-neutral-100 p-4 text-left font-normal text-neutral-950 transition-colors hover:bg-neutral-200 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950",
473
+ children: /* @__PURE__ */ jsx(DatasourceToolResultCardContent, {
474
+ title,
475
+ description,
476
+ type,
477
+ resourceCount: resources.length
478
+ })
479
+ }), /* @__PURE__ */ jsxs(SheetContent, {
480
+ closeLabel: "Cerrar",
481
+ children: [/* @__PURE__ */ jsxs(SheetHeader, {
482
+ className: "pr-10",
483
+ children: [/* @__PURE__ */ jsxs("div", {
484
+ className: "flex items-start gap-3",
485
+ children: [/* @__PURE__ */ jsx("span", {
486
+ className: "mt-0.5 shrink-0",
487
+ children: /* @__PURE__ */ jsx(DatasourceIcon, {})
488
+ }), /* @__PURE__ */ jsx(SheetTitle, { children: title })]
489
+ }), description ? /* @__PURE__ */ jsx(SheetDescription, {
490
+ className: "line-clamp-2 text-xs leading-4",
491
+ children: description
492
+ }) : null]
493
+ }), /* @__PURE__ */ jsxs("section", {
494
+ className: "mt-5",
495
+ "aria-label": "Recursos",
496
+ children: [/* @__PURE__ */ jsx("h3", {
497
+ className: "text-xs font-medium leading-4",
498
+ children: "Ver"
499
+ }), /* @__PURE__ */ jsx("div", {
500
+ className: "mt-7 space-y-3",
501
+ children: resources.map((resource, index) => /* @__PURE__ */ jsx(ResourceCard, { ...resource }, `${resource.url ?? resource.title}-${index}`))
502
+ })]
503
+ })]
504
+ })] });
505
+ }
506
+
507
+ //#endregion
508
+ //#region src/components/datasource-tool-resources.tsx
509
+ function isObject(value) {
510
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
511
+ }
512
+ function readString(value, key) {
513
+ const candidate = value?.[key];
514
+ return typeof candidate === "string" && candidate.trim() ? candidate : null;
515
+ }
516
+ function getResourceIcon(resource) {
517
+ return readString(resource.extra_info, "icon") ?? readString(resource.metadata, "icon") ?? void 0;
518
+ }
519
+ function toResourceCardProps(resource) {
520
+ return {
521
+ icon: getResourceIcon(resource),
522
+ title: resource.type,
523
+ description: resource.name,
524
+ url: resource.external_url ?? resource.url
525
+ };
526
+ }
527
+ function isDatasourceResource(value) {
528
+ if (!isObject(value)) return false;
529
+ const folder = value.folder;
530
+ const datasource = value.datasource;
531
+ return typeof value.id === "string" && typeof value.name === "string" && typeof value.type === "string" && (value.url === null || typeof value.url === "string") && (value.external_url === null || typeof value.external_url === "string") && isObject(value.metadata) && isObject(value.extra_info) && (folder === null || isObject(folder) && typeof folder.id === "string" && typeof folder.name === "string" && isObject(folder.extra_info)) && (datasource === null || isObject(datasource) && typeof datasource.type === "string" && (datasource.description === null || typeof datasource.description === "string") && (datasource.summary === null || typeof datasource.summary === "string"));
532
+ }
533
+ function DatasourceToolResources({ resources, className }) {
534
+ const folderGroups = /* @__PURE__ */ new Map();
535
+ const ungroupedResources = [];
536
+ for (const resource of resources) {
537
+ if (!resource.folder) {
538
+ ungroupedResources.push(resource);
539
+ continue;
540
+ }
541
+ const existingGroup = folderGroups.get(resource.folder.id);
542
+ if (existingGroup) existingGroup.resources.push(resource);
543
+ else folderGroups.set(resource.folder.id, {
544
+ folder: resource.folder,
545
+ resources: [resource]
546
+ });
547
+ }
548
+ return /* @__PURE__ */ jsxs("section", {
549
+ "aria-label": "Recursos de la fuente de datos",
550
+ className: joinClasses("flex flex-wrap gap-3", className),
551
+ children: [Array.from(folderGroups.values()).map(({ folder, resources: group }) => {
552
+ const datasource = group[0]?.datasource;
553
+ const type = readString(folder.extra_info, "type") ?? datasource?.type ?? "FOLDER";
554
+ const description = readString(folder.extra_info, "description") ?? readString(folder.extra_info, "summary") ?? datasource?.description ?? datasource?.summary ?? "";
555
+ return /* @__PURE__ */ jsx(DatasourceFolderCard, {
556
+ title: folder.name,
557
+ description,
558
+ type,
559
+ resources: group.map(toResourceCardProps)
560
+ }, folder.id);
561
+ }), ungroupedResources.map((resource) => /* @__PURE__ */ jsx(ResourceCard, { ...toResourceCardProps(resource) }, resource.id))]
562
+ });
563
+ }
564
+
565
+ //#endregion
566
+ //#region src/components/internal/format-tool-error-details.ts
567
+ function formatToolErrorDetails(value) {
568
+ if (typeof value === "string") return value;
569
+ if (!value || typeof value !== "object" || Array.isArray(value)) return value == null ? null : String(value);
570
+ const details = value;
571
+ for (const key of [
572
+ "error_message",
573
+ "message",
574
+ "detail"
575
+ ]) {
576
+ const candidate = details[key];
577
+ if (typeof candidate === "string" && candidate.trim()) return candidate;
578
+ }
579
+ try {
580
+ return JSON.stringify(value, null, 2);
581
+ } catch {
582
+ return "Unknown tool error";
583
+ }
584
+ }
585
+
586
+ //#endregion
587
+ //#region src/components/tool-result-email-send-card.tsx
588
+ function formatStatus$1(status) {
589
+ if (status === "pending") return "pending";
590
+ if (status === "error") return "error";
591
+ return "completed";
592
+ }
593
+ function asObject(value) {
594
+ if (!value || typeof value !== "object" || Array.isArray(value)) return null;
595
+ return value;
596
+ }
597
+ function toStringOrNull$2(value) {
598
+ return typeof value === "string" && value.trim().length > 0 ? value : null;
599
+ }
600
+ function toNumberOrNull(value) {
601
+ return typeof value === "number" && Number.isFinite(value) ? value : null;
602
+ }
603
+ function parseEmailSendInput(part) {
604
+ const input = asObject(part.input);
605
+ if (!input) return {
606
+ to: [],
607
+ subject: null,
608
+ text: null
609
+ };
610
+ return {
611
+ to: Array.isArray(input.to) ? input.to.filter((item) => typeof item === "string") : [],
612
+ subject: toStringOrNull$2(input.subject),
613
+ text: toStringOrNull$2(input.text)
614
+ };
615
+ }
616
+ function parseEmailSendResult(part) {
617
+ const initial = {
618
+ status: null,
619
+ recipientsCount: null,
620
+ subject: null,
621
+ receiptProvider: null,
622
+ receiptMessageId: null,
623
+ receiptRawStatus: null,
624
+ raw: null
625
+ };
626
+ if (part.result === void 0 || part.result === null) return initial;
627
+ let resultObject = asObject(part.result);
628
+ let raw = null;
629
+ if (!resultObject && typeof part.result === "string") try {
630
+ resultObject = asObject(JSON.parse(part.result));
631
+ } catch {
632
+ raw = part.result;
633
+ }
634
+ if (!resultObject) return {
635
+ ...initial,
636
+ raw
637
+ };
638
+ const receipt = asObject(resultObject.receipt);
639
+ return {
640
+ status: toStringOrNull$2(resultObject.status),
641
+ recipientsCount: toNumberOrNull(resultObject.recipients_count),
642
+ subject: toStringOrNull$2(resultObject.subject),
643
+ receiptProvider: receipt ? toStringOrNull$2(receipt.provider) : null,
644
+ receiptMessageId: receipt ? toStringOrNull$2(receipt.message_id) : null,
645
+ receiptRawStatus: receipt ? toStringOrNull$2(receipt.raw_status) : null,
646
+ raw
647
+ };
648
+ }
649
+ function ToolResultEmailSendCard({ part, className }) {
650
+ const [isExpanded, setIsExpanded] = useState(false);
651
+ const emailInput = parseEmailSendInput(part);
652
+ const emailResult = parseEmailSendResult(part);
653
+ const errorDetails = formatToolErrorDetails(part.errorDetails);
654
+ const statusClass = part.state === "error" ? "border-rose-200 bg-rose-50 text-rose-800" : part.state === "pending" ? "border-amber-200 bg-amber-50 text-amber-800" : "border-emerald-200 bg-emerald-50 text-emerald-800";
655
+ return /* @__PURE__ */ jsxs("section", {
656
+ className: joinClasses("space-y-4 rounded-lg border border-slate-200 bg-slate-50 p-3", className),
657
+ "data-tool-call-id": part.id,
658
+ "data-tool-call-state": part.state,
659
+ children: [
660
+ /* @__PURE__ */ jsxs("header", {
661
+ className: "flex items-center justify-between gap-2",
662
+ children: [/* @__PURE__ */ jsx("h4", {
663
+ className: "text-sm font-semibold text-slate-900",
664
+ children: "Email"
665
+ }), /* @__PURE__ */ jsxs("div", {
666
+ className: "flex items-center gap-2",
667
+ children: [/* @__PURE__ */ jsx("span", {
668
+ className: joinClasses("rounded-full border px-2 py-0.5 text-xs font-medium", statusClass),
669
+ children: formatStatus$1(part.state)
670
+ }), /* @__PURE__ */ jsx("button", {
671
+ type: "button",
672
+ "aria-expanded": isExpanded,
673
+ "aria-controls": `tool-email-details-${part.id}`,
674
+ className: "inline-flex h-6 w-6 items-center justify-center rounded border border-slate-300 bg-white text-xs text-slate-700 hover:bg-slate-100",
675
+ onClick: () => setIsExpanded((previous) => !previous),
676
+ children: /* @__PURE__ */ jsx("span", {
677
+ className: "inline-block",
678
+ children: isExpanded ? "^" : "v"
679
+ })
680
+ })]
681
+ })]
682
+ }),
683
+ emailInput.subject ? /* @__PURE__ */ jsxs("div", {
684
+ className: "space-y-1",
685
+ children: [/* @__PURE__ */ jsx("p", {
686
+ className: "text-xs font-semibold text-slate-600",
687
+ children: "Subject"
688
+ }), /* @__PURE__ */ jsx("p", {
689
+ className: "text-sm text-slate-900",
690
+ children: emailInput.subject
691
+ })]
692
+ }) : null,
693
+ /* @__PURE__ */ jsxs("div", {
694
+ className: "space-y-1",
695
+ children: [/* @__PURE__ */ jsx("p", {
696
+ className: "text-xs font-semibold text-slate-600",
697
+ children: "To"
698
+ }), emailInput.to.length > 0 ? /* @__PURE__ */ jsx("div", {
699
+ className: "flex flex-wrap gap-2",
700
+ children: emailInput.to.map((recipient) => /* @__PURE__ */ jsx("span", {
701
+ className: "rounded-full border border-slate-300 bg-slate-100 px-2 py-0.5 text-xs text-slate-700",
702
+ children: recipient
703
+ }, recipient))
704
+ }) : /* @__PURE__ */ jsx("p", {
705
+ className: "text-sm text-slate-500",
706
+ children: "No recipients provided."
707
+ })]
708
+ }),
709
+ /* @__PURE__ */ jsxs("div", {
710
+ id: `tool-email-details-${part.id}`,
711
+ "data-testid": "email-send-details",
712
+ className: joinClasses(isExpanded ? "block space-y-4" : "hidden"),
713
+ children: [emailInput.text ? /* @__PURE__ */ jsxs("div", {
714
+ className: "space-y-1",
715
+ children: [/* @__PURE__ */ jsx("p", {
716
+ className: "text-xs font-semibold text-slate-600",
717
+ children: "Body"
718
+ }), /* @__PURE__ */ jsx("p", {
719
+ className: "whitespace-pre-wrap rounded border border-slate-200 bg-slate-50 p-2 text-sm text-slate-700",
720
+ children: emailInput.text
721
+ })]
722
+ }) : null, part.result !== void 0 ? /* @__PURE__ */ jsxs("div", {
723
+ className: "space-y-2 rounded border border-slate-200 bg-white p-3",
724
+ children: [
725
+ /* @__PURE__ */ jsx("p", {
726
+ className: "text-xs font-semibold uppercase tracking-wide text-slate-500",
727
+ children: "Delivery"
728
+ }),
729
+ emailResult.status ? /* @__PURE__ */ jsxs("p", {
730
+ className: "text-sm text-slate-700",
731
+ children: [
732
+ /* @__PURE__ */ jsx("span", {
733
+ className: "font-semibold text-slate-900",
734
+ children: "Status:"
735
+ }),
736
+ " ",
737
+ emailResult.status
738
+ ]
739
+ }) : null,
740
+ emailResult.recipientsCount !== null ? /* @__PURE__ */ jsxs("p", {
741
+ className: "text-sm text-slate-700",
742
+ children: [
743
+ /* @__PURE__ */ jsx("span", {
744
+ className: "font-semibold text-slate-900",
745
+ children: "Recipients:"
746
+ }),
747
+ " ",
748
+ emailResult.recipientsCount
749
+ ]
750
+ }) : null,
751
+ emailResult.receiptProvider ? /* @__PURE__ */ jsxs("p", {
752
+ className: "text-sm text-slate-700",
753
+ children: [
754
+ /* @__PURE__ */ jsx("span", {
755
+ className: "font-semibold text-slate-900",
756
+ children: "Provider:"
757
+ }),
758
+ " ",
759
+ emailResult.receiptProvider
760
+ ]
761
+ }) : null,
762
+ emailResult.receiptMessageId ? /* @__PURE__ */ jsxs("p", {
763
+ className: "break-all text-sm text-slate-700",
764
+ children: [
765
+ /* @__PURE__ */ jsx("span", {
766
+ className: "font-semibold text-slate-900",
767
+ children: "Message ID:"
768
+ }),
769
+ " ",
770
+ emailResult.receiptMessageId
771
+ ]
772
+ }) : null,
773
+ emailResult.receiptRawStatus ? /* @__PURE__ */ jsxs("p", {
774
+ className: "text-sm text-slate-700",
775
+ children: [
776
+ /* @__PURE__ */ jsx("span", {
777
+ className: "font-semibold text-slate-900",
778
+ children: "Raw status:"
779
+ }),
780
+ " ",
781
+ emailResult.receiptRawStatus
782
+ ]
783
+ }) : null,
784
+ emailResult.raw ? /* @__PURE__ */ jsx("p", {
785
+ className: "whitespace-pre-wrap text-sm text-slate-700",
786
+ children: emailResult.raw
787
+ }) : null
788
+ ]
789
+ }) : null]
790
+ }),
791
+ errorDetails ? /* @__PURE__ */ jsxs("p", {
792
+ className: "rounded border border-rose-200 bg-rose-50 px-3 py-2 text-xs text-rose-700",
793
+ children: [
794
+ /* @__PURE__ */ jsx("span", {
795
+ className: "font-semibold",
796
+ children: "Error details:"
797
+ }),
798
+ " ",
799
+ errorDetails
800
+ ]
801
+ }) : null
802
+ ]
803
+ });
804
+ }
805
+
806
+ //#endregion
807
+ //#region src/components/tool-result-agent-invocation-card.tsx
808
+ function formatAgentName(part) {
809
+ if (part.metadata?.agent_name) return part.metadata.agent_name;
810
+ return part.name.replace(/^invoke_/, "").replace(/[_-]+/g, " ").trim() || "Agent";
811
+ }
812
+ function formatOutput(value) {
813
+ if (typeof value === "string") return value;
814
+ if (value == null) return null;
815
+ try {
816
+ return JSON.stringify(value, null, 2);
817
+ } catch {
818
+ return null;
819
+ }
820
+ }
821
+ function getPrompt(value) {
822
+ if (!value || typeof value !== "object" || Array.isArray(value)) return null;
823
+ const prompt = value.input;
824
+ return typeof prompt === "string" && prompt.trim() ? prompt : null;
825
+ }
826
+ function ToolResultAgentInvocationCard({ part, className, onOpenThread }) {
827
+ const metadata = part.metadata;
828
+ const agentName = formatAgentName(part);
829
+ const output$1 = formatOutput(part.result);
830
+ const prompt = getPrompt(part.input);
831
+ const error = formatToolErrorDetails(part.errorDetails);
832
+ const threadId = metadata?.thread_id;
833
+ const failed = part.state === "error";
834
+ const pending = part.state === "pending";
835
+ return /* @__PURE__ */ jsxs(Sheet, { children: [/* @__PURE__ */ jsx(SheetTrigger, {
836
+ "aria-label": `Agent: ${agentName}`,
837
+ className: joinClasses("w-full rounded-lg border border-neutral-200 bg-white text-left font-normal text-neutral-950 transition-colors hover:bg-neutral-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950", className),
838
+ children: /* @__PURE__ */ jsxs("span", {
839
+ className: "flex items-center gap-2 px-3 py-2 text-sm",
840
+ children: [
841
+ /* @__PURE__ */ jsx(Bot, {
842
+ className: "h-4 w-4 shrink-0 text-neutral-500",
843
+ "aria-hidden": "true"
844
+ }),
845
+ /* @__PURE__ */ jsx("span", {
846
+ className: "shrink-0 font-medium",
847
+ children: "Agent"
848
+ }),
849
+ /* @__PURE__ */ jsx("span", {
850
+ className: "min-w-0 truncate text-neutral-600",
851
+ children: agentName
852
+ }),
853
+ /* @__PURE__ */ jsx("span", {
854
+ className: "ml-auto shrink-0",
855
+ children: pending ? /* @__PURE__ */ jsx(LoaderCircle, {
856
+ className: "h-4 w-4 animate-spin text-amber-600",
857
+ "aria-label": "Pending"
858
+ }) : failed ? /* @__PURE__ */ jsx(XCircle, {
859
+ className: "h-4 w-4 text-rose-600",
860
+ "aria-hidden": "true"
861
+ }) : /* @__PURE__ */ jsx(CheckCircle2, {
862
+ className: "h-4 w-4 text-emerald-600",
863
+ "aria-hidden": "true"
864
+ })
865
+ })
866
+ ]
867
+ })
868
+ }), /* @__PURE__ */ jsxs(SheetContent, {
869
+ closeLabel: "Close",
870
+ children: [/* @__PURE__ */ jsx(SheetHeader, {
871
+ className: "pr-10",
872
+ children: /* @__PURE__ */ jsx(SheetTitle, { children: agentName })
873
+ }), /* @__PURE__ */ jsxs("section", {
874
+ className: "mt-6 space-y-4",
875
+ "aria-label": "Agent invocation result",
876
+ children: [
877
+ /* @__PURE__ */ jsxs("dl", {
878
+ className: "grid gap-2 text-sm",
879
+ children: [
880
+ /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("dt", {
881
+ className: "text-xs text-neutral-500",
882
+ children: "Status"
883
+ }), /* @__PURE__ */ jsx("dd", { children: part.state })] }),
884
+ metadata?.agent_id ? /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("dt", {
885
+ className: "text-xs text-neutral-500",
886
+ children: "Agent ID"
887
+ }), /* @__PURE__ */ jsx("dd", {
888
+ className: "break-all",
889
+ children: metadata.agent_id
890
+ })] }) : null,
891
+ metadata?.tracked_execution_id ? /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("dt", {
892
+ className: "text-xs text-neutral-500",
893
+ children: "Execution ID"
894
+ }), /* @__PURE__ */ jsx("dd", {
895
+ className: "break-all",
896
+ children: metadata.tracked_execution_id
897
+ })] }) : null,
898
+ threadId ? /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("dt", {
899
+ className: "text-xs text-neutral-500",
900
+ children: "Thread ID"
901
+ }), /* @__PURE__ */ jsx("dd", {
902
+ className: "break-all",
903
+ children: threadId
904
+ })] }) : null
905
+ ]
906
+ }),
907
+ prompt ? /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("h3", {
908
+ className: "mb-2 text-sm font-medium",
909
+ children: "Prompt"
910
+ }), /* @__PURE__ */ jsx("p", {
911
+ className: "whitespace-pre-wrap rounded-md border border-neutral-200 bg-neutral-50 p-3 text-sm leading-6",
912
+ children: prompt
913
+ })] }) : null,
914
+ output$1 ? /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("h3", {
915
+ className: "mb-2 text-sm font-medium",
916
+ children: "Result"
917
+ }), /* @__PURE__ */ jsx("pre", {
918
+ className: "overflow-x-auto whitespace-pre-wrap rounded-md border border-neutral-200 bg-neutral-50 p-3 text-xs leading-5",
919
+ children: output$1
920
+ })] }) : null,
921
+ threadId && onOpenThread ? /* @__PURE__ */ jsxs("button", {
922
+ type: "button",
923
+ className: "inline-flex items-center gap-2 rounded-md border border-neutral-300 px-3 py-2 text-sm font-medium text-neutral-900 transition-colors hover:bg-neutral-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950",
924
+ onClick: () => onOpenThread(threadId),
925
+ children: [/* @__PURE__ */ jsx(MessageSquare, {
926
+ className: "h-4 w-4",
927
+ "aria-hidden": "true"
928
+ }), "Open conversation"]
929
+ }) : null,
930
+ error ? /* @__PURE__ */ jsxs("p", {
931
+ className: "rounded border border-rose-200 bg-rose-50 px-3 py-2 text-xs text-rose-700",
932
+ children: [
933
+ /* @__PURE__ */ jsx("span", {
934
+ className: "font-semibold",
935
+ children: "Error details:"
936
+ }),
937
+ " ",
938
+ error
939
+ ]
940
+ }) : null
941
+ ]
942
+ })]
943
+ })] });
944
+ }
945
+
946
+ //#endregion
947
+ //#region src/components/tool-result-browser-card.tsx
948
+ const SCRAPER_LABELS = {
949
+ beautifulsoup: "BeautifulSoup",
950
+ firecrawl: "Firecrawl",
951
+ scraperapi: "ScraperAPI"
952
+ };
953
+ function getUrl(input) {
954
+ if (!input || typeof input !== "object" || Array.isArray(input)) return null;
955
+ const url = input.url;
956
+ return typeof url === "string" && url.trim() ? url : null;
957
+ }
958
+ function toOutput(value) {
959
+ if (typeof value === "string") return value;
960
+ if (value == null) return null;
961
+ try {
962
+ return JSON.stringify(value, null, 2);
963
+ } catch {
964
+ return null;
965
+ }
966
+ }
967
+ function getPreview(output$1) {
968
+ if (!output$1) return null;
969
+ return output$1.length > 200 ? `${output$1.slice(0, 200)}…` : output$1;
970
+ }
971
+ function ToolResultBrowserCard({ part, className }) {
972
+ const [expanded, setExpanded] = useState(false);
973
+ const metadata = part.metadata;
974
+ const preview = getPreview(toOutput(part.result));
975
+ const url = getUrl(part.input);
976
+ const error = formatToolErrorDetails(part.errorDetails);
977
+ const scraperLabel = metadata?.scraper_type ? SCRAPER_LABELS[metadata.scraper_type] : null;
978
+ const credits = metadata?.scraper_api_tokens ?? metadata?.firecrawl_credits;
979
+ const subtitle = [scraperLabel, typeof credits === "number" ? `${credits} credit${credits === 1 ? "" : "s"}` : null].filter(Boolean).join(" · ");
980
+ const failed = part.state === "error";
981
+ const pending = part.state === "pending";
982
+ const canExpand = Boolean(preview || error || url);
983
+ return /* @__PURE__ */ jsxs("section", {
984
+ className: joinClasses("overflow-hidden rounded-lg border border-neutral-200 bg-white", className),
985
+ "aria-label": "Browser result",
986
+ children: [/* @__PURE__ */ jsxs("div", {
987
+ className: "flex items-center gap-2 px-3 py-2 text-sm",
988
+ children: [
989
+ /* @__PURE__ */ jsx(Globe, {
990
+ className: "h-4 w-4 shrink-0 text-neutral-500",
991
+ "aria-hidden": "true"
992
+ }),
993
+ /* @__PURE__ */ jsx("span", {
994
+ className: "font-medium text-neutral-800",
995
+ children: "Browser"
996
+ }),
997
+ subtitle ? /* @__PURE__ */ jsx("span", {
998
+ className: "min-w-0 truncate text-xs text-neutral-500",
999
+ children: subtitle
1000
+ }) : null,
1001
+ /* @__PURE__ */ jsxs("span", {
1002
+ className: "ml-auto flex shrink-0 items-center gap-2",
1003
+ children: [pending ? /* @__PURE__ */ jsx(LoaderCircle, {
1004
+ className: "h-4 w-4 animate-spin text-amber-600",
1005
+ "aria-label": "Pending"
1006
+ }) : failed ? /* @__PURE__ */ jsx(XCircle, {
1007
+ className: "h-4 w-4 text-rose-600",
1008
+ "aria-hidden": "true"
1009
+ }) : /* @__PURE__ */ jsx(CheckCircle2, {
1010
+ className: "h-4 w-4 text-emerald-600",
1011
+ "aria-hidden": "true"
1012
+ }), canExpand ? /* @__PURE__ */ jsx("button", {
1013
+ type: "button",
1014
+ "aria-label": expanded ? "Collapse browser result" : "Expand browser result",
1015
+ "aria-expanded": expanded,
1016
+ className: "flex h-5 w-5 items-center justify-center rounded text-neutral-400 transition-colors hover:bg-neutral-100 hover:text-neutral-600 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950",
1017
+ onClick: () => setExpanded((value) => !value),
1018
+ children: expanded ? /* @__PURE__ */ jsx(Minus, {
1019
+ className: "h-3 w-3",
1020
+ "aria-hidden": "true"
1021
+ }) : /* @__PURE__ */ jsx(Plus, {
1022
+ className: "h-3 w-3",
1023
+ "aria-hidden": "true"
1024
+ })
1025
+ }) : null]
1026
+ })
1027
+ ]
1028
+ }), expanded ? /* @__PURE__ */ jsxs("div", {
1029
+ className: "space-y-3 border-t border-neutral-200 bg-neutral-50 p-3",
1030
+ children: [
1031
+ url ? /* @__PURE__ */ jsx("a", {
1032
+ href: url,
1033
+ target: "_blank",
1034
+ rel: "noreferrer noopener",
1035
+ className: "block truncate text-xs text-blue-700 underline underline-offset-2 hover:text-blue-900",
1036
+ children: url
1037
+ }) : null,
1038
+ preview ? /* @__PURE__ */ jsx("p", {
1039
+ className: "whitespace-pre-wrap text-xs leading-5 text-neutral-600",
1040
+ children: preview
1041
+ }) : null,
1042
+ error ? /* @__PURE__ */ jsxs("p", {
1043
+ className: "text-xs text-rose-700",
1044
+ children: [
1045
+ /* @__PURE__ */ jsx("span", {
1046
+ className: "font-semibold",
1047
+ children: "Error details:"
1048
+ }),
1049
+ " ",
1050
+ error
1051
+ ]
1052
+ }) : null
1053
+ ]
1054
+ }) : null]
1055
+ });
1056
+ }
1057
+
1058
+ //#endregion
1059
+ //#region src/components/tool-result-external-datasource-card.tsx
1060
+ function isRecord(value) {
1061
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
1062
+ }
1063
+ function toStringOrNull$1(value) {
1064
+ return typeof value === "string" && value.trim().length > 0 ? value : null;
1065
+ }
1066
+ function formatCell(value) {
1067
+ if (value === null || value === void 0) return "-";
1068
+ if (typeof value === "string") return value;
1069
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
1070
+ try {
1071
+ return JSON.stringify(value);
1072
+ } catch {
1073
+ return String(value);
1074
+ }
1075
+ }
1076
+ function parseJson(value) {
1077
+ try {
1078
+ return JSON.parse(value);
1079
+ } catch {
1080
+ return null;
1081
+ }
1082
+ }
1083
+ function normalizeTable(value) {
1084
+ const raw = typeof value === "string" ? parseJson(value) : value;
1085
+ if (!raw) return {
1086
+ columns: [],
1087
+ rows: []
1088
+ };
1089
+ if (Array.isArray(raw)) {
1090
+ const records = raw.filter(isRecord);
1091
+ const columns$1 = Array.from(new Set(records.flatMap((record) => Object.keys(record))));
1092
+ return {
1093
+ columns: columns$1,
1094
+ rows: records.map((record) => columns$1.map((column) => record[column]))
1095
+ };
1096
+ }
1097
+ if (!isRecord(raw)) return {
1098
+ columns: [],
1099
+ rows: []
1100
+ };
1101
+ if (Array.isArray(raw.columns) && Array.isArray(raw.rows)) return {
1102
+ columns: raw.columns.filter((column) => typeof column === "string"),
1103
+ rows: raw.rows.map((row) => Array.isArray(row) ? row : [row])
1104
+ };
1105
+ const columns = Object.keys(raw);
1106
+ if (columns.length === 0) return {
1107
+ columns: [],
1108
+ rows: []
1109
+ };
1110
+ const columnValues = columns.map((column) => raw[column]);
1111
+ if (!columnValues.some(isRecord)) return {
1112
+ columns: ["Property", "Value"],
1113
+ rows: Object.entries(raw)
1114
+ };
1115
+ return {
1116
+ columns,
1117
+ rows: Array.from(new Set(columnValues.flatMap((column) => isRecord(column) ? Object.keys(column) : []))).map((rowKey) => columnValues.map((column) => isRecord(column) ? column[rowKey] : null))
1118
+ };
1119
+ }
1120
+ function getTableValue(part) {
1121
+ if (part.metadata?.json_table !== void 0) return part.metadata.json_table;
1122
+ return typeof part.result === "string" ? part.result : null;
1123
+ }
1124
+ function formatStatus(state) {
1125
+ if (state === "pending") return "pending";
1126
+ if (state === "error") return "error";
1127
+ return "completed";
1128
+ }
1129
+ function ToolResultExternalDatasourceCard({ part, className }) {
1130
+ const sqlQuery = toStringOrNull$1(part.metadata?.sql_query);
1131
+ const table = normalizeTable(getTableValue(part));
1132
+ const errorDetails = formatToolErrorDetails(part.errorDetails);
1133
+ const statusClass = part.state === "error" ? "text-rose-600" : part.state === "pending" ? "text-amber-600" : "text-emerald-600";
1134
+ return /* @__PURE__ */ jsxs(Sheet, { children: [/* @__PURE__ */ jsx(SheetTrigger, {
1135
+ "aria-label": `External datasource: ${formatStatus(part.state)}`,
1136
+ className: joinClasses("w-full overflow-hidden rounded-lg border border-neutral-200 bg-white text-left font-normal text-neutral-950 transition-colors hover:bg-neutral-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950", className),
1137
+ children: /* @__PURE__ */ jsxs("span", {
1138
+ className: "flex items-center gap-2 px-3 py-2 text-sm",
1139
+ children: [
1140
+ /* @__PURE__ */ jsx(Database, {
1141
+ className: "h-4 w-4 shrink-0 text-neutral-500",
1142
+ "aria-hidden": "true"
1143
+ }),
1144
+ /* @__PURE__ */ jsx("span", {
1145
+ className: "truncate font-medium",
1146
+ children: "External datasource"
1147
+ }),
1148
+ /* @__PURE__ */ jsx("span", {
1149
+ className: joinClasses("ml-auto shrink-0", statusClass),
1150
+ children: part.state === "error" ? /* @__PURE__ */ jsx(XCircle, {
1151
+ className: "h-4 w-4",
1152
+ "aria-hidden": "true"
1153
+ }) : part.state === "completed" ? /* @__PURE__ */ jsx(CheckCircle2, {
1154
+ className: "h-4 w-4",
1155
+ "aria-hidden": "true"
1156
+ }) : /* @__PURE__ */ jsx("span", {
1157
+ className: "text-xs",
1158
+ children: "pending"
1159
+ })
1160
+ })
1161
+ ]
1162
+ })
1163
+ }), /* @__PURE__ */ jsxs(SheetContent, {
1164
+ closeLabel: "Close",
1165
+ children: [/* @__PURE__ */ jsx(SheetHeader, {
1166
+ className: "pr-10",
1167
+ children: /* @__PURE__ */ jsx(SheetTitle, { children: "External datasource" })
1168
+ }), /* @__PURE__ */ jsxs("section", {
1169
+ className: "mt-6 space-y-8",
1170
+ "aria-label": "External datasource result",
1171
+ children: [
1172
+ sqlQuery ? /* @__PURE__ */ jsxs("div", {
1173
+ className: "space-y-2",
1174
+ children: [/* @__PURE__ */ jsxs("div", {
1175
+ className: "flex items-center gap-2",
1176
+ children: [/* @__PURE__ */ jsx(Database, {
1177
+ className: "h-5 w-5 text-blue-500",
1178
+ "aria-hidden": "true"
1179
+ }), /* @__PURE__ */ jsx("h3", {
1180
+ className: "text-sm font-medium",
1181
+ children: "SQL query"
1182
+ })]
1183
+ }), /* @__PURE__ */ jsx("pre", {
1184
+ className: "overflow-x-auto rounded-md border border-neutral-200 bg-neutral-50 p-3 text-xs leading-5 text-neutral-800",
1185
+ children: sqlQuery
1186
+ })]
1187
+ }) : null,
1188
+ table.columns.length > 0 && table.rows.length > 0 ? /* @__PURE__ */ jsxs("div", {
1189
+ className: "space-y-2",
1190
+ children: [/* @__PURE__ */ jsx("h3", {
1191
+ className: "text-sm font-medium",
1192
+ children: "Query results"
1193
+ }), /* @__PURE__ */ jsx("div", {
1194
+ className: "overflow-x-auto rounded-md border border-neutral-200",
1195
+ children: /* @__PURE__ */ jsxs("table", {
1196
+ className: "w-full border-collapse text-left text-xs",
1197
+ children: [/* @__PURE__ */ jsx("thead", {
1198
+ className: "bg-neutral-50 text-neutral-600",
1199
+ children: /* @__PURE__ */ jsx("tr", { children: table.columns.map((column) => /* @__PURE__ */ jsx("th", {
1200
+ className: "max-w-56 truncate border-b border-neutral-200 px-3 py-2 font-medium",
1201
+ title: column,
1202
+ children: column
1203
+ }, column)) })
1204
+ }), /* @__PURE__ */ jsx("tbody", { children: table.rows.map((row) => /* @__PURE__ */ jsx("tr", {
1205
+ className: "border-b border-neutral-100 last:border-0",
1206
+ children: table.columns.map((column, columnIndex) => {
1207
+ const value = formatCell(row[columnIndex]);
1208
+ return /* @__PURE__ */ jsx("td", {
1209
+ className: "max-w-56 truncate px-3 py-2 text-neutral-700",
1210
+ title: value,
1211
+ children: value
1212
+ }, column);
1213
+ })
1214
+ }, row.map(formatCell).join("\0"))) })]
1215
+ })
1216
+ })]
1217
+ }) : part.state === "completed" && !errorDetails ? /* @__PURE__ */ jsx("p", {
1218
+ className: "text-sm text-neutral-600",
1219
+ children: "No data to display."
1220
+ }) : null,
1221
+ errorDetails ? /* @__PURE__ */ jsxs("p", {
1222
+ className: "rounded border border-rose-200 bg-rose-50 px-3 py-2 text-xs text-rose-700",
1223
+ children: [
1224
+ /* @__PURE__ */ jsx("span", {
1225
+ className: "font-semibold",
1226
+ children: "Error details:"
1227
+ }),
1228
+ " ",
1229
+ errorDetails
1230
+ ]
1231
+ }) : null
1232
+ ]
1233
+ })]
1234
+ })] });
1235
+ }
1236
+
1237
+ //#endregion
1238
+ //#region src/components/tool-result-http-request-card.tsx
1239
+ function output(value) {
1240
+ if (typeof value === "string") return value;
1241
+ if (value === void 0 || value === null) return null;
1242
+ try {
1243
+ return JSON.stringify(value, null, 2);
1244
+ } catch {
1245
+ return null;
1246
+ }
1247
+ }
1248
+ function ToolResultHttpRequestCard({ part, className }) {
1249
+ const metadata = part.metadata;
1250
+ const statusCode = metadata?.status_code;
1251
+ const failed = part.state === "error" || typeof statusCode === "number" && statusCode >= 400;
1252
+ const response = output(part.result);
1253
+ const errorDetails = formatToolErrorDetails(part.errorDetails);
1254
+ const status = statusCode ? `${statusCode}${metadata?.status_reason ? ` ${metadata.status_reason}` : ""}` : part.state;
1255
+ return /* @__PURE__ */ jsxs(Sheet, { children: [/* @__PURE__ */ jsx(SheetTrigger, {
1256
+ "aria-label": `HTTP request: ${status}`,
1257
+ className: joinClasses("w-full rounded-lg border border-neutral-200 bg-white text-left font-normal text-neutral-950 transition-colors hover:bg-neutral-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950", className),
1258
+ children: /* @__PURE__ */ jsxs("span", {
1259
+ className: "flex items-center gap-2 px-3 py-2 text-sm",
1260
+ children: [
1261
+ /* @__PURE__ */ jsx(Globe, {
1262
+ className: "h-4 w-4 shrink-0 text-neutral-500",
1263
+ "aria-hidden": "true"
1264
+ }),
1265
+ /* @__PURE__ */ jsx("span", {
1266
+ className: "shrink-0 font-medium",
1267
+ children: metadata?.method ?? "HTTP"
1268
+ }),
1269
+ /* @__PURE__ */ jsx("span", {
1270
+ className: "min-w-0 truncate text-neutral-600",
1271
+ children: metadata?.url ?? part.name
1272
+ }),
1273
+ /* @__PURE__ */ jsx("span", {
1274
+ className: joinClasses("ml-auto shrink-0", failed ? "text-rose-600" : "text-emerald-600"),
1275
+ children: failed ? /* @__PURE__ */ jsx(XCircle, {
1276
+ className: "h-4 w-4",
1277
+ "aria-hidden": "true"
1278
+ }) : /* @__PURE__ */ jsx(CheckCircle2, {
1279
+ className: "h-4 w-4",
1280
+ "aria-hidden": "true"
1281
+ })
1282
+ })
1283
+ ]
1284
+ })
1285
+ }), /* @__PURE__ */ jsxs(SheetContent, {
1286
+ closeLabel: "Close",
1287
+ children: [/* @__PURE__ */ jsx(SheetHeader, {
1288
+ className: "pr-10",
1289
+ children: /* @__PURE__ */ jsx(SheetTitle, { children: "HTTP request" })
1290
+ }), /* @__PURE__ */ jsxs("section", {
1291
+ className: "mt-6 space-y-5",
1292
+ "aria-label": "HTTP response",
1293
+ children: [
1294
+ /* @__PURE__ */ jsxs("dl", {
1295
+ className: "grid gap-2 text-sm",
1296
+ children: [
1297
+ /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("dt", {
1298
+ className: "text-xs text-neutral-500",
1299
+ children: "Status"
1300
+ }), /* @__PURE__ */ jsx("dd", { children: status })] }),
1301
+ metadata?.url ? /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("dt", {
1302
+ className: "text-xs text-neutral-500",
1303
+ children: "Request URL"
1304
+ }), /* @__PURE__ */ jsx("dd", {
1305
+ className: "break-all",
1306
+ children: metadata.url
1307
+ })] }) : null,
1308
+ metadata?.response_url && metadata.response_url !== metadata.url ? /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("dt", {
1309
+ className: "text-xs text-neutral-500",
1310
+ children: "Response URL"
1311
+ }), /* @__PURE__ */ jsx("dd", {
1312
+ className: "break-all",
1313
+ children: metadata.response_url
1314
+ })] }) : null,
1315
+ metadata?.content_type ? /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("dt", {
1316
+ className: "text-xs text-neutral-500",
1317
+ children: "Content type"
1318
+ }), /* @__PURE__ */ jsx("dd", { children: metadata.content_type })] }) : null
1319
+ ]
1320
+ }),
1321
+ response ? /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("h3", {
1322
+ className: "mb-2 text-sm font-medium",
1323
+ children: "Response"
1324
+ }), /* @__PURE__ */ jsx("pre", {
1325
+ className: "overflow-x-auto rounded-md border border-neutral-200 bg-neutral-50 p-3 text-xs leading-5",
1326
+ children: response
1327
+ })] }) : null,
1328
+ metadata?.response_headers && Object.keys(metadata.response_headers).length ? /* @__PURE__ */ jsxs("details", { children: [/* @__PURE__ */ jsx("summary", {
1329
+ className: "cursor-pointer text-sm font-medium",
1330
+ children: "Response headers"
1331
+ }), /* @__PURE__ */ jsx("pre", {
1332
+ className: "mt-2 overflow-x-auto rounded-md border border-neutral-200 bg-neutral-50 p-3 text-xs",
1333
+ children: JSON.stringify(metadata.response_headers, null, 2)
1334
+ })] }) : null,
1335
+ errorDetails ? /* @__PURE__ */ jsxs("p", {
1336
+ className: "rounded border border-rose-200 bg-rose-50 px-3 py-2 text-xs text-rose-700",
1337
+ children: [
1338
+ /* @__PURE__ */ jsx("span", {
1339
+ className: "font-semibold",
1340
+ children: "Error details:"
1341
+ }),
1342
+ " ",
1343
+ errorDetails
1344
+ ]
1345
+ }) : null
1346
+ ]
1347
+ })]
1348
+ })] });
1349
+ }
1350
+
1351
+ //#endregion
1352
+ //#region src/components/tool-result-mcp-card.tsx
1353
+ function text(value) {
1354
+ if (typeof value === "string") return value;
1355
+ if (value == null) return null;
1356
+ try {
1357
+ return JSON.stringify(value, null, 2);
1358
+ } catch {
1359
+ return null;
1360
+ }
1361
+ }
1362
+ function ToolResultMcpCard({ part, className }) {
1363
+ const meta = part.metadata;
1364
+ const server = meta?.mcp_server_url ?? meta?.server_name;
1365
+ const tool = meta?.mcp_tool_name ?? meta?.tool_name ?? part.name;
1366
+ const result = text(part.result);
1367
+ const error = formatToolErrorDetails(part.errorDetails);
1368
+ const failed = part.state === "error";
1369
+ return /* @__PURE__ */ jsxs(Sheet, { children: [/* @__PURE__ */ jsx(SheetTrigger, {
1370
+ "aria-label": `MCP tool: ${tool}`,
1371
+ className: joinClasses("w-full rounded-lg border border-neutral-200 bg-white text-left font-normal text-neutral-950 transition-colors hover:bg-neutral-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950", className),
1372
+ children: /* @__PURE__ */ jsxs("span", {
1373
+ className: "flex items-center gap-2 px-3 py-2 text-sm",
1374
+ children: [
1375
+ /* @__PURE__ */ jsx(Plug, {
1376
+ className: "h-4 w-4 shrink-0 text-neutral-500",
1377
+ "aria-hidden": "true"
1378
+ }),
1379
+ /* @__PURE__ */ jsx("span", {
1380
+ className: "truncate font-medium",
1381
+ children: "MCP"
1382
+ }),
1383
+ /* @__PURE__ */ jsx("span", {
1384
+ className: "min-w-0 truncate text-neutral-600",
1385
+ children: tool
1386
+ }),
1387
+ /* @__PURE__ */ jsx("span", {
1388
+ className: joinClasses("ml-auto shrink-0", failed ? "text-rose-600" : "text-emerald-600"),
1389
+ children: failed ? /* @__PURE__ */ jsx(XCircle, {
1390
+ className: "h-4 w-4",
1391
+ "aria-hidden": "true"
1392
+ }) : /* @__PURE__ */ jsx(CheckCircle2, {
1393
+ className: "h-4 w-4",
1394
+ "aria-hidden": "true"
1395
+ })
1396
+ })
1397
+ ]
1398
+ })
1399
+ }), /* @__PURE__ */ jsxs(SheetContent, {
1400
+ closeLabel: "Close",
1401
+ children: [/* @__PURE__ */ jsx(SheetHeader, {
1402
+ className: "pr-10",
1403
+ children: /* @__PURE__ */ jsx(SheetTitle, { children: "MCP tool" })
1404
+ }), /* @__PURE__ */ jsxs("section", {
1405
+ className: "mt-6 space-y-4",
1406
+ "aria-label": "MCP tool result",
1407
+ children: [
1408
+ /* @__PURE__ */ jsxs("dl", {
1409
+ className: "grid gap-2 text-sm",
1410
+ children: [/* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("dt", {
1411
+ className: "text-xs text-neutral-500",
1412
+ children: "Tool"
1413
+ }), /* @__PURE__ */ jsx("dd", { children: tool })] }), server ? /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("dt", {
1414
+ className: "text-xs text-neutral-500",
1415
+ children: "Server"
1416
+ }), /* @__PURE__ */ jsx("dd", {
1417
+ className: "break-all",
1418
+ children: server
1419
+ })] }) : null]
1420
+ }),
1421
+ result ? /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("h3", {
1422
+ className: "mb-2 text-sm font-medium",
1423
+ children: "Result"
1424
+ }), /* @__PURE__ */ jsx("pre", {
1425
+ className: "overflow-x-auto rounded-md border border-neutral-200 bg-neutral-50 p-3 text-xs leading-5",
1426
+ children: result
1427
+ })] }) : null,
1428
+ error ? /* @__PURE__ */ jsxs("p", {
1429
+ className: "rounded border border-rose-200 bg-rose-50 px-3 py-2 text-xs text-rose-700",
1430
+ children: [
1431
+ /* @__PURE__ */ jsx("span", {
1432
+ className: "font-semibold",
1433
+ children: "Error details:"
1434
+ }),
1435
+ " ",
1436
+ error
1437
+ ]
1438
+ }) : null
1439
+ ]
1440
+ })]
1441
+ })] });
1442
+ }
1443
+
1444
+ //#endregion
1445
+ //#region src/components/tool-result-office-documents-card.tsx
1446
+ function getFiles(part) {
1447
+ const files = part.metadata?.media_files;
1448
+ return Array.isArray(files) ? files.filter((file) => Boolean(file) && typeof file === "object" && typeof file.id === "string") : [];
1449
+ }
1450
+ function getExtension(file) {
1451
+ const name = file.name ?? file.pathname ?? "";
1452
+ const extension = name.split(".").pop()?.toLowerCase();
1453
+ if (extension && extension !== name.toLowerCase()) return extension;
1454
+ const mime = file.content_type ?? "";
1455
+ if (mime.includes("pdf")) return "pdf";
1456
+ if (mime.includes("wordprocessingml")) return "docx";
1457
+ if (mime.includes("spreadsheetml")) return "xlsx";
1458
+ if (mime.includes("presentationml")) return "pptx";
1459
+ return null;
1460
+ }
1461
+ function iconClass(extension) {
1462
+ if (extension === "pdf") return "text-rose-500";
1463
+ if (["doc", "docx"].includes(extension ?? "")) return "text-blue-600";
1464
+ if ([
1465
+ "xls",
1466
+ "xlsx",
1467
+ "csv"
1468
+ ].includes(extension ?? "")) return "text-emerald-600";
1469
+ if (["ppt", "pptx"].includes(extension ?? "")) return "text-orange-600";
1470
+ return "text-neutral-500";
1471
+ }
1472
+ function ToolResultOfficeDocumentsCard({ part, className }) {
1473
+ const files = getFiles(part);
1474
+ const errorDetails = formatToolErrorDetails(part.errorDetails);
1475
+ const fileLabel = files.length === 1 ? "file" : "files";
1476
+ const statusClass = part.state === "error" ? "text-rose-600" : part.state === "pending" ? "text-amber-600" : "text-emerald-600";
1477
+ return /* @__PURE__ */ jsxs(Sheet, { children: [/* @__PURE__ */ jsx(SheetTrigger, {
1478
+ "aria-label": `Office documents: ${files.length} ${fileLabel}`,
1479
+ className: joinClasses("w-full overflow-hidden rounded-lg border border-neutral-200 bg-white text-left font-normal text-neutral-950 transition-colors hover:bg-neutral-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950", className),
1480
+ children: /* @__PURE__ */ jsxs("span", {
1481
+ className: "flex items-center gap-2 px-3 py-2 text-sm",
1482
+ children: [
1483
+ /* @__PURE__ */ jsx(FileText, {
1484
+ className: "h-4 w-4 shrink-0 text-neutral-500",
1485
+ "aria-hidden": "true"
1486
+ }),
1487
+ /* @__PURE__ */ jsx("span", {
1488
+ className: "truncate font-medium",
1489
+ children: "Office documents"
1490
+ }),
1491
+ /* @__PURE__ */ jsx("span", {
1492
+ className: "truncate text-xs text-neutral-500",
1493
+ children: part.state === "pending" ? "pending" : `${files.length} ${fileLabel}`
1494
+ }),
1495
+ /* @__PURE__ */ jsx("span", {
1496
+ className: joinClasses("ml-auto shrink-0", statusClass),
1497
+ children: part.state === "error" ? /* @__PURE__ */ jsx(XCircle, {
1498
+ className: "h-4 w-4",
1499
+ "aria-hidden": "true"
1500
+ }) : part.state === "completed" ? /* @__PURE__ */ jsx(CheckCircle2, {
1501
+ className: "h-4 w-4",
1502
+ "aria-hidden": "true"
1503
+ }) : null
1504
+ })
1505
+ ]
1506
+ })
1507
+ }), /* @__PURE__ */ jsxs(SheetContent, {
1508
+ closeLabel: "Close",
1509
+ children: [/* @__PURE__ */ jsx(SheetHeader, {
1510
+ className: "pr-10",
1511
+ children: /* @__PURE__ */ jsx(SheetTitle, { children: "Office documents" })
1512
+ }), /* @__PURE__ */ jsxs("section", {
1513
+ className: "mt-6 space-y-3",
1514
+ "aria-label": "Generated files",
1515
+ children: [
1516
+ files.map((file) => {
1517
+ const extension = getExtension(file);
1518
+ const label = file.name ?? file.pathname ?? file.id;
1519
+ const content = /* @__PURE__ */ jsxs("div", {
1520
+ className: "flex items-center gap-3 rounded-lg border border-neutral-200 bg-white p-3",
1521
+ children: [/* @__PURE__ */ jsx(File, {
1522
+ className: joinClasses("h-8 w-8 shrink-0", iconClass(extension)),
1523
+ "aria-hidden": "true"
1524
+ }), /* @__PURE__ */ jsxs("div", {
1525
+ className: "min-w-0",
1526
+ children: [/* @__PURE__ */ jsx("p", {
1527
+ className: "truncate text-sm font-medium",
1528
+ children: label
1529
+ }), extension ? /* @__PURE__ */ jsx("p", {
1530
+ className: "text-[11px] uppercase text-neutral-500",
1531
+ children: extension
1532
+ }) : null]
1533
+ })]
1534
+ }, file.id);
1535
+ return file.url ? /* @__PURE__ */ jsx("a", {
1536
+ href: file.url,
1537
+ target: "_blank",
1538
+ rel: "noreferrer noopener",
1539
+ className: "block hover:bg-neutral-50",
1540
+ children: content
1541
+ }, file.id) : /* @__PURE__ */ jsx("div", { children: content }, file.id);
1542
+ }),
1543
+ files.length === 0 && part.state === "pending" ? /* @__PURE__ */ jsx("p", {
1544
+ className: "text-sm text-neutral-600",
1545
+ children: "Creating documents…"
1546
+ }) : null,
1547
+ files.length === 0 && part.state === "completed" && !errorDetails ? /* @__PURE__ */ jsx("p", {
1548
+ className: "text-sm text-neutral-600",
1549
+ children: "No files were generated."
1550
+ }) : null,
1551
+ errorDetails ? /* @__PURE__ */ jsxs("p", {
1552
+ className: "rounded border border-rose-200 bg-rose-50 px-3 py-2 text-xs text-rose-700",
1553
+ children: [
1554
+ /* @__PURE__ */ jsx("span", {
1555
+ className: "font-semibold",
1556
+ children: "Error details:"
1557
+ }),
1558
+ " ",
1559
+ errorDetails
1560
+ ]
1561
+ }) : null
1562
+ ]
1563
+ })]
1564
+ })] });
1565
+ }
1566
+
1567
+ //#endregion
1568
+ //#region src/components/tool-result-web-search-card.tsx
1569
+ function isWebSearchResult(value) {
1570
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
1571
+ const candidate = value;
1572
+ return typeof candidate.title === "string" && typeof candidate.url === "string";
1573
+ }
1574
+ function toStringOrNull(value) {
1575
+ return typeof value === "string" && value.trim().length > 0 ? value : null;
1576
+ }
1577
+ function getHostname(url) {
1578
+ try {
1579
+ return new URL(url).hostname;
1580
+ } catch {
1581
+ return null;
1582
+ }
1583
+ }
1584
+ function getLegacyCitationResults(metadata) {
1585
+ if (!Array.isArray(metadata.citations)) return [];
1586
+ return metadata.citations.filter((citation) => typeof citation === "string").map((url) => ({
1587
+ title: getHostname(url) ?? url,
1588
+ url
1589
+ }));
1590
+ }
1591
+ function getSearchResults(part) {
1592
+ const searchResults = part.metadata?.search_results;
1593
+ if (Array.isArray(searchResults)) {
1594
+ const results = searchResults.filter(isWebSearchResult);
1595
+ if (results.length > 0) return results;
1596
+ }
1597
+ return getLegacyCitationResults(part.metadata ?? {});
1598
+ }
1599
+ function getFaviconUrl(url) {
1600
+ const hostname = getHostname(url);
1601
+ return hostname ? `https://www.google.com/s2/favicons?domain=${encodeURIComponent(hostname)}&sz=32` : null;
1602
+ }
1603
+ function WebSearchResultItem({ result }) {
1604
+ const hostname = getHostname(result.url);
1605
+ const source = toStringOrNull(result.source);
1606
+ const snippet = toStringOrNull(result.snippet);
1607
+ const date = toStringOrNull(result.date) ?? toStringOrNull(result.last_updated);
1608
+ const imageUrl = toStringOrNull(result.image_url);
1609
+ const faviconUrl = getFaviconUrl(result.url);
1610
+ return /* @__PURE__ */ jsx("article", {
1611
+ className: "rounded-lg border border-neutral-200 bg-white p-4",
1612
+ children: /* @__PURE__ */ jsxs("div", {
1613
+ className: "flex items-start gap-3",
1614
+ children: [/* @__PURE__ */ jsxs("span", {
1615
+ "aria-hidden": "true",
1616
+ className: "relative flex h-8 w-8 shrink-0 items-center justify-center overflow-hidden rounded-full border border-neutral-300 bg-white text-neutral-600",
1617
+ children: [/* @__PURE__ */ jsx(Globe, { className: "h-4 w-4" }), faviconUrl ? /* @__PURE__ */ jsx("img", {
1618
+ src: faviconUrl,
1619
+ alt: "",
1620
+ width: "16",
1621
+ height: "16",
1622
+ className: "absolute h-4 w-4 bg-white",
1623
+ onError: (event) => {
1624
+ event.currentTarget.style.display = "none";
1625
+ }
1626
+ }) : null]
1627
+ }), /* @__PURE__ */ jsxs("div", {
1628
+ className: "min-w-0 flex-1 space-y-1",
1629
+ children: [
1630
+ /* @__PURE__ */ jsx("a", {
1631
+ href: result.url,
1632
+ target: "_blank",
1633
+ rel: "noreferrer noopener",
1634
+ className: "block line-clamp-2 text-sm font-medium leading-5 text-neutral-950 hover:underline",
1635
+ children: result.title
1636
+ }),
1637
+ /* @__PURE__ */ jsxs("div", {
1638
+ className: "flex flex-wrap items-center gap-1.5 text-xs text-neutral-500",
1639
+ children: [
1640
+ date ? /* @__PURE__ */ jsx("span", { children: date }) : null,
1641
+ source ? /* @__PURE__ */ jsx("span", {
1642
+ className: "rounded-full bg-neutral-100 px-1.5 py-0.5 capitalize text-neutral-600",
1643
+ children: source
1644
+ }) : null,
1645
+ !date && !source && hostname ? /* @__PURE__ */ jsx("span", { children: hostname }) : null
1646
+ ]
1647
+ }),
1648
+ snippet ? /* @__PURE__ */ jsx("p", {
1649
+ className: "line-clamp-2 text-xs leading-5 text-neutral-600",
1650
+ children: snippet
1651
+ }) : null,
1652
+ imageUrl ? /* @__PURE__ */ jsx("img", {
1653
+ src: imageUrl,
1654
+ alt: "",
1655
+ className: "max-h-64 w-full rounded-md border border-neutral-200 object-contain",
1656
+ loading: "lazy"
1657
+ }) : null
1658
+ ]
1659
+ })]
1660
+ })
1661
+ });
1662
+ }
1663
+ function ToolResultWebSearchCard({ part, className }) {
1664
+ const searchResults = getSearchResults(part);
1665
+ const errorDetails = formatToolErrorDetails(part.errorDetails);
1666
+ const sourceLabel = searchResults.length === 1 ? "source" : "sources";
1667
+ return /* @__PURE__ */ jsxs(Sheet, { children: [/* @__PURE__ */ jsxs(SheetTrigger, {
1668
+ "aria-label": `Web: Internet search results, ${searchResults.length} ${sourceLabel}`,
1669
+ className: joinClasses("min-h-[183px] w-[186px] rounded-lg bg-neutral-100 p-4 text-left font-normal text-neutral-950 transition-colors hover:bg-neutral-200 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950", className),
1670
+ children: [
1671
+ /* @__PURE__ */ jsxs("header", {
1672
+ className: "flex items-center gap-3",
1673
+ children: [/* @__PURE__ */ jsx(Globe, {
1674
+ className: "h-5 w-5 shrink-0",
1675
+ "aria-hidden": "true"
1676
+ }), /* @__PURE__ */ jsx("p", {
1677
+ className: "text-base leading-5",
1678
+ children: "WEB"
1679
+ })]
1680
+ }),
1681
+ /* @__PURE__ */ jsx("h3", {
1682
+ className: "mt-3 line-clamp-2 text-sm font-normal leading-5",
1683
+ children: "Internet search results"
1684
+ }),
1685
+ /* @__PURE__ */ jsx("p", {
1686
+ className: "mt-2 text-xs leading-4 text-neutral-600",
1687
+ children: part.state === "pending" ? "Searching…" : `${searchResults.length} ${sourceLabel}`
1688
+ })
1689
+ ]
1690
+ }), /* @__PURE__ */ jsxs(SheetContent, {
1691
+ closeLabel: "Close",
1692
+ children: [/* @__PURE__ */ jsx(SheetHeader, {
1693
+ className: "pr-10",
1694
+ children: /* @__PURE__ */ jsx(SheetTitle, { children: "Internet search results" })
1695
+ }), /* @__PURE__ */ jsxs("section", {
1696
+ className: "mt-5",
1697
+ "aria-label": "Search results",
1698
+ children: [searchResults.length > 0 ? /* @__PURE__ */ jsx("div", {
1699
+ className: "space-y-3",
1700
+ children: searchResults.map((result, index) => /* @__PURE__ */ jsx(WebSearchResultItem, { result }, `${result.url}-${index}`))
1701
+ }) : part.state === "pending" ? /* @__PURE__ */ jsx("p", {
1702
+ className: "text-sm text-neutral-600",
1703
+ children: "Searching for sources…"
1704
+ }) : /* @__PURE__ */ jsx("p", {
1705
+ className: "text-sm text-neutral-600",
1706
+ children: "No sources returned."
1707
+ }), errorDetails ? /* @__PURE__ */ jsxs("p", {
1708
+ className: "mt-4 rounded border border-rose-200 bg-rose-50 px-3 py-2 text-xs text-rose-700",
1709
+ children: [
1710
+ /* @__PURE__ */ jsx("span", {
1711
+ className: "font-semibold",
1712
+ children: "Error details:"
1713
+ }),
1714
+ " ",
1715
+ errorDetails
1716
+ ]
1717
+ }) : null]
1718
+ })]
1719
+ })] });
1720
+ }
1721
+
1722
+ //#endregion
1723
+ //#region src/components/tool-result-workflow-dispatch-card.tsx
1724
+ function parseResult(value) {
1725
+ let parsed = value;
1726
+ if (typeof value === "string") try {
1727
+ parsed = JSON.parse(value);
1728
+ } catch {
1729
+ return {};
1730
+ }
1731
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return {};
1732
+ const result = parsed;
1733
+ return {
1734
+ executionId: typeof result.execution_id === "string" ? result.execution_id : void 0,
1735
+ workflowId: typeof result.workflow_id === "string" ? result.workflow_id : void 0,
1736
+ workflowName: typeof result.workflow_name_slug === "string" ? result.workflow_name_slug : void 0,
1737
+ status: typeof result.status === "string" ? result.status : void 0
1738
+ };
1739
+ }
1740
+ function formatResult(value) {
1741
+ if (typeof value === "string") return value;
1742
+ if (value == null) return null;
1743
+ try {
1744
+ return JSON.stringify(value, null, 2);
1745
+ } catch {
1746
+ return null;
1747
+ }
1748
+ }
1749
+ function ToolResultWorkflowDispatchCard({ part, className }) {
1750
+ const result = parseResult(part.result);
1751
+ const workflowName = result.workflowName ?? part.name;
1752
+ const status = result.status ?? part.state;
1753
+ const failed = part.state === "error";
1754
+ const output$1 = formatResult(part.result);
1755
+ const error = formatToolErrorDetails(part.errorDetails);
1756
+ return /* @__PURE__ */ jsxs(Sheet, { children: [/* @__PURE__ */ jsx(SheetTrigger, {
1757
+ "aria-label": `Workflow: ${workflowName}`,
1758
+ className: joinClasses("w-full rounded-lg border border-neutral-200 bg-white text-left font-normal text-neutral-950 transition-colors hover:bg-neutral-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950", className),
1759
+ children: /* @__PURE__ */ jsxs("span", {
1760
+ className: "flex items-center gap-2 px-3 py-2 text-sm",
1761
+ children: [
1762
+ /* @__PURE__ */ jsx(Workflow, {
1763
+ className: "h-4 w-4 shrink-0 text-neutral-500",
1764
+ "aria-hidden": "true"
1765
+ }),
1766
+ /* @__PURE__ */ jsx("span", {
1767
+ className: "shrink-0 font-medium",
1768
+ children: "Workflow"
1769
+ }),
1770
+ /* @__PURE__ */ jsx("span", {
1771
+ className: "min-w-0 truncate text-neutral-600",
1772
+ children: workflowName
1773
+ }),
1774
+ /* @__PURE__ */ jsxs("span", {
1775
+ className: "ml-auto flex shrink-0 items-center gap-2",
1776
+ children: [/* @__PURE__ */ jsx("span", {
1777
+ className: "text-xs text-neutral-500",
1778
+ children: status
1779
+ }), failed ? /* @__PURE__ */ jsx(XCircle, {
1780
+ className: "h-4 w-4 text-rose-600",
1781
+ "aria-hidden": "true"
1782
+ }) : /* @__PURE__ */ jsx(CheckCircle2, {
1783
+ className: "h-4 w-4 text-emerald-600",
1784
+ "aria-hidden": "true"
1785
+ })]
1786
+ })
1787
+ ]
1788
+ })
1789
+ }), /* @__PURE__ */ jsxs(SheetContent, {
1790
+ closeLabel: "Close",
1791
+ children: [/* @__PURE__ */ jsx(SheetHeader, {
1792
+ className: "pr-10",
1793
+ children: /* @__PURE__ */ jsx(SheetTitle, { children: "Workflow dispatch" })
1794
+ }), /* @__PURE__ */ jsxs("section", {
1795
+ className: "mt-6 space-y-4",
1796
+ "aria-label": "Workflow dispatch result",
1797
+ children: [
1798
+ /* @__PURE__ */ jsxs("dl", {
1799
+ className: "grid gap-2 text-sm",
1800
+ children: [
1801
+ /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("dt", {
1802
+ className: "text-xs text-neutral-500",
1803
+ children: "Workflow"
1804
+ }), /* @__PURE__ */ jsx("dd", { children: workflowName })] }),
1805
+ /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("dt", {
1806
+ className: "text-xs text-neutral-500",
1807
+ children: "Status"
1808
+ }), /* @__PURE__ */ jsx("dd", { children: status })] }),
1809
+ result.executionId ? /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("dt", {
1810
+ className: "text-xs text-neutral-500",
1811
+ children: "Execution ID"
1812
+ }), /* @__PURE__ */ jsx("dd", {
1813
+ className: "break-all",
1814
+ children: result.executionId
1815
+ })] }) : null,
1816
+ result.workflowId ? /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("dt", {
1817
+ className: "text-xs text-neutral-500",
1818
+ children: "Workflow ID"
1819
+ }), /* @__PURE__ */ jsx("dd", {
1820
+ className: "break-all",
1821
+ children: result.workflowId
1822
+ })] }) : null
1823
+ ]
1824
+ }),
1825
+ output$1 ? /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("h3", {
1826
+ className: "mb-2 text-sm font-medium",
1827
+ children: "Result"
1828
+ }), /* @__PURE__ */ jsx("pre", {
1829
+ className: "overflow-x-auto rounded-md border border-neutral-200 bg-neutral-50 p-3 text-xs leading-5",
1830
+ children: output$1
1831
+ })] }) : null,
1832
+ error ? /* @__PURE__ */ jsxs("p", {
1833
+ className: "rounded border border-rose-200 bg-rose-50 px-3 py-2 text-xs text-rose-700",
1834
+ children: [
1835
+ /* @__PURE__ */ jsx("span", {
1836
+ className: "font-semibold",
1837
+ children: "Error details:"
1838
+ }),
1839
+ " ",
1840
+ error
1841
+ ]
1842
+ }) : null
1843
+ ]
1844
+ })]
1845
+ })] });
1846
+ }
1847
+
1848
+ //#endregion
1849
+ //#region src/components/tool-result-card.tsx
1850
+ function formatJson(value) {
1851
+ return JSON.stringify(value, null, 2);
1852
+ }
1853
+ function getOfficeDocumentMediaFiles(part) {
1854
+ if (part.toolType !== "office_documents") return [];
1855
+ if (!part.metadata || typeof part.metadata !== "object") return [];
1856
+ const metadata = part.metadata;
1857
+ if (!Array.isArray(metadata.media_files)) return [];
1858
+ return metadata.media_files.filter((file) => Boolean(file) && typeof file === "object" && typeof file.id === "string");
1859
+ }
1860
+ function getDatasourceResources(part) {
1861
+ if (part.toolType !== "datasource" || !part.metadata) return [];
1862
+ const resources = part.metadata.resources;
1863
+ return Array.isArray(resources) ? resources.filter(isDatasourceResource) : [];
1864
+ }
1865
+ function ToolResultCard({ part, className, detailsOpen = true, onOpenAgentThread }) {
1866
+ const officeMediaFiles = getOfficeDocumentMediaFiles(part);
1867
+ const datasourceResources = getDatasourceResources(part);
1868
+ const errorDetails = formatToolErrorDetails(part.errorDetails);
1869
+ if (part.toolType === "email_send") return /* @__PURE__ */ jsx(ToolResultEmailSendCard, {
1870
+ part,
1871
+ className
1872
+ });
1873
+ if (part.toolType === "agent_invocation") return /* @__PURE__ */ jsx(ToolResultAgentInvocationCard, {
1874
+ part,
1875
+ className,
1876
+ onOpenThread: onOpenAgentThread
1877
+ });
1878
+ if (part.toolType === "browser") return /* @__PURE__ */ jsx(ToolResultBrowserCard, {
1879
+ part,
1880
+ className
1881
+ });
1882
+ if (part.toolType === "external_datasource") return /* @__PURE__ */ jsx(ToolResultExternalDatasourceCard, {
1883
+ part,
1884
+ className
1885
+ });
1886
+ if (part.toolType === "office_documents") return /* @__PURE__ */ jsx(ToolResultOfficeDocumentsCard, {
1887
+ part,
1888
+ className
1889
+ });
1890
+ if (part.toolType === "http_request") return /* @__PURE__ */ jsx(ToolResultHttpRequestCard, {
1891
+ part,
1892
+ className
1893
+ });
1894
+ if (part.toolType === "mcp_tool") return /* @__PURE__ */ jsx(ToolResultMcpCard, {
1895
+ part,
1896
+ className
1897
+ });
1898
+ if (part.toolType === "workflow_dispatch") return /* @__PURE__ */ jsx(ToolResultWorkflowDispatchCard, {
1899
+ part,
1900
+ className
1901
+ });
1902
+ if (part.toolType === "perplexity" || part.toolType === "firecrawl_search") return /* @__PURE__ */ jsx(ToolResultWebSearchCard, {
1903
+ part,
1904
+ className
1905
+ });
1906
+ if (datasourceResources.length > 0) return /* @__PURE__ */ jsx(DatasourceToolResources, {
1907
+ resources: datasourceResources,
1908
+ className
1909
+ });
1910
+ const statusClass = part.state === "error" ? "border-rose-200 bg-rose-50 text-rose-800" : part.state === "pending" ? "border-amber-200 bg-amber-50 text-amber-800" : "border-emerald-200 bg-emerald-50 text-emerald-800";
1911
+ const statusLabel = part.state === "pending" ? "pending" : part.state === "error" ? "error" : "completed";
1912
+ return /* @__PURE__ */ jsxs("section", {
1913
+ className: joinClasses("space-y-3 rounded-lg border border-slate-200 bg-slate-50 p-3", className),
1914
+ "data-tool-call-id": part.id,
1915
+ "data-tool-call-state": part.state,
1916
+ children: [
1917
+ /* @__PURE__ */ jsxs("header", {
1918
+ className: "flex items-center justify-between gap-2",
1919
+ children: [/* @__PURE__ */ jsx("h4", {
1920
+ className: "text-sm font-semibold text-slate-900",
1921
+ children: part.name
1922
+ }), /* @__PURE__ */ jsx("span", {
1923
+ className: joinClasses("rounded-full border px-2 py-0.5 text-xs font-medium", statusClass),
1924
+ children: statusLabel
1925
+ })]
1926
+ }),
1927
+ part.toolType ? /* @__PURE__ */ jsxs("p", {
1928
+ className: "text-xs text-slate-600",
1929
+ children: [
1930
+ /* @__PURE__ */ jsx("span", {
1931
+ className: "font-semibold",
1932
+ children: "Type:"
1933
+ }),
1934
+ " ",
1935
+ part.toolType
1936
+ ]
1937
+ }) : null,
1938
+ /* @__PURE__ */ jsxs("details", {
1939
+ open: detailsOpen,
1940
+ className: "space-y-2",
1941
+ children: [/* @__PURE__ */ jsx("summary", {
1942
+ className: "cursor-pointer text-xs font-semibold text-slate-600",
1943
+ children: "Input"
1944
+ }), /* @__PURE__ */ jsx("pre", {
1945
+ className: "overflow-x-auto rounded bg-slate-900 p-2 text-xs text-slate-100",
1946
+ children: formatJson(part.input)
1947
+ })]
1948
+ }),
1949
+ part.result !== void 0 ? /* @__PURE__ */ jsxs("details", {
1950
+ open: detailsOpen,
1951
+ className: "space-y-2",
1952
+ children: [
1953
+ /* @__PURE__ */ jsx("summary", {
1954
+ className: "cursor-pointer text-xs font-semibold text-slate-600",
1955
+ children: "Output"
1956
+ }),
1957
+ officeMediaFiles.length > 0 ? /* @__PURE__ */ jsxs("div", {
1958
+ className: "space-y-2 rounded border border-slate-200 bg-white p-3",
1959
+ children: [/* @__PURE__ */ jsx("p", {
1960
+ className: "text-xs font-semibold text-slate-700",
1961
+ children: "Generated files"
1962
+ }), /* @__PURE__ */ jsx("ul", {
1963
+ className: "space-y-1",
1964
+ children: officeMediaFiles.map((file) => {
1965
+ const label = file.name || file.pathname || file.id;
1966
+ return /* @__PURE__ */ jsx("li", {
1967
+ className: "text-xs text-slate-700",
1968
+ children: file.url ? /* @__PURE__ */ jsx("a", {
1969
+ href: file.url,
1970
+ target: "_blank",
1971
+ rel: "noreferrer noopener",
1972
+ className: "hover:underline",
1973
+ children: label
1974
+ }) : /* @__PURE__ */ jsx("span", { children: label })
1975
+ }, file.id);
1976
+ })
1977
+ })]
1978
+ }) : null,
1979
+ /* @__PURE__ */ jsx("pre", {
1980
+ className: "overflow-x-auto rounded bg-slate-900 p-2 text-xs text-slate-100",
1981
+ children: formatJson(part.result)
1982
+ })
1983
+ ]
1984
+ }) : null,
1985
+ errorDetails ? /* @__PURE__ */ jsxs("p", {
1986
+ className: "rounded border border-rose-200 bg-rose-50 px-3 py-2 text-xs text-rose-700",
1987
+ children: [
1988
+ /* @__PURE__ */ jsx("span", {
1989
+ className: "font-semibold",
1990
+ children: "Error details:"
1991
+ }),
1992
+ " ",
1993
+ errorDetails
1994
+ ]
1995
+ }) : null
1996
+ ]
1997
+ });
1998
+ }
1999
+
2000
+ //#endregion
2001
+ //#region src/components/message.tsx
2002
+ const DEFAULT_COLLAPSE_THRESHOLD = 600;
2003
+ function getFilenameFromUrl(url) {
2004
+ try {
2005
+ const parsed = new URL(url);
2006
+ return decodeURIComponent(parsed.pathname).split("/").pop()?.trim() || void 0;
2007
+ } catch {
2008
+ return (url.split("?")[0]?.split("#")[0] ?? "").split("/").pop()?.trim() || void 0;
2009
+ }
2010
+ }
2011
+ function getDisplayExtension(label) {
2012
+ if (!label) return;
2013
+ const ext = label.split(".").pop()?.trim().toUpperCase();
2014
+ if (!ext || ext === label.toUpperCase()) return;
2015
+ return ext;
2016
+ }
2017
+ function renderPart(part, index, onOpenAgentThread) {
2018
+ if (part.type === "text") return /* @__PURE__ */ jsx("p", {
2019
+ className: "whitespace-pre-wrap text-sm leading-6",
2020
+ children: part.text
2021
+ }, `text-${index}`);
2022
+ if (part.type === "input_image") {
2023
+ const title = part.metadata?.originalFileName || part.title || getFilenameFromUrl(part.url);
2024
+ return /* @__PURE__ */ jsxs("a", {
2025
+ href: part.url,
2026
+ target: "_blank",
2027
+ rel: "noreferrer noopener",
2028
+ className: "block w-fit rounded-lg border border-slate-200 bg-white p-2",
2029
+ children: [/* @__PURE__ */ jsx("img", {
2030
+ src: part.url,
2031
+ alt: title || "Uploaded image",
2032
+ className: "max-h-56 rounded-md object-contain"
2033
+ }), title ? /* @__PURE__ */ jsx("p", {
2034
+ className: "mt-2 max-w-xs truncate text-xs font-medium text-slate-700",
2035
+ children: title
2036
+ }) : null]
2037
+ }, `image-${index}`);
2038
+ }
2039
+ if (part.type === "input_file") {
2040
+ const fileName = part.metadata?.originalFileName || part.title || getFilenameFromUrl(part.fileUrl) || "Document";
2041
+ const extension = getDisplayExtension(fileName);
2042
+ return /* @__PURE__ */ jsxs("a", {
2043
+ href: part.fileUrl,
2044
+ target: "_blank",
2045
+ rel: "noreferrer noopener",
2046
+ className: "flex w-fit items-center gap-3 rounded-lg border border-slate-200 bg-slate-50 px-3 py-2 text-left",
2047
+ children: [/* @__PURE__ */ jsx("span", {
2048
+ className: "rounded bg-slate-900 px-2 py-1 text-[10px] font-semibold uppercase tracking-wide text-white",
2049
+ children: extension ?? "File"
2050
+ }), /* @__PURE__ */ jsxs("div", {
2051
+ className: "min-w-0",
2052
+ children: [/* @__PURE__ */ jsx("p", {
2053
+ className: "max-w-xs truncate text-sm font-medium text-slate-900",
2054
+ children: fileName
2055
+ }), part.mimeType ? /* @__PURE__ */ jsx("p", {
2056
+ className: "text-xs text-slate-500",
2057
+ children: part.mimeType
2058
+ }) : null]
2059
+ })]
2060
+ }, `file-${index}`);
2061
+ }
2062
+ if (part.type === "tool-call") return /* @__PURE__ */ jsx(ToolResultCard, {
2063
+ part,
2064
+ onOpenAgentThread
2065
+ }, `tool-${part.id}-${index}`);
2066
+ if (part.type === "guardrail") return /* @__PURE__ */ jsx("p", {
2067
+ className: "rounded-lg border border-amber-300 bg-amber-50 px-3 py-2 text-sm text-amber-900",
2068
+ children: part.content
2069
+ }, `guardrail-${index}`);
2070
+ return null;
2071
+ }
2072
+ function orderMessageParts(parts, datasourceToolResultsPosition) {
2073
+ const indexedParts = parts.map((part, index) => ({
2074
+ part,
2075
+ index
2076
+ }));
2077
+ if (datasourceToolResultsPosition === "inline") return indexedParts;
2078
+ const datasourceParts = [];
2079
+ const remainingParts = [];
2080
+ for (const indexedPart of indexedParts) if (indexedPart.part.type === "tool-call" && indexedPart.part.toolType === "datasource") datasourceParts.push(indexedPart);
2081
+ else remainingParts.push(indexedPart);
2082
+ return [...datasourceParts, ...remainingParts];
2083
+ }
2084
+ function Message({ message, className, showRole = true, collapseThreshold = DEFAULT_COLLAPSE_THRESHOLD, datasourceToolResultsPosition = "inline", onOpenAgentThread }) {
2085
+ const roleLabel = message.role.charAt(0).toUpperCase() + message.role.slice(1);
2086
+ const [isExpanded, setIsExpanded] = useState(false);
2087
+ const textContent = useMemo(() => {
2088
+ return message.parts.filter((part) => part.type === "text").map((part) => part.text).join("\n");
2089
+ }, [message.parts]);
2090
+ const canCollapse = message.role === "user" && message.parts.every((part) => part.type === "text") && textContent.length > collapseThreshold;
2091
+ const previewText = canCollapse ? `${textContent.slice(0, collapseThreshold).trimEnd()}...` : textContent;
2092
+ const orderedParts = useMemo(() => orderMessageParts(message.parts, datasourceToolResultsPosition), [message.parts, datasourceToolResultsPosition]);
2093
+ return /* @__PURE__ */ jsxs("article", {
2094
+ className: joinClasses("space-y-3 rounded-xl border border-slate-200 bg-white p-4 shadow-sm", className),
2095
+ "data-message-role": message.role,
2096
+ children: [
2097
+ showRole ? /* @__PURE__ */ jsx("header", {
2098
+ className: "text-xs font-semibold uppercase tracking-wide text-slate-500",
2099
+ children: roleLabel
2100
+ }) : null,
2101
+ /* @__PURE__ */ jsx("div", {
2102
+ className: "space-y-2",
2103
+ children: canCollapse && !isExpanded ? /* @__PURE__ */ jsx("p", {
2104
+ className: "whitespace-pre-wrap text-sm leading-6",
2105
+ children: previewText
2106
+ }) : orderedParts.map(({ part, index }) => renderPart(part, index, onOpenAgentThread))
2107
+ }),
2108
+ canCollapse ? /* @__PURE__ */ jsx("button", {
2109
+ type: "button",
2110
+ className: "text-left text-xs font-medium text-slate-600 hover:text-slate-900",
2111
+ onClick: () => setIsExpanded((previous) => !previous),
2112
+ children: isExpanded ? "Read less" : "Read more"
2113
+ }) : null
2114
+ ]
2115
+ });
2116
+ }
2117
+
2118
+ //#endregion
2119
+ //#region src/components/prompt-form/file-attachments.ts
2120
+ const ALLOWED_EXTENSIONS = new Set([
2121
+ ".pdf",
2122
+ ".jpg",
2123
+ ".jpeg",
2124
+ ".png",
2125
+ ".gif",
2126
+ ".webp",
2127
+ ".bmp",
2128
+ ".doc",
2129
+ ".docx",
2130
+ ".xls",
2131
+ ".xlsx",
2132
+ ".ppt",
2133
+ ".pptx"
2134
+ ]);
2135
+ const PROMPT_FORM_FILE_ACCEPT = "image/*,.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx";
2136
+ function getFileExtension(filename) {
2137
+ const lastDotIndex = filename.lastIndexOf(".");
2138
+ if (lastDotIndex <= 0 || lastDotIndex === filename.length - 1) return;
2139
+ return filename.slice(lastDotIndex).toLowerCase();
2140
+ }
2141
+ function isAllowedPromptFormFile(file) {
2142
+ const extension = getFileExtension(file.name);
2143
+ if (extension && ALLOWED_EXTENSIONS.has(extension)) return true;
2144
+ return file.type.startsWith("image/");
2145
+ }
2146
+ function partitionPromptFormFiles(files) {
2147
+ const validFiles = [];
2148
+ const invalidFiles = [];
2149
+ for (const file of files) {
2150
+ if (isAllowedPromptFormFile(file)) {
2151
+ validFiles.push(file);
2152
+ continue;
2153
+ }
2154
+ invalidFiles.push({
2155
+ file,
2156
+ reason: `File type is not supported for "${file.name}".`
2157
+ });
2158
+ }
2159
+ return {
2160
+ validFiles,
2161
+ invalidFiles
2162
+ };
2163
+ }
2164
+
2165
+ //#endregion
2166
+ //#region src/components/prompt-form/prompt-form-attach-button.tsx
2167
+ function PromptFormAttachButton({ onFilesSelected, onInvalidFiles, accept = PROMPT_FORM_FILE_ACCEPT, disabled = false, label = "Attach file",...props }) {
2168
+ const fileInputRef = useRef(null);
2169
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("input", {
2170
+ ref: fileInputRef,
2171
+ type: "file",
2172
+ accept,
2173
+ multiple: true,
2174
+ className: "hidden",
2175
+ onChange: (event) => {
2176
+ const { validFiles, invalidFiles } = partitionPromptFormFiles(Array.from(event.target.files ?? []));
2177
+ if (invalidFiles.length > 0) onInvalidFiles?.(invalidFiles);
2178
+ if (validFiles.length > 0) onFilesSelected(validFiles);
2179
+ event.currentTarget.value = "";
2180
+ }
2181
+ }), /* @__PURE__ */ jsx(PromptFormIconButton, {
2182
+ type: "button",
2183
+ "aria-label": label,
2184
+ disabled,
2185
+ onClick: () => fileInputRef.current?.click(),
2186
+ ...props,
2187
+ children: /* @__PURE__ */ jsx(Paperclip, { className: "size-4" })
2188
+ })] });
2189
+ }
2190
+
2191
+ //#endregion
2192
+ //#region src/components/prompt-form/prompt-form.tsx
2193
+ const actionButtonClassName = "inline-flex size-8 shrink-0 items-center justify-center rounded-md bg-neutral-100 text-neutral-600 transition-colors hover:bg-neutral-200 disabled:cursor-not-allowed disabled:opacity-40";
2194
+ const defaultMaxRows = 4;
2195
+ function resizeTextarea(textarea, maxRows) {
2196
+ const styles = getComputedStyle(textarea);
2197
+ const lineHeight = Number.parseFloat(styles.lineHeight);
2198
+ const paddingTop = Number.parseFloat(styles.paddingTop);
2199
+ const paddingBottom = Number.parseFloat(styles.paddingBottom);
2200
+ const maxHeight = lineHeight * maxRows + paddingTop + paddingBottom;
2201
+ textarea.style.height = "auto";
2202
+ textarea.style.height = `${Math.min(textarea.scrollHeight, maxHeight)}px`;
2203
+ textarea.style.overflowY = textarea.scrollHeight > maxHeight ? "auto" : "hidden";
2204
+ }
2205
+ function PromptFormIconButton({ className, type = "button",...props }) {
2206
+ return /* @__PURE__ */ jsx("button", {
2207
+ type,
2208
+ className: joinClasses(actionButtonClassName, className),
2209
+ ...props
2210
+ });
2211
+ }
2212
+ function PromptForm({ value, onValueChange, onSubmit, status = "ready", onStop, disabled = false, clearOnSubmit = true, enabledTools, placeholder = "Type a message...", sendLabel = "Send", stopLabel = "Stop", attachLabel = "Attach file", removeFileLabel = "Remove file", enableAttachments = true, files: filesProp, onFilesChange, onInvalidFiles, accept = PROMPT_FORM_FILE_ACCEPT, className, textareaClassName, attachmentsClassName, maxRows = defaultMaxRows, submitButtonClassName, stopButtonClassName, leftSlot, rightSlot }) {
2213
+ const textareaRef = useRef(null);
2214
+ const [internalFiles, setInternalFiles] = useState([]);
2215
+ const [isSubmitting, setIsSubmitting] = useState(false);
2216
+ const files = filesProp ?? internalFiles;
2217
+ const setFiles = onFilesChange ?? setInternalFiles;
2218
+ const isStreaming = status === "streaming" || status === "submitted";
2219
+ const hasContent = value.trim().length > 0 || files.length > 0;
2220
+ const canSubmit = !disabled && !isSubmitting && !isStreaming && hasContent;
2221
+ const isInteractionDisabled = disabled || isSubmitting;
2222
+ useLayoutEffect(() => {
2223
+ const textarea = textareaRef.current;
2224
+ if (textarea) resizeTextarea(textarea, maxRows);
2225
+ }, [maxRows]);
2226
+ function addFiles(nextFiles) {
2227
+ const { validFiles, invalidFiles } = partitionPromptFormFiles(nextFiles);
2228
+ if (invalidFiles.length > 0) onInvalidFiles?.(invalidFiles);
2229
+ if (validFiles.length > 0) setFiles([...files, ...validFiles]);
2230
+ }
2231
+ function removeFile(index) {
2232
+ setFiles(files.filter((_, fileIndex) => fileIndex !== index));
2233
+ }
2234
+ return /* @__PURE__ */ jsxs("form", {
2235
+ className: joinClasses("flex w-full flex-col overflow-hidden rounded-2xl border border-neutral-200 bg-white", className),
2236
+ onSubmit: async (event) => {
2237
+ event.preventDefault();
2238
+ if (!canSubmit) return;
2239
+ setIsSubmitting(true);
2240
+ try {
2241
+ const text$1 = value.trim();
2242
+ const selectedFiles = [...files];
2243
+ await onSubmit({
2244
+ text: text$1,
2245
+ ...enabledTools ? { enabledTools } : {},
2246
+ files: selectedFiles
2247
+ });
2248
+ if (clearOnSubmit) {
2249
+ onValueChange("");
2250
+ setFiles([]);
2251
+ }
2252
+ } finally {
2253
+ setIsSubmitting(false);
2254
+ queueMicrotask(() => {
2255
+ const textarea = textareaRef.current;
2256
+ if (textarea) resizeTextarea(textarea, maxRows);
2257
+ });
2258
+ }
2259
+ },
2260
+ children: [/* @__PURE__ */ jsxs("div", {
2261
+ className: "flex items-end gap-3 px-4 py-3",
2262
+ children: [
2263
+ leftSlot ?? null,
2264
+ /* @__PURE__ */ jsx("textarea", {
2265
+ ref: textareaRef,
2266
+ rows: 1,
2267
+ className: joinClasses("min-h-10 flex-1 resize-none overflow-hidden border-0 bg-transparent px-0 py-1.5 text-sm leading-6 text-neutral-900 outline-none placeholder:text-neutral-400", textareaClassName),
2268
+ value,
2269
+ onChange: (event) => {
2270
+ onValueChange(event.target.value);
2271
+ resizeTextarea(event.currentTarget, maxRows);
2272
+ },
2273
+ placeholder,
2274
+ disabled: isInteractionDisabled
2275
+ }),
2276
+ /* @__PURE__ */ jsxs("div", {
2277
+ className: "flex shrink-0 items-center gap-2",
2278
+ children: [
2279
+ enableAttachments ? /* @__PURE__ */ jsx(PromptFormAttachButton, {
2280
+ accept,
2281
+ disabled: isInteractionDisabled,
2282
+ label: attachLabel,
2283
+ onFilesSelected: addFiles,
2284
+ onInvalidFiles
2285
+ }) : null,
2286
+ rightSlot ?? null,
2287
+ isStreaming ? /* @__PURE__ */ jsx(PromptFormIconButton, {
2288
+ "aria-label": stopLabel,
2289
+ className: stopButtonClassName,
2290
+ onClick: onStop,
2291
+ disabled: disabled || !onStop,
2292
+ children: /* @__PURE__ */ jsx(Stop, { className: "size-4" })
2293
+ }) : /* @__PURE__ */ jsx(PromptFormIconButton, {
2294
+ type: "submit",
2295
+ "aria-label": sendLabel,
2296
+ className: submitButtonClassName,
2297
+ disabled: !canSubmit,
2298
+ children: /* @__PURE__ */ jsx(ArrowUp, { className: "size-4" })
2299
+ })
2300
+ ]
2301
+ })
2302
+ ]
2303
+ }), files.length > 0 ? /* @__PURE__ */ jsx("div", {
2304
+ className: joinClasses("flex flex-wrap gap-2 border-t border-neutral-200 px-4 py-2", attachmentsClassName),
2305
+ children: files.map((file, index) => /* @__PURE__ */ jsxs("div", {
2306
+ className: "flex max-w-full items-center gap-1.5 rounded-md bg-neutral-100 px-2 py-1 text-xs text-neutral-700",
2307
+ children: [/* @__PURE__ */ jsx("span", {
2308
+ className: "max-w-[150px] truncate",
2309
+ children: file.name
2310
+ }), /* @__PURE__ */ jsx("button", {
2311
+ type: "button",
2312
+ className: "text-neutral-500 transition-colors hover:text-neutral-800 disabled:cursor-not-allowed disabled:opacity-40",
2313
+ "aria-label": `${removeFileLabel}: ${file.name}`,
2314
+ disabled: isInteractionDisabled,
2315
+ onClick: () => removeFile(index),
2316
+ children: /* @__PURE__ */ jsx(X, { className: "size-3.5" })
2317
+ })]
2318
+ }, `${file.name}-${file.lastModified}-${index}`))
2319
+ }) : null]
2320
+ });
2321
+ }
2322
+
2323
+ //#endregion
2324
+ //#region src/components/speech-to-text-toggle/transcribe-audio.ts
2325
+ const DUMMY_TRANSCRIPTION = "Hello, this is a demo transcription.";
2326
+ async function dummyTranscribeAudio(_audio, signal) {
2327
+ await new Promise((resolve, reject) => {
2328
+ const timeoutId = setTimeout(resolve, 600);
2329
+ signal?.addEventListener("abort", () => {
2330
+ clearTimeout(timeoutId);
2331
+ reject(signal.reason ?? new DOMException("Aborted", "AbortError"));
2332
+ }, { once: true });
2333
+ });
2334
+ return DUMMY_TRANSCRIPTION;
2335
+ }
2336
+ async function transcribeAudioViaEndpoint(audio, endpoint, signal) {
2337
+ const response = await fetch(endpoint, {
2338
+ method: "POST",
2339
+ body: audio,
2340
+ signal
2341
+ });
2342
+ if (!response.ok) throw new Error(`Transcription request failed with status ${response.status}`);
2343
+ const body = await response.json();
2344
+ const text$1 = body.text ?? body.data;
2345
+ if (!text$1) throw new Error("Transcription response did not include text");
2346
+ return text$1;
2347
+ }
2348
+
2349
+ //#endregion
2350
+ //#region src/components/speech-to-text-toggle/use-voice-recording.ts
2351
+ function useVoiceRecording() {
2352
+ const mediaRecorderRef = useRef(null);
2353
+ const streamRef = useRef(null);
2354
+ const chunksRef = useRef([]);
2355
+ const stopTracks = useCallback(() => {
2356
+ for (const track of streamRef.current?.getTracks() ?? []) track.stop();
2357
+ streamRef.current = null;
2358
+ }, []);
2359
+ return {
2360
+ start: useCallback(async () => {
2361
+ const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
2362
+ const recorder = new MediaRecorder(stream);
2363
+ streamRef.current = stream;
2364
+ chunksRef.current = [];
2365
+ mediaRecorderRef.current = recorder;
2366
+ recorder.addEventListener("dataavailable", (event) => {
2367
+ if (event.data.size > 0) chunksRef.current.push(event.data);
2368
+ });
2369
+ recorder.start();
2370
+ }, []),
2371
+ stop: useCallback(async () => {
2372
+ const recorder = mediaRecorderRef.current;
2373
+ if (!recorder || recorder.state === "inactive") throw new Error("No active voice recording");
2374
+ const audio = await new Promise((resolve, reject) => {
2375
+ recorder.addEventListener("stop", () => {
2376
+ stopTracks();
2377
+ mediaRecorderRef.current = null;
2378
+ resolve(new Blob(chunksRef.current, { type: recorder.mimeType || "audio/webm" }));
2379
+ }, { once: true });
2380
+ recorder.addEventListener("error", () => {
2381
+ stopTracks();
2382
+ mediaRecorderRef.current = null;
2383
+ reject(/* @__PURE__ */ new Error("Failed to stop voice recording"));
2384
+ }, { once: true });
2385
+ recorder.stop();
2386
+ });
2387
+ chunksRef.current = [];
2388
+ return audio;
2389
+ }, [stopTracks]),
2390
+ cancel: useCallback(() => {
2391
+ const recorder = mediaRecorderRef.current;
2392
+ if (recorder && recorder.state !== "inactive") recorder.stop();
2393
+ stopTracks();
2394
+ mediaRecorderRef.current = null;
2395
+ chunksRef.current = [];
2396
+ }, [stopTracks])
2397
+ };
2398
+ }
2399
+
2400
+ //#endregion
2401
+ //#region src/components/speech-to-text-toggle/speech-to-text-toggle.tsx
2402
+ function SpeechToTextToggle({ onTranscriptionComplete, onTranscriptionError, transcribe, transcriptionEndpoint, disabled = false, label = "Voice input", listeningLabel = "Stop recording", loadingLabel = "Transcribing...", className,...props }) {
2403
+ const [isListening, setIsListening] = useState(false);
2404
+ const [isTranscribing, setIsTranscribing] = useState(false);
2405
+ const abortControllerRef = useRef(null);
2406
+ const { start, stop } = useVoiceRecording();
2407
+ const transcribeAudio = transcribe ?? (transcriptionEndpoint ? (audio, signal) => transcribeAudioViaEndpoint(audio, transcriptionEndpoint, signal) : dummyTranscribeAudio);
2408
+ async function handleToggle() {
2409
+ if (disabled || isTranscribing) return;
2410
+ if (!isListening) {
2411
+ try {
2412
+ await start();
2413
+ setIsListening(true);
2414
+ } catch (error) {
2415
+ onTranscriptionError?.(error instanceof Error ? error : /* @__PURE__ */ new Error("Microphone access was denied"));
2416
+ }
2417
+ return;
2418
+ }
2419
+ setIsListening(false);
2420
+ setIsTranscribing(true);
2421
+ const abortController = new AbortController();
2422
+ abortControllerRef.current = abortController;
2423
+ try {
2424
+ const text$1 = await transcribeAudio(await stop(), abortController.signal);
2425
+ if (text$1.trim()) onTranscriptionComplete(text$1);
2426
+ } catch (error) {
2427
+ if (error instanceof DOMException && error.name === "AbortError") return;
2428
+ onTranscriptionError?.(error instanceof Error ? error : /* @__PURE__ */ new Error("Transcription failed"));
2429
+ } finally {
2430
+ abortControllerRef.current = null;
2431
+ setIsTranscribing(false);
2432
+ }
2433
+ }
2434
+ const ariaLabel = isTranscribing ? loadingLabel : isListening ? listeningLabel : label;
2435
+ return /* @__PURE__ */ jsx(PromptFormIconButton, {
2436
+ type: "button",
2437
+ "aria-label": ariaLabel,
2438
+ "aria-pressed": isListening,
2439
+ "aria-busy": isTranscribing,
2440
+ disabled: disabled || isTranscribing,
2441
+ className: joinClasses(isListening && !isTranscribing ? "text-red-600" : void 0, className),
2442
+ onClick: () => {
2443
+ handleToggle();
2444
+ },
2445
+ ...props,
2446
+ children: isTranscribing ? /* @__PURE__ */ jsx(Loader, { className: "size-4 animate-spin" }) : /* @__PURE__ */ jsx(Microphone, { className: "size-4" })
2447
+ });
2448
+ }
2449
+
2450
+ //#endregion
2451
+ export { ArrowUp, DatasourceFolderCard, DatasourceToolResources, DatasourceToolResultCard, Loader, Message, Microphone, PROMPT_FORM_FILE_ACCEPT, Paperclip, PromptForm, PromptFormAttachButton, PromptFormIconButton, ResourceCard, Sheet, SheetClose, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetTitle, SheetTrigger, SpeechToTextToggle, Stop, ToolResultAgentInvocationCard, ToolResultBrowserCard, ToolResultCard, ToolResultExternalDatasourceCard, ToolResultHttpRequestCard, ToolResultMcpCard, ToolResultOfficeDocumentsCard, ToolResultWebSearchCard, ToolResultWorkflowDispatchCard, X, dummyTranscribeAudio, isAllowedPromptFormFile, partitionPromptFormFiles, transcribeAudioViaEndpoint };
2452
+ //# sourceMappingURL=index.js.map