@seclai/react 0.0.13 → 0.0.15
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 +38 -1
- package/dist/index.cjs +51 -15
- package/dist/index.d.cts +54 -3
- package/dist/index.d.ts +54 -3
- package/dist/index.js +49 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -160,7 +160,7 @@ const run = useAgentRun();
|
|
|
160
160
|
To upload files and run the agent from your own UI, use the helpers the components are built on:
|
|
161
161
|
|
|
162
162
|
- **`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`.
|
|
163
|
+
- **`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
164
|
- **`fetchAttachmentReferences(endpoints, { signal }?)`** resolves with the files the agent accepts (`accepted_attachments`) and whether it needs any (`requires_uploads`).
|
|
165
165
|
|
|
166
166
|
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 +189,21 @@ A run's `attachments` already have a `url`. To show a file from a run you stored
|
|
|
189
189
|
<a href={endpoints.download(saved.runId, saved.attachmentId, saved.name)}>{saved.name}</a>
|
|
190
190
|
```
|
|
191
191
|
|
|
192
|
+
#### Sending data with an upload
|
|
193
|
+
|
|
194
|
+
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.
|
|
195
|
+
|
|
196
|
+
If the server's `onUpload` returns its own record, name its type:
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
type MyUpload = { id: string; uploadId: string; sessionId: string; kind: "headshot" | "style" };
|
|
200
|
+
|
|
201
|
+
const upload = await uploadFile<MyUpload>(endpoints, file, { params: { sessionId, kind: "headshot" } });
|
|
202
|
+
upload.kind; // "headshot" | "style"
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
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`.
|
|
206
|
+
|
|
192
207
|
## Styling
|
|
193
208
|
|
|
194
209
|
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 +259,28 @@ import { MediaGallery, MediaPreview, MediaSkeleton, MediaViewer } from "@seclai/
|
|
|
244
259
|
|
|
245
260
|
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
261
|
|
|
262
|
+
### Saving files
|
|
263
|
+
|
|
264
|
+
The gallery's downloads are exported, for your own buttons:
|
|
265
|
+
|
|
266
|
+
```tsx
|
|
267
|
+
import { MediaGallery, saveFile } from "@seclai/react";
|
|
268
|
+
|
|
269
|
+
<MediaGallery
|
|
270
|
+
attachments={run.attachments}
|
|
271
|
+
actions={(attachment, number) => (
|
|
272
|
+
<button onClick={async () => saveFile(await toJpeg(attachment.url), `thumbnail-${number}.jpg`)}>
|
|
273
|
+
Save as JPEG
|
|
274
|
+
</button>
|
|
275
|
+
)}
|
|
276
|
+
/>
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
Here `toJpeg` is your own code that draws the image on a canvas and returns `canvas.toBlob()`'s result.
|
|
280
|
+
|
|
281
|
+
- **`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.
|
|
282
|
+
- **`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.
|
|
283
|
+
|
|
247
284
|
## Placeholders
|
|
248
285
|
|
|
249
286
|
These are exported so the API is in place, but they're minimal until they're ported from the main Seclai app:
|
package/dist/index.cjs
CHANGED
|
@@ -33,6 +33,8 @@ __export(index_exports, {
|
|
|
33
33
|
attachmentFileName: () => attachmentFileName,
|
|
34
34
|
createEndpoints: () => createEndpoints,
|
|
35
35
|
fetchAttachmentReferences: () => fetchAttachmentReferences,
|
|
36
|
+
saveFile: () => saveFile,
|
|
37
|
+
saveFiles: () => saveFiles,
|
|
36
38
|
uploadFile: () => uploadFile,
|
|
37
39
|
useAgentRun: () => useAgentRun
|
|
38
40
|
});
|
|
@@ -78,10 +80,20 @@ async function readJson(res) {
|
|
|
78
80
|
async function fetchAttachmentReferences(endpoints, { signal } = {}) {
|
|
79
81
|
return readJson(await fetch(endpoints.attachmentReferences, { signal }));
|
|
80
82
|
}
|
|
81
|
-
async function uploadFile(endpoints, file, { signal } = {}) {
|
|
83
|
+
async function uploadFile(endpoints, file, { signal, params } = {}) {
|
|
82
84
|
const form = new FormData();
|
|
83
85
|
form.set("file", file);
|
|
84
|
-
|
|
86
|
+
const url = params ? withQuery(endpoints.upload, params) : endpoints.upload;
|
|
87
|
+
return readJson(await fetch(url, { method: "POST", body: form, signal }));
|
|
88
|
+
}
|
|
89
|
+
function withQuery(url, params) {
|
|
90
|
+
const query = new URLSearchParams(params).toString();
|
|
91
|
+
if (!query) return url;
|
|
92
|
+
const hashAt = url.indexOf("#");
|
|
93
|
+
const base = hashAt < 0 ? url : url.slice(0, hashAt);
|
|
94
|
+
const hash = hashAt < 0 ? "" : url.slice(hashAt);
|
|
95
|
+
const separator = !base.includes("?") ? "?" : base.endsWith("?") || base.endsWith("&") ? "" : "&";
|
|
96
|
+
return `${base}${separator}${query}${hash}`;
|
|
85
97
|
}
|
|
86
98
|
|
|
87
99
|
// src/attachments.ts
|
|
@@ -297,11 +309,9 @@ function isSafeUrl(url) {
|
|
|
297
309
|
return false;
|
|
298
310
|
}
|
|
299
311
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
return;
|
|
304
|
-
}
|
|
312
|
+
var SAVE_FILES_DELAY_MS = 400;
|
|
313
|
+
var REVOKE_OBJECT_URL_DELAY_MS = 4e4;
|
|
314
|
+
function clickDownloadLink(url, filename) {
|
|
305
315
|
const link = document.createElement("a");
|
|
306
316
|
link.href = url;
|
|
307
317
|
link.download = filename;
|
|
@@ -311,14 +321,39 @@ function triggerDownload(url, filename) {
|
|
|
311
321
|
link.click();
|
|
312
322
|
link.remove();
|
|
313
323
|
}
|
|
314
|
-
|
|
324
|
+
function saveFile(urlOrBlob, filename) {
|
|
325
|
+
if (typeof document === "undefined") return;
|
|
326
|
+
if (typeof urlOrBlob !== "string") {
|
|
327
|
+
const url = URL.createObjectURL(urlOrBlob);
|
|
328
|
+
clickDownloadLink(url, filename);
|
|
329
|
+
setTimeout(() => URL.revokeObjectURL(url), REVOKE_OBJECT_URL_DELAY_MS);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (!isSafeUrl(urlOrBlob)) {
|
|
333
|
+
console.warn(`seclai: not downloading ${filename}, its URL isn't http(s), blob or relative.`);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
clickDownloadLink(urlOrBlob, filename);
|
|
337
|
+
}
|
|
338
|
+
async function saveFiles(files, { delayMs = SAVE_FILES_DELAY_MS, signal } = {}) {
|
|
315
339
|
for (const [i, file] of files.entries()) {
|
|
316
340
|
if (signal?.aborted) return;
|
|
317
|
-
if (i > 0) await
|
|
341
|
+
if (i > 0) await wait(delayMs, signal);
|
|
318
342
|
if (signal?.aborted) return;
|
|
319
|
-
|
|
343
|
+
saveFile(file.source, file.filename);
|
|
320
344
|
}
|
|
321
345
|
}
|
|
346
|
+
function wait(ms, signal) {
|
|
347
|
+
return new Promise((resolve) => {
|
|
348
|
+
const done = () => {
|
|
349
|
+
clearTimeout(timer);
|
|
350
|
+
signal?.removeEventListener("abort", done);
|
|
351
|
+
resolve();
|
|
352
|
+
};
|
|
353
|
+
const timer = setTimeout(done, ms);
|
|
354
|
+
signal?.addEventListener("abort", done, { once: true });
|
|
355
|
+
});
|
|
356
|
+
}
|
|
322
357
|
|
|
323
358
|
// src/placeholders/MediaPreview.tsx
|
|
324
359
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
@@ -533,7 +568,7 @@ function MediaViewer({ attachments, index, onIndexChange, onClose }) {
|
|
|
533
568
|
|
|
534
569
|
// src/media/MediaGallery.tsx
|
|
535
570
|
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
536
|
-
var DOWNLOAD_ALL_DELAY_MS =
|
|
571
|
+
var DOWNLOAD_ALL_DELAY_MS = SAVE_FILES_DELAY_MS;
|
|
537
572
|
function MediaGallery({
|
|
538
573
|
attachments,
|
|
539
574
|
aspectRatio,
|
|
@@ -553,10 +588,9 @@ function MediaGallery({
|
|
|
553
588
|
abort.current = controller;
|
|
554
589
|
setDownloading(true);
|
|
555
590
|
try {
|
|
556
|
-
await
|
|
557
|
-
images.map((image, i) => ({
|
|
558
|
-
DOWNLOAD_ALL_DELAY_MS,
|
|
559
|
-
controller.signal
|
|
591
|
+
await saveFiles(
|
|
592
|
+
images.map((image, i) => ({ source: image.url, filename: attachmentFileName(image, i + 1) })),
|
|
593
|
+
{ delayMs: DOWNLOAD_ALL_DELAY_MS, signal: controller.signal }
|
|
560
594
|
);
|
|
561
595
|
} finally {
|
|
562
596
|
if (!controller.signal.aborted) setDownloading(false);
|
|
@@ -1339,6 +1373,8 @@ function SeclaiAgent({ mode, basePath, agentId }) {
|
|
|
1339
1373
|
attachmentFileName,
|
|
1340
1374
|
createEndpoints,
|
|
1341
1375
|
fetchAttachmentReferences,
|
|
1376
|
+
saveFile,
|
|
1377
|
+
saveFiles,
|
|
1342
1378
|
uploadFile,
|
|
1343
1379
|
useAgentRun
|
|
1344
1380
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -284,15 +284,36 @@ interface EndpointRequestOptions {
|
|
|
284
284
|
* OK. Rejects as `fetch` does on a network failure or when `signal` aborts.
|
|
285
285
|
*/
|
|
286
286
|
declare function fetchAttachmentReferences(endpoints: AgentEndpoints, { signal }?: EndpointRequestOptions): Promise<AttachmentReferences>;
|
|
287
|
+
/** Options for {@link uploadFile}. */
|
|
288
|
+
interface UploadFileOptions extends EndpointRequestOptions {
|
|
289
|
+
/**
|
|
290
|
+
* Your app's data for this upload, for example the session it belongs to or
|
|
291
|
+
* the file's role. Added to the upload URL's query string, after any query
|
|
292
|
+
* the URL already has; the `upload` handler passes them to `authorizeUpload`
|
|
293
|
+
* and `upload.onUpload`. They are visible in the URL and in server logs, so
|
|
294
|
+
* keep secrets out of them.
|
|
295
|
+
*/
|
|
296
|
+
params?: Record<string, string> | URLSearchParams;
|
|
297
|
+
}
|
|
287
298
|
/**
|
|
288
299
|
* Upload one file through the `upload` handler. Pass the returned `id` to
|
|
289
300
|
* `start({ uploadIds })` from `useAgentRun`.
|
|
290
301
|
*
|
|
291
|
-
*
|
|
302
|
+
* The result is the handler's JSON response. When your `upload.onUpload`
|
|
303
|
+
* returns its own `Response`, name its type: `uploadFile<MyUpload>(…)`. The
|
|
304
|
+
* type isn't checked at run time.
|
|
305
|
+
*
|
|
306
|
+
* @param options `signal` aborts the upload. `params` is added to the upload
|
|
307
|
+
* URL's query string.
|
|
292
308
|
* @throws An `Error` with the handler's error message when the response isn't
|
|
293
309
|
* OK. Rejects as `fetch` does on a network failure or when `signal` aborts.
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* const upload = await uploadFile<MyUpload>(endpoints, file, {
|
|
313
|
+
* params: { sessionId, kind: "headshot" },
|
|
314
|
+
* });
|
|
294
315
|
*/
|
|
295
|
-
declare function uploadFile(endpoints: AgentEndpoints, file: File, { signal }?:
|
|
316
|
+
declare function uploadFile<T = UploadResult>(endpoints: AgentEndpoints, file: File, { signal, params }?: UploadFileOptions): Promise<T>;
|
|
296
317
|
|
|
297
318
|
/** A CSS aspect ratio: a number such as `16 / 9`, or a string such as `"16 / 9"`. */
|
|
298
319
|
type MediaAspectRatio = number | string;
|
|
@@ -376,6 +397,36 @@ declare function MediaViewer({ attachments, index, onIndexChange, onClose }: Med
|
|
|
376
397
|
* (or its id) with an extension from its mime type.
|
|
377
398
|
*/
|
|
378
399
|
declare function attachmentFileName(attachment: AgentAttachment, number?: number): string;
|
|
400
|
+
/**
|
|
401
|
+
* Saves a file in the browser, through a temporary `<a download>`.
|
|
402
|
+
*
|
|
403
|
+
* - With a URL, a URL that isn't `http:`, `https:`, `blob:` or relative (such as
|
|
404
|
+
* `javascript:`) is skipped with a warning.
|
|
405
|
+
* - With a `Blob`, such as an image drawn on a canvas, it makes an object URL
|
|
406
|
+
* and revokes it 40 seconds later.
|
|
407
|
+
*
|
|
408
|
+
* Call it from an event handler. Without a `document`, as in server rendering,
|
|
409
|
+
* it does nothing.
|
|
410
|
+
*/
|
|
411
|
+
declare function saveFile(urlOrBlob: string | Blob, filename: string): void;
|
|
412
|
+
/** A file for `saveFiles`: a URL or a `Blob`, and the name to save it as. */
|
|
413
|
+
interface SaveFilesEntry {
|
|
414
|
+
/** A URL, checked as `saveFile` checks it, or a `Blob`. */
|
|
415
|
+
source: string | Blob;
|
|
416
|
+
filename: string;
|
|
417
|
+
}
|
|
418
|
+
interface SaveFilesOptions {
|
|
419
|
+
/** How long to wait between files. Defaults to 400 ms. */
|
|
420
|
+
delayMs?: number;
|
|
421
|
+
/** Stops before the next file once aborted. */
|
|
422
|
+
signal?: AbortSignal;
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Saves several files one after another with `saveFile`. Browsers block a burst
|
|
426
|
+
* of simultaneous downloads, so each one waits `delayMs` after the last. The
|
|
427
|
+
* promise resolves once the last file is started, or when `signal` aborts.
|
|
428
|
+
*/
|
|
429
|
+
declare function saveFiles(files: readonly SaveFilesEntry[], { delayMs, signal }?: SaveFilesOptions): Promise<void>;
|
|
379
430
|
|
|
380
431
|
interface MarkdownContentProps {
|
|
381
432
|
content: string;
|
|
@@ -392,4 +443,4 @@ interface MarkdownContentProps {
|
|
|
392
443
|
*/
|
|
393
444
|
declare function MarkdownContent({ content }: MarkdownContentProps): react.JSX.Element;
|
|
394
445
|
|
|
395
|
-
export { type AcceptedAttachmentSlot, type AgentAttachment, AgentChat, type AgentChatAttachButton, type AgentChatAttachment, type AgentChatFile, type AgentChatMessage, type AgentChatProps, type AgentChatTurn, type AgentEndpointOptions, type AgentEndpoints, type AgentRunEvent, AgentRunForm, type AgentRunFormProps, type AgentRunState, type AgentRunStatus, type AgentRunStep, type AgentUiMode, type AttachmentReferences, DEFAULT_BASE_PATH, type EndpointRequestOptions, MarkdownContent, type MarkdownContentProps, type MediaAspectRatio, MediaGallery, type MediaGalleryProps, MediaPreview, type MediaPreviewProps, MediaSkeleton, type MediaSkeletonProps, MediaViewer, type MediaViewerProps, SeclaiAgent, type SeclaiAgentProps, type StartRunOptions, type UploadResult, type UseAgentRunOptions, type UseAgentRunResult, attachmentFileName, createEndpoints, fetchAttachmentReferences, uploadFile, useAgentRun };
|
|
446
|
+
export { type AcceptedAttachmentSlot, type AgentAttachment, AgentChat, type AgentChatAttachButton, type AgentChatAttachment, type AgentChatFile, type AgentChatMessage, type AgentChatProps, type AgentChatTurn, type AgentEndpointOptions, type AgentEndpoints, type AgentRunEvent, AgentRunForm, type AgentRunFormProps, type AgentRunState, type AgentRunStatus, type AgentRunStep, type AgentUiMode, type AttachmentReferences, DEFAULT_BASE_PATH, type EndpointRequestOptions, MarkdownContent, type MarkdownContentProps, type MediaAspectRatio, MediaGallery, type MediaGalleryProps, MediaPreview, type MediaPreviewProps, MediaSkeleton, type MediaSkeletonProps, MediaViewer, type MediaViewerProps, type SaveFilesEntry, type SaveFilesOptions, SeclaiAgent, type SeclaiAgentProps, type StartRunOptions, type UploadFileOptions, type UploadResult, type UseAgentRunOptions, type UseAgentRunResult, attachmentFileName, createEndpoints, fetchAttachmentReferences, saveFile, saveFiles, uploadFile, useAgentRun };
|
package/dist/index.d.ts
CHANGED
|
@@ -284,15 +284,36 @@ interface EndpointRequestOptions {
|
|
|
284
284
|
* OK. Rejects as `fetch` does on a network failure or when `signal` aborts.
|
|
285
285
|
*/
|
|
286
286
|
declare function fetchAttachmentReferences(endpoints: AgentEndpoints, { signal }?: EndpointRequestOptions): Promise<AttachmentReferences>;
|
|
287
|
+
/** Options for {@link uploadFile}. */
|
|
288
|
+
interface UploadFileOptions extends EndpointRequestOptions {
|
|
289
|
+
/**
|
|
290
|
+
* Your app's data for this upload, for example the session it belongs to or
|
|
291
|
+
* the file's role. Added to the upload URL's query string, after any query
|
|
292
|
+
* the URL already has; the `upload` handler passes them to `authorizeUpload`
|
|
293
|
+
* and `upload.onUpload`. They are visible in the URL and in server logs, so
|
|
294
|
+
* keep secrets out of them.
|
|
295
|
+
*/
|
|
296
|
+
params?: Record<string, string> | URLSearchParams;
|
|
297
|
+
}
|
|
287
298
|
/**
|
|
288
299
|
* Upload one file through the `upload` handler. Pass the returned `id` to
|
|
289
300
|
* `start({ uploadIds })` from `useAgentRun`.
|
|
290
301
|
*
|
|
291
|
-
*
|
|
302
|
+
* The result is the handler's JSON response. When your `upload.onUpload`
|
|
303
|
+
* returns its own `Response`, name its type: `uploadFile<MyUpload>(…)`. The
|
|
304
|
+
* type isn't checked at run time.
|
|
305
|
+
*
|
|
306
|
+
* @param options `signal` aborts the upload. `params` is added to the upload
|
|
307
|
+
* URL's query string.
|
|
292
308
|
* @throws An `Error` with the handler's error message when the response isn't
|
|
293
309
|
* OK. Rejects as `fetch` does on a network failure or when `signal` aborts.
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* const upload = await uploadFile<MyUpload>(endpoints, file, {
|
|
313
|
+
* params: { sessionId, kind: "headshot" },
|
|
314
|
+
* });
|
|
294
315
|
*/
|
|
295
|
-
declare function uploadFile(endpoints: AgentEndpoints, file: File, { signal }?:
|
|
316
|
+
declare function uploadFile<T = UploadResult>(endpoints: AgentEndpoints, file: File, { signal, params }?: UploadFileOptions): Promise<T>;
|
|
296
317
|
|
|
297
318
|
/** A CSS aspect ratio: a number such as `16 / 9`, or a string such as `"16 / 9"`. */
|
|
298
319
|
type MediaAspectRatio = number | string;
|
|
@@ -376,6 +397,36 @@ declare function MediaViewer({ attachments, index, onIndexChange, onClose }: Med
|
|
|
376
397
|
* (or its id) with an extension from its mime type.
|
|
377
398
|
*/
|
|
378
399
|
declare function attachmentFileName(attachment: AgentAttachment, number?: number): string;
|
|
400
|
+
/**
|
|
401
|
+
* Saves a file in the browser, through a temporary `<a download>`.
|
|
402
|
+
*
|
|
403
|
+
* - With a URL, a URL that isn't `http:`, `https:`, `blob:` or relative (such as
|
|
404
|
+
* `javascript:`) is skipped with a warning.
|
|
405
|
+
* - With a `Blob`, such as an image drawn on a canvas, it makes an object URL
|
|
406
|
+
* and revokes it 40 seconds later.
|
|
407
|
+
*
|
|
408
|
+
* Call it from an event handler. Without a `document`, as in server rendering,
|
|
409
|
+
* it does nothing.
|
|
410
|
+
*/
|
|
411
|
+
declare function saveFile(urlOrBlob: string | Blob, filename: string): void;
|
|
412
|
+
/** A file for `saveFiles`: a URL or a `Blob`, and the name to save it as. */
|
|
413
|
+
interface SaveFilesEntry {
|
|
414
|
+
/** A URL, checked as `saveFile` checks it, or a `Blob`. */
|
|
415
|
+
source: string | Blob;
|
|
416
|
+
filename: string;
|
|
417
|
+
}
|
|
418
|
+
interface SaveFilesOptions {
|
|
419
|
+
/** How long to wait between files. Defaults to 400 ms. */
|
|
420
|
+
delayMs?: number;
|
|
421
|
+
/** Stops before the next file once aborted. */
|
|
422
|
+
signal?: AbortSignal;
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Saves several files one after another with `saveFile`. Browsers block a burst
|
|
426
|
+
* of simultaneous downloads, so each one waits `delayMs` after the last. The
|
|
427
|
+
* promise resolves once the last file is started, or when `signal` aborts.
|
|
428
|
+
*/
|
|
429
|
+
declare function saveFiles(files: readonly SaveFilesEntry[], { delayMs, signal }?: SaveFilesOptions): Promise<void>;
|
|
379
430
|
|
|
380
431
|
interface MarkdownContentProps {
|
|
381
432
|
content: string;
|
|
@@ -392,4 +443,4 @@ interface MarkdownContentProps {
|
|
|
392
443
|
*/
|
|
393
444
|
declare function MarkdownContent({ content }: MarkdownContentProps): react.JSX.Element;
|
|
394
445
|
|
|
395
|
-
export { type AcceptedAttachmentSlot, type AgentAttachment, AgentChat, type AgentChatAttachButton, type AgentChatAttachment, type AgentChatFile, type AgentChatMessage, type AgentChatProps, type AgentChatTurn, type AgentEndpointOptions, type AgentEndpoints, type AgentRunEvent, AgentRunForm, type AgentRunFormProps, type AgentRunState, type AgentRunStatus, type AgentRunStep, type AgentUiMode, type AttachmentReferences, DEFAULT_BASE_PATH, type EndpointRequestOptions, MarkdownContent, type MarkdownContentProps, type MediaAspectRatio, MediaGallery, type MediaGalleryProps, MediaPreview, type MediaPreviewProps, MediaSkeleton, type MediaSkeletonProps, MediaViewer, type MediaViewerProps, SeclaiAgent, type SeclaiAgentProps, type StartRunOptions, type UploadResult, type UseAgentRunOptions, type UseAgentRunResult, attachmentFileName, createEndpoints, fetchAttachmentReferences, uploadFile, useAgentRun };
|
|
446
|
+
export { type AcceptedAttachmentSlot, type AgentAttachment, AgentChat, type AgentChatAttachButton, type AgentChatAttachment, type AgentChatFile, type AgentChatMessage, type AgentChatProps, type AgentChatTurn, type AgentEndpointOptions, type AgentEndpoints, type AgentRunEvent, AgentRunForm, type AgentRunFormProps, type AgentRunState, type AgentRunStatus, type AgentRunStep, type AgentUiMode, type AttachmentReferences, DEFAULT_BASE_PATH, type EndpointRequestOptions, MarkdownContent, type MarkdownContentProps, type MediaAspectRatio, MediaGallery, type MediaGalleryProps, MediaPreview, type MediaPreviewProps, MediaSkeleton, type MediaSkeletonProps, MediaViewer, type MediaViewerProps, type SaveFilesEntry, type SaveFilesOptions, SeclaiAgent, type SeclaiAgentProps, type StartRunOptions, type UploadFileOptions, type UploadResult, type UseAgentRunOptions, type UseAgentRunResult, attachmentFileName, createEndpoints, fetchAttachmentReferences, saveFile, saveFiles, uploadFile, useAgentRun };
|
package/dist/index.js
CHANGED
|
@@ -46,10 +46,20 @@ async function readJson(res) {
|
|
|
46
46
|
async function fetchAttachmentReferences(endpoints, { signal } = {}) {
|
|
47
47
|
return readJson(await fetch(endpoints.attachmentReferences, { signal }));
|
|
48
48
|
}
|
|
49
|
-
async function uploadFile(endpoints, file, { signal } = {}) {
|
|
49
|
+
async function uploadFile(endpoints, file, { signal, params } = {}) {
|
|
50
50
|
const form = new FormData();
|
|
51
51
|
form.set("file", file);
|
|
52
|
-
|
|
52
|
+
const url = params ? withQuery(endpoints.upload, params) : endpoints.upload;
|
|
53
|
+
return readJson(await fetch(url, { method: "POST", body: form, signal }));
|
|
54
|
+
}
|
|
55
|
+
function withQuery(url, params) {
|
|
56
|
+
const query = new URLSearchParams(params).toString();
|
|
57
|
+
if (!query) return url;
|
|
58
|
+
const hashAt = url.indexOf("#");
|
|
59
|
+
const base = hashAt < 0 ? url : url.slice(0, hashAt);
|
|
60
|
+
const hash = hashAt < 0 ? "" : url.slice(hashAt);
|
|
61
|
+
const separator = !base.includes("?") ? "?" : base.endsWith("?") || base.endsWith("&") ? "" : "&";
|
|
62
|
+
return `${base}${separator}${query}${hash}`;
|
|
53
63
|
}
|
|
54
64
|
|
|
55
65
|
// src/attachments.ts
|
|
@@ -265,11 +275,9 @@ function isSafeUrl(url) {
|
|
|
265
275
|
return false;
|
|
266
276
|
}
|
|
267
277
|
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
return;
|
|
272
|
-
}
|
|
278
|
+
var SAVE_FILES_DELAY_MS = 400;
|
|
279
|
+
var REVOKE_OBJECT_URL_DELAY_MS = 4e4;
|
|
280
|
+
function clickDownloadLink(url, filename) {
|
|
273
281
|
const link = document.createElement("a");
|
|
274
282
|
link.href = url;
|
|
275
283
|
link.download = filename;
|
|
@@ -279,14 +287,39 @@ function triggerDownload(url, filename) {
|
|
|
279
287
|
link.click();
|
|
280
288
|
link.remove();
|
|
281
289
|
}
|
|
282
|
-
|
|
290
|
+
function saveFile(urlOrBlob, filename) {
|
|
291
|
+
if (typeof document === "undefined") return;
|
|
292
|
+
if (typeof urlOrBlob !== "string") {
|
|
293
|
+
const url = URL.createObjectURL(urlOrBlob);
|
|
294
|
+
clickDownloadLink(url, filename);
|
|
295
|
+
setTimeout(() => URL.revokeObjectURL(url), REVOKE_OBJECT_URL_DELAY_MS);
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
if (!isSafeUrl(urlOrBlob)) {
|
|
299
|
+
console.warn(`seclai: not downloading ${filename}, its URL isn't http(s), blob or relative.`);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
clickDownloadLink(urlOrBlob, filename);
|
|
303
|
+
}
|
|
304
|
+
async function saveFiles(files, { delayMs = SAVE_FILES_DELAY_MS, signal } = {}) {
|
|
283
305
|
for (const [i, file] of files.entries()) {
|
|
284
306
|
if (signal?.aborted) return;
|
|
285
|
-
if (i > 0) await
|
|
307
|
+
if (i > 0) await wait(delayMs, signal);
|
|
286
308
|
if (signal?.aborted) return;
|
|
287
|
-
|
|
309
|
+
saveFile(file.source, file.filename);
|
|
288
310
|
}
|
|
289
311
|
}
|
|
312
|
+
function wait(ms, signal) {
|
|
313
|
+
return new Promise((resolve) => {
|
|
314
|
+
const done = () => {
|
|
315
|
+
clearTimeout(timer);
|
|
316
|
+
signal?.removeEventListener("abort", done);
|
|
317
|
+
resolve();
|
|
318
|
+
};
|
|
319
|
+
const timer = setTimeout(done, ms);
|
|
320
|
+
signal?.addEventListener("abort", done, { once: true });
|
|
321
|
+
});
|
|
322
|
+
}
|
|
290
323
|
|
|
291
324
|
// src/placeholders/MediaPreview.tsx
|
|
292
325
|
import { Fragment as Fragment2, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
@@ -501,7 +534,7 @@ function MediaViewer({ attachments, index, onIndexChange, onClose }) {
|
|
|
501
534
|
|
|
502
535
|
// src/media/MediaGallery.tsx
|
|
503
536
|
import { jsx as jsx5, jsxs as jsxs5 } from "react/jsx-runtime";
|
|
504
|
-
var DOWNLOAD_ALL_DELAY_MS =
|
|
537
|
+
var DOWNLOAD_ALL_DELAY_MS = SAVE_FILES_DELAY_MS;
|
|
505
538
|
function MediaGallery({
|
|
506
539
|
attachments,
|
|
507
540
|
aspectRatio,
|
|
@@ -521,10 +554,9 @@ function MediaGallery({
|
|
|
521
554
|
abort.current = controller;
|
|
522
555
|
setDownloading(true);
|
|
523
556
|
try {
|
|
524
|
-
await
|
|
525
|
-
images.map((image, i) => ({
|
|
526
|
-
DOWNLOAD_ALL_DELAY_MS,
|
|
527
|
-
controller.signal
|
|
557
|
+
await saveFiles(
|
|
558
|
+
images.map((image, i) => ({ source: image.url, filename: attachmentFileName(image, i + 1) })),
|
|
559
|
+
{ delayMs: DOWNLOAD_ALL_DELAY_MS, signal: controller.signal }
|
|
528
560
|
);
|
|
529
561
|
} finally {
|
|
530
562
|
if (!controller.signal.aborted) setDownloading(false);
|
|
@@ -1306,6 +1338,8 @@ export {
|
|
|
1306
1338
|
attachmentFileName,
|
|
1307
1339
|
createEndpoints,
|
|
1308
1340
|
fetchAttachmentReferences,
|
|
1341
|
+
saveFile,
|
|
1342
|
+
saveFiles,
|
|
1309
1343
|
uploadFile,
|
|
1310
1344
|
useAgentRun
|
|
1311
1345
|
};
|