@ai-sdk/anthropic 2.0.0-canary.2 → 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 +13 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +48 -49
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +37 -38
- 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 +45 -46
- package/internal/dist/index.js.map +1 -1
- package/internal/dist/index.mjs +37 -38
- package/internal/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
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
|
+
|
|
3
16
|
## 2.0.0-canary.2
|
|
4
17
|
|
|
5
18
|
### 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
|
|
@@ -167,7 +167,7 @@ function prepareTools({
|
|
|
167
167
|
default: {
|
|
168
168
|
const _exhaustiveCheck = type;
|
|
169
169
|
throw new import_provider.UnsupportedFunctionalityError({
|
|
170
|
-
functionality: `
|
|
170
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
171
171
|
});
|
|
172
172
|
}
|
|
173
173
|
}
|
|
@@ -175,13 +175,12 @@ function prepareTools({
|
|
|
175
175
|
|
|
176
176
|
// src/convert-to-anthropic-messages-prompt.ts
|
|
177
177
|
var import_provider2 = require("@ai-sdk/provider");
|
|
178
|
-
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
179
178
|
function convertToAnthropicMessagesPrompt({
|
|
180
179
|
prompt,
|
|
181
180
|
sendReasoning,
|
|
182
181
|
warnings
|
|
183
182
|
}) {
|
|
184
|
-
var _a, _b, _c
|
|
183
|
+
var _a, _b, _c;
|
|
185
184
|
const betas = /* @__PURE__ */ new Set();
|
|
186
185
|
const blocks = groupIntoBlocks(prompt);
|
|
187
186
|
let system = void 0;
|
|
@@ -229,42 +228,39 @@ function convertToAnthropicMessagesPrompt({
|
|
|
229
228
|
});
|
|
230
229
|
break;
|
|
231
230
|
}
|
|
232
|
-
case "image": {
|
|
233
|
-
anthropicContent.push({
|
|
234
|
-
type: "image",
|
|
235
|
-
source: part.image instanceof URL ? {
|
|
236
|
-
type: "url",
|
|
237
|
-
url: part.image.toString()
|
|
238
|
-
} : {
|
|
239
|
-
type: "base64",
|
|
240
|
-
media_type: (_b = part.mimeType) != null ? _b : "image/jpeg",
|
|
241
|
-
data: (0, import_provider_utils2.convertUint8ArrayToBase64)(part.image)
|
|
242
|
-
},
|
|
243
|
-
cache_control: cacheControl
|
|
244
|
-
});
|
|
245
|
-
break;
|
|
246
|
-
}
|
|
247
231
|
case "file": {
|
|
248
|
-
if (part.
|
|
249
|
-
|
|
250
|
-
|
|
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
|
|
251
244
|
});
|
|
252
|
-
}
|
|
253
|
-
|
|
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 {
|
|
254
260
|
throw new import_provider2.UnsupportedFunctionalityError({
|
|
255
|
-
functionality:
|
|
261
|
+
functionality: `media type: ${part.mediaType}`
|
|
256
262
|
});
|
|
257
263
|
}
|
|
258
|
-
betas.add("pdfs-2024-09-25");
|
|
259
|
-
anthropicContent.push({
|
|
260
|
-
type: "document",
|
|
261
|
-
source: {
|
|
262
|
-
type: "base64",
|
|
263
|
-
media_type: "application/pdf",
|
|
264
|
-
data: part.data
|
|
265
|
-
},
|
|
266
|
-
cache_control: cacheControl
|
|
267
|
-
});
|
|
268
264
|
break;
|
|
269
265
|
}
|
|
270
266
|
}
|
|
@@ -275,7 +271,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
275
271
|
for (let i2 = 0; i2 < content.length; i2++) {
|
|
276
272
|
const part = content[i2];
|
|
277
273
|
const isLastPart = i2 === content.length - 1;
|
|
278
|
-
const cacheControl = (
|
|
274
|
+
const cacheControl = (_b = getCacheControl(part.providerOptions)) != null ? _b : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
279
275
|
const toolResultContent = part.content != null ? part.content.map((part2) => {
|
|
280
276
|
var _a2;
|
|
281
277
|
switch (part2.type) {
|
|
@@ -290,7 +286,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
290
286
|
type: "image",
|
|
291
287
|
source: {
|
|
292
288
|
type: "base64",
|
|
293
|
-
media_type: (_a2 = part2.
|
|
289
|
+
media_type: (_a2 = part2.mediaType) != null ? _a2 : "image/jpeg",
|
|
294
290
|
data: part2.data
|
|
295
291
|
},
|
|
296
292
|
cache_control: void 0
|
|
@@ -325,7 +321,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
325
321
|
for (let k = 0; k < content.length; k++) {
|
|
326
322
|
const part = content[k];
|
|
327
323
|
const isLastContentPart = k === content.length - 1;
|
|
328
|
-
const cacheControl = (
|
|
324
|
+
const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
|
|
329
325
|
switch (part.type) {
|
|
330
326
|
case "text": {
|
|
331
327
|
anthropicContent.push({
|
|
@@ -382,7 +378,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
382
378
|
}
|
|
383
379
|
default: {
|
|
384
380
|
const _exhaustiveCheck = type;
|
|
385
|
-
throw new Error(`
|
|
381
|
+
throw new Error(`content type: ${_exhaustiveCheck}`);
|
|
386
382
|
}
|
|
387
383
|
}
|
|
388
384
|
}
|
|
@@ -462,6 +458,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
462
458
|
this.settings = settings;
|
|
463
459
|
this.config = config;
|
|
464
460
|
}
|
|
461
|
+
supportsUrl(url) {
|
|
462
|
+
return url.protocol === "https:";
|
|
463
|
+
}
|
|
465
464
|
get provider() {
|
|
466
465
|
return this.config.provider;
|
|
467
466
|
}
|
|
@@ -516,7 +515,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
516
515
|
sendReasoning: (_a = this.settings.sendReasoning) != null ? _a : true,
|
|
517
516
|
warnings
|
|
518
517
|
});
|
|
519
|
-
const anthropicOptions = (0,
|
|
518
|
+
const anthropicOptions = (0, import_provider_utils2.parseProviderOptions)({
|
|
520
519
|
provider: "anthropic",
|
|
521
520
|
providerOptions,
|
|
522
521
|
schema: anthropicProviderOptionsSchema
|
|
@@ -592,8 +591,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
592
591
|
betas,
|
|
593
592
|
headers
|
|
594
593
|
}) {
|
|
595
|
-
return (0,
|
|
596
|
-
await (0,
|
|
594
|
+
return (0, import_provider_utils2.combineHeaders)(
|
|
595
|
+
await (0, import_provider_utils2.resolve)(this.config.headers),
|
|
597
596
|
betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {},
|
|
598
597
|
headers
|
|
599
598
|
);
|
|
@@ -613,12 +612,12 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
613
612
|
responseHeaders,
|
|
614
613
|
value: response,
|
|
615
614
|
rawValue: rawResponse
|
|
616
|
-
} = await (0,
|
|
615
|
+
} = await (0, import_provider_utils2.postJsonToApi)({
|
|
617
616
|
url: this.buildRequestUrl(false),
|
|
618
617
|
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
619
618
|
body: this.transformRequestBody(args),
|
|
620
619
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
621
|
-
successfulResponseHandler: (0,
|
|
620
|
+
successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
|
|
622
621
|
anthropicMessagesResponseSchema
|
|
623
622
|
),
|
|
624
623
|
abortSignal: options.abortSignal,
|
|
@@ -688,12 +687,12 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
688
687
|
async doStream(options) {
|
|
689
688
|
const { args, warnings, betas } = await this.getArgs(options);
|
|
690
689
|
const body = { ...args, stream: true };
|
|
691
|
-
const { responseHeaders, value: response } = await (0,
|
|
690
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
|
|
692
691
|
url: this.buildRequestUrl(true),
|
|
693
692
|
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
694
693
|
body: this.transformRequestBody(body),
|
|
695
694
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
696
|
-
successfulResponseHandler: (0,
|
|
695
|
+
successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(
|
|
697
696
|
anthropicMessagesChunkSchema
|
|
698
697
|
),
|
|
699
698
|
abortSignal: options.abortSignal,
|
|
@@ -1137,10 +1136,10 @@ var anthropicTools = {
|
|
|
1137
1136
|
// src/anthropic-provider.ts
|
|
1138
1137
|
function createAnthropic(options = {}) {
|
|
1139
1138
|
var _a;
|
|
1140
|
-
const baseURL = (_a = (0,
|
|
1139
|
+
const baseURL = (_a = (0, import_provider_utils3.withoutTrailingSlash)(options.baseURL)) != null ? _a : "https://api.anthropic.com/v1";
|
|
1141
1140
|
const getHeaders = () => ({
|
|
1142
1141
|
"anthropic-version": "2023-06-01",
|
|
1143
|
-
"x-api-key": (0,
|
|
1142
|
+
"x-api-key": (0, import_provider_utils3.loadApiKey)({
|
|
1144
1143
|
apiKey: options.apiKey,
|
|
1145
1144
|
environmentVariableName: "ANTHROPIC_API_KEY",
|
|
1146
1145
|
description: "Anthropic"
|