@seclai/react 0.0.6 → 0.0.8

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
@@ -63,7 +63,9 @@ To tell files apart, add named buttons with tags, then use the tags in `onBefore
63
63
  Build your own UI on the hook:
64
64
 
65
65
  ```tsx
66
- 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
+ });
67
69
 
68
70
  await start({ input: "Hello", uploadIds: ["..."], sessionId: "..." });
69
71
  ```
@@ -71,8 +73,35 @@ await start({ input: "Hello", uploadIds: ["..."], sessionId: "..." });
71
73
  - `status` is `idle`, `running`, `completed`, `failed` or `cancelled`.
72
74
  - `text` holds streamed tokens while running, then the run's final output.
73
75
  - `attachments` have a `url` that goes through your download handler.
74
- - `start` resolves with the final state and doesn't reject. Failures appear in `status` and `error`.
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`.
75
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
+ ```
76
105
 
77
106
  ### Building your own UI
78
107
 
@@ -125,6 +154,44 @@ Fonts and text colour come from your page. The default colours follow your page'
125
154
 
126
155
  **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.
127
156
 
157
+ ## Media
158
+
159
+ `AgentRunForm` and `AgentChat` show a run's attachments in a `MediaGallery`. The media components are exported too, for your own UI:
160
+
161
+ ```tsx
162
+ import { MediaGallery, MediaPreview, MediaSkeleton, MediaViewer } from "@seclai/react";
163
+
164
+ <MediaGallery
165
+ attachments={run.attachments}
166
+ aspectRatio="16 / 9"
167
+ pending={run.status === "running" ? 4 : 0}
168
+ actions={(attachment, number) => <button onClick={() => refine(number)}>Refine</button>}
169
+ />
170
+ ```
171
+
172
+ - **`MediaPreview`** shows one attachment. An image gets a card: a fixed-ratio frame so the layout doesn't jump while it loads, an optional number badge and a download link. Files that aren't images are a download link.
173
+ - **`MediaGallery`** lays out a run's images as numbered cards that open a full-size viewer, with "Download all" when there's more than one. It downloads the files one at a time, because browsers block a burst of simultaneous downloads. Other files follow the images as download links.
174
+ - **`MediaSkeleton`** is a placeholder card for an image that's still being generated. It pulses, unless the user prefers reduced motion.
175
+ - **`MediaViewer`** is the gallery's full-size viewer, a modal `<dialog>` with previous and next (also on the arrow keys) and a download button. Escape, the close button or a click on the backdrop closes it, and focus goes back to where it was. It's controlled: pass the `index` to show, or `null` when closed.
176
+ - **`attachmentFileName(attachment, number?)`** is the filename the components save a file as: its own name, or else `image-<number>` (or its id) with an extension from the mime type, such as `image-2.png`.
177
+
178
+ | `MediaPreview` prop | |
179
+ |---|---|
180
+ | `attachment` | Required. The file to show |
181
+ | `aspectRatio` | The frame's ratio, as a number (`16 / 9`) or a string (`"16 / 9"`). Without it the frame takes the image's own ratio once it loads, and `--seclai-media-aspect-ratio` (square) until then |
182
+ | `number` | A badge on the image, matching the agent's numbered list. Also names an unnamed download `image-<number>` |
183
+ | `actions` | App-specific buttons after the download link: a node, or a function of the attachment |
184
+ | `onOpen` | Makes the image a button, for opening a viewer |
185
+
186
+ | `MediaGallery` prop | |
187
+ |---|---|
188
+ | `attachments` | Required. A run's attachments |
189
+ | `aspectRatio` | Every card's ratio. Without it each card takes its image's own ratio |
190
+ | `pending` | How many skeleton cards to show after the images, numbered on from them |
191
+ | `actions` | `(attachment, number) => ReactNode`, for each image card |
192
+
193
+ 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.
194
+
128
195
  ## Placeholders
129
196
 
130
197
  These are exported so the API is in place, but they're minimal until they're ported from the main Seclai app:
@@ -132,7 +199,7 @@ These are exported so the API is in place, but they're minimal until they're por
132
199
  | Export | For now | Coming |
133
200
  |---|---|---|
134
201
  | `MarkdownContent` | Plain text | GFM tables, KaTeX, mermaid, sanitized SVG |
135
- | `MediaPreview` | Images inline, a download link for everything else | Image, audio, video, PDF, download fallback |
202
+ | `MediaPreview` | Image cards (see [Media](#media)), a download link for everything else | Audio, video and PDF previews |
136
203
 
137
204
  ## License
138
205