ai 3.1.37 → 3.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "3.1.37",
3
+ "version": "3.2.1",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -59,11 +59,11 @@
59
59
  "dependencies": {
60
60
  "@ai-sdk/provider": "0.0.10",
61
61
  "@ai-sdk/provider-utils": "0.0.15",
62
- "@ai-sdk/react": "0.0.3",
63
- "@ai-sdk/solid": "0.0.3",
64
- "@ai-sdk/svelte": "0.0.3",
65
- "@ai-sdk/ui-utils": "0.0.3",
66
- "@ai-sdk/vue": "0.0.3",
62
+ "@ai-sdk/react": "0.0.4",
63
+ "@ai-sdk/solid": "0.0.4",
64
+ "@ai-sdk/svelte": "0.0.4",
65
+ "@ai-sdk/ui-utils": "0.0.4",
66
+ "@ai-sdk/vue": "0.0.4",
67
67
  "eventsource-parser": "1.1.2",
68
68
  "jsondiffpatch": "0.6.0",
69
69
  "json-schema": "0.4.0",
@@ -1,7 +1,7 @@
1
1
  import { ReactNode } from 'react';
2
2
  import OpenAI from 'openai';
3
3
  import { z } from 'zod';
4
- import { LanguageModelV1 } from '@ai-sdk/provider';
4
+ import { LanguageModelV1FinishReason, LanguageModelV1CallWarning, LanguageModelV1 } from '@ai-sdk/provider';
5
5
 
6
6
  type AIAction<T = any, R = any> = (...args: T[]) => Promise<R>;
7
7
  type AIActions<T = any, R = any> = Record<string, AIAction<T, R>>;
@@ -422,6 +422,23 @@ type Prompt = {
422
422
  messages?: Array<CoreMessage>;
423
423
  };
424
424
 
425
+ /**
426
+ Reason why a language model finished generating a response.
427
+
428
+ Can be one of the following:
429
+ - `stop`: model generated stop sequence
430
+ - `length`: model generated maximum number of tokens
431
+ - `content-filter`: content filter violation stopped the model
432
+ - `tool-calls`: model triggered tool calls
433
+ - `error`: model stopped because of an error
434
+ - `other`: model stopped for other reasons
435
+ */
436
+ type FinishReason = LanguageModelV1FinishReason;
437
+ /**
438
+ Warning from the model provider for this call. The call will proceed, but e.g.
439
+ some settings might not be supported, which can lead to suboptimal results.
440
+ */
441
+ type CallWarning = LanguageModelV1CallWarning;
425
442
  /**
426
443
  Tool choice for the generation. It supports the following settings:
427
444
 
@@ -435,6 +452,24 @@ type CoreToolChoice<TOOLS extends Record<string, unknown>> = 'auto' | 'none' | '
435
452
  toolName: keyof TOOLS;
436
453
  };
437
454
 
455
+ /**
456
+ Represents the number of tokens used in a prompt and completion.
457
+ */
458
+ type TokenUsage = {
459
+ /**
460
+ The number of tokens used in the prompt
461
+ */
462
+ promptTokens: number;
463
+ /**
464
+ The number of tokens used in the completion.
465
+ */
466
+ completionTokens: number;
467
+ /**
468
+ The total number of tokens used (promptTokens + completionTokens).
469
+ */
470
+ totalTokens: number;
471
+ };
472
+
438
473
  type Streamable = ReactNode | Promise<ReactNode>;
439
474
  type Renderer<T extends Array<any>> = (...args: T) => Streamable | Generator<Streamable, Streamable, void> | AsyncGenerator<Streamable, Streamable, void>;
440
475
  type RenderTool<PARAMETERS extends z.ZodTypeAny = any> = {
@@ -473,7 +508,7 @@ type RenderResult = {
473
508
  */
474
509
  declare function streamUI<TOOLS extends {
475
510
  [name: string]: z.ZodTypeAny;
476
- } = {}>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, initial, text, ...settings }: CallSettings & Prompt & {
511
+ } = {}>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, initial, text, onFinish, ...settings }: CallSettings & Prompt & {
477
512
  /**
478
513
  * The language model to use.
479
514
  */
@@ -485,11 +520,41 @@ declare function streamUI<TOOLS extends {
485
520
  [name in keyof TOOLS]: RenderTool<TOOLS[name]>;
486
521
  };
487
522
  /**
488
- The tool choice strategy. Default: 'auto'.
523
+ * The tool choice strategy. Default: 'auto'.
489
524
  */
490
525
  toolChoice?: CoreToolChoice<TOOLS>;
491
526
  text?: RenderText;
492
527
  initial?: ReactNode;
528
+ /**
529
+ * Callback that is called when the LLM response and the final object validation are finished.
530
+ */
531
+ onFinish?: (event: {
532
+ /**
533
+ * The reason why the generation finished.
534
+ */
535
+ finishReason: FinishReason;
536
+ /**
537
+ * The token usage of the generated response.
538
+ */
539
+ usage: TokenUsage;
540
+ /**
541
+ * The final ui node that was generated.
542
+ */
543
+ value: ReactNode;
544
+ /**
545
+ * Warnings from the model provider (e.g. unsupported settings)
546
+ */
547
+ warnings?: CallWarning[];
548
+ /**
549
+ * Optional raw response data.
550
+ */
551
+ rawResponse?: {
552
+ /**
553
+ * Response headers.
554
+ */
555
+ headers?: Record<string, string>;
556
+ };
557
+ }) => Promise<void> | void;
493
558
  }): Promise<RenderResult>;
