ai 3.2.26 → 3.2.28

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.mts CHANGED
@@ -706,15 +706,12 @@ The result of a `streamObject` call that contains the partial object stream and
706
706
  */
707
707
  declare class StreamObjectResult<T> {
708
708
  private readonly originalStream;
709
+ private readonly objectPromise;
709
710
  /**
710
711
  Warnings from the model provider (e.g. unsupported settings)
711
712
  */
712
713
  readonly warnings: CallWarning[] | undefined;
713
714
  /**
714
- The generated object (typed according to the schema). Resolved when the response is finished.
715
- */
716
- readonly object: Promise<T>;
717
- /**
718
715
  The token usage of the generated response. Resolved when the response is finished.
719
716
  */
720
717
  readonly usage: Promise<CompletionTokenUsage$1>;
@@ -737,6 +734,10 @@ declare class StreamObjectResult<T> {
737
734
  onFinish: Parameters<typeof streamObject<T>>[0]['onFinish'];
738
735
  });
739
736
  /**
737
+ The generated object (typed according to the schema). Resolved when the response is finished.
738
+ */
739
+ get object(): Promise<T>;
740
+ /**
740
741
  Stream of partial objects. It gets more complete as the stream progresses.
741
742
 
742
743
  Note that the partial object is not validated.
@@ -909,7 +910,7 @@ Converts an array of messages from useChat into an array of CoreMessages that ca
909
910
  with the AI core functions (e.g. `streamText`).
910
911
  */
