ai 3.4.16 → 3.4.18
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 +12 -0
- package/dist/index.d.mts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +96 -133
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +96 -133
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/rsc/dist/index.d.ts +3 -2
- package/rsc/dist/rsc-server.d.mts +3 -2
- package/rsc/dist/rsc-server.mjs +17 -31
- package/rsc/dist/rsc-server.mjs.map +1 -1
package/dist/index.mjs
CHANGED
@@ -288,6 +288,9 @@ function selectTelemetryAttributes({
|
|
288
288
|
telemetry,
|
289
289
|
attributes
|
290
290
|
}) {
|
291
|
+
if ((telemetry == null ? void 0 : telemetry.isEnabled) !== true) {
|
292
|
+
return {};
|
293
|
+
}
|
291
294
|
return Object.entries(attributes).reduce((attributes2, [key, value]) => {
|
292
295
|
if (value === void 0) {
|
293
296
|
return attributes2;
|
@@ -843,33 +846,16 @@ async function convertToLanguageModelPrompt({
|
|
843
846
|
modelSupportsImageUrls = true,
|
844
847
|
downloadImplementation = download
|
845
848
|
}) {
|
849
|
+
const downloadedAssets = modelSupportsImageUrls || prompt.messages == null ? null : await downloadAssets(prompt.messages, downloadImplementation);
|
846
850
|
const languageModelMessages = [];
|
847
851
|
if (prompt.system != null) {
|
848
852
|
languageModelMessages.push({ role: "system", content: prompt.system });
|
849
853
|
}
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
role: "user",
|
856
|
-
content: [{ type: "text", text: prompt.prompt }]
|
857
|
-
});
|
858
|
-
break;
|
859
|
-
}
|
860
|
-
case "messages": {
|
861
|
-
languageModelMessages.push(
|
862
|
-
...prompt.messages.map(
|
863
|
-
(message) => convertToLanguageModelMessage(message, downloadedAssets)
|
864
|
-
)
|
865
|
-
);
|
866
|
-
break;
|
867
|
-
}
|
868
|
-
default: {
|
869
|
-
const _exhaustiveCheck = promptType;
|
870
|
-
throw new Error(`Unsupported prompt type: ${_exhaustiveCheck}`);
|
871
|
-
}
|
872
|
-
}
|
854
|
+
languageModelMessages.push(
|
855
|
+
...prompt.messages.map(
|
856
|
+
(message) => convertToLanguageModelMessage(message, downloadedAssets)
|
857
|
+
)
|
858
|
+
);
|
873
859
|
return languageModelMessages;
|
874
860
|
}
|
875
861
|
function convertToLanguageModelMessage(message, downloadedAssets) {
|
@@ -1283,7 +1269,7 @@ function prepareCallSettings({
|
|
1283
1269
|
};
|
1284
1270
|
}
|
1285
1271
|
|
1286
|
-
// core/prompt/
|
1272
|
+
// core/prompt/standardize-prompt.ts
|
1287
1273
|
import { InvalidPromptError } from "@ai-sdk/provider";
|
1288
1274
|
import { safeValidateTypes } from "@ai-sdk/provider-utils";
|
1289
1275
|
import { z as z6 } from "zod";
|
@@ -1381,8 +1367,8 @@ var coreMessageSchema = z5.union([
|
|
1381
1367
|
coreToolMessageSchema
|
1382
1368
|
]);
|
1383
1369
|
|
1384
|
-
// core/prompt/
|
1385
|
-
function
|
1370
|
+
// core/prompt/standardize-prompt.ts
|
1371
|
+
function standardizePrompt(prompt) {
|
1386
1372
|
if (prompt.prompt == null && prompt.messages == null) {
|
1387
1373
|
throw new InvalidPromptError({
|
1388
1374
|
prompt,
|
@@ -1410,9 +1396,13 @@ function validatePrompt(prompt) {
|
|
1410
1396
|
}
|
1411
1397
|
return {
|
1412
1398
|
type: "prompt",
|
1413
|
-
|
1414
|
-
messages:
|
1415
|
-
|
1399
|
+
system: prompt.system,
|
1400
|
+
messages: [
|
1401
|
+
{
|
1402
|
+
role: "user",
|
1403
|
+
content: prompt.prompt
|
1404
|
+
}
|
1405
|
+
]
|
1416
1406
|
};
|
1417
1407
|
}
|
1418
1408
|
if (prompt.messages != null) {
|
@@ -1429,7 +1419,6 @@ function validatePrompt(prompt) {
|
|
1429
1419
|
}
|
1430
1420
|
return {
|
1431
1421
|
type: "messages",
|
1432
|
-
prompt: void 0,
|
1433
1422
|
messages: prompt.messages,
|
1434
1423
|
// only possible case bc of checks above
|
1435
1424
|
system: prompt.system
|
@@ -1968,7 +1957,7 @@ async function generateObject({
|
|
1968
1957
|
let resultProviderMetadata;
|
1969
1958
|
switch (mode) {
|
1970
1959
|
case "json": {
|
1971
|
-
const
|
1960
|
+
const standardPrompt = standardizePrompt({
|
1972
1961
|
system: outputStrategy.jsonSchema == null ? injectJsonInstruction({ prompt: system }) : model.supportsStructuredOutputs ? system : injectJsonInstruction({
|
1973
1962
|
prompt: system,
|
1974
1963
|
schema: outputStrategy.jsonSchema
|
@@ -1977,10 +1966,9 @@ async function generateObject({
|
|
1977
1966
|
messages
|
1978
1967
|
});
|
1979
1968
|
const promptMessages = await convertToLanguageModelPrompt({
|
1980
|
-
prompt:
|
1969
|
+
prompt: standardPrompt,
|
1981
1970
|
modelSupportsImageUrls: model.supportsImageUrls
|
1982
1971
|
});
|
1983
|
-
const inputFormat = validatedPrompt.type;
|
1984
1972
|
const generateResult = await retry(
|
1985
1973
|
() => recordSpan({
|
1986
1974
|
name: "ai.generateObject.doGenerate",
|
@@ -1993,7 +1981,7 @@ async function generateObject({
|
|
1993
1981
|
}),
|
1994
1982
|
...baseTelemetryAttributes,
|
1995
1983
|
"ai.prompt.format": {
|
1996
|
-
input: () =>
|
1984
|
+
input: () => standardPrompt.type
|
1997
1985
|
},
|
1998
1986
|
"ai.prompt.messages": {
|
1999
1987
|
input: () => JSON.stringify(promptMessages)
|
@@ -2021,7 +2009,7 @@ async function generateObject({
|
|
2021
2009
|
description: schemaDescription
|
2022
2010
|
},
|
2023
2011
|
...prepareCallSettings(settings),
|
2024
|
-
inputFormat,
|
2012
|
+
inputFormat: standardPrompt.type,
|
2025
2013
|
prompt: promptMessages,
|
2026
2014
|
providerMetadata,
|
2027
2015
|
abortSignal,
|
@@ -2073,7 +2061,7 @@ async function generateObject({
|
|
2073
2061
|
break;
|
2074
2062
|
}
|
2075
2063
|
case "tool": {
|
2076
|
-
const validatedPrompt =
|
2064
|
+
const validatedPrompt = standardizePrompt({
|
2077
2065
|
system,
|
2078
2066
|
prompt,
|
2079
2067
|
messages
|
@@ -2444,7 +2432,7 @@ async function streamObject({
|
|
2444
2432
|
let transformer;
|
2445
2433
|
switch (mode) {
|
2446
2434
|
case "json": {
|
2447
|
-
const
|
2435
|
+
const standardPrompt = standardizePrompt({
|
2448
2436
|
system: outputStrategy.jsonSchema == null ? injectJsonInstruction({ prompt: system }) : model.supportsStructuredOutputs ? system : injectJsonInstruction({
|
2449
2437
|
prompt: system,
|
2450
2438
|
schema: outputStrategy.jsonSchema
|
@@ -2460,9 +2448,9 @@ async function streamObject({
|
|
2460
2448
|
description: schemaDescription
|
2461
2449
|
},
|
2462
2450
|
...prepareCallSettings(settings),
|
2463
|
-
inputFormat:
|
2451
|
+
inputFormat: standardPrompt.type,
|
2464
2452
|
prompt: await convertToLanguageModelPrompt({
|
2465
|
-
prompt:
|
2453
|
+
prompt: standardPrompt,
|
2466
2454
|
modelSupportsImageUrls: model.supportsImageUrls
|
2467
2455
|
}),
|
2468
2456
|
providerMetadata,
|
@@ -2486,7 +2474,7 @@ async function streamObject({
|
|
2486
2474
|
break;
|
2487
2475
|
}
|
2488
2476
|
case "tool": {
|
2489
|
-
const validatedPrompt =
|
2477
|
+
const validatedPrompt = standardizePrompt({
|
2490
2478
|
system,
|
2491
2479
|
prompt,
|
2492
2480
|
messages
|
@@ -3166,20 +3154,12 @@ async function generateText({
|
|
3166
3154
|
fn: async (span) => {
|
3167
3155
|
var _a11, _b, _c, _d, _e;
|
3168
3156
|
const retry = retryWithExponentialBackoff({ maxRetries });
|
3169
|
-
const
|
3170
|
-
system,
|
3171
|
-
prompt,
|
3172
|
-
messages
|
3173
|
-
});
|
3157
|
+
const currentPrompt = standardizePrompt({ system, prompt, messages });
|
3174
3158
|
const mode = {
|
3175
3159
|
type: "regular",
|
3176
3160
|
...prepareToolsAndToolChoice({ tools, toolChoice, activeTools })
|
3177
3161
|
};
|
3178
3162
|
const callSettings = prepareCallSettings(settings);
|
3179
|
-
const promptMessages = await convertToLanguageModelPrompt({
|
3180
|
-
prompt: validatedPrompt,
|
3181
|
-
modelSupportsImageUrls: model.supportsImageUrls
|
3182
|
-
});
|
3183
3163
|
let currentModelResponse;
|
3184
3164
|
let currentToolCalls = [];
|
3185
3165
|
let currentToolResults = [];
|
@@ -3194,7 +3174,13 @@ async function generateText({
|
|
3194
3174
|
};
|
3195
3175
|
let stepType = "initial";
|
3196
3176
|
do {
|
3197
|
-
|
3177
|
+
if (stepCount === 1) {
|
3178
|
+
currentPrompt.type = "messages";
|
3179
|
+
}
|
3180
|
+
const promptMessages = await convertToLanguageModelPrompt({
|
3181
|
+
prompt: currentPrompt,
|
3182
|
+
modelSupportsImageUrls: model.supportsImageUrls
|
3183
|
+
});
|
3198
3184
|
currentModelResponse = await retry(
|
3199
3185
|
() => recordSpan({
|
3200
3186
|
name: "ai.generateText.doGenerate",
|
@@ -3206,7 +3192,7 @@ async function generateText({
|
|
3206
3192
|
telemetry
|
3207
3193
|
}),
|
3208
3194
|
...baseTelemetryAttributes,
|
3209
|
-
"ai.prompt.format": { input: () =>
|
3195
|
+
"ai.prompt.format": { input: () => currentPrompt.type },
|
3210
3196
|
"ai.prompt.messages": {
|
3211
3197
|
input: () => JSON.stringify(promptMessages)
|
3212
3198
|
},
|
@@ -3228,7 +3214,7 @@ async function generateText({
|
|
3228
3214
|
const result = await model.doGenerate({
|
3229
3215
|
mode,
|
3230
3216
|
...callSettings,
|
3231
|
-
inputFormat:
|
3217
|
+
inputFormat: currentPrompt.type,
|
3232
3218
|
prompt: promptMessages,
|
3233
3219
|
providerMetadata,
|
3234
3220
|
abortSignal,
|
@@ -3326,44 +3312,25 @@ async function generateText({
|
|
3326
3312
|
steps.push(currentStep);
|
3327
3313
|
await (onStepFinish == null ? void 0 : onStepFinish(currentStep));
|
3328
3314
|
if (stepType === "continue") {
|
3329
|
-
const
|
3330
|
-
|
3331
|
-
|
3332
|
-
lastResponseMessage.content = text;
|
3315
|
+
const lastMessage = currentPrompt.messages[currentPrompt.messages.length - 1];
|
3316
|
+
if (typeof lastMessage.content === "string") {
|
3317
|
+
lastMessage.content = text;
|
3333
3318
|
} else {
|
3334
|
-
|
3319
|
+
lastMessage.content.push({
|
3335
3320
|
text: stepText,
|
3336
3321
|
type: "text"
|
3337
3322
|
});
|
3338
3323
|
}
|
3339
|
-
responseMessages.
|
3340
|
-
|
3341
|
-
convertToLanguageModelMessage(lastResponseMessage, null)
|
3342
|
-
);
|
3343
|
-
} else if (nextStepType === "continue") {
|
3344
|
-
const newResponseMessages = toResponseMessages({
|
3345
|
-
text,
|
3346
|
-
toolCalls: currentToolCalls,
|
3347
|
-
toolResults: currentToolResults
|
3348
|
-
});
|
3349
|
-
responseMessages.push(...newResponseMessages);
|
3350
|
-
promptMessages.push(
|
3351
|
-
...newResponseMessages.map(
|
3352
|
-
(message) => convertToLanguageModelMessage(message, null)
|
3353
|
-
)
|
3354
|
-
);
|
3324
|
+
responseMessages[responseMessages.length - 1] = lastMessage;
|
3325
|
+
currentPrompt.messages[currentPrompt.messages.length - 1] = lastMessage;
|
3355
3326
|
} else {
|
3356
3327
|
const newResponseMessages = toResponseMessages({
|
3357
|
-
text
|
3328
|
+
text,
|
3358
3329
|
toolCalls: currentToolCalls,
|
3359
3330
|
toolResults: currentToolResults
|
3360
3331
|
});
|
3361
3332
|
responseMessages.push(...newResponseMessages);
|
3362
|
-
|
3363
|
-
...newResponseMessages.map(
|
3364
|
-
(message) => convertToLanguageModelMessage(message, null)
|
3365
|
-
)
|
3366
|
-
);
|
3333
|
+
currentPrompt.messages.push(...newResponseMessages);
|
3367
3334
|
}
|
3368
3335
|
stepType = nextStepType;
|
3369
3336
|
} while (stepType !== "done");
|
@@ -3896,9 +3863,12 @@ async function streamText({
|
|
3896
3863
|
fn: async (rootSpan) => {
|
3897
3864
|
const retry = retryWithExponentialBackoff({ maxRetries });
|
3898
3865
|
const startStep = async ({
|
3899
|
-
|
3900
|
-
promptType
|
3866
|
+
currentPrompt: currentPrompt2
|
3901
3867
|
}) => {
|
3868
|
+
const promptMessages = await convertToLanguageModelPrompt({
|
3869
|
+
prompt: currentPrompt2,
|
3870
|
+
modelSupportsImageUrls: model.supportsImageUrls
|
3871
|
+
});
|
3902
3872
|
const {
|
3903
3873
|
result: { stream: stream2, warnings: warnings2, rawResponse: rawResponse2 },
|
3904
3874
|
doStreamSpan: doStreamSpan2,
|
@@ -3915,10 +3885,10 @@ async function streamText({
|
|
3915
3885
|
}),
|
3916
3886
|
...baseTelemetryAttributes,
|
3917
3887
|
"ai.prompt.format": {
|
3918
|
-
input: () =>
|
3888
|
+
input: () => currentPrompt2.type
|
3919
3889
|
},
|
3920
3890
|
"ai.prompt.messages": {
|
3921
|
-
input: () => JSON.stringify(
|
3891
|
+
input: () => JSON.stringify(promptMessages)
|
3922
3892
|
},
|
3923
3893
|
// standardized gen-ai llm span attributes:
|
3924
3894
|
"gen_ai.system": model.provider,
|
@@ -3948,8 +3918,8 @@ async function streamText({
|
|
3948
3918
|
})
|
3949
3919
|
},
|
3950
3920
|
...prepareCallSettings(settings),
|
3951
|
-
inputFormat:
|
3952
|
-
prompt:
|
3921
|
+
inputFormat: currentPrompt2.type,
|
3922
|
+
prompt: promptMessages,
|
3953
3923
|
providerMetadata,
|
3954
3924
|
abortSignal,
|
3955
3925
|
headers
|
@@ -3974,18 +3944,12 @@ async function streamText({
|
|
3974
3944
|
startTimestampMs: startTimestampMs2
|
3975
3945
|
};
|
3976
3946
|
};
|
3977
|
-
const
|
3978
|
-
prompt: validatePrompt({ system, prompt, messages }),
|
3979
|
-
modelSupportsImageUrls: model.supportsImageUrls
|
3980
|
-
});
|
3947
|
+
const currentPrompt = standardizePrompt({ system, prompt, messages });
|
3981
3948
|
const {
|
3982
3949
|
result: { stream, warnings, rawResponse },
|
3983
3950
|
doStreamSpan,
|
3984
3951
|
startTimestampMs
|
3985
|
-
} = await startStep({
|
3986
|
-
promptType: validatePrompt({ system, prompt, messages }).type,
|
3987
|
-
promptMessages
|
3988
|
-
});
|
3952
|
+
} = await startStep({ currentPrompt });
|
3989
3953
|
return new DefaultStreamTextResult({
|
3990
3954
|
stream,
|
3991
3955
|
warnings,
|
@@ -4000,7 +3964,7 @@ async function streamText({
|
|
4000
3964
|
maxSteps,
|
4001
3965
|
continueSteps,
|
4002
3966
|
startStep,
|
4003
|
-
|
3967
|
+
currentPrompt,
|
4004
3968
|
modelId: model.modelId,
|
4005
3969
|
now: now2,
|
4006
3970
|
currentDate,
|
@@ -4024,7 +3988,7 @@ var DefaultStreamTextResult = class {
|
|
4024
3988
|
maxSteps,
|
4025
3989
|
continueSteps,
|
4026
3990
|
startStep,
|
4027
|
-
|
3991
|
+
currentPrompt,
|
4028
3992
|
modelId,
|
4029
3993
|
now: now2,
|
4030
3994
|
currentDate,
|
@@ -4069,7 +4033,7 @@ var DefaultStreamTextResult = class {
|
|
4069
4033
|
startTimestamp,
|
4070
4034
|
doStreamSpan: doStreamSpan2,
|
4071
4035
|
currentStep,
|
4072
|
-
|
4036
|
+
currentPrompt: currentPrompt2,
|
4073
4037
|
usage = {
|
4074
4038
|
promptTokens: 0,
|
4075
4039
|
completionTokens: 0,
|
@@ -4294,29 +4258,34 @@ var DefaultStreamTextResult = class {
|
|
4294
4258
|
};
|
4295
4259
|
if (nextStepType !== "done") {
|
4296
4260
|
if (stepType === "continue") {
|
4297
|
-
const
|
4298
|
-
|
4261
|
+
const lastMessage = currentPrompt2.messages[currentPrompt2.messages.length - 1];
|
4262
|
+
if (typeof lastMessage.content === "string") {
|
4263
|
+
lastMessage.content = stepText;
|
4264
|
+
} else {
|
4265
|
+
lastMessage.content.push({
|
4266
|
+
text: stepText,
|
4267
|
+
type: "text"
|
4268
|
+
});
|
4269
|
+
}
|
4270
|
+
currentPrompt2.messages[currentPrompt2.messages.length - 1] = lastMessage;
|
4271
|
+
} else {
|
4272
|
+
const newResponseMessages = toResponseMessages({
|
4299
4273
|
text: stepText,
|
4300
|
-
|
4274
|
+
toolCalls: stepToolCalls,
|
4275
|
+
toolResults: stepToolResults
|
4301
4276
|
});
|
4302
|
-
|
4303
|
-
promptMessages2.push(
|
4304
|
-
...toResponseMessages({
|
4305
|
-
text: stepText,
|
4306
|
-
toolCalls: stepToolCalls,
|
4307
|
-
toolResults: stepToolResults
|
4308
|
-
}).map(
|
4309
|
-
(message) => convertToLanguageModelMessage(message, null)
|
4310
|
-
)
|
4311
|
-
);
|
4277
|
+
currentPrompt2.messages.push(...newResponseMessages);
|
4312
4278
|
}
|
4313
4279
|
const {
|
4314
4280
|
result,
|
4315
4281
|
doStreamSpan: doStreamSpan3,
|
4316
4282
|
startTimestampMs: startTimestamp2
|
4317
4283
|
} = await startStep({
|
4318
|
-
|
4319
|
-
|
4284
|
+
currentPrompt: {
|
4285
|
+
type: "messages",
|
4286
|
+
system: currentPrompt2.system,
|
4287
|
+
messages: currentPrompt2.messages
|
4288
|
+
}
|
4320
4289
|
});
|
4321
4290
|
self.warnings = result.warnings;
|
4322
4291
|
self.rawResponse = result.rawResponse;
|
@@ -4325,7 +4294,7 @@ var DefaultStreamTextResult = class {
|
|
4325
4294
|
startTimestamp: startTimestamp2,
|
4326
4295
|
doStreamSpan: doStreamSpan3,
|
4327
4296
|
currentStep: currentStep + 1,
|
4328
|
-
|
4297
|
+
currentPrompt: currentPrompt2,
|
4329
4298
|
usage: combinedUsage,
|
4330
4299
|
stepType: nextStepType,
|
4331
4300
|
previousStepText: fullStepText
|
@@ -4432,7 +4401,7 @@ var DefaultStreamTextResult = class {
|
|
4432
4401
|
startTimestamp: startTimestampMs,
|
4433
4402
|
doStreamSpan,
|
4434
4403
|
currentStep: 0,
|
4435
|
-
|
4404
|
+
currentPrompt,
|
4436
4405
|
usage: void 0,
|
4437
4406
|
stepType: "initial"
|
4438
4407
|
});
|
@@ -4806,7 +4775,7 @@ function convertToCoreMessages(messages) {
|
|
4806
4775
|
role: "assistant",
|
4807
4776
|
content: [
|
4808
4777
|
{ type: "text", text: content },
|
4809
|
-
...toolInvocations.map(({ toolCallId, toolName, args }) => ({
|
4778
|
+
...toolInvocations.filter((invocation) => invocation.state !== "partial-call").map(({ toolCallId, toolName, args }) => ({
|
4810
4779
|
type: "tool-call",
|
4811
4780
|
toolCallId,
|
4812
4781
|
toolName,
|
@@ -4814,25 +4783,19 @@ function convertToCoreMessages(messages) {
|
|
4814
4783
|
}))
|
4815
4784
|
]
|
4816
4785
|
});
|
4817
|
-
|
4818
|
-
|
4819
|
-
|
4820
|
-
|
4821
|
-
|
4822
|
-
|
4823
|
-
|
4824
|
-
|
4825
|
-
|
4826
|
-
|
4827
|
-
|
4828
|
-
|
4829
|
-
|
4830
|
-
toolName,
|
4831
|
-
args,
|
4832
|
-
result
|
4833
|
-
};
|
4834
|
-
})
|
4835
|
-
});
|
4786
|
+
const toolResults = toolInvocations.filter((invocation) => invocation.state === "result").map(({ toolCallId, toolName, args, result }) => ({
|
4787
|
+
type: "tool-result",
|
4788
|
+
toolCallId,
|
4789
|
+
toolName,
|
4790
|
+
args,
|
4791
|
+
result
|
4792
|
+
}));
|
4793
|
+
if (toolResults.length > 0) {
|
4794
|
+
coreMessages.push({
|
4795
|
+
role: "tool",
|
4796
|
+
content: toolResults
|
4797
|
+
});
|
4798
|
+
}
|
4836
4799
|
break;
|
4837
4800
|
}
|
4838
4801
|
case "function":
|