ai 3.2.27 → 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.
@@ -1643,6 +1683,7 @@ var import_ui_utils2 = require("@ai-sdk/ui-utils");
1643
1683
  function runToolsTransformation({
1644
1684
  tools,
1645
1685
  generatorStream,
1686
+ toolCallStreaming,
1646
1687
  tracer
1647
1688
  }) {
1648
1689
  let canClose = false;
@@ -1653,6 +1694,7 @@ function runToolsTransformation({
1653
1694
  toolResultsStreamController = controller;
1654
1695
  }
1655
1696
  });
1697
+ const activeToolCalls = {};
1656
1698
  const forwardStream = new TransformStream({
1657
1699
  transform(chunk, controller) {
1658
1700
  const chunkType = chunk.type;
@@ -1662,6 +1704,25 @@ function runToolsTransformation({
1662
1704
  controller.enqueue(chunk);
1663
1705
  break;
1664
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
+ }
1665
1726
  case "tool-call": {
1666
1727
  const toolName = chunk.toolName;
1667
1728
  if (tools == null) {
@@ -1747,9 +1808,6 @@ function runToolsTransformation({
1747
1808
  });
1748
1809
  break;
1749
1810
  }
1750
- case "tool-call-delta": {
1751
- break;
1752
- }
1753
1811
  default: {
1754
1812
  const _exhaustiveCheck = chunkType;
1755
1813
  throw new Error(`Unhandled chunk type: ${_exhaustiveCheck}`);
@@ -1802,6 +1860,7 @@ async function streamText({
1802
1860
  abortSignal,
1803
1861
  headers,
1804
1862
  experimental_telemetry: telemetry,
1863
+ experimental_toolCallStreaming: toolCallStreaming = false,
1805
1864
  onFinish,
1806
1865
  ...settings
1807
1866
  }) {
@@ -1862,6 +1921,7 @@ async function streamText({
1862
1921
  stream: runToolsTransformation({
1863
1922
  tools,
1864
1923
  generatorStream: stream,
1924
+ toolCallStreaming,
1865
1925
  tracer
1866
1926
  }),
1867
1927
  warnings,
@@ -1939,6 +1999,8 @@ var StreamTextResult = class {
1939
1999
  resolveText(text);
1940
2000
  resolveToolCalls(toolCalls);
1941
2001
  break;
2002
+ case "tool-call-streaming-start":
2003
+ case "tool-call-delta":
1942
2004
  case "error":
1943
2005
  break;
1944
2006
  default: {
@@ -2082,10 +2144,27 @@ var StreamTextResult = class {
2082
2144
  });
2083
2145
  const streamDataTransformer = new TransformStream({
2084
2146
  transform: async (chunk, controller) => {
2085
- switch (chunk.type) {
2147
+ const chunkType = chunk.type;
2148
+ switch (chunkType) {
2086
2149
  case "text-delta":
2087
2150
  controller.enqueue((0, import_ui_utils6.formatStreamPart)("text", chunk.textDelta));
2088
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;
2089
2168
  case "tool-call":
2090
2169
  controller.enqueue(
2091
2170
  (0, import_ui_utils6.formatStreamPart)("tool_call", {
@@ -2110,6 +2189,12 @@ var StreamTextResult = class {
2110
2189
  (0, import_ui_utils6.formatStreamPart)("error", JSON.stringify(chunk.error))
2111
2190
  );
2112
2191
  break;
2192
+ case "finish":
2193
+ break;
2194
+ default: {
2195
+ const exhaustiveCheck = chunkType;
2196
+ throw new Error(`Unknown chunk type: ${exhaustiveCheck}`);
2197
+ }
2113
2198
  }
2114
2199
  }
2115
2200
  });
@@ -2272,6 +2357,13 @@ function convertToCoreMessages(messages) {
2272
2357
  experimental_attachments
2273
2358
  } of messages) {
2274
2359
  switch (role) {
2360
+ case "system": {
2361
+ coreMessages.push({
2362
+ role: "system",
2363
+ content
2364
+ });
2365
+ break;
2366
+ }
2275
2367
  case "user": {
2276
2368
  coreMessages.push({
2277
2369
  role: "user",