@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/index.js
CHANGED
|
@@ -2812,6 +2812,494 @@ function getFileIconClass(extension) {
|
|
|
2812
2812
|
return "text-muted";
|
|
2813
2813
|
}
|
|
2814
2814
|
|
|
2815
|
+
//#endregion
|
|
2816
|
+
//#region src/components/internal/download-file.ts
|
|
2817
|
+
/**
|
|
2818
|
+
* Single entry point for downloading a chat file.
|
|
2819
|
+
*
|
|
2820
|
+
* Fetches the bytes and saves them under the file name, so the browser neither
|
|
2821
|
+
* navigates nor opens a tab. That read needs the file host to allow this origin
|
|
2822
|
+
* (CORS); when it does not, the URL opens in a new tab instead. An HTTP error
|
|
2823
|
+
* (expired, deleted or private object) rejects so the caller can say so.
|
|
2824
|
+
*/
|
|
2825
|
+
async function downloadFile(file) {
|
|
2826
|
+
let response;
|
|
2827
|
+
try {
|
|
2828
|
+
response = await fetch(file.url, { credentials: "omit" });
|
|
2829
|
+
} catch {
|
|
2830
|
+
openInNewTab(file.url);
|
|
2831
|
+
return;
|
|
2832
|
+
}
|
|
2833
|
+
if (!response.ok) throw new Error(`Download failed with status ${response.status}`);
|
|
2834
|
+
saveBlob(await response.blob(), file.name || getFilenameFromUrl(file.url) || "download");
|
|
2835
|
+
}
|
|
2836
|
+
function saveBlob(blob, fileName) {
|
|
2837
|
+
const objectUrl = URL.createObjectURL(blob);
|
|
2838
|
+
const anchor = document.createElement("a");
|
|
2839
|
+
anchor.href = objectUrl;
|
|
2840
|
+
anchor.download = fileName;
|
|
2841
|
+
anchor.rel = "noopener";
|
|
2842
|
+
anchor.style.display = "none";
|
|
2843
|
+
document.body.appendChild(anchor);
|
|
2844
|
+
anchor.click();
|
|
2845
|
+
anchor.remove();
|
|
2846
|
+
window.setTimeout(() => URL.revokeObjectURL(objectUrl), 1e3);
|
|
2847
|
+
}
|
|
2848
|
+
function openInNewTab(url) {
|
|
2849
|
+
window.open(url, "_blank", "noopener,noreferrer");
|
|
2850
|
+
}
|
|
2851
|
+
|
|
2852
|
+
//#endregion
|
|
2853
|
+
//#region src/components/internal/file-download-button.tsx
|
|
2854
|
+
function FileDownloadButton({ file, label, copy, className }) {
|
|
2855
|
+
const [state, setState] = useState("idle");
|
|
2856
|
+
const isDownloading = state === "downloading";
|
|
2857
|
+
const handleClick = async (event) => {
|
|
2858
|
+
event.stopPropagation();
|
|
2859
|
+
if (isDownloading) return;
|
|
2860
|
+
setState("downloading");
|
|
2861
|
+
try {
|
|
2862
|
+
await downloadFile(file);
|
|
2863
|
+
setState("idle");
|
|
2864
|
+
} catch {
|
|
2865
|
+
setState("error");
|
|
2866
|
+
}
|
|
2867
|
+
};
|
|
2868
|
+
return /* @__PURE__ */ jsxs("span", {
|
|
2869
|
+
className: "ml-auto flex shrink-0 items-center gap-2",
|
|
2870
|
+
children: [state === "error" ? /* @__PURE__ */ jsx("span", {
|
|
2871
|
+
role: "alert",
|
|
2872
|
+
className: "text-xs text-danger",
|
|
2873
|
+
children: copy.downloadFailed
|
|
2874
|
+
}) : null, /* @__PURE__ */ jsx("button", {
|
|
2875
|
+
type: "button",
|
|
2876
|
+
onClick: handleClick,
|
|
2877
|
+
disabled: isDownloading,
|
|
2878
|
+
"aria-label": `${isDownloading ? copy.downloading : copy.download}: ${label}`,
|
|
2879
|
+
title: isDownloading ? copy.downloading : copy.download,
|
|
2880
|
+
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),
|
|
2881
|
+
children: isDownloading ? /* @__PURE__ */ jsx(LoaderCircle, {
|
|
2882
|
+
className: "h-4 w-4 animate-spin",
|
|
2883
|
+
"aria-hidden": "true"
|
|
2884
|
+
}) : /* @__PURE__ */ jsx(Download, {
|
|
2885
|
+
className: "h-4 w-4",
|
|
2886
|
+
"aria-hidden": "true"
|
|
2887
|
+
})
|
|
2888
|
+
})]
|
|
2889
|
+
});
|
|
2890
|
+
}
|
|
2891
|
+
|
|
2892
|
+
//#endregion
|
|
2893
|
+
//#region src/components/internal/generated-files.tsx
|
|
2894
|
+
/** Keeps the entries of `metadata.media_files` that look like a media file. */
|
|
2895
|
+
function getGeneratedFiles(files) {
|
|
2896
|
+
return Array.isArray(files) ? files.filter((file) => Boolean(file) && typeof file === "object" && typeof file.id === "string") : [];
|
|
2897
|
+
}
|
|
2898
|
+
function getGeneratedFileExtension(file) {
|
|
2899
|
+
return getFileExtension(file.name ?? file.pathname, file.content_type);
|
|
2900
|
+
}
|
|
2901
|
+
function getGeneratedFileLabel(file) {
|
|
2902
|
+
return file.name ?? file.pathname ?? file.id;
|
|
2903
|
+
}
|
|
2904
|
+
/** A generated file with its extension and a download button, for a sheet. */
|
|
2905
|
+
function GeneratedFileRow({ file, copy }) {
|
|
2906
|
+
const extension = getGeneratedFileExtension(file);
|
|
2907
|
+
const label = getGeneratedFileLabel(file);
|
|
2908
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
2909
|
+
className: "flex items-center gap-3 rounded-lg border border-border bg-surface p-3",
|
|
2910
|
+
children: [
|
|
2911
|
+
/* @__PURE__ */ jsx(File, {
|
|
2912
|
+
className: joinClasses("h-8 w-8 shrink-0", getFileIconClass(extension)),
|
|
2913
|
+
"aria-hidden": "true"
|
|
2914
|
+
}),
|
|
2915
|
+
/* @__PURE__ */ jsxs("div", {
|
|
2916
|
+
className: "min-w-0",
|
|
2917
|
+
children: [/* @__PURE__ */ jsx("p", {
|
|
2918
|
+
className: "truncate text-sm font-medium",
|
|
2919
|
+
children: label
|
|
2920
|
+
}), extension ? /* @__PURE__ */ jsx("p", {
|
|
2921
|
+
className: "text-[11px] uppercase text-muted",
|
|
2922
|
+
children: extension
|
|
2923
|
+
}) : null]
|
|
2924
|
+
}),
|
|
2925
|
+
file.url ? /* @__PURE__ */ jsx(FileDownloadButton, {
|
|
2926
|
+
file: {
|
|
2927
|
+
url: file.url,
|
|
2928
|
+
name: file.name
|
|
2929
|
+
},
|
|
2930
|
+
label,
|
|
2931
|
+
copy
|
|
2932
|
+
}) : null
|
|
2933
|
+
]
|
|
2934
|
+
});
|
|
2935
|
+
}
|
|
2936
|
+
|
|
2937
|
+
//#endregion
|
|
2938
|
+
//#region src/components/resolve-generated-file-links.ts
|
|
2939
|
+
/** Nested links deeper than this are left as they are. */
|
|
2940
|
+
const MAX_DEPTH = 8;
|
|
2941
|
+
/** The text of a link or image longer than this is not looked at. */
|
|
2942
|
+
const MAX_LINK_TEXT = 1e3;
|
|
2943
|
+
/** Same for a destination, so a half-written one costs little. */
|
|
2944
|
+
const MAX_DESTINATION = 2e3;
|
|
2945
|
+
const IMAGE_EXTENSIONS = new Set([
|
|
2946
|
+
"png",
|
|
2947
|
+
"jpg",
|
|
2948
|
+
"jpeg",
|
|
2949
|
+
"gif",
|
|
2950
|
+
"webp",
|
|
2951
|
+
"svg",
|
|
2952
|
+
"bmp",
|
|
2953
|
+
"avif"
|
|
2954
|
+
]);
|
|
2955
|
+
const STRICT_SCHEMES = new Set([
|
|
2956
|
+
"sandbox",
|
|
2957
|
+
"file",
|
|
2958
|
+
"attachment"
|
|
2959
|
+
]);
|
|
2960
|
+
const SCHEME = /^([a-zA-Z][a-zA-Z0-9+.-]*):/;
|
|
2961
|
+
const BACKTICK_FENCE_OPEN = /^[ \t>]*(`{3,})([^\n]*)$/;
|
|
2962
|
+
const TILDE_FENCE_OPEN = /^[ \t>]*(~{3,})/;
|
|
2963
|
+
const BACKTICK_FENCE_CLOSE = /^[ \t>]*(`{3,})[ \t]*\r?$/;
|
|
2964
|
+
const TILDE_FENCE_CLOSE = /^[ \t>]*(~{3,})[ \t]*\r?$/;
|
|
2965
|
+
function lineEnd(src, from) {
|
|
2966
|
+
const newline = src.indexOf("\n", from);
|
|
2967
|
+
return newline === -1 ? src.length : newline;
|
|
2968
|
+
}
|
|
2969
|
+
/** The end of the fence that opens at `from` (the whole text if it never closes). */
|
|
2970
|
+
function fenceEnd(src, from) {
|
|
2971
|
+
const first = src.slice(from, lineEnd(src, from));
|
|
2972
|
+
const backticks = BACKTICK_FENCE_OPEN.exec(first.replace(/\r$/, ""));
|
|
2973
|
+
const opening = backticks && !backticks[2].includes("`") ? backticks[1] : TILDE_FENCE_OPEN.exec(first)?.[1] ?? null;
|
|
2974
|
+
if (!opening) return null;
|
|
2975
|
+
const close = opening[0] === "`" ? BACKTICK_FENCE_CLOSE : TILDE_FENCE_CLOSE;
|
|
2976
|
+
let position = lineEnd(src, from) + 1;
|
|
2977
|
+
while (position < src.length) {
|
|
2978
|
+
const end = lineEnd(src, position);
|
|
2979
|
+
const closing = close.exec(src.slice(position, end))?.[1];
|
|
2980
|
+
if (closing && closing.length >= opening.length) return end;
|
|
2981
|
+
position = end + 1;
|
|
2982
|
+
}
|
|
2983
|
+
return src.length;
|
|
2984
|
+
}
|
|
2985
|
+
/** The end of the code span whose opening run of `length` backticks ends at `from`. */
|
|
2986
|
+
function codeSpanEnd(src, from, length) {
|
|
2987
|
+
let index = from;
|
|
2988
|
+
while (index < src.length) {
|
|
2989
|
+
const char = src[index];
|
|
2990
|
+
if (char === "\n") {
|
|
2991
|
+
let next = index + 1;
|
|
2992
|
+
while (src[next] === " " || src[next] === " ") next++;
|
|
2993
|
+
if (next >= src.length || src[next] === "\n") return null;
|
|
2994
|
+
}
|
|
2995
|
+
if (char === "`") {
|
|
2996
|
+
let end = index;
|
|
2997
|
+
while (src[end] === "`") end++;
|
|
2998
|
+
if (end - index === length) return end;
|
|
2999
|
+
index = end;
|
|
3000
|
+
continue;
|
|
3001
|
+
}
|
|
3002
|
+
index++;
|
|
3003
|
+
}
|
|
3004
|
+
return null;
|
|
3005
|
+
}
|
|
3006
|
+
/** Fenced code blocks and code spans, which are never rewritten. */
|
|
3007
|
+
function findProtectedRanges(src) {
|
|
3008
|
+
const ranges = [];
|
|
3009
|
+
let index = 0;
|
|
3010
|
+
let atLineStart = true;
|
|
3011
|
+
while (index < src.length) {
|
|
3012
|
+
if (atLineStart) {
|
|
3013
|
+
atLineStart = false;
|
|
3014
|
+
const end = fenceEnd(src, index);
|
|
3015
|
+
if (end !== null) {
|
|
3016
|
+
ranges.push([index, end]);
|
|
3017
|
+
index = end;
|
|
3018
|
+
continue;
|
|
3019
|
+
}
|
|
3020
|
+
}
|
|
3021
|
+
const char = src[index];
|
|
3022
|
+
if (char === "\n") {
|
|
3023
|
+
atLineStart = true;
|
|
3024
|
+
index++;
|
|
3025
|
+
} else if (char === "\\") index += src[index + 1] === "\n" ? 1 : 2;
|
|
3026
|
+
else if (char === "`") {
|
|
3027
|
+
let runEnd = index;
|
|
3028
|
+
while (src[runEnd] === "`") runEnd++;
|
|
3029
|
+
const end = codeSpanEnd(src, runEnd, runEnd - index);
|
|
3030
|
+
if (end === null) index = runEnd;
|
|
3031
|
+
else {
|
|
3032
|
+
ranges.push([index, end]);
|
|
3033
|
+
index = end;
|
|
3034
|
+
}
|
|
3035
|
+
} else index++;
|
|
3036
|
+
}
|
|
3037
|
+
return ranges;
|
|
3038
|
+
}
|
|
3039
|
+
function rangeAt(context, position) {
|
|
3040
|
+
const { ranges } = context;
|
|
3041
|
+
let low = 0;
|
|
3042
|
+
let high = ranges.length - 1;
|
|
3043
|
+
while (low <= high) {
|
|
3044
|
+
const middle = low + high >> 1;
|
|
3045
|
+
const range = ranges[middle];
|
|
3046
|
+
if (position < range[0]) high = middle - 1;
|
|
3047
|
+
else if (position >= range[1]) low = middle + 1;
|
|
3048
|
+
else return range;
|
|
3049
|
+
}
|
|
3050
|
+
}
|
|
3051
|
+
function isBlank(char) {
|
|
3052
|
+
return char === " " || char === " " || char === "\n" || char === "\r";
|
|
3053
|
+
}
|
|
3054
|
+
function startsLikeGeneratedPath(text$2) {
|
|
3055
|
+
const scheme = SCHEME.exec(text$2)?.[1]?.toLowerCase();
|
|
3056
|
+
return scheme !== void 0 && STRICT_SCHEMES.has(scheme) || text$2.startsWith("/mnt/");
|
|
3057
|
+
}
|
|
3058
|
+
/**
|
|
3059
|
+
* Reads `[text](destination "title")` starting at the `[` at `open`. Returns
|
|
3060
|
+
* null when it is not (yet) a complete inline link, so text that is still
|
|
3061
|
+
* streaming is left as it is.
|
|
3062
|
+
*/
|
|
3063
|
+
function parseLink(context, open, limit) {
|
|
3064
|
+
const { src } = context;
|
|
3065
|
+
const textLimit = Math.min(limit, open + MAX_LINK_TEXT);
|
|
3066
|
+
let depth = 1;
|
|
3067
|
+
let close = open + 1;
|
|
3068
|
+
for (; close < textLimit; close++) {
|
|
3069
|
+
const char = src[close];
|
|
3070
|
+
if (char === "\\") {
|
|
3071
|
+
close++;
|
|
3072
|
+
continue;
|
|
3073
|
+
}
|
|
3074
|
+
const range = rangeAt(context, close);
|
|
3075
|
+
if (range) {
|
|
3076
|
+
close = range[1] - 1;
|
|
3077
|
+
continue;
|
|
3078
|
+
}
|
|
3079
|
+
if (char === "\n") {
|
|
3080
|
+
let next = close + 1;
|
|
3081
|
+
while (src[next] === " " || src[next] === " ") next++;
|
|
3082
|
+
if (src[next] === "\n") return null;
|
|
3083
|
+
} else if (char === "[") depth++;
|
|
3084
|
+
else if (char === "]" && --depth === 0) break;
|
|
3085
|
+
}
|
|
3086
|
+
if (close >= textLimit || src[close] !== "]" || src[close + 1] !== "(") return null;
|
|
3087
|
+
let position = close + 2;
|
|
3088
|
+
while (isBlank(src[position])) position++;
|
|
3089
|
+
const destinationLimit = Math.min(limit, position + MAX_DESTINATION);
|
|
3090
|
+
let destination;
|
|
3091
|
+
let destinationEnd;
|
|
3092
|
+
if (src[position] === "<") {
|
|
3093
|
+
let end$1 = position + 1;
|
|
3094
|
+
while (end$1 < destinationLimit && src[end$1] !== ">" && src[end$1] !== "\n") end$1 += src[end$1] === "\\" ? 2 : 1;
|
|
3095
|
+
if (src[end$1] !== ">") return null;
|
|
3096
|
+
destination = src.slice(position + 1, end$1);
|
|
3097
|
+
destinationEnd = end$1 + 1;
|
|
3098
|
+
} else {
|
|
3099
|
+
let parentheses$1 = 0;
|
|
3100
|
+
let end$1 = position;
|
|
3101
|
+
for (; end$1 < destinationLimit; end$1++) {
|
|
3102
|
+
const char = src[end$1];
|
|
3103
|
+
if (char === "\\") end$1++;
|
|
3104
|
+
else if (isBlank(char)) break;
|
|
3105
|
+
else if (char === "(") parentheses$1++;
|
|
3106
|
+
else if (char === ")") {
|
|
3107
|
+
if (parentheses$1 === 0) break;
|
|
3108
|
+
parentheses$1--;
|
|
3109
|
+
}
|
|
3110
|
+
}
|
|
3111
|
+
destination = src.slice(position, end$1);
|
|
3112
|
+
destinationEnd = end$1;
|
|
3113
|
+
}
|
|
3114
|
+
const textStart = open + 1;
|
|
3115
|
+
let end = destinationEnd;
|
|
3116
|
+
while (isBlank(src[end])) end++;
|
|
3117
|
+
let title = "";
|
|
3118
|
+
const quote = src[end];
|
|
3119
|
+
if (end > destinationEnd && (quote === "\"" || quote === "'" || quote === "(")) {
|
|
3120
|
+
const closing = quote === "(" ? ")" : quote;
|
|
3121
|
+
let titleEnd = end + 1;
|
|
3122
|
+
while (titleEnd < destinationLimit && src[titleEnd] !== closing) titleEnd += src[titleEnd] === "\\" ? 2 : 1;
|
|
3123
|
+
if (src[titleEnd] === closing) {
|
|
3124
|
+
title = src.slice(destinationEnd, titleEnd + 1);
|
|
3125
|
+
end = titleEnd + 1;
|
|
3126
|
+
while (isBlank(src[end])) end++;
|
|
3127
|
+
}
|
|
3128
|
+
}
|
|
3129
|
+
if (src[end] === ")") return {
|
|
3130
|
+
open,
|
|
3131
|
+
textStart,
|
|
3132
|
+
textEnd: close,
|
|
3133
|
+
destination,
|
|
3134
|
+
title,
|
|
3135
|
+
end: end + 1
|
|
3136
|
+
};
|
|
3137
|
+
if (!startsLikeGeneratedPath(src.slice(position, position + 16))) return null;
|
|
3138
|
+
let parentheses = 0;
|
|
3139
|
+
for (let index = position; index < destinationLimit; index++) {
|
|
3140
|
+
const char = src[index];
|
|
3141
|
+
if (char === "\n") return null;
|
|
3142
|
+
if (char === "(") parentheses++;
|
|
3143
|
+
else if (char === ")") {
|
|
3144
|
+
if (parentheses === 0) return {
|
|
3145
|
+
open,
|
|
3146
|
+
textStart,
|
|
3147
|
+
textEnd: close,
|
|
3148
|
+
destination: src.slice(position, index).trim(),
|
|
3149
|
+
title: "",
|
|
3150
|
+
end: index + 1
|
|
3151
|
+
};
|
|
3152
|
+
parentheses--;
|
|
3153
|
+
}
|
|
3154
|
+
}
|
|
3155
|
+
return null;
|
|
3156
|
+
}
|
|
3157
|
+
function decode(segment) {
|
|
3158
|
+
try {
|
|
3159
|
+
return decodeURIComponent(segment);
|
|
3160
|
+
} catch {
|
|
3161
|
+
return segment;
|
|
3162
|
+
}
|
|
3163
|
+
}
|
|
3164
|
+
/** The file name a destination points to, or null when it names none. */
|
|
3165
|
+
function fileNameOf(destination) {
|
|
3166
|
+
const name = decode(destination.replace(SCHEME, "").split(/[?#]/, 1)[0].split("/").pop() ?? "").trim();
|
|
3167
|
+
return name && name !== "." && name !== ".." ? name : null;
|
|
3168
|
+
}
|
|
3169
|
+
function fold(name) {
|
|
3170
|
+
return name.normalize("NFC").toLowerCase();
|
|
3171
|
+
}
|
|
3172
|
+
function findFile(files, name) {
|
|
3173
|
+
const exact = files.find((file) => file.name === name);
|
|
3174
|
+
if (exact) return exact;
|
|
3175
|
+
const folded = fold(name);
|
|
3176
|
+
return files.find((file) => fold(file.name) === folded);
|
|
3177
|
+
}
|
|
3178
|
+
function decide(destination, files) {
|
|
3179
|
+
const target = destination.trim();
|
|
3180
|
+
if (!target) return { kind: "keep" };
|
|
3181
|
+
const scheme = SCHEME.exec(target)?.[1]?.toLowerCase();
|
|
3182
|
+
let strict = false;
|
|
3183
|
+
if (scheme !== void 0) {
|
|
3184
|
+
if (!STRICT_SCHEMES.has(scheme)) return { kind: "keep" };
|
|
3185
|
+
strict = true;
|
|
3186
|
+
} else if (target.startsWith("/mnt/")) strict = true;
|
|
3187
|
+
else if (/^[/#?]/.test(target)) return { kind: "keep" };
|
|
3188
|
+
const name = fileNameOf(target);
|
|
3189
|
+
const file = name ? findFile(files, name) : void 0;
|
|
3190
|
+
if (file) return {
|
|
3191
|
+
kind: "file",
|
|
3192
|
+
file
|
|
3193
|
+
};
|
|
3194
|
+
return strict ? { kind: "strip" } : { kind: "keep" };
|
|
3195
|
+
}
|
|
3196
|
+
function isImageFile(file) {
|
|
3197
|
+
const type = file.contentType?.toLowerCase().trim();
|
|
3198
|
+
if (type?.startsWith("image/")) return true;
|
|
3199
|
+
if (type && type !== "application/octet-stream") return false;
|
|
3200
|
+
const extension = file.name.split(".").pop()?.toLowerCase() ?? "";
|
|
3201
|
+
return IMAGE_EXTENSIONS.has(extension);
|
|
3202
|
+
}
|
|
3203
|
+
/** A destination that survives markdown parsing whatever the URL contains. */
|
|
3204
|
+
function formatDestination(url) {
|
|
3205
|
+
const safe = url.replace(/[\s<>]/g, (char) => encodeURIComponent(char));
|
|
3206
|
+
return /[()]/.test(safe) ? `<${safe}>` : safe;
|
|
3207
|
+
}
|
|
3208
|
+
function rewrite(context, from, to, depth) {
|
|
3209
|
+
const { src } = context;
|
|
3210
|
+
let out = "";
|
|
3211
|
+
let copied = from;
|
|
3212
|
+
let index = from;
|
|
3213
|
+
while (index < to) {
|
|
3214
|
+
const char = src[index];
|
|
3215
|
+
if (char === "\\") {
|
|
3216
|
+
index += 2;
|
|
3217
|
+
continue;
|
|
3218
|
+
}
|
|
3219
|
+
const range = rangeAt(context, index);
|
|
3220
|
+
if (range) {
|
|
3221
|
+
index = range[1];
|
|
3222
|
+
continue;
|
|
3223
|
+
}
|
|
3224
|
+
const isImage = char === "!" && src[index + 1] === "[";
|
|
3225
|
+
if (char !== "[" && !isImage) {
|
|
3226
|
+
index++;
|
|
3227
|
+
continue;
|
|
3228
|
+
}
|
|
3229
|
+
const open = isImage ? index + 1 : index;
|
|
3230
|
+
const link = depth < MAX_DEPTH ? parseLink(context, open, to) : null;
|
|
3231
|
+
if (!link) {
|
|
3232
|
+
index = open + 1;
|
|
3233
|
+
continue;
|
|
3234
|
+
}
|
|
3235
|
+
const text$2 = rewrite(context, link.textStart, link.textEnd, depth + 1);
|
|
3236
|
+
const decision = decide(link.destination, context.files);
|
|
3237
|
+
let replacement;
|
|
3238
|
+
if (decision.kind === "keep") replacement = `${src.slice(index, link.textStart)}${text$2}${src.slice(link.textEnd, link.end)}`;
|
|
3239
|
+
else if (decision.kind === "strip") replacement = text$2;
|
|
3240
|
+
else {
|
|
3241
|
+
const { file } = decision;
|
|
3242
|
+
const destination = `${formatDestination(file.url)}${link.title}`;
|
|
3243
|
+
if (isImage && isImageFile(file)) replacement = ``;
|
|
3244
|
+
else if (isImage) replacement = `[${text$2.trim() ? text$2 : file.name}](${destination})`;
|
|
3245
|
+
else replacement = `[${text$2}](${destination})`;
|
|
3246
|
+
}
|
|
3247
|
+
out += src.slice(copied, index) + replacement;
|
|
3248
|
+
copied = link.end;
|
|
3249
|
+
index = link.end;
|
|
3250
|
+
}
|
|
3251
|
+
return out + src.slice(copied, to);
|
|
3252
|
+
}
|
|
3253
|
+
/**
|
|
3254
|
+
* Points the markdown links and images the model wrote to the files a tool
|
|
3255
|
+
* generated at the URL the file can really be downloaded from.
|
|
3256
|
+
*
|
|
3257
|
+
* The model often writes `` or
|
|
3258
|
+
* `[report](/mnt/data/report.xlsx)`, paths that exist only in the sandbox, and
|
|
3259
|
+
* the renderer blocks them. A destination is looked at when it has the
|
|
3260
|
+
* `sandbox:`, `file:` or `attachment:` scheme, starts with `/mnt/` or is a
|
|
3261
|
+
* relative path; `http(s):`, `mailto:`, anchors and every other destination are
|
|
3262
|
+
* never touched. Its file name (without query or fragment, percent-decoded) is
|
|
3263
|
+
* matched to `files` by name, ignoring case if needed. A match becomes that
|
|
3264
|
+
* file's `url`: an image of a file that is not an image turns into a link. A
|
|
3265
|
+
* `sandbox:`, `file:`, `attachment:` or `/mnt/` destination with no matching file
|
|
3266
|
+
* is removed, leaving its text (the alt of an image), so no broken link is
|
|
3267
|
+
* left; a relative path with no match stays as written.
|
|
3268
|
+
*
|
|
3269
|
+
* Code blocks and code spans are not changed. Links that are not complete yet,
|
|
3270
|
+
* as in text that is still streaming, are left as they are, and the result can
|
|
3271
|
+
* go through the function again unchanged.
|
|
3272
|
+
*/
|
|
3273
|
+
function resolveGeneratedFileLinks(markdown, files) {
|
|
3274
|
+
if (!markdown.includes("](")) return markdown;
|
|
3275
|
+
return rewrite({
|
|
3276
|
+
src: markdown,
|
|
3277
|
+
ranges: findProtectedRanges(markdown),
|
|
3278
|
+
files: files.filter((file) => file.name && file.url)
|
|
3279
|
+
}, 0, markdown.length, 0);
|
|
3280
|
+
}
|
|
3281
|
+
/**
|
|
3282
|
+
* The files generated by the code_execution and office_documents tool calls of
|
|
3283
|
+
* a message, with the same `url` the cards use to download them.
|
|
3284
|
+
*/
|
|
3285
|
+
function getMessageGeneratedFiles(message) {
|
|
3286
|
+
const targets = [];
|
|
3287
|
+
for (const part of message.parts) {
|
|
3288
|
+
if (part.type !== "tool-call" || part.toolType !== "code_execution" && part.toolType !== "office_documents") continue;
|
|
3289
|
+
for (const file of getGeneratedFiles(part.metadata?.media_files)) {
|
|
3290
|
+
if (!file.url) continue;
|
|
3291
|
+
const name = file.name ?? getFilenameFromUrl(file.pathname ?? "") ?? getFilenameFromUrl(file.url);
|
|
3292
|
+
if (!name) continue;
|
|
3293
|
+
targets.push({
|
|
3294
|
+
name,
|
|
3295
|
+
url: file.url,
|
|
3296
|
+
contentType: file.content_type
|
|
3297
|
+
});
|
|
3298
|
+
}
|
|
3299
|
+
}
|
|
3300
|
+
return targets;
|
|
3301
|
+
}
|
|
3302
|
+
|
|
2815
3303
|
//#endregion
|
|
2816
3304
|
//#region src/components/memory-proposal-card.tsx
|
|
2817
3305
|
const COPY$1 = {
|
|
@@ -3566,128 +4054,6 @@ function ToolResultBrowserCard({ part, className, locale }) {
|
|
|
3566
4054
|
});
|
|
3567
4055
|
}
|
|
3568
4056
|
|
|
3569
|
-
//#endregion
|
|
3570
|
-
//#region src/components/internal/download-file.ts
|
|
3571
|
-
/**
|
|
3572
|
-
* Single entry point for downloading a chat file.
|
|
3573
|
-
*
|
|
3574
|
-
* Fetches the bytes and saves them under the file name, so the browser neither
|
|
3575
|
-
* navigates nor opens a tab. That read needs the file host to allow this origin
|
|
3576
|
-
* (CORS); when it does not, the URL opens in a new tab instead. An HTTP error
|
|
3577
|
-
* (expired, deleted or private object) rejects so the caller can say so.
|
|
3578
|
-
*/
|
|
3579
|
-
async function downloadFile(file) {
|
|
3580
|
-
let response;
|
|
3581
|
-
try {
|
|
3582
|
-
response = await fetch(file.url, { credentials: "omit" });
|
|
3583
|
-
} catch {
|
|
3584
|
-
openInNewTab(file.url);
|
|
3585
|
-
return;
|
|
3586
|
-
}
|
|
3587
|
-
if (!response.ok) throw new Error(`Download failed with status ${response.status}`);
|
|
3588
|
-
saveBlob(await response.blob(), file.name || getFilenameFromUrl(file.url) || "download");
|
|
3589
|
-
}
|
|
3590
|
-
function saveBlob(blob, fileName) {
|
|
3591
|
-
const objectUrl = URL.createObjectURL(blob);
|
|
3592
|
-
const anchor = document.createElement("a");
|
|
3593
|
-
anchor.href = objectUrl;
|
|
3594
|
-
anchor.download = fileName;
|
|
3595
|
-
anchor.rel = "noopener";
|
|
3596
|
-
anchor.style.display = "none";
|
|
3597
|
-
document.body.appendChild(anchor);
|
|
3598
|
-
anchor.click();
|
|
3599
|
-
anchor.remove();
|
|
3600
|
-
window.setTimeout(() => URL.revokeObjectURL(objectUrl), 1e3);
|
|
3601
|
-
}
|
|
3602
|
-
function openInNewTab(url) {
|
|
3603
|
-
window.open(url, "_blank", "noopener,noreferrer");
|
|
3604
|
-
}
|
|
3605
|
-
|
|
3606
|
-
//#endregion
|
|
3607
|
-
//#region src/components/internal/file-download-button.tsx
|
|
3608
|
-
function FileDownloadButton({ file, label, copy, className }) {
|
|
3609
|
-
const [state, setState] = useState("idle");
|
|
3610
|
-
const isDownloading = state === "downloading";
|
|
3611
|
-
const handleClick = async (event) => {
|
|
3612
|
-
event.stopPropagation();
|
|
3613
|
-
if (isDownloading) return;
|
|
3614
|
-
setState("downloading");
|
|
3615
|
-
try {
|
|
3616
|
-
await downloadFile(file);
|
|
3617
|
-
setState("idle");
|
|
3618
|
-
} catch {
|
|
3619
|
-
setState("error");
|
|
3620
|
-
}
|
|
3621
|
-
};
|
|
3622
|
-
return /* @__PURE__ */ jsxs("span", {
|
|
3623
|
-
className: "ml-auto flex shrink-0 items-center gap-2",
|
|
3624
|
-
children: [state === "error" ? /* @__PURE__ */ jsx("span", {
|
|
3625
|
-
role: "alert",
|
|
3626
|
-
className: "text-xs text-danger",
|
|
3627
|
-
children: copy.downloadFailed
|
|
3628
|
-
}) : null, /* @__PURE__ */ jsx("button", {
|
|
3629
|
-
type: "button",
|
|
3630
|
-
onClick: handleClick,
|
|
3631
|
-
disabled: isDownloading,
|
|
3632
|
-
"aria-label": `${isDownloading ? copy.downloading : copy.download}: ${label}`,
|
|
3633
|
-
title: isDownloading ? copy.downloading : copy.download,
|
|
3634
|
-
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),
|
|
3635
|
-
children: isDownloading ? /* @__PURE__ */ jsx(LoaderCircle, {
|
|
3636
|
-
className: "h-4 w-4 animate-spin",
|
|
3637
|
-
"aria-hidden": "true"
|
|
3638
|
-
}) : /* @__PURE__ */ jsx(Download, {
|
|
3639
|
-
className: "h-4 w-4",
|
|
3640
|
-
"aria-hidden": "true"
|
|
3641
|
-
})
|
|
3642
|
-
})]
|
|
3643
|
-
});
|
|
3644
|
-
}
|
|
3645
|
-
|
|
3646
|
-
//#endregion
|
|
3647
|
-
//#region src/components/internal/generated-files.tsx
|
|
3648
|
-
/** Keeps the entries of `metadata.media_files` that look like a media file. */
|
|
3649
|
-
function getGeneratedFiles(files) {
|
|
3650
|
-
return Array.isArray(files) ? files.filter((file) => Boolean(file) && typeof file === "object" && typeof file.id === "string") : [];
|
|
3651
|
-
}
|
|
3652
|
-
function getGeneratedFileExtension(file) {
|
|
3653
|
-
return getFileExtension(file.name ?? file.pathname, file.content_type);
|
|
3654
|
-
}
|
|
3655
|
-
function getGeneratedFileLabel(file) {
|
|
3656
|
-
return file.name ?? file.pathname ?? file.id;
|
|
3657
|
-
}
|
|
3658
|
-
/** A generated file with its extension and a download button, for a sheet. */
|
|
3659
|
-
function GeneratedFileRow({ file, copy }) {
|
|
3660
|
-
const extension = getGeneratedFileExtension(file);
|
|
3661
|
-
const label = getGeneratedFileLabel(file);
|
|
3662
|
-
return /* @__PURE__ */ jsxs("div", {
|
|
3663
|
-
className: "flex items-center gap-3 rounded-lg border border-border bg-surface p-3",
|
|
3664
|
-
children: [
|
|
3665
|
-
/* @__PURE__ */ jsx(File, {
|
|
3666
|
-
className: joinClasses("h-8 w-8 shrink-0", getFileIconClass(extension)),
|
|
3667
|
-
"aria-hidden": "true"
|
|
3668
|
-
}),
|
|
3669
|
-
/* @__PURE__ */ jsxs("div", {
|
|
3670
|
-
className: "min-w-0",
|
|
3671
|
-
children: [/* @__PURE__ */ jsx("p", {
|
|
3672
|
-
className: "truncate text-sm font-medium",
|
|
3673
|
-
children: label
|
|
3674
|
-
}), extension ? /* @__PURE__ */ jsx("p", {
|
|
3675
|
-
className: "text-[11px] uppercase text-muted",
|
|
3676
|
-
children: extension
|
|
3677
|
-
}) : null]
|
|
3678
|
-
}),
|
|
3679
|
-
file.url ? /* @__PURE__ */ jsx(FileDownloadButton, {
|
|
3680
|
-
file: {
|
|
3681
|
-
url: file.url,
|
|
3682
|
-
name: file.name
|
|
3683
|
-
},
|
|
3684
|
-
label,
|
|
3685
|
-
copy
|
|
3686
|
-
}) : null
|
|
3687
|
-
]
|
|
3688
|
-
});
|
|
3689
|
-
}
|
|
3690
|
-
|
|
3691
4057
|
//#endregion
|
|
3692
4058
|
//#region src/components/tool-result-code-execution-card.tsx
|
|
3693
4059
|
const STEP_ICONS = {
|
|
@@ -6134,7 +6500,7 @@ function PendingConfirmationFooter({ pending, toolConfirmation, copy }) {
|
|
|
6134
6500
|
]
|
|
6135
6501
|
}) : null;
|
|
6136
6502
|
}
|
|
6137
|
-
const decide = async (decisions) => {
|
|
6503
|
+
const decide$1 = async (decisions) => {
|
|
6138
6504
|
setFailed(false);
|
|
6139
6505
|
setApplying(false);
|
|
6140
6506
|
try {
|
|
@@ -6150,7 +6516,7 @@ function PendingConfirmationFooter({ pending, toolConfirmation, copy }) {
|
|
|
6150
6516
|
};
|
|
6151
6517
|
const send = async (decision) => {
|
|
6152
6518
|
setSending(decision);
|
|
6153
|
-
await decide([{
|
|
6519
|
+
await decide$1([{
|
|
6154
6520
|
action_id: pending.action_id,
|
|
6155
6521
|
decision
|
|
6156
6522
|
}]);
|
|
@@ -6162,7 +6528,7 @@ function PendingConfirmationFooter({ pending, toolConfirmation, copy }) {
|
|
|
6162
6528
|
setServerErrors({});
|
|
6163
6529
|
if (Object.keys(errors).length > 0) return;
|
|
6164
6530
|
setSending("confirm");
|
|
6165
|
-
await decide([{
|
|
6531
|
+
await decide$1([{
|
|
6166
6532
|
action_id: pending.action_id,
|
|
6167
6533
|
decision: "confirm",
|
|
6168
6534
|
...Object.keys(patch).length > 0 ? { arguments: patch } : {}
|
|
@@ -7877,7 +8243,7 @@ function aggregateSourceToolParts(parts, position) {
|
|
|
7877
8243
|
...remainingParts.slice(insertionIndex)
|
|
7878
8244
|
];
|
|
7879
8245
|
}
|
|
7880
|
-
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 }) {
|
|
8246
|
+
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 }) {
|
|
7881
8247
|
const copy = getUiCopy(locale);
|
|
7882
8248
|
const [isExpanded, setIsExpanded] = useState(false);
|
|
7883
8249
|
const textContent = useMemo(() => message.parts.filter((part) => part.type === "text").map((part) => part.text).join("\n"), [message.parts]);
|
|
@@ -7887,12 +8253,28 @@ function MessageParts({ message, collapseThreshold = DEFAULT_COLLAPSE_THRESHOLD,
|
|
|
7887
8253
|
const textAlignmentClassName = getMessageTextAlignmentClassName(message.role);
|
|
7888
8254
|
const resolvedSourceResultsPosition = sourceToolResultsPosition ?? datasourceToolResultsPosition ?? (sourceToolResultsLayout === "aggregated" && !renderToolCall ? "before-content" : "inline");
|
|
7889
8255
|
const shouldAggregateSourceResults = message.role === "assistant" && sourceToolResultsLayout === "aggregated" && !renderToolCall;
|
|
7890
|
-
const
|
|
8256
|
+
const displayParts = useMemo(() => {
|
|
8257
|
+
if (!resolveFileLinks || message.role !== "assistant") return message.parts;
|
|
8258
|
+
const files = generatedFiles ?? getMessageGeneratedFiles(message);
|
|
8259
|
+
return message.parts.map((part) => {
|
|
8260
|
+
if (part.type !== "text") return part;
|
|
8261
|
+
const text$2 = resolveGeneratedFileLinks(part.text, files);
|
|
8262
|
+
return text$2 === part.text ? part : {
|
|
8263
|
+
...part,
|
|
8264
|
+
text: text$2
|
|
8265
|
+
};
|
|
8266
|
+
});
|
|
8267
|
+
}, [
|
|
8268
|
+
message,
|
|
8269
|
+
resolveFileLinks,
|
|
8270
|
+
generatedFiles
|
|
8271
|
+
]);
|
|
8272
|
+
const orderedParts = useMemo(() => shouldAggregateSourceResults ? aggregateSourceToolParts(displayParts, resolvedSourceResultsPosition) : orderMessageParts(displayParts, resolvedSourceResultsPosition).map(({ part, index }) => ({
|
|
7891
8273
|
kind: "part",
|
|
7892
8274
|
part,
|
|
7893
8275
|
index
|
|
7894
8276
|
})), [
|
|
7895
|
-
|
|
8277
|
+
displayParts,
|
|
7896
8278
|
resolvedSourceResultsPosition,
|
|
7897
8279
|
shouldAggregateSourceResults
|
|
7898
8280
|
]);
|
|
@@ -7941,6 +8323,7 @@ function AssistantMessage({ message, avatar, footer, messageClassName, contentCl
|
|
|
7941
8323
|
const contentParts = message.parts.filter((part) => part.type !== "tool-call");
|
|
7942
8324
|
const renderableToolParts = toolParts.filter((part) => isRenderableToolPart(part, messagePartsProps?.locale, Boolean(messagePartsProps?.renderToolCall), Boolean(messagePartsProps?.memoryProposal)));
|
|
7943
8325
|
const hasContentRow = contentParts.length > 0 || footer !== void 0;
|
|
8326
|
+
const generatedFiles = messagePartsProps?.generatedFiles ?? (messagePartsProps?.resolveFileLinks === false ? void 0 : getMessageGeneratedFiles(message));
|
|
7944
8327
|
return /* @__PURE__ */ jsxs("div", {
|
|
7945
8328
|
className: joinClasses("plai-assistant-message flex w-full flex-col gap-2", className),
|
|
7946
8329
|
...props,
|
|
@@ -7964,6 +8347,7 @@ function AssistantMessage({ message, avatar, footer, messageClassName, contentCl
|
|
|
7964
8347
|
className: contentClassName,
|
|
7965
8348
|
children: [contentParts.length > 0 ? /* @__PURE__ */ jsx(MessageParts, {
|
|
7966
8349
|
...messagePartsProps,
|
|
8350
|
+
generatedFiles,
|
|
7967
8351
|
message: withParts(message, contentParts),
|
|
7968
8352
|
className: contentPartsClassName
|
|
7969
8353
|
}) : null, footer]
|
|
@@ -8809,5 +9193,5 @@ function ConversationFilesSheet({ messages, locale, className, open, onOpenChang
|
|
|
8809
9193
|
}
|
|
8810
9194
|
|
|
8811
9195
|
//#endregion
|
|
8812
|
-
export { AggregatedSourceResults, ArrowUp, AssistantMessage, ChatError, Clipboard, ConversationFilesList, ConversationFilesSheet, DatasourceFolderCard, DatasourceToolResources, DatasourceToolResultCard, Loader, MemoryProposalCard, Message, MessageAvatar, MessageContent, MessageFooter, MessageHeader, MessageParts, MessageRoot, Microphone, PROMPT_FORM_FILE_ACCEPT, Paperclip, 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, getResourceDescription, getResourceIcon, getResourceOpenGraphImage, getResourceOpenGraphType, getResourceTitle, getResourceType, getResourceUrl, getToolUseLabel, isAllowedPromptFormFile, partitionPromptFormFiles, useChat, useChatRegistryBusy, useOwnedChatRegistry, useRegistryChat, useSpeechToText, useStreamCoordinator, useToolConfirmationSync, useToolConfirmations };
|
|
9196
|
+
export { AggregatedSourceResults, ArrowUp, AssistantMessage, ChatError, Clipboard, ConversationFilesList, ConversationFilesSheet, DatasourceFolderCard, DatasourceToolResources, DatasourceToolResultCard, Loader, MemoryProposalCard, Message, MessageAvatar, MessageContent, MessageFooter, MessageHeader, MessageParts, MessageRoot, Microphone, PROMPT_FORM_FILE_ACCEPT, Paperclip, 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, getResourceDescription, getResourceIcon, getResourceOpenGraphImage, getResourceOpenGraphType, getResourceTitle, getResourceType, getResourceUrl, getToolUseLabel, isAllowedPromptFormFile, partitionPromptFormFiles, resolveGeneratedFileLinks, useChat, useChatRegistryBusy, useOwnedChatRegistry, useRegistryChat, useSpeechToText, useStreamCoordinator, useToolConfirmationSync, useToolConfirmations };
|
|
8813
9197
|
//# sourceMappingURL=index.js.map
|