494
559
 
495
560
  declare function createAI<AIState = any, UIState = any, Actions extends AIActions = {}>({ actions, initialAIState, initialUIState, onSetAIState, onGetUIState, }: {
@@ -1,7 +1,7 @@
1
1
  import { ReactNode } from 'react';
2
2
  import OpenAI from 'openai';
3
3
  import { z } from 'zod';
4
- import { LanguageModelV1 } from '@ai-sdk/provider';
4
+ import { LanguageModelV1FinishReason, LanguageModelV1CallWarning, LanguageModelV1 } from '@ai-sdk/provider';
5
5
 
6
6
  type AIAction<T = any, R = any> = (...args: T[]) => Promise<R>;
7
7
  type AIActions<T = any, R = any> = Record<string, AIAction<T, R>>;
@@ -420,6 +420,23 @@ type Prompt = {
420
420
  messages?: Array<CoreMessage>;
421
421
  };
422
422
 
423
+ /**
424
+ Reason why a language model finished generating a response.
425
+
426
+ Can be one of the following:
427
+ - `stop`: model generated stop sequence
428
+ - `length`: model generated maximum number of tokens
429
+ - `content-filter`: content filter violation stopped the model
430
+ - `tool-calls`: model triggered tool calls
431
+ - `error`: model stopped because of an error
432
+ - `other`: model stopped for other reasons
433
+ */
434
+ type FinishReason = LanguageModelV1FinishReason;
435
+ /**
436
+ Warning from the model provider for this call. The call will proceed, but e.g.
437
+ some settings might not be supported, which can lead to suboptimal results.
438
+ */
439
+ type CallWarning = LanguageModelV1CallWarning;
423
440
  /**
424
441
  Tool choice for the generation. It supports the following settings:
425
442
 
@@ -433,6 +450,24 @@ type CoreToolChoice<TOOLS extends Record<string, unknown>> = 'auto' | 'none' | '
433
450
  toolName: keyof TOOLS;
434
451
  };
435
452
 
453
+ /**
454
+ Represents the number of tokens used in a prompt and completion.
455
+ */
456
+ type TokenUsage = {
457
+ /**
458
+ The number of tokens used in the prompt
459
+ */
460
+ promptTokens: number;
461
+ /**
462
+ The number of tokens used in the completion.
463
+ */
464
+ completionTokens: number;
465
+ /**
466
+ The total number of tokens used (promptTokens + completionTokens).
467
+ */
468
+ totalTokens: number;
469
+ };
470
+
436
471
  type Streamable = ReactNode | Promise<ReactNode>;
437
472
  type Renderer<T extends Array<any>> = (...args: T) => Streamable | Generator<Streamable, Streamable, void> | AsyncGenerator<Streamable, Streamable, void>;
438
473
  type RenderTool<PARAMETERS extends z.ZodTypeAny = any> = {
@@ -471,7 +506,7 @@ type RenderResult = {
471
506
  */
472
507
  declare function streamUI<TOOLS extends {
473
508
  [name: string]: z.ZodTypeAny;
474
- } = {}>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, initial, text, ...settings }: CallSettings & Prompt & {
509
+ } = {}>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, initial, text, onFinish, ...settings }: CallSettings & Prompt & {
475
510
  /**
476
511
  * The language model to use.
477
512
  */
@@ -483,11 +518,41 @@ declare function streamUI<TOOLS extends {
483
518
  [name in keyof TOOLS]: RenderTool<TOOLS[name]>;
484
519
  };
485
520
  /**
486
- The tool choice strategy. Default: 'auto'.
521
+ * The tool choice strategy. Default: 'auto'.
487
522
  */
488
523
  toolChoice?: CoreToolChoice<TOOLS>;
489
524
  text?: RenderText;
490
525
  initial?: ReactNode;
526
+ /**
527
+ * Callback that is called when the LLM response and the final object validation are finished.
528
+ */
529
+ onFinish?: (event: {
530
+ /**
531
+ * The reason why the generation finished.
532
+ */
533
+ finishReason: FinishReason;
534
+ /**
535
+ * The token usage of the generated response.
536
+ */
537
+ usage: TokenUsage;
538
+ /**
539
+ * The final ui node that was generated.
540
+ */
541
+ value: ReactNode;
542
+ /**
543
+ * Warnings from the model provider (e.g. unsupported settings)
544
+ */
545
+ warnings?: CallWarning[];
546
+ /**
547
+ * Optional raw response data.
548
+ */
549
+ rawResponse?: {
550
+ /**
551
+ * Response headers.
552
+ */
553
+ headers?: Record<string, string>;
554
+ };
555
+ }) => Promise<void> | void;
491
556
  }): Promise<RenderResult>;
492
557
 
493
558
  declare function createAI<AIState = any, UIState = any, Actions extends AIActions = {}>({ actions, initialAIState, initialUIState, onSetAIState, onGetUIState, }: {
@@ -210,6 +210,15 @@ async function _retryWithExponentialBackoff(f, {
210
210
  }
211
211
  }
212
212
 
213
+ // core/generate-text/token-usage.ts
214
+ function calculateTokenUsage(usage) {
215
+ return {
216
+ promptTokens: usage.promptTokens,
217
+ completionTokens: usage.completionTokens,
218
+ totalTokens: usage.promptTokens + usage.completionTokens
219
+ };
220
+ }
221
+
213
222
  // core/util/detect-image-mimetype.ts
214
223
  var mimeTypeSignatures = [
215
224
  { mimeType: "image/gif", bytes: [71, 73, 70] },
@@ -1429,6 +1438,7 @@ async function streamUI({
1429
1438
  abortSignal,
1430
1439
  initial,
1431
1440
  text,
1441
+ onFinish,
1432
1442
  ...settings
1433
1443
  }) {
1434
1444
  if (typeof model === "string") {
@@ -1591,6 +1601,13 @@ async function streamUI({
1591
1601
  throw value.error;
1592
1602
  }
1593
1603
  case "finish": {
1604
+ onFinish == null ? void 0 : onFinish({
1605
+ finishReason: value.finishReason,
1606
+ usage: calculateTokenUsage(value.usage),
1607
+ value: ui.value,
1608
+ warnings: result.warnings,
1609
+ rawResponse: result.rawResponse
1610
+ });
1594
1611
  }
1595
1612
  }
1596
1613
  }