@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.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { loadApiKey, withoutTrailingSlash } from "@ai-sdk/provider-utils";
|
|
|
3
3
|
|
|
4
4
|
// src/anthropic-messages-language-model.ts
|
|
5
5
|
import {
|
|
6
|
-
UnsupportedFunctionalityError as
|
|
6
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
7
7
|
} from "@ai-sdk/provider";
|
|
8
8
|
import {
|
|
9
9
|
combineHeaders,
|
|
@@ -38,6 +38,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
38
38
|
cacheControl: isCacheControlEnabled
|
|
39
39
|
}) {
|
|
40
40
|
var _a, _b, _c, _d;
|
|
41
|
+
const betas = /* @__PURE__ */ new Set();
|
|
41
42
|
const blocks = groupIntoBlocks(prompt);
|
|
42
43
|
let system = void 0;
|
|
43
44
|
const messages = [];
|
|
@@ -103,6 +104,29 @@ function convertToAnthropicMessagesPrompt({
|
|
|
103
104
|
});
|
|
104
105
|
break;
|
|
105
106
|
}
|
|
107
|
+
case "file": {
|
|
108
|
+
if (part.data instanceof URL) {
|
|
109
|
+
throw new UnsupportedFunctionalityError({
|
|
110
|
+
functionality: "Image URLs in user messages"
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
if (part.mimeType !== "application/pdf") {
|
|
114
|
+
throw new UnsupportedFunctionalityError({
|
|
115
|
+
functionality: "Non-PDF files in user messages"
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
betas.add("pdfs-2024-09-25");
|
|
119
|
+
anthropicContent.push({
|
|
120
|
+
type: "document",
|
|
121
|
+
source: {
|
|
122
|
+
type: "base64",
|
|
123
|
+
media_type: "application/pdf",
|
|
124
|
+
data: part.data
|
|
125
|
+
},
|
|
126
|
+
cache_control: cacheControl
|
|
127
|
+
});
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
106
130
|
}
|
|
107
131
|
}
|
|
108
132
|
break;
|
|
@@ -112,10 +136,31 @@ function convertToAnthropicMessagesPrompt({
|
|
|
112
136
|
const part = content[i2];
|
|
113
137
|
const isLastPart = i2 === content.length - 1;
|
|
114
138
|
const cacheControl = (_c = getCacheControl(part.providerMetadata)) != null ? _c : isLastPart ? getCacheControl(message.providerMetadata) : void 0;
|
|
139
|
+
const toolResultContent = part.content != null ? part.content.map((part2) => {
|
|
140
|
+
var _a2;
|
|
141
|
+
switch (part2.type) {
|
|
142
|
+
case "text":
|
|
143
|
+
return {
|
|
144
|
+
type: "text",
|
|
145
|
+
text: part2.text,
|
|
146
|
+
cache_control: void 0
|
|
147
|
+
};
|
|
148
|
+
case "image":
|
|
149
|
+
return {
|
|
150
|
+
type: "image",
|
|
151
|
+
source: {
|
|
152
|
+
type: "base64",
|
|
153
|
+
media_type: (_a2 = part2.mimeType) != null ? _a2 : "image/jpeg",
|
|
154
|
+
data: part2.data
|
|
155
|
+
},
|
|
156
|
+
cache_control: void 0
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
}) : JSON.stringify(part.result);
|
|
115
160
|
anthropicContent.push({
|
|
116
161
|
type: "tool_result",
|
|
117
162
|
tool_use_id: part.toolCallId,
|
|
118
|
-
content:
|
|
163
|
+
content: toolResultContent,
|
|
119
164
|
is_error: part.isError,
|
|
120
165
|
cache_control: cacheControl
|
|
121
166
|
});
|
|
@@ -176,8 +221,8 @@ function convertToAnthropicMessagesPrompt({
|
|
|
176
221
|
}
|
|
177
222
|
}
|
|
178
223
|
return {
|
|
179
|
-
system,
|
|
180
|
-
|
|
224
|
+
prompt: { system, messages },
|
|
225
|
+
betas
|
|
181
226
|
};
|
|
182
227
|
}
|
|
183
228
|
function groupIntoBlocks(prompt) {
|
|
@@ -242,6 +287,105 @@ function mapAnthropicStopReason(finishReason) {
|
|
|
242
287
|
}
|
|
243
288
|
}
|
|
244
289
|
|
|
290
|
+
// src/anthropic-prepare-tools.ts
|
|
291
|
+
import {
|
|
292
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
293
|
+
} from "@ai-sdk/provider";
|
|
294
|
+
function prepareTools(mode) {
|
|
295
|
+
var _a;
|
|
296
|
+
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
|
297
|
+
const toolWarnings = [];
|
|
298
|
+
const betas = /* @__PURE__ */ new Set();
|
|
299
|
+
if (tools == null) {
|
|
300
|
+
return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
|
|
301
|
+
}
|
|
302
|
+
const anthropicTools2 = [];
|
|
303
|
+
for (const tool of tools) {
|
|
304
|
+
switch (tool.type) {
|
|
305
|
+
case "function":
|
|
306
|
+
anthropicTools2.push({
|
|
307
|
+
name: tool.name,
|
|
308
|
+
description: tool.description,
|
|
309
|
+
input_schema: tool.parameters
|
|
310
|
+
});
|
|
311
|
+
break;
|
|
312
|
+
case "provider-defined":
|
|
313
|
+
betas.add("computer-use-2024-10-22");
|
|
314
|
+
switch (tool.id) {
|
|
315
|
+
case "anthropic.computer_20241022":
|
|
316
|
+
anthropicTools2.push({
|
|
317
|
+
name: tool.name,
|
|
318
|
+
type: "computer_20241022",
|
|
319
|
+
display_width_px: tool.args.displayWidthPx,
|
|
320
|
+
display_height_px: tool.args.displayHeightPx,
|
|
321
|
+
display_number: tool.args.displayNumber
|
|
322
|
+
});
|
|
323
|
+
break;
|
|
324
|
+
case "anthropic.text_editor_20241022":
|
|
325
|
+
anthropicTools2.push({
|
|
326
|
+
name: tool.name,
|
|
327
|
+
type: "text_editor_20241022"
|
|
328
|
+
});
|
|
329
|
+
break;
|
|
330
|
+
case "anthropic.bash_20241022":
|
|
331
|
+
anthropicTools2.push({
|
|
332
|
+
name: tool.name,
|
|
333
|
+
type: "bash_20241022"
|
|
334
|
+
});
|
|
335
|
+
break;
|
|
336
|
+
default:
|
|
337
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
break;
|
|
341
|
+
default:
|
|
342
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
const toolChoice = mode.toolChoice;
|
|
347
|
+
if (toolChoice == null) {
|
|
348
|
+
return {
|
|
349
|
+
tools: anthropicTools2,
|
|
350
|
+
tool_choice: void 0,
|
|
351
|
+
toolWarnings,
|
|
352
|
+
betas
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
const type = toolChoice.type;
|
|
356
|
+
switch (type) {
|
|
357
|
+
case "auto":
|
|
358
|
+
return {
|
|
359
|
+
tools: anthropicTools2,
|
|
360
|
+
tool_choice: { type: "auto" },
|
|
361
|
+
toolWarnings,
|
|
362
|
+
betas
|
|
363
|
+
};
|
|
364
|
+
case "required":
|
|
365
|
+
return {
|
|
366
|
+
tools: anthropicTools2,
|
|
367
|
+
tool_choice: { type: "any" },
|
|
368
|
+
toolWarnings,
|
|
369
|
+
betas
|
|
370
|
+
};
|
|
371
|
+
case "none":
|
|
372
|
+
return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
|
|
373
|
+
case "tool":
|
|
374
|
+
return {
|
|
375
|
+
tools: anthropicTools2,
|
|
376
|
+
tool_choice: { type: "tool", name: toolChoice.toolName },
|
|
377
|
+
toolWarnings,
|
|
378
|
+
betas
|
|
379
|
+
};
|
|
380
|
+
default: {
|
|
381
|
+
const _exhaustiveCheck = type;
|
|
382
|
+
throw new UnsupportedFunctionalityError2({
|
|
383
|
+
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
245
389
|
// src/anthropic-messages-language-model.ts
|
|
246
390
|
var AnthropicMessagesLanguageModel = class {
|
|
247
391
|
constructor(modelId, settings, config) {
|
|
@@ -296,7 +440,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
296
440
|
details: "JSON response format is not supported."
|
|
297
441
|
});
|
|
298
442
|
}
|
|
299
|
-
const messagesPrompt = convertToAnthropicMessagesPrompt({
|
|
443
|
+
const { prompt: messagesPrompt, betas: messagesBetas } = convertToAnthropicMessagesPrompt({
|
|
300
444
|
prompt,
|
|
301
445
|
cacheControl: (_a = this.settings.cacheControl) != null ? _a : false
|
|
302
446
|
});
|
|
@@ -317,13 +461,20 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
317
461
|
};
|
|
318
462
|
switch (type) {
|
|
319
463
|
case "regular": {
|
|
464
|
+
const {
|
|
465
|
+
tools,
|
|
466
|
+
tool_choice,
|
|
467
|
+
toolWarnings,
|
|
468
|
+
betas: toolsBetas
|
|
469
|
+
} = prepareTools(mode);
|
|
320
470
|
return {
|
|
321
|
-
args: { ...baseArgs,
|
|
322
|
-
warnings
|
|
471
|
+
args: { ...baseArgs, tools, tool_choice },
|
|
472
|
+
warnings: [...warnings, ...toolWarnings],
|
|
473
|
+
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
|
|
323
474
|
};
|
|
324
475
|
}
|
|
325
476
|
case "object-json": {
|
|
326
|
-
throw new
|
|
477
|
+
throw new UnsupportedFunctionalityError3({
|
|
327
478
|
functionality: "json-mode object generation"
|
|
328
479
|
});
|
|
329
480
|
}
|
|
@@ -335,7 +486,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
335
486
|
tools: [{ name, description, input_schema: parameters }],
|
|
336
487
|
tool_choice: { type: "tool", name }
|
|
337
488
|
},
|
|
338
|
-
warnings
|
|
489
|
+
warnings,
|
|
490
|
+
betas: messagesBetas
|
|
339
491
|
};
|
|
340
492
|
}
|
|
341
493
|
default: {
|
|
@@ -344,19 +496,25 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
344
496
|
}
|
|
345
497
|
}
|
|
346
498
|
}
|
|
347
|
-
getHeaders(
|
|
499
|
+
getHeaders({
|
|
500
|
+
betas,
|
|
501
|
+
headers
|
|
502
|
+
}) {
|
|
503
|
+
if (this.settings.cacheControl) {
|
|
504
|
+
betas.add("prompt-caching-2024-07-31");
|
|
505
|
+
}
|
|
348
506
|
return combineHeaders(
|
|
349
507
|
this.config.headers(),
|
|
350
|
-
|
|
351
|
-
|
|
508
|
+
betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {},
|
|
509
|
+
headers
|
|
352
510
|
);
|
|
353
511
|
}
|
|
354
512
|
async doGenerate(options) {
|
|
355
513
|
var _a, _b, _c, _d;
|
|
356
|
-
const { args, warnings } = await this.getArgs(options);
|
|
514
|
+
const { args, warnings, betas } = await this.getArgs(options);
|
|
357
515
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
358
516
|
url: `${this.config.baseURL}/messages`,
|
|
359
|
-
headers: this.getHeaders(options.headers),
|
|
517
|
+
headers: this.getHeaders({ betas, headers: options.headers }),
|
|
360
518
|
body: args,
|
|
361
519
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
362
520
|
successfulResponseHandler: createJsonResponseHandler(
|
|
@@ -406,15 +564,17 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
406
564
|
cacheCreationInputTokens: (_c = response.usage.cache_creation_input_tokens) != null ? _c : null,
|
|
407
565
|
cacheReadInputTokens: (_d = response.usage.cache_read_input_tokens) != null ? _d : null
|
|
408
566
|
}
|
|
409
|
-
} : void 0
|
|
567
|
+
} : void 0,
|
|
568
|
+
request: { body: JSON.stringify(args) }
|
|
410
569
|
};
|
|
411
570
|
}
|
|
412
571
|
async doStream(options) {
|
|
413
|
-
const { args, warnings } = await this.getArgs(options);
|
|
572
|
+
const { args, warnings, betas } = await this.getArgs(options);
|
|
573
|
+
const body = { ...args, stream: true };
|
|
414
574
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
415
575
|
url: `${this.config.baseURL}/messages`,
|
|
416
|
-
headers: this.getHeaders(options.headers),
|
|
417
|
-
body
|
|
576
|
+
headers: this.getHeaders({ betas, headers: options.headers }),
|
|
577
|
+
body,
|
|
418
578
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
419
579
|
successfulResponseHandler: createEventSourceResponseHandler(
|
|
420
580
|
anthropicMessagesChunkSchema
|
|
@@ -557,7 +717,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
557
717
|
),
|
|
558
718
|
rawCall: { rawPrompt, rawSettings },
|
|
559
719
|
rawResponse: { headers: responseHeaders },
|
|
560
|
-
warnings
|
|
720
|
+
warnings,
|
|
721
|
+
request: { body: JSON.stringify(body) }
|
|
561
722
|
};
|
|
562
723
|
}
|
|
563
724
|
};
|
|
@@ -653,40 +814,6 @@ var anthropicMessagesChunkSchema = z2.discriminatedUnion("type", [
|
|
|
653
814
|
type: z2.literal("ping")
|
|
654
815
|
})
|
|
655
816
|
]);
|
|
656
|
-
function prepareToolsAndToolChoice(mode) {
|
|
657
|
-
var _a;
|
|
658
|
-
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
|
659
|
-
if (tools == null) {
|
|
660
|
-
return { tools: void 0, tool_choice: void 0 };
|
|
661
|
-
}
|
|
662
|
-
const mappedTools = tools.map((tool) => ({
|
|
663
|
-
name: tool.name,
|
|
664
|
-
description: tool.description,
|
|
665
|
-
input_schema: tool.parameters
|
|
666
|
-
}));
|
|
667
|
-
const toolChoice = mode.toolChoice;
|
|
668
|
-
if (toolChoice == null) {
|
|
669
|
-
return { tools: mappedTools, tool_choice: void 0 };
|
|
670
|
-
}
|
|
671
|
-
const type = toolChoice.type;
|
|
672
|
-
switch (type) {
|
|
673
|
-
case "auto":
|
|
674
|
-
return { tools: mappedTools, tool_choice: { type: "auto" } };
|
|
675
|
-
case "required":
|
|
676
|
-
return { tools: mappedTools, tool_choice: { type: "any" } };
|
|
677
|
-
case "none":
|
|
678
|
-
return { tools: void 0, tool_choice: void 0 };
|
|
679
|
-
case "tool":
|
|
680
|
-
return {
|
|
681
|
-
tools: mappedTools,
|
|
682
|
-
tool_choice: { type: "tool", name: toolChoice.toolName }
|
|
683
|
-
};
|
|
684
|
-
default: {
|
|
685
|
-
const _exhaustiveCheck = type;
|
|
686
|
-
throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`);
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
817
|
|
|
691
818
|
// src/anthropic-facade.ts
|
|
692
819
|
var Anthropic = class {
|
|
@@ -735,6 +862,79 @@ import {
|
|
|
735
862
|
loadApiKey as loadApiKey2,
|
|
736
863
|
withoutTrailingSlash as withoutTrailingSlash2
|
|
737
864
|
} from "@ai-sdk/provider-utils";
|
|
865
|
+
|
|
866
|
+
// src/anthropic-tools.ts
|
|
867
|
+
import { z as z3 } from "zod";
|
|
868
|
+
var Bash20241022Parameters = z3.object({
|
|
869
|
+
command: z3.string(),
|
|
870
|
+
restart: z3.boolean().optional()
|
|
871
|
+
});
|
|
872
|
+
function bashTool_20241022(options = {}) {
|
|
873
|
+
return {
|
|
874
|
+
type: "provider-defined",
|
|
875
|
+
id: "anthropic.bash_20241022",
|
|
876
|
+
args: {},
|
|
877
|
+
parameters: Bash20241022Parameters,
|
|
878
|
+
execute: options.execute,
|
|
879
|
+
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
var TextEditor20241022Parameters = z3.object({
|
|
883
|
+
command: z3.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
|
|
884
|
+
path: z3.string(),
|
|
885
|
+
file_text: z3.string().optional(),
|
|
886
|
+
insert_line: z3.number().int().optional(),
|
|
887
|
+
new_str: z3.string().optional(),
|
|
888
|
+
old_str: z3.string().optional(),
|
|
889
|
+
view_range: z3.array(z3.number().int()).optional()
|
|
890
|
+
});
|
|
891
|
+
function textEditorTool_20241022(options = {}) {
|
|
892
|
+
return {
|
|
893
|
+
type: "provider-defined",
|
|
894
|
+
id: "anthropic.text_editor_20241022",
|
|
895
|
+
args: {},
|
|
896
|
+
parameters: TextEditor20241022Parameters,
|
|
897
|
+
execute: options.execute,
|
|
898
|
+
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
899
|
+
};
|
|
900
|
+
}
|
|
901
|
+
var Computer20241022Parameters = z3.object({
|
|
902
|
+
action: z3.enum([
|
|
903
|
+
"key",
|
|
904
|
+
"type",
|
|
905
|
+
"mouse_move",
|
|
906
|
+
"left_click",
|
|
907
|
+
"left_click_drag",
|
|
908
|
+
"right_click",
|
|
909
|
+
"middle_click",
|
|
910
|
+
"double_click",
|
|
911
|
+
"screenshot",
|
|
912
|
+
"cursor_position"
|
|
913
|
+
]),
|
|
914
|
+
coordinate: z3.array(z3.number().int()).optional(),
|
|
915
|
+
text: z3.string().optional()
|
|
916
|
+
});
|
|
917
|
+
function computerTool_20241022(options) {
|
|
918
|
+
return {
|
|
919
|
+
type: "provider-defined",
|
|
920
|
+
id: "anthropic.computer_20241022",
|
|
921
|
+
args: {
|
|
922
|
+
displayWidthPx: options.displayWidthPx,
|
|
923
|
+
displayHeightPx: options.displayHeightPx,
|
|
924
|
+
displayNumber: options.displayNumber
|
|
925
|
+
},
|
|
926
|
+
parameters: Computer20241022Parameters,
|
|
927
|
+
execute: options.execute,
|
|
928
|
+
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
var anthropicTools = {
|
|
932
|
+
bash_20241022: bashTool_20241022,
|
|
933
|
+
textEditor_20241022: textEditorTool_20241022,
|
|
934
|
+
computer_20241022: computerTool_20241022
|
|
935
|
+
};
|
|
936
|
+
|
|
937
|
+
// src/anthropic-provider.ts
|
|
738
938
|
function createAnthropic(options = {}) {
|
|
739
939
|
var _a, _b;
|
|
740
940
|
const baseURL = (_b = withoutTrailingSlash2((_a = options.baseURL) != null ? _a : options.baseUrl)) != null ? _b : "https://api.anthropic.com/v1";
|
|
@@ -767,6 +967,7 @@ function createAnthropic(options = {}) {
|
|
|
767
967
|
provider.textEmbeddingModel = (modelId) => {
|
|
768
968
|
throw new NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
|
|
769
969
|
};
|
|
970
|
+
provider.tools = anthropicTools;
|
|
770
971
|
return provider;
|
|
771
972
|
}
|
|
772
973
|
var anthropic = createAnthropic();
|