@plurnk/plurnk-providers 5.0.2 → 6.0.1
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 +18 -84
- package/README.md +21 -22
- package/SPEC.md +110 -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 +218 -68
- package/dist/catalogProvider.js.map +1 -1
- package/dist/compatibleProvider.d.ts.map +1 -1
- package/dist/compatibleProvider.js +19 -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 +4 -4
- package/dist/env.d.ts.map +1 -1
- package/dist/env.js +43 -80
- 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 +466 -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
package/dist/aiSdkTransport.js
DELETED
|
@@ -1,651 +0,0 @@
|
|
|
1
|
-
import { isRecord, isSafeInteger } from "@plurnk/plurnk-contracts";
|
|
2
|
-
import { chatMessageText } from "./types.js";
|
|
3
|
-
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
4
|
-
import { APICallError, generateText, streamText, wrapLanguageModel } from "ai";
|
|
5
|
-
import { z } from "zod";
|
|
6
|
-
import { normalizeUsage, UsageDetailError } from "./usage.js";
|
|
7
|
-
import { withoutNativeTools } from "./native-tools.js";
|
|
8
|
-
const errorSchema = z.object({
|
|
9
|
-
error: z.object({
|
|
10
|
-
message: z.string(),
|
|
11
|
-
type: z.string().nullish(),
|
|
12
|
-
param: z.unknown().nullish(),
|
|
13
|
-
code: z.union([z.string(), z.number()]).nullish(),
|
|
14
|
-
}).loose(),
|
|
15
|
-
}).loose();
|
|
16
|
-
const retryDirective = (status, headers) => {
|
|
17
|
-
const raw = headers instanceof Headers
|
|
18
|
-
? headers.get("x-should-retry")
|
|
19
|
-
: Object.entries(headers).find(([name]) => name.toLowerCase() === "x-should-retry")?.[1];
|
|
20
|
-
const directive = raw?.trim().toLowerCase();
|
|
21
|
-
if (directive === "false")
|
|
22
|
-
return false;
|
|
23
|
-
if (directive === "true")
|
|
24
|
-
return true;
|
|
25
|
-
if (status !== undefined && status >= 520 && status <= 527)
|
|
26
|
-
return false;
|
|
27
|
-
return null;
|
|
28
|
-
};
|
|
29
|
-
// {§provider-connectivity} (#479): a Retry-After header on any status is the
|
|
30
|
-
// provider directing a wait (RFC 9110 defines it on 503 exactly for this);
|
|
31
|
-
// its presence, like a bare 429, earns the bounded transport retry.
|
|
32
|
-
const retryAfterPresent = (headers) => {
|
|
33
|
-
const raw = headers instanceof Headers
|
|
34
|
-
? headers.get("retry-after")
|
|
35
|
-
: Object.entries(headers).find(([name]) => name.toLowerCase() === "retry-after")?.[1];
|
|
36
|
-
return raw !== undefined && raw !== null && raw.trim() !== "";
|
|
37
|
-
};
|
|
38
|
-
const errorStructure = {
|
|
39
|
-
errorSchema,
|
|
40
|
-
errorToMessage: ({ error }) => error.message,
|
|
41
|
-
isRetryable(response) {
|
|
42
|
-
// {§provider-connectivity} (#479): only a provider-directed wait — 429,
|
|
43
|
-
// a Retry-After, or an explicit X-Should-Retry — earns a transport retry.
|
|
44
|
-
return retryDirective(response.status, response.headers)
|
|
45
|
-
?? (response.status === 429 || retryAfterPresent(response.headers));
|
|
46
|
-
},
|
|
47
|
-
};
|
|
48
|
-
const baseUrl = (completionUrl) => {
|
|
49
|
-
const url = new URL(completionUrl);
|
|
50
|
-
if (!url.pathname.endsWith("/chat/completions")) {
|
|
51
|
-
throw new Error(`OpenAI-compatible URL must end in /chat/completions: ${completionUrl}`);
|
|
52
|
-
}
|
|
53
|
-
url.pathname = url.pathname.slice(0, -"/chat/completions".length);
|
|
54
|
-
return url.toString().replace(/\/$/, "");
|
|
55
|
-
};
|
|
56
|
-
const usageOf = (usage) => normalizeUsage({
|
|
57
|
-
prompt_tokens: usage.inputTokens,
|
|
58
|
-
completion_tokens: usage.outputTokens,
|
|
59
|
-
total_tokens: usage.totalTokens,
|
|
60
|
-
prompt_cache_miss_tokens: usage.inputTokenDetails.noCacheTokens,
|
|
61
|
-
prompt_tokens_details: {
|
|
62
|
-
cached_tokens: usage.inputTokenDetails.cacheReadTokens,
|
|
63
|
-
cache_write_tokens: usage.inputTokenDetails.cacheWriteTokens,
|
|
64
|
-
},
|
|
65
|
-
completion_tokens_details: usage.outputTokenDetails.reasoningTokens !== undefined
|
|
66
|
-
? { reasoning_tokens: usage.outputTokenDetails.reasoningTokens }
|
|
67
|
-
: undefined,
|
|
68
|
-
});
|
|
69
|
-
const wireUsageOf = (values, sdkUsage) => {
|
|
70
|
-
for (let index = values.length - 1; index >= 0; index -= 1) {
|
|
71
|
-
const usage = recordOf(values[index])?.usage;
|
|
72
|
-
const raw = recordOf(usage);
|
|
73
|
-
if (raw !== null && ("prompt_tokens" in raw || "completion_tokens" in raw || "total_tokens" in raw)) {
|
|
74
|
-
return normalizeUsage(raw, sdkUsage === undefined ? undefined : {
|
|
75
|
-
inputTokens: sdkUsage.inputTokens,
|
|
76
|
-
inputTokenDetails: sdkUsage.inputTokenDetails,
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return undefined;
|
|
81
|
-
};
|
|
82
|
-
const settledUsage = (values, sdkUsage) => {
|
|
83
|
-
try {
|
|
84
|
-
const usage = wireUsageOf(values, sdkUsage) ?? (sdkUsage === undefined ? undefined : usageOf(sdkUsage));
|
|
85
|
-
return usage === undefined ? {} : { usage };
|
|
86
|
-
}
|
|
87
|
-
catch (cause) {
|
|
88
|
-
if (!(cause instanceof TypeError))
|
|
89
|
-
throw cause;
|
|
90
|
-
return {
|
|
91
|
-
...(cause instanceof UsageDetailError ? { usage: cause.usage } : {}),
|
|
92
|
-
usageRefusal: { reason: cause.message, usage: wireUsageEvidenceOf(values) ?? sdkUsage },
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
const wireUsageEvidenceOf = (values) => {
|
|
97
|
-
for (let index = values.length - 1; index >= 0; index -= 1) {
|
|
98
|
-
const record = recordOf(values[index]);
|
|
99
|
-
if (record !== null && record.usage !== undefined)
|
|
100
|
-
return record.usage;
|
|
101
|
-
}
|
|
102
|
-
return undefined;
|
|
103
|
-
};
|
|
104
|
-
const wireChargeEvidenceOf = (values) => {
|
|
105
|
-
for (let index = values.length - 1; index >= 0; index -= 1) {
|
|
106
|
-
const record = recordOf(values[index]);
|
|
107
|
-
if (record !== null && record.charge !== undefined)
|
|
108
|
-
return record.charge;
|
|
109
|
-
}
|
|
110
|
-
return undefined;
|
|
111
|
-
};
|
|
112
|
-
// {§provider-finish-reason} The AI SDK's unified classification is the finish reason; the raw vendor
|
|
113
|
-
// value is evidence beside it. One upstream semantic the SDK folds into `other` stays distinct: an exact
|
|
114
|
-
// `insufficient_system_resource` is a resource interruption ({§provider-interrupted-attempt}).
|
|
115
|
-
const finishReasonOf = (unified, raw) => {
|
|
116
|
-
if (raw === "insufficient_system_resource")
|
|
117
|
-
return "resource_interrupted";
|
|
118
|
-
switch (unified) {
|
|
119
|
-
case "stop": return "stop";
|
|
120
|
-
case "length": return "length";
|
|
121
|
-
case "tool-calls": return "tool_calls";
|
|
122
|
-
case "content-filter": return "content_filter";
|
|
123
|
-
case "error": return "error";
|
|
124
|
-
default: return null;
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
const recordOf = (value) => isRecord(value) ? value : null;
|
|
128
|
-
const metadataOf = (values) => {
|
|
129
|
-
const metadata = {};
|
|
130
|
-
for (const value of values) {
|
|
131
|
-
const record = recordOf(value);
|
|
132
|
-
if (record === null)
|
|
133
|
-
continue;
|
|
134
|
-
for (const [key, item] of Object.entries(record)) {
|
|
135
|
-
if (key !== "choices" && key !== "usage" && key !== "charge")
|
|
136
|
-
metadata[key] = item;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
return metadata;
|
|
140
|
-
};
|
|
141
|
-
const streamFailures = new WeakMap();
|
|
142
|
-
const streamFailureOutput = new WeakSet();
|
|
143
|
-
const retainStreamFailureValues = (source, target) => {
|
|
144
|
-
if (typeof source !== "object" || source === null)
|
|
145
|
-
return target;
|
|
146
|
-
const evidence = streamFailures.get(source);
|
|
147
|
-
if (evidence !== undefined)
|
|
148
|
-
streamFailures.set(target, evidence);
|
|
149
|
-
if (streamFailureOutput.has(source))
|
|
150
|
-
streamFailureOutput.add(target);
|
|
151
|
-
return target;
|
|
152
|
-
};
|
|
153
|
-
// {§provider-cancellation-evidence} (#971) — evidence is keyed by the failure object, so the object
|
|
154
|
-
// must be this request's own. A caller's abort reason is shared by every request on that signal (the
|
|
155
|
-
// SDK rejects each with the reason itself), so it is wrapped per request before anything is keyed on it.
|
|
156
|
-
const preserveStreamFailure = (error, evidence, outputObserved, signal) => {
|
|
157
|
-
const shared = signal !== undefined && signal.aborted && error === signal.reason;
|
|
158
|
-
const failure = typeof error === "object" && error !== null && !shared
|
|
159
|
-
? error
|
|
160
|
-
: new Error(shared ? "Provider stream cancelled" : "Provider stream failed", { cause: error });
|
|
161
|
-
streamFailures.set(failure, { ...evidence, error });
|
|
162
|
-
if (outputObserved)
|
|
163
|
-
streamFailureOutput.add(failure);
|
|
164
|
-
return failure;
|
|
165
|
-
};
|
|
166
|
-
export const transportFailureOutputObserved = (error) => {
|
|
167
|
-
const seen = new Set();
|
|
168
|
-
let current = error;
|
|
169
|
-
while (typeof current === "object" && current !== null && !seen.has(current)) {
|
|
170
|
-
if (streamFailureOutput.has(current))
|
|
171
|
-
return true;
|
|
172
|
-
seen.add(current);
|
|
173
|
-
current = current.cause;
|
|
174
|
-
}
|
|
175
|
-
return false;
|
|
176
|
-
};
|
|
177
|
-
export const normalizeRetryAttemptError = (error) => {
|
|
178
|
-
if (!APICallError.isInstance(error)) {
|
|
179
|
-
// Node's Undici stream reader reports a peer-aborted HTTP/2 body as this
|
|
180
|
-
// raw TypeError after headers have arrived. Normalize it at the attempt
|
|
181
|
-
// boundary so the owned scheduler sees the same retryability that the
|
|
182
|
-
// public ProviderError contract would otherwise assign too late.
|
|
183
|
-
if (error instanceof TypeError && error.message.trim().toLowerCase() === "terminated") {
|
|
184
|
-
return retainStreamFailureValues(error, new APICallError({
|
|
185
|
-
message: error.message,
|
|
186
|
-
url: "model:generation",
|
|
187
|
-
requestBodyValues: {},
|
|
188
|
-
cause: error,
|
|
189
|
-
isRetryable: false,
|
|
190
|
-
}));
|
|
191
|
-
}
|
|
192
|
-
return error;
|
|
193
|
-
}
|
|
194
|
-
const directed = retryDirective(error.statusCode, error.responseHeaders ?? {});
|
|
195
|
-
// {§provider-connectivity} (#479): without an explicit directive the only
|
|
196
|
-
// transport-retryable failures are the provider-directed waits — a 429, or
|
|
197
|
-
// any status carrying Retry-After; those live in headers the engine never
|
|
198
|
-
// sees. Every other failure — a bare 408/409/5xx, a network error, and the
|
|
199
|
-
// 2xx invalid-response #446 once promoted — surfaces at once;
|
|
200
|
-
// {§provider-recovery} owns re-issue.
|
|
201
|
-
const policy = directed
|
|
202
|
-
?? (error.statusCode === 429 || retryAfterPresent(error.responseHeaders ?? {}));
|
|
203
|
-
if (policy === error.isRetryable)
|
|
204
|
-
return error;
|
|
205
|
-
return retainStreamFailureValues(error, new APICallError({
|
|
206
|
-
message: error.message,
|
|
207
|
-
url: error.url,
|
|
208
|
-
requestBodyValues: error.requestBodyValues,
|
|
209
|
-
statusCode: error.statusCode,
|
|
210
|
-
responseHeaders: error.responseHeaders,
|
|
211
|
-
responseBody: error.responseBody,
|
|
212
|
-
cause: error,
|
|
213
|
-
isRetryable: policy,
|
|
214
|
-
data: error.data,
|
|
215
|
-
}));
|
|
216
|
-
};
|
|
217
|
-
const executeModel = async (request) => {
|
|
218
|
-
try {
|
|
219
|
-
return await executeModelOnce(request);
|
|
220
|
-
}
|
|
221
|
-
catch (cause) {
|
|
222
|
-
if (request.signal?.aborted) {
|
|
223
|
-
throw retainStreamFailureValues(cause, new Error("Provider call cancelled", { cause: request.signal.reason }));
|
|
224
|
-
}
|
|
225
|
-
throw normalizeRetryAttemptError(cause);
|
|
226
|
-
}
|
|
227
|
-
};
|
|
228
|
-
const executeModelOnce = async (request) => {
|
|
229
|
-
const { languageModel, providerOptions, systemProviderOptions, temperature, topP, topK, presencePenalty, frequencyPenalty, stopSequences, seed, maxOutputTokens, reasoning, } = request;
|
|
230
|
-
const resolvedModel = typeof languageModel === "function"
|
|
231
|
-
? languageModel(request.fetch ?? ((input, init) => globalThis.fetch(input, init)))
|
|
232
|
-
: languageModel;
|
|
233
|
-
// {§provider-native-tool-calls} — requests pass toolChoice: "none" ({§provider-native-tools-disabled}),
|
|
234
|
-
// and raw wire tool_calls plus finishReason are preserved on the response without AI SDK's
|
|
235
|
-
// ToolChoiceViolationError aborting the completed exchange.
|
|
236
|
-
const model = typeof resolvedModel === "string"
|
|
237
|
-
? resolvedModel
|
|
238
|
-
: wrapLanguageModel({
|
|
239
|
-
model: resolvedModel,
|
|
240
|
-
middleware: {
|
|
241
|
-
specificationVersion: "v4",
|
|
242
|
-
wrapGenerate: async ({ doGenerate }) => {
|
|
243
|
-
const generated = await doGenerate();
|
|
244
|
-
return {
|
|
245
|
-
...generated,
|
|
246
|
-
content: generated.content.filter((part) => part.type !== "tool-call"),
|
|
247
|
-
};
|
|
248
|
-
},
|
|
249
|
-
wrapStream: async ({ doStream }) => {
|
|
250
|
-
const streamed = await doStream();
|
|
251
|
-
return {
|
|
252
|
-
...streamed,
|
|
253
|
-
stream: streamed.stream.pipeThrough(new TransformStream({
|
|
254
|
-
transform(chunk, controller) {
|
|
255
|
-
if (chunk.type !== "tool-call")
|
|
256
|
-
controller.enqueue(chunk);
|
|
257
|
-
},
|
|
258
|
-
})),
|
|
259
|
-
};
|
|
260
|
-
},
|
|
261
|
-
},
|
|
262
|
-
});
|
|
263
|
-
const settings = {
|
|
264
|
-
...(temperature === undefined ? {} : { temperature }),
|
|
265
|
-
...(topP === undefined ? {} : { topP }),
|
|
266
|
-
...(topK === undefined ? {} : { topK }),
|
|
267
|
-
...(presencePenalty === undefined ? {} : { presencePenalty }),
|
|
268
|
-
...(frequencyPenalty === undefined ? {} : { frequencyPenalty }),
|
|
269
|
-
...(stopSequences === undefined ? {} : { stopSequences }),
|
|
270
|
-
...(seed === undefined ? {} : { seed }),
|
|
271
|
-
...(maxOutputTokens === undefined ? {} : { maxOutputTokens }),
|
|
272
|
-
...(reasoning === undefined ? {} : { reasoning }),
|
|
273
|
-
...(providerOptions === undefined ? {} : { providerOptions }),
|
|
274
|
-
};
|
|
275
|
-
const firstNonSystem = request.messages.findIndex((message) => message.role !== "system");
|
|
276
|
-
const instructionCount = firstNonSystem === -1 ? request.messages.length : firstNonSystem;
|
|
277
|
-
if (request.messages.slice(instructionCount).some((message) => message.role === "system")) {
|
|
278
|
-
throw new Error("provider messages: system instructions must precede user and assistant messages");
|
|
279
|
-
}
|
|
280
|
-
const instructions = request.messages.slice(0, instructionCount).map(({ content }, index) => ({
|
|
281
|
-
role: "system",
|
|
282
|
-
content: chatMessageText({ content }),
|
|
283
|
-
...(systemProviderOptions !== undefined && index === instructionCount - 1
|
|
284
|
-
? { providerOptions: systemProviderOptions }
|
|
285
|
-
: {}),
|
|
286
|
-
}));
|
|
287
|
-
// {§provider-input-modalities} — user and assistant messages in the SDK's own shape: a user message
|
|
288
|
-
// may be parts (text beside native files distinguished by media type); every other role is text.
|
|
289
|
-
const messages = request.messages.slice(instructionCount).map((message) => {
|
|
290
|
-
if (message.role === "user") {
|
|
291
|
-
return typeof message.content === "string"
|
|
292
|
-
? { role: "user", content: message.content }
|
|
293
|
-
: {
|
|
294
|
-
role: "user",
|
|
295
|
-
content: message.content.map((part) => part.type === "text"
|
|
296
|
-
? { type: "text", text: part.text }
|
|
297
|
-
: { type: "file", data: part.data, mediaType: part.mediaType }),
|
|
298
|
-
};
|
|
299
|
-
}
|
|
300
|
-
return message.role === "assistant"
|
|
301
|
-
? { role: "assistant", content: chatMessageText(message) }
|
|
302
|
-
: { role: "system", content: chatMessageText(message) };
|
|
303
|
-
});
|
|
304
|
-
const common = {
|
|
305
|
-
model,
|
|
306
|
-
toolChoice: "none",
|
|
307
|
-
...(instructions.length === 0 ? {} : { instructions }),
|
|
308
|
-
messages: messages.length > 0
|
|
309
|
-
? messages
|
|
310
|
-
: [{ role: "user", content: "" }],
|
|
311
|
-
// AiSdkProvider owns retries so every physical request is independently
|
|
312
|
-
// observed and accounted. The SDK transport executes exactly once.
|
|
313
|
-
maxRetries: 0,
|
|
314
|
-
abortSignal: request.signal,
|
|
315
|
-
headers: request.headers,
|
|
316
|
-
...settings,
|
|
317
|
-
};
|
|
318
|
-
if (!request.streaming) {
|
|
319
|
-
const result = await generateText({
|
|
320
|
-
...common,
|
|
321
|
-
include: { responseBody: true },
|
|
322
|
-
});
|
|
323
|
-
const { finalStep } = result;
|
|
324
|
-
const rawBody = finalStep.response.body;
|
|
325
|
-
const values = [rawBody];
|
|
326
|
-
const evidence = extractEvidence(values);
|
|
327
|
-
const accountingUsage = wireUsageEvidenceOf(values);
|
|
328
|
-
const reasoningText = evidence.reasoning || finalStep.reasoningText || "";
|
|
329
|
-
const rawFinishReason = result.rawFinishReason;
|
|
330
|
-
return {
|
|
331
|
-
model: finalStep.response.modelId,
|
|
332
|
-
content: result.text,
|
|
333
|
-
reasoning: reasoningText,
|
|
334
|
-
reasoningProjected: evidence.reasoningProjected,
|
|
335
|
-
wire: wireEmissionOf(values),
|
|
336
|
-
finishReason: finishReasonOf(result.finishReason, rawFinishReason),
|
|
337
|
-
...(rawFinishReason === undefined ? {} : { rawFinishReason }),
|
|
338
|
-
...settledUsage(values, result.usage),
|
|
339
|
-
metadata: metadataOf(values),
|
|
340
|
-
logprobs: evidence.logprobs,
|
|
341
|
-
chargeEvidence: {
|
|
342
|
-
...(wireChargeEvidenceOf(values) === undefined
|
|
343
|
-
? {}
|
|
344
|
-
: { charge: wireChargeEvidenceOf(values) }),
|
|
345
|
-
...(accountingUsage === undefined ? {} : { usage: accountingUsage }),
|
|
346
|
-
...(finalStep.providerMetadata === undefined
|
|
347
|
-
? {}
|
|
348
|
-
: { providerMetadata: finalStep.providerMetadata }),
|
|
349
|
-
response: {
|
|
350
|
-
id: finalStep.response.id,
|
|
351
|
-
...(finalStep.response.headers === undefined
|
|
352
|
-
? {}
|
|
353
|
-
: { headers: finalStep.response.headers }),
|
|
354
|
-
},
|
|
355
|
-
},
|
|
356
|
-
...(request.captureRawBody ? { rawBody } : {}),
|
|
357
|
-
warnings: result.warnings ?? [],
|
|
358
|
-
};
|
|
359
|
-
}
|
|
360
|
-
const result = streamText({
|
|
361
|
-
...common,
|
|
362
|
-
include: { rawChunks: true },
|
|
363
|
-
onError: () => { },
|
|
364
|
-
});
|
|
365
|
-
const rawChunks = [];
|
|
366
|
-
let streamError;
|
|
367
|
-
let outputObserved = false;
|
|
368
|
-
let partialContent = "";
|
|
369
|
-
let partialReasoning = "";
|
|
370
|
-
let partialResponse;
|
|
371
|
-
try {
|
|
372
|
-
for await (const part of result.stream) {
|
|
373
|
-
if (part.type === "raw") {
|
|
374
|
-
rawChunks.push(part.rawValue);
|
|
375
|
-
}
|
|
376
|
-
if (part.type === "text-delta" && part.text.length > 0) {
|
|
377
|
-
outputObserved = true;
|
|
378
|
-
partialContent += part.text;
|
|
379
|
-
request.observeText?.(part.text);
|
|
380
|
-
}
|
|
381
|
-
if (part.type === "reasoning-delta" && part.text.length > 0) {
|
|
382
|
-
outputObserved = true;
|
|
383
|
-
partialReasoning += part.text;
|
|
384
|
-
request.observeReasoning?.(part.text);
|
|
385
|
-
}
|
|
386
|
-
if (part.type === "error")
|
|
387
|
-
streamError ??= part.error;
|
|
388
|
-
if (part.type === "finish-step")
|
|
389
|
-
partialResponse = part.response;
|
|
390
|
-
}
|
|
391
|
-
if (streamError !== undefined)
|
|
392
|
-
throw streamError;
|
|
393
|
-
request.signal?.throwIfAborted();
|
|
394
|
-
const evidence = extractEvidence(rawChunks);
|
|
395
|
-
const accountingUsage = wireUsageEvidenceOf(rawChunks);
|
|
396
|
-
const content = await result.text;
|
|
397
|
-
const [finalStep, warnings] = await Promise.all([result.finalStep, result.warnings]);
|
|
398
|
-
const { response, providerMetadata } = finalStep;
|
|
399
|
-
const reasoningText = evidence.reasoning || finalStep.reasoningText || "";
|
|
400
|
-
const rawFinishReason = await result.rawFinishReason;
|
|
401
|
-
const unifiedFinishReason = await result.finishReason;
|
|
402
|
-
return {
|
|
403
|
-
model: response.modelId,
|
|
404
|
-
content,
|
|
405
|
-
reasoning: reasoningText,
|
|
406
|
-
reasoningProjected: evidence.reasoningProjected,
|
|
407
|
-
wire: wireEmissionOf(rawChunks),
|
|
408
|
-
finishReason: finishReasonOf(unifiedFinishReason, rawFinishReason),
|
|
409
|
-
...(rawFinishReason === undefined ? {} : { rawFinishReason }),
|
|
410
|
-
...settledUsage(rawChunks, await result.usage),
|
|
411
|
-
metadata: metadataOf(rawChunks),
|
|
412
|
-
logprobs: evidence.logprobs,
|
|
413
|
-
chargeEvidence: {
|
|
414
|
-
...(wireChargeEvidenceOf(rawChunks) === undefined
|
|
415
|
-
? {}
|
|
416
|
-
: { charge: wireChargeEvidenceOf(rawChunks) }),
|
|
417
|
-
...(accountingUsage === undefined ? {} : { usage: accountingUsage }),
|
|
418
|
-
...(providerMetadata === undefined ? {} : { providerMetadata }),
|
|
419
|
-
response: {
|
|
420
|
-
id: response.id,
|
|
421
|
-
...(response.headers === undefined ? {} : { headers: response.headers }),
|
|
422
|
-
},
|
|
423
|
-
},
|
|
424
|
-
...(request.captureRawBody ? { rawBody: rawChunks } : {}),
|
|
425
|
-
warnings: warnings ?? [],
|
|
426
|
-
};
|
|
427
|
-
}
|
|
428
|
-
catch (error) {
|
|
429
|
-
throw preserveStreamFailure(error, { rawBody: rawChunks, content: partialContent, reasoning: partialReasoning, response: partialResponse }, outputObserved, request.signal);
|
|
430
|
-
}
|
|
431
|
-
};
|
|
432
|
-
export const executeAiSdkModel = executeModel;
|
|
433
|
-
const SDK_OWNED_BODY_KEYS = ["model", "messages", "stream", "stream_options", "tools", "tool_choice", "functions", "function_call", "parallel_tool_calls"];
|
|
434
|
-
export const executeOpenAICompatible = async (request) => {
|
|
435
|
-
for (const key of SDK_OWNED_BODY_KEYS) {
|
|
436
|
-
if (Object.hasOwn(request.body, key)) {
|
|
437
|
-
throw new TypeError(`OpenAI-compatible request extensions may not override SDK-owned field ${JSON.stringify(key)}`);
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
const provider = createOpenAICompatible({
|
|
441
|
-
name: "plurnk",
|
|
442
|
-
baseURL: baseUrl(request.url),
|
|
443
|
-
headers: request.headers,
|
|
444
|
-
fetch: request.fetch,
|
|
445
|
-
includeUsage: true,
|
|
446
|
-
transformRequestBody: (sdkBody) => withoutNativeTools({
|
|
447
|
-
...sdkBody,
|
|
448
|
-
...request.body,
|
|
449
|
-
stream: sdkBody.stream,
|
|
450
|
-
...(sdkBody.stream_options !== undefined
|
|
451
|
-
? { stream_options: sdkBody.stream_options }
|
|
452
|
-
: {}),
|
|
453
|
-
}, "openai"),
|
|
454
|
-
});
|
|
455
|
-
const model = provider.languageModel(request.model, { errorStructure });
|
|
456
|
-
return executeModel({
|
|
457
|
-
languageModel: model,
|
|
458
|
-
headers: {},
|
|
459
|
-
messages: request.messages,
|
|
460
|
-
signal: request.signal,
|
|
461
|
-
streaming: request.streaming,
|
|
462
|
-
captureRawBody: request.captureRawBody,
|
|
463
|
-
...(request.observeReasoning === undefined ? {} : { observeReasoning: request.observeReasoning }),
|
|
464
|
-
...(request.observeText === undefined ? {} : { observeText: request.observeText }),
|
|
465
|
-
});
|
|
466
|
-
};
|
|
467
|
-
const responseBodyValues = (error) => {
|
|
468
|
-
if (error.responseBody === undefined || error.responseBody.length === 0)
|
|
469
|
-
return [];
|
|
470
|
-
try {
|
|
471
|
-
return [JSON.parse(error.responseBody)];
|
|
472
|
-
}
|
|
473
|
-
catch {
|
|
474
|
-
return [];
|
|
475
|
-
}
|
|
476
|
-
};
|
|
477
|
-
const failureRecord = (error, seen = new Set()) => {
|
|
478
|
-
if (!(error instanceof Error))
|
|
479
|
-
return error;
|
|
480
|
-
if (seen.has(error))
|
|
481
|
-
return { name: error.name, message: "Circular error cause" };
|
|
482
|
-
seen.add(error);
|
|
483
|
-
return {
|
|
484
|
-
name: error.name,
|
|
485
|
-
message: error.message,
|
|
486
|
-
...("code" in error ? { code: error.code } : {}),
|
|
487
|
-
...("data" in error ? { data: error.data } : {}),
|
|
488
|
-
...(error.cause === undefined ? {} : { cause: failureRecord(error.cause, seen) }),
|
|
489
|
-
};
|
|
490
|
-
};
|
|
491
|
-
export const transportFailureEvidence = (error) => {
|
|
492
|
-
const partial = typeof error === "object" && error !== null ? streamFailures.get(error) : undefined;
|
|
493
|
-
const apiError = APICallError.isInstance(error) ? error : undefined;
|
|
494
|
-
const values = [...(partial?.rawBody ?? (apiError === undefined ? [] : responseBodyValues(apiError))), error];
|
|
495
|
-
const emission = extractEvidence(values);
|
|
496
|
-
const settled = settledUsage(values, undefined);
|
|
497
|
-
const usageEvidence = wireUsageEvidenceOf(values);
|
|
498
|
-
const charge = wireChargeEvidenceOf(values);
|
|
499
|
-
const responseId = metadataOf(values).id;
|
|
500
|
-
const wireStatus = values
|
|
501
|
-
.map(recordOf)
|
|
502
|
-
.find((record) => Number.isInteger(record?.status))?.status;
|
|
503
|
-
const apiStatus = apiError?.statusCode;
|
|
504
|
-
const httpStatus = (value) => isSafeInteger(value) && value >= 100 && value <= 599 ? value : undefined;
|
|
505
|
-
const status = httpStatus(apiStatus) ?? httpStatus(wireStatus);
|
|
506
|
-
return {
|
|
507
|
-
content: emission.content || partial?.content || "",
|
|
508
|
-
reasoning: emission.reasoning || partial?.reasoning || "",
|
|
509
|
-
...(partial !== undefined ? { rawBody: partial.rawBody }
|
|
510
|
-
: apiError?.responseBody === undefined ? {} : { rawBody: apiError.responseBody }),
|
|
511
|
-
error: failureRecord(error),
|
|
512
|
-
...(partial === undefined || partial.error === error ? {} : { transportError: failureRecord(partial.error) }),
|
|
513
|
-
...settled,
|
|
514
|
-
chargeEvidence: {
|
|
515
|
-
...(charge === undefined ? {} : { charge }),
|
|
516
|
-
...(usageEvidence === undefined ? {} : { usage: usageEvidence }),
|
|
517
|
-
response: {
|
|
518
|
-
...(typeof responseId === "string" ? { id: responseId } : {}),
|
|
519
|
-
...(apiError?.responseHeaders === undefined ? {} : { headers: apiError.responseHeaders }),
|
|
520
|
-
...partial?.response,
|
|
521
|
-
},
|
|
522
|
-
},
|
|
523
|
-
...(status === undefined ? {} : { status }),
|
|
524
|
-
};
|
|
525
|
-
};
|
|
526
|
-
const RETAINED_FIELDS = new Set(["content", "reasoning_content", "reasoning", "thinking", "role"]);
|
|
527
|
-
// {§provider-wire-emission} — the emission as the wire carried it: field counts, chunks that
|
|
528
|
-
// carried nothing, merged tool calls, and the verbatim text of every channel the normalized record
|
|
529
|
-
// does not already hold. A blank emission is then readable instead of guessed at.
|
|
530
|
-
const wireEmissionOf = (values) => {
|
|
531
|
-
let chunks = 0;
|
|
532
|
-
let emptyChunks = 0;
|
|
533
|
-
const fields = {};
|
|
534
|
-
const channels = {};
|
|
535
|
-
const toolCalls = new Map();
|
|
536
|
-
const finishReasons = new Set();
|
|
537
|
-
const unmappedChunks = [];
|
|
538
|
-
for (const value of values) {
|
|
539
|
-
const record = recordOf(value);
|
|
540
|
-
chunks += 1;
|
|
541
|
-
if (record === null || !Array.isArray(record.choices)) {
|
|
542
|
-
unmappedChunks.push(value);
|
|
543
|
-
continue;
|
|
544
|
-
}
|
|
545
|
-
const choices = record.choices;
|
|
546
|
-
let carried = false;
|
|
547
|
-
for (const item of choices) {
|
|
548
|
-
const choice = recordOf(item);
|
|
549
|
-
if (choice === null)
|
|
550
|
-
continue;
|
|
551
|
-
if (typeof choice.finish_reason === "string")
|
|
552
|
-
finishReasons.add(choice.finish_reason);
|
|
553
|
-
const message = recordOf(choice.delta) ?? recordOf(choice.message);
|
|
554
|
-
if (message === null)
|
|
555
|
-
continue;
|
|
556
|
-
for (const [key, field] of Object.entries(message)) {
|
|
557
|
-
if (field === null || field === undefined || key === "role")
|
|
558
|
-
continue;
|
|
559
|
-
if (typeof field === "string" && field.length === 0)
|
|
560
|
-
continue;
|
|
561
|
-
carried = true;
|
|
562
|
-
fields[key] = (fields[key] ?? 0) + 1;
|
|
563
|
-
if (key === "tool_calls" && Array.isArray(field)) {
|
|
564
|
-
for (const [position, callValue] of field.entries()) {
|
|
565
|
-
const call = recordOf(callValue);
|
|
566
|
-
if (call === null)
|
|
567
|
-
continue;
|
|
568
|
-
const index = typeof call.index === "number" ? call.index : position;
|
|
569
|
-
const merged = toolCalls.get(index) ?? { index, arguments: "" };
|
|
570
|
-
if (typeof call.id === "string")
|
|
571
|
-
merged.id = call.id;
|
|
572
|
-
if (typeof call.type === "string")
|
|
573
|
-
merged.type = call.type;
|
|
574
|
-
const fn = recordOf(call.function);
|
|
575
|
-
if (typeof fn?.name === "string")
|
|
576
|
-
merged.name = fn.name;
|
|
577
|
-
if (typeof fn?.arguments === "string")
|
|
578
|
-
merged.arguments += fn.arguments;
|
|
579
|
-
toolCalls.set(index, merged);
|
|
580
|
-
}
|
|
581
|
-
continue;
|
|
582
|
-
}
|
|
583
|
-
if (RETAINED_FIELDS.has(key))
|
|
584
|
-
continue;
|
|
585
|
-
channels[key] = (channels[key] ?? "") + (typeof field === "string" ? field : JSON.stringify(field));
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
if (choices.length > 0 && !carried)
|
|
589
|
-
emptyChunks += 1;
|
|
590
|
-
}
|
|
591
|
-
return {
|
|
592
|
-
chunks,
|
|
593
|
-
emptyChunks,
|
|
594
|
-
fields,
|
|
595
|
-
channels,
|
|
596
|
-
toolCalls: [...toolCalls.values()].toSorted((a, b) => a.index - b.index),
|
|
597
|
-
finishReasons: [...finishReasons],
|
|
598
|
-
...(unmappedChunks.length === 0 ? {} : { unmappedChunks }),
|
|
599
|
-
};
|
|
600
|
-
};
|
|
601
|
-
const extractEvidence = (values) => {
|
|
602
|
-
const logprobs = [];
|
|
603
|
-
let content = "";
|
|
604
|
-
let reasoning = "";
|
|
605
|
-
let reasoningProjected = false;
|
|
606
|
-
for (const value of values) {
|
|
607
|
-
const choices = recordOf(value)?.choices;
|
|
608
|
-
if (!Array.isArray(choices))
|
|
609
|
-
continue;
|
|
610
|
-
const choice = recordOf(choices[0]);
|
|
611
|
-
if (choice === null)
|
|
612
|
-
continue;
|
|
613
|
-
const logprobRecord = recordOf(choice.logprobs);
|
|
614
|
-
const entries = logprobRecord?.content;
|
|
615
|
-
if (Array.isArray(entries)) {
|
|
616
|
-
for (const entryValue of entries) {
|
|
617
|
-
const entry = recordOf(entryValue);
|
|
618
|
-
if (typeof entry?.token !== "string" || typeof entry.logprob !== "number")
|
|
619
|
-
continue;
|
|
620
|
-
const top = Array.isArray(entry.top_logprobs)
|
|
621
|
-
? entry.top_logprobs.flatMap((candidate) => {
|
|
622
|
-
const item = recordOf(candidate);
|
|
623
|
-
return typeof item?.token === "string" && typeof item.logprob === "number"
|
|
624
|
-
? [{ token: item.token, logprob: item.logprob }]
|
|
625
|
-
: [];
|
|
626
|
-
})
|
|
627
|
-
: undefined;
|
|
628
|
-
logprobs.push(top === undefined
|
|
629
|
-
? { token: entry.token, logprob: entry.logprob }
|
|
630
|
-
: { token: entry.token, logprob: entry.logprob, top });
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
const delta = recordOf(choice.delta);
|
|
634
|
-
const message = delta ?? recordOf(choice.message) ?? {};
|
|
635
|
-
if (typeof message.content === "string")
|
|
636
|
-
content += message.content;
|
|
637
|
-
for (const key of ["reasoning_content", "reasoning", "thinking"]) {
|
|
638
|
-
if (typeof message[key] === "string") {
|
|
639
|
-
reasoningProjected = true;
|
|
640
|
-
reasoning += message[key];
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
return {
|
|
645
|
-
logprobs,
|
|
646
|
-
content,
|
|
647
|
-
reasoning,
|
|
648
|
-
reasoningProjected,
|
|
649
|
-
};
|
|
650
|
-
};
|
|
651
|
-
//# sourceMappingURL=aiSdkTransport.js.map
|