@jerome-benoit/sap-ai-provider-v2 5.0.3 → 5.1.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 (44) hide show
  1. package/README.md +44 -24
  2. package/dist/chunk-4ILJD6PI.js +333 -0
  3. package/dist/chunk-4ILJD6PI.js.map +1 -0
  4. package/dist/chunk-BHA5MIN6.js +224 -0
  5. package/dist/chunk-BHA5MIN6.js.map +1 -0
  6. package/dist/{chunk-4JLNWASF.js → chunk-DLGBK3C3.js} +91 -7
  7. package/dist/chunk-DLGBK3C3.js.map +1 -0
  8. package/dist/{chunk-E76ZDPIW.js → chunk-EUBI5ENK.js} +54 -27
  9. package/dist/chunk-EUBI5ENK.js.map +1 -0
  10. package/dist/{chunk-YP3AADWD.js → chunk-FKHOJ3ZC.js} +24 -3
  11. package/dist/chunk-FKHOJ3ZC.js.map +1 -0
  12. package/dist/{chunk-UIBQ5AFS.js → chunk-QFMHNGLB.js} +56 -218
  13. package/dist/chunk-QFMHNGLB.js.map +1 -0
  14. package/dist/{chunk-3H5C5U22.js → chunk-QQQYMQPU.js} +19 -10
  15. package/dist/chunk-QQQYMQPU.js.map +1 -0
  16. package/dist/{chunk-NF355OFT.js → chunk-ZOBD7PXJ.js} +116 -21
  17. package/dist/chunk-ZOBD7PXJ.js.map +1 -0
  18. package/dist/{foundation-models-embedding-model-strategy-SAVQFBF4.js → foundation-models-embedding-model-strategy-3WAKJXBY.js} +5 -4
  19. package/dist/{foundation-models-embedding-model-strategy-SAVQFBF4.js.map → foundation-models-embedding-model-strategy-3WAKJXBY.js.map} +1 -1
  20. package/dist/{foundation-models-language-model-strategy-ZDOWVYTA.js → foundation-models-language-model-strategy-PJ26VXEA.js} +21 -8
  21. package/dist/foundation-models-language-model-strategy-PJ26VXEA.js.map +1 -0
  22. package/dist/index.cjs +2008 -497
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.cts +798 -185
  25. package/dist/index.d.ts +798 -185
  26. package/dist/index.js +148 -24
  27. package/dist/index.js.map +1 -1
  28. package/dist/{orchestration-embedding-model-strategy-GT27AFII.js → orchestration-embedding-model-strategy-AZ76XLMD.js} +6 -5
  29. package/dist/{orchestration-embedding-model-strategy-GT27AFII.js.map → orchestration-embedding-model-strategy-AZ76XLMD.js.map} +1 -1
  30. package/dist/{orchestration-language-model-strategy-7QTPQHCC.js → orchestration-language-model-strategy-CT6RHCYO.js} +48 -17
  31. package/dist/orchestration-language-model-strategy-CT6RHCYO.js.map +1 -0
  32. package/dist/responses-language-model-strategy-UYTV6TPM.js +738 -0
  33. package/dist/responses-language-model-strategy-UYTV6TPM.js.map +1 -0
  34. package/dist/sap-ai-error-UPKYWPSS.js +16 -0
  35. package/dist/sap-ai-error-UPKYWPSS.js.map +1 -0
  36. package/package.json +79 -39
  37. package/dist/chunk-3H5C5U22.js.map +0 -1
  38. package/dist/chunk-4JLNWASF.js.map +0 -1
  39. package/dist/chunk-E76ZDPIW.js.map +0 -1
  40. package/dist/chunk-NF355OFT.js.map +0 -1
  41. package/dist/chunk-UIBQ5AFS.js.map +0 -1
  42. package/dist/chunk-YP3AADWD.js.map +0 -1
  43. package/dist/foundation-models-language-model-strategy-ZDOWVYTA.js.map +0 -1
  44. package/dist/orchestration-language-model-strategy-7QTPQHCC.js.map +0 -1
