@ai-sdk/anthropic 2.0.0-canary.2 → 2.0.0-canary.4
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 +21 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +52 -55
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +41 -44
- 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 +49 -52
- package/internal/dist/index.js.map +1 -1
- package/internal/dist/index.mjs +41 -44
- package/internal/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# @ai-sdk/anthropic
|
|
2
2
|
|
|
3
|
+
## 2.0.0-canary.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [d1a1aa1]
|
|
8
|
+
- @ai-sdk/provider@2.0.0-canary.3
|
|
9
|
+
- @ai-sdk/provider-utils@3.0.0-canary.4
|
|
10
|
+
|
|
11
|
+
## 2.0.0-canary.3
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- 61ab528: Add support for URL-based PDF documents in the Anthropic provider
|
|
16
|
+
- Updated dependencies [a166433]
|
|
17
|
+
- Updated dependencies [abf9a79]
|
|
18
|
+
- Updated dependencies [9f95b35]
|
|
19
|
+
- Updated dependencies [0a87932]
|
|
20
|
+
- Updated dependencies [6dc848c]
|
|
21
|
+
- @ai-sdk/provider-utils@3.0.0-canary.3
|
|
22
|
+
- @ai-sdk/provider@2.0.0-canary.2
|
|
23
|
+
|
|
3
24
|
## 2.0.0-canary.2
|
|
4
25
|
|
|
5
26
|
### 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,
|
|
@@ -667,13 +666,11 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
667
666
|
completionTokens: response.usage.output_tokens
|
|
668
667
|
},
|
|
669
668
|
rawCall: { rawPrompt, rawSettings },
|
|
670
|
-
rawResponse: {
|
|
671
|
-
headers: responseHeaders,
|
|
672
|
-
body: rawResponse
|
|
673
|
-
},
|
|
674
669
|
response: {
|
|
675
670
|
id: (_a = response.id) != null ? _a : void 0,
|
|
676
|
-
modelId: (_b = response.model) != null ? _b : void 0
|
|
671
|
+
modelId: (_b = response.model) != null ? _b : void 0,
|
|
672
|
+
headers: responseHeaders,
|
|
673
|
+
body: rawResponse
|
|
677
674
|
},
|
|
678
675
|
warnings,
|
|
679
676
|
providerMetadata: {
|
|
@@ -688,12 +685,12 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
688
685
|
async doStream(options) {
|
|
689
686
|
const { args, warnings, betas } = await this.getArgs(options);
|
|
690
687
|
const body = { ...args, stream: true };
|
|
691
|
-
const { responseHeaders, value: response } = await (0,
|
|
688
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
|
|
692
689
|
url: this.buildRequestUrl(true),
|
|
693
690
|
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
694
691
|
body: this.transformRequestBody(body),
|
|
695
692
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
696
|
-
successfulResponseHandler: (0,
|
|
693
|
+
successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(
|
|
697
694
|
anthropicMessagesChunkSchema
|
|
698
695
|
),
|
|
699
696
|
abortSignal: options.abortSignal,
|
|
@@ -857,7 +854,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
857
854
|
})
|
|
858
855
|
),
|
|
859
856
|
rawCall: { rawPrompt, rawSettings },
|
|
860
|
-
|
|
857
|
+
response: { headers: responseHeaders },
|
|
861
858
|
warnings,
|
|
862
859
|
request: { body: JSON.stringify(body) }
|
|
863
860
|
};
|
|
@@ -1137,10 +1134,10 @@ var anthropicTools = {
|
|
|
1137
1134
|
// src/anthropic-provider.ts
|
|
1138
1135
|
function createAnthropic(options = {}) {
|
|
1139
1136
|
var _a;
|
|
1140
|
-
const baseURL = (_a = (0,
|
|
1137
|
+
const baseURL = (_a = (0, import_provider_utils3.withoutTrailingSlash)(options.baseURL)) != null ? _a : "https://api.anthropic.com/v1";
|
|
1141
1138
|
const getHeaders = () => ({
|
|
1142
1139
|
"anthropic-version": "2023-06-01",
|
|
1143
|
-
"x-api-key": (0,
|
|
1140
|
+
"x-api-key": (0, import_provider_utils3.loadApiKey)({
|
|
1144
1141
|
apiKey: options.apiKey,
|
|
1145
1142
|
environmentVariableName: "ANTHROPIC_API_KEY",
|
|
1146
1143
|
description: "Anthropic"
|