ai 3.3.38 → 3.3.40

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/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # ai
2
2
 
3
+ ## 3.3.40
4
+
5
+ ### Patch Changes
6
+
7
+ - a91c308: feat (ai/core): add responseMessages to streamText
8
+
9
+ ## 3.3.39
10
+
11
+ ### Patch Changes
12
+
13
+ - 33cf3e1: feat (ai/core): add providerMetadata to StepResult
14
+ - 17ee757: feat (ai/core): add onStepFinish callback to generateText
15
+
3
16
  ## 3.3.38
4
17
 
5
18
  ### Patch Changes
package/README.md CHANGED
@@ -148,6 +148,13 @@ async function submitMessage() {
148
148
  ui: stream.value,
149
149
  };
150
150
  }
151
+ ```
152
+
153
+ ###### @/app/ai.ts (Next.js App Router)
154
+
155
+ ```tsx
156
+ import { createAI } from 'ai/rsc';
157
+ import { submitMessage } from '@/app/actions';
151
158
 
152
159
  export const AI = createAI({
153
160
  initialAIState: {},
@@ -162,7 +169,7 @@ export const AI = createAI({
162
169
 
163
170
  ```tsx
164
171
  import { ReactNode } from 'react';
165
- import { AI } from '@/app/actions';
172
+ import { AI } from '@/app/ai';
166
173
 
167
174
  export default function Layout({ children }: { children: ReactNode }) {
168
175
  <AI>{children}</AI>;
package/dist/index.d.mts CHANGED
@@ -1356,6 +1356,12 @@ type StepResult<TOOLS extends Record<string, CoreTool>> = {
1356
1356
  Additional response information.
1357
1357
  */
1358
1358
  readonly response: LanguageModelResponseMetadataWithHeaders;
1359
+ /**
1360
+ Additional provider-specific metadata. They are passed through
1361
+ from the provider to the AI SDK and enable provider-specific
1362
+ results that can be fully encapsulated in the provider.
1363
+ */
1364
+ readonly experimental_providerMetadata: ProviderMetadata | undefined;
1359
1365
  };
1360
1366
 
1361
1367
  /**
@@ -1388,11 +1394,12 @@ interface GenerateTextResult<TOOLS extends Record<string, CoreTool>> {
1388
1394
  */
1389
1395
  readonly warnings: CallWarning[] | undefined;
1390
1396
  /**
1391
- The response messages that were generated during the call. It consists of an assistant message,
1392
- potentially containing tool calls.
1393
- When there are tool results, there is an additional tool message with the tool results that are available.
1394
- If there are tools that do not have execute functions, they are not included in the tool results and
1395
- need to be added separately.
1397
+ The response messages that were generated during the call. It consists of an assistant message,
1398
+ potentially containing tool calls.
1399
+
1400
+ When there are tool results, there is an additional tool message with the tool results that are available.
1401
+ If there are tools that do not have execute functions, they are not included in the tool results and
1402
+ need to be added separately.
1396
1403
  */
1397
1404
  readonly responseMessages: Array<CoreAssistantMessage | CoreToolMessage>;
1398
1405
  /**
@@ -1479,10 +1486,12 @@ If set and supported by the model, calls will generate deterministic results.
1479
1486
 
1480
1487
  @param maxSteps - Maximum number of sequential LLM calls (steps), e.g. when you use tool calls.
1481
1488
 
1489
+ @param onStepFinish - Callback that is called when each step (LLM call) is finished, including intermediate steps.
1490
+
1482
1491
  @returns
1483
1492
  A result object that contains the generated text, the results of the tool calls, and additional information.
1484
1493
  */
1485
- declare function generateText<TOOLS extends Record<string, CoreTool>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, headers, maxAutomaticRoundtrips, maxToolRoundtrips, maxSteps, experimental_telemetry: telemetry, experimental_providerMetadata: providerMetadata, _internal: { generateId, currentDate, }, ...settings }: CallSettings & Prompt & {
1494
+ declare function generateText<TOOLS extends Record<string, CoreTool>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, headers, maxAutomaticRoundtrips, maxToolRoundtrips, maxSteps, experimental_telemetry: telemetry, experimental_providerMetadata: providerMetadata, _internal: { generateId, currentDate, }, onStepFinish, ...settings }: CallSettings & Prompt & {
1486
1495
  /**
1487
1496
  The language model to use.
1488
1497
  */
@@ -1532,6 +1541,10 @@ to the provider from the AI SDK and enable provider-specific
1532
1541
  functionality that can be fully encapsulated in the provider.
1533
1542
  */
1534
1543
  experimental_providerMetadata?: ProviderMetadata;
1544
+ /**
1545
+ Callback that is called when each step (LLM call) is finished, including intermediate steps.
1546
+ */
1547
+ onStepFinish?: (event: StepResult<TOOLS>) => Promise<void> | void;
1535
1548
  /**
1536
1549
  * Internal. For test use only. May change without notice.
1537
1550
  */
@@ -1602,6 +1615,17 @@ interface StreamTextResult<TOOLS extends Record<string, CoreTool>> {
1602
1615
  headers?: Record<string, string>;
1603
1616
  };
1604
1617
  /**
1618
+ The response messages that were generated during the call. It consists of an assistant message,
1619
+ potentially containing tool calls.
1620
+
1621
+ When there are tool results, there is an additional tool message with the tool results that are available.
1622
+ If there are tools that do not have execute functions, they are not included in the tool results and
1623
+ need to be added separately.
1624
+
1625
+ Resolved when the response is finished.
1626
+ */
1627
+ readonly responseMessages: Promise<Array<CoreAssistantMessage | CoreToolMessage>>;
1628
+ /**
1605
1629
  Details for all steps.
1606
1630
  You can use this to get information about intermediate steps,
1607
1631
  such as the tool calls or the response headers.
@@ -1800,6 +1824,7 @@ If set and supported by the model, calls will generate deterministic results.
1800
1824
  @param maxSteps - Maximum number of sequential LLM calls (steps), e.g. when you use tool calls.
1801
1825
 
1802
1826
  @param onChunk - Callback that is called for each chunk of the stream. The stream processing will pause until the callback promise is resolved.
1827
+ @param onStepFinish - Callback that is called when each step (LLM call) is finished, including intermediate steps.
1803
1828
  @param onFinish - Callback that is called when the LLM response and all request tool executions
1804
1829
  (for tools that have an `execute` function) are finished.
1805
1830
 
@@ -1867,18 +1892,23 @@ Callback that is called for each chunk of the stream. The stream processing will
1867
1892
  /**
1868
1893
  Callback that is called when the LLM response and all request tool executions
1869
1894
  (for tools that have an `execute` function) are finished.
1895
+
1896
+ The usage is the combined usage of all steps.
1870
1897
  */
1871
1898
  onFinish?: (event: StepResult<TOOLS> & {
1872
1899
  /**
1873
1900
  Details for all steps.
1874
1901
  */
1875
- steps: StepResult<TOOLS>[];
1902
+ readonly steps: StepResult<TOOLS>[];
1876
1903
  /**
1877
- Additional provider-specific metadata. They are passed through
1878
- from the provider to the AI SDK and enable provider-specific
1879
- results that can be fully encapsulated in the provider.
1880
- */
1881
- readonly experimental_providerMetadata: ProviderMetadata | undefined;
1904
+ The response messages that were generated during the call. It consists of an assistant message,
1905
+ potentially containing tool calls.
1906
+
1907
+ When there are tool results, there is an additional tool message with the tool results that are available.
1908
+ If there are tools that do not have execute functions, they are not included in the tool results and
1909
+ need to be added separately.
1910
+ */
1911
+ readonly responseMessages: Array<CoreAssistantMessage | CoreToolMessage>;
1882
1912
  }) => Promise<void> | void;
1883
1913
  /**
1884
1914
  Callback that is called when each step (LLM call) is finished, including intermediate steps.
package/dist/index.d.ts CHANGED
@@ -1356,6 +1356,12 @@ type StepResult<TOOLS extends Record<string, CoreTool>> = {
1356
1356
  Additional response information.
1357
1357
  */
1358
1358
  readonly response: LanguageModelResponseMetadataWithHeaders;
1359
+ /**
1360
+ Additional provider-specific metadata. They are passed through
1361
+ from the provider to the AI SDK and enable provider-specific
1362
+ results that can be fully encapsulated in the provider.
1363
+ */
1364
+ readonly experimental_providerMetadata: ProviderMetadata | undefined;
1359
1365
  };
1360
1366
 
1361
1367
  /**
@@ -1388,11 +1394,12 @@ interface GenerateTextResult<TOOLS extends Record<string, CoreTool>> {
1388
1394
  */
1389
1395
  readonly warnings: CallWarning[] | undefined;
1390
1396
  /**
1391
- The response messages that were generated during the call. It consists of an assistant message,
1392
- potentially containing tool calls.
1393
- When there are tool results, there is an additional tool message with the tool results that are available.
1394
- If there are tools that do not have execute functions, they are not included in the tool results and
1395
- need to be added separately.
1397
+ The response messages that were generated during the call. It consists of an assistant message,
1398
+ potentially containing tool calls.
1399
+
1400
+ When there are tool results, there is an additional tool message with the tool results that are available.
1401
+ If there are tools that do not have execute functions, they are not included in the tool results and
1402
+ need to be added separately.
1396
1403
  */
1397
1404
  readonly responseMessages: Array<CoreAssistantMessage | CoreToolMessage>;
1398
1405
  /**
@@ -1479,10 +1486,12 @@ If set and supported by the model, calls will generate deterministic results.
1479
1486
 
1480
1487
  @param maxSteps - Maximum number of sequential LLM calls (steps), e.g. when you use tool calls.
1481
1488
 
1489
+ @param onStepFinish - Callback that is called when each step (LLM call) is finished, including intermediate steps.
1490
+
1482
1491
  @returns
1483
1492
  A result object that contains the generated text, the results of the tool calls, and additional information.
1484
1493
  */
1485
- declare function generateText<TOOLS extends Record<string, CoreTool>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, headers, maxAutomaticRoundtrips, maxToolRoundtrips, maxSteps, experimental_telemetry: telemetry, experimental_providerMetadata: providerMetadata, _internal: { generateId, currentDate, }, ...settings }: CallSettings & Prompt & {
1494
+ declare function generateText<TOOLS extends Record<string, CoreTool>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, headers, maxAutomaticRoundtrips, maxToolRoundtrips, maxSteps, experimental_telemetry: telemetry, experimental_providerMetadata: providerMetadata, _internal: { generateId, currentDate, }, onStepFinish, ...settings }: CallSettings & Prompt & {
1486
1495
  /**
1487
1496
  The language model to use.
1488
1497
  */
@@ -1532,6 +1541,10 @@ to the provider from the AI SDK and enable provider-specific
1532
1541
  functionality that can be fully encapsulated in the provider.
1533
1542
  */
1534
1543
  experimental_providerMetadata?: ProviderMetadata;
1544
+ /**
1545
+ Callback that is called when each step (LLM call) is finished, including intermediate steps.
1546
+ */
1547
+ onStepFinish?: (event: StepResult<TOOLS>) => Promise<void> | void;
1535
1548
  /**
1536
1549
  * Internal. For test use only. May change without notice.
1537
1550
  */
@@ -1602,6 +1615,17 @@ interface StreamTextResult<TOOLS extends Record<string, CoreTool>> {
1602
1615
  headers?: Record<string, string>;
1603
1616
  };
1604
1617
  /**
1618
+ The response messages that were generated during the call. It consists of an assistant message,
1619
+ potentially containing tool calls.
1620
+
1621
+ When there are tool results, there is an additional tool message with the tool results that are available.
1622
+ If there are tools that do not have execute functions, they are not included in the tool results and
1623
+ need to be added separately.
1624
+
1625
+ Resolved when the response is finished.
1626
+ */
1627
+ readonly responseMessages: Promise<Array<CoreAssistantMessage | CoreToolMessage>>;
1628
+ /**
1605
1629
  Details for all steps.
1606
1630
  You can use this to get information about intermediate steps,
1607
1631
  such as the tool calls or the response headers.
@@ -1800,6 +1824,7 @@ If set and supported by the model, calls will generate deterministic results.
1800
1824
  @param maxSteps - Maximum number of sequential LLM calls (steps), e.g. when you use tool calls.
1801
1825
 
1802
1826
  @param onChunk - Callback that is called for each chunk of the stream. The stream processing will pause until the callback promise is resolved.
1827
+ @param onStepFinish - Callback that is called when each step (LLM call) is finished, including intermediate steps.
1803
1828
  @param onFinish - Callback that is called when the LLM response and all request tool executions
1804
1829
  (for tools that have an `execute` function) are finished.
1805
1830
 
@@ -1867,18 +1892,23 @@ Callback that is called for each chunk of the stream. The stream processing will
1867
1892
  /**
1868
1893
  Callback that is called when the LLM response and all request tool executions
1869
1894
  (for tools that have an `execute` function) are finished.
1895
+
1896
+ The usage is the combined usage of all steps.
1870
1897
  */
1871
1898
  onFinish?: (event: StepResult<TOOLS> & {
1872
1899
  /**
1873
1900
  Details for all steps.
1874
1901
  */
1875
- steps: StepResult<TOOLS>[];
1902
+ readonly steps: StepResult<TOOLS>[];
1876
1903
  /**
1877
- Additional provider-specific metadata. They are passed through
1878
- from the provider to the AI SDK and enable provider-specific
1879
- results that can be fully encapsulated in the provider.
1880
- */
1881
- readonly experimental_providerMetadata: ProviderMetadata | undefined;
1904
+ The response messages that were generated during the call. It consists of an assistant message,
1905
+ potentially containing tool calls.
1906
+
1907
+ When there are tool results, there is an additional tool message with the tool results that are available.
1908
+ If there are tools that do not have execute functions, they are not included in the tool results and
1909
+ need to be added separately.
1910
+ */
1911
+ readonly responseMessages: Array<CoreAssistantMessage | CoreToolMessage>;
1882
1912
  }) => Promise<void> | void;
1883
1913
  /**
1884
1914
  Callback that is called when each step (LLM call) is finished, including intermediate steps.
package/dist/index.js CHANGED
@@ -3059,6 +3059,7 @@ async function generateText({
3059
3059
  generateId: generateId3 = originalGenerateId3,
3060
3060
  currentDate = () => /* @__PURE__ */ new Date()
3061
3061
  } = {},
3062
+ onStepFinish,
3062
3063
  ...settings
3063
3064
  }) {
3064
3065
  var _a11;
@@ -3220,7 +3221,7 @@ async function generateText({
3220
3221
  usage.completionTokens += currentUsage.completionTokens;
3221
3222
  usage.promptTokens += currentUsage.promptTokens;
3222
3223
  usage.totalTokens += currentUsage.totalTokens;
3223
- steps.push({
3224
+ const currentStep = {
3224
3225
  text: (_b = currentModelResponse.text) != null ? _b : "",
3225
3226
  toolCalls: currentToolCalls,
3226
3227
  toolResults: currentToolResults,
@@ -3231,8 +3232,11 @@ async function generateText({
3231
3232
  response: {
3232
3233
  ...currentModelResponse.response,
3233
3234
  headers: (_c = currentModelResponse.rawResponse) == null ? void 0 : _c.headers
3234
- }
3235
- });
3235
+ },
3236
+ experimental_providerMetadata: currentModelResponse.providerMetadata
3237
+ };
3238
+ steps.push(currentStep);
3239
+ await (onStepFinish == null ? void 0 : onStepFinish(currentStep));
3236
3240
  const newResponseMessages = toResponseMessages({
3237
3241
  text: currentModelResponse.text,
3238
3242
  toolCalls: currentToolCalls,
@@ -3927,6 +3931,11 @@ var DefaultStreamTextResult = class {
3927
3931
  this.experimental_providerMetadata = providerMetadataPromise;
3928
3932
  const { resolve: resolveResponse, promise: responsePromise } = createResolvablePromise();
3929
3933
  this.response = responsePromise;
3934
+ const {
3935
+ resolve: resolveResponseMessages,
3936
+ promise: responseMessagesPromise
3937
+ } = createResolvablePromise();
3938
+ this.responseMessages = responseMessagesPromise;
3930
3939
  const {
3931
3940
  stream: stitchableStream,
3932
3941
  addStream,
@@ -4098,7 +4107,8 @@ var DefaultStreamTextResult = class {
4098
4107
  warnings: self.warnings,
4099
4108
  logprobs: stepLogProbs,
4100
4109
  response: stepResponse,
4101
- rawResponse: self.rawResponse
4110
+ rawResponse: self.rawResponse,
4111
+ experimental_providerMetadata: stepProviderMetadata
4102
4112
  };
4103
4113
  stepResults.push(stepResult);
4104
4114
  await (onStepFinish == null ? void 0 : onStepFinish(stepResult));
@@ -4172,6 +4182,17 @@ var DefaultStreamTextResult = class {
4172
4182
  }
4173
4183
  })
4174
4184
  );
4185
+ const responseMessages = stepResults.reduce(
4186
+ (responseMessages2, step) => [
4187
+ ...responseMessages2,
4188
+ ...toResponseMessages({
4189
+ text: step.text,
4190
+ toolCalls: step.toolCalls,
4191
+ toolResults: step.toolResults
4192
+ })
4193
+ ],
4194
+ []
4195
+ );
4175
4196
  resolveUsage(combinedUsage);
4176
4197
  resolveFinishReason(stepFinishReason);
4177
4198
  resolveText(stepText);
@@ -4183,6 +4204,7 @@ var DefaultStreamTextResult = class {
4183
4204
  headers: rawResponse == null ? void 0 : rawResponse.headers
4184
4205
  });
4185
4206
  resolveSteps(stepResults);
4207
+ resolveResponseMessages(responseMessages);
4186
4208
  await (onFinish == null ? void 0 : onFinish({
4187
4209
  finishReason: stepFinishReason,
4188
4210
  logprobs: stepLogProbs,
@@ -4201,8 +4223,8 @@ var DefaultStreamTextResult = class {
4201
4223
  },
4202
4224
  warnings,
4203
4225
  experimental_providerMetadata: stepProviderMetadata,
4204
- steps: stepResults
4205
- // see tool results comment above
4226
+ steps: stepResults,
4227
+ responseMessages
4206
4228
  }));
4207
4229
  } catch (error) {
4208
4230
  controller.error(error);