@seclai/react 0.0.0-stage → 0.0.1
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/LICENSE +21 -0
- package/README.md +79 -2
- package/dist/index.cjs +516 -0
- package/dist/index.d.cts +140 -0
- package/dist/index.d.ts +140 -0
- package/dist/index.js +484 -0
- package/dist/styles.css +321 -0
- package/package.json +58 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Seclai
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,80 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @seclai/react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React components and hooks for running [Seclai](https://seclai.com) agents. They talk to [`@seclai/handlers`](https://www.npmjs.com/package/@seclai/handlers) on your server, never to the Seclai API directly. Works in React 18+, Next.js and Astro islands.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @seclai/react
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`react` and `react-dom` 18 or later are peer dependencies. Mount the handlers on your server first: see the [`@seclai/handlers` README](https://www.npmjs.com/package/@seclai/handlers#usage).
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { SeclaiAgent } from "@seclai/react";
|
|
17
|
+
import "@seclai/react/styles.css"; // optional
|
|
18
|
+
|
|
19
|
+
export function App() {
|
|
20
|
+
return <SeclaiAgent mode="form" />;
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
| Prop | Default | |
|
|
25
|
+
|---|---|---|
|
|
26
|
+
| `mode` | Required | `"form"` for a one-shot run, `"chat"` for a conversation |
|
|
27
|
+
| `basePath` | `/api/seclai` | Where the handlers are mounted |
|
|
28
|
+
| `agentId` | | For a server hosting several agents: requests go to `${basePath}/${agentId}/run`. The server still decides which agents it serves |
|
|
29
|
+
|
|
30
|
+
### Components
|
|
31
|
+
|
|
32
|
+
- **`AgentRunForm`** builds a text input and file inputs from the agent's accepted attachments, uploads the files, then runs with text and files together.
|
|
33
|
+
- **`AgentChat`** sends each message as a run with a shared session id and streams the reply.
|
|
34
|
+
- **`SeclaiAgent`** renders one of the two based on `mode`.
|
|
35
|
+
|
|
36
|
+
### `useAgentRun`
|
|
37
|
+
|
|
38
|
+
Build your own UI on the hook:
|
|
39
|
+
|
|
40
|
+
```tsx
|
|
41
|
+
const { status, text, steps, attachments, error, start, cancel } = useAgentRun();
|
|
42
|
+
|
|
43
|
+
await start({ input: "Hello", uploadIds: ["..."], sessionId: "..." });
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
- `status` is `idle`, `running`, `completed`, `failed` or `cancelled`.
|
|
47
|
+
- `text` holds streamed tokens while running, then the run's final output.
|
|
48
|
+
- `attachments` have a `url` that goes through your download handler.
|
|
49
|
+
- `start` resolves with the final state and doesn't reject. Failures appear in `status` and `error`.
|
|
50
|
+
- `cancel` stops streaming, but the run carries on at Seclai.
|
|
51
|
+
|
|
52
|
+
## Styling
|
|
53
|
+
|
|
54
|
+
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:
|
|
55
|
+
|
|
56
|
+
```css
|
|
57
|
+
:root {
|
|
58
|
+
--seclai-accent: #0f766e;
|
|
59
|
+
--seclai-accent-hover: #115e59;
|
|
60
|
+
--seclai-on-accent: #ffffff;
|
|
61
|
+
--seclai-radius: 4px;
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Fonts and text colour come from your page. The default colours follow your page's `color-scheme`, using CSS `light-dark()`, so they need a browser from 2024 or later. Setting a variable to a plain colour works in any browser that supports `color-mix()`.
|
|
66
|
+
|
|
67
|
+
**On a dark page, declare `color-scheme: dark`** (or `light dark`). Without it the defaults stay in their light versions, and the purple links and focus rings are too dark to read.
|
|
68
|
+
|
|
69
|
+
## Placeholders
|
|
70
|
+
|
|
71
|
+
These are exported so the API is in place, but they're minimal until they're ported from the main Seclai app:
|
|
72
|
+
|
|
73
|
+
| Export | For now | Coming |
|
|
74
|
+
|---|---|---|
|
|
75
|
+
| `MarkdownContent` | Plain text | GFM tables, KaTeX, mermaid, sanitized SVG |
|
|
76
|
+
| `MediaPreview` | Images inline, a download link for everything else | Image, audio, video, PDF, download fallback |
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
AgentChat: () => AgentChat,
|
|
25
|
+
AgentRunForm: () => AgentRunForm,
|
|
26
|
+
DEFAULT_BASE_PATH: () => DEFAULT_BASE_PATH,
|
|
27
|
+
MarkdownContent: () => MarkdownContent,
|
|
28
|
+
MediaPreview: () => MediaPreview,
|
|
29
|
+
SeclaiAgent: () => SeclaiAgent,
|
|
30
|
+
useAgentRun: () => useAgentRun
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(index_exports);
|
|
33
|
+
|
|
34
|
+
// src/AgentChat.tsx
|
|
35
|
+
var import_react2 = require("react");
|
|
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
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/useAgentRun.ts
|
|
54
|
+
var import_react = require("react");
|
|
55
|
+
|
|
56
|
+
// src/endpoints.ts
|
|
57
|
+
var DEFAULT_BASE_PATH = "/api/seclai";
|
|
58
|
+
function createEndpoints({
|
|
59
|
+
basePath = DEFAULT_BASE_PATH,
|
|
60
|
+
agentId
|
|
61
|
+
} = {}) {
|
|
62
|
+
const trimmed = basePath.replace(/\/+$/, "");
|
|
63
|
+
const base = agentId ? `${trimmed}/${encodeURIComponent(agentId)}` : trimmed;
|
|
64
|
+
return {
|
|
65
|
+
attachmentReferences: `${base}/attachment-references`,
|
|
66
|
+
upload: `${base}/upload`,
|
|
67
|
+
run: `${base}/run`,
|
|
68
|
+
download(runId, attachmentId, name) {
|
|
69
|
+
const params = new URLSearchParams({ runId, attachmentId });
|
|
70
|
+
if (name) params.set("name", name);
|
|
71
|
+
return `${base}/download?${params}`;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
async function readErrorMessage(res) {
|
|
76
|
+
try {
|
|
77
|
+
const body = await res.json();
|
|
78
|
+
if (typeof body.error === "string" && body.error) return body.error;
|
|
79
|
+
} catch {
|
|
80
|
+
}
|
|
81
|
+
return `The request failed with status ${res.status}.`;
|
|
82
|
+
}
|
|
83
|
+
async function readJson(res) {
|
|
84
|
+
if (!res.ok) throw new Error(await readErrorMessage(res));
|
|
85
|
+
return await res.json();
|
|
86
|
+
}
|
|
87
|
+
async function fetchAttachmentReferences(endpoints, signal) {
|
|
88
|
+
return readJson(await fetch(endpoints.attachmentReferences, { signal }));
|
|
89
|
+
}
|
|
90
|
+
async function uploadFile(endpoints, file) {
|
|
91
|
+
const form = new FormData();
|
|
92
|
+
form.set("file", file);
|
|
93
|
+
return readJson(await fetch(endpoints.upload, { method: "POST", body: form }));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// src/sse.ts
|
|
97
|
+
function parseData(data) {
|
|
98
|
+
try {
|
|
99
|
+
return JSON.parse(data);
|
|
100
|
+
} catch {
|
|
101
|
+
return data;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
async function* readSseStream(stream) {
|
|
105
|
+
const reader = stream.getReader();
|
|
106
|
+
const decoder = new TextDecoder();
|
|
107
|
+
let buffer = "";
|
|
108
|
+
let event = "message";
|
|
109
|
+
let dataLines = [];
|
|
110
|
+
function takeEvent() {
|
|
111
|
+
const parsed = dataLines.length ? { event, data: parseData(dataLines.join("\n")) } : void 0;
|
|
112
|
+
event = "message";
|
|
113
|
+
dataLines = [];
|
|
114
|
+
return parsed;
|
|
115
|
+
}
|
|
116
|
+
function* drain(final) {
|
|
117
|
+
while (true) {
|
|
118
|
+
const match = /\r\n|\n|\r/.exec(buffer);
|
|
119
|
+
if (!match) return;
|
|
120
|
+
if (match[0] === "\r" && match.index === buffer.length - 1 && !final) return;
|
|
121
|
+
const line = buffer.slice(0, match.index);
|
|
122
|
+
buffer = buffer.slice(match.index + match[0].length);
|
|
123
|
+
if (line === "") {
|
|
124
|
+
const parsed = takeEvent();
|
|
125
|
+
if (parsed) yield parsed;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (line.startsWith(":")) continue;
|
|
129
|
+
const colon = line.indexOf(":");
|
|
130
|
+
const field = colon === -1 ? line : line.slice(0, colon);
|
|
131
|
+
let value = colon === -1 ? "" : line.slice(colon + 1);
|
|
132
|
+
if (value.startsWith(" ")) value = value.slice(1);
|
|
133
|
+
if (field === "event") event = value;
|
|
134
|
+
else if (field === "data") dataLines.push(value);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
while (true) {
|
|
139
|
+
const { value, done } = await reader.read();
|
|
140
|
+
if (done) break;
|
|
141
|
+
buffer += decoder.decode(value, { stream: true });
|
|
142
|
+
yield* drain(false);
|
|
143
|
+
}
|
|
144
|
+
buffer += decoder.decode();
|
|
145
|
+
buffer += "\n\n";
|
|
146
|
+
yield* drain(true);
|
|
147
|
+
} finally {
|
|
148
|
+
void reader.cancel().catch(() => {
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// src/useAgentRun.ts
|
|
154
|
+
var IDLE = {
|
|
155
|
+
status: "idle",
|
|
156
|
+
text: "",
|
|
157
|
+
steps: [],
|
|
158
|
+
attachments: [],
|
|
159
|
+
error: null,
|
|
160
|
+
runId: null
|
|
161
|
+
};
|
|
162
|
+
function toAttachments(files, runId, endpoints) {
|
|
163
|
+
if (!files || !runId) return [];
|
|
164
|
+
return files.map(({ id, name, mime, bytes }) => ({
|
|
165
|
+
id,
|
|
166
|
+
name,
|
|
167
|
+
mime,
|
|
168
|
+
bytes,
|
|
169
|
+
url: endpoints.download(runId, id, name)
|
|
170
|
+
}));
|
|
171
|
+
}
|
|
172
|
+
function lastAttemptError(run) {
|
|
173
|
+
for (const attempt of [...run.attempts ?? []].reverse()) {
|
|
174
|
+
if (attempt.error) return attempt.error;
|
|
175
|
+
}
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
function applyEvent(state, { event, data }, endpoints) {
|
|
179
|
+
const payload = typeof data === "object" && data !== null ? data : {};
|
|
180
|
+
switch (event) {
|
|
181
|
+
case "init":
|
|
182
|
+
return { ...state, runId: typeof payload.run_id === "string" ? payload.run_id : state.runId };
|
|
183
|
+
case "stream_token":
|
|
184
|
+
return typeof payload.token === "string" ? { ...state, text: state.text + payload.token } : state;
|
|
185
|
+
case "step_progress": {
|
|
186
|
+
if (typeof payload.step_id !== "string") return state;
|
|
187
|
+
const step = {
|
|
188
|
+
id: payload.step_id,
|
|
189
|
+
name: typeof payload.step_name === "string" ? payload.step_name : payload.step_id,
|
|
190
|
+
message: typeof payload.message === "string" ? payload.message : ""
|
|
191
|
+
};
|
|
192
|
+
const known = state.steps.some((s) => s.id === step.id);
|
|
193
|
+
return {
|
|
194
|
+
...state,
|
|
195
|
+
steps: known ? state.steps.map((s) => s.id === step.id ? step : s) : [...state.steps, step]
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
case "done": {
|
|
199
|
+
const run = payload;
|
|
200
|
+
const runId = run.run_id ?? state.runId ?? void 0;
|
|
201
|
+
const attachments = toAttachments(run.attachments, runId, endpoints);
|
|
202
|
+
if (run.status === "failed") {
|
|
203
|
+
return {
|
|
204
|
+
...state,
|
|
205
|
+
status: "failed",
|
|
206
|
+
runId: runId ?? null,
|
|
207
|
+
attachments,
|
|
208
|
+
error: lastAttemptError(run) ?? "The run failed."
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
return {
|
|
212
|
+
...state,
|
|
213
|
+
status: "completed",
|
|
214
|
+
runId: runId ?? null,
|
|
215
|
+
attachments,
|
|
216
|
+
// `output` wins over the tokens, which are only a progress channel. Agents
|
|
217
|
+
// with no streaming step send no tokens, so this is their only text.
|
|
218
|
+
text: run.output || state.text
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
case "error":
|
|
222
|
+
return {
|
|
223
|
+
...state,
|
|
224
|
+
status: "failed",
|
|
225
|
+
error: typeof payload.error === "string" ? payload.error : "The run failed."
|
|
226
|
+
};
|
|
227
|
+
case "timeout":
|
|
228
|
+
return {
|
|
229
|
+
...state,
|
|
230
|
+
status: "failed",
|
|
231
|
+
error: "Stopped waiting for the run. It may still finish on Seclai."
|
|
232
|
+
};
|
|
233
|
+
default:
|
|
234
|
+
return state;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function useAgentRun(options = {}) {
|
|
238
|
+
const { basePath, agentId } = options;
|
|
239
|
+
const endpoints = (0, import_react.useMemo)(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
240
|
+
const [state, setState] = (0, import_react.useState)(IDLE);
|
|
241
|
+
const active = (0, import_react.useRef)(null);
|
|
242
|
+
(0, import_react.useEffect)(() => () => active.current?.abort(), []);
|
|
243
|
+
const start = (0, import_react.useCallback)(
|
|
244
|
+
async ({ input, uploadIds, sessionId }) => {
|
|
245
|
+
active.current?.abort();
|
|
246
|
+
const controller = new AbortController();
|
|
247
|
+
active.current = controller;
|
|
248
|
+
let current = { ...IDLE, status: "running" };
|
|
249
|
+
const update = (next) => {
|
|
250
|
+
current = next;
|
|
251
|
+
if (active.current === controller) setState(next);
|
|
252
|
+
};
|
|
253
|
+
update(current);
|
|
254
|
+
try {
|
|
255
|
+
const res = await fetch(endpoints.run, {
|
|
256
|
+
method: "POST",
|
|
257
|
+
headers: { "content-type": "application/json" },
|
|
258
|
+
body: JSON.stringify({ input, uploadIds, sessionId }),
|
|
259
|
+
signal: controller.signal
|
|
260
|
+
});
|
|
261
|
+
if (!res.ok) {
|
|
262
|
+
update({ ...current, status: "failed", error: await readErrorMessage(res) });
|
|
263
|
+
return current;
|
|
264
|
+
}
|
|
265
|
+
if (!res.body) {
|
|
266
|
+
update({ ...current, status: "failed", error: "The response had no body to stream." });
|
|
267
|
+
return current;
|
|
268
|
+
}
|
|
269
|
+
for await (const event of readSseStream(res.body)) {
|
|
270
|
+
update(applyEvent(current, event, endpoints));
|
|
271
|
+
if (current.status !== "running") break;
|
|
272
|
+
}
|
|
273
|
+
if (current.status === "running") {
|
|
274
|
+
update({
|
|
275
|
+
...current,
|
|
276
|
+
status: "failed",
|
|
277
|
+
error: "The connection closed before the run finished."
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
} catch (err) {
|
|
281
|
+
if (controller.signal.aborted) {
|
|
282
|
+
update({ ...current, status: "cancelled" });
|
|
283
|
+
} else {
|
|
284
|
+
update({
|
|
285
|
+
...current,
|
|
286
|
+
status: "failed",
|
|
287
|
+
error: err instanceof Error ? err.message : "The run failed."
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return current;
|
|
292
|
+
},
|
|
293
|
+
[endpoints]
|
|
294
|
+
);
|
|
295
|
+
const cancel = (0, import_react.useCallback)(() => active.current?.abort(), []);
|
|
296
|
+
return { ...state, start, cancel };
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// src/AgentChat.tsx
|
|
300
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
301
|
+
function createSessionId() {
|
|
302
|
+
if (typeof crypto.randomUUID === "function") return crypto.randomUUID();
|
|
303
|
+
return Array.from(
|
|
304
|
+
crypto.getRandomValues(new Uint8Array(16)),
|
|
305
|
+
(byte) => byte.toString(16).padStart(2, "0")
|
|
306
|
+
).join("");
|
|
307
|
+
}
|
|
308
|
+
function Turn({ turn }) {
|
|
309
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("li", { className: `seclai-turn seclai-turn-${turn.role}`, children: [
|
|
310
|
+
turn.text && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(MarkdownContent, { content: turn.text }),
|
|
311
|
+
turn.attachments?.map((attachment) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(MediaPreview, { attachment }, attachment.id)),
|
|
312
|
+
turn.error && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "seclai-error", role: "alert", children: turn.error }),
|
|
313
|
+
turn.cancelled && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "seclai-notice", children: "Stopped." })
|
|
314
|
+
] });
|
|
315
|
+
}
|
|
316
|
+
function AgentChat({ basePath, agentId, placeholder = "Send a message" }) {
|
|
317
|
+
const run = useAgentRun({ basePath, agentId });
|
|
318
|
+
const [turns, setTurns] = (0, import_react2.useState)([]);
|
|
319
|
+
const [message, setMessage] = (0, import_react2.useState)("");
|
|
320
|
+
const sessionId = (0, import_react2.useRef)(null);
|
|
321
|
+
const nextTurnId = (0, import_react2.useRef)(0);
|
|
322
|
+
const running = run.status === "running";
|
|
323
|
+
async function send() {
|
|
324
|
+
const text = message.trim();
|
|
325
|
+
if (!text || running) return;
|
|
326
|
+
sessionId.current ??= createSessionId();
|
|
327
|
+
setMessage("");
|
|
328
|
+
setTurns((prev) => [...prev, { id: nextTurnId.current++, role: "user", text }]);
|
|
329
|
+
const result = await run.start({ input: text, sessionId: sessionId.current });
|
|
330
|
+
setTurns((prev) => [
|
|
331
|
+
...prev,
|
|
332
|
+
{
|
|
333
|
+
id: nextTurnId.current++,
|
|
334
|
+
role: "assistant",
|
|
335
|
+
text: result.text,
|
|
336
|
+
attachments: result.attachments,
|
|
337
|
+
error: result.error,
|
|
338
|
+
cancelled: result.status === "cancelled"
|
|
339
|
+
}
|
|
340
|
+
]);
|
|
341
|
+
}
|
|
342
|
+
function onSubmit(event) {
|
|
343
|
+
event.preventDefault();
|
|
344
|
+
void send();
|
|
345
|
+
}
|
|
346
|
+
function onKeyDown(event) {
|
|
347
|
+
if (event.key === "Enter" && !event.shiftKey && !event.nativeEvent.isComposing) {
|
|
348
|
+
event.preventDefault();
|
|
349
|
+
void send();
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "seclai-agent seclai-chat", children: [
|
|
353
|
+
/* @__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)),
|
|
355
|
+
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
|
+
] }),
|
|
357
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("form", { className: "seclai-form", onSubmit, children: [
|
|
358
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
359
|
+
"textarea",
|
|
360
|
+
{
|
|
361
|
+
rows: 2,
|
|
362
|
+
"aria-label": "Message",
|
|
363
|
+
placeholder,
|
|
364
|
+
value: message,
|
|
365
|
+
onChange: (e) => setMessage(e.target.value),
|
|
366
|
+
onKeyDown
|
|
367
|
+
}
|
|
368
|
+
),
|
|
369
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "seclai-actions", children: [
|
|
370
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
371
|
+
"button",
|
|
372
|
+
{
|
|
373
|
+
type: "submit",
|
|
374
|
+
className: "seclai-button seclai-button-primary",
|
|
375
|
+
disabled: running || !message.trim(),
|
|
376
|
+
children: "Send"
|
|
377
|
+
}
|
|
378
|
+
),
|
|
379
|
+
running && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { type: "button", className: "seclai-button", onClick: run.cancel, children: "Stop" })
|
|
380
|
+
] })
|
|
381
|
+
] })
|
|
382
|
+
] });
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// src/AgentRunForm.tsx
|
|
386
|
+
var import_react3 = require("react");
|
|
387
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
388
|
+
function slotProblem(slot, count) {
|
|
389
|
+
const label = slot.label ?? slot.name;
|
|
390
|
+
const min = slot.min ?? 0;
|
|
391
|
+
if (count < min) return `${label} needs at least ${min} file${min === 1 ? "" : "s"}.`;
|
|
392
|
+
if (slot.max != null && count > slot.max) {
|
|
393
|
+
return `${label} takes at most ${slot.max} file${slot.max === 1 ? "" : "s"}.`;
|
|
394
|
+
}
|
|
395
|
+
return null;
|
|
396
|
+
}
|
|
397
|
+
function AgentRunForm({ basePath, agentId, inputLabel = "Input", onFinish }) {
|
|
398
|
+
const endpoints = (0, import_react3.useMemo)(() => createEndpoints({ basePath, agentId }), [basePath, agentId]);
|
|
399
|
+
const run = useAgentRun({ basePath, agentId });
|
|
400
|
+
const [slots, setSlots] = (0, import_react3.useState)(null);
|
|
401
|
+
const [loadError, setLoadError] = (0, import_react3.useState)(null);
|
|
402
|
+
const [input, setInput] = (0, import_react3.useState)("");
|
|
403
|
+
const [files, setFiles] = (0, import_react3.useState)({});
|
|
404
|
+
const [uploading, setUploading] = (0, import_react3.useState)(false);
|
|
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]);
|
|
416
|
+
const selected = Object.values(files).flat();
|
|
417
|
+
const busy = uploading || run.status === "running";
|
|
418
|
+
async function submit(event) {
|
|
419
|
+
event.preventDefault();
|
|
420
|
+
if (busy || !slots) return;
|
|
421
|
+
const problem = slots.map((slot) => slotProblem(slot, files[slot.name]?.length ?? 0)).find(Boolean);
|
|
422
|
+
if (problem) {
|
|
423
|
+
setFormError(problem);
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
if (!input.trim() && selected.length === 0) {
|
|
427
|
+
setFormError("Enter some text or choose a file.");
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
setFormError(null);
|
|
431
|
+
setUploading(true);
|
|
432
|
+
let uploadIds;
|
|
433
|
+
try {
|
|
434
|
+
const uploads = await Promise.all(selected.map((file) => uploadFile(endpoints, file)));
|
|
435
|
+
uploadIds = uploads.map((upload) => upload.id);
|
|
436
|
+
} catch (err) {
|
|
437
|
+
setFormError(err instanceof Error ? err.message : "The upload failed.");
|
|
438
|
+
return;
|
|
439
|
+
} finally {
|
|
440
|
+
setUploading(false);
|
|
441
|
+
}
|
|
442
|
+
const result = await run.start({
|
|
443
|
+
input: input.trim() || void 0,
|
|
444
|
+
uploadIds: uploadIds.length ? uploadIds : void 0
|
|
445
|
+
});
|
|
446
|
+
onFinish?.(result);
|
|
447
|
+
}
|
|
448
|
+
if (loadError) {
|
|
449
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "seclai-agent", children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { className: "seclai-error", role: "alert", children: loadError }) });
|
|
450
|
+
}
|
|
451
|
+
if (!slots) {
|
|
452
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "seclai-agent", children: [
|
|
453
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { className: "seclai-notice", children: "Loading\u2026" }),
|
|
454
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "seclai-skeleton", "aria-hidden": "true", children: [
|
|
455
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", {}),
|
|
456
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", {})
|
|
457
|
+
] })
|
|
458
|
+
] });
|
|
459
|
+
}
|
|
460
|
+
const error = formError ?? run.error;
|
|
461
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "seclai-agent", children: [
|
|
462
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("form", { className: "seclai-form", onSubmit: submit, children: [
|
|
463
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("label", { className: "seclai-field", children: [
|
|
464
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { children: inputLabel }),
|
|
465
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("textarea", { rows: 4, value: input, onChange: (e) => setInput(e.target.value) })
|
|
466
|
+
] }),
|
|
467
|
+
slots.map((slot) => /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("label", { className: "seclai-field", children: [
|
|
468
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("span", { children: [
|
|
469
|
+
slot.label ?? slot.name,
|
|
470
|
+
(slot.min ?? 0) > 0 ? " (required)" : ""
|
|
471
|
+
] }),
|
|
472
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
473
|
+
"input",
|
|
474
|
+
{
|
|
475
|
+
type: "file",
|
|
476
|
+
name: slot.name,
|
|
477
|
+
accept: slot.content_types?.length ? slot.content_types.join(",") : void 0,
|
|
478
|
+
multiple: slot.max == null || slot.max > 1,
|
|
479
|
+
onChange: (e) => setFiles((prev) => ({ ...prev, [slot.name]: Array.from(e.target.files ?? []) }))
|
|
480
|
+
}
|
|
481
|
+
)
|
|
482
|
+
] }, slot.name)),
|
|
483
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "seclai-actions", children: [
|
|
484
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("button", { type: "submit", className: "seclai-button seclai-button-primary", disabled: busy, children: uploading ? "Uploading\u2026" : "Run" }),
|
|
485
|
+
run.status === "running" && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("button", { type: "button", className: "seclai-button", onClick: run.cancel, children: "Stop" })
|
|
486
|
+
] })
|
|
487
|
+
] }),
|
|
488
|
+
run.steps.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("ul", { className: "seclai-steps", children: run.steps.map((step) => /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("li", { children: [
|
|
489
|
+
step.name,
|
|
490
|
+
": ",
|
|
491
|
+
step.message
|
|
492
|
+
] }, step.id)) }),
|
|
493
|
+
error && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { className: "seclai-error", role: "alert", children: error }),
|
|
494
|
+
run.status === "cancelled" && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { className: "seclai-notice", children: "Stopped." }),
|
|
495
|
+
(run.text || run.attachments.length > 0) && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "seclai-output", "aria-live": "polite", "aria-busy": run.status === "running", children: [
|
|
496
|
+
run.text && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(MarkdownContent, { content: run.text }),
|
|
497
|
+
run.attachments.map((attachment) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(MediaPreview, { attachment }, attachment.id))
|
|
498
|
+
] })
|
|
499
|
+
] });
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// src/SeclaiAgent.tsx
|
|
503
|
+
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
504
|
+
function SeclaiAgent({ mode, basePath, agentId }) {
|
|
505
|
+
return mode === "chat" ? /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(AgentChat, { basePath, agentId }) : /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(AgentRunForm, { basePath, agentId });
|
|
506
|
+
}
|
|
507
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
508
|
+
0 && (module.exports = {
|
|
509
|
+
AgentChat,
|
|
510
|
+
AgentRunForm,
|
|
511
|
+
DEFAULT_BASE_PATH,
|
|
512
|
+
MarkdownContent,
|
|
513
|
+
MediaPreview,
|
|
514
|
+
SeclaiAgent,
|
|
515
|
+
useAgentRun
|
|
516
|
+
});
|