@ai-sdk/anthropic 0.0.51 → 0.0.54
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 +32 -0
- package/dist/index.d.mts +194 -1
- package/dist/index.d.ts +194 -1
- package/dist/index.js +254 -55
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +254 -53
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -30,7 +30,7 @@ module.exports = __toCommonJS(src_exports);
|
|
|
30
30
|
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
|
31
31
|
|
|
32
32
|
// src/anthropic-messages-language-model.ts
|
|
33
|
-
var
|
|
33
|
+
var import_provider3 = require("@ai-sdk/provider");
|
|
34
34
|
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
35
35
|
var import_zod2 = require("zod");
|
|
36
36
|
|
|
@@ -57,6 +57,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
57
57
|
cacheControl: isCacheControlEnabled
|
|
58
58
|
}) {
|
|
59
59
|
var _a, _b, _c, _d;
|
|
60
|
+
const betas = /* @__PURE__ */ new Set();
|
|
60
61
|
const blocks = groupIntoBlocks(prompt);
|
|
61
62
|
let system = void 0;
|
|
62
63
|
const messages = [];
|
|
@@ -122,6 +123,29 @@ function convertToAnthropicMessagesPrompt({
|
|
|
122
123
|
});
|
|
123
124
|
break;
|
|
124
125
|
}
|
|
126
|
+
case "file": {
|
|
127
|
+
if (part.data instanceof URL) {
|
|
128
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
|
129
|
+
functionality: "Image URLs in user messages"
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
if (part.mimeType !== "application/pdf") {
|
|
133
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
|
134
|
+
functionality: "Non-PDF files in user messages"
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
betas.add("pdfs-2024-09-25");
|
|
138
|
+
anthropicContent.push({
|
|
139
|
+
type: "document",
|
|
140
|
+
source: {
|
|
141
|
+
type: "base64",
|
|
142
|
+
media_type: "application/pdf",
|
|
143
|
+
data: part.data
|
|
144
|
+
},
|
|
145
|
+
cache_control: cacheControl
|
|
146
|
+
});
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
125
149
|
}
|
|
126
150
|
}
|
|
127
151
|
break;
|
|
@@ -131,10 +155,31 @@ function convertToAnthropicMessagesPrompt({
|
|
|
131
155
|
const part = content[i2];
|
|
132
156
|
const isLastPart = i2 === content.length - 1;
|
|
133
157
|
const cacheControl = (_c = getCacheControl(part.providerMetadata)) != null ? _c : isLastPart ? getCacheControl(message.providerMetadata) : void 0;
|
|
158
|
+
const toolResultContent = part.content != null ? part.content.map((part2) => {
|
|
159
|
+
var _a2;
|
|
160
|
+
switch (part2.type) {
|
|
161
|
+
case "text":
|
|
162
|
+
return {
|
|
163
|
+
type: "text",
|
|
164
|
+
text: part2.text,
|
|
165
|
+
cache_control: void 0
|
|
166
|
+
};
|
|
167
|
+
case "image":
|
|
168
|
+
return {
|
|
169
|
+
type: "image",
|
|
170
|
+
source: {
|
|
171
|
+
type: "base64",
|
|
172
|
+
media_type: (_a2 = part2.mimeType) != null ? _a2 : "image/jpeg",
|
|
173
|
+
data: part2.data
|
|
174
|
+
},
|
|
175
|
+
cache_control: void 0
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
}) : JSON.stringify(part.result);
|
|
134
179
|
anthropicContent.push({
|
|
135
180
|
type: "tool_result",
|
|
136
181
|
tool_use_id: part.toolCallId,
|
|
137
|
-
content:
|
|
182
|
+
content: toolResultContent,
|
|
138
183
|
is_error: part.isError,
|
|
139
184
|
cache_control: cacheControl
|
|
140
185
|
});
|
|
@@ -195,8 +240,8 @@ function convertToAnthropicMessagesPrompt({
|
|
|
195
240
|
}
|
|
196
241
|
}
|
|
197
242
|
return {
|
|
198
|
-
system,
|
|
199
|
-
|
|
243
|
+
prompt: { system, messages },
|
|
244
|
+
betas
|
|
200
245
|
};
|
|
201
246
|
}
|
|
202
247
|
function groupIntoBlocks(prompt) {
|
|
@@ -261,6 +306,103 @@ function mapAnthropicStopReason(finishReason) {
|
|
|
261
306
|
}
|
|
262
307
|
}
|
|
263
308
|
|
|
309
|
+
// src/anthropic-prepare-tools.ts
|
|
310
|
+
var import_provider2 = require("@ai-sdk/provider");
|
|
311
|
+
function prepareTools(mode) {
|
|
312
|
+
var _a;
|
|
313
|
+
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
|
314
|
+
const toolWarnings = [];
|
|
315
|
+
const betas = /* @__PURE__ */ new Set();
|
|
316
|
+
if (tools == null) {
|
|
317
|
+
return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
|
|
318
|
+
}
|
|
319
|
+
const anthropicTools2 = [];
|
|
320
|
+
for (const tool of tools) {
|
|
321
|
+
switch (tool.type) {
|
|
322
|
+
case "function":
|
|
323
|
+
anthropicTools2.push({
|
|
324
|
+
name: tool.name,
|
|
325
|
+
description: tool.description,
|
|
326
|
+
input_schema: tool.parameters
|
|
327
|
+
});
|
|
328
|
+
break;
|
|
329
|
+
case "provider-defined":
|
|
330
|
+
betas.add("computer-use-2024-10-22");
|
|
331
|
+
switch (tool.id) {
|
|
332
|
+
case "anthropic.computer_20241022":
|
|
333
|
+
anthropicTools2.push({
|
|
334
|
+
name: tool.name,
|
|
335
|
+
type: "computer_20241022",
|
|
336
|
+
display_width_px: tool.args.displayWidthPx,
|
|
337
|
+
display_height_px: tool.args.displayHeightPx,
|
|
338
|
+
display_number: tool.args.displayNumber
|
|
339
|
+
});
|
|
340
|
+
break;
|
|
341
|
+
case "anthropic.text_editor_20241022":
|
|
342
|
+
anthropicTools2.push({
|
|
343
|
+
name: tool.name,
|
|
344
|
+
type: "text_editor_20241022"
|
|
345
|
+
});
|
|
346
|
+
break;
|
|
347
|
+
case "anthropic.bash_20241022":
|
|
348
|
+
anthropicTools2.push({
|
|
349
|
+
name: tool.name,
|
|
350
|
+
type: "bash_20241022"
|
|
351
|
+
});
|
|
352
|
+
break;
|
|
353
|
+
default:
|
|
354
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
break;
|
|
358
|
+
default:
|
|
359
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
const toolChoice = mode.toolChoice;
|
|
364
|
+
if (toolChoice == null) {
|
|
365
|
+
return {
|
|
366
|
+
tools: anthropicTools2,
|
|
367
|
+
tool_choice: void 0,
|
|
368
|
+
toolWarnings,
|
|
369
|
+
betas
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
const type = toolChoice.type;
|
|
373
|
+
switch (type) {
|
|
374
|
+
case "auto":
|
|
375
|
+
return {
|
|
376
|
+
tools: anthropicTools2,
|
|
377
|
+
tool_choice: { type: "auto" },
|
|
378
|
+
toolWarnings,
|
|
379
|
+
betas
|
|
380
|
+
};
|
|
381
|
+
case "required":
|
|
382
|
+
return {
|
|
383
|
+
tools: anthropicTools2,
|
|
384
|
+
tool_choice: { type: "any" },
|
|
385
|
+
toolWarnings,
|
|
386
|
+
betas
|
|
387
|
+
};
|
|
388
|
+
case "none":
|
|
389
|
+
return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
|
|
390
|
+
case "tool":
|
|
391
|
+
return {
|
|
392
|
+
tools: anthropicTools2,
|
|
393
|
+
tool_choice: { type: "tool", name: toolChoice.toolName },
|
|
394
|
+
toolWarnings,
|
|
395
|
+
betas
|
|
396
|
+
};
|
|
397
|
+
default: {
|
|
398
|
+
const _exhaustiveCheck = type;
|
|
399
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
400
|
+
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
264
406
|
// src/anthropic-messages-language-model.ts
|
|
265
407
|
var AnthropicMessagesLanguageModel = class {
|
|
266
408
|
constructor(modelId, settings, config) {
|
|
@@ -315,7 +457,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
315
457
|
details: "JSON response format is not supported."
|
|
316
458
|
});
|
|
317
459
|
}
|
|
318
|
-
const messagesPrompt = convertToAnthropicMessagesPrompt({
|
|
460
|
+
const { prompt: messagesPrompt, betas: messagesBetas } = convertToAnthropicMessagesPrompt({
|
|
319
461
|
prompt,
|
|
320
462
|
cacheControl: (_a = this.settings.cacheControl) != null ? _a : false
|
|
321
463
|
});
|
|
@@ -336,13 +478,20 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
336
478
|
};
|
|
337
479
|
switch (type) {
|
|
338
480
|
case "regular": {
|
|
481
|
+
const {
|
|
482
|
+
tools,
|
|
483
|
+
tool_choice,
|
|
484
|
+
toolWarnings,
|
|
485
|
+
betas: toolsBetas
|
|
486
|
+
} = prepareTools(mode);
|
|
339
487
|
return {
|
|
340
|
-
args: { ...baseArgs,
|
|
341
|
-
warnings
|
|
488
|
+
args: { ...baseArgs, tools, tool_choice },
|
|
489
|
+
warnings: [...warnings, ...toolWarnings],
|
|
490
|
+
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
|
|
342
491
|
};
|
|
343
492
|
}
|
|
344
493
|
case "object-json": {
|
|
345
|
-
throw new
|
|
494
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
346
495
|
functionality: "json-mode object generation"
|
|
347
496
|
});
|
|
348
497
|
}
|
|
@@ -354,7 +503,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
354
503
|
tools: [{ name, description, input_schema: parameters }],
|
|
355
504
|
tool_choice: { type: "tool", name }
|
|
356
505
|
},
|
|
357
|
-
warnings
|
|
506
|
+
warnings,
|
|
507
|
+
betas: messagesBetas
|
|
358
508
|
};
|
|
359
509
|
}
|
|
360
510
|
default: {
|
|
@@ -363,19 +513,25 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
363
513
|
}
|
|
364
514
|
}
|
|
365
515
|
}
|
|
366
|
-
getHeaders(
|
|
516
|
+
getHeaders({
|
|
517
|
+
betas,
|
|
518
|
+
headers
|
|
519
|
+
}) {
|
|
520
|
+
if (this.settings.cacheControl) {
|
|
521
|
+
betas.add("prompt-caching-2024-07-31");
|
|
522
|
+
}
|
|
367
523
|
return (0, import_provider_utils3.combineHeaders)(
|
|
368
524
|
this.config.headers(),
|
|
369
|
-
|
|
370
|
-
|
|
525
|
+
betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {},
|
|
526
|
+
headers
|
|
371
527
|
);
|
|
372
528
|
}
|
|
373
529
|
async doGenerate(options) {
|
|
374
530
|
var _a, _b, _c, _d;
|
|
375
|
-
const { args, warnings } = await this.getArgs(options);
|
|
531
|
+
const { args, warnings, betas } = await this.getArgs(options);
|
|
376
532
|
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
|
|
377
533
|
url: `${this.config.baseURL}/messages`,
|
|
378
|
-
headers: this.getHeaders(options.headers),
|
|
534
|
+
headers: this.getHeaders({ betas, headers: options.headers }),
|
|
379
535
|
body: args,
|
|
380
536
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
381
537
|
successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
|
|
@@ -425,15 +581,17 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
425
581
|
cacheCreationInputTokens: (_c = response.usage.cache_creation_input_tokens) != null ? _c : null,
|
|
426
582
|
cacheReadInputTokens: (_d = response.usage.cache_read_input_tokens) != null ? _d : null
|
|
427
583
|
}
|
|
428
|
-
} : void 0
|
|
584
|
+
} : void 0,
|
|
585
|
+
request: { body: JSON.stringify(args) }
|
|
429
586
|
};
|
|
430
587
|
}
|
|
431
588
|
async doStream(options) {
|
|
432
|
-
const { args, warnings } = await this.getArgs(options);
|
|
589
|
+
const { args, warnings, betas } = await this.getArgs(options);
|
|
590
|
+
const body = { ...args, stream: true };
|
|
433
591
|
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
|
|
434
592
|
url: `${this.config.baseURL}/messages`,
|
|
435
|
-
headers: this.getHeaders(options.headers),
|
|
436
|
-
body
|
|
593
|
+
headers: this.getHeaders({ betas, headers: options.headers }),
|
|
594
|
+
body,
|
|
437
595
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
438
596
|
successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)(
|
|
439
597
|
anthropicMessagesChunkSchema
|
|
@@ -576,7 +734,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
576
734
|
),
|
|
577
735
|
rawCall: { rawPrompt, rawSettings },
|
|
578
736
|
rawResponse: { headers: responseHeaders },
|
|
579
|
-
warnings
|
|
737
|
+
warnings,
|
|
738
|
+
request: { body: JSON.stringify(body) }
|
|
580
739
|
};
|
|
581
740
|
}
|
|
582
741
|
};
|
|
@@ -672,40 +831,6 @@ var anthropicMessagesChunkSchema = import_zod2.z.discriminatedUnion("type", [
|
|
|
672
831
|
type: import_zod2.z.literal("ping")
|
|
673
832
|
})
|
|
674
833
|
]);
|
|
675
|
-
function prepareToolsAndToolChoice(mode) {
|
|
676
|
-
var _a;
|
|
677
|
-
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
|
678
|
-
if (tools == null) {
|
|
679
|
-
return { tools: void 0, tool_choice: void 0 };
|
|
680
|
-
}
|
|
681
|
-
const mappedTools = tools.map((tool) => ({
|
|
682
|
-
name: tool.name,
|
|
683
|
-
description: tool.description,
|
|
684
|
-
input_schema: tool.parameters
|
|
685
|
-
}));
|
|
686
|
-
const toolChoice = mode.toolChoice;
|
|
687
|
-
if (toolChoice == null) {
|
|
688
|
-
return { tools: mappedTools, tool_choice: void 0 };
|
|
689
|
-
}
|
|
690
|
-
const type = toolChoice.type;
|
|
691
|
-
switch (type) {
|
|
692
|
-
case "auto":
|
|
693
|
-
return { tools: mappedTools, tool_choice: { type: "auto" } };
|
|
694
|
-
case "required":
|
|
695
|
-
return { tools: mappedTools, tool_choice: { type: "any" } };
|
|
696
|
-
case "none":
|
|
697
|
-
return { tools: void 0, tool_choice: void 0 };
|
|
698
|
-
case "tool":
|
|
699
|
-
return {
|
|
700
|
-
tools: mappedTools,
|
|
701
|
-
tool_choice: { type: "tool", name: toolChoice.toolName }
|
|
702
|
-
};
|
|
703
|
-
default: {
|
|
704
|
-
const _exhaustiveCheck = type;
|
|
705
|
-
throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`);
|
|
706
|
-
}
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
834
|
|
|
710
835
|
// src/anthropic-facade.ts
|
|
711
836
|
var Anthropic = class {
|
|
@@ -747,8 +872,81 @@ var Anthropic = class {
|
|
|
747
872
|
};
|
|
748
873
|
|
|
749
874
|
// src/anthropic-provider.ts
|
|
750
|
-
var
|
|
875
|
+
var import_provider4 = require("@ai-sdk/provider");
|
|
751
876
|
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
|
877
|
+
|
|
878
|
+
// src/anthropic-tools.ts
|
|
879
|
+
var import_zod3 = require("zod");
|
|
880
|
+
var Bash20241022Parameters = import_zod3.z.object({
|
|
881
|
+
command: import_zod3.z.string(),
|
|
882
|
+
restart: import_zod3.z.boolean().optional()
|
|
883
|
+
});
|
|
884
|
+
function bashTool_20241022(options = {}) {
|
|
885
|
+
return {
|
|
886
|
+
type: "provider-defined",
|
|
887
|
+
id: "anthropic.bash_20241022",
|
|
888
|
+
args: {},
|
|
889
|
+
parameters: Bash20241022Parameters,
|
|
890
|
+
execute: options.execute,
|
|
891
|
+
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
892
|
+
};
|
|
893
|
+
}
|
|
894
|
+
var TextEditor20241022Parameters = import_zod3.z.object({
|
|
895
|
+
command: import_zod3.z.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
|
|
896
|
+
path: import_zod3.z.string(),
|
|
897
|
+
file_text: import_zod3.z.string().optional(),
|
|
898
|
+
insert_line: import_zod3.z.number().int().optional(),
|
|
899
|
+
new_str: import_zod3.z.string().optional(),
|
|
900
|
+
old_str: import_zod3.z.string().optional(),
|
|
901
|
+
view_range: import_zod3.z.array(import_zod3.z.number().int()).optional()
|
|
902
|
+
});
|
|
903
|
+
function textEditorTool_20241022(options = {}) {
|
|
904
|
+
return {
|
|
905
|
+
type: "provider-defined",
|
|
906
|
+
id: "anthropic.text_editor_20241022",
|
|
907
|
+
args: {},
|
|
908
|
+
parameters: TextEditor20241022Parameters,
|
|
909
|
+
execute: options.execute,
|
|
910
|
+
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
911
|
+
};
|
|
912
|
+
}
|
|
913
|
+
var Computer20241022Parameters = import_zod3.z.object({
|
|
914
|
+
action: import_zod3.z.enum([
|
|
915
|
+
"key",
|
|
916
|
+
"type",
|
|
917
|
+
"mouse_move",
|
|
918
|
+
"left_click",
|
|
919
|
+
"left_click_drag",
|
|
920
|
+
"right_click",
|
|
921
|
+
"middle_click",
|
|
922
|
+
"double_click",
|
|
923
|
+
"screenshot",
|
|
924
|
+
"cursor_position"
|
|
925
|
+
]),
|
|
926
|
+
coordinate: import_zod3.z.array(import_zod3.z.number().int()).optional(),
|
|
927
|
+
text: import_zod3.z.string().optional()
|
|
928
|
+
});
|
|
929
|
+
function computerTool_20241022(options) {
|
|
930
|
+
return {
|
|
931
|
+
type: "provider-defined",
|
|
932
|
+
id: "anthropic.computer_20241022",
|
|
933
|
+
args: {
|
|
934
|
+
displayWidthPx: options.displayWidthPx,
|
|
935
|
+
displayHeightPx: options.displayHeightPx,
|
|
936
|
+
displayNumber: options.displayNumber
|
|
937
|
+
},
|
|
938
|
+
parameters: Computer20241022Parameters,
|
|
939
|
+
execute: options.execute,
|
|
940
|
+
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
941
|
+
};
|
|
942
|
+
}
|
|
943
|
+
var anthropicTools = {
|
|
944
|
+
bash_20241022: bashTool_20241022,
|
|
945
|
+
textEditor_20241022: textEditorTool_20241022,
|
|
946
|
+
computer_20241022: computerTool_20241022
|
|
947
|
+
};
|
|
948
|
+
|
|
949
|
+
// src/anthropic-provider.ts
|
|
752
950
|
function createAnthropic(options = {}) {
|
|
753
951
|
var _a, _b;
|
|
754
952
|
const baseURL = (_b = (0, import_provider_utils5.withoutTrailingSlash)((_a = options.baseURL) != null ? _a : options.baseUrl)) != null ? _b : "https://api.anthropic.com/v1";
|
|
@@ -779,8 +977,9 @@ function createAnthropic(options = {}) {
|
|
|
779
977
|
provider.chat = createChatModel;
|
|
780
978
|
provider.messages = createChatModel;
|
|
781
979
|
provider.textEmbeddingModel = (modelId) => {
|
|
782
|
-
throw new
|
|
980
|
+
throw new import_provider4.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
|
|
783
981
|
};
|
|
982
|
+
provider.tools = anthropicTools;
|
|
784
983
|
return provider;
|
|
785
984
|
}
|
|
786
985
|
var anthropic = createAnthropic();
|