@ai-sdk/anthropic 2.0.0-canary.1 → 2.0.0-canary.3
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 +22 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +80 -101
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +69 -90
- package/dist/index.mjs.map +1 -1
- package/internal/dist/index.d.mts +2 -1
- package/internal/dist/index.d.ts +2 -1
- package/internal/dist/index.js +77 -98
- package/internal/dist/index.js.map +1 -1
- package/internal/dist/index.mjs +69 -90
- package/internal/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# @ai-sdk/anthropic
|
|
2
2
|
|
|
3
|
+
## 2.0.0-canary.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 61ab528: Add support for URL-based PDF documents in the Anthropic provider
|
|
8
|
+
- Updated dependencies [a166433]
|
|
9
|
+
- Updated dependencies [abf9a79]
|
|
10
|
+
- Updated dependencies [9f95b35]
|
|
11
|
+
- Updated dependencies [0a87932]
|
|
12
|
+
- Updated dependencies [6dc848c]
|
|
13
|
+
- @ai-sdk/provider-utils@3.0.0-canary.3
|
|
14
|
+
- @ai-sdk/provider@2.0.0-canary.2
|
|
15
|
+
|
|
16
|
+
## 2.0.0-canary.2
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- Updated dependencies [c57e248]
|
|
21
|
+
- Updated dependencies [33f4a6a]
|
|
22
|
+
- @ai-sdk/provider@2.0.0-canary.1
|
|
23
|
+
- @ai-sdk/provider-utils@3.0.0-canary.2
|
|
24
|
+
|
|
3
25
|
## 2.0.0-canary.1
|
|
4
26
|
|
|
5
27
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -27,11 +27,11 @@ module.exports = __toCommonJS(src_exports);
|
|
|
27
27
|
|
|
28
28
|
// src/anthropic-provider.ts
|
|
29
29
|
var import_provider4 = require("@ai-sdk/provider");
|
|
30
|
-
var
|
|
30
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
31
31
|
|
|
32
32
|
// src/anthropic-messages-language-model.ts
|
|
33
33
|
var import_provider3 = require("@ai-sdk/provider");
|
|
34
|
-
var
|
|
34
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
35
35
|
var import_zod2 = require("zod");
|
|
36
36
|
|
|
37
37
|
// src/anthropic-error.ts
|
|
@@ -51,13 +51,15 @@ var anthropicFailedResponseHandler = (0, import_provider_utils.createJsonErrorRe
|
|
|
51
51
|
|
|
52
52
|
// src/anthropic-prepare-tools.ts
|
|
53
53
|
var import_provider = require("@ai-sdk/provider");
|
|
54
|
-
function prepareTools(
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
function prepareTools({
|
|
55
|
+
tools,
|
|
56
|
+
toolChoice
|
|
57
|
+
}) {
|
|
58
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
57
59
|
const toolWarnings = [];
|
|
58
60
|
const betas = /* @__PURE__ */ new Set();
|
|
59
61
|
if (tools == null) {
|
|
60
|
-
return { tools: void 0,
|
|
62
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
|
|
61
63
|
}
|
|
62
64
|
const anthropicTools2 = [];
|
|
63
65
|
for (const tool of tools) {
|
|
@@ -129,11 +131,10 @@ function prepareTools(mode) {
|
|
|
129
131
|
break;
|
|
130
132
|
}
|
|
131
133
|
}
|
|
132
|
-
const toolChoice = mode.toolChoice;
|
|
133
134
|
if (toolChoice == null) {
|
|
134
135
|
return {
|
|
135
136
|
tools: anthropicTools2,
|
|
136
|
-
|
|
137
|
+
toolChoice: void 0,
|
|
137
138
|
toolWarnings,
|
|
138
139
|
betas
|
|
139
140
|
};
|
|
@@ -143,30 +144,30 @@ function prepareTools(mode) {
|
|
|
143
144
|
case "auto":
|
|
144
145
|
return {
|
|
145
146
|
tools: anthropicTools2,
|
|
146
|
-
|
|
147
|
+
toolChoice: { type: "auto" },
|
|
147
148
|
toolWarnings,
|
|
148
149
|
betas
|
|
149
150
|
};
|
|
150
151
|
case "required":
|
|
151
152
|
return {
|
|
152
153
|
tools: anthropicTools2,
|
|
153
|
-
|
|
154
|
+
toolChoice: { type: "any" },
|
|
154
155
|
toolWarnings,
|
|
155
156
|
betas
|
|
156
157
|
};
|
|
157
158
|
case "none":
|
|
158
|
-
return { tools: void 0,
|
|
159
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
|
|
159
160
|
case "tool":
|
|
160
161
|
return {
|
|
161
162
|
tools: anthropicTools2,
|
|
162
|
-
|
|
163
|
+
toolChoice: { type: "tool", name: toolChoice.toolName },
|
|
163
164
|
toolWarnings,
|
|
164
165
|
betas
|
|
165
166
|
};
|
|
166
167
|
default: {
|
|
167
168
|
const _exhaustiveCheck = type;
|
|
168
169
|
throw new import_provider.UnsupportedFunctionalityError({
|
|
169
|
-
functionality: `
|
|
170
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
170
171
|
});
|
|
171
172
|
}
|
|
172
173
|
}
|
|
@@ -174,13 +175,12 @@ function prepareTools(mode) {
|
|
|
174
175
|
|
|
175
176
|
// src/convert-to-anthropic-messages-prompt.ts
|
|
176
177
|
var import_provider2 = require("@ai-sdk/provider");
|
|
177
|
-
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
178
178
|
function convertToAnthropicMessagesPrompt({
|
|
179
179
|
prompt,
|
|
180
180
|
sendReasoning,
|
|
181
181
|
warnings
|
|
182
182
|
}) {
|
|
183
|
-
var _a, _b, _c
|
|
183
|
+
var _a, _b, _c;
|
|
184
184
|
const betas = /* @__PURE__ */ new Set();
|
|
185
185
|
const blocks = groupIntoBlocks(prompt);
|
|
186
186
|
let system = void 0;
|
|
@@ -202,10 +202,10 @@ function convertToAnthropicMessagesPrompt({
|
|
|
202
202
|
functionality: "Multiple system messages that are separated by user/assistant messages"
|
|
203
203
|
});
|
|
204
204
|
}
|
|
205
|
-
system = block.messages.map(({ content,
|
|
205
|
+
system = block.messages.map(({ content, providerOptions }) => ({
|
|
206
206
|
type: "text",
|
|
207
207
|
text: content,
|
|
208
|
-
cache_control: getCacheControl(
|
|
208
|
+
cache_control: getCacheControl(providerOptions)
|
|
209
209
|
}));
|
|
210
210
|
break;
|
|
211
211
|
}
|
|
@@ -218,7 +218,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
218
218
|
for (let j = 0; j < content.length; j++) {
|
|
219
219
|
const part = content[j];
|
|
220
220
|
const isLastPart = j === content.length - 1;
|
|
221
|
-
const cacheControl = (_a = getCacheControl(part.
|
|
221
|
+
const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
222
222
|
switch (part.type) {
|
|
223
223
|
case "text": {
|
|
224
224
|
anthropicContent.push({
|
|
@@ -228,42 +228,39 @@ function convertToAnthropicMessagesPrompt({
|
|
|
228
228
|
});
|
|
229
229
|
break;
|
|
230
230
|
}
|
|
231
|
-
case "image": {
|
|
232
|
-
anthropicContent.push({
|
|
233
|
-
type: "image",
|
|
234
|
-
source: part.image instanceof URL ? {
|
|
235
|
-
type: "url",
|
|
236
|
-
url: part.image.toString()
|
|
237
|
-
} : {
|
|
238
|
-
type: "base64",
|
|
239
|
-
media_type: (_b = part.mimeType) != null ? _b : "image/jpeg",
|
|
240
|
-
data: (0, import_provider_utils2.convertUint8ArrayToBase64)(part.image)
|
|
241
|
-
},
|
|
242
|
-
cache_control: cacheControl
|
|
243
|
-
});
|
|
244
|
-
break;
|
|
245
|
-
}
|
|
246
231
|
case "file": {
|
|
247
|
-
if (part.
|
|
248
|
-
|
|
249
|
-
|
|
232
|
+
if (part.mediaType.startsWith("image/")) {
|
|
233
|
+
anthropicContent.push({
|
|
234
|
+
type: "image",
|
|
235
|
+
source: part.data instanceof URL ? {
|
|
236
|
+
type: "url",
|
|
237
|
+
url: part.data.toString()
|
|
238
|
+
} : {
|
|
239
|
+
type: "base64",
|
|
240
|
+
media_type: part.mediaType === "image/*" ? "image/jpeg" : part.mediaType,
|
|
241
|
+
data: part.data
|
|
242
|
+
},
|
|
243
|
+
cache_control: cacheControl
|
|
250
244
|
});
|
|
251
|
-
}
|
|
252
|
-
|
|
245
|
+
} else if (part.mediaType === "application/pdf") {
|
|
246
|
+
betas.add("pdfs-2024-09-25");
|
|
247
|
+
anthropicContent.push({
|
|
248
|
+
type: "document",
|
|
249
|
+
source: part.data instanceof URL ? {
|
|
250
|
+
type: "url",
|
|
251
|
+
url: part.data.toString()
|
|
252
|
+
} : {
|
|
253
|
+
type: "base64",
|
|
254
|
+
media_type: "application/pdf",
|
|
255
|
+
data: part.data
|
|
256
|
+
},
|
|
257
|
+
cache_control: cacheControl
|
|
258
|
+
});
|
|
259
|
+
} else {
|
|
253
260
|
throw new import_provider2.UnsupportedFunctionalityError({
|
|
254
|
-
functionality:
|
|
261
|
+
functionality: `media type: ${part.mediaType}`
|
|
255
262
|
});
|
|
256
263
|
}
|
|
257
|
-
betas.add("pdfs-2024-09-25");
|
|
258
|
-
anthropicContent.push({
|
|
259
|
-
type: "document",
|
|
260
|
-
source: {
|
|
261
|
-
type: "base64",
|
|
262
|
-
media_type: "application/pdf",
|
|
263
|
-
data: part.data
|
|
264
|
-
},
|
|
265
|
-
cache_control: cacheControl
|
|
266
|
-
});
|
|
267
264
|
break;
|
|
268
265
|
}
|
|
269
266
|
}
|
|
@@ -274,7 +271,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
274
271
|
for (let i2 = 0; i2 < content.length; i2++) {
|
|
275
272
|
const part = content[i2];
|
|
276
273
|
const isLastPart = i2 === content.length - 1;
|
|
277
|
-
const cacheControl = (
|
|
274
|
+
const cacheControl = (_b = getCacheControl(part.providerOptions)) != null ? _b : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
278
275
|
const toolResultContent = part.content != null ? part.content.map((part2) => {
|
|
279
276
|
var _a2;
|
|
280
277
|
switch (part2.type) {
|
|
@@ -289,7 +286,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
289
286
|
type: "image",
|
|
290
287
|
source: {
|
|
291
288
|
type: "base64",
|
|
292
|
-
media_type: (_a2 = part2.
|
|
289
|
+
media_type: (_a2 = part2.mediaType) != null ? _a2 : "image/jpeg",
|
|
293
290
|
data: part2.data
|
|
294
291
|
},
|
|
295
292
|
cache_control: void 0
|
|
@@ -324,7 +321,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
324
321
|
for (let k = 0; k < content.length; k++) {
|
|
325
322
|
const part = content[k];
|
|
326
323
|
const isLastContentPart = k === content.length - 1;
|
|
327
|
-
const cacheControl = (
|
|
324
|
+
const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
|
|
328
325
|
switch (part.type) {
|
|
329
326
|
case "text": {
|
|
330
327
|
anthropicContent.push({
|
|
@@ -381,7 +378,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
381
378
|
}
|
|
382
379
|
default: {
|
|
383
380
|
const _exhaustiveCheck = type;
|
|
384
|
-
throw new Error(`
|
|
381
|
+
throw new Error(`content type: ${_exhaustiveCheck}`);
|
|
385
382
|
}
|
|
386
383
|
}
|
|
387
384
|
}
|
|
@@ -461,6 +458,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
461
458
|
this.settings = settings;
|
|
462
459
|
this.config = config;
|
|
463
460
|
}
|
|
461
|
+
supportsUrl(url) {
|
|
462
|
+
return url.protocol === "https:";
|
|
463
|
+
}
|
|
464
464
|
get provider() {
|
|
465
465
|
return this.config.provider;
|
|
466
466
|
}
|
|
@@ -468,7 +468,6 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
468
468
|
return this.config.supportsImageUrls;
|
|
469
469
|
}
|
|
470
470
|
async getArgs({
|
|
471
|
-
mode,
|
|
472
471
|
prompt,
|
|
473
472
|
maxTokens = 4096,
|
|
474
473
|
// 4096: max model output tokens TODO update default in v5
|
|
@@ -480,10 +479,11 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
480
479
|
stopSequences,
|
|
481
480
|
responseFormat,
|
|
482
481
|
seed,
|
|
483
|
-
|
|
482
|
+
tools,
|
|
483
|
+
toolChoice,
|
|
484
|
+
providerOptions
|
|
484
485
|
}) {
|
|
485
486
|
var _a, _b, _c;
|
|
486
|
-
const type = mode.type;
|
|
487
487
|
const warnings = [];
|
|
488
488
|
if (frequencyPenalty != null) {
|
|
489
489
|
warnings.push({
|
|
@@ -515,7 +515,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
515
515
|
sendReasoning: (_a = this.settings.sendReasoning) != null ? _a : true,
|
|
516
516
|
warnings
|
|
517
517
|
});
|
|
518
|
-
const anthropicOptions = (0,
|
|
518
|
+
const anthropicOptions = (0, import_provider_utils2.parseProviderOptions)({
|
|
519
519
|
provider: "anthropic",
|
|
520
520
|
providerOptions,
|
|
521
521
|
schema: anthropicProviderOptionsSchema
|
|
@@ -571,49 +571,28 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
571
571
|
}
|
|
572
572
|
baseArgs.max_tokens = maxTokens + thinkingBudget;
|
|
573
573
|
}
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
throw new import_provider3.UnsupportedFunctionalityError({
|
|
590
|
-
functionality: "json-mode object generation"
|
|
591
|
-
});
|
|
592
|
-
}
|
|
593
|
-
case "object-tool": {
|
|
594
|
-
const { name, description, parameters } = mode.tool;
|
|
595
|
-
return {
|
|
596
|
-
args: {
|
|
597
|
-
...baseArgs,
|
|
598
|
-
tools: [{ name, description, input_schema: parameters }],
|
|
599
|
-
tool_choice: { type: "tool", name }
|
|
600
|
-
},
|
|
601
|
-
warnings,
|
|
602
|
-
betas: messagesBetas
|
|
603
|
-
};
|
|
604
|
-
}
|
|
605
|
-
default: {
|
|
606
|
-
const _exhaustiveCheck = type;
|
|
607
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
608
|
-
}
|
|
609
|
-
}
|
|
574
|
+
const {
|
|
575
|
+
tools: anthropicTools2,
|
|
576
|
+
toolChoice: anthropicToolChoice,
|
|
577
|
+
toolWarnings,
|
|
578
|
+
betas: toolsBetas
|
|
579
|
+
} = prepareTools({ tools, toolChoice });
|
|
580
|
+
return {
|
|
581
|
+
args: {
|
|
582
|
+
...baseArgs,
|
|
583
|
+
tools: anthropicTools2,
|
|
584
|
+
tool_choice: anthropicToolChoice
|
|
585
|
+
},
|
|
586
|
+
warnings: [...warnings, ...toolWarnings],
|
|
587
|
+
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
|
|
588
|
+
};
|
|
610
589
|
}
|
|
611
590
|
async getHeaders({
|
|
612
591
|
betas,
|
|
613
592
|
headers
|
|
614
593
|
}) {
|
|
615
|
-
return (0,
|
|
616
|
-
await (0,
|
|
594
|
+
return (0, import_provider_utils2.combineHeaders)(
|
|
595
|
+
await (0, import_provider_utils2.resolve)(this.config.headers),
|
|
617
596
|
betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {},
|
|
618
597
|
headers
|
|
619
598
|
);
|
|
@@ -633,12 +612,12 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
633
612
|
responseHeaders,
|
|
634
613
|
value: response,
|
|
635
614
|
rawValue: rawResponse
|
|
636
|
-
} = await (0,
|
|
615
|
+
} = await (0, import_provider_utils2.postJsonToApi)({
|
|
637
616
|
url: this.buildRequestUrl(false),
|
|
638
617
|
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
639
618
|
body: this.transformRequestBody(args),
|
|
640
619
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
641
|
-
successfulResponseHandler: (0,
|
|
620
|
+
successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
|
|
642
621
|
anthropicMessagesResponseSchema
|
|
643
622
|
),
|
|
644
623
|
abortSignal: options.abortSignal,
|
|
@@ -708,12 +687,12 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
708
687
|
async doStream(options) {
|
|
709
688
|
const { args, warnings, betas } = await this.getArgs(options);
|
|
710
689
|
const body = { ...args, stream: true };
|
|
711
|
-
const { responseHeaders, value: response } = await (0,
|
|
690
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
|
|
712
691
|
url: this.buildRequestUrl(true),
|
|
713
692
|
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
714
693
|
body: this.transformRequestBody(body),
|
|
715
694
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
716
|
-
successfulResponseHandler: (0,
|
|
695
|
+
successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(
|
|
717
696
|
anthropicMessagesChunkSchema
|
|
718
697
|
),
|
|
719
698
|
abortSignal: options.abortSignal,
|
|
@@ -1157,10 +1136,10 @@ var anthropicTools = {
|
|
|
1157
1136
|
// src/anthropic-provider.ts
|
|
1158
1137
|
function createAnthropic(options = {}) {
|
|
1159
1138
|
var _a;
|
|
1160
|
-
const baseURL = (_a = (0,
|
|
1139
|
+
const baseURL = (_a = (0, import_provider_utils3.withoutTrailingSlash)(options.baseURL)) != null ? _a : "https://api.anthropic.com/v1";
|
|
1161
1140
|
const getHeaders = () => ({
|
|
1162
1141
|
"anthropic-version": "2023-06-01",
|
|
1163
|
-
"x-api-key": (0,
|
|
1142
|
+
"x-api-key": (0, import_provider_utils3.loadApiKey)({
|
|
1164
1143
|
apiKey: options.apiKey,
|
|
1165
1144
|
environmentVariableName: "ANTHROPIC_API_KEY",
|
|
1166
1145
|
description: "Anthropic"
|