@plaisolutions/react 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +116 -1
- package/dist/components/index.d.ts +162 -17
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/index.js +464 -139
- package/dist/components/index.js.map +1 -1
- package/dist/index.d.ts +170 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +486 -155
- package/dist/index.js.map +1 -1
- package/dist/styles.css +1 -1
- package/package.json +2 -2
package/dist/components/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createContext, forwardRef, useCallback, useContext, useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
2
2
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
-
import { Bot, CheckCircle2, Database, File, FileText, Globe, LoaderCircle, MessageSquare, Minus, Plug, Plus, Workflow, XCircle } from "lucide-react";
|
|
3
|
+
import { Bot, Check, CheckCircle2, Copy, Database, File, FileText, Globe, LoaderCircle, MessageSquare, Minus, Plug, Plus, ThumbsDown, ThumbsUp, Workflow, XCircle } from "lucide-react";
|
|
4
4
|
|
|
5
5
|
//#region src/components/internal/join-classes.ts
|
|
6
6
|
function joinClasses(...classes) {
|
|
@@ -42,11 +42,15 @@ function DatasourceIcon() {
|
|
|
42
42
|
]
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
|
-
function DatasourceToolResultCardContent({ title, description, type, resourceCount }) {
|
|
45
|
+
function DatasourceToolResultCardContent({ title, description, type, icon, resourceCount }) {
|
|
46
46
|
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
47
47
|
/* @__PURE__ */ jsxs("header", {
|
|
48
48
|
className: "flex items-center gap-3",
|
|
49
|
-
children: [
|
|
49
|
+
children: [icon ? /* @__PURE__ */ jsx("img", {
|
|
50
|
+
src: icon,
|
|
51
|
+
alt: "",
|
|
52
|
+
className: "size-5 shrink-0 object-contain"
|
|
53
|
+
}) : /* @__PURE__ */ jsx(DatasourceIcon, {}), /* @__PURE__ */ jsx("p", {
|
|
50
54
|
className: "min-w-0 truncate text-base leading-5",
|
|
51
55
|
children: type
|
|
52
56
|
})]
|
|
@@ -72,33 +76,43 @@ function DatasourceToolResultCardContent({ title, description, type, resourceCou
|
|
|
72
76
|
function DatasourceToolResultCard(props) {
|
|
73
77
|
return /* @__PURE__ */ jsx("article", {
|
|
74
78
|
"aria-label": `${props.type}: ${props.title}`,
|
|
75
|
-
className: "w-
|
|
79
|
+
className: "w-46.5 rounded-lg bg-neutral-100 p-4 text-neutral-950",
|
|
76
80
|
children: /* @__PURE__ */ jsx(DatasourceToolResultCardContent, { ...props })
|
|
77
81
|
});
|
|
78
82
|
}
|
|
79
83
|
|
|
80
84
|
//#endregion
|
|
81
85
|
//#region src/components/resource-card.tsx
|
|
82
|
-
function ResourceCardContent({ icon, title, description }) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
86
|
+
function ResourceCardContent({ icon, type, title, description }) {
|
|
87
|
+
const heading = type ?? title;
|
|
88
|
+
const bodyTitle = type ? title : description;
|
|
89
|
+
const bodyDescription = type ? description : void 0;
|
|
90
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
91
|
+
/* @__PURE__ */ jsxs("header", {
|
|
92
|
+
className: "flex min-w-0 items-center gap-3",
|
|
93
|
+
children: [icon ? /* @__PURE__ */ jsx("img", {
|
|
94
|
+
src: icon,
|
|
95
|
+
alt: "",
|
|
96
|
+
width: "20",
|
|
97
|
+
height: "20",
|
|
98
|
+
className: "h-5 w-5 shrink-0 object-contain"
|
|
99
|
+
}) : /* @__PURE__ */ jsx(File, {
|
|
100
|
+
className: "h-5 w-5 shrink-0",
|
|
101
|
+
"aria-hidden": "true"
|
|
102
|
+
}), /* @__PURE__ */ jsx("p", {
|
|
103
|
+
className: "min-w-0 truncate text-base leading-5",
|
|
104
|
+
children: heading
|
|
105
|
+
})]
|
|
106
|
+
}),
|
|
107
|
+
bodyTitle ? /* @__PURE__ */ jsx("p", {
|
|
108
|
+
className: "mt-2 line-clamp-2 text-sm font-medium leading-5",
|
|
109
|
+
children: bodyTitle
|
|
110
|
+
}) : null,
|
|
111
|
+
bodyDescription ? /* @__PURE__ */ jsx("p", {
|
|
112
|
+
className: "mt-2 line-clamp-2 text-xs leading-4 text-neutral-600",
|
|
113
|
+
children: bodyDescription
|
|
114
|
+
}) : null
|
|
115
|
+
] });
|
|
102
116
|
}
|
|
103
117
|
function ResourceCard(props) {
|
|
104
118
|
const className = "block w-full rounded-lg bg-neutral-100 p-4 text-left text-neutral-950";
|
|
@@ -466,11 +480,12 @@ const SheetDescription = forwardRef(function SheetDescription$1({ className, id,
|
|
|
466
480
|
|
|
467
481
|
//#endregion
|
|
468
482
|
//#region src/components/datasource-folder-card.tsx
|
|
469
|
-
function DatasourceFolderCard({ title, description, type, resources }) {
|
|
483
|
+
function DatasourceFolderCard({ icon, title, description, type, url, resources }) {
|
|
470
484
|
return /* @__PURE__ */ jsxs(Sheet, { children: [/* @__PURE__ */ jsx(SheetTrigger, {
|
|
471
485
|
"aria-label": `${type}: ${title}`,
|
|
472
486
|
className: "min-h-[183px] w-[186px] rounded-lg bg-neutral-100 p-4 text-left font-normal text-neutral-950 transition-colors hover:bg-neutral-200 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950",
|
|
473
487
|
children: /* @__PURE__ */ jsx(DatasourceToolResultCardContent, {
|
|
488
|
+
icon,
|
|
474
489
|
title,
|
|
475
490
|
description,
|
|
476
491
|
type,
|
|
@@ -480,16 +495,30 @@ function DatasourceFolderCard({ title, description, type, resources }) {
|
|
|
480
495
|
closeLabel: "Cerrar",
|
|
481
496
|
children: [/* @__PURE__ */ jsxs(SheetHeader, {
|
|
482
497
|
className: "pr-10",
|
|
483
|
-
children: [
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
498
|
+
children: [
|
|
499
|
+
/* @__PURE__ */ jsxs("div", {
|
|
500
|
+
className: "flex items-start gap-3",
|
|
501
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
502
|
+
className: "mt-0.5 shrink-0",
|
|
503
|
+
children: icon ? /* @__PURE__ */ jsx("img", {
|
|
504
|
+
src: icon,
|
|
505
|
+
alt: "",
|
|
506
|
+
className: "size-5 object-contain"
|
|
507
|
+
}) : /* @__PURE__ */ jsx(DatasourceIcon, {})
|
|
508
|
+
}), /* @__PURE__ */ jsx(SheetTitle, { children: title })]
|
|
509
|
+
}),
|
|
510
|
+
description ? /* @__PURE__ */ jsx(SheetDescription, {
|
|
511
|
+
className: "line-clamp-2 text-xs leading-4",
|
|
512
|
+
children: description
|
|
513
|
+
}) : null,
|
|
514
|
+
url ? /* @__PURE__ */ jsx("a", {
|
|
515
|
+
href: url,
|
|
516
|
+
target: "_blank",
|
|
517
|
+
rel: "noreferrer noopener",
|
|
518
|
+
className: "text-xs font-medium text-neutral-700 underline underline-offset-2",
|
|
519
|
+
children: "View"
|
|
520
|
+
}) : null
|
|
521
|
+
]
|
|
493
522
|
}), /* @__PURE__ */ jsxs("section", {
|
|
494
523
|
className: "mt-5",
|
|
495
524
|
"aria-label": "Recursos",
|
|
@@ -504,6 +533,109 @@ function DatasourceFolderCard({ title, description, type, resources }) {
|
|
|
504
533
|
})] });
|
|
505
534
|
}
|
|
506
535
|
|
|
536
|
+
//#endregion
|
|
537
|
+
//#region src/components/opengraph.ts
|
|
538
|
+
function normalizeLocale(locale) {
|
|
539
|
+
return locale?.trim().toLowerCase();
|
|
540
|
+
}
|
|
541
|
+
function getCurrentLocale() {
|
|
542
|
+
if (typeof document !== "undefined" && document.documentElement.lang) return document.documentElement.lang;
|
|
543
|
+
if (typeof navigator !== "undefined") return navigator.language || navigator.userLanguage;
|
|
544
|
+
}
|
|
545
|
+
function getLocaleCandidates(locale) {
|
|
546
|
+
const normalized = normalizeLocale(locale);
|
|
547
|
+
if (!normalized) return [];
|
|
548
|
+
const [language] = normalized.split("-");
|
|
549
|
+
return language === normalized ? [normalized] : [normalized, language];
|
|
550
|
+
}
|
|
551
|
+
function hasStringValue(value) {
|
|
552
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
553
|
+
}
|
|
554
|
+
function getTranslation(translations, locale) {
|
|
555
|
+
if (!translations) return void 0;
|
|
556
|
+
return Object.entries(translations).find(([translationLocale]) => normalizeLocale(translationLocale) === locale)?.[1];
|
|
557
|
+
}
|
|
558
|
+
function getLocalizedOpenGraphValue(container, field, locale) {
|
|
559
|
+
const opengraph = container.extra_info?.opengraph;
|
|
560
|
+
if (!opengraph) return void 0;
|
|
561
|
+
for (const candidate of getLocaleCandidates(locale ?? getCurrentLocale())) {
|
|
562
|
+
const translated = getTranslation(opengraph.translations, candidate)?.[field];
|
|
563
|
+
if (hasStringValue(translated)) return translated.trim();
|
|
564
|
+
}
|
|
565
|
+
const fallback = opengraph[field];
|
|
566
|
+
return hasStringValue(fallback) ? fallback.trim() : void 0;
|
|
567
|
+
}
|
|
568
|
+
function getResourceTitle(resource, locale) {
|
|
569
|
+
return getLocalizedOpenGraphValue(resource, "title", locale) ?? resource.name;
|
|
570
|
+
}
|
|
571
|
+
function getResourceDescription(resource, locale) {
|
|
572
|
+
return getLocalizedOpenGraphValue(resource, "description", locale) ?? resource.summary ?? resource.content ?? void 0;
|
|
573
|
+
}
|
|
574
|
+
function getResourceOpenGraphImage(resource, locale) {
|
|
575
|
+
return getLocalizedOpenGraphValue(resource, "image", locale);
|
|
576
|
+
}
|
|
577
|
+
function getResourceOpenGraphType(resource, locale) {
|
|
578
|
+
return getLocalizedOpenGraphValue(resource, "type", locale);
|
|
579
|
+
}
|
|
580
|
+
function readNestedString(records, path) {
|
|
581
|
+
for (const record of records) {
|
|
582
|
+
let current = record;
|
|
583
|
+
for (const key of path) {
|
|
584
|
+
if (!current || typeof current !== "object" || Array.isArray(current)) {
|
|
585
|
+
current = void 0;
|
|
586
|
+
break;
|
|
587
|
+
}
|
|
588
|
+
current = current[key];
|
|
589
|
+
}
|
|
590
|
+
if (hasStringValue(current)) return current.trim();
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
function isGoogleDriveUrl(url) {
|
|
594
|
+
if (!url) return false;
|
|
595
|
+
try {
|
|
596
|
+
const hostname = new URL(url).hostname;
|
|
597
|
+
return hostname === "drive.google.com" || hostname.endsWith(".google.com");
|
|
598
|
+
} catch {
|
|
599
|
+
return false;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
function isGoogleDriveResource(resource) {
|
|
603
|
+
const records = [resource.extra_info, resource.metadata];
|
|
604
|
+
return [
|
|
605
|
+
resource.datasource?.source,
|
|
606
|
+
readNestedString(records, ["datasource_source"]),
|
|
607
|
+
readNestedString(records, ["source"]),
|
|
608
|
+
readNestedString(records, ["origin"]),
|
|
609
|
+
readNestedString(records, ["datasource", "source"]),
|
|
610
|
+
readNestedString(records, ["datasource", "origin"])
|
|
611
|
+
].some((candidate) => candidate?.toUpperCase() === "GOOGLE_DRIVE") || isGoogleDriveUrl(resource.external_url) || isGoogleDriveUrl(resource.url);
|
|
612
|
+
}
|
|
613
|
+
function getResourceUrl(resource, locale) {
|
|
614
|
+
const openGraphUrl = getLocalizedOpenGraphValue(resource, "url", locale);
|
|
615
|
+
if (openGraphUrl) return openGraphUrl;
|
|
616
|
+
if (isGoogleDriveResource(resource)) return resource.url ?? resource.external_url;
|
|
617
|
+
return resource.external_url ?? resource.url;
|
|
618
|
+
}
|
|
619
|
+
function getResourceType(resource, locale) {
|
|
620
|
+
const openGraphType = getResourceOpenGraphType(resource, locale);
|
|
621
|
+
if (openGraphType) return openGraphType;
|
|
622
|
+
if (resource.type?.trim()) return resource.type.charAt(0).toUpperCase() + resource.type.slice(1);
|
|
623
|
+
const metadataType = resource.metadata.type;
|
|
624
|
+
if (hasStringValue(metadataType)) return metadataType.charAt(0).toUpperCase() + metadataType.slice(1);
|
|
625
|
+
const url = resource.url ?? resource.external_url ?? "";
|
|
626
|
+
if (/\.pdf(?:$|[?#])/i.test(url)) return "PDF";
|
|
627
|
+
if (/\.docx?(?:$|[?#])/i.test(url)) return "Document";
|
|
628
|
+
if (/\.xlsx?(?:$|[?#])/i.test(url)) return "Spreadsheet";
|
|
629
|
+
if (/\.pptx?(?:$|[?#])/i.test(url)) return "Presentation";
|
|
630
|
+
if (/\.(?:jpe?g|png|gif|webp)(?:$|[?#])/i.test(url)) return "Image";
|
|
631
|
+
if (/\.(?:mp4|mov|avi)(?:$|[?#])/i.test(url)) return "Video";
|
|
632
|
+
if (/^https?:\/\//i.test(url)) return "Webpage";
|
|
633
|
+
return "Document";
|
|
634
|
+
}
|
|
635
|
+
function getResourceIcon(resource, locale) {
|
|
636
|
+
return getResourceOpenGraphImage(resource, locale) ?? readNestedString([resource.extra_info], ["icon"]) ?? readNestedString([resource.metadata], ["icon"]);
|
|
637
|
+
}
|
|
638
|
+
|
|
507
639
|
//#endregion
|
|
508
640
|
//#region src/components/datasource-tool-resources.tsx
|
|
509
641
|
function isObject(value) {
|
|
@@ -513,15 +645,13 @@ function readString(value, key) {
|
|
|
513
645
|
const candidate = value?.[key];
|
|
514
646
|
return typeof candidate === "string" && candidate.trim() ? candidate : null;
|
|
515
647
|
}
|
|
516
|
-
function
|
|
517
|
-
return readString(resource.extra_info, "icon") ?? readString(resource.metadata, "icon") ?? void 0;
|
|
518
|
-
}
|
|
519
|
-
function toResourceCardProps(resource) {
|
|
648
|
+
function toResourceCardProps(resource, locale) {
|
|
520
649
|
return {
|
|
521
|
-
icon: getResourceIcon(resource),
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
650
|
+
icon: getResourceIcon(resource, locale),
|
|
651
|
+
type: getResourceType(resource, locale),
|
|
652
|
+
title: getResourceTitle(resource, locale),
|
|
653
|
+
description: getResourceDescription(resource, locale),
|
|
654
|
+
url: getResourceUrl(resource, locale)
|
|
525
655
|
};
|
|
526
656
|
}
|
|
527
657
|
function isDatasourceResource(value) {
|
|
@@ -530,10 +660,17 @@ function isDatasourceResource(value) {
|
|
|
530
660
|
const datasource = value.datasource;
|
|
531
661
|
return typeof value.id === "string" && typeof value.name === "string" && typeof value.type === "string" && (value.url === null || typeof value.url === "string") && (value.external_url === null || typeof value.external_url === "string") && isObject(value.metadata) && isObject(value.extra_info) && (folder === null || isObject(folder) && typeof folder.id === "string" && typeof folder.name === "string" && isObject(folder.extra_info)) && (datasource === null || isObject(datasource) && typeof datasource.type === "string" && (datasource.description === null || typeof datasource.description === "string") && (datasource.summary === null || typeof datasource.summary === "string"));
|
|
532
662
|
}
|
|
533
|
-
function DatasourceToolResources({ resources, className }) {
|
|
663
|
+
function DatasourceToolResources({ resources, locale, className }) {
|
|
534
664
|
const folderGroups = /* @__PURE__ */ new Map();
|
|
535
665
|
const ungroupedResources = [];
|
|
666
|
+
const seenUrls = /* @__PURE__ */ new Set();
|
|
536
667
|
for (const resource of resources) {
|
|
668
|
+
if (resource.extra_info.public === false) continue;
|
|
669
|
+
const resourceUrl = getResourceUrl(resource, locale);
|
|
670
|
+
if (resourceUrl) {
|
|
671
|
+
if (seenUrls.has(resourceUrl)) continue;
|
|
672
|
+
seenUrls.add(resourceUrl);
|
|
673
|
+
}
|
|
537
674
|
if (!resource.folder) {
|
|
538
675
|
ungroupedResources.push(resource);
|
|
539
676
|
continue;
|
|
@@ -550,15 +687,20 @@ function DatasourceToolResources({ resources, className }) {
|
|
|
550
687
|
className: joinClasses("flex flex-wrap gap-3", className),
|
|
551
688
|
children: [Array.from(folderGroups.values()).map(({ folder, resources: group }) => {
|
|
552
689
|
const datasource = group[0]?.datasource;
|
|
553
|
-
const type = readString(folder.extra_info, "type") ?? datasource?.type ?? "FOLDER";
|
|
554
|
-
const
|
|
690
|
+
const type = getLocalizedOpenGraphValue(folder, "type", locale) ?? readString(folder.extra_info, "type") ?? datasource?.type ?? "FOLDER";
|
|
691
|
+
const title = getLocalizedOpenGraphValue(folder, "title", locale) ?? folder.name;
|
|
692
|
+
const description = getLocalizedOpenGraphValue(folder, "description", locale) ?? readString(folder.extra_info, "description") ?? readString(folder.extra_info, "summary") ?? datasource?.description ?? datasource?.summary ?? "";
|
|
693
|
+
const icon = getLocalizedOpenGraphValue(folder, "image", locale);
|
|
694
|
+
const url = getLocalizedOpenGraphValue(folder, "url", locale);
|
|
555
695
|
return /* @__PURE__ */ jsx(DatasourceFolderCard, {
|
|
556
|
-
|
|
696
|
+
icon,
|
|
697
|
+
title,
|
|
557
698
|
description,
|
|
558
|
-
type,
|
|
559
|
-
|
|
699
|
+
type: getLocalizedOpenGraphValue(folder, "type", locale) ?? type,
|
|
700
|
+
url,
|
|
701
|
+
resources: group.map((resource) => toResourceCardProps(resource, locale))
|
|
560
702
|
}, folder.id);
|
|
561
|
-
}), ungroupedResources.map((resource) => /* @__PURE__ */ jsx(ResourceCard, { ...toResourceCardProps(resource) }, resource.id))]
|
|
703
|
+
}), ungroupedResources.map((resource) => /* @__PURE__ */ jsx(ResourceCard, { ...toResourceCardProps(resource, locale) }, resource.id))]
|
|
562
704
|
});
|
|
563
705
|
}
|
|
564
706
|
|
|
@@ -1862,7 +2004,7 @@ function getDatasourceResources(part) {
|
|
|
1862
2004
|
const resources = part.metadata.resources;
|
|
1863
2005
|
return Array.isArray(resources) ? resources.filter(isDatasourceResource) : [];
|
|
1864
2006
|
}
|
|
1865
|
-
function ToolResultCard({ part, className, detailsOpen = true, onOpenAgentThread }) {
|
|
2007
|
+
function ToolResultCard({ part, className, detailsOpen = true, locale, onOpenAgentThread }) {
|
|
1866
2008
|
const officeMediaFiles = getOfficeDocumentMediaFiles(part);
|
|
1867
2009
|
const datasourceResources = getDatasourceResources(part);
|
|
1868
2010
|
const errorDetails = formatToolErrorDetails(part.errorDetails);
|
|
@@ -1905,6 +2047,7 @@ function ToolResultCard({ part, className, detailsOpen = true, onOpenAgentThread
|
|
|
1905
2047
|
});
|
|
1906
2048
|
if (datasourceResources.length > 0) return /* @__PURE__ */ jsx(DatasourceToolResources, {
|
|
1907
2049
|
resources: datasourceResources,
|
|
2050
|
+
locale,
|
|
1908
2051
|
className
|
|
1909
2052
|
});
|
|
1910
2053
|
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";
|
|
@@ -1999,6 +2142,56 @@ function ToolResultCard({ part, className, detailsOpen = true, onOpenAgentThread
|
|
|
1999
2142
|
|
|
2000
2143
|
//#endregion
|
|
2001
2144
|
//#region src/components/message.tsx
|
|
2145
|
+
function Message({ align = "start", className,...props }) {
|
|
2146
|
+
return /* @__PURE__ */ jsx("article", {
|
|
2147
|
+
className: joinClasses("flex w-full items-start gap-3", align === "end" ? "flex-row-reverse" : void 0, className),
|
|
2148
|
+
"data-align": align,
|
|
2149
|
+
...props
|
|
2150
|
+
});
|
|
2151
|
+
}
|
|
2152
|
+
function getFallbackInitials(fallback) {
|
|
2153
|
+
const words = fallback.trim().split(/\s+/).filter(Boolean);
|
|
2154
|
+
if (words.length === 0) return "?";
|
|
2155
|
+
if (words.length === 1) return words[0].slice(0, 2).toUpperCase();
|
|
2156
|
+
return `${words[0][0]}${words[words.length - 1][0]}`.toUpperCase();
|
|
2157
|
+
}
|
|
2158
|
+
function MessageAvatar({ src, fallback, alt = fallback, className, imageClassName,...props }) {
|
|
2159
|
+
const [failedSrc, setFailedSrc] = useState(null);
|
|
2160
|
+
const showImage = src.trim().length > 0 && failedSrc !== src;
|
|
2161
|
+
const initials = getFallbackInitials(fallback);
|
|
2162
|
+
return /* @__PURE__ */ jsx("div", {
|
|
2163
|
+
className: joinClasses("flex size-10 shrink-0 items-center justify-center overflow-hidden rounded-full bg-neutral-200 text-xs font-semibold uppercase text-neutral-700", className),
|
|
2164
|
+
...props,
|
|
2165
|
+
children: showImage ? /* @__PURE__ */ jsx("img", {
|
|
2166
|
+
src,
|
|
2167
|
+
alt,
|
|
2168
|
+
className: joinClasses("size-full rounded-full object-cover", imageClassName),
|
|
2169
|
+
onError: () => setFailedSrc(src)
|
|
2170
|
+
}) : /* @__PURE__ */ jsx("span", {
|
|
2171
|
+
role: "img",
|
|
2172
|
+
"aria-label": alt,
|
|
2173
|
+
children: initials
|
|
2174
|
+
})
|
|
2175
|
+
});
|
|
2176
|
+
}
|
|
2177
|
+
function MessageContent({ className,...props }) {
|
|
2178
|
+
return /* @__PURE__ */ jsx("div", {
|
|
2179
|
+
className: joinClasses("min-w-0 max-w-full flex-1 space-y-2", className),
|
|
2180
|
+
...props
|
|
2181
|
+
});
|
|
2182
|
+
}
|
|
2183
|
+
function MessageHeader({ className,...props }) {
|
|
2184
|
+
return /* @__PURE__ */ jsx("header", {
|
|
2185
|
+
className: joinClasses("text-xs font-semibold uppercase tracking-wide text-neutral-500", className),
|
|
2186
|
+
...props
|
|
2187
|
+
});
|
|
2188
|
+
}
|
|
2189
|
+
function MessageFooter({ className,...props }) {
|
|
2190
|
+
return /* @__PURE__ */ jsx("footer", {
|
|
2191
|
+
className: joinClasses("flex items-center gap-2", className),
|
|
2192
|
+
...props
|
|
2193
|
+
});
|
|
2194
|
+
}
|
|
2002
2195
|
const DEFAULT_COLLAPSE_THRESHOLD = 600;
|
|
2003
2196
|
function getFilenameFromUrl(url) {
|
|
2004
2197
|
try {
|
|
@@ -2009,16 +2202,22 @@ function getFilenameFromUrl(url) {
|
|
|
2009
2202
|
}
|
|
2010
2203
|
}
|
|
2011
2204
|
function getDisplayExtension(label) {
|
|
2012
|
-
if (!label) return;
|
|
2205
|
+
if (!label) return void 0;
|
|
2013
2206
|
const ext = label.split(".").pop()?.trim().toUpperCase();
|
|
2014
|
-
if (!ext || ext === label.toUpperCase()) return;
|
|
2207
|
+
if (!ext || ext === label.toUpperCase()) return void 0;
|
|
2015
2208
|
return ext;
|
|
2016
2209
|
}
|
|
2017
|
-
function renderPart(part, index, onOpenAgentThread) {
|
|
2018
|
-
if (part.type === "text")
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2210
|
+
function renderPart(part, index, message, isStreaming, onOpenAgentThread, locale, renderText, renderToolCall) {
|
|
2211
|
+
if (part.type === "text") {
|
|
2212
|
+
if (renderText) return /* @__PURE__ */ jsx("div", { children: renderText(part, {
|
|
2213
|
+
message,
|
|
2214
|
+
isStreaming
|
|
2215
|
+
}) }, `text-${index}`);
|
|
2216
|
+
return /* @__PURE__ */ jsx("p", {
|
|
2217
|
+
className: "whitespace-pre-wrap text-sm leading-6",
|
|
2218
|
+
children: part.text
|
|
2219
|
+
}, `text-${index}`);
|
|
2220
|
+
}
|
|
2022
2221
|
if (part.type === "input_image") {
|
|
2023
2222
|
const title = part.metadata?.originalFileName || part.title || getFilenameFromUrl(part.url);
|
|
2024
2223
|
return /* @__PURE__ */ jsxs("a", {
|
|
@@ -2059,59 +2258,157 @@ function renderPart(part, index, onOpenAgentThread) {
|
|
|
2059
2258
|
})]
|
|
2060
2259
|
}, `file-${index}`);
|
|
2061
2260
|
}
|
|
2062
|
-
if (part.type === "tool-call")
|
|
2063
|
-
part,
|
|
2064
|
-
|
|
2065
|
-
|
|
2261
|
+
if (part.type === "tool-call") {
|
|
2262
|
+
if (renderToolCall) return /* @__PURE__ */ jsx("div", { children: renderToolCall(part, { message }) }, `tool-${part.id}-${index}`);
|
|
2263
|
+
return /* @__PURE__ */ jsx(ToolResultCard, {
|
|
2264
|
+
part,
|
|
2265
|
+
locale,
|
|
2266
|
+
onOpenAgentThread
|
|
2267
|
+
}, `tool-${part.id}-${index}`);
|
|
2268
|
+
}
|
|
2066
2269
|
if (part.type === "guardrail") return /* @__PURE__ */ jsx("p", {
|
|
2067
2270
|
className: "rounded-lg border border-amber-300 bg-amber-50 px-3 py-2 text-sm text-amber-900",
|
|
2068
2271
|
children: part.content
|
|
2069
2272
|
}, `guardrail-${index}`);
|
|
2070
2273
|
return null;
|
|
2071
2274
|
}
|
|
2072
|
-
function orderMessageParts(parts,
|
|
2275
|
+
function orderMessageParts(parts, position) {
|
|
2073
2276
|
const indexedParts = parts.map((part, index) => ({
|
|
2074
2277
|
part,
|
|
2075
2278
|
index
|
|
2076
2279
|
}));
|
|
2077
|
-
if (
|
|
2280
|
+
if (position === "inline") return indexedParts;
|
|
2078
2281
|
const datasourceParts = [];
|
|
2079
2282
|
const remainingParts = [];
|
|
2080
2283
|
for (const indexedPart of indexedParts) if (indexedPart.part.type === "tool-call" && indexedPart.part.toolType === "datasource") datasourceParts.push(indexedPart);
|
|
2081
2284
|
else remainingParts.push(indexedPart);
|
|
2082
2285
|
return [...datasourceParts, ...remainingParts];
|
|
2083
2286
|
}
|
|
2084
|
-
function
|
|
2085
|
-
const roleLabel = message.role.charAt(0).toUpperCase() + message.role.slice(1);
|
|
2287
|
+
function MessageParts({ message, collapseThreshold = DEFAULT_COLLAPSE_THRESHOLD, datasourceToolResultsPosition = "inline", onOpenAgentThread, locale, renderText, renderToolCall, isStreaming = false, readMoreLabel = "Read more", readLessLabel = "Read less", className,...props }) {
|
|
2086
2288
|
const [isExpanded, setIsExpanded] = useState(false);
|
|
2087
|
-
const textContent = useMemo(() =>
|
|
2088
|
-
return message.parts.filter((part) => part.type === "text").map((part) => part.text).join("\n");
|
|
2089
|
-
}, [message.parts]);
|
|
2289
|
+
const textContent = useMemo(() => message.parts.filter((part) => part.type === "text").map((part) => part.text).join("\n"), [message.parts]);
|
|
2090
2290
|
const canCollapse = message.role === "user" && message.parts.every((part) => part.type === "text") && textContent.length > collapseThreshold;
|
|
2091
2291
|
const previewText = canCollapse ? `${textContent.slice(0, collapseThreshold).trimEnd()}...` : textContent;
|
|
2092
2292
|
const orderedParts = useMemo(() => orderMessageParts(message.parts, datasourceToolResultsPosition), [message.parts, datasourceToolResultsPosition]);
|
|
2093
|
-
return /* @__PURE__ */ jsxs("
|
|
2094
|
-
className: joinClasses("space-y-
|
|
2293
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
2294
|
+
className: joinClasses("space-y-2", className),
|
|
2095
2295
|
"data-message-role": message.role,
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2296
|
+
...props,
|
|
2297
|
+
children: [canCollapse && !isExpanded ? /* @__PURE__ */ jsx("p", {
|
|
2298
|
+
className: "whitespace-pre-wrap text-sm leading-6",
|
|
2299
|
+
children: previewText
|
|
2300
|
+
}) : orderedParts.map(({ part, index }) => renderPart(part, index, message, isStreaming, onOpenAgentThread, locale, renderText, renderToolCall)), canCollapse ? /* @__PURE__ */ jsx("button", {
|
|
2301
|
+
type: "button",
|
|
2302
|
+
className: "text-left text-xs font-medium text-slate-600 hover:text-slate-900",
|
|
2303
|
+
onClick: () => setIsExpanded((previous) => !previous),
|
|
2304
|
+
children: isExpanded ? readLessLabel : readMoreLabel
|
|
2305
|
+
}) : null]
|
|
2306
|
+
});
|
|
2307
|
+
}
|
|
2308
|
+
|
|
2309
|
+
//#endregion
|
|
2310
|
+
//#region src/components/clipboard.tsx
|
|
2311
|
+
async function writeTextToClipboard(text$1) {
|
|
2312
|
+
if (typeof navigator !== "undefined" && navigator.clipboard?.writeText) {
|
|
2313
|
+
await navigator.clipboard.writeText(text$1);
|
|
2314
|
+
return;
|
|
2315
|
+
}
|
|
2316
|
+
if (typeof document === "undefined") throw new Error("Clipboard is not available in this environment.");
|
|
2317
|
+
const textarea = document.createElement("textarea");
|
|
2318
|
+
textarea.value = text$1;
|
|
2319
|
+
textarea.setAttribute("readonly", "");
|
|
2320
|
+
textarea.style.position = "fixed";
|
|
2321
|
+
textarea.style.opacity = "0";
|
|
2322
|
+
document.body.appendChild(textarea);
|
|
2323
|
+
textarea.select();
|
|
2324
|
+
try {
|
|
2325
|
+
if (!document.execCommand("copy")) throw new Error("The browser rejected the clipboard operation.");
|
|
2326
|
+
} finally {
|
|
2327
|
+
textarea.remove();
|
|
2328
|
+
}
|
|
2329
|
+
}
|
|
2330
|
+
function Clipboard({ text: text$1, onCopy, copiedDuration = 2e3, copyLabel = "Copy", copiedLabel = "Copied", children, className, disabled, onClick, type = "button",...props }) {
|
|
2331
|
+
const [isCopied, setIsCopied] = useState(false);
|
|
2332
|
+
const [isCopying, setIsCopying] = useState(false);
|
|
2333
|
+
const timeoutRef = useRef();
|
|
2334
|
+
useEffect(() => () => {
|
|
2335
|
+
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
|
2336
|
+
}, []);
|
|
2337
|
+
async function handleClick(event) {
|
|
2338
|
+
onClick?.(event);
|
|
2339
|
+
if (event.defaultPrevented || disabled || isCopying) return;
|
|
2340
|
+
setIsCopying(true);
|
|
2341
|
+
try {
|
|
2342
|
+
await writeTextToClipboard(text$1);
|
|
2343
|
+
setIsCopied(true);
|
|
2344
|
+
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
|
2345
|
+
timeoutRef.current = setTimeout(() => setIsCopied(false), copiedDuration);
|
|
2346
|
+
try {
|
|
2347
|
+
await onCopy?.(text$1);
|
|
2348
|
+
} catch {}
|
|
2349
|
+
} catch {
|
|
2350
|
+
setIsCopied(false);
|
|
2351
|
+
} finally {
|
|
2352
|
+
setIsCopying(false);
|
|
2353
|
+
}
|
|
2354
|
+
}
|
|
2355
|
+
const state = {
|
|
2356
|
+
isCopied,
|
|
2357
|
+
isCopying
|
|
2358
|
+
};
|
|
2359
|
+
return /* @__PURE__ */ jsxs("button", {
|
|
2360
|
+
type,
|
|
2361
|
+
"aria-label": isCopied ? copiedLabel : copyLabel,
|
|
2362
|
+
className: joinClasses("inline-flex size-9 shrink-0 items-center justify-center rounded-lg border border-neutral-200 bg-white text-neutral-700 transition-colors hover:bg-neutral-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950 disabled:cursor-not-allowed disabled:opacity-40", className),
|
|
2363
|
+
disabled: disabled || isCopying,
|
|
2364
|
+
onClick: handleClick,
|
|
2365
|
+
...props,
|
|
2366
|
+
children: [typeof children === "function" ? children(state) : children !== void 0 && children !== null ? children : isCopied ? /* @__PURE__ */ jsx(Check, {
|
|
2367
|
+
className: "size-4",
|
|
2368
|
+
"aria-hidden": "true"
|
|
2369
|
+
}) : /* @__PURE__ */ jsx(Copy, {
|
|
2370
|
+
className: "size-4",
|
|
2371
|
+
"aria-hidden": "true"
|
|
2372
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
2373
|
+
className: "sr-only",
|
|
2374
|
+
"aria-live": "polite",
|
|
2375
|
+
children: isCopied ? copiedLabel : copyLabel
|
|
2376
|
+
})]
|
|
2377
|
+
});
|
|
2378
|
+
}
|
|
2379
|
+
|
|
2380
|
+
//#endregion
|
|
2381
|
+
//#region src/components/thumbs.tsx
|
|
2382
|
+
function ThumbButton({ label, icon, className, type = "button",...props }) {
|
|
2383
|
+
return /* @__PURE__ */ jsxs("button", {
|
|
2384
|
+
type,
|
|
2385
|
+
"aria-label": label,
|
|
2386
|
+
className: joinClasses("inline-flex size-9 shrink-0 items-center justify-center rounded-lg border border-neutral-200 bg-white text-neutral-700 transition-colors hover:bg-neutral-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950 disabled:cursor-not-allowed disabled:opacity-40 aria-pressed:border-neutral-950 aria-pressed:bg-neutral-950 aria-pressed:text-white", className),
|
|
2387
|
+
...props,
|
|
2388
|
+
children: [icon, /* @__PURE__ */ jsx("span", {
|
|
2389
|
+
className: "sr-only",
|
|
2390
|
+
children: label
|
|
2391
|
+
})]
|
|
2392
|
+
});
|
|
2393
|
+
}
|
|
2394
|
+
function ThumbUp({ label = "Rate positively",...props }) {
|
|
2395
|
+
return /* @__PURE__ */ jsx(ThumbButton, {
|
|
2396
|
+
label,
|
|
2397
|
+
icon: /* @__PURE__ */ jsx(ThumbsUp, {
|
|
2398
|
+
className: "size-4",
|
|
2399
|
+
"aria-hidden": "true"
|
|
2400
|
+
}),
|
|
2401
|
+
...props
|
|
2402
|
+
});
|
|
2403
|
+
}
|
|
2404
|
+
function ThumbDown({ label = "Rate negatively",...props }) {
|
|
2405
|
+
return /* @__PURE__ */ jsx(ThumbButton, {
|
|
2406
|
+
label,
|
|
2407
|
+
icon: /* @__PURE__ */ jsx(ThumbsDown, {
|
|
2408
|
+
className: "size-4",
|
|
2409
|
+
"aria-hidden": "true"
|
|
2410
|
+
}),
|
|
2411
|
+
...props
|
|
2115
2412
|
});
|
|
2116
2413
|
}
|
|
2117
2414
|
|
|
@@ -2209,16 +2506,17 @@ function PromptFormIconButton({ className, type = "button",...props }) {
|
|
|
2209
2506
|
...props
|
|
2210
2507
|
});
|
|
2211
2508
|
}
|
|
2212
|
-
function PromptForm({ value, onValueChange, onSubmit, status = "ready", onStop, disabled = false, clearOnSubmit = true, enabledTools, placeholder = "Type a message...", sendLabel = "Send", stopLabel = "Stop", attachLabel = "Attach file", removeFileLabel = "Remove file", enableAttachments = true, files: filesProp, onFilesChange, onInvalidFiles, accept = PROMPT_FORM_FILE_ACCEPT, className, textareaClassName, attachmentsClassName, maxRows = defaultMaxRows, submitButtonClassName, stopButtonClassName, leftSlot, rightSlot }) {
|
|
2509
|
+
function PromptForm({ value, onValueChange, onSubmit, status = "ready", uploadState, onStop, disabled = false, clearOnSubmit = true, enabledTools, placeholder = "Type a message...", sendLabel = "Send", stopLabel = "Stop", attachLabel = "Attach file", removeFileLabel = "Remove file", uploadingLabel = "Uploading", processingUploadLabel = "Processing upload", uploadErrorLabel = "Upload failed", enableAttachments = true, files: filesProp, onFilesChange, onInvalidFiles, accept = PROMPT_FORM_FILE_ACCEPT, className, textareaClassName, attachmentsClassName, uploadProgressClassName, maxRows = defaultMaxRows, submitButtonClassName, stopButtonClassName, leftSlot, rightSlot, submitOnEnter = true, onTextareaKeyDown }) {
|
|
2213
2510
|
const textareaRef = useRef(null);
|
|
2214
2511
|
const [internalFiles, setInternalFiles] = useState([]);
|
|
2215
2512
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
2216
2513
|
const files = filesProp ?? internalFiles;
|
|
2217
2514
|
const setFiles = onFilesChange ?? setInternalFiles;
|
|
2218
2515
|
const isStreaming = status === "streaming" || status === "submitted";
|
|
2516
|
+
const isUploading = uploadState?.status === "uploading" || uploadState?.status === "processing";
|
|
2219
2517
|
const hasContent = value.trim().length > 0 || files.length > 0;
|
|
2220
|
-
const canSubmit = !disabled && !isSubmitting && !isStreaming && hasContent;
|
|
2221
|
-
const isInteractionDisabled = disabled || isSubmitting;
|
|
2518
|
+
const canSubmit = !disabled && !isSubmitting && !isStreaming && !isUploading && hasContent;
|
|
2519
|
+
const isInteractionDisabled = disabled || isSubmitting || isUploading;
|
|
2222
2520
|
useLayoutEffect(() => {
|
|
2223
2521
|
const textarea = textareaRef.current;
|
|
2224
2522
|
if (textarea) resizeTextarea(textarea, maxRows);
|
|
@@ -2270,6 +2568,13 @@ function PromptForm({ value, onValueChange, onSubmit, status = "ready", onStop,
|
|
|
2270
2568
|
onValueChange(event.target.value);
|
|
2271
2569
|
resizeTextarea(event.currentTarget, maxRows);
|
|
2272
2570
|
},
|
|
2571
|
+
onKeyDown: (event) => {
|
|
2572
|
+
onTextareaKeyDown?.(event);
|
|
2573
|
+
if (submitOnEnter && !event.defaultPrevented && event.key === "Enter" && !event.shiftKey && !event.nativeEvent.isComposing) {
|
|
2574
|
+
event.preventDefault();
|
|
2575
|
+
event.currentTarget.form?.requestSubmit();
|
|
2576
|
+
}
|
|
2577
|
+
},
|
|
2273
2578
|
placeholder,
|
|
2274
2579
|
disabled: isInteractionDisabled
|
|
2275
2580
|
}),
|
|
@@ -2300,52 +2605,53 @@ function PromptForm({ value, onValueChange, onSubmit, status = "ready", onStop,
|
|
|
2300
2605
|
]
|
|
2301
2606
|
})
|
|
2302
2607
|
]
|
|
2303
|
-
}), files.length > 0 ? /* @__PURE__ */
|
|
2304
|
-
className: joinClasses("
|
|
2305
|
-
children:
|
|
2306
|
-
className: "flex
|
|
2307
|
-
children:
|
|
2308
|
-
className: "max-w-
|
|
2309
|
-
children:
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2608
|
+
}), files.length > 0 ? /* @__PURE__ */ jsxs("div", {
|
|
2609
|
+
className: joinClasses("border-t border-neutral-200", attachmentsClassName),
|
|
2610
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
2611
|
+
className: "flex flex-wrap gap-2 px-4 py-2",
|
|
2612
|
+
children: files.map((file, index) => /* @__PURE__ */ jsxs("div", {
|
|
2613
|
+
className: "flex max-w-full items-center gap-1.5 rounded-md bg-neutral-100 px-2 py-1 text-xs text-neutral-700",
|
|
2614
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
2615
|
+
className: "max-w-[150px] truncate",
|
|
2616
|
+
children: file.name
|
|
2617
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
2618
|
+
type: "button",
|
|
2619
|
+
className: "text-neutral-500 transition-colors hover:text-neutral-800 disabled:cursor-not-allowed disabled:opacity-40",
|
|
2620
|
+
"aria-label": `${removeFileLabel}: ${file.name}`,
|
|
2621
|
+
disabled: isInteractionDisabled,
|
|
2622
|
+
onClick: () => removeFile(index),
|
|
2623
|
+
children: /* @__PURE__ */ jsx(X, { className: "size-3.5" })
|
|
2624
|
+
})]
|
|
2625
|
+
}, `${file.name}-${file.lastModified}-${index}`))
|
|
2626
|
+
}), isUploading && uploadState ? /* @__PURE__ */ jsxs("div", {
|
|
2627
|
+
className: "space-y-1.5 px-4 pb-3",
|
|
2628
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
2629
|
+
className: "flex items-center justify-between gap-3 text-xs text-neutral-500",
|
|
2630
|
+
children: [/* @__PURE__ */ jsxs("span", {
|
|
2631
|
+
className: "truncate",
|
|
2632
|
+
children: [uploadState.status === "processing" ? processingUploadLabel : uploadingLabel, uploadState.fileName ? `: ${uploadState.fileName}` : ""]
|
|
2633
|
+
}), /* @__PURE__ */ jsxs("span", { children: [Math.round(uploadState.progress), "%"] })]
|
|
2634
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
2635
|
+
role: "progressbar",
|
|
2636
|
+
tabIndex: 0,
|
|
2637
|
+
"aria-label": uploadState.status === "processing" ? processingUploadLabel : uploadingLabel,
|
|
2638
|
+
"aria-valuemin": 0,
|
|
2639
|
+
"aria-valuemax": 100,
|
|
2640
|
+
"aria-valuenow": Math.round(uploadState.progress),
|
|
2641
|
+
className: joinClasses("h-1.5 overflow-hidden rounded-full bg-neutral-200", uploadProgressClassName),
|
|
2642
|
+
children: /* @__PURE__ */ jsx("div", {
|
|
2643
|
+
className: "h-full rounded-full bg-neutral-900 transition-[width] duration-150",
|
|
2644
|
+
style: { width: `${uploadState.progress}%` }
|
|
2645
|
+
})
|
|
2317
2646
|
})]
|
|
2318
|
-
},
|
|
2647
|
+
}) : uploadState?.status === "error" ? /* @__PURE__ */ jsx("p", {
|
|
2648
|
+
className: "px-4 pb-3 text-xs text-red-600",
|
|
2649
|
+
children: uploadErrorLabel
|
|
2650
|
+
}) : null]
|
|
2319
2651
|
}) : null]
|
|
2320
2652
|
});
|
|
2321
2653
|
}
|
|
2322
2654
|
|
|
2323
|
-
//#endregion
|
|
2324
|
-
//#region src/components/speech-to-text-toggle/transcribe-audio.ts
|
|
2325
|
-
const DUMMY_TRANSCRIPTION = "Hello, this is a demo transcription.";
|
|
2326
|
-
async function dummyTranscribeAudio(_audio, signal) {
|
|
2327
|
-
await new Promise((resolve, reject) => {
|
|
2328
|
-
const timeoutId = setTimeout(resolve, 600);
|
|
2329
|
-
signal?.addEventListener("abort", () => {
|
|
2330
|
-
clearTimeout(timeoutId);
|
|
2331
|
-
reject(signal.reason ?? new DOMException("Aborted", "AbortError"));
|
|
2332
|
-
}, { once: true });
|
|
2333
|
-
});
|
|
2334
|
-
return DUMMY_TRANSCRIPTION;
|
|
2335
|
-
}
|
|
2336
|
-
async function transcribeAudioViaEndpoint(audio, endpoint, signal) {
|
|
2337
|
-
const response = await fetch(endpoint, {
|
|
2338
|
-
method: "POST",
|
|
2339
|
-
body: audio,
|
|
2340
|
-
signal
|
|
2341
|
-
});
|
|
2342
|
-
if (!response.ok) throw new Error(`Transcription request failed with status ${response.status}`);
|
|
2343
|
-
const body = await response.json();
|
|
2344
|
-
const text$1 = body.text ?? body.data;
|
|
2345
|
-
if (!text$1) throw new Error("Transcription response did not include text");
|
|
2346
|
-
return text$1;
|
|
2347
|
-
}
|
|
2348
|
-
|
|
2349
2655
|
//#endregion
|
|
2350
2656
|
//#region src/components/speech-to-text-toggle/use-voice-recording.ts
|
|
2351
2657
|
function useVoiceRecording() {
|
|
@@ -2399,12 +2705,17 @@ function useVoiceRecording() {
|
|
|
2399
2705
|
|
|
2400
2706
|
//#endregion
|
|
2401
2707
|
//#region src/components/speech-to-text-toggle/speech-to-text-toggle.tsx
|
|
2402
|
-
function SpeechToTextToggle({ onTranscriptionComplete, onTranscriptionError, transcribe,
|
|
2708
|
+
function SpeechToTextToggle({ onTranscriptionComplete, onTranscriptionError, transcribe, disabled = false, label = "Voice input", listeningLabel = "Stop recording", loadingLabel = "Transcribing...", className,...props }) {
|
|
2403
2709
|
const [isListening, setIsListening] = useState(false);
|
|
2404
2710
|
const [isTranscribing, setIsTranscribing] = useState(false);
|
|
2405
2711
|
const abortControllerRef = useRef(null);
|
|
2406
|
-
const { start, stop } = useVoiceRecording();
|
|
2407
|
-
|
|
2712
|
+
const { start, stop, cancel } = useVoiceRecording();
|
|
2713
|
+
useEffect(() => {
|
|
2714
|
+
return () => {
|
|
2715
|
+
abortControllerRef.current?.abort();
|
|
2716
|
+
cancel();
|
|
2717
|
+
};
|
|
2718
|
+
}, [cancel]);
|
|
2408
2719
|
async function handleToggle() {
|
|
2409
2720
|
if (disabled || isTranscribing) return;
|
|
2410
2721
|
if (!isListening) {
|
|
@@ -2421,7 +2732,7 @@ function SpeechToTextToggle({ onTranscriptionComplete, onTranscriptionError, tra
|
|
|
2421
2732
|
const abortController = new AbortController();
|
|
2422
2733
|
abortControllerRef.current = abortController;
|
|
2423
2734
|
try {
|
|
2424
|
-
const text$1 = await
|
|
2735
|
+
const text$1 = await transcribe(await stop(), abortController.signal);
|
|
2425
2736
|
if (text$1.trim()) onTranscriptionComplete(text$1);
|
|
2426
2737
|
} catch (error) {
|
|
2427
2738
|
if (error instanceof DOMException && error.name === "AbortError") return;
|
|
@@ -2448,5 +2759,19 @@ function SpeechToTextToggle({ onTranscriptionComplete, onTranscriptionError, tra
|
|
|
2448
2759
|
}
|
|
2449
2760
|
|
|
2450
2761
|
//#endregion
|
|
2451
|
-
|
|
2762
|
+
//#region src/components/speech-to-text-toggle/transcribe-audio.ts
|
|
2763
|
+
const DUMMY_TRANSCRIPTION = "Hello, this is a demo transcription.";
|
|
2764
|
+
async function dummyTranscribeAudio(_audio, signal) {
|
|
2765
|
+
await new Promise((resolve, reject) => {
|
|
2766
|
+
const timeoutId = setTimeout(resolve, 600);
|
|
2767
|
+
signal?.addEventListener("abort", () => {
|
|
2768
|
+
clearTimeout(timeoutId);
|
|
2769
|
+
reject(signal.reason ?? new DOMException("Aborted", "AbortError"));
|
|
2770
|
+
}, { once: true });
|
|
2771
|
+
});
|
|
2772
|
+
return DUMMY_TRANSCRIPTION;
|
|
2773
|
+
}
|
|
2774
|
+
|
|
2775
|
+
//#endregion
|
|
2776
|
+
export { ArrowUp, Clipboard, DatasourceFolderCard, DatasourceToolResources, DatasourceToolResultCard, Loader, Message, MessageAvatar, MessageContent, MessageFooter, MessageHeader, MessageParts, Microphone, PROMPT_FORM_FILE_ACCEPT, Paperclip, PromptForm, PromptFormAttachButton, PromptFormIconButton, ResourceCard, Sheet, SheetClose, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetTitle, SheetTrigger, SpeechToTextToggle, Stop, ThumbDown, ThumbUp, ToolResultAgentInvocationCard, ToolResultBrowserCard, ToolResultCard, ToolResultExternalDatasourceCard, ToolResultHttpRequestCard, ToolResultMcpCard, ToolResultOfficeDocumentsCard, ToolResultWebSearchCard, ToolResultWorkflowDispatchCard, X, dummyTranscribeAudio, getLocalizedOpenGraphValue, getResourceDescription, getResourceIcon, getResourceOpenGraphImage, getResourceOpenGraphType, getResourceTitle, getResourceType, getResourceUrl, isAllowedPromptFormFile, partitionPromptFormFiles };
|
|
2452
2777
|
//# sourceMappingURL=index.js.map
|