@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.d.cts
CHANGED
|
@@ -1,5 +1,43 @@
|
|
|
1
1
|
import { ApiVersion } from '@seclai/sdk';
|
|
2
2
|
|
|
3
|
+
/** One event of the run stream, as the API sent it. `data` is the parsed JSON. */
|
|
4
|
+
interface RunEvent {
|
|
5
|
+
event: string;
|
|
6
|
+
data: unknown;
|
|
7
|
+
}
|
|
8
|
+
/** What the run callbacks get besides the event or outcome. The same object for the whole run. */
|
|
9
|
+
interface RunCallbackContext {
|
|
10
|
+
/** The validated body of the `run` request. */
|
|
11
|
+
body: RunRequestBody;
|
|
12
|
+
/** The browser's request to the `run` handler. */
|
|
13
|
+
request: Request;
|
|
14
|
+
}
|
|
15
|
+
type RunOutcomeStatus = "completed" | "failed" | "cancelled";
|
|
16
|
+
/** A file the run produced, as stored with the outcome. Fetch it with the `download` handler. */
|
|
17
|
+
interface RunOutcomeAttachment {
|
|
18
|
+
id: string;
|
|
19
|
+
name: string | null;
|
|
20
|
+
mime: string;
|
|
21
|
+
}
|
|
22
|
+
/** How a run ended, as reported to `onRunEnd`. */
|
|
23
|
+
interface RunOutcome {
|
|
24
|
+
/** From `init` or `done`. `null` if the stream ended before either. */
|
|
25
|
+
runId: string | null;
|
|
26
|
+
/**
|
|
27
|
+
* `cancelled` means the browser went away before the run ended. The run
|
|
28
|
+
* itself may still finish on Seclai.
|
|
29
|
+
*/
|
|
30
|
+
status: RunOutcomeStatus;
|
|
31
|
+
/** The run's output when it completed, otherwise the text streamed so far. */
|
|
32
|
+
output: string;
|
|
33
|
+
/** Files from `done`. Empty unless the run sent `done`. */
|
|
34
|
+
attachments: RunOutcomeAttachment[];
|
|
35
|
+
/** Why the run failed. `null` unless `status` is `failed`. */
|
|
36
|
+
error: string | null;
|
|
37
|
+
}
|
|
38
|
+
type RunEventCallback = (event: RunEvent, ctx: RunCallbackContext) => void | Promise<void>;
|
|
39
|
+
type RunEndCallback = (outcome: RunOutcome, ctx: RunCallbackContext) => void | Promise<void>;
|
|
40
|
+
|
|
3
41
|
/**
|
|
4
42
|
* From this version uploads are `ready` at once and a run's `output` is plain
|
|
5
43
|
* text with files in `attachments`. The handlers rely on both.
|
|
@@ -10,6 +48,51 @@ declare const MIN_API_VERSION: "2026-10-16";
|
|
|
10
48
|
* and could change response shapes underneath a deployed app.
|
|
11
49
|
*/
|
|
12
50
|
declare const DEFAULT_API_VERSION: "2026-10-28";
|
|
51
|
+
/**
|
|
52
|
+
* How long `upload.checkContentType` reuses the agent's attachment slots. Short,
|
|
53
|
+
* so a slot edited on Seclai takes effect within a minute, but long enough that a
|
|
54
|
+
* burst of uploads makes one lookup.
|
|
55
|
+
*/
|
|
56
|
+
declare const ATTACHMENT_SLOTS_TTL_MS: number;
|
|
57
|
+
/** What the `upload` handler returns, and what `upload.onUpload` receives. */
|
|
58
|
+
interface UploadResult {
|
|
59
|
+
/** Pass this in `uploadIds` to the `run` handler. */
|
|
60
|
+
id: string;
|
|
61
|
+
status: string;
|
|
62
|
+
filename: string;
|
|
63
|
+
content_type: string;
|
|
64
|
+
file_size: number;
|
|
65
|
+
}
|
|
66
|
+
/** Checks and a hook for the `upload` handler. All are off by default. */
|
|
67
|
+
interface SeclaiUploadOptions {
|
|
68
|
+
/**
|
|
69
|
+
* Largest file accepted, in bytes. Anything bigger gets a 413 without reaching
|
|
70
|
+
* the API. A request whose `content-length` is too big is refused before its
|
|
71
|
+
* body is read; otherwise the body is counted as it streams and reading stops
|
|
72
|
+
* once it passes the limit plus 64 KB for the multipart envelope.
|
|
73
|
+
*/
|
|
74
|
+
maxBytes?: number;
|
|
75
|
+
/**
|
|
76
|
+
* Refuse with a 415, without reaching the API, a file whose type none of the
|
|
77
|
+
* agent's accepted attachment slots allows. A file of unknown type (none, or
|
|
78
|
+
* `application/octet-stream`, which a typeless file arrives as) isn't checked:
|
|
79
|
+
* the API decides. The slots are read with
|
|
80
|
+
* `getAgentAttachmentReferences` and reused for {@link ATTACHMENT_SLOTS_TTL_MS}.
|
|
81
|
+
* An agent with no slots accepts nothing, and a failed lookup refuses the
|
|
82
|
+
* upload.
|
|
83
|
+
*
|
|
84
|
+
* The reused slots belong to this `createSeclaiHandlers` instance. Where the
|
|
85
|
+
* handlers are created per request, as on Cloudflare Workers, every upload makes
|
|
86
|
+
* its own lookup.
|
|
87
|
+
*/
|
|
88
|
+
checkContentType?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Called after a successful upload, for example to keep a copy of the file or
|
|
91
|
+
* record which session it belongs to. Return a `Response` to send it instead of
|
|
92
|
+
* the default JSON. `req`'s body has already been read. A throw becomes a 500.
|
|
93
|
+
*/
|
|
94
|
+
onUpload?(file: File, result: UploadResult, req: Request): void | Response | Promise<void | Response>;
|
|
95
|
+
}
|
|
13
96
|
interface SeclaiHandlersOptions<Ctx = undefined> {
|
|
14
97
|
/** Seclai API key. Server-side only: never expose it to the browser. */
|
|
15
98
|
apiKey: string;
|
|
@@ -19,6 +102,37 @@ interface SeclaiHandlersOptions<Ctx = undefined> {
|
|
|
19
102
|
apiVersion?: ApiVersion;
|
|
20
103
|
/** How long a streaming run may take, in milliseconds. Defaults to 11 minutes. */
|
|
21
104
|
runTimeoutMs?: number;
|
|
105
|
+
/** Size limit, content-type check and hook for the `upload` handler. */
|
|
106
|
+
upload?: SeclaiUploadOptions;
|
|
107
|
+
/**
|
|
108
|
+
* Called with each event of a run stream, in order, and awaited before the
|
|
109
|
+
* event is relayed to the browser. If it throws, the stream ends with an
|
|
110
|
+
* `error` event.
|
|
111
|
+
*/
|
|
112
|
+
onEvent?: RunEventCallback;
|
|
113
|
+
/**
|
|
114
|
+
* Called exactly once for each run that started streaming, with how it ended.
|
|
115
|
+
* Awaited before the final `done`, `error` or `timeout` event is relayed, so
|
|
116
|
+
* whatever it stores is in place when the browser sees the run end. If it
|
|
117
|
+
* throws, the browser gets an `error` event instead.
|
|
118
|
+
*/
|
|
119
|
+
onRunEnd?: RunEndCallback;
|
|
120
|
+
/**
|
|
121
|
+
* What happens to a run when the browser disconnects: closes the tab, moves
|
|
122
|
+
* to another page, or aborts the request. The run itself always carries on at
|
|
123
|
+
* Seclai and uses credits.
|
|
124
|
+
*
|
|
125
|
+
* - `false` (default): the handler stops reading it. `onRunEnd` reports
|
|
126
|
+
* `cancelled` with what had streamed so far.
|
|
127
|
+
* - `true`: the handler keeps reading it in the background until it ends,
|
|
128
|
+
* still calling `onEvent` for each event, so `onRunEnd` gets the real
|
|
129
|
+
* outcome, with its output and files. Use it when `onRunEnd` saves replies.
|
|
130
|
+
* Reading stops at `runTimeoutMs` at the latest.
|
|
131
|
+
*
|
|
132
|
+
* Only takes effect with `onEvent` or `onRunEnd` set: without them there is
|
|
133
|
+
* nothing to read the rest of the run for.
|
|
134
|
+
*/
|
|
135
|
+
keepReadingAfterDisconnect?: boolean;
|
|
22
136
|
/**
|
|
23
137
|
* Your app's per-request context (for example the signed-in user), passed to
|
|
24
138
|
* each hook as its last argument. Called only when a hook is about to run.
|
|
@@ -137,4 +251,4 @@ declare function createSeclaiHandlers<Ctx = undefined>(options: SeclaiHandlersOp
|
|
|
137
251
|
context?: undefined;
|
|
138
252
|
}): UnboundSeclaiHandlers<Ctx>;
|
|
139
253
|
|
|
140
|
-
export { type AuthorizeDownload, type AuthorizeRun, type AuthorizeUpload, DEFAULT_API_VERSION, type DownloadIds, MIN_API_VERSION, type RunRequestBody, type SeclaiHandler, type SeclaiHandlers, type SeclaiHandlersOptions, type UnboundSeclaiHandlers, type WithContext, createSeclaiHandlers, deny };
|
|
254
|
+
export { ATTACHMENT_SLOTS_TTL_MS, 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 SeclaiUploadOptions, type UnboundSeclaiHandlers, type UploadResult, type WithContext, createSeclaiHandlers, deny };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,43 @@
|
|
|
1
1
|
import { ApiVersion } from '@seclai/sdk';
|
|
2
2
|
|
|
3
|
+
/** One event of the run stream, as the API sent it. `data` is the parsed JSON. */
|
|
4
|
+
interface RunEvent {
|
|
5
|
+
event: string;
|
|
6
|
+
data: unknown;
|
|
7
|
+
}
|
|
8
|
+
/** What the run callbacks get besides the event or outcome. The same object for the whole run. */
|
|
9
|
+
interface RunCallbackContext {
|
|
10
|
+
/** The validated body of the `run` request. */
|
|
11
|
+
body: RunRequestBody;
|
|
12
|
+
/** The browser's request to the `run` handler. */
|
|
13
|
+
request: Request;
|
|
14
|
+
}
|
|
15
|
+
type RunOutcomeStatus = "completed" | "failed" | "cancelled";
|
|
16
|
+
/** A file the run produced, as stored with the outcome. Fetch it with the `download` handler. */
|
|
17
|
+
interface RunOutcomeAttachment {
|
|
18
|
+
id: string;
|
|
19
|
+
name: string | null;
|
|
20
|
+
mime: string;
|
|
21
|
+
}
|
|
22
|
+
/** How a run ended, as reported to `onRunEnd`. */
|
|
23
|
+
interface RunOutcome {
|
|
24
|
+
/** From `init` or `done`. `null` if the stream ended before either. */
|
|
25
|
+
runId: string | null;
|
|
26
|
+
/**
|
|
27
|
+
* `cancelled` means the browser went away before the run ended. The run
|
|
28
|
+
* itself may still finish on Seclai.
|
|
29
|
+
*/
|
|
30
|
+
status: RunOutcomeStatus;
|
|
31
|
+
/** The run's output when it completed, otherwise the text streamed so far. */
|
|
32
|
+
output: string;
|
|
33
|
+
/** Files from `done`. Empty unless the run sent `done`. */
|
|
34
|
+
attachments: RunOutcomeAttachment[];
|
|
35
|
+
/** Why the run failed. `null` unless `status` is `failed`. */
|
|
36
|
+
error: string | null;
|
|
37
|
+
}
|
|
38
|
+
type RunEventCallback = (event: RunEvent, ctx: RunCallbackContext) => void | Promise<void>;
|
|
39
|
+
type RunEndCallback = (outcome: RunOutcome, ctx: RunCallbackContext) => void | Promise<void>;
|
|
40
|
+
|
|
3
41
|
/**
|
|
4
42
|
* From this version uploads are `ready` at once and a run's `output` is plain
|
|
5
43
|
* text with files in `attachments`. The handlers rely on both.
|
|
@@ -10,6 +48,51 @@ declare const MIN_API_VERSION: "2026-10-16";
|
|
|
10
48
|
* and could change response shapes underneath a deployed app.
|
|
11
49
|
*/
|
|
12
50
|
declare const DEFAULT_API_VERSION: "2026-10-28";
|
|
51
|
+
/**
|
|
52
|
+
* How long `upload.checkContentType` reuses the agent's attachment slots. Short,
|
|
53
|
+
* so a slot edited on Seclai takes effect within a minute, but long enough that a
|
|
54
|
+
* burst of uploads makes one lookup.
|
|
55
|
+
*/
|
|
56
|
+
declare const ATTACHMENT_SLOTS_TTL_MS: number;
|
|
57
|
+
/** What the `upload` handler returns, and what `upload.onUpload` receives. */
|
|
58
|
+
interface UploadResult {
|
|
59
|
+
/** Pass this in `uploadIds` to the `run` handler. */
|
|
60
|
+
id: string;
|
|
61
|
+
status: string;
|
|
62
|
+
filename: string;
|
|
63
|
+
content_type: string;
|
|
64
|
+
file_size: number;
|
|
65
|
+
}
|
|
66
|
+
/** Checks and a hook for the `upload` handler. All are off by default. */
|
|
67
|
+
interface SeclaiUploadOptions {
|
|
68
|
+
/**
|
|
69
|
+
* Largest file accepted, in bytes. Anything bigger gets a 413 without reaching
|
|
70
|
+
* the API. A request whose `content-length` is too big is refused before its
|
|
71
|
+
* body is read; otherwise the body is counted as it streams and reading stops
|
|
72
|
+
* once it passes the limit plus 64 KB for the multipart envelope.
|
|
73
|
+
*/
|
|
74
|
+
maxBytes?: number;
|
|
75
|
+
/**
|
|
76
|
+
* Refuse with a 415, without reaching the API, a file whose type none of the
|
|
77
|
+
* agent's accepted attachment slots allows. A file of unknown type (none, or
|
|
78
|
+
* `application/octet-stream`, which a typeless file arrives as) isn't checked:
|
|
79
|
+
* the API decides. The slots are read with
|
|
80
|
+
* `getAgentAttachmentReferences` and reused for {@link ATTACHMENT_SLOTS_TTL_MS}.
|
|
81
|
+
* An agent with no slots accepts nothing, and a failed lookup refuses the
|
|
82
|
+
* upload.
|
|
83
|
+
*
|
|
84
|
+
* The reused slots belong to this `createSeclaiHandlers` instance. Where the
|
|
85
|
+
* handlers are created per request, as on Cloudflare Workers, every upload makes
|
|
86
|
+
* its own lookup.
|
|
87
|
+
*/
|
|
88
|
+
checkContentType?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Called after a successful upload, for example to keep a copy of the file or
|
|
91
|
+
* record which session it belongs to. Return a `Response` to send it instead of
|
|
92
|
+
* the default JSON. `req`'s body has already been read. A throw becomes a 500.
|
|
93
|
+
*/
|
|
94
|
+
onUpload?(file: File, result: UploadResult, req: Request): void | Response | Promise<void | Response>;
|
|
95
|
+
}
|
|
13
96
|
interface SeclaiHandlersOptions<Ctx = undefined> {
|
|
14
97
|
/** Seclai API key. Server-side only: never expose it to the browser. */
|
|
15
98
|
apiKey: string;
|
|
@@ -19,6 +102,37 @@ interface SeclaiHandlersOptions<Ctx = undefined> {
|
|
|
19
102
|
apiVersion?: ApiVersion;
|
|
20
103
|
/** How long a streaming run may take, in milliseconds. Defaults to 11 minutes. */
|
|
21
104
|
runTimeoutMs?: number;
|
|
105
|
+
/** Size limit, content-type check and hook for the `upload` handler. */
|
|
106
|
+
upload?: SeclaiUploadOptions;
|
|
107
|
+
/**
|
|
108
|
+
* Called with each event of a run stream, in order, and awaited before the
|
|
109
|
+
* event is relayed to the browser. If it throws, the stream ends with an
|
|
110
|
+
* `error` event.
|
|
111
|
+
*/
|
|
112
|
+
onEvent?: RunEventCallback;
|
|
113
|
+
/**
|
|
114
|
+
* Called exactly once for each run that started streaming, with how it ended.
|
|
115
|
+
* Awaited before the final `done`, `error` or `timeout` event is relayed, so
|
|
116
|
+
* whatever it stores is in place when the browser sees the run end. If it
|
|
117
|
+
* throws, the browser gets an `error` event instead.
|
|
118
|
+
*/
|
|
119
|
+
onRunEnd?: RunEndCallback;
|
|
120
|
+
/**
|
|
121
|
+
* What happens to a run when the browser disconnects: closes the tab, moves
|
|
122
|
+
* to another page, or aborts the request. The run itself always carries on at
|
|
123
|
+
* Seclai and uses credits.
|
|
124
|
+
*
|
|
125
|
+
* - `false` (default): the handler stops reading it. `onRunEnd` reports
|
|
126
|
+
* `cancelled` with what had streamed so far.
|
|
127
|
+
* - `true`: the handler keeps reading it in the background until it ends,
|
|
128
|
+
* still calling `onEvent` for each event, so `onRunEnd` gets the real
|
|
129
|
+
* outcome, with its output and files. Use it when `onRunEnd` saves replies.
|
|
130
|
+
* Reading stops at `runTimeoutMs` at the latest.
|
|
131
|
+
*
|
|
132
|
+
* Only takes effect with `onEvent` or `onRunEnd` set: without them there is
|
|
133
|
+
* nothing to read the rest of the run for.
|
|
134
|
+
*/
|
|
135
|
+
keepReadingAfterDisconnect?: boolean;
|
|
22
136
|
/**
|
|
23
137
|
* Your app's per-request context (for example the signed-in user), passed to
|
|
24
138
|
* each hook as its last argument. Called only when a hook is about to run.
|
|
@@ -137,4 +251,4 @@ declare function createSeclaiHandlers<Ctx = undefined>(options: SeclaiHandlersOp
|
|
|
137
251
|
context?: undefined;
|
|
138
252
|
}): UnboundSeclaiHandlers<Ctx>;
|
|
139
253
|
|
|
140
|
-
export { type AuthorizeDownload, type AuthorizeRun, type AuthorizeUpload, DEFAULT_API_VERSION, type DownloadIds, MIN_API_VERSION, type RunRequestBody, type SeclaiHandler, type SeclaiHandlers, type SeclaiHandlersOptions, type UnboundSeclaiHandlers, type WithContext, createSeclaiHandlers, deny };
|
|
254
|
+
export { ATTACHMENT_SLOTS_TTL_MS, 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 SeclaiUploadOptions, type UnboundSeclaiHandlers, type UploadResult, type WithContext, createSeclaiHandlers, deny };
|