@deepseek-ai/dsh-repeat-tool-reminder 0.1.1-rc.2 → 0.1.2-alpha.2
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.i18n.yaml +2 -2
- package/README.md +116 -20
- package/README.zh.md +126 -30
- package/lib/index.js +1104 -15
- package/package.json +15 -14
package/lib/index.js
CHANGED
|
@@ -1,24 +1,157 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
2
|
import z from "@deepseek-ai/schemastery";
|
|
3
|
-
import "@deepseek-ai/cordis";
|
|
4
|
-
//#region ../../
|
|
3
|
+
import { Service } from "@deepseek-ai/cordis";
|
|
4
|
+
//#region ../../typert/protocol/src/remote-error.ts
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* One Remote call failure: a real Error carrying its stable code and typed
|
|
7
|
+
* details. Owners throw it at the failure point; the Host Gateway encodes it
|
|
8
|
+
* onto the wire unchanged; the Client face rebuilds an instance for the
|
|
9
|
+
* `RemoteResult` error branch, so `throw result.error` keeps throw semantics.
|
|
10
|
+
* Discrimination is always by `code`, never by instanceof.
|
|
9
11
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
var RemoteError = class extends Error {
|
|
13
|
+
code;
|
|
14
|
+
details;
|
|
15
|
+
/** Structural marker: cross-realm/bundle identification never uses instanceof. */
|
|
16
|
+
isDSHRemoteError = true;
|
|
17
|
+
/**
|
|
18
|
+
* @param code - stable failure code declared in {@link RemoteErrorDetailsMap}.
|
|
19
|
+
* @param message - human diagnostic carried across the wire.
|
|
20
|
+
* @param details - structured payload typed by the code.
|
|
21
|
+
* @param options - standard Error options (`cause` survives in-process only).
|
|
22
|
+
*/
|
|
23
|
+
constructor(code, message, details, options) {
|
|
24
|
+
super(message, options);
|
|
25
|
+
this.code = code;
|
|
26
|
+
this.details = details;
|
|
27
|
+
this.name = "RemoteError";
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region ../../typert/protocol/src/index.ts
|
|
32
|
+
/**
|
|
33
|
+
* Remote decorators and explicit Gateway bindings backed by versioned
|
|
34
|
+
* descriptors carried on decorated class prototypes. Strict reflection
|
|
35
|
+
* remains a Typert compiler responsibility.
|
|
36
|
+
* @module @deepseek-ai/dsh-typert-protocol
|
|
37
|
+
*/
|
|
38
|
+
const TYPERT_REMOTE_SEGMENT_PATTERN = /^[A-Za-z0-9_$.-]+$/;
|
|
39
|
+
/**
|
|
40
|
+
* Test one generated Remote name against the Connection endpoint grammar.
|
|
41
|
+
* @param value - namespace, method, lookup, or Context segment.
|
|
42
|
+
* @returns whether the value can cross the shared RPC carrier unchanged.
|
|
43
|
+
*/
|
|
44
|
+
function isTypertRemoteSegment(value) {
|
|
45
|
+
return value !== "." && value !== ".." && TYPERT_REMOTE_SEGMENT_PATTERN.test(value);
|
|
46
|
+
}
|
|
47
|
+
const REMOTE_METHOD_DESCRIPTOR = "@deepseek-ai/dsh-typert-protocol/remote-methods";
|
|
48
|
+
/**
|
|
49
|
+
* Bind one visible Service field to a Cordis key and Remote namespace.
|
|
50
|
+
* @param service - owning Service instance, normally `this`.
|
|
51
|
+
* @param serviceKey - exact Cordis service key.
|
|
52
|
+
* @param options - optional distinct wire namespace.
|
|
53
|
+
* @returns a frozen, inspectable binding with no compiler-injected metadata.
|
|
54
|
+
*/
|
|
55
|
+
function bindTypertRemote(service, serviceKey, options = {}) {
|
|
56
|
+
validateName("service key", serviceKey);
|
|
57
|
+
const namespace = options.namespace ?? serviceKey;
|
|
58
|
+
validateName("namespace", namespace);
|
|
59
|
+
return Object.freeze({
|
|
60
|
+
service,
|
|
61
|
+
serviceKey,
|
|
62
|
+
namespace
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/** Cordis Service base that exposes its registered name through Typert Gateway. */
|
|
66
|
+
var TypertRemoteService = class extends Service {
|
|
67
|
+
/** Visible binding consumed by the Gateway's source-mode discovery. */
|
|
68
|
+
typertRemote;
|
|
69
|
+
/**
|
|
70
|
+
* Register the Service and bind the same key to Typert Gateway.
|
|
71
|
+
* @param ctx - owning Cordis Context.
|
|
72
|
+
* @param serviceKey - exact Cordis service key and default wire namespace.
|
|
73
|
+
* @param options - optional distinct wire namespace.
|
|
74
|
+
*/
|
|
75
|
+
constructor(ctx, serviceKey, options = {}) {
|
|
76
|
+
super(ctx, serviceKey);
|
|
77
|
+
this.typertRemote = bindTypertRemote(this, this.name, options);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
function Remote(methodExportOrOptions, context) {
|
|
81
|
+
if (typeof methodExportOrOptions === "string") {
|
|
82
|
+
validateName("Remote export name", methodExportOrOptions);
|
|
83
|
+
return remoteDecorator({ kind: "direct" }, void 0, methodExportOrOptions);
|
|
84
|
+
}
|
|
85
|
+
if (typeof methodExportOrOptions === "object") {
|
|
86
|
+
if (remoteOptionMode(methodExportOrOptions) !== "stream" || Reflect.ownKeys(methodExportOrOptions).length !== 1) throw new TypeError("typert-protocol: Remote options must contain exactly mode: \"stream\"");
|
|
87
|
+
return remoteDecorator({ kind: "direct" }, "stream");
|
|
88
|
+
}
|
|
89
|
+
if (context === void 0) throw new TypeError("typert-protocol: Remote decorator context is missing");
|
|
90
|
+
addMarkerInitializer(context, { kind: "direct" });
|
|
91
|
+
}
|
|
92
|
+
function remoteOptionMode(options) {
|
|
93
|
+
return Reflect.get(options, "mode");
|
|
94
|
+
}
|
|
95
|
+
function remoteDecorator(invocation, mode, exportName) {
|
|
96
|
+
return function(_method, context) {
|
|
97
|
+
addMarkerInitializer(context, invocation, mode, exportName);
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function readRemoteMethodDescriptor(prototype) {
|
|
101
|
+
const property = Object.getOwnPropertyDescriptor(prototype, REMOTE_METHOD_DESCRIPTOR);
|
|
102
|
+
if (property === void 0) return void 0;
|
|
103
|
+
const descriptor = property.value;
|
|
104
|
+
if (descriptor === null || typeof descriptor !== "object") throw new TypeError("typert-protocol: Remote method descriptor must be an object");
|
|
105
|
+
const version = Reflect.get(descriptor, "version");
|
|
106
|
+
if (version !== 1) throw new TypeError(`typert-protocol: unsupported Remote method descriptor version ${String(version)}`);
|
|
107
|
+
const methods = Reflect.get(descriptor, "methods");
|
|
108
|
+
if (!Array.isArray(methods)) throw new TypeError("typert-protocol: Remote method descriptor methods must be an array");
|
|
109
|
+
return descriptor;
|
|
110
|
+
}
|
|
111
|
+
function addMarkerInitializer(context, invocation, mode, exportName) {
|
|
112
|
+
if (context.private || context.static || typeof context.name !== "string") throw new TypeError("typert-protocol: Remote decorators require a public instance method with a string name");
|
|
113
|
+
const method = context.name;
|
|
114
|
+
context.addInitializer(function() {
|
|
115
|
+
const prototype = Object.getPrototypeOf(this);
|
|
116
|
+
if (prototype === null) throw new TypeError(`typert-protocol: cannot mark Remote method "${method}" on an object without a prototype`);
|
|
117
|
+
mark(prototype, method, invocation, mode, exportName);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
function mark(prototype, method, invocation, mode, exportName) {
|
|
121
|
+
const descriptor = readRemoteMethodDescriptor(prototype);
|
|
122
|
+
const marker = Object.freeze({
|
|
123
|
+
method,
|
|
124
|
+
...exportName === void 0 || exportName === method ? {} : { exportName },
|
|
125
|
+
...mode === void 0 ? {} : { mode },
|
|
126
|
+
invocation: Object.freeze(invocation)
|
|
127
|
+
});
|
|
128
|
+
const current = descriptor?.methods.find((candidate) => candidate.method === method);
|
|
129
|
+
if (current !== void 0) {
|
|
130
|
+
if (current.exportName === marker.exportName && current.mode === marker.mode && sameInvocation(current.invocation, invocation)) return;
|
|
131
|
+
throw new Error(`typert-protocol: Remote method "${method}" has conflicting invocation markers`);
|
|
132
|
+
}
|
|
133
|
+
Object.defineProperty(prototype, REMOTE_METHOD_DESCRIPTOR, {
|
|
134
|
+
configurable: true,
|
|
135
|
+
value: Object.freeze({
|
|
136
|
+
version: 1,
|
|
137
|
+
methods: Object.freeze([...descriptor?.methods ?? [], marker])
|
|
138
|
+
})
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
function sameInvocation(left, right) {
|
|
142
|
+
if (left.kind === "direct") return right.kind === "direct";
|
|
143
|
+
if (right.kind === "direct") return false;
|
|
144
|
+
return left.context === right.context;
|
|
145
|
+
}
|
|
146
|
+
function validateName(subject, value) {
|
|
147
|
+
if (!isTypertRemoteSegment(value)) throw new TypeError(`typert-protocol: ${subject} must contain only RPC endpoint segment characters`);
|
|
12
148
|
}
|
|
13
149
|
//#endregion
|
|
14
|
-
//#region ../../
|
|
150
|
+
//#region ../../util/values/src/index.ts
|
|
15
151
|
/**
|
|
16
|
-
* Deep-freeze
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* request's live cancellation channel and freezing them breaks abort.
|
|
20
|
-
* @param value - the value to freeze in place.
|
|
21
|
-
* @returns the same value, frozen.
|
|
152
|
+
* Deep-freeze an object graph in place while leaving live AbortSignal objects mutable.
|
|
153
|
+
* @param value - value to freeze.
|
|
154
|
+
* @returns the same value after every reachable enumerable child is frozen.
|
|
22
155
|
*/
|
|
23
156
|
function deepFreeze(value) {
|
|
24
157
|
const seen = /* @__PURE__ */ new WeakSet();
|
|
@@ -58,6 +191,29 @@ function deepFreeze(value) {
|
|
|
58
191
|
return value;
|
|
59
192
|
}
|
|
60
193
|
//#endregion
|
|
194
|
+
//#region ../../util/crypto/src/index.ts
|
|
195
|
+
/**
|
|
196
|
+
* Random v4 UUID, minted from `crypto.getRandomValues`.
|
|
197
|
+
* @returns the UUID string.
|
|
198
|
+
*/
|
|
199
|
+
function randomUUID() {
|
|
200
|
+
const bytes = globalThis.crypto.getRandomValues(new Uint8Array(16));
|
|
201
|
+
const hex = Array.from(bytes, (byte, index) => {
|
|
202
|
+
return (index === 6 ? byte & 15 | 64 : index === 8 ? byte & 63 | 128 : byte).toString(16).padStart(2, "0");
|
|
203
|
+
}).join("");
|
|
204
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
205
|
+
}
|
|
206
|
+
//#endregion
|
|
207
|
+
//#region ../../util/brand/src/index.ts
|
|
208
|
+
/**
|
|
209
|
+
* Apply a compile-time string brand without changing the value.
|
|
210
|
+
* @param value - string admitted by the domain that owns the target brand.
|
|
211
|
+
* @returns the same string with the requested compile-time brand.
|
|
212
|
+
*/
|
|
213
|
+
function brandString(value) {
|
|
214
|
+
return value;
|
|
215
|
+
}
|
|
216
|
+
//#endregion
|
|
61
217
|
//#region ../../llm/llm/src/message.ts
|
|
62
218
|
/** Message value types, identity, and immutable construction helpers. */
|
|
63
219
|
/**
|
|
@@ -76,7 +232,7 @@ function freezeMessage(message) {
|
|
|
76
232
|
function createMessage(input) {
|
|
77
233
|
return freezeMessage({
|
|
78
234
|
...input,
|
|
79
|
-
id:
|
|
235
|
+
id: brandString(randomUUID())
|
|
80
236
|
});
|
|
81
237
|
}
|
|
82
238
|
/**
|
|
@@ -97,6 +253,26 @@ const MAX_TIMER_DELAY_MS = 2147483647;
|
|
|
97
253
|
//#endregion
|
|
98
254
|
//#region ../../llm/llm/src/error.ts
|
|
99
255
|
/**
|
|
256
|
+
* Harness error base with a stable machine-routable code and chained cause.
|
|
257
|
+
* Package errors extend it so tool results and replay can retain failure class.
|
|
258
|
+
* @module @deepseek-ai/dsh-llm/error
|
|
259
|
+
*/
|
|
260
|
+
/**
|
|
261
|
+
* Base class for all harness errors. Carries a `code` (stable, programmatic —
|
|
262
|
+
* e.g. `NO_ADAPTER`, `INVALID_ARGS`, `INVARIANT`) distinct from the
|
|
263
|
+
* human-readable `message`, and supports `cause` chaining via the standard
|
|
264
|
+
* `ErrorOptions`. `name` defaults to the subclass constructor name.
|
|
265
|
+
*/
|
|
266
|
+
var HarnessError = class extends Error {
|
|
267
|
+
/** Stable machine-routable failure class (e.g. `RATE_LIMIT`); route on this, never by parsing `message`. */
|
|
268
|
+
code;
|
|
269
|
+
constructor(message, code, options) {
|
|
270
|
+
super(message, options);
|
|
271
|
+
this.code = code;
|
|
272
|
+
this.name = new.target.name;
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
/**
|
|
100
276
|
* Canonical provider-neutral code for a response that completed normally but
|
|
101
277
|
* carried no content blocks at all. Providers occasionally emit a degenerate
|
|
102
278
|
* completion (a terminal stop with zero output); adapters classify it as this
|
|
@@ -146,6 +322,240 @@ const alwaysPolicySchema = z.object({
|
|
|
146
322
|
backoff: backoffSchema
|
|
147
323
|
});
|
|
148
324
|
z.union([normalPolicySchema, alwaysPolicySchema]);
|
|
325
|
+
const NORMAL_POLICY_KEYS = new Set([
|
|
326
|
+
"mode",
|
|
327
|
+
"maxRetries",
|
|
328
|
+
"retryableCodes",
|
|
329
|
+
"backoff"
|
|
330
|
+
]);
|
|
331
|
+
const ALWAYS_POLICY_KEYS = new Set([
|
|
332
|
+
"mode",
|
|
333
|
+
"maxRetries",
|
|
334
|
+
"retryableCodes",
|
|
335
|
+
"backoff"
|
|
336
|
+
]);
|
|
337
|
+
const BACKOFF_KEYS = new Set([
|
|
338
|
+
"initialDelayMs",
|
|
339
|
+
"maxDelayMs",
|
|
340
|
+
"jitterRatio"
|
|
341
|
+
]);
|
|
342
|
+
function validateKeys(value, allowed, path) {
|
|
343
|
+
for (const key of Object.keys(value)) if (!allowed.has(key)) throw new Error(`${path}: unknown key "${key}"`);
|
|
344
|
+
}
|
|
345
|
+
function resolveBackoff(config, path) {
|
|
346
|
+
if (config !== void 0) validateKeys(config, BACKOFF_KEYS, path);
|
|
347
|
+
const initialDelayMs = config?.initialDelayMs ?? DEFAULT_INITIAL_DELAY_MS;
|
|
348
|
+
const maxDelayMs = config?.maxDelayMs ?? DEFAULT_MAX_DELAY_MS;
|
|
349
|
+
const jitterRatio = config?.jitterRatio ?? DEFAULT_JITTER_RATIO;
|
|
350
|
+
if (!Number.isFinite(initialDelayMs) || initialDelayMs <= 0 || initialDelayMs > 2147483647) throw new Error(`${path}.initialDelayMs must be a positive finite number no greater than ${MAX_TIMER_DELAY_MS}`);
|
|
351
|
+
if (!Number.isFinite(maxDelayMs) || maxDelayMs <= 0 || maxDelayMs > 2147483647) throw new Error(`${path}.maxDelayMs must be a positive finite number no greater than ${MAX_TIMER_DELAY_MS}`);
|
|
352
|
+
if (initialDelayMs > maxDelayMs) throw new Error(`${path}.initialDelayMs must be less than or equal to maxDelayMs`);
|
|
353
|
+
if (!Number.isFinite(jitterRatio) || jitterRatio < 0 || jitterRatio > 1) throw new Error(`${path}.jitterRatio must be between 0 and 1`);
|
|
354
|
+
return Object.freeze({
|
|
355
|
+
initialDelayMs,
|
|
356
|
+
maxDelayMs,
|
|
357
|
+
jitterRatio
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Validate, default, and detach one provider-owned retry policy.
|
|
362
|
+
* @param config - optional provider configuration; omission selects normal defaults.
|
|
363
|
+
* @param path - diagnostic path naming the provider config that owns the value.
|
|
364
|
+
* @returns an immutable policy safe to capture in provider registration state.
|
|
365
|
+
*/
|
|
366
|
+
function resolveRetryPolicy(config, path) {
|
|
367
|
+
if (config === void 0) return Object.freeze({
|
|
368
|
+
mode: "normal",
|
|
369
|
+
maxRetries: DEFAULT_MAX_RETRIES,
|
|
370
|
+
retryableCodes: DEFAULT_RETRYABLE_CODES,
|
|
371
|
+
...resolveBackoff(void 0, `${path}.backoff`)
|
|
372
|
+
});
|
|
373
|
+
switch (config.mode) {
|
|
374
|
+
case "normal": {
|
|
375
|
+
validateKeys(config, NORMAL_POLICY_KEYS, path);
|
|
376
|
+
const maxRetries = config.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
377
|
+
const retryableCodes = config.retryableCodes ?? [...DEFAULT_RETRYABLE_CODES];
|
|
378
|
+
if (!Number.isSafeInteger(maxRetries) || maxRetries < 0) throw new Error(`${path}.maxRetries must be a non-negative safe integer`);
|
|
379
|
+
if (retryableCodes.length === 0) throw new Error(`${path}.retryableCodes must not be empty`);
|
|
380
|
+
if (retryableCodes.some((code) => typeof code !== "string" || code.length === 0)) throw new Error(`${path}.retryableCodes must contain only non-empty strings`);
|
|
381
|
+
if (new Set(retryableCodes).size !== retryableCodes.length) throw new Error(`${path}.retryableCodes must not contain duplicates`);
|
|
382
|
+
return Object.freeze({
|
|
383
|
+
mode: "normal",
|
|
384
|
+
maxRetries,
|
|
385
|
+
retryableCodes: Object.freeze([...retryableCodes]),
|
|
386
|
+
...resolveBackoff(config.backoff, `${path}.backoff`)
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
case "always":
|
|
390
|
+
validateKeys(config, ALWAYS_POLICY_KEYS, path);
|
|
391
|
+
return Object.freeze({
|
|
392
|
+
mode: "always",
|
|
393
|
+
...resolveBackoff(config.backoff, `${path}.backoff`)
|
|
394
|
+
});
|
|
395
|
+
default: throw new Error(`${path}.mode must be "normal" or "always"`);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
//#endregion
|
|
399
|
+
//#region ../../llm/llm/src/call-config.ts
|
|
400
|
+
/**
|
|
401
|
+
* Field-wise equality over {@link LlmCallConfig} — the comparison a caller
|
|
402
|
+
* runs to decide whether a proposed configuration is a real change (worth a
|
|
403
|
+
* logged header snapshot) or the held one restated.
|
|
404
|
+
* @param a - one configuration.
|
|
405
|
+
* @param b - the other.
|
|
406
|
+
* @returns whether every field (including the `stop` list, element-wise) matches.
|
|
407
|
+
*/
|
|
408
|
+
function callConfigEquals(a, b) {
|
|
409
|
+
if (a.provider !== b.provider || a.model !== b.model || a.reasoningEffort !== b.reasoningEffort || a.temperature !== b.temperature || a.maxTokens !== b.maxTokens) return false;
|
|
410
|
+
if (a.stop === void 0 || b.stop === void 0) return a.stop === b.stop;
|
|
411
|
+
return a.stop.length === b.stop.length && a.stop.every((s, i) => s === b.stop?.[i]);
|
|
412
|
+
}
|
|
413
|
+
//#endregion
|
|
414
|
+
//#region ../../llm/llm/src/adapter-failure.ts
|
|
415
|
+
/**
|
|
416
|
+
* Normalization for values thrown by a final LLM adapter boundary.
|
|
417
|
+
*
|
|
418
|
+
* @module @deepseek-ai/dsh-llm/adapter-failure
|
|
419
|
+
*/
|
|
420
|
+
/**
|
|
421
|
+
* Detach serializable provider facts from a value thrown by an adapter.
|
|
422
|
+
* @param value - arbitrary value thrown during adapter dispatch or iteration.
|
|
423
|
+
* @returns immutable provider-neutral facts suitable for a terminal finish chunk.
|
|
424
|
+
* @internal
|
|
425
|
+
*/
|
|
426
|
+
function normalizeLlmFailure(value) {
|
|
427
|
+
const error = value instanceof Error ? value : new HarnessError(thrownMessage(value), "UNKNOWN", { cause: value });
|
|
428
|
+
const carried = ownFailureSnapshot(error);
|
|
429
|
+
if (carried !== void 0 && carried.code === ownErrorCode(error)) return carried;
|
|
430
|
+
return Object.freeze({
|
|
431
|
+
message: errorMessage(error),
|
|
432
|
+
code: harnessErrorCode(error)
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
/** Render a non-Error throw without letting hostile coercion escape normalization. */
|
|
436
|
+
function thrownMessage(value) {
|
|
437
|
+
try {
|
|
438
|
+
const message = String(value);
|
|
439
|
+
return message.length > 0 ? message : "LLM adapter failed";
|
|
440
|
+
} catch (_hostileThrownValue) {
|
|
441
|
+
return "LLM adapter failed";
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
/** Read a foreign error's own data-backed `code` without invoking accessors. */
|
|
445
|
+
function ownErrorCode(error) {
|
|
446
|
+
try {
|
|
447
|
+
const descriptor = Object.getOwnPropertyDescriptor(error, "code");
|
|
448
|
+
return descriptor !== void 0 && "value" in descriptor ? descriptor.value : void 0;
|
|
449
|
+
} catch (_sdkPropertyTrap) {
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
/** Snapshot an own data property without invoking an SDK-defined accessor. */
|
|
454
|
+
function ownFailureSnapshot(error) {
|
|
455
|
+
try {
|
|
456
|
+
const descriptor = Object.getOwnPropertyDescriptor(error, "failure");
|
|
457
|
+
return descriptor !== void 0 && "value" in descriptor ? failureSnapshot(descriptor.value) : void 0;
|
|
458
|
+
} catch (_sdkPropertyTrap) {
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
/** Validate and detach an arbitrary serializable failure payload. */
|
|
463
|
+
function failureSnapshot(value) {
|
|
464
|
+
if (typeof value !== "object" || value === null) return void 0;
|
|
465
|
+
try {
|
|
466
|
+
const candidate = value;
|
|
467
|
+
const message = candidate.message;
|
|
468
|
+
const code = candidate.code;
|
|
469
|
+
const status = candidate.status;
|
|
470
|
+
const providerRetryAfterMs = candidate.providerRetryAfterMs;
|
|
471
|
+
const requestId = candidate.requestId;
|
|
472
|
+
if (typeof message !== "string" || message.length === 0 || typeof code !== "string" || code.length === 0 || status !== void 0 && (!Number.isInteger(status) || status < 100 || status > 599) || providerRetryAfterMs !== void 0 && (!Number.isFinite(providerRetryAfterMs) || providerRetryAfterMs <= 0) || requestId !== void 0 && (typeof requestId !== "string" || requestId.length === 0)) return void 0;
|
|
473
|
+
return Object.freeze({
|
|
474
|
+
message,
|
|
475
|
+
code,
|
|
476
|
+
...status === void 0 ? {} : { status },
|
|
477
|
+
...providerRetryAfterMs === void 0 ? {} : { providerRetryAfterMs },
|
|
478
|
+
...requestId === void 0 ? {} : { requestId }
|
|
479
|
+
});
|
|
480
|
+
} catch (_sdkFailureGetter) {
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
/** Read an SDK error message without letting an accessor replace the primary failure. */
|
|
485
|
+
function errorMessage(error) {
|
|
486
|
+
try {
|
|
487
|
+
const message = error.message;
|
|
488
|
+
if (typeof message === "string" && message.length > 0) return message;
|
|
489
|
+
} catch (_sdkMessageGetter) {}
|
|
490
|
+
return "LLM adapter failed";
|
|
491
|
+
}
|
|
492
|
+
/** Trust only Harness-owned codes; third-party SDK codes are not our taxonomy. */
|
|
493
|
+
function harnessErrorCode(error) {
|
|
494
|
+
return error instanceof HarnessError ? error.code : "UNKNOWN";
|
|
495
|
+
}
|
|
496
|
+
//#endregion
|
|
497
|
+
//#region ../../llm/llm/src/content.ts
|
|
498
|
+
/**
|
|
499
|
+
* Stable text shown to a model that cannot accept one durable image reference.
|
|
500
|
+
* @param ref - durable normalized attachment omitted from the request.
|
|
501
|
+
* @returns deterministic text-only placeholder.
|
|
502
|
+
*/
|
|
503
|
+
function textOnlyImageText(ref) {
|
|
504
|
+
return `[image omitted because this model accepts text only; attachment sha256:${String(ref.attachmentId).slice(7, 15)}]`;
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* True when typed model content contains an image block, walking nested
|
|
508
|
+
* tool-result content. This is the one recursive image walk shared by every
|
|
509
|
+
* image policy (capability gating, text-only serialization, compaction
|
|
510
|
+
* survey), so a consumer cannot silently diverge on nesting depth.
|
|
511
|
+
* @param content - typed model content blocks.
|
|
512
|
+
* @returns whether any nested block is an image.
|
|
513
|
+
*/
|
|
514
|
+
function contentHasImage(content) {
|
|
515
|
+
return content.some((block) => block.type === "image" || block.type === "tool-result" && contentHasImage(block.content));
|
|
516
|
+
}
|
|
517
|
+
/** Replace every image occurrence, including nested tool results, for a text-only model. */
|
|
518
|
+
function replaceImagesForTextModel(blocks) {
|
|
519
|
+
let next;
|
|
520
|
+
for (const [index, block] of blocks.entries()) {
|
|
521
|
+
if (block.type === "image") {
|
|
522
|
+
next ??= blocks.slice(0, index);
|
|
523
|
+
next.push({
|
|
524
|
+
type: "text",
|
|
525
|
+
text: textOnlyImageText(block.attachment)
|
|
526
|
+
});
|
|
527
|
+
continue;
|
|
528
|
+
}
|
|
529
|
+
if (block.type === "tool-result") {
|
|
530
|
+
const content = replaceImagesForTextModel(block.content);
|
|
531
|
+
if (content !== block.content) {
|
|
532
|
+
next ??= blocks.slice(0, index);
|
|
533
|
+
next.push({
|
|
534
|
+
...block,
|
|
535
|
+
content
|
|
536
|
+
});
|
|
537
|
+
continue;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
next?.push(block);
|
|
541
|
+
}
|
|
542
|
+
return next ?? blocks;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Project durable image history into deterministic text for an exact text-only model.
|
|
546
|
+
* @param messages - complete request history.
|
|
547
|
+
* @returns the original list without images, otherwise shallow message copies with stable placeholders.
|
|
548
|
+
*/
|
|
549
|
+
function projectImagesForTextModel(messages) {
|
|
550
|
+
if (!messages.some((message) => contentHasImage(message.content))) return messages;
|
|
551
|
+
return messages.map((message) => {
|
|
552
|
+
const content = replaceImagesForTextModel(message.content);
|
|
553
|
+
return content === message.content ? message : {
|
|
554
|
+
...message,
|
|
555
|
+
content
|
|
556
|
+
};
|
|
557
|
+
});
|
|
558
|
+
}
|
|
149
559
|
//#endregion
|
|
150
560
|
//#region ../../llm/llm/src/attribution.ts
|
|
151
561
|
/**
|
|
@@ -158,6 +568,685 @@ z.union([normalPolicySchema, alwaysPolicySchema]);
|
|
|
158
568
|
*/
|
|
159
569
|
const { version } = createRequire(import.meta.url)("../package.json");
|
|
160
570
|
//#endregion
|
|
571
|
+
//#region ../../llm/llm/src/index.ts
|
|
572
|
+
/**
|
|
573
|
+
* LLM service: adapter registry with a waterfall-interceptable streaming call
|
|
574
|
+
* API. Exports the `LlmRuntime` default, the abstract `LlmAdapter` for
|
|
575
|
+
* provider backends, and `BlockAssembler` for chunk assembly.
|
|
576
|
+
*
|
|
577
|
+
* @module @deepseek-ai/dsh-llm
|
|
578
|
+
*/
|
|
579
|
+
var __runInitializers = function(thisArg, initializers, value) {
|
|
580
|
+
var useValue = arguments.length > 2;
|
|
581
|
+
for (var i = 0; i < initializers.length; i++) value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
582
|
+
return useValue ? value : void 0;
|
|
583
|
+
};
|
|
584
|
+
var __esDecorate = function(ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
585
|
+
function accept(f) {
|
|
586
|
+
if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected");
|
|
587
|
+
return f;
|
|
588
|
+
}
|
|
589
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
590
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
591
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
592
|
+
var _, done = false;
|
|
593
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
594
|
+
var context = {};
|
|
595
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
596
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
597
|
+
context.addInitializer = function(f) {
|
|
598
|
+
if (done) throw new TypeError("Cannot add initializers after decoration has completed");
|
|
599
|
+
extraInitializers.push(accept(f || null));
|
|
600
|
+
};
|
|
601
|
+
var result = (0, decorators[i])(kind === "accessor" ? {
|
|
602
|
+
get: descriptor.get,
|
|
603
|
+
set: descriptor.set
|
|
604
|
+
} : descriptor[key], context);
|
|
605
|
+
if (kind === "accessor") {
|
|
606
|
+
if (result === void 0) continue;
|
|
607
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
608
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
609
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
610
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
611
|
+
} else if (_ = accept(result)) if (kind === "field") initializers.unshift(_);
|
|
612
|
+
else descriptor[key] = _;
|
|
613
|
+
}
|
|
614
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
615
|
+
done = true;
|
|
616
|
+
};
|
|
617
|
+
/**
|
|
618
|
+
* Typed error for LLM-related failures. Extends {@link HarnessError}, so the
|
|
619
|
+
* `code` string (e.g. `AUTH`, `RATE_LIMIT`, `NO_ADAPTER`) is shared taxonomy.
|
|
620
|
+
*/
|
|
621
|
+
var LlmError = class extends HarnessError {
|
|
622
|
+
/** Serializable facts retained beside this live Error. */
|
|
623
|
+
failure;
|
|
624
|
+
/**
|
|
625
|
+
* @param message - non-empty human-readable failure summary.
|
|
626
|
+
* @param code - non-empty stable provider-neutral machine code.
|
|
627
|
+
* @param options - optional cause and validated serializable provider facts.
|
|
628
|
+
*/
|
|
629
|
+
constructor(message, code, options) {
|
|
630
|
+
if (typeof message !== "string" || message.length === 0) throw new Error("LlmError message must be a non-empty string");
|
|
631
|
+
if (typeof code !== "string" || code.length === 0) throw new Error("LlmError code must be a non-empty string");
|
|
632
|
+
if (options?.status !== void 0 && (!Number.isInteger(options.status) || options.status < 100 || options.status > 599)) throw new Error("LlmError status must be an integer from 100 through 599");
|
|
633
|
+
if (options?.providerRetryAfterMs !== void 0 && (!Number.isFinite(options.providerRetryAfterMs) || options.providerRetryAfterMs <= 0)) throw new Error("LlmError providerRetryAfterMs must be a positive finite number");
|
|
634
|
+
if (options?.requestId !== void 0 && (typeof options.requestId !== "string" || options.requestId.length === 0)) throw new Error("LlmError requestId must be a non-empty string");
|
|
635
|
+
super(message, code, options);
|
|
636
|
+
this.name = "LlmError";
|
|
637
|
+
this.failure = Object.freeze({
|
|
638
|
+
message,
|
|
639
|
+
code,
|
|
640
|
+
...options?.status === void 0 ? {} : { status: options.status },
|
|
641
|
+
...options?.providerRetryAfterMs === void 0 ? {} : { providerRetryAfterMs: options.providerRetryAfterMs },
|
|
642
|
+
...options?.requestId === void 0 ? {} : { requestId: options.requestId }
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
(() => {
|
|
647
|
+
let _classSuper = TypertRemoteService;
|
|
648
|
+
let _instanceExtraInitializers = [];
|
|
649
|
+
let _listProviders_decorators;
|
|
650
|
+
let _listConfigurableProviders_decorators;
|
|
651
|
+
let _remoteDiscoverModels_decorators;
|
|
652
|
+
return class LlmRuntime extends _classSuper {
|
|
653
|
+
static {
|
|
654
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
655
|
+
_listProviders_decorators = [Remote];
|
|
656
|
+
_listConfigurableProviders_decorators = [Remote];
|
|
657
|
+
_remoteDiscoverModels_decorators = [Remote("discoverModels")];
|
|
658
|
+
__esDecorate(this, null, _listProviders_decorators, {
|
|
659
|
+
kind: "method",
|
|
660
|
+
name: "listProviders",
|
|
661
|
+
static: false,
|
|
662
|
+
private: false,
|
|
663
|
+
access: {
|
|
664
|
+
has: (obj) => "listProviders" in obj,
|
|
665
|
+
get: (obj) => obj.listProviders
|
|
666
|
+
},
|
|
667
|
+
metadata: _metadata
|
|
668
|
+
}, null, _instanceExtraInitializers);
|
|
669
|
+
__esDecorate(this, null, _listConfigurableProviders_decorators, {
|
|
670
|
+
kind: "method",
|
|
671
|
+
name: "listConfigurableProviders",
|
|
672
|
+
static: false,
|
|
673
|
+
private: false,
|
|
674
|
+
access: {
|
|
675
|
+
has: (obj) => "listConfigurableProviders" in obj,
|
|
676
|
+
get: (obj) => obj.listConfigurableProviders
|
|
677
|
+
},
|
|
678
|
+
metadata: _metadata
|
|
679
|
+
}, null, _instanceExtraInitializers);
|
|
680
|
+
__esDecorate(this, null, _remoteDiscoverModels_decorators, {
|
|
681
|
+
kind: "method",
|
|
682
|
+
name: "remoteDiscoverModels",
|
|
683
|
+
static: false,
|
|
684
|
+
private: false,
|
|
685
|
+
access: {
|
|
686
|
+
has: (obj) => "remoteDiscoverModels" in obj,
|
|
687
|
+
get: (obj) => obj.remoteDiscoverModels
|
|
688
|
+
},
|
|
689
|
+
metadata: _metadata
|
|
690
|
+
}, null, _instanceExtraInitializers);
|
|
691
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, {
|
|
692
|
+
enumerable: true,
|
|
693
|
+
configurable: true,
|
|
694
|
+
writable: true,
|
|
695
|
+
value: _metadata
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
adapters = (__runInitializers(this, _instanceExtraInitializers), /* @__PURE__ */ new Map());
|
|
699
|
+
directory = /* @__PURE__ */ new Map();
|
|
700
|
+
discoveries = /* @__PURE__ */ new Map();
|
|
701
|
+
constructor(ctx) {
|
|
702
|
+
super(ctx, "llm");
|
|
703
|
+
}
|
|
704
|
+
/** Notify topology observers without letting one broken listener veto the commit. */
|
|
705
|
+
emitAdaptersUpdated() {
|
|
706
|
+
let invariantFailure;
|
|
707
|
+
for (const listener of this.ctx.events.dispatch("emit", ["llm/adapters-updated"])) try {
|
|
708
|
+
const returned = listener();
|
|
709
|
+
if (returned != null && typeof returned.then === "function") Promise.resolve(returned).then(void 0, (error) => {
|
|
710
|
+
this.warnAdaptersListenerFailure(error);
|
|
711
|
+
});
|
|
712
|
+
} catch (error) {
|
|
713
|
+
if (error?.code === "INVARIANT") {
|
|
714
|
+
invariantFailure ??= error;
|
|
715
|
+
continue;
|
|
716
|
+
}
|
|
717
|
+
this.warnAdaptersListenerFailure(error);
|
|
718
|
+
}
|
|
719
|
+
if (invariantFailure !== void 0) throw invariantFailure;
|
|
720
|
+
}
|
|
721
|
+
/** Contained-listener diagnostic shared by the sync and async failure paths. */
|
|
722
|
+
warnAdaptersListenerFailure(error) {
|
|
723
|
+
this.ctx.logger.warn("llm: an llm/adapters-updated listener failed");
|
|
724
|
+
this.ctx.logger.warn(error);
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* Register an adapter for the given provider routes. Throws `LlmError` with code
|
|
728
|
+
* `DUPLICATE_ADAPTER` if any provider already has an adapter (all-or-nothing).
|
|
729
|
+
* Disposed with the fiber.
|
|
730
|
+
* @param providers - every provider route this adapter should serve.
|
|
731
|
+
* @param adapter - the adapter that streams calls for those providers.
|
|
732
|
+
* @returns the disposer, carrying {@link AdapterRegistrationHandle.replace}.
|
|
733
|
+
*/
|
|
734
|
+
registerAdapter(providers, adapter) {
|
|
735
|
+
const owned = /* @__PURE__ */ new Set();
|
|
736
|
+
let released = false;
|
|
737
|
+
const dispose = this.ctx.effect(function* () {
|
|
738
|
+
if (providers.length === 0) throw new LlmError("an adapter must register at least one provider", "INVALID_ADAPTER");
|
|
739
|
+
this.commitRoutes(owned, this.prepareRoutes(providers, adapter, owned));
|
|
740
|
+
yield () => {
|
|
741
|
+
released = true;
|
|
742
|
+
for (const provider of owned) this.adapters.delete(provider);
|
|
743
|
+
owned.clear();
|
|
744
|
+
this.emitAdaptersUpdated();
|
|
745
|
+
};
|
|
746
|
+
}.bind(this), "llm.registerAdapter()");
|
|
747
|
+
const handle = (() => void dispose());
|
|
748
|
+
handle.replace = (next) => {
|
|
749
|
+
if (released) throw new LlmError("a disposed adapter registration cannot replace its routes", "REGISTRATION_DISPOSED");
|
|
750
|
+
this.commitRoutes(owned, this.prepareRoutes(next, adapter, owned));
|
|
751
|
+
};
|
|
752
|
+
return handle;
|
|
753
|
+
}
|
|
754
|
+
/**
|
|
755
|
+
* Validate one candidate route set for `adapter`, treating routes this
|
|
756
|
+
* registration already holds as available. Nothing is mutated: a rejected
|
|
757
|
+
* candidate leaves the registry exactly as it was.
|
|
758
|
+
*/
|
|
759
|
+
prepareRoutes(providers, adapter, owned) {
|
|
760
|
+
const unique = /* @__PURE__ */ new Set();
|
|
761
|
+
const registrations = [];
|
|
762
|
+
for (const provider of providers) {
|
|
763
|
+
if (provider.length === 0) throw new LlmError("adapter provider names must be non-empty", "INVALID_ADAPTER");
|
|
764
|
+
if (unique.has(provider) || this.adapters.has(provider) && !owned.has(provider)) throw new LlmError(`an adapter for provider "${provider}" is already registered`, "DUPLICATE_ADAPTER");
|
|
765
|
+
const info = adapter.providerInfo(provider);
|
|
766
|
+
if (typeof info.id !== "string" || info.id !== provider || typeof info.name !== "string" || info.name.length === 0) throw new LlmError(`adapter metadata for provider "${provider}" must preserve its id and have a non-empty name`, "INVALID_ADAPTER");
|
|
767
|
+
unique.add(provider);
|
|
768
|
+
const retryPolicy = adapter.providerRetryPolicy(provider) ?? resolveRetryPolicy(void 0, `llm: provider "${provider}" retryPolicy`);
|
|
769
|
+
registrations.push({
|
|
770
|
+
adapter,
|
|
771
|
+
provider: {
|
|
772
|
+
id: info.id,
|
|
773
|
+
name: info.name
|
|
774
|
+
},
|
|
775
|
+
retryPolicy
|
|
776
|
+
});
|
|
777
|
+
}
|
|
778
|
+
return registrations;
|
|
779
|
+
}
|
|
780
|
+
/**
|
|
781
|
+
* Swap this registration's routes for the prepared ones in one synchronous
|
|
782
|
+
* section, so no observer can see the registry between the release and the
|
|
783
|
+
* re-registration. The route set's one mutation point is also where
|
|
784
|
+
* `llm/adapters-updated` is published, so a `replace` announces itself
|
|
785
|
+
* exactly like a first registration.
|
|
786
|
+
*/
|
|
787
|
+
commitRoutes(owned, registrations) {
|
|
788
|
+
for (const provider of owned) this.adapters.delete(provider);
|
|
789
|
+
owned.clear();
|
|
790
|
+
for (const registration of registrations) {
|
|
791
|
+
this.adapters.set(registration.provider.id, registration);
|
|
792
|
+
owned.add(registration.provider.id);
|
|
793
|
+
}
|
|
794
|
+
this.emitAdaptersUpdated();
|
|
795
|
+
}
|
|
796
|
+
/**
|
|
797
|
+
* Describe provider routes with a registered adapter.
|
|
798
|
+
* @returns detached provider metadata in registration order.
|
|
799
|
+
*/
|
|
800
|
+
listProviders() {
|
|
801
|
+
return [...this.adapters.values()].map(({ provider }) => ({ ...provider }));
|
|
802
|
+
}
|
|
803
|
+
/**
|
|
804
|
+
* Declare provider routes an adapter plugin can activate through
|
|
805
|
+
* configuration. Registration is all-or-nothing: an empty list, invalid
|
|
806
|
+
* entry, or a provider already declared by any registration throws
|
|
807
|
+
* `LlmError` without registering the rest. Disposed with the fiber.
|
|
808
|
+
* @param entries - every configurable provider this plugin owns.
|
|
809
|
+
* @returns a handle that withdraws all of them, and can atomically replace them.
|
|
810
|
+
*/
|
|
811
|
+
registerConfigurableProviders(entries) {
|
|
812
|
+
let held = [];
|
|
813
|
+
let disposed = false;
|
|
814
|
+
/**
|
|
815
|
+
* Validate a candidate set in full against everything this registration
|
|
816
|
+
* does not already hold, then publish it. Nothing is written until the
|
|
817
|
+
* whole set passes, so a refused candidate leaves the current entries in
|
|
818
|
+
* place — the property that makes `replace` a swap rather than a
|
|
819
|
+
* delete-then-add that can strand the directory empty.
|
|
820
|
+
*/
|
|
821
|
+
const commit = (candidates) => {
|
|
822
|
+
const detached = [];
|
|
823
|
+
const own = new Set(held.map((entry) => entry.provider));
|
|
824
|
+
for (const entry of candidates) {
|
|
825
|
+
if (entry.provider.length === 0 || entry.displayName.length === 0 || entry.settingsNs.length === 0) throw new LlmError("configurable providers need a non-empty provider, displayName, and settingsNs", "INVALID_DIRECTORY");
|
|
826
|
+
if (entry.settingsPath.some((segment) => segment.length === 0)) throw new LlmError(`configurable provider "${entry.provider}" has an empty settingsPath segment`, "INVALID_DIRECTORY");
|
|
827
|
+
if (this.directory.has(entry.provider) && !own.has(entry.provider) || detached.some((seen) => seen.provider === entry.provider)) throw new LlmError(`configurable provider "${entry.provider}" is already declared`, "DUPLICATE_DIRECTORY");
|
|
828
|
+
detached.push({
|
|
829
|
+
...entry,
|
|
830
|
+
settingsPath: [...entry.settingsPath]
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
for (const entry of held) this.directory.delete(entry.provider);
|
|
834
|
+
for (const entry of detached) this.directory.set(entry.provider, entry);
|
|
835
|
+
held = detached;
|
|
836
|
+
this.emitAdaptersUpdated();
|
|
837
|
+
};
|
|
838
|
+
const dispose = this.ctx.effect(function* () {
|
|
839
|
+
if (entries.length === 0) throw new LlmError("a configurable-provider registration must declare at least one provider", "INVALID_DIRECTORY");
|
|
840
|
+
commit(entries);
|
|
841
|
+
yield () => {
|
|
842
|
+
disposed = true;
|
|
843
|
+
for (const entry of held) this.directory.delete(entry.provider);
|
|
844
|
+
held = [];
|
|
845
|
+
this.emitAdaptersUpdated();
|
|
846
|
+
};
|
|
847
|
+
}.bind(this), "llm.registerConfigurableProviders()");
|
|
848
|
+
const handle = (() => void dispose());
|
|
849
|
+
handle.replace = (next) => {
|
|
850
|
+
if (disposed) throw new LlmError("this configurable-provider registration was disposed", "REGISTRATION_DISPOSED");
|
|
851
|
+
commit(next);
|
|
852
|
+
};
|
|
853
|
+
return handle;
|
|
854
|
+
}
|
|
855
|
+
/**
|
|
856
|
+
* List every declared configurable provider, registered or dormant.
|
|
857
|
+
* @returns detached directory entries in declaration order.
|
|
858
|
+
*/
|
|
859
|
+
listConfigurableProviders() {
|
|
860
|
+
return [...this.directory.values()].map((entry) => ({
|
|
861
|
+
...entry,
|
|
862
|
+
settingsPath: [...entry.settingsPath]
|
|
863
|
+
}));
|
|
864
|
+
}
|
|
865
|
+
/**
|
|
866
|
+
* Offer to interrogate provider endpoints on behalf of the settings
|
|
867
|
+
* namespace this plugin owns. The namespace is the key because that is what
|
|
868
|
+
* a configuration surface already holds from the configurable-provider
|
|
869
|
+
* directory, and because a provider being *added* has no route to name yet.
|
|
870
|
+
* Disposed with the fiber.
|
|
871
|
+
* @param settingsNs - the namespace whose profiles this discovery serves.
|
|
872
|
+
* @param discover - interrogates one endpoint and must honor the supplied signal.
|
|
873
|
+
* @returns the disposer that withdraws the offer.
|
|
874
|
+
*/
|
|
875
|
+
registerModelDiscovery(settingsNs, discover) {
|
|
876
|
+
const dispose = this.ctx.effect(function* () {
|
|
877
|
+
if (settingsNs.length === 0) throw new LlmError("model discovery needs a non-empty settings namespace", "INVALID_DISCOVERY");
|
|
878
|
+
if (this.discoveries.has(settingsNs)) throw new LlmError(`model discovery for "${settingsNs}" is already registered`, "DUPLICATE_DISCOVERY");
|
|
879
|
+
this.discoveries.set(settingsNs, discover);
|
|
880
|
+
yield () => {
|
|
881
|
+
this.discoveries.delete(settingsNs);
|
|
882
|
+
};
|
|
883
|
+
}.bind(this), "llm.registerModelDiscovery()");
|
|
884
|
+
return () => void dispose();
|
|
885
|
+
}
|
|
886
|
+
/**
|
|
887
|
+
* Interrogate one provider endpoint for the models it advertises. The
|
|
888
|
+
* request describes a draft, not a stored route, so nothing here reads or
|
|
889
|
+
* writes settings or credentials — the caller owns both, and the reply is
|
|
890
|
+
* candidate metadata a surface may offer for adoption.
|
|
891
|
+
* @param settingsNs - namespace whose registered discovery serves this draft.
|
|
892
|
+
* @param request - the endpoint, protocol, and one-shot credential to use.
|
|
893
|
+
* @param signal - caller cancellation.
|
|
894
|
+
* @returns the advertised models, deduplicated in endpoint order.
|
|
895
|
+
*/
|
|
896
|
+
async discoverModels(settingsNs, request, signal) {
|
|
897
|
+
const discover = this.discoveries.get(settingsNs);
|
|
898
|
+
if (discover === void 0) throw new LlmError(`no model discovery is registered for "${settingsNs}"`, "NO_DISCOVERY");
|
|
899
|
+
if ((request.provider ?? "").length === 0 && (request.baseURL ?? "").length === 0) throw new LlmError("model discovery needs a provider route or a baseURL", "INVALID_DISCOVERY");
|
|
900
|
+
const discovered = signal === void 0 ? await discover(request) : await discover(request, signal);
|
|
901
|
+
const seen = /* @__PURE__ */ new Set();
|
|
902
|
+
const models = [];
|
|
903
|
+
for (const model of discovered) {
|
|
904
|
+
if (typeof model.id !== "string" || model.id.length === 0 || seen.has(model.id)) continue;
|
|
905
|
+
seen.add(model.id);
|
|
906
|
+
models.push({
|
|
907
|
+
id: model.id,
|
|
908
|
+
...model.name === void 0 ? {} : { name: model.name },
|
|
909
|
+
...model.contextWindow === void 0 ? {} : { contextWindow: model.contextWindow },
|
|
910
|
+
...model.maxTokens === void 0 ? {} : { maxTokens: model.maxTokens }
|
|
911
|
+
});
|
|
912
|
+
}
|
|
913
|
+
return models;
|
|
914
|
+
}
|
|
915
|
+
/**
|
|
916
|
+
* Remote adapter for one draft provider interrogation.
|
|
917
|
+
* @param settingsNs - namespace whose registered discovery serves this draft.
|
|
918
|
+
* @param request - endpoint, protocol, and one-shot credential to use.
|
|
919
|
+
* @param signal - caller cancellation supplied by the Remote carrier.
|
|
920
|
+
* @returns advertised models in endpoint order.
|
|
921
|
+
* @throws RemoteError with `llm/model-discovery-rejected` when discovery refuses or fails.
|
|
922
|
+
*/
|
|
923
|
+
async remoteDiscoverModels(settingsNs, request, signal) {
|
|
924
|
+
try {
|
|
925
|
+
return await this.discoverModels(settingsNs, request, signal);
|
|
926
|
+
} catch (error) {
|
|
927
|
+
throw new RemoteError("llm/model-discovery-rejected", error instanceof Error ? error.message : String(error), {
|
|
928
|
+
settingsNs,
|
|
929
|
+
...request.baseURL === void 0 ? {} : { baseURL: request.baseURL }
|
|
930
|
+
}, { cause: error });
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
/**
|
|
934
|
+
* Resolve the retry policy captured when one provider route was registered.
|
|
935
|
+
* @param provider - registered provider route to inspect.
|
|
936
|
+
* @returns the provider-owned policy, with normal defaults already resolved.
|
|
937
|
+
*/
|
|
938
|
+
providerRetryPolicy(provider) {
|
|
939
|
+
return this.registration(provider).retryPolicy;
|
|
940
|
+
}
|
|
941
|
+
/**
|
|
942
|
+
* Resolve provider-side request-image pricing for one exact route, or
|
|
943
|
+
* `undefined` when the provider is unregistered or declares none. Unknown
|
|
944
|
+
* providers degrade to `undefined` rather than throwing because callers
|
|
945
|
+
* price durable history whose route may no longer be mounted.
|
|
946
|
+
* @param provider - provider route named by a request header.
|
|
947
|
+
* @param model - exact model id named by the same header.
|
|
948
|
+
* @returns the owning adapter's image pricing for the route, when declared.
|
|
949
|
+
*/
|
|
950
|
+
imageRequestPricing(provider, model) {
|
|
951
|
+
return this.adapters.get(provider)?.adapter.imageRequestPricing(provider, model);
|
|
952
|
+
}
|
|
953
|
+
/** Detach typed adapter-owned modality metadata. */
|
|
954
|
+
detachedModalities(modalities) {
|
|
955
|
+
return modalities === void 0 ? void 0 : [...modalities];
|
|
956
|
+
}
|
|
957
|
+
/**
|
|
958
|
+
* Discover models advertised by one registered provider. Catalog membership
|
|
959
|
+
* is advisory and never changes routing or request validation.
|
|
960
|
+
* @param provider - registered provider route to inspect.
|
|
961
|
+
* @returns detached model metadata in adapter-preferred order.
|
|
962
|
+
*/
|
|
963
|
+
async listModels(provider) {
|
|
964
|
+
const models = await this.registration(provider).adapter.listModels(provider);
|
|
965
|
+
const seen = /* @__PURE__ */ new Set();
|
|
966
|
+
return models.map((model) => {
|
|
967
|
+
if (typeof model.provider !== "string" || model.provider !== provider || typeof model.id !== "string" || model.id.length === 0 || typeof model.name !== "string" || model.name.length === 0 || model.description !== void 0 && typeof model.description !== "string" || seen.has(model.id)) throw new LlmError(`adapter returned invalid or duplicate model metadata for provider "${provider}"`, "INVALID_CATALOG");
|
|
968
|
+
seen.add(model.id);
|
|
969
|
+
const inputModalities = this.detachedModalities(model.inputModalities);
|
|
970
|
+
return {
|
|
971
|
+
provider: model.provider,
|
|
972
|
+
id: model.id,
|
|
973
|
+
name: model.name,
|
|
974
|
+
...model.description === void 0 ? {} : { description: model.description },
|
|
975
|
+
...inputModalities === void 0 ? {} : { inputModalities }
|
|
976
|
+
};
|
|
977
|
+
});
|
|
978
|
+
}
|
|
979
|
+
/**
|
|
980
|
+
* Resolve and validate all metadata from the adapter that owns one exact
|
|
981
|
+
* route. The result is detached from adapter-owned objects; catalog
|
|
982
|
+
* membership remains advisory and does not control request routing.
|
|
983
|
+
* @param provider - registered provider route to inspect.
|
|
984
|
+
* @param model - exact model id passed to the adapter.
|
|
985
|
+
* @param signal - optional cancellation for adapter-owned asynchronous lookup.
|
|
986
|
+
* @returns exact model identity plus available context and reasoning metadata.
|
|
987
|
+
*/
|
|
988
|
+
async resolveModelInfo(provider, model, signal) {
|
|
989
|
+
return this.resolveModelInfoFor(this.registration(provider), model, signal);
|
|
990
|
+
}
|
|
991
|
+
async resolveModelInfoFor(registration, model, signal) {
|
|
992
|
+
const resolved = await registration.adapter.resolveModel(registration.provider.id, model, signal);
|
|
993
|
+
return this.normalizeModelInfo(registration, model, resolved);
|
|
994
|
+
}
|
|
995
|
+
/** Validate and detach one adapter-returned exact model result. */
|
|
996
|
+
normalizeModelInfo(registration, model, resolved) {
|
|
997
|
+
const provider = registration.provider.id;
|
|
998
|
+
if (typeof resolved.provider !== "string" || resolved.provider !== provider || typeof resolved.id !== "string" || resolved.id !== model || typeof resolved.name !== "string" || resolved.name.length === 0 || resolved.description !== void 0 && typeof resolved.description !== "string") throw new LlmError(`adapter returned invalid exact model metadata for provider "${provider}" model "${model}"`, "INVALID_MODEL_INFO");
|
|
999
|
+
const context = resolved.context;
|
|
1000
|
+
if (context !== void 0 && (!Number.isInteger(context.contextWindow) || context.contextWindow <= 0)) throw new LlmError(`adapter returned invalid context metadata for provider "${provider}" model "${model}"`, "INVALID_MODEL_CONTEXT");
|
|
1001
|
+
const inputModalities = this.detachedModalities(resolved.inputModalities);
|
|
1002
|
+
const defaultMaxTokens = resolved.defaultMaxTokens;
|
|
1003
|
+
if (defaultMaxTokens !== void 0 && (!Number.isSafeInteger(defaultMaxTokens) || defaultMaxTokens <= 0)) throw new LlmError(`adapter returned invalid default maxTokens for provider "${provider}" model "${model}"`, "INVALID_MODEL_MAX_TOKENS");
|
|
1004
|
+
const info = {
|
|
1005
|
+
provider,
|
|
1006
|
+
id: model,
|
|
1007
|
+
name: resolved.name,
|
|
1008
|
+
...resolved.description === void 0 ? {} : { description: resolved.description },
|
|
1009
|
+
...inputModalities === void 0 ? {} : { inputModalities },
|
|
1010
|
+
...context === void 0 ? {} : { context: { contextWindow: context.contextWindow } },
|
|
1011
|
+
...defaultMaxTokens === void 0 ? {} : { defaultMaxTokens }
|
|
1012
|
+
};
|
|
1013
|
+
const reasoning = resolved.reasoning;
|
|
1014
|
+
if (reasoning === void 0) return info;
|
|
1015
|
+
if (reasoning.efforts.length === 0) throw new LlmError(`adapter returned invalid reasoning metadata for provider "${provider}" model "${model}"`, "INVALID_MODEL_REASONING");
|
|
1016
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1017
|
+
const efforts = reasoning.efforts.map((effort) => {
|
|
1018
|
+
if (typeof effort.id !== "string" || effort.id.length === 0 || typeof effort.name !== "string" || effort.name.length === 0 || effort.description !== void 0 && typeof effort.description !== "string" || seen.has(effort.id)) throw new LlmError(`adapter returned invalid or duplicate reasoning effort metadata for provider "${provider}" model "${model}"`, "INVALID_MODEL_REASONING");
|
|
1019
|
+
seen.add(effort.id);
|
|
1020
|
+
return {
|
|
1021
|
+
id: effort.id,
|
|
1022
|
+
name: effort.name,
|
|
1023
|
+
...effort.description === void 0 ? {} : { description: effort.description }
|
|
1024
|
+
};
|
|
1025
|
+
});
|
|
1026
|
+
if (reasoning.defaultEffort !== void 0 && !seen.has(reasoning.defaultEffort)) throw new LlmError(`adapter returned an unknown default reasoning effort for provider "${provider}" model "${model}"`, "INVALID_MODEL_REASONING");
|
|
1027
|
+
return {
|
|
1028
|
+
...info,
|
|
1029
|
+
reasoning: {
|
|
1030
|
+
efforts,
|
|
1031
|
+
...reasoning.defaultEffort === void 0 ? {} : { defaultEffort: reasoning.defaultEffort }
|
|
1032
|
+
}
|
|
1033
|
+
};
|
|
1034
|
+
}
|
|
1035
|
+
/**
|
|
1036
|
+
* Validate a conversation call config against its exact model capability and
|
|
1037
|
+
* materialize adapter-configured defaults. Unsupported explicit efforts
|
|
1038
|
+
* reject before provider I/O; no clamping or aliasing is performed. This
|
|
1039
|
+
* standalone query does not bind a later dispatch; use {@link prepareCall}
|
|
1040
|
+
* when logging and streaming must share one adapter registration.
|
|
1041
|
+
* @param config - provider/model route and optional request controls.
|
|
1042
|
+
* @param signal - optional cancellation for adapter-owned capability lookup.
|
|
1043
|
+
* @returns a detached config only when a default must be materialized.
|
|
1044
|
+
*/
|
|
1045
|
+
async resolveCallConfig(config, signal) {
|
|
1046
|
+
return (await this.resolveCallFor(this.registration(config.provider), config, signal)).config;
|
|
1047
|
+
}
|
|
1048
|
+
async resolveCallFor(registration, config, signal) {
|
|
1049
|
+
const info = await this.resolveModelInfoFor(registration, config.model, signal);
|
|
1050
|
+
return this.resolveCallWithInfo(config, info);
|
|
1051
|
+
}
|
|
1052
|
+
/** Validate request controls against one already-bound exact model result. */
|
|
1053
|
+
resolveCallWithInfo(config, info) {
|
|
1054
|
+
const defaulted = config.maxTokens === void 0 && info.defaultMaxTokens !== void 0 ? {
|
|
1055
|
+
...config,
|
|
1056
|
+
maxTokens: info.defaultMaxTokens
|
|
1057
|
+
} : config;
|
|
1058
|
+
const reasoning = info.reasoning;
|
|
1059
|
+
const requested = defaulted.reasoningEffort;
|
|
1060
|
+
let resolvedConfig = defaulted;
|
|
1061
|
+
if (reasoning === void 0) {
|
|
1062
|
+
if (requested !== void 0) throw new LlmError(`provider "${config.provider}" model "${config.model}" does not support reasoning effort "${requested}"`, "UNSUPPORTED_REASONING_EFFORT");
|
|
1063
|
+
} else {
|
|
1064
|
+
const effective = requested ?? reasoning.defaultEffort;
|
|
1065
|
+
if (effective !== void 0) {
|
|
1066
|
+
if (!reasoning.efforts.some((effort) => effort.id === effective)) throw new LlmError(`provider "${config.provider}" model "${config.model}" does not support reasoning effort "${effective}"`, "UNSUPPORTED_REASONING_EFFORT");
|
|
1067
|
+
if (requested !== effective) resolvedConfig = {
|
|
1068
|
+
...defaulted,
|
|
1069
|
+
reasoningEffort: effective
|
|
1070
|
+
};
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
return {
|
|
1074
|
+
config: resolvedConfig,
|
|
1075
|
+
...info.context === void 0 ? {} : { context: info.context },
|
|
1076
|
+
modelInfo: info
|
|
1077
|
+
};
|
|
1078
|
+
}
|
|
1079
|
+
/**
|
|
1080
|
+
* Resolve one call under its current adapter registration. The returned
|
|
1081
|
+
* one-shot handle keeps that registration across header logging and dispatch,
|
|
1082
|
+
* so HMR cannot combine one adapter's capability result with another adapter.
|
|
1083
|
+
* @param config - provider/model route and optional request controls.
|
|
1084
|
+
* @param signal - optional cancellation for adapter-owned capability lookup.
|
|
1085
|
+
* @returns a prepared config and its registration-bound stream entry point.
|
|
1086
|
+
*/
|
|
1087
|
+
async prepareCall(config, signal) {
|
|
1088
|
+
const registration = this.registration(config.provider);
|
|
1089
|
+
const adapterCall = await registration.adapter.prepareCall(config.provider, config.model, signal);
|
|
1090
|
+
const modelInfo = this.normalizeModelInfo(registration, config.model, adapterCall.model);
|
|
1091
|
+
const resolved = this.resolveCallWithInfo(config, modelInfo);
|
|
1092
|
+
const resolvedConfig = deepFreeze(structuredClone(resolved.config));
|
|
1093
|
+
const context = resolved.context === void 0 ? void 0 : deepFreeze(structuredClone(resolved.context));
|
|
1094
|
+
const adapterDefaults = deepFreeze({
|
|
1095
|
+
...config.reasoningEffort === void 0 && resolvedConfig.reasoningEffort !== void 0 ? { reasoningEffort: true } : {},
|
|
1096
|
+
...config.maxTokens === void 0 && resolvedConfig.maxTokens !== void 0 ? { maxTokens: true } : {}
|
|
1097
|
+
});
|
|
1098
|
+
let dispatched = false;
|
|
1099
|
+
return Object.freeze({
|
|
1100
|
+
config: resolvedConfig,
|
|
1101
|
+
retryPolicy: registration.retryPolicy,
|
|
1102
|
+
adapterDefaults,
|
|
1103
|
+
...context === void 0 ? {} : { context },
|
|
1104
|
+
...modelInfo.inputModalities === void 0 ? {} : { inputModalities: Object.freeze([...modelInfo.inputModalities]) },
|
|
1105
|
+
stream: (options) => {
|
|
1106
|
+
if (dispatched) throw new LlmError("a prepared LLM call can only be dispatched once", "INVALID_PREPARED_CALL");
|
|
1107
|
+
if (!callConfigEquals(options, resolvedConfig)) throw new LlmError("prepared LLM call config changed before adapter dispatch", "INVALID_PREPARED_CALL");
|
|
1108
|
+
dispatched = true;
|
|
1109
|
+
return this.streamWithRegistration(options, {
|
|
1110
|
+
registration,
|
|
1111
|
+
config: resolvedConfig,
|
|
1112
|
+
modelInfo,
|
|
1113
|
+
dispatch: (options) => adapterCall.stream(options)
|
|
1114
|
+
});
|
|
1115
|
+
}
|
|
1116
|
+
});
|
|
1117
|
+
}
|
|
1118
|
+
registration(provider) {
|
|
1119
|
+
const registration = this.adapters.get(provider);
|
|
1120
|
+
if (!registration) throw new LlmError(`no adapter registered for provider "${provider}"`, "NO_ADAPTER");
|
|
1121
|
+
return registration;
|
|
1122
|
+
}
|
|
1123
|
+
/** Remove replay state whose historical route is owned by another adapter. */
|
|
1124
|
+
forAdapter(options, adapter) {
|
|
1125
|
+
const messages = options.messages.map((message) => {
|
|
1126
|
+
const source = message.source;
|
|
1127
|
+
if (message.role !== "assistant" || source.kind !== "model" || source.replayState === void 0) return message;
|
|
1128
|
+
if (this.adapters.get(source.provider)?.adapter === adapter) return message;
|
|
1129
|
+
return freezeMessage({
|
|
1130
|
+
...message,
|
|
1131
|
+
source: {
|
|
1132
|
+
kind: "model",
|
|
1133
|
+
provider: source.provider,
|
|
1134
|
+
model: source.model
|
|
1135
|
+
}
|
|
1136
|
+
});
|
|
1137
|
+
});
|
|
1138
|
+
if (messages.every((message, index) => message === options.messages[index])) return options;
|
|
1139
|
+
const filtered = {
|
|
1140
|
+
...options,
|
|
1141
|
+
messages
|
|
1142
|
+
};
|
|
1143
|
+
return Object.isFrozen(options) ? deepFreeze(filtered) : filtered;
|
|
1144
|
+
}
|
|
1145
|
+
/**
|
|
1146
|
+
* Final adapter boundary. Adapter selection, dispatch, iterator construction,
|
|
1147
|
+
* and iteration failures become one terminal failure chunk. Middleware and
|
|
1148
|
+
* downstream consumer failures remain thrown plugin or consumer errors.
|
|
1149
|
+
*/
|
|
1150
|
+
async *adapterStream(options, prepared) {
|
|
1151
|
+
let iterator;
|
|
1152
|
+
try {
|
|
1153
|
+
const registration = prepared?.registration ?? this.registration(options.provider);
|
|
1154
|
+
const adapter = registration.adapter;
|
|
1155
|
+
let modelInfo;
|
|
1156
|
+
let resolvedConfig;
|
|
1157
|
+
let dispatch;
|
|
1158
|
+
if (prepared === void 0) {
|
|
1159
|
+
const adapterCall = await adapter.prepareCall(options.provider, options.model, options.signal);
|
|
1160
|
+
modelInfo = this.normalizeModelInfo(registration, options.model, adapterCall.model);
|
|
1161
|
+
resolvedConfig = this.resolveCallWithInfo(options, modelInfo).config;
|
|
1162
|
+
dispatch = (options) => adapterCall.stream(options);
|
|
1163
|
+
} else {
|
|
1164
|
+
modelInfo = prepared.modelInfo;
|
|
1165
|
+
resolvedConfig = prepared.config;
|
|
1166
|
+
dispatch = prepared.dispatch;
|
|
1167
|
+
}
|
|
1168
|
+
if (prepared !== void 0 && !callConfigEquals(options, resolvedConfig)) throw new LlmError("prepared LLM call config changed before adapter dispatch", "INVALID_PREPARED_CALL");
|
|
1169
|
+
const resolvedOptions = callConfigEquals(options, resolvedConfig) ? options : Object.isFrozen(options) ? deepFreeze({
|
|
1170
|
+
...options,
|
|
1171
|
+
...resolvedConfig
|
|
1172
|
+
}) : {
|
|
1173
|
+
...options,
|
|
1174
|
+
...resolvedConfig
|
|
1175
|
+
};
|
|
1176
|
+
const projectedOptions = modelInfo.inputModalities !== void 0 && !modelInfo.inputModalities.includes("image") && resolvedOptions.messages.some((message) => contentHasImage(message.content)) ? Object.isFrozen(resolvedOptions) ? deepFreeze({
|
|
1177
|
+
...resolvedOptions,
|
|
1178
|
+
messages: projectImagesForTextModel(resolvedOptions.messages)
|
|
1179
|
+
}) : {
|
|
1180
|
+
...resolvedOptions,
|
|
1181
|
+
messages: projectImagesForTextModel(resolvedOptions.messages)
|
|
1182
|
+
} : resolvedOptions;
|
|
1183
|
+
iterator = dispatch(this.forAdapter(projectedOptions, adapter))[Symbol.asyncIterator]();
|
|
1184
|
+
} catch (error) {
|
|
1185
|
+
yield adapterFailureChunk(error, options.signal);
|
|
1186
|
+
return;
|
|
1187
|
+
}
|
|
1188
|
+
let completed = false;
|
|
1189
|
+
try {
|
|
1190
|
+
while (true) {
|
|
1191
|
+
let item;
|
|
1192
|
+
try {
|
|
1193
|
+
const next = await iterator.next();
|
|
1194
|
+
item = next.done ? { done: true } : {
|
|
1195
|
+
done: false,
|
|
1196
|
+
value: next.value
|
|
1197
|
+
};
|
|
1198
|
+
} catch (error) {
|
|
1199
|
+
completed = true;
|
|
1200
|
+
yield adapterFailureChunk(error, options.signal);
|
|
1201
|
+
return;
|
|
1202
|
+
}
|
|
1203
|
+
if (item.done) {
|
|
1204
|
+
completed = true;
|
|
1205
|
+
return;
|
|
1206
|
+
}
|
|
1207
|
+
yield item.value;
|
|
1208
|
+
}
|
|
1209
|
+
} finally {
|
|
1210
|
+
if (!completed) {
|
|
1211
|
+
const close = iterator.return?.bind(iterator);
|
|
1212
|
+
if (close) await close();
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
/**
|
|
1217
|
+
* Stream one model call as raw chunks (token-level deltas). Replay state is
|
|
1218
|
+
* retained only when the same adapter instance owns its historical provider
|
|
1219
|
+
* and the target provider. Final adapter selection remains fixed through
|
|
1220
|
+
* asynchronous exact-model resolution and dispatch. Adapter selection,
|
|
1221
|
+
* dispatch, and iteration failures become terminal `error` or `aborted`
|
|
1222
|
+
* finish chunks; middleware, nested-call, cleanup, and consumer failures
|
|
1223
|
+
* remain thrown.
|
|
1224
|
+
* @param options - the full request; `options.provider` selects the adapter.
|
|
1225
|
+
* @returns the chunk stream, possibly wrapped by `llm/stream` listeners.
|
|
1226
|
+
*/
|
|
1227
|
+
stream(options) {
|
|
1228
|
+
return this.streamWithRegistration(options);
|
|
1229
|
+
}
|
|
1230
|
+
streamWithRegistration(options, prepared) {
|
|
1231
|
+
return this.ctx.waterfall(this, "llm/stream", options, () => this.adapterStream(options, prepared));
|
|
1232
|
+
}
|
|
1233
|
+
};
|
|
1234
|
+
})();
|
|
1235
|
+
/** Convert one adapter throw into the stream protocol's terminal outcome. */
|
|
1236
|
+
function adapterFailureChunk(error, signal) {
|
|
1237
|
+
const failure = normalizeLlmFailure(error);
|
|
1238
|
+
return {
|
|
1239
|
+
type: "finish",
|
|
1240
|
+
reason: signal?.aborted || failure.code === "ABORTED" ? {
|
|
1241
|
+
kind: "aborted",
|
|
1242
|
+
failure
|
|
1243
|
+
} : {
|
|
1244
|
+
kind: "error",
|
|
1245
|
+
failure
|
|
1246
|
+
}
|
|
1247
|
+
};
|
|
1248
|
+
}
|
|
1249
|
+
//#endregion
|
|
161
1250
|
//#region lib/types/index.js
|
|
162
1251
|
/**
|
|
163
1252
|
* Advisory per-agent repeat-call detector. It enriches post-execute decisions
|