@seclai/handlers 0.0.2 → 0.0.4
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 +105 -1
- package/dist/index.cjs +378 -33
- package/dist/index.d.cts +115 -1
- package/dist/index.d.ts +115 -1
- package/dist/index.js +381 -34
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
// src/handlers.ts
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
Seclai,
|
|
4
|
+
SeclaiApiVersion
|
|
5
|
+
} from "@seclai/sdk";
|
|
3
6
|
|
|
4
7
|
// src/errors.ts
|
|
5
8
|
import {
|
|
@@ -85,52 +88,296 @@ data: ${JSON.stringify(data)}
|
|
|
85
88
|
|
|
86
89
|
`);
|
|
87
90
|
}
|
|
88
|
-
|
|
91
|
+
var OBSERVER_ERROR_MESSAGE = "The server could not process the run.";
|
|
92
|
+
function relayRunEvents(first, events, abort, observer, options = {}) {
|
|
93
|
+
const keepReading = !!options.keepReadingAfterDisconnect && !!observer;
|
|
94
|
+
let pending = first;
|
|
89
95
|
let closed = false;
|
|
96
|
+
let cancelled = false;
|
|
97
|
+
let closeReport;
|
|
98
|
+
let inflight;
|
|
90
99
|
let keepalive;
|
|
100
|
+
let ctrl;
|
|
101
|
+
let draining = false;
|
|
102
|
+
let pulling;
|
|
91
103
|
const finish = () => {
|
|
92
104
|
closed = true;
|
|
93
105
|
clearInterval(keepalive);
|
|
94
106
|
};
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
107
|
+
const stopUpstream = () => {
|
|
108
|
+
abort();
|
|
109
|
+
void events.return().catch(() => {
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
const call = async (hook) => {
|
|
113
|
+
try {
|
|
114
|
+
await hook();
|
|
115
|
+
return true;
|
|
116
|
+
} catch (err) {
|
|
117
|
+
try {
|
|
118
|
+
observer?.onError(err);
|
|
119
|
+
} catch {
|
|
120
|
+
}
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
const reportClose = (reason) => {
|
|
125
|
+
if (!observer) return Promise.resolve(true);
|
|
126
|
+
return closeReport ??= call(() => observer.onClose(reason));
|
|
127
|
+
};
|
|
128
|
+
const end = (controller, last) => {
|
|
129
|
+
if (cancelled) return;
|
|
130
|
+
if (last) controller.enqueue(last);
|
|
131
|
+
controller.close();
|
|
132
|
+
};
|
|
133
|
+
const fail = async (controller) => {
|
|
134
|
+
finish();
|
|
135
|
+
stopUpstream();
|
|
136
|
+
await reportClose("error");
|
|
137
|
+
end(controller, frame("error", { error: OBSERVER_ERROR_MESSAGE }));
|
|
138
|
+
};
|
|
139
|
+
const relay = (controller, event, source = "upstream") => {
|
|
140
|
+
const work = (async () => {
|
|
141
|
+
if (observer && !await call(() => observer.onEvent(event, source))) {
|
|
142
|
+
if (!closed) await fail(controller);
|
|
100
143
|
return;
|
|
101
144
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
145
|
+
if (closed || cancelled) return;
|
|
146
|
+
controller.enqueue(frame(event.event, event.data));
|
|
147
|
+
})();
|
|
148
|
+
inflight = work;
|
|
149
|
+
return work;
|
|
150
|
+
};
|
|
151
|
+
const pullOnce = async (controller) => {
|
|
152
|
+
if (pending) {
|
|
153
|
+
const event = pending;
|
|
154
|
+
pending = void 0;
|
|
155
|
+
await relay(controller, event);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
let next;
|
|
159
|
+
try {
|
|
160
|
+
next = await events.next();
|
|
161
|
+
} catch (err) {
|
|
162
|
+
if (closed) return;
|
|
163
|
+
await relay(
|
|
164
|
+
controller,
|
|
165
|
+
{ event: "error", data: { error: streamErrorMessage(err) } },
|
|
166
|
+
"connection-lost"
|
|
167
|
+
);
|
|
108
168
|
if (closed) return;
|
|
169
|
+
finish();
|
|
170
|
+
await reportClose("end");
|
|
171
|
+
end(controller);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (closed) return;
|
|
175
|
+
if (next.done) {
|
|
176
|
+
finish();
|
|
177
|
+
const ok = await reportClose("end");
|
|
178
|
+
end(controller, ok ? void 0 : frame("error", { error: OBSERVER_ERROR_MESSAGE }));
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
await relay(controller, next.value);
|
|
182
|
+
};
|
|
183
|
+
const drain = async (controller) => {
|
|
184
|
+
await pulling;
|
|
185
|
+
await inflight;
|
|
186
|
+
if (closed) return;
|
|
187
|
+
if (pending) {
|
|
188
|
+
const event = pending;
|
|
189
|
+
pending = void 0;
|
|
190
|
+
await relay(controller, event);
|
|
191
|
+
}
|
|
192
|
+
while (!closed) {
|
|
193
|
+
let next;
|
|
109
194
|
try {
|
|
110
|
-
|
|
111
|
-
if (closed) return;
|
|
112
|
-
if (next.done) {
|
|
113
|
-
finish();
|
|
114
|
-
controller.close();
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
controller.enqueue(frame(next.value.event, next.value.data));
|
|
195
|
+
next = await events.next();
|
|
118
196
|
} catch (err) {
|
|
197
|
+
if (closed) return;
|
|
198
|
+
await relay(
|
|
199
|
+
controller,
|
|
200
|
+
{ event: "error", data: { error: streamErrorMessage(err) } },
|
|
201
|
+
"connection-lost"
|
|
202
|
+
);
|
|
119
203
|
if (closed) return;
|
|
120
204
|
finish();
|
|
121
|
-
|
|
205
|
+
await reportClose("end");
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
if (closed) return;
|
|
209
|
+
if (next.done) {
|
|
210
|
+
finish();
|
|
211
|
+
await reportClose("end");
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
await relay(controller, next.value);
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
const startDrain = (fromCancel) => {
|
|
218
|
+
if (draining || closed || !ctrl) return;
|
|
219
|
+
draining = true;
|
|
220
|
+
cancelled = true;
|
|
221
|
+
clearInterval(keepalive);
|
|
222
|
+
const controller = ctrl;
|
|
223
|
+
if (!fromCancel) {
|
|
224
|
+
try {
|
|
122
225
|
controller.close();
|
|
226
|
+
} catch {
|
|
123
227
|
}
|
|
228
|
+
}
|
|
229
|
+
try {
|
|
230
|
+
observer?.onDisconnect?.();
|
|
231
|
+
} catch {
|
|
232
|
+
}
|
|
233
|
+
void drain(controller).catch((err) => {
|
|
234
|
+
try {
|
|
235
|
+
observer?.onError(err);
|
|
236
|
+
} catch {
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
};
|
|
240
|
+
return new ReadableStream({
|
|
241
|
+
start(controller) {
|
|
242
|
+
ctrl = controller;
|
|
243
|
+
keepalive = setInterval(() => {
|
|
244
|
+
if (!closed && !cancelled) controller.enqueue(encoder.encode(": keepalive\n\n"));
|
|
245
|
+
}, KEEPALIVE_INTERVAL_MS);
|
|
246
|
+
if (keepReading && options.signal) {
|
|
247
|
+
const { signal } = options;
|
|
248
|
+
const onAbort = () => startDrain(false);
|
|
249
|
+
if (signal.aborted) queueMicrotask(onAbort);
|
|
250
|
+
else signal.addEventListener("abort", onAbort, { once: true });
|
|
251
|
+
}
|
|
252
|
+
},
|
|
253
|
+
pull(controller) {
|
|
254
|
+
if (closed || draining) return;
|
|
255
|
+
const work = pullOnce(controller);
|
|
256
|
+
pulling = work;
|
|
257
|
+
return work;
|
|
124
258
|
},
|
|
125
|
-
cancel() {
|
|
259
|
+
async cancel() {
|
|
260
|
+
if (keepReading) {
|
|
261
|
+
startDrain(true);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
cancelled = true;
|
|
126
265
|
finish();
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
266
|
+
stopUpstream();
|
|
267
|
+
const unrelayed = pending;
|
|
268
|
+
pending = void 0;
|
|
269
|
+
if (unrelayed && observer?.onUnrelayed) {
|
|
270
|
+
await call(() => observer.onUnrelayed(unrelayed));
|
|
271
|
+
}
|
|
272
|
+
await inflight;
|
|
273
|
+
await reportClose("cancel");
|
|
130
274
|
}
|
|
131
275
|
});
|
|
132
276
|
}
|
|
133
277
|
|
|
278
|
+
// src/run-callbacks.ts
|
|
279
|
+
var NO_RESULT_MESSAGE = "The stream ended without a result.";
|
|
280
|
+
var TIMEOUT_MESSAGE = "Stopped waiting for the run. It may still finish on Seclai.";
|
|
281
|
+
var FAILED_MESSAGE = "The run failed.";
|
|
282
|
+
function lastAttemptError(run) {
|
|
283
|
+
if (!Array.isArray(run.attempts)) return null;
|
|
284
|
+
for (let i = run.attempts.length - 1; i >= 0; i--) {
|
|
285
|
+
const error = run.attempts[i]?.error;
|
|
286
|
+
if (typeof error === "string" && error) return error;
|
|
287
|
+
}
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
function toAttachments(raw) {
|
|
291
|
+
if (!Array.isArray(raw)) return [];
|
|
292
|
+
return raw.flatMap(
|
|
293
|
+
(item) => typeof item?.id === "string" ? [
|
|
294
|
+
{
|
|
295
|
+
id: item.id,
|
|
296
|
+
name: typeof item.name === "string" ? item.name : null,
|
|
297
|
+
mime: typeof item.mime === "string" ? item.mime : "application/octet-stream"
|
|
298
|
+
}
|
|
299
|
+
] : []
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
function createRunObserver(callbacks, ctx, { keepReading = false } = {}) {
|
|
303
|
+
const { onEvent, onRunEnd } = callbacks;
|
|
304
|
+
const browserStoppedUpstream = () => !keepReading && !!ctx.request.signal?.aborted;
|
|
305
|
+
let runId = null;
|
|
306
|
+
let text = "";
|
|
307
|
+
let terminal;
|
|
308
|
+
let ended = false;
|
|
309
|
+
const outcome = (o) => ({
|
|
310
|
+
runId,
|
|
311
|
+
attachments: [],
|
|
312
|
+
...o
|
|
313
|
+
});
|
|
314
|
+
const end = async (o) => {
|
|
315
|
+
if (ended) return;
|
|
316
|
+
ended = true;
|
|
317
|
+
await onRunEnd?.(o, ctx);
|
|
318
|
+
};
|
|
319
|
+
const failed = (error) => outcome({ status: "failed", output: text, error });
|
|
320
|
+
const cancelled = () => outcome({ status: "cancelled", output: text, error: null });
|
|
321
|
+
const record = (event, source) => {
|
|
322
|
+
const payload = typeof event.data === "object" && event.data !== null ? event.data : {};
|
|
323
|
+
if (typeof payload.run_id === "string" && runId === null) runId = payload.run_id;
|
|
324
|
+
if (terminal) return;
|
|
325
|
+
switch (event.event) {
|
|
326
|
+
case "stream_token":
|
|
327
|
+
if (typeof payload.token === "string") text += payload.token;
|
|
328
|
+
return;
|
|
329
|
+
case "done": {
|
|
330
|
+
const run = payload;
|
|
331
|
+
if (typeof run.run_id === "string") runId = run.run_id;
|
|
332
|
+
const attachments = toAttachments(run.attachments);
|
|
333
|
+
terminal = run.status === "failed" ? outcome({
|
|
334
|
+
status: "failed",
|
|
335
|
+
output: text,
|
|
336
|
+
attachments,
|
|
337
|
+
error: lastAttemptError(run) ?? FAILED_MESSAGE
|
|
338
|
+
}) : outcome({
|
|
339
|
+
status: "completed",
|
|
340
|
+
// Agents with no streaming step send no tokens, so `output`
|
|
341
|
+
// may be the only text.
|
|
342
|
+
output: typeof run.output === "string" && run.output || text,
|
|
343
|
+
attachments,
|
|
344
|
+
error: null
|
|
345
|
+
});
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
case "error":
|
|
349
|
+
terminal = source === "connection-lost" && browserStoppedUpstream() ? cancelled() : failed(typeof payload.error === "string" && payload.error ? payload.error : FAILED_MESSAGE);
|
|
350
|
+
return;
|
|
351
|
+
case "timeout":
|
|
352
|
+
terminal = failed(TIMEOUT_MESSAGE);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
return {
|
|
357
|
+
async onEvent(event, source) {
|
|
358
|
+
record(event, source);
|
|
359
|
+
await onEvent?.(event, ctx);
|
|
360
|
+
if (terminal) await end(terminal);
|
|
361
|
+
},
|
|
362
|
+
onUnrelayed(event) {
|
|
363
|
+
record(event, "upstream");
|
|
364
|
+
},
|
|
365
|
+
async onClose(reason) {
|
|
366
|
+
switch (reason) {
|
|
367
|
+
case "end":
|
|
368
|
+
return end(terminal ?? (browserStoppedUpstream() ? cancelled() : failed(NO_RESULT_MESSAGE)));
|
|
369
|
+
case "error":
|
|
370
|
+
return end(failed(OBSERVER_ERROR_MESSAGE));
|
|
371
|
+
case "cancel":
|
|
372
|
+
return end(terminal ?? cancelled());
|
|
373
|
+
}
|
|
374
|
+
},
|
|
375
|
+
onError(err) {
|
|
376
|
+
console.error("@seclai/handlers: an onEvent or onRunEnd callback threw.", err);
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
|
|
134
381
|
// src/handlers.ts
|
|
135
382
|
var MIN_API_VERSION = SeclaiApiVersion.V2026_10_16;
|
|
136
383
|
var DEFAULT_API_VERSION = SeclaiApiVersion.V2026_10_28;
|
|
@@ -138,6 +385,35 @@ var DEFAULT_RUN_TIMEOUT_MS = 11 * 60 * 1e3;
|
|
|
138
385
|
var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
139
386
|
var ATTACHMENT_ID = /^[A-Za-z0-9_-]+={0,2}$/;
|
|
140
387
|
var MAX_DOWNLOAD_NAME_LENGTH = 255;
|
|
388
|
+
var ATTACHMENT_SLOTS_TTL_MS = 60 * 1e3;
|
|
389
|
+
var MULTIPART_OVERHEAD_BYTES = 64 * 1024;
|
|
390
|
+
async function readLimitedForm(req, limit) {
|
|
391
|
+
if (!req.body) return req.formData();
|
|
392
|
+
let received = 0;
|
|
393
|
+
let exceeded = false;
|
|
394
|
+
const counted = req.body.pipeThrough(
|
|
395
|
+
new TransformStream({
|
|
396
|
+
transform(chunk, controller) {
|
|
397
|
+
received += chunk.byteLength;
|
|
398
|
+
if (received > limit) {
|
|
399
|
+
exceeded = true;
|
|
400
|
+
controller.error(new Error("The request body is over the size limit."));
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
controller.enqueue(chunk);
|
|
404
|
+
}
|
|
405
|
+
})
|
|
406
|
+
);
|
|
407
|
+
const headers = new Headers();
|
|
408
|
+
const contentType = req.headers.get("content-type");
|
|
409
|
+
if (contentType) headers.set("content-type", contentType);
|
|
410
|
+
try {
|
|
411
|
+
return await new Response(counted, { headers }).formData();
|
|
412
|
+
} catch (err) {
|
|
413
|
+
if (exceeded) return "too-large";
|
|
414
|
+
throw err;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
141
417
|
var ROUTES = {
|
|
142
418
|
"attachment-references": "attachmentReferences",
|
|
143
419
|
upload: "upload",
|
|
@@ -170,17 +446,36 @@ function parseRunBody(body) {
|
|
|
170
446
|
}
|
|
171
447
|
return { input, uploadIds, sessionId };
|
|
172
448
|
}
|
|
449
|
+
var UNKNOWN_TYPES = /* @__PURE__ */ new Set(["", "application/octet-stream"]);
|
|
450
|
+
function essence(type) {
|
|
451
|
+
return type.split(";")[0].trim().toLowerCase();
|
|
452
|
+
}
|
|
453
|
+
function slotAllows(slot, type) {
|
|
454
|
+
const patterns = slot.content_types ?? [];
|
|
455
|
+
if (patterns.length === 0) return true;
|
|
456
|
+
return patterns.some((raw) => {
|
|
457
|
+
const pattern = essence(raw);
|
|
458
|
+
if (pattern === "*" || pattern === "*/*") return true;
|
|
459
|
+
if (pattern.endsWith("/*")) return type.startsWith(pattern.slice(0, -1));
|
|
460
|
+
return pattern === type;
|
|
461
|
+
});
|
|
462
|
+
}
|
|
173
463
|
function createSeclaiHandlers(options) {
|
|
174
464
|
const {
|
|
175
465
|
apiKey,
|
|
176
466
|
agentId,
|
|
177
467
|
apiVersion = DEFAULT_API_VERSION,
|
|
178
468
|
runTimeoutMs = DEFAULT_RUN_TIMEOUT_MS,
|
|
469
|
+
upload: uploadOptions = {},
|
|
470
|
+
keepReadingAfterDisconnect = false,
|
|
471
|
+
onEvent,
|
|
472
|
+
onRunEnd,
|
|
179
473
|
context,
|
|
180
474
|
authorizeRun,
|
|
181
475
|
authorizeUpload,
|
|
182
476
|
authorizeDownload
|
|
183
477
|
} = options;
|
|
478
|
+
const { maxBytes, checkContentType = false, onUpload } = uploadOptions;
|
|
184
479
|
if (!apiKey) throw new Error("createSeclaiHandlers: `apiKey` is required.");
|
|
185
480
|
if (!agentId) throw new Error("createSeclaiHandlers: `agentId` is required.");
|
|
186
481
|
if (apiVersion < MIN_API_VERSION) {
|
|
@@ -188,6 +483,9 @@ function createSeclaiHandlers(options) {
|
|
|
188
483
|
`createSeclaiHandlers: \`apiVersion\` must be ${MIN_API_VERSION} or later, got ${apiVersion}.`
|
|
189
484
|
);
|
|
190
485
|
}
|
|
486
|
+
if (maxBytes !== void 0 && !(Number.isFinite(maxBytes) && maxBytes > 0)) {
|
|
487
|
+
throw new Error("createSeclaiHandlers: `upload.maxBytes` must be a positive number.");
|
|
488
|
+
}
|
|
191
489
|
const client = new Seclai({
|
|
192
490
|
apiKey,
|
|
193
491
|
apiVersion,
|
|
@@ -195,6 +493,21 @@ function createSeclaiHandlers(options) {
|
|
|
195
493
|
// when called with any `this` but the global, so it can't be passed bare.
|
|
196
494
|
fetch: (input, init) => globalThis.fetch(input, init)
|
|
197
495
|
});
|
|
496
|
+
let slots;
|
|
497
|
+
function attachmentSlots() {
|
|
498
|
+
if (!slots || Date.now() >= slots.expires) {
|
|
499
|
+
const entry = {
|
|
500
|
+
promise: client.getAgentAttachmentReferences(agentId).then((refs) => refs.accepted_attachments ?? []),
|
|
501
|
+
expires: Date.now() + ATTACHMENT_SLOTS_TTL_MS
|
|
502
|
+
};
|
|
503
|
+
entry.promise.catch(() => {
|
|
504
|
+
if (slots === entry) slots = void 0;
|
|
505
|
+
});
|
|
506
|
+
slots = entry;
|
|
507
|
+
}
|
|
508
|
+
return slots.promise;
|
|
509
|
+
}
|
|
510
|
+
const tooLarge = () => jsonError(413, `The file is larger than the ${maxBytes}-byte limit.`);
|
|
198
511
|
let warnedDownloadResult = false;
|
|
199
512
|
async function callHook(name, req, resolveCtx, hook) {
|
|
200
513
|
let ctx;
|
|
@@ -220,9 +533,16 @@ function createSeclaiHandlers(options) {
|
|
|
220
533
|
},
|
|
221
534
|
async upload(req) {
|
|
222
535
|
if (req.method !== "POST") return wrongMethod("POST");
|
|
536
|
+
const bodyLimit = maxBytes === void 0 ? void 0 : maxBytes + MULTIPART_OVERHEAD_BYTES;
|
|
537
|
+
if (bodyLimit !== void 0) {
|
|
538
|
+
const length = Number(req.headers.get("content-length"));
|
|
539
|
+
if (length > bodyLimit) return tooLarge();
|
|
540
|
+
}
|
|
223
541
|
let form;
|
|
224
542
|
try {
|
|
225
|
-
|
|
543
|
+
const read = bodyLimit === void 0 ? await req.formData() : await readLimitedForm(req, bodyLimit);
|
|
544
|
+
if (read === "too-large") return tooLarge();
|
|
545
|
+
form = read;
|
|
226
546
|
} catch {
|
|
227
547
|
return jsonError(400, "The request body must be multipart form data.");
|
|
228
548
|
}
|
|
@@ -230,15 +550,16 @@ function createSeclaiHandlers(options) {
|
|
|
230
550
|
if (file === null || typeof file === "string") {
|
|
231
551
|
return jsonError(400, "The form must include a `file` field holding a file.");
|
|
232
552
|
}
|
|
553
|
+
if (maxBytes !== void 0 && file.size > maxBytes) return tooLarge();
|
|
233
554
|
if (authorizeUpload) {
|
|
234
|
-
const
|
|
555
|
+
const result2 = await callHook(
|
|
235
556
|
"authorizeUpload",
|
|
236
557
|
req,
|
|
237
558
|
resolveCtx,
|
|
238
559
|
(ctx) => authorizeUpload(req, file, ctx)
|
|
239
560
|
);
|
|
240
|
-
if ("response" in
|
|
241
|
-
const allowed =
|
|
561
|
+
if ("response" in result2) return result2.response;
|
|
562
|
+
const allowed = result2.value;
|
|
242
563
|
if (allowed instanceof Response) return allowed;
|
|
243
564
|
if (allowed === false) return jsonError(403, "This upload isn't allowed.");
|
|
244
565
|
if (allowed !== void 0 && allowed !== true) {
|
|
@@ -246,22 +567,39 @@ function createSeclaiHandlers(options) {
|
|
|
246
567
|
return jsonError(500, NOT_AUTHORIZED);
|
|
247
568
|
}
|
|
248
569
|
}
|
|
570
|
+
let result;
|
|
249
571
|
try {
|
|
572
|
+
const type = essence(file.type);
|
|
573
|
+
if (checkContentType && !UNKNOWN_TYPES.has(type)) {
|
|
574
|
+
const accepted = await attachmentSlots();
|
|
575
|
+
if (!accepted.some((slot) => slotAllows(slot, type))) {
|
|
576
|
+
return jsonError(415, `This agent does not accept files of type ${type}.`);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
250
579
|
const upload = await client.uploadAgentInput(agentId, {
|
|
251
580
|
file,
|
|
252
581
|
fileName: file.name,
|
|
253
582
|
mimeType: file.type || void 0
|
|
254
583
|
});
|
|
255
|
-
|
|
584
|
+
result = {
|
|
256
585
|
id: upload.id,
|
|
257
586
|
status: upload.status,
|
|
258
587
|
filename: upload.filename,
|
|
259
588
|
content_type: upload.content_type,
|
|
260
589
|
file_size: upload.file_size
|
|
261
|
-
}
|
|
590
|
+
};
|
|
262
591
|
} catch (err) {
|
|
263
592
|
return toErrorResponse(err);
|
|
264
593
|
}
|
|
594
|
+
if (onUpload) {
|
|
595
|
+
try {
|
|
596
|
+
const replacement = await onUpload(file, result, req);
|
|
597
|
+
if (replacement instanceof Response) return replacement;
|
|
598
|
+
} catch {
|
|
599
|
+
return jsonError(500, "The server failed to finish handling the upload.");
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
return json(result);
|
|
265
603
|
},
|
|
266
604
|
async run(req) {
|
|
267
605
|
if (req.method !== "POST") return wrongMethod("POST");
|
|
@@ -297,7 +635,8 @@ function createSeclaiHandlers(options) {
|
|
|
297
635
|
}
|
|
298
636
|
const upstream = new AbortController();
|
|
299
637
|
const abort = () => upstream.abort();
|
|
300
|
-
|
|
638
|
+
const keepReading = keepReadingAfterDisconnect && !!(onEvent || onRunEnd);
|
|
639
|
+
if (!keepReading) req.signal?.addEventListener("abort", abort, { once: true });
|
|
301
640
|
const events = client.runStreamingAgent(
|
|
302
641
|
agentId,
|
|
303
642
|
{
|
|
@@ -309,8 +648,15 @@ function createSeclaiHandlers(options) {
|
|
|
309
648
|
);
|
|
310
649
|
try {
|
|
311
650
|
const first = await events.next();
|
|
651
|
+
const observer = onEvent || onRunEnd ? createRunObserver({ onEvent, onRunEnd }, { body, request: req }, { keepReading }) : void 0;
|
|
312
652
|
return new Response(
|
|
313
|
-
relayRunEvents(
|
|
653
|
+
relayRunEvents(
|
|
654
|
+
first.done ? void 0 : first.value,
|
|
655
|
+
events,
|
|
656
|
+
abort,
|
|
657
|
+
observer,
|
|
658
|
+
keepReading ? { keepReadingAfterDisconnect: true, signal: req.signal } : void 0
|
|
659
|
+
),
|
|
314
660
|
{ headers: SSE_HEADERS }
|
|
315
661
|
);
|
|
316
662
|
} catch (err) {
|
|
@@ -390,6 +736,7 @@ function createSeclaiHandlers(options) {
|
|
|
390
736
|
return { ...unbound, withContext: (ctx) => build(async () => ctx) };
|
|
391
737
|
}
|
|
392
738
|
export {
|
|
739
|
+
ATTACHMENT_SLOTS_TTL_MS,
|
|
393
740
|
DEFAULT_API_VERSION,
|
|
394
741
|
MIN_API_VERSION,
|
|
395
742
|
createSeclaiHandlers,
|
package/package.json
CHANGED