@ai-sdk/anthropic 1.2.12 → 2.0.0-alpha.10
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 +244 -24
- package/dist/index.d.mts +101 -30
- package/dist/index.d.ts +101 -30
- package/dist/index.js +629 -347
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +634 -349
- package/dist/index.mjs.map +1 -1
- package/{internal/dist → dist/internal}/index.d.mts +12 -37
- package/{internal/dist → dist/internal}/index.d.ts +12 -37
- package/{internal/dist → dist/internal}/index.js +611 -338
- package/dist/internal/index.js.map +1 -0
- package/{internal/dist → dist/internal}/index.mjs +615 -340
- package/dist/internal/index.mjs.map +1 -0
- package/internal.d.ts +1 -0
- package/package.json +18 -16
- package/internal/dist/index.js.map +0 -1
- package/internal/dist/index.mjs.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -3,23 +3,26 @@ 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";
|
|
9
10
|
|
|
10
11
|
// src/anthropic-messages-language-model.ts
|
|
11
12
|
import {
|
|
13
|
+
APICallError,
|
|
12
14
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
13
15
|
} from "@ai-sdk/provider";
|
|
14
16
|
import {
|
|
15
17
|
combineHeaders,
|
|
16
18
|
createEventSourceResponseHandler,
|
|
17
19
|
createJsonResponseHandler,
|
|
18
|
-
|
|
20
|
+
generateId,
|
|
21
|
+
parseProviderOptions as parseProviderOptions2,
|
|
19
22
|
postJsonToApi,
|
|
20
23
|
resolve
|
|
21
24
|
} from "@ai-sdk/provider-utils";
|
|
22
|
-
import { z as
|
|
25
|
+
import { z as z3 } from "zod";
|
|
23
26
|
|
|
24
27
|
// src/anthropic-error.ts
|
|
25
28
|
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
|
@@ -36,20 +39,76 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
36
39
|
errorToMessage: (data) => data.error.message
|
|
37
40
|
});
|
|
38
41
|
|
|
42
|
+
// src/anthropic-messages-options.ts
|
|
43
|
+
import { z as z2 } from "zod";
|
|
44
|
+
var webSearchLocationSchema = z2.object({
|
|
45
|
+
type: z2.literal("approximate"),
|
|
46
|
+
city: z2.string().optional(),
|
|
47
|
+
region: z2.string().optional(),
|
|
48
|
+
country: z2.string(),
|
|
49
|
+
timezone: z2.string().optional()
|
|
50
|
+
});
|
|
51
|
+
var anthropicProviderOptions = z2.object({
|
|
52
|
+
/**
|
|
53
|
+
Include reasoning content in requests sent to the model. Defaults to `true`.
|
|
54
|
+
|
|
55
|
+
If you are experiencing issues with the model handling requests involving
|
|
56
|
+
*/
|
|
57
|
+
sendReasoning: z2.boolean().optional(),
|
|
58
|
+
thinking: z2.object({
|
|
59
|
+
type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
|
|
60
|
+
budgetTokens: z2.number().optional()
|
|
61
|
+
}).optional(),
|
|
62
|
+
/**
|
|
63
|
+
* Web search tool configuration for Claude models that support it.
|
|
64
|
+
* When provided, automatically adds the web search tool to the request.
|
|
65
|
+
*/
|
|
66
|
+
webSearch: z2.object({
|
|
67
|
+
/**
|
|
68
|
+
* Limit the number of searches per request (optional)
|
|
69
|
+
* Defaults to 5 if not specified
|
|
70
|
+
*/
|
|
71
|
+
maxUses: z2.number().min(1).max(20).optional(),
|
|
72
|
+
/**
|
|
73
|
+
* Only include results from these domains (optional)
|
|
74
|
+
* Cannot be used with blockedDomains
|
|
75
|
+
*/
|
|
76
|
+
allowedDomains: z2.array(z2.string()).optional(),
|
|
77
|
+
/**
|
|
78
|
+
* Never include results from these domains (optional)
|
|
79
|
+
* Cannot be used with allowedDomains
|
|
80
|
+
*/
|
|
81
|
+
blockedDomains: z2.array(z2.string()).optional(),
|
|
82
|
+
/**
|
|
83
|
+
* Localize search results based on user location (optional)
|
|
84
|
+
*/
|
|
85
|
+
userLocation: webSearchLocationSchema.optional()
|
|
86
|
+
}).optional()
|
|
87
|
+
});
|
|
88
|
+
|
|
39
89
|
// src/anthropic-prepare-tools.ts
|
|
40
90
|
import {
|
|
41
91
|
UnsupportedFunctionalityError
|
|
42
92
|
} from "@ai-sdk/provider";
|
|
43
|
-
function
|
|
44
|
-
|
|
45
|
-
|
|
93
|
+
function isWebSearchTool(tool) {
|
|
94
|
+
return typeof tool === "object" && tool !== null && "type" in tool && tool.type === "web_search_20250305";
|
|
95
|
+
}
|
|
96
|
+
function prepareTools({
|
|
97
|
+
tools,
|
|
98
|
+
toolChoice
|
|
99
|
+
}) {
|
|
100
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
46
101
|
const toolWarnings = [];
|
|
47
102
|
const betas = /* @__PURE__ */ new Set();
|
|
48
103
|
if (tools == null) {
|
|
49
|
-
return { tools: void 0,
|
|
104
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
|
|
50
105
|
}
|
|
51
106
|
const anthropicTools2 = [];
|
|
52
107
|
for (const tool of tools) {
|
|
108
|
+
if (isWebSearchTool(tool)) {
|
|
109
|
+
anthropicTools2.push(tool);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
53
112
|
switch (tool.type) {
|
|
54
113
|
case "function":
|
|
55
114
|
anthropicTools2.push({
|
|
@@ -118,11 +177,10 @@ function prepareTools(mode) {
|
|
|
118
177
|
break;
|
|
119
178
|
}
|
|
120
179
|
}
|
|
121
|
-
const toolChoice = mode.toolChoice;
|
|
122
180
|
if (toolChoice == null) {
|
|
123
181
|
return {
|
|
124
182
|
tools: anthropicTools2,
|
|
125
|
-
|
|
183
|
+
toolChoice: void 0,
|
|
126
184
|
toolWarnings,
|
|
127
185
|
betas
|
|
128
186
|
};
|
|
@@ -132,30 +190,30 @@ function prepareTools(mode) {
|
|
|
132
190
|
case "auto":
|
|
133
191
|
return {
|
|
134
192
|
tools: anthropicTools2,
|
|
135
|
-
|
|
193
|
+
toolChoice: { type: "auto" },
|
|
136
194
|
toolWarnings,
|
|
137
195
|
betas
|
|
138
196
|
};
|
|
139
197
|
case "required":
|
|
140
198
|
return {
|
|
141
199
|
tools: anthropicTools2,
|
|
142
|
-
|
|
200
|
+
toolChoice: { type: "any" },
|
|
143
201
|
toolWarnings,
|
|
144
202
|
betas
|
|
145
203
|
};
|
|
146
204
|
case "none":
|
|
147
|
-
return { tools: void 0,
|
|
205
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
|
|
148
206
|
case "tool":
|
|
149
207
|
return {
|
|
150
208
|
tools: anthropicTools2,
|
|
151
|
-
|
|
209
|
+
toolChoice: { type: "tool", name: toolChoice.toolName },
|
|
152
210
|
toolWarnings,
|
|
153
211
|
betas
|
|
154
212
|
};
|
|
155
213
|
default: {
|
|
156
214
|
const _exhaustiveCheck = type;
|
|
157
215
|
throw new UnsupportedFunctionalityError({
|
|
158
|
-
functionality: `
|
|
216
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
159
217
|
});
|
|
160
218
|
}
|
|
161
219
|
}
|
|
@@ -165,13 +223,13 @@ function prepareTools(mode) {
|
|
|
165
223
|
import {
|
|
166
224
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
167
225
|
} from "@ai-sdk/provider";
|
|
168
|
-
import {
|
|
169
|
-
function convertToAnthropicMessagesPrompt({
|
|
226
|
+
import { convertToBase64, parseProviderOptions } from "@ai-sdk/provider-utils";
|
|
227
|
+
async function convertToAnthropicMessagesPrompt({
|
|
170
228
|
prompt,
|
|
171
229
|
sendReasoning,
|
|
172
230
|
warnings
|
|
173
231
|
}) {
|
|
174
|
-
var _a, _b, _c
|
|
232
|
+
var _a, _b, _c;
|
|
175
233
|
const betas = /* @__PURE__ */ new Set();
|
|
176
234
|
const blocks = groupIntoBlocks(prompt);
|
|
177
235
|
let system = void 0;
|
|
@@ -193,10 +251,10 @@ function convertToAnthropicMessagesPrompt({
|
|
|
193
251
|
functionality: "Multiple system messages that are separated by user/assistant messages"
|
|
194
252
|
});
|
|
195
253
|
}
|
|
196
|
-
system = block.messages.map(({ content,
|
|
254
|
+
system = block.messages.map(({ content, providerOptions }) => ({
|
|
197
255
|
type: "text",
|
|
198
256
|
text: content,
|
|
199
|
-
cache_control: getCacheControl(
|
|
257
|
+
cache_control: getCacheControl(providerOptions)
|
|
200
258
|
}));
|
|
201
259
|
break;
|
|
202
260
|
}
|
|
@@ -209,7 +267,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
209
267
|
for (let j = 0; j < content.length; j++) {
|
|
210
268
|
const part = content[j];
|
|
211
269
|
const isLastPart = j === content.length - 1;
|
|
212
|
-
const cacheControl = (_a = getCacheControl(part.
|
|
270
|
+
const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
213
271
|
switch (part.type) {
|
|
214
272
|
case "text": {
|
|
215
273
|
anthropicContent.push({
|
|
@@ -219,40 +277,39 @@ function convertToAnthropicMessagesPrompt({
|
|
|
219
277
|
});
|
|
220
278
|
break;
|
|
221
279
|
}
|
|
222
|
-
case "image": {
|
|
223
|
-
anthropicContent.push({
|
|
224
|
-
type: "image",
|
|
225
|
-
source: part.image instanceof URL ? {
|
|
226
|
-
type: "url",
|
|
227
|
-
url: part.image.toString()
|
|
228
|
-
} : {
|
|
229
|
-
type: "base64",
|
|
230
|
-
media_type: (_b = part.mimeType) != null ? _b : "image/jpeg",
|
|
231
|
-
data: convertUint8ArrayToBase64(part.image)
|
|
232
|
-
},
|
|
233
|
-
cache_control: cacheControl
|
|
234
|
-
});
|
|
235
|
-
break;
|
|
236
|
-
}
|
|
237
280
|
case "file": {
|
|
238
|
-
if (part.
|
|
281
|
+
if (part.mediaType.startsWith("image/")) {
|
|
282
|
+
anthropicContent.push({
|
|
283
|
+
type: "image",
|
|
284
|
+
source: part.data instanceof URL ? {
|
|
285
|
+
type: "url",
|
|
286
|
+
url: part.data.toString()
|
|
287
|
+
} : {
|
|
288
|
+
type: "base64",
|
|
289
|
+
media_type: part.mediaType === "image/*" ? "image/jpeg" : part.mediaType,
|
|
290
|
+
data: convertToBase64(part.data)
|
|
291
|
+
},
|
|
292
|
+
cache_control: cacheControl
|
|
293
|
+
});
|
|
294
|
+
} else if (part.mediaType === "application/pdf") {
|
|
295
|
+
betas.add("pdfs-2024-09-25");
|
|
296
|
+
anthropicContent.push({
|
|
297
|
+
type: "document",
|
|
298
|
+
source: part.data instanceof URL ? {
|
|
299
|
+
type: "url",
|
|
300
|
+
url: part.data.toString()
|
|
301
|
+
} : {
|
|
302
|
+
type: "base64",
|
|
303
|
+
media_type: "application/pdf",
|
|
304
|
+
data: convertToBase64(part.data)
|
|
305
|
+
},
|
|
306
|
+
cache_control: cacheControl
|
|
307
|
+
});
|
|
308
|
+
} else {
|
|
239
309
|
throw new UnsupportedFunctionalityError2({
|
|
240
|
-
functionality:
|
|
310
|
+
functionality: `media type: ${part.mediaType}`
|
|
241
311
|
});
|
|
242
312
|
}
|
|
243
|
-
betas.add("pdfs-2024-09-25");
|
|
244
|
-
anthropicContent.push({
|
|
245
|
-
type: "document",
|
|
246
|
-
source: part.data instanceof URL ? {
|
|
247
|
-
type: "url",
|
|
248
|
-
url: part.data.toString()
|
|
249
|
-
} : {
|
|
250
|
-
type: "base64",
|
|
251
|
-
media_type: "application/pdf",
|
|
252
|
-
data: part.data
|
|
253
|
-
},
|
|
254
|
-
cache_control: cacheControl
|
|
255
|
-
});
|
|
256
313
|
break;
|
|
257
314
|
}
|
|
258
315
|
}
|
|
@@ -263,7 +320,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
263
320
|
for (let i2 = 0; i2 < content.length; i2++) {
|
|
264
321
|
const part = content[i2];
|
|
265
322
|
const isLastPart = i2 === content.length - 1;
|
|
266
|
-
const cacheControl = (
|
|
323
|
+
const cacheControl = (_b = getCacheControl(part.providerOptions)) != null ? _b : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
267
324
|
const toolResultContent = part.content != null ? part.content.map((part2) => {
|
|
268
325
|
var _a2;
|
|
269
326
|
switch (part2.type) {
|
|
@@ -278,7 +335,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
278
335
|
type: "image",
|
|
279
336
|
source: {
|
|
280
337
|
type: "base64",
|
|
281
|
-
media_type: (_a2 = part2.
|
|
338
|
+
media_type: (_a2 = part2.mediaType) != null ? _a2 : "image/jpeg",
|
|
282
339
|
data: part2.data
|
|
283
340
|
},
|
|
284
341
|
cache_control: void 0
|
|
@@ -313,7 +370,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
313
370
|
for (let k = 0; k < content.length; k++) {
|
|
314
371
|
const part = content[k];
|
|
315
372
|
const isLastContentPart = k === content.length - 1;
|
|
316
|
-
const cacheControl = (
|
|
373
|
+
const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
|
|
317
374
|
switch (part.type) {
|
|
318
375
|
case "text": {
|
|
319
376
|
anthropicContent.push({
|
|
@@ -330,12 +387,37 @@ function convertToAnthropicMessagesPrompt({
|
|
|
330
387
|
}
|
|
331
388
|
case "reasoning": {
|
|
332
389
|
if (sendReasoning) {
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
cache_control: cacheControl
|
|
390
|
+
const reasoningMetadata = await parseProviderOptions({
|
|
391
|
+
provider: "anthropic",
|
|
392
|
+
providerOptions: part.providerOptions,
|
|
393
|
+
schema: anthropicReasoningMetadataSchema
|
|
338
394
|
});
|
|
395
|
+
if (reasoningMetadata != null) {
|
|
396
|
+
if (reasoningMetadata.signature != null) {
|
|
397
|
+
anthropicContent.push({
|
|
398
|
+
type: "thinking",
|
|
399
|
+
thinking: part.text,
|
|
400
|
+
signature: reasoningMetadata.signature,
|
|
401
|
+
cache_control: cacheControl
|
|
402
|
+
});
|
|
403
|
+
} else if (reasoningMetadata.redactedData != null) {
|
|
404
|
+
anthropicContent.push({
|
|
405
|
+
type: "redacted_thinking",
|
|
406
|
+
data: reasoningMetadata.redactedData,
|
|
407
|
+
cache_control: cacheControl
|
|
408
|
+
});
|
|
409
|
+
} else {
|
|
410
|
+
warnings.push({
|
|
411
|
+
type: "other",
|
|
412
|
+
message: "unsupported reasoning metadata"
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
} else {
|
|
416
|
+
warnings.push({
|
|
417
|
+
type: "other",
|
|
418
|
+
message: "unsupported reasoning metadata"
|
|
419
|
+
});
|
|
420
|
+
}
|
|
339
421
|
} else {
|
|
340
422
|
warnings.push({
|
|
341
423
|
type: "other",
|
|
@@ -344,14 +426,6 @@ function convertToAnthropicMessagesPrompt({
|
|
|
344
426
|
}
|
|
345
427
|
break;
|
|
346
428
|
}
|
|
347
|
-
case "redacted-reasoning": {
|
|
348
|
-
anthropicContent.push({
|
|
349
|
-
type: "redacted_thinking",
|
|
350
|
-
data: part.data,
|
|
351
|
-
cache_control: cacheControl
|
|
352
|
-
});
|
|
353
|
-
break;
|
|
354
|
-
}
|
|
355
429
|
case "tool-call": {
|
|
356
430
|
anthropicContent.push({
|
|
357
431
|
type: "tool_use",
|
|
@@ -370,7 +444,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
370
444
|
}
|
|
371
445
|
default: {
|
|
372
446
|
const _exhaustiveCheck = type;
|
|
373
|
-
throw new Error(`
|
|
447
|
+
throw new Error(`content type: ${_exhaustiveCheck}`);
|
|
374
448
|
}
|
|
375
449
|
}
|
|
376
450
|
}
|
|
@@ -427,13 +501,16 @@ function groupIntoBlocks(prompt) {
|
|
|
427
501
|
}
|
|
428
502
|
|
|
429
503
|
// src/map-anthropic-stop-reason.ts
|
|
430
|
-
function mapAnthropicStopReason(
|
|
504
|
+
function mapAnthropicStopReason({
|
|
505
|
+
finishReason,
|
|
506
|
+
isJsonResponseFromTool
|
|
507
|
+
}) {
|
|
431
508
|
switch (finishReason) {
|
|
432
509
|
case "end_turn":
|
|
433
510
|
case "stop_sequence":
|
|
434
511
|
return "stop";
|
|
435
512
|
case "tool_use":
|
|
436
|
-
return "tool-calls";
|
|
513
|
+
return isJsonResponseFromTool ? "stop" : "tool-calls";
|
|
437
514
|
case "max_tokens":
|
|
438
515
|
return "length";
|
|
439
516
|
default:
|
|
@@ -443,12 +520,12 @@ function mapAnthropicStopReason(finishReason) {
|
|
|
443
520
|
|
|
444
521
|
// src/anthropic-messages-language-model.ts
|
|
445
522
|
var AnthropicMessagesLanguageModel = class {
|
|
446
|
-
constructor(modelId,
|
|
447
|
-
this.specificationVersion = "
|
|
448
|
-
|
|
523
|
+
constructor(modelId, config) {
|
|
524
|
+
this.specificationVersion = "v2";
|
|
525
|
+
var _a;
|
|
449
526
|
this.modelId = modelId;
|
|
450
|
-
this.settings = settings;
|
|
451
527
|
this.config = config;
|
|
528
|
+
this.generateId = (_a = config.generateId) != null ? _a : generateId;
|
|
452
529
|
}
|
|
453
530
|
supportsUrl(url) {
|
|
454
531
|
return url.protocol === "https:";
|
|
@@ -456,13 +533,13 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
456
533
|
get provider() {
|
|
457
534
|
return this.config.provider;
|
|
458
535
|
}
|
|
459
|
-
get
|
|
460
|
-
|
|
536
|
+
get supportedUrls() {
|
|
537
|
+
var _a, _b, _c;
|
|
538
|
+
return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
|
|
461
539
|
}
|
|
462
540
|
async getArgs({
|
|
463
|
-
mode,
|
|
464
541
|
prompt,
|
|
465
|
-
|
|
542
|
+
maxOutputTokens = 4096,
|
|
466
543
|
// 4096: max model output tokens TODO update default in v5
|
|
467
544
|
temperature,
|
|
468
545
|
topP,
|
|
@@ -472,10 +549,11 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
472
549
|
stopSequences,
|
|
473
550
|
responseFormat,
|
|
474
551
|
seed,
|
|
475
|
-
|
|
552
|
+
tools,
|
|
553
|
+
toolChoice,
|
|
554
|
+
providerOptions
|
|
476
555
|
}) {
|
|
477
556
|
var _a, _b, _c;
|
|
478
|
-
const type = mode.type;
|
|
479
557
|
const warnings = [];
|
|
480
558
|
if (frequencyPenalty != null) {
|
|
481
559
|
warnings.push({
|
|
@@ -495,22 +573,36 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
495
573
|
setting: "seed"
|
|
496
574
|
});
|
|
497
575
|
}
|
|
498
|
-
if (responseFormat
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
576
|
+
if ((responseFormat == null ? void 0 : responseFormat.type) === "json") {
|
|
577
|
+
if (responseFormat.schema == null) {
|
|
578
|
+
warnings.push({
|
|
579
|
+
type: "unsupported-setting",
|
|
580
|
+
setting: "responseFormat",
|
|
581
|
+
details: "JSON response format requires a schema. The response format is ignored."
|
|
582
|
+
});
|
|
583
|
+
} else if (tools != null) {
|
|
584
|
+
warnings.push({
|
|
585
|
+
type: "unsupported-setting",
|
|
586
|
+
setting: "tools",
|
|
587
|
+
details: "JSON response format does not support tools. The provided tools are ignored."
|
|
588
|
+
});
|
|
589
|
+
}
|
|
504
590
|
}
|
|
505
|
-
const
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
591
|
+
const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
|
|
592
|
+
type: "function",
|
|
593
|
+
name: "json",
|
|
594
|
+
description: "Respond with a JSON object.",
|
|
595
|
+
parameters: responseFormat.schema
|
|
596
|
+
} : void 0;
|
|
597
|
+
const anthropicOptions = await parseProviderOptions2({
|
|
511
598
|
provider: "anthropic",
|
|
512
599
|
providerOptions,
|
|
513
|
-
schema:
|
|
600
|
+
schema: anthropicProviderOptions
|
|
601
|
+
});
|
|
602
|
+
const { prompt: messagesPrompt, betas: messagesBetas } = await convertToAnthropicMessagesPrompt({
|
|
603
|
+
prompt,
|
|
604
|
+
sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
|
|
605
|
+
warnings
|
|
514
606
|
});
|
|
515
607
|
const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
|
|
516
608
|
const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
|
|
@@ -518,7 +610,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
518
610
|
// model id:
|
|
519
611
|
model: this.modelId,
|
|
520
612
|
// standardized settings:
|
|
521
|
-
max_tokens:
|
|
613
|
+
max_tokens: maxOutputTokens,
|
|
522
614
|
temperature,
|
|
523
615
|
top_k: topK,
|
|
524
616
|
top_p: topP,
|
|
@@ -561,44 +653,50 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
561
653
|
details: "topP is not supported when thinking is enabled"
|
|
562
654
|
});
|
|
563
655
|
}
|
|
564
|
-
baseArgs.max_tokens =
|
|
656
|
+
baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
|
|
565
657
|
}
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
const { name, description, parameters } = mode.tool;
|
|
587
|
-
return {
|
|
588
|
-
args: {
|
|
589
|
-
...baseArgs,
|
|
590
|
-
tools: [{ name, description, input_schema: parameters }],
|
|
591
|
-
tool_choice: { type: "tool", name }
|
|
592
|
-
},
|
|
593
|
-
warnings,
|
|
594
|
-
betas: messagesBetas
|
|
595
|
-
};
|
|
596
|
-
}
|
|
597
|
-
default: {
|
|
598
|
-
const _exhaustiveCheck = type;
|
|
599
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
600
|
-
}
|
|
658
|
+
let modifiedTools = tools;
|
|
659
|
+
let modifiedToolChoice = toolChoice;
|
|
660
|
+
if (anthropicOptions == null ? void 0 : anthropicOptions.webSearch) {
|
|
661
|
+
const webSearchTool = {
|
|
662
|
+
type: "web_search_20250305",
|
|
663
|
+
name: "web_search",
|
|
664
|
+
max_uses: anthropicOptions.webSearch.maxUses,
|
|
665
|
+
allowed_domains: anthropicOptions.webSearch.allowedDomains,
|
|
666
|
+
blocked_domains: anthropicOptions.webSearch.blockedDomains,
|
|
667
|
+
...anthropicOptions.webSearch.userLocation && {
|
|
668
|
+
user_location: {
|
|
669
|
+
type: anthropicOptions.webSearch.userLocation.type,
|
|
670
|
+
country: anthropicOptions.webSearch.userLocation.country,
|
|
671
|
+
city: anthropicOptions.webSearch.userLocation.city,
|
|
672
|
+
region: anthropicOptions.webSearch.userLocation.region,
|
|
673
|
+
timezone: anthropicOptions.webSearch.userLocation.timezone
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
modifiedTools = tools ? [...tools, webSearchTool] : [webSearchTool];
|
|
601
678
|
}
|
|
679
|
+
const {
|
|
680
|
+
tools: anthropicTools2,
|
|
681
|
+
toolChoice: anthropicToolChoice,
|
|
682
|
+
toolWarnings,
|
|
683
|
+
betas: toolsBetas
|
|
684
|
+
} = prepareTools(
|
|
685
|
+
jsonResponseTool != null ? {
|
|
686
|
+
tools: [jsonResponseTool],
|
|
687
|
+
toolChoice: { type: "tool", toolName: jsonResponseTool.name }
|
|
688
|
+
} : { tools: modifiedTools, toolChoice: modifiedToolChoice }
|
|
689
|
+
);
|
|
690
|
+
return {
|
|
691
|
+
args: {
|
|
692
|
+
...baseArgs,
|
|
693
|
+
tools: anthropicTools2,
|
|
694
|
+
tool_choice: anthropicToolChoice
|
|
695
|
+
},
|
|
696
|
+
warnings: [...warnings, ...toolWarnings],
|
|
697
|
+
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
|
|
698
|
+
jsonResponseTool
|
|
699
|
+
};
|
|
602
700
|
}
|
|
603
701
|
async getHeaders({
|
|
604
702
|
betas,
|
|
@@ -619,8 +717,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
619
717
|
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
620
718
|
}
|
|
621
719
|
async doGenerate(options) {
|
|
622
|
-
var _a, _b, _c, _d;
|
|
623
|
-
const { args, warnings, betas } = await this.getArgs(options);
|
|
720
|
+
var _a, _b, _c, _d, _e;
|
|
721
|
+
const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
|
|
624
722
|
const {
|
|
625
723
|
responseHeaders,
|
|
626
724
|
value: response,
|
|
@@ -636,69 +734,118 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
636
734
|
abortSignal: options.abortSignal,
|
|
637
735
|
fetch: this.config.fetch
|
|
638
736
|
});
|
|
639
|
-
const
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
737
|
+
const content = [];
|
|
738
|
+
for (const part of response.content) {
|
|
739
|
+
switch (part.type) {
|
|
740
|
+
case "text": {
|
|
741
|
+
if (jsonResponseTool == null) {
|
|
742
|
+
content.push({ type: "text", text: part.text });
|
|
743
|
+
}
|
|
744
|
+
break;
|
|
745
|
+
}
|
|
746
|
+
case "thinking": {
|
|
747
|
+
content.push({
|
|
748
|
+
type: "reasoning",
|
|
749
|
+
text: part.thinking,
|
|
750
|
+
providerMetadata: {
|
|
751
|
+
anthropic: {
|
|
752
|
+
signature: part.signature
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
});
|
|
756
|
+
break;
|
|
757
|
+
}
|
|
758
|
+
case "redacted_thinking": {
|
|
759
|
+
content.push({
|
|
760
|
+
type: "reasoning",
|
|
761
|
+
text: "",
|
|
762
|
+
providerMetadata: {
|
|
763
|
+
anthropic: {
|
|
764
|
+
redactedData: part.data
|
|
765
|
+
}
|
|
766
|
+
}
|
|
656
767
|
});
|
|
768
|
+
break;
|
|
769
|
+
}
|
|
770
|
+
case "tool_use": {
|
|
771
|
+
content.push(
|
|
772
|
+
// when a json response tool is used, the tool call becomes the text:
|
|
773
|
+
jsonResponseTool != null ? {
|
|
774
|
+
type: "text",
|
|
775
|
+
text: JSON.stringify(part.input)
|
|
776
|
+
} : {
|
|
777
|
+
type: "tool-call",
|
|
778
|
+
toolCallType: "function",
|
|
779
|
+
toolCallId: part.id,
|
|
780
|
+
toolName: part.name,
|
|
781
|
+
args: JSON.stringify(part.input)
|
|
782
|
+
}
|
|
783
|
+
);
|
|
784
|
+
break;
|
|
785
|
+
}
|
|
786
|
+
case "server_tool_use": {
|
|
787
|
+
continue;
|
|
788
|
+
}
|
|
789
|
+
case "web_search_tool_result": {
|
|
790
|
+
if (Array.isArray(part.content)) {
|
|
791
|
+
for (const result of part.content) {
|
|
792
|
+
if (result.type === "web_search_result") {
|
|
793
|
+
content.push({
|
|
794
|
+
type: "source",
|
|
795
|
+
sourceType: "url",
|
|
796
|
+
id: this.generateId(),
|
|
797
|
+
url: result.url,
|
|
798
|
+
title: result.title,
|
|
799
|
+
providerMetadata: {
|
|
800
|
+
anthropic: {
|
|
801
|
+
encryptedContent: result.encrypted_content,
|
|
802
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
} else if (part.content.type === "web_search_tool_result_error") {
|
|
809
|
+
throw new APICallError({
|
|
810
|
+
message: `Web search failed: ${part.content.error_code}`,
|
|
811
|
+
url: "web_search_api",
|
|
812
|
+
requestBodyValues: { tool_use_id: part.tool_use_id },
|
|
813
|
+
data: { error_code: part.content.error_code }
|
|
814
|
+
});
|
|
815
|
+
}
|
|
816
|
+
break;
|
|
657
817
|
}
|
|
658
818
|
}
|
|
659
819
|
}
|
|
660
|
-
const reasoning = response.content.filter(
|
|
661
|
-
(content) => content.type === "redacted_thinking" || content.type === "thinking"
|
|
662
|
-
).map(
|
|
663
|
-
(content) => content.type === "thinking" ? {
|
|
664
|
-
type: "text",
|
|
665
|
-
text: content.thinking,
|
|
666
|
-
signature: content.signature
|
|
667
|
-
} : {
|
|
668
|
-
type: "redacted",
|
|
669
|
-
data: content.data
|
|
670
|
-
}
|
|
671
|
-
);
|
|
672
820
|
return {
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
821
|
+
content,
|
|
822
|
+
finishReason: mapAnthropicStopReason({
|
|
823
|
+
finishReason: response.stop_reason,
|
|
824
|
+
isJsonResponseFromTool: jsonResponseTool != null
|
|
825
|
+
}),
|
|
677
826
|
usage: {
|
|
678
|
-
|
|
679
|
-
|
|
827
|
+
inputTokens: response.usage.input_tokens,
|
|
828
|
+
outputTokens: response.usage.output_tokens,
|
|
829
|
+
totalTokens: response.usage.input_tokens + response.usage.output_tokens,
|
|
830
|
+
cachedInputTokens: (_b = response.usage.cache_read_input_tokens) != null ? _b : void 0
|
|
680
831
|
},
|
|
681
|
-
|
|
682
|
-
|
|
832
|
+
request: { body: args },
|
|
833
|
+
response: {
|
|
834
|
+
id: (_c = response.id) != null ? _c : void 0,
|
|
835
|
+
modelId: (_d = response.model) != null ? _d : void 0,
|
|
683
836
|
headers: responseHeaders,
|
|
684
837
|
body: rawResponse
|
|
685
838
|
},
|
|
686
|
-
response: {
|
|
687
|
-
id: (_a = response.id) != null ? _a : void 0,
|
|
688
|
-
modelId: (_b = response.model) != null ? _b : void 0
|
|
689
|
-
},
|
|
690
839
|
warnings,
|
|
691
840
|
providerMetadata: {
|
|
692
841
|
anthropic: {
|
|
693
|
-
cacheCreationInputTokens: (
|
|
694
|
-
cacheReadInputTokens: (_d = response.usage.cache_read_input_tokens) != null ? _d : null
|
|
842
|
+
cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null
|
|
695
843
|
}
|
|
696
|
-
}
|
|
697
|
-
request: { body: JSON.stringify(args) }
|
|
844
|
+
}
|
|
698
845
|
};
|
|
699
846
|
}
|
|
700
847
|
async doStream(options) {
|
|
701
|
-
const { args, warnings, betas } = await this.getArgs(options);
|
|
848
|
+
const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
|
|
702
849
|
const body = { ...args, stream: true };
|
|
703
850
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
704
851
|
url: this.buildRequestUrl(true),
|
|
@@ -711,20 +858,25 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
711
858
|
abortSignal: options.abortSignal,
|
|
712
859
|
fetch: this.config.fetch
|
|
713
860
|
});
|
|
714
|
-
const { messages: rawPrompt, ...rawSettings } = args;
|
|
715
861
|
let finishReason = "unknown";
|
|
716
862
|
const usage = {
|
|
717
|
-
|
|
718
|
-
|
|
863
|
+
inputTokens: void 0,
|
|
864
|
+
outputTokens: void 0,
|
|
865
|
+
totalTokens: void 0
|
|
719
866
|
};
|
|
720
867
|
const toolCallContentBlocks = {};
|
|
721
868
|
let providerMetadata = void 0;
|
|
722
869
|
let blockType = void 0;
|
|
870
|
+
const config = this.config;
|
|
871
|
+
const generateId3 = this.generateId;
|
|
723
872
|
return {
|
|
724
873
|
stream: response.pipeThrough(
|
|
725
874
|
new TransformStream({
|
|
875
|
+
start(controller) {
|
|
876
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
877
|
+
},
|
|
726
878
|
transform(chunk, controller) {
|
|
727
|
-
var _a, _b, _c, _d;
|
|
879
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
728
880
|
if (!chunk.success) {
|
|
729
881
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
730
882
|
return;
|
|
@@ -744,9 +896,15 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
744
896
|
}
|
|
745
897
|
case "redacted_thinking": {
|
|
746
898
|
controller.enqueue({
|
|
747
|
-
type: "
|
|
748
|
-
|
|
899
|
+
type: "reasoning",
|
|
900
|
+
text: "",
|
|
901
|
+
providerMetadata: {
|
|
902
|
+
anthropic: {
|
|
903
|
+
redactedData: value.content_block.data
|
|
904
|
+
}
|
|
905
|
+
}
|
|
749
906
|
});
|
|
907
|
+
controller.enqueue({ type: "reasoning-part-finish" });
|
|
750
908
|
return;
|
|
751
909
|
}
|
|
752
910
|
case "tool_use": {
|
|
@@ -757,6 +915,40 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
757
915
|
};
|
|
758
916
|
return;
|
|
759
917
|
}
|
|
918
|
+
case "server_tool_use": {
|
|
919
|
+
return;
|
|
920
|
+
}
|
|
921
|
+
case "web_search_tool_result": {
|
|
922
|
+
if (Array.isArray(value.content_block.content)) {
|
|
923
|
+
for (const result of value.content_block.content) {
|
|
924
|
+
if (result.type === "web_search_result") {
|
|
925
|
+
controller.enqueue({
|
|
926
|
+
type: "source",
|
|
927
|
+
sourceType: "url",
|
|
928
|
+
id: generateId3(),
|
|
929
|
+
url: result.url,
|
|
930
|
+
title: result.title,
|
|
931
|
+
providerMetadata: {
|
|
932
|
+
anthropic: {
|
|
933
|
+
encryptedContent: result.encrypted_content,
|
|
934
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
} else if (value.content_block.content.type === "web_search_tool_result_error") {
|
|
941
|
+
controller.enqueue({
|
|
942
|
+
type: "error",
|
|
943
|
+
error: {
|
|
944
|
+
type: "web-search-error",
|
|
945
|
+
message: `Web search failed: ${value.content_block.content.error_code}`,
|
|
946
|
+
code: value.content_block.content.error_code
|
|
947
|
+
}
|
|
948
|
+
});
|
|
949
|
+
}
|
|
950
|
+
return;
|
|
951
|
+
}
|
|
760
952
|
default: {
|
|
761
953
|
const _exhaustiveCheck = contentBlockType;
|
|
762
954
|
throw new Error(
|
|
@@ -768,13 +960,15 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
768
960
|
case "content_block_stop": {
|
|
769
961
|
if (toolCallContentBlocks[value.index] != null) {
|
|
770
962
|
const contentBlock = toolCallContentBlocks[value.index];
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
963
|
+
if (jsonResponseTool == null) {
|
|
964
|
+
controller.enqueue({
|
|
965
|
+
type: "tool-call",
|
|
966
|
+
toolCallType: "function",
|
|
967
|
+
toolCallId: contentBlock.toolCallId,
|
|
968
|
+
toolName: contentBlock.toolName,
|
|
969
|
+
args: contentBlock.jsonText
|
|
970
|
+
});
|
|
971
|
+
}
|
|
778
972
|
delete toolCallContentBlocks[value.index];
|
|
779
973
|
}
|
|
780
974
|
blockType = void 0;
|
|
@@ -784,40 +978,60 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
784
978
|
const deltaType = value.delta.type;
|
|
785
979
|
switch (deltaType) {
|
|
786
980
|
case "text_delta": {
|
|
981
|
+
if (jsonResponseTool != null) {
|
|
982
|
+
return;
|
|
983
|
+
}
|
|
787
984
|
controller.enqueue({
|
|
788
|
-
type: "text
|
|
789
|
-
|
|
985
|
+
type: "text",
|
|
986
|
+
text: value.delta.text
|
|
790
987
|
});
|
|
791
988
|
return;
|
|
792
989
|
}
|
|
793
990
|
case "thinking_delta": {
|
|
794
991
|
controller.enqueue({
|
|
795
992
|
type: "reasoning",
|
|
796
|
-
|
|
993
|
+
text: value.delta.thinking
|
|
797
994
|
});
|
|
798
995
|
return;
|
|
799
996
|
}
|
|
800
997
|
case "signature_delta": {
|
|
801
998
|
if (blockType === "thinking") {
|
|
802
999
|
controller.enqueue({
|
|
803
|
-
type: "reasoning
|
|
804
|
-
|
|
1000
|
+
type: "reasoning",
|
|
1001
|
+
text: "",
|
|
1002
|
+
providerMetadata: {
|
|
1003
|
+
anthropic: {
|
|
1004
|
+
signature: value.delta.signature
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
805
1007
|
});
|
|
1008
|
+
controller.enqueue({ type: "reasoning-part-finish" });
|
|
806
1009
|
}
|
|
807
1010
|
return;
|
|
808
1011
|
}
|
|
809
1012
|
case "input_json_delta": {
|
|
810
1013
|
const contentBlock = toolCallContentBlocks[value.index];
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
1014
|
+
if (!contentBlock) {
|
|
1015
|
+
return;
|
|
1016
|
+
}
|
|
1017
|
+
controller.enqueue(
|
|
1018
|
+
jsonResponseTool != null ? {
|
|
1019
|
+
type: "text",
|
|
1020
|
+
text: value.delta.partial_json
|
|
1021
|
+
} : {
|
|
1022
|
+
type: "tool-call-delta",
|
|
1023
|
+
toolCallType: "function",
|
|
1024
|
+
toolCallId: contentBlock.toolCallId,
|
|
1025
|
+
toolName: contentBlock.toolName,
|
|
1026
|
+
argsTextDelta: value.delta.partial_json
|
|
1027
|
+
}
|
|
1028
|
+
);
|
|
818
1029
|
contentBlock.jsonText += value.delta.partial_json;
|
|
819
1030
|
return;
|
|
820
1031
|
}
|
|
1032
|
+
case "citations_delta": {
|
|
1033
|
+
return;
|
|
1034
|
+
}
|
|
821
1035
|
default: {
|
|
822
1036
|
const _exhaustiveCheck = deltaType;
|
|
823
1037
|
throw new Error(
|
|
@@ -827,24 +1041,27 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
827
1041
|
}
|
|
828
1042
|
}
|
|
829
1043
|
case "message_start": {
|
|
830
|
-
usage.
|
|
831
|
-
usage.
|
|
1044
|
+
usage.inputTokens = value.message.usage.input_tokens;
|
|
1045
|
+
usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
|
|
832
1046
|
providerMetadata = {
|
|
833
1047
|
anthropic: {
|
|
834
|
-
cacheCreationInputTokens: (
|
|
835
|
-
cacheReadInputTokens: (_b = value.message.usage.cache_read_input_tokens) != null ? _b : null
|
|
1048
|
+
cacheCreationInputTokens: (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null
|
|
836
1049
|
}
|
|
837
1050
|
};
|
|
838
1051
|
controller.enqueue({
|
|
839
1052
|
type: "response-metadata",
|
|
840
|
-
id: (
|
|
841
|
-
modelId: (
|
|
1053
|
+
id: (_d = value.message.id) != null ? _d : void 0,
|
|
1054
|
+
modelId: (_e = value.message.model) != null ? _e : void 0
|
|
842
1055
|
});
|
|
843
1056
|
return;
|
|
844
1057
|
}
|
|
845
1058
|
case "message_delta": {
|
|
846
|
-
usage.
|
|
847
|
-
|
|
1059
|
+
usage.outputTokens = value.usage.output_tokens;
|
|
1060
|
+
usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
|
|
1061
|
+
finishReason = mapAnthropicStopReason({
|
|
1062
|
+
finishReason: value.delta.stop_reason,
|
|
1063
|
+
isJsonResponseFromTool: jsonResponseTool != null
|
|
1064
|
+
});
|
|
848
1065
|
return;
|
|
849
1066
|
}
|
|
850
1067
|
case "message_stop": {
|
|
@@ -868,142 +1085,201 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
868
1085
|
}
|
|
869
1086
|
})
|
|
870
1087
|
),
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
warnings,
|
|
874
|
-
request: { body: JSON.stringify(body) }
|
|
1088
|
+
request: { body },
|
|
1089
|
+
response: { headers: responseHeaders }
|
|
875
1090
|
};
|
|
876
1091
|
}
|
|
877
1092
|
};
|
|
878
|
-
var anthropicMessagesResponseSchema =
|
|
879
|
-
type:
|
|
880
|
-
id:
|
|
881
|
-
model:
|
|
882
|
-
content:
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
type:
|
|
886
|
-
text:
|
|
1093
|
+
var anthropicMessagesResponseSchema = z3.object({
|
|
1094
|
+
type: z3.literal("message"),
|
|
1095
|
+
id: z3.string().nullish(),
|
|
1096
|
+
model: z3.string().nullish(),
|
|
1097
|
+
content: z3.array(
|
|
1098
|
+
z3.discriminatedUnion("type", [
|
|
1099
|
+
z3.object({
|
|
1100
|
+
type: z3.literal("text"),
|
|
1101
|
+
text: z3.string()
|
|
1102
|
+
}),
|
|
1103
|
+
z3.object({
|
|
1104
|
+
type: z3.literal("thinking"),
|
|
1105
|
+
thinking: z3.string(),
|
|
1106
|
+
signature: z3.string()
|
|
887
1107
|
}),
|
|
888
|
-
|
|
889
|
-
type:
|
|
890
|
-
|
|
891
|
-
signature: z2.string()
|
|
1108
|
+
z3.object({
|
|
1109
|
+
type: z3.literal("redacted_thinking"),
|
|
1110
|
+
data: z3.string()
|
|
892
1111
|
}),
|
|
893
|
-
|
|
894
|
-
type:
|
|
895
|
-
|
|
1112
|
+
z3.object({
|
|
1113
|
+
type: z3.literal("tool_use"),
|
|
1114
|
+
id: z3.string(),
|
|
1115
|
+
name: z3.string(),
|
|
1116
|
+
input: z3.unknown()
|
|
896
1117
|
}),
|
|
897
|
-
|
|
898
|
-
type:
|
|
899
|
-
id:
|
|
900
|
-
name:
|
|
901
|
-
input:
|
|
1118
|
+
z3.object({
|
|
1119
|
+
type: z3.literal("server_tool_use"),
|
|
1120
|
+
id: z3.string(),
|
|
1121
|
+
name: z3.string(),
|
|
1122
|
+
input: z3.record(z3.unknown()).nullish()
|
|
1123
|
+
}),
|
|
1124
|
+
z3.object({
|
|
1125
|
+
type: z3.literal("web_search_tool_result"),
|
|
1126
|
+
tool_use_id: z3.string(),
|
|
1127
|
+
content: z3.union([
|
|
1128
|
+
z3.array(
|
|
1129
|
+
z3.object({
|
|
1130
|
+
type: z3.literal("web_search_result"),
|
|
1131
|
+
url: z3.string(),
|
|
1132
|
+
title: z3.string(),
|
|
1133
|
+
encrypted_content: z3.string(),
|
|
1134
|
+
page_age: z3.string().nullish()
|
|
1135
|
+
})
|
|
1136
|
+
),
|
|
1137
|
+
z3.object({
|
|
1138
|
+
type: z3.literal("web_search_tool_result_error"),
|
|
1139
|
+
error_code: z3.string()
|
|
1140
|
+
})
|
|
1141
|
+
])
|
|
902
1142
|
})
|
|
903
1143
|
])
|
|
904
1144
|
),
|
|
905
|
-
stop_reason:
|
|
906
|
-
usage:
|
|
907
|
-
input_tokens:
|
|
908
|
-
output_tokens:
|
|
909
|
-
cache_creation_input_tokens:
|
|
910
|
-
cache_read_input_tokens:
|
|
1145
|
+
stop_reason: z3.string().nullish(),
|
|
1146
|
+
usage: z3.object({
|
|
1147
|
+
input_tokens: z3.number(),
|
|
1148
|
+
output_tokens: z3.number(),
|
|
1149
|
+
cache_creation_input_tokens: z3.number().nullish(),
|
|
1150
|
+
cache_read_input_tokens: z3.number().nullish(),
|
|
1151
|
+
server_tool_use: z3.object({
|
|
1152
|
+
web_search_requests: z3.number()
|
|
1153
|
+
}).nullish()
|
|
911
1154
|
})
|
|
912
1155
|
});
|
|
913
|
-
var anthropicMessagesChunkSchema =
|
|
914
|
-
|
|
915
|
-
type:
|
|
916
|
-
message:
|
|
917
|
-
id:
|
|
918
|
-
model:
|
|
919
|
-
usage:
|
|
920
|
-
input_tokens:
|
|
921
|
-
output_tokens:
|
|
922
|
-
cache_creation_input_tokens:
|
|
923
|
-
cache_read_input_tokens:
|
|
1156
|
+
var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
|
|
1157
|
+
z3.object({
|
|
1158
|
+
type: z3.literal("message_start"),
|
|
1159
|
+
message: z3.object({
|
|
1160
|
+
id: z3.string().nullish(),
|
|
1161
|
+
model: z3.string().nullish(),
|
|
1162
|
+
usage: z3.object({
|
|
1163
|
+
input_tokens: z3.number(),
|
|
1164
|
+
output_tokens: z3.number(),
|
|
1165
|
+
cache_creation_input_tokens: z3.number().nullish(),
|
|
1166
|
+
cache_read_input_tokens: z3.number().nullish()
|
|
924
1167
|
})
|
|
925
1168
|
})
|
|
926
1169
|
}),
|
|
927
|
-
|
|
928
|
-
type:
|
|
929
|
-
index:
|
|
930
|
-
content_block:
|
|
931
|
-
|
|
932
|
-
type:
|
|
933
|
-
text:
|
|
1170
|
+
z3.object({
|
|
1171
|
+
type: z3.literal("content_block_start"),
|
|
1172
|
+
index: z3.number(),
|
|
1173
|
+
content_block: z3.discriminatedUnion("type", [
|
|
1174
|
+
z3.object({
|
|
1175
|
+
type: z3.literal("text"),
|
|
1176
|
+
text: z3.string()
|
|
1177
|
+
}),
|
|
1178
|
+
z3.object({
|
|
1179
|
+
type: z3.literal("thinking"),
|
|
1180
|
+
thinking: z3.string()
|
|
934
1181
|
}),
|
|
935
|
-
|
|
936
|
-
type:
|
|
937
|
-
|
|
1182
|
+
z3.object({
|
|
1183
|
+
type: z3.literal("tool_use"),
|
|
1184
|
+
id: z3.string(),
|
|
1185
|
+
name: z3.string()
|
|
938
1186
|
}),
|
|
939
|
-
|
|
940
|
-
type:
|
|
941
|
-
|
|
942
|
-
name: z2.string()
|
|
1187
|
+
z3.object({
|
|
1188
|
+
type: z3.literal("redacted_thinking"),
|
|
1189
|
+
data: z3.string()
|
|
943
1190
|
}),
|
|
944
|
-
|
|
945
|
-
type:
|
|
946
|
-
|
|
1191
|
+
z3.object({
|
|
1192
|
+
type: z3.literal("server_tool_use"),
|
|
1193
|
+
id: z3.string(),
|
|
1194
|
+
name: z3.string(),
|
|
1195
|
+
input: z3.record(z3.unknown()).nullish()
|
|
1196
|
+
}),
|
|
1197
|
+
z3.object({
|
|
1198
|
+
type: z3.literal("web_search_tool_result"),
|
|
1199
|
+
tool_use_id: z3.string(),
|
|
1200
|
+
content: z3.union([
|
|
1201
|
+
z3.array(
|
|
1202
|
+
z3.object({
|
|
1203
|
+
type: z3.literal("web_search_result"),
|
|
1204
|
+
url: z3.string(),
|
|
1205
|
+
title: z3.string(),
|
|
1206
|
+
encrypted_content: z3.string(),
|
|
1207
|
+
page_age: z3.string().nullish()
|
|
1208
|
+
})
|
|
1209
|
+
),
|
|
1210
|
+
z3.object({
|
|
1211
|
+
type: z3.literal("web_search_tool_result_error"),
|
|
1212
|
+
error_code: z3.string()
|
|
1213
|
+
})
|
|
1214
|
+
])
|
|
947
1215
|
})
|
|
948
1216
|
])
|
|
949
1217
|
}),
|
|
950
|
-
|
|
951
|
-
type:
|
|
952
|
-
index:
|
|
953
|
-
delta:
|
|
954
|
-
|
|
955
|
-
type:
|
|
956
|
-
partial_json:
|
|
1218
|
+
z3.object({
|
|
1219
|
+
type: z3.literal("content_block_delta"),
|
|
1220
|
+
index: z3.number(),
|
|
1221
|
+
delta: z3.discriminatedUnion("type", [
|
|
1222
|
+
z3.object({
|
|
1223
|
+
type: z3.literal("input_json_delta"),
|
|
1224
|
+
partial_json: z3.string()
|
|
1225
|
+
}),
|
|
1226
|
+
z3.object({
|
|
1227
|
+
type: z3.literal("text_delta"),
|
|
1228
|
+
text: z3.string()
|
|
957
1229
|
}),
|
|
958
|
-
|
|
959
|
-
type:
|
|
960
|
-
|
|
1230
|
+
z3.object({
|
|
1231
|
+
type: z3.literal("thinking_delta"),
|
|
1232
|
+
thinking: z3.string()
|
|
961
1233
|
}),
|
|
962
|
-
|
|
963
|
-
type:
|
|
964
|
-
|
|
1234
|
+
z3.object({
|
|
1235
|
+
type: z3.literal("signature_delta"),
|
|
1236
|
+
signature: z3.string()
|
|
965
1237
|
}),
|
|
966
|
-
|
|
967
|
-
type:
|
|
968
|
-
|
|
1238
|
+
z3.object({
|
|
1239
|
+
type: z3.literal("citations_delta"),
|
|
1240
|
+
citation: z3.object({
|
|
1241
|
+
type: z3.literal("web_search_result_location"),
|
|
1242
|
+
cited_text: z3.string(),
|
|
1243
|
+
url: z3.string(),
|
|
1244
|
+
title: z3.string(),
|
|
1245
|
+
encrypted_index: z3.string()
|
|
1246
|
+
})
|
|
969
1247
|
})
|
|
970
1248
|
])
|
|
971
1249
|
}),
|
|
972
|
-
|
|
973
|
-
type:
|
|
974
|
-
index:
|
|
1250
|
+
z3.object({
|
|
1251
|
+
type: z3.literal("content_block_stop"),
|
|
1252
|
+
index: z3.number()
|
|
975
1253
|
}),
|
|
976
|
-
|
|
977
|
-
type:
|
|
978
|
-
error:
|
|
979
|
-
type:
|
|
980
|
-
message:
|
|
1254
|
+
z3.object({
|
|
1255
|
+
type: z3.literal("error"),
|
|
1256
|
+
error: z3.object({
|
|
1257
|
+
type: z3.string(),
|
|
1258
|
+
message: z3.string()
|
|
981
1259
|
})
|
|
982
1260
|
}),
|
|
983
|
-
|
|
984
|
-
type:
|
|
985
|
-
delta:
|
|
986
|
-
usage:
|
|
1261
|
+
z3.object({
|
|
1262
|
+
type: z3.literal("message_delta"),
|
|
1263
|
+
delta: z3.object({ stop_reason: z3.string().nullish() }),
|
|
1264
|
+
usage: z3.object({ output_tokens: z3.number() })
|
|
987
1265
|
}),
|
|
988
|
-
|
|
989
|
-
type:
|
|
1266
|
+
z3.object({
|
|
1267
|
+
type: z3.literal("message_stop")
|
|
990
1268
|
}),
|
|
991
|
-
|
|
992
|
-
type:
|
|
1269
|
+
z3.object({
|
|
1270
|
+
type: z3.literal("ping")
|
|
993
1271
|
})
|
|
994
1272
|
]);
|
|
995
|
-
var
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
budgetTokens: z2.number().optional()
|
|
999
|
-
}).optional()
|
|
1273
|
+
var anthropicReasoningMetadataSchema = z3.object({
|
|
1274
|
+
signature: z3.string().optional(),
|
|
1275
|
+
redactedData: z3.string().optional()
|
|
1000
1276
|
});
|
|
1001
1277
|
|
|
1002
1278
|
// src/anthropic-tools.ts
|
|
1003
|
-
import { z as
|
|
1004
|
-
var Bash20241022Parameters =
|
|
1005
|
-
command:
|
|
1006
|
-
restart:
|
|
1279
|
+
import { z as z4 } from "zod";
|
|
1280
|
+
var Bash20241022Parameters = z4.object({
|
|
1281
|
+
command: z4.string(),
|
|
1282
|
+
restart: z4.boolean().optional()
|
|
1007
1283
|
});
|
|
1008
1284
|
function bashTool_20241022(options = {}) {
|
|
1009
1285
|
return {
|
|
@@ -1015,9 +1291,9 @@ function bashTool_20241022(options = {}) {
|
|
|
1015
1291
|
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
1016
1292
|
};
|
|
1017
1293
|
}
|
|
1018
|
-
var Bash20250124Parameters =
|
|
1019
|
-
command:
|
|
1020
|
-
restart:
|
|
1294
|
+
var Bash20250124Parameters = z4.object({
|
|
1295
|
+
command: z4.string(),
|
|
1296
|
+
restart: z4.boolean().optional()
|
|
1021
1297
|
});
|
|
1022
1298
|
function bashTool_20250124(options = {}) {
|
|
1023
1299
|
return {
|
|
@@ -1029,14 +1305,14 @@ function bashTool_20250124(options = {}) {
|
|
|
1029
1305
|
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
1030
1306
|
};
|
|
1031
1307
|
}
|
|
1032
|
-
var TextEditor20241022Parameters =
|
|
1033
|
-
command:
|
|
1034
|
-
path:
|
|
1035
|
-
file_text:
|
|
1036
|
-
insert_line:
|
|
1037
|
-
new_str:
|
|
1038
|
-
old_str:
|
|
1039
|
-
view_range:
|
|
1308
|
+
var TextEditor20241022Parameters = z4.object({
|
|
1309
|
+
command: z4.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
|
|
1310
|
+
path: z4.string(),
|
|
1311
|
+
file_text: z4.string().optional(),
|
|
1312
|
+
insert_line: z4.number().int().optional(),
|
|
1313
|
+
new_str: z4.string().optional(),
|
|
1314
|
+
old_str: z4.string().optional(),
|
|
1315
|
+
view_range: z4.array(z4.number().int()).optional()
|
|
1040
1316
|
});
|
|
1041
1317
|
function textEditorTool_20241022(options = {}) {
|
|
1042
1318
|
return {
|
|
@@ -1048,14 +1324,14 @@ function textEditorTool_20241022(options = {}) {
|
|
|
1048
1324
|
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
1049
1325
|
};
|
|
1050
1326
|
}
|
|
1051
|
-
var TextEditor20250124Parameters =
|
|
1052
|
-
command:
|
|
1053
|
-
path:
|
|
1054
|
-
file_text:
|
|
1055
|
-
insert_line:
|
|
1056
|
-
new_str:
|
|
1057
|
-
old_str:
|
|
1058
|
-
view_range:
|
|
1327
|
+
var TextEditor20250124Parameters = z4.object({
|
|
1328
|
+
command: z4.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
|
|
1329
|
+
path: z4.string(),
|
|
1330
|
+
file_text: z4.string().optional(),
|
|
1331
|
+
insert_line: z4.number().int().optional(),
|
|
1332
|
+
new_str: z4.string().optional(),
|
|
1333
|
+
old_str: z4.string().optional(),
|
|
1334
|
+
view_range: z4.array(z4.number().int()).optional()
|
|
1059
1335
|
});
|
|
1060
1336
|
function textEditorTool_20250124(options = {}) {
|
|
1061
1337
|
return {
|
|
@@ -1067,8 +1343,8 @@ function textEditorTool_20250124(options = {}) {
|
|
|
1067
1343
|
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
1068
1344
|
};
|
|
1069
1345
|
}
|
|
1070
|
-
var Computer20241022Parameters =
|
|
1071
|
-
action:
|
|
1346
|
+
var Computer20241022Parameters = z4.object({
|
|
1347
|
+
action: z4.enum([
|
|
1072
1348
|
"key",
|
|
1073
1349
|
"type",
|
|
1074
1350
|
"mouse_move",
|
|
@@ -1080,8 +1356,8 @@ var Computer20241022Parameters = z3.object({
|
|
|
1080
1356
|
"screenshot",
|
|
1081
1357
|
"cursor_position"
|
|
1082
1358
|
]),
|
|
1083
|
-
coordinate:
|
|
1084
|
-
text:
|
|
1359
|
+
coordinate: z4.array(z4.number().int()).optional(),
|
|
1360
|
+
text: z4.string().optional()
|
|
1085
1361
|
});
|
|
1086
1362
|
function computerTool_20241022(options) {
|
|
1087
1363
|
return {
|
|
@@ -1097,8 +1373,8 @@ function computerTool_20241022(options) {
|
|
|
1097
1373
|
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
1098
1374
|
};
|
|
1099
1375
|
}
|
|
1100
|
-
var Computer20250124Parameters =
|
|
1101
|
-
action:
|
|
1376
|
+
var Computer20250124Parameters = z4.object({
|
|
1377
|
+
action: z4.enum([
|
|
1102
1378
|
"key",
|
|
1103
1379
|
"hold_key",
|
|
1104
1380
|
"type",
|
|
@@ -1116,12 +1392,12 @@ var Computer20250124Parameters = z3.object({
|
|
|
1116
1392
|
"wait",
|
|
1117
1393
|
"screenshot"
|
|
1118
1394
|
]),
|
|
1119
|
-
coordinate:
|
|
1120
|
-
duration:
|
|
1121
|
-
scroll_amount:
|
|
1122
|
-
scroll_direction:
|
|
1123
|
-
start_coordinate:
|
|
1124
|
-
text:
|
|
1395
|
+
coordinate: z4.tuple([z4.number().int(), z4.number().int()]).optional(),
|
|
1396
|
+
duration: z4.number().optional(),
|
|
1397
|
+
scroll_amount: z4.number().optional(),
|
|
1398
|
+
scroll_direction: z4.enum(["up", "down", "left", "right"]).optional(),
|
|
1399
|
+
start_coordinate: z4.tuple([z4.number().int(), z4.number().int()]).optional(),
|
|
1400
|
+
text: z4.string().optional()
|
|
1125
1401
|
});
|
|
1126
1402
|
function computerTool_20250124(options) {
|
|
1127
1403
|
return {
|
|
@@ -1159,20 +1435,26 @@ function createAnthropic(options = {}) {
|
|
|
1159
1435
|
}),
|
|
1160
1436
|
...options.headers
|
|
1161
1437
|
});
|
|
1162
|
-
const createChatModel = (modelId
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1438
|
+
const createChatModel = (modelId) => {
|
|
1439
|
+
var _a2;
|
|
1440
|
+
return new AnthropicMessagesLanguageModel(modelId, {
|
|
1441
|
+
provider: "anthropic.messages",
|
|
1442
|
+
baseURL,
|
|
1443
|
+
headers: getHeaders,
|
|
1444
|
+
fetch: options.fetch,
|
|
1445
|
+
generateId: (_a2 = options.generateId) != null ? _a2 : generateId2,
|
|
1446
|
+
supportedUrls: () => ({
|
|
1447
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
1448
|
+
})
|
|
1449
|
+
});
|
|
1450
|
+
};
|
|
1451
|
+
const provider = function(modelId) {
|
|
1170
1452
|
if (new.target) {
|
|
1171
1453
|
throw new Error(
|
|
1172
1454
|
"The Anthropic model function cannot be called with the new keyword."
|
|
1173
1455
|
);
|
|
1174
1456
|
}
|
|
1175
|
-
return createChatModel(modelId
|
|
1457
|
+
return createChatModel(modelId);
|
|
1176
1458
|
};
|
|
1177
1459
|
provider.languageModel = createChatModel;
|
|
1178
1460
|
provider.chat = createChatModel;
|
|
@@ -1180,6 +1462,9 @@ function createAnthropic(options = {}) {
|
|
|
1180
1462
|
provider.textEmbeddingModel = (modelId) => {
|
|
1181
1463
|
throw new NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
|
|
1182
1464
|
};
|
|
1465
|
+
provider.imageModel = (modelId) => {
|
|
1466
|
+
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
1467
|
+
};
|
|
1183
1468
|
provider.tools = anthropicTools;
|
|
1184
1469
|
return provider;
|
|
1185
1470
|
}
|