ai 6.0.91 → 6.0.93
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 +13 -0
- package/dist/index.d.mts +357 -13
- package/dist/index.d.ts +357 -13
- package/dist/index.js +198 -26
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +198 -26
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.mjs +1 -1
- package/docs/02-foundations/02-providers-and-models.mdx +1 -0
- package/docs/03-agents/02-building-agents.mdx +8 -7
- package/docs/03-agents/06-memory.mdx +33 -0
- package/docs/03-ai-sdk-core/05-generating-text.mdx +54 -0
- package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +44 -2
- package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +667 -146
- package/package.json +2 -2
- package/src/generate-text/execute-tool-call.ts +83 -0
- package/src/generate-text/generate-text.ts +534 -11
- package/src/generate-text/index.ts +4 -0
- package/src/generate-text/step-result.ts +52 -0
- package/src/generate-text/stream-text.ts +15 -1
package/dist/index.mjs
CHANGED
|
@@ -1104,7 +1104,7 @@ import {
|
|
|
1104
1104
|
} from "@ai-sdk/provider-utils";
|
|
1105
1105
|
|
|
1106
1106
|
// src/version.ts
|
|
1107
|
-
var VERSION = true ? "6.0.
|
|
1107
|
+
var VERSION = true ? "6.0.93" : "0.0.0-test";
|
|
1108
1108
|
|
|
1109
1109
|
// src/util/download/download.ts
|
|
1110
1110
|
var download = async ({
|
|
@@ -2653,6 +2653,14 @@ function collectToolApprovals({
|
|
|
2653
2653
|
|
|
2654
2654
|
// src/generate-text/execute-tool-call.ts
|
|
2655
2655
|
import { executeTool } from "@ai-sdk/provider-utils";
|
|
2656
|
+
|
|
2657
|
+
// src/util/now.ts
|
|
2658
|
+
function now() {
|
|
2659
|
+
var _a21, _b;
|
|
2660
|
+
return (_b = (_a21 = globalThis == null ? void 0 : globalThis.performance) == null ? void 0 : _a21.now()) != null ? _b : Date.now();
|
|
2661
|
+
}
|
|
2662
|
+
|
|
2663
|
+
// src/generate-text/execute-tool-call.ts
|
|
2656
2664
|
async function executeToolCall({
|
|
2657
2665
|
toolCall,
|
|
2658
2666
|
tools,
|
|
@@ -2661,7 +2669,11 @@ async function executeToolCall({
|
|
|
2661
2669
|
messages,
|
|
2662
2670
|
abortSignal,
|
|
2663
2671
|
experimental_context,
|
|
2664
|
-
|
|
2672
|
+
stepNumber,
|
|
2673
|
+
model,
|
|
2674
|
+
onPreliminaryToolResult,
|
|
2675
|
+
onToolCallStart,
|
|
2676
|
+
onToolCallFinish
|
|
2665
2677
|
}) {
|
|
2666
2678
|
const { toolName, toolCallId, input } = toolCall;
|
|
2667
2679
|
const tool2 = tools == null ? void 0 : tools[toolName];
|
|
@@ -2687,6 +2699,20 @@ async function executeToolCall({
|
|
|
2687
2699
|
tracer,
|
|
2688
2700
|
fn: async (span) => {
|
|
2689
2701
|
let output;
|
|
2702
|
+
try {
|
|
2703
|
+
await (onToolCallStart == null ? void 0 : onToolCallStart({
|
|
2704
|
+
stepNumber,
|
|
2705
|
+
model,
|
|
2706
|
+
toolCall,
|
|
2707
|
+
messages,
|
|
2708
|
+
abortSignal,
|
|
2709
|
+
functionId: telemetry == null ? void 0 : telemetry.functionId,
|
|
2710
|
+
metadata: telemetry == null ? void 0 : telemetry.metadata,
|
|
2711
|
+
experimental_context
|
|
2712
|
+
}));
|
|
2713
|
+
} catch (_ignored) {
|
|
2714
|
+
}
|
|
2715
|
+
const startTime = now();
|
|
2690
2716
|
try {
|
|
2691
2717
|
const stream = executeTool({
|
|
2692
2718
|
execute: tool2.execute.bind(tool2),
|
|
@@ -2711,6 +2737,23 @@ async function executeToolCall({
|
|
|
2711
2737
|
}
|
|
2712
2738
|
}
|
|
2713
2739
|
} catch (error) {
|
|
2740
|
+
const durationMs2 = now() - startTime;
|
|
2741
|
+
try {
|
|
2742
|
+
await (onToolCallFinish == null ? void 0 : onToolCallFinish({
|
|
2743
|
+
stepNumber,
|
|
2744
|
+
model,
|
|
2745
|
+
toolCall,
|
|
2746
|
+
messages,
|
|
2747
|
+
abortSignal,
|
|
2748
|
+
success: false,
|
|
2749
|
+
error,
|
|
2750
|
+
durationMs: durationMs2,
|
|
2751
|
+
functionId: telemetry == null ? void 0 : telemetry.functionId,
|
|
2752
|
+
metadata: telemetry == null ? void 0 : telemetry.metadata,
|
|
2753
|
+
experimental_context
|
|
2754
|
+
}));
|
|
2755
|
+
} catch (_ignored) {
|
|
2756
|
+
}
|
|
2714
2757
|
recordErrorOnSpan(span, error);
|
|
2715
2758
|
return {
|
|
2716
2759
|
type: "tool-error",
|
|
@@ -2722,6 +2765,23 @@ async function executeToolCall({
|
|
|
2722
2765
|
...toolCall.providerMetadata != null ? { providerMetadata: toolCall.providerMetadata } : {}
|
|
2723
2766
|
};
|
|
2724
2767
|
}
|
|
2768
|
+
const durationMs = now() - startTime;
|
|
2769
|
+
try {
|
|
2770
|
+
await (onToolCallFinish == null ? void 0 : onToolCallFinish({
|
|
2771
|
+
stepNumber,
|
|
2772
|
+
model,
|
|
2773
|
+
toolCall,
|
|
2774
|
+
messages,
|
|
2775
|
+
abortSignal,
|
|
2776
|
+
success: true,
|
|
2777
|
+
output,
|
|
2778
|
+
durationMs,
|
|
2779
|
+
functionId: telemetry == null ? void 0 : telemetry.functionId,
|
|
2780
|
+
metadata: telemetry == null ? void 0 : telemetry.metadata,
|
|
2781
|
+
experimental_context
|
|
2782
|
+
}));
|
|
2783
|
+
} catch (_ignored) {
|
|
2784
|
+
}
|
|
2725
2785
|
try {
|
|
2726
2786
|
span.setAttributes(
|
|
2727
2787
|
await selectTelemetryAttributes({
|
|
@@ -3626,6 +3686,11 @@ async function doParseToolCall({
|
|
|
3626
3686
|
// src/generate-text/step-result.ts
|
|
3627
3687
|
var DefaultStepResult = class {
|
|
3628
3688
|
constructor({
|
|
3689
|
+
stepNumber,
|
|
3690
|
+
model,
|
|
3691
|
+
functionId,
|
|
3692
|
+
metadata,
|
|
3693
|
+
experimental_context,
|
|
3629
3694
|
content,
|
|
3630
3695
|
finishReason,
|
|
3631
3696
|
rawFinishReason,
|
|
@@ -3635,6 +3700,11 @@ var DefaultStepResult = class {
|
|
|
3635
3700
|
response,
|
|
3636
3701
|
providerMetadata
|
|
3637
3702
|
}) {
|
|
3703
|
+
this.stepNumber = stepNumber;
|
|
3704
|
+
this.model = model;
|
|
3705
|
+
this.functionId = functionId;
|
|
3706
|
+
this.metadata = metadata;
|
|
3707
|
+
this.experimental_context = experimental_context;
|
|
3638
3708
|
this.content = content;
|
|
3639
3709
|
this.finishReason = finishReason;
|
|
3640
3710
|
this.rawFinishReason = rawFinishReason;
|
|
@@ -3892,6 +3962,10 @@ async function generateText({
|
|
|
3892
3962
|
experimental_context,
|
|
3893
3963
|
experimental_include: include,
|
|
3894
3964
|
_internal: { generateId: generateId2 = originalGenerateId } = {},
|
|
3965
|
+
experimental_onStart: onStart,
|
|
3966
|
+
experimental_onStepStart: onStepStart,
|
|
3967
|
+
experimental_onToolCallStart: onToolCallStart,
|
|
3968
|
+
experimental_onToolCallFinish: onToolCallFinish,
|
|
3895
3969
|
onStepFinish,
|
|
3896
3970
|
onFinish,
|
|
3897
3971
|
...settings
|
|
@@ -3926,6 +4000,37 @@ async function generateText({
|
|
|
3926
4000
|
prompt,
|
|
3927
4001
|
messages
|
|
3928
4002
|
});
|
|
4003
|
+
try {
|
|
4004
|
+
await (onStart == null ? void 0 : onStart({
|
|
4005
|
+
model: { provider: model.provider, modelId: model.modelId },
|
|
4006
|
+
system,
|
|
4007
|
+
prompt,
|
|
4008
|
+
messages,
|
|
4009
|
+
tools,
|
|
4010
|
+
toolChoice,
|
|
4011
|
+
activeTools,
|
|
4012
|
+
maxOutputTokens: callSettings.maxOutputTokens,
|
|
4013
|
+
temperature: callSettings.temperature,
|
|
4014
|
+
topP: callSettings.topP,
|
|
4015
|
+
topK: callSettings.topK,
|
|
4016
|
+
presencePenalty: callSettings.presencePenalty,
|
|
4017
|
+
frequencyPenalty: callSettings.frequencyPenalty,
|
|
4018
|
+
stopSequences: callSettings.stopSequences,
|
|
4019
|
+
seed: callSettings.seed,
|
|
4020
|
+
maxRetries,
|
|
4021
|
+
timeout,
|
|
4022
|
+
headers,
|
|
4023
|
+
providerOptions,
|
|
4024
|
+
stopWhen,
|
|
4025
|
+
output,
|
|
4026
|
+
abortSignal,
|
|
4027
|
+
include,
|
|
4028
|
+
functionId: telemetry == null ? void 0 : telemetry.functionId,
|
|
4029
|
+
metadata: telemetry == null ? void 0 : telemetry.metadata,
|
|
4030
|
+
experimental_context
|
|
4031
|
+
}));
|
|
4032
|
+
} catch (_ignored) {
|
|
4033
|
+
}
|
|
3929
4034
|
const tracer = getTracer(telemetry);
|
|
3930
4035
|
try {
|
|
3931
4036
|
return await recordSpan({
|
|
@@ -3949,7 +4054,7 @@ async function generateText({
|
|
|
3949
4054
|
}),
|
|
3950
4055
|
tracer,
|
|
3951
4056
|
fn: async (span) => {
|
|
3952
|
-
var _a21, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
4057
|
+
var _a21, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
3953
4058
|
const initialMessages = initialPrompt.messages;
|
|
3954
4059
|
const responseMessages = [];
|
|
3955
4060
|
const { approvedToolApprovals, deniedToolApprovals } = collectToolApprovals({ messages: initialMessages });
|
|
@@ -3966,7 +4071,11 @@ async function generateText({
|
|
|
3966
4071
|
telemetry,
|
|
3967
4072
|
messages: initialMessages,
|
|
3968
4073
|
abortSignal: mergedAbortSignal,
|
|
3969
|
-
experimental_context
|
|
4074
|
+
experimental_context,
|
|
4075
|
+
stepNumber: 0,
|
|
4076
|
+
model: { provider: model.provider, modelId: model.modelId },
|
|
4077
|
+
onToolCallStart,
|
|
4078
|
+
onToolCallFinish
|
|
3970
4079
|
});
|
|
3971
4080
|
const toolContent = [];
|
|
3972
4081
|
for (const output2 of toolOutputs) {
|
|
@@ -4055,11 +4164,44 @@ async function generateText({
|
|
|
4055
4164
|
download: download2
|
|
4056
4165
|
});
|
|
4057
4166
|
experimental_context = (_d = prepareStepResult == null ? void 0 : prepareStepResult.experimental_context) != null ? _d : experimental_context;
|
|
4167
|
+
const stepActiveTools = (_e = prepareStepResult == null ? void 0 : prepareStepResult.activeTools) != null ? _e : activeTools;
|
|
4058
4168
|
const { toolChoice: stepToolChoice, tools: stepTools } = await prepareToolsAndToolChoice({
|
|
4059
4169
|
tools,
|
|
4060
|
-
toolChoice: (
|
|
4061
|
-
activeTools:
|
|
4170
|
+
toolChoice: (_f = prepareStepResult == null ? void 0 : prepareStepResult.toolChoice) != null ? _f : toolChoice,
|
|
4171
|
+
activeTools: stepActiveTools
|
|
4062
4172
|
});
|
|
4173
|
+
const stepMessages = (_g = prepareStepResult == null ? void 0 : prepareStepResult.messages) != null ? _g : stepInputMessages;
|
|
4174
|
+
const stepSystem = (_h = prepareStepResult == null ? void 0 : prepareStepResult.system) != null ? _h : initialPrompt.system;
|
|
4175
|
+
const stepProviderOptions = mergeObjects(
|
|
4176
|
+
providerOptions,
|
|
4177
|
+
prepareStepResult == null ? void 0 : prepareStepResult.providerOptions
|
|
4178
|
+
);
|
|
4179
|
+
try {
|
|
4180
|
+
await (onStepStart == null ? void 0 : onStepStart({
|
|
4181
|
+
stepNumber: steps.length,
|
|
4182
|
+
model: {
|
|
4183
|
+
provider: stepModel.provider,
|
|
4184
|
+
modelId: stepModel.modelId
|
|
4185
|
+
},
|
|
4186
|
+
system: stepSystem,
|
|
4187
|
+
messages: stepMessages,
|
|
4188
|
+
tools,
|
|
4189
|
+
toolChoice: stepToolChoice,
|
|
4190
|
+
activeTools: stepActiveTools,
|
|
4191
|
+
steps: [...steps],
|
|
4192
|
+
providerOptions: stepProviderOptions,
|
|
4193
|
+
timeout,
|
|
4194
|
+
headers,
|
|
4195
|
+
stopWhen,
|
|
4196
|
+
output,
|
|
4197
|
+
abortSignal,
|
|
4198
|
+
include,
|
|
4199
|
+
functionId: telemetry == null ? void 0 : telemetry.functionId,
|
|
4200
|
+
metadata: telemetry == null ? void 0 : telemetry.metadata,
|
|
4201
|
+
experimental_context
|
|
4202
|
+
}));
|
|
4203
|
+
} catch (_ignored) {
|
|
4204
|
+
}
|
|
4063
4205
|
currentModelResponse = await retry(
|
|
4064
4206
|
() => {
|
|
4065
4207
|
var _a22;
|
|
@@ -4102,10 +4244,6 @@ async function generateText({
|
|
|
4102
4244
|
tracer,
|
|
4103
4245
|
fn: async (span2) => {
|
|
4104
4246
|
var _a23, _b2, _c2, _d2, _e2, _f2, _g2, _h2;
|
|
4105
|
-
const stepProviderOptions = mergeObjects(
|
|
4106
|
-
providerOptions,
|
|
4107
|
-
prepareStepResult == null ? void 0 : prepareStepResult.providerOptions
|
|
4108
|
-
);
|
|
4109
4247
|
const result = await stepModel.doGenerate({
|
|
4110
4248
|
...callSettings2,
|
|
4111
4249
|
tools: stepTools,
|
|
@@ -4237,7 +4375,14 @@ async function generateText({
|
|
|
4237
4375
|
telemetry,
|
|
4238
4376
|
messages: stepInputMessages,
|
|
4239
4377
|
abortSignal: mergedAbortSignal,
|
|
4240
|
-
experimental_context
|
|
4378
|
+
experimental_context,
|
|
4379
|
+
stepNumber: steps.length,
|
|
4380
|
+
model: {
|
|
4381
|
+
provider: stepModel.provider,
|
|
4382
|
+
modelId: stepModel.modelId
|
|
4383
|
+
},
|
|
4384
|
+
onToolCallStart,
|
|
4385
|
+
onToolCallFinish
|
|
4241
4386
|
})
|
|
4242
4387
|
);
|
|
4243
4388
|
}
|
|
@@ -4274,15 +4419,24 @@ async function generateText({
|
|
|
4274
4419
|
tools
|
|
4275
4420
|
})
|
|
4276
4421
|
);
|
|
4277
|
-
const stepRequest = ((
|
|
4422
|
+
const stepRequest = ((_i = include == null ? void 0 : include.requestBody) != null ? _i : true) ? (_j = currentModelResponse.request) != null ? _j : {} : { ...currentModelResponse.request, body: void 0 };
|
|
4278
4423
|
const stepResponse = {
|
|
4279
4424
|
...currentModelResponse.response,
|
|
4280
4425
|
// deep clone msgs to avoid mutating past messages in multi-step:
|
|
4281
4426
|
messages: structuredClone(responseMessages),
|
|
4282
4427
|
// Conditionally include response body:
|
|
4283
|
-
body: ((
|
|
4428
|
+
body: ((_k = include == null ? void 0 : include.responseBody) != null ? _k : true) ? (_l = currentModelResponse.response) == null ? void 0 : _l.body : void 0
|
|
4284
4429
|
};
|
|
4430
|
+
const stepNumber = steps.length;
|
|
4285
4431
|
const currentStepResult = new DefaultStepResult({
|
|
4432
|
+
stepNumber,
|
|
4433
|
+
model: {
|
|
4434
|
+
provider: stepModel.provider,
|
|
4435
|
+
modelId: stepModel.modelId
|
|
4436
|
+
},
|
|
4437
|
+
functionId: telemetry == null ? void 0 : telemetry.functionId,
|
|
4438
|
+
metadata: telemetry == null ? void 0 : telemetry.metadata,
|
|
4439
|
+
experimental_context,
|
|
4286
4440
|
content: stepContent,
|
|
4287
4441
|
finishReason: currentModelResponse.finishReason.unified,
|
|
4288
4442
|
rawFinishReason: currentModelResponse.finishReason.raw,
|
|
@@ -4293,7 +4447,7 @@ async function generateText({
|
|
|
4293
4447
|
response: stepResponse
|
|
4294
4448
|
});
|
|
4295
4449
|
logWarnings({
|
|
4296
|
-
warnings: (
|
|
4450
|
+
warnings: (_m = currentModelResponse.warnings) != null ? _m : [],
|
|
4297
4451
|
provider: stepModel.provider,
|
|
4298
4452
|
model: stepModel.modelId
|
|
4299
4453
|
});
|
|
@@ -4351,6 +4505,11 @@ async function generateText({
|
|
|
4351
4505
|
}
|
|
4352
4506
|
);
|
|
4353
4507
|
await (onFinish == null ? void 0 : onFinish({
|
|
4508
|
+
stepNumber: lastStep.stepNumber,
|
|
4509
|
+
model: lastStep.model,
|
|
4510
|
+
functionId: lastStep.functionId,
|
|
4511
|
+
metadata: lastStep.metadata,
|
|
4512
|
+
experimental_context: lastStep.experimental_context,
|
|
4354
4513
|
finishReason: lastStep.finishReason,
|
|
4355
4514
|
rawFinishReason: lastStep.rawFinishReason,
|
|
4356
4515
|
usage: lastStep.usage,
|
|
@@ -4371,8 +4530,7 @@ async function generateText({
|
|
|
4371
4530
|
warnings: lastStep.warnings,
|
|
4372
4531
|
providerMetadata: lastStep.providerMetadata,
|
|
4373
4532
|
steps,
|
|
4374
|
-
totalUsage
|
|
4375
|
-
experimental_context
|
|
4533
|
+
totalUsage
|
|
4376
4534
|
}));
|
|
4377
4535
|
let resolvedOutput;
|
|
4378
4536
|
if (lastStep.finishReason === "stop") {
|
|
@@ -4404,7 +4562,11 @@ async function executeTools({
|
|
|
4404
4562
|
telemetry,
|
|
4405
4563
|
messages,
|
|
4406
4564
|
abortSignal,
|
|
4407
|
-
experimental_context
|
|
4565
|
+
experimental_context,
|
|
4566
|
+
stepNumber,
|
|
4567
|
+
model,
|
|
4568
|
+
onToolCallStart,
|
|
4569
|
+
onToolCallFinish
|
|
4408
4570
|
}) {
|
|
4409
4571
|
const toolOutputs = await Promise.all(
|
|
4410
4572
|
toolCalls.map(
|
|
@@ -4415,7 +4577,11 @@ async function executeTools({
|
|
|
4415
4577
|
telemetry,
|
|
4416
4578
|
messages,
|
|
4417
4579
|
abortSignal,
|
|
4418
|
-
experimental_context
|
|
4580
|
+
experimental_context,
|
|
4581
|
+
stepNumber,
|
|
4582
|
+
model,
|
|
4583
|
+
onToolCallStart,
|
|
4584
|
+
onToolCallFinish
|
|
4419
4585
|
})
|
|
4420
4586
|
)
|
|
4421
4587
|
);
|
|
@@ -5806,12 +5972,6 @@ function createStitchableStream() {
|
|
|
5806
5972
|
};
|
|
5807
5973
|
}
|
|
5808
5974
|
|
|
5809
|
-
// src/util/now.ts
|
|
5810
|
-
function now() {
|
|
5811
|
-
var _a21, _b;
|
|
5812
|
-
return (_b = (_a21 = globalThis == null ? void 0 : globalThis.performance) == null ? void 0 : _a21.now()) != null ? _b : Date.now();
|
|
5813
|
-
}
|
|
5814
|
-
|
|
5815
5975
|
// src/generate-text/run-tools-transformation.ts
|
|
5816
5976
|
import {
|
|
5817
5977
|
getErrorMessage as getErrorMessage6
|
|
@@ -6373,6 +6533,14 @@ var DefaultStreamTextResult = class {
|
|
|
6373
6533
|
tools
|
|
6374
6534
|
});
|
|
6375
6535
|
const currentStepResult = new DefaultStepResult({
|
|
6536
|
+
stepNumber: recordedSteps.length,
|
|
6537
|
+
model: {
|
|
6538
|
+
provider: model.provider,
|
|
6539
|
+
modelId: model.modelId
|
|
6540
|
+
},
|
|
6541
|
+
functionId: telemetry == null ? void 0 : telemetry.functionId,
|
|
6542
|
+
metadata: telemetry == null ? void 0 : telemetry.metadata,
|
|
6543
|
+
experimental_context,
|
|
6376
6544
|
content: recordedContent,
|
|
6377
6545
|
finishReason: part.finishReason,
|
|
6378
6546
|
rawFinishReason: part.rawFinishReason,
|
|
@@ -6421,6 +6589,11 @@ var DefaultStreamTextResult = class {
|
|
|
6421
6589
|
self._steps.resolve(recordedSteps);
|
|
6422
6590
|
const finalStep = recordedSteps[recordedSteps.length - 1];
|
|
6423
6591
|
await (onFinish == null ? void 0 : onFinish({
|
|
6592
|
+
stepNumber: finalStep.stepNumber,
|
|
6593
|
+
model: finalStep.model,
|
|
6594
|
+
functionId: finalStep.functionId,
|
|
6595
|
+
metadata: finalStep.metadata,
|
|
6596
|
+
experimental_context: finalStep.experimental_context,
|
|
6424
6597
|
finishReason: finalStep.finishReason,
|
|
6425
6598
|
rawFinishReason: finalStep.rawFinishReason,
|
|
6426
6599
|
totalUsage,
|
|
@@ -6441,8 +6614,7 @@ var DefaultStreamTextResult = class {
|
|
|
6441
6614
|
response: finalStep.response,
|
|
6442
6615
|
warnings: finalStep.warnings,
|
|
6443
6616
|
providerMetadata: finalStep.providerMetadata,
|
|
6444
|
-
steps: recordedSteps
|
|
6445
|
-
experimental_context
|
|
6617
|
+
steps: recordedSteps
|
|
6446
6618
|
}));
|
|
6447
6619
|
rootSpan.setAttributes(
|
|
6448
6620
|
await selectTelemetryAttributes({
|