@ai-sdk/anthropic 4.0.42 → 4.0.44
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 +19 -0
- package/dist/index.js +79 -83
- package/dist/index.js.map +1 -1
- package/dist/internal/index.js +61 -10
- package/dist/internal/index.js.map +1 -1
- package/docs/05-anthropic.mdx +17 -8
- package/package.json +2 -2
- package/src/anthropic-api.ts +4 -0
- package/src/anthropic-language-model.ts +61 -5
- package/src/anthropic-messages-batch.ts +22 -88
- package/src/skills/anthropic-skills.ts +12 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# @ai-sdk/anthropic
|
|
2
2
|
|
|
3
|
+
## 4.0.44
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [3e125ba]
|
|
8
|
+
- @ai-sdk/provider-utils@5.0.32
|
|
9
|
+
|
|
10
|
+
## 4.0.43
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- 7de3612: Encode provider-returned identifiers before using them in credentialed follow-up request paths.
|
|
15
|
+
- a9782e1: fix: align batch result parsing, request counts, and lifecycle behavior across providers
|
|
16
|
+
- 35841f5: feat: normalize mid-stream provider error events across supported providers into public StreamProviderError instances and preserve provider-owned type, code, status, retry, and raw payload metadata
|
|
17
|
+
- Updated dependencies [a9782e1]
|
|
18
|
+
- Updated dependencies [35841f5]
|
|
19
|
+
- Updated dependencies [d2f3353]
|
|
20
|
+
- @ai-sdk/provider-utils@5.0.31
|
|
21
|
+
|
|
3
22
|
## 4.0.42
|
|
4
23
|
|
|
5
24
|
### Patch Changes
|
package/dist/index.js
CHANGED
|
@@ -114,18 +114,19 @@ var AnthropicFiles = class {
|
|
|
114
114
|
|
|
115
115
|
// src/anthropic-messages-batch.ts
|
|
116
116
|
import {
|
|
117
|
-
EmptyResponseBodyError,
|
|
118
117
|
InvalidArgumentError,
|
|
118
|
+
InvalidResponseDataError as InvalidResponseDataError2,
|
|
119
119
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
120
120
|
} from "@ai-sdk/provider";
|
|
121
121
|
import {
|
|
122
122
|
combineHeaders as combineHeaders3,
|
|
123
123
|
convertAsyncIteratorToReadableStream,
|
|
124
|
+
createJsonLinesResponseHandler,
|
|
124
125
|
createJsonResponseHandler as createJsonResponseHandler3,
|
|
125
126
|
getFromApi,
|
|
126
127
|
lazySchema as lazySchema14,
|
|
128
|
+
normalizeBatchRequestCounts,
|
|
127
129
|
normalizeHeaders,
|
|
128
|
-
parseJSON,
|
|
129
130
|
parseProviderOptions as parseProviderOptions3,
|
|
130
131
|
postJsonToApi as postJsonToApi2,
|
|
131
132
|
resolve as resolve2,
|
|
@@ -840,7 +841,11 @@ var anthropicChunkSchema = lazySchema3(
|
|
|
840
841
|
type: z3.literal("error"),
|
|
841
842
|
error: z3.object({
|
|
842
843
|
type: z3.string(),
|
|
843
|
-
message: z3.string()
|
|
844
|
+
message: z3.string(),
|
|
845
|
+
code: z3.union([z3.string(), z3.number()]).nullish(),
|
|
846
|
+
statusCode: z3.number().nullish(),
|
|
847
|
+
isRetryable: z3.boolean().nullish(),
|
|
848
|
+
data: z3.unknown().nullish()
|
|
844
849
|
})
|
|
845
850
|
}),
|
|
846
851
|
z3.object({
|
|
@@ -935,9 +940,11 @@ import {
|
|
|
935
940
|
combineHeaders as combineHeaders2,
|
|
936
941
|
createEventSourceResponseHandler,
|
|
937
942
|
createJsonResponseHandler as createJsonResponseHandler2,
|
|
943
|
+
createProviderStreamError,
|
|
938
944
|
createToolNameMapping,
|
|
939
945
|
generateId,
|
|
940
946
|
isCustomReasoning,
|
|
947
|
+
isProviderStreamError,
|
|
941
948
|
mapReasoningToProviderBudget,
|
|
942
949
|
mapReasoningToProviderEffort,
|
|
943
950
|
parseProviderOptions as parseProviderOptions2,
|
|
@@ -3611,6 +3618,41 @@ function formatConstraintValue(value) {
|
|
|
3611
3618
|
}
|
|
3612
3619
|
|
|
3613
3620
|
// src/anthropic-language-model.ts
|
|
3621
|
+
function createAnthropicStreamError(error) {
|
|
3622
|
+
var _a, _b, _c;
|
|
3623
|
+
const inferredMetadata = getAnthropicStreamErrorMetadata(error.type);
|
|
3624
|
+
return createProviderStreamError({
|
|
3625
|
+
message: error.message,
|
|
3626
|
+
type: error.type,
|
|
3627
|
+
code: (_a = error.code) != null ? _a : void 0,
|
|
3628
|
+
statusCode: (_b = error.statusCode) != null ? _b : inferredMetadata.statusCode,
|
|
3629
|
+
isRetryable: (_c = error.isRetryable) != null ? _c : inferredMetadata.isRetryable,
|
|
3630
|
+
data: "data" in error ? error.data : error
|
|
3631
|
+
});
|
|
3632
|
+
}
|
|
3633
|
+
function getAnthropicStreamErrorMetadata(type) {
|
|
3634
|
+
switch (type) {
|
|
3635
|
+
case "api_error":
|
|
3636
|
+
return { statusCode: 500, isRetryable: true };
|
|
3637
|
+
case "overloaded_error":
|
|
3638
|
+
return { statusCode: 529, isRetryable: true };
|
|
3639
|
+
case "rate_limit_error":
|
|
3640
|
+
return { statusCode: 429, isRetryable: true };
|
|
3641
|
+
case "request_too_large":
|
|
3642
|
+
return { statusCode: 413, isRetryable: false };
|
|
3643
|
+
case "authentication_error":
|
|
3644
|
+
return { statusCode: 401, isRetryable: false };
|
|
3645
|
+
case "permission_error":
|
|
3646
|
+
return { statusCode: 403, isRetryable: false };
|
|
3647
|
+
case "not_found_error":
|
|
3648
|
+
return { statusCode: 404, isRetryable: false };
|
|
3649
|
+
case "billing_error":
|
|
3650
|
+
case "invalid_request_error":
|
|
3651
|
+
return { statusCode: 400, isRetryable: false };
|
|
3652
|
+
default:
|
|
3653
|
+
return {};
|
|
3654
|
+
}
|
|
3655
|
+
}
|
|
3614
3656
|
function createCitationSource(citation, citationDocuments, generateId3) {
|
|
3615
3657
|
var _a;
|
|
3616
3658
|
if (citation.type === "web_search_result_location") {
|
|
@@ -4737,7 +4779,7 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
|
|
|
4737
4779
|
}
|
|
4738
4780
|
async doStream(options) {
|
|
4739
4781
|
"use step";
|
|
4740
|
-
var _a, _b;
|
|
4782
|
+
var _a, _b, _c, _d;
|
|
4741
4783
|
const {
|
|
4742
4784
|
args: body,
|
|
4743
4785
|
warnings,
|
|
@@ -4798,7 +4840,7 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
|
|
|
4798
4840
|
controller.enqueue({ type: "stream-start", warnings });
|
|
4799
4841
|
},
|
|
4800
4842
|
transform(chunk, controller) {
|
|
4801
|
-
var _a2, _b2,
|
|
4843
|
+
var _a2, _b2, _c2, _d2, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
4802
4844
|
if (hasInvalidMessageSequence) {
|
|
4803
4845
|
return;
|
|
4804
4846
|
}
|
|
@@ -5091,7 +5133,7 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
|
|
|
5091
5133
|
stdout: part.content.stdout,
|
|
5092
5134
|
stderr: part.content.stderr,
|
|
5093
5135
|
return_code: part.content.return_code,
|
|
5094
|
-
content: (
|
|
5136
|
+
content: (_c2 = part.content.content) != null ? _c2 : []
|
|
5095
5137
|
}
|
|
5096
5138
|
});
|
|
5097
5139
|
} else if (part.content.type === "encrypted_code_execution_result") {
|
|
@@ -5104,7 +5146,7 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
|
|
|
5104
5146
|
encrypted_stdout: part.content.encrypted_stdout,
|
|
5105
5147
|
stderr: part.content.stderr,
|
|
5106
5148
|
return_code: part.content.return_code,
|
|
5107
|
-
content: (
|
|
5149
|
+
content: (_d2 = part.content.content) != null ? _d2 : []
|
|
5108
5150
|
}
|
|
5109
5151
|
});
|
|
5110
5152
|
} else if (part.content.type === "code_execution_tool_result_error") {
|
|
@@ -5598,7 +5640,10 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
|
|
|
5598
5640
|
return;
|
|
5599
5641
|
}
|
|
5600
5642
|
case "error": {
|
|
5601
|
-
controller.enqueue({
|
|
5643
|
+
controller.enqueue({
|
|
5644
|
+
type: "error",
|
|
5645
|
+
error: createAnthropicStreamError(value.error)
|
|
5646
|
+
});
|
|
5602
5647
|
return;
|
|
5603
5648
|
}
|
|
5604
5649
|
default: {
|
|
@@ -5619,14 +5664,17 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
|
|
|
5619
5664
|
}
|
|
5620
5665
|
if (((_b = result.value) == null ? void 0 : _b.type) === "error") {
|
|
5621
5666
|
const error = result.value.error;
|
|
5667
|
+
if (!isProviderStreamError(error)) {
|
|
5668
|
+
throw new Error("Expected a normalized Anthropic stream error");
|
|
5669
|
+
}
|
|
5622
5670
|
throw new APICallError({
|
|
5623
5671
|
message: error.message,
|
|
5624
5672
|
url,
|
|
5625
5673
|
requestBodyValues: body,
|
|
5626
|
-
statusCode: error.
|
|
5674
|
+
statusCode: (_c = error.statusCode) != null ? _c : 500,
|
|
5627
5675
|
responseHeaders,
|
|
5628
|
-
responseBody: JSON.stringify(error),
|
|
5629
|
-
isRetryable: error.
|
|
5676
|
+
responseBody: JSON.stringify(error.data),
|
|
5677
|
+
isRetryable: (_d = error.isRetryable) != null ? _d : false
|
|
5630
5678
|
});
|
|
5631
5679
|
}
|
|
5632
5680
|
} finally {
|
|
@@ -6003,24 +6051,26 @@ var AnthropicMessagesBatchLanguageModel = class _AnthropicMessagesBatchLanguageM
|
|
|
6003
6051
|
});
|
|
6004
6052
|
}
|
|
6005
6053
|
if (batch.results_url == null) {
|
|
6006
|
-
throw new
|
|
6007
|
-
|
|
6008
|
-
message: `Anthropic batch "${options.batchId}"
|
|
6054
|
+
throw new InvalidResponseDataError2({
|
|
6055
|
+
data: batch,
|
|
6056
|
+
message: `Anthropic batch "${options.batchId}" completed without batch output.`
|
|
6009
6057
|
});
|
|
6010
6058
|
}
|
|
6011
|
-
const { value:
|
|
6059
|
+
const { value: lines } = await getFromApi({
|
|
6012
6060
|
url: batch.results_url,
|
|
6013
6061
|
validateUrl: true,
|
|
6014
6062
|
credentialedOrigin: this.config.baseURL,
|
|
6015
6063
|
trustedOrigin: this.config.baseURL,
|
|
6016
6064
|
headers: await this.getBatchHeaders(options.headers),
|
|
6017
6065
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
6018
|
-
successfulResponseHandler:
|
|
6066
|
+
successfulResponseHandler: createJsonLinesResponseHandler(
|
|
6067
|
+
anthropicBatchResultLineSchema
|
|
6068
|
+
),
|
|
6019
6069
|
abortSignal: options.abortSignal,
|
|
6020
6070
|
fetch: this.config.fetch
|
|
6021
6071
|
});
|
|
6022
6072
|
return convertAsyncIteratorToReadableStream(
|
|
6023
|
-
this.iterateBatchResults(
|
|
6073
|
+
this.iterateBatchResults(lines)
|
|
6024
6074
|
);
|
|
6025
6075
|
}
|
|
6026
6076
|
async retrieveBatch(options) {
|
|
@@ -6037,8 +6087,8 @@ var AnthropicMessagesBatchLanguageModel = class _AnthropicMessagesBatchLanguageM
|
|
|
6037
6087
|
});
|
|
6038
6088
|
return batch;
|
|
6039
6089
|
}
|
|
6040
|
-
async *iterateBatchResults(
|
|
6041
|
-
for await (const line of
|
|
6090
|
+
async *iterateBatchResults(lines) {
|
|
6091
|
+
for await (const line of lines) {
|
|
6042
6092
|
yield await convertAnthropicBatchResult(line);
|
|
6043
6093
|
}
|
|
6044
6094
|
}
|
|
@@ -6144,22 +6194,12 @@ function mapAnthropicBatchStatus(rawStatus) {
|
|
|
6144
6194
|
}
|
|
6145
6195
|
}
|
|
6146
6196
|
function convertAnthropicRequestCounts(counts) {
|
|
6147
|
-
|
|
6148
|
-
counts.processing,
|
|
6149
|
-
counts.succeeded,
|
|
6150
|
-
counts.errored,
|
|
6151
|
-
counts.canceled,
|
|
6152
|
-
counts.expired
|
|
6153
|
-
];
|
|
6154
|
-
if (values.some((value) => !Number.isFinite(value) || value < 0)) {
|
|
6155
|
-
return void 0;
|
|
6156
|
-
}
|
|
6157
|
-
return {
|
|
6158
|
-
total: values.reduce((total, value) => total + value, 0),
|
|
6197
|
+
return normalizeBatchRequestCounts({
|
|
6198
|
+
total: counts.processing + counts.succeeded + counts.errored + counts.canceled + counts.expired,
|
|
6159
6199
|
pending: counts.processing,
|
|
6160
6200
|
completed: counts.succeeded,
|
|
6161
6201
|
failed: counts.errored + counts.canceled + counts.expired
|
|
6162
|
-
};
|
|
6202
|
+
});
|
|
6163
6203
|
}
|
|
6164
6204
|
async function convertAnthropicBatchResult(line) {
|
|
6165
6205
|
switch (line.result.type) {
|
|
@@ -6208,7 +6248,7 @@ async function convertAnthropicBatchResult(line) {
|
|
|
6208
6248
|
status: "failed",
|
|
6209
6249
|
error: {
|
|
6210
6250
|
message: `Anthropic returned a "${unsupportedPart.type}" content block, but tool content is not supported in AI SDK text batches.`,
|
|
6211
|
-
code: "
|
|
6251
|
+
code: "unsupported_content"
|
|
6212
6252
|
}
|
|
6213
6253
|
};
|
|
6214
6254
|
}
|
|
@@ -6353,54 +6393,6 @@ function mapAnthropicStopDetails2(stopDetails) {
|
|
|
6353
6393
|
...stopDetails.recommended_model != null ? { recommendedModel: stopDetails.recommended_model } : {}
|
|
6354
6394
|
};
|
|
6355
6395
|
}
|
|
6356
|
-
var rawStreamResponseHandler = async ({ response }) => {
|
|
6357
|
-
if (response.body == null) {
|
|
6358
|
-
throw new EmptyResponseBodyError();
|
|
6359
|
-
}
|
|
6360
|
-
return { value: response.body };
|
|
6361
|
-
};
|
|
6362
|
-
async function* parseJsonLines(stream) {
|
|
6363
|
-
const reader = stream.getReader();
|
|
6364
|
-
const decoder = new TextDecoder();
|
|
6365
|
-
let buffer = "";
|
|
6366
|
-
let finished = false;
|
|
6367
|
-
try {
|
|
6368
|
-
while (true) {
|
|
6369
|
-
const { done, value } = await reader.read();
|
|
6370
|
-
if (done) {
|
|
6371
|
-
finished = true;
|
|
6372
|
-
buffer += decoder.decode();
|
|
6373
|
-
break;
|
|
6374
|
-
}
|
|
6375
|
-
buffer += decoder.decode(value, { stream: true });
|
|
6376
|
-
let lineEnd = buffer.indexOf("\n");
|
|
6377
|
-
while (lineEnd !== -1) {
|
|
6378
|
-
const line = buffer.slice(0, lineEnd).replace(/\r$/, "");
|
|
6379
|
-
buffer = buffer.slice(lineEnd + 1);
|
|
6380
|
-
if (line.trim().length > 0) {
|
|
6381
|
-
yield await parseJSON({
|
|
6382
|
-
text: line,
|
|
6383
|
-
schema: anthropicBatchResultLineSchema
|
|
6384
|
-
});
|
|
6385
|
-
}
|
|
6386
|
-
lineEnd = buffer.indexOf("\n");
|
|
6387
|
-
}
|
|
6388
|
-
}
|
|
6389
|
-
const finalLine = buffer.replace(/\r$/, "");
|
|
6390
|
-
if (finalLine.trim().length > 0) {
|
|
6391
|
-
yield await parseJSON({
|
|
6392
|
-
text: finalLine,
|
|
6393
|
-
schema: anthropicBatchResultLineSchema
|
|
6394
|
-
});
|
|
6395
|
-
}
|
|
6396
|
-
} finally {
|
|
6397
|
-
if (!finished) {
|
|
6398
|
-
await reader.cancel().catch(() => {
|
|
6399
|
-
});
|
|
6400
|
-
}
|
|
6401
|
-
reader.releaseLock();
|
|
6402
|
-
}
|
|
6403
|
-
}
|
|
6404
6396
|
|
|
6405
6397
|
// src/tool/bash_20241022.ts
|
|
6406
6398
|
import {
|
|
@@ -7059,6 +7051,10 @@ var anthropicSkillVersionResponseSchema = lazySchema25(
|
|
|
7059
7051
|
);
|
|
7060
7052
|
|
|
7061
7053
|
// src/skills/anthropic-skills.ts
|
|
7054
|
+
function encodePathSegment(value) {
|
|
7055
|
+
const encodedValue = encodeURIComponent(value);
|
|
7056
|
+
return encodedValue === "." ? "%252E" : encodedValue === ".." ? "%252E%252E" : encodedValue;
|
|
7057
|
+
}
|
|
7062
7058
|
var AnthropicSkills = class {
|
|
7063
7059
|
constructor(config) {
|
|
7064
7060
|
this.config = config;
|
|
@@ -7078,7 +7074,7 @@ var AnthropicSkills = class {
|
|
|
7078
7074
|
headers
|
|
7079
7075
|
}) {
|
|
7080
7076
|
const { value: versionResponse } = await getFromApi2({
|
|
7081
|
-
url: `${this.config.baseURL}/skills/${skillId}/versions/${version}`,
|
|
7077
|
+
url: `${this.config.baseURL}/skills/${encodePathSegment(skillId)}/versions/${encodePathSegment(version)}`,
|
|
7082
7078
|
validateUrl: false,
|
|
7083
7079
|
headers,
|
|
7084
7080
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
@@ -7140,7 +7136,7 @@ var AnthropicSkills = class {
|
|
|
7140
7136
|
};
|
|
7141
7137
|
|
|
7142
7138
|
// src/version.ts
|
|
7143
|
-
var VERSION = true ? "4.0.
|
|
7139
|
+
var VERSION = true ? "4.0.44" : "0.0.0-test";
|
|
7144
7140
|
|
|
7145
7141
|
// src/anthropic-provider.ts
|
|
7146
7142
|
var ANTHROPIC_API_URL = "https://api.anthropic.com";
|