@seclai/react 0.0.6 → 0.0.8
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 +70 -3
- package/dist/index.cjs +450 -88
- package/dist/index.d.cts +116 -11
- package/dist/index.d.ts +116 -11
- package/dist/index.js +444 -86
- package/dist/styles.css +276 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
// src/AgentChat.tsx
|
|
4
4
|
import {
|
|
5
|
-
useEffect as
|
|
5
|
+
useEffect as useEffect6,
|
|
6
6
|
useMemo as useMemo2,
|
|
7
|
-
useRef as
|
|
8
|
-
useState as
|
|
7
|
+
useRef as useRef5,
|
|
8
|
+
useState as useState5
|
|
9
9
|
} from "react";
|
|
10
10
|
|
|
11
11
|
// src/attachments.ts
|
|
@@ -99,18 +99,353 @@ function MarkdownContent({ content }) {
|
|
|
99
99
|
return /* @__PURE__ */ jsx("div", { className: "seclai-markdown", children: content });
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
+
// src/media/MediaGallery.tsx
|
|
103
|
+
import { useEffect as useEffect4, useRef as useRef3, useState as useState3 } from "react";
|
|
104
|
+
|
|
105
|
+
// src/placeholders/MediaPreview.tsx
|
|
106
|
+
import { useEffect as useEffect2, useRef, useState as useState2 } from "react";
|
|
107
|
+
|
|
108
|
+
// src/media/files.ts
|
|
109
|
+
var EXTENSIONS = {
|
|
110
|
+
"image/jpeg": "jpg",
|
|
111
|
+
"image/svg+xml": "svg",
|
|
112
|
+
"image/x-icon": "ico",
|
|
113
|
+
"image/vnd.microsoft.icon": "ico",
|
|
114
|
+
"audio/mpeg": "mp3",
|
|
115
|
+
"video/quicktime": "mov",
|
|
116
|
+
"text/plain": "txt",
|
|
117
|
+
"text/markdown": "md",
|
|
118
|
+
"application/octet-stream": "bin"
|
|
119
|
+
};
|
|
120
|
+
function isImage(attachment) {
|
|
121
|
+
return attachment.mime.toLowerCase().startsWith("image/");
|
|
122
|
+
}
|
|
123
|
+
function extensionForMime(mime) {
|
|
124
|
+
const type = mime.split(";")[0].trim().toLowerCase();
|
|
125
|
+
const known = EXTENSIONS[type];
|
|
126
|
+
if (known) return known;
|
|
127
|
+
const subtype = type.split("/")[1] ?? "";
|
|
128
|
+
const ext = (subtype.split("+").at(-1) ?? "").replace(/^x-/, "");
|
|
129
|
+
return /^[a-z0-9]{1,5}$/.test(ext) ? ext : "bin";
|
|
130
|
+
}
|
|
131
|
+
function attachmentFileName(attachment, number) {
|
|
132
|
+
if (attachment.name) return attachment.name;
|
|
133
|
+
const base = number != null ? `image-${number}` : attachment.id;
|
|
134
|
+
return `${base}.${extensionForMime(attachment.mime)}`;
|
|
135
|
+
}
|
|
136
|
+
var SAFE_PROTOCOLS = /* @__PURE__ */ new Set(["http:", "https:", "blob:"]);
|
|
137
|
+
function isSafeUrl(url) {
|
|
138
|
+
try {
|
|
139
|
+
return SAFE_PROTOCOLS.has(new URL(url, document.baseURI).protocol);
|
|
140
|
+
} catch {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function triggerDownload(url, filename) {
|
|
145
|
+
if (!isSafeUrl(url)) {
|
|
146
|
+
console.warn(`seclai: not downloading ${filename}, its URL isn't http(s), blob or relative.`);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const link = document.createElement("a");
|
|
150
|
+
link.href = url;
|
|
151
|
+
link.download = filename;
|
|
152
|
+
link.rel = "noopener";
|
|
153
|
+
link.style.display = "none";
|
|
154
|
+
document.body.append(link);
|
|
155
|
+
link.click();
|
|
156
|
+
link.remove();
|
|
157
|
+
}
|
|
158
|
+
async function downloadSequentially(files, delayMs, signal) {
|
|
159
|
+
for (const [i, file] of files.entries()) {
|
|
160
|
+
if (signal?.aborted) return;
|
|
161
|
+
if (i > 0) await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
162
|
+
if (signal?.aborted) return;
|
|
163
|
+
triggerDownload(file.url, file.filename);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
102
167
|
// src/placeholders/MediaPreview.tsx
|
|
103
|
-
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
104
|
-
function
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
168
|
+
import { Fragment, jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
169
|
+
function hasContent(node) {
|
|
170
|
+
return node != null && node !== false && node !== "";
|
|
171
|
+
}
|
|
172
|
+
function aspectRatioStyle(ratio) {
|
|
173
|
+
return ratio == null ? void 0 : { aspectRatio: String(ratio) };
|
|
174
|
+
}
|
|
175
|
+
function imageLabel(attachment, number) {
|
|
176
|
+
return attachment.name ?? (number != null ? `Image ${number}` : "Image");
|
|
177
|
+
}
|
|
178
|
+
function MediaPreview({ attachment, aspectRatio, number, actions, onOpen }) {
|
|
179
|
+
const filename = attachmentFileName(attachment, number);
|
|
180
|
+
const extra = typeof actions === "function" ? actions(attachment) : actions;
|
|
181
|
+
if (!isImage(attachment)) {
|
|
182
|
+
return /* @__PURE__ */ jsx2("figure", { className: "seclai-media", children: /* @__PURE__ */ jsxs("figcaption", { children: [
|
|
183
|
+
/* @__PURE__ */ jsx2("a", { href: attachment.url, download: filename, children: attachment.name ?? filename }),
|
|
184
|
+
hasContent(extra) && /* @__PURE__ */ jsx2("span", { className: "seclai-media-actions", children: extra })
|
|
185
|
+
] }) });
|
|
186
|
+
}
|
|
187
|
+
return /* @__PURE__ */ jsx2(
|
|
188
|
+
ImageCard,
|
|
189
|
+
{
|
|
190
|
+
attachment,
|
|
191
|
+
aspectRatio,
|
|
192
|
+
number,
|
|
193
|
+
filename,
|
|
194
|
+
extra,
|
|
195
|
+
onOpen
|
|
196
|
+
}
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
function ImageCard({ attachment, aspectRatio, number, filename, extra, onOpen }) {
|
|
200
|
+
const img = useRef(null);
|
|
201
|
+
const [natural, setNatural] = useState2(null);
|
|
202
|
+
const [failed, setFailed] = useState2(false);
|
|
203
|
+
const label = imageLabel(attachment, number);
|
|
204
|
+
function measure() {
|
|
205
|
+
const el = img.current;
|
|
206
|
+
if (el && el.naturalWidth > 0 && el.naturalHeight > 0) {
|
|
207
|
+
setNatural(`${el.naturalWidth} / ${el.naturalHeight}`);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
useEffect2(() => {
|
|
211
|
+
setNatural(null);
|
|
212
|
+
setFailed(false);
|
|
213
|
+
const el = img.current;
|
|
214
|
+
if (el?.complete) {
|
|
215
|
+
if (el.naturalWidth === 0) setFailed(true);
|
|
216
|
+
else measure();
|
|
217
|
+
}
|
|
218
|
+
}, [attachment.url]);
|
|
219
|
+
const frameContent = /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
220
|
+
failed ? /* @__PURE__ */ jsx2("span", { className: "seclai-media-error", children: "Couldn't load the image." }) : /* @__PURE__ */ jsx2(
|
|
221
|
+
"img",
|
|
222
|
+
{
|
|
223
|
+
ref: img,
|
|
224
|
+
src: attachment.url,
|
|
225
|
+
alt: label,
|
|
226
|
+
loading: "lazy",
|
|
227
|
+
decoding: "async",
|
|
228
|
+
onLoad: measure,
|
|
229
|
+
onError: () => setFailed(true)
|
|
230
|
+
}
|
|
231
|
+
),
|
|
232
|
+
number != null && /* @__PURE__ */ jsx2("span", { className: "seclai-media-badge", children: number })
|
|
233
|
+
] });
|
|
234
|
+
const frameStyle = aspectRatioStyle(aspectRatio ?? natural ?? void 0);
|
|
235
|
+
return /* @__PURE__ */ jsxs("figure", { className: "seclai-media seclai-media-image", children: [
|
|
236
|
+
onOpen ? /* @__PURE__ */ jsx2(
|
|
237
|
+
"button",
|
|
238
|
+
{
|
|
239
|
+
type: "button",
|
|
240
|
+
className: "seclai-media-frame",
|
|
241
|
+
style: frameStyle,
|
|
242
|
+
"aria-label": `Open ${label} full size`,
|
|
243
|
+
"aria-haspopup": "dialog",
|
|
244
|
+
onClick: onOpen,
|
|
245
|
+
children: frameContent
|
|
246
|
+
}
|
|
247
|
+
) : /* @__PURE__ */ jsx2("div", { className: "seclai-media-frame", style: frameStyle, children: frameContent }),
|
|
248
|
+
/* @__PURE__ */ jsxs("figcaption", { className: "seclai-media-bar", children: [
|
|
249
|
+
/* @__PURE__ */ jsx2("a", { href: attachment.url, download: filename, "aria-label": `Download ${label}`, children: "Download" }),
|
|
250
|
+
hasContent(extra) && /* @__PURE__ */ jsx2("span", { className: "seclai-media-actions", children: extra })
|
|
251
|
+
] })
|
|
252
|
+
] });
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// src/media/MediaSkeleton.tsx
|
|
256
|
+
import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
257
|
+
function MediaSkeleton({ aspectRatio, number, label }) {
|
|
258
|
+
const name = label ?? (number != null ? `Image ${number} is being generated` : "Image is being generated");
|
|
259
|
+
return /* @__PURE__ */ jsxs2("div", { className: "seclai-media seclai-media-image seclai-media-skeleton", role: "img", "aria-label": name, children: [
|
|
260
|
+
/* @__PURE__ */ jsx3("div", { className: "seclai-media-frame", style: aspectRatioStyle(aspectRatio), children: number != null && /* @__PURE__ */ jsx3("span", { className: "seclai-media-badge", children: number }) }),
|
|
261
|
+
/* @__PURE__ */ jsx3("div", { className: "seclai-media-bar", children: /* @__PURE__ */ jsx3("span", { className: "seclai-media-skeleton-line" }) })
|
|
262
|
+
] });
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// src/media/MediaViewer.tsx
|
|
266
|
+
import { useEffect as useEffect3, useId, useRef as useRef2 } from "react";
|
|
267
|
+
import { Fragment as Fragment2, jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
268
|
+
function MediaViewer({ attachments, index, onIndexChange, onClose }) {
|
|
269
|
+
const dialog = useRef2(null);
|
|
270
|
+
const closeButton = useRef2(null);
|
|
271
|
+
const returnFocus = useRef2(null);
|
|
272
|
+
const isOpen = useRef2(false);
|
|
273
|
+
const titleId = useId();
|
|
274
|
+
const current = index != null ? attachments[index] : void 0;
|
|
275
|
+
const open = current != null;
|
|
276
|
+
isOpen.current = open;
|
|
277
|
+
useEffect3(() => {
|
|
278
|
+
const el = dialog.current;
|
|
279
|
+
if (!el) return;
|
|
280
|
+
if (open && !el.open) {
|
|
281
|
+
returnFocus.current = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
|
282
|
+
el.showModal();
|
|
283
|
+
closeButton.current?.focus();
|
|
284
|
+
} else if (!open && el.open) {
|
|
285
|
+
el.close();
|
|
286
|
+
restoreFocus();
|
|
287
|
+
}
|
|
288
|
+
}, [open]);
|
|
289
|
+
useEffect3(
|
|
290
|
+
() => () => {
|
|
291
|
+
if (isOpen.current) restoreFocus();
|
|
292
|
+
},
|
|
293
|
+
[]
|
|
294
|
+
);
|
|
295
|
+
function restoreFocus() {
|
|
296
|
+
const target = returnFocus.current;
|
|
297
|
+
returnFocus.current = null;
|
|
298
|
+
if (target?.isConnected) target.focus();
|
|
299
|
+
}
|
|
300
|
+
const last = attachments.length - 1;
|
|
301
|
+
const atStart = index == null || index <= 0;
|
|
302
|
+
const atEnd = index == null || index >= last;
|
|
303
|
+
function go(step) {
|
|
304
|
+
if (index == null) return;
|
|
305
|
+
const next = index + step;
|
|
306
|
+
if (next >= 0 && next <= last) onIndexChange(next);
|
|
307
|
+
}
|
|
308
|
+
function onKeyDown(event) {
|
|
309
|
+
if (event.key !== "ArrowLeft" && event.key !== "ArrowRight" && event.key !== "Escape") return;
|
|
310
|
+
event.preventDefault();
|
|
311
|
+
event.stopPropagation();
|
|
312
|
+
if (event.key === "ArrowLeft") go(-1);
|
|
313
|
+
else if (event.key === "ArrowRight") go(1);
|
|
314
|
+
else onClose();
|
|
315
|
+
}
|
|
316
|
+
function onCancel(event) {
|
|
317
|
+
event.preventDefault();
|
|
318
|
+
onClose();
|
|
319
|
+
}
|
|
320
|
+
function onDialogClose() {
|
|
321
|
+
if (isOpen.current) onClose();
|
|
322
|
+
}
|
|
323
|
+
function onClick(event) {
|
|
324
|
+
if (event.target === event.currentTarget) onClose();
|
|
325
|
+
}
|
|
326
|
+
const number = index != null ? index + 1 : 0;
|
|
327
|
+
const label = current ? imageLabel(current, number) : "";
|
|
328
|
+
return /* @__PURE__ */ jsx4(
|
|
329
|
+
"dialog",
|
|
330
|
+
{
|
|
331
|
+
ref: dialog,
|
|
332
|
+
className: "seclai-viewer",
|
|
333
|
+
"aria-labelledby": titleId,
|
|
334
|
+
onKeyDown,
|
|
335
|
+
onCancel,
|
|
336
|
+
onClose: onDialogClose,
|
|
337
|
+
onClick,
|
|
338
|
+
children: current && /* @__PURE__ */ jsxs3("div", { className: "seclai-viewer-body", children: [
|
|
339
|
+
/* @__PURE__ */ jsx4("img", { className: "seclai-viewer-image", src: current.url, alt: label }),
|
|
340
|
+
/* @__PURE__ */ jsxs3("div", { className: "seclai-viewer-bar", children: [
|
|
341
|
+
/* @__PURE__ */ jsxs3("p", { className: "seclai-viewer-title", id: titleId, children: [
|
|
342
|
+
label,
|
|
343
|
+
attachments.length > 1 && /* @__PURE__ */ jsxs3("span", { className: "seclai-viewer-count", children: [
|
|
344
|
+
number,
|
|
345
|
+
" of ",
|
|
346
|
+
attachments.length
|
|
347
|
+
] })
|
|
348
|
+
] }),
|
|
349
|
+
attachments.length > 1 && /* @__PURE__ */ jsxs3(Fragment2, { children: [
|
|
350
|
+
/* @__PURE__ */ jsx4(
|
|
351
|
+
"button",
|
|
352
|
+
{
|
|
353
|
+
type: "button",
|
|
354
|
+
className: "seclai-button",
|
|
355
|
+
"aria-disabled": atStart,
|
|
356
|
+
onClick: () => go(-1),
|
|
357
|
+
children: "Previous"
|
|
358
|
+
}
|
|
359
|
+
),
|
|
360
|
+
/* @__PURE__ */ jsx4("button", { type: "button", className: "seclai-button", "aria-disabled": atEnd, onClick: () => go(1), children: "Next" })
|
|
361
|
+
] }),
|
|
362
|
+
/* @__PURE__ */ jsx4(
|
|
363
|
+
"a",
|
|
364
|
+
{
|
|
365
|
+
className: "seclai-button seclai-button-primary",
|
|
366
|
+
href: current.url,
|
|
367
|
+
download: attachmentFileName(current, number),
|
|
368
|
+
children: "Download"
|
|
369
|
+
}
|
|
370
|
+
),
|
|
371
|
+
/* @__PURE__ */ jsx4("button", { type: "button", className: "seclai-button", ref: closeButton, onClick: onClose, children: "Close" })
|
|
372
|
+
] })
|
|
373
|
+
] })
|
|
374
|
+
}
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// src/media/MediaGallery.tsx
|
|
379
|
+
import { jsx as jsx5, jsxs as jsxs4 } from "react/jsx-runtime";
|
|
380
|
+
var DOWNLOAD_ALL_DELAY_MS = 400;
|
|
381
|
+
function MediaGallery({
|
|
382
|
+
attachments,
|
|
383
|
+
aspectRatio,
|
|
384
|
+
pending = 0,
|
|
385
|
+
actions
|
|
386
|
+
}) {
|
|
387
|
+
const images = attachments.filter(isImage);
|
|
388
|
+
const files = attachments.filter((attachment) => !isImage(attachment));
|
|
389
|
+
const [viewing, setViewing] = useState3(null);
|
|
390
|
+
const [downloading, setDownloading] = useState3(false);
|
|
391
|
+
const abort = useRef3(null);
|
|
392
|
+
useEffect4(() => () => abort.current?.abort(), []);
|
|
393
|
+
if (viewing != null && viewing >= images.length) setViewing(null);
|
|
394
|
+
async function downloadAll() {
|
|
395
|
+
if (downloading) return;
|
|
396
|
+
const controller = new AbortController();
|
|
397
|
+
abort.current = controller;
|
|
398
|
+
setDownloading(true);
|
|
399
|
+
try {
|
|
400
|
+
await downloadSequentially(
|
|
401
|
+
images.map((image, i) => ({ url: image.url, filename: attachmentFileName(image, i + 1) })),
|
|
402
|
+
DOWNLOAD_ALL_DELAY_MS,
|
|
403
|
+
controller.signal
|
|
404
|
+
);
|
|
405
|
+
} finally {
|
|
406
|
+
if (!controller.signal.aborted) setDownloading(false);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
const skeletons = Math.max(0, Math.floor(pending));
|
|
410
|
+
return /* @__PURE__ */ jsxs4("div", { className: "seclai-gallery", children: [
|
|
411
|
+
images.length + skeletons > 0 && /* @__PURE__ */ jsxs4("ul", { className: "seclai-gallery-grid", children: [
|
|
412
|
+
images.map((image, i) => /* @__PURE__ */ jsx5("li", { children: /* @__PURE__ */ jsx5(
|
|
413
|
+
MediaPreview,
|
|
414
|
+
{
|
|
415
|
+
attachment: image,
|
|
416
|
+
aspectRatio,
|
|
417
|
+
number: i + 1,
|
|
418
|
+
actions: actions?.(image, i + 1),
|
|
419
|
+
onOpen: () => setViewing(i)
|
|
420
|
+
}
|
|
421
|
+
) }, image.id)),
|
|
422
|
+
Array.from({ length: skeletons }, (_, i) => /* @__PURE__ */ jsx5("li", { children: /* @__PURE__ */ jsx5(MediaSkeleton, { aspectRatio, number: images.length + i + 1 }) }, `pending-${i}`))
|
|
423
|
+
] }),
|
|
424
|
+
images.length > 1 && /* @__PURE__ */ jsx5("div", { className: "seclai-gallery-actions", children: /* @__PURE__ */ jsx5(
|
|
425
|
+
"button",
|
|
426
|
+
{
|
|
427
|
+
type: "button",
|
|
428
|
+
className: "seclai-button",
|
|
429
|
+
"aria-disabled": downloading,
|
|
430
|
+
onClick: downloadAll,
|
|
431
|
+
children: downloading ? "Downloading\u2026" : `Download all ${images.length}`
|
|
432
|
+
}
|
|
433
|
+
) }),
|
|
434
|
+
files.map((file) => /* @__PURE__ */ jsx5(MediaPreview, { attachment: file }, file.id)),
|
|
435
|
+
images.length > 0 && /* @__PURE__ */ jsx5(
|
|
436
|
+
MediaViewer,
|
|
437
|
+
{
|
|
438
|
+
attachments: images,
|
|
439
|
+
index: viewing,
|
|
440
|
+
onIndexChange: setViewing,
|
|
441
|
+
onClose: () => setViewing(null)
|
|
442
|
+
}
|
|
443
|
+
)
|
|
109
444
|
] });
|
|
110
445
|
}
|
|
111
446
|
|
|
112
447
|
// src/useAgentRun.ts
|
|
113
|
-
import { useCallback, useEffect as
|
|
448
|
+
import { useCallback, useEffect as useEffect5, useMemo, useRef as useRef4, useState as useState4 } from "react";
|
|
114
449
|
|
|
115
450
|
// src/sse.ts
|
|
116
451
|
function parseData(data) {
|
|
@@ -176,7 +511,8 @@ var IDLE = {
|
|
|
176
511
|
steps: [],
|
|
177
512
|
attachments: [],
|
|
178
513
|
error: null,
|
|
179
|
-
runId: null
|
|
514
|
+
runId: null,
|
|
515
|
+
startedAt: null
|
|
180
516
|
};
|
|
181
517
|
function toAttachments(files, runId, endpoints) {
|
|
182
518
|
if (!files || !runId) return [];
|
|
@@ -254,17 +590,21 @@ function applyEvent(state, { event, data }, endpoints) {
|
|
|
254
590
|
}
|
|
255
591
|
}
|
|
256
592
|
function useAgentRun(options = {}) {
|
|
257
|
-
const { basePath, agentId } = options;
|
|
593
|
+
const { basePath, agentId, onEvent } = options;
|
|
258
594
|
const endpoints = useMemo(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
259
|
-
const [state, setState] =
|
|
260
|
-
const active =
|
|
261
|
-
|
|
595
|
+
const [state, setState] = useState4(IDLE);
|
|
596
|
+
const active = useRef4(null);
|
|
597
|
+
const onEventRef = useRef4(onEvent);
|
|
598
|
+
useEffect5(() => {
|
|
599
|
+
onEventRef.current = onEvent;
|
|
600
|
+
});
|
|
601
|
+
useEffect5(() => () => active.current?.abort(), []);
|
|
262
602
|
const start = useCallback(
|
|
263
603
|
async ({ input, uploadIds, sessionId }) => {
|
|
264
604
|
active.current?.abort();
|
|
265
605
|
const controller = new AbortController();
|
|
266
606
|
active.current = controller;
|
|
267
|
-
let current = { ...IDLE, status: "running" };
|
|
607
|
+
let current = { ...IDLE, status: "running", startedAt: Date.now() };
|
|
268
608
|
const update = (next) => {
|
|
269
609
|
current = next;
|
|
270
610
|
if (active.current === controller) setState(next);
|
|
@@ -286,10 +626,18 @@ function useAgentRun(options = {}) {
|
|
|
286
626
|
return current;
|
|
287
627
|
}
|
|
288
628
|
for await (const event of readSseStream(res.body)) {
|
|
629
|
+
if (controller.signal.aborted) break;
|
|
289
630
|
update(applyEvent(current, event, endpoints));
|
|
631
|
+
try {
|
|
632
|
+
onEventRef.current?.(event);
|
|
633
|
+
} catch (err) {
|
|
634
|
+
console.error("useAgentRun: onEvent threw", err);
|
|
635
|
+
}
|
|
290
636
|
if (current.status !== "running") break;
|
|
291
637
|
}
|
|
292
|
-
if (
|
|
638
|
+
if (controller.signal.aborted) {
|
|
639
|
+
if (current.status === "running") update({ ...current, status: "cancelled" });
|
|
640
|
+
} else if (current.status === "running") {
|
|
293
641
|
update({
|
|
294
642
|
...current,
|
|
295
643
|
status: "failed",
|
|
@@ -312,11 +660,17 @@ function useAgentRun(options = {}) {
|
|
|
312
660
|
[endpoints]
|
|
313
661
|
);
|
|
314
662
|
const cancel = useCallback(() => active.current?.abort(), []);
|
|
315
|
-
|
|
663
|
+
const reset = useCallback(() => {
|
|
664
|
+
const controller = active.current;
|
|
665
|
+
active.current = null;
|
|
666
|
+
controller?.abort();
|
|
667
|
+
setState(IDLE);
|
|
668
|
+
}, []);
|
|
669
|
+
return { ...state, start, cancel, reset };
|
|
316
670
|
}
|
|
317
671
|
|
|
318
672
|
// src/AgentChat.tsx
|
|
319
|
-
import { jsx as
|
|
673
|
+
import { jsx as jsx6, jsxs as jsxs5 } from "react/jsx-runtime";
|
|
320
674
|
function createSessionId() {
|
|
321
675
|
if (typeof crypto.randomUUID === "function") return crypto.randomUUID();
|
|
322
676
|
return Array.from(
|
|
@@ -340,13 +694,13 @@ function plural(count, word) {
|
|
|
340
694
|
}
|
|
341
695
|
function FileChip({ name, tag, preview, status, error, onRemove }) {
|
|
342
696
|
const meta = status === "uploading" ? "Uploading\u2026" : status === "error" ? error : tag;
|
|
343
|
-
return /* @__PURE__ */
|
|
344
|
-
preview && /* @__PURE__ */
|
|
345
|
-
/* @__PURE__ */
|
|
346
|
-
/* @__PURE__ */
|
|
347
|
-
meta && /* @__PURE__ */
|
|
697
|
+
return /* @__PURE__ */ jsxs5("li", { className: "seclai-file", "data-status": status, children: [
|
|
698
|
+
preview && /* @__PURE__ */ jsx6("img", { className: "seclai-file-thumb", src: preview, alt: "" }),
|
|
699
|
+
/* @__PURE__ */ jsxs5("span", { className: "seclai-file-text", children: [
|
|
700
|
+
/* @__PURE__ */ jsx6("span", { className: "seclai-file-name", title: name, children: name }),
|
|
701
|
+
meta && /* @__PURE__ */ jsx6("span", { className: "seclai-file-meta", children: meta })
|
|
348
702
|
] }),
|
|
349
|
-
onRemove && /* @__PURE__ */
|
|
703
|
+
onRemove && /* @__PURE__ */ jsx6(
|
|
350
704
|
"button",
|
|
351
705
|
{
|
|
352
706
|
type: "button",
|
|
@@ -359,8 +713,8 @@ function FileChip({ name, tag, preview, status, error, onRemove }) {
|
|
|
359
713
|
] });
|
|
360
714
|
}
|
|
361
715
|
function Turn({ turn, previews }) {
|
|
362
|
-
return /* @__PURE__ */
|
|
363
|
-
turn.files && turn.files.length > 0 && /* @__PURE__ */
|
|
716
|
+
return /* @__PURE__ */ jsxs5("li", { className: `seclai-turn seclai-turn-${turn.role}`, children: [
|
|
717
|
+
turn.files && turn.files.length > 0 && /* @__PURE__ */ jsx6("ul", { className: "seclai-files", "aria-label": "Attached files", children: turn.files.map((file) => /* @__PURE__ */ jsx6(
|
|
364
718
|
FileChip,
|
|
365
719
|
{
|
|
366
720
|
name: file.name,
|
|
@@ -369,10 +723,10 @@ function Turn({ turn, previews }) {
|
|
|
369
723
|
},
|
|
370
724
|
file.uploadId
|
|
371
725
|
)) }),
|
|
372
|
-
turn.text && /* @__PURE__ */
|
|
373
|
-
turn.attachments
|
|
374
|
-
turn.error && /* @__PURE__ */
|
|
375
|
-
turn.cancelled && /* @__PURE__ */
|
|
726
|
+
turn.text && /* @__PURE__ */ jsx6(MarkdownContent, { content: turn.text }),
|
|
727
|
+
turn.attachments && turn.attachments.length > 0 && /* @__PURE__ */ jsx6(MediaGallery, { attachments: turn.attachments }),
|
|
728
|
+
turn.error && /* @__PURE__ */ jsx6("p", { className: "seclai-error", role: "alert", children: turn.error }),
|
|
729
|
+
turn.cancelled && /* @__PURE__ */ jsx6("p", { className: "seclai-notice", children: "Stopped." })
|
|
376
730
|
] });
|
|
377
731
|
}
|
|
378
732
|
function AttachControl({
|
|
@@ -382,9 +736,9 @@ function AttachControl({
|
|
|
382
736
|
disabled,
|
|
383
737
|
onFiles
|
|
384
738
|
}) {
|
|
385
|
-
return /* @__PURE__ */
|
|
739
|
+
return /* @__PURE__ */ jsxs5("label", { className: "seclai-button seclai-attach", "aria-disabled": disabled || void 0, children: [
|
|
386
740
|
label,
|
|
387
|
-
/* @__PURE__ */
|
|
741
|
+
/* @__PURE__ */ jsx6(
|
|
388
742
|
"input",
|
|
389
743
|
{
|
|
390
744
|
type: "file",
|
|
@@ -411,16 +765,16 @@ function AgentChat({
|
|
|
411
765
|
const endpoints = useMemo2(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
412
766
|
const run = useAgentRun({ basePath, agentId });
|
|
413
767
|
const { slots } = useAcceptedAttachments(endpoints);
|
|
414
|
-
const [turns, setTurns] =
|
|
415
|
-
const [message, setMessage] =
|
|
416
|
-
const [files, setFiles] =
|
|
417
|
-
const [notice, setNotice] =
|
|
418
|
-
const [dragging, setDragging] =
|
|
419
|
-
const sessionId =
|
|
420
|
-
const nextTurnId =
|
|
421
|
-
const sentPreviews =
|
|
422
|
-
const pendingPreviews =
|
|
423
|
-
|
|
768
|
+
const [turns, setTurns] = useState5([]);
|
|
769
|
+
const [message, setMessage] = useState5("");
|
|
770
|
+
const [files, setFiles] = useState5([]);
|
|
771
|
+
const [notice, setNotice] = useState5(null);
|
|
772
|
+
const [dragging, setDragging] = useState5(false);
|
|
773
|
+
const sessionId = useRef5(null);
|
|
774
|
+
const nextTurnId = useRef5(0);
|
|
775
|
+
const sentPreviews = useRef5(/* @__PURE__ */ new Map());
|
|
776
|
+
const pendingPreviews = useRef5(/* @__PURE__ */ new Set());
|
|
777
|
+
useEffect6(
|
|
424
778
|
() => () => {
|
|
425
779
|
pendingPreviews.current.forEach(revoke);
|
|
426
780
|
sentPreviews.current.forEach(revoke);
|
|
@@ -568,19 +922,19 @@ function AgentChat({
|
|
|
568
922
|
}
|
|
569
923
|
} : {};
|
|
570
924
|
const buttons = attachButtons?.length ? attachButtons : [{ label: "Attach" }];
|
|
571
|
-
return /* @__PURE__ */
|
|
572
|
-
/* @__PURE__ */
|
|
573
|
-
turns.map((turn) => /* @__PURE__ */
|
|
574
|
-
running && /* @__PURE__ */
|
|
925
|
+
return /* @__PURE__ */ jsxs5("div", { className: "seclai-agent seclai-chat", children: [
|
|
926
|
+
/* @__PURE__ */ jsxs5("ul", { className: "seclai-turns", "aria-live": "polite", "aria-busy": running, children: [
|
|
927
|
+
turns.map((turn) => /* @__PURE__ */ jsx6(Turn, { turn, previews: sentPreviews.current }, turn.id)),
|
|
928
|
+
running && /* @__PURE__ */ jsx6("li", { className: "seclai-turn seclai-turn-assistant", children: run.text ? /* @__PURE__ */ jsx6(MarkdownContent, { content: run.text }) : /* @__PURE__ */ jsx6("p", { className: "seclai-notice", children: "Thinking\u2026" }) })
|
|
575
929
|
] }),
|
|
576
|
-
/* @__PURE__ */
|
|
930
|
+
/* @__PURE__ */ jsxs5(
|
|
577
931
|
"form",
|
|
578
932
|
{
|
|
579
933
|
className: dragging ? "seclai-form seclai-dropping" : "seclai-form",
|
|
580
934
|
onSubmit,
|
|
581
935
|
...dropHandlers,
|
|
582
936
|
children: [
|
|
583
|
-
files.length > 0 && /* @__PURE__ */
|
|
937
|
+
files.length > 0 && /* @__PURE__ */ jsx6("ul", { className: "seclai-files", "aria-label": "Files to send", children: files.map((f) => /* @__PURE__ */ jsx6(
|
|
584
938
|
FileChip,
|
|
585
939
|
{
|
|
586
940
|
name: f.name,
|
|
@@ -592,7 +946,7 @@ function AgentChat({
|
|
|
592
946
|
},
|
|
593
947
|
f.localId
|
|
594
948
|
)) }),
|
|
595
|
-
/* @__PURE__ */
|
|
949
|
+
/* @__PURE__ */ jsx6(
|
|
596
950
|
"textarea",
|
|
597
951
|
{
|
|
598
952
|
rows: 2,
|
|
@@ -604,9 +958,9 @@ function AgentChat({
|
|
|
604
958
|
onPaste: acceptsFiles ? onPaste : void 0
|
|
605
959
|
}
|
|
606
960
|
),
|
|
607
|
-
notice && /* @__PURE__ */
|
|
608
|
-
/* @__PURE__ */
|
|
609
|
-
acceptsFiles && buttons.map((button) => /* @__PURE__ */
|
|
961
|
+
notice && /* @__PURE__ */ jsx6("p", { className: "seclai-error", role: "alert", children: notice }),
|
|
962
|
+
/* @__PURE__ */ jsxs5("div", { className: "seclai-actions", children: [
|
|
963
|
+
acceptsFiles && buttons.map((button) => /* @__PURE__ */ jsx6(
|
|
610
964
|
AttachControl,
|
|
611
965
|
{
|
|
612
966
|
label: button.label,
|
|
@@ -617,8 +971,8 @@ function AgentChat({
|
|
|
617
971
|
},
|
|
618
972
|
`${button.label}\0${button.tag ?? ""}`
|
|
619
973
|
)),
|
|
620
|
-
/* @__PURE__ */
|
|
621
|
-
running && /* @__PURE__ */
|
|
974
|
+
/* @__PURE__ */ jsx6("button", { type: "submit", className: "seclai-button seclai-button-primary", disabled: !canSend, children: "Send" }),
|
|
975
|
+
running && /* @__PURE__ */ jsx6("button", { type: "button", className: "seclai-button", onClick: run.cancel, children: "Stop" })
|
|
622
976
|
] })
|
|
623
977
|
]
|
|
624
978
|
}
|
|
@@ -627,8 +981,8 @@ function AgentChat({
|
|
|
627
981
|
}
|
|
628
982
|
|
|
629
983
|
// src/AgentRunForm.tsx
|
|
630
|
-
import { useMemo as useMemo3, useState as
|
|
631
|
-
import { jsx as
|
|
984
|
+
import { useMemo as useMemo3, useState as useState6 } from "react";
|
|
985
|
+
import { jsx as jsx7, jsxs as jsxs6 } from "react/jsx-runtime";
|
|
632
986
|
function slotProblem(slot, count) {
|
|
633
987
|
const label = slot.label ?? slot.name;
|
|
634
988
|
const min = slot.min ?? 0;
|
|
@@ -642,10 +996,10 @@ function AgentRunForm({ basePath, agentId, inputLabel = "Input", onFinish }) {
|
|
|
642
996
|
const endpoints = useMemo3(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
643
997
|
const run = useAgentRun({ basePath, agentId });
|
|
644
998
|
const { slots, error: loadError } = useAcceptedAttachments(endpoints);
|
|
645
|
-
const [input, setInput] =
|
|
646
|
-
const [files, setFiles] =
|
|
647
|
-
const [uploading, setUploading] =
|
|
648
|
-
const [formError, setFormError] =
|
|
999
|
+
const [input, setInput] = useState6("");
|
|
1000
|
+
const [files, setFiles] = useState6({});
|
|
1001
|
+
const [uploading, setUploading] = useState6(false);
|
|
1002
|
+
const [formError, setFormError] = useState6(null);
|
|
649
1003
|
const selected = Object.values(files).flat();
|
|
650
1004
|
const busy = uploading || run.status === "running";
|
|
651
1005
|
async function submit(event) {
|
|
@@ -679,30 +1033,30 @@ function AgentRunForm({ basePath, agentId, inputLabel = "Input", onFinish }) {
|
|
|
679
1033
|
onFinish?.(result);
|
|
680
1034
|
}
|
|
681
1035
|
if (loadError) {
|
|
682
|
-
return /* @__PURE__ */
|
|
1036
|
+
return /* @__PURE__ */ jsx7("div", { className: "seclai-agent", children: /* @__PURE__ */ jsx7("p", { className: "seclai-error", role: "alert", children: loadError }) });
|
|
683
1037
|
}
|
|
684
1038
|
if (!slots) {
|
|
685
|
-
return /* @__PURE__ */
|
|
686
|
-
/* @__PURE__ */
|
|
687
|
-
/* @__PURE__ */
|
|
688
|
-
/* @__PURE__ */
|
|
689
|
-
/* @__PURE__ */
|
|
1039
|
+
return /* @__PURE__ */ jsxs6("div", { className: "seclai-agent", children: [
|
|
1040
|
+
/* @__PURE__ */ jsx7("p", { className: "seclai-notice", children: "Loading\u2026" }),
|
|
1041
|
+
/* @__PURE__ */ jsxs6("div", { className: "seclai-skeleton", "aria-hidden": "true", children: [
|
|
1042
|
+
/* @__PURE__ */ jsx7("span", {}),
|
|
1043
|
+
/* @__PURE__ */ jsx7("span", {})
|
|
690
1044
|
] })
|
|
691
1045
|
] });
|
|
692
1046
|
}
|
|
693
1047
|
const error = formError ?? run.error;
|
|
694
|
-
return /* @__PURE__ */
|
|
695
|
-
/* @__PURE__ */
|
|
696
|
-
/* @__PURE__ */
|
|
697
|
-
/* @__PURE__ */
|
|
698
|
-
/* @__PURE__ */
|
|
1048
|
+
return /* @__PURE__ */ jsxs6("div", { className: "seclai-agent", children: [
|
|
1049
|
+
/* @__PURE__ */ jsxs6("form", { className: "seclai-form", onSubmit: submit, children: [
|
|
1050
|
+
/* @__PURE__ */ jsxs6("label", { className: "seclai-field", children: [
|
|
1051
|
+
/* @__PURE__ */ jsx7("span", { children: inputLabel }),
|
|
1052
|
+
/* @__PURE__ */ jsx7("textarea", { rows: 4, value: input, onChange: (e) => setInput(e.target.value) })
|
|
699
1053
|
] }),
|
|
700
|
-
slots.map((slot) => /* @__PURE__ */
|
|
701
|
-
/* @__PURE__ */
|
|
1054
|
+
slots.map((slot) => /* @__PURE__ */ jsxs6("label", { className: "seclai-field", children: [
|
|
1055
|
+
/* @__PURE__ */ jsxs6("span", { children: [
|
|
702
1056
|
slot.label ?? slot.name,
|
|
703
1057
|
(slot.min ?? 0) > 0 ? " (required)" : ""
|
|
704
1058
|
] }),
|
|
705
|
-
/* @__PURE__ */
|
|
1059
|
+
/* @__PURE__ */ jsx7(
|
|
706
1060
|
"input",
|
|
707
1061
|
{
|
|
708
1062
|
type: "file",
|
|
@@ -713,37 +1067,41 @@ function AgentRunForm({ basePath, agentId, inputLabel = "Input", onFinish }) {
|
|
|
713
1067
|
}
|
|
714
1068
|
)
|
|
715
1069
|
] }, slot.name)),
|
|
716
|
-
/* @__PURE__ */
|
|
717
|
-
/* @__PURE__ */
|
|
718
|
-
run.status === "running" && /* @__PURE__ */
|
|
1070
|
+
/* @__PURE__ */ jsxs6("div", { className: "seclai-actions", children: [
|
|
1071
|
+
/* @__PURE__ */ jsx7("button", { type: "submit", className: "seclai-button seclai-button-primary", disabled: busy, children: uploading ? "Uploading\u2026" : "Run" }),
|
|
1072
|
+
run.status === "running" && /* @__PURE__ */ jsx7("button", { type: "button", className: "seclai-button", onClick: run.cancel, children: "Stop" })
|
|
719
1073
|
] })
|
|
720
1074
|
] }),
|
|
721
|
-
run.steps.length > 0 && /* @__PURE__ */
|
|
1075
|
+
run.steps.length > 0 && /* @__PURE__ */ jsx7("ul", { className: "seclai-steps", children: run.steps.map((step) => /* @__PURE__ */ jsxs6("li", { children: [
|
|
722
1076
|
step.name,
|
|
723
1077
|
": ",
|
|
724
1078
|
step.message
|
|
725
1079
|
] }, step.id)) }),
|
|
726
|
-
error && /* @__PURE__ */
|
|
727
|
-
run.status === "cancelled" && /* @__PURE__ */
|
|
728
|
-
(run.text || run.attachments.length > 0) && /* @__PURE__ */
|
|
729
|
-
run.text && /* @__PURE__ */
|
|
730
|
-
run.attachments.
|
|
1080
|
+
error && /* @__PURE__ */ jsx7("p", { className: "seclai-error", role: "alert", children: error }),
|
|
1081
|
+
run.status === "cancelled" && /* @__PURE__ */ jsx7("p", { className: "seclai-notice", children: "Stopped." }),
|
|
1082
|
+
(run.text || run.attachments.length > 0) && /* @__PURE__ */ jsxs6("div", { className: "seclai-output", "aria-live": "polite", "aria-busy": run.status === "running", children: [
|
|
1083
|
+
run.text && /* @__PURE__ */ jsx7(MarkdownContent, { content: run.text }),
|
|
1084
|
+
run.attachments.length > 0 && /* @__PURE__ */ jsx7(MediaGallery, { attachments: run.attachments })
|
|
731
1085
|
] })
|
|
732
1086
|
] });
|
|
733
1087
|
}
|
|
734
1088
|
|
|
735
1089
|
// src/SeclaiAgent.tsx
|
|
736
|
-
import { jsx as
|
|
1090
|
+
import { jsx as jsx8 } from "react/jsx-runtime";
|
|
737
1091
|
function SeclaiAgent({ mode, basePath, agentId }) {
|
|
738
|
-
return mode === "chat" ? /* @__PURE__ */
|
|
1092
|
+
return mode === "chat" ? /* @__PURE__ */ jsx8(AgentChat, { basePath, agentId }) : /* @__PURE__ */ jsx8(AgentRunForm, { basePath, agentId });
|
|
739
1093
|
}
|
|
740
1094
|
export {
|
|
741
1095
|
AgentChat,
|
|
742
1096
|
AgentRunForm,
|
|
743
1097
|
DEFAULT_BASE_PATH,
|
|
744
1098
|
MarkdownContent,
|
|
1099
|
+
MediaGallery,
|
|
745
1100
|
MediaPreview,
|
|
1101
|
+
MediaSkeleton,
|
|
1102
|
+
MediaViewer,
|
|
746
1103
|
SeclaiAgent,
|
|
1104
|
+
attachmentFileName,
|
|
747
1105
|
createEndpoints,
|
|
748
1106
|
fetchAttachmentReferences,
|
|
749
1107
|
uploadFile,
|