911
912
  declare function convertToCoreMessages(messages: Array<{
912
- role: 'user' | 'assistant';
913
+ role: 'user' | 'assistant' | 'system';
913
914
  content: string;
914
915
  toolInvocations?: Array<ToolResult<string, unknown, unknown>>;
915
916
  experimental_attachments?: Attachment[];
@@ -1183,7 +1184,7 @@ If set and supported by the model, calls will generate deterministic results.
1183
1184
  @return
1184
1185
  A result object for accessing different stream types and additional information.
1185
1186
  */
1186
- declare function streamText<TOOLS extends Record<string, CoreTool>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, headers, experimental_telemetry: telemetry, onFinish, ...settings }: CallSettings & Prompt & {
1187
+ declare function streamText<TOOLS extends Record<string, CoreTool>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, headers, experimental_telemetry: telemetry, experimental_toolCallStreaming: toolCallStreaming, onFinish, ...settings }: CallSettings & Prompt & {
1187
1188
  /**
1188
1189
  The language model to use.
1189
1190
  */
@@ -1197,10 +1198,14 @@ The tool choice strategy. Default: 'auto'.
1197
1198
  */
1198
1199
  toolChoice?: CoreToolChoice<TOOLS>;
1199
1200
  /**
1200
- * Optional telemetry configuration (experimental).
1201
+ Optional telemetry configuration (experimental).
1201
1202
  */
1202
1203
  experimental_telemetry?: TelemetrySettings;
1203
1204
  /**
1205
+ Enable streaming of tool call deltas as they are generated. Disabled by default.
1206
+ */
1207
+ experimental_toolCallStreaming?: boolean;
1208
+ /**
1204
1209
  Callback that is called when the LLM response and all request tool executions
1205
1210
  (for tools that have an `execute` function) are finished.
1206
1211
  */
@@ -1246,8 +1251,14 @@ type TextStreamPart<TOOLS extends Record<string, CoreTool>> = {
1246
1251
  } | ({
1247
1252
  type: 'tool-call';
1248
1253
  } & ToToolCall<TOOLS>) | {
1249
- type: 'error';
1250
- error: unknown;
1254
+ type: 'tool-call-streaming-start';
1255
+ toolCallId: string;
1256
+ toolName: string;
1257
+ } | {
1258
+ type: 'tool-call-delta';
1259
+ toolCallId: string;
1260
+ toolName: string;
1261
+ argsTextDelta: string;
1251
1262
  } | ({
1252
1263
  type: 'tool-result';
1253
1264
  } & ToToolResult<TOOLS>) | {
@@ -1259,6 +1270,9 @@ type TextStreamPart<TOOLS extends Record<string, CoreTool>> = {
1259
1270
  completionTokens: number;
1260
1271
  totalTokens: number;
1261
1272
  };
1273
+ } | {
1274
+ type: 'error';
1275
+ error: unknown;
1262
1276
  };
1263
1277
  /**
1264
1278
  A result object for accessing different stream types and additional information.
package/dist/index.d.ts CHANGED
@@ -706,15 +706,12 @@ The result of a `streamObject` call that contains the partial object stream and
706
706
  */
707
707
  declare class StreamObjectResult<T> {
708
708
  private readonly originalStream;
709
+ private readonly objectPromise;
709
710
  /**
710
711
  Warnings from the model provider (e.g. unsupported settings)
711
712
  */
712
713
  readonly warnings: CallWarning[] | undefined;
713
714
  /**
714
- The generated object (typed according to the schema). Resolved when the response is finished.
715
- */
716
- readonly object: Promise<T>;
717
- /**
718
715
  The token usage of the generated response. Resolved when the response is finished.
719
716
  */
720
717
  readonly usage: Promise<CompletionTokenUsage$1>;
@@ -737,6 +734,10 @@ declare class StreamObjectResult<T> {
737
734
  onFinish: Parameters<typeof streamObject<T>>[0]['onFinish'];
738
735
  });
739
736
  /**
737
+ The generated object (typed according to the schema). Resolved when the response is finished.
738
+ */
739
+ get object(): Promise<T>;
740
+ /**
740
741
  Stream of partial objects. It gets more complete as the stream progresses.
741
742
 
742
743
  Note that the partial object is not validated.
@@ -909,7 +910,7 @@ Converts an array of messages from useChat into an array of CoreMessages that ca
909
910
  with the AI core functions (e.g. `streamText`).
910
911
  */
911
912
  declare function convertToCoreMessages(messages: Array<{
912
- role: 'user' | 'assistant';
913
+ role: 'user' | 'assistant' | 'system';
913
914
  content: string;
914
915
  toolInvocations?: Array<ToolResult<string, unknown, unknown>>;
915
916
  experimental_attachments?: Attachment[];
@@ -1183,7 +1184,7 @@ If set and supported by the model, calls will generate deterministic results.
1183
1184
  @return
1184
1185
  A result object for accessing different stream types and additional information.
1185
1186
  */
1186
- declare function streamText<TOOLS extends Record<string, CoreTool>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, headers, experimental_telemetry: telemetry, onFinish, ...settings }: CallSettings & Prompt & {
1187
+ declare function streamText<TOOLS extends Record<string, CoreTool>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, headers, experimental_telemetry: telemetry, experimental_toolCallStreaming: toolCallStreaming, onFinish, ...settings }: CallSettings & Prompt & {
1187
1188
  /**
1188
1189
  The language model to use.
1189
1190
  */
@@ -1197,10 +1198,14 @@ The tool choice strategy. Default: 'auto'.
1197
1198
  */
1198
1199
  toolChoice?: CoreToolChoice<TOOLS>;
1199
1200
  /**
1200
- * Optional telemetry configuration (experimental).
1201
+ Optional telemetry configuration (experimental).
1201
1202
  */
1202
1203
  experimental_telemetry?: TelemetrySettings;
1203
1204
  /**
1205
+ Enable streaming of tool call deltas as they are generated. Disabled by default.
1206
+ */
1207
+ experimental_toolCallStreaming?: boolean;
1208
+ /**
1204
1209
  Callback that is called when the LLM response and all request tool executions
1205
1210
  (for tools that have an `execute` function) are finished.
1206
1211
  */
@@ -1246,8 +1251,14 @@ type TextStreamPart<TOOLS extends Record<string, CoreTool>> = {
1246
1251
  } | ({
1247
1252
  type: 'tool-call';
1248
1253
  } & ToToolCall<TOOLS>) | {
1249
- type: 'error';
1250
- error: unknown;
1254
+ type: 'tool-call-streaming-start';
1255
+ toolCallId: string;
1256
+ toolName: string;
1257
+ } | {
1258
+ type: 'tool-call-delta';
1259
+ toolCallId: string;
1260
+ toolName: string;
1261
+ argsTextDelta: string;
1251
1262
  } | ({
1252
1263
  type: 'tool-result';
1253
1264
  } & ToToolResult<TOOLS>) | {
@@ -1259,6 +1270,9 @@ type TextStreamPart<TOOLS extends Record<string, CoreTool>> = {
1259
1270
  completionTokens: number;
1260
1271
  totalTokens: number;
1261
1272
  };
1273
+ } | {
1274
+ type: 'error';
1275
+ error: unknown;
1262
1276
  };
1263
1277
  /**
1264
1278
  A result object for accessing different stream types and additional information.
package/dist/index.js CHANGED
@@ -841,6 +841,44 @@ function createAsyncIterableStream(source, transformer) {
841
841
  return transformedStream;
842
842
  }
843
843
 
844
+ // core/util/delayed-promise.ts
845
+ var DelayedPromise = class {
846
+ constructor() {
847
+ this.status = { type: "pending" };
848
+ this._resolve = void 0;
849
+ this._reject = void 0;
850
+ }
851
+ get value() {
852
+ if (this.promise) {
853
+ return this.promise;
854
+ }
855
+ this.promise = new Promise((resolve, reject) => {
856
+ if (this.status.type === "resolved") {
857
+ resolve(this.status.value);
858
+ } else if (this.status.type === "rejected") {
859
+ reject(this.status.error);
860
+ }
861
+ this._resolve = resolve;
862
+ this._reject = reject;
863
+ });
864
+ return this.promise;
865
+ }
866
+ resolve(value) {
867
+ var _a;
868
+ this.status = { type: "resolved", value };
869
+ if (this.promise) {
870
+ (_a = this._resolve) == null ? void 0 : _a.call(this, value);
871
+ }
872
+ }
873
+ reject(error) {
874
+ var _a;
875
+ this.status = { type: "rejected", error };
876
+ if (this.promise) {
877
+ (_a = this._reject) == null ? void 0 : _a.call(this, error);
878
+ }
879
+ }
880
+ };
881
+
844
882
  // core/generate-object/stream-object.ts
845
883
  async function streamObject({
846
884
  model,
@@ -985,12 +1023,7 @@ var StreamObjectResult = class {
985
1023
  }) {
986
1024
  this.warnings = warnings;
987
1025
  this.rawResponse = rawResponse;
988
- let resolveObject;
989
- let rejectObject;
990
- this.object = new Promise((resolve, reject) => {
991
- resolveObject = resolve;
992
- rejectObject = reject;
993
- });
1026
+ this.objectPromise = new DelayedPromise();
994
1027
  let resolveUsage;
995
1028
  this.usage = new Promise((resolve) => {
996
1029
  resolveUsage = resolve;
@@ -1001,6 +1034,7 @@ var StreamObjectResult = class {
1001
1034
  let accumulatedText = "";
1002
1035
  let delta = "";
1003
1036
  let latestObject = void 0;
1037
+ const self = this;
1004
1038
  this.originalStream = stream.pipeThrough(
1005
1039
  new TransformStream({
1006
1040
  async transform(chunk, controller) {
@@ -1041,10 +1075,10 @@ var StreamObjectResult = class {
1041
1075
  });
1042
1076
  if (validationResult.success) {
1043
1077
  object = validationResult.value;
1044
- resolveObject(object);
1078
+ self.objectPromise.resolve(object);
1045
1079
  } else {
1046
1080
  error = validationResult.error;
1047
- rejectObject(error);
1081
+ self.objectPromise.reject(error);
1048
1082
  }
1049
1083
  break;
1050
1084
  }
@@ -1076,6 +1110,12 @@ var StreamObjectResult = class {
1076
1110
  );
1077
1111
  }
1078
1112
  /**
1113
+ The generated object (typed according to the schema). Resolved when the response is finished.
1114
+ */
1115
+ get object() {
1116
+ return this.objectPromise.value;
1117
+ }
1118
+ /**
1079
1119
  Stream of partial objects. It gets more complete as the stream progresses.
1080
1120
 
1081
1121
  Note that the partial object is not validated.
@@ -1457,6 +1497,11 @@ async function generateText({
1457
1497
  let roundtripCount = 0;
1458
1498
  const responseMessages = [];
1459
1499
  const roundtrips = [];
1500
+ const usage = {
1501
+ completionTokens: 0,
1502
+ promptTokens: 0,
1503
+ totalTokens: 0
1504
+ };
1460
1505
  do {
1461
1506
  const currentInputFormat = roundtripCount === 0 ? validatedPrompt.type : "messages";
1462
1507
  currentModelResponse = await retry(
@@ -1496,12 +1541,18 @@ async function generateText({
1496
1541
  tools,
1497
1542
  tracer
1498
1543
  });
1544
+ const currentUsage = calculateCompletionTokenUsage(
1545
+ currentModelResponse.usage
1546
+ );
1547
+ usage.completionTokens += currentUsage.completionTokens;
1548
+ usage.promptTokens += currentUsage.promptTokens;
1549
+ usage.totalTokens += currentUsage.totalTokens;
1499
1550
  roundtrips.push({
1500
1551
  text: (_b = currentModelResponse.text) != null ? _b : "",
1501
1552
  toolCalls: currentToolCalls,
1502
1553
  toolResults: currentToolResults,
1503
1554
  finishReason: currentModelResponse.finishReason,
1504
- usage: calculateCompletionTokenUsage(currentModelResponse.usage),
1555
+ usage: currentUsage,
1505
1556
  warnings: currentModelResponse.warnings,
1506
1557
  logprobs: currentModelResponse.logprobs
1507
1558
  });
@@ -1535,7 +1586,7 @@ async function generateText({
1535
1586
  toolCalls: currentToolCalls,
1536
1587
  toolResults: currentToolResults,
1537
1588
  finishReason: currentModelResponse.finishReason,
1538
- usage: calculateCompletionTokenUsage(currentModelResponse.usage),
1589
+ usage,
1539
1590
  warnings: currentModelResponse.warnings,
1540
1591
  rawResponse: currentModelResponse.rawResponse,
1541
1592
  logprobs: currentModelResponse.logprobs,
@@ -1632,6 +1683,7 @@ var import_ui_utils2 = require("@ai-sdk/ui-utils");
1632
1683
  function runToolsTransformation({
1633
1684
  tools,
1634
1685
  generatorStream,
1686
+ toolCallStreaming,
1635
1687
  tracer
1636
1688
  }) {
1637
1689
  let canClose = false;
@@ -1642,6 +1694,7 @@ function runToolsTransformation({
1642
1694
  toolResultsStreamController = controller;
1643
1695
  }
1644
1696
  });
1697
+ const activeToolCalls = {};
1645
1698
  const forwardStream = new TransformStream({
1646
1699
  transform(chunk, controller) {
1647
1700
  const chunkType = chunk.type;
@@ -1651,6 +1704,25 @@ function runToolsTransformation({
1651
1704
  controller.enqueue(chunk);
1652
1705
  break;
1653
1706
  }
1707
+ case "tool-call-delta": {
1708
+ if (toolCallStreaming) {
1709
+ if (!activeToolCalls[chunk.toolCallId]) {
1710
+ controller.enqueue({
1711
+ type: "tool-call-streaming-start",
1712
+ toolCallId: chunk.toolCallId,
1713
+ toolName: chunk.toolName
1714
+ });
1715
+ activeToolCalls[chunk.toolCallId] = true;
1716
+ }
1717
+ controller.enqueue({
1718
+ type: "tool-call-delta",
1719
+ toolCallId: chunk.toolCallId,
1720
+ toolName: chunk.toolName,
1721
+ argsTextDelta: chunk.argsTextDelta
1722
+ });
1723
+ }
1724
+ break;
1725
+ }
1654
1726
  case "tool-call": {
1655
1727
  const toolName = chunk.toolName;
1656
1728
  if (tools == null) {
@@ -1736,9 +1808,6 @@ function runToolsTransformation({
1736
1808
  });
1737
1809
  break;
1738
1810
  }
1739
- case "tool-call-delta": {
1740
- break;
1741
- }
1742
1811
  default: {
1743
1812
  const _exhaustiveCheck = chunkType;
1744
1813
  throw new Error(`Unhandled chunk type: ${_exhaustiveCheck}`);
@@ -1791,6 +1860,7 @@ async function streamText({
1791
1860
  abortSignal,
1792
1861
  headers,
1793
1862
  experimental_telemetry: telemetry,
1863
+ experimental_toolCallStreaming: toolCallStreaming = false,
1794
1864
  onFinish,
1795
1865
  ...settings
1796
1866
  }) {
@@ -1851,6 +1921,7 @@ async function streamText({
1851
1921
  stream: runToolsTransformation({
1852
1922
  tools,
1853
1923
  generatorStream: stream,
1924
+ toolCallStreaming,
1854
1925
  tracer
1855
1926
  }),
1856
1927
  warnings,
@@ -1928,6 +1999,8 @@ var StreamTextResult = class {
1928
1999
  resolveText(text);
1929
2000
  resolveToolCalls(toolCalls);
1930
2001
  break;
2002
+ case "tool-call-streaming-start":
2003
+ case "tool-call-delta":
1931
2004
  case "error":
1932
2005
  break;
1933
2006
  default: {
@@ -2071,10 +2144,27 @@ var StreamTextResult = class {
2071
2144
  });
2072
2145
  const streamDataTransformer = new TransformStream({
2073
2146
  transform: async (chunk, controller) => {
2074
- switch (chunk.type) {
2147
+ const chunkType = chunk.type;
2148
+ switch (chunkType) {
2075
2149
  case "text-delta":
2076
2150
  controller.enqueue((0, import_ui_utils6.formatStreamPart)("text", chunk.textDelta));
2077
2151
  break;
2152
+ case "tool-call-streaming-start":
2153
+ controller.enqueue(
2154
+ (0, import_ui_utils6.formatStreamPart)("tool_call_streaming_start", {
2155
+ toolCallId: chunk.toolCallId,
2156
+ toolName: chunk.toolName
2157
+ })
2158
+ );
2159
+ break;
2160
+ case "tool-call-delta":
2161
+ controller.enqueue(
2162
+ (0, import_ui_utils6.formatStreamPart)("tool_call_delta", {
2163
+ toolCallId: chunk.toolCallId,
2164
+ argsTextDelta: chunk.argsTextDelta
2165
+ })
2166
+ );
2167
+ break;
2078
2168
  case "tool-call":
2079
2169
  controller.enqueue(
2080
2170
  (0, import_ui_utils6.formatStreamPart)("tool_call", {
@@ -2099,6 +2189,12 @@ var StreamTextResult = class {
2099
2189
  (0, import_ui_utils6.formatStreamPart)("error", JSON.stringify(chunk.error))
2100
2190
  );
2101
2191
  break;
2192
+ case "finish":
2193
+ break;
2194
+ default: {
2195
+ const exhaustiveCheck = chunkType;
2196
+ throw new Error(`Unknown chunk type: ${exhaustiveCheck}`);
2197
+ }
2102
2198
  }
2103
2199
  }
2104
2200
  });
@@ -2261,6 +2357,13 @@ function convertToCoreMessages(messages) {
2261
2357
  experimental_attachments
2262
2358
  } of messages) {
2263
2359
  switch (role) {
2360
+ case "system": {
2361
+ coreMessages.push({
2362
+ role: "system",
2363
+ content
2364
+ });
2365
+ break;
2366
+ }
2264
2367
  case "user": {
2265
2368
  coreMessages.push({
2266
2369
  role: "user",