ai 7.0.127 → 7.0.128

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.
Files changed (72) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/index.d.ts +213 -153
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +239 -178
  5. package/dist/index.js.map +1 -1
  6. package/dist/internal/index.d.ts +278 -68
  7. package/dist/internal/index.d.ts.map +1 -1
  8. package/dist/internal/index.js +340 -61
  9. package/dist/internal/index.js.map +1 -1
  10. package/dist/test/index.d.ts +25 -17
  11. package/dist/test/index.d.ts.map +1 -1
  12. package/dist/test/index.js +27 -21
  13. package/dist/test/index.js.map +1 -1
  14. package/docs/02-getting-started/03-nextjs-pages-router.mdx +1 -1
  15. package/docs/02-getting-started/07-expo.mdx +1 -1
  16. package/docs/03-agents/07-workflow-agent.mdx +80 -12
  17. package/docs/03-ai-sdk-core/16-mcp-tools.mdx +44 -0
  18. package/docs/03-ai-sdk-core/{32-evaluation.mdx → 32-decisions.mdx} +49 -49
  19. package/docs/03-ai-sdk-core/35-image-generation.mdx +1 -1
  20. package/docs/03-ai-sdk-core/45-provider-management.mdx +8 -8
  21. package/docs/03-ai-sdk-core/60-telemetry.mdx +40 -31
  22. package/docs/03-ai-sdk-core/65-lifecycle-callbacks.mdx +21 -22
  23. package/docs/03-ai-sdk-harnesses/08-terminal-ui.mdx +2 -2
  24. package/docs/06-advanced/09-multistep-interfaces.mdx +1 -1
  25. package/docs/07-reference/01-ai-sdk-core/14-decide.mdx +74 -0
  26. package/docs/07-reference/01-ai-sdk-core/23-create-mcp-client.mdx +58 -4
  27. package/docs/07-reference/01-ai-sdk-core/40-provider-registry.mdx +11 -11
  28. package/docs/07-reference/01-ai-sdk-core/42-custom-provider.mdx +9 -9
  29. package/docs/07-reference/01-ai-sdk-core/66-extract-reasoning-middleware.mdx +24 -8
  30. package/docs/07-reference/02-ai-sdk-ui/32-prune-messages.mdx +1 -1
  31. package/docs/07-reference/03-ai-sdk-rsc/01-stream-ui.mdx +1 -0
  32. package/docs/07-reference/04-ai-sdk-workflow/01-workflow-agent.mdx +39 -2
  33. package/docs/07-reference/05-ai-sdk-errors/ai-decision-unsupported-question-type-error.mdx +31 -0
  34. package/docs/07-reference/05-ai-sdk-errors/ai-no-such-model-error.mdx +3 -3
  35. package/docs/07-reference/05-ai-sdk-errors/ai-no-such-provider-error.mdx +3 -3
  36. package/docs/08-migration-guides/23-migration-guide-7-0.mdx +1 -1
  37. package/docs/08-migration-guides/29-migration-guide-4-0.mdx +1 -1
  38. package/docs/09-troubleshooting/05-tool-invocation-missing-result.mdx +2 -2
  39. package/package.json +12 -12
  40. package/src/decide/decide-events.ts +129 -0
  41. package/src/{evaluate/evaluate.ts → decide/decide.ts} +36 -38
  42. package/src/decide/decision-provider.ts +11 -0
  43. package/src/decide/decision-result.ts +39 -0
  44. package/src/decide/index.ts +64 -0
  45. package/src/{evaluate → decide}/restricted-telemetry-dispatcher.ts +7 -7
  46. package/src/{evaluate/validate-evaluation.ts → decide/validate-decision.ts} +9 -9
  47. package/src/error/index.ts +10 -1
  48. package/src/generate-speech/generated-audio-file.ts +5 -4
  49. package/src/generate-text/default-generate-text-result.ts +127 -0
  50. package/src/generate-text/generate-text.ts +2 -122
  51. package/src/global.ts +1 -1
  52. package/src/index.ts +1 -1
  53. package/src/middleware/extract-reasoning-middleware.ts +25 -6
  54. package/src/model/resolve-model.ts +45 -14
  55. package/src/registry/custom-provider.ts +38 -26
  56. package/src/registry/index.ts +13 -1
  57. package/src/registry/no-such-provider-error.ts +2 -0
  58. package/src/registry/provider-registry.ts +56 -29
  59. package/src/telemetry/create-telemetry-dispatcher.ts +17 -5
  60. package/src/telemetry/telemetry.ts +35 -19
  61. package/src/telemetry/tracing-channel.ts +1 -1
  62. package/src/test/decision-mock-model-v4.ts +35 -0
  63. package/src/transcribe/transcribe.ts +1 -1
  64. package/src/ui/chat.ts +9 -1
  65. package/src/ui/process-ui-message-stream.ts +21 -0
  66. package/docs/07-reference/01-ai-sdk-core/14-evaluate.mdx +0 -74
  67. package/docs/07-reference/05-ai-sdk-errors/ai-evaluation-unsupported-question-type-error.mdx +0 -31
  68. package/src/evaluate/evaluate-events.ts +0 -131
  69. package/src/evaluate/evaluation-provider.ts +0 -6
  70. package/src/evaluate/evaluation-result.ts +0 -39
  71. package/src/evaluate/index.ts +0 -13
  72. package/src/test/evaluation-mock-model-v4.ts +0 -27
