ai 6.0.0-beta.160 → 6.0.0-beta.162

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
@@ -24,68 +24,6 @@ import {
24
24
  withUserAgentSuffix as withUserAgentSuffix2
25
25
  } from "@ai-sdk/provider-utils";
26
26
 
27
- // src/logger/log-warnings.ts
28
- function formatWarning({
29
- warning,
30
- provider,
31
- model
32
- }) {
33
- const prefix = `AI SDK Warning (${provider} / ${model}):`;
34
- switch (warning.type) {
35
- case "unsupported": {
36
- let message = `${prefix} The feature "${warning.feature}" is not supported.`;
37
- if (warning.details) {
38
- message += ` ${warning.details}`;
39
- }
40
- return message;
41
- }
42
- case "compatibility": {
43
- let message = `${prefix} The feature "${warning.feature}" is used in a compatibility mode.`;
44
- if (warning.details) {
45
- message += ` ${warning.details}`;
46
- }
47
- return message;
48
- }
49
- case "other": {
50
- return `${prefix} ${warning.message}`;
51
- }
52
- default: {
53
- return `${prefix} ${JSON.stringify(warning, null, 2)}`;
54
- }
55
- }
56
- }
57
- var FIRST_WARNING_INFO_MESSAGE = "AI SDK Warning System: To turn off warning logging, set the AI_SDK_LOG_WARNINGS global to false.";
58
- var hasLoggedBefore = false;
59
- var logWarnings = (options) => {
60
- if (options.warnings.length === 0) {
61
- return;
62
- }
63
- const logger = globalThis.AI_SDK_LOG_WARNINGS;
64
- if (logger === false) {
65
- return;
66
- }
67
- if (typeof logger === "function") {
68
- logger(options);
69
- return;
70
- }
71
- if (!hasLoggedBefore) {
72
- hasLoggedBefore = true;
73
- console.info(FIRST_WARNING_INFO_MESSAGE);
74
- }
75
- for (const warning of options.warnings) {
76
- console.warn(
77
- formatWarning({
78
- warning,
79
- provider: options.provider,
80
- model: options.model
81
- })
82
- );
83
- }
84
- };
85
-
86
- // src/model/resolve-model.ts
87
- import { gateway } from "@ai-sdk/gateway";
88
-
89
27
  // src/error/index.ts
