@seclai/react 0.0.14 → 0.0.16
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 +61 -16
- package/dist/index.cjs +218 -106
- package/dist/index.d.cts +82 -3
- package/dist/index.d.ts +82 -3
- package/dist/index.js +201 -93
- package/dist/styles.css +61 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,6 +32,7 @@ export function App() {
|
|
|
32
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
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
|
+
- **`RunProgress`** shows a running run's steps and elapsed time. See [below](#runprogress).
|
|
35
36
|
|
|
36
37
|
### Files in `AgentChat`
|
|
37
38
|
|
|
@@ -133,34 +134,41 @@ await start({ input: "Hello", uploadIds: ["..."], sessionId: "..." });
|
|
|
133
134
|
|
|
134
135
|
Options also take `basePath` and `agentId`, as the components do.
|
|
135
136
|
|
|
136
|
-
|
|
137
|
+
### `RunProgress`
|
|
138
|
+
|
|
139
|
+
Runs that generate images can take a minute or two. `RunProgress` shows a running run's steps and the time elapsed, and renders nothing otherwise:
|
|
137
140
|
|
|
138
141
|
```tsx
|
|
139
|
-
|
|
140
|
-
const [now, setNow] = useState(Date.now());
|
|
141
|
-
useEffect(() => {
|
|
142
|
-
const timer = setInterval(() => setNow(Date.now()), 1000);
|
|
143
|
-
return () => clearInterval(timer);
|
|
144
|
-
}, []);
|
|
145
|
-
const seconds = Math.max(0, Math.floor((now - startedAt) / 1000));
|
|
146
|
-
return <>{Math.floor(seconds / 60)}:{String(seconds % 60).padStart(2, "0")}</>;
|
|
147
|
-
}
|
|
142
|
+
import { RunProgress, useAgentRun } from "@seclai/react";
|
|
148
143
|
|
|
149
144
|
const run = useAgentRun();
|
|
150
145
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
)}
|
|
146
|
+
<RunProgress
|
|
147
|
+
run={run}
|
|
148
|
+
message={(seconds) => (seconds < 150 ? "Generating images usually takes a minute or two." : "Still working…")}
|
|
149
|
+
/>
|
|
156
150
|
```
|
|
157
151
|
|
|
152
|
+
- It shows "Starting the run…" until the first step, then each step's name and message, with ✓ for finished steps and ● for the current one.
|
|
153
|
+
- The clock reads `m:ss`, and screen readers get "42 seconds elapsed". Steps and the message are announced as they change, but the clock isn't, so keep the seconds out of `message`.
|
|
154
|
+
- `run` needs only `status`, `steps` and `startedAt`, so a plain object works too.
|
|
155
|
+
- `AgentChat` shows it until the reply starts streaming.
|
|
156
|
+
|
|
157
|
+
| Prop | |
|
|
158
|
+
|---|---|
|
|
159
|
+
| `run` | Required. `{ status, steps, startedAt }`, such as a `useAgentRun` result |
|
|
160
|
+
| `showSteps` | Show the step list. Defaults to `true` |
|
|
161
|
+
| `message` | A note next to the clock: a node, or `(seconds) => ReactNode` |
|
|
162
|
+
| `className` | Added to the outer element |
|
|
163
|
+
|
|
164
|
+
For just the clock, `useElapsed(startedAt, running)` returns whole seconds since `startedAt`, ticking while `running`. It's 0 on the first render, so server and client markup match.
|
|
165
|
+
|
|
158
166
|
### Building your own UI
|
|
159
167
|
|
|
160
168
|
To upload files and run the agent from your own UI, use the helpers the components are built on:
|
|
161
169
|
|
|
162
170
|
- **`createEndpoints({ basePath, agentId })`** returns the handler URLs (`AgentEndpoints`): `attachmentReferences`, `upload`, `run`, and `download(runId, attachmentId, name?)`. Pass it the same options as `useAgentRun`.
|
|
163
|
-
- **`uploadFile(endpoints, file, { signal }?)`** uploads one file and resolves with its `id`.
|
|
171
|
+
- **`uploadFile(endpoints, file, { signal, params }?)`** uploads one file and resolves with its `id`. `params` is added to the upload URL's query string, for the server's upload hooks. See [Sending data with an upload](#sending-data-with-an-upload).
|
|
164
172
|
- **`fetchAttachmentReferences(endpoints, { signal }?)`** resolves with the files the agent accepts (`accepted_attachments`) and whether it needs any (`requires_uploads`).
|
|
165
173
|
|
|
166
174
|
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.
|
|
@@ -189,6 +197,21 @@ A run's `attachments` already have a `url`. To show a file from a run you stored
|
|
|
189
197
|
<a href={endpoints.download(saved.runId, saved.attachmentId, saved.name)}>{saved.name}</a>
|
|
190
198
|
```
|
|
191
199
|
|
|
200
|
+
#### Sending data with an upload
|
|
201
|
+
|
|
202
|
+
Pass `params` to tell the server what an upload is for, such as the session it belongs to or its role. They're added to the upload URL, after any query string it already has, and `@seclai/handlers` passes them to `authorizeUpload` and `upload.onUpload`. They're visible in the URL, so keep secrets out of them.
|
|
203
|
+
|
|
204
|
+
If the server's `onUpload` returns its own record, name its type:
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
type MyUpload = { id: string; uploadId: string; sessionId: string; kind: "headshot" | "style" };
|
|
208
|
+
|
|
209
|
+
const upload = await uploadFile<MyUpload>(endpoints, file, { params: { sessionId, kind: "headshot" } });
|
|
210
|
+
upload.kind; // "headshot" | "style"
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
The type isn't checked at run time: it's whatever the server sends. Without one, the result is an `UploadResult`. When the record replaces the default JSON, start the run with the Seclai upload id it holds (`uploadId` here), not your record's own `id`.
|
|
214
|
+
|
|
192
215
|
## Styling
|
|
193
216
|
|
|
194
217
|
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:
|
|
@@ -244,6 +267,28 @@ import { MediaGallery, MediaPreview, MediaSkeleton, MediaViewer } from "@seclai/
|
|
|
244
267
|
|
|
245
268
|
The stylesheet adds these variables for media: `--seclai-media-aspect-ratio` (a frame's ratio before its image loads, `1 / 1`), `--seclai-media-fit` (`object-fit` in the frame, `scale-down`, so small images keep their natural size), `--seclai-media-min-width` (a gallery column's minimum width, `220px`) and `--seclai-media-max-width` (a card's width outside a gallery, `480px`). The viewer is dark in both colour schemes.
|
|
246
269
|
|
|
270
|
+
### Saving files
|
|
271
|
+
|
|
272
|
+
The gallery's downloads are exported, for your own buttons:
|
|
273
|
+
|
|
274
|
+
```tsx
|
|
275
|
+
import { MediaGallery, saveFile } from "@seclai/react";
|
|
276
|
+
|
|
277
|
+
<MediaGallery
|
|
278
|
+
attachments={run.attachments}
|
|
279
|
+
actions={(attachment, number) => (
|
|
280
|
+
<button onClick={async () => saveFile(await toJpeg(attachment.url), `thumbnail-${number}.jpg`)}>
|
|
281
|
+
Save as JPEG
|
|
282
|
+
</button>
|
|
283
|
+
)}
|
|
284
|
+
/>
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
Here `toJpeg` is your own code that draws the image on a canvas and returns `canvas.toBlob()`'s result.
|
|
288
|
+
|
|
289
|
+
- **`saveFile(urlOrBlob, filename)`** saves a URL or a `Blob` through a temporary `<a download>`. A URL that isn't `http:`, `https:`, `blob:` or relative (such as `javascript:`) is skipped with a warning. For a `Blob` it makes an object URL and revokes it 40 seconds later, once the browser has had time to start the download. Without a `document`, as in server rendering, it does nothing.
|
|
290
|
+
- **`saveFiles(files, { delayMs, signal })`** saves `{ source, filename }` files one at a time with `saveFile`, waiting `delayMs` (400 ms) between them, as "Download all" does. A `source` is a URL or a `Blob`. It stops when `signal` aborts.
|
|
291
|
+
|
|
247
292
|
## Placeholders
|
|
248
293
|
|
|
249
294
|
These are exported so the API is in place, but they're minimal until they're ported from the main Seclai app:
|