ai 4.0.19 → 4.0.21

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.mjs CHANGED
@@ -32,15 +32,19 @@ function createDataStream({
32
32
  controller = controllerArg;
33
33
  }
34
34
  });
35
+ function safeEnqueue(data) {
36
+ try {
37
+ controller.enqueue(data);
38
+ } catch (error) {
39
+ }
40
+ }
35
41
  try {
36
42
  const result = execute({
37
43
  writeData(data) {
38
- controller.enqueue(formatDataStreamPart("data", [data]));
44
+ safeEnqueue(formatDataStreamPart("data", [data]));
39
45
  },
40
46
  writeMessageAnnotation(annotation) {
41
- controller.enqueue(
42
- formatDataStreamPart("message_annotations", [annotation])
43
- );
47
+ safeEnqueue(formatDataStreamPart("message_annotations", [annotation]));
44
48
  },
45
49
  merge(streamArg) {
46
50
  ongoingStreamPromises.push(
@@ -50,10 +54,10 @@ function createDataStream({
50
54
  const { done, value } = await reader.read();
51
55
  if (done)
52
56
  break;
53
- controller.enqueue(value);
57
+ safeEnqueue(value);
54
58
  }
55
59
  })().catch((error) => {
56
- controller.enqueue(formatDataStreamPart("error", onError(error)));
60
+ safeEnqueue(formatDataStreamPart("error", onError(error)));
57
61
  })
58
62
  );
59
63
  },
@@ -62,12 +66,12 @@ function createDataStream({
62
66
  if (result) {
63
67
  ongoingStreamPromises.push(
64
68
  result.catch((error) => {
65
- controller.enqueue(formatDataStreamPart("error", onError(error)));
69
+ safeEnqueue(formatDataStreamPart("error", onError(error)));
66
70
  })
67
71
  );
68
72
  }
69
73
  } catch (error) {
70
- controller.enqueue(formatDataStreamPart("error", onError(error)));
74
+ safeEnqueue(formatDataStreamPart("error", onError(error)));
71
75
  }
72
76
  const waitForStreams = new Promise(async (resolve) => {
73
77
  while (ongoingStreamPromises.length > 0) {
@@ -76,7 +80,10 @@ function createDataStream({
76
80
  resolve();
77
81
  });
78
82
  waitForStreams.finally(() => {
79
- controller.close();
83
+ try {
84
+ controller.close();
85
+ } catch (error) {
86
+ }
80
87
  });
81
88
  return stream;
82
89
  }
@@ -1162,6 +1169,7 @@ async function downloadAssets(messages, downloadImplementation, modelSupportsIma
1162
1169
  );
1163
1170
  }
1164
1171
  function convertPartToLanguageModelPart(part, downloadedAssets) {
1172
+ var _a13;
1165
1173
  if (part.type === "text") {
1166
1174
  return {
1167
1175
  type: "text",
@@ -1212,9 +1220,9 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
1212
1220
  normalizedData = convertDataContentToUint8Array(content);
1213
1221
  }
1214
1222
  switch (type) {
1215
- case "image":
1216
- if (mimeType == null && normalizedData instanceof Uint8Array) {
1217
- mimeType = detectImageMimeType(normalizedData);
1223
+ case "image": {
1224
+ if (normalizedData instanceof Uint8Array) {
1225
+ mimeType = (_a13 = detectImageMimeType(normalizedData)) != null ? _a13 : mimeType;
1218
1226
  }
1219
1227
  return {
1220
1228
  type: "image",
@@ -1222,7 +1230,8 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
1222
1230
  mimeType,
1223
1231
  providerMetadata: part.experimental_providerMetadata
1224
1232
  };
1225
- case "file":
1233
+ }
1234
+ case "file": {
1226
1235
  if (mimeType == null) {
1227
1236
  throw new Error(`Mime type is missing for file part`);
1228
1237
  }
@@ -1232,6 +1241,7 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
1232
1241
  mimeType,
1233
1242
  providerMetadata: part.experimental_providerMetadata
1234
1243
  };
1244
+ }
1235
1245
  }
1236
1246
  }
1237
1247
 
@@ -1783,12 +1793,10 @@ import { safeValidateTypes as safeValidateTypes2 } from "@ai-sdk/provider-utils"
1783
1793
  import { asSchema } from "@ai-sdk/ui-utils";
1784
1794
 
1785
1795
  // core/util/async-iterable-stream.ts
1786
- function createAsyncIterableStream(source, transformer) {
1787
- const transformedStream = source.pipeThrough(
1788
- new TransformStream(transformer)
1789
- );
1790
- transformedStream[Symbol.asyncIterator] = () => {
1791
- const reader = transformedStream.getReader();
1796
+ function createAsyncIterableStream(source) {
1797
+ const stream = source.pipeThrough(new TransformStream());
1798
+ stream[Symbol.asyncIterator] = () => {
1799
+ const reader = stream.getReader();
1792
1800
  return {
1793
1801
  async next() {
1794
1802
  const { done, value } = await reader.read();
@@ -1796,7 +1804,7 @@ function createAsyncIterableStream(source, transformer) {
1796
1804
  }
1797
1805
  };
1798
1806
  };
1799
- return transformedStream;
1807
+ return stream;
1800
1808
  }
1801
1809
 
1802
1810
  // core/generate-object/output-strategy.ts
@@ -1926,29 +1934,35 @@ var arrayOutputStrategy = (schema) => {
1926
1934
  },
1927
1935
  createElementStream(originalStream) {
1928
1936
  let publishedElements = 0;
1929
- return createAsyncIterableStream(originalStream, {
1930
- transform(chunk, controller) {
1931
- switch (chunk.type) {
1932
- case "object": {
1933
- const array = chunk.object;
1934
- for (; publishedElements < array.length; publishedElements++) {
1935
- controller.enqueue(array[publishedElements]);
1937
+ return createAsyncIterableStream(
1938
+ originalStream.pipeThrough(
1939
+ new TransformStream({
1940
+ transform(chunk, controller) {
1941
+ switch (chunk.type) {
1942
+ case "object": {
1943
+ const array = chunk.object;
1944
+ for (; publishedElements < array.length; publishedElements++) {
1945
+ controller.enqueue(array[publishedElements]);
1946
+ }
1947
+ break;
1948
+ }
1949
+ case "text-delta":
1950
+ case "finish":
1951
+ break;
1952
+ case "error":
1953
+ controller.error(chunk.error);
1954
+ break;
1955
+ default: {
1956
+ const _exhaustiveCheck = chunk;
1957
+ throw new Error(
1958
+ `Unsupported chunk type: ${_exhaustiveCheck}`
1959
+ );
1960
+ }
1936
1961
  }
1937
- break;
1938
- }
1939
- case "text-delta":
1940
- case "finish":
1941
- break;
1942
- case "error":
1943
- controller.error(chunk.error);
1944
- break;
1945
- default: {
1946
- const _exhaustiveCheck = chunk;
1947
- throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
1948
1962
  }
1949
- }
1950
- }
1951
- });
1963
+ })
1964
+ )
1965
+ );
1952
1966
  }
1953
1967
  };
1954
1968
  };
@@ -3147,25 +3161,29 @@ var DefaultStreamObjectResult = class {
3147
3161
  return this.responsePromise.value;
3148
3162
  }
3149
3163
  get partialObjectStream() {
3150
- return createAsyncIterableStream(this.stitchableStream.stream, {
3151
- transform(chunk, controller) {
3152
- switch (chunk.type) {
3153
- case "object":
3154
- controller.enqueue(chunk.object);
3155
- break;
3156
- case "text-delta":
3157
- case "finish":
3158
- break;
3159
- case "error":
3160
- controller.error(chunk.error);
3161
- break;
3162
- default: {
3163
- const _exhaustiveCheck = chunk;
3164
- throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
3164
+ return createAsyncIterableStream(
3165
+ this.stitchableStream.stream.pipeThrough(
3166
+ new TransformStream({
3167
+ transform(chunk, controller) {
3168
+ switch (chunk.type) {
3169
+ case "object":
3170
+ controller.enqueue(chunk.object);
3171
+ break;
3172
+ case "text-delta":
3173
+ case "finish":
3174
+ break;
3175
+ case "error":
3176
+ controller.error(chunk.error);
3177
+ break;
3178
+ default: {
3179
+ const _exhaustiveCheck = chunk;
3180
+ throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
3181
+ }
3182
+ }
3165
3183
  }
3166
- }
3167
- }
3168
- });
3184
+ })
3185
+ )
3186
+ );
3169
3187
  }
3170
3188
  get elementStream() {
3171
3189
  return this.outputStrategy.createElementStream(
@@ -3173,32 +3191,32 @@ var DefaultStreamObjectResult = class {
3173
3191
  );
3174
3192
  }
3175
3193
  get textStream() {
3176
- return createAsyncIterableStream(this.stitchableStream.stream, {
3177
- transform(chunk, controller) {
3178
- switch (chunk.type) {
3179
- case "text-delta":
3180
- controller.enqueue(chunk.textDelta);
3181
- break;
3182
- case "object":
3183
- case "finish":
3184
- break;
3185
- case "error":
3186
- controller.error(chunk.error);
3187
- break;
3188
- default: {
3189
- const _exhaustiveCheck = chunk;
3190
- throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
3194
+ return createAsyncIterableStream(
3195
+ this.stitchableStream.stream.pipeThrough(
3196
+ new TransformStream({
3197
+ transform(chunk, controller) {
3198
+ switch (chunk.type) {
3199
+ case "text-delta":
3200
+ controller.enqueue(chunk.textDelta);
3201
+ break;
3202
+ case "object":
3203
+ case "finish":
3204
+ break;
3205
+ case "error":
3206
+ controller.error(chunk.error);
3207
+ break;
3208
+ default: {
3209
+ const _exhaustiveCheck = chunk;
3210
+ throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
3211
+ }
3212
+ }
3191
3213
  }
3192
- }
3193
- }
3194
- });
3214
+ })
3215
+ )
3216
+ );
3195
3217
  }
3196
3218
  get fullStream() {
3197
- return createAsyncIterableStream(this.stitchableStream.stream, {
3198
- transform(chunk, controller) {
3199
- controller.enqueue(chunk);
3200
- }
3201
- });
3219
+ return createAsyncIterableStream(this.stitchableStream.stream);
3202
3220
  }
3203
3221
  pipeTextStreamToResponse(response, init) {
3204
3222
  writeToServerResponse({
@@ -3883,6 +3901,7 @@ async function executeTools({
3883
3901
  }
3884
3902
  });
3885
3903
  return {
3904
+ type: "tool-result",
3886
3905
  toolCallId,
3887
3906
  toolName,
3888
3907
  args,
@@ -4127,25 +4146,6 @@ function runToolsTransformation({
4127
4146
  break;
4128
4147
  }
4129
4148
  case "tool-call": {
4130
- const toolName = chunk.toolName;
4131
- if (tools == null) {
4132
- toolResultsStreamController.enqueue({
4133
- type: "error",
4134
- error: new NoSuchToolError({ toolName: chunk.toolName })
4135
- });
4136
- break;
4137
- }
4138
- const tool2 = tools[toolName];
4139
- if (tool2 == null) {
4140
- toolResultsStreamController.enqueue({
4141
- type: "error",
4142
- error: new NoSuchToolError({
4143
- toolName: chunk.toolName,
4144
- availableTools: Object.keys(tools)
4145
- })
4146
- });
4147
- break;
4148
- }
4149
4149
  try {
4150
4150
  const toolCall = await parseToolCall({
4151
4151
  toolCall: chunk,
@@ -4155,6 +4155,7 @@ function runToolsTransformation({
4155
4155
  messages
4156
4156
  });
4157
4157
  controller.enqueue(toolCall);
4158
+ const tool2 = tools[toolCall.toolName];
4158
4159
  if (tool2.execute != null) {
4159
4160
  const toolExecutionId = generateId();
4160
4161
  outstandingToolResults.add(toolExecutionId);
@@ -4978,22 +4979,22 @@ var DefaultStreamTextResult = class {
4978
4979
  return stream1;
4979
4980
  }
4980
4981
  get textStream() {
4981
- return createAsyncIterableStream(this.teeStream(), {
4982
- transform(chunk, controller) {
4983
- if (chunk.type === "text-delta") {
4984
- controller.enqueue(chunk.textDelta);
4985
- } else if (chunk.type === "error") {
4986
- controller.error(chunk.error);
4987
- }
4988
- }
4989
- });
4982
+ return createAsyncIterableStream(
4983
+ this.teeStream().pipeThrough(
4984
+ new TransformStream({
4985
+ transform(chunk, controller) {
4986
+ if (chunk.type === "text-delta") {
4987
+ controller.enqueue(chunk.textDelta);
4988
+ } else if (chunk.type === "error") {
4989
+ controller.error(chunk.error);
4990
+ }
4991
+ }
4992
+ })
4993
+ )
4994
+ );
4990
4995
  }
4991
4996
  get fullStream() {
4992
- return createAsyncIterableStream(this.teeStream(), {
4993
- transform(chunk, controller) {
4994
- controller.enqueue(chunk);
4995
- }
4996
- });
4997
+ return createAsyncIterableStream(this.teeStream());
4997
4998
  }
4998
4999
  toDataStreamInternal({
4999
5000
  getErrorMessage: getErrorMessage5 = () => "An error occurred.",