package/dist/index.js CHANGED
@@ -3,22 +3,70 @@ import {
3
3
  mergeSettingsWithApi,
4
4
  resolveApi,
5
5
  validateSettings
6
- } from "./chunk-E76ZDPIW.js";
6
+ } from "./chunk-EUBI5ENK.js";
7
7
  import {
8
8
  escapeOrchestrationPlaceholders,
9
9
  unescapeOrchestrationPlaceholders
10
- } from "./chunk-YP3AADWD.js";
10
+ } from "./chunk-FKHOJ3ZC.js";
11
11
  import {
12
- ApiSwitchError,
13
12
  SAP_AI_PROVIDER_NAME,
14
- UnsupportedFeatureError,
15
13
  VERSION,
16
14
  getProviderName,
17
15
  sapAIEmbeddingProviderOptions,
18
16
  sapAILanguageModelProviderOptions,
19
17
  validateEmbeddingModelParamsSettings,
20
18
  validateModelParamsSettings
21
- } from "./chunk-UIBQ5AFS.js";
19
+ } from "./chunk-BHA5MIN6.js";
20
+ import {
21
+ ApiSwitchError,
22
+ UnsupportedFeatureError
23
+ } from "./chunk-QFMHNGLB.js";
24
+
25
+ // src/sap-ai-adapters-v3-to-v2.ts
26
+ import { UnsupportedFunctionalityError } from "@ai-sdk/provider";
27
+
28
+ // src/map-readable-stream.ts
29
+ function mapReadableStream(source, convert) {
30
+ const reader = source.getReader();
31
+ let cancelled = false;
32
+ return new ReadableStream(
33
+ {
34
+ async cancel(reason) {
35
+ cancelled = true;
36
+ try {
37
+ await reader.cancel(reason);
38
+ } finally {
39
+ reader.releaseLock();
40
+ }
41
+ },
42
+ async pull(controller) {
43
+ try {
44
+ for (; ; ) {
45
+ const next = await reader.read();
46
+ if (cancelled) return;
47
+ if (next.done) {
48
+ reader.releaseLock();
49
+ controller.close();
50
+ return;
51
+ }
52
+ const converted = convert(next.value);
53
+ if (converted !== null) {
54
+ controller.enqueue(converted);
55
+ return;
56
+ }
57
+ }
58
+ } catch (error) {
59
+ if (cancelled) return;
60
+ await reader.cancel(error).catch(() => void 0);
61
+ if (cancelled) return;
62
+ reader.releaseLock();
63
+ controller.error(error);
64
+ }
65
+ }
66
+ },
67
+ { highWaterMark: 0 }
68
+ );
69
+ }
22
70
 
23
71
  // src/sap-ai-adapters-v3-to-v2.ts
