@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/README.md
CHANGED
|
@@ -33,6 +33,31 @@ export function App() {
|
|
|
33
33
|
- **`AgentChat`** sends each message as a run with a shared session id and streams the reply.
|
|
34
34
|
- **`SeclaiAgent`** renders one of the two based on `mode`.
|
|
35
35
|
|
|
36
|
+
### Files in `AgentChat`
|
|
37
|
+
|
|
38
|
+
When the agent accepts attachments, `AgentChat` shows an **Attach** button. Files can also be dropped on the composer or pasted into the message box. Each file uploads as soon as it's added and shows as a chip, with a thumbnail for images, and its upload id goes with the next message. Send stays disabled while a file is uploading. The files a user sent show in their turn. Agents that take no files get the text-only chat, with no attach button.
|
|
39
|
+
|
|
40
|
+
The chat applies the agent's attachment slots to each message as a whole: it accepts the content types of all the slots, and up to the sum of their `max` (no limit if any slot has none). A file that doesn't fit is refused with a message under the box. A file the browser reports no type for (such as HEIC on Windows, or `.md`) isn't refused for its type: the server decides. A run takes one flat list of upload ids, so the chat can't fill slots separately.
|
|
41
|
+
|
|
42
|
+
To tell files apart, add named buttons with tags, then use the tags in `onBeforeSend` to describe the files to the agent. Whatever it returns is sent as the run's input, and the transcript keeps what the user typed:
|
|
43
|
+
|
|
44
|
+
```tsx
|
|
45
|
+
<AgentChat
|
|
46
|
+
attachButtons={[
|
|
47
|
+
{ label: "+ Headshot", tag: "headshot" },
|
|
48
|
+
{ label: "+ Style reference", tag: "style" },
|
|
49
|
+
]}
|
|
50
|
+
onBeforeSend={({ input, files }) =>
|
|
51
|
+
[...files.map((f, i) => `Image ${i + 1} (${f.name}) is the ${f.tag ?? "image"}.`), input].join("\n")
|
|
52
|
+
}
|
|
53
|
+
/>
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
| Prop | |
|
|
57
|
+
|---|---|
|
|
58
|
+
| `attachButtons` | `{ label: string; tag?: string }[]`. Replaces the single Attach button. Dropped and pasted files have no tag |
|
|
59
|
+
| `onBeforeSend` | `({ input, files }) => string \| void`, where `files` are `{ uploadId, name, contentType, tag? }`. Return a string to send it in place of the typed text |
|
|
60
|
+
|
|
36
61
|
### `useAgentRun`
|
|
37
62
|
|
|
38
63
|
Build your own UI on the hook:
|
|
@@ -49,6 +74,40 @@ await start({ input: "Hello", uploadIds: ["..."], sessionId: "..." });
|
|
|
49
74
|
- `start` resolves with the final state and doesn't reject. Failures appear in `status` and `error`.
|
|
50
75
|
- `cancel` stops streaming, but the run carries on at Seclai.
|
|
51
76
|
|
|
77
|
+
### Building your own UI
|
|
78
|
+
|
|
79
|
+
To upload files and run the agent from your own UI, use the helpers the components are built on:
|
|
80
|
+
|
|
81
|
+
- **`createEndpoints({ basePath, agentId })`** returns the handler URLs (`AgentEndpoints`): `attachmentReferences`, `upload`, `run`, and `download(runId, attachmentId, name?)`. Pass it the same options as `useAgentRun`.
|
|
82
|
+
- **`uploadFile(endpoints, file, { signal }?)`** uploads one file and resolves with its `id`.
|
|
83
|
+
- **`fetchAttachmentReferences(endpoints, { signal }?)`** resolves with the files the agent accepts (`accepted_attachments`) and whether it needs any (`requires_uploads`).
|
|
84
|
+
|
|
85
|
+
On an error response, `uploadFile` and `fetchAttachmentReferences` reject with an `Error` whose message comes from the handler. On a network failure or an aborted `signal`, they reject as `fetch` does.
|
|
86
|
+
|
|
87
|
+
Upload the files first, then start the run with their ids:
|
|
88
|
+
|
|
89
|
+
```tsx
|
|
90
|
+
import { createEndpoints, uploadFile, useAgentRun } from "@seclai/react";
|
|
91
|
+
|
|
92
|
+
const endpoints = createEndpoints({ agentId: "my-agent" });
|
|
93
|
+
|
|
94
|
+
function Ask() {
|
|
95
|
+
const { start } = useAgentRun({ agentId: "my-agent" });
|
|
96
|
+
|
|
97
|
+
async function send(input: string, files: File[]) {
|
|
98
|
+
const uploads = await Promise.all(files.map((file) => uploadFile(endpoints, file)));
|
|
99
|
+
await start({ input, uploadIds: uploads.map((upload) => upload.id) });
|
|
100
|
+
}
|
|
101
|
+
// ...
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
A run's `attachments` already have a `url`. To show a file from a run you stored earlier, build the URL with `download` rather than by hand:
|
|
106
|
+
|
|
107
|
+
```tsx
|
|
108
|
+
<a href={endpoints.download(saved.runId, saved.attachmentId, saved.name)}>{saved.name}</a>
|
|
109
|
+
```
|
|
110
|
+
|
|
52
111
|
## Styling
|
|
53
112
|
|
|
54
113
|
The components are unstyled apart from the optional stylesheet, which uses Seclai's colours by default. It's built on CSS variables you can set on `:root` or any ancestor, along with `seclai-*` class names:
|
package/dist/index.cjs
CHANGED
|
@@ -27,30 +27,17 @@ __export(index_exports, {
|
|
|
27
27
|
MarkdownContent: () => MarkdownContent,
|
|
28
28
|
MediaPreview: () => MediaPreview,
|
|
29
29
|
SeclaiAgent: () => SeclaiAgent,
|
|
30
|
+
createEndpoints: () => createEndpoints,
|
|
31
|
+
fetchAttachmentReferences: () => fetchAttachmentReferences,
|
|
32
|
+
uploadFile: () => uploadFile,
|
|
30
33
|
useAgentRun: () => useAgentRun
|
|
31
34
|
});
|
|
32
35
|
module.exports = __toCommonJS(index_exports);
|
|
33
36
|
|
|
34
37
|
// src/AgentChat.tsx
|
|
35
|
-
var
|
|
36
|
-
|
|
37
|
-
// src/placeholders/MarkdownContent.tsx
|
|
38
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
39
|
-
function MarkdownContent({ content }) {
|
|
40
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "seclai-markdown", children: content });
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// src/placeholders/MediaPreview.tsx
|
|
44
|
-
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
45
|
-
function MediaPreview({ attachment }) {
|
|
46
|
-
const name = attachment.name ?? attachment.id;
|
|
47
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("figure", { className: "seclai-media", children: [
|
|
48
|
-
attachment.mime.startsWith("image/") && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("img", { src: attachment.url, alt: name }),
|
|
49
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("figcaption", { children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("a", { href: attachment.url, download: name, children: name }) })
|
|
50
|
-
] });
|
|
51
|
-
}
|
|
38
|
+
var import_react3 = require("react");
|
|
52
39
|
|
|
53
|
-
// src/
|
|
40
|
+
// src/attachments.ts
|
|
54
41
|
var import_react = require("react");
|
|
55
42
|
|
|
56
43
|
// src/endpoints.ts
|
|
@@ -84,15 +71,76 @@ async function readJson(res) {
|
|
|
84
71
|
if (!res.ok) throw new Error(await readErrorMessage(res));
|
|
85
72
|
return await res.json();
|
|
86
73
|
}
|
|
87
|
-
async function fetchAttachmentReferences(endpoints, signal) {
|
|
74
|
+
async function fetchAttachmentReferences(endpoints, { signal } = {}) {
|
|
88
75
|
return readJson(await fetch(endpoints.attachmentReferences, { signal }));
|
|
89
76
|
}
|
|
90
|
-
async function uploadFile(endpoints, file) {
|
|
77
|
+
async function uploadFile(endpoints, file, { signal } = {}) {
|
|
91
78
|
const form = new FormData();
|
|
92
79
|
form.set("file", file);
|
|
93
|
-
return readJson(await fetch(endpoints.upload, { method: "POST", body: form }));
|
|
80
|
+
return readJson(await fetch(endpoints.upload, { method: "POST", body: form, signal }));
|
|
94
81
|
}
|
|
95
82
|
|
|
83
|
+
// src/attachments.ts
|
|
84
|
+
function useAcceptedAttachments(endpoints) {
|
|
85
|
+
const [state, setState] = (0, import_react.useState)({ slots: null, error: null });
|
|
86
|
+
(0, import_react.useEffect)(() => {
|
|
87
|
+
const controller = new AbortController();
|
|
88
|
+
setState({ slots: null, error: null });
|
|
89
|
+
fetchAttachmentReferences(endpoints, { signal: controller.signal }).then((refs) => setState({ slots: refs.accepted_attachments ?? [], error: null })).catch((err) => {
|
|
90
|
+
if (controller.signal.aborted) return;
|
|
91
|
+
setState({
|
|
92
|
+
slots: null,
|
|
93
|
+
error: err instanceof Error ? err.message : "Could not load this agent."
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
return () => controller.abort();
|
|
97
|
+
}, [endpoints]);
|
|
98
|
+
return state;
|
|
99
|
+
}
|
|
100
|
+
function acceptAttribute(contentTypes) {
|
|
101
|
+
return contentTypes?.length ? contentTypes.join(",") : void 0;
|
|
102
|
+
}
|
|
103
|
+
function acceptsType(contentTypes, type) {
|
|
104
|
+
if (!contentTypes?.length || !type) return true;
|
|
105
|
+
const [major] = type.toLowerCase().split("/");
|
|
106
|
+
return contentTypes.some((pattern) => {
|
|
107
|
+
const p = pattern.trim().toLowerCase();
|
|
108
|
+
if (p === "*/*" || p === "*") return true;
|
|
109
|
+
if (p.endsWith("/*")) return major === p.slice(0, -2);
|
|
110
|
+
return p === type.toLowerCase();
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
function messageFileLimits(slots) {
|
|
114
|
+
let max = 0;
|
|
115
|
+
let anyType = false;
|
|
116
|
+
const types = /* @__PURE__ */ new Set();
|
|
117
|
+
for (const slot of slots) {
|
|
118
|
+
max = max == null || slot.max == null ? null : max + slot.max;
|
|
119
|
+
if (slot.content_types?.length) slot.content_types.forEach((t) => types.add(t));
|
|
120
|
+
else anyType = true;
|
|
121
|
+
}
|
|
122
|
+
return { max, contentTypes: anyType ? [] : [...types] };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// src/placeholders/MarkdownContent.tsx
|
|
126
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
127
|
+
function MarkdownContent({ content }) {
|
|
128
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "seclai-markdown", children: content });
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// src/placeholders/MediaPreview.tsx
|
|
132
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
133
|
+
function MediaPreview({ attachment }) {
|
|
134
|
+
const name = attachment.name ?? attachment.id;
|
|
135
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("figure", { className: "seclai-media", children: [
|
|
136
|
+
attachment.mime.startsWith("image/") && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("img", { src: attachment.url, alt: name }),
|
|
137
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("figcaption", { children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("a", { href: attachment.url, download: name, children: name }) })
|
|
138
|
+
] });
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// src/useAgentRun.ts
|
|
142
|
+
var import_react2 = require("react");
|
|
143
|
+
|
|
96
144
|
// src/sse.ts
|
|
97
145
|
function parseData(data) {
|
|
98
146
|
try {
|
|
@@ -236,11 +284,11 @@ function applyEvent(state, { event, data }, endpoints) {
|
|
|
236
284
|
}
|
|
237
285
|
function useAgentRun(options = {}) {
|
|
238
286
|
const { basePath, agentId } = options;
|
|
239
|
-
const endpoints = (0,
|
|
240
|
-
const [state, setState] = (0,
|
|
241
|
-
const active = (0,
|
|
242
|
-
(0,
|
|
243
|
-
const start = (0,
|
|
287
|
+
const endpoints = (0, import_react2.useMemo)(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
288
|
+
const [state, setState] = (0, import_react2.useState)(IDLE);
|
|
289
|
+
const active = (0, import_react2.useRef)(null);
|
|
290
|
+
(0, import_react2.useEffect)(() => () => active.current?.abort(), []);
|
|
291
|
+
const start = (0, import_react2.useCallback)(
|
|
244
292
|
async ({ input, uploadIds, sessionId }) => {
|
|
245
293
|
active.current?.abort();
|
|
246
294
|
const controller = new AbortController();
|
|
@@ -292,7 +340,7 @@ function useAgentRun(options = {}) {
|
|
|
292
340
|
},
|
|
293
341
|
[endpoints]
|
|
294
342
|
);
|
|
295
|
-
const cancel = (0,
|
|
343
|
+
const cancel = (0, import_react2.useCallback)(() => active.current?.abort(), []);
|
|
296
344
|
return { ...state, start, cancel };
|
|
297
345
|
}
|
|
298
346
|
|
|
@@ -305,28 +353,203 @@ function createSessionId() {
|
|
|
305
353
|
(byte) => byte.toString(16).padStart(2, "0")
|
|
306
354
|
).join("");
|
|
307
355
|
}
|
|
308
|
-
|
|
356
|
+
var nextLocalId = 0;
|
|
357
|
+
function createPreview(file) {
|
|
358
|
+
if (!file.type.startsWith("image/") || typeof URL.createObjectURL !== "function") return null;
|
|
359
|
+
return URL.createObjectURL(file);
|
|
360
|
+
}
|
|
361
|
+
function revoke(url) {
|
|
362
|
+
if (url && typeof URL.revokeObjectURL === "function") URL.revokeObjectURL(url);
|
|
363
|
+
}
|
|
364
|
+
function hasFiles(dataTransfer) {
|
|
365
|
+
return !!dataTransfer && Array.from(dataTransfer.types ?? []).includes("Files");
|
|
366
|
+
}
|
|
367
|
+
function plural(count, word) {
|
|
368
|
+
return `${count} ${word}${count === 1 ? "" : "s"}`;
|
|
369
|
+
}
|
|
370
|
+
function FileChip({ name, tag, preview, status, error, onRemove }) {
|
|
371
|
+
const meta = status === "uploading" ? "Uploading\u2026" : status === "error" ? error : tag;
|
|
372
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("li", { className: "seclai-file", "data-status": status, children: [
|
|
373
|
+
preview && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("img", { className: "seclai-file-thumb", src: preview, alt: "" }),
|
|
374
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("span", { className: "seclai-file-text", children: [
|
|
375
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "seclai-file-name", title: name, children: name }),
|
|
376
|
+
meta && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "seclai-file-meta", children: meta })
|
|
377
|
+
] }),
|
|
378
|
+
onRemove && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
379
|
+
"button",
|
|
380
|
+
{
|
|
381
|
+
type: "button",
|
|
382
|
+
className: "seclai-file-remove",
|
|
383
|
+
"aria-label": `Remove ${name}`,
|
|
384
|
+
onClick: onRemove,
|
|
385
|
+
children: "\xD7"
|
|
386
|
+
}
|
|
387
|
+
)
|
|
388
|
+
] });
|
|
389
|
+
}
|
|
390
|
+
function Turn({ turn, previews }) {
|
|
309
391
|
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("li", { className: `seclai-turn seclai-turn-${turn.role}`, children: [
|
|
392
|
+
turn.files && turn.files.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("ul", { className: "seclai-files", "aria-label": "Attached files", children: turn.files.map((file) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
393
|
+
FileChip,
|
|
394
|
+
{
|
|
395
|
+
name: file.name,
|
|
396
|
+
tag: file.tag,
|
|
397
|
+
preview: previews.get(file.uploadId)
|
|
398
|
+
},
|
|
399
|
+
file.uploadId
|
|
400
|
+
)) }),
|
|
310
401
|
turn.text && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(MarkdownContent, { content: turn.text }),
|
|
311
402
|
turn.attachments?.map((attachment) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(MediaPreview, { attachment }, attachment.id)),
|
|
312
403
|
turn.error && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "seclai-error", role: "alert", children: turn.error }),
|
|
313
404
|
turn.cancelled && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "seclai-notice", children: "Stopped." })
|
|
314
405
|
] });
|
|
315
406
|
}
|
|
316
|
-
function
|
|
407
|
+
function AttachControl({
|
|
408
|
+
label,
|
|
409
|
+
accept,
|
|
410
|
+
multiple,
|
|
411
|
+
disabled,
|
|
412
|
+
onFiles
|
|
413
|
+
}) {
|
|
414
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("label", { className: "seclai-button seclai-attach", "aria-disabled": disabled || void 0, children: [
|
|
415
|
+
label,
|
|
416
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
417
|
+
"input",
|
|
418
|
+
{
|
|
419
|
+
type: "file",
|
|
420
|
+
className: "seclai-sr-only",
|
|
421
|
+
accept,
|
|
422
|
+
multiple,
|
|
423
|
+
disabled,
|
|
424
|
+
onChange: (e) => {
|
|
425
|
+
const picked = Array.from(e.target.files ?? []);
|
|
426
|
+
e.target.value = "";
|
|
427
|
+
if (picked.length) onFiles(picked);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
)
|
|
431
|
+
] });
|
|
432
|
+
}
|
|
433
|
+
function AgentChat({
|
|
434
|
+
basePath,
|
|
435
|
+
agentId,
|
|
436
|
+
placeholder = "Send a message",
|
|
437
|
+
attachButtons,
|
|
438
|
+
onBeforeSend
|
|
439
|
+
}) {
|
|
440
|
+
const endpoints = (0, import_react3.useMemo)(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
317
441
|
const run = useAgentRun({ basePath, agentId });
|
|
318
|
-
const
|
|
319
|
-
const [
|
|
320
|
-
const
|
|
321
|
-
const
|
|
442
|
+
const { slots } = useAcceptedAttachments(endpoints);
|
|
443
|
+
const [turns, setTurns] = (0, import_react3.useState)([]);
|
|
444
|
+
const [message, setMessage] = (0, import_react3.useState)("");
|
|
445
|
+
const [files, setFiles] = (0, import_react3.useState)([]);
|
|
446
|
+
const [notice, setNotice] = (0, import_react3.useState)(null);
|
|
447
|
+
const [dragging, setDragging] = (0, import_react3.useState)(false);
|
|
448
|
+
const sessionId = (0, import_react3.useRef)(null);
|
|
449
|
+
const nextTurnId = (0, import_react3.useRef)(0);
|
|
450
|
+
const sentPreviews = (0, import_react3.useRef)(/* @__PURE__ */ new Map());
|
|
451
|
+
const pendingPreviews = (0, import_react3.useRef)(/* @__PURE__ */ new Set());
|
|
452
|
+
(0, import_react3.useEffect)(
|
|
453
|
+
() => () => {
|
|
454
|
+
pendingPreviews.current.forEach(revoke);
|
|
455
|
+
sentPreviews.current.forEach(revoke);
|
|
456
|
+
},
|
|
457
|
+
[]
|
|
458
|
+
);
|
|
459
|
+
const limits = slots?.length ? messageFileLimits(slots) : null;
|
|
460
|
+
const acceptsFiles = !!limits && (limits.max == null || limits.max > 0);
|
|
322
461
|
const running = run.status === "running";
|
|
462
|
+
const uploading = files.some((f) => f.status === "uploading");
|
|
463
|
+
const ready = files.filter((f) => f.status === "ready" && f.uploadId);
|
|
464
|
+
const room = limits?.max == null ? Infinity : limits.max - files.filter((f) => f.status !== "error").length;
|
|
465
|
+
const canSend = !running && !uploading && (!!message.trim() || ready.length > 0);
|
|
466
|
+
function setFile(localId, patch) {
|
|
467
|
+
setFiles((prev) => prev.map((f) => f.localId === localId ? { ...f, ...patch } : f));
|
|
468
|
+
}
|
|
469
|
+
function upload(item, file) {
|
|
470
|
+
uploadFile(endpoints, file).then(
|
|
471
|
+
(result) => setFile(item.localId, { status: "ready", uploadId: result.id }),
|
|
472
|
+
(err) => setFile(item.localId, {
|
|
473
|
+
status: "error",
|
|
474
|
+
error: err instanceof Error ? err.message : "The upload failed."
|
|
475
|
+
})
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
function addFiles(incoming, tag) {
|
|
479
|
+
if (!acceptsFiles || !limits || incoming.length === 0) return;
|
|
480
|
+
const problems = [];
|
|
481
|
+
const added = [];
|
|
482
|
+
let left = room;
|
|
483
|
+
let tooMany = false;
|
|
484
|
+
for (const file of incoming) {
|
|
485
|
+
if (!acceptsType(limits.contentTypes, file.type)) {
|
|
486
|
+
problems.push(`${file.name} isn't a type of file this agent accepts.`);
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
if (left <= 0) {
|
|
490
|
+
tooMany = true;
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
left--;
|
|
494
|
+
const preview = createPreview(file);
|
|
495
|
+
if (preview) pendingPreviews.current.add(preview);
|
|
496
|
+
added.push([
|
|
497
|
+
{
|
|
498
|
+
localId: `file-${++nextLocalId}`,
|
|
499
|
+
name: file.name,
|
|
500
|
+
contentType: file.type,
|
|
501
|
+
tag,
|
|
502
|
+
preview,
|
|
503
|
+
status: "uploading"
|
|
504
|
+
},
|
|
505
|
+
file
|
|
506
|
+
]);
|
|
507
|
+
}
|
|
508
|
+
if (tooMany) problems.push(`You can attach up to ${plural(limits.max, "file")} per message.`);
|
|
509
|
+
setNotice(problems.length ? problems.join(" ") : null);
|
|
510
|
+
if (added.length === 0) return;
|
|
511
|
+
setFiles((prev) => [...prev, ...added.map(([item]) => item)]);
|
|
512
|
+
for (const [item, file] of added) upload(item, file);
|
|
513
|
+
}
|
|
514
|
+
function remove(localId) {
|
|
515
|
+
const gone = files.find((f) => f.localId === localId);
|
|
516
|
+
if (gone?.preview) {
|
|
517
|
+
revoke(gone.preview);
|
|
518
|
+
pendingPreviews.current.delete(gone.preview);
|
|
519
|
+
}
|
|
520
|
+
setFiles((prev) => prev.filter((f) => f.localId !== localId));
|
|
521
|
+
setNotice(null);
|
|
522
|
+
}
|
|
323
523
|
async function send() {
|
|
524
|
+
if (!canSend) return;
|
|
324
525
|
const text = message.trim();
|
|
325
|
-
|
|
526
|
+
const sent = ready.map((f) => ({
|
|
527
|
+
uploadId: f.uploadId,
|
|
528
|
+
name: f.name,
|
|
529
|
+
contentType: f.contentType,
|
|
530
|
+
...f.tag ? { tag: f.tag } : {}
|
|
531
|
+
}));
|
|
532
|
+
const override = onBeforeSend?.({ input: text, files: sent });
|
|
533
|
+
const input = (typeof override === "string" ? override : text) || void 0;
|
|
534
|
+
for (const f of files) {
|
|
535
|
+
if (!f.preview) continue;
|
|
536
|
+
pendingPreviews.current.delete(f.preview);
|
|
537
|
+
if (f.status === "ready" && f.uploadId) sentPreviews.current.set(f.uploadId, f.preview);
|
|
538
|
+
else revoke(f.preview);
|
|
539
|
+
}
|
|
326
540
|
sessionId.current ??= createSessionId();
|
|
327
541
|
setMessage("");
|
|
328
|
-
|
|
329
|
-
|
|
542
|
+
setFiles([]);
|
|
543
|
+
setNotice(null);
|
|
544
|
+
setTurns((prev) => [
|
|
545
|
+
...prev,
|
|
546
|
+
{ id: nextTurnId.current++, role: "user", text, ...sent.length ? { files: sent } : {} }
|
|
547
|
+
]);
|
|
548
|
+
const result = await run.start({
|
|
549
|
+
input,
|
|
550
|
+
uploadIds: sent.length ? sent.map((f) => f.uploadId) : void 0,
|
|
551
|
+
sessionId: sessionId.current
|
|
552
|
+
});
|
|
330
553
|
setTurns((prev) => [
|
|
331
554
|
...prev,
|
|
332
555
|
{
|
|
@@ -349,41 +572,91 @@ function AgentChat({ basePath, agentId, placeholder = "Send a message" }) {
|
|
|
349
572
|
void send();
|
|
350
573
|
}
|
|
351
574
|
}
|
|
575
|
+
function onPaste(event) {
|
|
576
|
+
const data = event.clipboardData;
|
|
577
|
+
const pasted = Array.from(data?.files ?? []);
|
|
578
|
+
if (pasted.length === 0) return;
|
|
579
|
+
if (data.getData("text/plain")) return;
|
|
580
|
+
event.preventDefault();
|
|
581
|
+
addFiles(pasted);
|
|
582
|
+
}
|
|
583
|
+
const dropHandlers = acceptsFiles ? {
|
|
584
|
+
onDragOver(event) {
|
|
585
|
+
if (!hasFiles(event.dataTransfer)) return;
|
|
586
|
+
event.preventDefault();
|
|
587
|
+
setDragging(true);
|
|
588
|
+
},
|
|
589
|
+
onDragLeave(event) {
|
|
590
|
+
if (!event.currentTarget.contains(event.relatedTarget)) setDragging(false);
|
|
591
|
+
},
|
|
592
|
+
onDrop(event) {
|
|
593
|
+
if (!hasFiles(event.dataTransfer)) return;
|
|
594
|
+
event.preventDefault();
|
|
595
|
+
setDragging(false);
|
|
596
|
+
addFiles(Array.from(event.dataTransfer.files));
|
|
597
|
+
}
|
|
598
|
+
} : {};
|
|
599
|
+
const buttons = attachButtons?.length ? attachButtons : [{ label: "Attach" }];
|
|
352
600
|
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "seclai-agent seclai-chat", children: [
|
|
353
601
|
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("ul", { className: "seclai-turns", "aria-live": "polite", "aria-busy": running, children: [
|
|
354
|
-
turns.map((turn) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Turn, { turn }, turn.id)),
|
|
602
|
+
turns.map((turn) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Turn, { turn, previews: sentPreviews.current }, turn.id)),
|
|
355
603
|
running && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("li", { className: "seclai-turn seclai-turn-assistant", children: run.text ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(MarkdownContent, { content: run.text }) : /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "seclai-notice", children: "Thinking\u2026" }) })
|
|
356
604
|
] }),
|
|
357
|
-
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
605
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
606
|
+
"form",
|
|
607
|
+
{
|
|
608
|
+
className: dragging ? "seclai-form seclai-dropping" : "seclai-form",
|
|
609
|
+
onSubmit,
|
|
610
|
+
...dropHandlers,
|
|
611
|
+
children: [
|
|
612
|
+
files.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("ul", { className: "seclai-files", "aria-label": "Files to send", children: files.map((f) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
613
|
+
FileChip,
|
|
614
|
+
{
|
|
615
|
+
name: f.name,
|
|
616
|
+
tag: f.tag,
|
|
617
|
+
preview: f.preview,
|
|
618
|
+
status: f.status,
|
|
619
|
+
error: f.error,
|
|
620
|
+
onRemove: () => remove(f.localId)
|
|
621
|
+
},
|
|
622
|
+
f.localId
|
|
623
|
+
)) }),
|
|
624
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
625
|
+
"textarea",
|
|
626
|
+
{
|
|
627
|
+
rows: 2,
|
|
628
|
+
"aria-label": "Message",
|
|
629
|
+
placeholder,
|
|
630
|
+
value: message,
|
|
631
|
+
onChange: (e) => setMessage(e.target.value),
|
|
632
|
+
onKeyDown,
|
|
633
|
+
onPaste: acceptsFiles ? onPaste : void 0
|
|
634
|
+
}
|
|
635
|
+
),
|
|
636
|
+
notice && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "seclai-error", role: "alert", children: notice }),
|
|
637
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "seclai-actions", children: [
|
|
638
|
+
acceptsFiles && buttons.map((button) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
639
|
+
AttachControl,
|
|
640
|
+
{
|
|
641
|
+
label: button.label,
|
|
642
|
+
accept: acceptAttribute(limits.contentTypes),
|
|
643
|
+
multiple: room > 1,
|
|
644
|
+
disabled: room <= 0,
|
|
645
|
+
onFiles: (picked) => addFiles(picked, button.tag)
|
|
646
|
+
},
|
|
647
|
+
`${button.label}\0${button.tag ?? ""}`
|
|
648
|
+
)),
|
|
649
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "submit", className: "seclai-button seclai-button-primary", disabled: !canSend, children: "Send" }),
|
|
650
|
+
running && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: "seclai-button", onClick: run.cancel, children: "Stop" })
|
|
651
|
+
] })
|
|
652
|
+
]
|
|
653
|
+
}
|
|
654
|
+
)
|
|
382
655
|
] });
|
|
383
656
|
}
|
|
384
657
|
|
|
385
658
|
// src/AgentRunForm.tsx
|
|
386
|
-
var
|
|
659
|
+
var import_react4 = require("react");
|
|
387
660
|
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
388
661
|
function slotProblem(slot, count) {
|
|
389
662
|
const label = slot.label ?? slot.name;
|
|
@@ -395,24 +668,13 @@ function slotProblem(slot, count) {
|
|
|
395
668
|
return null;
|
|
396
669
|
}
|
|
397
670
|
function AgentRunForm({ basePath, agentId, inputLabel = "Input", onFinish }) {
|
|
398
|
-
const endpoints = (0,
|
|
671
|
+
const endpoints = (0, import_react4.useMemo)(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
399
672
|
const run = useAgentRun({ basePath, agentId });
|
|
400
|
-
const
|
|
401
|
-
const [
|
|
402
|
-
const [
|
|
403
|
-
const [
|
|
404
|
-
const [
|
|
405
|
-
const [formError, setFormError] = (0, import_react3.useState)(null);
|
|
406
|
-
(0, import_react3.useEffect)(() => {
|
|
407
|
-
const controller = new AbortController();
|
|
408
|
-
setSlots(null);
|
|
409
|
-
setLoadError(null);
|
|
410
|
-
fetchAttachmentReferences(endpoints, controller.signal).then((refs) => setSlots(refs.accepted_attachments ?? [])).catch((err) => {
|
|
411
|
-
if (controller.signal.aborted) return;
|
|
412
|
-
setLoadError(err instanceof Error ? err.message : "Could not load this agent.");
|
|
413
|
-
});
|
|
414
|
-
return () => controller.abort();
|
|
415
|
-
}, [endpoints]);
|
|
673
|
+
const { slots, error: loadError } = useAcceptedAttachments(endpoints);
|
|
674
|
+
const [input, setInput] = (0, import_react4.useState)("");
|
|
675
|
+
const [files, setFiles] = (0, import_react4.useState)({});
|
|
676
|
+
const [uploading, setUploading] = (0, import_react4.useState)(false);
|
|
677
|
+
const [formError, setFormError] = (0, import_react4.useState)(null);
|
|
416
678
|
const selected = Object.values(files).flat();
|
|
417
679
|
const busy = uploading || run.status === "running";
|
|
418
680
|
async function submit(event) {
|
|
@@ -474,7 +736,7 @@ function AgentRunForm({ basePath, agentId, inputLabel = "Input", onFinish }) {
|
|
|
474
736
|
{
|
|
475
737
|
type: "file",
|
|
476
738
|
name: slot.name,
|
|
477
|
-
accept: slot.content_types
|
|
739
|
+
accept: acceptAttribute(slot.content_types),
|
|
478
740
|
multiple: slot.max == null || slot.max > 1,
|
|
479
741
|
onChange: (e) => setFiles((prev) => ({ ...prev, [slot.name]: Array.from(e.target.files ?? []) }))
|
|
480
742
|
}
|
|
@@ -512,5 +774,8 @@ function SeclaiAgent({ mode, basePath, agentId }) {
|
|
|
512
774
|
MarkdownContent,
|
|
513
775
|
MediaPreview,
|
|
514
776
|
SeclaiAgent,
|
|
777
|
+
createEndpoints,
|
|
778
|
+
fetchAttachmentReferences,
|
|
779
|
+
uploadFile,
|
|
515
780
|
useAgentRun
|
|
516
781
|
});
|