@jerome-benoit/sap-ai-provider 4.6.9 → 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.
Files changed (40) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +1 -1
  3. package/dist/{chunk-4RRRJBEL.js → chunk-36DFHUVQ.js} +407 -275
  4. package/dist/chunk-36DFHUVQ.js.map +1 -0
  5. package/dist/{chunk-EXOXZ5OU.js → chunk-4JIMBRMV.js} +44 -7
  6. package/dist/chunk-4JIMBRMV.js.map +1 -0
  7. package/dist/{chunk-DYFBCE2W.js → chunk-BZZJKLDG.js} +60 -6
  8. package/dist/chunk-BZZJKLDG.js.map +1 -0
  9. package/dist/chunk-GMMX46AH.js +93 -0
  10. package/dist/chunk-GMMX46AH.js.map +1 -0
  11. package/dist/{chunk-F4MUYVQJ.js → chunk-TVXWNZQT.js} +1714 -865
  12. package/dist/chunk-TVXWNZQT.js.map +1 -0
  13. package/dist/chunk-WDUOKIKD.js +188 -0
  14. package/dist/chunk-WDUOKIKD.js.map +1 -0
  15. package/dist/{foundation-models-embedding-model-strategy-3MMM3QDC.js → foundation-models-embedding-model-strategy-YWPDIJEN.js} +8 -4
  16. package/dist/foundation-models-embedding-model-strategy-YWPDIJEN.js.map +1 -0
  17. package/dist/{foundation-models-language-model-strategy-OPGTN6DA.js → foundation-models-language-model-strategy-WDSQWU64.js} +15 -10
  18. package/dist/foundation-models-language-model-strategy-WDSQWU64.js.map +1 -0
  19. package/dist/index.cjs +2620 -1503
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.d.cts +66 -66
  22. package/dist/index.d.ts +66 -66
  23. package/dist/index.js +11 -172
  24. package/dist/index.js.map +1 -1
  25. package/dist/{orchestration-embedding-model-strategy-R6G6VVU6.js → orchestration-embedding-model-strategy-66QYAO64.js} +14 -4
  26. package/dist/orchestration-embedding-model-strategy-66QYAO64.js.map +1 -0
  27. package/dist/{orchestration-language-model-strategy-KFW3CFRY.js → orchestration-language-model-strategy-4TKRZK7U.js} +52 -77
  28. package/dist/orchestration-language-model-strategy-4TKRZK7U.js.map +1 -0
  29. package/package.json +20 -14
  30. package/LICENSE.md +0 -187
  31. package/dist/chunk-4RRRJBEL.js.map +0 -1
  32. package/dist/chunk-DYFBCE2W.js.map +0 -1
  33. package/dist/chunk-EXOXZ5OU.js.map +0 -1
  34. package/dist/chunk-F4MUYVQJ.js.map +0 -1
  35. package/dist/chunk-NQJIUL3F.js +0 -61
  36. package/dist/chunk-NQJIUL3F.js.map +0 -1
  37. package/dist/foundation-models-embedding-model-strategy-3MMM3QDC.js.map +0 -1
  38. package/dist/foundation-models-language-model-strategy-OPGTN6DA.js.map +0 -1
  39. package/dist/orchestration-embedding-model-strategy-R6G6VVU6.js.map +0 -1
  40. package/dist/orchestration-language-model-strategy-KFW3CFRY.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-F4MUYVQJ.js";
8
+ } from "./chunk-TVXWNZQT.js";
8
9
 
9
10
  // src/strategy-utils.ts
10
11
  import { TooManyEmbeddingValuesForCallError } from "@ai-sdk/provider";
11
- import { convertAsyncIteratorToReadableStream, parseProviderOptions } from "@ai-sdk/provider-utils";
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.
@@ -30,6 +34,286 @@ var StreamIdGenerator = class {
30
34
  return crypto.randomUUID();
31
35
  }
