ai 4.0.10 → 4.0.12

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.js CHANGED
@@ -4,8 +4,8 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
6
  var __export = (target, all) => {
7
- for (var name11 in all)
8
- __defProp(target, name11, { get: all[name11], enumerable: true });
7
+ for (var name12 in all)
8
+ __defProp(target, name12, { get: all[name12], enumerable: true });
9
9
  };
10
10
  var __copyProps = (to, from, except, desc) => {
11
11
  if (from && typeof from === "object" || typeof from === "function") {
@@ -20,32 +20,33 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // streams/index.ts
21
21
  var streams_exports = {};
22
22
  __export(streams_exports, {
23
- AISDKError: () => import_provider13.AISDKError,
24
- APICallError: () => import_provider13.APICallError,
23
+ AISDKError: () => import_provider14.AISDKError,
24
+ APICallError: () => import_provider14.APICallError,
25
25
  AssistantResponse: () => AssistantResponse,
26
26
  DownloadError: () => DownloadError,
27
- EmptyResponseBodyError: () => import_provider13.EmptyResponseBodyError,
27
+ EmptyResponseBodyError: () => import_provider14.EmptyResponseBodyError,
28
28
  InvalidArgumentError: () => InvalidArgumentError,
29
29
  InvalidDataContentError: () => InvalidDataContentError,
30
30
  InvalidMessageRoleError: () => InvalidMessageRoleError,
31
- InvalidPromptError: () => import_provider13.InvalidPromptError,
32
- InvalidResponseDataError: () => import_provider13.InvalidResponseDataError,
31
+ InvalidPromptError: () => import_provider14.InvalidPromptError,
32
+ InvalidResponseDataError: () => import_provider14.InvalidResponseDataError,
33
33
  InvalidToolArgumentsError: () => InvalidToolArgumentsError,
34
- JSONParseError: () => import_provider13.JSONParseError,
34
+ JSONParseError: () => import_provider14.JSONParseError,
35
35
  LangChainAdapter: () => langchain_adapter_exports,
36
36
  LlamaIndexAdapter: () => llamaindex_adapter_exports,
37
- LoadAPIKeyError: () => import_provider13.LoadAPIKeyError,
37
+ LoadAPIKeyError: () => import_provider14.LoadAPIKeyError,
38
38
  MessageConversionError: () => MessageConversionError,
39
- NoContentGeneratedError: () => import_provider13.NoContentGeneratedError,
39
+ NoContentGeneratedError: () => import_provider14.NoContentGeneratedError,
40
40
  NoObjectGeneratedError: () => NoObjectGeneratedError,
41
- NoSuchModelError: () => import_provider13.NoSuchModelError,
41
+ NoSuchModelError: () => import_provider14.NoSuchModelError,
42
42
  NoSuchProviderError: () => NoSuchProviderError,
43
43
  NoSuchToolError: () => NoSuchToolError,
44
44
  Output: () => output_exports,
45
45
  RetryError: () => RetryError,
46
46
  StreamData: () => StreamData,
47
- TypeValidationError: () => import_provider13.TypeValidationError,
48
- UnsupportedFunctionalityError: () => import_provider13.UnsupportedFunctionalityError,
47
+ ToolExecutionError: () => ToolExecutionError,
48
+ TypeValidationError: () => import_provider14.TypeValidationError,
49
+ UnsupportedFunctionalityError: () => import_provider14.UnsupportedFunctionalityError,
49
50
  convertToCoreMessages: () => convertToCoreMessages,
50
51
  cosineSimilarity: () => cosineSimilarity,
51
52
  createDataStream: () => createDataStream,
@@ -406,7 +407,7 @@ function getBaseTelemetryAttributes({
406
407
  telemetry,
407
408
  headers
408
409
  }) {
409
- var _a11;
410
+ var _a12;
410
411
  return {
411
412
  "ai.model.provider": model.provider,
412
413
  "ai.model.id": model.modelId,
@@ -416,7 +417,7 @@ function getBaseTelemetryAttributes({
416
417
  return attributes;
417
418
  }, {}),
418
419
  // add metadata as attributes:
419
- ...Object.entries((_a11 = telemetry == null ? void 0 : telemetry.metadata) != null ? _a11 : {}).reduce(
420
+ ...Object.entries((_a12 = telemetry == null ? void 0 : telemetry.metadata) != null ? _a12 : {}).reduce(
420
421
  (attributes, [key, value]) => {
421
422
  attributes[`ai.telemetry.metadata.${key}`] = value;
422
423
  return attributes;
@@ -441,7 +442,7 @@ var noopTracer = {
441
442
  startSpan() {
442
443
  return noopSpan;
443
444
  },
444
- startActiveSpan(name11, arg1, arg2, arg3) {
445
+ startActiveSpan(name12, arg1, arg2, arg3) {
445
446
  if (typeof arg1 === "function") {
446
447
  return arg1(noopSpan);
447
448
  }
@@ -511,13 +512,13 @@ function getTracer({
511
512
  // core/telemetry/record-span.ts
512
513
  var import_api2 = require("@opentelemetry/api");
513
514
  function recordSpan({
514
- name: name11,
515
+ name: name12,
515
516
  tracer,
516
517
  attributes,
517
518
  fn,
518
519
  endWhenDone = true
519
520
  }) {
520
- return tracer.startActiveSpan(name11, { attributes }, async (span) => {
521
+ return tracer.startActiveSpan(name12, { attributes }, async (span) => {
521
522
  try {
522
523
  const result = await fn(span);
523
524
  if (endWhenDone) {
@@ -625,14 +626,14 @@ async function embed({
625
626
  }),
626
627
  tracer,
627
628
  fn: async (doEmbedSpan) => {
628
- var _a11;
629
+ var _a12;
629
630
  const modelResponse = await model.doEmbed({
630
631
  values: [value],
631
632
  abortSignal,
632
633
  headers
633
634
  });
634
635
  const embedding2 = modelResponse.embeddings[0];
635
- const usage2 = (_a11 = modelResponse.usage) != null ? _a11 : { tokens: NaN };
636
+ const usage2 = (_a12 = modelResponse.usage) != null ? _a12 : { tokens: NaN };
636
637
  doEmbedSpan.setAttributes(
637
638
  selectTelemetryAttributes({
638
639
  telemetry,
@@ -742,14 +743,14 @@ async function embedMany({
742
743
  }),
743
744
  tracer,
744
745
  fn: async (doEmbedSpan) => {
745
- var _a11;
746
+ var _a12;
746
747
  const modelResponse = await model.doEmbed({
747
748
  values,
748
749
  abortSignal,
749
750
  headers
750
751
  });
751
752
  const embeddings3 = modelResponse.embeddings;
752
- const usage2 = (_a11 = modelResponse.usage) != null ? _a11 : { tokens: NaN };
753
+ const usage2 = (_a12 = modelResponse.usage) != null ? _a12 : { tokens: NaN };
753
754
  doEmbedSpan.setAttributes(
754
755
  selectTelemetryAttributes({
755
756
  telemetry,
@@ -801,14 +802,14 @@ async function embedMany({
801
802
  }),
802
803
  tracer,
803
804
  fn: async (doEmbedSpan) => {
804
- var _a11;
805
+ var _a12;
805
806
  const modelResponse = await model.doEmbed({
806
807
  values: chunk,
807
808
  abortSignal,
808
809
  headers
809
810
  });
810
811
  const embeddings2 = modelResponse.embeddings;
811
- const usage2 = (_a11 = modelResponse.usage) != null ? _a11 : { tokens: NaN };
812
+ const usage2 = (_a12 = modelResponse.usage) != null ? _a12 : { tokens: NaN };
812
813
  doEmbedSpan.setAttributes(
813
814
  selectTelemetryAttributes({
814
815
  telemetry,
@@ -888,7 +889,7 @@ async function download({
888
889
  url,
889
890
  fetchImplementation = fetch
890
891
  }) {
891
- var _a11;
892
+ var _a12;
892
893
  const urlText = url.toString();
893
894
  try {
894
895
  const response = await fetchImplementation(urlText);
@@ -901,7 +902,7 @@ async function download({
901
902
  }
902
903
  return {
903
904
  data: new Uint8Array(await response.arrayBuffer()),
904
- mimeType: (_a11 = response.headers.get("content-type")) != null ? _a11 : void 0
905
+ mimeType: (_a12 = response.headers.get("content-type")) != null ? _a12 : void 0
905
906
  };
906
907
  } catch (error) {
907
908
  if (DownloadError.isInstance(error)) {
@@ -961,8 +962,8 @@ var dataContentSchema = import_zod.z.union([
961
962
  import_zod.z.custom(
962
963
  // Buffer might not be available in some environments such as CloudFlare:
963
964
  (value) => {
964
- var _a11, _b;
965
- return (_b = (_a11 = globalThis.Buffer) == null ? void 0 : _a11.isBuffer(value)) != null ? _b : false;
965
+ var _a12, _b;
966
+ return (_b = (_a12 = globalThis.Buffer) == null ? void 0 : _a12.isBuffer(value)) != null ? _b : false;
966
967
  },
967
968
  { message: "Must be a Buffer" }
968
969
  )
@@ -1470,7 +1471,7 @@ function detectSingleMessageCharacteristics(message) {
1470
1471
 
1471
1472
  // core/prompt/attachments-to-parts.ts
1472
1473
  function attachmentsToParts(attachments) {
1473
- var _a11, _b, _c;
1474
+ var _a12, _b, _c;
1474
1475
  const parts = [];
1475
1476
  for (const attachment of attachments) {
1476
1477
  let url;
@@ -1482,7 +1483,7 @@ function attachmentsToParts(attachments) {
1482
1483
  switch (url.protocol) {
1483
1484
  case "http:":
1484
1485
  case "https:": {
1485
- if ((_a11 = attachment.contentType) == null ? void 0 : _a11.startsWith("image/")) {
1486
+ if ((_a12 = attachment.contentType) == null ? void 0 : _a12.startsWith("image/")) {
1486
1487
  parts.push({ type: "image", image: url });
1487
1488
  } else {
1488
1489
  if (!attachment.contentType) {
@@ -1568,8 +1569,8 @@ _a6 = symbol6;
1568
1569
 
1569
1570
  // core/prompt/convert-to-core-messages.ts
1570
1571
  function convertToCoreMessages(messages, options) {
1571
- var _a11;
1572
- const tools = (_a11 = options == null ? void 0 : options.tools) != null ? _a11 : {};
1572
+ var _a12;
1573
+ const tools = (_a12 = options == null ? void 0 : options.tools) != null ? _a12 : {};
1573
1574
  const coreMessages = [];
1574
1575
  for (const message of messages) {
1575
1576
  const { role, content, toolInvocations, experimental_attachments } = message;
@@ -1851,7 +1852,7 @@ var arrayOutputStrategy = (schema) => {
1851
1852
  additionalProperties: false
1852
1853
  },
1853
1854
  validatePartialResult({ value, latestObject, isFirstDelta, isFinalDelta }) {
1854
- var _a11;
1855
+ var _a12;
1855
1856
  if (!(0, import_provider10.isJSONObject)(value) || !(0, import_provider10.isJSONArray)(value.elements)) {
1856
1857
  return {
1857
1858
  success: false,
@@ -1874,7 +1875,7 @@ var arrayOutputStrategy = (schema) => {
1874
1875
  }
1875
1876
  resultArray.push(result.value);
1876
1877
  }
1877
- const publishedElementCount = (_a11 = latestObject == null ? void 0 : latestObject.length) != null ? _a11 : 0;
1878
+ const publishedElementCount = (_a12 = latestObject == null ? void 0 : latestObject.length) != null ? _a12 : 0;
1878
1879
  let textDelta = "";
1879
1880
  if (isFirstDelta) {
1880
1881
  textDelta += "[";
@@ -2206,7 +2207,7 @@ async function generateObject({
2206
2207
  }),
2207
2208
  tracer,
2208
2209
  fn: async (span) => {
2209
- var _a11, _b;
2210
+ var _a12, _b;
2210
2211
  if (mode === "auto" || mode == null) {
2211
2212
  mode = model.defaultObjectGenerationMode;
2212
2213
  }
@@ -2268,7 +2269,7 @@ async function generateObject({
2268
2269
  }),
2269
2270
  tracer,
2270
2271
  fn: async (span2) => {
2271
- var _a12, _b2, _c, _d, _e, _f;
2272
+ var _a13, _b2, _c, _d, _e, _f;
2272
2273
  const result2 = await model.doGenerate({
2273
2274
  mode: {
2274
2275
  type: "object-json",
@@ -2287,7 +2288,7 @@ async function generateObject({
2287
2288
  throw new NoObjectGeneratedError();
2288
2289
  }
2289
2290
  const responseData = {
2290
- id: (_b2 = (_a12 = result2.response) == null ? void 0 : _a12.id) != null ? _b2 : generateId3(),
2291
+ id: (_b2 = (_a13 = result2.response) == null ? void 0 : _a13.id) != null ? _b2 : generateId3(),
2291
2292
  timestamp: (_d = (_c = result2.response) == null ? void 0 : _c.timestamp) != null ? _d : currentDate(),
2292
2293
  modelId: (_f = (_e = result2.response) == null ? void 0 : _e.modelId) != null ? _f : model.modelId
2293
2294
  };
@@ -2322,7 +2323,7 @@ async function generateObject({
2322
2323
  rawResponse = generateResult.rawResponse;
2323
2324
  logprobs = generateResult.logprobs;
2324
2325
  resultProviderMetadata = generateResult.providerMetadata;
2325
- request = (_a11 = generateResult.request) != null ? _a11 : {};
2326
+ request = (_a12 = generateResult.request) != null ? _a12 : {};
2326
2327
  response = generateResult.responseData;
2327
2328
  break;
2328
2329
  }
@@ -2368,7 +2369,7 @@ async function generateObject({
2368
2369
  }),
2369
2370
  tracer,
2370
2371
  fn: async (span2) => {
2371
- var _a12, _b2, _c, _d, _e, _f, _g, _h;
2372
+ var _a13, _b2, _c, _d, _e, _f, _g, _h;
2372
2373
  const result2 = await model.doGenerate({
2373
2374
  mode: {
2374
2375
  type: "object-tool",
@@ -2386,7 +2387,7 @@ async function generateObject({
2386
2387
  abortSignal,
2387
2388
  headers
2388
2389
  });
2389
- const objectText = (_b2 = (_a12 = result2.toolCalls) == null ? void 0 : _a12[0]) == null ? void 0 : _b2.args;
2390
+ const objectText = (_b2 = (_a13 = result2.toolCalls) == null ? void 0 : _a13[0]) == null ? void 0 : _b2.args;
2390
2391
  if (objectText === void 0) {
2391
2392
  throw new NoObjectGeneratedError();
2392
2393
  }
@@ -2491,9 +2492,9 @@ var DefaultGenerateObjectResult = class {
2491
2492
  this.logprobs = options.logprobs;
2492
2493
  }
2493
2494
  toJsonResponse(init) {
2494
- var _a11;
2495
+ var _a12;
2495
2496
  return new Response(JSON.stringify(this.object), {
2496
- status: (_a11 = init == null ? void 0 : init.status) != null ? _a11 : 200,
2497
+ status: (_a12 = init == null ? void 0 : init.status) != null ? _a12 : 200,
2497
2498
  headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
2498
2499
  contentType: "application/json; charset=utf-8"
2499
2500
  })
@@ -2528,17 +2529,17 @@ var DelayedPromise = class {
2528
2529
  return this.promise;
2529
2530
  }
2530
2531
  resolve(value) {
2531
- var _a11;
2532
+ var _a12;
2532
2533
  this.status = { type: "resolved", value };
2533
2534
  if (this.promise) {
2534
- (_a11 = this._resolve) == null ? void 0 : _a11.call(this, value);
2535
+ (_a12 = this._resolve) == null ? void 0 : _a12.call(this, value);
2535
2536
  }
2536
2537
  }
2537
2538
  reject(error) {
2538
- var _a11;
2539
+ var _a12;
2539
2540
  this.status = { type: "rejected", error };
2540
2541
  if (this.promise) {
2541
- (_a11 = this._reject) == null ? void 0 : _a11.call(this, error);
2542
+ (_a12 = this._reject) == null ? void 0 : _a12.call(this, error);
2542
2543
  }
2543
2544
  }
2544
2545
  };
@@ -2627,8 +2628,8 @@ function createStitchableStream() {
2627
2628
 
2628
2629
  // core/util/now.ts
2629
2630
  function now() {
2630
- var _a11, _b;
2631
- return (_b = (_a11 = globalThis == null ? void 0 : globalThis.performance) == null ? void 0 : _a11.now()) != null ? _b : Date.now();
2631
+ var _a12, _b;
2632
+ return (_b = (_a12 = globalThis == null ? void 0 : globalThis.performance) == null ? void 0 : _a12.now()) != null ? _b : Date.now();
2632
2633
  }
2633
2634
 
2634
2635
  // core/generate-object/stream-object.ts
@@ -2917,7 +2918,7 @@ var DefaultStreamObjectResult = class {
2917
2918
  const transformedStream = stream.pipeThrough(new TransformStream(transformer)).pipeThrough(
2918
2919
  new TransformStream({
2919
2920
  async transform(chunk, controller) {
2920
- var _a11, _b, _c;
2921
+ var _a12, _b, _c;
2921
2922
  if (isFirstChunk) {
2922
2923
  const msToFirstChunk = now2() - startTimestampMs;
2923
2924
  isFirstChunk = false;
@@ -2963,7 +2964,7 @@ var DefaultStreamObjectResult = class {
2963
2964
  switch (chunk.type) {
2964
2965
  case "response-metadata": {
2965
2966
  response = {
2966
- id: (_a11 = chunk.id) != null ? _a11 : response.id,
2967
+ id: (_a12 = chunk.id) != null ? _a12 : response.id,
2967
2968
  timestamp: (_b = chunk.timestamp) != null ? _b : response.timestamp,
2968
2969
  modelId: (_c = chunk.modelId) != null ? _c : response.modelId
2969
2970
  };
@@ -3160,9 +3161,9 @@ var DefaultStreamObjectResult = class {
3160
3161
  });
3161
3162
  }
3162
3163
  toTextStreamResponse(init) {
3163
- var _a11;
3164
+ var _a12;
3164
3165
  return new Response(this.textStream.pipeThrough(new TextEncoderStream()), {
3165
- status: (_a11 = init == null ? void 0 : init.status) != null ? _a11 : 200,
3166
+ status: (_a12 = init == null ? void 0 : init.status) != null ? _a12 : 200,
3166
3167
  headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
3167
3168
  contentType: "text/plain; charset=utf-8"
3168
3169
  })
@@ -3174,7 +3175,7 @@ var DefaultStreamObjectResult = class {
3174
3175
  var import_provider_utils8 = require("@ai-sdk/provider-utils");
3175
3176
 
3176
3177
  // errors/index.ts
3177
- var import_provider13 = require("@ai-sdk/provider");
3178
+ var import_provider14 = require("@ai-sdk/provider");
3178
3179
 
3179
3180
  // errors/invalid-tool-arguments-error.ts
3180
3181
  var import_provider11 = require("@ai-sdk/provider");
@@ -3225,6 +3226,30 @@ var NoSuchToolError = class extends import_provider12.AISDKError {
3225
3226
  };
3226
3227
  _a9 = symbol9;
3227
3228
 
3229
+ // errors/tool-execution-error.ts
3230
+ var import_provider13 = require("@ai-sdk/provider");
3231
+ var name10 = "AI_ToolExecutionError";
3232
+ var marker10 = `vercel.ai.error.${name10}`;
3233
+ var symbol10 = Symbol.for(marker10);
3234
+ var _a10;
3235
+ var ToolExecutionError = class extends import_provider13.AISDKError {
3236
+ constructor({
3237
+ toolArgs,
3238
+ toolName,
3239
+ cause,
3240
+ message = `Error executing tool ${toolName}: ${(0, import_provider13.getErrorMessage)(cause)}`
3241
+ }) {
3242
+ super({ name: name10, message, cause });
3243
+ this[_a10] = true;
3244
+ this.toolArgs = toolArgs;
3245
+ this.toolName = toolName;
3246
+ }
3247
+ static isInstance(error) {
3248
+ return import_provider13.AISDKError.hasMarker(error, marker10);
3249
+ }
3250
+ };
3251
+ _a10 = symbol10;
3252
+
3228
3253
  // core/prompt/prepare-tools-and-tool-choice.ts
3229
3254
  var import_ui_utils4 = require("@ai-sdk/ui-utils");
3230
3255
 
@@ -3246,24 +3271,24 @@ function prepareToolsAndToolChoice({
3246
3271
  };
3247
3272
  }
3248
3273
  const filteredTools = activeTools != null ? Object.entries(tools).filter(
3249
- ([name11]) => activeTools.includes(name11)
3274
+ ([name12]) => activeTools.includes(name12)
3250
3275
  ) : Object.entries(tools);
3251
3276
  return {
3252
- tools: filteredTools.map(([name11, tool2]) => {
3277
+ tools: filteredTools.map(([name12, tool2]) => {
3253
3278
  const toolType = tool2.type;
3254
3279
  switch (toolType) {
3255
3280
  case void 0:
3256
3281
  case "function":
3257
3282
  return {
3258
3283
  type: "function",
3259
- name: name11,
3284
+ name: name12,
3260
3285
  description: tool2.description,
3261
3286
  parameters: (0, import_ui_utils4.asSchema)(tool2.parameters).jsonSchema
3262
3287
  };
3263
3288
  case "provider-defined":
3264
3289
  return {
3265
3290
  type: "provider-defined",
3266
- name: name11,
3291
+ name: name12,
3267
3292
  id: tool2.id,
3268
3293
  args: tool2.args
3269
3294
  };
@@ -3387,7 +3412,7 @@ async function generateText({
3387
3412
  onStepFinish,
3388
3413
  ...settings
3389
3414
  }) {
3390
- var _a11;
3415
+ var _a12;
3391
3416
  if (maxSteps < 1) {
3392
3417
  throw new InvalidArgumentError({
3393
3418
  parameter: "maxSteps",
@@ -3404,7 +3429,7 @@ async function generateText({
3404
3429
  });
3405
3430
  const initialPrompt = standardizePrompt({
3406
3431
  prompt: {
3407
- system: (_a11 = output == null ? void 0 : output.injectIntoSystemPrompt({ system, model })) != null ? _a11 : system,
3432
+ system: (_a12 = output == null ? void 0 : output.injectIntoSystemPrompt({ system, model })) != null ? _a12 : system,
3408
3433
  prompt,
3409
3434
  messages
3410
3435
  },
@@ -3430,7 +3455,7 @@ async function generateText({
3430
3455
  }),
3431
3456
  tracer,
3432
3457
  fn: async (span) => {
3433
- var _a12, _b, _c, _d, _e, _f;
3458
+ var _a13, _b, _c, _d, _e, _f;
3434
3459
  const mode = {
3435
3460
  type: "regular",
3436
3461
  ...prepareToolsAndToolChoice({ tools, toolChoice, activeTools })
@@ -3482,8 +3507,8 @@ async function generateText({
3482
3507
  "ai.prompt.tools": {
3483
3508
  // convert the language model level tools:
3484
3509
  input: () => {
3485
- var _a13;
3486
- return (_a13 = mode.tools) == null ? void 0 : _a13.map((tool2) => JSON.stringify(tool2));
3510
+ var _a14;
3511
+ return (_a14 = mode.tools) == null ? void 0 : _a14.map((tool2) => JSON.stringify(tool2));
3487
3512
  }
3488
3513
  },
3489
3514
  "ai.prompt.toolChoice": {
@@ -3503,7 +3528,7 @@ async function generateText({
3503
3528
  }),
3504
3529
  tracer,
3505
3530
  fn: async (span2) => {
3506
- var _a13, _b2, _c2, _d2, _e2, _f2;
3531
+ var _a14, _b2, _c2, _d2, _e2, _f2;
3507
3532
  const result = await model.doGenerate({
3508
3533
  mode,
3509
3534
  ...callSettings,
@@ -3515,7 +3540,7 @@ async function generateText({
3515
3540
  headers
3516
3541
  });
3517
3542
  const responseData = {
3518
- id: (_b2 = (_a13 = result.response) == null ? void 0 : _a13.id) != null ? _b2 : generateId3(),
3543
+ id: (_b2 = (_a14 = result.response) == null ? void 0 : _a14.id) != null ? _b2 : generateId3(),
3519
3544
  timestamp: (_d2 = (_c2 = result.response) == null ? void 0 : _c2.timestamp) != null ? _d2 : currentDate(),
3520
3545
  modelId: (_f2 = (_e2 = result.response) == null ? void 0 : _e2.modelId) != null ? _f2 : model.modelId
3521
3546
  };
@@ -3548,7 +3573,7 @@ async function generateText({
3548
3573
  }
3549
3574
  })
3550
3575
  );
3551
- currentToolCalls = ((_a12 = currentModelResponse.toolCalls) != null ? _a12 : []).map(
3576
+ currentToolCalls = ((_a13 = currentModelResponse.toolCalls) != null ? _a13 : []).map(
3552
3577
  (modelToolCall) => parseToolCall({ toolCall: modelToolCall, tools })
3553
3578
  );
3554
3579
  currentToolResults = tools == null ? [] : await executeTools({
@@ -3695,25 +3720,33 @@ async function executeTools({
3695
3720
  }),
3696
3721
  tracer,
3697
3722
  fn: async (span) => {
3698
- const result2 = await tool2.execute(args, {
3699
- toolCallId,
3700
- messages,
3701
- abortSignal
3702
- });
3703
3723
  try {
3704
- span.setAttributes(
3705
- selectTelemetryAttributes({
3706
- telemetry,
3707
- attributes: {
3708
- "ai.toolCall.result": {
3709
- output: () => JSON.stringify(result2)
3724
+ const result2 = await tool2.execute(args, {
3725
+ toolCallId,
3726
+ messages,
3727
+ abortSignal
3728
+ });
3729
+ try {
3730
+ span.setAttributes(
3731
+ selectTelemetryAttributes({
3732
+ telemetry,
3733
+ attributes: {
3734
+ "ai.toolCall.result": {
3735
+ output: () => JSON.stringify(result2)
3736
+ }
3710
3737
  }
3711
- }
3712
- })
3713
- );
3714
- } catch (ignored) {
3738
+ })
3739
+ );
3740
+ } catch (ignored) {
3741
+ }
3742
+ return result2;
3743
+ } catch (error) {
3744
+ throw new ToolExecutionError({
3745
+ toolName,
3746
+ toolArgs: args,
3747
+ cause: error
3748
+ });
3715
3749
  }
3716
- return result2;
3717
3750
  }
3718
3751
  });
3719
3752
  return {
@@ -3971,7 +4004,11 @@ function runToolsTransformation({
3971
4004
  (error) => {
3972
4005
  toolResultsStreamController.enqueue({
3973
4006
  type: "error",
3974
- error
4007
+ error: new ToolExecutionError({
4008
+ toolName: toolCall.toolName,
4009
+ toolArgs: toolCall.args,
4010
+ cause: error
4011
+ })
3975
4012
  });
3976
4013
  outstandingToolResults.delete(toolExecutionId);
3977
4014
  attemptClose();
@@ -4214,8 +4251,8 @@ var DefaultStreamTextResult = class {
4214
4251
  "ai.prompt.tools": {
4215
4252
  // convert the language model level tools:
4216
4253
  input: () => {
4217
- var _a11;
4218
- return (_a11 = mode.tools) == null ? void 0 : _a11.map((tool2) => JSON.stringify(tool2));
4254
+ var _a12;
4255
+ return (_a12 = mode.tools) == null ? void 0 : _a12.map((tool2) => JSON.stringify(tool2));
4219
4256
  }
4220
4257
  },
4221
4258
  "ai.prompt.toolChoice": {
@@ -4298,7 +4335,7 @@ var DefaultStreamTextResult = class {
4298
4335
  transformedStream.pipeThrough(
4299
4336
  new TransformStream({
4300
4337
  async transform(chunk, controller) {
4301
- var _a11, _b, _c;
4338
+ var _a12, _b, _c;
4302
4339
  if (stepFirstChunk) {
4303
4340
  const msToFirstChunk = now2() - startTimestampMs;
4304
4341
  stepFirstChunk = false;
@@ -4352,7 +4389,7 @@ var DefaultStreamTextResult = class {
4352
4389
  }
4353
4390
  case "response-metadata": {
4354
4391
  stepResponse = {
4355
- id: (_a11 = chunk.id) != null ? _a11 : stepResponse.id,
4392
+ id: (_a12 = chunk.id) != null ? _a12 : stepResponse.id,
4356
4393
  timestamp: (_b = chunk.timestamp) != null ? _b : stepResponse.timestamp,
4357
4394
  modelId: (_c = chunk.modelId) != null ? _c : stepResponse.modelId
4358
4395
  };
@@ -4599,7 +4636,8 @@ var DefaultStreamTextResult = class {
4599
4636
  self.stitchableStream.addStream(
4600
4637
  new ReadableStream({
4601
4638
  start(controller) {
4602
- controller.error(error);
4639
+ controller.enqueue({ type: "error", error });
4640
+ controller.close();
4603
4641
  }
4604
4642
  })
4605
4643
  );
@@ -4668,7 +4706,7 @@ var DefaultStreamTextResult = class {
4668
4706
  });
4669
4707
  }
4670
4708
  toDataStreamInternal({
4671
- getErrorMessage: getErrorMessage3 = () => "An error occurred.",
4709
+ getErrorMessage: getErrorMessage4 = () => "An error occurred.",
4672
4710
  // mask error messages for safety by default
4673
4711
  sendUsage = true
4674
4712
  } = {}) {
@@ -4728,7 +4766,7 @@ var DefaultStreamTextResult = class {
4728
4766
  }
4729
4767
  case "error": {
4730
4768
  controller.enqueue(
4731
- (0, import_ui_utils7.formatDataStreamPart)("error", getErrorMessage3(chunk.error))
4769
+ (0, import_ui_utils7.formatDataStreamPart)("error", getErrorMessage4(chunk.error))
4732
4770
  );
4733
4771
  break;
4734
4772
  }
@@ -4771,7 +4809,7 @@ var DefaultStreamTextResult = class {
4771
4809
  statusText,
4772
4810
  headers,
4773
4811
  data,
4774
- getErrorMessage: getErrorMessage3,
4812
+ getErrorMessage: getErrorMessage4,
4775
4813
  sendUsage
4776
4814
  } = {}) {
4777
4815
  writeToServerResponse({
@@ -4782,7 +4820,7 @@ var DefaultStreamTextResult = class {
4782
4820
  contentType: "text/plain; charset=utf-8",
4783
4821
  dataStreamVersion: "v1"
4784
4822
  }),
4785
- stream: this.toDataStream({ data, getErrorMessage: getErrorMessage3, sendUsage })
4823
+ stream: this.toDataStream({ data, getErrorMessage: getErrorMessage4, sendUsage })
4786
4824
  });
4787
4825
  }
4788
4826
  pipeTextStreamToResponse(response, init) {
@@ -4816,11 +4854,11 @@ var DefaultStreamTextResult = class {
4816
4854
  status,
4817
4855
  statusText,
4818
4856
  data,
4819
- getErrorMessage: getErrorMessage3,
4857
+ getErrorMessage: getErrorMessage4,
4820
4858
  sendUsage
4821
4859
  } = {}) {
4822
4860
  return new Response(
4823
- this.toDataStream({ data, getErrorMessage: getErrorMessage3, sendUsage }),
4861
+ this.toDataStream({ data, getErrorMessage: getErrorMessage4, sendUsage }),
4824
4862
  {
4825
4863
  status,
4826
4864
  statusText,
@@ -4832,9 +4870,9 @@ var DefaultStreamTextResult = class {
4832
4870
  );
4833
4871
  }
4834
4872
  toTextStreamResponse(init) {
4835
- var _a11;
4873
+ var _a12;
4836
4874
  return new Response(this.textStream.pipeThrough(new TextEncoderStream()), {
4837
- status: (_a11 = init == null ? void 0 : init.status) != null ? _a11 : 200,
4875
+ status: (_a12 = init == null ? void 0 : init.status) != null ? _a12 : 200,
4838
4876
  headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
4839
4877
  contentType: "text/plain; charset=utf-8"
4840
4878
  })
@@ -4917,7 +4955,7 @@ var experimental_wrapLanguageModel = ({
4917
4955
  };
4918
4956
 
4919
4957
  // core/registry/custom-provider.ts
4920
- var import_provider14 = require("@ai-sdk/provider");
4958
+ var import_provider15 = require("@ai-sdk/provider");
4921
4959
  function experimental_customProvider({
4922
4960
  languageModels,
4923
4961
  textEmbeddingModels,
@@ -4931,7 +4969,7 @@ function experimental_customProvider({
4931
4969
  if (fallbackProvider) {
4932
4970
  return fallbackProvider.languageModel(modelId);
4933
4971
  }
4934
- throw new import_provider14.NoSuchModelError({ modelId, modelType: "languageModel" });
4972
+ throw new import_provider15.NoSuchModelError({ modelId, modelType: "languageModel" });
4935
4973
  },
4936
4974
  textEmbeddingModel(modelId) {
4937
4975
  if (textEmbeddingModels != null && modelId in textEmbeddingModels) {
@@ -4940,18 +4978,18 @@ function experimental_customProvider({
4940
4978
  if (fallbackProvider) {
4941
4979
  return fallbackProvider.textEmbeddingModel(modelId);
4942
4980
  }
4943
- throw new import_provider14.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
4981
+ throw new import_provider15.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
4944
4982
  }
4945
4983
  };
4946
4984
  }
4947
4985
 
4948
4986
  // core/registry/no-such-provider-error.ts
4949
- var import_provider15 = require("@ai-sdk/provider");
4950
- var name10 = "AI_NoSuchProviderError";
4951
- var marker10 = `vercel.ai.error.${name10}`;
4952
- var symbol10 = Symbol.for(marker10);
4953
- var _a10;
4954
- var NoSuchProviderError = class extends import_provider15.NoSuchModelError {
4987
+ var import_provider16 = require("@ai-sdk/provider");
4988
+ var name11 = "AI_NoSuchProviderError";
4989
+ var marker11 = `vercel.ai.error.${name11}`;
4990
+ var symbol11 = Symbol.for(marker11);
4991
+ var _a11;
4992
+ var NoSuchProviderError = class extends import_provider16.NoSuchModelError {
4955
4993
  constructor({
4956
4994
  modelId,
4957
4995
  modelType,
@@ -4959,19 +4997,19 @@ var NoSuchProviderError = class extends import_provider15.NoSuchModelError {
4959
4997
  availableProviders,
4960
4998
  message = `No such provider: ${providerId} (available providers: ${availableProviders.join()})`
4961
4999
  }) {
4962
- super({ errorName: name10, modelId, modelType, message });
4963
- this[_a10] = true;
5000
+ super({ errorName: name11, modelId, modelType, message });
5001
+ this[_a11] = true;
4964
5002
  this.providerId = providerId;
4965
5003
  this.availableProviders = availableProviders;
4966
5004
  }
4967
5005
  static isInstance(error) {
4968
- return import_provider15.AISDKError.hasMarker(error, marker10);
5006
+ return import_provider16.AISDKError.hasMarker(error, marker11);
4969
5007
  }
4970
5008
  };
4971
- _a10 = symbol10;
5009
+ _a11 = symbol11;
4972
5010
 
4973
5011
  // core/registry/provider-registry.ts
4974
- var import_provider16 = require("@ai-sdk/provider");
5012
+ var import_provider17 = require("@ai-sdk/provider");
4975
5013
  function experimental_createProviderRegistry(providers) {
4976
5014
  const registry = new DefaultProviderRegistry();
4977
5015
  for (const [id, provider] of Object.entries(providers)) {
@@ -5001,7 +5039,7 @@ var DefaultProviderRegistry = class {
5001
5039
  splitId(id, modelType) {
5002
5040
  const index = id.indexOf(":");
5003
5041
  if (index === -1) {
5004
- throw new import_provider16.NoSuchModelError({
5042
+ throw new import_provider17.NoSuchModelError({
5005
5043
  modelId: id,
5006
5044
  modelType,
5007
5045
  message: `Invalid ${modelType} id for registry: ${id} (must be in the format "providerId:modelId")`
@@ -5010,21 +5048,21 @@ var DefaultProviderRegistry = class {
5010
5048
  return [id.slice(0, index), id.slice(index + 1)];
5011
5049
  }
5012
5050
  languageModel(id) {
5013
- var _a11, _b;
5051
+ var _a12, _b;
5014
5052
  const [providerId, modelId] = this.splitId(id, "languageModel");
5015
- const model = (_b = (_a11 = this.getProvider(providerId)).languageModel) == null ? void 0 : _b.call(_a11, modelId);
5053
+ const model = (_b = (_a12 = this.getProvider(providerId)).languageModel) == null ? void 0 : _b.call(_a12, modelId);
5016
5054
  if (model == null) {
5017
- throw new import_provider16.NoSuchModelError({ modelId: id, modelType: "languageModel" });
5055
+ throw new import_provider17.NoSuchModelError({ modelId: id, modelType: "languageModel" });
5018
5056
  }
5019
5057
  return model;
5020
5058
  }
5021
5059
  textEmbeddingModel(id) {
5022
- var _a11;
5060
+ var _a12;
5023
5061
  const [providerId, modelId] = this.splitId(id, "textEmbeddingModel");
5024
5062
  const provider = this.getProvider(providerId);
5025
- const model = (_a11 = provider.textEmbeddingModel) == null ? void 0 : _a11.call(provider, modelId);
5063
+ const model = (_a12 = provider.textEmbeddingModel) == null ? void 0 : _a12.call(provider, modelId);
5026
5064
  if (model == null) {
5027
- throw new import_provider16.NoSuchModelError({
5065
+ throw new import_provider17.NoSuchModelError({
5028
5066
  modelId: id,
5029
5067
  modelType: "textEmbeddingModel"
5030
5068
  });
@@ -5068,7 +5106,7 @@ var import_ui_utils10 = require("@ai-sdk/ui-utils");
5068
5106
  function AssistantResponse({ threadId, messageId }, process2) {
5069
5107
  const stream = new ReadableStream({
5070
5108
  async start(controller) {
5071
- var _a11;
5109
+ var _a12;
5072
5110
  const textEncoder = new TextEncoder();
5073
5111
  const sendMessage = (message) => {
5074
5112
  controller.enqueue(
@@ -5090,7 +5128,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
5090
5128
  );
5091
5129
  };
5092
5130
  const forwardStream = async (stream2) => {
5093
- var _a12, _b;
5131
+ var _a13, _b;
5094
5132
  let result = void 0;
5095
5133
  for await (const value of stream2) {
5096
5134
  switch (value.event) {
@@ -5107,7 +5145,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
5107
5145
  break;
5108
5146
  }
5109
5147
  case "thread.message.delta": {
5110
- const content = (_a12 = value.data.delta.content) == null ? void 0 : _a12[0];
5148
+ const content = (_a13 = value.data.delta.content) == null ? void 0 : _a13[0];
5111
5149
  if ((content == null ? void 0 : content.type) === "text" && ((_b = content.text) == null ? void 0 : _b.value) != null) {
5112
5150
  controller.enqueue(
5113
5151
  textEncoder.encode(
@@ -5141,7 +5179,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
5141
5179
  forwardStream
5142
5180
  });
5143
5181
  } catch (error) {
5144
- sendError((_a11 = error.message) != null ? _a11 : `${error}`);
5182
+ sendError((_a12 = error.message) != null ? _a12 : `${error}`);
5145
5183
  } finally {
5146
5184
  controller.close();
5147
5185
  }
@@ -5202,7 +5240,7 @@ function toDataStreamInternal(stream, callbacks) {
5202
5240
  return stream.pipeThrough(
5203
5241
  new TransformStream({
5204
5242
  transform: async (value, controller) => {
5205
- var _a11;
5243
+ var _a12;
5206
5244
  if (typeof value === "string") {
5207
5245
  controller.enqueue(value);
5208
5246
  return;
@@ -5210,7 +5248,7 @@ function toDataStreamInternal(stream, callbacks) {
5210
5248
  if ("event" in value) {
5211
5249
  if (value.event === "on_chat_model_stream") {
5212
5250
  forwardAIMessageChunk(
5213
- (_a11 = value.data) == null ? void 0 : _a11.chunk,
5251
+ (_a12 = value.data) == null ? void 0 : _a12.chunk,
5214
5252
  controller
5215
5253
  );
5216
5254
  }
@@ -5233,7 +5271,7 @@ function toDataStream(stream, callbacks) {
5233
5271
  );
5234
5272
  }
5235
5273
  function toDataStreamResponse(stream, options) {
5236
- var _a11;
5274
+ var _a12;
5237
5275
  const dataStream = toDataStreamInternal(
5238
5276
  stream,
5239
5277
  options == null ? void 0 : options.callbacks
@@ -5242,7 +5280,7 @@ function toDataStreamResponse(stream, options) {
5242
5280
  const init = options == null ? void 0 : options.init;
5243
5281
  const responseStream = data ? mergeStreams(data.stream, dataStream) : dataStream;
5244
5282
  return new Response(responseStream, {
5245
- status: (_a11 = init == null ? void 0 : init.status) != null ? _a11 : 200,
5283
+ status: (_a12 = init == null ? void 0 : init.status) != null ? _a12 : 200,
5246
5284
  statusText: init == null ? void 0 : init.statusText,
5247
5285
  headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
5248
5286
  contentType: "text/plain; charset=utf-8",
@@ -5297,14 +5335,14 @@ function toDataStream2(stream, callbacks) {
5297
5335
  );
5298
5336
  }
5299
5337
  function toDataStreamResponse2(stream, options = {}) {
5300
- var _a11;
5338
+ var _a12;
5301
5339
  const { init, data, callbacks } = options;
5302
5340
  const dataStream = toDataStreamInternal2(stream, callbacks).pipeThrough(
5303
5341
  new TextEncoderStream()
5304
5342
  );
5305
5343
  const responseStream = data ? mergeStreams(data.stream, dataStream) : dataStream;
5306
5344
  return new Response(responseStream, {
5307
- status: (_a11 = init == null ? void 0 : init.status) != null ? _a11 : 200,
5345
+ status: (_a12 = init == null ? void 0 : init.status) != null ? _a12 : 200,
5308
5346
  statusText: init == null ? void 0 : init.statusText,
5309
5347
  headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
5310
5348
  contentType: "text/plain; charset=utf-8",
@@ -5421,6 +5459,7 @@ var StreamData = class {
5421
5459
  Output,
5422
5460
  RetryError,
5423
5461
  StreamData,
5462
+ ToolExecutionError,
5424
5463
  TypeValidationError,
5425
5464
  UnsupportedFunctionalityError,
5426
5465
  convertToCoreMessages,