24
72
  function convertStreamPartToV2(internalPart) {
@@ -37,7 +85,7 @@ function convertStreamPartToV2(internalPart) {
37
85
  case "stream-start":
38
86
  return { ...compatiblePart, warnings: convertWarningsToV2(compatiblePart.warnings) };
39
87
  case "tool-approval-request":
40
- return null;
88
+ throw createV2ToolApprovalError();
41
89
  case "tool-call": {
42
90
  const { dynamic: _dynamic, ...part } = compatiblePart;
43
91
  return part;
@@ -75,14 +123,12 @@ function convertWarningToV2(internalWarning) {
75
123
  };
76
124
  }
77
125
  function createV2StreamFromInternal(internalStream) {
78
- return internalStream.pipeThrough(
79
- new TransformStream({
80
- transform(chunk, controller) {
81
- const converted = convertStreamPartToV2(chunk);
82
- if (converted !== null) controller.enqueue(converted);
83
- }
84
- })
85
- );
126
+ return mapReadableStream(internalStream, convertStreamPartToV2);
127
+ }
128
+ function createV2ToolApprovalError() {
129
+ return new UnsupportedFunctionalityError({
130
+ functionality: "Tool approvals are unavailable through the V2 provider. Use /v3 with AI SDK 6 or /v4 with AI SDK 7."
131
+ });
86
132
  }
87
133
 
88
134
  // src/sap-ai-embedding-model.ts
@@ -117,23 +163,31 @@ function getOrCreateLanguageModelStrategy(api) {
117
163
  return strategyPromise;
118
164
  }
119
165
  async function createEmbeddingModelStrategy(api) {
166
+ if (api === "responses") {
167
+ const { UnsupportedFeatureError: UnsupportedFeatureError2 } = await import("./sap-ai-error-UPKYWPSS.js");
168
+ throw new UnsupportedFeatureError2("Embeddings", "responses", "foundation-models");
169
+ }
120
170
  if (api === "foundation-models") {
121
171
  const { AzureOpenAiEmbeddingClient } = await import("@sap-ai-sdk/foundation-models");
122
- const { FoundationModelsEmbeddingModelStrategy } = await import("./foundation-models-embedding-model-strategy-SAVQFBF4.js");
172
+ const { FoundationModelsEmbeddingModelStrategy } = await import("./foundation-models-embedding-model-strategy-3WAKJXBY.js");
123
173
  return new FoundationModelsEmbeddingModelStrategy(AzureOpenAiEmbeddingClient);
124
174
  }
125
175
  const { OrchestrationEmbeddingClient: OrchestrationEmbeddingClient2 } = await import("@sap-ai-sdk/orchestration");
126
- const { OrchestrationEmbeddingModelStrategy } = await import("./orchestration-embedding-model-strategy-GT27AFII.js");
176
+ const { OrchestrationEmbeddingModelStrategy } = await import("./orchestration-embedding-model-strategy-AZ76XLMD.js");
127
177
  return new OrchestrationEmbeddingModelStrategy(OrchestrationEmbeddingClient2);
128
178
  }
129
179
  async function createLanguageModelStrategy(api) {
180
+ if (api === "responses") {
181
+ const { ResponsesLanguageModelStrategy } = await import("./responses-language-model-strategy-UYTV6TPM.js");
182
+ return new ResponsesLanguageModelStrategy();
183
+ }
130
184
  if (api === "foundation-models") {
131
185
  const { AzureOpenAiChatClient } = await import("@sap-ai-sdk/foundation-models");
132
- const { FoundationModelsLanguageModelStrategy } = await import("./foundation-models-language-model-strategy-ZDOWVYTA.js");
186
+ const { FoundationModelsLanguageModelStrategy } = await import("./foundation-models-language-model-strategy-PJ26VXEA.js");
133
187
  return new FoundationModelsLanguageModelStrategy(AzureOpenAiChatClient);
134
188
  }
135
189
  const { OrchestrationClient: OrchestrationClient2 } = await import("@sap-ai-sdk/orchestration");
136
- const { OrchestrationLanguageModelStrategy } = await import("./orchestration-language-model-strategy-7QTPQHCC.js");
190
+ const { OrchestrationLanguageModelStrategy } = await import("./orchestration-language-model-strategy-CT6RHCYO.js");
137
191
  return new OrchestrationLanguageModelStrategy(OrchestrationClient2);
138
192
  }
139
193
 
@@ -189,7 +243,12 @@ var SAPAIEmbeddingModel = class {
189
243
  provider: this.config.provider,
190
244
  requestConfig: this.config.requestConfig
191
245
  };
192
- return strategy.doEmbed(strategyConfig, this.settings, options, this.maxEmbeddingsPerCall);
246
+ return strategy.doEmbed(
247
+ strategyConfig,
248
+ this.settings,
249
+ options,
250
+ this.maxEmbeddingsPerCall
251
+ );
193
252
  }
194
253
  };
195
254
 
@@ -241,6 +300,55 @@ var SAPAIEmbeddingModelV2 = class {
241
300
  }
242
301
  };
243
302
 
303
+ // src/sap-ai-adapters-v2-to-v3.ts
304
+ import { resolveFullMediaType } from "@ai-sdk/provider-utils";
305
+ function normalizeV2PromptToV3(prompt) {
306
+ return prompt.map((message) => {
307
+ switch (message.role) {
308
+ case "assistant":
309
+ return {
310
+ ...message,
311
+ content: message.content.map(
312
+ (part) => part.type === "tool-result" ? normalizeToolResultPart(part) : part
313
+ )
314
+ };
315
+ case "tool":
316
+ return {
317
+ ...message,
318
+ content: message.content.map(
319
+ (part) => part.type === "tool-result" ? normalizeToolResultPart(part) : part
320
+ )
321
+ };
322
+ default:
323
+ return message;
324
+ }
325
+ });
326
+ }
327
+ function normalizeToolOutput(output) {
328
+ if (output.type !== "content") return output;
329
+ return {
330
+ ...output,
331
+ value: output.value.map((item) => {
332
+ if (item.type !== "media") return item;
333
+ const mediaType = resolveFullMediaType({
334
+ part: {
335
+ data: { data: item.data, type: "data" },
336
+ mediaType: item.mediaType.toLowerCase(),
337
+ type: "file"
338
+ }
339
+ });
340
+ return {
341
+ ...item,
342
+ mediaType,
343
+ type: mediaType.startsWith("image/") ? "image-data" : "file-data"
344
+ };
345
+ })
346
+ };
347
+ }
348
+ function normalizeToolResultPart(part) {
349
+ return { ...part, output: normalizeToolOutput(part.output) };
350
+ }
351
+
244
352
  // src/sap-ai-language-model.ts
