@plaisolutions/react 0.23.0 → 0.24.1
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 +56 -0
- package/dist/components/index.d.ts +53 -1
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/index.js +513 -129
- package/dist/components/index.js.map +1 -1
- package/dist/index.d.ts +53 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +513 -129
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/components/index.js
CHANGED
|
@@ -2620,6 +2620,494 @@ function getFileIconClass(extension) {
|
|
|
2620
2620
|
return "text-muted";
|
|
2621
2621
|
}
|
|
2622
2622
|
|
|
2623
|
+
//#endregion
|
|
2624
|
+
//#region src/components/internal/download-file.ts
|
|
2625
|
+
/**
|
|
2626
|
+
* Single entry point for downloading a chat file.
|
|
2627
|
+
*
|
|
2628
|
+
* Fetches the bytes and saves them under the file name, so the browser neither
|
|
2629
|
+
* navigates nor opens a tab. That read needs the file host to allow this origin
|
|
2630
|
+
* (CORS); when it does not, the URL opens in a new tab instead. An HTTP error
|
|
2631
|
+
* (expired, deleted or private object) rejects so the caller can say so.
|
|
2632
|
+
*/
|
|
2633
|
+
async function downloadFile(file) {
|
|
2634
|
+
let response;
|
|
2635
|
+
try {
|
|
2636
|
+
response = await fetch(file.url, { credentials: "omit" });
|
|
2637
|
+
} catch {
|
|
2638
|
+
openInNewTab(file.url);
|
|
2639
|
+
return;
|
|
2640
|
+
}
|
|
2641
|
+
if (!response.ok) throw new Error(`Download failed with status ${response.status}`);
|
|
2642
|
+
saveBlob(await response.blob(), file.name || getFilenameFromUrl(file.url) || "download");
|
|
2643
|
+
}
|
|
2644
|
+
function saveBlob(blob, fileName) {
|
|
2645
|
+
const objectUrl = URL.createObjectURL(blob);
|
|
2646
|
+
const anchor = document.createElement("a");
|
|
2647
|
+
anchor.href = objectUrl;
|
|
2648
|
+
anchor.download = fileName;
|
|
2649
|
+
anchor.rel = "noopener";
|
|
2650
|
+
anchor.style.display = "none";
|
|
2651
|
+
document.body.appendChild(anchor);
|
|
2652
|
+
anchor.click();
|
|
2653
|
+
anchor.remove();
|
|
2654
|
+
window.setTimeout(() => URL.revokeObjectURL(objectUrl), 1e3);
|
|
2655
|
+
}
|
|
2656
|
+
function openInNewTab(url) {
|
|
2657
|
+
window.open(url, "_blank", "noopener,noreferrer");
|
|
2658
|
+
}
|
|
2659
|
+
|
|
2660
|
+
//#endregion
|
|
2661
|
+
//#region src/components/internal/file-download-button.tsx
|
|
2662
|
+
function FileDownloadButton({ file, label, copy, className }) {
|
|
2663
|
+
const [state, setState] = useState("idle");
|
|
2664
|
+
const isDownloading = state === "downloading";
|
|
2665
|
+
const handleClick = async (event) => {
|
|
2666
|
+
event.stopPropagation();
|
|
2667
|
+
if (isDownloading) return;
|
|
2668
|
+
setState("downloading");
|
|
2669
|
+
try {
|
|
2670
|
+
await downloadFile(file);
|
|
2671
|
+
setState("idle");
|
|
2672
|
+
} catch {
|
|
2673
|
+
setState("error");
|
|
2674
|
+
}
|
|
2675
|
+
};
|
|
2676
|
+
return /* @__PURE__ */ jsxs("span", {
|
|
2677
|
+
className: "ml-auto flex shrink-0 items-center gap-2",
|
|
2678
|
+
children: [state === "error" ? /* @__PURE__ */ jsx("span", {
|
|
2679
|
+
role: "alert",
|
|
2680
|
+
className: "text-xs text-danger",
|
|
2681
|
+
children: copy.downloadFailed
|
|
2682
|
+
}) : null, /* @__PURE__ */ jsx("button", {
|
|
2683
|
+
type: "button",
|
|
2684
|
+
onClick: handleClick,
|
|
2685
|
+
disabled: isDownloading,
|
|
2686
|
+
"aria-label": `${isDownloading ? copy.downloading : copy.download}: ${label}`,
|
|
2687
|
+
title: isDownloading ? copy.downloading : copy.download,
|
|
2688
|
+
className: joinClasses("inline-flex h-8 w-8 items-center justify-center rounded-md border border-border bg-surface text-strong transition-colors hover:bg-surface-subtle focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring disabled:cursor-wait disabled:opacity-60", className),
|
|
2689
|
+
children: isDownloading ? /* @__PURE__ */ jsx(LoaderCircle, {
|
|
2690
|
+
className: "h-4 w-4 animate-spin",
|
|
2691
|
+
"aria-hidden": "true"
|
|
2692
|
+
}) : /* @__PURE__ */ jsx(Download, {
|
|
2693
|
+
className: "h-4 w-4",
|
|
2694
|
+
"aria-hidden": "true"
|
|
2695
|
+
})
|
|
2696
|
+
})]
|
|
2697
|
+
});
|
|
2698
|
+
}
|
|
2699
|
+
|
|
2700
|
+
//#endregion
|
|
2701
|
+
//#region src/components/internal/generated-files.tsx
|
|
2702
|
+
/** Keeps the entries of `metadata.media_files` that look like a media file. */
|
|
2703
|
+
function getGeneratedFiles(files) {
|
|
2704
|
+
return Array.isArray(files) ? files.filter((file) => Boolean(file) && typeof file === "object" && typeof file.id === "string") : [];
|
|
2705
|
+
}
|
|
2706
|
+
function getGeneratedFileExtension(file) {
|
|
2707
|
+
return getFileExtension(file.name ?? file.pathname, file.content_type);
|
|
2708
|
+
}
|
|
2709
|
+
function getGeneratedFileLabel(file) {
|
|
2710
|
+
return file.name ?? file.pathname ?? file.id;
|
|
2711
|
+
}
|
|
2712
|
+
/** A generated file with its extension and a download button, for a sheet. */
|
|
2713
|
+
function GeneratedFileRow({ file, copy }) {
|
|
2714
|
+
const extension = getGeneratedFileExtension(file);
|
|
2715
|
+
const label = getGeneratedFileLabel(file);
|
|
2716
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
2717
|
+
className: "flex items-center gap-3 rounded-lg border border-border bg-surface p-3",
|
|
2718
|
+
children: [
|
|
2719
|
+
/* @__PURE__ */ jsx(File, {
|
|
2720
|
+
className: joinClasses("h-8 w-8 shrink-0", getFileIconClass(extension)),
|
|
2721
|
+
"aria-hidden": "true"
|
|
2722
|
+
}),
|
|
2723
|
+
/* @__PURE__ */ jsxs("div", {
|
|
2724
|
+
className: "min-w-0",
|
|
2725
|
+
children: [/* @__PURE__ */ jsx("p", {
|
|
2726
|
+
className: "truncate text-sm font-medium",
|
|
2727
|
+
children: label
|
|
2728
|
+
}), extension ? /* @__PURE__ */ jsx("p", {
|
|
2729
|
+
className: "text-[11px] uppercase text-muted",
|
|
2730
|
+
children: extension
|
|
2731
|
+
}) : null]
|
|
2732
|
+
}),
|
|
2733
|
+
file.url ? /* @__PURE__ */ jsx(FileDownloadButton, {
|
|
2734
|
+
file: {
|
|
2735
|
+
url: file.url,
|
|
2736
|
+
name: file.name
|
|
2737
|
+
},
|
|
2738
|
+
label,
|
|
2739
|
+
copy
|
|
2740
|
+
}) : null
|
|
2741
|
+
]
|
|
2742
|
+
});
|
|
2743
|
+
}
|
|
2744
|
+
|
|
2745
|
+
//#endregion
|
|
2746
|
+
//#region src/components/resolve-generated-file-links.ts
|
|
2747
|
+
/** Nested links deeper than this are left as they are. */
|
|
2748
|
+
const MAX_DEPTH = 8;
|
|
2749
|
+
/** The text of a link or image longer than this is not looked at. */
|
|
2750
|
+
const MAX_LINK_TEXT = 1e3;
|
|
2751
|
+
/** Same for a destination, so a half-written one costs little. */
|
|
2752
|
+
const MAX_DESTINATION = 2e3;
|
|
2753
|
+
const IMAGE_EXTENSIONS = new Set([
|
|
2754
|
+
"png",
|
|
2755
|
+
"jpg",
|
|
2756
|
+
"jpeg",
|
|
2757
|
+
"gif",
|
|
2758
|
+
"webp",
|
|
2759
|
+
"svg",
|
|
2760
|
+
"bmp",
|
|
2761
|
+
"avif"
|
|
2762
|
+
]);
|
|
2763
|
+
const STRICT_SCHEMES = new Set([
|
|
2764
|
+
"sandbox",
|
|
2765
|
+
"file",
|
|
2766
|
+
"attachment"
|
|
2767
|
+
]);
|
|
2768
|
+
const SCHEME = /^([a-zA-Z][a-zA-Z0-9+.-]*):/;
|
|
2769
|
+
const BACKTICK_FENCE_OPEN = /^[ \t>]*(`{3,})([^\n]*)$/;
|
|
2770
|
+
const TILDE_FENCE_OPEN = /^[ \t>]*(~{3,})/;
|
|
2771
|
+
const BACKTICK_FENCE_CLOSE = /^[ \t>]*(`{3,})[ \t]*\r?$/;
|
|
2772
|
+
const TILDE_FENCE_CLOSE = /^[ \t>]*(~{3,})[ \t]*\r?$/;
|
|
2773
|
+
function lineEnd(src, from) {
|
|
2774
|
+
const newline = src.indexOf("\n", from);
|
|
2775
|
+
return newline === -1 ? src.length : newline;
|
|
2776
|
+
}
|
|
2777
|
+
/** The end of the fence that opens at `from` (the whole text if it never closes). */
|
|
2778
|
+
function fenceEnd(src, from) {
|
|
2779
|
+
const first = src.slice(from, lineEnd(src, from));
|
|
2780
|
+
const backticks = BACKTICK_FENCE_OPEN.exec(first.replace(/\r$/, ""));
|
|
2781
|
+
const opening = backticks && !backticks[2].includes("`") ? backticks[1] : TILDE_FENCE_OPEN.exec(first)?.[1] ?? null;
|
|
2782
|
+
if (!opening) return null;
|
|
2783
|
+
const close = opening[0] === "`" ? BACKTICK_FENCE_CLOSE : TILDE_FENCE_CLOSE;
|
|
2784
|
+
let position = lineEnd(src, from) + 1;
|
|
2785
|
+
while (position < src.length) {
|
|
2786
|
+
const end = lineEnd(src, position);
|
|
2787
|
+
const closing = close.exec(src.slice(position, end))?.[1];
|
|
2788
|
+
if (closing && closing.length >= opening.length) return end;
|
|
2789
|
+
position = end + 1;
|
|
2790
|
+
}
|
|
2791
|
+
return src.length;
|
|
2792
|
+
}
|
|
2793
|
+
/** The end of the code span whose opening run of `length` backticks ends at `from`. */
|
|
2794
|
+
function codeSpanEnd(src, from, length) {
|
|
2795
|
+
let index = from;
|
|
2796
|
+
while (index < src.length) {
|
|
2797
|
+
const char = src[index];
|
|
2798
|
+
if (char === "\n") {
|
|
2799
|
+
let next = index + 1;
|
|
2800
|
+
while (src[next] === " " || src[next] === " ") next++;
|
|
2801
|
+
if (next >= src.length || src[next] === "\n") return null;
|
|
2802
|
+
}
|
|
2803
|
+
if (char === "`") {
|
|
2804
|
+
let end = index;
|
|
2805
|
+
while (src[end] === "`") end++;
|
|
2806
|
+
if (end - index === length) return end;
|
|
2807
|
+
index = end;
|
|
2808
|
+
continue;
|
|
2809
|
+
}
|
|
2810
|
+
index++;
|
|
2811
|
+
}
|
|
2812
|
+
return null;
|
|
2813
|
+
}
|
|
2814
|
+
/** Fenced code blocks and code spans, which are never rewritten. */
|
|
2815
|
+
function findProtectedRanges(src) {
|
|
2816
|
+
const ranges = [];
|
|
2817
|
+
let index = 0;
|
|
2818
|
+
let atLineStart = true;
|
|
2819
|
+
while (index < src.length) {
|
|
2820
|
+
if (atLineStart) {
|
|
2821
|
+
atLineStart = false;
|
|
2822
|
+
const end = fenceEnd(src, index);
|
|
2823
|
+
if (end !== null) {
|
|
2824
|
+
ranges.push([index, end]);
|
|
2825
|
+
index = end;
|
|
2826
|
+
continue;
|
|
2827
|
+
}
|
|
2828
|
+
}
|
|
2829
|
+
const char = src[index];
|
|
2830
|
+
if (char === "\n") {
|
|
2831
|
+
atLineStart = true;
|
|
2832
|
+
index++;
|
|
2833
|
+
} else if (char === "\\") index += src[index + 1] === "\n" ? 1 : 2;
|
|
2834
|
+
else if (char === "`") {
|
|
2835
|
+
let runEnd = index;
|
|
2836
|
+
while (src[runEnd] === "`") runEnd++;
|
|
2837
|
+
const end = codeSpanEnd(src, runEnd, runEnd - index);
|
|
2838
|
+
if (end === null) index = runEnd;
|
|
2839
|
+
else {
|
|
2840
|
+
ranges.push([index, end]);
|
|
2841
|
+
index = end;
|
|
2842
|
+
}
|
|
2843
|
+
} else index++;
|
|
2844
|
+
}
|
|
2845
|
+
return ranges;
|
|
2846
|
+
}
|
|
2847
|
+
function rangeAt(context, position) {
|
|
2848
|
+
const { ranges } = context;
|
|
2849
|
+
let low = 0;
|
|
2850
|
+
let high = ranges.length - 1;
|
|
2851
|
+
while (low <= high) {
|
|
2852
|
+
const middle = low + high >> 1;
|
|
2853
|
+
const range = ranges[middle];
|
|
2854
|
+
if (position < range[0]) high = middle - 1;
|
|
2855
|
+
else if (position >= range[1]) low = middle + 1;
|
|
2856
|
+
else return range;
|
|
2857
|
+
}
|
|
2858
|
+
}
|
|
2859
|
+
function isBlank(char) {
|
|
2860
|
+
return char === " " || char === " " || char === "\n" || char === "\r";
|
|
2861
|
+
}
|
|
2862
|
+
function startsLikeGeneratedPath(text$2) {
|
|
2863
|
+
const scheme = SCHEME.exec(text$2)?.[1]?.toLowerCase();
|
|
2864
|
+
return scheme !== void 0 && STRICT_SCHEMES.has(scheme) || text$2.startsWith("/mnt/");
|
|
2865
|
+
}
|
|
2866
|
+
/**
|
|
2867
|
+
* Reads `[text](destination "title")` starting at the `[` at `open`. Returns
|
|
2868
|
+
* null when it is not (yet) a complete inline link, so text that is still
|
|
2869
|
+
* streaming is left as it is.
|
|
2870
|
+
*/
|
|
2871
|
+
function parseLink(context, open, limit) {
|
|
2872
|
+
const { src } = context;
|
|
2873
|
+
const textLimit = Math.min(limit, open + MAX_LINK_TEXT);
|
|
2874
|
+
let depth = 1;
|
|
2875
|
+
let close = open + 1;
|
|
2876
|
+
for (; close < textLimit; close++) {
|
|
2877
|
+
const char = src[close];
|
|
2878
|
+
if (char === "\\") {
|
|
2879
|
+
close++;
|
|
2880
|
+
continue;
|
|
2881
|
+
}
|
|
2882
|
+
const range = rangeAt(context, close);
|
|
2883
|
+
if (range) {
|
|
2884
|
+
close = range[1] - 1;
|
|
2885
|
+
continue;
|
|
2886
|
+
}
|
|
2887
|
+
if (char === "\n") {
|
|
2888
|
+
let next = close + 1;
|
|
2889
|
+
while (src[next] === " " || src[next] === " ") next++;
|
|
2890
|
+
if (src[next] === "\n") return null;
|
|
2891
|
+
} else if (char === "[") depth++;
|
|
2892
|
+
else if (char === "]" && --depth === 0) break;
|
|
2893
|
+
}
|
|
2894
|
+
if (close >= textLimit || src[close] !== "]" || src[close + 1] !== "(") return null;
|
|
2895
|
+
let position = close + 2;
|
|
2896
|
+
while (isBlank(src[position])) position++;
|
|
2897
|
+
const destinationLimit = Math.min(limit, position + MAX_DESTINATION);
|
|
2898
|
+
let destination;
|
|
2899
|
+
let destinationEnd;
|
|
2900
|
+
if (src[position] === "<") {
|
|
2901
|
+
let end$1 = position + 1;
|
|
2902
|
+
while (end$1 < destinationLimit && src[end$1] !== ">" && src[end$1] !== "\n") end$1 += src[end$1] === "\\" ? 2 : 1;
|
|
2903
|
+
if (src[end$1] !== ">") return null;
|
|
2904
|
+
destination = src.slice(position + 1, end$1);
|
|
2905
|
+
destinationEnd = end$1 + 1;
|
|
2906
|
+
} else {
|
|
2907
|
+
let parentheses$1 = 0;
|
|
2908
|
+
let end$1 = position;
|
|
2909
|
+
for (; end$1 < destinationLimit; end$1++) {
|
|
2910
|
+
const char = src[end$1];
|
|
2911
|
+
if (char === "\\") end$1++;
|
|
2912
|
+
else if (isBlank(char)) break;
|
|
2913
|
+
else if (char === "(") parentheses$1++;
|
|
2914
|
+
else if (char === ")") {
|
|
2915
|
+
if (parentheses$1 === 0) break;
|
|
2916
|
+
parentheses$1--;
|
|
2917
|
+
}
|
|
2918
|
+
}
|
|
2919
|
+
destination = src.slice(position, end$1);
|
|
2920
|
+
destinationEnd = end$1;
|
|
2921
|
+
}
|
|
2922
|
+
const textStart = open + 1;
|
|
2923
|
+
let end = destinationEnd;
|
|
2924
|
+
while (isBlank(src[end])) end++;
|
|
2925
|
+
let title = "";
|
|
2926
|
+
const quote = src[end];
|
|
2927
|
+
if (end > destinationEnd && (quote === "\"" || quote === "'" || quote === "(")) {
|
|
2928
|
+
const closing = quote === "(" ? ")" : quote;
|
|
2929
|
+
let titleEnd = end + 1;
|
|
2930
|
+
while (titleEnd < destinationLimit && src[titleEnd] !== closing) titleEnd += src[titleEnd] === "\\" ? 2 : 1;
|
|
2931
|
+
if (src[titleEnd] === closing) {
|
|
2932
|
+
title = src.slice(destinationEnd, titleEnd + 1);
|
|
2933
|
+
end = titleEnd + 1;
|
|
2934
|
+
while (isBlank(src[end])) end++;
|
|
2935
|
+
}
|
|
2936
|
+
}
|
|
2937
|
+
if (src[end] === ")") return {
|
|
2938
|
+
open,
|
|
2939
|
+
textStart,
|
|
2940
|
+
textEnd: close,
|
|
2941
|
+
destination,
|
|
2942
|
+
title,
|
|
2943
|
+
end: end + 1
|
|
2944
|
+
};
|
|
2945
|
+
if (!startsLikeGeneratedPath(src.slice(position, position + 16))) return null;
|
|
2946
|
+
let parentheses = 0;
|
|
2947
|
+
for (let index = position; index < destinationLimit; index++) {
|
|
2948
|
+
const char = src[index];
|
|
2949
|
+
if (char === "\n") return null;
|
|
2950
|
+
if (char === "(") parentheses++;
|
|
2951
|
+
else if (char === ")") {
|
|
2952
|
+
if (parentheses === 0) return {
|
|
2953
|
+
open,
|
|
2954
|
+
textStart,
|
|
2955
|
+
textEnd: close,
|
|
2956
|
+
destination: src.slice(position, index).trim(),
|
|
2957
|
+
title: "",
|
|
2958
|
+
end: index + 1
|
|
2959
|
+
};
|
|
2960
|
+
parentheses--;
|
|
2961
|
+
}
|
|
2962
|
+
}
|
|
2963
|
+
return null;
|
|
2964
|
+
}
|
|
2965
|
+
function decode(segment) {
|
|
2966
|
+
try {
|
|
2967
|
+
return decodeURIComponent(segment);
|
|
2968
|
+
} catch {
|
|
2969
|
+
return segment;
|
|
2970
|
+
}
|
|
2971
|
+
}
|
|
2972
|
+
/** The file name a destination points to, or null when it names none. */
|
|
2973
|
+
function fileNameOf(destination) {
|
|
2974
|
+
const name = decode(destination.replace(SCHEME, "").split(/[?#]/, 1)[0].split("/").pop() ?? "").trim();
|
|
2975
|
+
return name && name !== "." && name !== ".." ? name : null;
|
|
2976
|
+
}
|
|
2977
|
+
function fold(name) {
|
|
2978
|
+
return name.normalize("NFC").toLowerCase();
|
|
2979
|
+
}
|
|
2980
|
+
function findFile(files, name) {
|
|
2981
|
+
const exact = files.find((file) => file.name === name);
|
|
2982
|
+
if (exact) return exact;
|
|
2983
|
+
const folded = fold(name);
|
|
2984
|
+
return files.find((file) => fold(file.name) === folded);
|
|
2985
|
+
}
|
|
2986
|
+
function decide(destination, files) {
|
|
2987
|
+
const target = destination.trim();
|
|
2988
|
+
if (!target) return { kind: "keep" };
|
|
2989
|
+
const scheme = SCHEME.exec(target)?.[1]?.toLowerCase();
|
|
2990
|
+
let strict = false;
|
|
2991
|
+
if (scheme !== void 0) {
|
|
2992
|
+
if (!STRICT_SCHEMES.has(scheme)) return { kind: "keep" };
|
|
2993
|
+
strict = true;
|
|
2994
|
+
} else if (target.startsWith("/mnt/")) strict = true;
|
|
2995
|
+
else if (/^[/#?]/.test(target)) return { kind: "keep" };
|
|
2996
|
+
const name = fileNameOf(target);
|
|
2997
|
+
const file = name ? findFile(files, name) : void 0;
|
|
2998
|
+
if (file) return {
|
|
2999
|
+
kind: "file",
|
|
3000
|
+
file
|
|
3001
|
+
};
|
|
3002
|
+
return strict ? { kind: "strip" } : { kind: "keep" };
|
|
3003
|
+
}
|
|
3004
|
+
function isImageFile(file) {
|
|
3005
|
+
const type = file.contentType?.toLowerCase().trim();
|
|
3006
|
+
if (type?.startsWith("image/")) return true;
|
|
3007
|
+
if (type && type !== "application/octet-stream") return false;
|
|
3008
|
+
const extension = file.name.split(".").pop()?.toLowerCase() ?? "";
|
|
3009
|
+
return IMAGE_EXTENSIONS.has(extension);
|
|
3010
|
+
}
|
|
3011
|
+
/** A destination that survives markdown parsing whatever the URL contains. */
|
|
3012
|
+
function formatDestination(url) {
|
|
3013
|
+
const safe = url.replace(/[\s<>]/g, (char) => encodeURIComponent(char));
|
|
3014
|
+
return /[()]/.test(safe) ? `<${safe}>` : safe;
|
|
3015
|
+
}
|
|
3016
|
+
function rewrite(context, from, to, depth) {
|
|
3017
|
+
const { src } = context;
|
|
3018
|
+
let out = "";
|
|
3019
|
+
let copied = from;
|
|
3020
|
+
let index = from;
|
|
3021
|
+
while (index < to) {
|
|
3022
|
+
const char = src[index];
|
|
3023
|
+
if (char === "\\") {
|
|
3024
|
+
index += 2;
|
|
3025
|
+
continue;
|
|
3026
|
+
}
|
|
3027
|
+
const range = rangeAt(context, index);
|
|
3028
|
+
if (range) {
|
|
3029
|
+
index = range[1];
|
|
3030
|
+
continue;
|
|
3031
|
+
}
|
|
3032
|
+
const isImage = char === "!" && src[index + 1] === "[";
|
|
3033
|
+
if (char !== "[" && !isImage) {
|
|
3034
|
+
index++;
|
|
3035
|
+
continue;
|
|
3036
|
+
}
|
|
3037
|
+
const open = isImage ? index + 1 : index;
|
|
3038
|
+
const link = depth < MAX_DEPTH ? parseLink(context, open, to) : null;
|
|
3039
|
+
if (!link) {
|
|
3040
|
+
index = open + 1;
|
|
3041
|
+
continue;
|
|
3042
|
+
}
|
|
3043
|
+
const text$2 = rewrite(context, link.textStart, link.textEnd, depth + 1);
|
|
3044
|
+
const decision = decide(link.destination, context.files);
|
|
3045
|
+
let replacement;
|
|
3046
|
+
if (decision.kind === "keep") replacement = `${src.slice(index, link.textStart)}${text$2}${src.slice(link.textEnd, link.end)}`;
|
|
3047
|
+
else if (decision.kind === "strip") replacement = text$2;
|
|
3048
|
+
else {
|
|
3049
|
+
const { file } = decision;
|
|
3050
|
+
const destination = `${formatDestination(file.url)}${link.title}`;
|
|
3051
|
+
if (isImage && isImageFile(file)) replacement = ``;
|
|
3052
|
+
else if (isImage) replacement = `[${text$2.trim() ? text$2 : file.name}](${destination})`;
|
|
3053
|
+
else replacement = `[${text$2}](${destination})`;
|
|
3054
|
+
}
|
|
3055
|
+
out += src.slice(copied, index) + replacement;
|
|
3056
|
+
copied = link.end;
|
|
3057
|
+
index = link.end;
|
|
3058
|
+
}
|
|
3059
|
+
return out + src.slice(copied, to);
|
|
3060
|
+
}
|
|
3061
|
+
/**
|
|
3062
|
+
* Points the markdown links and images the model wrote to the files a tool
|
|
3063
|
+
* generated at the URL the file can really be downloaded from.
|
|
3064
|
+
*
|
|
3065
|
+
* The model often writes `` or
|
|
3066
|
+
* `[report](/mnt/data/report.xlsx)`, paths that exist only in the sandbox, and
|
|
3067
|
+
* the renderer blocks them. A destination is looked at when it has the
|
|
3068
|
+
* `sandbox:`, `file:` or `attachment:` scheme, starts with `/mnt/` or is a
|
|
3069
|
+
* relative path; `http(s):`, `mailto:`, anchors and every other destination are
|
|
3070
|
+
* never touched. Its file name (without query or fragment, percent-decoded) is
|
|
3071
|
+
* matched to `files` by name, ignoring case if needed. A match becomes that
|
|
3072
|
+
* file's `url`: an image of a file that is not an image turns into a link. A
|
|
3073
|
+
* `sandbox:`, `file:`, `attachment:` or `/mnt/` destination with no matching file
|
|
3074
|
+
* is removed, leaving its text (the alt of an image), so no broken link is
|
|
3075
|
+
* left; a relative path with no match stays as written.
|
|
3076
|
+
*
|
|
3077
|
+
* Code blocks and code spans are not changed. Links that are not complete yet,
|
|
3078
|
+
* as in text that is still streaming, are left as they are, and the result can
|
|
3079
|
+
* go through the function again unchanged.
|
|
3080
|
+
*/
|
|
3081
|
+
function resolveGeneratedFileLinks(markdown, files) {
|
|
3082
|
+
if (!markdown.includes("](")) return markdown;
|
|
3083
|
+
return rewrite({
|
|
3084
|
+
src: markdown,
|
|
3085
|
+
ranges: findProtectedRanges(markdown),
|
|
3086
|
+
files: files.filter((file) => file.name && file.url)
|
|
3087
|
+
}, 0, markdown.length, 0);
|
|
3088
|
+
}
|
|
3089
|
+
/**
|
|
3090
|
+
* The files generated by the code_execution and office_documents tool calls of
|
|
3091
|
+
* a message, with the same `url` the cards use to download them.
|
|
3092
|
+
*/
|
|
3093
|
+
function getMessageGeneratedFiles(message) {
|
|
3094
|
+
const targets = [];
|
|
3095
|
+
for (const part of message.parts) {
|
|
3096
|
+
if (part.type !== "tool-call" || part.toolType !== "code_execution" && part.toolType !== "office_documents") continue;
|
|
3097
|
+
for (const file of getGeneratedFiles(part.metadata?.media_files)) {
|
|
3098
|
+
if (!file.url) continue;
|
|
3099
|
+
const name = file.name ?? getFilenameFromUrl(file.pathname ?? "") ?? getFilenameFromUrl(file.url);
|
|
3100
|
+
if (!name) continue;
|
|
3101
|
+
targets.push({
|
|
3102
|
+
name,
|
|
3103
|
+
url: file.url,
|
|
3104
|
+
contentType: file.content_type
|
|
3105
|
+
});
|
|
3106
|
+
}
|
|
3107
|
+
}
|
|
3108
|
+
return targets;
|
|
3109
|
+
}
|
|
3110
|
+
|
|
2623
3111
|
//#endregion
|
|
2624
3112
|
//#region src/components/memory-proposal-card.tsx
|
|
2625
3113
|
const COPY$1 = {
|
|
@@ -3374,128 +3862,6 @@ function ToolResultBrowserCard({ part, className, locale }) {
|
|
|
3374
3862
|
});
|
|
3375
3863
|
}
|
|
3376
3864
|
|
|
3377
|
-
//#endregion
|
|
3378
|
-
//#region src/components/internal/download-file.ts
|
|
3379
|
-
/**
|
|
3380
|
-
* Single entry point for downloading a chat file.
|
|
3381
|
-
*
|
|
3382
|
-
* Fetches the bytes and saves them under the file name, so the browser neither
|
|
3383
|
-
* navigates nor opens a tab. That read needs the file host to allow this origin
|
|
3384
|
-
* (CORS); when it does not, the URL opens in a new tab instead. An HTTP error
|
|
3385
|
-
* (expired, deleted or private object) rejects so the caller can say so.
|
|
3386
|
-
*/
|
|
3387
|
-
async function downloadFile(file) {
|
|
3388
|
-
let response;
|
|
3389
|
-
try {
|
|
3390
|
-
response = await fetch(file.url, { credentials: "omit" });
|
|
3391
|
-
} catch {
|
|
3392
|
-
openInNewTab(file.url);
|
|
3393
|
-
return;
|
|
3394
|
-
}
|
|
3395
|
-
if (!response.ok) throw new Error(`Download failed with status ${response.status}`);
|
|
3396
|
-
saveBlob(await response.blob(), file.name || getFilenameFromUrl(file.url) || "download");
|
|
3397
|
-
}
|
|
3398
|
-
function saveBlob(blob, fileName) {
|
|
3399
|
-
const objectUrl = URL.createObjectURL(blob);
|
|
3400
|
-
const anchor = document.createElement("a");
|
|
3401
|
-
anchor.href = objectUrl;
|
|
3402
|
-
anchor.download = fileName;
|
|
3403
|
-
anchor.rel = "noopener";
|
|
3404
|
-
anchor.style.display = "none";
|
|
3405
|
-
document.body.appendChild(anchor);
|
|
3406
|
-
anchor.click();
|
|
3407
|
-
anchor.remove();
|
|
3408
|
-
window.setTimeout(() => URL.revokeObjectURL(objectUrl), 1e3);
|
|
3409
|
-
}
|
|
3410
|
-
function openInNewTab(url) {
|
|
3411
|
-
window.open(url, "_blank", "noopener,noreferrer");
|
|
3412
|
-
}
|
|
3413
|
-
|
|
3414
|
-
//#endregion
|
|
3415
|
-
//#region src/components/internal/file-download-button.tsx
|
|
3416
|
-
function FileDownloadButton({ file, label, copy, className }) {
|
|
3417
|
-
const [state, setState] = useState("idle");
|
|
3418
|
-
const isDownloading = state === "downloading";
|
|
3419
|
-
const handleClick = async (event) => {
|
|
3420
|
-
event.stopPropagation();
|
|
3421
|
-
if (isDownloading) return;
|
|
3422
|
-
setState("downloading");
|
|
3423
|
-
try {
|
|
3424
|
-
await downloadFile(file);
|
|
3425
|
-
setState("idle");
|
|
3426
|
-
} catch {
|
|
3427
|
-
setState("error");
|
|
3428
|
-
}
|
|
3429
|
-
};
|
|
3430
|
-
return /* @__PURE__ */ jsxs("span", {
|
|
3431
|
-
className: "ml-auto flex shrink-0 items-center gap-2",
|
|
3432
|
-
children: [state === "error" ? /* @__PURE__ */ jsx("span", {
|
|
3433
|
-
role: "alert",
|
|
3434
|
-
className: "text-xs text-danger",
|
|
3435
|
-
children: copy.downloadFailed
|
|
3436
|
-
}) : null, /* @__PURE__ */ jsx("button", {
|
|
3437
|
-
type: "button",
|
|
3438
|
-
onClick: handleClick,
|
|
3439
|
-
disabled: isDownloading,
|
|
3440
|
-
"aria-label": `${isDownloading ? copy.downloading : copy.download}: ${label}`,
|
|
3441
|
-
title: isDownloading ? copy.downloading : copy.download,
|
|
3442
|
-
className: joinClasses("inline-flex h-8 w-8 items-center justify-center rounded-md border border-border bg-surface text-strong transition-colors hover:bg-surface-subtle focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring disabled:cursor-wait disabled:opacity-60", className),
|
|
3443
|
-
children: isDownloading ? /* @__PURE__ */ jsx(LoaderCircle, {
|
|
3444
|
-
className: "h-4 w-4 animate-spin",
|
|
3445
|
-
"aria-hidden": "true"
|
|
3446
|
-
}) : /* @__PURE__ */ jsx(Download, {
|
|
3447
|
-
className: "h-4 w-4",
|
|
3448
|
-
"aria-hidden": "true"
|
|
3449
|
-
})
|
|
3450
|
-
})]
|
|
3451
|
-
});
|
|
3452
|
-
}
|
|
3453
|
-
|
|
3454
|
-
//#endregion
|
|
3455
|
-
//#region src/components/internal/generated-files.tsx
|
|
3456
|
-
/** Keeps the entries of `metadata.media_files` that look like a media file. */
|
|
3457
|
-
function getGeneratedFiles(files) {
|
|
3458
|
-
return Array.isArray(files) ? files.filter((file) => Boolean(file) && typeof file === "object" && typeof file.id === "string") : [];
|
|
3459
|
-
}
|
|
3460
|
-
function getGeneratedFileExtension(file) {
|
|
3461
|
-
return getFileExtension(file.name ?? file.pathname, file.content_type);
|
|
3462
|
-
}
|
|
3463
|
-
function getGeneratedFileLabel(file) {
|
|
3464
|
-
return file.name ?? file.pathname ?? file.id;
|
|
3465
|
-
}
|
|
3466
|
-
/** A generated file with its extension and a download button, for a sheet. */
|
|
3467
|
-
function GeneratedFileRow({ file, copy }) {
|
|
3468
|
-
const extension = getGeneratedFileExtension(file);
|
|
3469
|
-
const label = getGeneratedFileLabel(file);
|
|
3470
|
-
return /* @__PURE__ */ jsxs("div", {
|
|
3471
|
-
className: "flex items-center gap-3 rounded-lg border border-border bg-surface p-3",
|
|
3472
|
-
children: [
|
|
3473
|
-
/* @__PURE__ */ jsx(File, {
|
|
3474
|
-
className: joinClasses("h-8 w-8 shrink-0", getFileIconClass(extension)),
|
|
3475
|
-
"aria-hidden": "true"
|
|
3476
|
-
}),
|
|
3477
|
-
/* @__PURE__ */ jsxs("div", {
|
|
3478
|
-
className: "min-w-0",
|
|
3479
|
-
children: [/* @__PURE__ */ jsx("p", {
|
|
3480
|
-
className: "truncate text-sm font-medium",
|
|
3481
|
-
children: label
|
|
3482
|
-
}), extension ? /* @__PURE__ */ jsx("p", {
|
|
3483
|
-
className: "text-[11px] uppercase text-muted",
|
|
3484
|
-
children: extension
|
|
3485
|
-
}) : null]
|
|
3486
|
-
}),
|
|
3487
|
-
file.url ? /* @__PURE__ */ jsx(FileDownloadButton, {
|
|
3488
|
-
file: {
|
|
3489
|
-
url: file.url,
|
|
3490
|
-
name: file.name
|
|
3491
|
-
},
|
|
3492
|
-
label,
|
|
3493
|
-
copy
|
|
3494
|
-
}) : null
|
|
3495
|
-
]
|
|
3496
|
-
});
|
|
3497
|
-
}
|
|
3498
|
-
|
|
3499
3865
|
//#endregion
|
|
3500
3866
|
//#region src/components/tool-result-code-execution-card.tsx
|
|
3501
3867
|
const STEP_ICONS = {
|
|
@@ -5942,7 +6308,7 @@ function PendingConfirmationFooter({ pending, toolConfirmation, copy }) {
|
|
|
5942
6308
|
]
|
|
5943
6309
|
}) : null;
|
|
5944
6310
|
}
|
|
5945
|
-
const decide = async (decisions) => {
|
|
6311
|
+
const decide$1 = async (decisions) => {
|
|
5946
6312
|
setFailed(false);
|
|
5947
6313
|
setApplying(false);
|
|
5948
6314
|
try {
|
|
@@ -5958,7 +6324,7 @@ function PendingConfirmationFooter({ pending, toolConfirmation, copy }) {
|
|
|
5958
6324
|
};
|
|
5959
6325
|
const send = async (decision) => {
|
|
5960
6326
|
setSending(decision);
|
|
5961
|
-
await decide([{
|
|
6327
|
+
await decide$1([{
|
|
5962
6328
|
action_id: pending.action_id,
|
|
5963
6329
|
decision
|
|
5964
6330
|
}]);
|
|
@@ -5970,7 +6336,7 @@ function PendingConfirmationFooter({ pending, toolConfirmation, copy }) {
|
|
|
5970
6336
|
setServerErrors({});
|
|
5971
6337
|
if (Object.keys(errors).length > 0) return;
|
|
5972
6338
|
setSending("confirm");
|
|
5973
|
-
await decide([{
|
|
6339
|
+
await decide$1([{
|
|
5974
6340
|
action_id: pending.action_id,
|
|
5975
6341
|
decision: "confirm",
|
|
5976
6342
|
...Object.keys(patch).length > 0 ? { arguments: patch } : {}
|
|
@@ -7685,7 +8051,7 @@ function aggregateSourceToolParts(parts, position) {
|
|
|
7685
8051
|
...remainingParts.slice(insertionIndex)
|
|
7686
8052
|
];
|
|
7687
8053
|
}
|
|
7688
|
-
function MessageParts({ message, collapseThreshold = DEFAULT_COLLAPSE_THRESHOLD, sourceToolResultsLayout = "aggregated", sourceToolResultsPosition, maxVisibleSourceCards = 3, datasourceToolResultsPosition, onOpenAgentThread, getResourceDownloadUrl, locale, memoryProposal, toolConfirmation, renderText, renderToolCall, renderThinking, isStreaming = false, thinkingLabel, completedThinkingLabel, readMoreLabel, readLessLabel, className,...props }) {
|
|
8054
|
+
function MessageParts({ message, collapseThreshold = DEFAULT_COLLAPSE_THRESHOLD, sourceToolResultsLayout = "aggregated", sourceToolResultsPosition, maxVisibleSourceCards = 3, datasourceToolResultsPosition, onOpenAgentThread, getResourceDownloadUrl, locale, memoryProposal, toolConfirmation, renderText, renderToolCall, renderThinking, isStreaming = false, resolveFileLinks = true, generatedFiles, thinkingLabel, completedThinkingLabel, readMoreLabel, readLessLabel, className,...props }) {
|
|
7689
8055
|
const copy = getUiCopy(locale);
|
|
7690
8056
|
const [isExpanded, setIsExpanded] = useState(false);
|
|
7691
8057
|
const textContent = useMemo(() => message.parts.filter((part) => part.type === "text").map((part) => part.text).join("\n"), [message.parts]);
|
|
@@ -7695,12 +8061,28 @@ function MessageParts({ message, collapseThreshold = DEFAULT_COLLAPSE_THRESHOLD,
|
|
|
7695
8061
|
const textAlignmentClassName = getMessageTextAlignmentClassName(message.role);
|
|
7696
8062
|
const resolvedSourceResultsPosition = sourceToolResultsPosition ?? datasourceToolResultsPosition ?? (sourceToolResultsLayout === "aggregated" && !renderToolCall ? "before-content" : "inline");
|
|
7697
8063
|
const shouldAggregateSourceResults = message.role === "assistant" && sourceToolResultsLayout === "aggregated" && !renderToolCall;
|
|
7698
|
-
const
|
|
8064
|
+
const displayParts = useMemo(() => {
|
|
8065
|
+
if (!resolveFileLinks || message.role !== "assistant") return message.parts;
|
|
8066
|
+
const files = generatedFiles ?? getMessageGeneratedFiles(message);
|
|
8067
|
+
return message.parts.map((part) => {
|
|
8068
|
+
if (part.type !== "text") return part;
|
|
8069
|
+
const text$2 = resolveGeneratedFileLinks(part.text, files);
|
|
8070
|
+
return text$2 === part.text ? part : {
|
|
8071
|
+
...part,
|
|
8072
|
+
text: text$2
|
|
8073
|
+
};
|
|
8074
|
+
});
|
|
8075
|
+
}, [
|
|
8076
|
+
message,
|
|
8077
|
+
resolveFileLinks,
|
|
8078
|
+
generatedFiles
|
|
8079
|
+
]);
|
|
8080
|
+
const orderedParts = useMemo(() => shouldAggregateSourceResults ? aggregateSourceToolParts(displayParts, resolvedSourceResultsPosition) : orderMessageParts(displayParts, resolvedSourceResultsPosition).map(({ part, index }) => ({
|
|
7699
8081
|
kind: "part",
|
|
7700
8082
|
part,
|
|
7701
8083
|
index
|
|
7702
8084
|
})), [
|
|
7703
|
-
|
|
8085
|
+
displayParts,
|
|
7704
8086
|
resolvedSourceResultsPosition,
|
|
7705
8087
|
shouldAggregateSourceResults
|
|
7706
8088
|
]);
|
|
@@ -7749,6 +8131,7 @@ function AssistantMessage({ message, avatar, footer, messageClassName, contentCl
|
|
|
7749
8131
|
const contentParts = message.parts.filter((part) => part.type !== "tool-call");
|
|
7750
8132
|
const renderableToolParts = toolParts.filter((part) => isRenderableToolPart(part, messagePartsProps?.locale, Boolean(messagePartsProps?.renderToolCall), Boolean(messagePartsProps?.memoryProposal)));
|
|
7751
8133
|
const hasContentRow = contentParts.length > 0 || footer !== void 0;
|
|
8134
|
+
const generatedFiles = messagePartsProps?.generatedFiles ?? (messagePartsProps?.resolveFileLinks === false ? void 0 : getMessageGeneratedFiles(message));
|
|
7752
8135
|
return /* @__PURE__ */ jsxs("div", {
|
|
7753
8136
|
className: joinClasses("plai-assistant-message flex w-full flex-col gap-2", className),
|
|
7754
8137
|
...props,
|
|
@@ -7772,6 +8155,7 @@ function AssistantMessage({ message, avatar, footer, messageClassName, contentCl
|
|
|
7772
8155
|
className: contentClassName,
|
|
7773
8156
|
children: [contentParts.length > 0 ? /* @__PURE__ */ jsx(MessageParts, {
|
|
7774
8157
|
...messagePartsProps,
|
|
8158
|
+
generatedFiles,
|
|
7775
8159
|
message: withParts(message, contentParts),
|
|
7776
8160
|
className: contentPartsClassName
|
|
7777
8161
|
}) : null, footer]
|
|
@@ -8617,5 +9001,5 @@ function ConversationFilesSheet({ messages, locale, className, open, onOpenChang
|
|
|
8617
9001
|
}
|
|
8618
9002
|
|
|
8619
9003
|
//#endregion
|
|
8620
|
-
export { AggregatedSourceResults, ArrowUp, AssistantMessage, ChatError, Clipboard, ConfirmationBadge, ConversationFilesList, ConversationFilesSheet, DatasourceFolderCard, DatasourceToolResources, DatasourceToolResultCard, Loader, MemoryProposalCard, Message, MessageAvatar, MessageContent, MessageFooter, MessageHeader, MessageParts, MessageRoot, Microphone, PROMPT_FORM_FILE_ACCEPT, Paperclip, PendingConfirmationFooter, PromptForm, PromptFormAttachButton, PromptFormIconButton, Reload, ResourceCard, Sheet, SheetClose, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetTitle, SheetTrigger, SpeechToTextToggle, Stop, Thinking, ThumbDown, ThumbUp, ToolError, ToolResultAgentInvocationCard, ToolResultBrowserCard, ToolResultCard, ToolResultCodeExecutionCard, ToolResultExternalDatasourceCard, ToolResultHttpRequestCard, ToolResultMcpCard, ToolResultOfficeDocumentsCard, ToolResultPendingConfirmationCard, ToolResultPersonalConnectorCard, ToolResultToolConfirmationCard, ToolResultWebSearchCard, ToolResultWorkflowDispatchCard, ToolUseIndicator, UserMessage, X, dummyTranscribeAudio, getConversationFiles, getLocalizedOpenGraphValue, getPendingConfirmation, getResourceDescription, getResourceIcon, getResourceOpenGraphImage, getResourceOpenGraphType, getResourceTitle, getResourceType, getResourceUrl, getToolUseLabel, hasPersonalConnectorView, hasToolConfirmationView, isAllowedPromptFormFile, partitionPromptFormFiles, reasonLabel, useSpeechToText };
|
|
9004
|
+
export { AggregatedSourceResults, ArrowUp, AssistantMessage, ChatError, Clipboard, ConfirmationBadge, ConversationFilesList, ConversationFilesSheet, DatasourceFolderCard, DatasourceToolResources, DatasourceToolResultCard, Loader, MemoryProposalCard, Message, MessageAvatar, MessageContent, MessageFooter, MessageHeader, MessageParts, MessageRoot, Microphone, PROMPT_FORM_FILE_ACCEPT, Paperclip, PendingConfirmationFooter, PromptForm, PromptFormAttachButton, PromptFormIconButton, Reload, ResourceCard, Sheet, SheetClose, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetTitle, SheetTrigger, SpeechToTextToggle, Stop, Thinking, ThumbDown, ThumbUp, ToolError, ToolResultAgentInvocationCard, ToolResultBrowserCard, ToolResultCard, ToolResultCodeExecutionCard, ToolResultExternalDatasourceCard, ToolResultHttpRequestCard, ToolResultMcpCard, ToolResultOfficeDocumentsCard, ToolResultPendingConfirmationCard, ToolResultPersonalConnectorCard, ToolResultToolConfirmationCard, ToolResultWebSearchCard, ToolResultWorkflowDispatchCard, ToolUseIndicator, UserMessage, X, dummyTranscribeAudio, getConversationFiles, getLocalizedOpenGraphValue, getMessageGeneratedFiles, getPendingConfirmation, getResourceDescription, getResourceIcon, getResourceOpenGraphImage, getResourceOpenGraphType, getResourceTitle, getResourceType, getResourceUrl, getToolUseLabel, hasPersonalConnectorView, hasToolConfirmationView, isAllowedPromptFormFile, partitionPromptFormFiles, reasonLabel, resolveGeneratedFileLinks, useSpeechToText };
|
|
8621
9005
|
//# sourceMappingURL=index.js.map
|