@jerome-benoit/sap-ai-provider 4.6.8 → 4.7.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/LICENSE +202 -0
- package/README.md +1 -1
- package/dist/{chunk-M3AV2YUV.js → chunk-36DFHUVQ.js} +414 -277
- package/dist/chunk-36DFHUVQ.js.map +1 -0
- package/dist/{chunk-IIBSUXGT.js → chunk-4JIMBRMV.js} +44 -7
- package/dist/chunk-4JIMBRMV.js.map +1 -0
- package/dist/{chunk-6BVUDEKZ.js → chunk-BZZJKLDG.js} +60 -6
- package/dist/chunk-BZZJKLDG.js.map +1 -0
- package/dist/chunk-GMMX46AH.js +93 -0
- package/dist/chunk-GMMX46AH.js.map +1 -0
- package/dist/{chunk-7OGNFVGC.js → chunk-TVXWNZQT.js} +5276 -5608
- package/dist/chunk-TVXWNZQT.js.map +1 -0
- package/dist/chunk-WDUOKIKD.js +188 -0
- package/dist/chunk-WDUOKIKD.js.map +1 -0
- package/dist/{foundation-models-embedding-model-strategy-GXJRGM4X.js → foundation-models-embedding-model-strategy-YWPDIJEN.js} +8 -4
- package/dist/foundation-models-embedding-model-strategy-YWPDIJEN.js.map +1 -0
- package/dist/{foundation-models-language-model-strategy-FGUGQPBL.js → foundation-models-language-model-strategy-WDSQWU64.js} +16 -13
- package/dist/foundation-models-language-model-strategy-WDSQWU64.js.map +1 -0
- package/dist/index.cjs +6357 -6418
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +66 -66
- package/dist/index.d.ts +66 -66
- package/dist/index.js +11 -172
- package/dist/index.js.map +1 -1
- package/dist/{orchestration-embedding-model-strategy-663QYZL7.js → orchestration-embedding-model-strategy-66QYAO64.js} +14 -4
- package/dist/orchestration-embedding-model-strategy-66QYAO64.js.map +1 -0
- package/dist/{orchestration-language-model-strategy-X3LKHORW.js → orchestration-language-model-strategy-4TKRZK7U.js} +52 -77
- package/dist/orchestration-language-model-strategy-4TKRZK7U.js.map +1 -0
- package/package.json +27 -21
- package/LICENSE.md +0 -187
- package/dist/chunk-6BVUDEKZ.js.map +0 -1
- package/dist/chunk-7OGNFVGC.js.map +0 -1
- package/dist/chunk-IIBSUXGT.js.map +0 -1
- package/dist/chunk-M3AV2YUV.js.map +0 -1
- package/dist/chunk-X66RDDSB.js +0 -61
- package/dist/chunk-X66RDDSB.js.map +0 -1
- package/dist/foundation-models-embedding-model-strategy-GXJRGM4X.js.map +0 -1
- package/dist/foundation-models-language-model-strategy-FGUGQPBL.js.map +0 -1
- package/dist/orchestration-embedding-model-strategy-663QYZL7.js.map +0 -1
- package/dist/orchestration-language-model-strategy-X3LKHORW.js.map +0 -1
|
@@ -2,14 +2,18 @@ import {createRequire as __createRequire} from 'module';var require=__createRequ
|
|
|
2
2
|
import {
|
|
3
3
|
deepMerge,
|
|
4
4
|
getProviderName,
|
|
5
|
+
normalizeHeaders,
|
|
5
6
|
sapAIEmbeddingProviderOptions,
|
|
6
7
|
validateModelParamsWithWarnings
|
|
7
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-TVXWNZQT.js";
|
|
8
9
|
|
|
9
10
|
// src/strategy-utils.ts
|
|
10
11
|
import { TooManyEmbeddingValuesForCallError } from "@ai-sdk/provider";
|
|
11
|
-
import {
|
|
12
|
+
import { parseProviderOptions } from "@ai-sdk/provider-utils";
|
|
12
13
|
import { z } from "zod";
|
|
14
|
+
|
|
15
|
+
// src/stream-transformer.ts
|
|
16
|
+
import { convertAsyncIteratorToReadableStream } from "@ai-sdk/provider-utils";
|
|
13
17
|
var StreamIdGenerator = class {
|
|
14
18
|
/**
|
|
15
19
|
* @returns A UUID string for identifying the response.
|
|
@@ -23,7 +27,293 @@ var StreamIdGenerator = class {
|
|
|
23
27
|
generateTextBlockId() {
|
|
24
28
|
return crypto.randomUUID();
|
|
25
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* @returns A UUID string for identifying a tool call when the API does not provide one.
|
|
32
|
+
*/
|
|
33
|
+
generateToolCallId() {
|
|
34
|
+
return crypto.randomUUID();
|
|
35
|
+
}
|
|
26
36
|
};
|
|
37
|
+
function createInitialStreamState() {
|
|
38
|
+
return {
|
|
39
|
+
activeText: false,
|
|
40
|
+
finishReason: {
|
|
41
|
+
raw: void 0,
|
|
42
|
+
unified: "other"
|
|
43
|
+
},
|
|
44
|
+
isFirstChunk: true,
|
|
45
|
+
usage: {
|
|
46
|
+
inputTokens: {
|
|
47
|
+
cacheRead: void 0,
|
|
48
|
+
cacheWrite: void 0,
|
|
49
|
+
noCache: void 0,
|
|
50
|
+
total: void 0
|
|
51
|
+
},
|
|
52
|
+
outputTokens: {
|
|
53
|
+
reasoning: void 0,
|
|
54
|
+
text: void 0,
|
|
55
|
+
total: void 0
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function createStreamTransformer(config) {
|
|
61
|
+
const {
|
|
62
|
+
convertToAISDKError,
|
|
63
|
+
idGenerator,
|
|
64
|
+
includeRawChunks,
|
|
65
|
+
modelId,
|
|
66
|
+
options,
|
|
67
|
+
providerName,
|
|
68
|
+
requestId,
|
|
69
|
+
responseId,
|
|
70
|
+
sdkStream,
|
|
71
|
+
streamResponseGetCitations,
|
|
72
|
+
streamResponseGetFinishReason,
|
|
73
|
+
streamResponseGetIntermediateFailures,
|
|
74
|
+
streamResponseGetTokenUsage,
|
|
75
|
+
url,
|
|
76
|
+
version,
|
|
77
|
+
warnings
|
|
78
|
+
} = config;
|
|
79
|
+
let textBlockId = null;
|
|
80
|
+
const streamState = createInitialStreamState();
|
|
81
|
+
const toolCallsInProgress = /* @__PURE__ */ new Map();
|
|
82
|
+
function emitToolInputStart(tc, controller) {
|
|
83
|
+
if (tc.didEmitInputStart || tc.id == null) return;
|
|
84
|
+
tc.didEmitInputStart = true;
|
|
85
|
+
controller.enqueue({
|
|
86
|
+
id: tc.id,
|
|
87
|
+
toolName: tc.toolName ?? "",
|
|
88
|
+
type: "tool-input-start"
|
|
89
|
+
});
|
|
90
|
+
if (tc.arguments.length > 0) {
|
|
91
|
+
controller.enqueue({
|
|
92
|
+
delta: tc.arguments,
|
|
93
|
+
id: tc.id,
|
|
94
|
+
type: "tool-input-delta"
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return convertAsyncIteratorToReadableStream(
|
|
99
|
+
safeIterate(sdkStream)[Symbol.asyncIterator]()
|
|
100
|
+
).pipeThrough(
|
|
101
|
+
new TransformStream({
|
|
102
|
+
flush(controller) {
|
|
103
|
+
const didEmitAnyToolCalls = finalizeToolCalls(
|
|
104
|
+
controller,
|
|
105
|
+
toolCallsInProgress,
|
|
106
|
+
idGenerator,
|
|
107
|
+
emitToolInputStart
|
|
108
|
+
);
|
|
109
|
+
if (streamState.activeText && textBlockId) {
|
|
110
|
+
controller.enqueue({ id: textBlockId, type: "text-end" });
|
|
111
|
+
}
|
|
112
|
+
const finalFinishReason = streamResponseGetFinishReason();
|
|
113
|
+
if (finalFinishReason) {
|
|
114
|
+
streamState.finishReason = mapFinishReason(finalFinishReason);
|
|
115
|
+
} else if (didEmitAnyToolCalls) {
|
|
116
|
+
streamState.finishReason = {
|
|
117
|
+
raw: void 0,
|
|
118
|
+
unified: "tool-calls"
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
const finalUsage = streamResponseGetTokenUsage();
|
|
122
|
+
if (finalUsage) {
|
|
123
|
+
streamState.usage = mapTokenUsage(finalUsage);
|
|
124
|
+
}
|
|
125
|
+
const streamCitations = streamResponseGetCitations?.();
|
|
126
|
+
if (streamCitations?.length) {
|
|
127
|
+
for (const citation of streamCitations) {
|
|
128
|
+
controller.enqueue({
|
|
129
|
+
id: String(citation.ref_id ?? citation.url),
|
|
130
|
+
sourceType: "url",
|
|
131
|
+
title: citation.title,
|
|
132
|
+
type: "source",
|
|
133
|
+
url: citation.url
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
const streamIntermediateFailures = streamResponseGetIntermediateFailures?.();
|
|
138
|
+
controller.enqueue({
|
|
139
|
+
finishReason: streamState.finishReason,
|
|
140
|
+
providerMetadata: {
|
|
141
|
+
[providerName]: {
|
|
142
|
+
...buildAnthropicCacheMetadata(finalUsage),
|
|
143
|
+
finishReason: streamState.finishReason.raw ?? "unknown",
|
|
144
|
+
finishReasonMapped: streamState.finishReason,
|
|
145
|
+
...streamIntermediateFailures?.length ? {
|
|
146
|
+
intermediateFailures: sanitizeAsJSONArray(streamIntermediateFailures)
|
|
147
|
+
} : {},
|
|
148
|
+
...requestId ? { requestId } : {},
|
|
149
|
+
responseId,
|
|
150
|
+
version
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
type: "finish",
|
|
154
|
+
usage: streamState.usage
|
|
155
|
+
});
|
|
156
|
+
},
|
|
157
|
+
start(controller) {
|
|
158
|
+
controller.enqueue({
|
|
159
|
+
type: "stream-start",
|
|
160
|
+
warnings: [...warnings]
|
|
161
|
+
});
|
|
162
|
+
},
|
|
163
|
+
transform(chunk, controller) {
|
|
164
|
+
if (chunk instanceof Error) {
|
|
165
|
+
handleStreamError(chunk, controller, convertToAISDKError, options, url);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (includeRawChunks) {
|
|
169
|
+
controller.enqueue({
|
|
170
|
+
rawValue: chunk._data ?? chunk,
|
|
171
|
+
type: "raw"
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
if (streamState.isFirstChunk) {
|
|
175
|
+
streamState.isFirstChunk = false;
|
|
176
|
+
controller.enqueue({
|
|
177
|
+
id: responseId,
|
|
178
|
+
modelId,
|
|
179
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
180
|
+
type: "response-metadata"
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
const deltaToolCalls = chunk.getDeltaToolCalls();
|
|
184
|
+
if (Array.isArray(deltaToolCalls) && deltaToolCalls.length > 0) {
|
|
185
|
+
streamState.finishReason = {
|
|
186
|
+
raw: void 0,
|
|
187
|
+
unified: "tool-calls"
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
const deltaContent = chunk.getDeltaContent();
|
|
191
|
+
if (typeof deltaContent === "string" && deltaContent.length > 0 && streamState.finishReason.unified !== "tool-calls") {
|
|
192
|
+
textBlockId = handleTextDelta(
|
|
193
|
+
deltaContent,
|
|
194
|
+
controller,
|
|
195
|
+
streamState,
|
|
196
|
+
textBlockId,
|
|
197
|
+
idGenerator
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
if (Array.isArray(deltaToolCalls) && deltaToolCalls.length > 0) {
|
|
201
|
+
handleToolCallDeltas(deltaToolCalls, controller, toolCallsInProgress, emitToolInputStart);
|
|
202
|
+
}
|
|
203
|
+
const chunkFinishReason = chunk.getFinishReason();
|
|
204
|
+
if (chunkFinishReason) {
|
|
205
|
+
streamState.finishReason = mapFinishReason(chunkFinishReason);
|
|
206
|
+
if (streamState.finishReason.unified === "tool-calls") {
|
|
207
|
+
finalizeToolCalls(controller, toolCallsInProgress, idGenerator, emitToolInputStart);
|
|
208
|
+
if (streamState.activeText && textBlockId) {
|
|
209
|
+
controller.enqueue({ id: textBlockId, type: "text-end" });
|
|
210
|
+
streamState.activeText = false;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
})
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
function finalizeToolCalls(controller, toolCallsInProgress, idGenerator, emitToolInputStart) {
|
|
219
|
+
const toolCalls = Array.from(toolCallsInProgress.values());
|
|
220
|
+
let didEmitAnyToolCalls = false;
|
|
221
|
+
for (const tc of toolCalls) {
|
|
222
|
+
if (tc.didEmitCall) {
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
tc.id ??= idGenerator.generateToolCallId();
|
|
226
|
+
emitToolInputStart(tc, controller);
|
|
227
|
+
didEmitAnyToolCalls = true;
|
|
228
|
+
tc.didEmitCall = true;
|
|
229
|
+
controller.enqueue({ id: tc.id, type: "tool-input-end" });
|
|
230
|
+
controller.enqueue({
|
|
231
|
+
input: tc.arguments,
|
|
232
|
+
toolCallId: tc.id,
|
|
233
|
+
toolName: tc.toolName ?? "",
|
|
234
|
+
type: "tool-call"
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
return didEmitAnyToolCalls;
|
|
238
|
+
}
|
|
239
|
+
function handleStreamError(error, controller, convertToAISDKError, options, url) {
|
|
240
|
+
const aiError = convertToAISDKError(error, {
|
|
241
|
+
operation: "doStream",
|
|
242
|
+
requestBody: createAISDKRequestBodySummary(options),
|
|
243
|
+
url
|
|
244
|
+
});
|
|
245
|
+
controller.enqueue({
|
|
246
|
+
error: aiError instanceof Error ? aiError : new Error(String(aiError)),
|
|
247
|
+
type: "error"
|
|
248
|
+
});
|
|
249
|
+
controller.terminate();
|
|
250
|
+
}
|
|
251
|
+
function handleTextDelta(deltaContent, controller, streamState, currentTextBlockId, idGenerator) {
|
|
252
|
+
let textBlockId = currentTextBlockId;
|
|
253
|
+
if (!streamState.activeText) {
|
|
254
|
+
textBlockId = idGenerator.generateTextBlockId();
|
|
255
|
+
controller.enqueue({ id: textBlockId, type: "text-start" });
|
|
256
|
+
streamState.activeText = true;
|
|
257
|
+
}
|
|
258
|
+
if (textBlockId) {
|
|
259
|
+
controller.enqueue({
|
|
260
|
+
delta: deltaContent,
|
|
261
|
+
id: textBlockId,
|
|
262
|
+
type: "text-delta"
|
|
263
|
+
});
|
|
264
|
+
return textBlockId;
|
|
265
|
+
}
|
|
266
|
+
return currentTextBlockId ?? idGenerator.generateTextBlockId();
|
|
267
|
+
}
|
|
268
|
+
function handleToolCallDeltas(deltaToolCalls, controller, toolCallsInProgress, emitToolInputStart) {
|
|
269
|
+
for (const toolCallChunk of deltaToolCalls) {
|
|
270
|
+
const index = toolCallChunk.index;
|
|
271
|
+
if (typeof index !== "number" || !Number.isFinite(index)) {
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
if (!toolCallsInProgress.has(index)) {
|
|
275
|
+
toolCallsInProgress.set(index, {
|
|
276
|
+
arguments: "",
|
|
277
|
+
didEmitCall: false,
|
|
278
|
+
didEmitInputStart: false,
|
|
279
|
+
id: typeof toolCallChunk.id === "string" && toolCallChunk.id.length > 0 ? toolCallChunk.id : void 0,
|
|
280
|
+
toolName: toolCallChunk.function?.name
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
const tc = toolCallsInProgress.get(index);
|
|
284
|
+
if (!tc) continue;
|
|
285
|
+
if (typeof toolCallChunk.id === "string" && toolCallChunk.id.length > 0 && tc.id === void 0) {
|
|
286
|
+
tc.id = toolCallChunk.id;
|
|
287
|
+
}
|
|
288
|
+
const nextToolName = toolCallChunk.function?.name;
|
|
289
|
+
if (typeof nextToolName === "string" && nextToolName.length > 0) {
|
|
290
|
+
tc.toolName = nextToolName;
|
|
291
|
+
}
|
|
292
|
+
if (!tc.didEmitInputStart && tc.toolName != null && tc.id != null) {
|
|
293
|
+
emitToolInputStart(tc, controller);
|
|
294
|
+
}
|
|
295
|
+
const argumentsDelta = toolCallChunk.function?.arguments;
|
|
296
|
+
if (typeof argumentsDelta === "string" && argumentsDelta.length > 0) {
|
|
297
|
+
tc.arguments += argumentsDelta;
|
|
298
|
+
if (tc.didEmitInputStart && tc.id != null) {
|
|
299
|
+
controller.enqueue({
|
|
300
|
+
delta: argumentsDelta,
|
|
301
|
+
id: tc.id,
|
|
302
|
+
type: "tool-input-delta"
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
async function* safeIterate(iterable) {
|
|
309
|
+
try {
|
|
310
|
+
yield* iterable;
|
|
311
|
+
} catch (error) {
|
|
312
|
+
yield error instanceof Error ? error : new Error(String(error));
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// src/strategy-utils.ts
|
|
27
317
|
function applyParameterOverrides(modelParams, options, sapModelParams, settingsModelParams, mappings) {
|
|
28
318
|
for (const mapping of mappings) {
|
|
29
319
|
const value = (mapping.optionKey ? options[mapping.optionKey] : void 0) ?? (mapping.camelCaseKey ? sapModelParams?.[mapping.camelCaseKey] : void 0) ?? (mapping.camelCaseKey ? settingsModelParams?.[mapping.camelCaseKey] : void 0);
|
|
@@ -35,23 +325,50 @@ function applyParameterOverrides(modelParams, options, sapModelParams, settingsM
|
|
|
35
325
|
}
|
|
36
326
|
}
|
|
37
327
|
}
|
|
328
|
+
function buildAnthropicCacheMetadata(tokenUsage) {
|
|
329
|
+
const details = tokenUsage?.prompt_tokens_details?.cache_creation_token_details;
|
|
330
|
+
if (!details || (details.ephemeral_5m_input_tokens ?? 0) === 0 && (details.ephemeral_1h_input_tokens ?? 0) === 0) {
|
|
331
|
+
return {};
|
|
332
|
+
}
|
|
333
|
+
return { cacheUsage: details };
|
|
334
|
+
}
|
|
38
335
|
function buildEmbeddingResult(config) {
|
|
39
|
-
const {
|
|
336
|
+
const {
|
|
337
|
+
embeddings,
|
|
338
|
+
modelId,
|
|
339
|
+
providerName,
|
|
340
|
+
requestId,
|
|
341
|
+
responseHeaders,
|
|
342
|
+
totalTokens,
|
|
343
|
+
version,
|
|
344
|
+
warnings
|
|
345
|
+
} = config;
|
|
40
346
|
const providerMetadata = {
|
|
41
347
|
[providerName]: {
|
|
42
348
|
model: modelId,
|
|
349
|
+
...requestId ? { requestId } : {},
|
|
43
350
|
version
|
|
44
351
|
}
|
|
45
352
|
};
|
|
46
353
|
return {
|
|
47
354
|
embeddings,
|
|
48
355
|
providerMetadata,
|
|
356
|
+
...responseHeaders ? { response: { headers: responseHeaders } } : {},
|
|
49
357
|
usage: { tokens: totalTokens },
|
|
50
|
-
warnings: []
|
|
358
|
+
warnings: warnings ? [...warnings] : []
|
|
51
359
|
};
|
|
52
360
|
}
|
|
53
361
|
function buildGenerateResult(config) {
|
|
54
|
-
const {
|
|
362
|
+
const {
|
|
363
|
+
modelId,
|
|
364
|
+
providerName,
|
|
365
|
+
requestBody,
|
|
366
|
+
requestId,
|
|
367
|
+
response,
|
|
368
|
+
responseHeaders,
|
|
369
|
+
version,
|
|
370
|
+
warnings
|
|
371
|
+
} = config;
|
|
55
372
|
const content = extractResponseContent(response);
|
|
56
373
|
const tokenUsage = response.getTokenUsage();
|
|
57
374
|
const finishReasonRaw = response.getFinishReason();
|
|
@@ -82,10 +399,11 @@ function buildGenerateResult(config) {
|
|
|
82
399
|
finishReason,
|
|
83
400
|
providerMetadata: {
|
|
84
401
|
[providerName]: {
|
|
402
|
+
...buildAnthropicCacheMetadata(tokenUsage),
|
|
85
403
|
finishReason: finishReasonRaw ?? "unknown",
|
|
86
404
|
finishReasonMapped: finishReason,
|
|
87
|
-
...intermediateFailures?.length ? { intermediateFailures } : {},
|
|
88
|
-
...
|
|
405
|
+
...intermediateFailures?.length ? { intermediateFailures: sanitizeAsJSONArray(intermediateFailures) } : {},
|
|
406
|
+
...requestId ? { requestId } : {},
|
|
89
407
|
version
|
|
90
408
|
}
|
|
91
409
|
},
|
|
@@ -167,6 +485,15 @@ function buildSAPToolParameters(schema) {
|
|
|
167
485
|
...additionalFields
|
|
168
486
|
};
|
|
169
487
|
}
|
|
488
|
+
function computeNoCache(promptTokens, cachedTokens, cacheWriteTokens) {
|
|
489
|
+
if (promptTokens == null) {
|
|
490
|
+
return void 0;
|
|
491
|
+
}
|
|
492
|
+
if (cachedTokens == null && cacheWriteTokens == null) {
|
|
493
|
+
return promptTokens;
|
|
494
|
+
}
|
|
495
|
+
return Math.max(0, promptTokens - (cachedTokens ?? 0) - (cacheWriteTokens ?? 0));
|
|
496
|
+
}
|
|
170
497
|
function convertResponseFormat(optionsResponseFormat, settingsResponseFormat) {
|
|
171
498
|
let responseFormat;
|
|
172
499
|
let warning;
|
|
@@ -191,18 +518,22 @@ function convertResponseFormat(optionsResponseFormat, settingsResponseFormat) {
|
|
|
191
518
|
}
|
|
192
519
|
return { responseFormat, warning };
|
|
193
520
|
}
|
|
194
|
-
function convertToolsToSAPFormat(tools) {
|
|
521
|
+
function convertToolsToSAPFormat(tools, options = {}) {
|
|
522
|
+
const { parser, warnings: parserWarnings } = options;
|
|
195
523
|
const warnings = [];
|
|
196
524
|
if (!tools || tools.length === 0) {
|
|
197
525
|
return { tools: void 0, warnings };
|
|
198
526
|
}
|
|
199
527
|
const convertedTools = tools.map((tool) => {
|
|
200
528
|
if (tool.type === "function") {
|
|
201
|
-
const
|
|
529
|
+
const functionTool = tool;
|
|
530
|
+
const { parameters, warning } = extractToolParameters(functionTool);
|
|
202
531
|
if (warning) {
|
|
203
532
|
warnings.push(warning);
|
|
204
533
|
}
|
|
534
|
+
const cacheControl = parser?.(functionTool.providerOptions, parserWarnings)?.cacheControl;
|
|
205
535
|
return {
|
|
536
|
+
...cacheControl ? { cache_control: cacheControl } : {},
|
|
206
537
|
function: {
|
|
207
538
|
name: tool.name,
|
|
208
539
|
parameters,
|
|
@@ -241,265 +572,29 @@ function createAISDKRequestBodySummary(options) {
|
|
|
241
572
|
topP: options.topP
|
|
242
573
|
};
|
|
243
574
|
}
|
|
244
|
-
function
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
usage: {
|
|
253
|
-
inputTokens: {
|
|
254
|
-
cacheRead: void 0,
|
|
255
|
-
cacheWrite: void 0,
|
|
256
|
-
noCache: void 0,
|
|
257
|
-
total: void 0
|
|
258
|
-
},
|
|
259
|
-
outputTokens: {
|
|
260
|
-
reasoning: void 0,
|
|
261
|
-
text: void 0,
|
|
262
|
-
total: void 0
|
|
263
|
-
}
|
|
575
|
+
function extractCompletionId(response, dataPath) {
|
|
576
|
+
let cursor = response._data;
|
|
577
|
+
for (const key of dataPath) {
|
|
578
|
+
if (cursor !== null && typeof cursor === "object" && key in cursor) {
|
|
579
|
+
cursor = cursor[key];
|
|
580
|
+
} else {
|
|
581
|
+
cursor = void 0;
|
|
582
|
+
break;
|
|
264
583
|
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
streamResponseGetFinishReason,
|
|
280
|
-
streamResponseGetIntermediateFailures,
|
|
281
|
-
streamResponseGetTokenUsage,
|
|
282
|
-
url,
|
|
283
|
-
version,
|
|
284
|
-
warnings
|
|
285
|
-
} = config;
|
|
286
|
-
let textBlockId = null;
|
|
287
|
-
const streamState = createInitialStreamState();
|
|
288
|
-
const toolCallsInProgress = /* @__PURE__ */ new Map();
|
|
289
|
-
return convertAsyncIteratorToReadableStream(
|
|
290
|
-
safeIterate(sdkStream)[Symbol.asyncIterator]()
|
|
291
|
-
).pipeThrough(
|
|
292
|
-
new TransformStream({
|
|
293
|
-
flush(controller) {
|
|
294
|
-
const toolCalls = Array.from(toolCallsInProgress.values());
|
|
295
|
-
let didEmitAnyToolCalls = false;
|
|
296
|
-
for (const tc of toolCalls) {
|
|
297
|
-
if (tc.didEmitCall) {
|
|
298
|
-
continue;
|
|
299
|
-
}
|
|
300
|
-
if (!tc.didEmitInputStart) {
|
|
301
|
-
tc.didEmitInputStart = true;
|
|
302
|
-
controller.enqueue({
|
|
303
|
-
id: tc.id,
|
|
304
|
-
toolName: tc.toolName ?? "",
|
|
305
|
-
type: "tool-input-start"
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
didEmitAnyToolCalls = true;
|
|
309
|
-
tc.didEmitCall = true;
|
|
310
|
-
controller.enqueue({ id: tc.id, type: "tool-input-end" });
|
|
311
|
-
controller.enqueue({
|
|
312
|
-
input: tc.arguments,
|
|
313
|
-
toolCallId: tc.id,
|
|
314
|
-
toolName: tc.toolName ?? "",
|
|
315
|
-
type: "tool-call"
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
if (streamState.activeText && textBlockId) {
|
|
319
|
-
controller.enqueue({ id: textBlockId, type: "text-end" });
|
|
320
|
-
}
|
|
321
|
-
const finalFinishReason = streamResponseGetFinishReason();
|
|
322
|
-
if (finalFinishReason) {
|
|
323
|
-
streamState.finishReason = mapFinishReason(finalFinishReason);
|
|
324
|
-
} else if (didEmitAnyToolCalls) {
|
|
325
|
-
streamState.finishReason = {
|
|
326
|
-
raw: void 0,
|
|
327
|
-
unified: "tool-calls"
|
|
328
|
-
};
|
|
329
|
-
}
|
|
330
|
-
const finalUsage = streamResponseGetTokenUsage();
|
|
331
|
-
if (finalUsage) {
|
|
332
|
-
const mapped = mapTokenUsage(finalUsage);
|
|
333
|
-
streamState.usage.inputTokens = mapped.inputTokens;
|
|
334
|
-
streamState.usage.outputTokens = mapped.outputTokens;
|
|
335
|
-
}
|
|
336
|
-
const streamCitations = streamResponseGetCitations?.();
|
|
337
|
-
if (streamCitations?.length) {
|
|
338
|
-
for (const citation of streamCitations) {
|
|
339
|
-
controller.enqueue({
|
|
340
|
-
id: String(citation.ref_id ?? citation.url),
|
|
341
|
-
sourceType: "url",
|
|
342
|
-
title: citation.title,
|
|
343
|
-
type: "source",
|
|
344
|
-
url: citation.url
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
const streamIntermediateFailures = streamResponseGetIntermediateFailures?.();
|
|
349
|
-
controller.enqueue({
|
|
350
|
-
finishReason: streamState.finishReason,
|
|
351
|
-
providerMetadata: {
|
|
352
|
-
[providerName]: {
|
|
353
|
-
finishReason: streamState.finishReason.raw,
|
|
354
|
-
...streamIntermediateFailures?.length ? {
|
|
355
|
-
intermediateFailures: streamIntermediateFailures
|
|
356
|
-
} : {},
|
|
357
|
-
...typeof responseHeaders?.["x-request-id"] === "string" ? { requestId: responseHeaders["x-request-id"] } : {},
|
|
358
|
-
responseId,
|
|
359
|
-
version
|
|
360
|
-
}
|
|
361
|
-
},
|
|
362
|
-
type: "finish",
|
|
363
|
-
usage: streamState.usage
|
|
364
|
-
});
|
|
365
|
-
},
|
|
366
|
-
start(controller) {
|
|
367
|
-
controller.enqueue({
|
|
368
|
-
type: "stream-start",
|
|
369
|
-
warnings: [...warnings]
|
|
370
|
-
});
|
|
371
|
-
},
|
|
372
|
-
transform(chunk, controller) {
|
|
373
|
-
if (chunk instanceof Error) {
|
|
374
|
-
const aiError = convertToAISDKError(chunk, {
|
|
375
|
-
operation: "doStream",
|
|
376
|
-
requestBody: createAISDKRequestBodySummary(options),
|
|
377
|
-
url
|
|
378
|
-
});
|
|
379
|
-
controller.enqueue({
|
|
380
|
-
error: aiError instanceof Error ? aiError : new Error(String(aiError)),
|
|
381
|
-
type: "error"
|
|
382
|
-
});
|
|
383
|
-
controller.terminate();
|
|
384
|
-
return;
|
|
385
|
-
}
|
|
386
|
-
if (includeRawChunks) {
|
|
387
|
-
controller.enqueue({
|
|
388
|
-
rawValue: chunk._data ?? chunk,
|
|
389
|
-
type: "raw"
|
|
390
|
-
});
|
|
391
|
-
}
|
|
392
|
-
if (streamState.isFirstChunk) {
|
|
393
|
-
streamState.isFirstChunk = false;
|
|
394
|
-
controller.enqueue({
|
|
395
|
-
id: responseId,
|
|
396
|
-
modelId,
|
|
397
|
-
timestamp: /* @__PURE__ */ new Date(),
|
|
398
|
-
type: "response-metadata"
|
|
399
|
-
});
|
|
400
|
-
}
|
|
401
|
-
const deltaToolCalls = chunk.getDeltaToolCalls();
|
|
402
|
-
if (Array.isArray(deltaToolCalls) && deltaToolCalls.length > 0) {
|
|
403
|
-
streamState.finishReason = {
|
|
404
|
-
raw: void 0,
|
|
405
|
-
unified: "tool-calls"
|
|
406
|
-
};
|
|
407
|
-
}
|
|
408
|
-
const deltaContent = chunk.getDeltaContent();
|
|
409
|
-
if (typeof deltaContent === "string" && deltaContent.length > 0 && streamState.finishReason.unified !== "tool-calls") {
|
|
410
|
-
if (!streamState.activeText) {
|
|
411
|
-
textBlockId = idGenerator.generateTextBlockId();
|
|
412
|
-
controller.enqueue({ id: textBlockId, type: "text-start" });
|
|
413
|
-
streamState.activeText = true;
|
|
414
|
-
}
|
|
415
|
-
if (textBlockId) {
|
|
416
|
-
controller.enqueue({
|
|
417
|
-
delta: deltaContent,
|
|
418
|
-
id: textBlockId,
|
|
419
|
-
type: "text-delta"
|
|
420
|
-
});
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
if (Array.isArray(deltaToolCalls) && deltaToolCalls.length > 0) {
|
|
424
|
-
for (const toolCallChunk of deltaToolCalls) {
|
|
425
|
-
const index = toolCallChunk.index;
|
|
426
|
-
if (typeof index !== "number" || !Number.isFinite(index)) {
|
|
427
|
-
continue;
|
|
428
|
-
}
|
|
429
|
-
if (!toolCallsInProgress.has(index)) {
|
|
430
|
-
toolCallsInProgress.set(index, {
|
|
431
|
-
arguments: "",
|
|
432
|
-
didEmitCall: false,
|
|
433
|
-
didEmitInputStart: false,
|
|
434
|
-
id: toolCallChunk.id ?? `tool_${String(index)}`,
|
|
435
|
-
toolName: toolCallChunk.function?.name
|
|
436
|
-
});
|
|
437
|
-
}
|
|
438
|
-
const tc = toolCallsInProgress.get(index);
|
|
439
|
-
if (!tc) continue;
|
|
440
|
-
if (toolCallChunk.id) {
|
|
441
|
-
tc.id = toolCallChunk.id;
|
|
442
|
-
}
|
|
443
|
-
const nextToolName = toolCallChunk.function?.name;
|
|
444
|
-
if (typeof nextToolName === "string" && nextToolName.length > 0) {
|
|
445
|
-
tc.toolName = nextToolName;
|
|
446
|
-
}
|
|
447
|
-
if (!tc.didEmitInputStart && tc.toolName) {
|
|
448
|
-
tc.didEmitInputStart = true;
|
|
449
|
-
controller.enqueue({
|
|
450
|
-
id: tc.id,
|
|
451
|
-
toolName: tc.toolName,
|
|
452
|
-
type: "tool-input-start"
|
|
453
|
-
});
|
|
454
|
-
}
|
|
455
|
-
const argumentsDelta = toolCallChunk.function?.arguments;
|
|
456
|
-
if (typeof argumentsDelta === "string" && argumentsDelta.length > 0) {
|
|
457
|
-
tc.arguments += argumentsDelta;
|
|
458
|
-
if (tc.didEmitInputStart) {
|
|
459
|
-
controller.enqueue({
|
|
460
|
-
delta: argumentsDelta,
|
|
461
|
-
id: tc.id,
|
|
462
|
-
type: "tool-input-delta"
|
|
463
|
-
});
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
const chunkFinishReason = chunk.getFinishReason();
|
|
469
|
-
if (chunkFinishReason) {
|
|
470
|
-
streamState.finishReason = mapFinishReason(chunkFinishReason);
|
|
471
|
-
if (streamState.finishReason.unified === "tool-calls") {
|
|
472
|
-
const toolCalls = Array.from(toolCallsInProgress.values());
|
|
473
|
-
for (const tc of toolCalls) {
|
|
474
|
-
if (tc.didEmitCall) {
|
|
475
|
-
continue;
|
|
476
|
-
}
|
|
477
|
-
if (!tc.didEmitInputStart) {
|
|
478
|
-
tc.didEmitInputStart = true;
|
|
479
|
-
controller.enqueue({
|
|
480
|
-
id: tc.id,
|
|
481
|
-
toolName: tc.toolName ?? "",
|
|
482
|
-
type: "tool-input-start"
|
|
483
|
-
});
|
|
484
|
-
}
|
|
485
|
-
tc.didEmitCall = true;
|
|
486
|
-
controller.enqueue({ id: tc.id, type: "tool-input-end" });
|
|
487
|
-
controller.enqueue({
|
|
488
|
-
input: tc.arguments,
|
|
489
|
-
toolCallId: tc.id,
|
|
490
|
-
toolName: tc.toolName ?? "",
|
|
491
|
-
type: "tool-call"
|
|
492
|
-
});
|
|
493
|
-
}
|
|
494
|
-
if (streamState.activeText && textBlockId) {
|
|
495
|
-
controller.enqueue({ id: textBlockId, type: "text-end" });
|
|
496
|
-
streamState.activeText = false;
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
})
|
|
502
|
-
);
|
|
584
|
+
}
|
|
585
|
+
if (typeof cursor === "string" && cursor.length > 0) {
|
|
586
|
+
return cursor;
|
|
587
|
+
}
|
|
588
|
+
const fn = response.getRequestId;
|
|
589
|
+
if (typeof fn !== "function") {
|
|
590
|
+
return void 0;
|
|
591
|
+
}
|
|
592
|
+
try {
|
|
593
|
+
const rid = fn.call(response);
|
|
594
|
+
return typeof rid === "string" && rid.length > 0 ? rid : void 0;
|
|
595
|
+
} catch {
|
|
596
|
+
return void 0;
|
|
597
|
+
}
|
|
503
598
|
}
|
|
504
599
|
function extractResponseContent(response) {
|
|
505
600
|
const content = [];
|
|
@@ -523,6 +618,30 @@ function extractResponseContent(response) {
|
|
|
523
618
|
}
|
|
524
619
|
return content;
|
|
525
620
|
}
|
|
621
|
+
function extractResponseMetadata(response, field) {
|
|
622
|
+
let requestId;
|
|
623
|
+
const fn = response?.getRequestId;
|
|
624
|
+
if (typeof fn === "function") {
|
|
625
|
+
try {
|
|
626
|
+
const value = fn.call(response);
|
|
627
|
+
requestId = typeof value === "string" && value.length > 0 ? value : void 0;
|
|
628
|
+
} catch {
|
|
629
|
+
requestId = void 0;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
let rawHeaders;
|
|
633
|
+
try {
|
|
634
|
+
const wrapper = response?.[field];
|
|
635
|
+
rawHeaders = wrapper?.headers;
|
|
636
|
+
} catch {
|
|
637
|
+
rawHeaders = void 0;
|
|
638
|
+
}
|
|
639
|
+
const headers = rawHeaders === void 0 ? void 0 : normalizeHeaders(rawHeaders);
|
|
640
|
+
if (requestId === void 0 && typeof headers?.["x-request-id"] === "string") {
|
|
641
|
+
requestId = headers["x-request-id"];
|
|
642
|
+
}
|
|
643
|
+
return { headers, requestId };
|
|
644
|
+
}
|
|
526
645
|
function extractToolParameters(tool) {
|
|
527
646
|
const inputSchema = tool.inputSchema;
|
|
528
647
|
const toolWithParams = tool;
|
|
@@ -571,6 +690,7 @@ function mapFinishReason(reason) {
|
|
|
571
690
|
}
|
|
572
691
|
switch (reason.toLowerCase()) {
|
|
573
692
|
case "content_filter":
|
|
693
|
+
case "guardrail_intervened":
|
|
574
694
|
return { raw, unified: "content-filter" };
|
|
575
695
|
case "end_turn":
|
|
576
696
|
case "eos":
|
|
@@ -582,6 +702,7 @@ function mapFinishReason(reason) {
|
|
|
582
702
|
case "function_call":
|
|
583
703
|
case "tool_call":
|
|
584
704
|
case "tool_calls":
|
|
705
|
+
case "tool_use":
|
|
585
706
|
return { raw, unified: "tool-calls" };
|
|
586
707
|
case "length":
|
|
587
708
|
case "max_tokens":
|
|
@@ -593,19 +714,25 @@ function mapFinishReason(reason) {
|
|
|
593
714
|
}
|
|
594
715
|
function mapTokenUsage(tokenUsage) {
|
|
595
716
|
const cachedTokens = tokenUsage?.prompt_tokens_details?.cached_tokens;
|
|
717
|
+
const cacheWriteTokens = tokenUsage?.prompt_tokens_details?.cache_creation_tokens;
|
|
596
718
|
const reasoningTokens = tokenUsage?.completion_tokens_details?.reasoning_tokens;
|
|
719
|
+
const promptTokens = tokenUsage?.prompt_tokens;
|
|
720
|
+
const promptDetails = tokenUsage?.prompt_tokens_details;
|
|
721
|
+
const completionDetails = tokenUsage?.completion_tokens_details;
|
|
722
|
+
const hasUnmappedFields = promptDetails?.audio_tokens != null || completionDetails?.audio_tokens != null || completionDetails?.accepted_prediction_tokens != null || completionDetails?.rejected_prediction_tokens != null;
|
|
597
723
|
return {
|
|
598
724
|
inputTokens: {
|
|
599
725
|
cacheRead: cachedTokens,
|
|
600
|
-
cacheWrite:
|
|
601
|
-
noCache:
|
|
602
|
-
total:
|
|
726
|
+
cacheWrite: cacheWriteTokens,
|
|
727
|
+
noCache: computeNoCache(promptTokens, cachedTokens, cacheWriteTokens),
|
|
728
|
+
total: promptTokens
|
|
603
729
|
},
|
|
604
730
|
outputTokens: {
|
|
605
731
|
reasoning: reasoningTokens,
|
|
606
732
|
text: reasoningTokens != null ? (tokenUsage?.completion_tokens ?? 0) - reasoningTokens : tokenUsage?.completion_tokens,
|
|
607
733
|
total: tokenUsage?.completion_tokens
|
|
608
|
-
}
|
|
734
|
+
},
|
|
735
|
+
...hasUnmappedFields && tokenUsage ? { raw: sanitizeAsJSONObject(tokenUsage) } : {}
|
|
609
736
|
};
|
|
610
737
|
}
|
|
611
738
|
function mapToolChoice(toolChoice) {
|
|
@@ -659,16 +786,25 @@ async function prepareEmbeddingCall(config, options) {
|
|
|
659
786
|
}
|
|
660
787
|
return { embeddingOptions: sapOptions, providerName };
|
|
661
788
|
}
|
|
662
|
-
|
|
789
|
+
var jsonReplacer = (_key, value) => typeof value === "bigint" ? value.toString() : value;
|
|
790
|
+
function sanitizeAsJSONArray(value) {
|
|
663
791
|
try {
|
|
664
|
-
|
|
665
|
-
} catch
|
|
666
|
-
|
|
792
|
+
return JSON.parse(JSON.stringify(value, jsonReplacer));
|
|
793
|
+
} catch {
|
|
794
|
+
return [];
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
function sanitizeAsJSONObject(value) {
|
|
798
|
+
try {
|
|
799
|
+
return JSON.parse(JSON.stringify(value, jsonReplacer));
|
|
800
|
+
} catch {
|
|
801
|
+
return {};
|
|
667
802
|
}
|
|
668
803
|
}
|
|
669
804
|
|
|
670
805
|
export {
|
|
671
806
|
StreamIdGenerator,
|
|
807
|
+
createStreamTransformer,
|
|
672
808
|
buildEmbeddingResult,
|
|
673
809
|
buildGenerateResult,
|
|
674
810
|
buildModelDeployment,
|
|
@@ -676,10 +812,11 @@ export {
|
|
|
676
812
|
convertResponseFormat,
|
|
677
813
|
convertToolsToSAPFormat,
|
|
678
814
|
createAISDKRequestBodySummary,
|
|
679
|
-
|
|
815
|
+
extractCompletionId,
|
|
816
|
+
extractResponseMetadata,
|
|
680
817
|
hasKeys,
|
|
681
818
|
mapToolChoice,
|
|
682
819
|
normalizeEmbedding,
|
|
683
820
|
prepareEmbeddingCall
|
|
684
821
|
};
|
|
685
|
-
//# sourceMappingURL=chunk-
|
|
822
|
+
//# sourceMappingURL=chunk-36DFHUVQ.js.map
|