@jerome-benoit/sap-ai-provider 4.0.0-rc.2 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -29910,9 +29910,7 @@ function convertToSAPMessages(prompt, options = {}) {
29910
29910
  }
29911
29911
  default: {
29912
29912
  const _exhaustiveCheck = message;
29913
- throw new Error(
29914
- `Unsupported role: ${_exhaustiveCheck.role}`
29915
- );
29913
+ throw new Error(`Unsupported role: ${_exhaustiveCheck.role}`);
29916
29914
  }
29917
29915
  }
29918
29916
  }
@@ -29981,18 +29979,29 @@ function convertToAISDKError(error, context) {
29981
29979
  if (error instanceof import_provider2.APICallError || error instanceof import_provider2.LoadAPIKeyError) {
29982
29980
  return error;
29983
29981
  }
29984
- if (isOrchestrationErrorResponse(error)) {
29985
- return convertSAPErrorToAPICallError(error, {
29982
+ const rootError = error instanceof Error && (0, import_util.isErrorWithCause)(error) ? error.rootCause : error;
29983
+ if (isOrchestrationErrorResponse(rootError)) {
29984
+ return convertSAPErrorToAPICallError(rootError, {
29986
29985
  ...context,
29987
29986
  responseHeaders: context?.responseHeaders ?? getAxiosResponseHeaders(error)
29988
29987
  });
29989
29988
  }
29989
+ if (rootError instanceof Error) {
29990
+ const parsedError = tryExtractSAPErrorFromMessage(rootError.message);
29991
+ if (parsedError && isOrchestrationErrorResponse(parsedError)) {
29992
+ return convertSAPErrorToAPICallError(parsedError, {
29993
+ ...context,
29994
+ responseHeaders: context?.responseHeaders ?? getAxiosResponseHeaders(error)
29995
+ });
29996
+ }
29997
+ }
29990
29998
  const responseHeaders = context?.responseHeaders ?? getAxiosResponseHeaders(error);
29991
- if (error instanceof Error) {
29992
- const errorMsg = error.message.toLowerCase();
29993
- if (errorMsg.includes("authentication") || errorMsg.includes("unauthorized") || errorMsg.includes("aicore_service_key") || errorMsg.includes("invalid credentials")) {
29999
+ if (rootError instanceof Error) {
30000
+ const errorMsg = rootError.message.toLowerCase();
30001
+ const originalMsg = rootError.message;
30002
+ if (errorMsg.includes("authentication") || errorMsg.includes("unauthorized") || errorMsg.includes("aicore_service_key") || errorMsg.includes("invalid credentials") || errorMsg.includes("service credentials") || errorMsg.includes("service binding")) {
29994
30003
  return new import_provider2.LoadAPIKeyError({
29995
- message: `SAP AI Core authentication failed: ${error.message}
30004
+ message: `SAP AI Core authentication failed: ${originalMsg}
29996
30005
 
29997
30006
  Make sure your AICORE_SERVICE_KEY environment variable is set correctly.
29998
30007
  See: https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/create-service-key`
@@ -30002,15 +30011,79 @@ See: https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/create-serv
30002
30011
  return new import_provider2.APICallError({
30003
30012
  cause: error,
30004
30013
  isRetryable: true,
30005
- message: `Network error connecting to SAP AI Core: ${error.message}`,
30014
+ message: `Network error connecting to SAP AI Core: ${originalMsg}`,
30006
30015
  requestBodyValues: context?.requestBody,
30007
30016
  responseHeaders,
30008
30017
  statusCode: 503,
30009
30018
  url: context?.url ?? ""
30010
30019
  });
30011
30020
  }
30021
+ if (errorMsg.includes("could not resolve destination")) {
30022
+ return new import_provider2.APICallError({
30023
+ cause: error,
30024
+ isRetryable: false,
30025
+ message: `SAP AI Core destination error: ${originalMsg}
30026
+
30027
+ Check your destination configuration or provide a valid destinationName.`,
30028
+ requestBodyValues: context?.requestBody,
30029
+ responseHeaders,
30030
+ statusCode: 500,
30031
+ url: context?.url ?? ""
30032
+ });
30033
+ }
30034
+ if (errorMsg.includes("failed to resolve deployment") || errorMsg.includes("no deployment matched")) {
30035
+ return new import_provider2.APICallError({
30036
+ cause: error,
30037
+ isRetryable: false,
30038
+ message: `SAP AI Core deployment error: ${originalMsg}
30039
+
30040
+ Make sure you have a running orchestration deployment in your AI Core instance.
30041
+ See: https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/create-deployment-for-orchestration`,
30042
+ requestBodyValues: context?.requestBody,
30043
+ responseHeaders,
30044
+ statusCode: 404,
30045
+ url: context?.url ?? ""
30046
+ });
30047
+ }
30048
+ if (errorMsg.includes("filtered by the output filter")) {
30049
+ return new import_provider2.APICallError({
30050
+ cause: error,
30051
+ isRetryable: false,
30052
+ message: `Content was filtered: ${originalMsg}
30053
+
30054
+ The model's response was blocked by content safety filters. Try a different prompt.`,
30055
+ requestBodyValues: context?.requestBody,
30056
+ responseHeaders,
30057
+ statusCode: 400,
30058
+ url: context?.url ?? ""
30059
+ });
30060
+ }
30061
+ const statusMatch = /status code (\d+)/i.exec(originalMsg);
30062
+ if (statusMatch) {
30063
+ const extractedStatus = parseInt(statusMatch[1], 10);
30064
+ return new import_provider2.APICallError({
30065
+ cause: error,
30066
+ isRetryable: isRetryable(extractedStatus),
30067
+ message: `SAP AI Core request failed: ${originalMsg}`,
30068
+ requestBodyValues: context?.requestBody,
30069
+ responseHeaders,
30070
+ statusCode: extractedStatus,
30071
+ url: context?.url ?? ""
30072
+ });
30073
+ }
30074
+ if (errorMsg.includes("iterate over") || errorMsg.includes("consumed stream") || errorMsg.includes("parse message into json") || errorMsg.includes("no body")) {
30075
+ return new import_provider2.APICallError({
30076
+ cause: error,
30077
+ isRetryable: true,
30078
+ message: `SAP AI Core streaming error: ${originalMsg}`,
30079
+ requestBodyValues: context?.requestBody,
30080
+ responseHeaders,
30081
+ statusCode: 500,
30082
+ url: context?.url ?? ""
30083
+ });
30084
+ }
30012
30085
  }
30013
- const message = error instanceof Error ? error.message : typeof error === "string" ? error : "Unknown error occurred";
30086
+ const message = rootError instanceof Error ? rootError.message : typeof rootError === "string" ? rootError : "Unknown error occurred";
30014
30087
  const fullMessage = context?.operation ? `SAP AI Core ${context.operation} failed: ${message}` : `SAP AI Core error: ${message}`;
30015
30088
  return new import_provider2.APICallError({
30016
30089
  cause: error,
@@ -30071,13 +30144,35 @@ function normalizeHeaders(headers) {
30071
30144
  if (entries.length === 0) return void 0;
30072
30145
  return Object.fromEntries(entries);
30073
30146
  }
30147
+ function tryExtractSAPErrorFromMessage(message) {
30148
+ const jsonMatch = /\{[\s\S]*\}/.exec(message);
30149
+ if (!jsonMatch) return null;
30150
+ try {
30151
+ const parsed = JSON.parse(jsonMatch[0]);
30152
+ if (parsed && typeof parsed === "object" && "error" in parsed) {
30153
+ return parsed;
30154
+ }
30155
+ if (parsed && typeof parsed === "object" && "message" in parsed) {
30156
+ return { error: parsed };
30157
+ }
30158
+ return null;
30159
+ } catch {
30160
+ return null;
30161
+ }
30162
+ }
30074
30163
 
30075
30164
  // src/sap-ai-language-model.ts
30076
30165
  var StreamIdGenerator = class {
30166
+ /**
30167
+ * Generates a unique ID for a response stream.
30168
+ * @returns RFC 4122-compliant UUID
30169
+ */
30170
+ generateResponseId() {
30171
+ return crypto.randomUUID();
30172
+ }
30077
30173
  /**
30078
30174
  * Generates a unique ID for a text block.
30079
- *
30080
- * @returns RFC 4122-compliant UUID string
30175
+ * @returns RFC 4122-compliant UUID
30081
30176
  */
30082
30177
  generateTextBlockId() {
30083
30178
  return crypto.randomUUID();
@@ -30094,7 +30189,11 @@ var SAPAILanguageModel = class {
30094
30189
  * SAP AI Core will fail the request at runtime.
30095
30190
  */
30096
30191
  supportsImageUrls = true;
30097
- /** Multiple completions via the `n` parameter (provider-specific support). */
30192
+ /**
30193
+ * Multiple completions via the `n` parameter.
30194
+ * Note: Amazon and Anthropic models do not support this feature.
30195
+ * The provider silently omits the parameter for unsupported models.
30196
+ */
30098
30197
  supportsMultipleCompletions = true;
30099
30198
  /** Parallel tool calls. */
30100
30199
  supportsParallelToolCalls = true;
@@ -30105,52 +30204,14 @@ var SAPAILanguageModel = class {
30105
30204
  /** Tool/function calling. */
30106
30205
  supportsToolCalls = true;
30107
30206
  /**
30108
- * Generates text completion using SAP AI Core's Orchestration API.
30109
- *
30110
- * This method implements the `LanguageModelV3.doGenerate` interface,
30111
- * providing synchronous (non-streaming) text generation with support for:
30112
- * - Multi-turn conversations with system/user/assistant messages
30113
- * - Tool calling (function calling) with structured outputs
30114
- * - Multi-modal inputs (text + images)
30115
- * - Data masking via SAP DPI
30116
- * - Content filtering via Azure Content Safety or Llama Guard
30117
- *
30118
- * **Return Structure:**
30119
- * - Finish reason: `{ unified: string, raw?: string }`
30120
- * - Usage: Nested structure with token breakdown `{ inputTokens: { total, ... }, outputTokens: { total, ... } }`
30121
- * - Warnings: Array of warnings with `type` and optional `feature` field
30122
- *
30123
- * @param options - Generation options including prompt, tools, temperature, etc.
30124
- * @returns Promise resolving to generation result with content, usage, and metadata
30125
- *
30126
- * @throws {InvalidPromptError} If prompt format is invalid
30127
- * @throws {InvalidArgumentError} If arguments are malformed
30128
- * @throws {APICallError} If the SAP AI Core API call fails
30129
- *
30130
- * @example
30131
- * ```typescript
30132
- * const result = await model.doGenerate({
30133
- * prompt: [
30134
- * { role: 'user', content: [{ type: 'text', text: 'Hello!' }] }
30135
- * ],
30136
- * temperature: 0.7,
30137
- * maxTokens: 100
30138
- * });
30139
- *
30140
- * console.log(result.content); // Array of V3 content parts
30141
- * console.log(result.finishReason.unified); // "stop", "length", "tool-calls", etc.
30142
- * console.log(result.usage.inputTokens.total); // Total input tokens
30143
- * ```
30144
- *
30145
- * @since 1.0.0
30146
- * @since 4.0.0 Updated to LanguageModelV3 interface
30207
+ * Returns the provider identifier.
30208
+ * @returns The provider name
30147
30209
  */
30148
30210
  get provider() {
30149
30211
  return this.config.provider;
30150
30212
  }
30151
30213
  /**
30152
30214
  * Returns supported URL patterns for different content types.
30153
- *
30154
30215
  * @returns Record of content types to regex patterns
30155
30216
  */
30156
30217
  get supportedUrls() {
@@ -30162,13 +30223,10 @@ var SAPAILanguageModel = class {
30162
30223
  settings;
30163
30224
  /**
30164
30225
  * Creates a new SAP AI Chat Language Model instance.
30165
- *
30226
+ * @internal
30166
30227
  * @param modelId - The model identifier
30167
30228
  * @param settings - Model-specific configuration settings
30168
30229
  * @param config - Internal configuration (deployment config, destination, etc.)
30169
- *
30170
- * @internal This constructor is not meant to be called directly.
30171
- * Use the provider function instead.
30172
30230
  */
30173
30231
  constructor(modelId, settings, config) {
30174
30232
  this.settings = settings;
@@ -30190,13 +30248,12 @@ var SAPAILanguageModel = class {
30190
30248
  *
30191
30249
  * **Note on Abort Signal:**
30192
30250
  * The abort signal implementation uses Promise.race to reject the promise when
30193
- * the signal is aborted. However, this does not cancel the underlying HTTP request
30194
- * to SAP AI Core - the request continues executing on the server. This is a
30195
- * limitation of the SAP AI SDK's chatCompletion API.
30196
- *
30251
+ * aborted. However, this does not cancel the underlying HTTP request to SAP AI Core -
30252
+ * the request continues executing on the server. This is a current limitation of the
30253
+ * SAP AI SDK's API. See https://github.com/SAP/ai-sdk-js/issues/1429
30197
30254
  * @param options - Generation options including prompt, tools, and settings
30198
30255
  * @returns Promise resolving to the generation result with content, usage, and metadata
30199
- *
30256
+ * @since 1.0.0
30200
30257
  * @example
30201
30258
  * ```typescript
30202
30259
  * const result = await model.doGenerate({
@@ -30355,33 +30412,29 @@ var SAPAILanguageModel = class {
30355
30412
  /**
30356
30413
  * Generates a streaming completion.
30357
30414
  *
30358
- * This method implements the `LanguageModelV3.doStream` interface,
30359
- * sending a streaming request to SAP AI Core and returning a stream of response parts.
30415
+ * Implements `LanguageModelV3.doStream`, sending a streaming request to SAP AI Core
30416
+ * and returning a stream of response parts.
30360
30417
  *
30361
30418
  * **Stream Events:**
30362
- * - `stream-start` - Stream initialization with warnings
30363
- * - `response-metadata` - Response metadata (model, timestamp)
30419
+ * - `stream-start` - Initialization with warnings
30420
+ * - `response-metadata` - Model, timestamp, response ID
30364
30421
  * - `text-start` - Text block begins (with unique ID)
30365
- * - `text-delta` - Incremental text chunks (with block ID)
30366
- * - `text-end` - Text block completes (with accumulated text)
30367
- * - `tool-input-start` - Tool input begins
30368
- * - `tool-input-delta` - Tool input chunk
30369
- * - `tool-input-end` - Tool input completes
30422
+ * - `text-delta` - Incremental text chunks
30423
+ * - `text-end` - Text block completes
30424
+ * - `tool-input-start/delta/end` - Tool input lifecycle
30370
30425
  * - `tool-call` - Complete tool call
30371
30426
  * - `finish` - Stream completes with usage and finish reason
30372
30427
  * - `error` - Error occurred
30373
30428
  *
30374
- * **Stream Structure:**
30375
- * - Text blocks have explicit lifecycle with unique IDs
30376
- * - Finish reason format: `{ unified: string, raw?: string }`
30377
- * - Usage format: `{ inputTokens: { total, ... }, outputTokens: { total, ... } }`
30378
- * - Warnings only in `stream-start` event
30429
+ * **Response ID:**
30430
+ * Client-generated UUID in `response-metadata.id` and `providerMetadata['sap-ai'].responseId`.
30431
+ * TODO: Use backend's `x-request-id` when `OrchestrationStreamResponse` exposes `rawResponse`.
30379
30432
  *
30433
+ * **Abort Signal:**
30434
+ * Same limitation as `doGenerate` - see its documentation for details.
30380
30435
  * @see {@link https://sdk.vercel.ai/docs/ai-sdk-core/streaming Vercel AI SDK Streaming}
30381
- *
30382
30436
  * @param options - Streaming options including prompt, tools, and settings
30383
30437
  * @returns Promise resolving to stream and request metadata
30384
- *
30385
30438
  * @example
30386
30439
  * ```typescript
30387
30440
  * const { stream } = await model.doStream({
@@ -30394,13 +30447,9 @@ var SAPAILanguageModel = class {
30394
30447
  * if (part.type === 'text-delta') {
30395
30448
  * process.stdout.write(part.delta);
30396
30449
  * }
30397
- * if (part.type === 'text-end') {
30398
- * console.log('Block complete:', part.id, part.text);
30399
- * }
30400
30450
  * }
30401
30451
  * ```
30402
- *
30403
- * @since 4.0.0
30452
+ * @since 1.0.0
30404
30453
  */
30405
30454
  async doStream(options) {
30406
30455
  try {
@@ -30423,12 +30472,11 @@ var SAPAILanguageModel = class {
30423
30472
  return filtering && Object.keys(filtering).length > 0 ? { filtering } : {};
30424
30473
  })()
30425
30474
  };
30426
- const streamResponse = await client.stream(
30427
- requestBody,
30428
- options.abortSignal,
30429
- { promptTemplating: { include_usage: true } }
30430
- );
30475
+ const streamResponse = await client.stream(requestBody, options.abortSignal, {
30476
+ promptTemplating: { include_usage: true }
30477
+ });
30431
30478
  const idGenerator = new StreamIdGenerator();
30479
+ const responseId = idGenerator.generateResponseId();
30432
30480
  let textBlockId = null;
30433
30481
  const streamState = {
30434
30482
  activeText: false,
@@ -30472,6 +30520,7 @@ var SAPAILanguageModel = class {
30472
30520
  if (streamState.isFirstChunk) {
30473
30521
  streamState.isFirstChunk = false;
30474
30522
  controller.enqueue({
30523
+ id: responseId,
30475
30524
  modelId,
30476
30525
  timestamp: /* @__PURE__ */ new Date(),
30477
30526
  type: "response-metadata"
@@ -30634,6 +30683,12 @@ var SAPAILanguageModel = class {
30634
30683
  }
30635
30684
  controller.enqueue({
30636
30685
  finishReason: streamState.finishReason,
30686
+ providerMetadata: {
30687
+ "sap-ai": {
30688
+ finishReason: streamState.finishReason.raw,
30689
+ responseId
30690
+ }
30691
+ },
30637
30692
  type: "finish",
30638
30693
  usage: streamState.usage
30639
30694
  });
@@ -30668,7 +30723,6 @@ var SAPAILanguageModel = class {
30668
30723
  }
30669
30724
  /**
30670
30725
  * Checks if a URL is supported for file/image uploads.
30671
- *
30672
30726
  * @param url - The URL to check
30673
30727
  * @returns True if the URL protocol is HTTPS or data with valid image format
30674
30728
  */
@@ -30681,7 +30735,6 @@ var SAPAILanguageModel = class {
30681
30735
  }
30682
30736
  /**
30683
30737
  * Builds orchestration module config for SAP AI SDK.
30684
- *
30685
30738
  * @param options - Call options from the AI SDK
30686
30739
  * @returns Object containing orchestration config, messages, and warnings
30687
30740
  * @internal
@@ -30714,12 +30767,9 @@ var SAPAILanguageModel = class {
30714
30767
  let parameters;
30715
30768
  if (toolWithParams.parameters && isZodSchema(toolWithParams.parameters)) {
30716
30769
  try {
30717
- const jsonSchema = (0, import_zod_to_json_schema.zodToJsonSchema)(
30718
- toolWithParams.parameters,
30719
- {
30720
- $refStrategy: "none"
30721
- }
30722
- );
30770
+ const jsonSchema = (0, import_zod_to_json_schema.zodToJsonSchema)(toolWithParams.parameters, {
30771
+ $refStrategy: "none"
30772
+ });
30723
30773
  const schemaRecord = jsonSchema;
30724
30774
  delete schemaRecord.$schema;
30725
30775
  parameters = buildSAPToolParameters(schemaRecord);
@@ -30859,17 +30909,12 @@ var SAPAILanguageModel = class {
30859
30909
  }
30860
30910
  /**
30861
30911
  * Creates an OrchestrationClient instance.
30862
- *
30863
30912
  * @param config - Orchestration module configuration
30864
30913
  * @returns OrchestrationClient instance
30865
30914
  * @internal
30866
30915
  */
30867
30916
  createClient(config) {
30868
- return new import_orchestration.OrchestrationClient(
30869
- config,
30870
- this.config.deploymentConfig,
30871
- this.config.destination
30872
- );
30917
+ return new import_orchestration.OrchestrationClient(config, this.config.deploymentConfig, this.config.destination);
30873
30918
  }
30874
30919
  };
30875
30920
  function buildSAPToolParameters(schema) {
@@ -30898,9 +30943,7 @@ function buildSAPToolParameters(schema) {
30898
30943
  function createAISDKRequestBodySummary(options) {
30899
30944
  return {
30900
30945
  hasImageParts: options.prompt.some(
30901
- (message) => message.role === "user" && message.content.some(
30902
- (part) => part.type === "file" && part.mediaType.startsWith("image/")
30903
- )
30946
+ (message) => message.role === "user" && message.content.some((part) => part.type === "file" && part.mediaType.startsWith("image/"))
30904
30947
  ),
30905
30948
  maxOutputTokens: options.maxOutputTokens,
30906
30949
  promptMessages: options.prompt.length,
@@ -31019,9 +31062,7 @@ function createSAPAIProvider(options = {}) {
31019
31062
  };
31020
31063
  const provider = function(modelId, settings) {
31021
31064
  if (new.target) {
31022
- throw new Error(
31023
- "The SAP AI provider function cannot be called with the new keyword."
31024
- );
31065
+ throw new Error("The SAP AI provider function cannot be called with the new keyword.");
31025
31066
  }
31026
31067
  return createModel(modelId, settings);
31027
31068
  };