90
28
  import {
91
29
  AISDKError as AISDKError15,
@@ -407,6 +345,68 @@ var RetryError = class extends AISDKError14 {
407
345
  };
408
346
  _a12 = symbol12;
409
347
 
348
+ // src/logger/log-warnings.ts
349
+ function formatWarning({
350
+ warning,
351
+ provider,
352
+ model
353
+ }) {
354
+ const prefix = `AI SDK Warning (${provider} / ${model}):`;
355
+ switch (warning.type) {
356
+ case "unsupported": {
357
+ let message = `${prefix} The feature "${warning.feature}" is not supported.`;
358
+ if (warning.details) {
359
+ message += ` ${warning.details}`;
360
+ }
361
+ return message;
362
+ }
363
+ case "compatibility": {
364
+ let message = `${prefix} The feature "${warning.feature}" is used in a compatibility mode.`;
365
+ if (warning.details) {
366
+ message += ` ${warning.details}`;
367
+ }
368
+ return message;
369
+ }
370
+ case "other": {
371
+ return `${prefix} ${warning.message}`;
372
+ }
373
+ default: {
374
+ return `${prefix} ${JSON.stringify(warning, null, 2)}`;
375
+ }
376
+ }
377
+ }
378
+ var FIRST_WARNING_INFO_MESSAGE = "AI SDK Warning System: To turn off warning logging, set the AI_SDK_LOG_WARNINGS global to false.";
379
+ var hasLoggedBefore = false;
380
+ var logWarnings = (options) => {
381
+ if (options.warnings.length === 0) {
382
+ return;
383
+ }
384
+ const logger = globalThis.AI_SDK_LOG_WARNINGS;
385
+ if (logger === false) {
386
+ return;
387
+ }
388
+ if (typeof logger === "function") {
389
+ logger(options);
390
+ return;
391
+ }
392
+ if (!hasLoggedBefore) {
393
+ hasLoggedBefore = true;
394
+ console.info(FIRST_WARNING_INFO_MESSAGE);
395
+ }
396
+ for (const warning of options.warnings) {
397
+ console.warn(
398
+ formatWarning({
399
+ warning,
400
+ provider: options.provider,
401
+ model: options.model
402
+ })
403
+ );
404
+ }
405
+ };
406
+
407
+ // src/model/resolve-model.ts
408
+ import { gateway } from "@ai-sdk/gateway";
409
+
410
410
  // src/util/log-v2-compatibility-warning.ts
411
411
  function logV2CompatibilityWarning({
412
412
  provider,
@@ -844,7 +844,7 @@ import {
844
844
  } from "@ai-sdk/provider-utils";
845
845
 
846
846
  // src/version.ts
847
- var VERSION = true ? "6.0.0-beta.160" : "0.0.0-test";
847
+ var VERSION = true ? "6.0.0-beta.162" : "0.0.0-test";
848
848
 
849
849
  // src/util/download/download.ts
850
850
  var download = async ({ url }) => {
@@ -2044,6 +2044,39 @@ function addImageModelUsage(usage1, usage2) {
2044
2044
  };
2045
2045
  }
2046
2046
 
2047
+ // src/util/merge-objects.ts
2048
+ function mergeObjects(base, overrides) {
2049
+ if (base === void 0 && overrides === void 0) {
2050
+ return void 0;
2051
+ }
2052
+ if (base === void 0) {
2053
+ return overrides;
2054
+ }
2055
+ if (overrides === void 0) {
2056
+ return base;
2057
+ }
2058
+ const result = { ...base };
2059
+ for (const key in overrides) {
2060
+ if (Object.prototype.hasOwnProperty.call(overrides, key)) {
2061
+ const overridesValue = overrides[key];
2062
+ if (overridesValue === void 0)
2063
+ continue;
2064
+ const baseValue = key in base ? base[key] : void 0;
2065
+ const isSourceObject = overridesValue !== null && typeof overridesValue === "object" && !Array.isArray(overridesValue) && !(overridesValue instanceof Date) && !(overridesValue instanceof RegExp);
2066
+ const isTargetObject = baseValue !== null && baseValue !== void 0 && typeof baseValue === "object" && !Array.isArray(baseValue) && !(baseValue instanceof Date) && !(baseValue instanceof RegExp);
2067
+ if (isSourceObject && isTargetObject) {
2068
+ result[key] = mergeObjects(
2069
+ baseValue,
2070
+ overridesValue
2071
+ );
2072
+ } else {
2073
+ result[key] = overridesValue;
2074
+ }
2075
+ }
2076
+ }
2077
+ return result;
2078
+ }
2079
+
2047
2080
  // src/util/retry-with-exponential-backoff.ts
2048
2081
  import { APICallError as APICallError2 } from "@ai-sdk/provider";
2049
2082
  import { delay, getErrorMessage as getErrorMessage4, isAbortError } from "@ai-sdk/provider-utils";
@@ -3507,6 +3540,7 @@ async function generateText({
3507
3540
  let clientToolCalls = [];
3508
3541
  let clientToolOutputs = [];
3509
3542
  const steps = [];
3543
+ const pendingDeferredToolCalls = /* @__PURE__ */ new Map();
3510
3544
  do {
3511
3545
  const stepInputMessages = [...initialMessages, ...responseMessages];
3512
3546
  const prepareStepResult = await (prepareStep == null ? void 0 : prepareStep({
@@ -3575,13 +3609,17 @@ async function generateText({
3575
3609
  tracer,
3576
3610
  fn: async (span2) => {
3577
3611
  var _a16, _b2, _c2, _d2, _e2, _f2, _g2, _h2;
3612
+ const stepProviderOptions = mergeObjects(
3613
+ providerOptions,
3614
+ prepareStepResult == null ? void 0 : prepareStepResult.providerOptions
3615
+ );
3578
3616
  const result = await stepModel.doGenerate({
3579
3617
  ...callSettings2,
3580
3618
  tools: stepTools,
3581
3619
  toolChoice: stepToolChoice,
3582
3620
  responseFormat: await (output == null ? void 0 : output.responseFormat),
3583
3621
  prompt: promptMessages,
3584
- providerOptions,
3622
+ providerOptions: stepProviderOptions,
3585
3623
  abortSignal,
3586
3624
  headers: headersWithUserAgent
3587
3625
  });
@@ -3705,11 +3743,32 @@ async function generateText({
3705
3743
  })
3706
3744
  );
3707
3745
  }
3746
+ for (const toolCall of stepToolCalls) {
3747
+ if (!toolCall.providerExecuted)
3748
+ continue;
3749
+ const tool2 = tools == null ? void 0 : tools[toolCall.toolName];
3750
+ if ((tool2 == null ? void 0 : tool2.type) === "provider" && tool2.supportsDeferredResults) {
3751
+ const hasResultInResponse = currentModelResponse.content.some(
3752
+ (part) => part.type === "tool-result" && part.toolCallId === toolCall.toolCallId
3753
+ );
3754
+ if (!hasResultInResponse) {
3755
+ pendingDeferredToolCalls.set(toolCall.toolCallId, {
3756
+ toolName: toolCall.toolName
3757
+ });
3758
+ }
3759
+ }
3760
+ }
3761
+ for (const part of currentModelResponse.content) {
3762
+ if (part.type === "tool-result") {
3763
+ pendingDeferredToolCalls.delete(part.toolCallId);
3764
+ }
3765
+ }
3708
3766
  const stepContent = asContent({
3709
3767
  content: currentModelResponse.content,
3710
3768
  toolCalls: stepToolCalls,
3711
3769
  toolOutputs: clientToolOutputs,
3712
- toolApprovalRequests: Object.values(toolApprovalRequests)
3770
+ toolApprovalRequests: Object.values(toolApprovalRequests),
3771
+ tools
3713
3772
  });
3714
3773
  responseMessages.push(
3715
3774
  ...await toResponseMessages({
@@ -3738,9 +3797,10 @@ async function generateText({
3738
3797
  steps.push(currentStepResult);
3739
3798
  await (onStepFinish == null ? void 0 : onStepFinish(currentStepResult));
3740
3799
  } while (
3741
- // there are tool calls:
3742
- clientToolCalls.length > 0 && // all current tool calls have outputs (incl. execution errors):
3743
- clientToolOutputs.length === clientToolCalls.length && // continue until a stop condition is met:
3800
+ // Continue if:
3801
+ // 1. There are client tool calls that have all been executed, OR
3802
+ // 2. There are pending deferred results from provider-executed tools
3803
+ (clientToolCalls.length > 0 && clientToolOutputs.length === clientToolCalls.length || pendingDeferredToolCalls.size > 0) && // continue until a stop condition is met:
3744
3804
  !await isStopConditionMet({ stopConditions, steps })
3745
3805
  );
3746
3806
  span.setAttributes(
@@ -3941,7 +4001,8 @@ function asContent({
3941
4001
  content,
3942
4002
  toolCalls,
3943
4003
  toolOutputs,
3944
- toolApprovalRequests
4004
+ toolApprovalRequests,
4005
+ tools
3945
4006
  }) {
3946
4007
  return [
3947
4008
  ...content.map((part) => {
@@ -3967,7 +4028,31 @@ function asContent({
3967
4028
  (toolCall2) => toolCall2.toolCallId === part.toolCallId
3968
4029
  );
3969
4030
  if (toolCall == null) {
3970
- throw new Error(`Tool call ${part.toolCallId} not found.`);
4031
+ const tool2 = tools == null ? void 0 : tools[part.toolName];
4032
+ const supportsDeferredResults = (tool2 == null ? void 0 : tool2.type) === "provider" && tool2.supportsDeferredResults;
4033
+ if (!supportsDeferredResults) {
4034
+ throw new Error(`Tool call ${part.toolCallId} not found.`);
4035
+ }
4036
+ if (part.isError) {
4037
+ return {
4038
+ type: "tool-error",
4039
+ toolCallId: part.toolCallId,
4040
+ toolName: part.toolName,
4041
+ input: void 0,
4042
+ error: part.result,
4043
+ providerExecuted: true,
4044
+ dynamic: part.dynamic
4045
+ };
4046
+ }
4047
+ return {
4048
+ type: "tool-result",
4049
+ toolCallId: part.toolCallId,
4050
+ toolName: part.toolName,
4051
+ input: void 0,
4052
+ output: part.result,
4053
+ providerExecuted: true,
4054
+ dynamic: part.dynamic
4055
+ };
3971
4056
  }
3972
4057
  if (part.isError) {
3973
4058
  return {
@@ -4322,39 +4407,6 @@ function isDataUIMessageChunk(chunk) {
4322
4407
  return chunk.type.startsWith("data-");
4323
4408
  }
4324
4409
 
4325
- // src/util/merge-objects.ts
4326
- function mergeObjects(base, overrides) {
4327
- if (base === void 0 && overrides === void 0) {
4328
- return void 0;
4329
- }
4330
- if (base === void 0) {
4331
- return overrides;
4332
- }
4333
- if (overrides === void 0) {
4334
- return base;
4335
- }
4336
- const result = { ...base };
4337
- for (const key in overrides) {
4338
- if (Object.prototype.hasOwnProperty.call(overrides, key)) {
4339
- const overridesValue = overrides[key];
4340
- if (overridesValue === void 0)
4341
- continue;
4342
- const baseValue = key in base ? base[key] : void 0;
4343
- const isSourceObject = overridesValue !== null && typeof overridesValue === "object" && !Array.isArray(overridesValue) && !(overridesValue instanceof Date) && !(overridesValue instanceof RegExp);
4344
- const isTargetObject = baseValue !== null && baseValue !== void 0 && typeof baseValue === "object" && !Array.isArray(baseValue) && !(baseValue instanceof Date) && !(baseValue instanceof RegExp);
4345
- if (isSourceObject && isTargetObject) {
4346
- result[key] = mergeObjects(
4347
- baseValue,
4348
- overridesValue
4349
- );
4350
- } else {
4351
- result[key] = overridesValue;
4352
- }
4353
- }
4354
- }
4355
- return result;
4356
- }
4357
-
4358
4410
  // src/ui/ui-messages.ts
4359
4411
  function isDataUIPart(part) {
4360
4412
  return part.type.startsWith("data-");
@@ -5538,6 +5590,7 @@ var DefaultStreamTextResult = class {
5538
5590
  let recordedRequest = {};
5539
5591
  let recordedWarnings = [];
5540
5592
  const recordedSteps = [];
5593
+ const pendingDeferredToolCalls = /* @__PURE__ */ new Map();
5541
5594
  let rootSpan;
5542
5595
  let activeTextContent = {};
5543
5596
  let activeReasoningContent = {};
@@ -5946,6 +5999,10 @@ var DefaultStreamTextResult = class {
5946
5999
  activeTools: (_e = prepareStepResult == null ? void 0 : prepareStepResult.activeTools) != null ? _e : activeTools
5947
6000
  });
5948
6001
  experimental_context = (_f = prepareStepResult == null ? void 0 : prepareStepResult.experimental_context) != null ? _f : experimental_context;
6002
+ const stepProviderOptions = mergeObjects(
6003
+ providerOptions,
6004
+ prepareStepResult == null ? void 0 : prepareStepResult.providerOptions
6005
+ );
5949
6006
  const {
5950
6007
  result: { stream: stream2, response, request },
5951
6008
  doStreamSpan,
@@ -5999,7 +6056,7 @@ var DefaultStreamTextResult = class {
5999
6056
  toolChoice: stepToolChoice,
6000
6057
  responseFormat: await (output == null ? void 0 : output.responseFormat),
6001
6058
  prompt: promptMessages,
6002
- providerOptions,
6059
+ providerOptions: stepProviderOptions,
6003
6060
  abortSignal,
6004
6061
  headers,
6005
6062
  includeRawChunks: includeRawChunks2
@@ -6247,12 +6304,36 @@ var DefaultStreamTextResult = class {
6247
6304
  const clientToolOutputs = stepToolOutputs.filter(
6248
6305
  (toolOutput) => toolOutput.providerExecuted !== true
6249
6306
  );
6250
- if (clientToolCalls.length > 0 && // all current tool calls have outputs (incl. execution errors):
6251
- clientToolOutputs.length === clientToolCalls.length && // continue until a stop condition is met:
6252
- !await isStopConditionMet({
6253
- stopConditions,
6254
- steps: recordedSteps
6255
- })) {
6307
+ for (const toolCall of stepToolCalls) {
6308
+ if (toolCall.providerExecuted !== true)
6309
+ continue;
6310
+ const tool2 = tools == null ? void 0 : tools[toolCall.toolName];
6311
+ if ((tool2 == null ? void 0 : tool2.type) === "provider" && tool2.supportsDeferredResults) {
6312
+ const hasResultInStep = stepToolOutputs.some(
6313
+ (output2) => output2.type === "tool-result" && output2.toolCallId === toolCall.toolCallId
6314
+ );
6315
+ if (!hasResultInStep) {
6316
+ pendingDeferredToolCalls.set(toolCall.toolCallId, {
6317
+ toolName: toolCall.toolName
6318
+ });
6319
+ }
6320
+ }
6321
+ }
6322
+ for (const output2 of stepToolOutputs) {
6323
+ if (output2.type === "tool-result") {
6324
+ pendingDeferredToolCalls.delete(output2.toolCallId);
6325
+ }
6326
+ }
6327
+ if (
6328
+ // Continue if:
6329
+ // 1. There are client tool calls that have all been executed, OR
6330
+ // 2. There are pending deferred results from provider-executed tools
6331
+ (clientToolCalls.length > 0 && clientToolOutputs.length === clientToolCalls.length || pendingDeferredToolCalls.size > 0) && // continue until a stop condition is met:
6332
+ !await isStopConditionMet({
6333
+ stopConditions,
6334
+ steps: recordedSteps
6335
+ })
6336
+ ) {
6256
6337
  responseMessages.push(
6257
6338
  ...await toResponseMessages({
6258
6339
  content: (