ai 3.2.22 → 3.2.24
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/README.md +6 -5
- package/dist/index.d.mts +57 -14
- package/dist/index.d.ts +57 -14
- package/dist/index.js +19 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +19 -7
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -9
- package/svelte/dist/index.d.mts +1 -1
- package/svelte/dist/index.d.ts +1 -1
- package/svelte/dist/index.js +8 -4
- package/svelte/dist/index.js.map +1 -1
- package/svelte/dist/index.mjs +8 -4
- package/svelte/dist/index.mjs.map +1 -1
package/README.md
CHANGED
@@ -52,12 +52,13 @@ The [AI SDK UI](https://sdk.vercel.ai/docs/ai-sdk-ui/overview) module provides a
|
|
52
52
|
###### @/app/page.tsx (Next.js App Router)
|
53
53
|
|
54
54
|
```tsx
|
55
|
-
|
55
|
+
'use client';
|
56
56
|
|
57
|
-
import { useChat } from 'ai/react'
|
57
|
+
import { useChat } from 'ai/react';
|
58
58
|
|
59
59
|
export default function Page() {
|
60
|
-
const { messages, input, handleSubmit, handleInputChange, isLoading } =
|
60
|
+
const { messages, input, handleSubmit, handleInputChange, isLoading } =
|
61
|
+
useChat();
|
61
62
|
|
62
63
|
return (
|
63
64
|
<div>
|
@@ -66,7 +67,7 @@ export default function Page() {
|
|
66
67
|
<div>{message.role}</div>
|
67
68
|
<div>{message.content}</div>
|
68
69
|
</div>
|
69
|
-
)}
|
70
|
+
))}
|
70
71
|
|
71
72
|
<form onSubmit={handleSubmit}>
|
72
73
|
<input
|
@@ -77,7 +78,7 @@ export default function Page() {
|
|
77
78
|
/>
|
78
79
|
</form>
|
79
80
|
</div>
|
80
|
-
)
|
81
|
+
);
|
81
82
|
}
|
82
83
|
```
|
83
84
|
|
package/dist/index.d.mts
CHANGED
@@ -1062,31 +1062,74 @@ declare class GenerateTextResult<TOOLS extends Record<string, CoreTool>> {
|
|
1062
1062
|
*/
|
1063
1063
|
readonly responseMessages: Array<CoreAssistantMessage | CoreToolMessage>;
|
1064
1064
|
/**
|
1065
|
+
Response information for every roundtrip.
|
1066
|
+
You can use this to get information about intermediate steps, such as the tool calls or the response headers.
|
1067
|
+
*/
|
1068
|
+
readonly roundtrips: Array<{
|
1069
|
+
/**
|
1070
|
+
The generated text.
|
1071
|
+
*/
|
1072
|
+
readonly text: string;
|
1073
|
+
/**
|
1074
|
+
The tool calls that were made during the generation.
|
1075
|
+
*/
|
1076
|
+
readonly toolCalls: ToToolCallArray<TOOLS>;
|
1077
|
+
/**
|
1078
|
+
The results of the tool calls.
|
1079
|
+
*/
|
1080
|
+
readonly toolResults: ToToolResultArray<TOOLS>;
|
1081
|
+
/**
|
1082
|
+
The reason why the generation finished.
|
1083
|
+
*/
|
1084
|
+
readonly finishReason: FinishReason;
|
1085
|
+
/**
|
1086
|
+
The token usage of the generated text.
|
1087
|
+
*/
|
1088
|
+
readonly usage: CompletionTokenUsage$1;
|
1089
|
+
/**
|
1090
|
+
Warnings from the model provider (e.g. unsupported settings)
|
1091
|
+
*/
|
1092
|
+
readonly warnings: CallWarning[] | undefined;
|
1093
|
+
/**
|
1094
|
+
Logprobs for the completion.
|
1095
|
+
`undefined` if the mode does not support logprobs or if was not enabled.
|
1096
|
+
*/
|
1097
|
+
readonly logprobs: LogProbs | undefined;
|
1098
|
+
/**
|
1099
|
+
Optional raw response data.
|
1100
|
+
*/
|
1101
|
+
readonly rawResponse?: {
|
1102
|
+
/**
|
1103
|
+
Response headers.
|
1104
|
+
*/
|
1105
|
+
readonly headers?: Record<string, string>;
|
1106
|
+
};
|
1107
|
+
}>;
|
1108
|
+
/**
|
1065
1109
|
Optional raw response data.
|
1066
1110
|
*/
|
1067
|
-
rawResponse?: {
|
1111
|
+
readonly rawResponse?: {
|
1068
1112
|
/**
|
1069
1113
|
Response headers.
|
1070
1114
|
*/
|
1071
|
-
headers?: Record<string, string>;
|
1115
|
+
readonly headers?: Record<string, string>;
|
1072
1116
|
};
|
1073
1117
|
/**
|
1074
1118
|
Logprobs for the completion.
|
1075
|
-
`undefined` if the mode does not support logprobs or if was not enabled
|
1119
|
+
`undefined` if the mode does not support logprobs or if was not enabled.
|
1076
1120
|
*/
|
1077
1121
|
readonly logprobs: LogProbs | undefined;
|
1078
1122
|
constructor(options: {
|
1079
|
-
text:
|
1080
|
-
toolCalls:
|
1081
|
-
toolResults:
|
1082
|
-
finishReason:
|
1083
|
-
usage:
|
1084
|
-
warnings:
|
1085
|
-
rawResponse?:
|
1086
|
-
|
1087
|
-
|
1088
|
-
|
1089
|
-
responseMessages: Array<CoreAssistantMessage | CoreToolMessage>;
|
1123
|
+
text: GenerateTextResult<TOOLS>['text'];
|
1124
|
+
toolCalls: GenerateTextResult<TOOLS>['toolCalls'];
|
1125
|
+
toolResults: GenerateTextResult<TOOLS>['toolResults'];
|
1126
|
+
finishReason: GenerateTextResult<TOOLS>['finishReason'];
|
1127
|
+
usage: GenerateTextResult<TOOLS>['usage'];
|
1128
|
+
warnings: GenerateTextResult<TOOLS>['warnings'];
|
1129
|
+
rawResponse?: GenerateTextResult<TOOLS>['rawResponse'];
|
1130
|
+
logprobs: GenerateTextResult<TOOLS>['logprobs'];
|
1131
|
+
responseMessages: GenerateTextResult<TOOLS>['responseMessages'];
|
1132
|
+
roundtrips: GenerateTextResult<TOOLS>['roundtrips'];
|
1090
1133
|
});
|
1091
1134
|
}
|
1092
1135
|
/**
|
package/dist/index.d.ts
CHANGED
@@ -1062,31 +1062,74 @@ declare class GenerateTextResult<TOOLS extends Record<string, CoreTool>> {
|
|
1062
1062
|
*/
|
1063
1063
|
readonly responseMessages: Array<CoreAssistantMessage | CoreToolMessage>;
|
1064
1064
|
/**
|
1065
|
+
Response information for every roundtrip.
|
1066
|
+
You can use this to get information about intermediate steps, such as the tool calls or the response headers.
|
1067
|
+
*/
|
1068
|
+
readonly roundtrips: Array<{
|
1069
|
+
/**
|
1070
|
+
The generated text.
|
1071
|
+
*/
|
1072
|
+
readonly text: string;
|
1073
|
+
/**
|
1074
|
+
The tool calls that were made during the generation.
|
1075
|
+
*/
|
1076
|
+
readonly toolCalls: ToToolCallArray<TOOLS>;
|
1077
|
+
/**
|
1078
|
+
The results of the tool calls.
|
1079
|
+
*/
|
1080
|
+
readonly toolResults: ToToolResultArray<TOOLS>;
|
1081
|
+
/**
|
1082
|
+
The reason why the generation finished.
|
1083
|
+
*/
|
1084
|
+
readonly finishReason: FinishReason;
|
1085
|
+
/**
|
1086
|
+
The token usage of the generated text.
|
1087
|
+
*/
|
1088
|
+
readonly usage: CompletionTokenUsage$1;
|
1089
|
+
/**
|
1090
|
+
Warnings from the model provider (e.g. unsupported settings)
|
1091
|
+
*/
|
1092
|
+
readonly warnings: CallWarning[] | undefined;
|
1093
|
+
/**
|
1094
|
+
Logprobs for the completion.
|
1095
|
+
`undefined` if the mode does not support logprobs or if was not enabled.
|
1096
|
+
*/
|
1097
|
+
readonly logprobs: LogProbs | undefined;
|
1098
|
+
/**
|
1099
|
+
Optional raw response data.
|
1100
|
+
*/
|
1101
|
+
readonly rawResponse?: {
|
1102
|
+
/**
|
1103
|
+
Response headers.
|
1104
|
+
*/
|
1105
|
+
readonly headers?: Record<string, string>;
|
1106
|
+
};
|
1107
|
+
}>;
|
1108
|
+
/**
|
1065
1109
|
Optional raw response data.
|
1066
1110
|
*/
|
1067
|
-
rawResponse?: {
|
1111
|
+
readonly rawResponse?: {
|
1068
1112
|
/**
|
1069
1113
|
Response headers.
|
1070
1114
|
*/
|
1071
|
-
headers?: Record<string, string>;
|
1115
|
+
readonly headers?: Record<string, string>;
|
1072
1116
|
};
|
1073
1117
|
/**
|
1074
1118
|
Logprobs for the completion.
|
1075
|
-
`undefined` if the mode does not support logprobs or if was not enabled
|
1119
|
+
`undefined` if the mode does not support logprobs or if was not enabled.
|
1076
1120
|
*/
|
1077
1121
|
readonly logprobs: LogProbs | undefined;
|
1078
1122
|
constructor(options: {
|
1079
|
-
text:
|
1080
|
-
toolCalls:
|
1081
|
-
toolResults:
|
1082
|
-
finishReason:
|
1083
|
-
usage:
|
1084
|
-
warnings:
|
1085
|
-
rawResponse?:
|
1086
|
-
|
1087
|
-
|
1088
|
-
|
1089
|
-
responseMessages: Array<CoreAssistantMessage | CoreToolMessage>;
|
1123
|
+
text: GenerateTextResult<TOOLS>['text'];
|
1124
|
+
toolCalls: GenerateTextResult<TOOLS>['toolCalls'];
|
1125
|
+
toolResults: GenerateTextResult<TOOLS>['toolResults'];
|
1126
|
+
finishReason: GenerateTextResult<TOOLS>['finishReason'];
|
1127
|
+
usage: GenerateTextResult<TOOLS>['usage'];
|
1128
|
+
warnings: GenerateTextResult<TOOLS>['warnings'];
|
1129
|
+
rawResponse?: GenerateTextResult<TOOLS>['rawResponse'];
|
1130
|
+
logprobs: GenerateTextResult<TOOLS>['logprobs'];
|
1131
|
+
responseMessages: GenerateTextResult<TOOLS>['responseMessages'];
|
1132
|
+
roundtrips: GenerateTextResult<TOOLS>['roundtrips'];
|
1090
1133
|
});
|
1091
1134
|
}
|
1092
1135
|
/**
|
package/dist/index.js
CHANGED
@@ -1428,7 +1428,7 @@ async function generateText({
|
|
1428
1428
|
},
|
1429
1429
|
tracer,
|
1430
1430
|
fn: async (span) => {
|
1431
|
-
var _a2, _b, _c;
|
1431
|
+
var _a2, _b, _c, _d;
|
1432
1432
|
const retry = retryWithExponentialBackoff({ maxRetries });
|
1433
1433
|
const validatedPrompt = getValidatedPrompt({
|
1434
1434
|
system,
|
@@ -1444,10 +1444,11 @@ async function generateText({
|
|
1444
1444
|
let currentModelResponse;
|
1445
1445
|
let currentToolCalls = [];
|
1446
1446
|
let currentToolResults = [];
|
1447
|
-
let
|
1447
|
+
let roundtripCount = 0;
|
1448
1448
|
const responseMessages = [];
|
1449
|
+
const roundtrips = [];
|
1449
1450
|
do {
|
1450
|
-
const currentInputFormat =
|
1451
|
+
const currentInputFormat = roundtripCount === 0 ? validatedPrompt.type : "messages";
|
1451
1452
|
currentModelResponse = await retry(
|
1452
1453
|
() => recordSpan({
|
1453
1454
|
name: "ai.generateText.doGenerate",
|
@@ -1485,9 +1486,18 @@ async function generateText({
|
|
1485
1486
|
tools,
|
1486
1487
|
tracer
|
1487
1488
|
});
|
1488
|
-
|
1489
|
+
roundtrips.push({
|
1489
1490
|
text: (_b = currentModelResponse.text) != null ? _b : "",
|
1490
1491
|
toolCalls: currentToolCalls,
|
1492
|
+
toolResults: currentToolResults,
|
1493
|
+
finishReason: currentModelResponse.finishReason,
|
1494
|
+
usage: calculateCompletionTokenUsage(currentModelResponse.usage),
|
1495
|
+
warnings: currentModelResponse.warnings,
|
1496
|
+
logprobs: currentModelResponse.logprobs
|
1497
|
+
});
|
1498
|
+
const newResponseMessages = toResponseMessages({
|
1499
|
+
text: (_c = currentModelResponse.text) != null ? _c : "",
|
1500
|
+
toolCalls: currentToolCalls,
|
1491
1501
|
toolResults: currentToolResults
|
1492
1502
|
});
|
1493
1503
|
responseMessages.push(...newResponseMessages);
|
@@ -1498,7 +1508,7 @@ async function generateText({
|
|
1498
1508
|
// there are tool calls:
|
1499
1509
|
currentToolCalls.length > 0 && // all current tool calls have results:
|
1500
1510
|
currentToolResults.length === currentToolCalls.length && // the number of roundtrips is less than the maximum:
|
1501
|
-
|
1511
|
+
roundtripCount++ < maxToolRoundtrips
|
1502
1512
|
);
|
1503
1513
|
span.setAttributes({
|
1504
1514
|
"ai.finishReason": currentModelResponse.finishReason,
|
@@ -1511,7 +1521,7 @@ async function generateText({
|
|
1511
1521
|
// Always return a string so that the caller doesn't have to check for undefined.
|
1512
1522
|
// If they need to check if the model did not return any text,
|
1513
1523
|
// they can check the length of the string:
|
1514
|
-
text: (
|
1524
|
+
text: (_d = currentModelResponse.text) != null ? _d : "",
|
1515
1525
|
toolCalls: currentToolCalls,
|
1516
1526
|
toolResults: currentToolResults,
|
1517
1527
|
finishReason: currentModelResponse.finishReason,
|
@@ -1519,7 +1529,8 @@ async function generateText({
|
|
1519
1529
|
warnings: currentModelResponse.warnings,
|
1520
1530
|
rawResponse: currentModelResponse.rawResponse,
|
1521
1531
|
logprobs: currentModelResponse.logprobs,
|
1522
|
-
responseMessages
|
1532
|
+
responseMessages,
|
1533
|
+
roundtrips
|
1523
1534
|
});
|
1524
1535
|
}
|
1525
1536
|
});
|
@@ -1577,6 +1588,7 @@ var GenerateTextResult = class {
|
|
1577
1588
|
this.rawResponse = options.rawResponse;
|
1578
1589
|
this.logprobs = options.logprobs;
|
1579
1590
|
this.responseMessages = options.responseMessages;
|
1591
|
+
this.roundtrips = options.roundtrips;
|
1580
1592
|
}
|
1581
1593
|
};
|
1582
1594
|
function toResponseMessages({
|