@seclai/sdk 1.8.0 → 1.10.0
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/CHANGELOG.md +40 -0
- package/README.md +194 -14
- package/dist/index.cjs +310 -47
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +523 -76
- package/dist/index.d.ts +523 -76
- package/dist/index.js +310 -47
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -14,10 +14,16 @@ var SeclaiApiVersion = {
|
|
|
14
14
|
V2026_10_19: "2026-10-19",
|
|
15
15
|
V2026_10_21: "2026-10-21",
|
|
16
16
|
V2026_10_28: "2026-10-28",
|
|
17
|
+
V2026_11_02: "2026-11-02",
|
|
18
|
+
V2026_11_04: "2026-11-04",
|
|
19
|
+
V2026_11_08: "2026-11-08",
|
|
20
|
+
V2026_11_09: "2026-11-09",
|
|
21
|
+
V2026_11_14: "2026-11-14",
|
|
22
|
+
V2026_11_19: "2026-11-19",
|
|
17
23
|
/** Baseline applied to an unpinned, header-less caller. */
|
|
18
24
|
Default: "2026-07-01",
|
|
19
25
|
/** Newest version known to this SDK release. May lag the server. */
|
|
20
|
-
Latest: "2026-
|
|
26
|
+
Latest: "2026-11-19"
|
|
21
27
|
};
|
|
22
28
|
var KNOWN_API_VERSIONS = Object.entries(SeclaiApiVersion).filter(([k]) => k.startsWith("V")).map(([, v]) => v);
|
|
23
29
|
|
|
@@ -43,6 +49,14 @@ var SeclaiAPIStatusError = class extends SeclaiError {
|
|
|
43
49
|
url;
|
|
44
50
|
/** Best-effort response body text (if available). */
|
|
45
51
|
responseText;
|
|
52
|
+
/**
|
|
53
|
+
* Headers of the error response, such as `Retry-After` on a 503, or
|
|
54
|
+
* `Content-Range` on a 416 from a ranged `downloadAgentRunAttachment()`, whose
|
|
55
|
+
* value ends in `/<size>`, the file's size in bytes. Empty when the response
|
|
56
|
+
* headers are unknown. In a browser, only the headers the server's CORS policy
|
|
57
|
+
* exposes are present.
|
|
58
|
+
*/
|
|
59
|
+
headers;
|
|
46
60
|
constructor(opts) {
|
|
47
61
|
super(opts.message);
|
|
48
62
|
this.name = "SeclaiAPIStatusError";
|
|
@@ -50,6 +64,7 @@ var SeclaiAPIStatusError = class extends SeclaiError {
|
|
|
50
64
|
this.method = opts.method;
|
|
51
65
|
this.url = opts.url;
|
|
52
66
|
this.responseText = opts.responseText;
|
|
67
|
+
this.headers = opts.headers ?? new Headers();
|
|
53
68
|
}
|
|
54
69
|
};
|
|
55
70
|
var SeclaiAPIValidationError = class extends SeclaiAPIStatusError {
|
|
@@ -64,10 +79,17 @@ var SeclaiAPIValidationError = class extends SeclaiAPIStatusError {
|
|
|
64
79
|
var SeclaiStreamingError = class extends SeclaiError {
|
|
65
80
|
/** The run ID associated with the failed stream, when available. */
|
|
66
81
|
runId;
|
|
67
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Set by `runStreamingAgentAndWait()` and `runAgentAndPoll()` to the latest
|
|
84
|
+
* run they had, if any (when it has `result_reported_at` set, its `output`
|
|
85
|
+
* is the answer). The run may still be in progress, and can still fail.
|
|
86
|
+
*/
|
|
87
|
+
run;
|
|
88
|
+
constructor(message, runId, run) {
|
|
68
89
|
super(message);
|
|
69
90
|
this.name = "SeclaiStreamingError";
|
|
70
91
|
this.runId = runId;
|
|
92
|
+
this.run = run;
|
|
71
93
|
}
|
|
72
94
|
};
|
|
73
95
|
|
|
@@ -420,6 +442,34 @@ function mergeHeaders(...layers) {
|
|
|
420
442
|
function headerKey(headers, name) {
|
|
421
443
|
return Object.keys(headers ?? {}).filter((key) => key.toLowerCase() === name).pop();
|
|
422
444
|
}
|
|
445
|
+
function byteRangeHeader(start, end, suffix) {
|
|
446
|
+
const hasStart = start !== void 0 && start !== null;
|
|
447
|
+
const hasEnd = end !== void 0 && end !== null;
|
|
448
|
+
const hasSuffix = suffix !== void 0 && suffix !== null;
|
|
449
|
+
if (!hasStart && !hasEnd && !hasSuffix) return void 0;
|
|
450
|
+
const fail = (why) => {
|
|
451
|
+
throw new SeclaiConfigurationError(`Invalid byte range: ${why}`);
|
|
452
|
+
};
|
|
453
|
+
if (hasSuffix) {
|
|
454
|
+
if (hasStart || hasEnd) fail("rangeSuffix cannot be combined with rangeStart or rangeEnd.");
|
|
455
|
+
if (!Number.isSafeInteger(suffix) || suffix < 1) {
|
|
456
|
+
fail(`rangeSuffix must be a whole number of at least 1, got ${String(suffix)}.`);
|
|
457
|
+
}
|
|
458
|
+
return `bytes=-${suffix}`;
|
|
459
|
+
}
|
|
460
|
+
if (!hasStart) fail("rangeEnd requires rangeStart; use rangeSuffix for the last bytes of the file.");
|
|
461
|
+
if (!Number.isSafeInteger(start) || start < 0) {
|
|
462
|
+
fail(`rangeStart must be a whole number of at least 0, got ${String(start)}.`);
|
|
463
|
+
}
|
|
464
|
+
if (!hasEnd) return `bytes=${start}-`;
|
|
465
|
+
if (!Number.isSafeInteger(end) || end < 0) {
|
|
466
|
+
fail(`rangeEnd must be a whole number of at least 0, got ${String(end)}.`);
|
|
467
|
+
}
|
|
468
|
+
if (end < start) {
|
|
469
|
+
fail(`rangeEnd (${end}) must not be less than rangeStart (${start}).`);
|
|
470
|
+
}
|
|
471
|
+
return `bytes=${start}-${end}`;
|
|
472
|
+
}
|
|
423
473
|
async function safeText(response) {
|
|
424
474
|
try {
|
|
425
475
|
return await response.clone().text();
|
|
@@ -686,6 +736,7 @@ var Seclai = class {
|
|
|
686
736
|
method,
|
|
687
737
|
url: url.toString(),
|
|
688
738
|
responseText,
|
|
739
|
+
headers: new Headers(response.headers),
|
|
689
740
|
validationError: validation
|
|
690
741
|
});
|
|
691
742
|
}
|
|
@@ -694,7 +745,8 @@ var Seclai = class {
|
|
|
694
745
|
statusCode: response.status,
|
|
695
746
|
method,
|
|
696
747
|
url: url.toString(),
|
|
697
|
-
responseText
|
|
748
|
+
responseText,
|
|
749
|
+
headers: new Headers(response.headers)
|
|
698
750
|
});
|
|
699
751
|
}
|
|
700
752
|
if (!response.body) return null;
|
|
@@ -741,6 +793,7 @@ var Seclai = class {
|
|
|
741
793
|
method,
|
|
742
794
|
url: url.toString(),
|
|
743
795
|
responseText,
|
|
796
|
+
headers: new Headers(response.headers),
|
|
744
797
|
validationError: validation
|
|
745
798
|
});
|
|
746
799
|
}
|
|
@@ -749,7 +802,8 @@ var Seclai = class {
|
|
|
749
802
|
statusCode: response.status,
|
|
750
803
|
method,
|
|
751
804
|
url: url.toString(),
|
|
752
|
-
responseText
|
|
805
|
+
responseText,
|
|
806
|
+
headers: new Headers(response.headers)
|
|
753
807
|
});
|
|
754
808
|
}
|
|
755
809
|
return response;
|
|
@@ -780,6 +834,7 @@ var Seclai = class {
|
|
|
780
834
|
method: "POST",
|
|
781
835
|
url: url.toString(),
|
|
782
836
|
responseText,
|
|
837
|
+
headers: new Headers(response.headers),
|
|
783
838
|
validationError: validation
|
|
784
839
|
});
|
|
785
840
|
}
|
|
@@ -788,7 +843,8 @@ var Seclai = class {
|
|
|
788
843
|
statusCode: response.status,
|
|
789
844
|
method: "POST",
|
|
790
845
|
url: url.toString(),
|
|
791
|
-
responseText
|
|
846
|
+
responseText,
|
|
847
|
+
headers: new Headers(response.headers)
|
|
792
848
|
});
|
|
793
849
|
}
|
|
794
850
|
return await response.json();
|
|
@@ -854,6 +910,10 @@ var Seclai = class {
|
|
|
854
910
|
/**
|
|
855
911
|
* List the agent gallery: example agents to start from.
|
|
856
912
|
*
|
|
913
|
+
* From `apiVersion` `2026-11-09`, `query` describes what the agent should do
|
|
914
|
+
* and entries come back ranked by how well they match it, best first. Before
|
|
915
|
+
* it, only entries containing every word of `query` are returned.
|
|
916
|
+
*
|
|
857
917
|
* Pass an entry's `key` as `agent_template` to {@link createAgent}, with
|
|
858
918
|
* `resource_bindings` for the resources it needs.
|
|
859
919
|
*
|
|
@@ -1007,6 +1067,11 @@ var Seclai = class {
|
|
|
1007
1067
|
/**
|
|
1008
1068
|
* Start an agent run.
|
|
1009
1069
|
*
|
|
1070
|
+
* From `apiVersion` `2026-11-14`, an agent whose definition fails to validate
|
|
1071
|
+
* is refused with HTTP 409 ({@link SeclaiAPIStatusError}) and no run is
|
|
1072
|
+
* created; earlier versions return the run, already failed with the
|
|
1073
|
+
* validation error as its cause.
|
|
1074
|
+
*
|
|
1010
1075
|
* @param agentId - Agent identifier.
|
|
1011
1076
|
* @param body - Run request payload (`input`, `metadata`, `priority`, etc.).
|
|
1012
1077
|
* @returns The created agent run.
|
|
@@ -1060,14 +1125,17 @@ var Seclai = class {
|
|
|
1060
1125
|
await this.cancelAgentRun(runId);
|
|
1061
1126
|
}
|
|
1062
1127
|
/**
|
|
1063
|
-
* Cancel
|
|
1128
|
+
* Cancel an agent run that has not ended: `queued`, `pending`, `processing`,
|
|
1129
|
+
* `waiting_human` or `waiting_scheduled`.
|
|
1064
1130
|
*
|
|
1065
1131
|
* Cancellation is `DELETE` on the run resource — the API exposes no
|
|
1066
1132
|
* `POST .../cancel` route, and no operation that deletes a run. Rejected when
|
|
1067
|
-
* the run has already
|
|
1133
|
+
* the run has already ended (`completed`, `failed` or `cancelled`). From
|
|
1134
|
+
* `apiVersion` `2026-11-08` the cancelled run ends with status `cancelled`;
|
|
1135
|
+
* on earlier versions it ends `failed`.
|
|
1068
1136
|
*
|
|
1069
1137
|
* @param runId - Run identifier.
|
|
1070
|
-
* @returns
|
|
1138
|
+
* @returns The updated run.
|
|
1071
1139
|
*/
|
|
1072
1140
|
async cancelAgentRun(runId) {
|
|
1073
1141
|
return await this.request("DELETE", `/agents/runs/${runId}`);
|
|
@@ -1080,17 +1148,58 @@ var Seclai = class {
|
|
|
1080
1148
|
*
|
|
1081
1149
|
* Consumes the entire SSE stream and returns only the terminal `done` payload.
|
|
1082
1150
|
* For real-time event access, use {@link runStreamingAgent} instead. The body
|
|
1083
|
-
* takes no `include_reasoning`, and one passed anyway
|
|
1084
|
-
* returns no `stream_reasoning`
|
|
1151
|
+
* takes no `include_reasoning` or `include_commentary`, and one passed anyway
|
|
1152
|
+
* is not sent: this method returns no `stream_reasoning` or
|
|
1153
|
+
* `stream_commentary` events.
|
|
1154
|
+
*
|
|
1155
|
+
* A `result` event carries the run's answer in `output` once
|
|
1156
|
+
* `result_reported_at` is set, and can arrive while later steps still run.
|
|
1157
|
+
* Pass `returnOnResult: true` to return that `result` snapshot as soon as it
|
|
1158
|
+
* arrives, without waiting for `done`. Such a run may still be in progress:
|
|
1159
|
+
* its `status` is not terminal and the run can still fail, so check
|
|
1160
|
+
* `result_reported_at`, not `status`.
|
|
1161
|
+
*
|
|
1162
|
+
* If `timeoutMs` elapses, this throws {@link SeclaiStreamingError} with the
|
|
1163
|
+
* latest snapshot in `run`: the `result` snapshot if one arrived, else the
|
|
1164
|
+
* `init` snapshot. An abort from `signal` rethrows the abort error.
|
|
1165
|
+
*
|
|
1166
|
+
* By default, a stream that ends before `done` returns the latest snapshot
|
|
1167
|
+
* when its status is not `pending`, so the run returned may not have
|
|
1168
|
+
* finished: check `status` and `result_reported_at`. A stream that breaks
|
|
1169
|
+
* throws the fetch error. Pass `throwOnIncomplete: true` to throw
|
|
1170
|
+
* {@link SeclaiStreamingError} with the snapshot in `run` in both cases
|
|
1171
|
+
* instead; it is recommended, and becomes the default in the next major
|
|
1172
|
+
* version.
|
|
1085
1173
|
*
|
|
1086
1174
|
* @param agentId - Agent identifier.
|
|
1087
|
-
* @param body - Streaming run request payload, without `include_reasoning
|
|
1088
|
-
*
|
|
1089
|
-
* @
|
|
1090
|
-
* @
|
|
1175
|
+
* @param body - Streaming run request payload, without `include_reasoning`
|
|
1176
|
+
* or `include_commentary`.
|
|
1177
|
+
* @param opts - Timeout, abort signal and `returnOnResult` options.
|
|
1178
|
+
* @returns The run from the `done` event; with `returnOnResult`, the
|
|
1179
|
+
* `result` snapshot; without `throwOnIncomplete`, possibly an unfinished
|
|
1180
|
+
* snapshot (see above).
|
|
1181
|
+
* @throws {@link SeclaiStreamingError} If `timeoutMs` elapses, or, with
|
|
1182
|
+
* `throwOnIncomplete`, the stream ends or breaks, before the run finished.
|
|
1183
|
+
* Its `run` holds the latest snapshot.
|
|
1184
|
+
*
|
|
1185
|
+
* @example
|
|
1186
|
+
* ```ts
|
|
1187
|
+
* try {
|
|
1188
|
+
* const run = await client.runStreamingAgentAndWait("agent-id", { input: "Hello" }, { throwOnIncomplete: true });
|
|
1189
|
+
* console.log(run.output);
|
|
1190
|
+
* } catch (err) {
|
|
1191
|
+
* if (err instanceof SeclaiStreamingError && err.run?.result_reported_at) {
|
|
1192
|
+
* console.log("Answer (run not finished):", err.run.output);
|
|
1193
|
+
* } else throw err;
|
|
1194
|
+
* }
|
|
1195
|
+
* ```
|
|
1091
1196
|
*/
|
|
1092
1197
|
async runStreamingAgentAndWait(agentId, body, opts) {
|
|
1093
|
-
const {
|
|
1198
|
+
const {
|
|
1199
|
+
include_reasoning: _reasoning,
|
|
1200
|
+
include_commentary: _commentary,
|
|
1201
|
+
...runBody
|
|
1202
|
+
} = body;
|
|
1094
1203
|
const url = buildURL(this.baseUrl, `/agents/${agentId}/runs/stream`);
|
|
1095
1204
|
const authHdrs = await this.authHeaders();
|
|
1096
1205
|
const headers = this.requestHeaders([
|
|
@@ -1107,6 +1216,7 @@ var Seclai = class {
|
|
|
1107
1216
|
}, timeoutMs);
|
|
1108
1217
|
const signal = anySignal([opts?.signal, timeoutController.signal]);
|
|
1109
1218
|
let lastSeen;
|
|
1219
|
+
let reported;
|
|
1110
1220
|
try {
|
|
1111
1221
|
const init = { method: "POST", headers, body: JSON.stringify(runBody) };
|
|
1112
1222
|
if (signal) init.signal = signal;
|
|
@@ -1123,6 +1233,7 @@ var Seclai = class {
|
|
|
1123
1233
|
method: "POST",
|
|
1124
1234
|
url: url.toString(),
|
|
1125
1235
|
responseText,
|
|
1236
|
+
headers: new Headers(response.headers),
|
|
1126
1237
|
validationError: validation
|
|
1127
1238
|
});
|
|
1128
1239
|
}
|
|
@@ -1131,11 +1242,19 @@ var Seclai = class {
|
|
|
1131
1242
|
statusCode: response.status,
|
|
1132
1243
|
method: "POST",
|
|
1133
1244
|
url: url.toString(),
|
|
1134
|
-
responseText
|
|
1245
|
+
responseText,
|
|
1246
|
+
headers: new Headers(response.headers)
|
|
1135
1247
|
});
|
|
1136
1248
|
}
|
|
1137
1249
|
if (isJson) {
|
|
1138
|
-
|
|
1250
|
+
const run2 = await response.json();
|
|
1251
|
+
const status = run2.status;
|
|
1252
|
+
const finished = status === "completed" || status === "failed" || status === "cancelled" || Boolean(opts?.returnOnResult && run2.result_reported_at);
|
|
1253
|
+
if (opts?.throwOnIncomplete && !finished) {
|
|
1254
|
+
const state2 = run2.result_reported_at ? "A result had been reported; see `run`." : "No result had been reported.";
|
|
1255
|
+
throw new SeclaiStreamingError(`The server answered with JSON for a run that has not finished. ${state2}`, run2.run_id, run2);
|
|
1256
|
+
}
|
|
1257
|
+
return run2;
|
|
1139
1258
|
}
|
|
1140
1259
|
if (!response.body) {
|
|
1141
1260
|
throw new SeclaiConfigurationError(
|
|
@@ -1147,31 +1266,48 @@ var Seclai = class {
|
|
|
1147
1266
|
let final;
|
|
1148
1267
|
const parser = createSseParser((msg) => {
|
|
1149
1268
|
if (!msg.data) return;
|
|
1150
|
-
if (msg.event === "init" || msg.event === "done") {
|
|
1269
|
+
if (msg.event === "init" || msg.event === "result" || msg.event === "done") {
|
|
1151
1270
|
try {
|
|
1152
1271
|
const parsed = JSON.parse(msg.data);
|
|
1153
1272
|
lastSeen = parsed;
|
|
1273
|
+
if (msg.event === "result") reported = parsed;
|
|
1154
1274
|
if (msg.event === "done") final = parsed;
|
|
1155
1275
|
} catch {
|
|
1156
1276
|
}
|
|
1157
1277
|
}
|
|
1158
1278
|
});
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1279
|
+
let readFailure;
|
|
1280
|
+
try {
|
|
1281
|
+
while (!final && !(opts?.returnOnResult && reported)) {
|
|
1282
|
+
const { value, done } = await reader.read();
|
|
1283
|
+
if (done) break;
|
|
1284
|
+
if (value) parser.feed(decoder.decode(value, { stream: true }));
|
|
1285
|
+
}
|
|
1286
|
+
} catch (err) {
|
|
1287
|
+
if (opts?.signal?.aborted || !opts?.throwOnIncomplete && !timedOut) throw err;
|
|
1288
|
+
readFailure = err instanceof Error ? err.message : String(err);
|
|
1163
1289
|
}
|
|
1290
|
+
parser.feed(decoder.decode());
|
|
1164
1291
|
parser.end();
|
|
1165
1292
|
if (final) return final;
|
|
1166
|
-
if (
|
|
1293
|
+
if (opts?.returnOnResult && reported) {
|
|
1294
|
+
reader.cancel().catch(() => {
|
|
1295
|
+
});
|
|
1296
|
+
return reported;
|
|
1297
|
+
}
|
|
1298
|
+
if (!opts?.throwOnIncomplete && !timedOut && lastSeen?.status && lastSeen.status !== "pending") {
|
|
1167
1299
|
return lastSeen;
|
|
1168
1300
|
}
|
|
1169
|
-
|
|
1301
|
+
const run = reported ?? lastSeen;
|
|
1302
|
+
const state = reported ? "A result had been reported; see `run`." : "No result had been reported.";
|
|
1303
|
+
const reason = timedOut ? `Timed out after ${timeoutMs}ms before the streaming agent run finished.` : readFailure !== void 0 ? `Stream failed before the run finished: ${readFailure}.` : "Stream ended before receiving a 'done' event.";
|
|
1304
|
+
throw new SeclaiStreamingError(`${reason} ${state}`, run?.run_id, run);
|
|
1170
1305
|
} catch (err) {
|
|
1171
|
-
if (timedOut) {
|
|
1306
|
+
if (timedOut && !opts?.signal?.aborted && !(err instanceof SeclaiStreamingError)) {
|
|
1172
1307
|
throw new SeclaiStreamingError(
|
|
1173
1308
|
`Timed out after ${timeoutMs}ms waiting for streaming agent run to complete.`,
|
|
1174
|
-
lastSeen?.run_id
|
|
1309
|
+
lastSeen?.run_id,
|
|
1310
|
+
reported ?? lastSeen
|
|
1175
1311
|
);
|
|
1176
1312
|
}
|
|
1177
1313
|
throw err;
|
|
@@ -1183,7 +1319,13 @@ var Seclai = class {
|
|
|
1183
1319
|
* Run an agent in streaming mode and yield each SSE event as it arrives.
|
|
1184
1320
|
*
|
|
1185
1321
|
* This is an `AsyncGenerator` suitable for real-time UIs that want to render
|
|
1186
|
-
* step progress as it happens.
|
|
1322
|
+
* step progress as it happens. Set `include_reasoning` or
|
|
1323
|
+
* `include_commentary` in `body` to also receive `stream_reasoning` or
|
|
1324
|
+
* `stream_commentary` events.
|
|
1325
|
+
*
|
|
1326
|
+
* From `apiVersion` `2026-11-14`, an agent whose definition fails to validate
|
|
1327
|
+
* is refused with HTTP 409 ({@link SeclaiAPIStatusError}) before any event,
|
|
1328
|
+
* and no run is created.
|
|
1187
1329
|
*
|
|
1188
1330
|
* @param agentId - Agent identifier.
|
|
1189
1331
|
* @param body - Streaming run request payload.
|
|
@@ -1193,6 +1335,10 @@ var Seclai = class {
|
|
|
1193
1335
|
* @example
|
|
1194
1336
|
* ```ts
|
|
1195
1337
|
* for await (const event of client.runStreamingAgent("agent-id", { input: "Hello!" })) {
|
|
1338
|
+
* // `result` carries the answer in `output`, possibly before later steps finish.
|
|
1339
|
+
* if (event.event === "result") {
|
|
1340
|
+
* console.log("Answer:", (event.data as AgentRunResponse).output);
|
|
1341
|
+
* }
|
|
1196
1342
|
* if (event.event === "done") {
|
|
1197
1343
|
* console.log("Final:", event.data);
|
|
1198
1344
|
* }
|
|
@@ -1231,6 +1377,7 @@ var Seclai = class {
|
|
|
1231
1377
|
method: "POST",
|
|
1232
1378
|
url: url.toString(),
|
|
1233
1379
|
responseText,
|
|
1380
|
+
headers: new Headers(response.headers),
|
|
1234
1381
|
validationError: validation
|
|
1235
1382
|
});
|
|
1236
1383
|
}
|
|
@@ -1239,7 +1386,8 @@ var Seclai = class {
|
|
|
1239
1386
|
statusCode: response.status,
|
|
1240
1387
|
method: "POST",
|
|
1241
1388
|
url: url.toString(),
|
|
1242
|
-
responseText
|
|
1389
|
+
responseText,
|
|
1390
|
+
headers: new Headers(response.headers)
|
|
1243
1391
|
});
|
|
1244
1392
|
}
|
|
1245
1393
|
if (isJson) {
|
|
@@ -1296,11 +1444,38 @@ var Seclai = class {
|
|
|
1296
1444
|
*
|
|
1297
1445
|
* This is useful in environments where SSE streaming is unavailable.
|
|
1298
1446
|
*
|
|
1447
|
+
* A run's answer can be ready before its last step finishes: once
|
|
1448
|
+
* `result_reported_at` is set, `output` holds the answer. Pass
|
|
1449
|
+
* `returnOnResult: true` to return as soon as a run has it set, the run
|
|
1450
|
+
* `runAgent()` returns included. With `includeStepOutputs`, when that run
|
|
1451
|
+
* would end the poll and `signal` is not aborted, it is fetched again at
|
|
1452
|
+
* once for its step outputs.
|
|
1453
|
+
* Such a run may still be in progress: its `status` is not terminal and the
|
|
1454
|
+
* run can still fail, so check `result_reported_at`, not `status`.
|
|
1455
|
+
*
|
|
1456
|
+
* When `timeoutMs` elapses first, this throws {@link SeclaiStreamingError}
|
|
1457
|
+
* with the latest run it has (the `runAgent()` response if no poll ran) in
|
|
1458
|
+
* `run`. If that run has `result_reported_at`
|
|
1459
|
+
* set, its `output` is the answer.
|
|
1460
|
+
*
|
|
1461
|
+
* `signal` is checked at the top of each poll, after the check for a run
|
|
1462
|
+
* that ends the poll. A run that has ended the poll by then, whether the
|
|
1463
|
+
* start run or a polled one, is returned even after an abort; the start run
|
|
1464
|
+
* then comes back without step outputs, even when `includeStepOutputs` is
|
|
1465
|
+
* set. Otherwise an abort throws `SeclaiError("Polling aborted.")`, which is
|
|
1466
|
+
* not an `AbortError` and does not carry `signal.reason`.
|
|
1467
|
+
*
|
|
1299
1468
|
* @param agentId - Agent identifier.
|
|
1300
1469
|
* @param body - Run request payload.
|
|
1301
1470
|
* @param opts - Polling configuration and abort signal.
|
|
1302
|
-
* @returns The terminal agent run
|
|
1303
|
-
*
|
|
1471
|
+
* @returns The terminal agent run, or, with `returnOnResult`, the first run
|
|
1472
|
+
* with `result_reported_at` set.
|
|
1473
|
+
* @throws {@link SeclaiStreamingError} If `timeoutMs` elapses before the run
|
|
1474
|
+
* finished. Its `run` holds the latest run it has (the `runAgent()`
|
|
1475
|
+
* response if no poll ran).
|
|
1476
|
+
* @throws {@link SeclaiError} `"Polling aborted."` If `signal` is aborted and,
|
|
1477
|
+
* at the next poll's check, the run has not ended the poll. It is not an
|
|
1478
|
+
* `AbortError` and does not carry `signal.reason`.
|
|
1304
1479
|
*/
|
|
1305
1480
|
async runAgentAndPoll(agentId, body, opts) {
|
|
1306
1481
|
const pollInterval = opts?.pollIntervalMs ?? 2e3;
|
|
@@ -1309,20 +1484,29 @@ var Seclai = class {
|
|
|
1309
1484
|
const run = await this.runAgent(agentId, body);
|
|
1310
1485
|
const runId = run.id ?? run.run_id;
|
|
1311
1486
|
if (!runId) throw new SeclaiError("Agent run response did not contain an id.");
|
|
1487
|
+
let current = run;
|
|
1488
|
+
if (opts?.includeStepOutputs) {
|
|
1489
|
+
const status = run.status;
|
|
1490
|
+
if (status === "completed" || status === "failed" || status === "cancelled" || opts.returnOnResult && run.result_reported_at) {
|
|
1491
|
+
if (!opts.signal?.aborted) current = await this.getAgentRun(runId, { includeStepOutputs: true });
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1312
1494
|
while (true) {
|
|
1495
|
+
const status = current.status;
|
|
1496
|
+
if (status === "completed" || status === "failed" || status === "cancelled") {
|
|
1497
|
+
return current;
|
|
1498
|
+
}
|
|
1499
|
+
if (opts?.returnOnResult && current.result_reported_at) return current;
|
|
1313
1500
|
if (opts?.signal?.aborted) throw new SeclaiError("Polling aborted.");
|
|
1314
1501
|
if (Date.now() - startTime > timeout) {
|
|
1315
|
-
|
|
1502
|
+
const state = current.result_reported_at ? "A result had been reported; see `run`." : "No result had been reported.";
|
|
1503
|
+
throw new SeclaiStreamingError(`Polling timed out after ${timeout}ms. ${state}`, runId, current);
|
|
1316
1504
|
}
|
|
1317
1505
|
await new Promise((r) => setTimeout(r, pollInterval));
|
|
1318
|
-
|
|
1506
|
+
current = await this.getAgentRun(
|
|
1319
1507
|
runId,
|
|
1320
1508
|
opts?.includeStepOutputs ? { includeStepOutputs: true } : void 0
|
|
1321
1509
|
);
|
|
1322
|
-
const status = current.status;
|
|
1323
|
-
if (status === "completed" || status === "failed" || status === "cancelled") {
|
|
1324
|
-
return current;
|
|
1325
|
-
}
|
|
1326
1510
|
}
|
|
1327
1511
|
}
|
|
1328
1512
|
// ═══════════════════════════════════════════════════════════════════════════
|
|
@@ -1391,22 +1575,50 @@ var Seclai = class {
|
|
|
1391
1575
|
return await this.request("GET", `/agents/${agentId}/attachment-references`);
|
|
1392
1576
|
}
|
|
1393
1577
|
/**
|
|
1394
|
-
* Download a file
|
|
1578
|
+
* Download a file of an agent run (one a step emitted or one the run started
|
|
1579
|
+
* with), or one byte range of it.
|
|
1395
1580
|
*
|
|
1396
1581
|
* Returns the raw `Response` so you can stream or save the binary data.
|
|
1397
1582
|
*
|
|
1583
|
+
* To fetch part of the file, set a byte range. Offsets are zero-based and
|
|
1584
|
+
* `rangeEnd` is inclusive:
|
|
1585
|
+
*
|
|
1586
|
+
* - `rangeStart` alone sends `Range: bytes=<rangeStart>-`, from that byte to the end.
|
|
1587
|
+
* - `rangeStart` and `rangeEnd` send `Range: bytes=<rangeStart>-<rangeEnd>`.
|
|
1588
|
+
* - `rangeSuffix` alone sends `Range: bytes=-<rangeSuffix>`, the last that many bytes.
|
|
1589
|
+
*
|
|
1590
|
+
* With none of them set, the SDK adds no `Range` header. The server answers a range
|
|
1591
|
+
* with status 206 and a `Content-Range` header, so check `resp.status` before
|
|
1592
|
+
* treating the body as the whole file. A range of an empty file is answered with
|
|
1593
|
+
* 200 and the whole (empty) file. A range that starts at or past the end of a
|
|
1594
|
+
* non-empty file throws {@link SeclaiAPIStatusError} with `statusCode` 416,
|
|
1595
|
+
* and its `headers.get("content-range")` ends in `/<size>`, the file's size in
|
|
1596
|
+
* bytes.
|
|
1597
|
+
*
|
|
1398
1598
|
* @param runId - Run identifier.
|
|
1399
|
-
* @param attachmentId -
|
|
1400
|
-
*
|
|
1599
|
+
* @param attachmentId - The `id` of an entry in the run's or a step's
|
|
1600
|
+
* `attachments`, or the URL-safe-base64-encoded storage key that webhook and
|
|
1601
|
+
* email links carry.
|
|
1401
1602
|
* @param opts - Optional `downloadName` filename hint for the download
|
|
1402
|
-
* disposition
|
|
1603
|
+
* disposition; `rangeStart`, `rangeEnd` and `rangeSuffix`, the byte range
|
|
1604
|
+
* described above; and `signal`, which cancels the request; with a standard `fetch` this
|
|
1403
1605
|
* includes reading the body, so `AbortSignal.timeout(ms)` bounds the whole
|
|
1404
1606
|
* transfer.
|
|
1405
|
-
* @returns Raw response with the attachment bytes
|
|
1607
|
+
* @returns Raw response with the attachment bytes: status 200 with the whole
|
|
1608
|
+
* file, or 206 with the requested range.
|
|
1609
|
+
* @throws {@link SeclaiConfigurationError} Before any request, when the range
|
|
1610
|
+
* options are not one of the three forms above: `rangeEnd` without
|
|
1611
|
+
* `rangeStart`, `rangeSuffix` with either of them, `rangeEnd` less than
|
|
1612
|
+
* `rangeStart`, a negative `rangeStart` or `rangeEnd`, a `rangeSuffix` below
|
|
1613
|
+
* 1, or a value that is not a safe integer (`Number.isSafeInteger`).
|
|
1614
|
+
* @throws {@link SeclaiAPIStatusError} On a non-2xx response, including 416
|
|
1615
|
+
* when the range cannot be satisfied.
|
|
1406
1616
|
*/
|
|
1407
1617
|
async downloadAgentRunAttachment(runId, attachmentId, opts = {}) {
|
|
1618
|
+
const range = byteRangeHeader(opts.rangeStart, opts.rangeEnd, opts.rangeSuffix);
|
|
1408
1619
|
return await this.requestRaw("GET", `/v2/agent-runs/${runId}/attachments/${attachmentId}`, {
|
|
1409
1620
|
query: { download_name: opts.downloadName },
|
|
1621
|
+
...range === void 0 ? {} : { headers: { Range: range } },
|
|
1410
1622
|
signal: opts.signal
|
|
1411
1623
|
});
|
|
1412
1624
|
}
|
|
@@ -1684,8 +1896,9 @@ var Seclai = class {
|
|
|
1684
1896
|
* Configure an agent's `SLACK_COMMAND` trigger, which people run from Slack
|
|
1685
1897
|
* with `/seclai` or the message shortcut.
|
|
1686
1898
|
*
|
|
1687
|
-
* Sets the Slack connection,
|
|
1688
|
-
* settings. Omitted fields
|
|
1899
|
+
* Sets the Slack connection, `alias` (the name people type after `/seclai`),
|
|
1900
|
+
* form, channels, reply visibility and conversation settings. Omitted fields
|
|
1901
|
+
* are left unchanged. `input` is deprecated and ignored by the API.
|
|
1689
1902
|
*
|
|
1690
1903
|
* @param agentId - Agent identifier.
|
|
1691
1904
|
* @param triggerId - Trigger identifier.
|
|
@@ -1715,6 +1928,40 @@ var Seclai = class {
|
|
|
1715
1928
|
json: body
|
|
1716
1929
|
});
|
|
1717
1930
|
}
|
|
1931
|
+
/**
|
|
1932
|
+
* Get the run metadata keys a trigger declares.
|
|
1933
|
+
*
|
|
1934
|
+
* Steps may reference a declared key as `{{metadata.<key>}}`. The response
|
|
1935
|
+
* also lists the system and source keys the trigger's type can declare.
|
|
1936
|
+
*
|
|
1937
|
+
* @param agentId - Agent identifier.
|
|
1938
|
+
* @param triggerId - Trigger identifier, as `getAgent()` returns it.
|
|
1939
|
+
* @returns The trigger's declared and declarable metadata keys.
|
|
1940
|
+
*/
|
|
1941
|
+
async getMetadataKeys(agentId, triggerId) {
|
|
1942
|
+
return await this.request(
|
|
1943
|
+
"GET",
|
|
1944
|
+
`/agents/${agentId}/triggers/${triggerId}/metadata-keys`
|
|
1945
|
+
);
|
|
1946
|
+
}
|
|
1947
|
+
/**
|
|
1948
|
+
* Replace the run metadata keys a trigger declares.
|
|
1949
|
+
*
|
|
1950
|
+
* The body is the whole declaration and replaces the current one. The API
|
|
1951
|
+
* answers 400 when the agent references a key that no trigger of its branch
|
|
1952
|
+
* declares.
|
|
1953
|
+
*
|
|
1954
|
+
* @param agentId - Agent identifier.
|
|
1955
|
+
* @param triggerId - Trigger identifier, as `getAgent()` returns it.
|
|
1956
|
+
* @param body - The full declaration; each key's `required` defaults to `false`.
|
|
1957
|
+
* @returns The trigger's metadata keys, with `warnings` naming agents whose
|
|
1958
|
+
* `call_agent` mapping no longer fits.
|
|
1959
|
+
*/
|
|
1960
|
+
async setMetadataKeys(agentId, triggerId, body) {
|
|
1961
|
+
return await this.request("PUT", `/agents/${agentId}/triggers/${triggerId}/metadata-keys`, {
|
|
1962
|
+
json: body
|
|
1963
|
+
});
|
|
1964
|
+
}
|
|
1718
1965
|
// ═══════════════════════════════════════════════════════════════════════════
|
|
1719
1966
|
// Agent Email Governance
|
|
1720
1967
|
// ═══════════════════════════════════════════════════════════════════════════
|
|
@@ -1824,11 +2071,12 @@ var Seclai = class {
|
|
|
1824
2071
|
return await this.request("GET", "/agents/inbound-email-status");
|
|
1825
2072
|
}
|
|
1826
2073
|
/**
|
|
1827
|
-
*
|
|
1828
|
-
* once.
|
|
2074
|
+
* Cancel all of the account's QUEUED (over-quota parked) inbound-email runs
|
|
2075
|
+
* at once.
|
|
1829
2076
|
*
|
|
1830
|
-
* A queued run consumed no quota or credits at queue time
|
|
1831
|
-
*
|
|
2077
|
+
* A queued run consumed no quota or credits at queue time. Each ends
|
|
2078
|
+
* `cancelled`, or `failed` with an `apiVersion` before `2026-11-08`.
|
|
2079
|
+
* Requires an account owner/admin.
|
|
1832
2080
|
*
|
|
1833
2081
|
* @returns The count cancelled.
|
|
1834
2082
|
*/
|
|
@@ -1881,6 +2129,21 @@ var Seclai = class {
|
|
|
1881
2129
|
async getKnowledgeBase(knowledgeBaseId) {
|
|
1882
2130
|
return await this.request("GET", `/knowledge_bases/${knowledgeBaseId}`);
|
|
1883
2131
|
}
|
|
2132
|
+
/**
|
|
2133
|
+
* Get the item-metadata keys seen in a knowledge base's content.
|
|
2134
|
+
*
|
|
2135
|
+
* Reports, for all time and the last 30 and 7 days, each key's item count,
|
|
2136
|
+
* number of distinct values and five most frequent values.
|
|
2137
|
+
*
|
|
2138
|
+
* @param knowledgeBaseId - Knowledge base identifier.
|
|
2139
|
+
* @returns The key statistics per window.
|
|
2140
|
+
*/
|
|
2141
|
+
async getKnowledgeBaseMetadataKeyStats(knowledgeBaseId) {
|
|
2142
|
+
return await this.request(
|
|
2143
|
+
"GET",
|
|
2144
|
+
`/knowledge_bases/${knowledgeBaseId}/metadata-key-stats`
|
|
2145
|
+
);
|
|
2146
|
+
}
|
|
1884
2147
|
/**
|
|
1885
2148
|
* Update a knowledge base.
|
|
1886
2149
|
*
|