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/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # ai
2
2
 
3
+ ## 4.0.21
4
+
5
+ ### Patch Changes
6
+
7
+ - a8669a2: fix (ai/core): prefer auto-detected image mimetype
8
+ - 6fb3e91: fix (ai/core): include type in generateText toolResults result property.
9
+
10
+ ## 4.0.20
11
+
12
+ ### Patch Changes
13
+
14
+ - da9d240: fix (ai/core): suppress errors caused by writing to closed stream
15
+ - 6f1bfde: fix (ai/core): invoke streamText tool call repair when tool cannot be found
16
+
3
17
  ## 4.0.19
4
18
 
5
19
  ### Patch Changes
package/dist/index.js CHANGED
@@ -95,15 +95,19 @@ function createDataStream({
95
95
  controller = controllerArg;
96
96
  }
97
97
  });
98
+ function safeEnqueue(data) {
99
+ try {
100
+ controller.enqueue(data);
101
+ } catch (error) {
102
+ }
103
+ }
98
104
  try {
99
105
  const result = execute({
100
106
  writeData(data) {
101
- controller.enqueue((0, import_ui_utils.formatDataStreamPart)("data", [data]));
107
+ safeEnqueue((0, import_ui_utils.formatDataStreamPart)("data", [data]));
102
108
  },
103
109
  writeMessageAnnotation(annotation) {
104
- controller.enqueue(
105
- (0, import_ui_utils.formatDataStreamPart)("message_annotations", [annotation])
106
- );
110
+ safeEnqueue((0, import_ui_utils.formatDataStreamPart)("message_annotations", [annotation]));
107
111
  },
108
112
  merge(streamArg) {
109
113
  ongoingStreamPromises.push(
@@ -113,10 +117,10 @@ function createDataStream({
113
117
  const { done, value } = await reader.read();
114
118
  if (done)
115
119
  break;
116
- controller.enqueue(value);
120
+ safeEnqueue(value);
117
121
  }
118
122
  })().catch((error) => {
119
- controller.enqueue((0, import_ui_utils.formatDataStreamPart)("error", onError(error)));
123
+ safeEnqueue((0, import_ui_utils.formatDataStreamPart)("error", onError(error)));
120
124
  })
121
125
  );
122
126
  },
@@ -125,12 +129,12 @@ function createDataStream({
125
129
  if (result) {
126
130
  ongoingStreamPromises.push(
127
131
  result.catch((error) => {
128
- controller.enqueue((0, import_ui_utils.formatDataStreamPart)("error", onError(error)));
132
+ safeEnqueue((0, import_ui_utils.formatDataStreamPart)("error", onError(error)));
129
133
  })
130
134
  );
131
135
  }
132
136
  } catch (error) {
133
- controller.enqueue((0, import_ui_utils.formatDataStreamPart)("error", onError(error)));
137
+ safeEnqueue((0, import_ui_utils.formatDataStreamPart)("error", onError(error)));
134
138
  }
135
139
  const waitForStreams = new Promise(async (resolve) => {
136
140
  while (ongoingStreamPromises.length > 0) {
@@ -139,7 +143,10 @@ function createDataStream({
139
143
  resolve();
140
144
  });
141
145
  waitForStreams.finally(() => {
142
- controller.close();
146
+ try {
147
+ controller.close();
148
+ } catch (error) {
149
+ }
143
150
  });
144
151
  return stream;
145
152
  }
@@ -1222,6 +1229,7 @@ async function downloadAssets(messages, downloadImplementation, modelSupportsIma
1222
1229
  );
1223
1230
  }
1224
1231
  function convertPartToLanguageModelPart(part, downloadedAssets) {
1232
+ var _a13;
1225
1233
  if (part.type === "text") {
1226
1234
  return {
1227
1235
  type: "text",
@@ -1272,9 +1280,9 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
1272
1280
  normalizedData = convertDataContentToUint8Array(content);
1273
1281
  }
1274
1282
  switch (type) {
1275
- case "image":
1276
- if (mimeType == null && normalizedData instanceof Uint8Array) {
1277
- mimeType = detectImageMimeType(normalizedData);
1283
+ case "image": {
1284
+ if (normalizedData instanceof Uint8Array) {
1285
+ mimeType = (_a13 = detectImageMimeType(normalizedData)) != null ? _a13 : mimeType;
1278
1286
  }
1279
1287
  return {
1280
1288
  type: "image",
@@ -1282,7 +1290,8 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
1282
1290
  mimeType,
1283
1291
  providerMetadata: part.experimental_providerMetadata
1284
1292
  };
1285
- case "file":
1293
+ }
1294
+ case "file": {
1286
1295
  if (mimeType == null) {
1287
1296
  throw new Error(`Mime type is missing for file part`);
1288
1297
  }
@@ -1292,6 +1301,7 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
1292
1301
  mimeType,
1293
1302
  providerMetadata: part.experimental_providerMetadata
1294
1303
  };
1304
+ }
1295
1305
  }
1296
1306
  }
1297
1307
 
@@ -1838,12 +1848,10 @@ var import_provider_utils5 = require("@ai-sdk/provider-utils");
1838
1848
  var import_ui_utils2 = require("@ai-sdk/ui-utils");
1839
1849
 
1840
1850
  // core/util/async-iterable-stream.ts
1841
- function createAsyncIterableStream(source, transformer) {
1842
- const transformedStream = source.pipeThrough(
1843
- new TransformStream(transformer)
1844
- );
1845
- transformedStream[Symbol.asyncIterator] = () => {
1846
- const reader = transformedStream.getReader();
1851
+ function createAsyncIterableStream(source) {
1852
+ const stream = source.pipeThrough(new TransformStream());
1853
+ stream[Symbol.asyncIterator] = () => {
1854
+ const reader = stream.getReader();
1847
1855
  return {
1848
1856
  async next() {
1849
1857
  const { done, value } = await reader.read();
@@ -1851,7 +1859,7 @@ function createAsyncIterableStream(source, transformer) {
1851
1859
  }
1852
1860
  };
1853
1861
  };
1854
- return transformedStream;
1862
+ return stream;
1855
1863
  }
1856
1864
 
1857
1865
  // core/generate-object/output-strategy.ts
@@ -1981,29 +1989,35 @@ var arrayOutputStrategy = (schema) => {
1981
1989
  },
1982
1990
  createElementStream(originalStream) {
1983
1991
  let publishedElements = 0;
1984
- return createAsyncIterableStream(originalStream, {
1985
- transform(chunk, controller) {
1986
- switch (chunk.type) {
1987
- case "object": {
1988
- const array = chunk.object;
1989
- for (; publishedElements < array.length; publishedElements++) {
1990
- controller.enqueue(array[publishedElements]);
1992
+ return createAsyncIterableStream(
1993
+ originalStream.pipeThrough(
1994
+ new TransformStream({
1995
+ transform(chunk, controller) {
1996
+ switch (chunk.type) {
1997
+ case "object": {
1998
+ const array = chunk.object;
1999
+ for (; publishedElements < array.length; publishedElements++) {
2000
+ controller.enqueue(array[publishedElements]);
2001
+ }
2002
+ break;
2003
+ }
2004
+ case "text-delta":
2005
+ case "finish":
2006
+ break;
2007
+ case "error":
2008
+ controller.error(chunk.error);
2009
+ break;
2010
+ default: {
2011
+ const _exhaustiveCheck = chunk;
2012
+ throw new Error(
2013
+ `Unsupported chunk type: ${_exhaustiveCheck}`
2014
+ );
2015
+ }
1991
2016
  }
1992
- break;
1993
- }
1994
- case "text-delta":
1995
- case "finish":
1996
- break;
1997
- case "error":
1998
- controller.error(chunk.error);
1999
- break;
2000
- default: {
2001
- const _exhaustiveCheck = chunk;
2002
- throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
2003
2017
  }
2004
- }
2005
- }
2006
- });
2018
+ })
2019
+ )
2020
+ );
2007
2021
  }
2008
2022
  };
2009
2023
  };
@@ -3199,25 +3213,29 @@ var DefaultStreamObjectResult = class {
3199
3213
  return this.responsePromise.value;
3200
3214
  }
3201
3215
  get partialObjectStream() {
3202
- return createAsyncIterableStream(this.stitchableStream.stream, {
3203
- transform(chunk, controller) {
3204
- switch (chunk.type) {
3205
- case "object":
3206
- controller.enqueue(chunk.object);
3207
- break;
3208
- case "text-delta":
3209
- case "finish":
3210
- break;
3211
- case "error":
3212
- controller.error(chunk.error);
3213
- break;
3214
- default: {
3215
- const _exhaustiveCheck = chunk;
3216
- throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
3216
+ return createAsyncIterableStream(
3217
+ this.stitchableStream.stream.pipeThrough(
3218
+ new TransformStream({
3219
+ transform(chunk, controller) {
3220
+ switch (chunk.type) {
3221
+ case "object":
3222
+ controller.enqueue(chunk.object);
3223
+ break;
3224
+ case "text-delta":
3225
+ case "finish":
3226
+ break;
3227
+ case "error":
3228
+ controller.error(chunk.error);
3229
+ break;
3230
+ default: {
3231
+ const _exhaustiveCheck = chunk;
3232
+ throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
3233
+ }
3234
+ }
3217
3235
  }
3218
- }
3219
- }
3220
- });
3236
+ })
3237
+ )
3238
+ );
3221
3239
  }
3222
3240
  get elementStream() {
3223
3241
  return this.outputStrategy.createElementStream(
@@ -3225,32 +3243,32 @@ var DefaultStreamObjectResult = class {
3225
3243
  );
3226
3244
  }
3227
3245
  get textStream() {
3228
- return createAsyncIterableStream(this.stitchableStream.stream, {
3229
- transform(chunk, controller) {
3230
- switch (chunk.type) {
3231
- case "text-delta":
3232
- controller.enqueue(chunk.textDelta);
3233
- break;
3234
- case "object":
3235
- case "finish":
3236
- break;
3237
- case "error":
3238
- controller.error(chunk.error);
3239
- break;
3240
- default: {
3241
- const _exhaustiveCheck = chunk;
3242
- throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
3246
+ return createAsyncIterableStream(
3247
+ this.stitchableStream.stream.pipeThrough(
3248
+ new TransformStream({
3249
+ transform(chunk, controller) {
3250
+ switch (chunk.type) {
3251
+ case "text-delta":
3252
+ controller.enqueue(chunk.textDelta);
3253
+ break;
3254
+ case "object":
3255
+ case "finish":
3256
+ break;
3257
+ case "error":
3258
+ controller.error(chunk.error);
3259
+ break;
3260
+ default: {
3261
+ const _exhaustiveCheck = chunk;
3262
+ throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
3263
+ }
3264
+ }
3243
3265
  }
3244
- }
3245
- }
3246
- });
3266
+ })
3267
+ )
3268
+ );
3247
3269
  }
3248
3270
  get fullStream() {
3249
- return createAsyncIterableStream(this.stitchableStream.stream, {
3250
- transform(chunk, controller) {
3251
- controller.enqueue(chunk);
3252
- }
3253
- });
3271
+ return createAsyncIterableStream(this.stitchableStream.stream);
3254
3272
  }
3255
3273
  pipeTextStreamToResponse(response, init) {
3256
3274
  writeToServerResponse({
@@ -3923,6 +3941,7 @@ async function executeTools({
3923
3941
  }
3924
3942
  });
3925
3943
  return {
3944
+ type: "tool-result",
3926
3945
  toolCallId,
3927
3946
  toolName,
3928
3947
  args,
@@ -4167,25 +4186,6 @@ function runToolsTransformation({
4167
4186
  break;
4168
4187
  }
4169
4188
  case "tool-call": {
4170
- const toolName = chunk.toolName;
4171
- if (tools == null) {
4172
- toolResultsStreamController.enqueue({
4173
- type: "error",
4174
- error: new NoSuchToolError({ toolName: chunk.toolName })
4175
- });
4176
- break;
4177
- }
4178
- const tool2 = tools[toolName];
4179
- if (tool2 == null) {
4180
- toolResultsStreamController.enqueue({
4181
- type: "error",
4182
- error: new NoSuchToolError({
4183
- toolName: chunk.toolName,
4184
- availableTools: Object.keys(tools)
4185
- })
4186
- });
4187
- break;
4188
- }
4189
4189
  try {
4190
4190
  const toolCall = await parseToolCall({
4191
4191
  toolCall: chunk,
@@ -4195,6 +4195,7 @@ function runToolsTransformation({
4195
4195
  messages
4196
4196
  });
4197
4197
  controller.enqueue(toolCall);
4198
+ const tool2 = tools[toolCall.toolName];
4198
4199
  if (tool2.execute != null) {
4199
4200
  const toolExecutionId = (0, import_ui_utils7.generateId)();
4200
4201
  outstandingToolResults.add(toolExecutionId);
@@ -5018,22 +5019,22 @@ var DefaultStreamTextResult = class {
5018
5019
  return stream1;
5019
5020
  }
5020
5021
  get textStream() {
5021
- return createAsyncIterableStream(this.teeStream(), {
5022
- transform(chunk, controller) {
5023
- if (chunk.type === "text-delta") {
5024
- controller.enqueue(chunk.textDelta);
5025
- } else if (chunk.type === "error") {
5026
- controller.error(chunk.error);
5027
- }
5028
- }
5029
- });
5022
+ return createAsyncIterableStream(
5023
+ this.teeStream().pipeThrough(
5024
+ new TransformStream({
5025
+ transform(chunk, controller) {
5026
+ if (chunk.type === "text-delta") {
5027
+ controller.enqueue(chunk.textDelta);
5028
+ } else if (chunk.type === "error") {
5029
+ controller.error(chunk.error);
5030
+ }
5031
+ }
5032
+ })
5033
+ )
5034
+ );
5030
5035
  }
5031
5036
  get fullStream() {
5032
- return createAsyncIterableStream(this.teeStream(), {
5033
- transform(chunk, controller) {
5034
- controller.enqueue(chunk);
5035
- }
5036
- });
5037
+ return createAsyncIterableStream(this.teeStream());
5037
5038
  }
5038
5039
  toDataStreamInternal({
5039
5040
  getErrorMessage: getErrorMessage5 = () => "An error occurred.",