245
353
  import { parseProviderOptions as parseProviderOptions2 } from "@ai-sdk/provider-utils";
246
354
  var SAPAILanguageModel = class {
@@ -274,11 +382,13 @@ var SAPAILanguageModel = class {
274
382
  }
275
383
  async doGenerate(options) {
276
384
  const { strategy, strategyConfig } = await this.prepareInvocation(options);
277
- return strategy.doGenerate(strategyConfig, this.settings, options);
385
+ const result = strategy.doGenerate(strategyConfig, this.settings, options);
386
+ return result;
278
387
  }
279
388
  async doStream(options) {
280
389
  const { strategy, strategyConfig } = await this.prepareInvocation(options);
281
- return strategy.doStream(strategyConfig, this.settings, options);
390
+ const result = strategy.doStream(strategyConfig, this.settings, options);
391
+ return result;
282
392
  }
283
393
  supportsUrl(url) {
284
394
  if (url.protocol === "https:") return true;
@@ -305,10 +415,12 @@ var SAPAILanguageModel = class {
305
415
  api: effectiveApi,
306
416
  invocationSettings: sapOptions ? {
307
417
  api: sapOptions.api,
418
+ dropUnsupportedParams: sapOptions.dropUnsupportedParams,
308
419
  escapeTemplatePlaceholders: sapOptions.escapeTemplatePlaceholders,
309
420
  orchestrationConfigRef: sapOptions.orchestrationConfigRef,
310
421
  placeholderValues: sapOptions.placeholderValues,
311
- promptTemplateRef: sapOptions.promptTemplateRef
422
+ promptTemplateRef: sapOptions.promptTemplateRef,
423
+ responses: sapOptions.responses
312
424
  } : void 0,
313
425
  modelApi: this.settings.api,
314
426
  modelSettings: this.settings
@@ -349,7 +461,10 @@ var SAPAILanguageModelV2 = class {
349
461
  this.internalModel = new SAPAILanguageModel(modelId, settings, config);
350
462
  }
351
463
  async doGenerate(options) {
352
- const result = await this.internalModel.doGenerate(options);
464
+ const result = await this.internalModel.doGenerate(normalizeCallOptions(options));
465
+ if (result.content.some((part) => part.type === "tool-approval-request")) {
466
+ throw createV2ToolApprovalError();
467
+ }
353
468
  return {
354
469
  content: result.content,
355
470
  finishReason: result.finishReason.unified,
@@ -361,7 +476,7 @@ var SAPAILanguageModelV2 = class {
361
476
  };
362
477
  }
363
478
  async doStream(options) {
364
- const result = await this.internalModel.doStream(options);
479
+ const result = await this.internalModel.doStream(normalizeCallOptions(options));
365
480
  return {
366
481
  request: result.request,
367
482
  response: result.response ? {
@@ -371,6 +486,15 @@ var SAPAILanguageModelV2 = class {
371
486
  };
372
487
  }
373
488
  };
489
+ function normalizeCallOptions(options) {
490
+ const normalized = {
491
+ ...options,
492
+ prompt: normalizeV2PromptToV3(options.prompt),
493
+ // Legacy provider-defined tools keep their unsupported warning/skip behavior.
494
+ tools: options.tools
495
+ };
496
+ return normalized;
497
+ }
374
498
 
375
499
  // src/sap-ai-provider-v2.ts
376
500
  import { NoSuchModelError } from "@ai-sdk/provider";