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