@seclai/react 0.0.5 → 0.0.7
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 +57 -3
- package/dist/index.cjs +361 -83
- package/dist/index.d.cts +66 -4
- package/dist/index.d.ts +66 -4
- package/dist/index.js +361 -78
- 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";
|
|
@@ -62,6 +51,67 @@ async function uploadFile(endpoints, file, { signal } = {}) {
|
|
|
62
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 {
|
|
@@ -126,7 +176,8 @@ var IDLE = {
|
|
|
126
176
|
steps: [],
|
|
127
177
|
attachments: [],
|
|
128
178
|
error: null,
|
|
129
|
-
runId: null
|
|
179
|
+
runId: null,
|
|
180
|
+
startedAt: null
|
|
130
181
|
};
|
|
131
182
|
function toAttachments(files, runId, endpoints) {
|
|
132
183
|
if (!files || !runId) return [];
|
|
@@ -204,17 +255,21 @@ function applyEvent(state, { event, data }, endpoints) {
|
|
|
204
255
|
}
|
|
205
256
|
}
|
|
206
257
|
function useAgentRun(options = {}) {
|
|
207
|
-
const { basePath, agentId } = options;
|
|
258
|
+
const { basePath, agentId, onEvent } = options;
|
|
208
259
|
const endpoints = useMemo(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
209
|
-
const [state, setState] =
|
|
260
|
+
const [state, setState] = useState2(IDLE);
|
|
210
261
|
const active = useRef(null);
|
|
211
|
-
|
|
262
|
+
const onEventRef = useRef(onEvent);
|
|
263
|
+
useEffect2(() => {
|
|
264
|
+
onEventRef.current = onEvent;
|
|
265
|
+
});
|
|
266
|
+
useEffect2(() => () => active.current?.abort(), []);
|
|
212
267
|
const start = useCallback(
|
|
213
268
|
async ({ input, uploadIds, sessionId }) => {
|
|
214
269
|
active.current?.abort();
|
|
215
270
|
const controller = new AbortController();
|
|
216
271
|
active.current = controller;
|
|
217
|
-
let current = { ...IDLE, status: "running" };
|
|
272
|
+
let current = { ...IDLE, status: "running", startedAt: Date.now() };
|
|
218
273
|
const update = (next) => {
|
|
219
274
|
current = next;
|
|
220
275
|
if (active.current === controller) setState(next);
|
|
@@ -236,10 +291,18 @@ function useAgentRun(options = {}) {
|
|
|
236
291
|
return current;
|
|
237
292
|
}
|
|
238
293
|
for await (const event of readSseStream(res.body)) {
|
|
294
|
+
if (controller.signal.aborted) break;
|
|
239
295
|
update(applyEvent(current, event, endpoints));
|
|
296
|
+
try {
|
|
297
|
+
onEventRef.current?.(event);
|
|
298
|
+
} catch (err) {
|
|
299
|
+
console.error("useAgentRun: onEvent threw", err);
|
|
300
|
+
}
|
|
240
301
|
if (current.status !== "running") break;
|
|
241
302
|
}
|
|
242
|
-
if (
|
|
303
|
+
if (controller.signal.aborted) {
|
|
304
|
+
if (current.status === "running") update({ ...current, status: "cancelled" });
|
|
305
|
+
} else if (current.status === "running") {
|
|
243
306
|
update({
|
|
244
307
|
...current,
|
|
245
308
|
status: "failed",
|
|
@@ -262,7 +325,13 @@ function useAgentRun(options = {}) {
|
|
|
262
325
|
[endpoints]
|
|
263
326
|
);
|
|
264
327
|
const cancel = useCallback(() => active.current?.abort(), []);
|
|
265
|
-
|
|
328
|
+
const reset = useCallback(() => {
|
|
329
|
+
const controller = active.current;
|
|
330
|
+
active.current = null;
|
|
331
|
+
controller?.abort();
|
|
332
|
+
setState(IDLE);
|
|
333
|
+
}, []);
|
|
334
|
+
return { ...state, start, cancel, reset };
|
|
266
335
|
}
|
|
267
336
|
|
|
268
337
|
// src/AgentChat.tsx
|
|
@@ -274,28 +343,203 @@ function createSessionId() {
|
|
|
274
343
|
(byte) => byte.toString(16).padStart(2, "0")
|
|
275
344
|
).join("");
|
|
276
345
|
}
|
|
277
|
-
|
|
346
|
+
var nextLocalId = 0;
|
|
347
|
+
function createPreview(file) {
|
|
348
|
+
if (!file.type.startsWith("image/") || typeof URL.createObjectURL !== "function") return null;
|
|
349
|
+
return URL.createObjectURL(file);
|
|
350
|
+
}
|
|
351
|
+
function revoke(url) {
|
|
352
|
+
if (url && typeof URL.revokeObjectURL === "function") URL.revokeObjectURL(url);
|
|
353
|
+
}
|
|
354
|
+
function hasFiles(dataTransfer) {
|
|
355
|
+
return !!dataTransfer && Array.from(dataTransfer.types ?? []).includes("Files");
|
|
356
|
+
}
|
|
357
|
+
function plural(count, word) {
|
|
358
|
+
return `${count} ${word}${count === 1 ? "" : "s"}`;
|
|
359
|
+
}
|
|
360
|
+
function FileChip({ name, tag, preview, status, error, onRemove }) {
|
|
361
|
+
const meta = status === "uploading" ? "Uploading\u2026" : status === "error" ? error : tag;
|
|
362
|
+
return /* @__PURE__ */ jsxs2("li", { className: "seclai-file", "data-status": status, children: [
|
|
363
|
+
preview && /* @__PURE__ */ jsx3("img", { className: "seclai-file-thumb", src: preview, alt: "" }),
|
|
364
|
+
/* @__PURE__ */ jsxs2("span", { className: "seclai-file-text", children: [
|
|
365
|
+
/* @__PURE__ */ jsx3("span", { className: "seclai-file-name", title: name, children: name }),
|
|
366
|
+
meta && /* @__PURE__ */ jsx3("span", { className: "seclai-file-meta", children: meta })
|
|
367
|
+
] }),
|
|
368
|
+
onRemove && /* @__PURE__ */ jsx3(
|
|
369
|
+
"button",
|
|
370
|
+
{
|
|
371
|
+
type: "button",
|
|
372
|
+
className: "seclai-file-remove",
|
|
373
|
+
"aria-label": `Remove ${name}`,
|
|
374
|
+
onClick: onRemove,
|
|
375
|
+
children: "\xD7"
|
|
376
|
+
}
|
|
377
|
+
)
|
|
378
|
+
] });
|
|
379
|
+
}
|
|
380
|
+
function Turn({ turn, previews }) {
|
|
278
381
|
return /* @__PURE__ */ jsxs2("li", { className: `seclai-turn seclai-turn-${turn.role}`, children: [
|
|
382
|
+
turn.files && turn.files.length > 0 && /* @__PURE__ */ jsx3("ul", { className: "seclai-files", "aria-label": "Attached files", children: turn.files.map((file) => /* @__PURE__ */ jsx3(
|
|
383
|
+
FileChip,
|
|
384
|
+
{
|
|
385
|
+
name: file.name,
|
|
386
|
+
tag: file.tag,
|
|
387
|
+
preview: previews.get(file.uploadId)
|
|
388
|
+
},
|
|
389
|
+
file.uploadId
|
|
390
|
+
)) }),
|
|
279
391
|
turn.text && /* @__PURE__ */ jsx3(MarkdownContent, { content: turn.text }),
|
|
280
392
|
turn.attachments?.map((attachment) => /* @__PURE__ */ jsx3(MediaPreview, { attachment }, attachment.id)),
|
|
281
393
|
turn.error && /* @__PURE__ */ jsx3("p", { className: "seclai-error", role: "alert", children: turn.error }),
|
|
282
394
|
turn.cancelled && /* @__PURE__ */ jsx3("p", { className: "seclai-notice", children: "Stopped." })
|
|
283
395
|
] });
|
|
284
396
|
}
|
|
285
|
-
function
|
|
397
|
+
function AttachControl({
|
|
398
|
+
label,
|
|
399
|
+
accept,
|
|
400
|
+
multiple,
|
|
401
|
+
disabled,
|
|
402
|
+
onFiles
|
|
403
|
+
}) {
|
|
404
|
+
return /* @__PURE__ */ jsxs2("label", { className: "seclai-button seclai-attach", "aria-disabled": disabled || void 0, children: [
|
|
405
|
+
label,
|
|
406
|
+
/* @__PURE__ */ jsx3(
|
|
407
|
+
"input",
|
|
408
|
+
{
|
|
409
|
+
type: "file",
|
|
410
|
+
className: "seclai-sr-only",
|
|
411
|
+
accept,
|
|
412
|
+
multiple,
|
|
413
|
+
disabled,
|
|
414
|
+
onChange: (e) => {
|
|
415
|
+
const picked = Array.from(e.target.files ?? []);
|
|
416
|
+
e.target.value = "";
|
|
417
|
+
if (picked.length) onFiles(picked);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
)
|
|
421
|
+
] });
|
|
422
|
+
}
|
|
423
|
+
function AgentChat({
|
|
424
|
+
basePath,
|
|
425
|
+
agentId,
|
|
426
|
+
placeholder = "Send a message",
|
|
427
|
+
attachButtons,
|
|
428
|
+
onBeforeSend
|
|
429
|
+
}) {
|
|
430
|
+
const endpoints = useMemo2(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
286
431
|
const run = useAgentRun({ basePath, agentId });
|
|
287
|
-
const
|
|
288
|
-
const [
|
|
432
|
+
const { slots } = useAcceptedAttachments(endpoints);
|
|
433
|
+
const [turns, setTurns] = useState3([]);
|
|
434
|
+
const [message, setMessage] = useState3("");
|
|
435
|
+
const [files, setFiles] = useState3([]);
|
|
436
|
+
const [notice, setNotice] = useState3(null);
|
|
437
|
+
const [dragging, setDragging] = useState3(false);
|
|
289
438
|
const sessionId = useRef2(null);
|
|
290
439
|
const nextTurnId = useRef2(0);
|
|
440
|
+
const sentPreviews = useRef2(/* @__PURE__ */ new Map());
|
|
441
|
+
const pendingPreviews = useRef2(/* @__PURE__ */ new Set());
|
|
442
|
+
useEffect3(
|
|
443
|
+
() => () => {
|
|
444
|
+
pendingPreviews.current.forEach(revoke);
|
|
445
|
+
sentPreviews.current.forEach(revoke);
|
|
446
|
+
},
|
|
447
|
+
[]
|
|
448
|
+
);
|
|
449
|
+
const limits = slots?.length ? messageFileLimits(slots) : null;
|
|
450
|
+
const acceptsFiles = !!limits && (limits.max == null || limits.max > 0);
|
|
291
451
|
const running = run.status === "running";
|
|
452
|
+
const uploading = files.some((f) => f.status === "uploading");
|
|
453
|
+
const ready = files.filter((f) => f.status === "ready" && f.uploadId);
|
|
454
|
+
const room = limits?.max == null ? Infinity : limits.max - files.filter((f) => f.status !== "error").length;
|
|
455
|
+
const canSend = !running && !uploading && (!!message.trim() || ready.length > 0);
|
|
456
|
+
function setFile(localId, patch) {
|
|
457
|
+
setFiles((prev) => prev.map((f) => f.localId === localId ? { ...f, ...patch } : f));
|
|
458
|
+
}
|
|
459
|
+
function upload(item, file) {
|
|
460
|
+
uploadFile(endpoints, file).then(
|
|
461
|
+
(result) => setFile(item.localId, { status: "ready", uploadId: result.id }),
|
|
462
|
+
(err) => setFile(item.localId, {
|
|
463
|
+
status: "error",
|
|
464
|
+
error: err instanceof Error ? err.message : "The upload failed."
|
|
465
|
+
})
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
function addFiles(incoming, tag) {
|
|
469
|
+
if (!acceptsFiles || !limits || incoming.length === 0) return;
|
|
470
|
+
const problems = [];
|
|
471
|
+
const added = [];
|
|
472
|
+
let left = room;
|
|
473
|
+
let tooMany = false;
|
|
474
|
+
for (const file of incoming) {
|
|
475
|
+
if (!acceptsType(limits.contentTypes, file.type)) {
|
|
476
|
+
problems.push(`${file.name} isn't a type of file this agent accepts.`);
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
if (left <= 0) {
|
|
480
|
+
tooMany = true;
|
|
481
|
+
continue;
|
|
482
|
+
}
|
|
483
|
+
left--;
|
|
484
|
+
const preview = createPreview(file);
|
|
485
|
+
if (preview) pendingPreviews.current.add(preview);
|
|
486
|
+
added.push([
|
|
487
|
+
{
|
|
488
|
+
localId: `file-${++nextLocalId}`,
|
|
489
|
+
name: file.name,
|
|
490
|
+
contentType: file.type,
|
|
491
|
+
tag,
|
|
492
|
+
preview,
|
|
493
|
+
status: "uploading"
|
|
494
|
+
},
|
|
495
|
+
file
|
|
496
|
+
]);
|
|
497
|
+
}
|
|
498
|
+
if (tooMany) problems.push(`You can attach up to ${plural(limits.max, "file")} per message.`);
|
|
499
|
+
setNotice(problems.length ? problems.join(" ") : null);
|
|
500
|
+
if (added.length === 0) return;
|
|
501
|
+
setFiles((prev) => [...prev, ...added.map(([item]) => item)]);
|
|
502
|
+
for (const [item, file] of added) upload(item, file);
|
|
503
|
+
}
|
|
504
|
+
function remove(localId) {
|
|
505
|
+
const gone = files.find((f) => f.localId === localId);
|
|
506
|
+
if (gone?.preview) {
|
|
507
|
+
revoke(gone.preview);
|
|
508
|
+
pendingPreviews.current.delete(gone.preview);
|
|
509
|
+
}
|
|
510
|
+
setFiles((prev) => prev.filter((f) => f.localId !== localId));
|
|
511
|
+
setNotice(null);
|
|
512
|
+
}
|
|
292
513
|
async function send() {
|
|
514
|
+
if (!canSend) return;
|
|
293
515
|
const text = message.trim();
|
|
294
|
-
|
|
516
|
+
const sent = ready.map((f) => ({
|
|
517
|
+
uploadId: f.uploadId,
|
|
518
|
+
name: f.name,
|
|
519
|
+
contentType: f.contentType,
|
|
520
|
+
...f.tag ? { tag: f.tag } : {}
|
|
521
|
+
}));
|
|
522
|
+
const override = onBeforeSend?.({ input: text, files: sent });
|
|
523
|
+
const input = (typeof override === "string" ? override : text) || void 0;
|
|
524
|
+
for (const f of files) {
|
|
525
|
+
if (!f.preview) continue;
|
|
526
|
+
pendingPreviews.current.delete(f.preview);
|
|
527
|
+
if (f.status === "ready" && f.uploadId) sentPreviews.current.set(f.uploadId, f.preview);
|
|
528
|
+
else revoke(f.preview);
|
|
529
|
+
}
|
|
295
530
|
sessionId.current ??= createSessionId();
|
|
296
531
|
setMessage("");
|
|
297
|
-
|
|
298
|
-
|
|
532
|
+
setFiles([]);
|
|
533
|
+
setNotice(null);
|
|
534
|
+
setTurns((prev) => [
|
|
535
|
+
...prev,
|
|
536
|
+
{ id: nextTurnId.current++, role: "user", text, ...sent.length ? { files: sent } : {} }
|
|
537
|
+
]);
|
|
538
|
+
const result = await run.start({
|
|
539
|
+
input,
|
|
540
|
+
uploadIds: sent.length ? sent.map((f) => f.uploadId) : void 0,
|
|
541
|
+
sessionId: sessionId.current
|
|
542
|
+
});
|
|
299
543
|
setTurns((prev) => [
|
|
300
544
|
...prev,
|
|
301
545
|
{
|
|
@@ -318,41 +562,91 @@ function AgentChat({ basePath, agentId, placeholder = "Send a message" }) {
|
|
|
318
562
|
void send();
|
|
319
563
|
}
|
|
320
564
|
}
|
|
565
|
+
function onPaste(event) {
|
|
566
|
+
const data = event.clipboardData;
|
|
567
|
+
const pasted = Array.from(data?.files ?? []);
|
|
568
|
+
if (pasted.length === 0) return;
|
|
569
|
+
if (data.getData("text/plain")) return;
|
|
570
|
+
event.preventDefault();
|
|
571
|
+
addFiles(pasted);
|
|
572
|
+
}
|
|
573
|
+
const dropHandlers = acceptsFiles ? {
|
|
574
|
+
onDragOver(event) {
|
|
575
|
+
if (!hasFiles(event.dataTransfer)) return;
|
|
576
|
+
event.preventDefault();
|
|
577
|
+
setDragging(true);
|
|
578
|
+
},
|
|
579
|
+
onDragLeave(event) {
|
|
580
|
+
if (!event.currentTarget.contains(event.relatedTarget)) setDragging(false);
|
|
581
|
+
},
|
|
582
|
+
onDrop(event) {
|
|
583
|
+
if (!hasFiles(event.dataTransfer)) return;
|
|
584
|
+
event.preventDefault();
|
|
585
|
+
setDragging(false);
|
|
586
|
+
addFiles(Array.from(event.dataTransfer.files));
|
|
587
|
+
}
|
|
588
|
+
} : {};
|
|
589
|
+
const buttons = attachButtons?.length ? attachButtons : [{ label: "Attach" }];
|
|
321
590
|
return /* @__PURE__ */ jsxs2("div", { className: "seclai-agent seclai-chat", children: [
|
|
322
591
|
/* @__PURE__ */ jsxs2("ul", { className: "seclai-turns", "aria-live": "polite", "aria-busy": running, children: [
|
|
323
|
-
turns.map((turn) => /* @__PURE__ */ jsx3(Turn, { turn }, turn.id)),
|
|
592
|
+
turns.map((turn) => /* @__PURE__ */ jsx3(Turn, { turn, previews: sentPreviews.current }, turn.id)),
|
|
324
593
|
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
594
|
] }),
|
|
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
|
-
|
|
595
|
+
/* @__PURE__ */ jsxs2(
|
|
596
|
+
"form",
|
|
597
|
+
{
|
|
598
|
+
className: dragging ? "seclai-form seclai-dropping" : "seclai-form",
|
|
599
|
+
onSubmit,
|
|
600
|
+
...dropHandlers,
|
|
601
|
+
children: [
|
|
602
|
+
files.length > 0 && /* @__PURE__ */ jsx3("ul", { className: "seclai-files", "aria-label": "Files to send", children: files.map((f) => /* @__PURE__ */ jsx3(
|
|
603
|
+
FileChip,
|
|
604
|
+
{
|
|
605
|
+
name: f.name,
|
|
606
|
+
tag: f.tag,
|
|
607
|
+
preview: f.preview,
|
|
608
|
+
status: f.status,
|
|
609
|
+
error: f.error,
|
|
610
|
+
onRemove: () => remove(f.localId)
|
|
611
|
+
},
|
|
612
|
+
f.localId
|
|
613
|
+
)) }),
|
|
614
|
+
/* @__PURE__ */ jsx3(
|
|
615
|
+
"textarea",
|
|
616
|
+
{
|
|
617
|
+
rows: 2,
|
|
618
|
+
"aria-label": "Message",
|
|
619
|
+
placeholder,
|
|
620
|
+
value: message,
|
|
621
|
+
onChange: (e) => setMessage(e.target.value),
|
|
622
|
+
onKeyDown,
|
|
623
|
+
onPaste: acceptsFiles ? onPaste : void 0
|
|
624
|
+
}
|
|
625
|
+
),
|
|
626
|
+
notice && /* @__PURE__ */ jsx3("p", { className: "seclai-error", role: "alert", children: notice }),
|
|
627
|
+
/* @__PURE__ */ jsxs2("div", { className: "seclai-actions", children: [
|
|
628
|
+
acceptsFiles && buttons.map((button) => /* @__PURE__ */ jsx3(
|
|
629
|
+
AttachControl,
|
|
630
|
+
{
|
|
631
|
+
label: button.label,
|
|
632
|
+
accept: acceptAttribute(limits.contentTypes),
|
|
633
|
+
multiple: room > 1,
|
|
634
|
+
disabled: room <= 0,
|
|
635
|
+
onFiles: (picked) => addFiles(picked, button.tag)
|
|
636
|
+
},
|
|
637
|
+
`${button.label}\0${button.tag ?? ""}`
|
|
638
|
+
)),
|
|
639
|
+
/* @__PURE__ */ jsx3("button", { type: "submit", className: "seclai-button seclai-button-primary", disabled: !canSend, children: "Send" }),
|
|
640
|
+
running && /* @__PURE__ */ jsx3("button", { type: "button", className: "seclai-button", onClick: run.cancel, children: "Stop" })
|
|
641
|
+
] })
|
|
642
|
+
]
|
|
643
|
+
}
|
|
644
|
+
)
|
|
351
645
|
] });
|
|
352
646
|
}
|
|
353
647
|
|
|
354
648
|
// src/AgentRunForm.tsx
|
|
355
|
-
import {
|
|
649
|
+
import { useMemo as useMemo3, useState as useState4 } from "react";
|
|
356
650
|
import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
357
651
|
function slotProblem(slot, count) {
|
|
358
652
|
const label = slot.label ?? slot.name;
|
|
@@ -364,24 +658,13 @@ function slotProblem(slot, count) {
|
|
|
364
658
|
return null;
|
|
365
659
|
}
|
|
366
660
|
function AgentRunForm({ basePath, agentId, inputLabel = "Input", onFinish }) {
|
|
367
|
-
const endpoints =
|
|
661
|
+
const endpoints = useMemo3(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
368
662
|
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, { signal: 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]);
|
|
663
|
+
const { slots, error: loadError } = useAcceptedAttachments(endpoints);
|
|
664
|
+
const [input, setInput] = useState4("");
|
|
665
|
+
const [files, setFiles] = useState4({});
|
|
666
|
+
const [uploading, setUploading] = useState4(false);
|
|
667
|
+
const [formError, setFormError] = useState4(null);
|
|
385
668
|
const selected = Object.values(files).flat();
|
|
386
669
|
const busy = uploading || run.status === "running";
|
|
387
670
|
async function submit(event) {
|
|
@@ -443,7 +726,7 @@ function AgentRunForm({ basePath, agentId, inputLabel = "Input", onFinish }) {
|
|
|
443
726
|
{
|
|
444
727
|
type: "file",
|
|
445
728
|
name: slot.name,
|
|
446
|
-
accept: slot.content_types
|
|
729
|
+
accept: acceptAttribute(slot.content_types),
|
|
447
730
|
multiple: slot.max == null || slot.max > 1,
|
|
448
731
|
onChange: (e) => setFiles((prev) => ({ ...prev, [slot.name]: Array.from(e.target.files ?? []) }))
|
|
449
732
|
}
|
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;
|