@seclai/react 0.0.16 → 0.0.18
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 +138 -13
- package/dist/chunk-OFKVHNZ6.js +117 -0
- package/dist/chunk-VRNPDCWI.js +134 -0
- package/dist/extensions-D2MHwNt1.d.ts +49 -0
- package/dist/html.d.ts +22 -0
- package/dist/html.js +11 -0
- package/dist/index.d.ts +39 -10
- package/dist/index.js +584 -258
- package/dist/math.d.ts +22 -0
- package/dist/math.js +106 -0
- package/dist/mermaid.d.ts +30 -0
- package/dist/mermaid.js +126 -0
- package/dist/styles.css +175 -5
- package/package.json +49 -12
- package/dist/index.cjs +0 -1456
- package/dist/index.d.cts +0 -474
package/dist/index.cjs
DELETED
|
@@ -1,1456 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
"use strict";
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
-
|
|
21
|
-
// src/index.ts
|
|
22
|
-
var index_exports = {};
|
|
23
|
-
__export(index_exports, {
|
|
24
|
-
AgentChat: () => AgentChat,
|
|
25
|
-
AgentRunForm: () => AgentRunForm,
|
|
26
|
-
DEFAULT_BASE_PATH: () => DEFAULT_BASE_PATH,
|
|
27
|
-
MarkdownContent: () => MarkdownContent,
|
|
28
|
-
MediaGallery: () => MediaGallery,
|
|
29
|
-
MediaPreview: () => MediaPreview,
|
|
30
|
-
MediaSkeleton: () => MediaSkeleton,
|
|
31
|
-
MediaViewer: () => MediaViewer,
|
|
32
|
-
RunProgress: () => RunProgress,
|
|
33
|
-
SeclaiAgent: () => SeclaiAgent,
|
|
34
|
-
attachmentFileName: () => attachmentFileName,
|
|
35
|
-
createEndpoints: () => createEndpoints,
|
|
36
|
-
fetchAttachmentReferences: () => fetchAttachmentReferences,
|
|
37
|
-
saveFile: () => saveFile,
|
|
38
|
-
saveFiles: () => saveFiles,
|
|
39
|
-
uploadFile: () => uploadFile,
|
|
40
|
-
useAgentRun: () => useAgentRun,
|
|
41
|
-
useElapsed: () => useElapsed
|
|
42
|
-
});
|
|
43
|
-
module.exports = __toCommonJS(index_exports);
|
|
44
|
-
|
|
45
|
-
// src/AgentChat.tsx
|
|
46
|
-
var import_react8 = require("react");
|
|
47
|
-
|
|
48
|
-
// src/attachments.ts
|
|
49
|
-
var import_react = require("react");
|
|
50
|
-
|
|
51
|
-
// src/endpoints.ts
|
|
52
|
-
var DEFAULT_BASE_PATH = "/api/seclai";
|
|
53
|
-
function createEndpoints({
|
|
54
|
-
basePath = DEFAULT_BASE_PATH,
|
|
55
|
-
agentId
|
|
56
|
-
} = {}) {
|
|
57
|
-
const trimmed = basePath.replace(/\/+$/, "");
|
|
58
|
-
const base = agentId ? `${trimmed}/${encodeURIComponent(agentId)}` : trimmed;
|
|
59
|
-
return {
|
|
60
|
-
attachmentReferences: `${base}/attachment-references`,
|
|
61
|
-
upload: `${base}/upload`,
|
|
62
|
-
run: `${base}/run`,
|
|
63
|
-
download(runId, attachmentId, name) {
|
|
64
|
-
const params = new URLSearchParams({ runId, attachmentId });
|
|
65
|
-
if (name) params.set("name", name);
|
|
66
|
-
return `${base}/download?${params}`;
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
async function readErrorMessage(res) {
|
|
71
|
-
try {
|
|
72
|
-
const body = await res.json();
|
|
73
|
-
if (typeof body.error === "string" && body.error) return body.error;
|
|
74
|
-
} catch {
|
|
75
|
-
}
|
|
76
|
-
return `The request failed with status ${res.status}.`;
|
|
77
|
-
}
|
|
78
|
-
async function readJson(res) {
|
|
79
|
-
if (!res.ok) throw new Error(await readErrorMessage(res));
|
|
80
|
-
return await res.json();
|
|
81
|
-
}
|
|
82
|
-
async function fetchAttachmentReferences(endpoints, { signal } = {}) {
|
|
83
|
-
return readJson(await fetch(endpoints.attachmentReferences, { signal }));
|
|
84
|
-
}
|
|
85
|
-
async function uploadFile(endpoints, file, { signal, params } = {}) {
|
|
86
|
-
const form = new FormData();
|
|
87
|
-
form.set("file", file);
|
|
88
|
-
const url = params ? withQuery(endpoints.upload, params) : endpoints.upload;
|
|
89
|
-
return readJson(await fetch(url, { method: "POST", body: form, signal }));
|
|
90
|
-
}
|
|
91
|
-
function withQuery(url, params) {
|
|
92
|
-
const query = new URLSearchParams(params).toString();
|
|
93
|
-
if (!query) return url;
|
|
94
|
-
const hashAt = url.indexOf("#");
|
|
95
|
-
const base = hashAt < 0 ? url : url.slice(0, hashAt);
|
|
96
|
-
const hash = hashAt < 0 ? "" : url.slice(hashAt);
|
|
97
|
-
const separator = !base.includes("?") ? "?" : base.endsWith("?") || base.endsWith("&") ? "" : "&";
|
|
98
|
-
return `${base}${separator}${query}${hash}`;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// src/attachments.ts
|
|
102
|
-
function useAcceptedAttachments(endpoints) {
|
|
103
|
-
const [state, setState] = (0, import_react.useState)({ slots: null, error: null });
|
|
104
|
-
(0, import_react.useEffect)(() => {
|
|
105
|
-
const controller = new AbortController();
|
|
106
|
-
setState({ slots: null, error: null });
|
|
107
|
-
fetchAttachmentReferences(endpoints, { signal: controller.signal }).then((refs) => setState({ slots: refs.accepted_attachments ?? [], error: null })).catch((err) => {
|
|
108
|
-
if (controller.signal.aborted) return;
|
|
109
|
-
setState({
|
|
110
|
-
slots: null,
|
|
111
|
-
error: err instanceof Error ? err.message : "Could not load this agent."
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
return () => controller.abort();
|
|
115
|
-
}, [endpoints]);
|
|
116
|
-
return state;
|
|
117
|
-
}
|
|
118
|
-
function acceptAttribute(contentTypes) {
|
|
119
|
-
return contentTypes?.length ? contentTypes.join(",") : void 0;
|
|
120
|
-
}
|
|
121
|
-
function acceptsType(contentTypes, type) {
|
|
122
|
-
if (!contentTypes?.length || !type) return true;
|
|
123
|
-
const [major] = type.toLowerCase().split("/");
|
|
124
|
-
return contentTypes.some((pattern) => {
|
|
125
|
-
const p = pattern.trim().toLowerCase();
|
|
126
|
-
if (p === "*/*" || p === "*") return true;
|
|
127
|
-
if (p.endsWith("/*")) return major === p.slice(0, -2);
|
|
128
|
-
return p === type.toLowerCase();
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
function messageFileLimits(slots) {
|
|
132
|
-
let max = 0;
|
|
133
|
-
let anyType = false;
|
|
134
|
-
const types = /* @__PURE__ */ new Set();
|
|
135
|
-
for (const slot of slots) {
|
|
136
|
-
max = max == null || slot.max == null ? null : max + slot.max;
|
|
137
|
-
if (slot.content_types?.length) slot.content_types.forEach((t) => types.add(t));
|
|
138
|
-
else anyType = true;
|
|
139
|
-
}
|
|
140
|
-
return { max, contentTypes: anyType ? [] : [...types] };
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// src/placeholders/MarkdownContent.tsx
|
|
144
|
-
var import_react2 = require("react");
|
|
145
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
146
|
-
var ORDERED = /^(\s*)(\d{1,9})[.)]\s+(\S.*)$/;
|
|
147
|
-
var BULLET = /^(\s*)[-*•]\s+(\S.*)$/;
|
|
148
|
-
var BARE_MARKER = /^\s*(?:\d{1,9}[.)]?\s*|[-*•_][-*_\s]*)$/;
|
|
149
|
-
var THEMATIC_BREAK = /^\s*([-*_])(?:[ \t]*\1){2,}[ \t]*$/;
|
|
150
|
-
var NEST = 2;
|
|
151
|
-
var FENCE = /^\s*(`{3,}(?!.*`)|~{3,})/;
|
|
152
|
-
var CLOSING_FENCE = /^\s*(`{3,}|~{3,})\s*$/;
|
|
153
|
-
function indentOf(space) {
|
|
154
|
-
let width = 0;
|
|
155
|
-
for (const char of space) width += char === " " ? 4 : 1;
|
|
156
|
-
return width;
|
|
157
|
-
}
|
|
158
|
-
function parse(content) {
|
|
159
|
-
const blocks = [];
|
|
160
|
-
const open = [];
|
|
161
|
-
let blank = true;
|
|
162
|
-
let fence = null;
|
|
163
|
-
const push = (block) => {
|
|
164
|
-
blocks.push(block);
|
|
165
|
-
open.length = 0;
|
|
166
|
-
};
|
|
167
|
-
for (const line of content.replace(/\r\n?/g, "\n").split("\n")) {
|
|
168
|
-
const last = blocks.at(-1);
|
|
169
|
-
if (fence && last?.type === "p") {
|
|
170
|
-
last.lines.push(line);
|
|
171
|
-
const closing = CLOSING_FENCE.exec(line)?.[1];
|
|
172
|
-
if (closing && closing[0] === fence[0] && closing.length >= fence.length) fence = null;
|
|
173
|
-
continue;
|
|
174
|
-
}
|
|
175
|
-
const opening = FENCE.exec(line);
|
|
176
|
-
if (opening) {
|
|
177
|
-
fence = opening[1];
|
|
178
|
-
push({ type: "p", lines: [line], verbatim: true });
|
|
179
|
-
blank = false;
|
|
180
|
-
continue;
|
|
181
|
-
}
|
|
182
|
-
if (!line.trim()) {
|
|
183
|
-
blank = true;
|
|
184
|
-
continue;
|
|
185
|
-
}
|
|
186
|
-
if (THEMATIC_BREAK.test(line)) {
|
|
187
|
-
push({ type: "hr" });
|
|
188
|
-
blank = false;
|
|
189
|
-
continue;
|
|
190
|
-
}
|
|
191
|
-
const ordered = ORDERED.exec(line);
|
|
192
|
-
const bullet = ordered || BARE_MARKER.test(line) ? null : BULLET.exec(line);
|
|
193
|
-
if (ordered || bullet) {
|
|
194
|
-
const type = ordered ? "ol" : "ul";
|
|
195
|
-
const item = ordered ? { n: Number(ordered[2]), indent: indentOf(ordered[1]), lines: [ordered[3]], lists: [] } : { indent: indentOf(bullet[1]), lines: [bullet[2]], lists: [] };
|
|
196
|
-
let level = 0;
|
|
197
|
-
while (level < open.length && item.indent >= open[level].items.at(-1).indent + NEST) level++;
|
|
198
|
-
const list = open[level];
|
|
199
|
-
if (list?.type === type) {
|
|
200
|
-
list.items.push(item);
|
|
201
|
-
open.length = level + 1;
|
|
202
|
-
} else {
|
|
203
|
-
const next = { type, items: [item] };
|
|
204
|
-
(level > 0 ? open[level - 1].items.at(-1).lists : blocks).push(next);
|
|
205
|
-
open.length = level;
|
|
206
|
-
open.push(next);
|
|
207
|
-
}
|
|
208
|
-
} else if (!blank && open.length > 0 && !BARE_MARKER.test(line)) {
|
|
209
|
-
open.at(-1).items.at(-1).lines.push(line.trim());
|
|
210
|
-
} else if (!blank && last?.type === "p" && !last.verbatim) {
|
|
211
|
-
last.lines.push(line);
|
|
212
|
-
} else {
|
|
213
|
-
push({ type: "p", lines: [line] });
|
|
214
|
-
}
|
|
215
|
-
blank = false;
|
|
216
|
-
}
|
|
217
|
-
return blocks;
|
|
218
|
-
}
|
|
219
|
-
var INLINE = /(`[^`\n]+`)|\*\*(?!\s)((?:[^*`\n]|`[^`\n]*`)*?(?:[^\s*`]|`[^`\n]*`))\*\*/g;
|
|
220
|
-
function inline(text) {
|
|
221
|
-
const nodes = [];
|
|
222
|
-
let at = 0;
|
|
223
|
-
for (const match of text.matchAll(INLINE)) {
|
|
224
|
-
if (match[2] === void 0) continue;
|
|
225
|
-
const index = match.index;
|
|
226
|
-
if (index > at) nodes.push(text.slice(at, index));
|
|
227
|
-
nodes.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)("strong", { children: match[2] }, index));
|
|
228
|
-
at = index + match[0].length;
|
|
229
|
-
}
|
|
230
|
-
if (at < text.length) nodes.push(text.slice(at));
|
|
231
|
-
return nodes;
|
|
232
|
-
}
|
|
233
|
-
function withBreaks(lines, format) {
|
|
234
|
-
return lines.map((line, i) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react2.Fragment, { children: [
|
|
235
|
-
i > 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("br", {}),
|
|
236
|
-
format(line)
|
|
237
|
-
] }, i));
|
|
238
|
-
}
|
|
239
|
-
function renderList(list, key) {
|
|
240
|
-
if (list.type === "ul") {
|
|
241
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("ul", { children: list.items.map((item, i) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("li", { children: [
|
|
242
|
-
withBreaks(item.lines, inline),
|
|
243
|
-
item.lists.map(renderList)
|
|
244
|
-
] }, i)) }, key);
|
|
245
|
-
}
|
|
246
|
-
return (
|
|
247
|
-
// `role="list"`: Safari drops the list role from a list styled with
|
|
248
|
-
// `list-style: none`, which this one is, for its number badges.
|
|
249
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("ol", { start: list.items[0].n, role: "list", children: list.items.map((item, i) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("li", { value: item.n, children: [
|
|
250
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "seclai-markdown-number", children: item.n }),
|
|
251
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [
|
|
252
|
-
withBreaks(item.lines, inline),
|
|
253
|
-
item.lists.map(renderList)
|
|
254
|
-
] })
|
|
255
|
-
] }, i)) }, key)
|
|
256
|
-
);
|
|
257
|
-
}
|
|
258
|
-
function MarkdownContent({ content }) {
|
|
259
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "seclai-markdown", children: parse(content).map((block, i) => {
|
|
260
|
-
switch (block.type) {
|
|
261
|
-
case "ol":
|
|
262
|
-
case "ul":
|
|
263
|
-
return renderList(block, i);
|
|
264
|
-
case "hr":
|
|
265
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("hr", {}, i);
|
|
266
|
-
default:
|
|
267
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { children: withBreaks(block.lines, block.verbatim ? (line) => line : inline) }, i);
|
|
268
|
-
}
|
|
269
|
-
}) });
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
// src/media/MediaGallery.tsx
|
|
273
|
-
var import_react5 = require("react");
|
|
274
|
-
|
|
275
|
-
// src/placeholders/MediaPreview.tsx
|
|
276
|
-
var import_react3 = require("react");
|
|
277
|
-
|
|
278
|
-
// src/media/files.ts
|
|
279
|
-
var EXTENSIONS = {
|
|
280
|
-
"image/jpeg": "jpg",
|
|
281
|
-
"image/svg+xml": "svg",
|
|
282
|
-
"image/x-icon": "ico",
|
|
283
|
-
"image/vnd.microsoft.icon": "ico",
|
|
284
|
-
"audio/mpeg": "mp3",
|
|
285
|
-
"video/quicktime": "mov",
|
|
286
|
-
"text/plain": "txt",
|
|
287
|
-
"text/markdown": "md",
|
|
288
|
-
"application/octet-stream": "bin"
|
|
289
|
-
};
|
|
290
|
-
function isImage(attachment) {
|
|
291
|
-
return attachment.mime.toLowerCase().startsWith("image/");
|
|
292
|
-
}
|
|
293
|
-
function extensionForMime(mime) {
|
|
294
|
-
const type = mime.split(";")[0].trim().toLowerCase();
|
|
295
|
-
const known = EXTENSIONS[type];
|
|
296
|
-
if (known) return known;
|
|
297
|
-
const subtype = type.split("/")[1] ?? "";
|
|
298
|
-
const ext = (subtype.split("+").at(-1) ?? "").replace(/^x-/, "");
|
|
299
|
-
return /^[a-z0-9]{1,5}$/.test(ext) ? ext : "bin";
|
|
300
|
-
}
|
|
301
|
-
function attachmentFileName(attachment, number) {
|
|
302
|
-
if (attachment.name) return attachment.name;
|
|
303
|
-
const base = number != null ? `image-${number}` : attachment.id;
|
|
304
|
-
return `${base}.${extensionForMime(attachment.mime)}`;
|
|
305
|
-
}
|
|
306
|
-
var SAFE_PROTOCOLS = /* @__PURE__ */ new Set(["http:", "https:", "blob:"]);
|
|
307
|
-
function isSafeUrl(url) {
|
|
308
|
-
try {
|
|
309
|
-
return SAFE_PROTOCOLS.has(new URL(url, document.baseURI).protocol);
|
|
310
|
-
} catch {
|
|
311
|
-
return false;
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
var SAVE_FILES_DELAY_MS = 400;
|
|
315
|
-
var REVOKE_OBJECT_URL_DELAY_MS = 4e4;
|
|
316
|
-
function clickDownloadLink(url, filename) {
|
|
317
|
-
const link = document.createElement("a");
|
|
318
|
-
link.href = url;
|
|
319
|
-
link.download = filename;
|
|
320
|
-
link.rel = "noopener";
|
|
321
|
-
link.style.display = "none";
|
|
322
|
-
document.body.append(link);
|
|
323
|
-
link.click();
|
|
324
|
-
link.remove();
|
|
325
|
-
}
|
|
326
|
-
function saveFile(urlOrBlob, filename) {
|
|
327
|
-
if (typeof document === "undefined") return;
|
|
328
|
-
if (typeof urlOrBlob !== "string") {
|
|
329
|
-
const url = URL.createObjectURL(urlOrBlob);
|
|
330
|
-
clickDownloadLink(url, filename);
|
|
331
|
-
setTimeout(() => URL.revokeObjectURL(url), REVOKE_OBJECT_URL_DELAY_MS);
|
|
332
|
-
return;
|
|
333
|
-
}
|
|
334
|
-
if (!isSafeUrl(urlOrBlob)) {
|
|
335
|
-
console.warn(`seclai: not downloading ${filename}, its URL isn't http(s), blob or relative.`);
|
|
336
|
-
return;
|
|
337
|
-
}
|
|
338
|
-
clickDownloadLink(urlOrBlob, filename);
|
|
339
|
-
}
|
|
340
|
-
async function saveFiles(files, { delayMs = SAVE_FILES_DELAY_MS, signal } = {}) {
|
|
341
|
-
for (const [i, file] of files.entries()) {
|
|
342
|
-
if (signal?.aborted) return;
|
|
343
|
-
if (i > 0) await wait(delayMs, signal);
|
|
344
|
-
if (signal?.aborted) return;
|
|
345
|
-
saveFile(file.source, file.filename);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
function wait(ms, signal) {
|
|
349
|
-
return new Promise((resolve) => {
|
|
350
|
-
const done = () => {
|
|
351
|
-
clearTimeout(timer);
|
|
352
|
-
signal?.removeEventListener("abort", done);
|
|
353
|
-
resolve();
|
|
354
|
-
};
|
|
355
|
-
const timer = setTimeout(done, ms);
|
|
356
|
-
signal?.addEventListener("abort", done, { once: true });
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
// src/placeholders/MediaPreview.tsx
|
|
361
|
-
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
362
|
-
function hasContent(node) {
|
|
363
|
-
return node != null && node !== false && node !== "";
|
|
364
|
-
}
|
|
365
|
-
function aspectRatioStyle(ratio) {
|
|
366
|
-
return ratio == null ? void 0 : { aspectRatio: String(ratio) };
|
|
367
|
-
}
|
|
368
|
-
function imageLabel(attachment, number) {
|
|
369
|
-
return attachment.name ?? (number != null ? `Image ${number}` : "Image");
|
|
370
|
-
}
|
|
371
|
-
function MediaPreview({ attachment, aspectRatio, number, actions, onOpen }) {
|
|
372
|
-
const filename = attachmentFileName(attachment, number);
|
|
373
|
-
const extra = typeof actions === "function" ? actions(attachment) : actions;
|
|
374
|
-
if (!isImage(attachment)) {
|
|
375
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("figure", { className: "seclai-media", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("figcaption", { children: [
|
|
376
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("a", { href: attachment.url, download: filename, children: attachment.name ?? filename }),
|
|
377
|
-
hasContent(extra) && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "seclai-media-actions", children: extra })
|
|
378
|
-
] }) });
|
|
379
|
-
}
|
|
380
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
381
|
-
ImageCard,
|
|
382
|
-
{
|
|
383
|
-
attachment,
|
|
384
|
-
aspectRatio,
|
|
385
|
-
number,
|
|
386
|
-
filename,
|
|
387
|
-
extra,
|
|
388
|
-
onOpen
|
|
389
|
-
}
|
|
390
|
-
);
|
|
391
|
-
}
|
|
392
|
-
function ImageCard({ attachment, aspectRatio, number, filename, extra, onOpen }) {
|
|
393
|
-
const img = (0, import_react3.useRef)(null);
|
|
394
|
-
const [natural, setNatural] = (0, import_react3.useState)(null);
|
|
395
|
-
const [failed, setFailed] = (0, import_react3.useState)(false);
|
|
396
|
-
const label = imageLabel(attachment, number);
|
|
397
|
-
function measure() {
|
|
398
|
-
const el = img.current;
|
|
399
|
-
if (el && el.naturalWidth > 0 && el.naturalHeight > 0) {
|
|
400
|
-
setNatural(`${el.naturalWidth} / ${el.naturalHeight}`);
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
(0, import_react3.useEffect)(() => {
|
|
404
|
-
setNatural(null);
|
|
405
|
-
setFailed(false);
|
|
406
|
-
const el = img.current;
|
|
407
|
-
if (el?.complete) {
|
|
408
|
-
if (el.naturalWidth === 0) setFailed(true);
|
|
409
|
-
else measure();
|
|
410
|
-
}
|
|
411
|
-
}, [attachment.url]);
|
|
412
|
-
const frameContent = /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
|
|
413
|
-
failed ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "seclai-media-error", children: "Couldn't load the image." }) : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
414
|
-
"img",
|
|
415
|
-
{
|
|
416
|
-
ref: img,
|
|
417
|
-
src: attachment.url,
|
|
418
|
-
alt: label,
|
|
419
|
-
loading: "lazy",
|
|
420
|
-
decoding: "async",
|
|
421
|
-
onLoad: measure,
|
|
422
|
-
onError: () => setFailed(true)
|
|
423
|
-
}
|
|
424
|
-
),
|
|
425
|
-
number != null && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "seclai-media-badge", children: number })
|
|
426
|
-
] });
|
|
427
|
-
const frameStyle = aspectRatioStyle(aspectRatio ?? natural ?? void 0);
|
|
428
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("figure", { className: "seclai-media seclai-media-image", children: [
|
|
429
|
-
onOpen ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
430
|
-
"button",
|
|
431
|
-
{
|
|
432
|
-
type: "button",
|
|
433
|
-
className: "seclai-media-frame",
|
|
434
|
-
style: frameStyle,
|
|
435
|
-
"aria-label": `Open ${label} full size`,
|
|
436
|
-
"aria-haspopup": "dialog",
|
|
437
|
-
onClick: onOpen,
|
|
438
|
-
children: frameContent
|
|
439
|
-
}
|
|
440
|
-
) : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "seclai-media-frame", style: frameStyle, children: frameContent }),
|
|
441
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("figcaption", { className: "seclai-media-bar", children: [
|
|
442
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("a", { href: attachment.url, download: filename, "aria-label": `Download ${label}`, children: "Download" }),
|
|
443
|
-
hasContent(extra) && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "seclai-media-actions", children: extra })
|
|
444
|
-
] })
|
|
445
|
-
] });
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
// src/media/MediaSkeleton.tsx
|
|
449
|
-
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
450
|
-
function MediaSkeleton({ aspectRatio, number, label }) {
|
|
451
|
-
const name = label ?? (number != null ? `Image ${number} is being generated` : "Image is being generated");
|
|
452
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "seclai-media seclai-media-image seclai-media-skeleton", role: "img", "aria-label": name, children: [
|
|
453
|
-
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "seclai-media-frame", style: aspectRatioStyle(aspectRatio), children: number != null && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "seclai-media-badge", children: number }) }),
|
|
454
|
-
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "seclai-media-bar", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "seclai-media-skeleton-line" }) })
|
|
455
|
-
] });
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
// src/media/MediaViewer.tsx
|
|
459
|
-
var import_react4 = require("react");
|
|
460
|
-
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
461
|
-
function MediaViewer({ attachments, index, onIndexChange, onClose }) {
|
|
462
|
-
const dialog = (0, import_react4.useRef)(null);
|
|
463
|
-
const closeButton = (0, import_react4.useRef)(null);
|
|
464
|
-
const returnFocus = (0, import_react4.useRef)(null);
|
|
465
|
-
const isOpen = (0, import_react4.useRef)(false);
|
|
466
|
-
const titleId = (0, import_react4.useId)();
|
|
467
|
-
const current = index != null ? attachments[index] : void 0;
|
|
468
|
-
const open = current != null;
|
|
469
|
-
isOpen.current = open;
|
|
470
|
-
(0, import_react4.useEffect)(() => {
|
|
471
|
-
const el = dialog.current;
|
|
472
|
-
if (!el) return;
|
|
473
|
-
if (open && !el.open) {
|
|
474
|
-
returnFocus.current = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
|
475
|
-
el.showModal();
|
|
476
|
-
closeButton.current?.focus();
|
|
477
|
-
} else if (!open && el.open) {
|
|
478
|
-
el.close();
|
|
479
|
-
restoreFocus();
|
|
480
|
-
}
|
|
481
|
-
}, [open]);
|
|
482
|
-
(0, import_react4.useEffect)(
|
|
483
|
-
() => () => {
|
|
484
|
-
if (isOpen.current) restoreFocus();
|
|
485
|
-
},
|
|
486
|
-
[]
|
|
487
|
-
);
|
|
488
|
-
function restoreFocus() {
|
|
489
|
-
const target = returnFocus.current;
|
|
490
|
-
returnFocus.current = null;
|
|
491
|
-
if (target?.isConnected) target.focus();
|
|
492
|
-
}
|
|
493
|
-
const last = attachments.length - 1;
|
|
494
|
-
const atStart = index == null || index <= 0;
|
|
495
|
-
const atEnd = index == null || index >= last;
|
|
496
|
-
function go(step) {
|
|
497
|
-
if (index == null) return;
|
|
498
|
-
const next = index + step;
|
|
499
|
-
if (next >= 0 && next <= last) onIndexChange(next);
|
|
500
|
-
}
|
|
501
|
-
function onKeyDown(event) {
|
|
502
|
-
if (event.key !== "ArrowLeft" && event.key !== "ArrowRight" && event.key !== "Escape") return;
|
|
503
|
-
event.preventDefault();
|
|
504
|
-
event.stopPropagation();
|
|
505
|
-
if (event.key === "ArrowLeft") go(-1);
|
|
506
|
-
else if (event.key === "ArrowRight") go(1);
|
|
507
|
-
else onClose();
|
|
508
|
-
}
|
|
509
|
-
function onCancel(event) {
|
|
510
|
-
event.preventDefault();
|
|
511
|
-
onClose();
|
|
512
|
-
}
|
|
513
|
-
function onDialogClose() {
|
|
514
|
-
if (isOpen.current) onClose();
|
|
515
|
-
}
|
|
516
|
-
function onClick(event) {
|
|
517
|
-
if (event.target === event.currentTarget) onClose();
|
|
518
|
-
}
|
|
519
|
-
const number = index != null ? index + 1 : 0;
|
|
520
|
-
const label = current ? imageLabel(current, number) : "";
|
|
521
|
-
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
522
|
-
"dialog",
|
|
523
|
-
{
|
|
524
|
-
ref: dialog,
|
|
525
|
-
className: "seclai-viewer",
|
|
526
|
-
"aria-labelledby": titleId,
|
|
527
|
-
onKeyDown,
|
|
528
|
-
onCancel,
|
|
529
|
-
onClose: onDialogClose,
|
|
530
|
-
onClick,
|
|
531
|
-
children: current && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "seclai-viewer-body", children: [
|
|
532
|
-
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("img", { className: "seclai-viewer-image", src: current.url, alt: label }),
|
|
533
|
-
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "seclai-viewer-bar", children: [
|
|
534
|
-
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("p", { className: "seclai-viewer-title", id: titleId, children: [
|
|
535
|
-
label,
|
|
536
|
-
attachments.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("span", { className: "seclai-viewer-count", children: [
|
|
537
|
-
number,
|
|
538
|
-
" of ",
|
|
539
|
-
attachments.length
|
|
540
|
-
] })
|
|
541
|
-
] }),
|
|
542
|
-
attachments.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_jsx_runtime4.Fragment, { children: [
|
|
543
|
-
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
544
|
-
"button",
|
|
545
|
-
{
|
|
546
|
-
type: "button",
|
|
547
|
-
className: "seclai-button",
|
|
548
|
-
"aria-disabled": atStart,
|
|
549
|
-
onClick: () => go(-1),
|
|
550
|
-
children: "Previous"
|
|
551
|
-
}
|
|
552
|
-
),
|
|
553
|
-
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("button", { type: "button", className: "seclai-button", "aria-disabled": atEnd, onClick: () => go(1), children: "Next" })
|
|
554
|
-
] }),
|
|
555
|
-
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
556
|
-
"a",
|
|
557
|
-
{
|
|
558
|
-
className: "seclai-button seclai-button-primary",
|
|
559
|
-
href: current.url,
|
|
560
|
-
download: attachmentFileName(current, number),
|
|
561
|
-
children: "Download"
|
|
562
|
-
}
|
|
563
|
-
),
|
|
564
|
-
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("button", { type: "button", className: "seclai-button", ref: closeButton, onClick: onClose, children: "Close" })
|
|
565
|
-
] })
|
|
566
|
-
] })
|
|
567
|
-
}
|
|
568
|
-
);
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
// src/media/MediaGallery.tsx
|
|
572
|
-
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
573
|
-
var DOWNLOAD_ALL_DELAY_MS = SAVE_FILES_DELAY_MS;
|
|
574
|
-
function MediaGallery({
|
|
575
|
-
attachments,
|
|
576
|
-
aspectRatio,
|
|
577
|
-
pending = 0,
|
|
578
|
-
actions
|
|
579
|
-
}) {
|
|
580
|
-
const images = attachments.filter(isImage);
|
|
581
|
-
const files = attachments.filter((attachment) => !isImage(attachment));
|
|
582
|
-
const [viewing, setViewing] = (0, import_react5.useState)(null);
|
|
583
|
-
const [downloading, setDownloading] = (0, import_react5.useState)(false);
|
|
584
|
-
const abort = (0, import_react5.useRef)(null);
|
|
585
|
-
(0, import_react5.useEffect)(() => () => abort.current?.abort(), []);
|
|
586
|
-
if (viewing != null && viewing >= images.length) setViewing(null);
|
|
587
|
-
async function downloadAll() {
|
|
588
|
-
if (downloading) return;
|
|
589
|
-
const controller = new AbortController();
|
|
590
|
-
abort.current = controller;
|
|
591
|
-
setDownloading(true);
|
|
592
|
-
try {
|
|
593
|
-
await saveFiles(
|
|
594
|
-
images.map((image, i) => ({ source: image.url, filename: attachmentFileName(image, i + 1) })),
|
|
595
|
-
{ delayMs: DOWNLOAD_ALL_DELAY_MS, signal: controller.signal }
|
|
596
|
-
);
|
|
597
|
-
} finally {
|
|
598
|
-
if (!controller.signal.aborted) setDownloading(false);
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
const skeletons = Math.max(0, Math.floor(pending));
|
|
602
|
-
return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { className: "seclai-gallery", children: [
|
|
603
|
-
images.length + skeletons > 0 && /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("ul", { className: "seclai-gallery-grid", children: [
|
|
604
|
-
images.map((image, i) => /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("li", { children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
|
|
605
|
-
MediaPreview,
|
|
606
|
-
{
|
|
607
|
-
attachment: image,
|
|
608
|
-
aspectRatio,
|
|
609
|
-
number: i + 1,
|
|
610
|
-
actions: actions?.(image, i + 1),
|
|
611
|
-
onOpen: () => setViewing(i)
|
|
612
|
-
}
|
|
613
|
-
) }, image.id)),
|
|
614
|
-
Array.from({ length: skeletons }, (_, i) => /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("li", { children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(MediaSkeleton, { aspectRatio, number: images.length + i + 1 }) }, `pending-${i}`))
|
|
615
|
-
] }),
|
|
616
|
-
images.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { className: "seclai-gallery-actions", children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
|
|
617
|
-
"button",
|
|
618
|
-
{
|
|
619
|
-
type: "button",
|
|
620
|
-
className: "seclai-button",
|
|
621
|
-
"aria-disabled": downloading,
|
|
622
|
-
onClick: downloadAll,
|
|
623
|
-
children: downloading ? "Downloading\u2026" : `Download all ${images.length}`
|
|
624
|
-
}
|
|
625
|
-
) }),
|
|
626
|
-
files.map((file) => /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(MediaPreview, { attachment: file }, file.id)),
|
|
627
|
-
images.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
|
|
628
|
-
MediaViewer,
|
|
629
|
-
{
|
|
630
|
-
attachments: images,
|
|
631
|
-
index: viewing,
|
|
632
|
-
onIndexChange: setViewing,
|
|
633
|
-
onClose: () => setViewing(null)
|
|
634
|
-
}
|
|
635
|
-
)
|
|
636
|
-
] });
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
// src/RunProgress.tsx
|
|
640
|
-
var import_react6 = require("react");
|
|
641
|
-
var import_jsx_runtime6 = require("react/jsx-runtime");
|
|
642
|
-
function useElapsed(startedAt, running) {
|
|
643
|
-
const [now, setNow] = (0, import_react6.useState)(null);
|
|
644
|
-
(0, import_react6.useEffect)(() => {
|
|
645
|
-
if (!running || startedAt === null) return;
|
|
646
|
-
let timer;
|
|
647
|
-
const tick = () => {
|
|
648
|
-
const time = Date.now();
|
|
649
|
-
setNow(time);
|
|
650
|
-
const into = ((time - startedAt) % 1e3 + 1e3) % 1e3;
|
|
651
|
-
timer = setTimeout(tick, 1e3 - into);
|
|
652
|
-
};
|
|
653
|
-
tick();
|
|
654
|
-
return () => clearTimeout(timer);
|
|
655
|
-
}, [running, startedAt]);
|
|
656
|
-
if (startedAt === null || now === null) return 0;
|
|
657
|
-
return Math.max(0, Math.floor((now - startedAt) / 1e3));
|
|
658
|
-
}
|
|
659
|
-
function formatClock(seconds) {
|
|
660
|
-
return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`;
|
|
661
|
-
}
|
|
662
|
-
function spokenDuration(seconds) {
|
|
663
|
-
const minutes = Math.floor(seconds / 60);
|
|
664
|
-
const rest = seconds % 60;
|
|
665
|
-
const unit = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
666
|
-
if (minutes === 0) return unit(rest, "second");
|
|
667
|
-
return rest === 0 ? unit(minutes, "minute") : `${unit(minutes, "minute")} ${unit(rest, "second")}`;
|
|
668
|
-
}
|
|
669
|
-
function RunProgress({ run, showSteps = true, message, className }) {
|
|
670
|
-
const running = run.status === "running";
|
|
671
|
-
const seconds = useElapsed(run.startedAt, running);
|
|
672
|
-
if (!running) return null;
|
|
673
|
-
const note = typeof message === "function" ? message(seconds) : message;
|
|
674
|
-
const last = run.steps.length - 1;
|
|
675
|
-
return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
|
|
676
|
-
"div",
|
|
677
|
-
{
|
|
678
|
-
className: className ? `seclai-run-progress ${className}` : "seclai-run-progress",
|
|
679
|
-
"aria-live": "polite",
|
|
680
|
-
children: [
|
|
681
|
-
showSteps && (run.steps.length === 0 ? /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("p", { className: "seclai-notice", children: "Starting the run\u2026" }) : /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("ol", { className: "seclai-steps", children: run.steps.map((step, i) => /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("li", { children: [
|
|
682
|
-
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { className: "seclai-step-marker", "aria-hidden": "true", children: i === last ? "\u25CF" : "\u2713" }),
|
|
683
|
-
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { className: "seclai-sr-only", children: i === last ? "In progress: " : "Done: " }),
|
|
684
|
-
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("span", { children: [
|
|
685
|
-
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { className: "seclai-step-name", children: step.name }),
|
|
686
|
-
step.message && /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("span", { className: "seclai-step-message", children: [
|
|
687
|
-
" \xB7 ",
|
|
688
|
-
step.message
|
|
689
|
-
] })
|
|
690
|
-
] })
|
|
691
|
-
] }, step.id)) })),
|
|
692
|
-
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "seclai-run-progress-note", children: [
|
|
693
|
-
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { className: "seclai-run-progress-pulse", "aria-hidden": "true" }),
|
|
694
|
-
note != null && note !== false && note !== "" && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { className: "seclai-run-progress-message", children: note }),
|
|
695
|
-
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
|
696
|
-
"span",
|
|
697
|
-
{
|
|
698
|
-
className: "seclai-run-progress-clock",
|
|
699
|
-
role: "timer",
|
|
700
|
-
"aria-live": "off",
|
|
701
|
-
"aria-label": `${spokenDuration(seconds)} elapsed`,
|
|
702
|
-
children: formatClock(seconds)
|
|
703
|
-
}
|
|
704
|
-
)
|
|
705
|
-
] })
|
|
706
|
-
]
|
|
707
|
-
}
|
|
708
|
-
);
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
// src/useAgentRun.ts
|
|
712
|
-
var import_react7 = require("react");
|
|
713
|
-
|
|
714
|
-
// src/sse.ts
|
|
715
|
-
function parseData(data) {
|
|
716
|
-
try {
|
|
717
|
-
return JSON.parse(data);
|
|
718
|
-
} catch {
|
|
719
|
-
return data;
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
async function* readSseStream(stream) {
|
|
723
|
-
const reader = stream.getReader();
|
|
724
|
-
const decoder = new TextDecoder();
|
|
725
|
-
let buffer = "";
|
|
726
|
-
let event = "message";
|
|
727
|
-
let dataLines = [];
|
|
728
|
-
function takeEvent() {
|
|
729
|
-
const parsed = dataLines.length ? { event, data: parseData(dataLines.join("\n")) } : void 0;
|
|
730
|
-
event = "message";
|
|
731
|
-
dataLines = [];
|
|
732
|
-
return parsed;
|
|
733
|
-
}
|
|
734
|
-
function* drain(final) {
|
|
735
|
-
while (true) {
|
|
736
|
-
const match = /\r\n|\n|\r/.exec(buffer);
|
|
737
|
-
if (!match) return;
|
|
738
|
-
if (match[0] === "\r" && match.index === buffer.length - 1 && !final) return;
|
|
739
|
-
const line = buffer.slice(0, match.index);
|
|
740
|
-
buffer = buffer.slice(match.index + match[0].length);
|
|
741
|
-
if (line === "") {
|
|
742
|
-
const parsed = takeEvent();
|
|
743
|
-
if (parsed) yield parsed;
|
|
744
|
-
continue;
|
|
745
|
-
}
|
|
746
|
-
if (line.startsWith(":")) continue;
|
|
747
|
-
const colon = line.indexOf(":");
|
|
748
|
-
const field = colon === -1 ? line : line.slice(0, colon);
|
|
749
|
-
let value = colon === -1 ? "" : line.slice(colon + 1);
|
|
750
|
-
if (value.startsWith(" ")) value = value.slice(1);
|
|
751
|
-
if (field === "event") event = value;
|
|
752
|
-
else if (field === "data") dataLines.push(value);
|
|
753
|
-
}
|
|
754
|
-
}
|
|
755
|
-
try {
|
|
756
|
-
while (true) {
|
|
757
|
-
const { value, done } = await reader.read();
|
|
758
|
-
if (done) break;
|
|
759
|
-
buffer += decoder.decode(value, { stream: true });
|
|
760
|
-
yield* drain(false);
|
|
761
|
-
}
|
|
762
|
-
buffer += decoder.decode();
|
|
763
|
-
buffer += "\n\n";
|
|
764
|
-
yield* drain(true);
|
|
765
|
-
} finally {
|
|
766
|
-
void reader.cancel().catch(() => {
|
|
767
|
-
});
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
|
|
771
|
-
// src/useAgentRun.ts
|
|
772
|
-
var IDLE = {
|
|
773
|
-
status: "idle",
|
|
774
|
-
text: "",
|
|
775
|
-
steps: [],
|
|
776
|
-
attachments: [],
|
|
777
|
-
error: null,
|
|
778
|
-
runId: null,
|
|
779
|
-
startedAt: null
|
|
780
|
-
};
|
|
781
|
-
function toAttachments(files, runId, endpoints) {
|
|
782
|
-
if (!files || !runId) return [];
|
|
783
|
-
return files.map(({ id, name, mime, bytes }) => ({
|
|
784
|
-
id,
|
|
785
|
-
name,
|
|
786
|
-
mime,
|
|
787
|
-
bytes,
|
|
788
|
-
url: endpoints.download(runId, id, name)
|
|
789
|
-
}));
|
|
790
|
-
}
|
|
791
|
-
function lastAttemptError(run) {
|
|
792
|
-
for (const attempt of [...run.attempts ?? []].reverse()) {
|
|
793
|
-
if (attempt.error) return attempt.error;
|
|
794
|
-
}
|
|
795
|
-
return null;
|
|
796
|
-
}
|
|
797
|
-
function applyEvent(state, { event, data }, endpoints) {
|
|
798
|
-
const payload = typeof data === "object" && data !== null ? data : {};
|
|
799
|
-
switch (event) {
|
|
800
|
-
case "init":
|
|
801
|
-
return { ...state, runId: typeof payload.run_id === "string" ? payload.run_id : state.runId };
|
|
802
|
-
case "stream_token":
|
|
803
|
-
return typeof payload.token === "string" ? { ...state, text: state.text + payload.token } : state;
|
|
804
|
-
case "step_progress": {
|
|
805
|
-
if (typeof payload.step_id !== "string") return state;
|
|
806
|
-
const step = {
|
|
807
|
-
id: payload.step_id,
|
|
808
|
-
name: typeof payload.step_name === "string" ? payload.step_name : payload.step_id,
|
|
809
|
-
message: typeof payload.message === "string" ? payload.message : ""
|
|
810
|
-
};
|
|
811
|
-
const known = state.steps.some((s) => s.id === step.id);
|
|
812
|
-
return {
|
|
813
|
-
...state,
|
|
814
|
-
steps: known ? state.steps.map((s) => s.id === step.id ? step : s) : [...state.steps, step]
|
|
815
|
-
};
|
|
816
|
-
}
|
|
817
|
-
case "done": {
|
|
818
|
-
const run = payload;
|
|
819
|
-
const runId = run.run_id ?? state.runId ?? void 0;
|
|
820
|
-
const attachments = toAttachments(run.attachments, runId, endpoints);
|
|
821
|
-
if (run.status === "failed") {
|
|
822
|
-
return {
|
|
823
|
-
...state,
|
|
824
|
-
status: "failed",
|
|
825
|
-
runId: runId ?? null,
|
|
826
|
-
attachments,
|
|
827
|
-
error: lastAttemptError(run) ?? "The run failed."
|
|
828
|
-
};
|
|
829
|
-
}
|
|
830
|
-
return {
|
|
831
|
-
...state,
|
|
832
|
-
status: "completed",
|
|
833
|
-
runId: runId ?? null,
|
|
834
|
-
attachments,
|
|
835
|
-
// `output` wins over the tokens, which are only a progress channel. Agents
|
|
836
|
-
// with no streaming step send no tokens, so this is their only text.
|
|
837
|
-
text: run.output || state.text
|
|
838
|
-
};
|
|
839
|
-
}
|
|
840
|
-
case "error":
|
|
841
|
-
return {
|
|
842
|
-
...state,
|
|
843
|
-
status: "failed",
|
|
844
|
-
error: typeof payload.error === "string" ? payload.error : "The run failed."
|
|
845
|
-
};
|
|
846
|
-
case "timeout":
|
|
847
|
-
return {
|
|
848
|
-
...state,
|
|
849
|
-
status: "failed",
|
|
850
|
-
error: "Stopped waiting for the run. It may still finish on Seclai."
|
|
851
|
-
};
|
|
852
|
-
default:
|
|
853
|
-
return state;
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
function useAgentRun(options = {}) {
|
|
857
|
-
const { basePath, agentId, onEvent } = options;
|
|
858
|
-
const endpoints = (0, import_react7.useMemo)(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
859
|
-
const [state, setState] = (0, import_react7.useState)(IDLE);
|
|
860
|
-
const active = (0, import_react7.useRef)(null);
|
|
861
|
-
const onEventRef = (0, import_react7.useRef)(onEvent);
|
|
862
|
-
(0, import_react7.useEffect)(() => {
|
|
863
|
-
onEventRef.current = onEvent;
|
|
864
|
-
});
|
|
865
|
-
(0, import_react7.useEffect)(() => () => active.current?.abort(), []);
|
|
866
|
-
const start = (0, import_react7.useCallback)(
|
|
867
|
-
async ({ input, uploadIds, sessionId }) => {
|
|
868
|
-
active.current?.abort();
|
|
869
|
-
const controller = new AbortController();
|
|
870
|
-
active.current = controller;
|
|
871
|
-
let current = { ...IDLE, status: "running", startedAt: Date.now() };
|
|
872
|
-
const update = (next) => {
|
|
873
|
-
current = next;
|
|
874
|
-
if (active.current === controller) setState(next);
|
|
875
|
-
};
|
|
876
|
-
update(current);
|
|
877
|
-
try {
|
|
878
|
-
const res = await fetch(endpoints.run, {
|
|
879
|
-
method: "POST",
|
|
880
|
-
headers: { "content-type": "application/json" },
|
|
881
|
-
body: JSON.stringify({ input, uploadIds, sessionId }),
|
|
882
|
-
signal: controller.signal
|
|
883
|
-
});
|
|
884
|
-
if (!res.ok) {
|
|
885
|
-
update({ ...current, status: "failed", error: await readErrorMessage(res) });
|
|
886
|
-
return current;
|
|
887
|
-
}
|
|
888
|
-
if (!res.body) {
|
|
889
|
-
update({ ...current, status: "failed", error: "The response had no body to stream." });
|
|
890
|
-
return current;
|
|
891
|
-
}
|
|
892
|
-
for await (const event of readSseStream(res.body)) {
|
|
893
|
-
if (controller.signal.aborted) break;
|
|
894
|
-
update(applyEvent(current, event, endpoints));
|
|
895
|
-
try {
|
|
896
|
-
onEventRef.current?.(event);
|
|
897
|
-
} catch (err) {
|
|
898
|
-
console.error("useAgentRun: onEvent threw", err);
|
|
899
|
-
}
|
|
900
|
-
if (current.status !== "running") break;
|
|
901
|
-
}
|
|
902
|
-
if (controller.signal.aborted) {
|
|
903
|
-
if (current.status === "running") update({ ...current, status: "cancelled" });
|
|
904
|
-
} else if (current.status === "running") {
|
|
905
|
-
update({
|
|
906
|
-
...current,
|
|
907
|
-
status: "failed",
|
|
908
|
-
error: "The connection closed before the run finished."
|
|
909
|
-
});
|
|
910
|
-
}
|
|
911
|
-
} catch (err) {
|
|
912
|
-
if (controller.signal.aborted) {
|
|
913
|
-
update({ ...current, status: "cancelled" });
|
|
914
|
-
} else {
|
|
915
|
-
update({
|
|
916
|
-
...current,
|
|
917
|
-
status: "failed",
|
|
918
|
-
error: err instanceof Error ? err.message : "The run failed."
|
|
919
|
-
});
|
|
920
|
-
}
|
|
921
|
-
}
|
|
922
|
-
return current;
|
|
923
|
-
},
|
|
924
|
-
[endpoints]
|
|
925
|
-
);
|
|
926
|
-
const cancel = (0, import_react7.useCallback)(() => active.current?.abort(), []);
|
|
927
|
-
const reset = (0, import_react7.useCallback)(() => {
|
|
928
|
-
const controller = active.current;
|
|
929
|
-
active.current = null;
|
|
930
|
-
controller?.abort();
|
|
931
|
-
setState(IDLE);
|
|
932
|
-
}, []);
|
|
933
|
-
return { ...state, start, cancel, reset };
|
|
934
|
-
}
|
|
935
|
-
|
|
936
|
-
// src/AgentChat.tsx
|
|
937
|
-
var import_jsx_runtime7 = require("react/jsx-runtime");
|
|
938
|
-
function createSessionId() {
|
|
939
|
-
if (typeof crypto.randomUUID === "function") return crypto.randomUUID();
|
|
940
|
-
return Array.from(
|
|
941
|
-
crypto.getRandomValues(new Uint8Array(16)),
|
|
942
|
-
(byte) => byte.toString(16).padStart(2, "0")
|
|
943
|
-
).join("");
|
|
944
|
-
}
|
|
945
|
-
var nextLocalId = 0;
|
|
946
|
-
function createPreview(file) {
|
|
947
|
-
if (!file.type.startsWith("image/") || typeof URL.createObjectURL !== "function") return null;
|
|
948
|
-
return URL.createObjectURL(file);
|
|
949
|
-
}
|
|
950
|
-
function revoke(url) {
|
|
951
|
-
if (url && typeof URL.revokeObjectURL === "function") URL.revokeObjectURL(url);
|
|
952
|
-
}
|
|
953
|
-
function hasFiles(dataTransfer) {
|
|
954
|
-
return !!dataTransfer && Array.from(dataTransfer.types ?? []).includes("Files");
|
|
955
|
-
}
|
|
956
|
-
function plural(count, word) {
|
|
957
|
-
return `${count} ${word}${count === 1 ? "" : "s"}`;
|
|
958
|
-
}
|
|
959
|
-
function FileChip({ name, tag, preview, status, error, onRemove }) {
|
|
960
|
-
const meta = status === "uploading" ? "Uploading\u2026" : status === "error" ? error : tag;
|
|
961
|
-
return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("li", { className: "seclai-file", "data-status": status, children: [
|
|
962
|
-
preview && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("img", { className: "seclai-file-thumb", src: preview, alt: "" }),
|
|
963
|
-
/* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("span", { className: "seclai-file-text", children: [
|
|
964
|
-
/* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { className: "seclai-file-name", title: name, children: name }),
|
|
965
|
-
meta && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { className: "seclai-file-meta", children: meta })
|
|
966
|
-
] }),
|
|
967
|
-
onRemove && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
|
|
968
|
-
"button",
|
|
969
|
-
{
|
|
970
|
-
type: "button",
|
|
971
|
-
className: "seclai-file-remove",
|
|
972
|
-
"aria-label": `Remove ${name}`,
|
|
973
|
-
onClick: onRemove,
|
|
974
|
-
children: "\xD7"
|
|
975
|
-
}
|
|
976
|
-
)
|
|
977
|
-
] });
|
|
978
|
-
}
|
|
979
|
-
function UserTurn({ message, previews }) {
|
|
980
|
-
return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("li", { className: "seclai-turn seclai-turn-user", children: [
|
|
981
|
-
message.files.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("ul", { className: "seclai-files", "aria-label": "Attached files", children: message.files.map((file) => /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
|
|
982
|
-
FileChip,
|
|
983
|
-
{
|
|
984
|
-
name: file.name,
|
|
985
|
-
tag: file.tag,
|
|
986
|
-
preview: previews.get(file.uploadId)
|
|
987
|
-
},
|
|
988
|
-
file.uploadId
|
|
989
|
-
)) }),
|
|
990
|
-
message.input && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(MarkdownContent, { content: message.input })
|
|
991
|
-
] });
|
|
992
|
-
}
|
|
993
|
-
function AssistantTurn({ turn, endpoints }) {
|
|
994
|
-
const { runId } = turn;
|
|
995
|
-
return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("li", { className: "seclai-turn seclai-turn-assistant", children: [
|
|
996
|
-
turn.output && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(MarkdownContent, { content: turn.output }),
|
|
997
|
-
runId && turn.attachments && turn.attachments.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
|
|
998
|
-
MediaGallery,
|
|
999
|
-
{
|
|
1000
|
-
attachments: turn.attachments.map(({ id, name, mime, bytes }) => ({
|
|
1001
|
-
id,
|
|
1002
|
-
name,
|
|
1003
|
-
mime,
|
|
1004
|
-
bytes,
|
|
1005
|
-
url: endpoints.download(runId, id, name)
|
|
1006
|
-
}))
|
|
1007
|
-
}
|
|
1008
|
-
),
|
|
1009
|
-
turn.error && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("p", { className: "seclai-error", role: "alert", children: turn.error }),
|
|
1010
|
-
turn.status === "cancelled" && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("p", { className: "seclai-notice", children: "Stopped." })
|
|
1011
|
-
] });
|
|
1012
|
-
}
|
|
1013
|
-
function toTurnAttachment({ id, name, mime, bytes }) {
|
|
1014
|
-
return { id, name, mime, bytes };
|
|
1015
|
-
}
|
|
1016
|
-
function AttachControl({
|
|
1017
|
-
label,
|
|
1018
|
-
accept,
|
|
1019
|
-
multiple,
|
|
1020
|
-
disabled,
|
|
1021
|
-
onFiles
|
|
1022
|
-
}) {
|
|
1023
|
-
return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("label", { className: "seclai-button seclai-attach", "aria-disabled": disabled || void 0, children: [
|
|
1024
|
-
label,
|
|
1025
|
-
/* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
|
|
1026
|
-
"input",
|
|
1027
|
-
{
|
|
1028
|
-
type: "file",
|
|
1029
|
-
className: "seclai-sr-only",
|
|
1030
|
-
accept,
|
|
1031
|
-
multiple,
|
|
1032
|
-
disabled,
|
|
1033
|
-
onChange: (e) => {
|
|
1034
|
-
const picked = Array.from(e.target.files ?? []);
|
|
1035
|
-
e.target.value = "";
|
|
1036
|
-
if (picked.length) onFiles(picked);
|
|
1037
|
-
}
|
|
1038
|
-
}
|
|
1039
|
-
)
|
|
1040
|
-
] });
|
|
1041
|
-
}
|
|
1042
|
-
function AgentChat({
|
|
1043
|
-
basePath,
|
|
1044
|
-
agentId,
|
|
1045
|
-
placeholder = "Send a message",
|
|
1046
|
-
attachButtons,
|
|
1047
|
-
onBeforeSend,
|
|
1048
|
-
sessionId,
|
|
1049
|
-
initialTurns,
|
|
1050
|
-
onTurnComplete
|
|
1051
|
-
}) {
|
|
1052
|
-
const endpoints = (0, import_react8.useMemo)(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
1053
|
-
const run = useAgentRun({ basePath, agentId });
|
|
1054
|
-
const { slots } = useAcceptedAttachments(endpoints);
|
|
1055
|
-
const [transcript, setTranscript] = (0, import_react8.useState)(() => ({
|
|
1056
|
-
sessionId,
|
|
1057
|
-
epoch: 0,
|
|
1058
|
-
turns: initialTurns ?? [],
|
|
1059
|
-
pending: null,
|
|
1060
|
-
generatedSessionId: null,
|
|
1061
|
-
runEpoch: null
|
|
1062
|
-
}));
|
|
1063
|
-
const [message, setMessage] = (0, import_react8.useState)("");
|
|
1064
|
-
const [files, setFiles] = (0, import_react8.useState)([]);
|
|
1065
|
-
const [notice, setNotice] = (0, import_react8.useState)(null);
|
|
1066
|
-
const [dragging, setDragging] = (0, import_react8.useState)(false);
|
|
1067
|
-
const generatedSessionId = (0, import_react8.useRef)(null);
|
|
1068
|
-
const shownEpoch = (0, import_react8.useRef)(transcript.epoch);
|
|
1069
|
-
const turnComplete = (0, import_react8.useRef)(onTurnComplete);
|
|
1070
|
-
const sentPreviews = (0, import_react8.useRef)(/* @__PURE__ */ new Map());
|
|
1071
|
-
const pendingPreviews = (0, import_react8.useRef)(/* @__PURE__ */ new Set());
|
|
1072
|
-
if (transcript.sessionId !== sessionId) {
|
|
1073
|
-
const adopting = transcript.sessionId === void 0 && sessionId !== void 0 && sessionId === transcript.generatedSessionId;
|
|
1074
|
-
setTranscript(
|
|
1075
|
-
adopting ? { ...transcript, sessionId } : {
|
|
1076
|
-
sessionId,
|
|
1077
|
-
epoch: transcript.epoch + 1,
|
|
1078
|
-
turns: initialTurns ?? [],
|
|
1079
|
-
pending: null,
|
|
1080
|
-
generatedSessionId: null,
|
|
1081
|
-
runEpoch: null
|
|
1082
|
-
}
|
|
1083
|
-
);
|
|
1084
|
-
}
|
|
1085
|
-
(0, import_react8.useEffect)(() => {
|
|
1086
|
-
turnComplete.current = onTurnComplete;
|
|
1087
|
-
});
|
|
1088
|
-
(0, import_react8.useEffect)(() => {
|
|
1089
|
-
if (shownEpoch.current === transcript.epoch) return;
|
|
1090
|
-
shownEpoch.current = transcript.epoch;
|
|
1091
|
-
run.cancel();
|
|
1092
|
-
generatedSessionId.current = null;
|
|
1093
|
-
pendingPreviews.current.forEach(revoke);
|
|
1094
|
-
pendingPreviews.current.clear();
|
|
1095
|
-
sentPreviews.current.forEach(revoke);
|
|
1096
|
-
sentPreviews.current.clear();
|
|
1097
|
-
setMessage("");
|
|
1098
|
-
setFiles([]);
|
|
1099
|
-
setNotice(null);
|
|
1100
|
-
}, [transcript.epoch, run.cancel]);
|
|
1101
|
-
(0, import_react8.useEffect)(
|
|
1102
|
-
() => () => {
|
|
1103
|
-
pendingPreviews.current.forEach(revoke);
|
|
1104
|
-
sentPreviews.current.forEach(revoke);
|
|
1105
|
-
},
|
|
1106
|
-
[]
|
|
1107
|
-
);
|
|
1108
|
-
const limits = slots?.length ? messageFileLimits(slots) : null;
|
|
1109
|
-
const acceptsFiles = !!limits && (limits.max == null || limits.max > 0);
|
|
1110
|
-
const running = run.status === "running";
|
|
1111
|
-
const streaming = running && transcript.runEpoch === transcript.epoch;
|
|
1112
|
-
const uploading = files.some((f) => f.status === "uploading");
|
|
1113
|
-
const ready = files.filter((f) => f.status === "ready" && f.uploadId);
|
|
1114
|
-
const room = limits?.max == null ? Infinity : limits.max - files.filter((f) => f.status !== "error").length;
|
|
1115
|
-
const canSend = !running && !uploading && (!!message.trim() || ready.length > 0);
|
|
1116
|
-
function setFile(localId, patch) {
|
|
1117
|
-
setFiles((prev) => prev.map((f) => f.localId === localId ? { ...f, ...patch } : f));
|
|
1118
|
-
}
|
|
1119
|
-
function upload(item, file) {
|
|
1120
|
-
uploadFile(endpoints, file).then(
|
|
1121
|
-
(result) => setFile(item.localId, { status: "ready", uploadId: result.id }),
|
|
1122
|
-
(err) => setFile(item.localId, {
|
|
1123
|
-
status: "error",
|
|
1124
|
-
error: err instanceof Error ? err.message : "The upload failed."
|
|
1125
|
-
})
|
|
1126
|
-
);
|
|
1127
|
-
}
|
|
1128
|
-
function addFiles(incoming, tag) {
|
|
1129
|
-
if (!acceptsFiles || !limits || incoming.length === 0) return;
|
|
1130
|
-
const problems = [];
|
|
1131
|
-
const added = [];
|
|
1132
|
-
let left = room;
|
|
1133
|
-
let tooMany = false;
|
|
1134
|
-
for (const file of incoming) {
|
|
1135
|
-
if (!acceptsType(limits.contentTypes, file.type)) {
|
|
1136
|
-
problems.push(`${file.name} isn't a type of file this agent accepts.`);
|
|
1137
|
-
continue;
|
|
1138
|
-
}
|
|
1139
|
-
if (left <= 0) {
|
|
1140
|
-
tooMany = true;
|
|
1141
|
-
continue;
|
|
1142
|
-
}
|
|
1143
|
-
left--;
|
|
1144
|
-
const preview = createPreview(file);
|
|
1145
|
-
if (preview) pendingPreviews.current.add(preview);
|
|
1146
|
-
added.push([
|
|
1147
|
-
{
|
|
1148
|
-
localId: `file-${++nextLocalId}`,
|
|
1149
|
-
name: file.name,
|
|
1150
|
-
contentType: file.type,
|
|
1151
|
-
tag,
|
|
1152
|
-
preview,
|
|
1153
|
-
status: "uploading"
|
|
1154
|
-
},
|
|
1155
|
-
file
|
|
1156
|
-
]);
|
|
1157
|
-
}
|
|
1158
|
-
if (tooMany) problems.push(`You can attach up to ${plural(limits.max, "file")} per message.`);
|
|
1159
|
-
setNotice(problems.length ? problems.join(" ") : null);
|
|
1160
|
-
if (added.length === 0) return;
|
|
1161
|
-
setFiles((prev) => [...prev, ...added.map(([item]) => item)]);
|
|
1162
|
-
for (const [item, file] of added) upload(item, file);
|
|
1163
|
-
}
|
|
1164
|
-
function remove(localId) {
|
|
1165
|
-
const gone = files.find((f) => f.localId === localId);
|
|
1166
|
-
if (gone?.preview) {
|
|
1167
|
-
revoke(gone.preview);
|
|
1168
|
-
pendingPreviews.current.delete(gone.preview);
|
|
1169
|
-
}
|
|
1170
|
-
setFiles((prev) => prev.filter((f) => f.localId !== localId));
|
|
1171
|
-
setNotice(null);
|
|
1172
|
-
}
|
|
1173
|
-
async function send() {
|
|
1174
|
-
if (!canSend) return;
|
|
1175
|
-
const text = message.trim();
|
|
1176
|
-
const sent = ready.map((f) => ({
|
|
1177
|
-
uploadId: f.uploadId,
|
|
1178
|
-
name: f.name,
|
|
1179
|
-
contentType: f.contentType,
|
|
1180
|
-
...f.tag ? { tag: f.tag } : {}
|
|
1181
|
-
}));
|
|
1182
|
-
const override = onBeforeSend?.({ input: text, files: sent });
|
|
1183
|
-
const input = (typeof override === "string" ? override : text) || void 0;
|
|
1184
|
-
for (const f of files) {
|
|
1185
|
-
if (!f.preview) continue;
|
|
1186
|
-
pendingPreviews.current.delete(f.preview);
|
|
1187
|
-
if (f.status === "ready" && f.uploadId) sentPreviews.current.set(f.uploadId, f.preview);
|
|
1188
|
-
else revoke(f.preview);
|
|
1189
|
-
}
|
|
1190
|
-
const runSessionId = sessionId ?? (generatedSessionId.current ??= createSessionId());
|
|
1191
|
-
const { epoch } = transcript;
|
|
1192
|
-
setMessage("");
|
|
1193
|
-
setFiles([]);
|
|
1194
|
-
setNotice(null);
|
|
1195
|
-
setTranscript((prev) => ({
|
|
1196
|
-
...prev,
|
|
1197
|
-
pending: { input: text, files: sent },
|
|
1198
|
-
runEpoch: epoch,
|
|
1199
|
-
...sessionId === void 0 ? { generatedSessionId: runSessionId } : {}
|
|
1200
|
-
}));
|
|
1201
|
-
const result = await run.start({
|
|
1202
|
-
input,
|
|
1203
|
-
uploadIds: sent.length ? sent.map((f) => f.uploadId) : void 0,
|
|
1204
|
-
sessionId: runSessionId
|
|
1205
|
-
});
|
|
1206
|
-
const turn = {
|
|
1207
|
-
input: text,
|
|
1208
|
-
...typeof override === "string" && override !== text ? { sentInput: override } : {},
|
|
1209
|
-
files: sent,
|
|
1210
|
-
output: result.text,
|
|
1211
|
-
attachments: result.attachments.map(toTurnAttachment),
|
|
1212
|
-
runId: result.runId,
|
|
1213
|
-
// `start` always settles, so the run is over.
|
|
1214
|
-
status: result.status,
|
|
1215
|
-
error: result.error
|
|
1216
|
-
};
|
|
1217
|
-
setTranscript(
|
|
1218
|
-
(prev) => prev.epoch === epoch ? { ...prev, turns: [...prev.turns, turn], pending: null, runEpoch: null } : prev
|
|
1219
|
-
);
|
|
1220
|
-
turnComplete.current?.(turn, { sessionId: runSessionId });
|
|
1221
|
-
}
|
|
1222
|
-
function onSubmit(event) {
|
|
1223
|
-
event.preventDefault();
|
|
1224
|
-
void send();
|
|
1225
|
-
}
|
|
1226
|
-
function onKeyDown(event) {
|
|
1227
|
-
if (event.key === "Enter" && !event.shiftKey && !event.nativeEvent.isComposing) {
|
|
1228
|
-
event.preventDefault();
|
|
1229
|
-
void send();
|
|
1230
|
-
}
|
|
1231
|
-
}
|
|
1232
|
-
function onPaste(event) {
|
|
1233
|
-
const data = event.clipboardData;
|
|
1234
|
-
const pasted = Array.from(data?.files ?? []);
|
|
1235
|
-
if (pasted.length === 0) return;
|
|
1236
|
-
if (data.getData("text/plain")) return;
|
|
1237
|
-
event.preventDefault();
|
|
1238
|
-
addFiles(pasted);
|
|
1239
|
-
}
|
|
1240
|
-
const dropHandlers = acceptsFiles ? {
|
|
1241
|
-
onDragOver(event) {
|
|
1242
|
-
if (!hasFiles(event.dataTransfer)) return;
|
|
1243
|
-
event.preventDefault();
|
|
1244
|
-
setDragging(true);
|
|
1245
|
-
},
|
|
1246
|
-
onDragLeave(event) {
|
|
1247
|
-
if (!event.currentTarget.contains(event.relatedTarget)) setDragging(false);
|
|
1248
|
-
},
|
|
1249
|
-
onDrop(event) {
|
|
1250
|
-
if (!hasFiles(event.dataTransfer)) return;
|
|
1251
|
-
event.preventDefault();
|
|
1252
|
-
setDragging(false);
|
|
1253
|
-
addFiles(Array.from(event.dataTransfer.files));
|
|
1254
|
-
}
|
|
1255
|
-
} : {};
|
|
1256
|
-
const buttons = attachButtons?.length ? attachButtons : [{ label: "Attach" }];
|
|
1257
|
-
return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "seclai-agent seclai-chat", children: [
|
|
1258
|
-
/* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("ul", { className: "seclai-turns", "aria-live": "polite", "aria-busy": streaming, children: [
|
|
1259
|
-
transcript.turns.map((turn, i) => /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(import_react8.Fragment, { children: [
|
|
1260
|
-
/* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
|
|
1261
|
-
UserTurn,
|
|
1262
|
-
{
|
|
1263
|
-
message: { input: turn.input, files: turn.files ?? [] },
|
|
1264
|
-
previews: sentPreviews.current
|
|
1265
|
-
}
|
|
1266
|
-
),
|
|
1267
|
-
/* @__PURE__ */ (0, import_jsx_runtime7.jsx)(AssistantTurn, { turn, endpoints })
|
|
1268
|
-
] }, `${transcript.epoch}:${i}`)),
|
|
1269
|
-
transcript.pending && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(UserTurn, { message: transcript.pending, previews: sentPreviews.current }),
|
|
1270
|
-
streaming && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("li", { className: "seclai-turn seclai-turn-assistant", children: run.text ? /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(MarkdownContent, { content: run.text }) : /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(RunProgress, { run }) })
|
|
1271
|
-
] }),
|
|
1272
|
-
/* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(
|
|
1273
|
-
"form",
|
|
1274
|
-
{
|
|
1275
|
-
className: dragging ? "seclai-form seclai-dropping" : "seclai-form",
|
|
1276
|
-
onSubmit,
|
|
1277
|
-
...dropHandlers,
|
|
1278
|
-
children: [
|
|
1279
|
-
files.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("ul", { className: "seclai-files", "aria-label": "Files to send", children: files.map((f) => /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
|
|
1280
|
-
FileChip,
|
|
1281
|
-
{
|
|
1282
|
-
name: f.name,
|
|
1283
|
-
tag: f.tag,
|
|
1284
|
-
preview: f.preview,
|
|
1285
|
-
status: f.status,
|
|
1286
|
-
error: f.error,
|
|
1287
|
-
onRemove: () => remove(f.localId)
|
|
1288
|
-
},
|
|
1289
|
-
f.localId
|
|
1290
|
-
)) }),
|
|
1291
|
-
/* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
|
|
1292
|
-
"textarea",
|
|
1293
|
-
{
|
|
1294
|
-
rows: 2,
|
|
1295
|
-
"aria-label": "Message",
|
|
1296
|
-
placeholder,
|
|
1297
|
-
value: message,
|
|
1298
|
-
onChange: (e) => setMessage(e.target.value),
|
|
1299
|
-
onKeyDown,
|
|
1300
|
-
onPaste: acceptsFiles ? onPaste : void 0
|
|
1301
|
-
}
|
|
1302
|
-
),
|
|
1303
|
-
notice && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("p", { className: "seclai-error", role: "alert", children: notice }),
|
|
1304
|
-
/* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "seclai-actions", children: [
|
|
1305
|
-
acceptsFiles && buttons.map((button) => /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
|
|
1306
|
-
AttachControl,
|
|
1307
|
-
{
|
|
1308
|
-
label: button.label,
|
|
1309
|
-
accept: acceptAttribute(limits.contentTypes),
|
|
1310
|
-
multiple: room > 1,
|
|
1311
|
-
disabled: room <= 0,
|
|
1312
|
-
onFiles: (picked) => addFiles(picked, button.tag)
|
|
1313
|
-
},
|
|
1314
|
-
`${button.label}\0${button.tag ?? ""}`
|
|
1315
|
-
)),
|
|
1316
|
-
/* @__PURE__ */ (0, import_jsx_runtime7.jsx)("button", { type: "submit", className: "seclai-button seclai-button-primary", disabled: !canSend, children: "Send" }),
|
|
1317
|
-
running && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("button", { type: "button", className: "seclai-button", onClick: run.cancel, children: "Stop" })
|
|
1318
|
-
] })
|
|
1319
|
-
]
|
|
1320
|
-
}
|
|
1321
|
-
)
|
|
1322
|
-
] });
|
|
1323
|
-
}
|
|
1324
|
-
|
|
1325
|
-
// src/AgentRunForm.tsx
|
|
1326
|
-
var import_react9 = require("react");
|
|
1327
|
-
var import_jsx_runtime8 = require("react/jsx-runtime");
|
|
1328
|
-
function slotProblem(slot, count) {
|
|
1329
|
-
const label = slot.label ?? slot.name;
|
|
1330
|
-
const min = slot.min ?? 0;
|
|
1331
|
-
if (count < min) return `${label} needs at least ${min} file${min === 1 ? "" : "s"}.`;
|
|
1332
|
-
if (slot.max != null && count > slot.max) {
|
|
1333
|
-
return `${label} takes at most ${slot.max} file${slot.max === 1 ? "" : "s"}.`;
|
|
1334
|
-
}
|
|
1335
|
-
return null;
|
|
1336
|
-
}
|
|
1337
|
-
function AgentRunForm({ basePath, agentId, inputLabel = "Input", onFinish }) {
|
|
1338
|
-
const endpoints = (0, import_react9.useMemo)(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
1339
|
-
const run = useAgentRun({ basePath, agentId });
|
|
1340
|
-
const { slots, error: loadError } = useAcceptedAttachments(endpoints);
|
|
1341
|
-
const [input, setInput] = (0, import_react9.useState)("");
|
|
1342
|
-
const [files, setFiles] = (0, import_react9.useState)({});
|
|
1343
|
-
const [uploading, setUploading] = (0, import_react9.useState)(false);
|
|
1344
|
-
const [formError, setFormError] = (0, import_react9.useState)(null);
|
|
1345
|
-
const selected = Object.values(files).flat();
|
|
1346
|
-
const busy = uploading || run.status === "running";
|
|
1347
|
-
async function submit(event) {
|
|
1348
|
-
event.preventDefault();
|
|
1349
|
-
if (busy || !slots) return;
|
|
1350
|
-
const problem = slots.map((slot) => slotProblem(slot, files[slot.name]?.length ?? 0)).find(Boolean);
|
|
1351
|
-
if (problem) {
|
|
1352
|
-
setFormError(problem);
|
|
1353
|
-
return;
|
|
1354
|
-
}
|
|
1355
|
-
if (!input.trim() && selected.length === 0) {
|
|
1356
|
-
setFormError("Enter some text or choose a file.");
|
|
1357
|
-
return;
|
|
1358
|
-
}
|
|
1359
|
-
setFormError(null);
|
|
1360
|
-
setUploading(true);
|
|
1361
|
-
let uploadIds;
|
|
1362
|
-
try {
|
|
1363
|
-
const uploads = await Promise.all(selected.map((file) => uploadFile(endpoints, file)));
|
|
1364
|
-
uploadIds = uploads.map((upload) => upload.id);
|
|
1365
|
-
} catch (err) {
|
|
1366
|
-
setFormError(err instanceof Error ? err.message : "The upload failed.");
|
|
1367
|
-
return;
|
|
1368
|
-
} finally {
|
|
1369
|
-
setUploading(false);
|
|
1370
|
-
}
|
|
1371
|
-
const result = await run.start({
|
|
1372
|
-
input: input.trim() || void 0,
|
|
1373
|
-
uploadIds: uploadIds.length ? uploadIds : void 0
|
|
1374
|
-
});
|
|
1375
|
-
onFinish?.(result);
|
|
1376
|
-
}
|
|
1377
|
-
if (loadError) {
|
|
1378
|
-
return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("div", { className: "seclai-agent", children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("p", { className: "seclai-error", role: "alert", children: loadError }) });
|
|
1379
|
-
}
|
|
1380
|
-
if (!slots) {
|
|
1381
|
-
return /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "seclai-agent", children: [
|
|
1382
|
-
/* @__PURE__ */ (0, import_jsx_runtime8.jsx)("p", { className: "seclai-notice", children: "Loading\u2026" }),
|
|
1383
|
-
/* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "seclai-skeleton", "aria-hidden": "true", children: [
|
|
1384
|
-
/* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", {}),
|
|
1385
|
-
/* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", {})
|
|
1386
|
-
] })
|
|
1387
|
-
] });
|
|
1388
|
-
}
|
|
1389
|
-
const error = formError ?? run.error;
|
|
1390
|
-
return /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "seclai-agent", children: [
|
|
1391
|
-
/* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("form", { className: "seclai-form", onSubmit: submit, children: [
|
|
1392
|
-
/* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("label", { className: "seclai-field", children: [
|
|
1393
|
-
/* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { children: inputLabel }),
|
|
1394
|
-
/* @__PURE__ */ (0, import_jsx_runtime8.jsx)("textarea", { rows: 4, value: input, onChange: (e) => setInput(e.target.value) })
|
|
1395
|
-
] }),
|
|
1396
|
-
slots.map((slot) => /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("label", { className: "seclai-field", children: [
|
|
1397
|
-
/* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("span", { children: [
|
|
1398
|
-
slot.label ?? slot.name,
|
|
1399
|
-
(slot.min ?? 0) > 0 ? " (required)" : ""
|
|
1400
|
-
] }),
|
|
1401
|
-
/* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
|
|
1402
|
-
"input",
|
|
1403
|
-
{
|
|
1404
|
-
type: "file",
|
|
1405
|
-
name: slot.name,
|
|
1406
|
-
accept: acceptAttribute(slot.content_types),
|
|
1407
|
-
multiple: slot.max == null || slot.max > 1,
|
|
1408
|
-
onChange: (e) => setFiles((prev) => ({ ...prev, [slot.name]: Array.from(e.target.files ?? []) }))
|
|
1409
|
-
}
|
|
1410
|
-
)
|
|
1411
|
-
] }, slot.name)),
|
|
1412
|
-
/* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "seclai-actions", children: [
|
|
1413
|
-
/* @__PURE__ */ (0, import_jsx_runtime8.jsx)("button", { type: "submit", className: "seclai-button seclai-button-primary", disabled: busy, children: uploading ? "Uploading\u2026" : "Run" }),
|
|
1414
|
-
run.status === "running" && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("button", { type: "button", className: "seclai-button", onClick: run.cancel, children: "Stop" })
|
|
1415
|
-
] })
|
|
1416
|
-
] }),
|
|
1417
|
-
run.steps.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("ul", { className: "seclai-steps", children: run.steps.map((step) => /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("li", { children: [
|
|
1418
|
-
step.name,
|
|
1419
|
-
": ",
|
|
1420
|
-
step.message
|
|
1421
|
-
] }, step.id)) }),
|
|
1422
|
-
error && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("p", { className: "seclai-error", role: "alert", children: error }),
|
|
1423
|
-
run.status === "cancelled" && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("p", { className: "seclai-notice", children: "Stopped." }),
|
|
1424
|
-
(run.text || run.attachments.length > 0) && /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: "seclai-output", "aria-live": "polite", "aria-busy": run.status === "running", children: [
|
|
1425
|
-
run.text && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(MarkdownContent, { content: run.text }),
|
|
1426
|
-
run.attachments.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(MediaGallery, { attachments: run.attachments })
|
|
1427
|
-
] })
|
|
1428
|
-
] });
|
|
1429
|
-
}
|
|
1430
|
-
|
|
1431
|
-
// src/SeclaiAgent.tsx
|
|
1432
|
-
var import_jsx_runtime9 = require("react/jsx-runtime");
|
|
1433
|
-
function SeclaiAgent({ mode, basePath, agentId }) {
|
|
1434
|
-
return mode === "chat" ? /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(AgentChat, { basePath, agentId }) : /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(AgentRunForm, { basePath, agentId });
|
|
1435
|
-
}
|
|
1436
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
1437
|
-
0 && (module.exports = {
|
|
1438
|
-
AgentChat,
|
|
1439
|
-
AgentRunForm,
|
|
1440
|
-
DEFAULT_BASE_PATH,
|
|
1441
|
-
MarkdownContent,
|
|
1442
|
-
MediaGallery,
|
|
1443
|
-
MediaPreview,
|
|
1444
|
-
MediaSkeleton,
|
|
1445
|
-
MediaViewer,
|
|
1446
|
-
RunProgress,
|
|
1447
|
-
SeclaiAgent,
|
|
1448
|
-
attachmentFileName,
|
|
1449
|
-
createEndpoints,
|
|
1450
|
-
fetchAttachmentReferences,
|
|
1451
|
-
saveFile,
|
|
1452
|
-
saveFiles,
|
|
1453
|
-
uploadFile,
|
|
1454
|
-
useAgentRun,
|
|
1455
|
-
useElapsed
|
|
1456
|
-
});
|