@ai-sdk/anthropic 2.0.0-canary.9 → 2.0.0
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 +481 -0
- package/README.md +2 -2
- package/dist/index.d.mts +129 -421
- package/dist/index.d.ts +129 -421
- package/dist/index.js +1173 -408
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1149 -392
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +178 -394
- package/dist/internal/index.d.ts +178 -394
- package/dist/internal/index.js +1159 -396
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +1135 -382
- package/dist/internal/index.mjs.map +1 -1
- package/internal.d.ts +1 -0
- package/package.json +9 -8
package/dist/index.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
NoSuchModelError
|
|
4
4
|
} from "@ai-sdk/provider";
|
|
5
5
|
import {
|
|
6
|
+
generateId as generateId2,
|
|
6
7
|
loadApiKey,
|
|
7
8
|
withoutTrailingSlash
|
|
8
9
|
} from "@ai-sdk/provider-utils";
|
|
@@ -15,15 +16,16 @@ import {
|
|
|
15
16
|
combineHeaders,
|
|
16
17
|
createEventSourceResponseHandler,
|
|
17
18
|
createJsonResponseHandler,
|
|
18
|
-
|
|
19
|
+
generateId,
|
|
20
|
+
parseProviderOptions as parseProviderOptions2,
|
|
19
21
|
postJsonToApi,
|
|
20
22
|
resolve
|
|
21
23
|
} from "@ai-sdk/provider-utils";
|
|
22
|
-
import { z as
|
|
24
|
+
import { z as z4 } from "zod/v4";
|
|
23
25
|
|
|
24
26
|
// src/anthropic-error.ts
|
|
25
27
|
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
|
26
|
-
import { z } from "zod";
|
|
28
|
+
import { z } from "zod/v4";
|
|
27
29
|
var anthropicErrorDataSchema = z.object({
|
|
28
30
|
type: z.literal("error"),
|
|
29
31
|
error: z.object({
|
|
@@ -36,13 +38,113 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
36
38
|
errorToMessage: (data) => data.error.message
|
|
37
39
|
});
|
|
38
40
|
|
|
41
|
+
// src/anthropic-messages-options.ts
|
|
42
|
+
import { z as z2 } from "zod/v4";
|
|
43
|
+
var anthropicFilePartProviderOptions = z2.object({
|
|
44
|
+
/**
|
|
45
|
+
* Citation configuration for this document.
|
|
46
|
+
* When enabled, this document will generate citations in the response.
|
|
47
|
+
*/
|
|
48
|
+
citations: z2.object({
|
|
49
|
+
/**
|
|
50
|
+
* Enable citations for this document
|
|
51
|
+
*/
|
|
52
|
+
enabled: z2.boolean()
|
|
53
|
+
}).optional(),
|
|
54
|
+
/**
|
|
55
|
+
* Custom title for the document.
|
|
56
|
+
* If not provided, the filename will be used.
|
|
57
|
+
*/
|
|
58
|
+
title: z2.string().optional(),
|
|
59
|
+
/**
|
|
60
|
+
* Context about the document that will be passed to the model
|
|
61
|
+
* but not used towards cited content.
|
|
62
|
+
* Useful for storing document metadata as text or stringified JSON.
|
|
63
|
+
*/
|
|
64
|
+
context: z2.string().optional()
|
|
65
|
+
});
|
|
66
|
+
var anthropicProviderOptions = z2.object({
|
|
67
|
+
sendReasoning: z2.boolean().optional(),
|
|
68
|
+
thinking: z2.object({
|
|
69
|
+
type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
|
|
70
|
+
budgetTokens: z2.number().optional()
|
|
71
|
+
}).optional(),
|
|
72
|
+
/**
|
|
73
|
+
* Whether to disable parallel function calling during tool use. Default is false.
|
|
74
|
+
* When set to true, Claude will use at most one tool per response.
|
|
75
|
+
*/
|
|
76
|
+
disableParallelToolUse: z2.boolean().optional()
|
|
77
|
+
});
|
|
78
|
+
|
|
39
79
|
// src/anthropic-prepare-tools.ts
|
|
40
80
|
import {
|
|
41
81
|
UnsupportedFunctionalityError
|
|
42
82
|
} from "@ai-sdk/provider";
|
|
83
|
+
|
|
84
|
+
// src/get-cache-control.ts
|
|
85
|
+
function getCacheControl(providerMetadata) {
|
|
86
|
+
var _a;
|
|
87
|
+
const anthropic2 = providerMetadata == null ? void 0 : providerMetadata.anthropic;
|
|
88
|
+
const cacheControlValue = (_a = anthropic2 == null ? void 0 : anthropic2.cacheControl) != null ? _a : anthropic2 == null ? void 0 : anthropic2.cache_control;
|
|
89
|
+
return cacheControlValue;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// src/tool/web-search_20250305.ts
|
|
93
|
+
import { createProviderDefinedToolFactoryWithOutputSchema } from "@ai-sdk/provider-utils";
|
|
94
|
+
import { z as z3 } from "zod/v4";
|
|
95
|
+
var webSearch_20250305ArgsSchema = z3.object({
|
|
96
|
+
/**
|
|
97
|
+
* Maximum number of web searches Claude can perform during the conversation.
|
|
98
|
+
*/
|
|
99
|
+
maxUses: z3.number().optional(),
|
|
100
|
+
/**
|
|
101
|
+
* Optional list of domains that Claude is allowed to search.
|
|
102
|
+
*/
|
|
103
|
+
allowedDomains: z3.array(z3.string()).optional(),
|
|
104
|
+
/**
|
|
105
|
+
* Optional list of domains that Claude should avoid when searching.
|
|
106
|
+
*/
|
|
107
|
+
blockedDomains: z3.array(z3.string()).optional(),
|
|
108
|
+
/**
|
|
109
|
+
* Optional user location information to provide geographically relevant search results.
|
|
110
|
+
*/
|
|
111
|
+
userLocation: z3.object({
|
|
112
|
+
type: z3.literal("approximate"),
|
|
113
|
+
city: z3.string().optional(),
|
|
114
|
+
region: z3.string().optional(),
|
|
115
|
+
country: z3.string().optional(),
|
|
116
|
+
timezone: z3.string().optional()
|
|
117
|
+
}).optional()
|
|
118
|
+
});
|
|
119
|
+
var webSearch_20250305OutputSchema = z3.array(
|
|
120
|
+
z3.object({
|
|
121
|
+
url: z3.string(),
|
|
122
|
+
title: z3.string(),
|
|
123
|
+
pageAge: z3.string().nullable(),
|
|
124
|
+
encryptedContent: z3.string(),
|
|
125
|
+
type: z3.string()
|
|
126
|
+
})
|
|
127
|
+
);
|
|
128
|
+
var factory = createProviderDefinedToolFactoryWithOutputSchema({
|
|
129
|
+
id: "anthropic.web_search_20250305",
|
|
130
|
+
name: "web_search",
|
|
131
|
+
inputSchema: z3.object({
|
|
132
|
+
query: z3.string()
|
|
133
|
+
}),
|
|
134
|
+
outputSchema: webSearch_20250305OutputSchema
|
|
135
|
+
});
|
|
136
|
+
var webSearch_20250305 = (args = {}) => {
|
|
137
|
+
return factory(args);
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// src/anthropic-prepare-tools.ts
|
|
141
|
+
function isWebSearchTool(tool) {
|
|
142
|
+
return typeof tool === "object" && tool !== null && "type" in tool && tool.type === "web_search_20250305";
|
|
143
|
+
}
|
|
43
144
|
function prepareTools({
|
|
44
145
|
tools,
|
|
45
|
-
toolChoice
|
|
146
|
+
toolChoice,
|
|
147
|
+
disableParallelToolUse
|
|
46
148
|
}) {
|
|
47
149
|
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
48
150
|
const toolWarnings = [];
|
|
@@ -52,12 +154,18 @@ function prepareTools({
|
|
|
52
154
|
}
|
|
53
155
|
const anthropicTools2 = [];
|
|
54
156
|
for (const tool of tools) {
|
|
157
|
+
if (isWebSearchTool(tool)) {
|
|
158
|
+
anthropicTools2.push(tool);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
55
161
|
switch (tool.type) {
|
|
56
162
|
case "function":
|
|
163
|
+
const cacheControl = getCacheControl(tool.providerOptions);
|
|
57
164
|
anthropicTools2.push({
|
|
58
165
|
name: tool.name,
|
|
59
166
|
description: tool.description,
|
|
60
|
-
input_schema: tool.
|
|
167
|
+
input_schema: tool.inputSchema,
|
|
168
|
+
cache_control: cacheControl
|
|
61
169
|
});
|
|
62
170
|
break;
|
|
63
171
|
case "provider-defined":
|
|
@@ -65,7 +173,7 @@ function prepareTools({
|
|
|
65
173
|
case "anthropic.computer_20250124":
|
|
66
174
|
betas.add("computer-use-2025-01-24");
|
|
67
175
|
anthropicTools2.push({
|
|
68
|
-
name:
|
|
176
|
+
name: "computer",
|
|
69
177
|
type: "computer_20250124",
|
|
70
178
|
display_width_px: tool.args.displayWidthPx,
|
|
71
179
|
display_height_px: tool.args.displayHeightPx,
|
|
@@ -75,7 +183,7 @@ function prepareTools({
|
|
|
75
183
|
case "anthropic.computer_20241022":
|
|
76
184
|
betas.add("computer-use-2024-10-22");
|
|
77
185
|
anthropicTools2.push({
|
|
78
|
-
name:
|
|
186
|
+
name: "computer",
|
|
79
187
|
type: "computer_20241022",
|
|
80
188
|
display_width_px: tool.args.displayWidthPx,
|
|
81
189
|
display_height_px: tool.args.displayHeightPx,
|
|
@@ -85,31 +193,50 @@ function prepareTools({
|
|
|
85
193
|
case "anthropic.text_editor_20250124":
|
|
86
194
|
betas.add("computer-use-2025-01-24");
|
|
87
195
|
anthropicTools2.push({
|
|
88
|
-
name:
|
|
196
|
+
name: "str_replace_editor",
|
|
89
197
|
type: "text_editor_20250124"
|
|
90
198
|
});
|
|
91
199
|
break;
|
|
92
200
|
case "anthropic.text_editor_20241022":
|
|
93
201
|
betas.add("computer-use-2024-10-22");
|
|
94
202
|
anthropicTools2.push({
|
|
95
|
-
name:
|
|
203
|
+
name: "str_replace_editor",
|
|
96
204
|
type: "text_editor_20241022"
|
|
97
205
|
});
|
|
98
206
|
break;
|
|
207
|
+
case "anthropic.text_editor_20250429":
|
|
208
|
+
betas.add("computer-use-2025-01-24");
|
|
209
|
+
anthropicTools2.push({
|
|
210
|
+
name: "str_replace_based_edit_tool",
|
|
211
|
+
type: "text_editor_20250429"
|
|
212
|
+
});
|
|
213
|
+
break;
|
|
99
214
|
case "anthropic.bash_20250124":
|
|
100
215
|
betas.add("computer-use-2025-01-24");
|
|
101
216
|
anthropicTools2.push({
|
|
102
|
-
name:
|
|
217
|
+
name: "bash",
|
|
103
218
|
type: "bash_20250124"
|
|
104
219
|
});
|
|
105
220
|
break;
|
|
106
221
|
case "anthropic.bash_20241022":
|
|
107
222
|
betas.add("computer-use-2024-10-22");
|
|
108
223
|
anthropicTools2.push({
|
|
109
|
-
name:
|
|
224
|
+
name: "bash",
|
|
110
225
|
type: "bash_20241022"
|
|
111
226
|
});
|
|
112
227
|
break;
|
|
228
|
+
case "anthropic.web_search_20250305": {
|
|
229
|
+
const args = webSearch_20250305ArgsSchema.parse(tool.args);
|
|
230
|
+
anthropicTools2.push({
|
|
231
|
+
type: "web_search_20250305",
|
|
232
|
+
name: "web_search",
|
|
233
|
+
max_uses: args.maxUses,
|
|
234
|
+
allowed_domains: args.allowedDomains,
|
|
235
|
+
blocked_domains: args.blockedDomains,
|
|
236
|
+
user_location: args.userLocation
|
|
237
|
+
});
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
113
240
|
default:
|
|
114
241
|
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
115
242
|
break;
|
|
@@ -123,7 +250,7 @@ function prepareTools({
|
|
|
123
250
|
if (toolChoice == null) {
|
|
124
251
|
return {
|
|
125
252
|
tools: anthropicTools2,
|
|
126
|
-
toolChoice: void 0,
|
|
253
|
+
toolChoice: disableParallelToolUse ? { type: "auto", disable_parallel_tool_use: disableParallelToolUse } : void 0,
|
|
127
254
|
toolWarnings,
|
|
128
255
|
betas
|
|
129
256
|
};
|
|
@@ -133,14 +260,20 @@ function prepareTools({
|
|
|
133
260
|
case "auto":
|
|
134
261
|
return {
|
|
135
262
|
tools: anthropicTools2,
|
|
136
|
-
toolChoice: {
|
|
263
|
+
toolChoice: {
|
|
264
|
+
type: "auto",
|
|
265
|
+
disable_parallel_tool_use: disableParallelToolUse
|
|
266
|
+
},
|
|
137
267
|
toolWarnings,
|
|
138
268
|
betas
|
|
139
269
|
};
|
|
140
270
|
case "required":
|
|
141
271
|
return {
|
|
142
272
|
tools: anthropicTools2,
|
|
143
|
-
toolChoice: {
|
|
273
|
+
toolChoice: {
|
|
274
|
+
type: "any",
|
|
275
|
+
disable_parallel_tool_use: disableParallelToolUse
|
|
276
|
+
},
|
|
144
277
|
toolWarnings,
|
|
145
278
|
betas
|
|
146
279
|
};
|
|
@@ -149,7 +282,11 @@ function prepareTools({
|
|
|
149
282
|
case "tool":
|
|
150
283
|
return {
|
|
151
284
|
tools: anthropicTools2,
|
|
152
|
-
toolChoice: {
|
|
285
|
+
toolChoice: {
|
|
286
|
+
type: "tool",
|
|
287
|
+
name: toolChoice.toolName,
|
|
288
|
+
disable_parallel_tool_use: disableParallelToolUse
|
|
289
|
+
},
|
|
153
290
|
toolWarnings,
|
|
154
291
|
betas
|
|
155
292
|
};
|
|
@@ -166,22 +303,52 @@ function prepareTools({
|
|
|
166
303
|
import {
|
|
167
304
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
168
305
|
} from "@ai-sdk/provider";
|
|
169
|
-
import { convertToBase64 } from "@ai-sdk/provider-utils";
|
|
170
|
-
function
|
|
306
|
+
import { convertToBase64, parseProviderOptions } from "@ai-sdk/provider-utils";
|
|
307
|
+
function convertToString(data) {
|
|
308
|
+
if (typeof data === "string") {
|
|
309
|
+
return Buffer.from(data, "base64").toString("utf-8");
|
|
310
|
+
}
|
|
311
|
+
if (data instanceof Uint8Array) {
|
|
312
|
+
return new TextDecoder().decode(data);
|
|
313
|
+
}
|
|
314
|
+
if (data instanceof URL) {
|
|
315
|
+
throw new UnsupportedFunctionalityError2({
|
|
316
|
+
functionality: "URL-based text documents are not supported for citations"
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
throw new UnsupportedFunctionalityError2({
|
|
320
|
+
functionality: `unsupported data type for text documents: ${typeof data}`
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
async function convertToAnthropicMessagesPrompt({
|
|
171
324
|
prompt,
|
|
172
325
|
sendReasoning,
|
|
173
326
|
warnings
|
|
174
327
|
}) {
|
|
175
|
-
var _a, _b, _c;
|
|
328
|
+
var _a, _b, _c, _d, _e;
|
|
176
329
|
const betas = /* @__PURE__ */ new Set();
|
|
177
330
|
const blocks = groupIntoBlocks(prompt);
|
|
178
331
|
let system = void 0;
|
|
179
332
|
const messages = [];
|
|
180
|
-
function
|
|
181
|
-
var _a2;
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
|
|
333
|
+
async function shouldEnableCitations(providerMetadata) {
|
|
334
|
+
var _a2, _b2;
|
|
335
|
+
const anthropicOptions = await parseProviderOptions({
|
|
336
|
+
provider: "anthropic",
|
|
337
|
+
providerOptions: providerMetadata,
|
|
338
|
+
schema: anthropicFilePartProviderOptions
|
|
339
|
+
});
|
|
340
|
+
return (_b2 = (_a2 = anthropicOptions == null ? void 0 : anthropicOptions.citations) == null ? void 0 : _a2.enabled) != null ? _b2 : false;
|
|
341
|
+
}
|
|
342
|
+
async function getDocumentMetadata(providerMetadata) {
|
|
343
|
+
const anthropicOptions = await parseProviderOptions({
|
|
344
|
+
provider: "anthropic",
|
|
345
|
+
providerOptions: providerMetadata,
|
|
346
|
+
schema: anthropicFilePartProviderOptions
|
|
347
|
+
});
|
|
348
|
+
return {
|
|
349
|
+
title: anthropicOptions == null ? void 0 : anthropicOptions.title,
|
|
350
|
+
context: anthropicOptions == null ? void 0 : anthropicOptions.context
|
|
351
|
+
};
|
|
185
352
|
}
|
|
186
353
|
for (let i = 0; i < blocks.length; i++) {
|
|
187
354
|
const block = blocks[i];
|
|
@@ -236,6 +403,12 @@ function convertToAnthropicMessagesPrompt({
|
|
|
236
403
|
});
|
|
237
404
|
} else if (part.mediaType === "application/pdf") {
|
|
238
405
|
betas.add("pdfs-2024-09-25");
|
|
406
|
+
const enableCitations = await shouldEnableCitations(
|
|
407
|
+
part.providerOptions
|
|
408
|
+
);
|
|
409
|
+
const metadata = await getDocumentMetadata(
|
|
410
|
+
part.providerOptions
|
|
411
|
+
);
|
|
239
412
|
anthropicContent.push({
|
|
240
413
|
type: "document",
|
|
241
414
|
source: part.data instanceof URL ? {
|
|
@@ -246,6 +419,35 @@ function convertToAnthropicMessagesPrompt({
|
|
|
246
419
|
media_type: "application/pdf",
|
|
247
420
|
data: convertToBase64(part.data)
|
|
248
421
|
},
|
|
422
|
+
title: (_b = metadata.title) != null ? _b : part.filename,
|
|
423
|
+
...metadata.context && { context: metadata.context },
|
|
424
|
+
...enableCitations && {
|
|
425
|
+
citations: { enabled: true }
|
|
426
|
+
},
|
|
427
|
+
cache_control: cacheControl
|
|
428
|
+
});
|
|
429
|
+
} else if (part.mediaType === "text/plain") {
|
|
430
|
+
const enableCitations = await shouldEnableCitations(
|
|
431
|
+
part.providerOptions
|
|
432
|
+
);
|
|
433
|
+
const metadata = await getDocumentMetadata(
|
|
434
|
+
part.providerOptions
|
|
435
|
+
);
|
|
436
|
+
anthropicContent.push({
|
|
437
|
+
type: "document",
|
|
438
|
+
source: part.data instanceof URL ? {
|
|
439
|
+
type: "url",
|
|
440
|
+
url: part.data.toString()
|
|
441
|
+
} : {
|
|
442
|
+
type: "text",
|
|
443
|
+
media_type: "text/plain",
|
|
444
|
+
data: convertToString(part.data)
|
|
445
|
+
},
|
|
446
|
+
title: (_c = metadata.title) != null ? _c : part.filename,
|
|
447
|
+
...metadata.context && { context: metadata.context },
|
|
448
|
+
...enableCitations && {
|
|
449
|
+
citations: { enabled: true }
|
|
450
|
+
},
|
|
249
451
|
cache_control: cacheControl
|
|
250
452
|
});
|
|
251
453
|
} else {
|
|
@@ -263,33 +465,53 @@ function convertToAnthropicMessagesPrompt({
|
|
|
263
465
|
for (let i2 = 0; i2 < content.length; i2++) {
|
|
264
466
|
const part = content[i2];
|
|
265
467
|
const isLastPart = i2 === content.length - 1;
|
|
266
|
-
const cacheControl = (
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
text:
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
468
|
+
const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
469
|
+
const output = part.output;
|
|
470
|
+
let contentValue;
|
|
471
|
+
switch (output.type) {
|
|
472
|
+
case "content":
|
|
473
|
+
contentValue = output.value.map((contentPart) => {
|
|
474
|
+
switch (contentPart.type) {
|
|
475
|
+
case "text":
|
|
476
|
+
return {
|
|
477
|
+
type: "text",
|
|
478
|
+
text: contentPart.text,
|
|
479
|
+
cache_control: void 0
|
|
480
|
+
};
|
|
481
|
+
case "media": {
|
|
482
|
+
if (contentPart.mediaType.startsWith("image/")) {
|
|
483
|
+
return {
|
|
484
|
+
type: "image",
|
|
485
|
+
source: {
|
|
486
|
+
type: "base64",
|
|
487
|
+
media_type: contentPart.mediaType,
|
|
488
|
+
data: contentPart.data
|
|
489
|
+
},
|
|
490
|
+
cache_control: void 0
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
throw new UnsupportedFunctionalityError2({
|
|
494
|
+
functionality: `media type: ${contentPart.mediaType}`
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
break;
|
|
500
|
+
case "text":
|
|
501
|
+
case "error-text":
|
|
502
|
+
contentValue = output.value;
|
|
503
|
+
break;
|
|
504
|
+
case "json":
|
|
505
|
+
case "error-json":
|
|
506
|
+
default:
|
|
507
|
+
contentValue = JSON.stringify(output.value);
|
|
508
|
+
break;
|
|
509
|
+
}
|
|
288
510
|
anthropicContent.push({
|
|
289
511
|
type: "tool_result",
|
|
290
512
|
tool_use_id: part.toolCallId,
|
|
291
|
-
content:
|
|
292
|
-
is_error:
|
|
513
|
+
content: contentValue,
|
|
514
|
+
is_error: output.type === "error-text" || output.type === "error-json" ? true : void 0,
|
|
293
515
|
cache_control: cacheControl
|
|
294
516
|
});
|
|
295
517
|
}
|
|
@@ -313,7 +535,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
313
535
|
for (let k = 0; k < content.length; k++) {
|
|
314
536
|
const part = content[k];
|
|
315
537
|
const isLastContentPart = k === content.length - 1;
|
|
316
|
-
const cacheControl = (
|
|
538
|
+
const cacheControl = (_e = getCacheControl(part.providerOptions)) != null ? _e : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
|
|
317
539
|
switch (part.type) {
|
|
318
540
|
case "text": {
|
|
319
541
|
anthropicContent.push({
|
|
@@ -330,12 +552,37 @@ function convertToAnthropicMessagesPrompt({
|
|
|
330
552
|
}
|
|
331
553
|
case "reasoning": {
|
|
332
554
|
if (sendReasoning) {
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
cache_control: cacheControl
|
|
555
|
+
const reasoningMetadata = await parseProviderOptions({
|
|
556
|
+
provider: "anthropic",
|
|
557
|
+
providerOptions: part.providerOptions,
|
|
558
|
+
schema: anthropicReasoningMetadataSchema
|
|
338
559
|
});
|
|
560
|
+
if (reasoningMetadata != null) {
|
|
561
|
+
if (reasoningMetadata.signature != null) {
|
|
562
|
+
anthropicContent.push({
|
|
563
|
+
type: "thinking",
|
|
564
|
+
thinking: part.text,
|
|
565
|
+
signature: reasoningMetadata.signature,
|
|
566
|
+
cache_control: cacheControl
|
|
567
|
+
});
|
|
568
|
+
} else if (reasoningMetadata.redactedData != null) {
|
|
569
|
+
anthropicContent.push({
|
|
570
|
+
type: "redacted_thinking",
|
|
571
|
+
data: reasoningMetadata.redactedData,
|
|
572
|
+
cache_control: cacheControl
|
|
573
|
+
});
|
|
574
|
+
} else {
|
|
575
|
+
warnings.push({
|
|
576
|
+
type: "other",
|
|
577
|
+
message: "unsupported reasoning metadata"
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
} else {
|
|
581
|
+
warnings.push({
|
|
582
|
+
type: "other",
|
|
583
|
+
message: "unsupported reasoning metadata"
|
|
584
|
+
});
|
|
585
|
+
}
|
|
339
586
|
} else {
|
|
340
587
|
warnings.push({
|
|
341
588
|
type: "other",
|
|
@@ -344,24 +591,66 @@ function convertToAnthropicMessagesPrompt({
|
|
|
344
591
|
}
|
|
345
592
|
break;
|
|
346
593
|
}
|
|
347
|
-
case "redacted-reasoning": {
|
|
348
|
-
anthropicContent.push({
|
|
349
|
-
type: "redacted_thinking",
|
|
350
|
-
data: part.data,
|
|
351
|
-
cache_control: cacheControl
|
|
352
|
-
});
|
|
353
|
-
break;
|
|
354
|
-
}
|
|
355
594
|
case "tool-call": {
|
|
595
|
+
if (part.providerExecuted) {
|
|
596
|
+
if (part.toolName === "web_search") {
|
|
597
|
+
anthropicContent.push({
|
|
598
|
+
type: "server_tool_use",
|
|
599
|
+
id: part.toolCallId,
|
|
600
|
+
name: "web_search",
|
|
601
|
+
input: part.input,
|
|
602
|
+
cache_control: cacheControl
|
|
603
|
+
});
|
|
604
|
+
break;
|
|
605
|
+
}
|
|
606
|
+
warnings.push({
|
|
607
|
+
type: "other",
|
|
608
|
+
message: `provider executed tool call for tool ${part.toolName} is not supported`
|
|
609
|
+
});
|
|
610
|
+
break;
|
|
611
|
+
}
|
|
356
612
|
anthropicContent.push({
|
|
357
613
|
type: "tool_use",
|
|
358
614
|
id: part.toolCallId,
|
|
359
615
|
name: part.toolName,
|
|
360
|
-
input: part.
|
|
616
|
+
input: part.input,
|
|
361
617
|
cache_control: cacheControl
|
|
362
618
|
});
|
|
363
619
|
break;
|
|
364
620
|
}
|
|
621
|
+
case "tool-result": {
|
|
622
|
+
if (part.toolName === "web_search") {
|
|
623
|
+
const output = part.output;
|
|
624
|
+
if (output.type !== "json") {
|
|
625
|
+
warnings.push({
|
|
626
|
+
type: "other",
|
|
627
|
+
message: `provider executed tool result output type ${output.type} for tool ${part.toolName} is not supported`
|
|
628
|
+
});
|
|
629
|
+
break;
|
|
630
|
+
}
|
|
631
|
+
const webSearchOutput = webSearch_20250305OutputSchema.parse(
|
|
632
|
+
output.value
|
|
633
|
+
);
|
|
634
|
+
anthropicContent.push({
|
|
635
|
+
type: "web_search_tool_result",
|
|
636
|
+
tool_use_id: part.toolCallId,
|
|
637
|
+
content: webSearchOutput.map((result) => ({
|
|
638
|
+
url: result.url,
|
|
639
|
+
title: result.title,
|
|
640
|
+
page_age: result.pageAge,
|
|
641
|
+
encrypted_content: result.encryptedContent,
|
|
642
|
+
type: result.type
|
|
643
|
+
})),
|
|
644
|
+
cache_control: cacheControl
|
|
645
|
+
});
|
|
646
|
+
break;
|
|
647
|
+
}
|
|
648
|
+
warnings.push({
|
|
649
|
+
type: "other",
|
|
650
|
+
message: `provider executed tool result for tool ${part.toolName} is not supported`
|
|
651
|
+
});
|
|
652
|
+
break;
|
|
653
|
+
}
|
|
365
654
|
}
|
|
366
655
|
}
|
|
367
656
|
}
|
|
@@ -427,13 +716,16 @@ function groupIntoBlocks(prompt) {
|
|
|
427
716
|
}
|
|
428
717
|
|
|
429
718
|
// src/map-anthropic-stop-reason.ts
|
|
430
|
-
function mapAnthropicStopReason(
|
|
719
|
+
function mapAnthropicStopReason({
|
|
720
|
+
finishReason,
|
|
721
|
+
isJsonResponseFromTool
|
|
722
|
+
}) {
|
|
431
723
|
switch (finishReason) {
|
|
432
724
|
case "end_turn":
|
|
433
725
|
case "stop_sequence":
|
|
434
726
|
return "stop";
|
|
435
727
|
case "tool_use":
|
|
436
|
-
return "tool-calls";
|
|
728
|
+
return isJsonResponseFromTool ? "stop" : "tool-calls";
|
|
437
729
|
case "max_tokens":
|
|
438
730
|
return "length";
|
|
439
731
|
default:
|
|
@@ -442,12 +734,86 @@ function mapAnthropicStopReason(finishReason) {
|
|
|
442
734
|
}
|
|
443
735
|
|
|
444
736
|
// src/anthropic-messages-language-model.ts
|
|
737
|
+
var citationSchemas = {
|
|
738
|
+
webSearchResult: z4.object({
|
|
739
|
+
type: z4.literal("web_search_result_location"),
|
|
740
|
+
cited_text: z4.string(),
|
|
741
|
+
url: z4.string(),
|
|
742
|
+
title: z4.string(),
|
|
743
|
+
encrypted_index: z4.string()
|
|
744
|
+
}),
|
|
745
|
+
pageLocation: z4.object({
|
|
746
|
+
type: z4.literal("page_location"),
|
|
747
|
+
cited_text: z4.string(),
|
|
748
|
+
document_index: z4.number(),
|
|
749
|
+
document_title: z4.string().nullable(),
|
|
750
|
+
start_page_number: z4.number(),
|
|
751
|
+
end_page_number: z4.number()
|
|
752
|
+
}),
|
|
753
|
+
charLocation: z4.object({
|
|
754
|
+
type: z4.literal("char_location"),
|
|
755
|
+
cited_text: z4.string(),
|
|
756
|
+
document_index: z4.number(),
|
|
757
|
+
document_title: z4.string().nullable(),
|
|
758
|
+
start_char_index: z4.number(),
|
|
759
|
+
end_char_index: z4.number()
|
|
760
|
+
})
|
|
761
|
+
};
|
|
762
|
+
var citationSchema = z4.discriminatedUnion("type", [
|
|
763
|
+
citationSchemas.webSearchResult,
|
|
764
|
+
citationSchemas.pageLocation,
|
|
765
|
+
citationSchemas.charLocation
|
|
766
|
+
]);
|
|
767
|
+
var documentCitationSchema = z4.discriminatedUnion("type", [
|
|
768
|
+
citationSchemas.pageLocation,
|
|
769
|
+
citationSchemas.charLocation
|
|
770
|
+
]);
|
|
771
|
+
function processCitation(citation, citationDocuments, generateId3, onSource) {
|
|
772
|
+
if (citation.type === "page_location" || citation.type === "char_location") {
|
|
773
|
+
const source = createCitationSource(
|
|
774
|
+
citation,
|
|
775
|
+
citationDocuments,
|
|
776
|
+
generateId3
|
|
777
|
+
);
|
|
778
|
+
if (source) {
|
|
779
|
+
onSource(source);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
function createCitationSource(citation, citationDocuments, generateId3) {
|
|
784
|
+
var _a;
|
|
785
|
+
const documentInfo = citationDocuments[citation.document_index];
|
|
786
|
+
if (!documentInfo) {
|
|
787
|
+
return null;
|
|
788
|
+
}
|
|
789
|
+
const providerMetadata = citation.type === "page_location" ? {
|
|
790
|
+
citedText: citation.cited_text,
|
|
791
|
+
startPageNumber: citation.start_page_number,
|
|
792
|
+
endPageNumber: citation.end_page_number
|
|
793
|
+
} : {
|
|
794
|
+
citedText: citation.cited_text,
|
|
795
|
+
startCharIndex: citation.start_char_index,
|
|
796
|
+
endCharIndex: citation.end_char_index
|
|
797
|
+
};
|
|
798
|
+
return {
|
|
799
|
+
type: "source",
|
|
800
|
+
sourceType: "document",
|
|
801
|
+
id: generateId3(),
|
|
802
|
+
mediaType: documentInfo.mediaType,
|
|
803
|
+
title: (_a = citation.document_title) != null ? _a : documentInfo.title,
|
|
804
|
+
filename: documentInfo.filename,
|
|
805
|
+
providerMetadata: {
|
|
806
|
+
anthropic: providerMetadata
|
|
807
|
+
}
|
|
808
|
+
};
|
|
809
|
+
}
|
|
445
810
|
var AnthropicMessagesLanguageModel = class {
|
|
446
|
-
constructor(modelId,
|
|
811
|
+
constructor(modelId, config) {
|
|
447
812
|
this.specificationVersion = "v2";
|
|
813
|
+
var _a;
|
|
448
814
|
this.modelId = modelId;
|
|
449
|
-
this.settings = settings;
|
|
450
815
|
this.config = config;
|
|
816
|
+
this.generateId = (_a = config.generateId) != null ? _a : generateId;
|
|
451
817
|
}
|
|
452
818
|
supportsUrl(url) {
|
|
453
819
|
return url.protocol === "https:";
|
|
@@ -455,9 +821,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
455
821
|
get provider() {
|
|
456
822
|
return this.config.provider;
|
|
457
823
|
}
|
|
458
|
-
|
|
824
|
+
get supportedUrls() {
|
|
459
825
|
var _a, _b, _c;
|
|
460
|
-
return (_c = (_b = (_a = this.config).
|
|
826
|
+
return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
|
|
461
827
|
}
|
|
462
828
|
async getArgs({
|
|
463
829
|
prompt,
|
|
@@ -495,22 +861,36 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
495
861
|
setting: "seed"
|
|
496
862
|
});
|
|
497
863
|
}
|
|
498
|
-
if (responseFormat
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
864
|
+
if ((responseFormat == null ? void 0 : responseFormat.type) === "json") {
|
|
865
|
+
if (responseFormat.schema == null) {
|
|
866
|
+
warnings.push({
|
|
867
|
+
type: "unsupported-setting",
|
|
868
|
+
setting: "responseFormat",
|
|
869
|
+
details: "JSON response format requires a schema. The response format is ignored."
|
|
870
|
+
});
|
|
871
|
+
} else if (tools != null) {
|
|
872
|
+
warnings.push({
|
|
873
|
+
type: "unsupported-setting",
|
|
874
|
+
setting: "tools",
|
|
875
|
+
details: "JSON response format does not support tools. The provided tools are ignored."
|
|
876
|
+
});
|
|
877
|
+
}
|
|
504
878
|
}
|
|
505
|
-
const
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
879
|
+
const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
|
|
880
|
+
type: "function",
|
|
881
|
+
name: "json",
|
|
882
|
+
description: "Respond with a JSON object.",
|
|
883
|
+
inputSchema: responseFormat.schema
|
|
884
|
+
} : void 0;
|
|
885
|
+
const anthropicOptions = await parseProviderOptions2({
|
|
511
886
|
provider: "anthropic",
|
|
512
887
|
providerOptions,
|
|
513
|
-
schema:
|
|
888
|
+
schema: anthropicProviderOptions
|
|
889
|
+
});
|
|
890
|
+
const { prompt: messagesPrompt, betas: messagesBetas } = await convertToAnthropicMessagesPrompt({
|
|
891
|
+
prompt,
|
|
892
|
+
sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
|
|
893
|
+
warnings
|
|
514
894
|
});
|
|
515
895
|
const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
|
|
516
896
|
const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
|
|
@@ -568,7 +948,17 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
568
948
|
toolChoice: anthropicToolChoice,
|
|
569
949
|
toolWarnings,
|
|
570
950
|
betas: toolsBetas
|
|
571
|
-
} = prepareTools(
|
|
951
|
+
} = prepareTools(
|
|
952
|
+
jsonResponseTool != null ? {
|
|
953
|
+
tools: [jsonResponseTool],
|
|
954
|
+
toolChoice: { type: "tool", toolName: jsonResponseTool.name },
|
|
955
|
+
disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
|
|
956
|
+
} : {
|
|
957
|
+
tools: tools != null ? tools : [],
|
|
958
|
+
toolChoice,
|
|
959
|
+
disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
|
|
960
|
+
}
|
|
961
|
+
);
|
|
572
962
|
return {
|
|
573
963
|
args: {
|
|
574
964
|
...baseArgs,
|
|
@@ -576,7 +966,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
576
966
|
tool_choice: anthropicToolChoice
|
|
577
967
|
},
|
|
578
968
|
warnings: [...warnings, ...toolWarnings],
|
|
579
|
-
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
|
|
969
|
+
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
|
|
970
|
+
usesJsonResponseTool: jsonResponseTool != null
|
|
580
971
|
};
|
|
581
972
|
}
|
|
582
973
|
async getHeaders({
|
|
@@ -597,9 +988,33 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
597
988
|
var _a, _b, _c;
|
|
598
989
|
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
599
990
|
}
|
|
991
|
+
extractCitationDocuments(prompt) {
|
|
992
|
+
const isCitationPart = (part) => {
|
|
993
|
+
var _a, _b;
|
|
994
|
+
if (part.type !== "file") {
|
|
995
|
+
return false;
|
|
996
|
+
}
|
|
997
|
+
if (part.mediaType !== "application/pdf" && part.mediaType !== "text/plain") {
|
|
998
|
+
return false;
|
|
999
|
+
}
|
|
1000
|
+
const anthropic2 = (_a = part.providerOptions) == null ? void 0 : _a.anthropic;
|
|
1001
|
+
const citationsConfig = anthropic2 == null ? void 0 : anthropic2.citations;
|
|
1002
|
+
return (_b = citationsConfig == null ? void 0 : citationsConfig.enabled) != null ? _b : false;
|
|
1003
|
+
};
|
|
1004
|
+
return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(isCitationPart).map((part) => {
|
|
1005
|
+
var _a;
|
|
1006
|
+
const filePart = part;
|
|
1007
|
+
return {
|
|
1008
|
+
title: (_a = filePart.filename) != null ? _a : "Untitled Document",
|
|
1009
|
+
filename: filePart.filename,
|
|
1010
|
+
mediaType: filePart.mediaType
|
|
1011
|
+
};
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
600
1014
|
async doGenerate(options) {
|
|
601
|
-
var _a, _b, _c, _d;
|
|
602
|
-
const { args, warnings, betas } = await this.getArgs(options);
|
|
1015
|
+
var _a, _b, _c, _d, _e;
|
|
1016
|
+
const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
|
|
1017
|
+
const citationDocuments = this.extractCitationDocuments(options.prompt);
|
|
603
1018
|
const {
|
|
604
1019
|
responseHeaders,
|
|
605
1020
|
value: response,
|
|
@@ -619,67 +1034,152 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
619
1034
|
for (const part of response.content) {
|
|
620
1035
|
switch (part.type) {
|
|
621
1036
|
case "text": {
|
|
622
|
-
|
|
1037
|
+
if (!usesJsonResponseTool) {
|
|
1038
|
+
content.push({ type: "text", text: part.text });
|
|
1039
|
+
if (part.citations) {
|
|
1040
|
+
for (const citation of part.citations) {
|
|
1041
|
+
processCitation(
|
|
1042
|
+
citation,
|
|
1043
|
+
citationDocuments,
|
|
1044
|
+
this.generateId,
|
|
1045
|
+
(source) => content.push(source)
|
|
1046
|
+
);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
623
1050
|
break;
|
|
624
1051
|
}
|
|
625
1052
|
case "thinking": {
|
|
626
1053
|
content.push({
|
|
627
1054
|
type: "reasoning",
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
signature: part.signature
|
|
1055
|
+
text: part.thinking,
|
|
1056
|
+
providerMetadata: {
|
|
1057
|
+
anthropic: {
|
|
1058
|
+
signature: part.signature
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
635
1061
|
});
|
|
636
1062
|
break;
|
|
637
1063
|
}
|
|
638
1064
|
case "redacted_thinking": {
|
|
639
1065
|
content.push({
|
|
640
1066
|
type: "reasoning",
|
|
641
|
-
|
|
642
|
-
|
|
1067
|
+
text: "",
|
|
1068
|
+
providerMetadata: {
|
|
1069
|
+
anthropic: {
|
|
1070
|
+
redactedData: part.data
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
643
1073
|
});
|
|
644
1074
|
break;
|
|
645
1075
|
}
|
|
646
1076
|
case "tool_use": {
|
|
647
|
-
content.push(
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
1077
|
+
content.push(
|
|
1078
|
+
// when a json response tool is used, the tool call becomes the text:
|
|
1079
|
+
usesJsonResponseTool ? {
|
|
1080
|
+
type: "text",
|
|
1081
|
+
text: JSON.stringify(part.input)
|
|
1082
|
+
} : {
|
|
1083
|
+
type: "tool-call",
|
|
1084
|
+
toolCallId: part.id,
|
|
1085
|
+
toolName: part.name,
|
|
1086
|
+
input: JSON.stringify(part.input)
|
|
1087
|
+
}
|
|
1088
|
+
);
|
|
1089
|
+
break;
|
|
1090
|
+
}
|
|
1091
|
+
case "server_tool_use": {
|
|
1092
|
+
if (part.name === "web_search") {
|
|
1093
|
+
content.push({
|
|
1094
|
+
type: "tool-call",
|
|
1095
|
+
toolCallId: part.id,
|
|
1096
|
+
toolName: part.name,
|
|
1097
|
+
input: JSON.stringify(part.input),
|
|
1098
|
+
providerExecuted: true
|
|
1099
|
+
});
|
|
1100
|
+
}
|
|
1101
|
+
break;
|
|
1102
|
+
}
|
|
1103
|
+
case "web_search_tool_result": {
|
|
1104
|
+
if (Array.isArray(part.content)) {
|
|
1105
|
+
content.push({
|
|
1106
|
+
type: "tool-result",
|
|
1107
|
+
toolCallId: part.tool_use_id,
|
|
1108
|
+
toolName: "web_search",
|
|
1109
|
+
result: part.content.map((result) => {
|
|
1110
|
+
var _a2;
|
|
1111
|
+
return {
|
|
1112
|
+
url: result.url,
|
|
1113
|
+
title: result.title,
|
|
1114
|
+
pageAge: (_a2 = result.page_age) != null ? _a2 : null,
|
|
1115
|
+
encryptedContent: result.encrypted_content,
|
|
1116
|
+
type: result.type
|
|
1117
|
+
};
|
|
1118
|
+
}),
|
|
1119
|
+
providerExecuted: true
|
|
1120
|
+
});
|
|
1121
|
+
for (const result of part.content) {
|
|
1122
|
+
content.push({
|
|
1123
|
+
type: "source",
|
|
1124
|
+
sourceType: "url",
|
|
1125
|
+
id: this.generateId(),
|
|
1126
|
+
url: result.url,
|
|
1127
|
+
title: result.title,
|
|
1128
|
+
providerMetadata: {
|
|
1129
|
+
anthropic: {
|
|
1130
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
});
|
|
1134
|
+
}
|
|
1135
|
+
} else {
|
|
1136
|
+
content.push({
|
|
1137
|
+
type: "tool-result",
|
|
1138
|
+
toolCallId: part.tool_use_id,
|
|
1139
|
+
toolName: "web_search",
|
|
1140
|
+
isError: true,
|
|
1141
|
+
result: {
|
|
1142
|
+
type: "web_search_tool_result_error",
|
|
1143
|
+
errorCode: part.content.error_code
|
|
1144
|
+
},
|
|
1145
|
+
providerExecuted: true
|
|
1146
|
+
});
|
|
1147
|
+
}
|
|
654
1148
|
break;
|
|
655
1149
|
}
|
|
656
1150
|
}
|
|
657
1151
|
}
|
|
658
1152
|
return {
|
|
659
1153
|
content,
|
|
660
|
-
finishReason: mapAnthropicStopReason(
|
|
1154
|
+
finishReason: mapAnthropicStopReason({
|
|
1155
|
+
finishReason: response.stop_reason,
|
|
1156
|
+
isJsonResponseFromTool: usesJsonResponseTool
|
|
1157
|
+
}),
|
|
661
1158
|
usage: {
|
|
662
1159
|
inputTokens: response.usage.input_tokens,
|
|
663
|
-
outputTokens: response.usage.output_tokens
|
|
1160
|
+
outputTokens: response.usage.output_tokens,
|
|
1161
|
+
totalTokens: response.usage.input_tokens + response.usage.output_tokens,
|
|
1162
|
+
cachedInputTokens: (_b = response.usage.cache_read_input_tokens) != null ? _b : void 0
|
|
664
1163
|
},
|
|
665
1164
|
request: { body: args },
|
|
666
1165
|
response: {
|
|
667
|
-
id: (
|
|
668
|
-
modelId: (
|
|
1166
|
+
id: (_c = response.id) != null ? _c : void 0,
|
|
1167
|
+
modelId: (_d = response.model) != null ? _d : void 0,
|
|
669
1168
|
headers: responseHeaders,
|
|
670
1169
|
body: rawResponse
|
|
671
1170
|
},
|
|
672
1171
|
warnings,
|
|
673
1172
|
providerMetadata: {
|
|
674
1173
|
anthropic: {
|
|
675
|
-
|
|
676
|
-
|
|
1174
|
+
usage: response.usage,
|
|
1175
|
+
cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null
|
|
677
1176
|
}
|
|
678
1177
|
}
|
|
679
1178
|
};
|
|
680
1179
|
}
|
|
681
1180
|
async doStream(options) {
|
|
682
|
-
const { args, warnings, betas } = await this.getArgs(options);
|
|
1181
|
+
const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
|
|
1182
|
+
const citationDocuments = this.extractCitationDocuments(options.prompt);
|
|
683
1183
|
const body = { ...args, stream: true };
|
|
684
1184
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
685
1185
|
url: this.buildRequestUrl(true),
|
|
@@ -695,11 +1195,13 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
695
1195
|
let finishReason = "unknown";
|
|
696
1196
|
const usage = {
|
|
697
1197
|
inputTokens: void 0,
|
|
698
|
-
outputTokens: void 0
|
|
1198
|
+
outputTokens: void 0,
|
|
1199
|
+
totalTokens: void 0
|
|
699
1200
|
};
|
|
700
|
-
const
|
|
1201
|
+
const contentBlocks = {};
|
|
701
1202
|
let providerMetadata = void 0;
|
|
702
1203
|
let blockType = void 0;
|
|
1204
|
+
const generateId3 = this.generateId;
|
|
703
1205
|
return {
|
|
704
1206
|
stream: response.pipeThrough(
|
|
705
1207
|
new TransformStream({
|
|
@@ -707,7 +1209,10 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
707
1209
|
controller.enqueue({ type: "stream-start", warnings });
|
|
708
1210
|
},
|
|
709
1211
|
transform(chunk, controller) {
|
|
710
|
-
var _a, _b, _c, _d;
|
|
1212
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
1213
|
+
if (options.includeRawChunks) {
|
|
1214
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
1215
|
+
}
|
|
711
1216
|
if (!chunk.success) {
|
|
712
1217
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
713
1218
|
return;
|
|
@@ -721,24 +1226,115 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
721
1226
|
const contentBlockType = value.content_block.type;
|
|
722
1227
|
blockType = contentBlockType;
|
|
723
1228
|
switch (contentBlockType) {
|
|
724
|
-
case "text":
|
|
1229
|
+
case "text": {
|
|
1230
|
+
contentBlocks[value.index] = { type: "text" };
|
|
1231
|
+
controller.enqueue({
|
|
1232
|
+
type: "text-start",
|
|
1233
|
+
id: String(value.index)
|
|
1234
|
+
});
|
|
1235
|
+
return;
|
|
1236
|
+
}
|
|
725
1237
|
case "thinking": {
|
|
1238
|
+
contentBlocks[value.index] = { type: "reasoning" };
|
|
1239
|
+
controller.enqueue({
|
|
1240
|
+
type: "reasoning-start",
|
|
1241
|
+
id: String(value.index)
|
|
1242
|
+
});
|
|
726
1243
|
return;
|
|
727
1244
|
}
|
|
728
1245
|
case "redacted_thinking": {
|
|
1246
|
+
contentBlocks[value.index] = { type: "reasoning" };
|
|
729
1247
|
controller.enqueue({
|
|
730
|
-
type: "reasoning",
|
|
731
|
-
|
|
732
|
-
|
|
1248
|
+
type: "reasoning-start",
|
|
1249
|
+
id: String(value.index),
|
|
1250
|
+
providerMetadata: {
|
|
1251
|
+
anthropic: {
|
|
1252
|
+
redactedData: value.content_block.data
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
733
1255
|
});
|
|
734
1256
|
return;
|
|
735
1257
|
}
|
|
736
1258
|
case "tool_use": {
|
|
737
|
-
|
|
1259
|
+
contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
|
|
1260
|
+
type: "tool-call",
|
|
738
1261
|
toolCallId: value.content_block.id,
|
|
739
1262
|
toolName: value.content_block.name,
|
|
740
|
-
|
|
1263
|
+
input: ""
|
|
741
1264
|
};
|
|
1265
|
+
controller.enqueue(
|
|
1266
|
+
usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
|
|
1267
|
+
type: "tool-input-start",
|
|
1268
|
+
id: value.content_block.id,
|
|
1269
|
+
toolName: value.content_block.name
|
|
1270
|
+
}
|
|
1271
|
+
);
|
|
1272
|
+
return;
|
|
1273
|
+
}
|
|
1274
|
+
case "server_tool_use": {
|
|
1275
|
+
if (value.content_block.name === "web_search") {
|
|
1276
|
+
contentBlocks[value.index] = {
|
|
1277
|
+
type: "tool-call",
|
|
1278
|
+
toolCallId: value.content_block.id,
|
|
1279
|
+
toolName: value.content_block.name,
|
|
1280
|
+
input: "",
|
|
1281
|
+
providerExecuted: true
|
|
1282
|
+
};
|
|
1283
|
+
controller.enqueue({
|
|
1284
|
+
type: "tool-input-start",
|
|
1285
|
+
id: value.content_block.id,
|
|
1286
|
+
toolName: value.content_block.name,
|
|
1287
|
+
providerExecuted: true
|
|
1288
|
+
});
|
|
1289
|
+
}
|
|
1290
|
+
return;
|
|
1291
|
+
}
|
|
1292
|
+
case "web_search_tool_result": {
|
|
1293
|
+
const part = value.content_block;
|
|
1294
|
+
if (Array.isArray(part.content)) {
|
|
1295
|
+
controller.enqueue({
|
|
1296
|
+
type: "tool-result",
|
|
1297
|
+
toolCallId: part.tool_use_id,
|
|
1298
|
+
toolName: "web_search",
|
|
1299
|
+
result: part.content.map((result) => {
|
|
1300
|
+
var _a2;
|
|
1301
|
+
return {
|
|
1302
|
+
url: result.url,
|
|
1303
|
+
title: result.title,
|
|
1304
|
+
pageAge: (_a2 = result.page_age) != null ? _a2 : null,
|
|
1305
|
+
encryptedContent: result.encrypted_content,
|
|
1306
|
+
type: result.type
|
|
1307
|
+
};
|
|
1308
|
+
}),
|
|
1309
|
+
providerExecuted: true
|
|
1310
|
+
});
|
|
1311
|
+
for (const result of part.content) {
|
|
1312
|
+
controller.enqueue({
|
|
1313
|
+
type: "source",
|
|
1314
|
+
sourceType: "url",
|
|
1315
|
+
id: generateId3(),
|
|
1316
|
+
url: result.url,
|
|
1317
|
+
title: result.title,
|
|
1318
|
+
providerMetadata: {
|
|
1319
|
+
anthropic: {
|
|
1320
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
});
|
|
1324
|
+
}
|
|
1325
|
+
} else {
|
|
1326
|
+
controller.enqueue({
|
|
1327
|
+
type: "tool-result",
|
|
1328
|
+
toolCallId: part.tool_use_id,
|
|
1329
|
+
toolName: "web_search",
|
|
1330
|
+
isError: true,
|
|
1331
|
+
result: {
|
|
1332
|
+
type: "web_search_tool_result_error",
|
|
1333
|
+
errorCode: part.content.error_code
|
|
1334
|
+
},
|
|
1335
|
+
providerExecuted: true
|
|
1336
|
+
});
|
|
1337
|
+
}
|
|
742
1338
|
return;
|
|
743
1339
|
}
|
|
744
1340
|
default: {
|
|
@@ -750,16 +1346,34 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
750
1346
|
}
|
|
751
1347
|
}
|
|
752
1348
|
case "content_block_stop": {
|
|
753
|
-
if (
|
|
754
|
-
const contentBlock =
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
1349
|
+
if (contentBlocks[value.index] != null) {
|
|
1350
|
+
const contentBlock = contentBlocks[value.index];
|
|
1351
|
+
switch (contentBlock.type) {
|
|
1352
|
+
case "text": {
|
|
1353
|
+
controller.enqueue({
|
|
1354
|
+
type: "text-end",
|
|
1355
|
+
id: String(value.index)
|
|
1356
|
+
});
|
|
1357
|
+
break;
|
|
1358
|
+
}
|
|
1359
|
+
case "reasoning": {
|
|
1360
|
+
controller.enqueue({
|
|
1361
|
+
type: "reasoning-end",
|
|
1362
|
+
id: String(value.index)
|
|
1363
|
+
});
|
|
1364
|
+
break;
|
|
1365
|
+
}
|
|
1366
|
+
case "tool-call":
|
|
1367
|
+
if (!usesJsonResponseTool) {
|
|
1368
|
+
controller.enqueue({
|
|
1369
|
+
type: "tool-input-end",
|
|
1370
|
+
id: contentBlock.toolCallId
|
|
1371
|
+
});
|
|
1372
|
+
controller.enqueue(contentBlock);
|
|
1373
|
+
}
|
|
1374
|
+
break;
|
|
1375
|
+
}
|
|
1376
|
+
delete contentBlocks[value.index];
|
|
763
1377
|
}
|
|
764
1378
|
blockType = void 0;
|
|
765
1379
|
return;
|
|
@@ -768,40 +1382,72 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
768
1382
|
const deltaType = value.delta.type;
|
|
769
1383
|
switch (deltaType) {
|
|
770
1384
|
case "text_delta": {
|
|
1385
|
+
if (usesJsonResponseTool) {
|
|
1386
|
+
return;
|
|
1387
|
+
}
|
|
771
1388
|
controller.enqueue({
|
|
772
|
-
type: "text",
|
|
773
|
-
|
|
1389
|
+
type: "text-delta",
|
|
1390
|
+
id: String(value.index),
|
|
1391
|
+
delta: value.delta.text
|
|
774
1392
|
});
|
|
775
1393
|
return;
|
|
776
1394
|
}
|
|
777
1395
|
case "thinking_delta": {
|
|
778
1396
|
controller.enqueue({
|
|
779
|
-
type: "reasoning",
|
|
780
|
-
|
|
781
|
-
|
|
1397
|
+
type: "reasoning-delta",
|
|
1398
|
+
id: String(value.index),
|
|
1399
|
+
delta: value.delta.thinking
|
|
782
1400
|
});
|
|
783
1401
|
return;
|
|
784
1402
|
}
|
|
785
1403
|
case "signature_delta": {
|
|
786
1404
|
if (blockType === "thinking") {
|
|
787
1405
|
controller.enqueue({
|
|
788
|
-
type: "reasoning",
|
|
789
|
-
|
|
790
|
-
|
|
1406
|
+
type: "reasoning-delta",
|
|
1407
|
+
id: String(value.index),
|
|
1408
|
+
delta: "",
|
|
1409
|
+
providerMetadata: {
|
|
1410
|
+
anthropic: {
|
|
1411
|
+
signature: value.delta.signature
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
791
1414
|
});
|
|
792
1415
|
}
|
|
793
1416
|
return;
|
|
794
1417
|
}
|
|
795
1418
|
case "input_json_delta": {
|
|
796
|
-
const contentBlock =
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
1419
|
+
const contentBlock = contentBlocks[value.index];
|
|
1420
|
+
const delta = value.delta.partial_json;
|
|
1421
|
+
if (usesJsonResponseTool) {
|
|
1422
|
+
if ((contentBlock == null ? void 0 : contentBlock.type) !== "text") {
|
|
1423
|
+
return;
|
|
1424
|
+
}
|
|
1425
|
+
controller.enqueue({
|
|
1426
|
+
type: "text-delta",
|
|
1427
|
+
id: String(value.index),
|
|
1428
|
+
delta
|
|
1429
|
+
});
|
|
1430
|
+
} else {
|
|
1431
|
+
if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
|
|
1432
|
+
return;
|
|
1433
|
+
}
|
|
1434
|
+
controller.enqueue({
|
|
1435
|
+
type: "tool-input-delta",
|
|
1436
|
+
id: contentBlock.toolCallId,
|
|
1437
|
+
delta
|
|
1438
|
+
});
|
|
1439
|
+
contentBlock.input += delta;
|
|
1440
|
+
}
|
|
1441
|
+
return;
|
|
1442
|
+
}
|
|
1443
|
+
case "citations_delta": {
|
|
1444
|
+
const citation = value.delta.citation;
|
|
1445
|
+
processCitation(
|
|
1446
|
+
citation,
|
|
1447
|
+
citationDocuments,
|
|
1448
|
+
generateId3,
|
|
1449
|
+
(source) => controller.enqueue(source)
|
|
1450
|
+
);
|
|
805
1451
|
return;
|
|
806
1452
|
}
|
|
807
1453
|
default: {
|
|
@@ -814,23 +1460,27 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
814
1460
|
}
|
|
815
1461
|
case "message_start": {
|
|
816
1462
|
usage.inputTokens = value.message.usage.input_tokens;
|
|
817
|
-
usage.
|
|
1463
|
+
usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
|
|
818
1464
|
providerMetadata = {
|
|
819
1465
|
anthropic: {
|
|
820
|
-
|
|
821
|
-
|
|
1466
|
+
usage: value.message.usage,
|
|
1467
|
+
cacheCreationInputTokens: (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null
|
|
822
1468
|
}
|
|
823
1469
|
};
|
|
824
1470
|
controller.enqueue({
|
|
825
1471
|
type: "response-metadata",
|
|
826
|
-
id: (
|
|
827
|
-
modelId: (
|
|
1472
|
+
id: (_d = value.message.id) != null ? _d : void 0,
|
|
1473
|
+
modelId: (_e = value.message.model) != null ? _e : void 0
|
|
828
1474
|
});
|
|
829
1475
|
return;
|
|
830
1476
|
}
|
|
831
1477
|
case "message_delta": {
|
|
832
1478
|
usage.outputTokens = value.usage.output_tokens;
|
|
833
|
-
|
|
1479
|
+
usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
|
|
1480
|
+
finishReason = mapAnthropicStopReason({
|
|
1481
|
+
finishReason: value.delta.stop_reason,
|
|
1482
|
+
isJsonResponseFromTool: usesJsonResponseTool
|
|
1483
|
+
});
|
|
834
1484
|
return;
|
|
835
1485
|
}
|
|
836
1486
|
case "message_stop": {
|
|
@@ -859,275 +1509,378 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
859
1509
|
};
|
|
860
1510
|
}
|
|
861
1511
|
};
|
|
862
|
-
var anthropicMessagesResponseSchema =
|
|
863
|
-
type:
|
|
864
|
-
id:
|
|
865
|
-
model:
|
|
866
|
-
content:
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
type:
|
|
870
|
-
text:
|
|
1512
|
+
var anthropicMessagesResponseSchema = z4.object({
|
|
1513
|
+
type: z4.literal("message"),
|
|
1514
|
+
id: z4.string().nullish(),
|
|
1515
|
+
model: z4.string().nullish(),
|
|
1516
|
+
content: z4.array(
|
|
1517
|
+
z4.discriminatedUnion("type", [
|
|
1518
|
+
z4.object({
|
|
1519
|
+
type: z4.literal("text"),
|
|
1520
|
+
text: z4.string(),
|
|
1521
|
+
citations: z4.array(citationSchema).optional()
|
|
871
1522
|
}),
|
|
872
|
-
|
|
873
|
-
type:
|
|
874
|
-
thinking:
|
|
875
|
-
signature:
|
|
1523
|
+
z4.object({
|
|
1524
|
+
type: z4.literal("thinking"),
|
|
1525
|
+
thinking: z4.string(),
|
|
1526
|
+
signature: z4.string()
|
|
876
1527
|
}),
|
|
877
|
-
|
|
878
|
-
type:
|
|
879
|
-
data:
|
|
1528
|
+
z4.object({
|
|
1529
|
+
type: z4.literal("redacted_thinking"),
|
|
1530
|
+
data: z4.string()
|
|
880
1531
|
}),
|
|
881
|
-
|
|
882
|
-
type:
|
|
883
|
-
id:
|
|
884
|
-
name:
|
|
885
|
-
input:
|
|
1532
|
+
z4.object({
|
|
1533
|
+
type: z4.literal("tool_use"),
|
|
1534
|
+
id: z4.string(),
|
|
1535
|
+
name: z4.string(),
|
|
1536
|
+
input: z4.unknown()
|
|
1537
|
+
}),
|
|
1538
|
+
z4.object({
|
|
1539
|
+
type: z4.literal("server_tool_use"),
|
|
1540
|
+
id: z4.string(),
|
|
1541
|
+
name: z4.string(),
|
|
1542
|
+
input: z4.record(z4.string(), z4.unknown()).nullish()
|
|
1543
|
+
}),
|
|
1544
|
+
z4.object({
|
|
1545
|
+
type: z4.literal("web_search_tool_result"),
|
|
1546
|
+
tool_use_id: z4.string(),
|
|
1547
|
+
content: z4.union([
|
|
1548
|
+
z4.array(
|
|
1549
|
+
z4.object({
|
|
1550
|
+
type: z4.literal("web_search_result"),
|
|
1551
|
+
url: z4.string(),
|
|
1552
|
+
title: z4.string(),
|
|
1553
|
+
encrypted_content: z4.string(),
|
|
1554
|
+
page_age: z4.string().nullish()
|
|
1555
|
+
})
|
|
1556
|
+
),
|
|
1557
|
+
z4.object({
|
|
1558
|
+
type: z4.literal("web_search_tool_result_error"),
|
|
1559
|
+
error_code: z4.string()
|
|
1560
|
+
})
|
|
1561
|
+
])
|
|
886
1562
|
})
|
|
887
1563
|
])
|
|
888
1564
|
),
|
|
889
|
-
stop_reason:
|
|
890
|
-
usage:
|
|
891
|
-
input_tokens:
|
|
892
|
-
output_tokens:
|
|
893
|
-
cache_creation_input_tokens:
|
|
894
|
-
cache_read_input_tokens:
|
|
1565
|
+
stop_reason: z4.string().nullish(),
|
|
1566
|
+
usage: z4.looseObject({
|
|
1567
|
+
input_tokens: z4.number(),
|
|
1568
|
+
output_tokens: z4.number(),
|
|
1569
|
+
cache_creation_input_tokens: z4.number().nullish(),
|
|
1570
|
+
cache_read_input_tokens: z4.number().nullish()
|
|
895
1571
|
})
|
|
896
1572
|
});
|
|
897
|
-
var anthropicMessagesChunkSchema =
|
|
898
|
-
|
|
899
|
-
type:
|
|
900
|
-
message:
|
|
901
|
-
id:
|
|
902
|
-
model:
|
|
903
|
-
usage:
|
|
904
|
-
input_tokens:
|
|
905
|
-
output_tokens:
|
|
906
|
-
cache_creation_input_tokens:
|
|
907
|
-
cache_read_input_tokens:
|
|
1573
|
+
var anthropicMessagesChunkSchema = z4.discriminatedUnion("type", [
|
|
1574
|
+
z4.object({
|
|
1575
|
+
type: z4.literal("message_start"),
|
|
1576
|
+
message: z4.object({
|
|
1577
|
+
id: z4.string().nullish(),
|
|
1578
|
+
model: z4.string().nullish(),
|
|
1579
|
+
usage: z4.looseObject({
|
|
1580
|
+
input_tokens: z4.number(),
|
|
1581
|
+
output_tokens: z4.number(),
|
|
1582
|
+
cache_creation_input_tokens: z4.number().nullish(),
|
|
1583
|
+
cache_read_input_tokens: z4.number().nullish()
|
|
908
1584
|
})
|
|
909
1585
|
})
|
|
910
1586
|
}),
|
|
911
|
-
|
|
912
|
-
type:
|
|
913
|
-
index:
|
|
914
|
-
content_block:
|
|
915
|
-
|
|
916
|
-
type:
|
|
917
|
-
text:
|
|
1587
|
+
z4.object({
|
|
1588
|
+
type: z4.literal("content_block_start"),
|
|
1589
|
+
index: z4.number(),
|
|
1590
|
+
content_block: z4.discriminatedUnion("type", [
|
|
1591
|
+
z4.object({
|
|
1592
|
+
type: z4.literal("text"),
|
|
1593
|
+
text: z4.string()
|
|
1594
|
+
}),
|
|
1595
|
+
z4.object({
|
|
1596
|
+
type: z4.literal("thinking"),
|
|
1597
|
+
thinking: z4.string()
|
|
918
1598
|
}),
|
|
919
|
-
|
|
920
|
-
type:
|
|
921
|
-
|
|
1599
|
+
z4.object({
|
|
1600
|
+
type: z4.literal("tool_use"),
|
|
1601
|
+
id: z4.string(),
|
|
1602
|
+
name: z4.string()
|
|
922
1603
|
}),
|
|
923
|
-
|
|
924
|
-
type:
|
|
925
|
-
|
|
926
|
-
name: z2.string()
|
|
1604
|
+
z4.object({
|
|
1605
|
+
type: z4.literal("redacted_thinking"),
|
|
1606
|
+
data: z4.string()
|
|
927
1607
|
}),
|
|
928
|
-
|
|
929
|
-
type:
|
|
930
|
-
|
|
1608
|
+
z4.object({
|
|
1609
|
+
type: z4.literal("server_tool_use"),
|
|
1610
|
+
id: z4.string(),
|
|
1611
|
+
name: z4.string(),
|
|
1612
|
+
input: z4.record(z4.string(), z4.unknown()).nullish()
|
|
1613
|
+
}),
|
|
1614
|
+
z4.object({
|
|
1615
|
+
type: z4.literal("web_search_tool_result"),
|
|
1616
|
+
tool_use_id: z4.string(),
|
|
1617
|
+
content: z4.union([
|
|
1618
|
+
z4.array(
|
|
1619
|
+
z4.object({
|
|
1620
|
+
type: z4.literal("web_search_result"),
|
|
1621
|
+
url: z4.string(),
|
|
1622
|
+
title: z4.string(),
|
|
1623
|
+
encrypted_content: z4.string(),
|
|
1624
|
+
page_age: z4.string().nullish()
|
|
1625
|
+
})
|
|
1626
|
+
),
|
|
1627
|
+
z4.object({
|
|
1628
|
+
type: z4.literal("web_search_tool_result_error"),
|
|
1629
|
+
error_code: z4.string()
|
|
1630
|
+
})
|
|
1631
|
+
])
|
|
931
1632
|
})
|
|
932
1633
|
])
|
|
933
1634
|
}),
|
|
934
|
-
|
|
935
|
-
type:
|
|
936
|
-
index:
|
|
937
|
-
delta:
|
|
938
|
-
|
|
939
|
-
type:
|
|
940
|
-
partial_json:
|
|
1635
|
+
z4.object({
|
|
1636
|
+
type: z4.literal("content_block_delta"),
|
|
1637
|
+
index: z4.number(),
|
|
1638
|
+
delta: z4.discriminatedUnion("type", [
|
|
1639
|
+
z4.object({
|
|
1640
|
+
type: z4.literal("input_json_delta"),
|
|
1641
|
+
partial_json: z4.string()
|
|
1642
|
+
}),
|
|
1643
|
+
z4.object({
|
|
1644
|
+
type: z4.literal("text_delta"),
|
|
1645
|
+
text: z4.string()
|
|
941
1646
|
}),
|
|
942
|
-
|
|
943
|
-
type:
|
|
944
|
-
|
|
1647
|
+
z4.object({
|
|
1648
|
+
type: z4.literal("thinking_delta"),
|
|
1649
|
+
thinking: z4.string()
|
|
945
1650
|
}),
|
|
946
|
-
|
|
947
|
-
type:
|
|
948
|
-
|
|
1651
|
+
z4.object({
|
|
1652
|
+
type: z4.literal("signature_delta"),
|
|
1653
|
+
signature: z4.string()
|
|
949
1654
|
}),
|
|
950
|
-
|
|
951
|
-
type:
|
|
952
|
-
|
|
1655
|
+
z4.object({
|
|
1656
|
+
type: z4.literal("citations_delta"),
|
|
1657
|
+
citation: citationSchema
|
|
953
1658
|
})
|
|
954
1659
|
])
|
|
955
1660
|
}),
|
|
956
|
-
|
|
957
|
-
type:
|
|
958
|
-
index:
|
|
1661
|
+
z4.object({
|
|
1662
|
+
type: z4.literal("content_block_stop"),
|
|
1663
|
+
index: z4.number()
|
|
959
1664
|
}),
|
|
960
|
-
|
|
961
|
-
type:
|
|
962
|
-
error:
|
|
963
|
-
type:
|
|
964
|
-
message:
|
|
1665
|
+
z4.object({
|
|
1666
|
+
type: z4.literal("error"),
|
|
1667
|
+
error: z4.object({
|
|
1668
|
+
type: z4.string(),
|
|
1669
|
+
message: z4.string()
|
|
965
1670
|
})
|
|
966
1671
|
}),
|
|
967
|
-
|
|
968
|
-
type:
|
|
969
|
-
delta:
|
|
970
|
-
usage:
|
|
1672
|
+
z4.object({
|
|
1673
|
+
type: z4.literal("message_delta"),
|
|
1674
|
+
delta: z4.object({ stop_reason: z4.string().nullish() }),
|
|
1675
|
+
usage: z4.object({ output_tokens: z4.number() })
|
|
971
1676
|
}),
|
|
972
|
-
|
|
973
|
-
type:
|
|
1677
|
+
z4.object({
|
|
1678
|
+
type: z4.literal("message_stop")
|
|
974
1679
|
}),
|
|
975
|
-
|
|
976
|
-
type:
|
|
1680
|
+
z4.object({
|
|
1681
|
+
type: z4.literal("ping")
|
|
977
1682
|
})
|
|
978
1683
|
]);
|
|
979
|
-
var
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
budgetTokens: z2.number().optional()
|
|
983
|
-
}).optional()
|
|
1684
|
+
var anthropicReasoningMetadataSchema = z4.object({
|
|
1685
|
+
signature: z4.string().optional(),
|
|
1686
|
+
redactedData: z4.string().optional()
|
|
984
1687
|
});
|
|
985
1688
|
|
|
986
|
-
// src/
|
|
987
|
-
import {
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
1689
|
+
// src/tool/bash_20241022.ts
|
|
1690
|
+
import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
|
|
1691
|
+
import z5 from "zod/v4";
|
|
1692
|
+
var bash_20241022 = createProviderDefinedToolFactory({
|
|
1693
|
+
id: "anthropic.bash_20241022",
|
|
1694
|
+
name: "bash",
|
|
1695
|
+
inputSchema: z5.object({
|
|
1696
|
+
command: z5.string(),
|
|
1697
|
+
restart: z5.boolean().optional()
|
|
1698
|
+
})
|
|
991
1699
|
});
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
command: z3.string(),
|
|
1004
|
-
restart: z3.boolean().optional()
|
|
1700
|
+
|
|
1701
|
+
// src/tool/bash_20250124.ts
|
|
1702
|
+
import { createProviderDefinedToolFactory as createProviderDefinedToolFactory2 } from "@ai-sdk/provider-utils";
|
|
1703
|
+
import z6 from "zod/v4";
|
|
1704
|
+
var bash_20250124 = createProviderDefinedToolFactory2({
|
|
1705
|
+
id: "anthropic.bash_20250124",
|
|
1706
|
+
name: "bash",
|
|
1707
|
+
inputSchema: z6.object({
|
|
1708
|
+
command: z6.string(),
|
|
1709
|
+
restart: z6.boolean().optional()
|
|
1710
|
+
})
|
|
1005
1711
|
});
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1712
|
+
|
|
1713
|
+
// src/tool/computer_20241022.ts
|
|
1714
|
+
import { createProviderDefinedToolFactory as createProviderDefinedToolFactory3 } from "@ai-sdk/provider-utils";
|
|
1715
|
+
import { z as z7 } from "zod/v4";
|
|
1716
|
+
var computer_20241022 = createProviderDefinedToolFactory3({
|
|
1717
|
+
id: "anthropic.computer_20241022",
|
|
1718
|
+
name: "computer",
|
|
1719
|
+
inputSchema: z7.object({
|
|
1720
|
+
action: z7.enum([
|
|
1721
|
+
"key",
|
|
1722
|
+
"type",
|
|
1723
|
+
"mouse_move",
|
|
1724
|
+
"left_click",
|
|
1725
|
+
"left_click_drag",
|
|
1726
|
+
"right_click",
|
|
1727
|
+
"middle_click",
|
|
1728
|
+
"double_click",
|
|
1729
|
+
"screenshot",
|
|
1730
|
+
"cursor_position"
|
|
1731
|
+
]),
|
|
1732
|
+
coordinate: z7.array(z7.number().int()).optional(),
|
|
1733
|
+
text: z7.string().optional()
|
|
1734
|
+
})
|
|
1024
1735
|
});
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1736
|
+
|
|
1737
|
+
// src/tool/computer_20250124.ts
|
|
1738
|
+
import { createProviderDefinedToolFactory as createProviderDefinedToolFactory4 } from "@ai-sdk/provider-utils";
|
|
1739
|
+
import { z as z8 } from "zod/v4";
|
|
1740
|
+
var computer_20250124 = createProviderDefinedToolFactory4({
|
|
1741
|
+
id: "anthropic.computer_20250124",
|
|
1742
|
+
name: "computer",
|
|
1743
|
+
inputSchema: z8.object({
|
|
1744
|
+
action: z8.enum([
|
|
1745
|
+
"key",
|
|
1746
|
+
"hold_key",
|
|
1747
|
+
"type",
|
|
1748
|
+
"cursor_position",
|
|
1749
|
+
"mouse_move",
|
|
1750
|
+
"left_mouse_down",
|
|
1751
|
+
"left_mouse_up",
|
|
1752
|
+
"left_click",
|
|
1753
|
+
"left_click_drag",
|
|
1754
|
+
"right_click",
|
|
1755
|
+
"middle_click",
|
|
1756
|
+
"double_click",
|
|
1757
|
+
"triple_click",
|
|
1758
|
+
"scroll",
|
|
1759
|
+
"wait",
|
|
1760
|
+
"screenshot"
|
|
1761
|
+
]),
|
|
1762
|
+
coordinate: z8.tuple([z8.number().int(), z8.number().int()]).optional(),
|
|
1763
|
+
duration: z8.number().optional(),
|
|
1764
|
+
scroll_amount: z8.number().optional(),
|
|
1765
|
+
scroll_direction: z8.enum(["up", "down", "left", "right"]).optional(),
|
|
1766
|
+
start_coordinate: z8.tuple([z8.number().int(), z8.number().int()]).optional(),
|
|
1767
|
+
text: z8.string().optional()
|
|
1768
|
+
})
|
|
1043
1769
|
});
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
"left_click_drag",
|
|
1061
|
-
"right_click",
|
|
1062
|
-
"middle_click",
|
|
1063
|
-
"double_click",
|
|
1064
|
-
"screenshot",
|
|
1065
|
-
"cursor_position"
|
|
1066
|
-
]),
|
|
1067
|
-
coordinate: z3.array(z3.number().int()).optional(),
|
|
1068
|
-
text: z3.string().optional()
|
|
1770
|
+
|
|
1771
|
+
// src/tool/text-editor_20241022.ts
|
|
1772
|
+
import { createProviderDefinedToolFactory as createProviderDefinedToolFactory5 } from "@ai-sdk/provider-utils";
|
|
1773
|
+
import { z as z9 } from "zod/v4";
|
|
1774
|
+
var textEditor_20241022 = createProviderDefinedToolFactory5({
|
|
1775
|
+
id: "anthropic.text_editor_20241022",
|
|
1776
|
+
name: "str_replace_editor",
|
|
1777
|
+
inputSchema: z9.object({
|
|
1778
|
+
command: z9.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
|
|
1779
|
+
path: z9.string(),
|
|
1780
|
+
file_text: z9.string().optional(),
|
|
1781
|
+
insert_line: z9.number().int().optional(),
|
|
1782
|
+
new_str: z9.string().optional(),
|
|
1783
|
+
old_str: z9.string().optional(),
|
|
1784
|
+
view_range: z9.array(z9.number().int()).optional()
|
|
1785
|
+
})
|
|
1069
1786
|
});
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
"key",
|
|
1087
|
-
"hold_key",
|
|
1088
|
-
"type",
|
|
1089
|
-
"cursor_position",
|
|
1090
|
-
"mouse_move",
|
|
1091
|
-
"left_mouse_down",
|
|
1092
|
-
"left_mouse_up",
|
|
1093
|
-
"left_click",
|
|
1094
|
-
"left_click_drag",
|
|
1095
|
-
"right_click",
|
|
1096
|
-
"middle_click",
|
|
1097
|
-
"double_click",
|
|
1098
|
-
"triple_click",
|
|
1099
|
-
"scroll",
|
|
1100
|
-
"wait",
|
|
1101
|
-
"screenshot"
|
|
1102
|
-
]),
|
|
1103
|
-
coordinate: z3.tuple([z3.number().int(), z3.number().int()]).optional(),
|
|
1104
|
-
duration: z3.number().optional(),
|
|
1105
|
-
scroll_amount: z3.number().optional(),
|
|
1106
|
-
scroll_direction: z3.enum(["up", "down", "left", "right"]).optional(),
|
|
1107
|
-
start_coordinate: z3.tuple([z3.number().int(), z3.number().int()]).optional(),
|
|
1108
|
-
text: z3.string().optional()
|
|
1787
|
+
|
|
1788
|
+
// src/tool/text-editor_20250124.ts
|
|
1789
|
+
import { createProviderDefinedToolFactory as createProviderDefinedToolFactory6 } from "@ai-sdk/provider-utils";
|
|
1790
|
+
import { z as z10 } from "zod/v4";
|
|
1791
|
+
var textEditor_20250124 = createProviderDefinedToolFactory6({
|
|
1792
|
+
id: "anthropic.text_editor_20250124",
|
|
1793
|
+
name: "str_replace_editor",
|
|
1794
|
+
inputSchema: z10.object({
|
|
1795
|
+
command: z10.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
|
|
1796
|
+
path: z10.string(),
|
|
1797
|
+
file_text: z10.string().optional(),
|
|
1798
|
+
insert_line: z10.number().int().optional(),
|
|
1799
|
+
new_str: z10.string().optional(),
|
|
1800
|
+
old_str: z10.string().optional(),
|
|
1801
|
+
view_range: z10.array(z10.number().int()).optional()
|
|
1802
|
+
})
|
|
1109
1803
|
});
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1804
|
+
|
|
1805
|
+
// src/tool/text-editor_20250429.ts
|
|
1806
|
+
import { createProviderDefinedToolFactory as createProviderDefinedToolFactory7 } from "@ai-sdk/provider-utils";
|
|
1807
|
+
import { z as z11 } from "zod/v4";
|
|
1808
|
+
var textEditor_20250429 = createProviderDefinedToolFactory7({
|
|
1809
|
+
id: "anthropic.text_editor_20250429",
|
|
1810
|
+
name: "str_replace_based_edit_tool",
|
|
1811
|
+
inputSchema: z11.object({
|
|
1812
|
+
command: z11.enum(["view", "create", "str_replace", "insert"]),
|
|
1813
|
+
path: z11.string(),
|
|
1814
|
+
file_text: z11.string().optional(),
|
|
1815
|
+
insert_line: z11.number().int().optional(),
|
|
1816
|
+
new_str: z11.string().optional(),
|
|
1817
|
+
old_str: z11.string().optional(),
|
|
1818
|
+
view_range: z11.array(z11.number().int()).optional()
|
|
1819
|
+
})
|
|
1820
|
+
});
|
|
1821
|
+
|
|
1822
|
+
// src/anthropic-tools.ts
|
|
1124
1823
|
var anthropicTools = {
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1824
|
+
/**
|
|
1825
|
+
* Creates a tool for running a bash command. Must have name "bash".
|
|
1826
|
+
*
|
|
1827
|
+
* Image results are supported.
|
|
1828
|
+
*
|
|
1829
|
+
* @param execute - The function to execute the tool. Optional.
|
|
1830
|
+
*/
|
|
1831
|
+
bash_20241022,
|
|
1832
|
+
/**
|
|
1833
|
+
* Creates a tool for running a bash command. Must have name "bash".
|
|
1834
|
+
*
|
|
1835
|
+
* Image results are supported.
|
|
1836
|
+
*
|
|
1837
|
+
* @param execute - The function to execute the tool. Optional.
|
|
1838
|
+
*/
|
|
1839
|
+
bash_20250124,
|
|
1840
|
+
/**
|
|
1841
|
+
* Creates a tool for editing text. Must have name "str_replace_editor".
|
|
1842
|
+
*/
|
|
1843
|
+
textEditor_20241022,
|
|
1844
|
+
/**
|
|
1845
|
+
* Creates a tool for editing text. Must have name "str_replace_editor".
|
|
1846
|
+
*/
|
|
1847
|
+
textEditor_20250124,
|
|
1848
|
+
/**
|
|
1849
|
+
* Creates a tool for editing text. Must have name "str_replace_based_edit_tool".
|
|
1850
|
+
* Note: This version does not support the "undo_edit" command.
|
|
1851
|
+
*/
|
|
1852
|
+
textEditor_20250429,
|
|
1853
|
+
/**
|
|
1854
|
+
* Creates a tool for executing actions on a computer. Must have name "computer".
|
|
1855
|
+
*
|
|
1856
|
+
* Image results are supported.
|
|
1857
|
+
*
|
|
1858
|
+
* @param displayWidthPx - The width of the display being controlled by the model in pixels.
|
|
1859
|
+
* @param displayHeightPx - The height of the display being controlled by the model in pixels.
|
|
1860
|
+
* @param displayNumber - The display number to control (only relevant for X11 environments). If specified, the tool will be provided a display number in the tool definition.
|
|
1861
|
+
*/
|
|
1862
|
+
computer_20241022,
|
|
1863
|
+
/**
|
|
1864
|
+
* Creates a tool for executing actions on a computer. Must have name "computer".
|
|
1865
|
+
*
|
|
1866
|
+
* Image results are supported.
|
|
1867
|
+
*
|
|
1868
|
+
* @param displayWidthPx - The width of the display being controlled by the model in pixels.
|
|
1869
|
+
* @param displayHeightPx - The height of the display being controlled by the model in pixels.
|
|
1870
|
+
* @param displayNumber - The display number to control (only relevant for X11 environments). If specified, the tool will be provided a display number in the tool definition.
|
|
1871
|
+
* @param execute - The function to execute the tool. Optional.
|
|
1872
|
+
*/
|
|
1873
|
+
computer_20250124,
|
|
1874
|
+
/**
|
|
1875
|
+
* Creates a web search tool that gives Claude direct access to real-time web content.
|
|
1876
|
+
* Must have name "web_search".
|
|
1877
|
+
*
|
|
1878
|
+
* @param maxUses - Maximum number of web searches Claude can perform during the conversation.
|
|
1879
|
+
* @param allowedDomains - Optional list of domains that Claude is allowed to search.
|
|
1880
|
+
* @param blockedDomains - Optional list of domains that Claude should avoid when searching.
|
|
1881
|
+
* @param userLocation - Optional user location information to provide geographically relevant search results.
|
|
1882
|
+
*/
|
|
1883
|
+
webSearch_20250305
|
|
1131
1884
|
};
|
|
1132
1885
|
|
|
1133
1886
|
// src/anthropic-provider.ts
|
|
@@ -1143,22 +1896,26 @@ function createAnthropic(options = {}) {
|
|
|
1143
1896
|
}),
|
|
1144
1897
|
...options.headers
|
|
1145
1898
|
});
|
|
1146
|
-
const createChatModel = (modelId
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1899
|
+
const createChatModel = (modelId) => {
|
|
1900
|
+
var _a2;
|
|
1901
|
+
return new AnthropicMessagesLanguageModel(modelId, {
|
|
1902
|
+
provider: "anthropic.messages",
|
|
1903
|
+
baseURL,
|
|
1904
|
+
headers: getHeaders,
|
|
1905
|
+
fetch: options.fetch,
|
|
1906
|
+
generateId: (_a2 = options.generateId) != null ? _a2 : generateId2,
|
|
1907
|
+
supportedUrls: () => ({
|
|
1908
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
1909
|
+
})
|
|
1910
|
+
});
|
|
1911
|
+
};
|
|
1912
|
+
const provider = function(modelId) {
|
|
1156
1913
|
if (new.target) {
|
|
1157
1914
|
throw new Error(
|
|
1158
1915
|
"The Anthropic model function cannot be called with the new keyword."
|
|
1159
1916
|
);
|
|
1160
1917
|
}
|
|
1161
|
-
return createChatModel(modelId
|
|
1918
|
+
return createChatModel(modelId);
|
|
1162
1919
|
};
|
|
1163
1920
|
provider.languageModel = createChatModel;
|
|
1164
1921
|
provider.chat = createChatModel;
|