@seclai/handlers 0.0.20 → 0.0.22
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 +22 -8
- package/dist/index.d.ts +16 -7
- package/dist/index.js +50 -8
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -52,6 +52,10 @@ export const ALL: APIRoute = ({ request }) => seclai.handle(request);
|
|
|
52
52
|
|
|
53
53
|
`run` needs `input`, `uploadIds`, or both. `sessionId` reaches the agent as `metadata.session_id`, which is what chat agents key a conversation on.
|
|
54
54
|
|
|
55
|
+
The API refuses a run whose metadata carries a custom key the agent's trigger doesn't declare, so an agent that gets `sessionId` must declare `session_id` among its trigger's metadata keys (in Seclai, or with the SDK's `setMetadataKeys`). Otherwise every run with a `sessionId` gets a 400.
|
|
56
|
+
|
|
57
|
+
`download` forwards a single byte range from the browser's `Range` header, so audio and video can seek and Safari can play video: the API's `206 Partial Content`, `Content-Range` and `Accept-Ranges` come back as they are, and a range the API can't satisfy gets its 416 with the API's `Content-Range`. Several ranges, a unit other than `bytes`, a malformed range, or a `Range` sent with `If-Range` are ignored, and the whole file is served.
|
|
58
|
+
|
|
55
59
|
### Attachment references on the server
|
|
56
60
|
|
|
57
61
|
A server-rendered page can read the agent's accepted files directly, for example to set a composer's file limit and `accept` types in a loader:
|
|
@@ -71,7 +75,7 @@ The `attachmentReferences` handler always looks up afresh, and a successful look
|
|
|
71
75
|
|---|---|---|
|
|
72
76
|
| `apiKey` | Required | Your Seclai API key |
|
|
73
77
|
| `agentId` | Required | The agent to run |
|
|
74
|
-
| `apiVersion` | `2026-
|
|
78
|
+
| `apiVersion` | `2026-11-19` | Dated API version. Must be `2026-10-16` or later. See [API versions](#api-versions) |
|
|
75
79
|
| `runTimeoutMs` | 11 minutes | How long a streaming run may take |
|
|
76
80
|
| `onEvent` | None | `(event, ctx)`: called with each run event before it is relayed. See [Run callbacks](#run-callbacks) |
|
|
77
81
|
| `onRunEnd` | None | `(outcome, ctx)`: called once per run with how it ended. See [Run callbacks](#run-callbacks) |
|
|
@@ -83,6 +87,15 @@ The `attachmentReferences` handler always looks up afresh, and a successful look
|
|
|
83
87
|
| `upload` | None | Size limit, content-type check and hook for `upload`, below |
|
|
84
88
|
| `logger` | None | `{ debug, info, warn, error }`, such as `console`: gets a line for each step of a run, upload and download. See [Logging](#logging) |
|
|
85
89
|
|
|
90
|
+
### API versions
|
|
91
|
+
|
|
92
|
+
The default is a dated version, not the SDK's `Latest`, so a newer `@seclai/sdk` doesn't change the API version the handlers send. `2026-11-19` differs from the earlier default, `2026-10-28`, in two ways the handlers' users can see:
|
|
93
|
+
|
|
94
|
+
- A run cancelled on Seclai ends with status `cancelled`, not `failed`. `onRunEnd` and `@seclai/react` report it as `cancelled`.
|
|
95
|
+
- A run of an agent whose definition doesn't validate is refused with a 409, and no run is created. Before, the run started and failed with the validation error. `run` passes the 409 and the API's message on to the browser.
|
|
96
|
+
|
|
97
|
+
Pass `apiVersion: "2026-10-28"` to keep the old behaviour.
|
|
98
|
+
|
|
86
99
|
### Upload options
|
|
87
100
|
|
|
88
101
|
```ts
|
|
@@ -208,7 +221,7 @@ The context's type is inferred from the `context` option, or set as the type par
|
|
|
208
221
|
|
|
209
222
|
- If the API rejects a run up front (a file the agent doesn't accept, no credits), `run` returns a real HTTP error with `{ error, status }`, not a stream.
|
|
210
223
|
- If the stream fails after it has started, it ends with an `error` event carrying `{ error }`.
|
|
211
|
-
-
|
|
224
|
+
- By default, when the browser disconnects, the upstream connection closes. **A run that was sent may keep going on Seclai** and keep using credits.
|
|
212
225
|
|
|
213
226
|
## Run callbacks
|
|
214
227
|
|
|
@@ -247,22 +260,23 @@ const seclai = createSeclaiHandlers({
|
|
|
247
260
|
|---|---|
|
|
248
261
|
| `runId` | From `init` or `done`. `null` if the stream ended before either |
|
|
249
262
|
| `status` | `completed`, `failed` or `cancelled` |
|
|
263
|
+
| `reason` | Set when `status` is `cancelled`: `not-sent` when the browser left before the run was sent, so nothing reached Seclai; `stopped` when it left once the handler had started sending the run, so the run may have reached Seclai and may still finish there and be billed; `run-cancelled` when the run was cancelled on Seclai and is over |
|
|
250
264
|
| `output` | The run's output when it completed. Otherwise the text streamed in `stream_token` events so far |
|
|
251
265
|
| `attachments` | Files from `done`, as `{ id, name, mime }`. Fetch them with `download` |
|
|
252
266
|
| `error` | Why the run failed, or `null` |
|
|
253
267
|
|
|
254
268
|
| The run stream... | `status` | `error` |
|
|
255
269
|
|---|---|---|
|
|
256
|
-
| Sends `done` | `completed`,
|
|
270
|
+
| Sends `done` | `completed`, `failed` if the run failed, or `cancelled` if it was cancelled on Seclai | The last attempt's error, or `null` |
|
|
257
271
|
| Sends `error`, or the upstream connection fails | `failed` | The event's `error` |
|
|
258
272
|
| Sends `timeout`: the API stopped streaming before the run ended | `failed` | `Stopped waiting for the run. It may still finish on Seclai.` |
|
|
259
273
|
| Ends with none of these | `failed` | `The stream ended without a result.` |
|
|
260
274
|
| Is cancelled by the browser | `cancelled`, unless `keepReadingAfterDisconnect` is on | `null` |
|
|
261
275
|
| Is refused by the API before it streams, for example for no credits or a file the agent doesn't accept, or fails before its first event because the API can't be reached or `runTimeoutMs` runs out | `failed` | The `error` the browser gets in the error response |
|
|
262
276
|
|
|
263
|
-
`cancelled` means the browser left
|
|
277
|
+
`cancelled` means the browser left (`reason: "not-sent"` or `"stopped"`), or that the run was cancelled on Seclai (`reason: "run-cancelled"`). If the upstream connection fails or ends after the browser has left, that is reported as `cancelled` too, but a refusal from the API (an HTTP error status), or an `error` or `timeout` event, is still `failed`. If the browser leaves while a callback is handling `done`, `error` or `timeout`, that event's outcome is reported.
|
|
264
278
|
|
|
265
|
-
When the API refuses a run up front, or the run fails before its first event, `onRunEnd` is awaited before the handler returns the error response, so what it stores is in place by the time the browser sees the error. The outcome has `runId: null`, empty `output` and no `attachments`, and `onEvent` isn't called. After a timeout, the run may still have started and may still finish on Seclai, so the error says so: `Timed out waiting for the Seclai API. The run may still finish on Seclai.` If the browser left while the stream was connecting, the outcome is `cancelled` instead, with `error: null`, unless `keepReadingAfterDisconnect` is on.
|
|
279
|
+
When the API refuses a run up front, or the run fails before its first event, `onRunEnd` is awaited before the handler returns the error response, so what it stores is in place by the time the browser sees the error. The outcome has `runId: null`, empty `output` and no `attachments`, and `onEvent` isn't called. After a timeout, the run may still have started and may still finish on Seclai, so the error says so: `Timed out waiting for the Seclai API. The run may still finish on Seclai.` If the browser left while the stream was connecting and the API hadn't refused the run, the outcome is `cancelled` instead, with `error: null`, unless `keepReadingAfterDisconnect` is on. If `onRunEnd` throws or rejects here, the error is logged like any other callback error (see below), and the browser still gets the original error response. A refusal from `authorizeRun`, `context` or the body check is your app's own decision, before the run is sent to the API, and doesn't call `onRunEnd`.
|
|
266
280
|
|
|
267
281
|
When `runTimeoutMs` runs out, the stream gets an `error` event, `Timed out waiting for the agent run to finish.`, not `timeout`. The time spent awaiting your callbacks counts against `runTimeoutMs`.
|
|
268
282
|
|
|
@@ -270,7 +284,7 @@ If `onEvent` or `onRunEnd` throws or rejects, the handler logs the error, with `
|
|
|
270
284
|
|
|
271
285
|
### Saving replies after a disconnect
|
|
272
286
|
|
|
273
|
-
By default, when the browser leaves (it closes the tab, navigates to another chat, or aborts the request), the handler stops reading the run and `onRunEnd` reports `cancelled` with the text streamed so far.
|
|
287
|
+
By default, when the browser leaves (it closes the tab, navigates to another chat, or aborts the request), the handler stops reading the run and `onRunEnd` reports `cancelled` with the text streamed so far. A run that was already sent may still finish on Seclai and be billed, and a chat agent's memory keeps its reply, so an app that saves replies ends up with a turn the agent remembers but the app never showed.
|
|
274
288
|
|
|
275
289
|
Set `keepReadingAfterDisconnect: true` to keep reading the run in the background instead:
|
|
276
290
|
|
|
@@ -283,7 +297,7 @@ const seclai = createSeclaiHandlers({
|
|
|
283
297
|
```
|
|
284
298
|
|
|
285
299
|
- **Callbacks.** The browser stops getting events, but `onEvent` still gets each one, and `onRunEnd` gets the run's real outcome with its output and files.
|
|
286
|
-
- **Outcomes.** A lost connection or a quiet end is reported as `failed`, since the browser leaving no longer stops the upstream, and `cancelled`
|
|
300
|
+
- **Outcomes.** A lost connection or a quiet end is reported as `failed`, since the browser leaving no longer stops the upstream, and `cancelled` means only that the run was cancelled on Seclai. A callback that throws during the background reading is `failed` too.
|
|
287
301
|
- **Unread responses.** The handler also watches the request's signal, so it reads on even when the framework drops the response unread after the browser aborts. React Router does this when the browser leaves before the response starts.
|
|
288
302
|
- **Limits.** Reading the upstream stops at `runTimeoutMs` at the latest, and stops at once if a callback throws. A callback that never settles isn't bounded by it, so put a timeout on slow writes.
|
|
289
303
|
- **Show the reply when the user comes back.** For example, reload the conversation when it ends with a message that has no reply yet.
|
|
@@ -333,7 +347,7 @@ run done { run_id: '3f2b8c1e-…', session_id: 'chat-42', status: 'completed', a
|
|
|
333
347
|
|
|
334
348
|
| Level | Lines |
|
|
335
349
|
|---|---|
|
|
336
|
-
| `info` | `run starting`, `stream open` (`ms` to the first event), `run started` (the `run_id` from `init`), `step` for each `step_progress` (once while the same step and message repeat), `first token`, `streaming` (every 5 seconds while tokens arrive), `run done`, `browser disconnected; the run continues on Seclai` (or, with `keepReadingAfterDisconnect`, `browser left; reading the rest of the run in the background`, and the run's end when it comes), `upload received`, `uploaded`, `download served`, and `run refused`, `upload refused` or `download refused` (with the `status`) when an [access-control hook](#access-control), `maxBytes` or `checkContentType` turns a request away |
|
|
350
|
+
| `info` | `run starting`, `stream open` (`ms` to the first event), `run started` (the `run_id` from `init`), `step` for each `step_progress` (once while the same step and message repeat), `first token`, `streaming` (every 5 seconds while tokens arrive), `run done`, `browser disconnected; the run continues on Seclai` (or, with `keepReadingAfterDisconnect`, `browser left; reading the rest of the run in the background`, and the run's end when it comes), `browser left while the run was connecting` and `browser left before the run was sent` (before the stream opened), `upload received`, `uploaded`, `download served`, `download cancelled` (the browser left before the API answered), and `run refused`, `upload refused` or `download refused` (with the `status`) when an [access-control hook](#access-control), `maxBytes` or `checkContentType` turns a request away |
|
|
337
351
|
| `warn` | `run failed` (a `done` with status `failed`), `stream ended without a result`, and a warning when a completed run streamed no tokens |
|
|
338
352
|
| `error` | `run rejected` (the API refused the run up front, with `status`, `upstream_status` and the API's `error`), `stream error` (with the real `cause` when the connection failed), `run timed out`, `run failed: a callback threw`, `upload failed`, `download failed`, callback errors from `onEvent` and `onRunEnd`, and errors from the access-control hooks, `context` and `onUpload` |
|
|
339
353
|
| `debug` | Every event, each token (its length, not its text) and each keepalive |
|
package/dist/index.d.ts
CHANGED
|
@@ -54,11 +54,19 @@ interface RunOutcomeAttachment {
|
|
|
54
54
|
interface RunOutcome {
|
|
55
55
|
/** From `init` or `done`. `null` if the stream ended before either. */
|
|
56
56
|
runId: string | null;
|
|
57
|
+
status: RunOutcomeStatus;
|
|
57
58
|
/**
|
|
58
|
-
* `
|
|
59
|
-
*
|
|
59
|
+
* Set when `status` is `cancelled`:
|
|
60
|
+
*
|
|
61
|
+
* - `not-sent`: the browser left before the run was sent. Nothing reached
|
|
62
|
+
* Seclai.
|
|
63
|
+
* - `stopped`: the browser left once the handler had started sending the
|
|
64
|
+
* run, so it stopped reading. The run may have reached Seclai, and may still finish
|
|
65
|
+
* there and be billed.
|
|
66
|
+
* - `run-cancelled`: the run was cancelled on Seclai and is over. Its `done`
|
|
67
|
+
* has status `cancelled`, from API version `2026-11-08`.
|
|
60
68
|
*/
|
|
61
|
-
|
|
69
|
+
reason?: "not-sent" | "stopped" | "run-cancelled";
|
|
62
70
|
/** The run's output when it completed, otherwise the text streamed so far. */
|
|
63
71
|
output: string;
|
|
64
72
|
/** Files from `done`. Empty unless the run sent `done`. */
|
|
@@ -78,7 +86,7 @@ declare const MIN_API_VERSION: "2026-10-16";
|
|
|
78
86
|
* A dated constant, not `SeclaiApiVersion.Latest`, which moves with SDK upgrades
|
|
79
87
|
* and could change response shapes underneath a deployed app.
|
|
80
88
|
*/
|
|
81
|
-
declare const DEFAULT_API_VERSION: "2026-
|
|
89
|
+
declare const DEFAULT_API_VERSION: "2026-11-19";
|
|
82
90
|
/**
|
|
83
91
|
* How long the agent's attachment references are reused by
|
|
84
92
|
* `getAttachmentReferences` and `upload.checkContentType`, which share them.
|
|
@@ -161,14 +169,15 @@ interface SeclaiHandlersOptions<Ctx = undefined> {
|
|
|
161
169
|
* it streams, or it fails before its first event (the API can't be reached,
|
|
162
170
|
* or `runTimeoutMs` runs out), and awaited before the error response is
|
|
163
171
|
* returned. The status is `cancelled` instead if the browser left while the
|
|
164
|
-
* stream was connecting
|
|
172
|
+
* stream was connecting and the API hadn't refused the run, unless
|
|
173
|
+
* `keepReadingAfterDisconnect` is on. A throw there is only logged. Not called when
|
|
165
174
|
* `authorizeRun`, `context` or the body check refuses the run.
|
|
166
175
|
*/
|
|
167
176
|
onRunEnd?: RunEndCallback<Ctx>;
|
|
168
177
|
/**
|
|
169
178
|
* What happens to a run when the browser disconnects: closes the tab, moves
|
|
170
|
-
* to another page, or aborts the request.
|
|
171
|
-
* Seclai and
|
|
179
|
+
* to another page, or aborts the request. A run that was already sent may
|
|
180
|
+
* carry on at Seclai and use credits.
|
|
172
181
|
*
|
|
173
182
|
* - `false` (default): the handler stops reading it. `onRunEnd` reports
|
|
174
183
|
* `cancelled` with what had streamed so far.
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/handlers.ts
|
|
2
2
|
import {
|
|
3
3
|
Seclai,
|
|
4
|
+
SeclaiAPIStatusError as SeclaiAPIStatusError2,
|
|
4
5
|
SeclaiApiVersion
|
|
5
6
|
} from "@seclai/sdk";
|
|
6
7
|
|
|
@@ -370,7 +371,7 @@ function reportCallbackError(log, err) {
|
|
|
370
371
|
async function reportRefusedRun(callbacks, ctx, how) {
|
|
371
372
|
const { onRunEnd, log } = callbacks;
|
|
372
373
|
if (!onRunEnd) return;
|
|
373
|
-
const outcome = "error" in how ? { runId: null, status: "failed", output: "", attachments: [], error: how.error } : { runId: null, status: "cancelled", output: "", attachments: [], error: null };
|
|
374
|
+
const outcome = "error" in how ? { runId: null, status: "failed", output: "", attachments: [], error: how.error } : { runId: null, status: "cancelled", reason: how.cancelled, output: "", attachments: [], error: null };
|
|
374
375
|
try {
|
|
375
376
|
await onRunEnd(outcome, ctx);
|
|
376
377
|
} catch (err) {
|
|
@@ -396,7 +397,7 @@ function createRunObserver(callbacks, ctx, { keepReading = false } = {}) {
|
|
|
396
397
|
await onRunEnd?.(o, ctx);
|
|
397
398
|
};
|
|
398
399
|
const failed = (error) => outcome({ status: "failed", output: text, error });
|
|
399
|
-
const cancelled = () => outcome({ status: "cancelled", output: text, error: null });
|
|
400
|
+
const cancelled = () => outcome({ status: "cancelled", reason: "stopped", output: text, error: null });
|
|
400
401
|
const record = (event, source) => {
|
|
401
402
|
note(event, source);
|
|
402
403
|
if (terminal && !terminalEvent) terminalEvent = { event, source };
|
|
@@ -413,6 +414,10 @@ function createRunObserver(callbacks, ctx, { keepReading = false } = {}) {
|
|
|
413
414
|
const run = payload;
|
|
414
415
|
if (typeof run.run_id === "string") runId = run.run_id;
|
|
415
416
|
const attachments = toAttachments(run.attachments);
|
|
417
|
+
if (run.status === "cancelled") {
|
|
418
|
+
terminal = outcome({ status: "cancelled", reason: "run-cancelled", output: text, attachments, error: null });
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
416
421
|
terminal = run.status === "failed" ? outcome({
|
|
417
422
|
status: "failed",
|
|
418
423
|
output: text,
|
|
@@ -580,7 +585,7 @@ ${message}`;
|
|
|
580
585
|
return;
|
|
581
586
|
}
|
|
582
587
|
log.info("run done", fields);
|
|
583
|
-
if (tokens === 0) log.warn(ZERO_TOKENS_MESSAGE, ctx());
|
|
588
|
+
if (tokens === 0 && payload.status !== "cancelled") log.warn(ZERO_TOKENS_MESSAGE, ctx());
|
|
584
589
|
return;
|
|
585
590
|
}
|
|
586
591
|
case "error":
|
|
@@ -642,7 +647,7 @@ ${message}`;
|
|
|
642
647
|
|
|
643
648
|
// src/handlers.ts
|
|
644
649
|
var MIN_API_VERSION = SeclaiApiVersion.V2026_10_16;
|
|
645
|
-
var DEFAULT_API_VERSION = SeclaiApiVersion.
|
|
650
|
+
var DEFAULT_API_VERSION = SeclaiApiVersion.V2026_11_19;
|
|
646
651
|
var DEFAULT_RUN_TIMEOUT_MS = 11 * 60 * 1e3;
|
|
647
652
|
var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
648
653
|
var ATTACHMENT_ID = /^[A-Za-z0-9_-]+={0,2}$/;
|
|
@@ -676,6 +681,23 @@ async function readLimitedForm(req, limit) {
|
|
|
676
681
|
throw err;
|
|
677
682
|
}
|
|
678
683
|
}
|
|
684
|
+
var SINGLE_BYTE_RANGE = /^bytes=(\d*)-(\d*)$/i;
|
|
685
|
+
function requestedRange(req) {
|
|
686
|
+
const header = req.headers.get("range");
|
|
687
|
+
if (!header || req.headers.has("if-range")) return void 0;
|
|
688
|
+
const match = SINGLE_BYTE_RANGE.exec(header.trim());
|
|
689
|
+
if (!match) return void 0;
|
|
690
|
+
const [, first = "", last = ""] = match;
|
|
691
|
+
const start = first ? Number(first) : void 0;
|
|
692
|
+
const end = last ? Number(last) : void 0;
|
|
693
|
+
if (start !== void 0 && !Number.isSafeInteger(start)) return void 0;
|
|
694
|
+
if (end !== void 0 && !Number.isSafeInteger(end)) return void 0;
|
|
695
|
+
if (start === void 0) {
|
|
696
|
+
return end !== void 0 && end >= 1 ? { rangeSuffix: end } : void 0;
|
|
697
|
+
}
|
|
698
|
+
if (end === void 0) return { rangeStart: start };
|
|
699
|
+
return end >= start ? { rangeStart: start, rangeEnd: end } : void 0;
|
|
700
|
+
}
|
|
679
701
|
function uploadParams(req) {
|
|
680
702
|
const params = /* @__PURE__ */ Object.create(null);
|
|
681
703
|
for (const [key, value] of new URL(req.url).searchParams) params[key] = value;
|
|
@@ -970,6 +992,7 @@ function createSeclaiHandlers(options) {
|
|
|
970
992
|
// failure after it is a real one.
|
|
971
993
|
disconnected: () => !keepReading && (req.signal?.aborted ?? false)
|
|
972
994
|
});
|
|
995
|
+
const sent = !upstream.signal.aborted;
|
|
973
996
|
const events = client.runStreamingAgent(
|
|
974
997
|
agentId,
|
|
975
998
|
{
|
|
@@ -999,14 +1022,16 @@ function createSeclaiHandlers(options) {
|
|
|
999
1022
|
);
|
|
1000
1023
|
} catch (err) {
|
|
1001
1024
|
const error = runErrorBody(err);
|
|
1002
|
-
const browserLeft = upstream.signal.aborted;
|
|
1025
|
+
const browserLeft = upstream.signal.aborted && !(err instanceof SeclaiAPIStatusError2);
|
|
1003
1026
|
await reportRefusedRun(
|
|
1004
1027
|
{ onRunEnd, log },
|
|
1005
1028
|
{ body, request: req, context: runContext },
|
|
1006
|
-
browserLeft ? { cancelled:
|
|
1029
|
+
browserLeft ? { cancelled: sent ? "stopped" : "not-sent" } : { error: error.error }
|
|
1007
1030
|
);
|
|
1008
|
-
if (browserLeft)
|
|
1009
|
-
|
|
1031
|
+
if (browserLeft) {
|
|
1032
|
+
const line = sent ? "browser left while the run was connecting" : "browser left before the run was sent";
|
|
1033
|
+
logger?.info(line, { session_id: body.sessionId });
|
|
1034
|
+
} else log?.rejected(err, error.status);
|
|
1010
1035
|
return json(error, error.status);
|
|
1011
1036
|
}
|
|
1012
1037
|
},
|
|
@@ -1042,9 +1067,12 @@ function createSeclaiHandlers(options) {
|
|
|
1042
1067
|
}
|
|
1043
1068
|
if (allowed !== true) return refused("download", jsonError(404, "Not found."), ids);
|
|
1044
1069
|
}
|
|
1070
|
+
const range = requestedRange(req);
|
|
1071
|
+
const rangeField = range ? { range: req.headers.get("range") } : {};
|
|
1045
1072
|
try {
|
|
1046
1073
|
const upstream = await client.downloadAgentRunAttachment(runId, attachmentId, {
|
|
1047
1074
|
...name ? { downloadName: name } : {},
|
|
1075
|
+
...range,
|
|
1048
1076
|
signal: req.signal
|
|
1049
1077
|
});
|
|
1050
1078
|
const contentType = upstream.headers.get("content-type") ?? "application/octet-stream";
|
|
@@ -1059,21 +1087,35 @@ function createSeclaiHandlers(options) {
|
|
|
1059
1087
|
if (length && !upstream.headers.has("content-encoding")) {
|
|
1060
1088
|
headers.set("content-length", length);
|
|
1061
1089
|
}
|
|
1090
|
+
for (const header of ["content-range", "accept-ranges"]) {
|
|
1091
|
+
const value = upstream.headers.get(header);
|
|
1092
|
+
if (value) headers.set(header, value);
|
|
1093
|
+
}
|
|
1062
1094
|
if (!contentType.toLowerCase().startsWith("application/pdf")) {
|
|
1063
1095
|
headers.set("content-security-policy", "sandbox; default-src 'none'");
|
|
1064
1096
|
}
|
|
1065
1097
|
logger?.info("download served", {
|
|
1066
1098
|
run_id: runId,
|
|
1067
1099
|
attachment_id: attachmentId,
|
|
1100
|
+
...rangeField,
|
|
1068
1101
|
status: upstream.status,
|
|
1069
1102
|
content_type: contentType
|
|
1070
1103
|
});
|
|
1071
1104
|
return new Response(upstream.body, { status: upstream.status, headers });
|
|
1072
1105
|
} catch (err) {
|
|
1073
1106
|
const res = toErrorResponse(err);
|
|
1107
|
+
if (req.signal?.aborted && !(err instanceof SeclaiAPIStatusError2)) {
|
|
1108
|
+
logger?.info("download cancelled", { run_id: runId, attachment_id: attachmentId, ...rangeField });
|
|
1109
|
+
return res;
|
|
1110
|
+
}
|
|
1111
|
+
if (err instanceof SeclaiAPIStatusError2 && err.statusCode === 416) {
|
|
1112
|
+
const contentRange = err.headers.get("content-range");
|
|
1113
|
+
if (contentRange) res.headers.set("content-range", contentRange);
|
|
1114
|
+
}
|
|
1074
1115
|
logger?.error("download failed", {
|
|
1075
1116
|
run_id: runId,
|
|
1076
1117
|
attachment_id: attachmentId,
|
|
1118
|
+
...rangeField,
|
|
1077
1119
|
status: res.status,
|
|
1078
1120
|
...errorLogFields(err)
|
|
1079
1121
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seclai/handlers",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.22",
|
|
4
4
|
"description": "Framework-free server handlers for running Seclai agents from a web app: upload, run with SSE relay, and download proxy.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"test": "vitest run"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@seclai/sdk": "^1.
|
|
37
|
+
"@seclai/sdk": "^1.11.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"tsup": "^8.5.0",
|