@ai-sdk/anthropic 4.0.61 → 4.0.63
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 +16 -0
- package/README.md +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +76 -22
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +6 -0
- package/dist/internal/index.js +54 -4
- package/dist/internal/index.js.map +1 -1
- package/docs/05-anthropic.mdx +113 -31
- package/package.json +2 -2
- package/src/anthropic-api.ts +3 -0
- package/src/anthropic-batch.ts +7 -2
- package/src/anthropic-language-model-options.ts +13 -0
- package/src/anthropic-language-model.ts +44 -0
- package/src/convert-to-anthropic-prompt.ts +12 -1
- package/src/forward-anthropic-container-id-from-last-step.ts +0 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @ai-sdk/anthropic
|
|
2
2
|
|
|
3
|
+
## 4.0.63
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 154221f: feat(anthropic): support on-demand compaction and preserve signed compaction blocks
|
|
8
|
+
|
|
9
|
+
## 4.0.62
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 771e74b: chore: enable dead code lint rules
|
|
14
|
+
- Updated dependencies [fe07867]
|
|
15
|
+
- Updated dependencies [a4b0940]
|
|
16
|
+
- Updated dependencies [771e74b]
|
|
17
|
+
- @ai-sdk/provider-utils@5.0.47
|
|
18
|
+
|
|
3
19
|
## 4.0.61
|
|
4
20
|
|
|
5
21
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -35,7 +35,7 @@ import { anthropic } from '@ai-sdk/anthropic';
|
|
|
35
35
|
import { generateText } from 'ai';
|
|
36
36
|
|
|
37
37
|
const { text } = await generateText({
|
|
38
|
-
model: anthropic('claude-
|
|
38
|
+
model: anthropic('claude-haiku-4-5'),
|
|
39
39
|
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
|
|
40
40
|
});
|
|
41
41
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -345,6 +345,10 @@ declare const anthropicLanguageModelOptions: z.ZodObject<{
|
|
|
345
345
|
type: z.ZodLiteral<"dangerous_tool_use">;
|
|
346
346
|
classifierContext: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
347
347
|
}, z.core.$strip>>>;
|
|
348
|
+
compaction: z.ZodOptional<z.ZodObject<{
|
|
349
|
+
type: z.ZodLiteral<"summarize">;
|
|
350
|
+
instructions: z.ZodOptional<z.ZodString>;
|
|
351
|
+
}, z.core.$strip>>;
|
|
348
352
|
contextManagement: z.ZodOptional<z.ZodObject<{
|
|
349
353
|
edits: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
350
354
|
type: z.ZodLiteral<"clear_tool_uses_20250919">;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/anthropic-provider.ts
|
|
2
2
|
import {
|
|
3
|
-
InvalidArgumentError as
|
|
3
|
+
InvalidArgumentError as InvalidArgumentError3,
|
|
4
4
|
NoSuchModelError
|
|
5
5
|
} from "@ai-sdk/provider";
|
|
6
6
|
import {
|
|
@@ -121,6 +121,7 @@ var AnthropicFiles = class {
|
|
|
121
121
|
// src/anthropic-language-model.ts
|
|
122
122
|
import {
|
|
123
123
|
APICallError,
|
|
124
|
+
InvalidArgumentError,
|
|
124
125
|
InvalidResponseDataError
|
|
125
126
|
} from "@ai-sdk/provider";
|
|
126
127
|
import {
|
|
@@ -272,7 +273,8 @@ var anthropicResponseSchema = lazySchema3(
|
|
|
272
273
|
}),
|
|
273
274
|
z3.object({
|
|
274
275
|
type: z3.literal("compaction"),
|
|
275
|
-
content: z3.string().nullish()
|
|
276
|
+
content: z3.string().nullish(),
|
|
277
|
+
signature: z3.string().nullish()
|
|
276
278
|
}),
|
|
277
279
|
z3.object({
|
|
278
280
|
type: z3.literal("tool_use"),
|
|
@@ -627,7 +629,8 @@ var anthropicChunkSchema = lazySchema3(
|
|
|
627
629
|
}),
|
|
628
630
|
z3.object({
|
|
629
631
|
type: z3.literal("compaction"),
|
|
630
|
-
content: z3.string().nullish()
|
|
632
|
+
content: z3.string().nullish(),
|
|
633
|
+
signature: z3.string().nullish()
|
|
631
634
|
}),
|
|
632
635
|
z3.object({
|
|
633
636
|
type: z3.literal("server_tool_use"),
|
|
@@ -1266,6 +1269,16 @@ var anthropicLanguageModelOptions = z4.object({
|
|
|
1266
1269
|
classifierContext: z4.record(z4.string(), z4.unknown()).optional()
|
|
1267
1270
|
})
|
|
1268
1271
|
).optional(),
|
|
1272
|
+
/**
|
|
1273
|
+
* Requests an on-demand summary of the supplied conversation.
|
|
1274
|
+
*
|
|
1275
|
+
* The required `compact-2026-09-04` beta is added automatically.
|
|
1276
|
+
* Cannot be combined with `contextManagement`.
|
|
1277
|
+
*/
|
|
1278
|
+
compaction: z4.object({
|
|
1279
|
+
type: z4.literal("summarize"),
|
|
1280
|
+
instructions: z4.string().optional()
|
|
1281
|
+
}).optional(),
|
|
1269
1282
|
contextManagement: z4.object({
|
|
1270
1283
|
edits: z4.array(
|
|
1271
1284
|
z4.discriminatedUnion("type", [
|
|
@@ -3246,9 +3259,15 @@ async function convertToAnthropicPrompt({
|
|
|
3246
3259
|
if (part.text === "") {
|
|
3247
3260
|
break;
|
|
3248
3261
|
}
|
|
3262
|
+
if (typeof textMetadata.signature === "string") {
|
|
3263
|
+
betas.add("compact-2026-09-04");
|
|
3264
|
+
}
|
|
3249
3265
|
anthropicContent.push({
|
|
3250
3266
|
type: "compaction",
|
|
3251
3267
|
content: part.text,
|
|
3268
|
+
...typeof textMetadata.signature === "string" && {
|
|
3269
|
+
signature: textMetadata.signature
|
|
3270
|
+
},
|
|
3252
3271
|
cache_control: cacheControl
|
|
3253
3272
|
});
|
|
3254
3273
|
} else {
|
|
@@ -4338,6 +4357,13 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
|
|
|
4338
4357
|
});
|
|
4339
4358
|
}
|
|
4340
4359
|
const contextManagement = anthropicOptions == null ? void 0 : anthropicOptions.contextManagement;
|
|
4360
|
+
const compaction = anthropicOptions == null ? void 0 : anthropicOptions.compaction;
|
|
4361
|
+
if (contextManagement != null && compaction != null) {
|
|
4362
|
+
throw new InvalidArgumentError({
|
|
4363
|
+
argument: "providerOptions",
|
|
4364
|
+
message: "Anthropic provider options `compaction` and `contextManagement` cannot be used together."
|
|
4365
|
+
});
|
|
4366
|
+
}
|
|
4341
4367
|
const cacheControlValidator = new CacheControlValidator();
|
|
4342
4368
|
const toolNameMapping = createToolNameMapping({
|
|
4343
4369
|
tools,
|
|
@@ -4534,6 +4560,14 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
|
|
|
4534
4560
|
}
|
|
4535
4561
|
}))
|
|
4536
4562
|
},
|
|
4563
|
+
...compaction && {
|
|
4564
|
+
compaction: {
|
|
4565
|
+
type: compaction.type,
|
|
4566
|
+
...compaction.instructions !== void 0 && {
|
|
4567
|
+
instructions: compaction.instructions
|
|
4568
|
+
}
|
|
4569
|
+
}
|
|
4570
|
+
},
|
|
4537
4571
|
...contextManagement && {
|
|
4538
4572
|
context_management: {
|
|
4539
4573
|
edits: contextManagement.edits.map((edit) => {
|
|
@@ -4649,6 +4683,9 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
|
|
|
4649
4683
|
if ((anthropicOptions == null ? void 0 : anthropicOptions.safeguards) && anthropicOptions.safeguards.length > 0) {
|
|
4650
4684
|
betas.add("dangerous-tool-use-2026-09-03");
|
|
4651
4685
|
}
|
|
4686
|
+
if (compaction) {
|
|
4687
|
+
betas.add("compact-2026-09-04");
|
|
4688
|
+
}
|
|
4652
4689
|
if (contextManagement) {
|
|
4653
4690
|
betas.add("context-management-2025-06-27");
|
|
4654
4691
|
if (contextManagement.edits.some((e) => e.type === "compact_20260112")) {
|
|
@@ -4906,7 +4943,10 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
|
|
|
4906
4943
|
text: part.content,
|
|
4907
4944
|
providerMetadata: {
|
|
4908
4945
|
anthropic: {
|
|
4909
|
-
type: "compaction"
|
|
4946
|
+
type: "compaction",
|
|
4947
|
+
...part.signature != null && {
|
|
4948
|
+
signature: part.signature
|
|
4949
|
+
}
|
|
4910
4950
|
}
|
|
4911
4951
|
}
|
|
4912
4952
|
});
|
|
@@ -5463,10 +5503,20 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
|
|
|
5463
5503
|
id: String(value.index),
|
|
5464
5504
|
providerMetadata: {
|
|
5465
5505
|
anthropic: {
|
|
5466
|
-
type: "compaction"
|
|
5506
|
+
type: "compaction",
|
|
5507
|
+
...part.signature != null && {
|
|
5508
|
+
signature: part.signature
|
|
5509
|
+
}
|
|
5467
5510
|
}
|
|
5468
5511
|
}
|
|
5469
5512
|
});
|
|
5513
|
+
if (part.signature != null && part.content != null && part.content !== "") {
|
|
5514
|
+
controller.enqueue({
|
|
5515
|
+
type: "text-delta",
|
|
5516
|
+
id: String(value.index),
|
|
5517
|
+
delta: part.content
|
|
5518
|
+
});
|
|
5519
|
+
}
|
|
5470
5520
|
return;
|
|
5471
5521
|
}
|
|
5472
5522
|
case "tool_use": {
|
|
@@ -6584,7 +6634,7 @@ function mapAnthropicStopDetails(stopDetails) {
|
|
|
6584
6634
|
|
|
6585
6635
|
// src/anthropic-batch.ts
|
|
6586
6636
|
import {
|
|
6587
|
-
InvalidArgumentError,
|
|
6637
|
+
InvalidArgumentError as InvalidArgumentError2,
|
|
6588
6638
|
InvalidResponseDataError as InvalidResponseDataError2,
|
|
6589
6639
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
6590
6640
|
} from "@ai-sdk/provider";
|
|
@@ -6853,13 +6903,13 @@ var AnthropicBatch = class {
|
|
|
6853
6903
|
async doGetBatchResults(options) {
|
|
6854
6904
|
const batch = await this.retrieveBatch(options);
|
|
6855
6905
|
if (convertAnthropicBatchStatus(batch).status === "pending") {
|
|
6856
|
-
throw new
|
|
6906
|
+
throw new InvalidArgumentError2({
|
|
6857
6907
|
argument: "batchId",
|
|
6858
6908
|
message: `Anthropic batch "${options.batchId}" is not complete.`
|
|
6859
6909
|
});
|
|
6860
6910
|
}
|
|
6861
6911
|
if (batch.archived_at != null) {
|
|
6862
|
-
throw new
|
|
6912
|
+
throw new InvalidArgumentError2({
|
|
6863
6913
|
argument: "batchId",
|
|
6864
6914
|
message: `Anthropic batch "${options.batchId}" results are no longer available.`
|
|
6865
6915
|
});
|
|
@@ -6954,13 +7004,13 @@ function validateRequestIds(requests) {
|
|
|
6954
7004
|
const ids = /* @__PURE__ */ new Set();
|
|
6955
7005
|
for (const request of requests) {
|
|
6956
7006
|
if (!anthropicBatchRequestIdPattern.test(request.id)) {
|
|
6957
|
-
throw new
|
|
7007
|
+
throw new InvalidArgumentError2({
|
|
6958
7008
|
argument: "requests",
|
|
6959
7009
|
message: `Anthropic batch request ID "${request.id}" must match ^[A-Za-z0-9_-]{1,64}$.`
|
|
6960
7010
|
});
|
|
6961
7011
|
}
|
|
6962
7012
|
if (ids.has(request.id)) {
|
|
6963
|
-
throw new
|
|
7013
|
+
throw new InvalidArgumentError2({
|
|
6964
7014
|
argument: "requests",
|
|
6965
7015
|
message: `Anthropic batch request IDs must be unique; duplicate ID "${request.id}".`
|
|
6966
7016
|
});
|
|
@@ -7114,7 +7164,7 @@ async function parseAnthropicBatchResponse(message) {
|
|
|
7114
7164
|
return recovered.success ? recovered.value : void 0;
|
|
7115
7165
|
}
|
|
7116
7166
|
function convertAnthropicBatchResponse(response, generateId4) {
|
|
7117
|
-
var _a, _b, _c, _d, _e, _f, _g
|
|
7167
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
7118
7168
|
const content = [];
|
|
7119
7169
|
const mcpToolCalls = {};
|
|
7120
7170
|
const serverToolCalls = {};
|
|
@@ -7174,7 +7224,12 @@ function convertAnthropicBatchResponse(response, generateId4) {
|
|
|
7174
7224
|
content.push({
|
|
7175
7225
|
type: "text",
|
|
7176
7226
|
text: part.content,
|
|
7177
|
-
providerMetadata: {
|
|
7227
|
+
providerMetadata: {
|
|
7228
|
+
anthropic: {
|
|
7229
|
+
type: "compaction",
|
|
7230
|
+
...part.signature != null && { signature: part.signature }
|
|
7231
|
+
}
|
|
7232
|
+
}
|
|
7178
7233
|
});
|
|
7179
7234
|
break;
|
|
7180
7235
|
case "tool_use":
|
|
@@ -7198,7 +7253,7 @@ function convertAnthropicBatchResponse(response, generateId4) {
|
|
|
7198
7253
|
toolCallId: part.id,
|
|
7199
7254
|
toolName,
|
|
7200
7255
|
input: JSON.stringify(
|
|
7201
|
-
isCodeExecutionAlias ? { type: part.name, ...
|
|
7256
|
+
isCodeExecutionAlias ? { type: part.name, ...part.input } : part.name === "code_execution" && part.input != null && "code" in part.input && !("type" in part.input) ? { type: "programmatic-tool-call", ...part.input } : part.input
|
|
7202
7257
|
),
|
|
7203
7258
|
providerExecuted: true,
|
|
7204
7259
|
// Batch results are retrieved without the original request tools, so
|
|
@@ -7232,7 +7287,7 @@ function convertAnthropicBatchResponse(response, generateId4) {
|
|
|
7232
7287
|
content.push({
|
|
7233
7288
|
type: "tool-result",
|
|
7234
7289
|
toolCallId: part.tool_use_id,
|
|
7235
|
-
toolName: (
|
|
7290
|
+
toolName: (_b = toolCall == null ? void 0 : toolCall.toolName) != null ? _b : "mcp",
|
|
7236
7291
|
isError: part.is_error,
|
|
7237
7292
|
result: part.content,
|
|
7238
7293
|
dynamic: true,
|
|
@@ -7308,7 +7363,7 @@ function convertAnthropicBatchResponse(response, generateId4) {
|
|
|
7308
7363
|
...result.title != null ? { title: result.title } : {},
|
|
7309
7364
|
providerMetadata: {
|
|
7310
7365
|
anthropic: {
|
|
7311
|
-
pageAge: (
|
|
7366
|
+
pageAge: (_c = result.page_age) != null ? _c : null
|
|
7312
7367
|
}
|
|
7313
7368
|
}
|
|
7314
7369
|
});
|
|
@@ -7339,7 +7394,7 @@ function convertAnthropicBatchResponse(response, generateId4) {
|
|
|
7339
7394
|
});
|
|
7340
7395
|
break;
|
|
7341
7396
|
case "tool_search_tool_result": {
|
|
7342
|
-
const toolName = (
|
|
7397
|
+
const toolName = (_d = serverToolCalls[part.tool_use_id]) != null ? _d : "tool_search_tool_regex";
|
|
7343
7398
|
content.push({
|
|
7344
7399
|
type: "tool-result",
|
|
7345
7400
|
toolCallId: part.tool_use_id,
|
|
@@ -7405,12 +7460,12 @@ function convertAnthropicBatchResponse(response, generateId4) {
|
|
|
7405
7460
|
unified: mapAnthropicStopReason({
|
|
7406
7461
|
finishReason: response.stop_reason
|
|
7407
7462
|
}),
|
|
7408
|
-
raw: (
|
|
7463
|
+
raw: (_e = response.stop_reason) != null ? _e : void 0
|
|
7409
7464
|
},
|
|
7410
7465
|
usage: convertAnthropicUsage({ usage: response.usage }),
|
|
7411
7466
|
response: {
|
|
7412
|
-
id: (
|
|
7413
|
-
modelId: (
|
|
7467
|
+
id: (_f = response.id) != null ? _f : void 0,
|
|
7468
|
+
modelId: (_g = response.model) != null ? _g : void 0
|
|
7414
7469
|
},
|
|
7415
7470
|
warnings: [],
|
|
7416
7471
|
providerMetadata: {
|
|
@@ -8286,7 +8341,7 @@ var AnthropicSkills = class {
|
|
|
8286
8341
|
};
|
|
8287
8342
|
|
|
8288
8343
|
// src/version.ts
|
|
8289
|
-
var VERSION = true ? "4.0.
|
|
8344
|
+
var VERSION = true ? "4.0.63" : "0.0.0-test";
|
|
8290
8345
|
|
|
8291
8346
|
// src/anthropic-provider.ts
|
|
8292
8347
|
var ANTHROPIC_API_URL = "https://api.anthropic.com";
|
|
@@ -8311,7 +8366,7 @@ function createAnthropic(options = {}) {
|
|
|
8311
8366
|
"application/pdf": [/^https?:\/\/.*$/]
|
|
8312
8367
|
};
|
|
8313
8368
|
if (options.apiKey && options.authToken) {
|
|
8314
|
-
throw new
|
|
8369
|
+
throw new InvalidArgumentError3({
|
|
8315
8370
|
argument: "apiKey/authToken",
|
|
8316
8371
|
message: "Both apiKey and authToken were provided. Please use only one authentication method."
|
|
8317
8372
|
});
|
|
@@ -8406,7 +8461,6 @@ function forwardAnthropicContainerIdFromLastStep({
|
|
|
8406
8461
|
};
|
|
8407
8462
|
}
|
|
8408
8463
|
}
|
|
8409
|
-
return void 0;
|
|
8410
8464
|
}
|
|
8411
8465
|
export {
|
|
8412
8466
|
VERSION,
|