32
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
33
317
  function applyParameterOverrides(modelParams, options, sapModelParams, settingsModelParams, mappings) {
34
318
  for (const mapping of mappings) {
35
319
  const value = (mapping.optionKey ? options[mapping.optionKey] : void 0) ?? (mapping.camelCaseKey ? sapModelParams?.[mapping.camelCaseKey] : void 0) ?? (mapping.camelCaseKey ? settingsModelParams?.[mapping.camelCaseKey] : void 0);
@@ -41,23 +325,50 @@ function applyParameterOverrides(modelParams, options, sapModelParams, settingsM
41
325
  }
42
326
  }
43
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
+ }
44
335
  function buildEmbeddingResult(config) {
45
- const { embeddings, modelId, providerName, totalTokens, version } = config;
336
+ const {
337
+ embeddings,
338
+ modelId,
339
+ providerName,
340
+ requestId,
341
+ responseHeaders,
342
+ totalTokens,
343
+ version,
344
+ warnings
345
+ } = config;
46
346
  const providerMetadata = {
47
347
  [providerName]: {
48
348
  model: modelId,
349
+ ...requestId ? { requestId } : {},
49
350
  version
50
351
  }
51
352
  };
52
353
  return {
53
354
  embeddings,
54
355
  providerMetadata,
356
+ ...responseHeaders ? { response: { headers: responseHeaders } } : {},
55
357
  usage: { tokens: totalTokens },
56
- warnings: []
358
+ warnings: warnings ? [...warnings] : []
57
359
  };
58
360
  }
