@seclai/handlers 0.0.16 → 0.0.18
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 +9 -1
- package/package.json +5 -12
- package/dist/index.cjs +0 -1132
- package/dist/index.d.cts +0 -344
package/dist/index.d.cts
DELETED
|
@@ -1,344 +0,0 @@
|
|
|
1
|
-
import { AgentAttachmentRefsApiResponse, ApiVersion } from '@seclai/sdk';
|
|
2
|
-
|
|
3
|
-
/** Structured details of a log line: ids, timings and counts. */
|
|
4
|
-
type SeclaiLogFields = Record<string, unknown>;
|
|
5
|
-
/**
|
|
6
|
-
* Where the handlers send their log lines, each called as `(message, fields)`.
|
|
7
|
-
* `console` fits, and so do winston and consola. pino takes `(fields, message)`
|
|
8
|
-
* and would drop the fields, so wrap it, one line per level:
|
|
9
|
-
* `info: (m, f) => log.info(f, m)`.
|
|
10
|
-
*/
|
|
11
|
-
interface SeclaiLogger {
|
|
12
|
-
debug(message: string, fields?: SeclaiLogFields): void;
|
|
13
|
-
info(message: string, fields?: SeclaiLogFields): void;
|
|
14
|
-
warn(message: string, fields?: SeclaiLogFields): void;
|
|
15
|
-
error(message: string, fields?: SeclaiLogFields): void;
|
|
16
|
-
}
|
|
17
|
-
type SeclaiLogLevel = "debug" | "info" | "warn" | "error" | "silent";
|
|
18
|
-
/**
|
|
19
|
-
* A {@link SeclaiLogger} that writes to `console` from `level` up, with each
|
|
20
|
-
* message prefixed `[seclai]`. Passing `console` itself also works, but logs
|
|
21
|
-
* `debug` lines too: every token and keepalive.
|
|
22
|
-
*
|
|
23
|
-
* @param level - The lowest level written. Defaults to `info`.
|
|
24
|
-
* @param target - Where to write. Defaults to `console`.
|
|
25
|
-
*/
|
|
26
|
-
declare function consoleLogger(level?: SeclaiLogLevel, target?: SeclaiLogger): SeclaiLogger;
|
|
27
|
-
|
|
28
|
-
/** One event of the run stream, as the API sent it. `data` is the parsed JSON. */
|
|
29
|
-
interface RunEvent {
|
|
30
|
-
event: string;
|
|
31
|
-
data: unknown;
|
|
32
|
-
}
|
|
33
|
-
/** What the run callbacks get besides the event or outcome. The same object for the whole run. */
|
|
34
|
-
interface RunCallbackContext<Ctx = undefined> {
|
|
35
|
-
/** The validated body of the `run` request: after any `authorizeRun` rewrite. */
|
|
36
|
-
body: RunRequestBody;
|
|
37
|
-
/** The browser's request to the `run` handler. */
|
|
38
|
-
request: Request;
|
|
39
|
-
/**
|
|
40
|
-
* The app's context for this request: what the `context` option returned, or
|
|
41
|
-
* what `withContext(ctx)` was given. The same value the access-control hooks
|
|
42
|
-
* get. `undefined` without either.
|
|
43
|
-
*/
|
|
44
|
-
context: Ctx;
|
|
45
|
-
}
|
|
46
|
-
type RunOutcomeStatus = "completed" | "failed" | "cancelled";
|
|
47
|
-
/** A file the run produced, as stored with the outcome. Fetch it with the `download` handler. */
|
|
48
|
-
interface RunOutcomeAttachment {
|
|
49
|
-
id: string;
|
|
50
|
-
name: string | null;
|
|
51
|
-
mime: string;
|
|
52
|
-
}
|
|
53
|
-
/** How a run ended, as reported to `onRunEnd`. */
|
|
54
|
-
interface RunOutcome {
|
|
55
|
-
/** From `init` or `done`. `null` if the stream ended before either. */
|
|
56
|
-
runId: string | null;
|
|
57
|
-
/**
|
|
58
|
-
* `cancelled` means the browser went away before the run ended. The run
|
|
59
|
-
* itself may still finish on Seclai.
|
|
60
|
-
*/
|
|
61
|
-
status: RunOutcomeStatus;
|
|
62
|
-
/** The run's output when it completed, otherwise the text streamed so far. */
|
|
63
|
-
output: string;
|
|
64
|
-
/** Files from `done`. Empty unless the run sent `done`. */
|
|
65
|
-
attachments: RunOutcomeAttachment[];
|
|
66
|
-
/** Why the run failed. `null` unless `status` is `failed`. */
|
|
67
|
-
error: string | null;
|
|
68
|
-
}
|
|
69
|
-
type RunEventCallback<Ctx = undefined> = (event: RunEvent, ctx: RunCallbackContext<Ctx>) => void | Promise<void>;
|
|
70
|
-
type RunEndCallback<Ctx = undefined> = (outcome: RunOutcome, ctx: RunCallbackContext<Ctx>) => void | Promise<void>;
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* From this version uploads are `ready` at once and a run's `output` is plain
|
|
74
|
-
* text with files in `attachments`. The handlers rely on both.
|
|
75
|
-
*/
|
|
76
|
-
declare const MIN_API_VERSION: "2026-10-16";
|
|
77
|
-
/**
|
|
78
|
-
* A dated constant, not `SeclaiApiVersion.Latest`, which moves with SDK upgrades
|
|
79
|
-
* and could change response shapes underneath a deployed app.
|
|
80
|
-
*/
|
|
81
|
-
declare const DEFAULT_API_VERSION: "2026-10-28";
|
|
82
|
-
/**
|
|
83
|
-
* How long the agent's attachment references are reused by
|
|
84
|
-
* `getAttachmentReferences` and `upload.checkContentType`, which share them.
|
|
85
|
-
* Short, so a slot edited on Seclai takes effect within a minute, but long enough
|
|
86
|
-
* that a page load and a burst of uploads make one lookup.
|
|
87
|
-
*/
|
|
88
|
-
declare const ATTACHMENT_SLOTS_TTL_MS: number;
|
|
89
|
-
/** What the `upload` handler returns, and what `upload.onUpload` receives. */
|
|
90
|
-
interface UploadResult {
|
|
91
|
-
/** Pass this in `uploadIds` to the `run` handler. */
|
|
92
|
-
id: string;
|
|
93
|
-
status: string;
|
|
94
|
-
filename: string;
|
|
95
|
-
content_type: string;
|
|
96
|
-
file_size: number;
|
|
97
|
-
}
|
|
98
|
-
/** Checks and a hook for the `upload` handler. All are off by default. */
|
|
99
|
-
interface SeclaiUploadOptions<Ctx = undefined> {
|
|
100
|
-
/**
|
|
101
|
-
* Largest file accepted, in bytes. Anything bigger gets a 413 without reaching
|
|
102
|
-
* the API. A request whose `content-length` is too big is refused before its
|
|
103
|
-
* body is read; otherwise the body is counted as it streams and reading stops
|
|
104
|
-
* once it passes the limit plus 64 KB for the multipart envelope.
|
|
105
|
-
*/
|
|
106
|
-
maxBytes?: number;
|
|
107
|
-
/**
|
|
108
|
-
* Refuse with a 415, without reaching the API, a file whose type none of the
|
|
109
|
-
* agent's accepted attachment slots allows. A file of unknown type (none, or
|
|
110
|
-
* `application/octet-stream`, which a typeless file arrives as) isn't checked:
|
|
111
|
-
* the API decides. The slots are read with
|
|
112
|
-
* `getAgentAttachmentReferences` and reused for {@link ATTACHMENT_SLOTS_TTL_MS},
|
|
113
|
-
* sharing the lookup with {@link SeclaiLookups.getAttachmentReferences}.
|
|
114
|
-
* An agent with no slots accepts nothing, and a failed lookup refuses the
|
|
115
|
-
* upload.
|
|
116
|
-
*
|
|
117
|
-
* The reused slots belong to this `createSeclaiHandlers` instance. Where the
|
|
118
|
-
* handlers are created per request, as on Cloudflare Workers, every upload makes
|
|
119
|
-
* its own lookup.
|
|
120
|
-
*/
|
|
121
|
-
checkContentType?: boolean;
|
|
122
|
-
/**
|
|
123
|
-
* Called after a successful upload, for example to keep a copy of the file or
|
|
124
|
-
* record which session it belongs to. Return a `Response` to send it instead of
|
|
125
|
-
* the default JSON. `req`'s body has already been read. A throw becomes a 500.
|
|
126
|
-
*
|
|
127
|
-
* `context` is the app's context for this request, the same value the
|
|
128
|
-
* access-control hooks get. It is resolved before the file is uploaded, so a
|
|
129
|
-
* `context` that refuses does so before the file reaches Seclai.
|
|
130
|
-
*
|
|
131
|
-
* `params` is the upload URL's query string, as sent by `uploadFile`'s
|
|
132
|
-
* `params` option. See {@link AuthorizeUpload} for how it is parsed. It comes
|
|
133
|
-
* from the browser: validate it before you use it.
|
|
134
|
-
*/
|
|
135
|
-
onUpload?(file: File, result: UploadResult, req: Request, context: Ctx, params: UploadParams): void | Response | Promise<void | Response>;
|
|
136
|
-
}
|
|
137
|
-
interface SeclaiHandlersOptions<Ctx = undefined> {
|
|
138
|
-
/** Seclai API key. Server-side only: never expose it to the browser. */
|
|
139
|
-
apiKey: string;
|
|
140
|
-
/** The agent these handlers run. */
|
|
141
|
-
agentId: string;
|
|
142
|
-
/** Dated API version. Must be {@link MIN_API_VERSION} or later. */
|
|
143
|
-
apiVersion?: ApiVersion;
|
|
144
|
-
/** How long a streaming run may take, in milliseconds. Defaults to 11 minutes. */
|
|
145
|
-
runTimeoutMs?: number;
|
|
146
|
-
/** Size limit, content-type check and hook for the `upload` handler. */
|
|
147
|
-
upload?: SeclaiUploadOptions<Ctx>;
|
|
148
|
-
/**
|
|
149
|
-
* Called with each event of a run stream, in order, and awaited before the
|
|
150
|
-
* event is relayed to the browser. If it throws, the stream ends with an
|
|
151
|
-
* `error` event.
|
|
152
|
-
*/
|
|
153
|
-
onEvent?: RunEventCallback<Ctx>;
|
|
154
|
-
/**
|
|
155
|
-
* Called exactly once for each run, with how it ended. Awaited before the
|
|
156
|
-
* final `done`, `error` or `timeout` event is relayed, so whatever it stores
|
|
157
|
-
* is in place when the browser sees the run end. If it throws, the browser
|
|
158
|
-
* gets an `error` event instead.
|
|
159
|
-
*
|
|
160
|
-
* Also called, with `status: "failed"`, when the API refuses the run before
|
|
161
|
-
* it streams, or it fails before its first event (the API can't be reached,
|
|
162
|
-
* or `runTimeoutMs` runs out), and awaited before the error response is
|
|
163
|
-
* returned. The status is `cancelled` instead if the browser left while the
|
|
164
|
-
* stream was connecting. A throw there is only logged. Not called when
|
|
165
|
-
* `authorizeRun`, `context` or the body check refuses the run.
|
|
166
|
-
*/
|
|
167
|
-
onRunEnd?: RunEndCallback<Ctx>;
|
|
168
|
-
/**
|
|
169
|
-
* What happens to a run when the browser disconnects: closes the tab, moves
|
|
170
|
-
* to another page, or aborts the request. The run itself always carries on at
|
|
171
|
-
* Seclai and uses credits.
|
|
172
|
-
*
|
|
173
|
-
* - `false` (default): the handler stops reading it. `onRunEnd` reports
|
|
174
|
-
* `cancelled` with what had streamed so far.
|
|
175
|
-
* - `true`: the handler keeps reading it in the background until it ends,
|
|
176
|
-
* still calling `onEvent` for each event, so `onRunEnd` gets the real
|
|
177
|
-
* outcome, with its output and files. Use it when `onRunEnd` saves replies.
|
|
178
|
-
* Reading stops at `runTimeoutMs` at the latest.
|
|
179
|
-
*
|
|
180
|
-
* Only takes effect with `onEvent` or `onRunEnd` set: without them there is
|
|
181
|
-
* nothing to read the rest of the run for.
|
|
182
|
-
*/
|
|
183
|
-
keepReadingAfterDisconnect?: boolean;
|
|
184
|
-
/**
|
|
185
|
-
* Receives log lines about runs, uploads and downloads, with ids, timings and
|
|
186
|
-
* counts in `fields`. `console` works. Never given the API key, the input
|
|
187
|
-
* text or file contents. Without one, nothing is logged, and the few errors
|
|
188
|
-
* the handlers report themselves go to `console`.
|
|
189
|
-
*/
|
|
190
|
-
logger?: SeclaiLogger;
|
|
191
|
-
/**
|
|
192
|
-
* Your app's per-request context (for example the signed-in user), passed to
|
|
193
|
-
* each hook as its last argument. Called only when a hook is about to run.
|
|
194
|
-
* Throw `deny(401, …)` here to refuse every hooked request at once. To pass
|
|
195
|
-
* context your framework already has instead, use
|
|
196
|
-
* {@link WithContext.withContext}.
|
|
197
|
-
*/
|
|
198
|
-
context?: (req: Request) => Ctx | Promise<Ctx>;
|
|
199
|
-
/**
|
|
200
|
-
* Called after the `run` body has been validated, before the agent runs. Check
|
|
201
|
-
* `sessionId` and `uploadIds` against your own data here.
|
|
202
|
-
*
|
|
203
|
-
* Return nothing (or `true`) to run the body as it is, a rewritten body to run
|
|
204
|
-
* that instead (it is validated again), or `false` or a `Response` to refuse.
|
|
205
|
-
* `false` sends a 403. A rewritten body that fails validation gets a 500, and
|
|
206
|
-
* the reason is logged.
|
|
207
|
-
*/
|
|
208
|
-
authorizeRun?: AuthorizeRun<Ctx>;
|
|
209
|
-
/**
|
|
210
|
-
* Called after `upload` has read the form, before the file is sent to Seclai.
|
|
211
|
-
* Return nothing (or `true`) to upload it, or `false` or a `Response` to
|
|
212
|
-
* refuse. `false` sends a 403.
|
|
213
|
-
*
|
|
214
|
-
* Its last argument is the upload URL's query parameters (see
|
|
215
|
-
* {@link UploadParams}), for example a `sessionId` to check against your data.
|
|
216
|
-
*/
|
|
217
|
-
authorizeUpload?: AuthorizeUpload<Ctx>;
|
|
218
|
-
/**
|
|
219
|
-
* Called after `download` has checked the ids' format, before the API call.
|
|
220
|
-
* Return `true` to serve the file, or `false` or a `Response` to refuse.
|
|
221
|
-
* `false` sends a 404, so a refusal doesn't confirm the file exists.
|
|
222
|
-
*
|
|
223
|
-
* Unlike the other hooks this one fails closed: returning nothing also
|
|
224
|
-
* refuses with a 404, and logs a warning, because it guards stored files.
|
|
225
|
-
*/
|
|
226
|
-
authorizeDownload?: AuthorizeDownload<Ctx>;
|
|
227
|
-
}
|
|
228
|
-
/** See {@link SeclaiHandlersOptions.authorizeRun}. */
|
|
229
|
-
type AuthorizeRun<Ctx = undefined> = (req: Request, body: RunRequestBody, ctx: Ctx) => RunRequestBody | boolean | Response | void | Promise<RunRequestBody | boolean | Response | void>;
|
|
230
|
-
/**
|
|
231
|
-
* The query parameters of an upload request, for example the `sessionId` or
|
|
232
|
-
* the role of the file the browser sent with `uploadFile`'s `params` option.
|
|
233
|
-
* When a key is repeated, the last value wins. The object has no prototype, so
|
|
234
|
-
* a key the browser didn't send reads as `undefined`.
|
|
235
|
-
*
|
|
236
|
-
* They come from the browser and the handler doesn't check them: validate them
|
|
237
|
-
* (and check that the user may use them) in your hooks. The handler itself
|
|
238
|
-
* doesn't read them.
|
|
239
|
-
*/
|
|
240
|
-
type UploadParams = Record<string, string | undefined>;
|
|
241
|
-
/**
|
|
242
|
-
* See {@link SeclaiHandlersOptions.authorizeUpload}. `params` is the upload
|
|
243
|
-
* URL's query string: see {@link UploadParams}.
|
|
244
|
-
*/
|
|
245
|
-
type AuthorizeUpload<Ctx = undefined> = (req: Request, file: File, ctx: Ctx, params: UploadParams) => boolean | Response | void | Promise<boolean | Response | void>;
|
|
246
|
-
/** The ids a `download` request asks for. */
|
|
247
|
-
interface DownloadIds {
|
|
248
|
-
runId: string;
|
|
249
|
-
attachmentId: string;
|
|
250
|
-
}
|
|
251
|
-
/** See {@link SeclaiHandlersOptions.authorizeDownload}. */
|
|
252
|
-
type AuthorizeDownload<Ctx = undefined> = (req: Request, ids: DownloadIds, ctx: Ctx) => boolean | Response | Promise<boolean | Response>;
|
|
253
|
-
/** JSON body accepted by the `run` handler. */
|
|
254
|
-
interface RunRequestBody {
|
|
255
|
-
/** The prompt text. */
|
|
256
|
-
input?: string;
|
|
257
|
-
/** Ids returned by the `upload` handler, sent together with `input`. */
|
|
258
|
-
uploadIds?: string[];
|
|
259
|
-
/** Keys a conversation: sent to the agent as `metadata.session_id`. */
|
|
260
|
-
sessionId?: string;
|
|
261
|
-
}
|
|
262
|
-
type SeclaiHandler = (req: Request) => Promise<Response>;
|
|
263
|
-
/** Which files the agent accepts: the `attachmentReferences` handler's JSON body. */
|
|
264
|
-
type AttachmentReferences = AgentAttachmentRefsApiResponse;
|
|
265
|
-
interface SeclaiHandlers {
|
|
266
|
-
/** `GET`: which files the agent accepts. Always a fresh lookup. */
|
|
267
|
-
attachmentReferences: SeclaiHandler;
|
|
268
|
-
/** `POST` multipart form data with a `file` field: returns the upload's `id`. */
|
|
269
|
-
upload: SeclaiHandler;
|
|
270
|
-
/** `POST` a {@link RunRequestBody}: returns the run as a server-sent event stream. */
|
|
271
|
-
run: SeclaiHandler;
|
|
272
|
-
/** `GET ?runId=&attachmentId=&name=`: streams a file from a run. */
|
|
273
|
-
download: SeclaiHandler;
|
|
274
|
-
/**
|
|
275
|
-
* All four in one, for a single catch-all route: picks the handler from the
|
|
276
|
-
* last segment of the URL path (`attachment-references`, `upload`, `run` or
|
|
277
|
-
* `download`) and returns a 404 for anything else.
|
|
278
|
-
*/
|
|
279
|
-
handle: SeclaiHandler;
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* Server-side lookups offered next to the handlers. They need no request or
|
|
283
|
-
* context, so they're available both before and after `withContext`.
|
|
284
|
-
*/
|
|
285
|
-
interface SeclaiLookups {
|
|
286
|
-
/**
|
|
287
|
-
* Which files the agent accepts, for server-side code such as a loader that
|
|
288
|
-
* sets a composer's file limit. The same value as the `attachmentReferences`
|
|
289
|
-
* handler's body, reused for {@link ATTACHMENT_SLOTS_TTL_MS} and shared with
|
|
290
|
-
* `upload.checkContentType`. Each call gets its own copy.
|
|
291
|
-
*
|
|
292
|
-
* Rejects if the lookup fails, with the SDK's error. A failure isn't reused:
|
|
293
|
-
* the next call tries again.
|
|
294
|
-
*/
|
|
295
|
-
getAttachmentReferences(): Promise<AttachmentReferences>;
|
|
296
|
-
}
|
|
297
|
-
/** What {@link createSeclaiHandlers} adds to the handlers. */
|
|
298
|
-
interface WithContext<Ctx> extends SeclaiLookups {
|
|
299
|
-
/**
|
|
300
|
-
* The same handlers, with `ctx` passed to the hooks instead of calling the
|
|
301
|
-
* `context` option. Use it when your framework already knows the user:
|
|
302
|
-
*
|
|
303
|
-
* ```ts
|
|
304
|
-
* export const action = ({ request, context }) =>
|
|
305
|
-
* seclai.withContext({ userId: context.get(user).id }).handle(request);
|
|
306
|
-
* ```
|
|
307
|
-
*/
|
|
308
|
-
withContext(ctx: Ctx): SeclaiHandlers & SeclaiLookups;
|
|
309
|
-
}
|
|
310
|
-
/**
|
|
311
|
-
* A refusal for a hook to return or throw: the `{ error, status }` JSON the
|
|
312
|
-
* handlers use for their own errors, which `@seclai/react` shows to the user.
|
|
313
|
-
*
|
|
314
|
-
* ```ts
|
|
315
|
-
* authorizeRun(req, body, { userId }) {
|
|
316
|
-
* if (!ownsSession(userId, body.sessionId)) return deny(403, "That isn't your conversation.");
|
|
317
|
-
* }
|
|
318
|
-
* ```
|
|
319
|
-
*/
|
|
320
|
-
declare function deny(status: number, message: string, headers?: HeadersInit): Response;
|
|
321
|
-
/**
|
|
322
|
-
* The handlers {@link createSeclaiHandlers} returns without a `context` option.
|
|
323
|
-
* When the hooks need a context (`Ctx` doesn't include `undefined`), only
|
|
324
|
-
* `withContext` is offered, so a route can't call the hooks without one.
|
|
325
|
-
*/
|
|
326
|
-
type UnboundSeclaiHandlers<Ctx> = undefined extends Ctx ? SeclaiHandlers & WithContext<Ctx> : WithContext<Ctx>;
|
|
327
|
-
/**
|
|
328
|
-
* Create the server handlers for one agent. Each takes a Web `Request` and
|
|
329
|
-
* returns a Web `Response`, so they mount on any runtime with those APIs. Routing
|
|
330
|
-
* is left to the host framework.
|
|
331
|
-
*
|
|
332
|
-
* With a `context` option, the handlers resolve the hooks' context from each
|
|
333
|
-
* request. Without one, a typed context is passed with `withContext(ctx)`.
|
|
334
|
-
*
|
|
335
|
-
* @throws If `apiKey` or `agentId` is missing, or `apiVersion` is too old.
|
|
336
|
-
*/
|
|
337
|
-
declare function createSeclaiHandlers<Ctx>(options: SeclaiHandlersOptions<Ctx> & {
|
|
338
|
-
context: (req: Request) => Ctx | Promise<Ctx>;
|
|
339
|
-
}): SeclaiHandlers & WithContext<Ctx>;
|
|
340
|
-
declare function createSeclaiHandlers<Ctx = undefined>(options: SeclaiHandlersOptions<Ctx> & {
|
|
341
|
-
context?: undefined;
|
|
342
|
-
}): UnboundSeclaiHandlers<Ctx>;
|
|
343
|
-
|
|
344
|
-
export { ATTACHMENT_SLOTS_TTL_MS, type AttachmentReferences, type AuthorizeDownload, type AuthorizeRun, type AuthorizeUpload, DEFAULT_API_VERSION, type DownloadIds, MIN_API_VERSION, type RunCallbackContext, type RunEndCallback, type RunEvent, type RunEventCallback, type RunOutcome, type RunOutcomeAttachment, type RunOutcomeStatus, type RunRequestBody, type SeclaiHandler, type SeclaiHandlers, type SeclaiHandlersOptions, type SeclaiLogFields, type SeclaiLogLevel, type SeclaiLogger, type SeclaiLookups, type SeclaiUploadOptions, type UnboundSeclaiHandlers, type UploadParams, type UploadResult, type WithContext, consoleLogger, createSeclaiHandlers, deny };
|