@seclai/react 0.0.3 → 0.0.5

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 CHANGED
@@ -49,6 +49,40 @@ await start({ input: "Hello", uploadIds: ["..."], sessionId: "..." });
49
49
  - `start` resolves with the final state and doesn't reject. Failures appear in `status` and `error`.
50
50
  - `cancel` stops streaming, but the run carries on at Seclai.
51
51
 
52
+ ### Building your own UI
53
+
54
+ `AgentChat` doesn't upload files. To send files from your own UI, use the helpers the components are built on:
55
+
56
+ - **`createEndpoints({ basePath, agentId })`** returns the handler URLs (`AgentEndpoints`): `attachmentReferences`, `upload`, `run`, and `download(runId, attachmentId, name?)`. Pass it the same options as `useAgentRun`.
57
+ - **`uploadFile(endpoints, file, { signal }?)`** uploads one file and resolves with its `id`.
58
+ - **`fetchAttachmentReferences(endpoints, { signal }?)`** resolves with the files the agent accepts (`accepted_attachments`) and whether it needs any (`requires_uploads`).
59
+
60
+ 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.
61
+
62
+ Upload the files first, then start the run with their ids:
63
+
64
+ ```tsx
65
+ import { createEndpoints, uploadFile, useAgentRun } from "@seclai/react";
66
+
67
+ const endpoints = createEndpoints({ agentId: "my-agent" });
68
+
69
+ function Ask() {
70
+ const { start } = useAgentRun({ agentId: "my-agent" });
71
+
72
+ async function send(input: string, files: File[]) {
73
+ const uploads = await Promise.all(files.map((file) => uploadFile(endpoints, file)));
74
+ await start({ input, uploadIds: uploads.map((upload) => upload.id) });
75
+ }
76
+ // ...
77
+ }
78
+ ```
79
+
80
+ 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:
81
+
82
+ ```tsx
83
+ <a href={endpoints.download(saved.runId, saved.attachmentId, saved.name)}>{saved.name}</a>
84
+ ```
85
+
52
86
  ## Styling
53
87
 
54
88
  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,6 +27,9 @@ __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);
@@ -84,13 +87,13 @@ async function readJson(res) {
84
87
  if (!res.ok) throw new Error(await readErrorMessage(res));
85
88
  return await res.json();
86
89
  }
