@plurnk/plurnk-providers 5.0.2 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.defaults +11 -81
- package/README.md +21 -22
- package/SPEC.md +109 -1190
- package/dist/LeadingReasoning.js +1 -1
- package/dist/LeadingReasoning.js.map +1 -1
- package/dist/Mock.d.ts +2 -2
- package/dist/Mock.d.ts.map +1 -1
- package/dist/Mock.js +2 -2
- package/dist/Mock.js.map +1 -1
- package/dist/{AiSdkProvider.d.ts → ModelProvider.d.ts} +12 -21
- package/dist/ModelProvider.d.ts.map +1 -0
- package/dist/{AiSdkProvider.js → ModelProvider.js} +140 -188
- package/dist/ModelProvider.js.map +1 -0
- package/dist/{AiSdkRequestBody.d.ts → ModelRequestBody.d.ts} +6 -10
- package/dist/ModelRequestBody.d.ts.map +1 -0
- package/dist/{AiSdkRequestBody.js → ModelRequestBody.js} +17 -63
- package/dist/ModelRequestBody.js.map +1 -0
- package/dist/Pool.js +24 -24
- package/dist/Pool.js.map +1 -1
- package/dist/ProviderRegistry.d.ts.map +1 -1
- package/dist/ProviderRegistry.js +15 -15
- package/dist/ProviderRegistry.js.map +1 -1
- package/dist/RequestCapture.d.ts +1 -0
- package/dist/RequestCapture.d.ts.map +1 -1
- package/dist/RequestCapture.js +9 -6
- package/dist/RequestCapture.js.map +1 -1
- package/dist/accounting.d.ts +1 -1
- package/dist/accounting.d.ts.map +1 -1
- package/dist/accounting.js +4 -4
- package/dist/accounting.js.map +1 -1
- package/dist/capacity.js +4 -4
- package/dist/capacity.js.map +1 -1
- package/dist/catalogProvider.d.ts +5 -8
- package/dist/catalogProvider.d.ts.map +1 -1
- package/dist/catalogProvider.js +216 -66
- package/dist/catalogProvider.js.map +1 -1
- package/dist/compatibleProvider.d.ts.map +1 -1
- package/dist/compatibleProvider.js +8 -8
- package/dist/compatibleProvider.js.map +1 -1
- package/dist/discover.d.ts +1 -1
- package/dist/discover.d.ts.map +1 -1
- package/dist/discover.js +1 -8
- package/dist/discover.js.map +1 -1
- package/dist/env.d.ts +2 -2
- package/dist/env.d.ts.map +1 -1
- package/dist/env.js +20 -59
- package/dist/env.js.map +1 -1
- package/dist/errors.d.ts +48 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +61 -4
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +5 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -7
- package/dist/index.js.map +1 -1
- package/dist/{sdkModels.d.ts → modelAdapters.d.ts} +7 -9
- package/dist/modelAdapters.d.ts.map +1 -0
- package/dist/{sdkModels.js → modelAdapters.js} +194 -87
- package/dist/modelAdapters.js.map +1 -0
- package/dist/native-tools.d.ts +1 -2
- package/dist/native-tools.d.ts.map +1 -1
- package/dist/native-tools.js +9 -11
- package/dist/native-tools.js.map +1 -1
- package/dist/ollama.d.ts.map +1 -1
- package/dist/ollama.js +26 -12
- package/dist/ollama.js.map +1 -1
- package/dist/openai.d.ts +3 -3
- package/dist/openai.d.ts.map +1 -1
- package/dist/openai.js +1 -1
- package/dist/pi-transport.d.ts +69 -0
- package/dist/pi-transport.d.ts.map +1 -0
- package/dist/pi-transport.js +697 -0
- package/dist/pi-transport.js.map +1 -0
- package/dist/providerError.js +1 -1
- package/dist/providerError.js.map +1 -1
- package/dist/retry-deadline.d.ts +3 -0
- package/dist/retry-deadline.d.ts.map +1 -0
- package/dist/retry-deadline.js +96 -0
- package/dist/retry-deadline.js.map +1 -0
- package/dist/stream-forensics.d.ts +51 -0
- package/dist/stream-forensics.d.ts.map +1 -0
- package/dist/stream-forensics.js +403 -0
- package/dist/stream-forensics.js.map +1 -0
- package/dist/types.d.ts +29 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/usage.js +2 -2
- package/dist/usage.js.map +1 -1
- package/docs/models.md +3 -3
- package/package.json +8 -21
- package/dist/AiSdkProvider.d.ts.map +0 -1
- package/dist/AiSdkProvider.js.map +0 -1
- package/dist/AiSdkRequestBody.d.ts.map +0 -1
- package/dist/AiSdkRequestBody.js.map +0 -1
- package/dist/RequestFields.d.ts +0 -24
- package/dist/RequestFields.d.ts.map +0 -1
- package/dist/RequestFields.js +0 -237
- package/dist/RequestFields.js.map +0 -1
- package/dist/aiSdkTransport.d.ts +0 -84
- package/dist/aiSdkTransport.d.ts.map +0 -1
- package/dist/aiSdkTransport.js +0 -651
- package/dist/aiSdkTransport.js.map +0 -1
- package/dist/model-options.d.ts +0 -3
- package/dist/model-options.d.ts.map +0 -1
- package/dist/model-options.js +0 -26
- package/dist/model-options.js.map +0 -1
- package/dist/sdkModels.d.ts.map +0 -1
- package/dist/sdkModels.js.map +0 -1
|
@@ -1,48 +1,26 @@
|
|
|
1
|
-
// PLURNK
|
|
2
|
-
// spine — signal merging,
|
|
3
|
-
//
|
|
1
|
+
// PLURNK provider over a pi-ai model descriptor or OpenAI-compatible endpoint. Implements
|
|
2
|
+
// the universal generate() spine — signal merging, capacity admission, retry/deadline
|
|
3
|
+
// orchestration, accounting settlement, and response assembly.
|
|
4
4
|
//
|
|
5
|
-
// Composition, not inheritance:
|
|
6
|
-
//
|
|
7
|
-
//
|
|
5
|
+
// Composition, not inheritance: a ProviderModel descriptor supplies the vendor
|
|
6
|
+
// protocol. The compatible URL path serves PLURNK extensions and local endpoint
|
|
7
|
+
// probes.
|
|
8
8
|
import { EFFORTS, MAX_TIMER_MS, isRecord, isSafeInteger } from "@plurnk/plurnk-contracts";
|
|
9
9
|
import { UnsupportedEffortError } from "./types.js";
|
|
10
|
-
import {
|
|
11
|
-
import { prepareRetries } from "ai/internal";
|
|
12
|
-
import { toProviderError, ProviderError, ProviderTimeoutError } from "./errors.js";
|
|
10
|
+
import { APICallError, toProviderError, ProviderError, ProviderTimeoutError } from "./errors.js";
|
|
13
11
|
import { assertPromptTokenMeasurement, estimatePromptTokens } from "./promptTokens.js";
|
|
14
12
|
import { emitWarningOnce } from "./warnings.js";
|
|
13
|
+
import { outboundFetch } from "@plurnk/plurnk-meta";
|
|
15
14
|
import { resolveProviderCost } from "./cost.js";
|
|
16
15
|
import { validateProviderRequestAccounting } from "./accounting.js";
|
|
17
16
|
import { validateProviderUsage } from "./usage.js";
|
|
18
17
|
import { assessRequestCapacity, effectiveInputCapacity, effectiveInputWall, effectiveOutputBudget, effectiveOutputFloor, effectiveReasoningBudget } from "./capacity.js";
|
|
19
|
-
import
|
|
20
|
-
import AiSdkRequestBody from "./AiSdkRequestBody.js";
|
|
18
|
+
import ModelRequestBody from "./ModelRequestBody.js";
|
|
21
19
|
import LeadingReasoning from "./LeadingReasoning.js";
|
|
22
20
|
import RequestCapture from "./RequestCapture.js";
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
// transport still wedged after the grace is force-rejected with the signal's own reason, so the existing
|
|
27
|
-
// operation/cancellation classification is unchanged. The grace is unwind slack, not a second deadline.
|
|
28
|
-
const OPERATION_DEADLINE_UNWIND_GRACE_MS = 1_000;
|
|
29
|
-
const raceAgainstDeadline = async (work, signal) => {
|
|
30
|
-
let timer;
|
|
31
|
-
const backstop = Promise.withResolvers();
|
|
32
|
-
const arm = () => { timer = setTimeout(() => backstop.reject(signal.reason), OPERATION_DEADLINE_UNWIND_GRACE_MS); };
|
|
33
|
-
if (signal.aborted)
|
|
34
|
-
arm();
|
|
35
|
-
else
|
|
36
|
-
signal.addEventListener("abort", arm, { once: true });
|
|
37
|
-
try {
|
|
38
|
-
return await Promise.race([work, backstop.promise]);
|
|
39
|
-
}
|
|
40
|
-
finally {
|
|
41
|
-
signal.removeEventListener("abort", arm);
|
|
42
|
-
if (timer !== undefined)
|
|
43
|
-
clearTimeout(timer);
|
|
44
|
-
}
|
|
45
|
-
};
|
|
21
|
+
import { executePiTransportStream } from "./pi-transport.js";
|
|
22
|
+
import { executeWithRetry, raceAgainstDeadline } from "./retry-deadline.js";
|
|
23
|
+
import { RETRYABLE_MALFORMED_FINISH_REASONS, readFailureEvidence } from "./stream-forensics.js";
|
|
46
24
|
class ProviderRequestObserverError extends Error {
|
|
47
25
|
constructor(cause) {
|
|
48
26
|
super("provider request accounting could not be durably settled", { cause });
|
|
@@ -74,40 +52,35 @@ const stripTrailingSpecial = (content, marker) => {
|
|
|
74
52
|
out = out.slice(0, -marker.length);
|
|
75
53
|
return out;
|
|
76
54
|
};
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
case "unsupported":
|
|
80
|
-
case "compatibility":
|
|
81
|
-
return `${warning.type} ${warning.feature}${warning.details === undefined ? "" : `: ${warning.details}`}`;
|
|
82
|
-
case "deprecated": return `deprecated ${warning.setting}: ${warning.message}`;
|
|
83
|
-
case "other": return warning.message;
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
// {§provider-request-controls} — a native route forwards a configured control only through its
|
|
87
|
-
// SDK's documented provider option; a control the SDK does not document is named once and not
|
|
55
|
+
// {§provider-request-authority} — a native route forwards a configured control only when its
|
|
56
|
+
// adapter documents it; a control the adapter does not document is named once and not
|
|
88
57
|
// sent, never dropped in silence (#988).
|
|
89
|
-
const
|
|
90
|
-
const options = {};
|
|
58
|
+
const resolveRequestControls = (source, controls, serviceTier, topLogprobs) => {
|
|
91
59
|
const unsupported = (knob) => emitWarningOnce(`${source}: ${knob} is not a provider option this route's SDK${controls === undefined ? "" : ` (${controls.namespace})`} documents, so it is not sent`, "PLURNK_REQUEST_CONTROL_UNSUPPORTED");
|
|
60
|
+
let resolvedServiceTier;
|
|
92
61
|
if (serviceTier !== undefined) {
|
|
93
62
|
if (controls?.serviceTier === true)
|
|
94
|
-
|
|
63
|
+
resolvedServiceTier = serviceTier;
|
|
95
64
|
else
|
|
96
65
|
unsupported("PLURNK_PROVIDERS_SERVICE_TIER");
|
|
97
66
|
}
|
|
67
|
+
let resolvedTopLogprobs;
|
|
98
68
|
if (topLogprobs !== null) {
|
|
99
69
|
if (controls?.logprobs === true)
|
|
100
|
-
|
|
70
|
+
resolvedTopLogprobs = topLogprobs;
|
|
101
71
|
else
|
|
102
72
|
unsupported("PLURNK_PROVIDERS_TOP_LOGPROBS");
|
|
103
73
|
}
|
|
104
|
-
return
|
|
74
|
+
return {
|
|
75
|
+
...(resolvedServiceTier === undefined ? {} : { serviceTier: resolvedServiceTier }),
|
|
76
|
+
...(resolvedTopLogprobs === undefined ? {} : { topLogprobs: resolvedTopLogprobs }),
|
|
77
|
+
};
|
|
105
78
|
};
|
|
106
|
-
export default class
|
|
79
|
+
export default class ModelProvider {
|
|
107
80
|
#inferenceAdmission;
|
|
108
81
|
#model;
|
|
109
82
|
#url;
|
|
110
|
-
#
|
|
83
|
+
#providerModel;
|
|
111
84
|
#fetchTimeoutMs;
|
|
112
85
|
#operationTimeoutMs;
|
|
113
86
|
#headers;
|
|
@@ -126,7 +99,7 @@ export default class AiSdkProvider {
|
|
|
126
99
|
#effort;
|
|
127
100
|
#supportedEfforts;
|
|
128
101
|
#adaptiveEffort;
|
|
129
|
-
#
|
|
102
|
+
#nativeAdaptiveThinking;
|
|
130
103
|
#samplingDefaults;
|
|
131
104
|
#repeatPenalty;
|
|
132
105
|
#dryMultiplier;
|
|
@@ -134,7 +107,8 @@ export default class AiSdkProvider {
|
|
|
134
107
|
#dryAllowedLength;
|
|
135
108
|
#repeatLastN;
|
|
136
109
|
#reasoningStyle;
|
|
137
|
-
#
|
|
110
|
+
#outputTokenField;
|
|
111
|
+
#reasoningRequestBody;
|
|
138
112
|
#reasoningResponseStyle;
|
|
139
113
|
#countPromptTokens;
|
|
140
114
|
#promptTokensUrl;
|
|
@@ -143,11 +117,9 @@ export default class AiSdkProvider {
|
|
|
143
117
|
#source;
|
|
144
118
|
#grammarStyle;
|
|
145
119
|
#cacheAffinity;
|
|
146
|
-
#
|
|
147
|
-
#reasoningResponseProviderOptions;
|
|
120
|
+
#systemCacheBreakpoint;
|
|
148
121
|
#serviceTier;
|
|
149
|
-
#
|
|
150
|
-
#streaming;
|
|
122
|
+
#resolvedControls;
|
|
151
123
|
#supportsSlotPinning;
|
|
152
124
|
#slotCount;
|
|
153
125
|
#retryAttempts;
|
|
@@ -157,7 +129,7 @@ export default class AiSdkProvider {
|
|
|
157
129
|
#servedModel;
|
|
158
130
|
#requiresOutputBudget;
|
|
159
131
|
attributions;
|
|
160
|
-
// Optional capability ({§
|
|
132
|
+
// Optional capability ({§model-fact-resolution}): exact tokenization served by the backend's
|
|
161
133
|
// own vocab. Assigned in the constructor ONLY when the config carries a
|
|
162
134
|
// tokenizeUrl (llama-server), so `provider.tokenize === undefined` remains
|
|
163
135
|
// the honest capability signal for every other backend.
|
|
@@ -167,10 +139,10 @@ export default class AiSdkProvider {
|
|
|
167
139
|
this.#inferenceAdmission = config.inferenceAdmission;
|
|
168
140
|
this.#model = config.model;
|
|
169
141
|
this.#url = config.url;
|
|
170
|
-
this.#
|
|
142
|
+
this.#providerModel = config.providerModel;
|
|
171
143
|
this.attributions = config.attributions;
|
|
172
|
-
if ((this.#url === undefined) === (this.#
|
|
173
|
-
throw new Error(`${config.source ?? "provider"}: configure exactly one
|
|
144
|
+
if ((this.#url === undefined) === (this.#providerModel === undefined)) {
|
|
145
|
+
throw new Error(`${config.source ?? "provider"}: configure exactly one model transport or OpenAI-compatible URL`);
|
|
174
146
|
}
|
|
175
147
|
for (const [name, value] of [
|
|
176
148
|
["fetchTimeoutMs", config.fetchTimeoutMs],
|
|
@@ -183,7 +155,7 @@ export default class AiSdkProvider {
|
|
|
183
155
|
this.#fetchTimeoutMs = config.fetchTimeoutMs;
|
|
184
156
|
this.#operationTimeoutMs = config.operationTimeoutMs;
|
|
185
157
|
this.#headers = config.headers ?? {};
|
|
186
|
-
this.#fetch = config.fetch ??
|
|
158
|
+
this.#fetch = config.fetch ?? outboundFetch;
|
|
187
159
|
this.#contextWindow = config.contextWindow ?? null;
|
|
188
160
|
this.#inputModalities = config.inputModalities ?? new Set();
|
|
189
161
|
this.#maxInputTokens = config.maxInputTokens ?? null;
|
|
@@ -197,7 +169,7 @@ export default class AiSdkProvider {
|
|
|
197
169
|
...new Set(config.supportedEfforts ?? EFFORTS),
|
|
198
170
|
]);
|
|
199
171
|
this.#adaptiveEffort = config.adaptiveEffort ?? "provider-default";
|
|
200
|
-
this.#
|
|
172
|
+
this.#nativeAdaptiveThinking = config.nativeAdaptiveThinking === true;
|
|
201
173
|
if (!this.#supportedEfforts.includes(this.#effort.mode)) {
|
|
202
174
|
throw new UnsupportedEffortError(config.source ?? "provider", this.#effort.mode, this.#supportedEfforts);
|
|
203
175
|
}
|
|
@@ -205,7 +177,7 @@ export default class AiSdkProvider {
|
|
|
205
177
|
// required tuning fields must fail at construction, not silently send
|
|
206
178
|
// undefined sampling on every grammar request.
|
|
207
179
|
if (config.temperature === undefined || config.repeatPenalty === undefined) {
|
|
208
|
-
throw new Error(`${config.source ?? "provider"}:
|
|
180
|
+
throw new Error(`${config.source ?? "provider"}: ModelProviderConfig requires temperature + repeatPenalty declared (PLURNK_PROVIDERS_TEMPERATURE / _REPEAT_PENALTY; null = provider default)`);
|
|
209
181
|
}
|
|
210
182
|
this.#samplingDefaults = Object.fromEntries(Object.entries({
|
|
211
183
|
temperature: config.temperature ?? undefined,
|
|
@@ -223,10 +195,11 @@ export default class AiSdkProvider {
|
|
|
223
195
|
this.#retryAttempts = config.retryAttempts;
|
|
224
196
|
this.#errorDetailLimit = config.errorDetailLimit;
|
|
225
197
|
this.#reasoningStyle = config.reasoningStyle ?? "none";
|
|
226
|
-
this.#
|
|
227
|
-
|
|
228
|
-
throw new TypeError("Request fields must use an SDK namespace for a native languageModel, and the body for a compatible URL");
|
|
198
|
+
if (this.#providerModel !== undefined && config.outputTokenField !== undefined) {
|
|
199
|
+
throw new TypeError(`${config.source ?? "provider"}: compatible request body projections require an OpenAI-compatible URL`);
|
|
229
200
|
}
|
|
201
|
+
this.#outputTokenField = config.outputTokenField ?? "max_tokens";
|
|
202
|
+
this.#reasoningRequestBody = config.reasoningRequestBody;
|
|
230
203
|
this.#reasoningResponseStyle = config.reasoningResponseStyle ?? "verbatim";
|
|
231
204
|
if (config.countPromptTokens !== undefined && config.promptTokensUrl !== undefined) {
|
|
232
205
|
throw new Error(`${config.source ?? "provider"}: configure countPromptTokens or promptTokensUrl, not both`);
|
|
@@ -242,47 +215,29 @@ export default class AiSdkProvider {
|
|
|
242
215
|
this.#source = config.source ?? "provider";
|
|
243
216
|
this.#grammarStyle = config.grammarStyle ?? "none";
|
|
244
217
|
this.#cacheAffinity = config.cacheAffinity;
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|| this.#cacheAffinity.target === "provider-option" && this.#cacheAffinity.provider === this.#requestFields.namespace)) {
|
|
249
|
-
throw new TypeError("cache affinity overlaps declared request controls");
|
|
250
|
-
}
|
|
251
|
-
this.#systemCacheProviderOptions = config.systemCacheProviderOptions;
|
|
252
|
-
this.#reasoningResponseProviderOptions = config.reasoningResponseProviderOptions;
|
|
253
|
-
if (this.#languageModel === undefined && this.#cacheAffinity?.target === "provider-option") {
|
|
254
|
-
throw new Error(`${this.#source}: native provider-option cache affinity requires an AI SDK model`);
|
|
255
|
-
}
|
|
256
|
-
if (this.#languageModel !== undefined && this.#cacheAffinity?.target === "body") {
|
|
257
|
-
throw new Error(`${this.#source}: body cache affinity requires an OpenAI-compatible URL`);
|
|
258
|
-
}
|
|
259
|
-
if (this.#languageModel === undefined && this.#systemCacheProviderOptions !== undefined) {
|
|
260
|
-
throw new Error(`${this.#source}: system cache provider options require an AI SDK model`);
|
|
261
|
-
}
|
|
262
|
-
if (this.#languageModel === undefined && this.#reasoningResponseProviderOptions !== undefined) {
|
|
263
|
-
throw new Error(`${this.#source}: reasoning response provider options require an AI SDK model`);
|
|
218
|
+
this.#systemCacheBreakpoint = config.systemCacheBreakpoint === true;
|
|
219
|
+
if (this.#providerModel === undefined && this.#systemCacheBreakpoint) {
|
|
220
|
+
throw new Error(`${this.#source}: system cache breakpoints require a model transport`);
|
|
264
221
|
}
|
|
265
|
-
if (this.#
|
|
266
|
-
throw new Error(`${this.#source}: additive reasoning projection requires
|
|
267
|
-
}
|
|
268
|
-
if (this.#cacheAffinity?.target === "provider-option"
|
|
269
|
-
&& Object.hasOwn(this.#reasoningResponseProviderOptions?.[this.#cacheAffinity.provider] ?? {}, this.#cacheAffinity.name)) {
|
|
270
|
-
throw new Error(`${this.#source}: reasoning response options conflict with cache-affinity option ${this.#cacheAffinity.provider}.${this.#cacheAffinity.name}`);
|
|
222
|
+
if (this.#providerModel === undefined && this.#additiveReasoningProvider !== undefined) {
|
|
223
|
+
throw new Error(`${this.#source}: additive reasoning projection requires a model transport`);
|
|
271
224
|
}
|
|
272
225
|
this.#serviceTier = config.serviceTier;
|
|
273
|
-
this.#streaming = config.streaming ?? true;
|
|
274
226
|
this.#apiKeyRejectedMessage = config.apiKeyRejectedMessage;
|
|
275
227
|
this.#eosText = config.eosText;
|
|
276
228
|
this.#hasApiKey = "Authorization" in this.#headers;
|
|
277
229
|
this.#supportsSlotPinning = config.supportsSlotPinning ?? false;
|
|
278
230
|
this.#slotCount = config.slotCount ?? null;
|
|
279
231
|
this.#topLogprobs = config.topLogprobs ?? null;
|
|
280
|
-
if (this.#
|
|
281
|
-
throw new Error(`${this.#source}: native request controls require
|
|
232
|
+
if (this.#providerModel === undefined && config.requestControls !== undefined) {
|
|
233
|
+
throw new Error(`${this.#source}: native request controls require a model transport`);
|
|
282
234
|
}
|
|
283
|
-
this.#
|
|
284
|
-
?
|
|
285
|
-
|
|
235
|
+
this.#resolvedControls = this.#providerModel === undefined
|
|
236
|
+
? {
|
|
237
|
+
...(this.#serviceTier === undefined ? {} : { serviceTier: this.#serviceTier }),
|
|
238
|
+
...(this.#topLogprobs === null ? {} : { topLogprobs: this.#topLogprobs }),
|
|
239
|
+
}
|
|
240
|
+
: resolveRequestControls(this.#source, config.requestControls, this.#serviceTier, this.#topLogprobs);
|
|
286
241
|
this.#rawBody = config.rawBody ?? false;
|
|
287
242
|
this.#servedModel = config.servedModel;
|
|
288
243
|
this.#requiresOutputBudget = config.requiresOutputBudget;
|
|
@@ -298,7 +253,7 @@ export default class AiSdkProvider {
|
|
|
298
253
|
throw new Error(`${this.#source}: ${name} must be a positive safe integer or null`);
|
|
299
254
|
}
|
|
300
255
|
}
|
|
301
|
-
// {§provider-
|
|
256
|
+
// {§provider-capacity-admission}: the floor is never above the output budget.
|
|
302
257
|
this.#outputFloor = effectiveOutputFloor({ configured: this.#outputFloor, outputBudget: this.#outputBudget });
|
|
303
258
|
if (this.#reasoningBudget !== null
|
|
304
259
|
&& this.#outputBudget !== null
|
|
@@ -308,7 +263,7 @@ export default class AiSdkProvider {
|
|
|
308
263
|
if (this.#effort.budget !== this.#reasoningBudget) {
|
|
309
264
|
throw new Error(`${this.#source}: effort and generation envelope disagree on reasoningBudget`);
|
|
310
265
|
}
|
|
311
|
-
this.#
|
|
266
|
+
this.#reasoningRequestBody?.(this.#effort.mode, this.#reasoningBudget);
|
|
312
267
|
if (this.#requiresOutputBudget === true && this.#outputBudget === null) {
|
|
313
268
|
throw new Error(`${this.#source}: this backend requires a resolved PLURNK_PROVIDERS_OUTPUT_BUDGET`);
|
|
314
269
|
}
|
|
@@ -333,7 +288,7 @@ export default class AiSdkProvider {
|
|
|
333
288
|
return tokens;
|
|
334
289
|
};
|
|
335
290
|
}
|
|
336
|
-
this.#requestBody = new
|
|
291
|
+
this.#requestBody = new ModelRequestBody({ reasoningBudget: this.#reasoningBudget, additiveReasoningProvider: this.#additiveReasoningProvider, effort: this.#effort, nativeAdaptiveThinking: this.#nativeAdaptiveThinking, repeatPenalty: this.#repeatPenalty, dryMultiplier: this.#dryMultiplier, dryBase: this.#dryBase, dryAllowedLength: this.#dryAllowedLength, repeatLastN: this.#repeatLastN, reasoningStyle: this.#reasoningStyle, source: this.#source, grammarStyle: this.#grammarStyle, supportsSlotPinning: this.#supportsSlotPinning, slotCount: this.#slotCount });
|
|
337
292
|
}
|
|
338
293
|
get contextWindow() { return this.#contextWindow; }
|
|
339
294
|
get inputModalities() { return this.#inputModalities; }
|
|
@@ -350,7 +305,7 @@ export default class AiSdkProvider {
|
|
|
350
305
|
outputBudget: this.#outputBudget,
|
|
351
306
|
});
|
|
352
307
|
}
|
|
353
|
-
// {§provider-
|
|
308
|
+
// {§provider-capacity-admission}: the physical input wall, the window less the floor.
|
|
354
309
|
get inputWall() {
|
|
355
310
|
return effectiveInputWall({
|
|
356
311
|
contextWindow: this.#contextWindow,
|
|
@@ -450,17 +405,17 @@ export default class AiSdkProvider {
|
|
|
450
405
|
cost: resolveProviderCost(direct, this.#estimateCost(knownUsage)),
|
|
451
406
|
});
|
|
452
407
|
}
|
|
453
|
-
async generate({ messages,
|
|
454
|
-
// {§provider-interface} The
|
|
455
|
-
if (
|
|
456
|
-
throw new Error("generate:
|
|
408
|
+
async generate({ messages, sessionId, signal, grammar, maxOutputTokens, sampling, observeRequest, observeReasoning, callKind }) {
|
|
409
|
+
// {§provider-interface} The session identity is required.
|
|
410
|
+
if (sessionId === undefined || sessionId.length === 0)
|
|
411
|
+
throw new Error("generate: sessionId is required — the session's stable, opaque identity");
|
|
457
412
|
if (callKind !== undefined && callKind !== "emission" && callKind !== "bare") {
|
|
458
413
|
throw new Error(`generate: unsupported callKind ${JSON.stringify(callKind)}`);
|
|
459
414
|
}
|
|
460
415
|
// Reject before any wire call when already aborted
|
|
461
416
|
// ({§provider-failure-normalization}).
|
|
462
417
|
signal?.throwIfAborted();
|
|
463
|
-
// Grammar transport ({§provider-
|
|
418
|
+
// Grammar transport ({§provider-request-authority}): an operator's grammar rides a
|
|
464
419
|
// llama-style route verbatim and is never sent on any other style.
|
|
465
420
|
const wantGrammar = grammar !== undefined && this.#grammarStyle !== "none";
|
|
466
421
|
const sendGrammar = wantGrammar ? grammar : undefined;
|
|
@@ -473,38 +428,41 @@ export default class AiSdkProvider {
|
|
|
473
428
|
}
|
|
474
429
|
throw new ProviderError(this.#source, "capacity_exceeded", `The exact provider request uses ${capacity.prompt.tokens} input tokens, exceeding its ${capacity.inputWall} token input wall.`, { capacity, extensions: { capacityStage: "preflight", capacity } });
|
|
475
430
|
}
|
|
476
|
-
// {§provider-
|
|
431
|
+
// {§provider-capacity-admission}: the wire grants the flexed allowance —
|
|
477
432
|
// the window's remainder, between the floor and the model's limit.
|
|
478
433
|
const effectiveMaxOutputTokens = capacity.responseMax ?? capacity.outputBudget ?? undefined;
|
|
479
434
|
const nativeReasoningBudget = this.#requestBody.nativeReasoningBudget(capacity.outputBudget, capacity.reasoningBudget);
|
|
480
435
|
// Both transports project the same resolved sampling; managed fields still win.
|
|
481
436
|
const requestSampling = {
|
|
482
437
|
...this.#samplingDefaults,
|
|
483
|
-
...this.#requestBody.samplingBody(sampling
|
|
438
|
+
...this.#requestBody.samplingBody(sampling),
|
|
484
439
|
};
|
|
485
440
|
const body = {
|
|
486
441
|
...this.#requestBody.repetitionPenaltyBody(),
|
|
487
442
|
...requestSampling,
|
|
488
443
|
...(this.#serviceTier !== undefined ? { service_tier: this.#serviceTier } : {}),
|
|
489
|
-
...(this.#
|
|
490
|
-
?
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
}
|
|
494
|
-
: this.#requestFields.body(this.#effort.mode, effectiveMaxOutputTokens ?? null, capacity.reasoningBudget)),
|
|
444
|
+
...(this.#reasoningRequestBody !== undefined
|
|
445
|
+
? this.#reasoningRequestBody(this.#effort.mode, capacity.reasoningBudget)
|
|
446
|
+
: this.#requestBody.reasoningBody(preserveGrammarSentence, capacity.reasoningBudget)),
|
|
447
|
+
...(effectiveMaxOutputTokens === undefined ? {} : { [this.#outputTokenField]: effectiveMaxOutputTokens }),
|
|
495
448
|
...this.#requestBody.grammarBody(sendGrammar),
|
|
496
449
|
// Request per-token logprobs only when enabled (managed field —
|
|
497
450
|
// reserved from caller sampling; the env flag is the single control).
|
|
498
451
|
...(this.#topLogprobs !== null ? { logprobs: true, top_logprobs: this.#topLogprobs } : {}),
|
|
499
|
-
...this.#requestBody.slotBody(
|
|
452
|
+
...this.#requestBody.slotBody(sessionId),
|
|
500
453
|
...(this.#cacheAffinity?.target === "body"
|
|
501
|
-
? { [this.#cacheAffinity.name]:
|
|
454
|
+
? { [this.#cacheAffinity.name]: sessionId }
|
|
502
455
|
: {}),
|
|
503
456
|
};
|
|
457
|
+
for (const field of ["tools", "tool_choice", "functions", "function_call", "parallel_tool_calls"]) {
|
|
458
|
+
if (field in body) {
|
|
459
|
+
throw new TypeError("PLURNK provider requests cannot declare native tools");
|
|
460
|
+
}
|
|
461
|
+
}
|
|
504
462
|
// Per-request headers = static auth/routing only.
|
|
505
463
|
const headers = new Headers(this.#headers);
|
|
506
464
|
if (this.#cacheAffinity?.target === "header") {
|
|
507
|
-
headers.set(this.#cacheAffinity.name,
|
|
465
|
+
headers.set(this.#cacheAffinity.name, sessionId);
|
|
508
466
|
}
|
|
509
467
|
const requestHeaders = Object.fromEntries(headers.entries());
|
|
510
468
|
const accounting = [];
|
|
@@ -530,9 +488,10 @@ export default class AiSdkProvider {
|
|
|
530
488
|
};
|
|
531
489
|
let successfulReasoningStream = "";
|
|
532
490
|
let recoveredAfterOutput = false;
|
|
491
|
+
let retryAttempt = 0;
|
|
492
|
+
let reasoningStepDown = 0;
|
|
533
493
|
const executeAdmittedRequest = async () => {
|
|
534
494
|
const capture = this.#rawBody ? new RequestCapture() : undefined;
|
|
535
|
-
const fetch = capture?.fetch(this.#fetch ?? ((input, init) => globalThis.fetch(input, init))) ?? this.#fetch;
|
|
536
495
|
let requestReasoningStream = "";
|
|
537
496
|
let structuredReasoning = false;
|
|
538
497
|
const envelopes = preserveGrammarSentence ? LeadingReasoning.TEMPLATE
|
|
@@ -607,58 +566,39 @@ export default class AiSdkProvider {
|
|
|
607
566
|
};
|
|
608
567
|
let response;
|
|
609
568
|
try {
|
|
610
|
-
response =
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
topP: typeof requestSampling.top_p === "number" ? requestSampling.top_p : undefined,
|
|
639
|
-
topK: typeof requestSampling.top_k === "number" ? requestSampling.top_k : undefined,
|
|
640
|
-
presencePenalty: typeof requestSampling.presence_penalty === "number" ? requestSampling.presence_penalty : undefined,
|
|
641
|
-
frequencyPenalty: typeof requestSampling.frequency_penalty === "number" ? requestSampling.frequency_penalty : undefined,
|
|
642
|
-
stopSequences: typeof requestSampling.stop === "string"
|
|
643
|
-
? [requestSampling.stop]
|
|
644
|
-
: Array.isArray(requestSampling.stop) && requestSampling.stop.every((value) => typeof value === "string")
|
|
645
|
-
? requestSampling.stop
|
|
646
|
-
: undefined,
|
|
647
|
-
seed: typeof requestSampling.seed === "number" ? requestSampling.seed : undefined,
|
|
648
|
-
maxOutputTokens: this.#requestBody.nativeMaxOutputTokens(capacity.outputBudget, nativeReasoningBudget),
|
|
649
|
-
reasoning: this.#requestFields?.namespace !== undefined
|
|
650
|
-
? "provider-default"
|
|
651
|
-
: this.#effort.mode === "off"
|
|
652
|
-
? "none"
|
|
653
|
-
: this.#effort.mode === "adaptive"
|
|
654
|
-
? this.#adaptiveEffort
|
|
655
|
-
: nativeFixedEffort(this.#effort.mode),
|
|
656
|
-
});
|
|
569
|
+
response = await executePiTransportStream({
|
|
570
|
+
model: this.#model,
|
|
571
|
+
source: this.#source,
|
|
572
|
+
url: this.#url,
|
|
573
|
+
providerModel: this.#providerModel,
|
|
574
|
+
fetch: this.#fetch,
|
|
575
|
+
requestHeaders,
|
|
576
|
+
messages,
|
|
577
|
+
sessionId,
|
|
578
|
+
operationSignal,
|
|
579
|
+
requestSampling,
|
|
580
|
+
body,
|
|
581
|
+
effectiveMaxOutputTokens,
|
|
582
|
+
nativeReasoningBudget,
|
|
583
|
+
capacity,
|
|
584
|
+
requestBody: this.#requestBody,
|
|
585
|
+
effort: this.#effort,
|
|
586
|
+
adaptiveEffort: this.#adaptiveEffort,
|
|
587
|
+
nativeAdaptiveThinking: this.#nativeAdaptiveThinking,
|
|
588
|
+
reasoningRequestBody: this.#reasoningRequestBody,
|
|
589
|
+
systemCacheBreakpoint: this.#systemCacheBreakpoint,
|
|
590
|
+
cacheAffinity: this.#cacheAffinity,
|
|
591
|
+
resolvedControls: this.#resolvedControls,
|
|
592
|
+
rawBody: this.#rawBody,
|
|
593
|
+
capture,
|
|
594
|
+
reasoningStepDown,
|
|
595
|
+
observers,
|
|
596
|
+
});
|
|
657
597
|
}
|
|
658
598
|
catch (error) {
|
|
659
|
-
|
|
599
|
+
const failure = readFailureEvidence(error);
|
|
600
|
+
if (failure.outputObserved)
|
|
660
601
|
recoveredAfterOutput = true;
|
|
661
|
-
const failure = transportFailureEvidence(error);
|
|
662
602
|
await settleAccounting("error", failure.usage, failure.chargeEvidence, { ...failure, ...(capture?.request === undefined ? {} : { request: capture.request }) }, failure.status);
|
|
663
603
|
throw error;
|
|
664
604
|
}
|
|
@@ -666,6 +606,31 @@ export default class AiSdkProvider {
|
|
|
666
606
|
observeRequestReasoning(liveProjection.finish());
|
|
667
607
|
successfulReasoningStream = requestReasoningStream;
|
|
668
608
|
await settleAccounting("response", response.usage, response.chargeEvidence, { ...response, ...(capture?.request === undefined ? {} : { request: capture.request }) });
|
|
609
|
+
const malformedAbort = response.finishReason === "error"
|
|
610
|
+
&& response.rawFinishReason !== undefined
|
|
611
|
+
&& RETRYABLE_MALFORMED_FINISH_REASONS.has(response.rawFinishReason);
|
|
612
|
+
const reasoningStarvedVisibleOutput = response.finishReason === "length"
|
|
613
|
+
&& response.content.trim().length === 0
|
|
614
|
+
&& response.reasoning.length > 0;
|
|
615
|
+
if ((malformedAbort || reasoningStarvedVisibleOutput)
|
|
616
|
+
&& retryAttempt < this.#retryAttempts
|
|
617
|
+
&& !operationSignal?.aborted) {
|
|
618
|
+
retryAttempt += 1;
|
|
619
|
+
if (reasoningStarvedVisibleOutput) {
|
|
620
|
+
reasoningStepDown += 1;
|
|
621
|
+
}
|
|
622
|
+
if (response.content.length > 0) {
|
|
623
|
+
recoveredAfterOutput = true;
|
|
624
|
+
}
|
|
625
|
+
throw new APICallError({
|
|
626
|
+
message: `Transient upstream finish reason (${response.rawFinishReason ?? response.finishReason})`,
|
|
627
|
+
url: this.#url ?? this.#providerModel?.baseUrl ?? "",
|
|
628
|
+
requestBodyValues: body,
|
|
629
|
+
statusCode: 503,
|
|
630
|
+
responseHeaders: { "retry-after-ms": "50" },
|
|
631
|
+
isRetryable: true,
|
|
632
|
+
});
|
|
633
|
+
}
|
|
669
634
|
return response;
|
|
670
635
|
};
|
|
671
636
|
const executeRequest = async () => {
|
|
@@ -680,17 +645,13 @@ export default class AiSdkProvider {
|
|
|
680
645
|
};
|
|
681
646
|
let raw;
|
|
682
647
|
try {
|
|
683
|
-
|
|
684
|
-
maxRetries: this.#retryAttempts,
|
|
685
|
-
abortSignal: operationSignal,
|
|
686
|
-
});
|
|
687
|
-
// {§provider-connectivity} — the operation deadline and caller cancellation are backstopped by
|
|
648
|
+
// {§provider-failure-normalization} — the operation deadline and caller cancellation are backstopped by
|
|
688
649
|
// racing, not only by the advisory operationSignal, so a transport that ignores the abort still
|
|
689
650
|
// surfaces the deadline (→ the operationTimeout/caller branches below) rather than hanging the
|
|
690
651
|
// loop (#505). A well-behaved transport settles first and wins the race with its own evidence.
|
|
691
652
|
raw = operationSignal === undefined
|
|
692
|
-
? await
|
|
693
|
-
: await raceAgainstDeadline(
|
|
653
|
+
? await executeWithRetry(executeRequest, this.#retryAttempts, operationSignal)
|
|
654
|
+
: await raceAgainstDeadline(executeWithRetry(executeRequest, this.#retryAttempts, operationSignal), operationSignal);
|
|
694
655
|
}
|
|
695
656
|
catch (err) {
|
|
696
657
|
if (err instanceof ProviderRequestObserverError
|
|
@@ -800,15 +761,6 @@ export default class AiSdkProvider {
|
|
|
800
761
|
position: null,
|
|
801
762
|
});
|
|
802
763
|
}
|
|
803
|
-
for (const warning of raw.warnings) {
|
|
804
|
-
(notices ??= []).push({
|
|
805
|
-
source: this.#source,
|
|
806
|
-
kind: "provider_warning",
|
|
807
|
-
level: "warn",
|
|
808
|
-
message: providerWarningMessage(warning),
|
|
809
|
-
position: null,
|
|
810
|
-
});
|
|
811
|
-
}
|
|
812
764
|
const usage = raw.usage;
|
|
813
765
|
if (sendGrammar !== undefined
|
|
814
766
|
&& this.tokenize !== undefined
|
|
@@ -857,7 +809,7 @@ export default class AiSdkProvider {
|
|
|
857
809
|
...(meta !== undefined ? { meta } : {}),
|
|
858
810
|
...(notices !== undefined ? { notices } : {}),
|
|
859
811
|
};
|
|
860
|
-
// {§provider-
|
|
812
|
+
// {§provider-capacity-admission} (#482): conformance judges the GRANT the
|
|
861
813
|
// wire actually sent, not the configured reservation — output between
|
|
862
814
|
// the two is overflow tolerance working, never a provider fault.
|
|
863
815
|
const grantedOutput = capacity.responseMax ?? capacity.outputBudget;
|
|
@@ -879,7 +831,7 @@ export default class AiSdkProvider {
|
|
|
879
831
|
},
|
|
880
832
|
});
|
|
881
833
|
}
|
|
882
|
-
// {§provider-
|
|
834
|
+
// {§provider-failure-normalization} The transport classified the finish as an error: the attempt failed with
|
|
883
835
|
// its evidence, and nothing here is a completed exchange or a transport outage.
|
|
884
836
|
if (raw.finishReason === "error") {
|
|
885
837
|
const attempt = {
|
|
@@ -919,4 +871,4 @@ export default class AiSdkProvider {
|
|
|
919
871
|
};
|
|
920
872
|
}
|
|
921
873
|
}
|
|
922
|
-
//# sourceMappingURL=
|
|
874
|
+
//# sourceMappingURL=ModelProvider.js.map
|