ai 7.0.42 → 7.0.44
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 +17 -0
- package/dist/index.d.ts +23 -4
- package/dist/index.js +145 -11
- package/dist/index.js.map +1 -1
- package/dist/internal/index.js +1 -1
- package/docs/03-ai-sdk-core/18-code-mode.mdx +246 -0
- package/docs/03-ai-sdk-core/65-lifecycle-callbacks.mdx +1 -1
- package/docs/03-ai-sdk-core/index.mdx +6 -0
- package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +9 -1
- package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +2 -1
- package/docs/07-reference/01-ai-sdk-core/23-create-mcp-client.mdx +7 -0
- package/package.json +4 -4
- package/src/generate-text/generate-text.ts +33 -8
- package/src/generate-text/index.ts +4 -0
- package/src/generate-text/stream-language-model-call.ts +3 -1
- package/src/generate-text/tool-caller-configuration.ts +186 -0
- package/src/index.ts +2 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import {
|
|
2
|
+
experimental_getToolCaller,
|
|
3
|
+
type Experimental_ToolCallerTool,
|
|
4
|
+
type Tool,
|
|
5
|
+
type ToolSet,
|
|
6
|
+
} from '@ai-sdk/provider-utils';
|
|
7
|
+
import { InvalidArgumentError } from '../error/invalid-argument-error';
|
|
8
|
+
|
|
9
|
+
export interface Experimental_ToolCallerReference<
|
|
10
|
+
NAME extends string = string,
|
|
11
|
+
> {
|
|
12
|
+
readonly toolName: NAME;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type ToolCallerName<TOOLS extends ToolSet> = {
|
|
16
|
+
[NAME in keyof TOOLS]: TOOLS[NAME] extends Experimental_ToolCallerTool
|
|
17
|
+
? NAME
|
|
18
|
+
: never;
|
|
19
|
+
}[keyof TOOLS] &
|
|
20
|
+
string;
|
|
21
|
+
|
|
22
|
+
type ToolCallerReferenceUnion<TOOLS extends ToolSet> = {
|
|
23
|
+
[NAME in ToolCallerName<TOOLS>]: Experimental_ToolCallerReference<NAME>;
|
|
24
|
+
}[ToolCallerName<TOOLS>];
|
|
25
|
+
|
|
26
|
+
export type Experimental_ToolCallers<TOOLS extends ToolSet> = (callers: {
|
|
27
|
+
[NAME in ToolCallerName<TOOLS>]: Experimental_ToolCallerReference<NAME>;
|
|
28
|
+
}) => {
|
|
29
|
+
[NAME in keyof TOOLS]?: ReadonlyArray<
|
|
30
|
+
'direct' | ToolCallerReferenceUnion<TOOLS>
|
|
31
|
+
>;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type ResolvedToolCallers = Record<
|
|
35
|
+
string,
|
|
36
|
+
ReadonlyArray<'direct' | string>
|
|
37
|
+
>;
|
|
38
|
+
|
|
39
|
+
export function resolveToolCallerConfiguration<TOOLS extends ToolSet>({
|
|
40
|
+
tools,
|
|
41
|
+
toolCallers,
|
|
42
|
+
}: {
|
|
43
|
+
tools: TOOLS | undefined;
|
|
44
|
+
toolCallers: Experimental_ToolCallers<TOOLS> | undefined;
|
|
45
|
+
}): ResolvedToolCallers | undefined {
|
|
46
|
+
if (tools == null || toolCallers == null) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const namesByReference = new WeakMap<object, string>();
|
|
51
|
+
const callerReferences: Record<string, Experimental_ToolCallerReference> = {};
|
|
52
|
+
|
|
53
|
+
for (const [toolName, tool] of Object.entries(tools)) {
|
|
54
|
+
if (experimental_getToolCaller(tool) == null) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const reference = Object.freeze({ toolName });
|
|
59
|
+
namesByReference.set(reference, toolName);
|
|
60
|
+
callerReferences[toolName] = reference;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const configuration = toolCallers(callerReferences as never);
|
|
64
|
+
const resolved: ResolvedToolCallers = {};
|
|
65
|
+
|
|
66
|
+
for (const [toolName, callers] of Object.entries(configuration)) {
|
|
67
|
+
if (!Object.prototype.hasOwnProperty.call(tools, toolName)) {
|
|
68
|
+
throw new InvalidArgumentError({
|
|
69
|
+
parameter: 'experimental_toolCallers',
|
|
70
|
+
value: configuration,
|
|
71
|
+
message: `unknown tool "${toolName}".`,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!Array.isArray(callers)) {
|
|
76
|
+
throw new InvalidArgumentError({
|
|
77
|
+
parameter: 'experimental_toolCallers',
|
|
78
|
+
value: configuration,
|
|
79
|
+
message: `callers for tool "${toolName}" must be an array.`,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
resolved[toolName] = callers.map(caller => {
|
|
84
|
+
if (caller === 'direct') {
|
|
85
|
+
return caller;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const callerName =
|
|
89
|
+
caller != null && typeof caller === 'object'
|
|
90
|
+
? namesByReference.get(caller)
|
|
91
|
+
: undefined;
|
|
92
|
+
|
|
93
|
+
if (callerName == null) {
|
|
94
|
+
throw new InvalidArgumentError({
|
|
95
|
+
parameter: 'experimental_toolCallers',
|
|
96
|
+
value: configuration,
|
|
97
|
+
message: `tool "${toolName}" contains an invalid caller reference.`,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return callerName;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return resolved;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function prepareToolsForToolCallers({
|
|
109
|
+
tools,
|
|
110
|
+
toolCallers,
|
|
111
|
+
}: {
|
|
112
|
+
tools: ToolSet | undefined;
|
|
113
|
+
toolCallers: ResolvedToolCallers | undefined;
|
|
114
|
+
}): {
|
|
115
|
+
executionTools: ToolSet | undefined;
|
|
116
|
+
modelTools: ToolSet | undefined;
|
|
117
|
+
} {
|
|
118
|
+
if (tools == null || toolCallers == null) {
|
|
119
|
+
return { executionTools: tools, modelTools: tools };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const executionTools: ToolSet = { ...tools };
|
|
123
|
+
const modelTools: ToolSet = { ...tools };
|
|
124
|
+
const localToolsByCaller = new Map<string, ToolSet>();
|
|
125
|
+
|
|
126
|
+
for (const [toolName, callerNames] of Object.entries(toolCallers)) {
|
|
127
|
+
const tool = executionTools[toolName];
|
|
128
|
+
if (tool == null) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
let availableDirectly = false;
|
|
133
|
+
let availableToProvider = false;
|
|
134
|
+
let preparedTool: Tool = tool;
|
|
135
|
+
|
|
136
|
+
for (const callerName of callerNames) {
|
|
137
|
+
if (callerName === 'direct') {
|
|
138
|
+
availableDirectly = true;
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const caller = experimental_getToolCaller(executionTools[callerName]);
|
|
143
|
+
if (caller == null) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (caller.type === 'provider') {
|
|
148
|
+
availableToProvider = true;
|
|
149
|
+
preparedTool = {
|
|
150
|
+
...preparedTool,
|
|
151
|
+
providerOptions: caller.prepareProviderOptions(
|
|
152
|
+
preparedTool.providerOptions,
|
|
153
|
+
),
|
|
154
|
+
} as Tool;
|
|
155
|
+
} else {
|
|
156
|
+
const localTools = localToolsByCaller.get(callerName) ?? {};
|
|
157
|
+
localTools[toolName] = preparedTool;
|
|
158
|
+
localToolsByCaller.set(callerName, localTools);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
executionTools[toolName] = preparedTool;
|
|
163
|
+
|
|
164
|
+
if (availableDirectly || availableToProvider) {
|
|
165
|
+
modelTools[toolName] = preparedTool;
|
|
166
|
+
} else {
|
|
167
|
+
delete modelTools[toolName];
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
for (const [callerName, callerTool] of Object.entries(executionTools)) {
|
|
172
|
+
const caller = experimental_getToolCaller(callerTool);
|
|
173
|
+
if (caller?.type !== 'local') {
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const boundCaller = caller.bind(localToolsByCaller.get(callerName) ?? {});
|
|
178
|
+
executionTools[callerName] = boundCaller;
|
|
179
|
+
|
|
180
|
+
if (Object.prototype.hasOwnProperty.call(modelTools, callerName)) {
|
|
181
|
+
modelTools[callerName] = boundCaller;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return { executionTools, modelTools };
|
|
186
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ export {
|
|
|
7
7
|
asSchema,
|
|
8
8
|
createIdGenerator,
|
|
9
9
|
dynamicTool,
|
|
10
|
+
experimental_toolCaller,
|
|
10
11
|
generateId,
|
|
11
12
|
jsonSchema,
|
|
12
13
|
parseJsonEventStream,
|
|
@@ -19,6 +20,7 @@ export {
|
|
|
19
20
|
type InferToolOutput,
|
|
20
21
|
type Experimental_SandboxSession,
|
|
21
22
|
type Experimental_SandboxProcess,
|
|
23
|
+
type Experimental_ToolCallerTool,
|
|
22
24
|
type Schema,
|
|
23
25
|
type Tool,
|
|
24
26
|
type ToolApprovalRequest,
|