@@ -111,7 +111,7 @@ function asAsyncIterableStream(stream) {
111
111
  }
112
112
  //#endregion
113
113
  //#region src/version.ts
114
- const VERSION = "7.0.127";
114
+ const VERSION = "7.0.128";
115
115
  //#endregion
116
116
  //#region src/util/download/download.ts
117
117
  /**
@@ -203,21 +203,21 @@ function mergeObjects(base, overrides) {
203
203
  }
204
204
  //#endregion
205
205
  //#region src/prompt/invalid-data-content-error.ts
206
- const name$10 = "AI_InvalidDataContentError";
207
- const marker$10 = `vercel.ai.error.${name$10}`;
208
- const symbol$10 = Symbol.for(marker$10);
206
+ const name$11 = "AI_InvalidDataContentError";
207
+ const marker$11 = `vercel.ai.error.${name$11}`;
208
+ const symbol$11 = Symbol.for(marker$11);
209
209
  var InvalidDataContentError = class extends AISDKError {
210
210
  constructor({ content, cause, message = `Invalid data content. Expected a base64 string, Uint8Array, ArrayBuffer, or Buffer, but got ${typeof content}.` }) {
211
211
  super({
212
- name: name$10,
212
+ name: name$11,
213
213
  message,
214
214
  cause
215
215
  });
216
- this[symbol$10] = true;
216
+ this[symbol$11] = true;
217
217
  this.content = content;
218
218
  }
219
219
  static isInstance(error) {
220
- return AISDKError.hasMarker(error, marker$10);
220
+ return AISDKError.hasMarker(error, marker$11);
221
221
  }
222
222
  };
223
223
  //#endregion
@@ -422,38 +422,38 @@ const logWarnings = (options) => {
422
422
  };
423
423
  //#endregion
424
424
  //#region src/prompt/invalid-message-role-error.ts
425
- const name$9 = "AI_InvalidMessageRoleError";
426
- const marker$9 = `vercel.ai.error.${name$9}`;
427
- const symbol$9 = Symbol.for(marker$9);
425
+ const name$10 = "AI_InvalidMessageRoleError";
426
+ const marker$10 = `vercel.ai.error.${name$10}`;
427
+ const symbol$10 = Symbol.for(marker$10);
428
428
  var InvalidMessageRoleError = class extends AISDKError {
429
429
  constructor({ role, message = `Invalid message role: '${role}'. Must be one of: "system", "user", "assistant", "tool".` }) {
430
430
  super({
431
- name: name$9,
431
+ name: name$10,
432
432
  message
433
433
  });
434
- this[symbol$9] = true;
434
+ this[symbol$10] = true;
435
435
  this.role = role;
436
436
  }
437
437
  static isInstance(error) {
438
- return AISDKError.hasMarker(error, marker$9);
438
+ return AISDKError.hasMarker(error, marker$10);
439
439
  }
440
440
  };
441
441
  //#endregion
442
442
  //#region src/error/missing-tool-result-error.ts
443
- const name$8 = "AI_MissingToolResultsError";
444
- const marker$8 = `vercel.ai.error.${name$8}`;
445
- const symbol$8 = Symbol.for(marker$8);
443
+ const name$9 = "AI_MissingToolResultsError";
444
+ const marker$9 = `vercel.ai.error.${name$9}`;
445
+ const symbol$9 = Symbol.for(marker$9);
446
446
  var MissingToolResultsError = class extends AISDKError {
447
447
  constructor({ toolCallIds }) {
448
448
  super({
449
- name: name$8,
449
+ name: name$9,
450
450
  message: `Tool result${toolCallIds.length > 1 ? "s are" : " is"} missing for tool call${toolCallIds.length > 1 ? "s" : ""} ${toolCallIds.join(", ")}.`
451
451
  });
452
- this[symbol$8] = true;
452
+ this[symbol$9] = true;
453
453
  this.toolCallIds = toolCallIds;
454
454
  }
455
455
  static isInstance(error) {
456
- return AISDKError.hasMarker(error, marker$8);
456
+ return AISDKError.hasMarker(error, marker$9);
457
457
  }
458
458
  };
459
459
  //#endregion
@@ -1432,21 +1432,21 @@ async function standardizePrompt({ allowSystemInMessages = false, system, instru
1432
1432
  }
1433
1433
  //#endregion
1434
1434
  //#region src/error/invalid-argument-error.ts
1435
- const name$7 = "AI_InvalidArgumentError";
1436
- const marker$7 = `vercel.ai.error.${name$7}`;
1437
- const symbol$7 = Symbol.for(marker$7);
1435
+ const name$8 = "AI_InvalidArgumentError";
1436
+ const marker$8 = `vercel.ai.error.${name$8}`;
1437
+ const symbol$8 = Symbol.for(marker$8);
1438
1438
  var InvalidArgumentError = class extends AISDKError {
1439
1439
  constructor({ parameter, value, message }) {
1440
1440
  super({
1441
- name: name$7,
1441
+ name: name$8,
1442
1442
  message: `Invalid argument for parameter ${parameter}: ${message}`
1443
1443
  });
1444
- this[symbol$7] = true;
1444
+ this[symbol$8] = true;
1445
1445
  this.parameter = parameter;
1446
1446
  this.value = value;
1447
1447
  }
1448
1448
  static isInstance(error) {
1449
- return AISDKError.hasMarker(error, marker$7);
1449
+ return AISDKError.hasMarker(error, marker$8);
1450
1450
  }
1451
1451
  };
1452
1452
  //#endregion
@@ -1523,22 +1523,22 @@ function prepareLanguageModelCallOptions({ maxOutputTokens, temperature, topP, t
1523
1523
  }
1524
1524
  //#endregion
1525
1525
  //#region src/util/retry-error.ts
1526
- const name$6 = "AI_RetryError";
1527
- const marker$6 = `vercel.ai.error.${name$6}`;
1528
- const symbol$6 = Symbol.for(marker$6);
1526
+ const name$7 = "AI_RetryError";
1527
+ const marker$7 = `vercel.ai.error.${name$7}`;
1528
+ const symbol$7 = Symbol.for(marker$7);
1529
1529
  var RetryError = class extends AISDKError {
1530
1530
  constructor({ message, reason, errors }) {
1531
1531
  super({
1532
- name: name$6,
1532
+ name: name$7,
1533
1533
  message
1534
1534
  });
1535
- this[symbol$6] = true;
1535
+ this[symbol$7] = true;
1536
1536
  this.reason = reason;
1537
1537
  this.errors = errors;
1538
1538
  this.lastError = errors[errors.length - 1];
1539
1539
  }
1540
1540
  static isInstance(error) {
1541
- return AISDKError.hasMarker(error, marker$6);
1541
+ return AISDKError.hasMarker(error, marker$7);
1542
1542
  }
1543
1543
  };
1544
1544
  //#endregion
@@ -1667,76 +1667,97 @@ function addTokenCounts(tokenCount1, tokenCount2) {
1667
1667
  }
1668
1668
  //#endregion
1669
1669
  //#region src/error/invalid-tool-approval-error.ts
1670
- const name$5 = "AI_InvalidToolApprovalError";
1671
- const marker$5 = `vercel.ai.error.${name$5}`;
1672
- const symbol$5 = Symbol.for(marker$5);
1670
+ const name$6 = "AI_InvalidToolApprovalError";
1671
+ const marker$6 = `vercel.ai.error.${name$6}`;
1672
+ const symbol$6 = Symbol.for(marker$6);
1673
1673
  var InvalidToolApprovalError = class extends AISDKError {
1674
1674
  constructor({ approvalId }) {
1675
1675
  super({
1676
- name: name$5,
1676
+ name: name$6,
1677
1677
  message: `Tool approval response references unknown approvalId: "${approvalId}". No matching tool-approval-request found in message history.`
1678
1678
  });
1679
- this[symbol$5] = true;
1679
+ this[symbol$6] = true;
1680
1680
  this.approvalId = approvalId;
1681
1681
  }
1682
1682
  static isInstance(error) {
1683
- return AISDKError.hasMarker(error, marker$5);
1683
+ return AISDKError.hasMarker(error, marker$6);
1684
1684
  }
1685
1685
  };
1686
1686
  //#endregion
1687
1687
  //#region src/error/invalid-tool-approval-signature-error.ts
1688
- const name$4 = "AI_InvalidToolApprovalSignatureError";
1689
- const marker$4 = `vercel.ai.error.${name$4}`;
1690
- const symbol$4 = Symbol.for(marker$4);
1688
+ const name$5 = "AI_InvalidToolApprovalSignatureError";
1689
+ const marker$5 = `vercel.ai.error.${name$5}`;
1690
+ const symbol$5 = Symbol.for(marker$5);
1691
1691
  var InvalidToolApprovalSignatureError = class extends AISDKError {
1692
1692
  constructor({ approvalId, toolCallId, reason }) {
1693
1693
  super({
1694
- name: name$4,
1694
+ name: name$5,
1695
1695
  message: `Tool approval signature verification failed for approval "${approvalId}" (tool call "${toolCallId}"): ${reason}`
1696
1696
  });
1697
- this[symbol$4] = true;
1697
+ this[symbol$5] = true;
1698
1698
  this.approvalId = approvalId;
1699
1699
  this.toolCallId = toolCallId;
1700
1700
  }
1701
1701
  static isInstance(error) {
1702
- return AISDKError.hasMarker(error, marker$4);
1702
+ return AISDKError.hasMarker(error, marker$5);
1703
1703
  }
1704
1704
  };
1705
1705
  //#endregion
1706
1706
  //#region src/error/invalid-tool-input-error.ts
1707
- const name$3 = "AI_InvalidToolInputError";
1708
- const marker$3 = `vercel.ai.error.${name$3}`;
1709
- const symbol$3 = Symbol.for(marker$3);
1707
+ const name$4 = "AI_InvalidToolInputError";
1708
+ const marker$4 = `vercel.ai.error.${name$4}`;
1709
+ const symbol$4 = Symbol.for(marker$4);
1710
1710
  var InvalidToolInputError = class extends AISDKError {
1711
1711
  constructor({ toolInput, toolName, cause, message = `Invalid input for tool ${toolName}: ${getErrorMessage(cause)}` }) {
1712
1712
  super({
1713
- name: name$3,
1713
+ name: name$4,
1714
1714
  message,
1715
1715
  cause
1716
1716
  });
1717
- this[symbol$3] = true;
1717
+ this[symbol$4] = true;
1718
1718
  this.toolInput = toolInput;
1719
1719
  this.toolName = toolName;
1720
1720
  }
1721
1721
  static isInstance(error) {
1722
- return AISDKError.hasMarker(error, marker$3);
1722
+ return AISDKError.hasMarker(error, marker$4);
1723
1723
  }
1724
1724
  };
1725
1725
  //#endregion
1726
1726
  //#region src/error/tool-call-not-found-for-approval-error.ts
1727
- const name$2 = "AI_ToolCallNotFoundForApprovalError";
1728
- const marker$2 = `vercel.ai.error.${name$2}`;
1729
- const symbol$2 = Symbol.for(marker$2);
1727
+ const name$3 = "AI_ToolCallNotFoundForApprovalError";
1728
+ const marker$3 = `vercel.ai.error.${name$3}`;
1729
+ const symbol$3 = Symbol.for(marker$3);
1730
1730
  var ToolCallNotFoundForApprovalError = class extends AISDKError {
1731
1731
  constructor({ toolCallId, approvalId }) {
1732
1732
  super({
1733
- name: name$2,
1733
+ name: name$3,
1734
1734
  message: `Tool call "${toolCallId}" not found for approval request "${approvalId}".`
1735
1735
  });
1736
- this[symbol$2] = true;
1736
+ this[symbol$3] = true;
1737
1737
  this.toolCallId = toolCallId;
1738
1738
  this.approvalId = approvalId;
1739
1739
  }
1740
+ static isInstance(error) {
1741
+ return AISDKError.hasMarker(error, marker$3);
1742
+ }
1743
+ };
1744
+ //#endregion
1745
+ //#region src/error/no-output-generated-error.ts
1746
+ const name$2 = "AI_NoOutputGeneratedError";
1747
+ const marker$2 = `vercel.ai.error.${name$2}`;
1748
+ const symbol$2 = Symbol.for(marker$2);
1749
+ /**
1750
+ * Thrown when no LLM output was generated, e.g. because of errors.
1751
+ */
1752
+ var NoOutputGeneratedError = class extends AISDKError {
1753
+ constructor({ message = "No output generated.", cause } = {}) {
1754
+ super({
1755
+ name: name$2,
1756
+ message,
1757
+ cause
1758
+ });
1759
+ this[symbol$2] = true;
1760
+ }
1740
1761
  static isInstance(error) {
1741
1762
  return AISDKError.hasMarker(error, marker$2);
1742
1763
  }
@@ -2364,8 +2385,11 @@ function createTelemetryDispatcher({ telemetry }) {
2364
2385
  includeRuntimeContext: telemetry?.includeRuntimeContext,
2365
2386
  includeToolsContext: telemetry?.includeToolsContext
2366
2387
  };
2367
- const mergeTelemetryCallback = (key) => {
2368
- const integrationCallbacks = integrations.map((integration) => integration[key]?.bind(integration)).filter(Boolean).map((callback) => ((event) => callback(augmentEvent(event, telemetryMetadata))));
2388
+ const mergeTelemetryCallback = (key, deprecatedKey) => {
2389
+ const integrationCallbacks = integrations.map((integration) => {
2390
+ const callback = integration[key] ?? (deprecatedKey == null ? void 0 : integration[deprecatedKey]);
2391
+ return typeof callback === "function" ? callback.bind(integration) : void 0;
2392
+ }).filter(Boolean).map((callback) => ((event) => callback(augmentEvent(event, telemetryMetadata))));
2369
2393
  if (integrationCallbacks.length === 0) return;
2370
2394
  const mergedIntegrationCallback = mergeCallbacks(...integrationCallbacks);
2371
2395
  return async (event) => {
@@ -2401,10 +2425,10 @@ function createTelemetryDispatcher({ telemetry }) {
2401
2425
  onEmbedEnd: mergeTelemetryCallback("onEmbedEnd"),
2402
2426
  onRerankStart: mergeTelemetryCallback("onRerankStart"),
2403
2427
  onRerankEnd: mergeTelemetryCallback("onRerankEnd"),
2404
- experimental_onEvaluateStart: mergeTelemetryCallback("experimental_onEvaluateStart"),
2405
- experimental_onEvaluationModelCallStart: mergeTelemetryCallback("experimental_onEvaluationModelCallStart"),
2406
- experimental_onEvaluationModelCallEnd: mergeTelemetryCallback("experimental_onEvaluationModelCallEnd"),
2407
- experimental_onEvaluateEnd: mergeTelemetryCallback("experimental_onEvaluateEnd"),
2428
+ experimental_onDecideStart: mergeTelemetryCallback("experimental_onDecideStart", "experimental_onEvaluateStart"),
2429
+ experimental_onDecisionModelCallStart: mergeTelemetryCallback("experimental_onDecisionModelCallStart", "experimental_onEvaluationModelCallStart"),
2430
+ experimental_onDecisionModelCallEnd: mergeTelemetryCallback("experimental_onDecisionModelCallEnd", "experimental_onEvaluationModelCallEnd"),
2431
+ experimental_onDecideEnd: mergeTelemetryCallback("experimental_onDecideEnd", "experimental_onEvaluateEnd"),
2408
2432
  experimental_onStreamTranscriptionStart: mergeTelemetryCallback("experimental_onStreamTranscriptionStart"),
2409
2433
  experimental_onStreamTranscriptionEnd: mergeTelemetryCallback("experimental_onStreamTranscriptionEnd"),
2410
2434
  onEnd: mergeTelemetryCallback("onEnd"),
@@ -2456,7 +2480,30 @@ function asReasoningText(reasoningParts) {
2456
2480
  return reasoningText.length > 0 ? reasoningText : void 0;
2457
2481
  }
2458
2482
  //#endregion
2483
+ //#region src/generate-text/generated-file.ts
2484
+ var DefaultGeneratedFile = class {
2485
+ constructor({ data, mediaType, providerMetadata }) {
2486
+ const isUint8Array = data instanceof Uint8Array;
2487
+ this.base64Data = isUint8Array ? void 0 : data;
2488
+ this.uint8ArrayData = isUint8Array ? data : void 0;
2489
+ this.mediaType = mediaType;
2490
+ this.providerMetadata = providerMetadata;
2491
+ }
2492
+ get base64() {
2493
+ if (this.base64Data == null) this.base64Data = convertUint8ArrayToBase64(this.uint8ArrayData);
2494
+ return this.base64Data;
2495
+ }
2496
+ get uint8Array() {
2497
+ if (this.uint8ArrayData == null) this.uint8ArrayData = convertBase64ToUint8Array(this.base64Data);
2498
+ return this.uint8ArrayData;
2499
+ }
2500
+ };
2501
+ //#endregion
2459
2502
  //#region src/generate-text/reasoning-output.ts
2503
+ function unwrapReasoningFileData(data) {
2504
+ if (typeof data === "object" && data !== null && "type" in data) return data.type === "data" ? data.data : data.url;
2505
+ return data;
2506
+ }
2460
2507
  function convertFromReasoningOutputs(parts) {
2461
2508
  return parts.map((part) => {
2462
2509
  if (part.type === "reasoning") return {
@@ -2472,6 +2519,24 @@ function convertFromReasoningOutputs(parts) {
2472
2519
  };
2473
2520
  });
2474
2521
  }
2522
+ function convertToReasoningOutputs(parts) {
2523
+ return parts.map((part) => {
2524
+ if (part.type === "reasoning") return {
2525
+ type: "reasoning",
2526
+ text: part.text,
2527
+ ...part.providerOptions != null ? { providerMetadata: part.providerOptions } : {}
2528
+ };
2529
+ const rawData = unwrapReasoningFileData(part.data);
2530
+ return {
2531
+ type: "reasoning-file",
2532
+ file: new DefaultGeneratedFile({
2533
+ data: rawData instanceof ArrayBuffer ? new Uint8Array(rawData) : rawData instanceof URL ? rawData.toString() : rawData,
2534
+ mediaType: part.mediaType
2535
+ }),
2536
+ ...part.providerOptions != null ? { providerMetadata: part.providerOptions } : {}
2537
+ };
2538
+ });
2539
+ }
2475
2540
  //#endregion
2476
2541
  //#region src/generate-text/step-result.ts
2477
2542
  var DefaultStepResult = class {
@@ -3407,6 +3472,220 @@ function sortToolResultContentByToolCallOrder({ toolResultContent, toolCallOrder
3407
3472
  return toolResultContent.map((part) => part.type === "tool-result" ? sortedToolResults[toolResultIndex++] : part);
3408
3473
  }
3409
3474
  //#endregion
3410
- export { DefaultStepResult, addLanguageModelUsage, asLanguageModelUsage, collectToolApprovals, convertAsyncIteratorToReadableStream, convertToLanguageModelPrompt, createAsyncIterableStream, createDefaultDownloadFunction, createNullLanguageModelUsage, createRestrictedTelemetryDispatcher, createTelemetryDispatcher, createToolModelOutput, createToolSearchState, downloadAssets, mapToolResultOutput, mergeAbortSignals, mergeCallbacks, parseToolCall, prepareLanguageModelCallOptions as prepareCallSettings, prepareLanguageModelCallOptions, prepareRetries, prepareToolChoice, prepareTools, resolveLanguageModel, signToolApproval, standardizePrompt, toResponseMessages, validateApprovedToolApprovals, validateToolContext, verifyToolApprovalSignature };
3475
+ //#region src/generate-text/default-generate-text-result.ts
3476
+ var DefaultGenerateTextResult = class {
3477
+ constructor(options) {
3478
+ this.initialResponseMessages = options.initialResponseMessages;
3479
+ this.steps = options.steps;
3480
+ this._output = options.output;
3481
+ this.totalUsage = options.totalUsage;
3482
+ }
3483
+ get finalStep() {
3484
+ return this.steps.at(-1);
3485
+ }
3486
+ get content() {
3487
+ return this.steps.flatMap((step) => step.content);
3488
+ }
3489
+ get text() {
3490
+ return this.finalStep.text;
3491
+ }
3492
+ get files() {
3493
+ return this.steps.flatMap((step) => step.files);
3494
+ }
3495
+ get reasoningText() {
3496
+ return this.finalStep.reasoningText;
3497
+ }
3498
+ get reasoning() {
3499
+ return convertToReasoningOutputs(this.finalStep.reasoning);
3500
+ }
3501
+ get toolCalls() {
3502
+ return this.steps.flatMap((step) => step.toolCalls);
3503
+ }
3504
+ get staticToolCalls() {
3505
+ return this.steps.flatMap((step) => step.staticToolCalls);
3506
+ }
3507
+ get dynamicToolCalls() {
3508
+ return this.steps.flatMap((step) => step.dynamicToolCalls);
3509
+ }
3510
+ get toolResults() {
3511
+ return this.steps.flatMap((step) => step.toolResults);
3512
+ }
3513
+ get staticToolResults() {
3514
+ return this.steps.flatMap((step) => step.staticToolResults);
3515
+ }
3516
+ get dynamicToolResults() {
3517
+ return this.steps.flatMap((step) => step.dynamicToolResults);
3518
+ }
3519
+ get sources() {
3520
+ return this.steps.flatMap((step) => step.sources);
3521
+ }
3522
+ get finishReason() {
3523
+ return this.finalStep.finishReason;
3524
+ }
3525
+ get rawFinishReason() {
3526
+ return this.finalStep.rawFinishReason;
3527
+ }
3528
+ get warnings() {
3529
+ return this.steps.flatMap((step) => step.warnings ?? []);
3530
+ }
3531
+ get providerMetadata() {
3532
+ return this.finalStep.providerMetadata;
3533
+ }
3534
+ get response() {
3535
+ return this.finalStep.response;
3536
+ }
3537
+ get responseMessages() {
3538
+ return [...this.initialResponseMessages, ...this.steps.flatMap((step) => step.response.messages)];
3539
+ }
3540
+ get request() {
3541
+ return this.finalStep.request;
3542
+ }
3543
+ get usage() {
3544
+ return this.totalUsage;
3545
+ }
3546
+ get output() {
3547
+ if (this._output == null) throw new NoOutputGeneratedError();
3548
+ return this._output;
3549
+ }
3550
+ };
3551
+ //#endregion
3552
+ //#region src/generate-text/resolve-generated-file-data.ts
3553
+ async function resolveGeneratedFileData({ data, abortSignal, cache }) {
3554
+ if (data.type === "data") return data.data;
3555
+ const cachedData = cache?.get(data);
3556
+ if (cachedData != null) return cachedData;
3557
+ const downloadedData = (await download({
3558
+ url: data.url,
3559
+ abortSignal
3560
+ })).data;
3561
+ cache?.set(data, downloadedData);
3562
+ return downloadedData;
3563
+ }
3564
+ //#endregion
3565
+ //#region src/generate-text/convert-language-model-content.ts
3566
+ async function convertLanguageModelContent({ content, toolCalls, toolOutputs, toolApprovalRequests, toolApprovalResponses, tools, abortSignal, generatedFileDataCache }) {
3567
+ const contentParts = [];
3568
+ const toolOutputsWithApprovalResponses = [];
3569
+ const toolOutputsWithoutApprovalResponses = [];
3570
+ const toolCallIdsWithApprovalResponses = new Set(toolApprovalResponses.map((toolApprovalResponse) => toolApprovalResponse.toolCall.toolCallId));
3571
+ for (const part of content) switch (part.type) {
3572
+ case "text":
3573
+ case "reasoning":
3574
+ case "custom":
3575
+ case "source":
3576
+ contentParts.push(part);
3577
+ break;
3578
+ case "file":
3579
+ case "reasoning-file":
3580
+ contentParts.push({
3581
+ type: part.type,
3582
+ file: new DefaultGeneratedFile({
3583
+ data: await resolveGeneratedFileData({
3584
+ data: part.data,
3585
+ abortSignal,
3586
+ cache: generatedFileDataCache
3587
+ }),
3588
+ mediaType: part.mediaType
3589
+ }),
3590
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
3591
+ });
3592
+ break;
3593
+ case "tool-call": {
3594
+ const toolCall = toolCalls.find((toolCall) => toolCall.toolCallId === part.toolCallId);
3595
+ if (toolCall == null) throw new Error(`Tool call ${part.toolCallId} not found.`);
3596
+ contentParts.push(toolCall);
3597
+ break;
3598
+ }
3599
+ case "tool-result": {
3600
+ const toolCall = toolCalls.find((toolCall) => toolCall.toolCallId === part.toolCallId);
3601
+ if (toolCall == null) {
3602
+ const tool = getOwn(tools, part.toolName);
3603
+ if (!(tool?.type === "provider" && tool.supportsDeferredResults)) throw new Error(`Tool call ${part.toolCallId} not found.`);
3604
+ if (part.isError) contentParts.push({
3605
+ type: "tool-error",
3606
+ toolCallId: part.toolCallId,
3607
+ toolName: part.toolName,
3608
+ input: void 0,
3609
+ error: part.result,
3610
+ providerExecuted: true,
3611
+ dynamic: part.dynamic,
3612
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {},
3613
+ ...tool?.metadata != null ? { toolMetadata: tool.metadata } : {}
3614
+ });
3615
+ else contentParts.push({
3616
+ type: "tool-result",
3617
+ toolCallId: part.toolCallId,
3618
+ toolName: part.toolName,
3619
+ input: void 0,
3620
+ output: part.result,
3621
+ providerExecuted: true,
3622
+ dynamic: part.dynamic,
3623
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {},
3624
+ ...tool?.metadata != null ? { toolMetadata: tool.metadata } : {}
3625
+ });
3626
+ break;
3627
+ }
3628
+ if (part.isError) contentParts.push({
3629
+ type: "tool-error",
3630
+ toolCallId: part.toolCallId,
3631
+ toolName: part.toolName,
3632
+ input: toolCall.input,
3633
+ error: part.result,
3634
+ providerExecuted: true,
3635
+ dynamic: toolCall.dynamic,
3636
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {},
3637
+ ...toolCall.toolMetadata != null ? { toolMetadata: toolCall.toolMetadata } : {}
3638
+ });
3639
+ else contentParts.push({
3640
+ type: "tool-result",
3641
+ toolCallId: part.toolCallId,
3642
+ toolName: part.toolName,
3643
+ input: toolCall.input,
3644
+ output: part.result,
3645
+ providerExecuted: true,
3646
+ dynamic: toolCall.dynamic,
3647
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {},
3648
+ ...toolCall.toolMetadata != null ? { toolMetadata: toolCall.toolMetadata } : {}
3649
+ });
3650
+ break;
3651
+ }
3652
+ case "tool-approval-request": {
3653
+ const toolCall = toolCalls.find((toolCall) => toolCall.toolCallId === part.toolCallId);
3654
+ if (toolCall == null) throw new ToolCallNotFoundForApprovalError({
3655
+ toolCallId: part.toolCallId,
3656
+ approvalId: part.approvalId
3657
+ });
3658
+ contentParts.push({
3659
+ type: "tool-approval-request",
3660
+ approvalId: part.approvalId,
3661
+ toolCall
3662
+ });
3663
+ break;
3664
+ }
3665
+ }
3666
+ for (const toolOutput of toolOutputs) if (toolCallIdsWithApprovalResponses.has(toolOutput.toolCallId)) toolOutputsWithApprovalResponses.push(toolOutput);
3667
+ else toolOutputsWithoutApprovalResponses.push(toolOutput);
3668
+ return [
3669
+ ...contentParts,
3670
+ ...toolOutputsWithoutApprovalResponses,
3671
+ ...toolApprovalRequests,
3672
+ ...toolApprovalResponses,
3673
+ ...toolOutputsWithApprovalResponses
3674
+ ];
3675
+ }
3676
+ //#endregion
3677
+ //#region src/generate-text/calculate-tokens-per-second.ts
3678
+ /**
3679
+ * Calculates a token rate in tokens per second.
3680
+ *
3681
+ * Returns 0 when the token count is unknown, the duration is unknown or 0, or
3682
+ * the computed rate cannot be represented as a finite JSON-safe number.
3683
+ */
3684
+ function calculateTokensPerSecond({ tokens, durationMs }) {
3685
+ const tokenRate = 1e3 * (tokens ?? 0) / (durationMs ?? 0);
3686
+ return Number.isFinite(tokenRate) ? tokenRate : 0;
3687
+ }
3688
+ //#endregion
3689
+ export { DefaultGenerateTextResult, DefaultStepResult, addLanguageModelUsage, asLanguageModelUsage, calculateTokensPerSecond, collectToolApprovals, convertAsyncIteratorToReadableStream, convertLanguageModelContent, convertToLanguageModelPrompt, createAsyncIterableStream, createDefaultDownloadFunction, createNullLanguageModelUsage, createRestrictedTelemetryDispatcher, createTelemetryDispatcher, createToolModelOutput, createToolSearchState, downloadAssets, mapToolResultOutput, mergeAbortSignals, mergeCallbacks, parseToolCall, prepareLanguageModelCallOptions as prepareCallSettings, prepareLanguageModelCallOptions, prepareRetries, prepareToolChoice, prepareTools, resolveLanguageModel, signToolApproval, standardizePrompt, toResponseMessages, validateApprovedToolApprovals, validateToolContext, verifyToolApprovalSignature };
3411
3690
 
3412
3691
  //# sourceMappingURL=index.js.map