@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
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
// {§provider-sdk-boundary} {§provider-request-authority} Vendor transport adapter over @earendil-works/pi-ai
|
|
2
|
+
// and OpenAI-compatible HTTP endpoints (#1305, #1313).
|
|
3
|
+
import { normalizeContext, } from "@earendil-works/pi-ai";
|
|
4
|
+
import { stream as streamAnthropic } from "@earendil-works/pi-ai/api/anthropic-messages";
|
|
5
|
+
import { stream as streamBedrockConverseStream } from "@earendil-works/pi-ai/api/bedrock-converse-stream";
|
|
6
|
+
import { stream as streamGoogleGenerativeAi } from "@earendil-works/pi-ai/api/google-generative-ai";
|
|
7
|
+
import { stream as streamOpenAICompletions } from "@earendil-works/pi-ai/api/openai-completions";
|
|
8
|
+
import { stream as streamOpenAIResponses } from "@earendil-works/pi-ai/api/openai-responses";
|
|
9
|
+
import { isRecord } from "@plurnk/plurnk-contracts";
|
|
10
|
+
import { outboundFetch, syncAmbientHttp } from "@plurnk/plurnk-meta";
|
|
11
|
+
import { APICallError } from "./errors.js";
|
|
12
|
+
import { withoutNativeTools } from "./native-tools.js";
|
|
13
|
+
import { nativeFixedEffort } from "./reasoning-effort.js";
|
|
14
|
+
import { abortFailure, attachFailureEvidence, collectChunkForensics, extractFromAssistantMessage, serializeError, toUsageFromPiMessage, } from "./stream-forensics.js";
|
|
15
|
+
const headersToRecord = (headers) => {
|
|
16
|
+
const out = {};
|
|
17
|
+
for (const [key, value] of headers.entries())
|
|
18
|
+
out[key] = value;
|
|
19
|
+
return out;
|
|
20
|
+
};
|
|
21
|
+
const toPiContext = (messages) => {
|
|
22
|
+
const systemParts = [];
|
|
23
|
+
const contextMessages = [];
|
|
24
|
+
let leadingSystem = true;
|
|
25
|
+
for (const message of messages) {
|
|
26
|
+
if (leadingSystem && message.role === "system") {
|
|
27
|
+
systemParts.push(typeof message.content === "string"
|
|
28
|
+
? message.content
|
|
29
|
+
: message.content.map((part) => part.type === "text" ? part.text : "").join(""));
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
leadingSystem = false;
|
|
33
|
+
if (message.role === "assistant") {
|
|
34
|
+
const text = typeof message.content === "string"
|
|
35
|
+
? message.content
|
|
36
|
+
: message.content.map((part) => part.type === "text" ? part.text : "").join("");
|
|
37
|
+
contextMessages.push({
|
|
38
|
+
role: "assistant",
|
|
39
|
+
content: [{ type: "text", text }],
|
|
40
|
+
api: "openai-completions",
|
|
41
|
+
provider: "openai",
|
|
42
|
+
model: "unknown",
|
|
43
|
+
usage: {
|
|
44
|
+
input: 0,
|
|
45
|
+
output: 0,
|
|
46
|
+
cacheRead: 0,
|
|
47
|
+
cacheWrite: 0,
|
|
48
|
+
totalTokens: 0,
|
|
49
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
50
|
+
},
|
|
51
|
+
stopReason: "stop",
|
|
52
|
+
timestamp: 0,
|
|
53
|
+
});
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
const content = typeof message.content === "string"
|
|
57
|
+
? message.content
|
|
58
|
+
: message.content.map((part) => part.type === "text"
|
|
59
|
+
? { type: "text", text: part.text }
|
|
60
|
+
: { type: "image", data: Buffer.from(part.data).toString("base64"), mimeType: part.mediaType });
|
|
61
|
+
contextMessages.push({
|
|
62
|
+
role: "user",
|
|
63
|
+
content,
|
|
64
|
+
timestamp: 0,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return normalizeContext({
|
|
68
|
+
...(systemParts.length > 0 ? { systemPrompt: systemParts.join("\n\n") } : {}),
|
|
69
|
+
messages: contextMessages,
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
const mapOpenAICompatibleMessages = (messages, systemCacheBreakpoint) => {
|
|
73
|
+
let lastLeadingSystemIndex = -1;
|
|
74
|
+
if (systemCacheBreakpoint) {
|
|
75
|
+
for (let i = 0; i < messages.length; i += 1) {
|
|
76
|
+
if (messages[i].role !== "system")
|
|
77
|
+
break;
|
|
78
|
+
lastLeadingSystemIndex = i;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return messages.map((message, index) => {
|
|
82
|
+
if (index === lastLeadingSystemIndex && typeof message.content === "string") {
|
|
83
|
+
return {
|
|
84
|
+
role: message.role,
|
|
85
|
+
content: [{
|
|
86
|
+
type: "text",
|
|
87
|
+
text: message.content,
|
|
88
|
+
cache_control: { type: "ephemeral" },
|
|
89
|
+
}],
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
if (typeof message.content === "string") {
|
|
93
|
+
return { role: message.role, content: message.content };
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
role: message.role,
|
|
97
|
+
content: message.content.map((part) => {
|
|
98
|
+
if (part.type === "text")
|
|
99
|
+
return { type: "text", text: part.text };
|
|
100
|
+
const base64 = Buffer.from(part.data).toString("base64");
|
|
101
|
+
if (part.mediaType.startsWith("audio/")) {
|
|
102
|
+
return {
|
|
103
|
+
type: "input_audio",
|
|
104
|
+
input_audio: {
|
|
105
|
+
data: base64,
|
|
106
|
+
format: part.mediaType.slice("audio/".length),
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
type: "image_url",
|
|
112
|
+
image_url: { url: `data:${part.mediaType};base64,${base64}` },
|
|
113
|
+
};
|
|
114
|
+
}),
|
|
115
|
+
};
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
const applyAnthropicPayloadAdjustments = (params, messages, systemCacheBreakpoint, serviceTier) => {
|
|
119
|
+
if (serviceTier !== undefined) {
|
|
120
|
+
params.service_tier = serviceTier;
|
|
121
|
+
}
|
|
122
|
+
if (systemCacheBreakpoint && Array.isArray(params.system) && params.system.length > 0) {
|
|
123
|
+
const last = params.system.at(-1);
|
|
124
|
+
if (isRecord(last)) {
|
|
125
|
+
last.cache_control = { type: "ephemeral" };
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const wireMessages = Array.isArray(params.messages) ? params.messages : undefined;
|
|
129
|
+
if (wireMessages === undefined)
|
|
130
|
+
return;
|
|
131
|
+
const nonSystem = messages.filter((m) => m.role !== "system");
|
|
132
|
+
for (let i = 0; i < Math.min(wireMessages.length, nonSystem.length); i += 1) {
|
|
133
|
+
const sourceMsg = nonSystem[i];
|
|
134
|
+
const wireMsg = wireMessages[i];
|
|
135
|
+
if (!Array.isArray(sourceMsg.content) || !isRecord(wireMsg) || !Array.isArray(wireMsg.content))
|
|
136
|
+
continue;
|
|
137
|
+
for (let j = 0; j < Math.min(sourceMsg.content.length, wireMsg.content.length); j += 1) {
|
|
138
|
+
const sourcePart = sourceMsg.content[j];
|
|
139
|
+
const wireBlock = wireMsg.content[j];
|
|
140
|
+
if (sourcePart.type === "file"
|
|
141
|
+
&& sourcePart.mediaType === "application/pdf"
|
|
142
|
+
&& isRecord(wireBlock)
|
|
143
|
+
&& wireBlock.type === "image") {
|
|
144
|
+
wireBlock.type = "document";
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
export const executePiTransportStream = async (opts) => {
|
|
150
|
+
const { model, source, url, providerModel, fetch: configuredFetch, requestHeaders, messages, sessionId, operationSignal, requestSampling, body, effectiveMaxOutputTokens, nativeReasoningBudget, capacity, requestBody, effort, adaptiveEffort, nativeAdaptiveThinking, reasoningRequestBody, systemCacheBreakpoint, cacheAffinity, resolvedControls, rawBody, capture, reasoningStepDown = 0, observers, } = opts;
|
|
151
|
+
const effectiveModel = providerModel ?? {
|
|
152
|
+
id: model,
|
|
153
|
+
api: "openai-completions",
|
|
154
|
+
provider: source,
|
|
155
|
+
baseUrl: url.replace(/\/chat\/completions\/?$/, ""),
|
|
156
|
+
compat: {
|
|
157
|
+
supportsStore: false,
|
|
158
|
+
supportsDeveloperRole: false,
|
|
159
|
+
supportsUsageInStreaming: false,
|
|
160
|
+
supportsFinishReason: true,
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
const buildPiModel = (api) => ({
|
|
164
|
+
id: effectiveModel.id,
|
|
165
|
+
name: effectiveModel.name ?? effectiveModel.id,
|
|
166
|
+
api,
|
|
167
|
+
provider: effectiveModel.provider,
|
|
168
|
+
baseUrl: effectiveModel.baseUrl,
|
|
169
|
+
reasoning: effectiveModel.reasoning ?? true,
|
|
170
|
+
input: [...(effectiveModel.input ?? ["text", "image"])],
|
|
171
|
+
cost: effectiveModel.cost ?? { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
172
|
+
contextWindow: effectiveModel.contextWindow ?? 128_000,
|
|
173
|
+
maxTokens: effectiveModel.maxTokens ?? 32_768,
|
|
174
|
+
...(effectiveModel.headers === undefined ? {} : { headers: { ...effectiveModel.headers } }),
|
|
175
|
+
});
|
|
176
|
+
const context = toPiContext(messages);
|
|
177
|
+
const rawChunks = [];
|
|
178
|
+
let httpResponse;
|
|
179
|
+
let httpErrorBody;
|
|
180
|
+
let capturedTransportError;
|
|
181
|
+
let requestUrl = url ?? effectiveModel.baseUrl;
|
|
182
|
+
let requestBodyValues;
|
|
183
|
+
const baseFetch = capture?.fetch(configuredFetch ?? outboundFetch) ?? configuredFetch ?? outboundFetch;
|
|
184
|
+
const customFetch = async (input, init) => {
|
|
185
|
+
const effectiveInput = providerModel === undefined && url !== undefined
|
|
186
|
+
? url
|
|
187
|
+
: input;
|
|
188
|
+
requestUrl = typeof effectiveInput === "string"
|
|
189
|
+
? effectiveInput
|
|
190
|
+
: effectiveInput instanceof URL
|
|
191
|
+
? effectiveInput.toString()
|
|
192
|
+
: effectiveInput.url;
|
|
193
|
+
if (typeof init?.body === "string") {
|
|
194
|
+
try {
|
|
195
|
+
requestBodyValues = JSON.parse(init.body);
|
|
196
|
+
}
|
|
197
|
+
catch {
|
|
198
|
+
requestBodyValues = init.body;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
try {
|
|
202
|
+
const res = await baseFetch(effectiveInput, init);
|
|
203
|
+
httpResponse = {
|
|
204
|
+
status: res.status,
|
|
205
|
+
headers: headersToRecord(res.headers),
|
|
206
|
+
ok: res.ok,
|
|
207
|
+
};
|
|
208
|
+
if (!res.ok) {
|
|
209
|
+
try {
|
|
210
|
+
httpErrorBody = await res.clone().text();
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
// ignore clone read error
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return res;
|
|
217
|
+
}
|
|
218
|
+
catch (err) {
|
|
219
|
+
capturedTransportError = err;
|
|
220
|
+
throw err;
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
const temperature = typeof requestSampling.temperature === "number" ? requestSampling.temperature : undefined;
|
|
224
|
+
const topP = typeof requestSampling.top_p === "number" ? requestSampling.top_p : undefined;
|
|
225
|
+
const topK = typeof requestSampling.top_k === "number" ? requestSampling.top_k : undefined;
|
|
226
|
+
const presencePenalty = typeof requestSampling.presence_penalty === "number" ? requestSampling.presence_penalty : undefined;
|
|
227
|
+
const frequencyPenalty = typeof requestSampling.frequency_penalty === "number" ? requestSampling.frequency_penalty : undefined;
|
|
228
|
+
const stopSequences = typeof requestSampling.stop === "string"
|
|
229
|
+
? [requestSampling.stop]
|
|
230
|
+
: Array.isArray(requestSampling.stop) && requestSampling.stop.every((value) => typeof value === "string")
|
|
231
|
+
? requestSampling.stop
|
|
232
|
+
: undefined;
|
|
233
|
+
const seed = typeof requestSampling.seed === "number" ? requestSampling.seed : undefined;
|
|
234
|
+
const maxTokens = providerModel === undefined
|
|
235
|
+
? effectiveMaxOutputTokens
|
|
236
|
+
: requestBody.nativeMaxOutputTokens(capacity.outputBudget, nativeReasoningBudget);
|
|
237
|
+
const reasoning = providerModel === undefined || reasoningRequestBody !== undefined
|
|
238
|
+
? "provider-default"
|
|
239
|
+
: effort.mode === "off"
|
|
240
|
+
? "none"
|
|
241
|
+
: effort.mode === "adaptive"
|
|
242
|
+
? adaptiveEffort
|
|
243
|
+
: nativeFixedEffort(effort.mode);
|
|
244
|
+
const commonOptions = {
|
|
245
|
+
signal: operationSignal,
|
|
246
|
+
headers: requestHeaders,
|
|
247
|
+
...(temperature === undefined ? {} : { temperature }),
|
|
248
|
+
...(maxTokens === undefined ? {} : { maxTokens }),
|
|
249
|
+
onProviderStreamEvent: (rawEvent) => {
|
|
250
|
+
rawChunks.push(rawEvent);
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
const onPayload = async (payload) => {
|
|
254
|
+
const transformed = effectiveModel.onPayload !== undefined
|
|
255
|
+
? await effectiveModel.onPayload(payload, effectiveModel)
|
|
256
|
+
: payload;
|
|
257
|
+
const params = isRecord(transformed) ? { ...transformed } : {};
|
|
258
|
+
if (effectiveModel.api === "openai-completions") {
|
|
259
|
+
if (topP !== undefined)
|
|
260
|
+
params.top_p = topP;
|
|
261
|
+
if (topK !== undefined && effectiveModel.includeTopK === true)
|
|
262
|
+
params.top_k = topK;
|
|
263
|
+
if (presencePenalty !== undefined)
|
|
264
|
+
params.presence_penalty = presencePenalty;
|
|
265
|
+
if (frequencyPenalty !== undefined)
|
|
266
|
+
params.frequency_penalty = frequencyPenalty;
|
|
267
|
+
if (stopSequences !== undefined)
|
|
268
|
+
params.stop = stopSequences;
|
|
269
|
+
if (seed !== undefined)
|
|
270
|
+
params.seed = seed;
|
|
271
|
+
if (systemCacheBreakpoint
|
|
272
|
+
|| messages.some((m) => Array.isArray(m.content) && m.content.some((p) => p.type === "file" && !p.mediaType.startsWith("image/")))) {
|
|
273
|
+
params.messages = mapOpenAICompatibleMessages(messages, systemCacheBreakpoint);
|
|
274
|
+
}
|
|
275
|
+
else if (messages[0]?.role === "system" && Array.isArray(params.messages)) {
|
|
276
|
+
const first = params.messages[0];
|
|
277
|
+
if (!isRecord(first) || (first.role !== "system" && first.role !== "developer")) {
|
|
278
|
+
params.messages.unshift({ role: "system", content: "" });
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (providerModel === undefined) {
|
|
282
|
+
for (const [key, value] of Object.entries(body)) {
|
|
283
|
+
if (value === undefined)
|
|
284
|
+
delete params[key];
|
|
285
|
+
else
|
|
286
|
+
params[key] = value;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
if (reasoningRequestBody !== undefined) {
|
|
291
|
+
Object.assign(params, reasoningRequestBody(effort.mode, capacity.reasoningBudget));
|
|
292
|
+
}
|
|
293
|
+
if (cacheAffinity?.target === "body") {
|
|
294
|
+
params[cacheAffinity.name] = sessionId;
|
|
295
|
+
}
|
|
296
|
+
if (resolvedControls.topLogprobs !== undefined) {
|
|
297
|
+
params.logprobs = true;
|
|
298
|
+
params.top_logprobs = resolvedControls.topLogprobs;
|
|
299
|
+
}
|
|
300
|
+
if (resolvedControls.serviceTier !== undefined) {
|
|
301
|
+
params.service_tier = resolvedControls.serviceTier;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
else if (effectiveModel.api === "openai-responses") {
|
|
306
|
+
if (topP !== undefined)
|
|
307
|
+
params.top_p = topP;
|
|
308
|
+
if (cacheAffinity?.target === "body") {
|
|
309
|
+
params[cacheAffinity.name] = sessionId;
|
|
310
|
+
}
|
|
311
|
+
if (resolvedControls.serviceTier !== undefined) {
|
|
312
|
+
params.service_tier = resolvedControls.serviceTier;
|
|
313
|
+
}
|
|
314
|
+
if (resolvedControls.topLogprobs !== undefined) {
|
|
315
|
+
const existingInclude = Array.isArray(params.include)
|
|
316
|
+
? params.include.filter((item) => typeof item === "string")
|
|
317
|
+
: [];
|
|
318
|
+
if (!existingInclude.includes("message.output_text.logprobs")) {
|
|
319
|
+
params.include = [...existingInclude, "message.output_text.logprobs"];
|
|
320
|
+
}
|
|
321
|
+
params.top_logprobs = resolvedControls.topLogprobs;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
else if (effectiveModel.api === "anthropic-messages") {
|
|
325
|
+
if (topP !== undefined)
|
|
326
|
+
params.top_p = topP;
|
|
327
|
+
if (topK !== undefined)
|
|
328
|
+
params.top_k = topK;
|
|
329
|
+
if (stopSequences !== undefined)
|
|
330
|
+
params.stop_sequences = stopSequences;
|
|
331
|
+
applyAnthropicPayloadAdjustments(params, messages, systemCacheBreakpoint, resolvedControls.serviceTier);
|
|
332
|
+
}
|
|
333
|
+
else if (effectiveModel.api === "google-generative-ai") {
|
|
334
|
+
const config = isRecord(params.config) ? { ...params.config } : {};
|
|
335
|
+
if (topP !== undefined)
|
|
336
|
+
config.topP = topP;
|
|
337
|
+
if (topK !== undefined)
|
|
338
|
+
config.topK = topK;
|
|
339
|
+
if (presencePenalty !== undefined)
|
|
340
|
+
config.presencePenalty = presencePenalty;
|
|
341
|
+
if (frequencyPenalty !== undefined)
|
|
342
|
+
config.frequencyPenalty = frequencyPenalty;
|
|
343
|
+
if (stopSequences !== undefined)
|
|
344
|
+
config.stopSequences = stopSequences;
|
|
345
|
+
if (seed !== undefined)
|
|
346
|
+
config.seed = seed;
|
|
347
|
+
if (resolvedControls.serviceTier !== undefined) {
|
|
348
|
+
config.serviceTier = resolvedControls.serviceTier;
|
|
349
|
+
}
|
|
350
|
+
params.config = withoutNativeTools(config, "generate-content");
|
|
351
|
+
try {
|
|
352
|
+
capture?.record({
|
|
353
|
+
method: "POST",
|
|
354
|
+
origin: new URL(effectiveModel.baseUrl).origin,
|
|
355
|
+
body: JSON.stringify(params),
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
catch {
|
|
359
|
+
// ignore invalid URL
|
|
360
|
+
}
|
|
361
|
+
requestBodyValues = params;
|
|
362
|
+
return params;
|
|
363
|
+
}
|
|
364
|
+
else if (effectiveModel.api === "bedrock-converse-stream") {
|
|
365
|
+
const inferenceConfig = isRecord(params.inferenceConfig)
|
|
366
|
+
? { ...params.inferenceConfig }
|
|
367
|
+
: {};
|
|
368
|
+
if (topP !== undefined)
|
|
369
|
+
inferenceConfig.topP = topP;
|
|
370
|
+
if (stopSequences !== undefined)
|
|
371
|
+
inferenceConfig.stopSequences = stopSequences;
|
|
372
|
+
if (Object.keys(inferenceConfig).length > 0)
|
|
373
|
+
params.inferenceConfig = inferenceConfig;
|
|
374
|
+
if (resolvedControls.serviceTier !== undefined) {
|
|
375
|
+
params.serviceTier = { type: resolvedControls.serviceTier };
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
const toolProtocol = effectiveModel.api === "bedrock-converse-stream"
|
|
379
|
+
? "amazon-bedrock"
|
|
380
|
+
: effectiveModel.api === "anthropic-messages"
|
|
381
|
+
? "messages"
|
|
382
|
+
: "openai";
|
|
383
|
+
const nextParams = withoutNativeTools(params, toolProtocol);
|
|
384
|
+
if (effectiveModel.api === "bedrock-converse-stream") {
|
|
385
|
+
try {
|
|
386
|
+
capture?.record({
|
|
387
|
+
method: "POST",
|
|
388
|
+
origin: new URL(effectiveModel.baseUrl).origin,
|
|
389
|
+
body: JSON.stringify(nextParams),
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
catch {
|
|
393
|
+
// ignore invalid URL
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
requestBodyValues = nextParams;
|
|
397
|
+
return nextParams;
|
|
398
|
+
};
|
|
399
|
+
const startStream = () => {
|
|
400
|
+
switch (effectiveModel.api) {
|
|
401
|
+
case "openai-completions": {
|
|
402
|
+
const reasoningEffort = providerModel === undefined || reasoning === "none" || reasoning === "provider-default"
|
|
403
|
+
? undefined
|
|
404
|
+
: reasoning;
|
|
405
|
+
return streamOpenAICompletions({
|
|
406
|
+
...buildPiModel("openai-completions"),
|
|
407
|
+
...(effectiveModel.compat === undefined ? {} : { compat: { ...effectiveModel.compat } }),
|
|
408
|
+
}, context, {
|
|
409
|
+
...commonOptions,
|
|
410
|
+
fetch: customFetch,
|
|
411
|
+
apiKey: effectiveModel.apiKey ?? "plurnk-unset",
|
|
412
|
+
...(reasoningEffort === undefined ? {} : { reasoningEffort }),
|
|
413
|
+
onPayload,
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
case "openai-responses": {
|
|
417
|
+
const reasoningEffort = reasoning === "none" || reasoning === "provider-default" ? undefined : reasoning;
|
|
418
|
+
return streamOpenAIResponses(buildPiModel("openai-responses"), context, {
|
|
419
|
+
...commonOptions,
|
|
420
|
+
fetch: customFetch,
|
|
421
|
+
apiKey: effectiveModel.apiKey ?? "plurnk-unset",
|
|
422
|
+
...(reasoningEffort === undefined ? {} : { reasoningEffort }),
|
|
423
|
+
onPayload,
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
case "anthropic-messages": {
|
|
427
|
+
const effortOption = reasoning !== "none" && reasoning !== "provider-default"
|
|
428
|
+
? (reasoning === "minimal" ? "low" : reasoning === "xhigh" ? "max" : reasoning)
|
|
429
|
+
: undefined;
|
|
430
|
+
return streamAnthropic({
|
|
431
|
+
...buildPiModel("anthropic-messages"),
|
|
432
|
+
...(nativeAdaptiveThinking ? { compat: { forceAdaptiveThinking: true } } : {}),
|
|
433
|
+
}, context, {
|
|
434
|
+
...commonOptions,
|
|
435
|
+
fetch: customFetch,
|
|
436
|
+
apiKey: effectiveModel.apiKey ?? "plurnk-unset",
|
|
437
|
+
cacheRetention: "none",
|
|
438
|
+
...(reasoning === "none"
|
|
439
|
+
? { thinkingEnabled: false }
|
|
440
|
+
: nativeAdaptiveThinking
|
|
441
|
+
? {
|
|
442
|
+
thinkingEnabled: true,
|
|
443
|
+
...(effortOption === undefined ? {} : { effort: effortOption }),
|
|
444
|
+
}
|
|
445
|
+
: nativeReasoningBudget !== null
|
|
446
|
+
? { thinkingEnabled: true, thinkingBudgetTokens: nativeReasoningBudget }
|
|
447
|
+
: {}),
|
|
448
|
+
onPayload,
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
case "google-generative-ai": {
|
|
452
|
+
syncAmbientHttp();
|
|
453
|
+
const googleLevels = ["HIGH", "MEDIUM", "LOW", "MINIMAL"];
|
|
454
|
+
const baseLevel = reasoning === "minimal"
|
|
455
|
+
? "MINIMAL"
|
|
456
|
+
: reasoning === "low"
|
|
457
|
+
? "LOW"
|
|
458
|
+
: reasoning === "medium"
|
|
459
|
+
? "MEDIUM"
|
|
460
|
+
: "HIGH";
|
|
461
|
+
const steppedIndex = Math.min(googleLevels.length - 1, googleLevels.indexOf(baseLevel) + reasoningStepDown);
|
|
462
|
+
const googleLevel = googleLevels[steppedIndex];
|
|
463
|
+
const rawBudget = nativeReasoningBudget ?? capacity.reasoningBudget;
|
|
464
|
+
const reservedVisible = capacity.outputFloor !== null
|
|
465
|
+
&& maxTokens !== undefined
|
|
466
|
+
&& maxTokens > capacity.outputFloor
|
|
467
|
+
? capacity.outputFloor
|
|
468
|
+
: 1;
|
|
469
|
+
const maxThinkingBudget = maxTokens !== undefined
|
|
470
|
+
? Math.max(1, maxTokens - reservedVisible)
|
|
471
|
+
: null;
|
|
472
|
+
const boundedBudget = rawBudget === null
|
|
473
|
+
? null
|
|
474
|
+
: maxThinkingBudget === null
|
|
475
|
+
? rawBudget
|
|
476
|
+
: Math.min(rawBudget, maxThinkingBudget);
|
|
477
|
+
const steppedBudget = boundedBudget === null
|
|
478
|
+
? null
|
|
479
|
+
: Math.max(1, Math.floor(boundedBudget / 2 ** reasoningStepDown));
|
|
480
|
+
const googleThinking = reasoning === "none"
|
|
481
|
+
? { enabled: false }
|
|
482
|
+
: steppedBudget !== null
|
|
483
|
+
? { enabled: true, budgetTokens: steppedBudget }
|
|
484
|
+
: reasoning !== "provider-default"
|
|
485
|
+
? {
|
|
486
|
+
enabled: true,
|
|
487
|
+
level: googleLevel,
|
|
488
|
+
}
|
|
489
|
+
: undefined;
|
|
490
|
+
return streamGoogleGenerativeAi(buildPiModel("google-generative-ai"), context, {
|
|
491
|
+
...commonOptions,
|
|
492
|
+
...(effectiveModel.apiKey === undefined ? {} : { apiKey: effectiveModel.apiKey }),
|
|
493
|
+
...(googleThinking === undefined ? {} : { thinking: googleThinking }),
|
|
494
|
+
onPayload,
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
case "bedrock-converse-stream": {
|
|
498
|
+
syncAmbientHttp();
|
|
499
|
+
const bedrockReasoning = reasoning === "none" || reasoning === "provider-default"
|
|
500
|
+
? undefined
|
|
501
|
+
: (reasoning === "xhigh" ? "max" : reasoning);
|
|
502
|
+
return streamBedrockConverseStream(buildPiModel("bedrock-converse-stream"), context, {
|
|
503
|
+
...commonOptions,
|
|
504
|
+
...(bedrockReasoning === undefined ? {} : { reasoning: bedrockReasoning }),
|
|
505
|
+
...(nativeReasoningBudget !== null && bedrockReasoning !== undefined
|
|
506
|
+
? {
|
|
507
|
+
thinkingBudgets: {
|
|
508
|
+
minimal: nativeReasoningBudget,
|
|
509
|
+
low: nativeReasoningBudget,
|
|
510
|
+
medium: nativeReasoningBudget,
|
|
511
|
+
high: nativeReasoningBudget,
|
|
512
|
+
},
|
|
513
|
+
}
|
|
514
|
+
: {}),
|
|
515
|
+
onPayload,
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
let outputObserved = false;
|
|
521
|
+
let finalMessage;
|
|
522
|
+
let lastErrorMessage;
|
|
523
|
+
let streamError;
|
|
524
|
+
const stream = startStream();
|
|
525
|
+
for await (const event of stream) {
|
|
526
|
+
if (event.type === "text_delta" && event.delta.length > 0) {
|
|
527
|
+
outputObserved = true;
|
|
528
|
+
observers.observeText?.(event.delta);
|
|
529
|
+
}
|
|
530
|
+
else if (event.type === "thinking_delta" && event.delta.length > 0) {
|
|
531
|
+
outputObserved = true;
|
|
532
|
+
observers.observeReasoning?.(event.delta);
|
|
533
|
+
}
|
|
534
|
+
else if (event.type === "toolcall_start" || event.type === "toolcall_delta" || event.type === "toolcall_end") {
|
|
535
|
+
outputObserved = true;
|
|
536
|
+
}
|
|
537
|
+
else if (event.type === "done") {
|
|
538
|
+
finalMessage = event.message;
|
|
539
|
+
}
|
|
540
|
+
else if (event.type === "error") {
|
|
541
|
+
lastErrorMessage = event.error;
|
|
542
|
+
if (event.error.rawStopReason !== undefined
|
|
543
|
+
&& !operationSignal?.aborted
|
|
544
|
+
&& capturedTransportError === undefined
|
|
545
|
+
&& httpResponse?.ok !== false) {
|
|
546
|
+
finalMessage = event.error;
|
|
547
|
+
}
|
|
548
|
+
else {
|
|
549
|
+
streamError = event.error.errorMessage ?? "Provider stream failed";
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
const forensics = collectChunkForensics(rawChunks);
|
|
554
|
+
const messageInfo = extractFromAssistantMessage(finalMessage ?? lastErrorMessage);
|
|
555
|
+
const responseHeaders = httpResponse?.headers;
|
|
556
|
+
const responseId = finalMessage?.responseId ?? lastErrorMessage?.responseId ?? forensics.id;
|
|
557
|
+
if (finalMessage === undefined) {
|
|
558
|
+
const failureRawBody = rawChunks.length > 0
|
|
559
|
+
? rawChunks
|
|
560
|
+
: httpErrorBody !== undefined && httpErrorBody.length > 0
|
|
561
|
+
? httpErrorBody
|
|
562
|
+
: undefined;
|
|
563
|
+
if (operationSignal?.aborted) {
|
|
564
|
+
const abortError = abortFailure(operationSignal);
|
|
565
|
+
const usage = toUsageFromPiMessage(lastErrorMessage, forensics.rawUsage);
|
|
566
|
+
attachFailureEvidence(abortError, {
|
|
567
|
+
status: httpResponse?.status,
|
|
568
|
+
usage,
|
|
569
|
+
chargeEvidence: {
|
|
570
|
+
...(forensics.charge === undefined ? {} : { charge: forensics.charge }),
|
|
571
|
+
...(forensics.rawUsage === undefined ? {} : { usage: forensics.rawUsage }),
|
|
572
|
+
response: {
|
|
573
|
+
...(responseId === undefined ? {} : { id: responseId }),
|
|
574
|
+
...(responseHeaders === undefined ? {} : { headers: responseHeaders }),
|
|
575
|
+
},
|
|
576
|
+
},
|
|
577
|
+
content: messageInfo.content,
|
|
578
|
+
reasoning: messageInfo.reasoning,
|
|
579
|
+
error: serializeError(abortError),
|
|
580
|
+
...(failureRawBody === undefined ? {} : { rawBody: failureRawBody }),
|
|
581
|
+
outputObserved: outputObserved || messageInfo.content.length > 0 || messageInfo.reasoning.length > 0,
|
|
582
|
+
});
|
|
583
|
+
throw abortError;
|
|
584
|
+
}
|
|
585
|
+
const statusCode = httpResponse !== undefined && !httpResponse.ok ? httpResponse.status : undefined;
|
|
586
|
+
if (capturedTransportError !== undefined && statusCode === undefined) {
|
|
587
|
+
const usage = toUsageFromPiMessage(lastErrorMessage, forensics.rawUsage);
|
|
588
|
+
attachFailureEvidence(capturedTransportError, {
|
|
589
|
+
status: httpResponse?.status,
|
|
590
|
+
usage,
|
|
591
|
+
chargeEvidence: {
|
|
592
|
+
...(forensics.charge === undefined ? {} : { charge: forensics.charge }),
|
|
593
|
+
...(forensics.rawUsage === undefined ? {} : { usage: forensics.rawUsage }),
|
|
594
|
+
response: {
|
|
595
|
+
...(responseId === undefined ? {} : { id: responseId }),
|
|
596
|
+
...(responseHeaders === undefined ? {} : { headers: responseHeaders }),
|
|
597
|
+
},
|
|
598
|
+
},
|
|
599
|
+
content: messageInfo.content,
|
|
600
|
+
reasoning: messageInfo.reasoning,
|
|
601
|
+
error: serializeError(capturedTransportError),
|
|
602
|
+
...(failureRawBody === undefined ? {} : { rawBody: failureRawBody }),
|
|
603
|
+
outputObserved: outputObserved || messageInfo.content.length > 0 || messageInfo.reasoning.length > 0,
|
|
604
|
+
});
|
|
605
|
+
throw capturedTransportError;
|
|
606
|
+
}
|
|
607
|
+
let parsedErrorData;
|
|
608
|
+
let errorMessage = streamError ?? `HTTP ${statusCode ?? "error"}`;
|
|
609
|
+
if (httpErrorBody !== undefined && httpErrorBody.length > 0) {
|
|
610
|
+
try {
|
|
611
|
+
parsedErrorData = JSON.parse(httpErrorBody);
|
|
612
|
+
if (isRecord(parsedErrorData)) {
|
|
613
|
+
const errField = parsedErrorData.error;
|
|
614
|
+
if (isRecord(errField) && typeof errField.message === "string" && errField.message.length > 0) {
|
|
615
|
+
errorMessage = errField.message;
|
|
616
|
+
}
|
|
617
|
+
else if (typeof errField === "string" && errField.length > 0) {
|
|
618
|
+
errorMessage = errField;
|
|
619
|
+
}
|
|
620
|
+
else if (typeof parsedErrorData.message === "string" && parsedErrorData.message.length > 0) {
|
|
621
|
+
errorMessage = parsedErrorData.message;
|
|
622
|
+
}
|
|
623
|
+
else {
|
|
624
|
+
errorMessage = httpErrorBody;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
catch {
|
|
629
|
+
errorMessage = httpErrorBody;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
const apiError = new APICallError({
|
|
633
|
+
message: errorMessage,
|
|
634
|
+
url: requestUrl,
|
|
635
|
+
requestBodyValues,
|
|
636
|
+
statusCode,
|
|
637
|
+
responseHeaders,
|
|
638
|
+
responseBody: httpErrorBody,
|
|
639
|
+
data: parsedErrorData,
|
|
640
|
+
});
|
|
641
|
+
const usage = toUsageFromPiMessage(lastErrorMessage, forensics.rawUsage);
|
|
642
|
+
let charge = forensics.charge;
|
|
643
|
+
if (parsedErrorData !== undefined && isRecord(parsedErrorData) && isRecord(parsedErrorData.usage)) {
|
|
644
|
+
charge = parsedErrorData.usage.cost ?? charge;
|
|
645
|
+
}
|
|
646
|
+
attachFailureEvidence(apiError, {
|
|
647
|
+
status: statusCode,
|
|
648
|
+
usage,
|
|
649
|
+
chargeEvidence: {
|
|
650
|
+
...(charge === undefined ? {} : { charge }),
|
|
651
|
+
...(forensics.rawUsage === undefined ? {} : { usage: forensics.rawUsage }),
|
|
652
|
+
response: {
|
|
653
|
+
...(responseId === undefined ? {} : { id: responseId }),
|
|
654
|
+
...(responseHeaders === undefined ? {} : { headers: responseHeaders }),
|
|
655
|
+
},
|
|
656
|
+
},
|
|
657
|
+
content: messageInfo.content,
|
|
658
|
+
reasoning: messageInfo.reasoning,
|
|
659
|
+
error: serializeError(apiError),
|
|
660
|
+
...(failureRawBody === undefined ? {} : { rawBody: failureRawBody }),
|
|
661
|
+
outputObserved: outputObserved || messageInfo.content.length > 0 || messageInfo.reasoning.length > 0,
|
|
662
|
+
});
|
|
663
|
+
throw apiError;
|
|
664
|
+
}
|
|
665
|
+
const usage = toUsageFromPiMessage(finalMessage, forensics.rawUsage);
|
|
666
|
+
const chargeEvidence = {
|
|
667
|
+
...(forensics.charge === undefined ? {} : { charge: forensics.charge }),
|
|
668
|
+
...(forensics.rawUsage === undefined ? {} : { usage: forensics.rawUsage }),
|
|
669
|
+
response: {
|
|
670
|
+
...(responseId === undefined ? {} : { id: responseId }),
|
|
671
|
+
...(responseHeaders === undefined ? {} : { headers: responseHeaders }),
|
|
672
|
+
},
|
|
673
|
+
};
|
|
674
|
+
return {
|
|
675
|
+
id: responseId,
|
|
676
|
+
model: forensics.model ?? finalMessage.model ?? effectiveModel.id,
|
|
677
|
+
content: messageInfo.content,
|
|
678
|
+
reasoning: messageInfo.reasoning,
|
|
679
|
+
reasoningProjected: messageInfo.reasoningProjected,
|
|
680
|
+
finishReason: messageInfo.finishReason,
|
|
681
|
+
...(messageInfo.rawFinishReason === undefined ? {} : { rawFinishReason: messageInfo.rawFinishReason }),
|
|
682
|
+
usage,
|
|
683
|
+
chargeEvidence,
|
|
684
|
+
logprobs: forensics.logprobs,
|
|
685
|
+
metadata: forensics.metadata,
|
|
686
|
+
wire: {
|
|
687
|
+
chunks: rawChunks.length,
|
|
688
|
+
emptyChunks: 0,
|
|
689
|
+
fields: {},
|
|
690
|
+
channels: forensics.channels,
|
|
691
|
+
toolCalls: messageInfo.toolCalls,
|
|
692
|
+
unmappedChunks: [],
|
|
693
|
+
},
|
|
694
|
+
...(rawBody ? { rawBody: rawChunks } : {}),
|
|
695
|
+
};
|
|
696
|
+
};
|
|
697
|
+
//# sourceMappingURL=pi-transport.js.map
|