@seclai/react 0.0.4 → 0.0.6
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 +59 -0
- package/dist/index.cjs +346 -81
- package/dist/index.d.cts +91 -3
- package/dist/index.d.ts +91 -3
- package/dist/index.js +343 -76
- package/dist/styles.css +134 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,26 +1,15 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
3
|
// src/AgentChat.tsx
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
useEffect as useEffect3,
|
|
6
|
+
useMemo as useMemo2,
|
|
7
|
+
useRef as useRef2,
|
|
8
|
+
useState as useState3
|
|
9
|
+
} from "react";
|
|
5
10
|
|
|
6
|
-
// src/
|
|
7
|
-
import {
|
|
8
|
-
function MarkdownContent({ content }) {
|
|
9
|
-
return /* @__PURE__ */ jsx("div", { className: "seclai-markdown", children: content });
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
// src/placeholders/MediaPreview.tsx
|
|
13
|
-
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
14
|
-
function MediaPreview({ attachment }) {
|
|
15
|
-
const name = attachment.name ?? attachment.id;
|
|
16
|
-
return /* @__PURE__ */ jsxs("figure", { className: "seclai-media", children: [
|
|
17
|
-
attachment.mime.startsWith("image/") && /* @__PURE__ */ jsx2("img", { src: attachment.url, alt: name }),
|
|
18
|
-
/* @__PURE__ */ jsx2("figcaption", { children: /* @__PURE__ */ jsx2("a", { href: attachment.url, download: name, children: name }) })
|
|
19
|
-
] });
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// src/useAgentRun.ts
|
|
23
|
-
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
11
|
+
// src/attachments.ts
|
|
12
|
+
import { useEffect, useState } from "react";
|
|
24
13
|
|
|
25
14
|
// src/endpoints.ts
|
|
26
15
|
var DEFAULT_BASE_PATH = "/api/seclai";
|
|
@@ -53,15 +42,76 @@ async function readJson(res) {
|
|
|
53
42
|
if (!res.ok) throw new Error(await readErrorMessage(res));
|
|
54
43
|
return await res.json();
|
|
55
44
|
}
|
|
56
|
-
async function fetchAttachmentReferences(endpoints, signal) {
|
|
45
|
+
async function fetchAttachmentReferences(endpoints, { signal } = {}) {
|
|
57
46
|
return readJson(await fetch(endpoints.attachmentReferences, { signal }));
|
|
58
47
|
}
|
|
59
|
-
async function uploadFile(endpoints, file) {
|
|
48
|
+
async function uploadFile(endpoints, file, { signal } = {}) {
|
|
60
49
|
const form = new FormData();
|
|
61
50
|
form.set("file", file);
|
|
62
|
-
return readJson(await fetch(endpoints.upload, { method: "POST", body: form }));
|
|
51
|
+
return readJson(await fetch(endpoints.upload, { method: "POST", body: form, signal }));
|
|
63
52
|
}
|
|
64
53
|
|
|
54
|
+
// src/attachments.ts
|
|
55
|
+
function useAcceptedAttachments(endpoints) {
|
|
56
|
+
const [state, setState] = useState({ slots: null, error: null });
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
const controller = new AbortController();
|
|
59
|
+
setState({ slots: null, error: null });
|
|
60
|
+
fetchAttachmentReferences(endpoints, { signal: controller.signal }).then((refs) => setState({ slots: refs.accepted_attachments ?? [], error: null })).catch((err) => {
|
|
61
|
+
if (controller.signal.aborted) return;
|
|
62
|
+
setState({
|
|
63
|
+
slots: null,
|
|
64
|
+
error: err instanceof Error ? err.message : "Could not load this agent."
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
return () => controller.abort();
|
|
68
|
+
}, [endpoints]);
|
|
69
|
+
return state;
|
|
70
|
+
}
|
|
71
|
+
function acceptAttribute(contentTypes) {
|
|
72
|
+
return contentTypes?.length ? contentTypes.join(",") : void 0;
|
|
73
|
+
}
|
|
74
|
+
function acceptsType(contentTypes, type) {
|
|
75
|
+
if (!contentTypes?.length || !type) return true;
|
|
76
|
+
const [major] = type.toLowerCase().split("/");
|
|
77
|
+
return contentTypes.some((pattern) => {
|
|
78
|
+
const p = pattern.trim().toLowerCase();
|
|
79
|
+
if (p === "*/*" || p === "*") return true;
|
|
80
|
+
if (p.endsWith("/*")) return major === p.slice(0, -2);
|
|
81
|
+
return p === type.toLowerCase();
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function messageFileLimits(slots) {
|
|
85
|
+
let max = 0;
|
|
86
|
+
let anyType = false;
|
|
87
|
+
const types = /* @__PURE__ */ new Set();
|
|
88
|
+
for (const slot of slots) {
|
|
89
|
+
max = max == null || slot.max == null ? null : max + slot.max;
|
|
90
|
+
if (slot.content_types?.length) slot.content_types.forEach((t) => types.add(t));
|
|
91
|
+
else anyType = true;
|
|
92
|
+
}
|
|
93
|
+
return { max, contentTypes: anyType ? [] : [...types] };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// src/placeholders/MarkdownContent.tsx
|
|
97
|
+
import { jsx } from "react/jsx-runtime";
|
|
98
|
+
function MarkdownContent({ content }) {
|
|
99
|
+
return /* @__PURE__ */ jsx("div", { className: "seclai-markdown", children: content });
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// src/placeholders/MediaPreview.tsx
|
|
103
|
+
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
104
|
+
function MediaPreview({ attachment }) {
|
|
105
|
+
const name = attachment.name ?? attachment.id;
|
|
106
|
+
return /* @__PURE__ */ jsxs("figure", { className: "seclai-media", children: [
|
|
107
|
+
attachment.mime.startsWith("image/") && /* @__PURE__ */ jsx2("img", { src: attachment.url, alt: name }),
|
|
108
|
+
/* @__PURE__ */ jsx2("figcaption", { children: /* @__PURE__ */ jsx2("a", { href: attachment.url, download: name, children: name }) })
|
|
109
|
+
] });
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// src/useAgentRun.ts
|
|
113
|
+
import { useCallback, useEffect as useEffect2, useMemo, useRef, useState as useState2 } from "react";
|
|
114
|
+
|
|
65
115
|
// src/sse.ts
|
|
66
116
|
function parseData(data) {
|
|
67
117
|
try {
|
|
@@ -206,9 +256,9 @@ function applyEvent(state, { event, data }, endpoints) {
|
|
|
206
256
|
function useAgentRun(options = {}) {
|
|
207
257
|
const { basePath, agentId } = options;
|
|
208
258
|
const endpoints = useMemo(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
209
|
-
const [state, setState] =
|
|
259
|
+
const [state, setState] = useState2(IDLE);
|
|
210
260
|
const active = useRef(null);
|
|
211
|
-
|
|
261
|
+
useEffect2(() => () => active.current?.abort(), []);
|
|
212
262
|
const start = useCallback(
|
|
213
263
|
async ({ input, uploadIds, sessionId }) => {
|
|
214
264
|
active.current?.abort();
|
|
@@ -274,28 +324,203 @@ function createSessionId() {
|
|
|
274
324
|
(byte) => byte.toString(16).padStart(2, "0")
|
|
275
325
|
).join("");
|
|
276
326
|
}
|
|
277
|
-
|
|
327
|
+
var nextLocalId = 0;
|
|
328
|
+
function createPreview(file) {
|
|
329
|
+
if (!file.type.startsWith("image/") || typeof URL.createObjectURL !== "function") return null;
|
|
330
|
+
return URL.createObjectURL(file);
|
|
331
|
+
}
|
|
332
|
+
function revoke(url) {
|
|
333
|
+
if (url && typeof URL.revokeObjectURL === "function") URL.revokeObjectURL(url);
|
|
334
|
+
}
|
|
335
|
+
function hasFiles(dataTransfer) {
|
|
336
|
+
return !!dataTransfer && Array.from(dataTransfer.types ?? []).includes("Files");
|
|
337
|
+
}
|
|
338
|
+
function plural(count, word) {
|
|
339
|
+
return `${count} ${word}${count === 1 ? "" : "s"}`;
|
|
340
|
+
}
|
|
341
|
+
function FileChip({ name, tag, preview, status, error, onRemove }) {
|
|
342
|
+
const meta = status === "uploading" ? "Uploading\u2026" : status === "error" ? error : tag;
|
|
343
|
+
return /* @__PURE__ */ jsxs2("li", { className: "seclai-file", "data-status": status, children: [
|
|
344
|
+
preview && /* @__PURE__ */ jsx3("img", { className: "seclai-file-thumb", src: preview, alt: "" }),
|
|
345
|
+
/* @__PURE__ */ jsxs2("span", { className: "seclai-file-text", children: [
|
|
346
|
+
/* @__PURE__ */ jsx3("span", { className: "seclai-file-name", title: name, children: name }),
|
|
347
|
+
meta && /* @__PURE__ */ jsx3("span", { className: "seclai-file-meta", children: meta })
|
|
348
|
+
] }),
|
|
349
|
+
onRemove && /* @__PURE__ */ jsx3(
|
|
350
|
+
"button",
|
|
351
|
+
{
|
|
352
|
+
type: "button",
|
|
353
|
+
className: "seclai-file-remove",
|
|
354
|
+
"aria-label": `Remove ${name}`,
|
|
355
|
+
onClick: onRemove,
|
|
356
|
+
children: "\xD7"
|
|
357
|
+
}
|
|
358
|
+
)
|
|
359
|
+
] });
|
|
360
|
+
}
|
|
361
|
+
function Turn({ turn, previews }) {
|
|
278
362
|
return /* @__PURE__ */ jsxs2("li", { className: `seclai-turn seclai-turn-${turn.role}`, children: [
|
|
363
|
+
turn.files && turn.files.length > 0 && /* @__PURE__ */ jsx3("ul", { className: "seclai-files", "aria-label": "Attached files", children: turn.files.map((file) => /* @__PURE__ */ jsx3(
|
|
364
|
+
FileChip,
|
|
365
|
+
{
|
|
366
|
+
name: file.name,
|
|
367
|
+
tag: file.tag,
|
|
368
|
+
preview: previews.get(file.uploadId)
|
|
369
|
+
},
|
|
370
|
+
file.uploadId
|
|
371
|
+
)) }),
|
|
279
372
|
turn.text && /* @__PURE__ */ jsx3(MarkdownContent, { content: turn.text }),
|
|
280
373
|
turn.attachments?.map((attachment) => /* @__PURE__ */ jsx3(MediaPreview, { attachment }, attachment.id)),
|
|
281
374
|
turn.error && /* @__PURE__ */ jsx3("p", { className: "seclai-error", role: "alert", children: turn.error }),
|
|
282
375
|
turn.cancelled && /* @__PURE__ */ jsx3("p", { className: "seclai-notice", children: "Stopped." })
|
|
283
376
|
] });
|
|
284
377
|
}
|
|
285
|
-
function
|
|
378
|
+
function AttachControl({
|
|
379
|
+
label,
|
|
380
|
+
accept,
|
|
381
|
+
multiple,
|
|
382
|
+
disabled,
|
|
383
|
+
onFiles
|
|
384
|
+
}) {
|
|
385
|
+
return /* @__PURE__ */ jsxs2("label", { className: "seclai-button seclai-attach", "aria-disabled": disabled || void 0, children: [
|
|
386
|
+
label,
|
|
387
|
+
/* @__PURE__ */ jsx3(
|
|
388
|
+
"input",
|
|
389
|
+
{
|
|
390
|
+
type: "file",
|
|
391
|
+
className: "seclai-sr-only",
|
|
392
|
+
accept,
|
|
393
|
+
multiple,
|
|
394
|
+
disabled,
|
|
395
|
+
onChange: (e) => {
|
|
396
|
+
const picked = Array.from(e.target.files ?? []);
|
|
397
|
+
e.target.value = "";
|
|
398
|
+
if (picked.length) onFiles(picked);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
)
|
|
402
|
+
] });
|
|
403
|
+
}
|
|
404
|
+
function AgentChat({
|
|
405
|
+
basePath,
|
|
406
|
+
agentId,
|
|
407
|
+
placeholder = "Send a message",
|
|
408
|
+
attachButtons,
|
|
409
|
+
onBeforeSend
|
|
410
|
+
}) {
|
|
411
|
+
const endpoints = useMemo2(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
286
412
|
const run = useAgentRun({ basePath, agentId });
|
|
287
|
-
const
|
|
288
|
-
const [
|
|
413
|
+
const { slots } = useAcceptedAttachments(endpoints);
|
|
414
|
+
const [turns, setTurns] = useState3([]);
|
|
415
|
+
const [message, setMessage] = useState3("");
|
|
416
|
+
const [files, setFiles] = useState3([]);
|
|
417
|
+
const [notice, setNotice] = useState3(null);
|
|
418
|
+
const [dragging, setDragging] = useState3(false);
|
|
289
419
|
const sessionId = useRef2(null);
|
|
290
420
|
const nextTurnId = useRef2(0);
|
|
421
|
+
const sentPreviews = useRef2(/* @__PURE__ */ new Map());
|
|
422
|
+
const pendingPreviews = useRef2(/* @__PURE__ */ new Set());
|
|
423
|
+
useEffect3(
|
|
424
|
+
() => () => {
|
|
425
|
+
pendingPreviews.current.forEach(revoke);
|
|
426
|
+
sentPreviews.current.forEach(revoke);
|
|
427
|
+
},
|
|
428
|
+
[]
|
|
429
|
+
);
|
|
430
|
+
const limits = slots?.length ? messageFileLimits(slots) : null;
|
|
431
|
+
const acceptsFiles = !!limits && (limits.max == null || limits.max > 0);
|
|
291
432
|
const running = run.status === "running";
|
|
433
|
+
const uploading = files.some((f) => f.status === "uploading");
|
|
434
|
+
const ready = files.filter((f) => f.status === "ready" && f.uploadId);
|
|
435
|
+
const room = limits?.max == null ? Infinity : limits.max - files.filter((f) => f.status !== "error").length;
|
|
436
|
+
const canSend = !running && !uploading && (!!message.trim() || ready.length > 0);
|
|
437
|
+
function setFile(localId, patch) {
|
|
438
|
+
setFiles((prev) => prev.map((f) => f.localId === localId ? { ...f, ...patch } : f));
|
|
439
|
+
}
|
|
440
|
+
function upload(item, file) {
|
|
441
|
+
uploadFile(endpoints, file).then(
|
|
442
|
+
(result) => setFile(item.localId, { status: "ready", uploadId: result.id }),
|
|
443
|
+
(err) => setFile(item.localId, {
|
|
444
|
+
status: "error",
|
|
445
|
+
error: err instanceof Error ? err.message : "The upload failed."
|
|
446
|
+
})
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
function addFiles(incoming, tag) {
|
|
450
|
+
if (!acceptsFiles || !limits || incoming.length === 0) return;
|
|
451
|
+
const problems = [];
|
|
452
|
+
const added = [];
|
|
453
|
+
let left = room;
|
|
454
|
+
let tooMany = false;
|
|
455
|
+
for (const file of incoming) {
|
|
456
|
+
if (!acceptsType(limits.contentTypes, file.type)) {
|
|
457
|
+
problems.push(`${file.name} isn't a type of file this agent accepts.`);
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
if (left <= 0) {
|
|
461
|
+
tooMany = true;
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
left--;
|
|
465
|
+
const preview = createPreview(file);
|
|
466
|
+
if (preview) pendingPreviews.current.add(preview);
|
|
467
|
+
added.push([
|
|
468
|
+
{
|
|
469
|
+
localId: `file-${++nextLocalId}`,
|
|
470
|
+
name: file.name,
|
|
471
|
+
contentType: file.type,
|
|
472
|
+
tag,
|
|
473
|
+
preview,
|
|
474
|
+
status: "uploading"
|
|
475
|
+
},
|
|
476
|
+
file
|
|
477
|
+
]);
|
|
478
|
+
}
|
|
479
|
+
if (tooMany) problems.push(`You can attach up to ${plural(limits.max, "file")} per message.`);
|
|
480
|
+
setNotice(problems.length ? problems.join(" ") : null);
|
|
481
|
+
if (added.length === 0) return;
|
|
482
|
+
setFiles((prev) => [...prev, ...added.map(([item]) => item)]);
|
|
483
|
+
for (const [item, file] of added) upload(item, file);
|
|
484
|
+
}
|
|
485
|
+
function remove(localId) {
|
|
486
|
+
const gone = files.find((f) => f.localId === localId);
|
|
487
|
+
if (gone?.preview) {
|
|
488
|
+
revoke(gone.preview);
|
|
489
|
+
pendingPreviews.current.delete(gone.preview);
|
|
490
|
+
}
|
|
491
|
+
setFiles((prev) => prev.filter((f) => f.localId !== localId));
|
|
492
|
+
setNotice(null);
|
|
493
|
+
}
|
|
292
494
|
async function send() {
|
|
495
|
+
if (!canSend) return;
|
|
293
496
|
const text = message.trim();
|
|
294
|
-
|
|
497
|
+
const sent = ready.map((f) => ({
|
|
498
|
+
uploadId: f.uploadId,
|
|
499
|
+
name: f.name,
|
|
500
|
+
contentType: f.contentType,
|
|
501
|
+
...f.tag ? { tag: f.tag } : {}
|
|
502
|
+
}));
|
|
503
|
+
const override = onBeforeSend?.({ input: text, files: sent });
|
|
504
|
+
const input = (typeof override === "string" ? override : text) || void 0;
|
|
505
|
+
for (const f of files) {
|
|
506
|
+
if (!f.preview) continue;
|
|
507
|
+
pendingPreviews.current.delete(f.preview);
|
|
508
|
+
if (f.status === "ready" && f.uploadId) sentPreviews.current.set(f.uploadId, f.preview);
|
|
509
|
+
else revoke(f.preview);
|
|
510
|
+
}
|
|
295
511
|
sessionId.current ??= createSessionId();
|
|
296
512
|
setMessage("");
|
|
297
|
-
|
|
298
|
-
|
|
513
|
+
setFiles([]);
|
|
514
|
+
setNotice(null);
|
|
515
|
+
setTurns((prev) => [
|
|
516
|
+
...prev,
|
|
517
|
+
{ id: nextTurnId.current++, role: "user", text, ...sent.length ? { files: sent } : {} }
|
|
518
|
+
]);
|
|
519
|
+
const result = await run.start({
|
|
520
|
+
input,
|
|
521
|
+
uploadIds: sent.length ? sent.map((f) => f.uploadId) : void 0,
|
|
522
|
+
sessionId: sessionId.current
|
|
523
|
+
});
|
|
299
524
|
setTurns((prev) => [
|
|
300
525
|
...prev,
|
|
301
526
|
{
|
|
@@ -318,41 +543,91 @@ function AgentChat({ basePath, agentId, placeholder = "Send a message" }) {
|
|
|
318
543
|
void send();
|
|
319
544
|
}
|
|
320
545
|
}
|
|
546
|
+
function onPaste(event) {
|
|
547
|
+
const data = event.clipboardData;
|
|
548
|
+
const pasted = Array.from(data?.files ?? []);
|
|
549
|
+
if (pasted.length === 0) return;
|
|
550
|
+
if (data.getData("text/plain")) return;
|
|
551
|
+
event.preventDefault();
|
|
552
|
+
addFiles(pasted);
|
|
553
|
+
}
|
|
554
|
+
const dropHandlers = acceptsFiles ? {
|
|
555
|
+
onDragOver(event) {
|
|
556
|
+
if (!hasFiles(event.dataTransfer)) return;
|
|
557
|
+
event.preventDefault();
|
|
558
|
+
setDragging(true);
|
|
559
|
+
},
|
|
560
|
+
onDragLeave(event) {
|
|
561
|
+
if (!event.currentTarget.contains(event.relatedTarget)) setDragging(false);
|
|
562
|
+
},
|
|
563
|
+
onDrop(event) {
|
|
564
|
+
if (!hasFiles(event.dataTransfer)) return;
|
|
565
|
+
event.preventDefault();
|
|
566
|
+
setDragging(false);
|
|
567
|
+
addFiles(Array.from(event.dataTransfer.files));
|
|
568
|
+
}
|
|
569
|
+
} : {};
|
|
570
|
+
const buttons = attachButtons?.length ? attachButtons : [{ label: "Attach" }];
|
|
321
571
|
return /* @__PURE__ */ jsxs2("div", { className: "seclai-agent seclai-chat", children: [
|
|
322
572
|
/* @__PURE__ */ jsxs2("ul", { className: "seclai-turns", "aria-live": "polite", "aria-busy": running, children: [
|
|
323
|
-
turns.map((turn) => /* @__PURE__ */ jsx3(Turn, { turn }, turn.id)),
|
|
573
|
+
turns.map((turn) => /* @__PURE__ */ jsx3(Turn, { turn, previews: sentPreviews.current }, turn.id)),
|
|
324
574
|
running && /* @__PURE__ */ jsx3("li", { className: "seclai-turn seclai-turn-assistant", children: run.text ? /* @__PURE__ */ jsx3(MarkdownContent, { content: run.text }) : /* @__PURE__ */ jsx3("p", { className: "seclai-notice", children: "Thinking\u2026" }) })
|
|
325
575
|
] }),
|
|
326
|
-
/* @__PURE__ */ jsxs2(
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
576
|
+
/* @__PURE__ */ jsxs2(
|
|
577
|
+
"form",
|
|
578
|
+
{
|
|
579
|
+
className: dragging ? "seclai-form seclai-dropping" : "seclai-form",
|
|
580
|
+
onSubmit,
|
|
581
|
+
...dropHandlers,
|
|
582
|
+
children: [
|
|
583
|
+
files.length > 0 && /* @__PURE__ */ jsx3("ul", { className: "seclai-files", "aria-label": "Files to send", children: files.map((f) => /* @__PURE__ */ jsx3(
|
|
584
|
+
FileChip,
|
|
585
|
+
{
|
|
586
|
+
name: f.name,
|
|
587
|
+
tag: f.tag,
|
|
588
|
+
preview: f.preview,
|
|
589
|
+
status: f.status,
|
|
590
|
+
error: f.error,
|
|
591
|
+
onRemove: () => remove(f.localId)
|
|
592
|
+
},
|
|
593
|
+
f.localId
|
|
594
|
+
)) }),
|
|
595
|
+
/* @__PURE__ */ jsx3(
|
|
596
|
+
"textarea",
|
|
597
|
+
{
|
|
598
|
+
rows: 2,
|
|
599
|
+
"aria-label": "Message",
|
|
600
|
+
placeholder,
|
|
601
|
+
value: message,
|
|
602
|
+
onChange: (e) => setMessage(e.target.value),
|
|
603
|
+
onKeyDown,
|
|
604
|
+
onPaste: acceptsFiles ? onPaste : void 0
|
|
605
|
+
}
|
|
606
|
+
),
|
|
607
|
+
notice && /* @__PURE__ */ jsx3("p", { className: "seclai-error", role: "alert", children: notice }),
|
|
608
|
+
/* @__PURE__ */ jsxs2("div", { className: "seclai-actions", children: [
|
|
609
|
+
acceptsFiles && buttons.map((button) => /* @__PURE__ */ jsx3(
|
|
610
|
+
AttachControl,
|
|
611
|
+
{
|
|
612
|
+
label: button.label,
|
|
613
|
+
accept: acceptAttribute(limits.contentTypes),
|
|
614
|
+
multiple: room > 1,
|
|
615
|
+
disabled: room <= 0,
|
|
616
|
+
onFiles: (picked) => addFiles(picked, button.tag)
|
|
617
|
+
},
|
|
618
|
+
`${button.label}\0${button.tag ?? ""}`
|
|
619
|
+
)),
|
|
620
|
+
/* @__PURE__ */ jsx3("button", { type: "submit", className: "seclai-button seclai-button-primary", disabled: !canSend, children: "Send" }),
|
|
621
|
+
running && /* @__PURE__ */ jsx3("button", { type: "button", className: "seclai-button", onClick: run.cancel, children: "Stop" })
|
|
622
|
+
] })
|
|
623
|
+
]
|
|
624
|
+
}
|
|
625
|
+
)
|
|
351
626
|
] });
|
|
352
627
|
}
|
|
353
628
|
|
|
354
629
|
// src/AgentRunForm.tsx
|
|
355
|
-
import {
|
|
630
|
+
import { useMemo as useMemo3, useState as useState4 } from "react";
|
|
356
631
|
import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
357
632
|
function slotProblem(slot, count) {
|
|
358
633
|
const label = slot.label ?? slot.name;
|
|
@@ -364,24 +639,13 @@ function slotProblem(slot, count) {
|
|
|
364
639
|
return null;
|
|
365
640
|
}
|
|
366
641
|
function AgentRunForm({ basePath, agentId, inputLabel = "Input", onFinish }) {
|
|
367
|
-
const endpoints =
|
|
642
|
+
const endpoints = useMemo3(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
368
643
|
const run = useAgentRun({ basePath, agentId });
|
|
369
|
-
const
|
|
370
|
-
const [
|
|
371
|
-
const [
|
|
372
|
-
const [
|
|
373
|
-
const [
|
|
374
|
-
const [formError, setFormError] = useState3(null);
|
|
375
|
-
useEffect2(() => {
|
|
376
|
-
const controller = new AbortController();
|
|
377
|
-
setSlots(null);
|
|
378
|
-
setLoadError(null);
|
|
379
|
-
fetchAttachmentReferences(endpoints, controller.signal).then((refs) => setSlots(refs.accepted_attachments ?? [])).catch((err) => {
|
|
380
|
-
if (controller.signal.aborted) return;
|
|
381
|
-
setLoadError(err instanceof Error ? err.message : "Could not load this agent.");
|
|
382
|
-
});
|
|
383
|
-
return () => controller.abort();
|
|
384
|
-
}, [endpoints]);
|
|
644
|
+
const { slots, error: loadError } = useAcceptedAttachments(endpoints);
|
|
645
|
+
const [input, setInput] = useState4("");
|
|
646
|
+
const [files, setFiles] = useState4({});
|
|
647
|
+
const [uploading, setUploading] = useState4(false);
|
|
648
|
+
const [formError, setFormError] = useState4(null);
|
|
385
649
|
const selected = Object.values(files).flat();
|
|
386
650
|
const busy = uploading || run.status === "running";
|
|
387
651
|
async function submit(event) {
|
|
@@ -443,7 +707,7 @@ function AgentRunForm({ basePath, agentId, inputLabel = "Input", onFinish }) {
|
|
|
443
707
|
{
|
|
444
708
|
type: "file",
|
|
445
709
|
name: slot.name,
|
|
446
|
-
accept: slot.content_types
|
|
710
|
+
accept: acceptAttribute(slot.content_types),
|
|
447
711
|
multiple: slot.max == null || slot.max > 1,
|
|
448
712
|
onChange: (e) => setFiles((prev) => ({ ...prev, [slot.name]: Array.from(e.target.files ?? []) }))
|
|
449
713
|
}
|
|
@@ -480,5 +744,8 @@ export {
|
|
|
480
744
|
MarkdownContent,
|
|
481
745
|
MediaPreview,
|
|
482
746
|
SeclaiAgent,
|
|
747
|
+
createEndpoints,
|
|
748
|
+
fetchAttachmentReferences,
|
|
749
|
+
uploadFile,
|
|
483
750
|
useAgentRun
|
|
484
751
|
};
|
package/dist/styles.css
CHANGED
|
@@ -170,6 +170,15 @@
|
|
|
170
170
|
.seclai-agent input[type="file"]:hover::file-selector-button {
|
|
171
171
|
background: var(--seclai-surface-hover);
|
|
172
172
|
}
|
|
173
|
+
|
|
174
|
+
.seclai-attach[aria-disabled="true"]:hover {
|
|
175
|
+
background: transparent;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
.seclai-file-remove:hover {
|
|
179
|
+
color: var(--seclai-text);
|
|
180
|
+
background: var(--seclai-surface-hover);
|
|
181
|
+
}
|
|
173
182
|
}
|
|
174
183
|
|
|
175
184
|
@media (pointer: coarse) {
|
|
@@ -236,6 +245,131 @@
|
|
|
236
245
|
border-radius: var(--seclai-radius);
|
|
237
246
|
}
|
|
238
247
|
|
|
248
|
+
/* Attachments in the chat composer */
|
|
249
|
+
.seclai-form.seclai-dropping {
|
|
250
|
+
outline: 2px dashed var(--seclai-accent);
|
|
251
|
+
outline-offset: 4px;
|
|
252
|
+
border-radius: var(--seclai-radius);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/* Hidden from view but not from the browser, so a wrapping label opens the picker. */
|
|
256
|
+
.seclai-agent .seclai-sr-only {
|
|
257
|
+
position: absolute;
|
|
258
|
+
width: 1px;
|
|
259
|
+
height: 1px;
|
|
260
|
+
min-height: 0;
|
|
261
|
+
margin: -1px;
|
|
262
|
+
padding: 0;
|
|
263
|
+
overflow: hidden;
|
|
264
|
+
clip: rect(0, 0, 0, 0);
|
|
265
|
+
white-space: nowrap;
|
|
266
|
+
border: 0;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
.seclai-attach:focus-within {
|
|
270
|
+
outline: 2px solid var(--seclai-focus);
|
|
271
|
+
outline-offset: 2px;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/* A mouse click on the label focuses the input too; ring keyboard focus only. */
|
|
275
|
+
@supports selector(:has(*)) {
|
|
276
|
+
.seclai-attach:focus-within:not(:has(:focus-visible)) {
|
|
277
|
+
outline: none;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
.seclai-attach[aria-disabled="true"] {
|
|
282
|
+
cursor: not-allowed;
|
|
283
|
+
opacity: 0.55;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
.seclai-files {
|
|
287
|
+
display: flex;
|
|
288
|
+
flex-wrap: wrap;
|
|
289
|
+
gap: 8px;
|
|
290
|
+
margin: 0;
|
|
291
|
+
padding: 0;
|
|
292
|
+
list-style: none;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
.seclai-file {
|
|
296
|
+
display: inline-flex;
|
|
297
|
+
align-items: center;
|
|
298
|
+
gap: 8px;
|
|
299
|
+
max-width: 240px;
|
|
300
|
+
min-height: 40px;
|
|
301
|
+
padding: 4px 4px 4px 8px;
|
|
302
|
+
font-size: 0.8125em;
|
|
303
|
+
background: var(--seclai-surface);
|
|
304
|
+
border: 1px solid var(--seclai-border);
|
|
305
|
+
border-radius: var(--seclai-radius);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
.seclai-file:has(.seclai-file-thumb) {
|
|
309
|
+
padding-inline-start: 4px;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
.seclai-file-thumb {
|
|
313
|
+
flex: none;
|
|
314
|
+
width: 32px;
|
|
315
|
+
height: 32px;
|
|
316
|
+
object-fit: cover;
|
|
317
|
+
border-radius: calc(var(--seclai-radius) - 3px);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
.seclai-file-text {
|
|
321
|
+
display: flex;
|
|
322
|
+
flex-direction: column;
|
|
323
|
+
min-width: 0;
|
|
324
|
+
line-height: 1.3;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
.seclai-file-name {
|
|
328
|
+
overflow: hidden;
|
|
329
|
+
text-overflow: ellipsis;
|
|
330
|
+
white-space: nowrap;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
.seclai-file-meta {
|
|
334
|
+
color: var(--seclai-muted);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
.seclai-file[data-status="uploading"] .seclai-file-thumb {
|
|
338
|
+
opacity: 0.5;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
.seclai-file[data-status="error"] {
|
|
342
|
+
border-color: var(--seclai-error);
|
|
343
|
+
background: var(--seclai-error-surface);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
.seclai-file[data-status="error"] .seclai-file-meta {
|
|
347
|
+
color: var(--seclai-error);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
.seclai-file-remove {
|
|
351
|
+
position: relative;
|
|
352
|
+
flex: none;
|
|
353
|
+
width: 28px;
|
|
354
|
+
height: 28px;
|
|
355
|
+
padding: 0;
|
|
356
|
+
font: inherit;
|
|
357
|
+
font-size: 1.25em;
|
|
358
|
+
line-height: 1;
|
|
359
|
+
color: var(--seclai-muted);
|
|
360
|
+
cursor: pointer;
|
|
361
|
+
background: transparent;
|
|
362
|
+
border: 0;
|
|
363
|
+
border-radius: calc(var(--seclai-radius) - 3px);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/* A 44px hit target around the small button. */
|
|
367
|
+
.seclai-file-remove::before {
|
|
368
|
+
content: "";
|
|
369
|
+
position: absolute;
|
|
370
|
+
inset: -8px;
|
|
371
|
+
}
|
|
372
|
+
|
|
239
373
|
.seclai-markdown {
|
|
240
374
|
white-space: pre-wrap;
|
|
241
375
|
overflow-wrap: anywhere;
|