@seclai/handlers 0.0.0-stage → 0.0.1
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/LICENSE +21 -0
- package/README.md +78 -2
- package/dist/index.cjs +338 -0
- package/dist/index.d.cts +58 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.js +313 -0
- package/package.json +49 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Seclai
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,79 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @seclai/handlers
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Server handlers for running [Seclai](https://seclai.com) agents from a web app. They use only the Web `Request` and `Response` APIs, and hold your API key on the server so the browser never sees it.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @seclai/handlers
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
`createSeclaiHandlers` returns a function per endpoint, each taking a `Request` and returning a `Promise<Response>`, plus `handle`, which serves all of them from one catch-all route.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
// Next.js: app/api/seclai/[action]/route.ts
|
|
17
|
+
import { createSeclaiHandlers } from "@seclai/handlers";
|
|
18
|
+
|
|
19
|
+
const seclai = createSeclaiHandlers({
|
|
20
|
+
apiKey: process.env.SECLAI_API_KEY!,
|
|
21
|
+
agentId: process.env.SECLAI_AGENT_ID!,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export const GET = seclai.handle;
|
|
25
|
+
export const POST = seclai.handle;
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
// Astro: src/pages/api/seclai/[action].ts
|
|
30
|
+
export const prerender = false;
|
|
31
|
+
export const ALL: APIRoute = ({ request }) => seclai.handle(request);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`handle` picks the handler from the last segment of the URL path and returns a 404 for anything else. To mount routes one by one instead, use `seclai.run`, `seclai.upload` and the rest directly.
|
|
35
|
+
|
|
36
|
+
`@seclai/react` expects these routes under its base path, which defaults to `/api/seclai`:
|
|
37
|
+
|
|
38
|
+
| Route | Handler | Request | Response |
|
|
39
|
+
|---|---|---|---|
|
|
40
|
+
| `GET /attachment-references` | `attachmentReferences` | | Which files the agent accepts |
|
|
41
|
+
| `POST /upload` | `upload` | Multipart form with a `file` field | `{ id, status, filename, content_type, file_size }` |
|
|
42
|
+
| `POST /run` | `run` | JSON `{ input?, uploadIds?, sessionId? }` | Server-sent event stream |
|
|
43
|
+
| `GET /download` | `download` | `?runId=&attachmentId=&name=` | The file's bytes |
|
|
44
|
+
|
|
45
|
+
`run` needs `input`, `uploadIds`, or both. `sessionId` reaches the agent as `metadata.session_id`, which is what chat agents key a conversation on.
|
|
46
|
+
|
|
47
|
+
### Options
|
|
48
|
+
|
|
49
|
+
| Option | Default | |
|
|
50
|
+
|---|---|---|
|
|
51
|
+
| `apiKey` | Required | Your Seclai API key |
|
|
52
|
+
| `agentId` | Required | The agent to run |
|
|
53
|
+
| `apiVersion` | `2026-10-28` | Dated API version. Must be `2026-10-16` or later |
|
|
54
|
+
| `runTimeoutMs` | 11 minutes | How long a streaming run may take |
|
|
55
|
+
|
|
56
|
+
## The run stream
|
|
57
|
+
|
|
58
|
+
`run` relays the API's events unchanged, including `init`, `step_progress`, `stream_token`, `stream_end` and `done`, and sends a `: keepalive` comment every 15 seconds.
|
|
59
|
+
|
|
60
|
+
- 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.
|
|
61
|
+
- If the stream fails after it has started, it ends with an `error` event carrying `{ error }`.
|
|
62
|
+
- When the browser disconnects, the upstream connection closes. **The run itself keeps going on Seclai** and may keep using credits.
|
|
63
|
+
|
|
64
|
+
## Limits to know before deploying
|
|
65
|
+
|
|
66
|
+
- **The endpoints have no auth.** Anyone who can reach them can run your agent on your credits. Put your own access control or rate limiting in front of a public deployment.
|
|
67
|
+
- **The API key is account-wide.** `download` checks that ids are well formed, but the API only checks that a run belongs to the account, not to this agent. Anyone holding a run's id and a file's id from the same account could fetch that file. Run ids are random UUIDs, so they can't be guessed.
|
|
68
|
+
- **Uploads pass through your function**, so your platform's body limit applies, not the API's 200 MB: about 4.5 MB on Vercel and 6 MB on Netlify. The API has no CORS, so the browser can't upload to it directly.
|
|
69
|
+
- **Your platform's function duration limit** may end a long run before `runTimeoutMs` does.
|
|
70
|
+
|
|
71
|
+
## Runtimes
|
|
72
|
+
|
|
73
|
+
The handlers need only Web APIs and a global `fetch`, so they are designed for Vercel, Netlify, Cloudflare Workers, Node 18+, Deno and Bun.
|
|
74
|
+
|
|
75
|
+
On Cloudflare Workers, the `nodejs_compat` flag isn't needed. Create the handlers inside your `fetch` handler, since that's where `env` holds your API key.
|
|
76
|
+
|
|
77
|
+
## License
|
|
78
|
+
|
|
79
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
DEFAULT_API_VERSION: () => DEFAULT_API_VERSION,
|
|
24
|
+
MIN_API_VERSION: () => MIN_API_VERSION,
|
|
25
|
+
createSeclaiHandlers: () => createSeclaiHandlers
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
|
|
29
|
+
// src/handlers.ts
|
|
30
|
+
var import_sdk2 = require("@seclai/sdk");
|
|
31
|
+
|
|
32
|
+
// src/errors.ts
|
|
33
|
+
var import_sdk = require("@seclai/sdk");
|
|
34
|
+
function json(body, status = 200, headers) {
|
|
35
|
+
const merged = new Headers(headers);
|
|
36
|
+
merged.set("content-type", "application/json");
|
|
37
|
+
return new Response(JSON.stringify(body), { status, headers: merged });
|
|
38
|
+
}
|
|
39
|
+
function jsonError(status, message, headers) {
|
|
40
|
+
return json({ error: message, status }, status, headers);
|
|
41
|
+
}
|
|
42
|
+
function upstreamMessage(responseText) {
|
|
43
|
+
if (!responseText) return void 0;
|
|
44
|
+
try {
|
|
45
|
+
const body = JSON.parse(responseText);
|
|
46
|
+
for (const candidate of [
|
|
47
|
+
body.detail,
|
|
48
|
+
body.message,
|
|
49
|
+
body.error?.message,
|
|
50
|
+
body.error
|
|
51
|
+
]) {
|
|
52
|
+
if (typeof candidate === "string" && candidate) return candidate;
|
|
53
|
+
}
|
|
54
|
+
} catch {
|
|
55
|
+
}
|
|
56
|
+
return void 0;
|
|
57
|
+
}
|
|
58
|
+
function toErrorResponse(err) {
|
|
59
|
+
if (err instanceof import_sdk.SeclaiAPIValidationError) {
|
|
60
|
+
return json(
|
|
61
|
+
{
|
|
62
|
+
error: "The Seclai API rejected this request as invalid.",
|
|
63
|
+
status: 422,
|
|
64
|
+
details: err.validationError
|
|
65
|
+
},
|
|
66
|
+
422
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
if (err instanceof import_sdk.SeclaiAPIStatusError) {
|
|
70
|
+
const { statusCode } = err;
|
|
71
|
+
if (statusCode === 401 || statusCode === 403) {
|
|
72
|
+
return jsonError(
|
|
73
|
+
502,
|
|
74
|
+
"The Seclai API rejected the server's credentials. Check SECLAI_API_KEY."
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
if (statusCode >= 400 && statusCode < 500) {
|
|
78
|
+
return jsonError(
|
|
79
|
+
statusCode,
|
|
80
|
+
upstreamMessage(err.responseText) ?? `The Seclai API returned status ${statusCode}.`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
return jsonError(502, `The Seclai API returned status ${statusCode}.`);
|
|
84
|
+
}
|
|
85
|
+
if (err instanceof import_sdk.SeclaiStreamingError) {
|
|
86
|
+
return jsonError(504, "Timed out waiting for the Seclai API.");
|
|
87
|
+
}
|
|
88
|
+
return jsonError(502, "Could not reach the Seclai API.");
|
|
89
|
+
}
|
|
90
|
+
function streamErrorMessage(err) {
|
|
91
|
+
if (err instanceof import_sdk.SeclaiStreamingError) {
|
|
92
|
+
return "Timed out waiting for the agent run to finish.";
|
|
93
|
+
}
|
|
94
|
+
return "The connection to the Seclai API was lost.";
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// src/sse.ts
|
|
98
|
+
var KEEPALIVE_INTERVAL_MS = 15e3;
|
|
99
|
+
var SSE_HEADERS = {
|
|
100
|
+
"content-type": "text/event-stream; charset=utf-8",
|
|
101
|
+
"cache-control": "no-cache, no-transform",
|
|
102
|
+
// Stops nginx-style proxies from buffering the whole stream.
|
|
103
|
+
"x-accel-buffering": "no"
|
|
104
|
+
};
|
|
105
|
+
var encoder = new TextEncoder();
|
|
106
|
+
function frame(event, data) {
|
|
107
|
+
return encoder.encode(`event: ${event}
|
|
108
|
+
data: ${JSON.stringify(data)}
|
|
109
|
+
|
|
110
|
+
`);
|
|
111
|
+
}
|
|
112
|
+
function relayRunEvents(first, events, abort) {
|
|
113
|
+
let closed = false;
|
|
114
|
+
let keepalive;
|
|
115
|
+
const finish = () => {
|
|
116
|
+
closed = true;
|
|
117
|
+
clearInterval(keepalive);
|
|
118
|
+
};
|
|
119
|
+
return new ReadableStream({
|
|
120
|
+
start(controller) {
|
|
121
|
+
if (!first) {
|
|
122
|
+
finish();
|
|
123
|
+
controller.close();
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
controller.enqueue(frame(first.event, first.data));
|
|
127
|
+
keepalive = setInterval(() => {
|
|
128
|
+
if (!closed) controller.enqueue(encoder.encode(": keepalive\n\n"));
|
|
129
|
+
}, KEEPALIVE_INTERVAL_MS);
|
|
130
|
+
},
|
|
131
|
+
async pull(controller) {
|
|
132
|
+
if (closed) return;
|
|
133
|
+
try {
|
|
134
|
+
const next = await events.next();
|
|
135
|
+
if (closed) return;
|
|
136
|
+
if (next.done) {
|
|
137
|
+
finish();
|
|
138
|
+
controller.close();
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
controller.enqueue(frame(next.value.event, next.value.data));
|
|
142
|
+
} catch (err) {
|
|
143
|
+
if (closed) return;
|
|
144
|
+
finish();
|
|
145
|
+
controller.enqueue(frame("error", { error: streamErrorMessage(err) }));
|
|
146
|
+
controller.close();
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
cancel() {
|
|
150
|
+
finish();
|
|
151
|
+
abort();
|
|
152
|
+
void events.return().catch(() => {
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// src/handlers.ts
|
|
159
|
+
var MIN_API_VERSION = import_sdk2.SeclaiApiVersion.V2026_10_16;
|
|
160
|
+
var DEFAULT_API_VERSION = import_sdk2.SeclaiApiVersion.V2026_10_28;
|
|
161
|
+
var DEFAULT_RUN_TIMEOUT_MS = 11 * 60 * 1e3;
|
|
162
|
+
var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
163
|
+
var ATTACHMENT_ID = /^[A-Za-z0-9_-]+={0,2}$/;
|
|
164
|
+
var MAX_DOWNLOAD_NAME_LENGTH = 255;
|
|
165
|
+
var ROUTES = {
|
|
166
|
+
"attachment-references": "attachmentReferences",
|
|
167
|
+
upload: "upload",
|
|
168
|
+
run: "run",
|
|
169
|
+
download: "download"
|
|
170
|
+
};
|
|
171
|
+
function wrongMethod(allow) {
|
|
172
|
+
return jsonError(405, "Method not allowed.", { allow });
|
|
173
|
+
}
|
|
174
|
+
function parseRunBody(body) {
|
|
175
|
+
if (typeof body !== "object" || body === null || Array.isArray(body)) {
|
|
176
|
+
return "The request body must be a JSON object.";
|
|
177
|
+
}
|
|
178
|
+
const { input, uploadIds, sessionId } = body;
|
|
179
|
+
if (input !== void 0 && typeof input !== "string") {
|
|
180
|
+
return "`input` must be a string.";
|
|
181
|
+
}
|
|
182
|
+
if (uploadIds !== void 0 && !(Array.isArray(uploadIds) && uploadIds.every((id) => typeof id === "string"))) {
|
|
183
|
+
return "`uploadIds` must be an array of strings.";
|
|
184
|
+
}
|
|
185
|
+
if (sessionId !== void 0 && (typeof sessionId !== "string" || !sessionId)) {
|
|
186
|
+
return "`sessionId` must be a non-empty string.";
|
|
187
|
+
}
|
|
188
|
+
if (!input && !uploadIds?.length) {
|
|
189
|
+
return "Provide `input`, `uploadIds`, or both.";
|
|
190
|
+
}
|
|
191
|
+
return { input, uploadIds, sessionId };
|
|
192
|
+
}
|
|
193
|
+
function createSeclaiHandlers(options) {
|
|
194
|
+
const {
|
|
195
|
+
apiKey,
|
|
196
|
+
agentId,
|
|
197
|
+
apiVersion = DEFAULT_API_VERSION,
|
|
198
|
+
runTimeoutMs = DEFAULT_RUN_TIMEOUT_MS
|
|
199
|
+
} = options;
|
|
200
|
+
if (!apiKey) throw new Error("createSeclaiHandlers: `apiKey` is required.");
|
|
201
|
+
if (!agentId) throw new Error("createSeclaiHandlers: `agentId` is required.");
|
|
202
|
+
if (apiVersion < MIN_API_VERSION) {
|
|
203
|
+
throw new Error(
|
|
204
|
+
`createSeclaiHandlers: \`apiVersion\` must be ${MIN_API_VERSION} or later, got ${apiVersion}.`
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
const client = new import_sdk2.Seclai({
|
|
208
|
+
apiKey,
|
|
209
|
+
apiVersion,
|
|
210
|
+
// The SDK calls this as a method. Workers' fetch throws "Illegal invocation"
|
|
211
|
+
// when called with any `this` but the global, so it can't be passed bare.
|
|
212
|
+
fetch: (input, init) => globalThis.fetch(input, init)
|
|
213
|
+
});
|
|
214
|
+
const handlers = {
|
|
215
|
+
async attachmentReferences(req) {
|
|
216
|
+
if (req.method !== "GET") return wrongMethod("GET");
|
|
217
|
+
try {
|
|
218
|
+
return json(await client.getAgentAttachmentReferences(agentId));
|
|
219
|
+
} catch (err) {
|
|
220
|
+
return toErrorResponse(err);
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
async upload(req) {
|
|
224
|
+
if (req.method !== "POST") return wrongMethod("POST");
|
|
225
|
+
let form;
|
|
226
|
+
try {
|
|
227
|
+
form = await req.formData();
|
|
228
|
+
} catch {
|
|
229
|
+
return jsonError(400, "The request body must be multipart form data.");
|
|
230
|
+
}
|
|
231
|
+
const file = form.get("file");
|
|
232
|
+
if (file === null || typeof file === "string") {
|
|
233
|
+
return jsonError(400, "The form must include a `file` field holding a file.");
|
|
234
|
+
}
|
|
235
|
+
try {
|
|
236
|
+
const upload = await client.uploadAgentInput(agentId, {
|
|
237
|
+
file,
|
|
238
|
+
fileName: file.name,
|
|
239
|
+
mimeType: file.type || void 0
|
|
240
|
+
});
|
|
241
|
+
return json({
|
|
242
|
+
id: upload.id,
|
|
243
|
+
status: upload.status,
|
|
244
|
+
filename: upload.filename,
|
|
245
|
+
content_type: upload.content_type,
|
|
246
|
+
file_size: upload.file_size
|
|
247
|
+
});
|
|
248
|
+
} catch (err) {
|
|
249
|
+
return toErrorResponse(err);
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
async run(req) {
|
|
253
|
+
if (req.method !== "POST") return wrongMethod("POST");
|
|
254
|
+
let raw;
|
|
255
|
+
try {
|
|
256
|
+
raw = await req.json();
|
|
257
|
+
} catch {
|
|
258
|
+
return jsonError(400, "The request body must be JSON.");
|
|
259
|
+
}
|
|
260
|
+
const body = parseRunBody(raw);
|
|
261
|
+
if (typeof body === "string") return jsonError(400, body);
|
|
262
|
+
const upstream = new AbortController();
|
|
263
|
+
const abort = () => upstream.abort();
|
|
264
|
+
req.signal?.addEventListener("abort", abort, { once: true });
|
|
265
|
+
const events = client.runStreamingAgent(
|
|
266
|
+
agentId,
|
|
267
|
+
{
|
|
268
|
+
...body.input ? { input: body.input } : {},
|
|
269
|
+
...body.uploadIds?.length ? { input_upload_ids: body.uploadIds } : {},
|
|
270
|
+
...body.sessionId ? { metadata: { session_id: body.sessionId } } : {}
|
|
271
|
+
},
|
|
272
|
+
{ timeoutMs: runTimeoutMs, signal: upstream.signal }
|
|
273
|
+
);
|
|
274
|
+
try {
|
|
275
|
+
const first = await events.next();
|
|
276
|
+
return new Response(
|
|
277
|
+
relayRunEvents(first.done ? void 0 : first.value, events, abort),
|
|
278
|
+
{ headers: SSE_HEADERS }
|
|
279
|
+
);
|
|
280
|
+
} catch (err) {
|
|
281
|
+
return toErrorResponse(err);
|
|
282
|
+
}
|
|
283
|
+
},
|
|
284
|
+
async download(req) {
|
|
285
|
+
if (req.method !== "GET") return wrongMethod("GET");
|
|
286
|
+
const params = new URL(req.url).searchParams;
|
|
287
|
+
const runId = params.get("runId") ?? "";
|
|
288
|
+
const attachmentId = params.get("attachmentId") ?? "";
|
|
289
|
+
const name = params.get("name") ?? void 0;
|
|
290
|
+
if (!UUID.test(runId)) return jsonError(400, "`runId` must be a UUID.");
|
|
291
|
+
if (!ATTACHMENT_ID.test(attachmentId)) {
|
|
292
|
+
return jsonError(400, "`attachmentId` is not valid.");
|
|
293
|
+
}
|
|
294
|
+
if (name !== void 0 && name.length > MAX_DOWNLOAD_NAME_LENGTH) {
|
|
295
|
+
return jsonError(400, `\`name\` must be ${MAX_DOWNLOAD_NAME_LENGTH} characters or fewer.`);
|
|
296
|
+
}
|
|
297
|
+
try {
|
|
298
|
+
const upstream = await client.downloadAgentRunAttachment(runId, attachmentId, {
|
|
299
|
+
...name ? { downloadName: name } : {},
|
|
300
|
+
signal: req.signal
|
|
301
|
+
});
|
|
302
|
+
const contentType = upstream.headers.get("content-type") ?? "application/octet-stream";
|
|
303
|
+
const headers = new Headers({
|
|
304
|
+
"content-type": contentType,
|
|
305
|
+
"cache-control": "private, no-store",
|
|
306
|
+
"x-content-type-options": "nosniff"
|
|
307
|
+
});
|
|
308
|
+
const disposition = upstream.headers.get("content-disposition");
|
|
309
|
+
if (disposition) headers.set("content-disposition", disposition);
|
|
310
|
+
const length = upstream.headers.get("content-length");
|
|
311
|
+
if (length && !upstream.headers.has("content-encoding")) {
|
|
312
|
+
headers.set("content-length", length);
|
|
313
|
+
}
|
|
314
|
+
if (!contentType.toLowerCase().startsWith("application/pdf")) {
|
|
315
|
+
headers.set("content-security-policy", "sandbox; default-src 'none'");
|
|
316
|
+
}
|
|
317
|
+
return new Response(upstream.body, { status: upstream.status, headers });
|
|
318
|
+
} catch (err) {
|
|
319
|
+
return toErrorResponse(err);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
return {
|
|
324
|
+
...handlers,
|
|
325
|
+
async handle(req) {
|
|
326
|
+
const segment = new URL(req.url).pathname.replace(/\/+$/, "").split("/").pop() ?? "";
|
|
327
|
+
const name = Object.hasOwn(ROUTES, segment) ? ROUTES[segment] : void 0;
|
|
328
|
+
if (!name) return jsonError(404, "Not found.");
|
|
329
|
+
return handlers[name](req);
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
334
|
+
0 && (module.exports = {
|
|
335
|
+
DEFAULT_API_VERSION,
|
|
336
|
+
MIN_API_VERSION,
|
|
337
|
+
createSeclaiHandlers
|
|
338
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ApiVersion } from '@seclai/sdk';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* From this version uploads are `ready` at once and a run's `output` is plain
|
|
5
|
+
* text with files in `attachments`. The handlers rely on both.
|
|
6
|
+
*/
|
|
7
|
+
declare const MIN_API_VERSION: "2026-10-16";
|
|
8
|
+
/**
|
|
9
|
+
* A dated constant, not `SeclaiApiVersion.Latest`, which moves with SDK upgrades
|
|
10
|
+
* and could change response shapes underneath a deployed app.
|
|
11
|
+
*/
|
|
12
|
+
declare const DEFAULT_API_VERSION: "2026-10-28";
|
|
13
|
+
interface SeclaiHandlersOptions {
|
|
14
|
+
/** Seclai API key. Server-side only: never expose it to the browser. */
|
|
15
|
+
apiKey: string;
|
|
16
|
+
/** The agent these handlers run. */
|
|
17
|
+
agentId: string;
|
|
18
|
+
/** Dated API version. Must be {@link MIN_API_VERSION} or later. */
|
|
19
|
+
apiVersion?: ApiVersion;
|
|
20
|
+
/** How long a streaming run may take, in milliseconds. Defaults to 11 minutes. */
|
|
21
|
+
runTimeoutMs?: number;
|
|
22
|
+
}
|
|
23
|
+
/** JSON body accepted by the `run` handler. */
|
|
24
|
+
interface RunRequestBody {
|
|
25
|
+
/** The prompt text. */
|
|
26
|
+
input?: string;
|
|
27
|
+
/** Ids returned by the `upload` handler, sent together with `input`. */
|
|
28
|
+
uploadIds?: string[];
|
|
29
|
+
/** Keys a conversation: sent to the agent as `metadata.session_id`. */
|
|
30
|
+
sessionId?: string;
|
|
31
|
+
}
|
|
32
|
+
type SeclaiHandler = (req: Request) => Promise<Response>;
|
|
33
|
+
interface SeclaiHandlers {
|
|
34
|
+
/** `GET`: which files the agent accepts. */
|
|
35
|
+
attachmentReferences: SeclaiHandler;
|
|
36
|
+
/** `POST` multipart form data with a `file` field: returns the upload's `id`. */
|
|
37
|
+
upload: SeclaiHandler;
|
|
38
|
+
/** `POST` a {@link RunRequestBody}: returns the run as a server-sent event stream. */
|
|
39
|
+
run: SeclaiHandler;
|
|
40
|
+
/** `GET ?runId=&attachmentId=&name=`: streams a file from a run. */
|
|
41
|
+
download: SeclaiHandler;
|
|
42
|
+
/**
|
|
43
|
+
* All four in one, for a single catch-all route: picks the handler from the
|
|
44
|
+
* last segment of the URL path (`attachment-references`, `upload`, `run` or
|
|
45
|
+
* `download`) and returns a 404 for anything else.
|
|
46
|
+
*/
|
|
47
|
+
handle: SeclaiHandler;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Create the server handlers for one agent. Each takes a Web `Request` and
|
|
51
|
+
* returns a Web `Response`, so they mount on any runtime with those APIs. Routing
|
|
52
|
+
* is left to the host framework.
|
|
53
|
+
*
|
|
54
|
+
* @throws If `apiKey` or `agentId` is missing, or `apiVersion` is too old.
|
|
55
|
+
*/
|
|
56
|
+
declare function createSeclaiHandlers(options: SeclaiHandlersOptions): SeclaiHandlers;
|
|
57
|
+
|
|
58
|
+
export { DEFAULT_API_VERSION, MIN_API_VERSION, type RunRequestBody, type SeclaiHandler, type SeclaiHandlers, type SeclaiHandlersOptions, createSeclaiHandlers };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ApiVersion } from '@seclai/sdk';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* From this version uploads are `ready` at once and a run's `output` is plain
|
|
5
|
+
* text with files in `attachments`. The handlers rely on both.
|
|
6
|
+
*/
|
|
7
|
+
declare const MIN_API_VERSION: "2026-10-16";
|
|
8
|
+
/**
|
|
9
|
+
* A dated constant, not `SeclaiApiVersion.Latest`, which moves with SDK upgrades
|
|
10
|
+
* and could change response shapes underneath a deployed app.
|
|
11
|
+
*/
|
|
12
|
+
declare const DEFAULT_API_VERSION: "2026-10-28";
|
|
13
|
+
interface SeclaiHandlersOptions {
|
|
14
|
+
/** Seclai API key. Server-side only: never expose it to the browser. */
|
|
15
|
+
apiKey: string;
|
|
16
|
+
/** The agent these handlers run. */
|
|
17
|
+
agentId: string;
|
|
18
|
+
/** Dated API version. Must be {@link MIN_API_VERSION} or later. */
|
|
19
|
+
apiVersion?: ApiVersion;
|
|
20
|
+
/** How long a streaming run may take, in milliseconds. Defaults to 11 minutes. */
|
|
21
|
+
runTimeoutMs?: number;
|
|
22
|
+
}
|
|
23
|
+
/** JSON body accepted by the `run` handler. */
|
|
24
|
+
interface RunRequestBody {
|
|
25
|
+
/** The prompt text. */
|
|
26
|
+
input?: string;
|
|
27
|
+
/** Ids returned by the `upload` handler, sent together with `input`. */
|
|
28
|
+
uploadIds?: string[];
|
|
29
|
+
/** Keys a conversation: sent to the agent as `metadata.session_id`. */
|
|
30
|
+
sessionId?: string;
|
|
31
|
+
}
|
|
32
|
+
type SeclaiHandler = (req: Request) => Promise<Response>;
|
|
33
|
+
interface SeclaiHandlers {
|
|
34
|
+
/** `GET`: which files the agent accepts. */
|
|
35
|
+
attachmentReferences: SeclaiHandler;
|
|
36
|
+
/** `POST` multipart form data with a `file` field: returns the upload's `id`. */
|
|
37
|
+
upload: SeclaiHandler;
|
|
38
|
+
/** `POST` a {@link RunRequestBody}: returns the run as a server-sent event stream. */
|
|
39
|
+
run: SeclaiHandler;
|
|
40
|
+
/** `GET ?runId=&attachmentId=&name=`: streams a file from a run. */
|
|
41
|
+
download: SeclaiHandler;
|
|
42
|
+
/**
|
|
43
|
+
* All four in one, for a single catch-all route: picks the handler from the
|
|
44
|
+
* last segment of the URL path (`attachment-references`, `upload`, `run` or
|
|
45
|
+
* `download`) and returns a 404 for anything else.
|
|
46
|
+
*/
|
|
47
|
+
handle: SeclaiHandler;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Create the server handlers for one agent. Each takes a Web `Request` and
|
|
51
|
+
* returns a Web `Response`, so they mount on any runtime with those APIs. Routing
|
|
52
|
+
* is left to the host framework.
|
|
53
|
+
*
|
|
54
|
+
* @throws If `apiKey` or `agentId` is missing, or `apiVersion` is too old.
|
|
55
|
+
*/
|
|
56
|
+
declare function createSeclaiHandlers(options: SeclaiHandlersOptions): SeclaiHandlers;
|
|
57
|
+
|
|
58
|
+
export { DEFAULT_API_VERSION, MIN_API_VERSION, type RunRequestBody, type SeclaiHandler, type SeclaiHandlers, type SeclaiHandlersOptions, createSeclaiHandlers };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
// src/handlers.ts
|
|
2
|
+
import { Seclai, SeclaiApiVersion } from "@seclai/sdk";
|
|
3
|
+
|
|
4
|
+
// src/errors.ts
|
|
5
|
+
import {
|
|
6
|
+
SeclaiAPIStatusError,
|
|
7
|
+
SeclaiAPIValidationError,
|
|
8
|
+
SeclaiStreamingError
|
|
9
|
+
} from "@seclai/sdk";
|
|
10
|
+
function json(body, status = 200, headers) {
|
|
11
|
+
const merged = new Headers(headers);
|
|
12
|
+
merged.set("content-type", "application/json");
|
|
13
|
+
return new Response(JSON.stringify(body), { status, headers: merged });
|
|
14
|
+
}
|
|
15
|
+
function jsonError(status, message, headers) {
|
|
16
|
+
return json({ error: message, status }, status, headers);
|
|
17
|
+
}
|
|
18
|
+
function upstreamMessage(responseText) {
|
|
19
|
+
if (!responseText) return void 0;
|
|
20
|
+
try {
|
|
21
|
+
const body = JSON.parse(responseText);
|
|
22
|
+
for (const candidate of [
|
|
23
|
+
body.detail,
|
|
24
|
+
body.message,
|
|
25
|
+
body.error?.message,
|
|
26
|
+
body.error
|
|
27
|
+
]) {
|
|
28
|
+
if (typeof candidate === "string" && candidate) return candidate;
|
|
29
|
+
}
|
|
30
|
+
} catch {
|
|
31
|
+
}
|
|
32
|
+
return void 0;
|
|
33
|
+
}
|
|
34
|
+
function toErrorResponse(err) {
|
|
35
|
+
if (err instanceof SeclaiAPIValidationError) {
|
|
36
|
+
return json(
|
|
37
|
+
{
|
|
38
|
+
error: "The Seclai API rejected this request as invalid.",
|
|
39
|
+
status: 422,
|
|
40
|
+
details: err.validationError
|
|
41
|
+
},
|
|
42
|
+
422
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
if (err instanceof SeclaiAPIStatusError) {
|
|
46
|
+
const { statusCode } = err;
|
|
47
|
+
if (statusCode === 401 || statusCode === 403) {
|
|
48
|
+
return jsonError(
|
|
49
|
+
502,
|
|
50
|
+
"The Seclai API rejected the server's credentials. Check SECLAI_API_KEY."
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
if (statusCode >= 400 && statusCode < 500) {
|
|
54
|
+
return jsonError(
|
|
55
|
+
statusCode,
|
|
56
|
+
upstreamMessage(err.responseText) ?? `The Seclai API returned status ${statusCode}.`
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
return jsonError(502, `The Seclai API returned status ${statusCode}.`);
|
|
60
|
+
}
|
|
61
|
+
if (err instanceof SeclaiStreamingError) {
|
|
62
|
+
return jsonError(504, "Timed out waiting for the Seclai API.");
|
|
63
|
+
}
|
|
64
|
+
return jsonError(502, "Could not reach the Seclai API.");
|
|
65
|
+
}
|
|
66
|
+
function streamErrorMessage(err) {
|
|
67
|
+
if (err instanceof SeclaiStreamingError) {
|
|
68
|
+
return "Timed out waiting for the agent run to finish.";
|
|
69
|
+
}
|
|
70
|
+
return "The connection to the Seclai API was lost.";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// src/sse.ts
|
|
74
|
+
var KEEPALIVE_INTERVAL_MS = 15e3;
|
|
75
|
+
var SSE_HEADERS = {
|
|
76
|
+
"content-type": "text/event-stream; charset=utf-8",
|
|
77
|
+
"cache-control": "no-cache, no-transform",
|
|
78
|
+
// Stops nginx-style proxies from buffering the whole stream.
|
|
79
|
+
"x-accel-buffering": "no"
|
|
80
|
+
};
|
|
81
|
+
var encoder = new TextEncoder();
|
|
82
|
+
function frame(event, data) {
|
|
83
|
+
return encoder.encode(`event: ${event}
|
|
84
|
+
data: ${JSON.stringify(data)}
|
|
85
|
+
|
|
86
|
+
`);
|
|
87
|
+
}
|
|
88
|
+
function relayRunEvents(first, events, abort) {
|
|
89
|
+
let closed = false;
|
|
90
|
+
let keepalive;
|
|
91
|
+
const finish = () => {
|
|
92
|
+
closed = true;
|
|
93
|
+
clearInterval(keepalive);
|
|
94
|
+
};
|
|
95
|
+
return new ReadableStream({
|
|
96
|
+
start(controller) {
|
|
97
|
+
if (!first) {
|
|
98
|
+
finish();
|
|
99
|
+
controller.close();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
controller.enqueue(frame(first.event, first.data));
|
|
103
|
+
keepalive = setInterval(() => {
|
|
104
|
+
if (!closed) controller.enqueue(encoder.encode(": keepalive\n\n"));
|
|
105
|
+
}, KEEPALIVE_INTERVAL_MS);
|
|
106
|
+
},
|
|
107
|
+
async pull(controller) {
|
|
108
|
+
if (closed) return;
|
|
109
|
+
try {
|
|
110
|
+
const next = await events.next();
|
|
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));
|
|
118
|
+
} catch (err) {
|
|
119
|
+
if (closed) return;
|
|
120
|
+
finish();
|
|
121
|
+
controller.enqueue(frame("error", { error: streamErrorMessage(err) }));
|
|
122
|
+
controller.close();
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
cancel() {
|
|
126
|
+
finish();
|
|
127
|
+
abort();
|
|
128
|
+
void events.return().catch(() => {
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// src/handlers.ts
|
|
135
|
+
var MIN_API_VERSION = SeclaiApiVersion.V2026_10_16;
|
|
136
|
+
var DEFAULT_API_VERSION = SeclaiApiVersion.V2026_10_28;
|
|
137
|
+
var DEFAULT_RUN_TIMEOUT_MS = 11 * 60 * 1e3;
|
|
138
|
+
var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
139
|
+
var ATTACHMENT_ID = /^[A-Za-z0-9_-]+={0,2}$/;
|
|
140
|
+
var MAX_DOWNLOAD_NAME_LENGTH = 255;
|
|
141
|
+
var ROUTES = {
|
|
142
|
+
"attachment-references": "attachmentReferences",
|
|
143
|
+
upload: "upload",
|
|
144
|
+
run: "run",
|
|
145
|
+
download: "download"
|
|
146
|
+
};
|
|
147
|
+
function wrongMethod(allow) {
|
|
148
|
+
return jsonError(405, "Method not allowed.", { allow });
|
|
149
|
+
}
|
|
150
|
+
function parseRunBody(body) {
|
|
151
|
+
if (typeof body !== "object" || body === null || Array.isArray(body)) {
|
|
152
|
+
return "The request body must be a JSON object.";
|
|
153
|
+
}
|
|
154
|
+
const { input, uploadIds, sessionId } = body;
|
|
155
|
+
if (input !== void 0 && typeof input !== "string") {
|
|
156
|
+
return "`input` must be a string.";
|
|
157
|
+
}
|
|
158
|
+
if (uploadIds !== void 0 && !(Array.isArray(uploadIds) && uploadIds.every((id) => typeof id === "string"))) {
|
|
159
|
+
return "`uploadIds` must be an array of strings.";
|
|
160
|
+
}
|
|
161
|
+
if (sessionId !== void 0 && (typeof sessionId !== "string" || !sessionId)) {
|
|
162
|
+
return "`sessionId` must be a non-empty string.";
|
|
163
|
+
}
|
|
164
|
+
if (!input && !uploadIds?.length) {
|
|
165
|
+
return "Provide `input`, `uploadIds`, or both.";
|
|
166
|
+
}
|
|
167
|
+
return { input, uploadIds, sessionId };
|
|
168
|
+
}
|
|
169
|
+
function createSeclaiHandlers(options) {
|
|
170
|
+
const {
|
|
171
|
+
apiKey,
|
|
172
|
+
agentId,
|
|
173
|
+
apiVersion = DEFAULT_API_VERSION,
|
|
174
|
+
runTimeoutMs = DEFAULT_RUN_TIMEOUT_MS
|
|
175
|
+
} = options;
|
|
176
|
+
if (!apiKey) throw new Error("createSeclaiHandlers: `apiKey` is required.");
|
|
177
|
+
if (!agentId) throw new Error("createSeclaiHandlers: `agentId` is required.");
|
|
178
|
+
if (apiVersion < MIN_API_VERSION) {
|
|
179
|
+
throw new Error(
|
|
180
|
+
`createSeclaiHandlers: \`apiVersion\` must be ${MIN_API_VERSION} or later, got ${apiVersion}.`
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
const client = new Seclai({
|
|
184
|
+
apiKey,
|
|
185
|
+
apiVersion,
|
|
186
|
+
// The SDK calls this as a method. Workers' fetch throws "Illegal invocation"
|
|
187
|
+
// when called with any `this` but the global, so it can't be passed bare.
|
|
188
|
+
fetch: (input, init) => globalThis.fetch(input, init)
|
|
189
|
+
});
|
|
190
|
+
const handlers = {
|
|
191
|
+
async attachmentReferences(req) {
|
|
192
|
+
if (req.method !== "GET") return wrongMethod("GET");
|
|
193
|
+
try {
|
|
194
|
+
return json(await client.getAgentAttachmentReferences(agentId));
|
|
195
|
+
} catch (err) {
|
|
196
|
+
return toErrorResponse(err);
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
async upload(req) {
|
|
200
|
+
if (req.method !== "POST") return wrongMethod("POST");
|
|
201
|
+
let form;
|
|
202
|
+
try {
|
|
203
|
+
form = await req.formData();
|
|
204
|
+
} catch {
|
|
205
|
+
return jsonError(400, "The request body must be multipart form data.");
|
|
206
|
+
}
|
|
207
|
+
const file = form.get("file");
|
|
208
|
+
if (file === null || typeof file === "string") {
|
|
209
|
+
return jsonError(400, "The form must include a `file` field holding a file.");
|
|
210
|
+
}
|
|
211
|
+
try {
|
|
212
|
+
const upload = await client.uploadAgentInput(agentId, {
|
|
213
|
+
file,
|
|
214
|
+
fileName: file.name,
|
|
215
|
+
mimeType: file.type || void 0
|
|
216
|
+
});
|
|
217
|
+
return json({
|
|
218
|
+
id: upload.id,
|
|
219
|
+
status: upload.status,
|
|
220
|
+
filename: upload.filename,
|
|
221
|
+
content_type: upload.content_type,
|
|
222
|
+
file_size: upload.file_size
|
|
223
|
+
});
|
|
224
|
+
} catch (err) {
|
|
225
|
+
return toErrorResponse(err);
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
async run(req) {
|
|
229
|
+
if (req.method !== "POST") return wrongMethod("POST");
|
|
230
|
+
let raw;
|
|
231
|
+
try {
|
|
232
|
+
raw = await req.json();
|
|
233
|
+
} catch {
|
|
234
|
+
return jsonError(400, "The request body must be JSON.");
|
|
235
|
+
}
|
|
236
|
+
const body = parseRunBody(raw);
|
|
237
|
+
if (typeof body === "string") return jsonError(400, body);
|
|
238
|
+
const upstream = new AbortController();
|
|
239
|
+
const abort = () => upstream.abort();
|
|
240
|
+
req.signal?.addEventListener("abort", abort, { once: true });
|
|
241
|
+
const events = client.runStreamingAgent(
|
|
242
|
+
agentId,
|
|
243
|
+
{
|
|
244
|
+
...body.input ? { input: body.input } : {},
|
|
245
|
+
...body.uploadIds?.length ? { input_upload_ids: body.uploadIds } : {},
|
|
246
|
+
...body.sessionId ? { metadata: { session_id: body.sessionId } } : {}
|
|
247
|
+
},
|
|
248
|
+
{ timeoutMs: runTimeoutMs, signal: upstream.signal }
|
|
249
|
+
);
|
|
250
|
+
try {
|
|
251
|
+
const first = await events.next();
|
|
252
|
+
return new Response(
|
|
253
|
+
relayRunEvents(first.done ? void 0 : first.value, events, abort),
|
|
254
|
+
{ headers: SSE_HEADERS }
|
|
255
|
+
);
|
|
256
|
+
} catch (err) {
|
|
257
|
+
return toErrorResponse(err);
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
async download(req) {
|
|
261
|
+
if (req.method !== "GET") return wrongMethod("GET");
|
|
262
|
+
const params = new URL(req.url).searchParams;
|
|
263
|
+
const runId = params.get("runId") ?? "";
|
|
264
|
+
const attachmentId = params.get("attachmentId") ?? "";
|
|
265
|
+
const name = params.get("name") ?? void 0;
|
|
266
|
+
if (!UUID.test(runId)) return jsonError(400, "`runId` must be a UUID.");
|
|
267
|
+
if (!ATTACHMENT_ID.test(attachmentId)) {
|
|
268
|
+
return jsonError(400, "`attachmentId` is not valid.");
|
|
269
|
+
}
|
|
270
|
+
if (name !== void 0 && name.length > MAX_DOWNLOAD_NAME_LENGTH) {
|
|
271
|
+
return jsonError(400, `\`name\` must be ${MAX_DOWNLOAD_NAME_LENGTH} characters or fewer.`);
|
|
272
|
+
}
|
|
273
|
+
try {
|
|
274
|
+
const upstream = await client.downloadAgentRunAttachment(runId, attachmentId, {
|
|
275
|
+
...name ? { downloadName: name } : {},
|
|
276
|
+
signal: req.signal
|
|
277
|
+
});
|
|
278
|
+
const contentType = upstream.headers.get("content-type") ?? "application/octet-stream";
|
|
279
|
+
const headers = new Headers({
|
|
280
|
+
"content-type": contentType,
|
|
281
|
+
"cache-control": "private, no-store",
|
|
282
|
+
"x-content-type-options": "nosniff"
|
|
283
|
+
});
|
|
284
|
+
const disposition = upstream.headers.get("content-disposition");
|
|
285
|
+
if (disposition) headers.set("content-disposition", disposition);
|
|
286
|
+
const length = upstream.headers.get("content-length");
|
|
287
|
+
if (length && !upstream.headers.has("content-encoding")) {
|
|
288
|
+
headers.set("content-length", length);
|
|
289
|
+
}
|
|
290
|
+
if (!contentType.toLowerCase().startsWith("application/pdf")) {
|
|
291
|
+
headers.set("content-security-policy", "sandbox; default-src 'none'");
|
|
292
|
+
}
|
|
293
|
+
return new Response(upstream.body, { status: upstream.status, headers });
|
|
294
|
+
} catch (err) {
|
|
295
|
+
return toErrorResponse(err);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
return {
|
|
300
|
+
...handlers,
|
|
301
|
+
async handle(req) {
|
|
302
|
+
const segment = new URL(req.url).pathname.replace(/\/+$/, "").split("/").pop() ?? "";
|
|
303
|
+
const name = Object.hasOwn(ROUTES, segment) ? ROUTES[segment] : void 0;
|
|
304
|
+
if (!name) return jsonError(404, "Not found.");
|
|
305
|
+
return handlers[name](req);
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
export {
|
|
310
|
+
DEFAULT_API_VERSION,
|
|
311
|
+
MIN_API_VERSION,
|
|
312
|
+
createSeclaiHandlers
|
|
313
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seclai/handlers",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Framework-free server handlers for running Seclai agents from a web app: upload, run with SSE relay, and download proxy.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"require": {
|
|
17
|
+
"types": "./dist/index.d.cts",
|
|
18
|
+
"default": "./dist/index.cjs"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"./package.json": "./package.json"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=18"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/seclai/seclai-ui.git",
|
|
36
|
+
"directory": "packages/handlers"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsup",
|
|
40
|
+
"typecheck": "tsc --noEmit",
|
|
41
|
+
"test": "vitest run"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@seclai/sdk": "^1.8.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"tsup": "^8.5.0",
|
|
48
|
+
"typescript": "^5.9.3",
|
|
49
|
+
"vitest": "^3.2.4"
|
|
50
|
+
}
|
|
51
|
+
}
|