@orkestrel/agent 0.0.21 → 0.0.22
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 +16 -13
- package/dist/src/core/index.cjs +952 -32
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +819 -26
- package/dist/src/core/index.d.ts +819 -26
- package/dist/src/core/index.js +927 -35
- package/dist/src/core/index.js.map +1 -1
- package/package.json +8 -6
package/dist/src/core/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { attempt, isArray, isBoolean, isFiniteNumber, isObject, isRecord, isString, parseJSONValue, rawShape, stringShape } from "@orkestrel/contract";
|
|
1
|
+
import { arrayOf, arrayShape, attempt, booleanShape, cloneJSONValue, createContract, isArray, isBoolean, isFiniteNumber, isObject, isRecord, isString, jsonShape, literalShape, numberShape, objectShape, optionalShape, parseJSONAs, parseJSONValue, rawShape, recordShape, stringShape, unionShape } from "@orkestrel/contract";
|
|
2
2
|
import { createDatabase, createMemoryDriver } from "@orkestrel/database";
|
|
3
3
|
import { createQueue } from "@orkestrel/queue";
|
|
4
4
|
import { createRunner, errorToMessage } from "@orkestrel/workflow";
|
|
5
5
|
import { createAbort } from "@orkestrel/abort";
|
|
6
|
-
import { createTimeout } from "@orkestrel/timeout";
|
|
6
|
+
import { Timeout, createTimeout } from "@orkestrel/timeout";
|
|
7
7
|
import { Emitter } from "@orkestrel/emitter";
|
|
8
8
|
import { ToolManager, isToolCall } from "@orkestrel/tool";
|
|
9
9
|
import { WorkspaceManager, isBinary, isText } from "@orkestrel/workspace";
|
|
@@ -95,6 +95,56 @@ var MESSAGE_TOKEN_OVERHEAD = 4;
|
|
|
95
95
|
* exact count.
|
|
96
96
|
*/
|
|
97
97
|
var IMAGE_TOKEN_ESTIMATE = 512;
|
|
98
|
+
/**
|
|
99
|
+
* Holds the default provider deadline in milliseconds — `120_000`, the wall-clock bound a call
|
|
100
|
+
* runs under when `AgentProviderInput.timeout` is omitted, folded with the caller's signal so
|
|
101
|
+
* whichever trips first cancels the call.
|
|
102
|
+
*/
|
|
103
|
+
var DEFAULT_PROVIDER_TIMEOUT = 12e4;
|
|
104
|
+
/**
|
|
105
|
+
* Bounds the decoded error excerpt's input in bytes — `2048`, the leading bytes of a non-OK
|
|
106
|
+
* response body handed to the decoder before the read cancels the remainder, so a `ProviderError`
|
|
107
|
+
* message never carries a longer excerpt.
|
|
108
|
+
*/
|
|
109
|
+
var MAX_ERROR_BODY_LENGTH = 2048;
|
|
110
|
+
/**
|
|
111
|
+
* Holds the default relay request limit in bytes — `1_048_576`, the byte budget a relay applies
|
|
112
|
+
* to an inbound body when `RelayOptions.limit` is omitted, refusing a body that reaches it.
|
|
113
|
+
*/
|
|
114
|
+
var DEFAULT_RELAY_LIMIT = 1048576;
|
|
115
|
+
/**
|
|
116
|
+
* Names the relay's newline-delimited JSON content type — `'application/x-ndjson; charset=utf-8'`,
|
|
117
|
+
* the header a relay response carries beside `cache-control: no-store`.
|
|
118
|
+
*/
|
|
119
|
+
var RELAY_CONTENT_TYPE = "application/x-ndjson; charset=utf-8";
|
|
120
|
+
/**
|
|
121
|
+
* Names the public message for an unexpected upstream relay failure — `'relay provider failed'`,
|
|
122
|
+
* the fixed text every `error` frame carries, so an upstream failure's own message never reaches
|
|
123
|
+
* the browser.
|
|
124
|
+
*/
|
|
125
|
+
var RELAY_PROVIDER_MESSAGE = "relay provider failed";
|
|
126
|
+
/**
|
|
127
|
+
* Names the status a relay answers when the `authorize` callback returns anything but `true` or throws —
|
|
128
|
+
* `401`, carried with no body and reaching the browser as a `ProviderError` with the `HTTP` code.
|
|
129
|
+
*/
|
|
130
|
+
var UNAUTHORIZED_RELAY_STATUS = 401;
|
|
131
|
+
/**
|
|
132
|
+
* Names the status a relay answers when the upstream provider call cannot be constructed — `502`,
|
|
133
|
+
* carried with no body after `provider.stream` was entered and threw before returning its
|
|
134
|
+
* iterator.
|
|
135
|
+
*/
|
|
136
|
+
var UPSTREAM_RELAY_STATUS = 502;
|
|
137
|
+
/**
|
|
138
|
+
* Names the status a relay answers for a body that is missing, unreadable, or rejected by
|
|
139
|
+
* `providerRequestContract` — `400`, carried with no body and reaching the browser as a
|
|
140
|
+
* `ProviderError` with the `HTTP` code.
|
|
141
|
+
*/
|
|
142
|
+
var INVALID_RELAY_STATUS = 400;
|
|
143
|
+
/**
|
|
144
|
+
* Names the status a relay answers for a request body at or above its byte budget — `413`,
|
|
145
|
+
* carried with no body and answered for an aborted inbound read as well.
|
|
146
|
+
*/
|
|
147
|
+
var OVERSIZED_RELAY_STATUS = 413;
|
|
98
148
|
//#endregion
|
|
99
149
|
//#region src/core/errors.ts
|
|
100
150
|
/**
|
|
@@ -106,14 +156,16 @@ var IMAGE_TOKEN_ESTIMATE = 512;
|
|
|
106
156
|
* Lets a caller recover the partial content (and any tool calls / usage seen so far)
|
|
107
157
|
* on cancellation: `catch` the throw, narrow with {@link isProviderAbortError}, and
|
|
108
158
|
* read `partial`. `code` is the machine-readable condition (`'ABORT'` — the only one this
|
|
109
|
-
* error reports), so a `catch` branches on it rather than on the message string.
|
|
159
|
+
* error reports), so a `catch` branches on it rather than on the message string. `cause`
|
|
160
|
+
* holds the failure the cancel superseded when a throw raced the abort — the wire decoder's
|
|
161
|
+
* {@link ProviderError}, say — and is undefined when the cancel was the only failure.
|
|
110
162
|
*/
|
|
111
163
|
var ProviderAbortError = class extends Error {
|
|
112
164
|
/** Names the machine-readable condition — `'ABORT'`: a stream cancelled mid-flight. */
|
|
113
165
|
code = "ABORT";
|
|
114
166
|
partial;
|
|
115
|
-
constructor(partial) {
|
|
116
|
-
super("provider stream aborted");
|
|
167
|
+
constructor(partial, options) {
|
|
168
|
+
super("provider stream aborted", options);
|
|
117
169
|
this.name = "ProviderAbortError";
|
|
118
170
|
this.partial = partial;
|
|
119
171
|
}
|
|
@@ -274,6 +326,140 @@ var AgentError = class extends Error {
|
|
|
274
326
|
function isAgentError(value) {
|
|
275
327
|
return value instanceof AgentError;
|
|
276
328
|
}
|
|
329
|
+
/**
|
|
330
|
+
* Reports a coded provider failure with its HTTP status and underlying cause when available.
|
|
331
|
+
*
|
|
332
|
+
* @remarks
|
|
333
|
+
* The provider base throws this error for a non-OK HTTP response, a missing response
|
|
334
|
+
* body, or a missing settled result when strict assembly is enabled. Concrete wire
|
|
335
|
+
* decoders also use it for malformed records and relayed provider failures.
|
|
336
|
+
* `status` is present only for an `HTTP` failure; other codes leave it undefined.
|
|
337
|
+
*/
|
|
338
|
+
var ProviderError = class extends Error {
|
|
339
|
+
/** Names the machine-readable condition — `'HTTP'`: a non-OK response, including a relay refusing an oversized request with 413; `'PROTOCOL'`: a missing response body, a malformed wire record, or a strict stream with no settled result; `'PROVIDER'`: an upstream failure carried by a relay error record. */
|
|
340
|
+
code;
|
|
341
|
+
/** Holds the response status for an HTTP failure, or undefined for other codes. */
|
|
342
|
+
status;
|
|
343
|
+
constructor(code, message, options) {
|
|
344
|
+
super(message, options);
|
|
345
|
+
this.name = "ProviderError";
|
|
346
|
+
this.code = code;
|
|
347
|
+
this.status = options?.status;
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
/**
|
|
351
|
+
* Narrows a caught value to the provider failure class through instanceof.
|
|
352
|
+
*
|
|
353
|
+
* @param value - The caught value
|
|
354
|
+
* @returns True if the value is a provider failure; false otherwise
|
|
355
|
+
* @example
|
|
356
|
+
* ```ts
|
|
357
|
+
* isProviderError(new ProviderError('HTTP', 'unavailable', { status: 503 })) // true
|
|
358
|
+
* ```
|
|
359
|
+
*/
|
|
360
|
+
function isProviderError(value) {
|
|
361
|
+
return value instanceof ProviderError;
|
|
362
|
+
}
|
|
363
|
+
//#endregion
|
|
364
|
+
//#region src/core/shapers.ts
|
|
365
|
+
/**
|
|
366
|
+
* Describes a tool call's JSON wire projection.
|
|
367
|
+
*
|
|
368
|
+
* @remarks
|
|
369
|
+
* The wire is strictly narrower than the domain: non-JSON arguments are refused.
|
|
370
|
+
* ToolCall.caller is not in this shape and never crosses a wire. The guard refuses
|
|
371
|
+
* that extra member; the contract parser drops it.
|
|
372
|
+
*/
|
|
373
|
+
var toolCallShape = objectShape({
|
|
374
|
+
id: stringShape(),
|
|
375
|
+
name: stringShape(),
|
|
376
|
+
arguments: recordShape(jsonShape())
|
|
377
|
+
});
|
|
378
|
+
/**
|
|
379
|
+
* Describes a conversation message's JSON wire projection.
|
|
380
|
+
*
|
|
381
|
+
* @remarks
|
|
382
|
+
* The wire is strictly narrower than Message: non-JSON call arguments are refused,
|
|
383
|
+
* and caller context is refused by the guard or dropped by the parser.
|
|
384
|
+
*/
|
|
385
|
+
var messageShape = objectShape({
|
|
386
|
+
id: stringShape(),
|
|
387
|
+
role: literalShape([
|
|
388
|
+
"system",
|
|
389
|
+
"user",
|
|
390
|
+
"assistant",
|
|
391
|
+
"tool"
|
|
392
|
+
]),
|
|
393
|
+
content: stringShape(),
|
|
394
|
+
calls: optionalShape(arrayShape(toolCallShape)),
|
|
395
|
+
images: optionalShape(arrayShape(stringShape()))
|
|
396
|
+
});
|
|
397
|
+
/**
|
|
398
|
+
* Describes a provider request's JSON wire projection.
|
|
399
|
+
*
|
|
400
|
+
* @remarks
|
|
401
|
+
* The wire is strictly narrower than ProviderRequest: non-JSON arguments, parameters,
|
|
402
|
+
* or schema members are refused, and caller context is refused or dropped.
|
|
403
|
+
*/
|
|
404
|
+
var providerRequestShape = objectShape({
|
|
405
|
+
messages: arrayShape(messageShape),
|
|
406
|
+
tools: optionalShape(arrayShape(objectShape({
|
|
407
|
+
name: stringShape(),
|
|
408
|
+
description: optionalShape(stringShape()),
|
|
409
|
+
parameters: optionalShape(recordShape(jsonShape()))
|
|
410
|
+
}))),
|
|
411
|
+
options: optionalShape(objectShape({
|
|
412
|
+
think: optionalShape(booleanShape()),
|
|
413
|
+
schema: optionalShape(recordShape(jsonShape()))
|
|
414
|
+
}))
|
|
415
|
+
});
|
|
416
|
+
/**
|
|
417
|
+
* Describes a provider result's JSON wire projection.
|
|
418
|
+
*
|
|
419
|
+
* @remarks
|
|
420
|
+
* The wire is strictly narrower than ProviderResult: non-JSON call arguments are
|
|
421
|
+
* refused, and caller context is refused or dropped.
|
|
422
|
+
*/
|
|
423
|
+
var providerResultShape = objectShape({
|
|
424
|
+
content: stringShape(),
|
|
425
|
+
thinking: optionalShape(stringShape()),
|
|
426
|
+
tools: optionalShape(arrayShape(toolCallShape)),
|
|
427
|
+
usage: optionalShape(objectShape({
|
|
428
|
+
prompt: numberShape(),
|
|
429
|
+
completion: numberShape(),
|
|
430
|
+
total: numberShape()
|
|
431
|
+
}))
|
|
432
|
+
});
|
|
433
|
+
/**
|
|
434
|
+
* Describes the channel-discriminated JSON relay wire projection.
|
|
435
|
+
*
|
|
436
|
+
* @remarks
|
|
437
|
+
* The wire is strictly narrower than RelayFrame through its result and partial
|
|
438
|
+
* fields: non-JSON arguments are refused, and caller context is refused or dropped.
|
|
439
|
+
*/
|
|
440
|
+
var relayFrameShape = unionShape(objectShape({
|
|
441
|
+
channel: literalShape(["content", "thinking"]),
|
|
442
|
+
text: stringShape()
|
|
443
|
+
}), objectShape({
|
|
444
|
+
channel: literalShape(["result"]),
|
|
445
|
+
result: providerResultShape
|
|
446
|
+
}), objectShape({
|
|
447
|
+
channel: literalShape(["abort"]),
|
|
448
|
+
partial: providerResultShape
|
|
449
|
+
}), objectShape({
|
|
450
|
+
channel: literalShape(["error"]),
|
|
451
|
+
message: stringShape()
|
|
452
|
+
}));
|
|
453
|
+
//#endregion
|
|
454
|
+
//#region src/core/contracts.ts
|
|
455
|
+
/** Validates and projects conversation messages at a JSON wire boundary. */
|
|
456
|
+
var messageContract = createContract(messageShape);
|
|
457
|
+
/** Validates and projects provider requests at a JSON wire boundary. */
|
|
458
|
+
var providerRequestContract = createContract(providerRequestShape);
|
|
459
|
+
/** Validates and projects provider results at a JSON wire boundary. */
|
|
460
|
+
var providerResultContract = createContract(providerResultShape);
|
|
461
|
+
/** Validates and projects channel-discriminated relay frames at a JSON wire boundary. */
|
|
462
|
+
var relayFrameContract = createContract(relayFrameShape);
|
|
277
463
|
//#endregion
|
|
278
464
|
//#region src/core/helpers.ts
|
|
279
465
|
/**
|
|
@@ -581,11 +767,13 @@ function sanitizeUsage(usage) {
|
|
|
581
767
|
* @example
|
|
582
768
|
* ```ts
|
|
583
769
|
* joinThinking(undefined, 'first') // 'first'
|
|
770
|
+
* joinThinking('', 'x') // 'x'
|
|
584
771
|
* joinThinking('first', 'second') // 'first\n\nsecond'
|
|
585
772
|
* ```
|
|
586
773
|
*/
|
|
587
774
|
function joinThinking(running, next) {
|
|
588
|
-
|
|
775
|
+
if (running === void 0 || running.length === 0) return next;
|
|
776
|
+
return next.length === 0 ? running : `${running}\n\n${next}`;
|
|
589
777
|
}
|
|
590
778
|
/**
|
|
591
779
|
* Adds two {@link TokenUsage} values field by field — the running total an agent run keeps
|
|
@@ -942,6 +1130,133 @@ function intersectKeys(parent, child) {
|
|
|
942
1130
|
const allowed = new Set(parent);
|
|
943
1131
|
return child.filter((key) => allowed.has(key));
|
|
944
1132
|
}
|
|
1133
|
+
/**
|
|
1134
|
+
* Assembles a provider result with only populated optional fields.
|
|
1135
|
+
*
|
|
1136
|
+
* @param content - The authoritative answer
|
|
1137
|
+
* @param thinking - The joined reasoning
|
|
1138
|
+
* @param tools - The accumulated calls
|
|
1139
|
+
* @param usage - The reported token usage
|
|
1140
|
+
* @returns The assembled result
|
|
1141
|
+
* @example
|
|
1142
|
+
* ```ts
|
|
1143
|
+
* buildProviderResult('ok', '', [], undefined) // { content: 'ok' }
|
|
1144
|
+
* ```
|
|
1145
|
+
*/
|
|
1146
|
+
function buildProviderResult(content, thinking, tools, usage) {
|
|
1147
|
+
return {
|
|
1148
|
+
content,
|
|
1149
|
+
...thinking.length === 0 ? {} : { thinking },
|
|
1150
|
+
...tools.length === 0 ? {} : { tools },
|
|
1151
|
+
...usage === void 0 ? {} : { usage }
|
|
1152
|
+
};
|
|
1153
|
+
}
|
|
1154
|
+
/**
|
|
1155
|
+
* Reads a UTF-8 prefix of a byte stream and cancels its remainder.
|
|
1156
|
+
*
|
|
1157
|
+
* @remarks
|
|
1158
|
+
* The `limit` parameter bounds bytes passed to the decoder, including a partial final
|
|
1159
|
+
* character. An omitted limit reads to completion. A source may deliver a chunk larger
|
|
1160
|
+
* than the remaining limit; its unused bytes are discarded without decoding.
|
|
1161
|
+
* The read can overshoot by one source chunk. An abort leaves completion false.
|
|
1162
|
+
*
|
|
1163
|
+
* @param body - The readable byte stream
|
|
1164
|
+
* @param limit - The maximum byte prefix, or undefined for the complete stream
|
|
1165
|
+
* @param signal - The optional cancellation bound; abort returns the decoded prefix
|
|
1166
|
+
* @returns The decoded prefix and whether EOF occurred within the byte budget
|
|
1167
|
+
* @example
|
|
1168
|
+
* ```ts
|
|
1169
|
+
* import { readText } from '@orkestrel/agent'
|
|
1170
|
+
*
|
|
1171
|
+
* const body = new Response('answer').body
|
|
1172
|
+
* if (body !== null) await readText(body, 3) // { text: 'ans', complete: false }
|
|
1173
|
+
* ```
|
|
1174
|
+
*/
|
|
1175
|
+
async function readText(body, limit, signal) {
|
|
1176
|
+
const reader = body.getReader();
|
|
1177
|
+
const decoder = new TextDecoder();
|
|
1178
|
+
const cleanup = new AbortController();
|
|
1179
|
+
let remaining = limit ?? Infinity;
|
|
1180
|
+
let text = "";
|
|
1181
|
+
let complete = false;
|
|
1182
|
+
try {
|
|
1183
|
+
signal?.addEventListener("abort", () => {
|
|
1184
|
+
reader.cancel(signal.reason).catch(() => {});
|
|
1185
|
+
}, {
|
|
1186
|
+
once: true,
|
|
1187
|
+
signal: cleanup.signal
|
|
1188
|
+
});
|
|
1189
|
+
if (signal?.aborted) await reader.cancel(signal.reason).catch(() => {});
|
|
1190
|
+
while (remaining > 0) {
|
|
1191
|
+
if (signal?.aborted) break;
|
|
1192
|
+
const step = await reader.read();
|
|
1193
|
+
if (signal?.aborted) break;
|
|
1194
|
+
if (step.done) {
|
|
1195
|
+
complete = true;
|
|
1196
|
+
break;
|
|
1197
|
+
}
|
|
1198
|
+
const bytes = step.value.subarray(0, remaining);
|
|
1199
|
+
text += decoder.decode(bytes, { stream: true });
|
|
1200
|
+
remaining -= bytes.byteLength;
|
|
1201
|
+
}
|
|
1202
|
+
return {
|
|
1203
|
+
text: text + decoder.decode(),
|
|
1204
|
+
complete
|
|
1205
|
+
};
|
|
1206
|
+
} finally {
|
|
1207
|
+
cleanup.abort();
|
|
1208
|
+
try {
|
|
1209
|
+
await reader.cancel();
|
|
1210
|
+
} catch {} finally {
|
|
1211
|
+
reader.releaseLock();
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
/**
|
|
1216
|
+
* Decodes UTF-8 chunks with a final flush and releases the stream on every exit.
|
|
1217
|
+
*
|
|
1218
|
+
* @param body - The readable byte stream
|
|
1219
|
+
* @param signal - The optional cancellation bound; abort ends iteration without further chunks
|
|
1220
|
+
* @returns Decoded text chunks, including a held decoder tail
|
|
1221
|
+
* @example
|
|
1222
|
+
* ```ts
|
|
1223
|
+
* const body = new Response('answer').body
|
|
1224
|
+
* if (body !== null) for await (const chunk of readChunks(body)) render(chunk)
|
|
1225
|
+
* ```
|
|
1226
|
+
*/
|
|
1227
|
+
async function* readChunks(body, signal) {
|
|
1228
|
+
const reader = body.getReader();
|
|
1229
|
+
const decoder = new TextDecoder();
|
|
1230
|
+
const cleanup = new AbortController();
|
|
1231
|
+
try {
|
|
1232
|
+
signal?.addEventListener("abort", () => {
|
|
1233
|
+
reader.cancel(signal.reason).catch(() => {});
|
|
1234
|
+
}, {
|
|
1235
|
+
once: true,
|
|
1236
|
+
signal: cleanup.signal
|
|
1237
|
+
});
|
|
1238
|
+
if (signal?.aborted) {
|
|
1239
|
+
await reader.cancel(signal.reason).catch(() => {});
|
|
1240
|
+
return;
|
|
1241
|
+
}
|
|
1242
|
+
for (;;) {
|
|
1243
|
+
const step = await reader.read();
|
|
1244
|
+
if (signal?.aborted) return;
|
|
1245
|
+
if (step.done) break;
|
|
1246
|
+
const chunk = decoder.decode(step.value, { stream: true });
|
|
1247
|
+
if (chunk.length > 0) yield chunk;
|
|
1248
|
+
}
|
|
1249
|
+
const tail = decoder.decode();
|
|
1250
|
+
if (tail.length > 0) yield tail;
|
|
1251
|
+
} finally {
|
|
1252
|
+
cleanup.abort();
|
|
1253
|
+
try {
|
|
1254
|
+
await reader.cancel();
|
|
1255
|
+
} catch {} finally {
|
|
1256
|
+
reader.releaseLock();
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
945
1260
|
//#endregion
|
|
946
1261
|
//#region src/core/conversations/Conversation.ts
|
|
947
1262
|
/**
|
|
@@ -1814,7 +2129,7 @@ var Agent = class {
|
|
|
1814
2129
|
} catch (error) {
|
|
1815
2130
|
if (abort.signal.aborted) {
|
|
1816
2131
|
if (isProviderAbortError(error)) {
|
|
1817
|
-
if (error.partial.thinking !== void 0) thinking = joinThinking(thinking, error.partial.thinking);
|
|
2132
|
+
if (error.partial.thinking !== void 0 && error.partial.thinking.length > 0) thinking = joinThinking(thinking, error.partial.thinking);
|
|
1818
2133
|
if (error.partial.usage !== void 0) {
|
|
1819
2134
|
const abortUsage = sanitizeUsage(error.partial.usage);
|
|
1820
2135
|
budget?.consume({
|
|
@@ -2125,41 +2440,32 @@ var Authority = class {
|
|
|
2125
2440
|
//#endregion
|
|
2126
2441
|
//#region src/core/validators.ts
|
|
2127
2442
|
/**
|
|
2128
|
-
* Checks whether
|
|
2129
|
-
* the {@link isConversationSnapshot} and {@link isSection} read-boundary narrows, where a present
|
|
2130
|
-
* `calls` must be an array of valid {@link import('@orkestrel/tool').ToolCall}s. Total, never
|
|
2131
|
-
* throwing, and never an assertion; the conversation analogue of
|
|
2132
|
-
* {@link import('@orkestrel/workspace').isFile}.
|
|
2443
|
+
* Checks whether a value satisfies the domain conversation-message contract.
|
|
2133
2444
|
*
|
|
2134
2445
|
* @remarks
|
|
2135
|
-
*
|
|
2136
|
-
*
|
|
2137
|
-
*
|
|
2138
|
-
* {@link import('@orkestrel/tool').ToolCall} ({@link isToolCall} — the
|
|
2139
|
-
* fail-closed deepening: a tampered `calls` element rejects the message, so the snapshot
|
|
2140
|
-
* reads back as absent rather than replaying a malformed call) and an `images` that is an array
|
|
2141
|
-
* (an absent optional passes). The `role` is left as a broad `string` here (an open
|
|
2142
|
-
* {@link import('./types.js').MessageRole}, so any storage-read role string is accepted
|
|
2143
|
-
* defensively rather than rejected against the current literal set). Enough to safely impose the
|
|
2144
|
-
* {@link Message} type at
|
|
2145
|
-
* a storage boundary without a cast.
|
|
2146
|
-
*
|
|
2147
|
-
* @param value - The value to test (one element of a snapshot's `messages` / a section's `messages`)
|
|
2148
|
-
* @returns True if `value` has the structural shape of a {@link Message}; false otherwise
|
|
2446
|
+
* Roles belong to MessageRole and image elements are strings. Tool arguments may
|
|
2447
|
+
* carry non-JSON values, as the domain type permits; the message wire contract is
|
|
2448
|
+
* narrower. Unreadable fields and hostile inputs return false.
|
|
2149
2449
|
*
|
|
2450
|
+
* @param value - The unknown message candidate
|
|
2451
|
+
* @returns True if the domain message fields are valid; false otherwise
|
|
2150
2452
|
* @example
|
|
2151
2453
|
* ```ts
|
|
2152
2454
|
* isMessage({ id: '1', role: 'user', content: 'hi' }) // true
|
|
2153
|
-
* isMessage({ id: '1', role: '
|
|
2154
|
-
* isMessage({ id: '1', role: 'user' }) // false
|
|
2155
|
-
* isMessage({ id: '1', role: 'assistant', content: '', calls: [null] }) // false (malformed call)
|
|
2455
|
+
* isMessage({ id: '1', role: 'other', content: '' }) // false
|
|
2456
|
+
* isMessage({ id: '1', role: 'user', content: '', images: [1] }) // false
|
|
2156
2457
|
* ```
|
|
2157
2458
|
*/
|
|
2158
2459
|
function isMessage(value) {
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2460
|
+
const checked = attempt(() => {
|
|
2461
|
+
if (!isRecord(value)) return false;
|
|
2462
|
+
const { id, role, content, calls, images } = value;
|
|
2463
|
+
if (!isString(id) || !isString(content)) return false;
|
|
2464
|
+
if (role !== "system" && role !== "user" && role !== "assistant" && role !== "tool") return false;
|
|
2465
|
+
if (calls !== void 0 && !arrayOf(isToolCall)(calls)) return false;
|
|
2466
|
+
return images === void 0 || arrayOf(isString)(images);
|
|
2467
|
+
});
|
|
2468
|
+
return checked.success && checked.value;
|
|
2163
2469
|
}
|
|
2164
2470
|
/**
|
|
2165
2471
|
* Checks whether an `unknown` is structurally a {@link Section} record — a `string` `id` and
|
|
@@ -2385,6 +2691,493 @@ var MemoryConversationStore = class {
|
|
|
2385
2691
|
}
|
|
2386
2692
|
};
|
|
2387
2693
|
//#endregion
|
|
2694
|
+
//#region src/core/AgentProvider.ts
|
|
2695
|
+
/**
|
|
2696
|
+
* Implements bounded HTTP streaming and result assembly behind concrete wire seams.
|
|
2697
|
+
*
|
|
2698
|
+
* @remarks
|
|
2699
|
+
* Every call owns its parser, splitter, deadline, and accumulation. Success bodies
|
|
2700
|
+
* have no size limit. A qwen3 implicit-open reclassification corrects the final
|
|
2701
|
+
* content while content deltas already yielded cannot be recalled. A subclass fills
|
|
2702
|
+
* `name`, `frame`, `body`, `read`, and `finish`; the constructor takes the `split`
|
|
2703
|
+
* and `strict` switches to control reasoning separation and settled-result requirements.
|
|
2704
|
+
*
|
|
2705
|
+
* @example Writing a provider for a new wire
|
|
2706
|
+
* ```ts
|
|
2707
|
+
* import type {
|
|
2708
|
+
* ProviderIncrement,
|
|
2709
|
+
* ProviderOptions,
|
|
2710
|
+
* ProviderParserInterface,
|
|
2711
|
+
* ProviderRequest,
|
|
2712
|
+
* } from '@orkestrel/agent'
|
|
2713
|
+
* import { AgentProvider } from '@orkestrel/agent'
|
|
2714
|
+
*
|
|
2715
|
+
* class TextFrame implements ProviderParserInterface<string> {
|
|
2716
|
+
* parse(chunk: string): readonly string[] {
|
|
2717
|
+
* return [chunk]
|
|
2718
|
+
* }
|
|
2719
|
+
* clear(): void {} // Raw text retains no framing state.
|
|
2720
|
+
* }
|
|
2721
|
+
*
|
|
2722
|
+
* interface TextOptions extends ProviderOptions {
|
|
2723
|
+
* readonly url: string
|
|
2724
|
+
* }
|
|
2725
|
+
*
|
|
2726
|
+
* class TextProvider extends AgentProvider<string> {
|
|
2727
|
+
* readonly name = 'text'
|
|
2728
|
+
* constructor(options: TextOptions) {
|
|
2729
|
+
* super({ ...options, path: '/generate' })
|
|
2730
|
+
* }
|
|
2731
|
+
* frame(): ProviderParserInterface<string> {
|
|
2732
|
+
* return new TextFrame()
|
|
2733
|
+
* }
|
|
2734
|
+
* body(request: ProviderRequest): object {
|
|
2735
|
+
* return { messages: request.messages }
|
|
2736
|
+
* }
|
|
2737
|
+
* read(record: string): ProviderIncrement {
|
|
2738
|
+
* return { content: record, thinking: '', tools: [] }
|
|
2739
|
+
* }
|
|
2740
|
+
* finish(_parser: ProviderParserInterface<string>): readonly string[] {
|
|
2741
|
+
* return [] // Raw text retains no records at end of input.
|
|
2742
|
+
* }
|
|
2743
|
+
* }
|
|
2744
|
+
* ```
|
|
2745
|
+
*/
|
|
2746
|
+
var AgentProvider = class {
|
|
2747
|
+
#id;
|
|
2748
|
+
#url;
|
|
2749
|
+
#path;
|
|
2750
|
+
#timeout;
|
|
2751
|
+
#transport;
|
|
2752
|
+
#headers;
|
|
2753
|
+
#format;
|
|
2754
|
+
#split;
|
|
2755
|
+
#strict;
|
|
2756
|
+
constructor(input) {
|
|
2757
|
+
this.#id = crypto.randomUUID();
|
|
2758
|
+
this.#url = input.url;
|
|
2759
|
+
this.#path = input.path ?? "";
|
|
2760
|
+
this.#timeout = input.timeout ?? 12e4;
|
|
2761
|
+
this.#transport = input.fetch ?? globalThis.fetch.bind(globalThis);
|
|
2762
|
+
this.#headers = input.headers;
|
|
2763
|
+
this.#format = input.format;
|
|
2764
|
+
this.#split = input.split ?? true;
|
|
2765
|
+
this.#strict = input.strict ?? false;
|
|
2766
|
+
}
|
|
2767
|
+
/** Exposes the instance's minted UUID. */
|
|
2768
|
+
get id() {
|
|
2769
|
+
return this.#id;
|
|
2770
|
+
}
|
|
2771
|
+
/** Exposes the context framing exactly as supplied. */
|
|
2772
|
+
get format() {
|
|
2773
|
+
return this.#format;
|
|
2774
|
+
}
|
|
2775
|
+
/**
|
|
2776
|
+
* Generates a complete turn by draining the shared stream engine.
|
|
2777
|
+
*
|
|
2778
|
+
* @param messages - The conversation turns
|
|
2779
|
+
* @param signal - The caller's cancellation bound
|
|
2780
|
+
* @param tools - The advertised tool definitions
|
|
2781
|
+
* @param options - The per-call generation configuration
|
|
2782
|
+
* @returns The terminal stream result
|
|
2783
|
+
*/
|
|
2784
|
+
async generate(messages, signal, tools, options) {
|
|
2785
|
+
const stream = this.stream(messages, signal, tools, options);
|
|
2786
|
+
let step = await stream.next();
|
|
2787
|
+
while (!step.done) step = await stream.next();
|
|
2788
|
+
return step.value;
|
|
2789
|
+
}
|
|
2790
|
+
/**
|
|
2791
|
+
* Streams decoded deltas and returns the authoritative or assembled turn result.
|
|
2792
|
+
*
|
|
2793
|
+
* @param messages - The conversation turns
|
|
2794
|
+
* @param signal - The caller's cancellation bound
|
|
2795
|
+
* @param tools - The advertised tool definitions
|
|
2796
|
+
* @param options - The per-call generation configuration
|
|
2797
|
+
* @returns Content and native thinking deltas followed by the settled result
|
|
2798
|
+
* @throws ProviderAbortError Thrown when the combined cancellation bound fires
|
|
2799
|
+
* @throws ProviderError Thrown for an HTTP or protocol failure
|
|
2800
|
+
*/
|
|
2801
|
+
async *stream(messages, signal, tools, options) {
|
|
2802
|
+
if (signal.aborted) throw new ProviderAbortError({ content: "" });
|
|
2803
|
+
const timeout = new Timeout({ ms: this.#timeout });
|
|
2804
|
+
timeout.start();
|
|
2805
|
+
const combined = AbortSignal.any([timeout.signal, signal]);
|
|
2806
|
+
let parser;
|
|
2807
|
+
let splitter;
|
|
2808
|
+
let state = {
|
|
2809
|
+
content: "",
|
|
2810
|
+
thinking: "",
|
|
2811
|
+
tools: []
|
|
2812
|
+
};
|
|
2813
|
+
try {
|
|
2814
|
+
parser = this.frame();
|
|
2815
|
+
splitter = this.#split ? createThinkSplitter() : void 0;
|
|
2816
|
+
const response = await this.#request({
|
|
2817
|
+
messages,
|
|
2818
|
+
...tools === void 0 ? {} : { tools },
|
|
2819
|
+
...options === void 0 ? {} : { options }
|
|
2820
|
+
}, combined);
|
|
2821
|
+
if (response.body === null) throw new ProviderError("PROTOCOL", "provider error: no response body");
|
|
2822
|
+
for await (const chunk of readChunks(response.body, combined)) {
|
|
2823
|
+
combined.throwIfAborted();
|
|
2824
|
+
for (const record of parser.parse(chunk)) {
|
|
2825
|
+
yield* this.#fold(record, state, splitter, combined, (increment) => {
|
|
2826
|
+
state = increment;
|
|
2827
|
+
});
|
|
2828
|
+
combined.throwIfAborted();
|
|
2829
|
+
if (state.result !== void 0) return state.result;
|
|
2830
|
+
}
|
|
2831
|
+
}
|
|
2832
|
+
combined.throwIfAborted();
|
|
2833
|
+
for (const record of this.finish(parser)) {
|
|
2834
|
+
yield* this.#fold(record, state, splitter, combined, (increment) => {
|
|
2835
|
+
state = increment;
|
|
2836
|
+
});
|
|
2837
|
+
combined.throwIfAborted();
|
|
2838
|
+
if (state.result !== void 0) return state.result;
|
|
2839
|
+
}
|
|
2840
|
+
const tail = splitter?.flush() ?? "";
|
|
2841
|
+
if (tail.length > 0) yield {
|
|
2842
|
+
channel: "content",
|
|
2843
|
+
text: tail
|
|
2844
|
+
};
|
|
2845
|
+
combined.throwIfAborted();
|
|
2846
|
+
if (this.#strict) throw new ProviderError("PROTOCOL", "provider error: missing settled result");
|
|
2847
|
+
return buildProviderResult(splitter?.content ?? state.content, joinThinking(splitter?.thinking, state.thinking), state.tools, state.usage);
|
|
2848
|
+
} catch (error) {
|
|
2849
|
+
if (combined.aborted) {
|
|
2850
|
+
splitter?.flush();
|
|
2851
|
+
throw new ProviderAbortError(buildProviderResult(splitter?.content ?? state.content, joinThinking(splitter?.thinking, state.thinking), state.tools, state.usage), error === combined.reason ? void 0 : { cause: error });
|
|
2852
|
+
}
|
|
2853
|
+
throw error;
|
|
2854
|
+
} finally {
|
|
2855
|
+
try {
|
|
2856
|
+
parser?.clear();
|
|
2857
|
+
} finally {
|
|
2858
|
+
timeout.clear();
|
|
2859
|
+
}
|
|
2860
|
+
}
|
|
2861
|
+
}
|
|
2862
|
+
*#fold(record, previous, splitter, signal, commit) {
|
|
2863
|
+
const increment = this.read(record);
|
|
2864
|
+
if (increment.result !== void 0) {
|
|
2865
|
+
commit(increment);
|
|
2866
|
+
return;
|
|
2867
|
+
}
|
|
2868
|
+
const content = splitter?.split(increment.content) ?? increment.content;
|
|
2869
|
+
const usage = increment.usage ?? previous.usage;
|
|
2870
|
+
commit({
|
|
2871
|
+
content: previous.content + increment.content,
|
|
2872
|
+
thinking: previous.thinking + increment.thinking,
|
|
2873
|
+
tools: [...previous.tools, ...increment.tools],
|
|
2874
|
+
...usage === void 0 ? {} : { usage }
|
|
2875
|
+
});
|
|
2876
|
+
if (content.length > 0) yield {
|
|
2877
|
+
channel: "content",
|
|
2878
|
+
text: content
|
|
2879
|
+
};
|
|
2880
|
+
signal.throwIfAborted();
|
|
2881
|
+
if (increment.thinking.length > 0) yield {
|
|
2882
|
+
channel: "thinking",
|
|
2883
|
+
text: increment.thinking
|
|
2884
|
+
};
|
|
2885
|
+
}
|
|
2886
|
+
async #request(request, signal) {
|
|
2887
|
+
const headers = await this.#requestHeaders(signal);
|
|
2888
|
+
signal.throwIfAborted();
|
|
2889
|
+
const response = await this.#transport(this.#url + this.#path, {
|
|
2890
|
+
method: "POST",
|
|
2891
|
+
headers,
|
|
2892
|
+
body: JSON.stringify(this.body(request)),
|
|
2893
|
+
signal
|
|
2894
|
+
});
|
|
2895
|
+
if (!response.ok) {
|
|
2896
|
+
let detail;
|
|
2897
|
+
try {
|
|
2898
|
+
detail = response.body === null ? "" : (await readText(response.body, MAX_ERROR_BODY_LENGTH, signal)).text;
|
|
2899
|
+
} catch (cause) {
|
|
2900
|
+
throw new ProviderError("HTTP", `provider error: ${response.status} - (error body unavailable)`, {
|
|
2901
|
+
status: response.status,
|
|
2902
|
+
cause
|
|
2903
|
+
});
|
|
2904
|
+
}
|
|
2905
|
+
signal.throwIfAborted();
|
|
2906
|
+
throw new ProviderError("HTTP", `provider error: ${response.status}${detail === "" ? "" : ` - ${detail}`}`, { status: response.status });
|
|
2907
|
+
}
|
|
2908
|
+
return response;
|
|
2909
|
+
}
|
|
2910
|
+
async #requestHeaders(signal) {
|
|
2911
|
+
const headers = new Headers({ "Content-Type": "application/json" });
|
|
2912
|
+
if (this.#headers === void 0) return headers;
|
|
2913
|
+
const cleanup = new AbortController();
|
|
2914
|
+
const aborted = Promise.withResolvers();
|
|
2915
|
+
signal.addEventListener("abort", () => aborted.reject(signal.reason), {
|
|
2916
|
+
once: true,
|
|
2917
|
+
signal: cleanup.signal
|
|
2918
|
+
});
|
|
2919
|
+
try {
|
|
2920
|
+
signal.throwIfAborted();
|
|
2921
|
+
const hook = this.#headers;
|
|
2922
|
+
const entries = await Promise.race([Promise.resolve().then(() => hook(signal)), aborted.promise]);
|
|
2923
|
+
for (const [key, value] of Object.entries(entries)) headers.set(key, value);
|
|
2924
|
+
return headers;
|
|
2925
|
+
} finally {
|
|
2926
|
+
cleanup.abort();
|
|
2927
|
+
}
|
|
2928
|
+
}
|
|
2929
|
+
};
|
|
2930
|
+
//#endregion
|
|
2931
|
+
//#region src/core/providers/RelayProvider.ts
|
|
2932
|
+
/**
|
|
2933
|
+
* Carries provider calls over an authenticated NDJSON relay endpoint.
|
|
2934
|
+
*
|
|
2935
|
+
* @remarks
|
|
2936
|
+
* The `ToolCall.caller` member never crosses the hop. The wire body is an owned snapshot of
|
|
2937
|
+
* the projection read through property descriptors, so a serializer reachable only through a
|
|
2938
|
+
* `get` trap or a prototype is never consulted; an own function-valued property such as a
|
|
2939
|
+
* `toJSON` method is a value outside JSON and is refused before fetching, with the clone's
|
|
2940
|
+
* failure as the refusal's `cause`. A remote abort reconstructs a `ProviderAbortError` instance
|
|
2941
|
+
* without aborting the
|
|
2942
|
+
* local signal; the `Agent` runtime treats that instance as an error unless its own bound
|
|
2943
|
+
* signal is aborted.
|
|
2944
|
+
* Content is preserved verbatim, including literal thinking tags.
|
|
2945
|
+
* A refusal reaches the browser as a `ProviderError` instance with the `HTTP` code and status.
|
|
2946
|
+
* This is the browser end alone; {@link createRelay} mounts the server end, and
|
|
2947
|
+
* {@link createRelayProvider}'s example is the browser half of that pair.
|
|
2948
|
+
*
|
|
2949
|
+
* @example
|
|
2950
|
+
* ```ts
|
|
2951
|
+
* import type { ProviderResult } from '@orkestrel/agent'
|
|
2952
|
+
* import { RelayProvider } from '@orkestrel/agent'
|
|
2953
|
+
* // The browser application supplies this parser dependency.
|
|
2954
|
+
* import { createNDJSONParser } from '@orkestrel/ndjson'
|
|
2955
|
+
*
|
|
2956
|
+
* export function ask(bearer: string, signal: AbortSignal): Promise<ProviderResult> {
|
|
2957
|
+
* const browser = new RelayProvider({
|
|
2958
|
+
* url: 'https://relay.example/relay',
|
|
2959
|
+
* parser: createNDJSONParser,
|
|
2960
|
+
* headers: () => ({ authorization: `Bearer ${bearer}` }),
|
|
2961
|
+
* })
|
|
2962
|
+
* return browser.generate([{ id: 'ask', role: 'user', content: 'ping' }], signal)
|
|
2963
|
+
* }
|
|
2964
|
+
* ```
|
|
2965
|
+
*/
|
|
2966
|
+
var RelayProvider = class extends AgentProvider {
|
|
2967
|
+
#parser;
|
|
2968
|
+
constructor(options) {
|
|
2969
|
+
const { url, timeout, fetch, headers, format } = options;
|
|
2970
|
+
super({
|
|
2971
|
+
url,
|
|
2972
|
+
split: false,
|
|
2973
|
+
strict: true,
|
|
2974
|
+
...timeout === void 0 ? {} : { timeout },
|
|
2975
|
+
...fetch === void 0 ? {} : { fetch },
|
|
2976
|
+
...headers === void 0 ? {} : { headers },
|
|
2977
|
+
...format === void 0 ? {} : { format }
|
|
2978
|
+
});
|
|
2979
|
+
this.#parser = options.parser;
|
|
2980
|
+
}
|
|
2981
|
+
/** Identifies the relay backend. */
|
|
2982
|
+
name = "relay";
|
|
2983
|
+
/** Creates fresh framing state for each response. */
|
|
2984
|
+
frame() {
|
|
2985
|
+
return this.#parser();
|
|
2986
|
+
}
|
|
2987
|
+
/**
|
|
2988
|
+
* Projects declared request fields and refuses values the JSON wire cannot carry.
|
|
2989
|
+
*
|
|
2990
|
+
* @remarks
|
|
2991
|
+
* The snapshot is taken from property descriptors, so a custom serializer a projected
|
|
2992
|
+
* value carries is ignored rather than consulted and cannot reach the wire.
|
|
2993
|
+
*
|
|
2994
|
+
* @param request - The domain conversation and call configuration
|
|
2995
|
+
* @returns The validated wire request with caller context omitted
|
|
2996
|
+
* @throws {ProviderError} Thrown when the snapshot cannot be taken — a hostile read or a
|
|
2997
|
+
* value outside JSON — carrying that failure as its `cause`, and when the snapshot the
|
|
2998
|
+
* projection produced is not a valid wire request
|
|
2999
|
+
*/
|
|
3000
|
+
body(request) {
|
|
3001
|
+
let snapshot;
|
|
3002
|
+
try {
|
|
3003
|
+
const projected = {
|
|
3004
|
+
messages: request.messages.map((message) => ({
|
|
3005
|
+
id: message.id,
|
|
3006
|
+
role: message.role,
|
|
3007
|
+
content: message.content,
|
|
3008
|
+
...message.calls === void 0 ? {} : { calls: message.calls.map((call) => ({
|
|
3009
|
+
id: call.id,
|
|
3010
|
+
name: call.name,
|
|
3011
|
+
arguments: call.arguments
|
|
3012
|
+
})) },
|
|
3013
|
+
...message.images === void 0 ? {} : { images: message.images }
|
|
3014
|
+
})),
|
|
3015
|
+
...request.tools === void 0 ? {} : { tools: request.tools.map((tool) => ({
|
|
3016
|
+
name: tool.name,
|
|
3017
|
+
...tool.description === void 0 ? {} : { description: tool.description },
|
|
3018
|
+
...tool.parameters === void 0 ? {} : { parameters: tool.parameters }
|
|
3019
|
+
})) },
|
|
3020
|
+
...request.options === void 0 ? {} : { options: {
|
|
3021
|
+
...request.options.think === void 0 ? {} : { think: request.options.think },
|
|
3022
|
+
...request.options.schema === void 0 ? {} : { schema: request.options.schema }
|
|
3023
|
+
} }
|
|
3024
|
+
};
|
|
3025
|
+
snapshot = cloneJSONValue(projected);
|
|
3026
|
+
} catch (cause) {
|
|
3027
|
+
throw new ProviderError("PROTOCOL", "relay request is not JSON", { cause });
|
|
3028
|
+
}
|
|
3029
|
+
if (!providerRequestContract.is(snapshot)) throw new ProviderError("PROTOCOL", "relay request is not JSON");
|
|
3030
|
+
return snapshot;
|
|
3031
|
+
}
|
|
3032
|
+
/**
|
|
3033
|
+
* Validates a relay frame and translates its channel into the shared stream engine.
|
|
3034
|
+
*
|
|
3035
|
+
* @param record - The framed wire record
|
|
3036
|
+
* @returns A delta contribution or authoritative result
|
|
3037
|
+
* @throws {ProviderAbortError} Thrown for a remote abort carrying its partial
|
|
3038
|
+
* @throws {ProviderError} Thrown for a malformed frame or remote provider failure
|
|
3039
|
+
*/
|
|
3040
|
+
read(record) {
|
|
3041
|
+
if (!relayFrameContract.is(record)) throw new ProviderError("PROTOCOL", "invalid relay frame");
|
|
3042
|
+
switch (record.channel) {
|
|
3043
|
+
case "content": return {
|
|
3044
|
+
content: record.text,
|
|
3045
|
+
thinking: "",
|
|
3046
|
+
tools: []
|
|
3047
|
+
};
|
|
3048
|
+
case "thinking": return {
|
|
3049
|
+
content: "",
|
|
3050
|
+
thinking: record.text,
|
|
3051
|
+
tools: []
|
|
3052
|
+
};
|
|
3053
|
+
case "result": return {
|
|
3054
|
+
content: "",
|
|
3055
|
+
thinking: "",
|
|
3056
|
+
tools: [],
|
|
3057
|
+
result: record.result
|
|
3058
|
+
};
|
|
3059
|
+
case "abort": throw new ProviderAbortError(record.partial);
|
|
3060
|
+
case "error": throw new ProviderError("PROVIDER", record.message);
|
|
3061
|
+
}
|
|
3062
|
+
}
|
|
3063
|
+
/**
|
|
3064
|
+
* Recovers an unterminated final frame by completing its NDJSON line.
|
|
3065
|
+
*
|
|
3066
|
+
* @param parser - The call's retained framing state
|
|
3067
|
+
* @returns Records completed by the final newline
|
|
3068
|
+
*/
|
|
3069
|
+
finish(parser) {
|
|
3070
|
+
return parser.parse("\n");
|
|
3071
|
+
}
|
|
3072
|
+
};
|
|
3073
|
+
//#endregion
|
|
3074
|
+
//#region src/core/RelayStream.ts
|
|
3075
|
+
/**
|
|
3076
|
+
* Streams a provider call as validated NDJSON frames under response backpressure.
|
|
3077
|
+
*
|
|
3078
|
+
* @remarks
|
|
3079
|
+
* Cancellation aborts upstream before returning its iterator. Request listeners are
|
|
3080
|
+
* released on settlement. Unexpected provider failures carry a fixed public message.
|
|
3081
|
+
* An inbound abort leaves the response body neither closed nor errored — a server runtime
|
|
3082
|
+
* cancels that body when the client disconnects — so a consumer that aborts the inbound
|
|
3083
|
+
* signal itself must cancel the body rather than keep reading it.
|
|
3084
|
+
*
|
|
3085
|
+
* @example
|
|
3086
|
+
* ```ts
|
|
3087
|
+
* import type { ProviderInterface } from '@orkestrel/agent'
|
|
3088
|
+
* import { RelayStream } from '@orkestrel/agent'
|
|
3089
|
+
*
|
|
3090
|
+
* export function respond(provider: ProviderInterface, signal: AbortSignal): Response {
|
|
3091
|
+
* return new RelayStream({ provider, request: { messages: [] }, signal }).response
|
|
3092
|
+
* }
|
|
3093
|
+
* ```
|
|
3094
|
+
*/
|
|
3095
|
+
var RelayStream = class {
|
|
3096
|
+
#upstream = new AbortController();
|
|
3097
|
+
#encoder = new TextEncoder();
|
|
3098
|
+
#iterator;
|
|
3099
|
+
#signal;
|
|
3100
|
+
#listener;
|
|
3101
|
+
#response;
|
|
3102
|
+
#settled = false;
|
|
3103
|
+
constructor(options) {
|
|
3104
|
+
this.#signal = options.signal;
|
|
3105
|
+
this.#listener = this.#cancel.bind(this);
|
|
3106
|
+
if (this.#signal.aborted) this.#abortProvider();
|
|
3107
|
+
this.#iterator = options.provider.stream(options.request.messages, this.#upstream.signal, options.request.tools, options.request.options);
|
|
3108
|
+
this.#signal.addEventListener("abort", this.#listener, { once: true });
|
|
3109
|
+
this.#response = new Response(new ReadableStream({
|
|
3110
|
+
pull: this.#pull.bind(this),
|
|
3111
|
+
cancel: this.#cancel.bind(this)
|
|
3112
|
+
}), { headers: {
|
|
3113
|
+
"content-type": RELAY_CONTENT_TYPE,
|
|
3114
|
+
"cache-control": "no-store"
|
|
3115
|
+
} });
|
|
3116
|
+
}
|
|
3117
|
+
/** Exposes the pull-driven response for the provider call. */
|
|
3118
|
+
get response() {
|
|
3119
|
+
return this.#response;
|
|
3120
|
+
}
|
|
3121
|
+
async #pull(controller) {
|
|
3122
|
+
if (this.#settled) return;
|
|
3123
|
+
try {
|
|
3124
|
+
const step = await this.#iterator.next();
|
|
3125
|
+
if (this.#settled) return;
|
|
3126
|
+
this.#write(controller, step.done ? {
|
|
3127
|
+
channel: "result",
|
|
3128
|
+
result: step.value
|
|
3129
|
+
} : step.value);
|
|
3130
|
+
if (step.done) this.#finish(controller);
|
|
3131
|
+
} catch (error) {
|
|
3132
|
+
if (this.#settled) return;
|
|
3133
|
+
const frame = error instanceof ProviderAbortError ? {
|
|
3134
|
+
channel: "abort",
|
|
3135
|
+
partial: error.partial
|
|
3136
|
+
} : {
|
|
3137
|
+
channel: "error",
|
|
3138
|
+
message: RELAY_PROVIDER_MESSAGE
|
|
3139
|
+
};
|
|
3140
|
+
this.#write(controller, relayFrameContract.is(frame) ? frame : {
|
|
3141
|
+
channel: "error",
|
|
3142
|
+
message: RELAY_PROVIDER_MESSAGE
|
|
3143
|
+
});
|
|
3144
|
+
this.#finish(controller);
|
|
3145
|
+
this.#abortProvider();
|
|
3146
|
+
await this.#return();
|
|
3147
|
+
}
|
|
3148
|
+
}
|
|
3149
|
+
async #cancel() {
|
|
3150
|
+
if (this.#settled) return;
|
|
3151
|
+
this.#settled = true;
|
|
3152
|
+
this.#abortProvider();
|
|
3153
|
+
try {
|
|
3154
|
+
await this.#return();
|
|
3155
|
+
} finally {
|
|
3156
|
+
this.#release();
|
|
3157
|
+
}
|
|
3158
|
+
}
|
|
3159
|
+
async #return() {
|
|
3160
|
+
try {
|
|
3161
|
+
await this.#iterator.return({ content: "" });
|
|
3162
|
+
} catch {}
|
|
3163
|
+
}
|
|
3164
|
+
#write(controller, frame) {
|
|
3165
|
+
if (!relayFrameContract.is(frame)) throw new ProviderError("PROTOCOL", "invalid relay frame");
|
|
3166
|
+
controller.enqueue(this.#encoder.encode(`${JSON.stringify(frame)}\n`));
|
|
3167
|
+
}
|
|
3168
|
+
#finish(controller) {
|
|
3169
|
+
this.#settled = true;
|
|
3170
|
+
this.#release();
|
|
3171
|
+
controller.close();
|
|
3172
|
+
}
|
|
3173
|
+
#abortProvider() {
|
|
3174
|
+
this.#upstream.abort(this.#signal.reason);
|
|
3175
|
+
}
|
|
3176
|
+
#release() {
|
|
3177
|
+
this.#signal.removeEventListener("abort", this.#listener);
|
|
3178
|
+
}
|
|
3179
|
+
};
|
|
3180
|
+
//#endregion
|
|
2388
3181
|
//#region src/core/scopes/Scope.ts
|
|
2389
3182
|
/**
|
|
2390
3183
|
* Represents a named, immutable filter over a richer context's items — an optional allow-list per
|
|
@@ -2639,6 +3432,105 @@ var ThinkSplitter = class {
|
|
|
2639
3432
|
//#endregion
|
|
2640
3433
|
//#region src/core/factories.ts
|
|
2641
3434
|
/**
|
|
3435
|
+
* Creates an authorized relay handler that validates a bounded JSON request before streaming.
|
|
3436
|
+
*
|
|
3437
|
+
* @remarks
|
|
3438
|
+
* Answers with the `401` status when authorization refuses or throws, the `400`
|
|
3439
|
+
* status when the body is missing, unreadable, or invalid, the `413` status when
|
|
3440
|
+
* the body fills its byte budget or the inbound read is aborted, and the `502`
|
|
3441
|
+
* status when the upstream provider call cannot be constructed. Refusals carry no body.
|
|
3442
|
+
*
|
|
3443
|
+
* @param options - The upstream provider, authorization decision, and optional byte budget
|
|
3444
|
+
* @returns A fetch-standard handler suitable for a router
|
|
3445
|
+
* @example Mounting the relay on your server
|
|
3446
|
+
* ```ts
|
|
3447
|
+
* import type { ProviderInterface } from '@orkestrel/agent'
|
|
3448
|
+
* import { createRelay } from '@orkestrel/agent'
|
|
3449
|
+
* import { createDispatcher } from '@orkestrel/router'
|
|
3450
|
+
* import { createServer } from '@orkestrel/server'
|
|
3451
|
+
*
|
|
3452
|
+
* declare const upstream: ProviderInterface // the server-side provider holding the credential
|
|
3453
|
+
* declare const bearer: string
|
|
3454
|
+
*
|
|
3455
|
+
* const handler = createRelay({
|
|
3456
|
+
* provider: upstream,
|
|
3457
|
+
* authorize: (request) => request.headers.get('authorization') === `Bearer ${bearer}`,
|
|
3458
|
+
* })
|
|
3459
|
+
* const dispatcher = createDispatcher({
|
|
3460
|
+
* routes: [{ method: 'POST', path: '/relay', handler }],
|
|
3461
|
+
* })
|
|
3462
|
+
*
|
|
3463
|
+
* export function serve(request: Request): Promise<Response> {
|
|
3464
|
+
* return dispatcher.handle(request, undefined)
|
|
3465
|
+
* }
|
|
3466
|
+
*
|
|
3467
|
+
* const server = createServer({ dispatcher, state: () => undefined })
|
|
3468
|
+
* await server.start()
|
|
3469
|
+
* process.on('SIGTERM', () => server.stop()) // signal cancellation, drain, then close the listener
|
|
3470
|
+
* ```
|
|
3471
|
+
*/
|
|
3472
|
+
function createRelay(options) {
|
|
3473
|
+
const { provider, authorize, limit } = options;
|
|
3474
|
+
return async (request) => {
|
|
3475
|
+
try {
|
|
3476
|
+
if (await authorize(request) !== true) return new Response(void 0, { status: 401 });
|
|
3477
|
+
} catch {
|
|
3478
|
+
return new Response(void 0, { status: 401 });
|
|
3479
|
+
}
|
|
3480
|
+
if (request.body === null) return new Response(void 0, { status: 400 });
|
|
3481
|
+
let read;
|
|
3482
|
+
try {
|
|
3483
|
+
read = await readText(request.body, limit ?? 1048576, request.signal);
|
|
3484
|
+
} catch {
|
|
3485
|
+
return new Response(void 0, { status: request.signal.aborted ? 413 : 400 });
|
|
3486
|
+
}
|
|
3487
|
+
if (!read.complete) return new Response(void 0, { status: 413 });
|
|
3488
|
+
const parsed = parseJSONAs(read.text, providerRequestContract.is);
|
|
3489
|
+
if (parsed === void 0) return new Response(void 0, { status: 400 });
|
|
3490
|
+
try {
|
|
3491
|
+
return new RelayStream({
|
|
3492
|
+
provider,
|
|
3493
|
+
request: parsed,
|
|
3494
|
+
signal: request.signal
|
|
3495
|
+
}).response;
|
|
3496
|
+
} catch {
|
|
3497
|
+
return new Response(void 0, { status: 502 });
|
|
3498
|
+
}
|
|
3499
|
+
};
|
|
3500
|
+
}
|
|
3501
|
+
/**
|
|
3502
|
+
* Creates a provider that carries calls through a relay endpoint.
|
|
3503
|
+
*
|
|
3504
|
+
* @remarks
|
|
3505
|
+
* This is the browser end alone. {@link createRelay} mounts the server end, and its example
|
|
3506
|
+
* is the server half this one pairs with.
|
|
3507
|
+
*
|
|
3508
|
+
* @param options - The endpoint, parser factory, and HTTP call configuration
|
|
3509
|
+
* @returns The concrete relay provider
|
|
3510
|
+
* @example Reaching the relay from the browser
|
|
3511
|
+
* ```ts
|
|
3512
|
+
* import type { ProviderInterface } from '@orkestrel/agent'
|
|
3513
|
+
* import { createRelayProvider } from '@orkestrel/agent'
|
|
3514
|
+
* import { createAbort } from '@orkestrel/abort'
|
|
3515
|
+
* // The browser application supplies this parser dependency.
|
|
3516
|
+
* import { createNDJSONParser } from '@orkestrel/ndjson'
|
|
3517
|
+
*
|
|
3518
|
+
* declare const bearer: string
|
|
3519
|
+
* const abort = createAbort()
|
|
3520
|
+
* const messages = [{ id: '1', role: 'user', content: 'Say hello.' }] as const
|
|
3521
|
+
*
|
|
3522
|
+
* const browser: ProviderInterface = createRelayProvider({
|
|
3523
|
+
* url: 'https://app.example/relay',
|
|
3524
|
+
* parser: createNDJSONParser,
|
|
3525
|
+
* headers: () => ({ authorization: `Bearer ${bearer}` }),
|
|
3526
|
+
* })
|
|
3527
|
+
* const result = await browser.generate(messages, abort.signal) // a ProviderResult like a local provider's
|
|
3528
|
+
* ```
|
|
3529
|
+
*/
|
|
3530
|
+
function createRelayProvider(options) {
|
|
3531
|
+
return new RelayProvider(options);
|
|
3532
|
+
}
|
|
3533
|
+
/**
|
|
2642
3534
|
* Creates a conversation — a {@link ConversationInterface} grouping messages above a flat
|
|
2643
3535
|
* message store it owns directly, with compaction into summarized sections, a regenerated
|
|
2644
3536
|
* rollup `summary`, on-demand `rehydrate`, and substring `search`, driven by a
|
|
@@ -2659,8 +3551,8 @@ var ThinkSplitter = class {
|
|
|
2659
3551
|
*
|
|
2660
3552
|
* @example Conversations & compaction
|
|
2661
3553
|
* ```ts
|
|
2662
|
-
* import { createConversation } from '@orkestrel/agent'
|
|
2663
3554
|
* import type { ProviderInterface } from '@orkestrel/agent'
|
|
3555
|
+
* import { createConversation } from '@orkestrel/agent'
|
|
2664
3556
|
*
|
|
2665
3557
|
* declare const provider: ProviderInterface // any concrete implementation supplied by the host app
|
|
2666
3558
|
* // The summarizer seam — built from the provider by the runtime; core stays provider-agnostic.
|
|
@@ -3217,6 +4109,6 @@ function createAgentRunner(options) {
|
|
|
3217
4109
|
});
|
|
3218
4110
|
}
|
|
3219
4111
|
//#endregion
|
|
3220
|
-
export { Agent, AgentContext, AgentError, AgentJobError, AgentRegistry, Authority, CONVERSATION_RECAP_PREFIX, Channel, Conversation, ConversationError, ConversationManager, DEFAULT_AGENT_LIMIT, DEFAULT_AUTHORITY_ZONE, DEFAULT_CONVERSATION_KEEP, DatabaseConversationStore, IMAGE_TOKEN_ESTIMATE, Instruction, InstructionManager, MESSAGE_TOKEN_OVERHEAD, MemoryConversationStore, ProviderAbortError, Scope, ScopeManager, THINK_CLOSE, THINK_OPEN, ThinkSplitter, WORKSPACE_SECTION_HEADER, agentResultToJSON, assembleResult, attachImages, attachUserImages, buildRecapMessage, buildSummaryMessage, collectImageData, createAgent, createAgentContext, createAgentQueue, createAgentRegistry, createAgentRunner, createAuthority, createChannel, createConversation, createConversationManager, createDatabaseConversationStore, createInstruction, createInstructionManager, createMemoryConversationStore, createScope, createScopeManager, createThinkSplitter, denyCall, estimateMessages, estimateTokens, filterAllowList, handleAgentQueueJob, handleAgentRunnerJob, intersectKeys, isAgentError, isAgentJobError, isConversationError, isConversationSnapshot, isMessage, isProviderAbortError, isSection, joinThinking, renderFencedFile, renderSection, resolveClose, resolveItem, resolveOpen, sanitizeToken, sanitizeUsage, settleAgentJob, sumUsage };
|
|
4112
|
+
export { Agent, AgentContext, AgentError, AgentJobError, AgentProvider, AgentRegistry, Authority, CONVERSATION_RECAP_PREFIX, Channel, Conversation, ConversationError, ConversationManager, DEFAULT_AGENT_LIMIT, DEFAULT_AUTHORITY_ZONE, DEFAULT_CONVERSATION_KEEP, DEFAULT_PROVIDER_TIMEOUT, DEFAULT_RELAY_LIMIT, DatabaseConversationStore, IMAGE_TOKEN_ESTIMATE, INVALID_RELAY_STATUS, Instruction, InstructionManager, MAX_ERROR_BODY_LENGTH, MESSAGE_TOKEN_OVERHEAD, MemoryConversationStore, OVERSIZED_RELAY_STATUS, ProviderAbortError, ProviderError, RELAY_CONTENT_TYPE, RELAY_PROVIDER_MESSAGE, RelayProvider, RelayStream, Scope, ScopeManager, THINK_CLOSE, THINK_OPEN, ThinkSplitter, UNAUTHORIZED_RELAY_STATUS, UPSTREAM_RELAY_STATUS, WORKSPACE_SECTION_HEADER, agentResultToJSON, assembleResult, attachImages, attachUserImages, buildProviderResult, buildRecapMessage, buildSummaryMessage, collectImageData, createAgent, createAgentContext, createAgentQueue, createAgentRegistry, createAgentRunner, createAuthority, createChannel, createConversation, createConversationManager, createDatabaseConversationStore, createInstruction, createInstructionManager, createMemoryConversationStore, createRelay, createRelayProvider, createScope, createScopeManager, createThinkSplitter, denyCall, estimateMessages, estimateTokens, filterAllowList, handleAgentQueueJob, handleAgentRunnerJob, intersectKeys, isAgentError, isAgentJobError, isConversationError, isConversationSnapshot, isMessage, isProviderAbortError, isProviderError, isSection, joinThinking, messageContract, messageShape, providerRequestContract, providerRequestShape, providerResultContract, providerResultShape, readChunks, readText, relayFrameContract, relayFrameShape, renderFencedFile, renderSection, resolveClose, resolveItem, resolveOpen, sanitizeToken, sanitizeUsage, settleAgentJob, sumUsage, toolCallShape };
|
|
3221
4113
|
|
|
3222
4114
|
//# sourceMappingURL=index.js.map
|