59
361
  function buildGenerateResult(config) {
60
- const { modelId, providerName, requestBody, response, responseHeaders, version, warnings } = config;
362
+ const {
363
+ modelId,
364
+ providerName,
365
+ requestBody,
366
+ requestId,
367
+ response,
368
+ responseHeaders,
369
+ version,
370
+ warnings
371
+ } = config;
61
372
  const content = extractResponseContent(response);
62
373
  const tokenUsage = response.getTokenUsage();
63
374
  const finishReasonRaw = response.getFinishReason();
@@ -88,10 +399,11 @@ function buildGenerateResult(config) {
88
399
  finishReason,
89
400
  providerMetadata: {
90
401
  [providerName]: {
402
+ ...buildAnthropicCacheMetadata(tokenUsage),
91
403
  finishReason: finishReasonRaw ?? "unknown",
92
404
  finishReasonMapped: finishReason,
93
- ...intermediateFailures?.length ? { intermediateFailures } : {},
94
- ...typeof responseHeaders?.["x-request-id"] === "string" ? { requestId: responseHeaders["x-request-id"] } : {},
405
+ ...intermediateFailures?.length ? { intermediateFailures: sanitizeAsJSONArray(intermediateFailures) } : {},
406
+ ...requestId ? { requestId } : {},
95
407
  version
96
408
  }
97
409
  },
@@ -173,6 +485,15 @@ function buildSAPToolParameters(schema) {
173
485
  ...additionalFields
174
486
  };
175
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
+ }
176
497
  function convertResponseFormat(optionsResponseFormat, settingsResponseFormat) {
177
498
  let responseFormat;
178
499
  let warning;
@@ -197,18 +518,22 @@ function convertResponseFormat(optionsResponseFormat, settingsResponseFormat) {
197
518
  }
198
519
  return { responseFormat, warning };
199
520
  }
200
- function convertToolsToSAPFormat(tools) {
521
+ function convertToolsToSAPFormat(tools, options = {}) {
522
+ const { parser, warnings: parserWarnings } = options;
201
523
  const warnings = [];
202
524
  if (!tools || tools.length === 0) {
203
525
  return { tools: void 0, warnings };
204
526
  }
205
527
  const convertedTools = tools.map((tool) => {
206
528
  if (tool.type === "function") {
207
- const { parameters, warning } = extractToolParameters(tool);
529
+ const functionTool = tool;
530
+ const { parameters, warning } = extractToolParameters(functionTool);
208
531
  if (warning) {
209
532
  warnings.push(warning);
210
533
  }
534
+ const cacheControl = parser?.(functionTool.providerOptions, parserWarnings)?.cacheControl;
211
535
  return {
536
+ ...cacheControl ? { cache_control: cacheControl } : {},
212
537
  function: {
213
538
  name: tool.name,
214
539
  parameters,
@@ -247,264 +572,29 @@ function createAISDKRequestBodySummary(options) {
247
572
  topP: options.topP
248
573
  };
249
574
  }
250
- function createInitialStreamState() {
251
- return {
252
- activeText: false,
253
- finishReason: {
254
- raw: void 0,
255
- unified: "other"
256
- },
257
- isFirstChunk: true,
258
- usage: {
259
- inputTokens: {
260
- cacheRead: void 0,
261
- cacheWrite: void 0,
262
- noCache: void 0,
263
- total: void 0
264
- },
265
- outputTokens: {
266
- reasoning: void 0,
267
- text: void 0,
268
- total: void 0
269
- }
270
- }
271
- };
272
- }
273
- function createStreamTransformer(config) {
274
- const {
275
- convertToAISDKError,
276
- idGenerator,
277
- includeRawChunks,
278
- modelId,
279
- options,
280
- providerName,
281
- responseHeaders,
282
- responseId,
283
- sdkStream,
284
- streamResponseGetCitations,
285
- streamResponseGetFinishReason,
286
- streamResponseGetIntermediateFailures,
287
- streamResponseGetTokenUsage,
288
- url,
289
- version,
290
- warnings
291
- } = config;
292
- let textBlockId = null;
293
- const streamState = createInitialStreamState();
294
- const toolCallsInProgress = /* @__PURE__ */ new Map();
295
- function emitToolInputStart(tc, controller) {
296
- if (tc.didEmitInputStart || tc.id == null) return;
297
- tc.didEmitInputStart = true;
298
- controller.enqueue({
299
- id: tc.id,
300
- toolName: tc.toolName ?? "",
301
- type: "tool-input-start"
302
- });
303
- if (tc.arguments.length > 0) {
304
- controller.enqueue({
305
- delta: tc.arguments,
306
- id: tc.id,
307
- type: "tool-input-delta"
308
- });
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;
309
583
  }
310
584
  }
311
- return convertAsyncIteratorToReadableStream(
312
- safeIterate(sdkStream)[Symbol.asyncIterator]()
313
- ).pipeThrough(
314
- new TransformStream({
315
- flush(controller) {
316
- const toolCalls = Array.from(toolCallsInProgress.values());
317
- let didEmitAnyToolCalls = false;
318
- for (const tc of toolCalls) {
319
- if (tc.didEmitCall) {
320
- continue;
321
- }
322
- tc.id ??= idGenerator.generateToolCallId();
323
- emitToolInputStart(tc, controller);
324
- didEmitAnyToolCalls = true;
325
- tc.didEmitCall = true;
326
- controller.enqueue({ id: tc.id, type: "tool-input-end" });
327
- controller.enqueue({
328
- input: tc.arguments,
329
- toolCallId: tc.id,
330
- toolName: tc.toolName ?? "",
331
- type: "tool-call"
332
- });
333
- }
334
- if (streamState.activeText && textBlockId) {
335
- controller.enqueue({ id: textBlockId, type: "text-end" });
336
- }
337
- const finalFinishReason = streamResponseGetFinishReason();
338
- if (finalFinishReason) {
339
- streamState.finishReason = mapFinishReason(finalFinishReason);
340
- } else if (didEmitAnyToolCalls) {
341
- streamState.finishReason = {
342
- raw: void 0,
343
- unified: "tool-calls"
344
- };
345
- }
346
- const finalUsage = streamResponseGetTokenUsage();
347
- if (finalUsage) {
348
- const mapped = mapTokenUsage(finalUsage);
349
- streamState.usage.inputTokens = mapped.inputTokens;
350
- streamState.usage.outputTokens = mapped.outputTokens;
351
- }
352
- const streamCitations = streamResponseGetCitations?.();
353
- if (streamCitations?.length) {
354
- for (const citation of streamCitations) {
355
- controller.enqueue({
356
- id: String(citation.ref_id ?? citation.url),
357
- sourceType: "url",
358
- title: citation.title,
359
- type: "source",
360
- url: citation.url
361
- });
362
- }
363
- }
364
- const streamIntermediateFailures = streamResponseGetIntermediateFailures?.();
365
- controller.enqueue({
366
- finishReason: streamState.finishReason,
367
- providerMetadata: {
368
- [providerName]: {
369
- finishReason: streamState.finishReason.raw,
370
- ...streamIntermediateFailures?.length ? {
371
- intermediateFailures: streamIntermediateFailures
372
- } : {},
373
- ...typeof responseHeaders?.["x-request-id"] === "string" ? { requestId: responseHeaders["x-request-id"] } : {},
374
- responseId,
375
- version
376
- }
377
- },
378
- type: "finish",
379
- usage: streamState.usage
380
- });
381
- },
382
- start(controller) {
383
- controller.enqueue({
384
- type: "stream-start",
385
- warnings: [...warnings]
386
- });
387
- },
388
- transform(chunk, controller) {
389
- if (chunk instanceof Error) {
390
- const aiError = convertToAISDKError(chunk, {
391
- operation: "doStream",
392
- requestBody: createAISDKRequestBodySummary(options),
393
- url
394
- });
395
- controller.enqueue({
396
- error: aiError instanceof Error ? aiError : new Error(String(aiError)),
397
- type: "error"
398
- });
399
- controller.terminate();
400
- return;
401
- }
402
- if (includeRawChunks) {
403
- controller.enqueue({
404
- rawValue: chunk._data ?? chunk,
405
- type: "raw"
406
- });
407
- }
408
- if (streamState.isFirstChunk) {
409
- streamState.isFirstChunk = false;
410
- controller.enqueue({
411
- id: responseId,
412
- modelId,
413
- timestamp: /* @__PURE__ */ new Date(),
414
- type: "response-metadata"
415
- });
416
- }
417
- const deltaToolCalls = chunk.getDeltaToolCalls();
418
- if (Array.isArray(deltaToolCalls) && deltaToolCalls.length > 0) {
419
- streamState.finishReason = {
420
- raw: void 0,
421
- unified: "tool-calls"
422
- };
423
- }
424
- const deltaContent = chunk.getDeltaContent();
425
- if (typeof deltaContent === "string" && deltaContent.length > 0 && streamState.finishReason.unified !== "tool-calls") {
426
- if (!streamState.activeText) {
427
- textBlockId = idGenerator.generateTextBlockId();
428
- controller.enqueue({ id: textBlockId, type: "text-start" });
429
- streamState.activeText = true;
430
- }
431
- if (textBlockId) {
432
- controller.enqueue({
433
- delta: deltaContent,
434
- id: textBlockId,
435
- type: "text-delta"
436
- });
437
- }
438
- }
439
- if (Array.isArray(deltaToolCalls) && deltaToolCalls.length > 0) {
440
- for (const toolCallChunk of deltaToolCalls) {
441
- const index = toolCallChunk.index;
442
- if (typeof index !== "number" || !Number.isFinite(index)) {
443
- continue;
444
- }
445
- if (!toolCallsInProgress.has(index)) {
446
- toolCallsInProgress.set(index, {
447
- arguments: "",
448
- didEmitCall: false,
449
- didEmitInputStart: false,
450
- id: typeof toolCallChunk.id === "string" && toolCallChunk.id.length > 0 ? toolCallChunk.id : void 0,
451
- toolName: toolCallChunk.function?.name
452
- });
453
- }
454
- const tc = toolCallsInProgress.get(index);
455
- if (!tc) continue;
456
- if (typeof toolCallChunk.id === "string" && toolCallChunk.id.length > 0 && tc.id === void 0) {
457
- tc.id = toolCallChunk.id;
458
- }
459
- const nextToolName = toolCallChunk.function?.name;
460
- if (typeof nextToolName === "string" && nextToolName.length > 0) {
461
- tc.toolName = nextToolName;
462
- }
463
- if (!tc.didEmitInputStart && tc.toolName != null && tc.id != null) {
464
- emitToolInputStart(tc, controller);
465
- }
466
- const argumentsDelta = toolCallChunk.function?.arguments;
467
- if (typeof argumentsDelta === "string" && argumentsDelta.length > 0) {
468
- tc.arguments += argumentsDelta;
469
- if (tc.didEmitInputStart && tc.id != null) {
470
- controller.enqueue({
471
- delta: argumentsDelta,
472
- id: tc.id,
473
- type: "tool-input-delta"
474
- });
475
- }
476
- }
477
- }
478
- }
479
- const chunkFinishReason = chunk.getFinishReason();
480
- if (chunkFinishReason) {
481
- streamState.finishReason = mapFinishReason(chunkFinishReason);
482
- if (streamState.finishReason.unified === "tool-calls") {
483
- const toolCalls = Array.from(toolCallsInProgress.values());
484
- for (const tc of toolCalls) {
485
- if (tc.didEmitCall) {
486
- continue;
487
- }
488
- tc.id ??= idGenerator.generateToolCallId();
489
- emitToolInputStart(tc, controller);
490
- tc.didEmitCall = true;
491
- controller.enqueue({ id: tc.id, type: "tool-input-end" });
492
- controller.enqueue({
493
- input: tc.arguments,
494
- toolCallId: tc.id,
495
- toolName: tc.toolName ?? "",
496
- type: "tool-call"
497
- });
498
- }
499
- if (streamState.activeText && textBlockId) {
500
- controller.enqueue({ id: textBlockId, type: "text-end" });
501
- streamState.activeText = false;
502
- }
503
- }
504
- }
505
- }
506
- })
507
- );
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
+ }
508
598
  }
509
599
  function extractResponseContent(response) {
510
600
  const content = [];
@@ -528,6 +618,30 @@ function extractResponseContent(response) {
528
618
  }
529
619
  return content;
530
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
+ }
531
645
  function extractToolParameters(tool) {
532
646
  const inputSchema = tool.inputSchema;
533
647
  const toolWithParams = tool;
@@ -576,6 +690,7 @@ function mapFinishReason(reason) {
576
690
  }
577
691
  switch (reason.toLowerCase()) {
578
692
  case "content_filter":
693
+ case "guardrail_intervened":
579
694
  return { raw, unified: "content-filter" };
580
695
  case "end_turn":
581
696
  case "eos":
@@ -587,6 +702,7 @@ function mapFinishReason(reason) {
587
702
  case "function_call":
588
703
  case "tool_call":
589
704
  case "tool_calls":
705
+ case "tool_use":
590
706
  return { raw, unified: "tool-calls" };
591
707
  case "length":
592
708
  case "max_tokens":
@@ -598,19 +714,25 @@ function mapFinishReason(reason) {
598
714
  }
599
715
  function mapTokenUsage(tokenUsage) {
600
716
  const cachedTokens = tokenUsage?.prompt_tokens_details?.cached_tokens;
717
+ const cacheWriteTokens = tokenUsage?.prompt_tokens_details?.cache_creation_tokens;
601
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;
602
723
  return {
603
724
  inputTokens: {
604
725
  cacheRead: cachedTokens,
605
- cacheWrite: void 0,
606
- noCache: cachedTokens != null ? (tokenUsage?.prompt_tokens ?? 0) - cachedTokens : tokenUsage?.prompt_tokens,
607
- total: tokenUsage?.prompt_tokens
726
+ cacheWrite: cacheWriteTokens,
727
+ noCache: computeNoCache(promptTokens, cachedTokens, cacheWriteTokens),
728
+ total: promptTokens
608
729
  },
609
730
  outputTokens: {
610
731
  reasoning: reasoningTokens,
611
732
  text: reasoningTokens != null ? (tokenUsage?.completion_tokens ?? 0) - reasoningTokens : tokenUsage?.completion_tokens,
612
733
  total: tokenUsage?.completion_tokens
613
- }
734
+ },
735
+ ...hasUnmappedFields && tokenUsage ? { raw: sanitizeAsJSONObject(tokenUsage) } : {}
614
736
  };
615
737
  }
616
738
  function mapToolChoice(toolChoice) {
@@ -664,16 +786,25 @@ async function prepareEmbeddingCall(config, options) {
664
786
  }
665
787
  return { embeddingOptions: sapOptions, providerName };
666
788
  }
667
- async function* safeIterate(iterable) {
789
+ var jsonReplacer = (_key, value) => typeof value === "bigint" ? value.toString() : value;
790
+ function sanitizeAsJSONArray(value) {
668
791
  try {
669
- yield* iterable;
670
- } catch (error) {
671
- yield error instanceof Error ? error : new Error(String(error));
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 {};
672
802
  }
673
803
  }
674
804
 
675
805
  export {
676
806
  StreamIdGenerator,
807
+ createStreamTransformer,
677
808
  buildEmbeddingResult,
678
809
  buildGenerateResult,
679
810
  buildModelDeployment,
@@ -681,10 +812,11 @@ export {
681
812
  convertResponseFormat,
682
813
  convertToolsToSAPFormat,
683
814
  createAISDKRequestBodySummary,
684
- createStreamTransformer,
815
+ extractCompletionId,
816
+ extractResponseMetadata,
685
817
  hasKeys,
686
818
  mapToolChoice,
687
819
  normalizeEmbedding,
688
820
  prepareEmbeddingCall
689
821
  };
690
- //# sourceMappingURL=chunk-4RRRJBEL.js.map
822
+ //# sourceMappingURL=chunk-36DFHUVQ.js.map