@plaisolutions/react 0.10.1 → 0.11.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 +8 -0
- package/dist/components/index.d.ts +16 -4
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/index.js +96 -26
- package/dist/components/index.js.map +1 -1
- package/dist/index.d.ts +67 -54
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +97 -26
- package/dist/index.js.map +1 -1
- package/dist/styles.css +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -30,6 +30,7 @@ function useChat(options) {
|
|
|
30
30
|
const actions = useMemo(() => ({
|
|
31
31
|
sendMessage: chat.sendMessage.bind(chat),
|
|
32
32
|
rateMessage: chat.rateMessage.bind(chat),
|
|
33
|
+
getResourceDownloadUrl: chat.getResourceDownloadUrl.bind(chat),
|
|
33
34
|
resendMessage: chat.resendMessage.bind(chat),
|
|
34
35
|
transcribeAudio: chat.transcribeAudio.bind(chat),
|
|
35
36
|
uploadFile: chat.uploadFile.bind(chat),
|
|
@@ -162,8 +163,64 @@ function ResourceCardContent({ icon, type, title, description }) {
|
|
|
162
163
|
] });
|
|
163
164
|
}
|
|
164
165
|
function ResourceCard(props) {
|
|
165
|
-
const { className: customClassName, variant = "card",...contentProps } = props;
|
|
166
|
+
const { className: customClassName, variant = "card", resourceId, requiresDownloadUrl = false, getResourceDownloadUrl,...contentProps } = props;
|
|
167
|
+
const [downloadState, setDownloadState] = useState("idle");
|
|
168
|
+
const abortControllerRef = useRef(null);
|
|
166
169
|
const className = joinClasses("block rounded-lg bg-neutral-100 p-4 text-left text-neutral-950", variant === "card" ? "min-h-[183px] w-[186px] max-w-full" : "w-full", customClassName);
|
|
170
|
+
const interactiveClassName = `${className} transition-colors hover:bg-neutral-200 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950`;
|
|
171
|
+
useEffect(() => () => {
|
|
172
|
+
abortControllerRef.current?.abort();
|
|
173
|
+
}, []);
|
|
174
|
+
const openProtectedResource = useCallback(async () => {
|
|
175
|
+
if (!resourceId || !getResourceDownloadUrl || downloadState === "loading") return;
|
|
176
|
+
abortControllerRef.current?.abort();
|
|
177
|
+
const abortController = new AbortController();
|
|
178
|
+
abortControllerRef.current = abortController;
|
|
179
|
+
setDownloadState("loading");
|
|
180
|
+
const popup = window.open("about:blank", "_blank");
|
|
181
|
+
if (popup) popup.opener = null;
|
|
182
|
+
try {
|
|
183
|
+
const downloadUrl = await getResourceDownloadUrl({
|
|
184
|
+
resourceId,
|
|
185
|
+
signal: abortController.signal
|
|
186
|
+
});
|
|
187
|
+
if (abortController.signal.aborted) {
|
|
188
|
+
popup?.close();
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (!downloadUrl.trim()) throw new Error("The resource download URL is empty.");
|
|
192
|
+
if (popup && !popup.closed) popup.location.replace(downloadUrl);
|
|
193
|
+
else if (!window.open(downloadUrl, "_blank", "noopener,noreferrer")) throw new Error("The resource window was blocked.");
|
|
194
|
+
setDownloadState("idle");
|
|
195
|
+
} catch {
|
|
196
|
+
popup?.close();
|
|
197
|
+
if (!abortController.signal.aborted) setDownloadState("error");
|
|
198
|
+
}
|
|
199
|
+
}, [
|
|
200
|
+
downloadState,
|
|
201
|
+
getResourceDownloadUrl,
|
|
202
|
+
resourceId
|
|
203
|
+
]);
|
|
204
|
+
if (requiresDownloadUrl && resourceId && getResourceDownloadUrl) return /* @__PURE__ */ jsxs("button", {
|
|
205
|
+
type: "button",
|
|
206
|
+
className: `${interactiveClassName} border-0 disabled:cursor-wait disabled:opacity-70`,
|
|
207
|
+
onClick: openProtectedResource,
|
|
208
|
+
disabled: downloadState === "loading",
|
|
209
|
+
"aria-busy": downloadState === "loading",
|
|
210
|
+
children: [
|
|
211
|
+
/* @__PURE__ */ jsx(ResourceCardContent, { ...contentProps }),
|
|
212
|
+
downloadState === "loading" ? /* @__PURE__ */ jsx("span", {
|
|
213
|
+
className: "mt-2 block text-xs text-neutral-600",
|
|
214
|
+
"aria-live": "polite",
|
|
215
|
+
children: "Opening resource…"
|
|
216
|
+
}) : null,
|
|
217
|
+
downloadState === "error" ? /* @__PURE__ */ jsx("span", {
|
|
218
|
+
className: "mt-2 block text-xs text-red-700",
|
|
219
|
+
role: "alert",
|
|
220
|
+
children: "Unable to open resource. Try again."
|
|
221
|
+
}) : null
|
|
222
|
+
]
|
|
223
|
+
});
|
|
167
224
|
if (!contentProps.url) return /* @__PURE__ */ jsx("article", {
|
|
168
225
|
"aria-label": `${contentProps.title}: ${contentProps.description}`,
|
|
169
226
|
className,
|
|
@@ -173,7 +230,7 @@ function ResourceCard(props) {
|
|
|
173
230
|
href: contentProps.url,
|
|
174
231
|
target: "_blank",
|
|
175
232
|
rel: "noreferrer noopener",
|
|
176
|
-
className:
|
|
233
|
+
className: interactiveClassName,
|
|
177
234
|
children: /* @__PURE__ */ jsx(ResourceCardContent, { ...contentProps })
|
|
178
235
|
});
|
|
179
236
|
}
|
|
@@ -661,6 +718,9 @@ function isGoogleDriveResource(resource) {
|
|
|
661
718
|
readNestedString(records, ["datasource", "origin"])
|
|
662
719
|
].some((candidate) => candidate?.toUpperCase() === "GOOGLE_DRIVE") || isGoogleDriveUrl(resource.external_url) || isGoogleDriveUrl(resource.url);
|
|
663
720
|
}
|
|
721
|
+
function requiresResourceDownloadUrl(resource, locale) {
|
|
722
|
+
return Boolean(resource.store && !resource.external_url && !getLocalizedOpenGraphValue(resource, "url", locale) && !isGoogleDriveResource(resource));
|
|
723
|
+
}
|
|
664
724
|
function getResourceUrl(resource, locale) {
|
|
665
725
|
const openGraphUrl = getLocalizedOpenGraphValue(resource, "url", locale);
|
|
666
726
|
if (openGraphUrl) return openGraphUrl;
|
|
@@ -696,16 +756,20 @@ function readString(value, key) {
|
|
|
696
756
|
const candidate = value?.[key];
|
|
697
757
|
return typeof candidate === "string" && candidate.trim() ? candidate : null;
|
|
698
758
|
}
|
|
699
|
-
function toResourceCardProps(resource, locale) {
|
|
759
|
+
function toResourceCardProps(resource, locale, getResourceDownloadUrl) {
|
|
760
|
+
const requiresDownloadUrl = requiresResourceDownloadUrl(resource, locale);
|
|
700
761
|
return {
|
|
701
762
|
icon: getResourceIcon(resource, locale),
|
|
702
763
|
type: getResourceType(resource, locale),
|
|
703
764
|
title: getResourceTitle(resource, locale),
|
|
704
765
|
description: getResourceDescription(resource, locale),
|
|
705
|
-
url: getResourceUrl(resource, locale)
|
|
766
|
+
url: requiresDownloadUrl ? null : getResourceUrl(resource, locale),
|
|
767
|
+
resourceId: resource.id,
|
|
768
|
+
getResourceDownloadUrl: requiresDownloadUrl ? getResourceDownloadUrl : void 0,
|
|
769
|
+
requiresDownloadUrl
|
|
706
770
|
};
|
|
707
771
|
}
|
|
708
|
-
function toFolderCardProps(folder, group, locale) {
|
|
772
|
+
function toFolderCardProps(folder, group, locale, getResourceDownloadUrl) {
|
|
709
773
|
const datasource = group[0]?.datasource;
|
|
710
774
|
const type = getLocalizedOpenGraphValue(folder, "type", locale) ?? readString(folder.extra_info, "type") ?? datasource?.type ?? "FOLDER";
|
|
711
775
|
return {
|
|
@@ -714,7 +778,7 @@ function toFolderCardProps(folder, group, locale) {
|
|
|
714
778
|
description: getLocalizedOpenGraphValue(folder, "description", locale) ?? readString(folder.extra_info, "description") ?? readString(folder.extra_info, "summary") ?? datasource?.description ?? datasource?.summary ?? "",
|
|
715
779
|
type,
|
|
716
780
|
url: getLocalizedOpenGraphValue(folder, "url", locale),
|
|
717
|
-
resources: group.map((resource) => toResourceCardProps(resource, locale))
|
|
781
|
+
resources: group.map((resource) => toResourceCardProps(resource, locale, getResourceDownloadUrl))
|
|
718
782
|
};
|
|
719
783
|
}
|
|
720
784
|
function isDatasourceResource(value) {
|
|
@@ -723,7 +787,7 @@ function isDatasourceResource(value) {
|
|
|
723
787
|
const datasource = value.datasource;
|
|
724
788
|
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"));
|
|
725
789
|
}
|
|
726
|
-
function getDatasourceResourceCards(resources, locale) {
|
|
790
|
+
function getDatasourceResourceCards(resources, locale, getResourceDownloadUrl) {
|
|
727
791
|
const folderGroups = /* @__PURE__ */ new Map();
|
|
728
792
|
const ungroupedResources = [];
|
|
729
793
|
const seenUrls = /* @__PURE__ */ new Set();
|
|
@@ -748,11 +812,11 @@ function getDatasourceResourceCards(resources, locale) {
|
|
|
748
812
|
return [...Array.from(folderGroups.values()).map(({ folder, resources: group }) => ({
|
|
749
813
|
kind: "folder",
|
|
750
814
|
key: `folder-${folder.id}`,
|
|
751
|
-
props: toFolderCardProps(folder, group, locale)
|
|
815
|
+
props: toFolderCardProps(folder, group, locale, getResourceDownloadUrl)
|
|
752
816
|
})), ...ungroupedResources.map((resource) => ({
|
|
753
817
|
kind: "resource",
|
|
754
818
|
key: `resource-${resource.id}`,
|
|
755
|
-
props: toResourceCardProps(resource, locale)
|
|
819
|
+
props: toResourceCardProps(resource, locale, getResourceDownloadUrl)
|
|
756
820
|
}))];
|
|
757
821
|
}
|
|
758
822
|
function DatasourceResourceCardView({ card, variant = "card" }) {
|
|
@@ -765,8 +829,8 @@ function DatasourceResourceCardView({ card, variant = "card" }) {
|
|
|
765
829
|
variant
|
|
766
830
|
});
|
|
767
831
|
}
|
|
768
|
-
function DatasourceToolResources({ resources, locale, className }) {
|
|
769
|
-
const cards = getDatasourceResourceCards(resources, locale);
|
|
832
|
+
function DatasourceToolResources({ resources, locale, className, getResourceDownloadUrl }) {
|
|
833
|
+
const cards = getDatasourceResourceCards(resources, locale, getResourceDownloadUrl);
|
|
770
834
|
return /* @__PURE__ */ jsx("section", {
|
|
771
835
|
"aria-label": "Recursos de la fuente de datos",
|
|
772
836
|
className: joinClasses("flex flex-wrap gap-3", className),
|
|
@@ -1002,8 +1066,8 @@ function getDatasourceResources$2(part) {
|
|
|
1002
1066
|
const resources = part.metadata?.resources;
|
|
1003
1067
|
return Array.isArray(resources) ? resources.filter(isDatasourceResource) : [];
|
|
1004
1068
|
}
|
|
1005
|
-
function getAggregatedCards(parts, locale) {
|
|
1006
|
-
const datasourceCards = parts.filter((part) => part.toolType === "datasource").flatMap((part) => getDatasourceResourceCards(getDatasourceResources$2(part), locale).map((card) => ({
|
|
1069
|
+
function getAggregatedCards(parts, locale, getResourceDownloadUrl) {
|
|
1070
|
+
const datasourceCards = parts.filter((part) => part.toolType === "datasource").flatMap((part) => getDatasourceResourceCards(getDatasourceResources$2(part), locale, getResourceDownloadUrl).map((card) => ({
|
|
1007
1071
|
kind: "datasource",
|
|
1008
1072
|
key: `${part.id}-${card.key}`,
|
|
1009
1073
|
card
|
|
@@ -1038,8 +1102,8 @@ function SourceCard({ source, variant = "card" }) {
|
|
|
1038
1102
|
function getTotalSourceCount(cards) {
|
|
1039
1103
|
return cards.reduce((count, source) => count + (source.kind === "web" ? getWebSearchResults(source.part).length : 1), 0);
|
|
1040
1104
|
}
|
|
1041
|
-
function AggregatedSourceResults({ parts, locale, maxVisible = 3, className }) {
|
|
1042
|
-
const cards = getAggregatedCards(parts, locale);
|
|
1105
|
+
function AggregatedSourceResults({ parts, locale, maxVisible = 3, className, getResourceDownloadUrl }) {
|
|
1106
|
+
const cards = getAggregatedCards(parts, locale, getResourceDownloadUrl);
|
|
1043
1107
|
if (cards.length === 0) return null;
|
|
1044
1108
|
const visibleLimit = Number.isFinite(maxVisible) && maxVisible >= 1 ? Math.floor(maxVisible) : 3;
|
|
1045
1109
|
const visibleCards = cards.slice(0, visibleLimit);
|
|
@@ -2494,7 +2558,7 @@ function getDatasourceResources$1(part) {
|
|
|
2494
2558
|
const resources = part.metadata.resources;
|
|
2495
2559
|
return Array.isArray(resources) ? resources.filter(isDatasourceResource) : [];
|
|
2496
2560
|
}
|
|
2497
|
-
function ToolResultCard({ part, className, detailsOpen = true, locale, onOpenAgentThread }) {
|
|
2561
|
+
function ToolResultCard({ part, className, detailsOpen = true, locale, onOpenAgentThread, getResourceDownloadUrl }) {
|
|
2498
2562
|
if (part.state === "pending") return /* @__PURE__ */ jsx(ToolUseIndicator, {
|
|
2499
2563
|
part,
|
|
2500
2564
|
locale,
|
|
@@ -2547,7 +2611,8 @@ function ToolResultCard({ part, className, detailsOpen = true, locale, onOpenAge
|
|
|
2547
2611
|
if (datasourceResources.length > 0) return /* @__PURE__ */ jsx(DatasourceToolResources, {
|
|
2548
2612
|
resources: datasourceResources,
|
|
2549
2613
|
locale,
|
|
2550
|
-
className
|
|
2614
|
+
className,
|
|
2615
|
+
getResourceDownloadUrl
|
|
2551
2616
|
});
|
|
2552
2617
|
if (part.toolType === "datasource") return null;
|
|
2553
2618
|
return /* @__PURE__ */ jsxs("section", {
|
|
@@ -2755,7 +2820,7 @@ function getMessageTextClassName(role) {
|
|
|
2755
2820
|
function getMessageTextAlignmentClassName(role) {
|
|
2756
2821
|
return role === "user" ? "text-right" : "text-left";
|
|
2757
2822
|
}
|
|
2758
|
-
function renderPart(part, index, message, isStreaming, onOpenAgentThread, locale, renderText, renderToolCall, renderThinking, thinkingLabel, completedThinkingLabel, textClassName) {
|
|
2823
|
+
function renderPart(part, index, message, isStreaming, onOpenAgentThread, getResourceDownloadUrl, locale, renderText, renderToolCall, renderThinking, thinkingLabel, completedThinkingLabel, textClassName) {
|
|
2759
2824
|
if (part.type === "text") {
|
|
2760
2825
|
if (renderText) return /* @__PURE__ */ jsx("div", { children: renderText(part, {
|
|
2761
2826
|
message,
|
|
@@ -2811,7 +2876,8 @@ function renderPart(part, index, message, isStreaming, onOpenAgentThread, locale
|
|
|
2811
2876
|
return /* @__PURE__ */ jsx(ToolResultCard, {
|
|
2812
2877
|
part,
|
|
2813
2878
|
locale,
|
|
2814
|
-
onOpenAgentThread
|
|
2879
|
+
onOpenAgentThread,
|
|
2880
|
+
getResourceDownloadUrl
|
|
2815
2881
|
}, `tool-${part.id}-${index}`);
|
|
2816
2882
|
}
|
|
2817
2883
|
if (part.type === "thinking") {
|
|
@@ -2867,7 +2933,7 @@ function aggregateSourceToolParts(parts, position) {
|
|
|
2867
2933
|
...remainingParts.slice(insertionIndex)
|
|
2868
2934
|
];
|
|
2869
2935
|
}
|
|
2870
|
-
function MessageParts({ message, collapseThreshold = DEFAULT_COLLAPSE_THRESHOLD, sourceToolResultsLayout = "aggregated", sourceToolResultsPosition, maxVisibleSourceCards = 3, datasourceToolResultsPosition, onOpenAgentThread, locale, renderText, renderToolCall, renderThinking, isStreaming = false, thinkingLabel, completedThinkingLabel, readMoreLabel = "Read more", readLessLabel = "Read less", className,...props }) {
|
|
2936
|
+
function MessageParts({ message, collapseThreshold = DEFAULT_COLLAPSE_THRESHOLD, sourceToolResultsLayout = "aggregated", sourceToolResultsPosition, maxVisibleSourceCards = 3, datasourceToolResultsPosition, onOpenAgentThread, getResourceDownloadUrl, locale, renderText, renderToolCall, renderThinking, isStreaming = false, thinkingLabel, completedThinkingLabel, readMoreLabel = "Read more", readLessLabel = "Read less", className,...props }) {
|
|
2871
2937
|
const [isExpanded, setIsExpanded] = useState(false);
|
|
2872
2938
|
const textContent = useMemo(() => message.parts.filter((part) => part.type === "text").map((part) => part.text).join("\n"), [message.parts]);
|
|
2873
2939
|
const canCollapse = message.role === "user" && message.parts.every((part) => part.type === "text") && textContent.length > collapseThreshold;
|
|
@@ -2895,8 +2961,9 @@ function MessageParts({ message, collapseThreshold = DEFAULT_COLLAPSE_THRESHOLD,
|
|
|
2895
2961
|
}) : orderedParts.map((item) => item.kind === "source-results" ? /* @__PURE__ */ jsx(AggregatedSourceResults, {
|
|
2896
2962
|
parts: item.parts,
|
|
2897
2963
|
locale,
|
|
2898
|
-
maxVisible: maxVisibleSourceCards
|
|
2899
|
-
|
|
2964
|
+
maxVisible: maxVisibleSourceCards,
|
|
2965
|
+
getResourceDownloadUrl
|
|
2966
|
+
}, `source-results-${item.index}`) : renderPart(item.part, item.index, message, isStreaming, onOpenAgentThread, getResourceDownloadUrl, locale, renderText, renderToolCall, renderThinking, thinkingLabel, completedThinkingLabel, textClassName)), canCollapse ? /* @__PURE__ */ jsx("button", {
|
|
2900
2967
|
type: "button",
|
|
2901
2968
|
className: "text-xs font-medium text-slate-600 hover:text-slate-900",
|
|
2902
2969
|
onClick: () => setIsExpanded((previous) => !previous),
|
|
@@ -2984,7 +3051,11 @@ function isRoutedMessageProps(props) {
|
|
|
2984
3051
|
}
|
|
2985
3052
|
function Message(props) {
|
|
2986
3053
|
if (!isRoutedMessageProps(props)) return /* @__PURE__ */ jsx(MessageRoot, { ...props });
|
|
2987
|
-
const { message, avatar, footer, messageClassName, contentClassName, partsClassName, toolResultsClassName, toolPartsClassName, messagePartsProps, className,...rootProps } = props;
|
|
3054
|
+
const { message, avatar, footer, messageClassName, contentClassName, partsClassName, toolResultsClassName, toolPartsClassName, messagePartsProps, getResourceDownloadUrl, className,...rootProps } = props;
|
|
3055
|
+
const routedMessagePartsProps = getResourceDownloadUrl ? {
|
|
3056
|
+
...messagePartsProps,
|
|
3057
|
+
getResourceDownloadUrl
|
|
3058
|
+
} : messagePartsProps;
|
|
2988
3059
|
if (message.role === "assistant") return /* @__PURE__ */ jsx(AssistantMessage, {
|
|
2989
3060
|
...rootProps,
|
|
2990
3061
|
className,
|
|
@@ -2996,7 +3067,7 @@ function Message(props) {
|
|
|
2996
3067
|
contentPartsClassName: partsClassName,
|
|
2997
3068
|
toolResultsClassName,
|
|
2998
3069
|
toolPartsClassName,
|
|
2999
|
-
messagePartsProps
|
|
3070
|
+
messagePartsProps: routedMessagePartsProps
|
|
3000
3071
|
});
|
|
3001
3072
|
if (message.role === "user") return /* @__PURE__ */ jsx(UserMessage, {
|
|
3002
3073
|
...rootProps,
|
|
@@ -3006,7 +3077,7 @@ function Message(props) {
|
|
|
3006
3077
|
footer,
|
|
3007
3078
|
contentClassName,
|
|
3008
3079
|
partsClassName,
|
|
3009
|
-
messagePartsProps
|
|
3080
|
+
messagePartsProps: routedMessagePartsProps
|
|
3010
3081
|
});
|
|
3011
3082
|
return /* @__PURE__ */ jsxs(MessageRoot, {
|
|
3012
3083
|
...rootProps,
|
|
@@ -3015,7 +3086,7 @@ function Message(props) {
|
|
|
3015
3086
|
children: [avatar, /* @__PURE__ */ jsxs(MessageContent, {
|
|
3016
3087
|
className: contentClassName,
|
|
3017
3088
|
children: [/* @__PURE__ */ jsx(MessageParts, {
|
|
3018
|
-
...
|
|
3089
|
+
...routedMessagePartsProps,
|
|
3019
3090
|
message,
|
|
3020
3091
|
className: partsClassName
|
|
3021
3092
|
}), footer]
|