@jerome-benoit/sap-ai-provider 3.0.0 → 4.0.0-rc.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.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { LanguageModelV2, LanguageModelV2CallOptions, LanguageModelV2Content, LanguageModelV2FinishReason, LanguageModelV2Usage, JSONValue, LanguageModelV2CallWarning, LanguageModelV2StreamPart, ProviderV2 } from '@ai-sdk/provider';
1
+ import { LanguageModelV3, LanguageModelV3CallOptions, LanguageModelV3GenerateResult, LanguageModelV3StreamResult, ProviderV3 } from '@ai-sdk/provider';
2
2
  import { HttpDestinationOrFetchOptions } from '@sap-cloud-sdk/connectivity';
3
3
  import { ResourceGroupConfig, DeploymentIdConfig } from '@sap-ai-sdk/ai-api/internal.js';
4
4
  import { MaskingModule, FilteringModule, ChatCompletionTool, ChatModel } from '@sap-ai-sdk/orchestration';
@@ -292,7 +292,7 @@ interface SAPAIConfig {
292
292
  /**
293
293
  * SAP AI Chat Language Model implementation.
294
294
  *
295
- * This class implements the AI SDK's `LanguageModelV2` interface,
295
+ * This class implements the AI SDK's `LanguageModelV3` interface,
296
296
  * providing a bridge between the AI SDK and SAP AI Core's Orchestration API
297
297
  * using the official SAP AI SDK (@sap-ai-sdk/orchestration).
298
298
  *
@@ -322,10 +322,10 @@ interface SAPAIConfig {
322
322
  * });
323
323
  * ```
324
324
  *
325
- * @implements {LanguageModelV2}
325
+ * @implements {LanguageModelV3}
326
326
  */
327
- declare class SAPAIChatLanguageModel implements LanguageModelV2 {
328
- readonly specificationVersion = "v2";
327
+ declare class SAPAIChatLanguageModel implements LanguageModelV3 {
328
+ readonly specificationVersion = "v3";
329
329
  readonly modelId: SAPAIModelId;
330
330
  private readonly config;
331
331
  private readonly settings;
@@ -344,7 +344,7 @@ declare class SAPAIChatLanguageModel implements LanguageModelV2 {
344
344
  * Checks if a URL is supported for file/image uploads.
345
345
  *
346
346
  * @param url - The URL to check
347
- * @returns True if the URL protocol is HTTPS or data (content-type rules are enforced via supportedUrls)
347
+ * @returns True if the URL protocol is HTTPS or data with valid image format
348
348
  */
349
349
  supportsUrl(url: URL): boolean;
350
350
  /**
@@ -354,9 +354,45 @@ declare class SAPAIChatLanguageModel implements LanguageModelV2 {
354
354
  */
355
355
  get supportedUrls(): Record<string, RegExp[]>;
356
356
  /**
357
- * Gets the provider identifier.
357
+ * Generates text completion using SAP AI Core's Orchestration API.
358
358
  *
359
- * @returns The provider name ('sap-ai')
359
+ * This method implements the `LanguageModelV3.doGenerate` interface,
360
+ * providing synchronous (non-streaming) text generation with support for:
361
+ * - Multi-turn conversations with system/user/assistant messages
362
+ * - Tool calling (function calling) with structured outputs
363
+ * - Multi-modal inputs (text + images)
364
+ * - Data masking via SAP DPI
365
+ * - Content filtering via Azure Content Safety or Llama Guard
366
+ *
367
+ * **Return Structure:**
368
+ * - Finish reason: `{ unified: string, raw?: string }`
369
+ * - Usage: Nested structure with token breakdown `{ inputTokens: { total, ... }, outputTokens: { total, ... } }`
370
+ * - Warnings: Array of warnings with `type` and optional `feature` field
371
+ *
372
+ * @param options - Generation options including prompt, tools, temperature, etc.
373
+ * @returns Promise resolving to generation result with content, usage, and metadata
374
+ *
375
+ * @throws {InvalidPromptError} If prompt format is invalid
376
+ * @throws {InvalidArgumentError} If arguments are malformed
377
+ * @throws {APICallError} If the SAP AI Core API call fails
378
+ *
379
+ * @example
380
+ * ```typescript
381
+ * const result = await model.doGenerate({
382
+ * prompt: [
383
+ * { role: 'user', content: [{ type: 'text', text: 'Hello!' }] }
384
+ * ],
385
+ * temperature: 0.7,
386
+ * maxTokens: 100
387
+ * });
388
+ *
389
+ * console.log(result.content); // Array of V3 content parts
390
+ * console.log(result.finishReason.unified); // "stop", "length", "tool-calls", etc.
391
+ * console.log(result.usage.inputTokens.total); // Total input tokens
392
+ * ```
393
+ *
394
+ * @since 1.0.0
395
+ * @since 4.0.0 Updated to LanguageModelV3 interface
360
396
  */
361
397
  get provider(): string;
362
398
  /**
@@ -396,7 +432,7 @@ declare class SAPAIChatLanguageModel implements LanguageModelV2 {
396
432
  /**
397
433
  * Generates a single completion (non-streaming).
398
434
  *
399
- * This method implements the `LanguageModelV2.doGenerate` interface,
435
+ * This method implements the `LanguageModelV3.doGenerate` interface,
400
436
  * sending a request to SAP AI Core and returning the complete response.
401
437
  *
402
438
  * **Features:**
@@ -404,6 +440,13 @@ declare class SAPAIChatLanguageModel implements LanguageModelV2 {
404
440
  * - Multi-modal input (text + images)
405
441
  * - Data masking (if configured)
406
442
  * - Content filtering (if configured)
443
+ * - Abort signal support (via Promise.race)
444
+ *
445
+ * **Note on Abort Signal:**
446
+ * The abort signal implementation uses Promise.race to reject the promise when
447
+ * the signal is aborted. However, this does not cancel the underlying HTTP request
448
+ * to SAP AI Core - the request continues executing on the server. This is a
449
+ * limitation of the SAP AI SDK's chatCompletion API.
407
450
  *
408
451
  * @param options - Generation options including prompt, tools, and settings
409
452
  * @returns Promise resolving to the generation result with content, usage, and metadata
@@ -420,38 +463,32 @@ declare class SAPAIChatLanguageModel implements LanguageModelV2 {
420
463
  * console.log(result.usage); // Token usage
421
464
  * ```
422
465
  */
423
- doGenerate(options: LanguageModelV2CallOptions): Promise<{
424
- content: LanguageModelV2Content[];
425
- finishReason: LanguageModelV2FinishReason;
426
- usage: LanguageModelV2Usage;
427
- providerMetadata?: Record<string, Record<string, JSONValue>>;
428
- request: {
429
- body?: unknown;
430
- };
431
- response: {
432
- timestamp: Date;
433
- modelId: string;
434
- headers?: Record<string, string>;
435
- body?: unknown;
436
- };
437
- warnings: LanguageModelV2CallWarning[];
438
- }>;
466
+ doGenerate(options: LanguageModelV3CallOptions): Promise<LanguageModelV3GenerateResult>;
439
467
  /**
440
468
  * Generates a streaming completion.
441
469
  *
442
- * This method implements the `LanguageModelV2.doStream` interface,
470
+ * This method implements the `LanguageModelV3.doStream` interface,
443
471
  * sending a streaming request to SAP AI Core and returning a stream of response parts.
444
472
  *
445
473
  * **Stream Events:**
446
- * - `stream-start` - Stream initialization
474
+ * - `stream-start` - Stream initialization with warnings
447
475
  * - `response-metadata` - Response metadata (model, timestamp)
448
- * - `text-start` - Text generation starts
449
- * - `text-delta` - Incremental text chunks
450
- * - `text-end` - Text generation completes
451
- * - `tool-call` - Tool call detected
476
+ * - `text-start` - Text block begins (with unique ID)
477
+ * - `text-delta` - Incremental text chunks (with block ID)
478
+ * - `text-end` - Text block completes (with accumulated text)
479
+ * - `tool-input-start` - Tool input begins
480
+ * - `tool-input-delta` - Tool input chunk
481
+ * - `tool-input-end` - Tool input completes
482
+ * - `tool-call` - Complete tool call
452
483
  * - `finish` - Stream completes with usage and finish reason
453
484
  * - `error` - Error occurred
454
485
  *
486
+ * **Stream Structure:**
487
+ * - Text blocks have explicit lifecycle with unique IDs
488
+ * - Finish reason format: `{ unified: string, raw?: string }`
489
+ * - Usage format: `{ inputTokens: { total, ... }, outputTokens: { total, ... } }`
490
+ * - Warnings only in `stream-start` event
491
+ *
455
492
  * @param options - Streaming options including prompt, tools, and settings
456
493
  * @returns Promise resolving to stream and request metadata
457
494
  *
@@ -467,26 +504,22 @@ declare class SAPAIChatLanguageModel implements LanguageModelV2 {
467
504
  * if (part.type === 'text-delta') {
468
505
  * process.stdout.write(part.delta);
469
506
  * }
507
+ * if (part.type === 'text-end') {
508
+ * console.log('Block complete:', part.id, part.text);
509
+ * }
470
510
  * }
471
511
  * ```
512
+ *
513
+ * @since 4.0.0
472
514
  */
473
- doStream(options: LanguageModelV2CallOptions): Promise<{
474
- stream: ReadableStream<LanguageModelV2StreamPart>;
475
- request?: {
476
- body?: unknown;
477
- };
478
- response?: {
479
- headers?: Record<string, string>;
480
- };
481
- warnings: LanguageModelV2CallWarning[];
482
- }>;
515
+ doStream(options: LanguageModelV3CallOptions): Promise<LanguageModelV3StreamResult>;
483
516
  }
484
517
 
485
518
  /**
486
519
  * SAP AI Provider interface.
487
520
  *
488
521
  * This is the main interface for creating and configuring SAP AI Core models.
489
- * It extends the standard AI SDK ProviderV2 interface with SAP-specific functionality.
522
+ * It extends the standard AI SDK ProviderV3 interface with SAP-specific functionality.
490
523
  *
491
524
  * @example
492
525
  * ```typescript
@@ -506,7 +539,7 @@ declare class SAPAIChatLanguageModel implements LanguageModelV2 {
506
539
  * const chatModel = provider.chat('gpt-4o');
507
540
  * ```
508
541
  */
509
- interface SAPAIProvider extends ProviderV2 {
542
+ interface SAPAIProvider extends ProviderV3 {
510
543
  /**
511
544
  * Create a language model instance.
512
545
  *
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { LanguageModelV2, LanguageModelV2CallOptions, LanguageModelV2Content, LanguageModelV2FinishReason, LanguageModelV2Usage, JSONValue, LanguageModelV2CallWarning, LanguageModelV2StreamPart, ProviderV2 } from '@ai-sdk/provider';
1
+ import { LanguageModelV3, LanguageModelV3CallOptions, LanguageModelV3GenerateResult, LanguageModelV3StreamResult, ProviderV3 } from '@ai-sdk/provider';
2
2
  import { HttpDestinationOrFetchOptions } from '@sap-cloud-sdk/connectivity';
3
3
  import { ResourceGroupConfig, DeploymentIdConfig } from '@sap-ai-sdk/ai-api/internal.js';
4
4
  import { MaskingModule, FilteringModule, ChatCompletionTool, ChatModel } from '@sap-ai-sdk/orchestration';
@@ -292,7 +292,7 @@ interface SAPAIConfig {
292
292
  /**
293
293
  * SAP AI Chat Language Model implementation.
294
294
  *
295
- * This class implements the AI SDK's `LanguageModelV2` interface,
295
+ * This class implements the AI SDK's `LanguageModelV3` interface,
296
296
  * providing a bridge between the AI SDK and SAP AI Core's Orchestration API
297
297
  * using the official SAP AI SDK (@sap-ai-sdk/orchestration).
298
298
  *
@@ -322,10 +322,10 @@ interface SAPAIConfig {
322
322
  * });
323
323
  * ```
324
324
  *
325
- * @implements {LanguageModelV2}
325
+ * @implements {LanguageModelV3}
326
326
  */
327
- declare class SAPAIChatLanguageModel implements LanguageModelV2 {
328
- readonly specificationVersion = "v2";
327
+ declare class SAPAIChatLanguageModel implements LanguageModelV3 {
328
+ readonly specificationVersion = "v3";
329
329
  readonly modelId: SAPAIModelId;
330
330
  private readonly config;
331
331
  private readonly settings;
@@ -344,7 +344,7 @@ declare class SAPAIChatLanguageModel implements LanguageModelV2 {
344
344
  * Checks if a URL is supported for file/image uploads.
345
345
  *
346
346
  * @param url - The URL to check
347
- * @returns True if the URL protocol is HTTPS or data (content-type rules are enforced via supportedUrls)
347
+ * @returns True if the URL protocol is HTTPS or data with valid image format
348
348
  */
349
349
  supportsUrl(url: URL): boolean;
350
350
  /**
@@ -354,9 +354,45 @@ declare class SAPAIChatLanguageModel implements LanguageModelV2 {
354
354
  */
355
355
  get supportedUrls(): Record<string, RegExp[]>;
356
356
  /**
357
- * Gets the provider identifier.
357
+ * Generates text completion using SAP AI Core's Orchestration API.
358
358
  *
359
- * @returns The provider name ('sap-ai')
359
+ * This method implements the `LanguageModelV3.doGenerate` interface,
360
+ * providing synchronous (non-streaming) text generation with support for:
361
+ * - Multi-turn conversations with system/user/assistant messages
362
+ * - Tool calling (function calling) with structured outputs
363
+ * - Multi-modal inputs (text + images)
364
+ * - Data masking via SAP DPI
365
+ * - Content filtering via Azure Content Safety or Llama Guard
366
+ *
367
+ * **Return Structure:**
368
+ * - Finish reason: `{ unified: string, raw?: string }`
369
+ * - Usage: Nested structure with token breakdown `{ inputTokens: { total, ... }, outputTokens: { total, ... } }`
370
+ * - Warnings: Array of warnings with `type` and optional `feature` field
371
+ *
372
+ * @param options - Generation options including prompt, tools, temperature, etc.
373
+ * @returns Promise resolving to generation result with content, usage, and metadata
374
+ *
375
+ * @throws {InvalidPromptError} If prompt format is invalid
376
+ * @throws {InvalidArgumentError} If arguments are malformed
377
+ * @throws {APICallError} If the SAP AI Core API call fails
378
+ *
379
+ * @example
380
+ * ```typescript
381
+ * const result = await model.doGenerate({
382
+ * prompt: [
383
+ * { role: 'user', content: [{ type: 'text', text: 'Hello!' }] }
384
+ * ],
385
+ * temperature: 0.7,
386
+ * maxTokens: 100
387
+ * });
388
+ *
389
+ * console.log(result.content); // Array of V3 content parts
390
+ * console.log(result.finishReason.unified); // "stop", "length", "tool-calls", etc.
391
+ * console.log(result.usage.inputTokens.total); // Total input tokens
392
+ * ```
393
+ *
394
+ * @since 1.0.0
395
+ * @since 4.0.0 Updated to LanguageModelV3 interface
360
396
  */
361
397
  get provider(): string;
362
398
  /**
@@ -396,7 +432,7 @@ declare class SAPAIChatLanguageModel implements LanguageModelV2 {
396
432
  /**
397
433
  * Generates a single completion (non-streaming).
398
434
  *
399
- * This method implements the `LanguageModelV2.doGenerate` interface,
435
+ * This method implements the `LanguageModelV3.doGenerate` interface,
400
436
  * sending a request to SAP AI Core and returning the complete response.
401
437
  *
402
438
  * **Features:**
@@ -404,6 +440,13 @@ declare class SAPAIChatLanguageModel implements LanguageModelV2 {
404
440
  * - Multi-modal input (text + images)
405
441
  * - Data masking (if configured)
406
442
  * - Content filtering (if configured)
443
+ * - Abort signal support (via Promise.race)
444
+ *
445
+ * **Note on Abort Signal:**
446
+ * The abort signal implementation uses Promise.race to reject the promise when
447
+ * the signal is aborted. However, this does not cancel the underlying HTTP request
448
+ * to SAP AI Core - the request continues executing on the server. This is a
449
+ * limitation of the SAP AI SDK's chatCompletion API.
407
450
  *
408
451
  * @param options - Generation options including prompt, tools, and settings
409
452
  * @returns Promise resolving to the generation result with content, usage, and metadata
@@ -420,38 +463,32 @@ declare class SAPAIChatLanguageModel implements LanguageModelV2 {
420
463
  * console.log(result.usage); // Token usage
421
464
  * ```
422
465
  */
423
- doGenerate(options: LanguageModelV2CallOptions): Promise<{
424
- content: LanguageModelV2Content[];
425
- finishReason: LanguageModelV2FinishReason;
426
- usage: LanguageModelV2Usage;
427
- providerMetadata?: Record<string, Record<string, JSONValue>>;
428
- request: {
429
- body?: unknown;
430
- };
431
- response: {
432
- timestamp: Date;
433
- modelId: string;
434
- headers?: Record<string, string>;
435
- body?: unknown;
436
- };
437
- warnings: LanguageModelV2CallWarning[];
438
- }>;
466
+ doGenerate(options: LanguageModelV3CallOptions): Promise<LanguageModelV3GenerateResult>;
439
467
  /**
440
468
  * Generates a streaming completion.
441
469
  *
442
- * This method implements the `LanguageModelV2.doStream` interface,
470
+ * This method implements the `LanguageModelV3.doStream` interface,
443
471
  * sending a streaming request to SAP AI Core and returning a stream of response parts.
444
472
  *
445
473
  * **Stream Events:**
446
- * - `stream-start` - Stream initialization
474
+ * - `stream-start` - Stream initialization with warnings
447
475
  * - `response-metadata` - Response metadata (model, timestamp)
448
- * - `text-start` - Text generation starts
449
- * - `text-delta` - Incremental text chunks
450
- * - `text-end` - Text generation completes
451
- * - `tool-call` - Tool call detected
476
+ * - `text-start` - Text block begins (with unique ID)
477
+ * - `text-delta` - Incremental text chunks (with block ID)
478
+ * - `text-end` - Text block completes (with accumulated text)
479
+ * - `tool-input-start` - Tool input begins
480
+ * - `tool-input-delta` - Tool input chunk
481
+ * - `tool-input-end` - Tool input completes
482
+ * - `tool-call` - Complete tool call
452
483
  * - `finish` - Stream completes with usage and finish reason
453
484
  * - `error` - Error occurred
454
485
  *
486
+ * **Stream Structure:**
487
+ * - Text blocks have explicit lifecycle with unique IDs
488
+ * - Finish reason format: `{ unified: string, raw?: string }`
489
+ * - Usage format: `{ inputTokens: { total, ... }, outputTokens: { total, ... } }`
490
+ * - Warnings only in `stream-start` event
491
+ *
455
492
  * @param options - Streaming options including prompt, tools, and settings
456
493
  * @returns Promise resolving to stream and request metadata
457
494
  *
@@ -467,26 +504,22 @@ declare class SAPAIChatLanguageModel implements LanguageModelV2 {
467
504
  * if (part.type === 'text-delta') {
468
505
  * process.stdout.write(part.delta);
469
506
  * }
507
+ * if (part.type === 'text-end') {
508
+ * console.log('Block complete:', part.id, part.text);
509
+ * }
470
510
  * }
471
511
  * ```
512
+ *
513
+ * @since 4.0.0
472
514
  */
473
- doStream(options: LanguageModelV2CallOptions): Promise<{
474
- stream: ReadableStream<LanguageModelV2StreamPart>;
475
- request?: {
476
- body?: unknown;
477
- };
478
- response?: {
479
- headers?: Record<string, string>;
480
- };
481
- warnings: LanguageModelV2CallWarning[];
482
- }>;
515
+ doStream(options: LanguageModelV3CallOptions): Promise<LanguageModelV3StreamResult>;
483
516
  }
484
517
 
485
518
  /**
486
519
  * SAP AI Provider interface.
487
520
  *
488
521
  * This is the main interface for creating and configuring SAP AI Core models.
489
- * It extends the standard AI SDK ProviderV2 interface with SAP-specific functionality.
522
+ * It extends the standard AI SDK ProviderV3 interface with SAP-specific functionality.
490
523
  *
491
524
  * @example
492
525
  * ```typescript
@@ -506,7 +539,7 @@ declare class SAPAIChatLanguageModel implements LanguageModelV2 {
506
539
  * const chatModel = provider.chat('gpt-4o');
507
540
  * ```
508
541
  */
509
- interface SAPAIProvider extends ProviderV2 {
542
+ interface SAPAIProvider extends ProviderV3 {
510
543
  /**
511
544
  * Create a language model instance.
512
545
  *