@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/README.md
CHANGED
|
@@ -33,12 +33,39 @@ 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:
|
|
39
64
|
|
|
40
65
|
```tsx
|
|
41
|
-
const { status, text, steps, attachments, error, start, cancel } = useAgentRun(
|
|
66
|
+
const { status, text, steps, attachments, error, startedAt, start, cancel, reset } = useAgentRun({
|
|
67
|
+
onEvent: (event) => console.debug(event.event, event.data),
|
|
68
|
+
});
|
|
42
69
|
|
|
43
70
|
await start({ input: "Hello", uploadIds: ["..."], sessionId: "..." });
|
|
44
71
|
```
|
|
@@ -46,12 +73,39 @@ await start({ input: "Hello", uploadIds: ["..."], sessionId: "..." });
|
|
|
46
73
|
- `status` is `idle`, `running`, `completed`, `failed` or `cancelled`.
|
|
47
74
|
- `text` holds streamed tokens while running, then the run's final output.
|
|
48
75
|
- `attachments` have a `url` that goes through your download handler.
|
|
49
|
-
- `
|
|
76
|
+
- `startedAt` is when `start` began, from `Date.now()`. It's kept after the run ends and is `null` before the first run and after `reset`.
|
|
77
|
+
- `start` resolves with the final state and doesn't reject. Failures appear in `status` and `error`. A run stopped by `cancel()` or `reset()` resolves as `cancelled`.
|
|
50
78
|
- `cancel` stops streaming, but the run carries on at Seclai.
|
|
79
|
+
- `reset` returns to `idle` and clears `text`, `steps`, `attachments`, `error`, `runId` and `startedAt`. During a run it stops streaming as `cancel` does, so the run also carries on at Seclai.
|
|
80
|
+
- `onEvent` is called with each event from the run stream, `{ event, data }`, in order, as the hook updates its state. Events from a stopped run aren't passed on. The latest callback is always used, so an inline function is fine. If it throws, the error goes to `console.error` and the run carries on.
|
|
81
|
+
|
|
82
|
+
Options also take `basePath` and `agentId`, as the components do.
|
|
83
|
+
|
|
84
|
+
Runs that generate images can take a minute or two, so show the time elapsed:
|
|
85
|
+
|
|
86
|
+
```tsx
|
|
87
|
+
function Elapsed({ startedAt }: { startedAt: number }) {
|
|
88
|
+
const [now, setNow] = useState(Date.now());
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
const timer = setInterval(() => setNow(Date.now()), 1000);
|
|
91
|
+
return () => clearInterval(timer);
|
|
92
|
+
}, []);
|
|
93
|
+
const seconds = Math.max(0, Math.floor((now - startedAt) / 1000));
|
|
94
|
+
return <>{Math.floor(seconds / 60)}:{String(seconds % 60).padStart(2, "0")}</>;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const run = useAgentRun();
|
|
98
|
+
|
|
99
|
+
{run.status === "running" && run.startedAt !== null && (
|
|
100
|
+
<p>
|
|
101
|
+
Usually takes a minute or two · <Elapsed startedAt={run.startedAt} />
|
|
102
|
+
</p>
|
|
103
|
+
)}
|
|
104
|
+
```
|
|
51
105
|
|
|
52
106
|
### Building your own UI
|
|
53
107
|
|
|
54
|
-
|
|
108
|
+
To upload files and run the agent from your own UI, use the helpers the components are built on:
|
|
55
109
|
|
|
56
110
|
- **`createEndpoints({ basePath, agentId })`** returns the handler URLs (`AgentEndpoints`): `attachmentReferences`, `upload`, `run`, and `download(runId, attachmentId, name?)`. Pass it the same options as `useAgentRun`.
|
|
57
111
|
- **`uploadFile(endpoints, file, { signal }?)`** uploads one file and resolves with its `id`.
|
package/dist/index.cjs
CHANGED
|
@@ -35,25 +35,9 @@ __export(index_exports, {
|
|
|
35
35
|
module.exports = __toCommonJS(index_exports);
|
|
36
36
|
|
|
37
37
|
// src/AgentChat.tsx
|
|
38
|
-
var
|
|
39
|
-
|
|
40
|
-
// src/placeholders/MarkdownContent.tsx
|
|
41
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
42
|
-
function MarkdownContent({ content }) {
|
|
43
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "seclai-markdown", children: content });
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// src/placeholders/MediaPreview.tsx
|
|
47
|
-
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
48
|
-
function MediaPreview({ attachment }) {
|
|
49
|
-
const name = attachment.name ?? attachment.id;
|
|
50
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("figure", { className: "seclai-media", children: [
|
|
51
|
-
attachment.mime.startsWith("image/") && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("img", { src: attachment.url, alt: name }),
|
|
52
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("figcaption", { children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("a", { href: attachment.url, download: name, children: name }) })
|
|
53
|
-
] });
|
|
54
|
-
}
|
|
38
|
+
var import_react3 = require("react");
|
|
55
39
|
|
|
56
|
-
// src/
|
|
40
|
+
// src/attachments.ts
|
|
57
41
|
var import_react = require("react");
|
|
58
42
|
|
|
59
43
|
// src/endpoints.ts
|
|
@@ -96,6 +80,67 @@ async function uploadFile(endpoints, file, { signal } = {}) {
|
|
|
96
80
|
return readJson(await fetch(endpoints.upload, { method: "POST", body: form, signal }));
|
|
97
81
|
}
|
|
98
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
|
+
|
|
99
144
|
// src/sse.ts
|
|
100
145
|
function parseData(data) {
|
|
101
146
|
try {
|
|
@@ -160,7 +205,8 @@ var IDLE = {
|
|
|
160
205
|
steps: [],
|
|
161
206
|
attachments: [],
|
|
162
207
|
error: null,
|
|
163
|
-
runId: null
|
|
208
|
+
runId: null,
|
|
209
|
+
startedAt: null
|
|
164
210
|
};
|
|
165
211
|
function toAttachments(files, runId, endpoints) {
|
|
166
212
|
if (!files || !runId) return [];
|
|
@@ -238,17 +284,21 @@ function applyEvent(state, { event, data }, endpoints) {
|
|
|
238
284
|
}
|
|
239
285
|
}
|
|
240
286
|
function useAgentRun(options = {}) {
|
|
241
|
-
const { basePath, agentId } = options;
|
|
242
|
-
const endpoints = (0,
|
|
243
|
-
const [state, setState] = (0,
|
|
244
|
-
const active = (0,
|
|
245
|
-
(0,
|
|
246
|
-
|
|
287
|
+
const { basePath, agentId, onEvent } = options;
|
|
288
|
+
const endpoints = (0, import_react2.useMemo)(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
289
|
+
const [state, setState] = (0, import_react2.useState)(IDLE);
|
|
290
|
+
const active = (0, import_react2.useRef)(null);
|
|
291
|
+
const onEventRef = (0, import_react2.useRef)(onEvent);
|
|
292
|
+
(0, import_react2.useEffect)(() => {
|
|
293
|
+
onEventRef.current = onEvent;
|
|
294
|
+
});
|
|
295
|
+
(0, import_react2.useEffect)(() => () => active.current?.abort(), []);
|
|
296
|
+
const start = (0, import_react2.useCallback)(
|
|
247
297
|
async ({ input, uploadIds, sessionId }) => {
|
|
248
298
|
active.current?.abort();
|
|
249
299
|
const controller = new AbortController();
|
|
250
300
|
active.current = controller;
|
|
251
|
-
let current = { ...IDLE, status: "running" };
|
|
301
|
+
let current = { ...IDLE, status: "running", startedAt: Date.now() };
|
|
252
302
|
const update = (next) => {
|
|
253
303
|
current = next;
|
|
254
304
|
if (active.current === controller) setState(next);
|
|
@@ -270,10 +320,18 @@ function useAgentRun(options = {}) {
|
|
|
270
320
|
return current;
|
|
271
321
|
}
|
|
272
322
|
for await (const event of readSseStream(res.body)) {
|
|
323
|
+
if (controller.signal.aborted) break;
|
|
273
324
|
update(applyEvent(current, event, endpoints));
|
|
325
|
+
try {
|
|
326
|
+
onEventRef.current?.(event);
|
|
327
|
+
} catch (err) {
|
|
328
|
+
console.error("useAgentRun: onEvent threw", err);
|
|
329
|
+
}
|
|
274
330
|
if (current.status !== "running") break;
|
|
275
331
|
}
|
|
276
|
-
if (
|
|
332
|
+
if (controller.signal.aborted) {
|
|
333
|
+
if (current.status === "running") update({ ...current, status: "cancelled" });
|
|
334
|
+
} else if (current.status === "running") {
|
|
277
335
|
update({
|
|
278
336
|
...current,
|
|
279
337
|
status: "failed",
|
|
@@ -295,8 +353,14 @@ function useAgentRun(options = {}) {
|
|
|
295
353
|
},
|
|
296
354
|
[endpoints]
|
|
297
355
|
);
|
|
298
|
-
const cancel = (0,
|
|
299
|
-
|
|
356
|
+
const cancel = (0, import_react2.useCallback)(() => active.current?.abort(), []);
|
|
357
|
+
const reset = (0, import_react2.useCallback)(() => {
|
|
358
|
+
const controller = active.current;
|
|
359
|
+
active.current = null;
|
|
360
|
+
controller?.abort();
|
|
361
|
+
setState(IDLE);
|
|
362
|
+
}, []);
|
|
363
|
+
return { ...state, start, cancel, reset };
|
|
300
364
|
}
|
|
301
365
|
|
|
302
366
|
// src/AgentChat.tsx
|
|
@@ -308,28 +372,203 @@ function createSessionId() {
|
|
|
308
372
|
(byte) => byte.toString(16).padStart(2, "0")
|
|
309
373
|
).join("");
|
|
310
374
|
}
|
|
311
|
-
|
|
375
|
+
var nextLocalId = 0;
|
|
376
|
+
function createPreview(file) {
|
|
377
|
+
if (!file.type.startsWith("image/") || typeof URL.createObjectURL !== "function") return null;
|
|
378
|
+
return URL.createObjectURL(file);
|
|
379
|
+
}
|
|
380
|
+
function revoke(url) {
|
|
381
|
+
if (url && typeof URL.revokeObjectURL === "function") URL.revokeObjectURL(url);
|
|
382
|
+
}
|
|
383
|
+
function hasFiles(dataTransfer) {
|
|
384
|
+
return !!dataTransfer && Array.from(dataTransfer.types ?? []).includes("Files");
|
|
385
|
+
}
|
|
386
|
+
function plural(count, word) {
|
|
387
|
+
return `${count} ${word}${count === 1 ? "" : "s"}`;
|
|
388
|
+
}
|
|
389
|
+
function FileChip({ name, tag, preview, status, error, onRemove }) {
|
|
390
|
+
const meta = status === "uploading" ? "Uploading\u2026" : status === "error" ? error : tag;
|
|
391
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("li", { className: "seclai-file", "data-status": status, children: [
|
|
392
|
+
preview && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("img", { className: "seclai-file-thumb", src: preview, alt: "" }),
|
|
393
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("span", { className: "seclai-file-text", children: [
|
|
394
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "seclai-file-name", title: name, children: name }),
|
|
395
|
+
meta && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "seclai-file-meta", children: meta })
|
|
396
|
+
] }),
|
|
397
|
+
onRemove && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
398
|
+
"button",
|
|
399
|
+
{
|
|
400
|
+
type: "button",
|
|
401
|
+
className: "seclai-file-remove",
|
|
402
|
+
"aria-label": `Remove ${name}`,
|
|
403
|
+
onClick: onRemove,
|
|
404
|
+
children: "\xD7"
|
|
405
|
+
}
|
|
406
|
+
)
|
|
407
|
+
] });
|
|
408
|
+
}
|
|
409
|
+
function Turn({ turn, previews }) {
|
|
312
410
|
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("li", { className: `seclai-turn seclai-turn-${turn.role}`, children: [
|
|
411
|
+
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)(
|
|
412
|
+
FileChip,
|
|
413
|
+
{
|
|
414
|
+
name: file.name,
|
|
415
|
+
tag: file.tag,
|
|
416
|
+
preview: previews.get(file.uploadId)
|
|
417
|
+
},
|
|
418
|
+
file.uploadId
|
|
419
|
+
)) }),
|
|
313
420
|
turn.text && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(MarkdownContent, { content: turn.text }),
|
|
314
421
|
turn.attachments?.map((attachment) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(MediaPreview, { attachment }, attachment.id)),
|
|
315
422
|
turn.error && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "seclai-error", role: "alert", children: turn.error }),
|
|
316
423
|
turn.cancelled && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "seclai-notice", children: "Stopped." })
|
|
317
424
|
] });
|
|
318
425
|
}
|
|
319
|
-
function
|
|
426
|
+
function AttachControl({
|
|
427
|
+
label,
|
|
428
|
+
accept,
|
|
429
|
+
multiple,
|
|
430
|
+
disabled,
|
|
431
|
+
onFiles
|
|
432
|
+
}) {
|
|
433
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("label", { className: "seclai-button seclai-attach", "aria-disabled": disabled || void 0, children: [
|
|
434
|
+
label,
|
|
435
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
436
|
+
"input",
|
|
437
|
+
{
|
|
438
|
+
type: "file",
|
|
439
|
+
className: "seclai-sr-only",
|
|
440
|
+
accept,
|
|
441
|
+
multiple,
|
|
442
|
+
disabled,
|
|
443
|
+
onChange: (e) => {
|
|
444
|
+
const picked = Array.from(e.target.files ?? []);
|
|
445
|
+
e.target.value = "";
|
|
446
|
+
if (picked.length) onFiles(picked);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
)
|
|
450
|
+
] });
|
|
451
|
+
}
|
|
452
|
+
function AgentChat({
|
|
453
|
+
basePath,
|
|
454
|
+
agentId,
|
|
455
|
+
placeholder = "Send a message",
|
|
456
|
+
attachButtons,
|
|
457
|
+
onBeforeSend
|
|
458
|
+
}) {
|
|
459
|
+
const endpoints = (0, import_react3.useMemo)(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
320
460
|
const run = useAgentRun({ basePath, agentId });
|
|
321
|
-
const
|
|
322
|
-
const [
|
|
323
|
-
const
|
|
324
|
-
const
|
|
461
|
+
const { slots } = useAcceptedAttachments(endpoints);
|
|
462
|
+
const [turns, setTurns] = (0, import_react3.useState)([]);
|
|
463
|
+
const [message, setMessage] = (0, import_react3.useState)("");
|
|
464
|
+
const [files, setFiles] = (0, import_react3.useState)([]);
|
|
465
|
+
const [notice, setNotice] = (0, import_react3.useState)(null);
|
|
466
|
+
const [dragging, setDragging] = (0, import_react3.useState)(false);
|
|
467
|
+
const sessionId = (0, import_react3.useRef)(null);
|
|
468
|
+
const nextTurnId = (0, import_react3.useRef)(0);
|
|
469
|
+
const sentPreviews = (0, import_react3.useRef)(/* @__PURE__ */ new Map());
|
|
470
|
+
const pendingPreviews = (0, import_react3.useRef)(/* @__PURE__ */ new Set());
|
|
471
|
+
(0, import_react3.useEffect)(
|
|
472
|
+
() => () => {
|
|
473
|
+
pendingPreviews.current.forEach(revoke);
|
|
474
|
+
sentPreviews.current.forEach(revoke);
|
|
475
|
+
},
|
|
476
|
+
[]
|
|
477
|
+
);
|
|
478
|
+
const limits = slots?.length ? messageFileLimits(slots) : null;
|
|
479
|
+
const acceptsFiles = !!limits && (limits.max == null || limits.max > 0);
|
|
325
480
|
const running = run.status === "running";
|
|
481
|
+
const uploading = files.some((f) => f.status === "uploading");
|
|
482
|
+
const ready = files.filter((f) => f.status === "ready" && f.uploadId);
|
|
483
|
+
const room = limits?.max == null ? Infinity : limits.max - files.filter((f) => f.status !== "error").length;
|
|
484
|
+
const canSend = !running && !uploading && (!!message.trim() || ready.length > 0);
|
|
485
|
+
function setFile(localId, patch) {
|
|
486
|
+
setFiles((prev) => prev.map((f) => f.localId === localId ? { ...f, ...patch } : f));
|
|
487
|
+
}
|
|
488
|
+
function upload(item, file) {
|
|
489
|
+
uploadFile(endpoints, file).then(
|
|
490
|
+
(result) => setFile(item.localId, { status: "ready", uploadId: result.id }),
|
|
491
|
+
(err) => setFile(item.localId, {
|
|
492
|
+
status: "error",
|
|
493
|
+
error: err instanceof Error ? err.message : "The upload failed."
|
|
494
|
+
})
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
function addFiles(incoming, tag) {
|
|
498
|
+
if (!acceptsFiles || !limits || incoming.length === 0) return;
|
|
499
|
+
const problems = [];
|
|
500
|
+
const added = [];
|
|
501
|
+
let left = room;
|
|
502
|
+
let tooMany = false;
|
|
503
|
+
for (const file of incoming) {
|
|
504
|
+
if (!acceptsType(limits.contentTypes, file.type)) {
|
|
505
|
+
problems.push(`${file.name} isn't a type of file this agent accepts.`);
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
if (left <= 0) {
|
|
509
|
+
tooMany = true;
|
|
510
|
+
continue;
|
|
511
|
+
}
|
|
512
|
+
left--;
|
|
513
|
+
const preview = createPreview(file);
|
|
514
|
+
if (preview) pendingPreviews.current.add(preview);
|
|
515
|
+
added.push([
|
|
516
|
+
{
|
|
517
|
+
localId: `file-${++nextLocalId}`,
|
|
518
|
+
name: file.name,
|
|
519
|
+
contentType: file.type,
|
|
520
|
+
tag,
|
|
521
|
+
preview,
|
|
522
|
+
status: "uploading"
|
|
523
|
+
},
|
|
524
|
+
file
|
|
525
|
+
]);
|
|
526
|
+
}
|
|
527
|
+
if (tooMany) problems.push(`You can attach up to ${plural(limits.max, "file")} per message.`);
|
|
528
|
+
setNotice(problems.length ? problems.join(" ") : null);
|
|
529
|
+
if (added.length === 0) return;
|
|
530
|
+
setFiles((prev) => [...prev, ...added.map(([item]) => item)]);
|
|
531
|
+
for (const [item, file] of added) upload(item, file);
|
|
532
|
+
}
|
|
533
|
+
function remove(localId) {
|
|
534
|
+
const gone = files.find((f) => f.localId === localId);
|
|
535
|
+
if (gone?.preview) {
|
|
536
|
+
revoke(gone.preview);
|
|
537
|
+
pendingPreviews.current.delete(gone.preview);
|
|
538
|
+
}
|
|
539
|
+
setFiles((prev) => prev.filter((f) => f.localId !== localId));
|
|
540
|
+
setNotice(null);
|
|
541
|
+
}
|
|
326
542
|
async function send() {
|
|
543
|
+
if (!canSend) return;
|
|
327
544
|
const text = message.trim();
|
|
328
|
-
|
|
545
|
+
const sent = ready.map((f) => ({
|
|
546
|
+
uploadId: f.uploadId,
|
|
547
|
+
name: f.name,
|
|
548
|
+
contentType: f.contentType,
|
|
549
|
+
...f.tag ? { tag: f.tag } : {}
|
|
550
|
+
}));
|
|
551
|
+
const override = onBeforeSend?.({ input: text, files: sent });
|
|
552
|
+
const input = (typeof override === "string" ? override : text) || void 0;
|
|
553
|
+
for (const f of files) {
|
|
554
|
+
if (!f.preview) continue;
|
|
555
|
+
pendingPreviews.current.delete(f.preview);
|
|
556
|
+
if (f.status === "ready" && f.uploadId) sentPreviews.current.set(f.uploadId, f.preview);
|
|
557
|
+
else revoke(f.preview);
|
|
558
|
+
}
|
|
329
559
|
sessionId.current ??= createSessionId();
|
|
330
560
|
setMessage("");
|
|
331
|
-
|
|
332
|
-
|
|
561
|
+
setFiles([]);
|
|
562
|
+
setNotice(null);
|
|
563
|
+
setTurns((prev) => [
|
|
564
|
+
...prev,
|
|
565
|
+
{ id: nextTurnId.current++, role: "user", text, ...sent.length ? { files: sent } : {} }
|
|
566
|
+
]);
|
|
567
|
+
const result = await run.start({
|
|
568
|
+
input,
|
|
569
|
+
uploadIds: sent.length ? sent.map((f) => f.uploadId) : void 0,
|
|
570
|
+
sessionId: sessionId.current
|
|
571
|
+
});
|
|
333
572
|
setTurns((prev) => [
|
|
334
573
|
...prev,
|
|
335
574
|
{
|
|
@@ -352,41 +591,91 @@ function AgentChat({ basePath, agentId, placeholder = "Send a message" }) {
|
|
|
352
591
|
void send();
|
|
353
592
|
}
|
|
354
593
|
}
|
|
594
|
+
function onPaste(event) {
|
|
595
|
+
const data = event.clipboardData;
|
|
596
|
+
const pasted = Array.from(data?.files ?? []);
|
|
597
|
+
if (pasted.length === 0) return;
|
|
598
|
+
if (data.getData("text/plain")) return;
|
|
599
|
+
event.preventDefault();
|
|
600
|
+
addFiles(pasted);
|
|
601
|
+
}
|
|
602
|
+
const dropHandlers = acceptsFiles ? {
|
|
603
|
+
onDragOver(event) {
|
|
604
|
+
if (!hasFiles(event.dataTransfer)) return;
|
|
605
|
+
event.preventDefault();
|
|
606
|
+
setDragging(true);
|
|
607
|
+
},
|
|
608
|
+
onDragLeave(event) {
|
|
609
|
+
if (!event.currentTarget.contains(event.relatedTarget)) setDragging(false);
|
|
610
|
+
},
|
|
611
|
+
onDrop(event) {
|
|
612
|
+
if (!hasFiles(event.dataTransfer)) return;
|
|
613
|
+
event.preventDefault();
|
|
614
|
+
setDragging(false);
|
|
615
|
+
addFiles(Array.from(event.dataTransfer.files));
|
|
616
|
+
}
|
|
617
|
+
} : {};
|
|
618
|
+
const buttons = attachButtons?.length ? attachButtons : [{ label: "Attach" }];
|
|
355
619
|
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "seclai-agent seclai-chat", children: [
|
|
356
620
|
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("ul", { className: "seclai-turns", "aria-live": "polite", "aria-busy": running, children: [
|
|
357
|
-
turns.map((turn) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Turn, { turn }, turn.id)),
|
|
621
|
+
turns.map((turn) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Turn, { turn, previews: sentPreviews.current }, turn.id)),
|
|
358
622
|
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" }) })
|
|
359
623
|
] }),
|
|
360
|
-
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
624
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
625
|
+
"form",
|
|
626
|
+
{
|
|
627
|
+
className: dragging ? "seclai-form seclai-dropping" : "seclai-form",
|
|
628
|
+
onSubmit,
|
|
629
|
+
...dropHandlers,
|
|
630
|
+
children: [
|
|
631
|
+
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)(
|
|
632
|
+
FileChip,
|
|
633
|
+
{
|
|
634
|
+
name: f.name,
|
|
635
|
+
tag: f.tag,
|
|
636
|
+
preview: f.preview,
|
|
637
|
+
status: f.status,
|
|
638
|
+
error: f.error,
|
|
639
|
+
onRemove: () => remove(f.localId)
|
|
640
|
+
},
|
|
641
|
+
f.localId
|
|
642
|
+
)) }),
|
|
643
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
644
|
+
"textarea",
|
|
645
|
+
{
|
|
646
|
+
rows: 2,
|
|
647
|
+
"aria-label": "Message",
|
|
648
|
+
placeholder,
|
|
649
|
+
value: message,
|
|
650
|
+
onChange: (e) => setMessage(e.target.value),
|
|
651
|
+
onKeyDown,
|
|
652
|
+
onPaste: acceptsFiles ? onPaste : void 0
|
|
653
|
+
}
|
|
654
|
+
),
|
|
655
|
+
notice && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "seclai-error", role: "alert", children: notice }),
|
|
656
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "seclai-actions", children: [
|
|
657
|
+
acceptsFiles && buttons.map((button) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
658
|
+
AttachControl,
|
|
659
|
+
{
|
|
660
|
+
label: button.label,
|
|
661
|
+
accept: acceptAttribute(limits.contentTypes),
|
|
662
|
+
multiple: room > 1,
|
|
663
|
+
disabled: room <= 0,
|
|
664
|
+
onFiles: (picked) => addFiles(picked, button.tag)
|
|
665
|
+
},
|
|
666
|
+
`${button.label}\0${button.tag ?? ""}`
|
|
667
|
+
)),
|
|
668
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "submit", className: "seclai-button seclai-button-primary", disabled: !canSend, children: "Send" }),
|
|
669
|
+
running && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: "seclai-button", onClick: run.cancel, children: "Stop" })
|
|
670
|
+
] })
|
|
671
|
+
]
|
|
672
|
+
}
|
|
673
|
+
)
|
|
385
674
|
] });
|
|
386
675
|
}
|
|
387
676
|
|
|
388
677
|
// src/AgentRunForm.tsx
|
|
389
|
-
var
|
|
678
|
+
var import_react4 = require("react");
|
|
390
679
|
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
391
680
|
function slotProblem(slot, count) {
|
|
392
681
|
const label = slot.label ?? slot.name;
|
|
@@ -398,24 +687,13 @@ function slotProblem(slot, count) {
|
|
|
398
687
|
return null;
|
|
399
688
|
}
|
|
400
689
|
function AgentRunForm({ basePath, agentId, inputLabel = "Input", onFinish }) {
|
|
401
|
-
const endpoints = (0,
|
|
690
|
+
const endpoints = (0, import_react4.useMemo)(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
402
691
|
const run = useAgentRun({ basePath, agentId });
|
|
403
|
-
const
|
|
404
|
-
const [
|
|
405
|
-
const [
|
|
406
|
-
const [
|
|
407
|
-
const [
|
|
408
|
-
const [formError, setFormError] = (0, import_react3.useState)(null);
|
|
409
|
-
(0, import_react3.useEffect)(() => {
|
|
410
|
-
const controller = new AbortController();
|
|
411
|
-
setSlots(null);
|
|
412
|
-
setLoadError(null);
|
|
413
|
-
fetchAttachmentReferences(endpoints, { signal: controller.signal }).then((refs) => setSlots(refs.accepted_attachments ?? [])).catch((err) => {
|
|
414
|
-
if (controller.signal.aborted) return;
|
|
415
|
-
setLoadError(err instanceof Error ? err.message : "Could not load this agent.");
|
|
416
|
-
});
|
|
417
|
-
return () => controller.abort();
|
|
418
|
-
}, [endpoints]);
|
|
692
|
+
const { slots, error: loadError } = useAcceptedAttachments(endpoints);
|
|
693
|
+
const [input, setInput] = (0, import_react4.useState)("");
|
|
694
|
+
const [files, setFiles] = (0, import_react4.useState)({});
|
|
695
|
+
const [uploading, setUploading] = (0, import_react4.useState)(false);
|
|
696
|
+
const [formError, setFormError] = (0, import_react4.useState)(null);
|
|
419
697
|
const selected = Object.values(files).flat();
|
|
420
698
|
const busy = uploading || run.status === "running";
|
|
421
699
|
async function submit(event) {
|
|
@@ -477,7 +755,7 @@ function AgentRunForm({ basePath, agentId, inputLabel = "Input", onFinish }) {
|
|
|
477
755
|
{
|
|
478
756
|
type: "file",
|
|
479
757
|
name: slot.name,
|
|
480
|
-
accept: slot.content_types
|
|
758
|
+
accept: acceptAttribute(slot.content_types),
|
|
481
759
|
multiple: slot.max == null || slot.max > 1,
|
|
482
760
|
onChange: (e) => setFiles((prev) => ({ ...prev, [slot.name]: Array.from(e.target.files ?? []) }))
|
|
483
761
|
}
|