87
- async function fetchAttachmentReferences(endpoints, signal) {
90
+ async function fetchAttachmentReferences(endpoints, { signal } = {}) {
88
91
  return readJson(await fetch(endpoints.attachmentReferences, { signal }));
89
92
  }
90
- async function uploadFile(endpoints, file) {
93
+ async function uploadFile(endpoints, file, { signal } = {}) {
91
94
  const form = new FormData();
92
95
  form.set("file", file);
93
- return readJson(await fetch(endpoints.upload, { method: "POST", body: form }));
96
+ return readJson(await fetch(endpoints.upload, { method: "POST", body: form, signal }));
94
97
  }
95
98
 
96
99
  // src/sse.ts
@@ -407,7 +410,7 @@ function AgentRunForm({ basePath, agentId, inputLabel = "Input", onFinish }) {
407
410
  const controller = new AbortController();
408
411
  setSlots(null);
409
412
  setLoadError(null);
410
- fetchAttachmentReferences(endpoints, controller.signal).then((refs) => setSlots(refs.accepted_attachments ?? [])).catch((err) => {
413
+ fetchAttachmentReferences(endpoints, { signal: controller.signal }).then((refs) => setSlots(refs.accepted_attachments ?? [])).catch((err) => {
411
414
  if (controller.signal.aborted) return;
412
415
  setLoadError(err instanceof Error ? err.message : "Could not load this agent.");
413
416
  });
@@ -512,5 +515,8 @@ function SeclaiAgent({ mode, basePath, agentId }) {
512
515
  MarkdownContent,
513
516
  MediaPreview,
514
517
  SeclaiAgent,
518
+ createEndpoints,
519
+ fetchAttachmentReferences,
520
+ uploadFile,
515
521
  useAgentRun
516
522
  });
package/dist/index.d.cts CHANGED
@@ -118,6 +118,62 @@ interface SeclaiAgentProps extends AgentEndpointOptions {
118
118
  declare function SeclaiAgent({ mode, basePath, agentId }: SeclaiAgentProps): react.JSX.Element;
119
119
 
120
120
  declare const DEFAULT_BASE_PATH = "/api/seclai";
121
+ /**
122
+ * The URLs of the `@seclai/handlers` routes. Build it with
123
+ * {@link createEndpoints} rather than by hand: members may be added in later
124
+ * versions.
125
+ */
126
+ interface AgentEndpoints {
127
+ /** `GET`: the files the agent accepts. See {@link fetchAttachmentReferences}. */
128
+ attachmentReferences: string;
129
+ /** `POST` multipart form data with a `file` field. See {@link uploadFile}. */
130
+ upload: string;
131
+ /** `POST` JSON `{ input, uploadIds, sessionId }`: streams the run as SSE. */
132
+ run: string;
133
+ /**
134
+ * The download proxy URL for a file a run produced. Use it in place of the
135
+ * API's `download_url`, which needs the API key the browser doesn't have.
136
+ *
137
+ * @param runId The run that produced the file.
138
+ * @param attachmentId The attachment's `id`.
139
+ * @param name Optional file name for the download. Left out when empty.
140
+ */
141
+ download(runId: string, attachmentId: string, name?: string | null): string;
142
+ }
143
+ /**
144
+ * Build the handler URLs for an agent, the same way `useAgentRun` and the
145
+ * components do. Use it to link to stored attachments with `download`, rather
146
+ * than writing the URL format by hand.
147
+ *
148
+ * Trailing slashes on `basePath` are dropped and `agentId` is URL-encoded.
149
+ *
150
+ * @example
151
+ * const endpoints = createEndpoints({ agentId: "my-agent" });
152
+ * endpoints.run; // "/api/seclai/my-agent/run"
153
+ * endpoints.download(runId, attachmentId, "report.pdf");
154
+ */
155
+ declare function createEndpoints({ basePath, agentId, }?: AgentEndpointOptions): AgentEndpoints;
156
+ /** Per-call options for {@link uploadFile} and {@link fetchAttachmentReferences}. */
157
+ interface EndpointRequestOptions {
158
+ /** Aborts the request. */
159
+ signal?: AbortSignal;
160
+ }
161
+ /**
162
+ * Fetch the files the agent's trigger accepts, and whether it requires any.
163
+ *
164
+ * @throws An `Error` with the handler's error message when the response isn't
165
+ * OK. Rejects as `fetch` does on a network failure or when `signal` aborts.
166
+ */
167
+ declare function fetchAttachmentReferences(endpoints: AgentEndpoints, { signal }?: EndpointRequestOptions): Promise<AttachmentReferences>;
168
+ /**
169
+ * Upload one file through the `upload` handler. Pass the returned `id` to
170
+ * `start({ uploadIds })` from `useAgentRun`.
171
+ *
172
+ * @param options `signal` aborts the upload.
173
+ * @throws An `Error` with the handler's error message when the response isn't
174
+ * OK. Rejects as `fetch` does on a network failure or when `signal` aborts.
175
+ */
176
+ declare function uploadFile(endpoints: AgentEndpoints, file: File, { signal }?: EndpointRequestOptions): Promise<UploadResult>;
121
177
 
122
178
  interface MarkdownContentProps {
123
179
  content: string;
@@ -137,4 +193,4 @@ interface MediaPreviewProps {
137
193
  */
138
194
  declare function MediaPreview({ attachment }: MediaPreviewProps): react.JSX.Element;
139
195
 
140
- export { type AcceptedAttachmentSlot, type AgentAttachment, AgentChat, type AgentChatProps, type AgentEndpointOptions, AgentRunForm, type AgentRunFormProps, type AgentRunState, type AgentRunStatus, type AgentRunStep, type AgentUiMode, type AttachmentReferences, DEFAULT_BASE_PATH, MarkdownContent, type MarkdownContentProps, MediaPreview, type MediaPreviewProps, SeclaiAgent, type SeclaiAgentProps, type StartRunOptions, type UploadResult, type UseAgentRunResult, useAgentRun };
196
+ export { type AcceptedAttachmentSlot, type AgentAttachment, AgentChat, type AgentChatProps, type AgentEndpointOptions, type AgentEndpoints, AgentRunForm, type AgentRunFormProps, type AgentRunState, type AgentRunStatus, type AgentRunStep, type AgentUiMode, type AttachmentReferences, DEFAULT_BASE_PATH, type EndpointRequestOptions, MarkdownContent, type MarkdownContentProps, MediaPreview, type MediaPreviewProps, SeclaiAgent, type SeclaiAgentProps, type StartRunOptions, type UploadResult, type UseAgentRunResult, createEndpoints, fetchAttachmentReferences, uploadFile, useAgentRun };
package/dist/index.d.ts CHANGED
@@ -118,6 +118,62 @@ interface SeclaiAgentProps extends AgentEndpointOptions {
118
118
  declare function SeclaiAgent({ mode, basePath, agentId }: SeclaiAgentProps): react.JSX.Element;
119
119
 
120
120
  declare const DEFAULT_BASE_PATH = "/api/seclai";
121
+ /**
122
+ * The URLs of the `@seclai/handlers` routes. Build it with
123
+ * {@link createEndpoints} rather than by hand: members may be added in later
124
+ * versions.
125
+ */
126
+ interface AgentEndpoints {
127
+ /** `GET`: the files the agent accepts. See {@link fetchAttachmentReferences}. */
128
+ attachmentReferences: string;
129
+ /** `POST` multipart form data with a `file` field. See {@link uploadFile}. */
130
+ upload: string;
131
+ /** `POST` JSON `{ input, uploadIds, sessionId }`: streams the run as SSE. */
132
+ run: string;
133
+ /**
134
+ * The download proxy URL for a file a run produced. Use it in place of the
135
+ * API's `download_url`, which needs the API key the browser doesn't have.
136
+ *
137
+ * @param runId The run that produced the file.
138
+ * @param attachmentId The attachment's `id`.
139
+ * @param name Optional file name for the download. Left out when empty.
140
+ */
141
+ download(runId: string, attachmentId: string, name?: string | null): string;
142
+ }
143
+ /**
144
+ * Build the handler URLs for an agent, the same way `useAgentRun` and the
145
+ * components do. Use it to link to stored attachments with `download`, rather
146
+ * than writing the URL format by hand.
147
+ *
148
+ * Trailing slashes on `basePath` are dropped and `agentId` is URL-encoded.
149
+ *
150
+ * @example
151
+ * const endpoints = createEndpoints({ agentId: "my-agent" });
152
+ * endpoints.run; // "/api/seclai/my-agent/run"
153
+ * endpoints.download(runId, attachmentId, "report.pdf");
154
+ */
155
+ declare function createEndpoints({ basePath, agentId, }?: AgentEndpointOptions): AgentEndpoints;
156
+ /** Per-call options for {@link uploadFile} and {@link fetchAttachmentReferences}. */
157
+ interface EndpointRequestOptions {
158
+ /** Aborts the request. */
159
+ signal?: AbortSignal;
160
+ }
161
+ /**
162
+ * Fetch the files the agent's trigger accepts, and whether it requires any.
163
+ *
164
+ * @throws An `Error` with the handler's error message when the response isn't
165
+ * OK. Rejects as `fetch` does on a network failure or when `signal` aborts.
166
+ */
167
+ declare function fetchAttachmentReferences(endpoints: AgentEndpoints, { signal }?: EndpointRequestOptions): Promise<AttachmentReferences>;
168
+ /**
169
+ * Upload one file through the `upload` handler. Pass the returned `id` to
170
+ * `start({ uploadIds })` from `useAgentRun`.
171
+ *
172
+ * @param options `signal` aborts the upload.
173
+ * @throws An `Error` with the handler's error message when the response isn't
174
+ * OK. Rejects as `fetch` does on a network failure or when `signal` aborts.
175
+ */
176
+ declare function uploadFile(endpoints: AgentEndpoints, file: File, { signal }?: EndpointRequestOptions): Promise<UploadResult>;
121
177
 
122
178
  interface MarkdownContentProps {
123
179
  content: string;
@@ -137,4 +193,4 @@ interface MediaPreviewProps {
137
193
  */
138
194
  declare function MediaPreview({ attachment }: MediaPreviewProps): react.JSX.Element;
139
195
 
140
- export { type AcceptedAttachmentSlot, type AgentAttachment, AgentChat, type AgentChatProps, type AgentEndpointOptions, AgentRunForm, type AgentRunFormProps, type AgentRunState, type AgentRunStatus, type AgentRunStep, type AgentUiMode, type AttachmentReferences, DEFAULT_BASE_PATH, MarkdownContent, type MarkdownContentProps, MediaPreview, type MediaPreviewProps, SeclaiAgent, type SeclaiAgentProps, type StartRunOptions, type UploadResult, type UseAgentRunResult, useAgentRun };
196
+ export { type AcceptedAttachmentSlot, type AgentAttachment, AgentChat, type AgentChatProps, type AgentEndpointOptions, type AgentEndpoints, AgentRunForm, type AgentRunFormProps, type AgentRunState, type AgentRunStatus, type AgentRunStep, type AgentUiMode, type AttachmentReferences, DEFAULT_BASE_PATH, type EndpointRequestOptions, MarkdownContent, type MarkdownContentProps, MediaPreview, type MediaPreviewProps, SeclaiAgent, type SeclaiAgentProps, type StartRunOptions, type UploadResult, type UseAgentRunResult, createEndpoints, fetchAttachmentReferences, uploadFile, useAgentRun };
package/dist/index.js CHANGED
@@ -53,13 +53,13 @@ async function readJson(res) {
53
53
  if (!res.ok) throw new Error(await readErrorMessage(res));
54
54
  return await res.json();
55
55
  }
56
- async function fetchAttachmentReferences(endpoints, signal) {
56
+ async function fetchAttachmentReferences(endpoints, { signal } = {}) {
57
57
  return readJson(await fetch(endpoints.attachmentReferences, { signal }));
58
58
  }
59
- async function uploadFile(endpoints, file) {
59
+ async function uploadFile(endpoints, file, { signal } = {}) {
60
60
  const form = new FormData();
61
61
  form.set("file", file);
62
- return readJson(await fetch(endpoints.upload, { method: "POST", body: form }));
62
+ return readJson(await fetch(endpoints.upload, { method: "POST", body: form, signal }));
63
63
  }
64
64
 
65
65
  // src/sse.ts
@@ -376,7 +376,7 @@ function AgentRunForm({ basePath, agentId, inputLabel = "Input", onFinish }) {
376
376
  const controller = new AbortController();
377
377
  setSlots(null);
378
378
  setLoadError(null);
379
- fetchAttachmentReferences(endpoints, controller.signal).then((refs) => setSlots(refs.accepted_attachments ?? [])).catch((err) => {
379
+ fetchAttachmentReferences(endpoints, { signal: controller.signal }).then((refs) => setSlots(refs.accepted_attachments ?? [])).catch((err) => {
380
380
  if (controller.signal.aborted) return;
381
381
  setLoadError(err instanceof Error ? err.message : "Could not load this agent.");
382
382
  });
@@ -480,5 +480,8 @@ export {
480
480
  MarkdownContent,
481
481
  MediaPreview,
482
482
  SeclaiAgent,
483
+ createEndpoints,
484
+ fetchAttachmentReferences,
485
+ uploadFile,
483
486
  useAgentRun
484
487
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seclai/react",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "React components and hooks for running Seclai agents: run form, chat, and streaming output.",
5
5
  "license": "MIT",
6
6
  "type": "module",