@ai-sdk/anthropic 2.0.48 → 2.0.50
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 +14 -0
- package/dist/index.js +457 -387
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +459 -387
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +1 -0
- package/dist/internal/index.d.ts +1 -0
- package/dist/internal/index.js +456 -386
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +458 -386
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -31,7 +31,7 @@ var import_provider4 = require("@ai-sdk/provider");
|
|
|
31
31
|
var import_provider_utils20 = require("@ai-sdk/provider-utils");
|
|
32
32
|
|
|
33
33
|
// src/version.ts
|
|
34
|
-
var VERSION = true ? "2.0.
|
|
34
|
+
var VERSION = true ? "2.0.50" : "0.0.0-test";
|
|
35
35
|
|
|
36
36
|
// src/anthropic-messages-language-model.ts
|
|
37
37
|
var import_provider3 = require("@ai-sdk/provider");
|
|
@@ -1769,6 +1769,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1769
1769
|
return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
|
|
1770
1770
|
}
|
|
1771
1771
|
async getArgs({
|
|
1772
|
+
userSuppliedBetas,
|
|
1772
1773
|
prompt,
|
|
1773
1774
|
maxOutputTokens,
|
|
1774
1775
|
temperature,
|
|
@@ -1986,7 +1987,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1986
1987
|
tool_choice: anthropicToolChoice
|
|
1987
1988
|
},
|
|
1988
1989
|
warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
|
|
1989
|
-
betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
|
|
1990
|
+
betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas, ...userSuppliedBetas]),
|
|
1990
1991
|
usesJsonResponseTool: jsonResponseTool != null
|
|
1991
1992
|
};
|
|
1992
1993
|
}
|
|
@@ -1996,8 +1997,20 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1996
1997
|
}) {
|
|
1997
1998
|
return (0, import_provider_utils11.combineHeaders)(
|
|
1998
1999
|
await (0, import_provider_utils11.resolve)(this.config.headers),
|
|
1999
|
-
|
|
2000
|
-
|
|
2000
|
+
headers,
|
|
2001
|
+
betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {}
|
|
2002
|
+
);
|
|
2003
|
+
}
|
|
2004
|
+
async getBetasFromHeaders(requestHeaders) {
|
|
2005
|
+
var _a, _b;
|
|
2006
|
+
const configHeaders = await (0, import_provider_utils11.resolve)(this.config.headers);
|
|
2007
|
+
const configBetaHeader = (_a = configHeaders["anthropic-beta"]) != null ? _a : "";
|
|
2008
|
+
const requestBetaHeader = (_b = requestHeaders == null ? void 0 : requestHeaders["anthropic-beta"]) != null ? _b : "";
|
|
2009
|
+
return new Set(
|
|
2010
|
+
[
|
|
2011
|
+
...configBetaHeader.toLowerCase().split(","),
|
|
2012
|
+
...requestBetaHeader.toLowerCase().split(",")
|
|
2013
|
+
].map((beta) => beta.trim()).filter((beta) => beta !== "")
|
|
2001
2014
|
);
|
|
2002
2015
|
}
|
|
2003
2016
|
buildRequestUrl(isStreaming) {
|
|
@@ -2033,7 +2046,10 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
2033
2046
|
}
|
|
2034
2047
|
async doGenerate(options) {
|
|
2035
2048
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
2036
|
-
const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(
|
|
2049
|
+
const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs({
|
|
2050
|
+
...options,
|
|
2051
|
+
userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
|
|
2052
|
+
});
|
|
2037
2053
|
const citationDocuments = this.extractCitationDocuments(options.prompt);
|
|
2038
2054
|
const {
|
|
2039
2055
|
responseHeaders,
|
|
@@ -2298,11 +2314,15 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
2298
2314
|
};
|
|
2299
2315
|
}
|
|
2300
2316
|
async doStream(options) {
|
|
2301
|
-
const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(
|
|
2317
|
+
const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs({
|
|
2318
|
+
...options,
|
|
2319
|
+
userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
|
|
2320
|
+
});
|
|
2302
2321
|
const citationDocuments = this.extractCitationDocuments(options.prompt);
|
|
2303
2322
|
const body = { ...args, stream: true };
|
|
2323
|
+
const url = this.buildRequestUrl(true);
|
|
2304
2324
|
const { responseHeaders, value: response } = await (0, import_provider_utils11.postJsonToApi)({
|
|
2305
|
-
url
|
|
2325
|
+
url,
|
|
2306
2326
|
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
2307
2327
|
body: this.transformRequestBody(body),
|
|
2308
2328
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
@@ -2325,441 +2345,491 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
2325
2345
|
let container = null;
|
|
2326
2346
|
let blockType = void 0;
|
|
2327
2347
|
const generateId3 = this.generateId;
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2348
|
+
let isFirstChunk = true;
|
|
2349
|
+
let stream = void 0;
|
|
2350
|
+
const returnPromise = new import_provider_utils11.DelayedPromise();
|
|
2351
|
+
const transformedStream = response.pipeThrough(
|
|
2352
|
+
new TransformStream({
|
|
2353
|
+
start(controller) {
|
|
2354
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
2355
|
+
},
|
|
2356
|
+
async flush() {
|
|
2357
|
+
if (returnPromise.isPending()) {
|
|
2358
|
+
returnPromise.resolve({
|
|
2359
|
+
stream,
|
|
2360
|
+
request: { body },
|
|
2361
|
+
response: { headers: responseHeaders }
|
|
2362
|
+
});
|
|
2363
|
+
}
|
|
2364
|
+
},
|
|
2365
|
+
transform(chunk, controller) {
|
|
2366
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
2367
|
+
if (options.includeRawChunks) {
|
|
2368
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
2369
|
+
}
|
|
2370
|
+
if (!chunk.success) {
|
|
2371
|
+
controller.enqueue({ type: "error", error: chunk.error });
|
|
2372
|
+
return;
|
|
2373
|
+
}
|
|
2374
|
+
if (isFirstChunk) {
|
|
2375
|
+
if (chunk.value.type === "error") {
|
|
2376
|
+
returnPromise.reject(
|
|
2377
|
+
new import_provider3.APICallError({
|
|
2378
|
+
message: chunk.value.error.message,
|
|
2379
|
+
url,
|
|
2380
|
+
requestBodyValues: body,
|
|
2381
|
+
statusCode: chunk.value.error.type === "overloaded_error" ? 529 : 500,
|
|
2382
|
+
responseHeaders,
|
|
2383
|
+
responseBody: JSON.stringify(chunk.value.error),
|
|
2384
|
+
isRetryable: chunk.value.error.type === "overloaded_error"
|
|
2385
|
+
})
|
|
2386
|
+
);
|
|
2387
|
+
controller.terminate();
|
|
2388
|
+
return;
|
|
2338
2389
|
}
|
|
2339
|
-
|
|
2340
|
-
|
|
2390
|
+
isFirstChunk = false;
|
|
2391
|
+
returnPromise.resolve({
|
|
2392
|
+
stream,
|
|
2393
|
+
request: { body },
|
|
2394
|
+
response: { headers: responseHeaders }
|
|
2395
|
+
});
|
|
2396
|
+
}
|
|
2397
|
+
const value = chunk.value;
|
|
2398
|
+
switch (value.type) {
|
|
2399
|
+
case "ping": {
|
|
2341
2400
|
return;
|
|
2342
2401
|
}
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
type: "reasoning-start",
|
|
2372
|
-
id: String(value.index),
|
|
2373
|
-
providerMetadata: {
|
|
2374
|
-
anthropic: {
|
|
2375
|
-
redactedData: value.content_block.data
|
|
2376
|
-
}
|
|
2402
|
+
case "content_block_start": {
|
|
2403
|
+
const contentBlockType = value.content_block.type;
|
|
2404
|
+
blockType = contentBlockType;
|
|
2405
|
+
switch (contentBlockType) {
|
|
2406
|
+
case "text": {
|
|
2407
|
+
contentBlocks[value.index] = { type: "text" };
|
|
2408
|
+
controller.enqueue({
|
|
2409
|
+
type: "text-start",
|
|
2410
|
+
id: String(value.index)
|
|
2411
|
+
});
|
|
2412
|
+
return;
|
|
2413
|
+
}
|
|
2414
|
+
case "thinking": {
|
|
2415
|
+
contentBlocks[value.index] = { type: "reasoning" };
|
|
2416
|
+
controller.enqueue({
|
|
2417
|
+
type: "reasoning-start",
|
|
2418
|
+
id: String(value.index)
|
|
2419
|
+
});
|
|
2420
|
+
return;
|
|
2421
|
+
}
|
|
2422
|
+
case "redacted_thinking": {
|
|
2423
|
+
contentBlocks[value.index] = { type: "reasoning" };
|
|
2424
|
+
controller.enqueue({
|
|
2425
|
+
type: "reasoning-start",
|
|
2426
|
+
id: String(value.index),
|
|
2427
|
+
providerMetadata: {
|
|
2428
|
+
anthropic: {
|
|
2429
|
+
redactedData: value.content_block.data
|
|
2377
2430
|
}
|
|
2378
|
-
}
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2431
|
+
}
|
|
2432
|
+
});
|
|
2433
|
+
return;
|
|
2434
|
+
}
|
|
2435
|
+
case "tool_use": {
|
|
2436
|
+
contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
|
|
2437
|
+
type: "tool-call",
|
|
2438
|
+
toolCallId: value.content_block.id,
|
|
2439
|
+
toolName: value.content_block.name,
|
|
2440
|
+
input: "",
|
|
2441
|
+
firstDelta: true
|
|
2442
|
+
};
|
|
2443
|
+
controller.enqueue(
|
|
2444
|
+
usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
|
|
2445
|
+
type: "tool-input-start",
|
|
2446
|
+
id: value.content_block.id,
|
|
2447
|
+
toolName: value.content_block.name
|
|
2448
|
+
}
|
|
2449
|
+
);
|
|
2450
|
+
return;
|
|
2451
|
+
}
|
|
2452
|
+
case "server_tool_use": {
|
|
2453
|
+
if ([
|
|
2454
|
+
"web_fetch",
|
|
2455
|
+
"web_search",
|
|
2456
|
+
// code execution 20250825:
|
|
2457
|
+
"code_execution",
|
|
2458
|
+
// code execution 20250825 text editor:
|
|
2459
|
+
"text_editor_code_execution",
|
|
2460
|
+
// code execution 20250825 bash:
|
|
2461
|
+
"bash_code_execution"
|
|
2462
|
+
].includes(value.content_block.name)) {
|
|
2463
|
+
contentBlocks[value.index] = {
|
|
2383
2464
|
type: "tool-call",
|
|
2384
2465
|
toolCallId: value.content_block.id,
|
|
2385
2466
|
toolName: value.content_block.name,
|
|
2386
2467
|
input: "",
|
|
2468
|
+
providerExecuted: true,
|
|
2387
2469
|
firstDelta: true
|
|
2388
2470
|
};
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
);
|
|
2396
|
-
return;
|
|
2397
|
-
}
|
|
2398
|
-
case "server_tool_use": {
|
|
2399
|
-
if ([
|
|
2400
|
-
"web_fetch",
|
|
2401
|
-
"web_search",
|
|
2402
|
-
// code execution 20250825:
|
|
2403
|
-
"code_execution",
|
|
2404
|
-
// code execution 20250825 text editor:
|
|
2405
|
-
"text_editor_code_execution",
|
|
2406
|
-
// code execution 20250825 bash:
|
|
2407
|
-
"bash_code_execution"
|
|
2408
|
-
].includes(value.content_block.name)) {
|
|
2409
|
-
contentBlocks[value.index] = {
|
|
2410
|
-
type: "tool-call",
|
|
2411
|
-
toolCallId: value.content_block.id,
|
|
2412
|
-
toolName: value.content_block.name,
|
|
2413
|
-
input: "",
|
|
2414
|
-
providerExecuted: true,
|
|
2415
|
-
firstDelta: true
|
|
2416
|
-
};
|
|
2417
|
-
const mappedToolName = value.content_block.name === "text_editor_code_execution" || value.content_block.name === "bash_code_execution" ? "code_execution" : value.content_block.name;
|
|
2418
|
-
controller.enqueue({
|
|
2419
|
-
type: "tool-input-start",
|
|
2420
|
-
id: value.content_block.id,
|
|
2421
|
-
toolName: mappedToolName,
|
|
2422
|
-
providerExecuted: true
|
|
2423
|
-
});
|
|
2424
|
-
}
|
|
2425
|
-
return;
|
|
2471
|
+
const mappedToolName = value.content_block.name === "text_editor_code_execution" || value.content_block.name === "bash_code_execution" ? "code_execution" : value.content_block.name;
|
|
2472
|
+
controller.enqueue({
|
|
2473
|
+
type: "tool-input-start",
|
|
2474
|
+
id: value.content_block.id,
|
|
2475
|
+
toolName: mappedToolName,
|
|
2476
|
+
providerExecuted: true
|
|
2477
|
+
});
|
|
2426
2478
|
}
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2479
|
+
return;
|
|
2480
|
+
}
|
|
2481
|
+
case "web_fetch_tool_result": {
|
|
2482
|
+
const part = value.content_block;
|
|
2483
|
+
if (part.content.type === "web_fetch_result") {
|
|
2484
|
+
controller.enqueue({
|
|
2485
|
+
type: "tool-result",
|
|
2486
|
+
toolCallId: part.tool_use_id,
|
|
2487
|
+
toolName: "web_fetch",
|
|
2488
|
+
result: {
|
|
2489
|
+
type: "web_fetch_result",
|
|
2490
|
+
url: part.content.url,
|
|
2491
|
+
retrievedAt: part.content.retrieved_at,
|
|
2492
|
+
content: {
|
|
2493
|
+
type: part.content.content.type,
|
|
2494
|
+
title: part.content.content.title,
|
|
2495
|
+
citations: part.content.content.citations,
|
|
2496
|
+
source: {
|
|
2497
|
+
type: part.content.content.source.type,
|
|
2498
|
+
mediaType: part.content.content.source.media_type,
|
|
2499
|
+
data: part.content.content.source.data
|
|
2447
2500
|
}
|
|
2448
|
-
}
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
}
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
}
|
|
2464
|
-
return;
|
|
2501
|
+
}
|
|
2502
|
+
},
|
|
2503
|
+
providerExecuted: true
|
|
2504
|
+
});
|
|
2505
|
+
} else if (part.content.type === "web_fetch_tool_result_error") {
|
|
2506
|
+
controller.enqueue({
|
|
2507
|
+
type: "tool-result",
|
|
2508
|
+
toolCallId: part.tool_use_id,
|
|
2509
|
+
toolName: "web_fetch",
|
|
2510
|
+
isError: true,
|
|
2511
|
+
result: {
|
|
2512
|
+
type: "web_fetch_tool_result_error",
|
|
2513
|
+
errorCode: part.content.error_code
|
|
2514
|
+
},
|
|
2515
|
+
providerExecuted: true
|
|
2516
|
+
});
|
|
2465
2517
|
}
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
pageAge: (_a2 = result.page_age) != null ? _a2 : null,
|
|
2479
|
-
encryptedContent: result.encrypted_content,
|
|
2480
|
-
type: result.type
|
|
2481
|
-
};
|
|
2482
|
-
}),
|
|
2483
|
-
providerExecuted: true
|
|
2484
|
-
});
|
|
2485
|
-
for (const result of part.content) {
|
|
2486
|
-
controller.enqueue({
|
|
2487
|
-
type: "source",
|
|
2488
|
-
sourceType: "url",
|
|
2489
|
-
id: generateId3(),
|
|
2518
|
+
return;
|
|
2519
|
+
}
|
|
2520
|
+
case "web_search_tool_result": {
|
|
2521
|
+
const part = value.content_block;
|
|
2522
|
+
if (Array.isArray(part.content)) {
|
|
2523
|
+
controller.enqueue({
|
|
2524
|
+
type: "tool-result",
|
|
2525
|
+
toolCallId: part.tool_use_id,
|
|
2526
|
+
toolName: "web_search",
|
|
2527
|
+
result: part.content.map((result) => {
|
|
2528
|
+
var _a2;
|
|
2529
|
+
return {
|
|
2490
2530
|
url: result.url,
|
|
2491
2531
|
title: result.title,
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
controller.enqueue({
|
|
2501
|
-
type: "tool-result",
|
|
2502
|
-
toolCallId: part.tool_use_id,
|
|
2503
|
-
toolName: "web_search",
|
|
2504
|
-
isError: true,
|
|
2505
|
-
result: {
|
|
2506
|
-
type: "web_search_tool_result_error",
|
|
2507
|
-
errorCode: part.content.error_code
|
|
2508
|
-
},
|
|
2509
|
-
providerExecuted: true
|
|
2510
|
-
});
|
|
2511
|
-
}
|
|
2512
|
-
return;
|
|
2513
|
-
}
|
|
2514
|
-
// code execution 20250522:
|
|
2515
|
-
case "code_execution_tool_result": {
|
|
2516
|
-
const part = value.content_block;
|
|
2517
|
-
if (part.content.type === "code_execution_result") {
|
|
2518
|
-
controller.enqueue({
|
|
2519
|
-
type: "tool-result",
|
|
2520
|
-
toolCallId: part.tool_use_id,
|
|
2521
|
-
toolName: "code_execution",
|
|
2522
|
-
result: {
|
|
2523
|
-
type: part.content.type,
|
|
2524
|
-
stdout: part.content.stdout,
|
|
2525
|
-
stderr: part.content.stderr,
|
|
2526
|
-
return_code: part.content.return_code
|
|
2527
|
-
},
|
|
2528
|
-
providerExecuted: true
|
|
2529
|
-
});
|
|
2530
|
-
} else if (part.content.type === "code_execution_tool_result_error") {
|
|
2532
|
+
pageAge: (_a2 = result.page_age) != null ? _a2 : null,
|
|
2533
|
+
encryptedContent: result.encrypted_content,
|
|
2534
|
+
type: result.type
|
|
2535
|
+
};
|
|
2536
|
+
}),
|
|
2537
|
+
providerExecuted: true
|
|
2538
|
+
});
|
|
2539
|
+
for (const result of part.content) {
|
|
2531
2540
|
controller.enqueue({
|
|
2532
|
-
type: "
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
+
type: "source",
|
|
2542
|
+
sourceType: "url",
|
|
2543
|
+
id: generateId3(),
|
|
2544
|
+
url: result.url,
|
|
2545
|
+
title: result.title,
|
|
2546
|
+
providerMetadata: {
|
|
2547
|
+
anthropic: {
|
|
2548
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
2549
|
+
}
|
|
2550
|
+
}
|
|
2541
2551
|
});
|
|
2542
2552
|
}
|
|
2543
|
-
|
|
2553
|
+
} else {
|
|
2554
|
+
controller.enqueue({
|
|
2555
|
+
type: "tool-result",
|
|
2556
|
+
toolCallId: part.tool_use_id,
|
|
2557
|
+
toolName: "web_search",
|
|
2558
|
+
isError: true,
|
|
2559
|
+
result: {
|
|
2560
|
+
type: "web_search_tool_result_error",
|
|
2561
|
+
errorCode: part.content.error_code
|
|
2562
|
+
},
|
|
2563
|
+
providerExecuted: true
|
|
2564
|
+
});
|
|
2544
2565
|
}
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2566
|
+
return;
|
|
2567
|
+
}
|
|
2568
|
+
// code execution 20250522:
|
|
2569
|
+
case "code_execution_tool_result": {
|
|
2570
|
+
const part = value.content_block;
|
|
2571
|
+
if (part.content.type === "code_execution_result") {
|
|
2549
2572
|
controller.enqueue({
|
|
2550
2573
|
type: "tool-result",
|
|
2551
2574
|
toolCallId: part.tool_use_id,
|
|
2552
2575
|
toolName: "code_execution",
|
|
2553
|
-
result:
|
|
2576
|
+
result: {
|
|
2577
|
+
type: part.content.type,
|
|
2578
|
+
stdout: part.content.stdout,
|
|
2579
|
+
stderr: part.content.stderr,
|
|
2580
|
+
return_code: part.content.return_code
|
|
2581
|
+
},
|
|
2582
|
+
providerExecuted: true
|
|
2583
|
+
});
|
|
2584
|
+
} else if (part.content.type === "code_execution_tool_result_error") {
|
|
2585
|
+
controller.enqueue({
|
|
2586
|
+
type: "tool-result",
|
|
2587
|
+
toolCallId: part.tool_use_id,
|
|
2588
|
+
toolName: "code_execution",
|
|
2589
|
+
isError: true,
|
|
2590
|
+
result: {
|
|
2591
|
+
type: "code_execution_tool_result_error",
|
|
2592
|
+
errorCode: part.content.error_code
|
|
2593
|
+
},
|
|
2554
2594
|
providerExecuted: true
|
|
2555
2595
|
});
|
|
2556
|
-
return;
|
|
2557
|
-
}
|
|
2558
|
-
default: {
|
|
2559
|
-
const _exhaustiveCheck = contentBlockType;
|
|
2560
|
-
throw new Error(
|
|
2561
|
-
`Unsupported content block type: ${_exhaustiveCheck}`
|
|
2562
|
-
);
|
|
2563
2596
|
}
|
|
2597
|
+
return;
|
|
2598
|
+
}
|
|
2599
|
+
// code execution 20250825:
|
|
2600
|
+
case "bash_code_execution_tool_result":
|
|
2601
|
+
case "text_editor_code_execution_tool_result": {
|
|
2602
|
+
const part = value.content_block;
|
|
2603
|
+
controller.enqueue({
|
|
2604
|
+
type: "tool-result",
|
|
2605
|
+
toolCallId: part.tool_use_id,
|
|
2606
|
+
toolName: "code_execution",
|
|
2607
|
+
result: part.content,
|
|
2608
|
+
providerExecuted: true
|
|
2609
|
+
});
|
|
2610
|
+
return;
|
|
2611
|
+
}
|
|
2612
|
+
default: {
|
|
2613
|
+
const _exhaustiveCheck = contentBlockType;
|
|
2614
|
+
throw new Error(
|
|
2615
|
+
`Unsupported content block type: ${_exhaustiveCheck}`
|
|
2616
|
+
);
|
|
2564
2617
|
}
|
|
2565
2618
|
}
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2619
|
+
}
|
|
2620
|
+
case "content_block_stop": {
|
|
2621
|
+
if (contentBlocks[value.index] != null) {
|
|
2622
|
+
const contentBlock = contentBlocks[value.index];
|
|
2623
|
+
switch (contentBlock.type) {
|
|
2624
|
+
case "text": {
|
|
2625
|
+
controller.enqueue({
|
|
2626
|
+
type: "text-end",
|
|
2627
|
+
id: String(value.index)
|
|
2628
|
+
});
|
|
2629
|
+
break;
|
|
2630
|
+
}
|
|
2631
|
+
case "reasoning": {
|
|
2632
|
+
controller.enqueue({
|
|
2633
|
+
type: "reasoning-end",
|
|
2634
|
+
id: String(value.index)
|
|
2635
|
+
});
|
|
2636
|
+
break;
|
|
2637
|
+
}
|
|
2638
|
+
case "tool-call":
|
|
2639
|
+
if (!usesJsonResponseTool) {
|
|
2571
2640
|
controller.enqueue({
|
|
2572
|
-
type: "
|
|
2573
|
-
id:
|
|
2641
|
+
type: "tool-input-end",
|
|
2642
|
+
id: contentBlock.toolCallId
|
|
2574
2643
|
});
|
|
2575
|
-
|
|
2576
|
-
}
|
|
2577
|
-
case "reasoning": {
|
|
2644
|
+
const toolName = contentBlock.toolName === "text_editor_code_execution" || contentBlock.toolName === "bash_code_execution" ? "code_execution" : contentBlock.toolName;
|
|
2578
2645
|
controller.enqueue({
|
|
2579
|
-
type: "
|
|
2580
|
-
|
|
2646
|
+
type: "tool-call",
|
|
2647
|
+
toolCallId: contentBlock.toolCallId,
|
|
2648
|
+
toolName,
|
|
2649
|
+
input: contentBlock.input,
|
|
2650
|
+
providerExecuted: contentBlock.providerExecuted
|
|
2581
2651
|
});
|
|
2582
|
-
break;
|
|
2583
2652
|
}
|
|
2584
|
-
|
|
2585
|
-
if (!usesJsonResponseTool) {
|
|
2586
|
-
controller.enqueue({
|
|
2587
|
-
type: "tool-input-end",
|
|
2588
|
-
id: contentBlock.toolCallId
|
|
2589
|
-
});
|
|
2590
|
-
const toolName = contentBlock.toolName === "text_editor_code_execution" || contentBlock.toolName === "bash_code_execution" ? "code_execution" : contentBlock.toolName;
|
|
2591
|
-
controller.enqueue({
|
|
2592
|
-
type: "tool-call",
|
|
2593
|
-
toolCallId: contentBlock.toolCallId,
|
|
2594
|
-
toolName,
|
|
2595
|
-
input: contentBlock.input,
|
|
2596
|
-
providerExecuted: contentBlock.providerExecuted
|
|
2597
|
-
});
|
|
2598
|
-
}
|
|
2599
|
-
break;
|
|
2600
|
-
}
|
|
2601
|
-
delete contentBlocks[value.index];
|
|
2653
|
+
break;
|
|
2602
2654
|
}
|
|
2603
|
-
|
|
2604
|
-
return;
|
|
2655
|
+
delete contentBlocks[value.index];
|
|
2605
2656
|
}
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
type: "text-delta",
|
|
2615
|
-
id: String(value.index),
|
|
2616
|
-
delta: value.delta.text
|
|
2617
|
-
});
|
|
2657
|
+
blockType = void 0;
|
|
2658
|
+
return;
|
|
2659
|
+
}
|
|
2660
|
+
case "content_block_delta": {
|
|
2661
|
+
const deltaType = value.delta.type;
|
|
2662
|
+
switch (deltaType) {
|
|
2663
|
+
case "text_delta": {
|
|
2664
|
+
if (usesJsonResponseTool) {
|
|
2618
2665
|
return;
|
|
2619
2666
|
}
|
|
2620
|
-
|
|
2667
|
+
controller.enqueue({
|
|
2668
|
+
type: "text-delta",
|
|
2669
|
+
id: String(value.index),
|
|
2670
|
+
delta: value.delta.text
|
|
2671
|
+
});
|
|
2672
|
+
return;
|
|
2673
|
+
}
|
|
2674
|
+
case "thinking_delta": {
|
|
2675
|
+
controller.enqueue({
|
|
2676
|
+
type: "reasoning-delta",
|
|
2677
|
+
id: String(value.index),
|
|
2678
|
+
delta: value.delta.thinking
|
|
2679
|
+
});
|
|
2680
|
+
return;
|
|
2681
|
+
}
|
|
2682
|
+
case "signature_delta": {
|
|
2683
|
+
if (blockType === "thinking") {
|
|
2621
2684
|
controller.enqueue({
|
|
2622
2685
|
type: "reasoning-delta",
|
|
2623
2686
|
id: String(value.index),
|
|
2624
|
-
delta:
|
|
2687
|
+
delta: "",
|
|
2688
|
+
providerMetadata: {
|
|
2689
|
+
anthropic: {
|
|
2690
|
+
signature: value.delta.signature
|
|
2691
|
+
}
|
|
2692
|
+
}
|
|
2625
2693
|
});
|
|
2626
|
-
return;
|
|
2627
2694
|
}
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
-
|
|
2634
|
-
providerMetadata: {
|
|
2635
|
-
anthropic: {
|
|
2636
|
-
signature: value.delta.signature
|
|
2637
|
-
}
|
|
2638
|
-
}
|
|
2639
|
-
});
|
|
2640
|
-
}
|
|
2695
|
+
return;
|
|
2696
|
+
}
|
|
2697
|
+
case "input_json_delta": {
|
|
2698
|
+
const contentBlock = contentBlocks[value.index];
|
|
2699
|
+
let delta = value.delta.partial_json;
|
|
2700
|
+
if (delta.length === 0) {
|
|
2641
2701
|
return;
|
|
2642
2702
|
}
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
let delta = value.delta.partial_json;
|
|
2646
|
-
if (delta.length === 0) {
|
|
2703
|
+
if (usesJsonResponseTool) {
|
|
2704
|
+
if ((contentBlock == null ? void 0 : contentBlock.type) !== "text") {
|
|
2647
2705
|
return;
|
|
2648
2706
|
}
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
});
|
|
2658
|
-
} else {
|
|
2659
|
-
if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
|
|
2660
|
-
return;
|
|
2661
|
-
}
|
|
2662
|
-
if (contentBlock.firstDelta && (contentBlock.toolName === "bash_code_execution" || contentBlock.toolName === "text_editor_code_execution")) {
|
|
2663
|
-
delta = `{"type": "${contentBlock.toolName}",${delta.substring(1)}`;
|
|
2664
|
-
}
|
|
2665
|
-
controller.enqueue({
|
|
2666
|
-
type: "tool-input-delta",
|
|
2667
|
-
id: contentBlock.toolCallId,
|
|
2668
|
-
delta
|
|
2669
|
-
});
|
|
2670
|
-
contentBlock.input += delta;
|
|
2671
|
-
contentBlock.firstDelta = false;
|
|
2707
|
+
controller.enqueue({
|
|
2708
|
+
type: "text-delta",
|
|
2709
|
+
id: String(value.index),
|
|
2710
|
+
delta
|
|
2711
|
+
});
|
|
2712
|
+
} else {
|
|
2713
|
+
if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
|
|
2714
|
+
return;
|
|
2672
2715
|
}
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
case "citations_delta": {
|
|
2676
|
-
const citation = value.delta.citation;
|
|
2677
|
-
const source = createCitationSource(
|
|
2678
|
-
citation,
|
|
2679
|
-
citationDocuments,
|
|
2680
|
-
generateId3
|
|
2681
|
-
);
|
|
2682
|
-
if (source) {
|
|
2683
|
-
controller.enqueue(source);
|
|
2716
|
+
if (contentBlock.firstDelta && (contentBlock.toolName === "bash_code_execution" || contentBlock.toolName === "text_editor_code_execution")) {
|
|
2717
|
+
delta = `{"type": "${contentBlock.toolName}",${delta.substring(1)}`;
|
|
2684
2718
|
}
|
|
2685
|
-
|
|
2719
|
+
controller.enqueue({
|
|
2720
|
+
type: "tool-input-delta",
|
|
2721
|
+
id: contentBlock.toolCallId,
|
|
2722
|
+
delta
|
|
2723
|
+
});
|
|
2724
|
+
contentBlock.input += delta;
|
|
2725
|
+
contentBlock.firstDelta = false;
|
|
2686
2726
|
}
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2727
|
+
return;
|
|
2728
|
+
}
|
|
2729
|
+
case "citations_delta": {
|
|
2730
|
+
const citation = value.delta.citation;
|
|
2731
|
+
const source = createCitationSource(
|
|
2732
|
+
citation,
|
|
2733
|
+
citationDocuments,
|
|
2734
|
+
generateId3
|
|
2735
|
+
);
|
|
2736
|
+
if (source) {
|
|
2737
|
+
controller.enqueue(source);
|
|
2692
2738
|
}
|
|
2739
|
+
return;
|
|
2740
|
+
}
|
|
2741
|
+
default: {
|
|
2742
|
+
const _exhaustiveCheck = deltaType;
|
|
2743
|
+
throw new Error(
|
|
2744
|
+
`Unsupported delta type: ${_exhaustiveCheck}`
|
|
2745
|
+
);
|
|
2693
2746
|
}
|
|
2694
2747
|
}
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
} : null
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2748
|
+
}
|
|
2749
|
+
case "message_start": {
|
|
2750
|
+
usage.inputTokens = value.message.usage.input_tokens;
|
|
2751
|
+
usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
|
|
2752
|
+
rawUsage = {
|
|
2753
|
+
...value.message.usage
|
|
2754
|
+
};
|
|
2755
|
+
cacheCreationInputTokens = (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null;
|
|
2756
|
+
controller.enqueue({
|
|
2757
|
+
type: "response-metadata",
|
|
2758
|
+
id: (_d = value.message.id) != null ? _d : void 0,
|
|
2759
|
+
modelId: (_e = value.message.model) != null ? _e : void 0
|
|
2760
|
+
});
|
|
2761
|
+
return;
|
|
2762
|
+
}
|
|
2763
|
+
case "message_delta": {
|
|
2764
|
+
usage.outputTokens = value.usage.output_tokens;
|
|
2765
|
+
usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
|
|
2766
|
+
finishReason = mapAnthropicStopReason({
|
|
2767
|
+
finishReason: value.delta.stop_reason,
|
|
2768
|
+
isJsonResponseFromTool: usesJsonResponseTool
|
|
2769
|
+
});
|
|
2770
|
+
stopSequence = (_h = value.delta.stop_sequence) != null ? _h : null;
|
|
2771
|
+
container = value.delta.container != null ? {
|
|
2772
|
+
expiresAt: value.delta.container.expires_at,
|
|
2773
|
+
id: value.delta.container.id,
|
|
2774
|
+
skills: (_j = (_i = value.delta.container.skills) == null ? void 0 : _i.map((skill) => ({
|
|
2775
|
+
type: skill.type,
|
|
2776
|
+
skillId: skill.skill_id,
|
|
2777
|
+
version: skill.version
|
|
2778
|
+
}))) != null ? _j : null
|
|
2779
|
+
} : null;
|
|
2780
|
+
rawUsage = {
|
|
2781
|
+
...rawUsage,
|
|
2782
|
+
...value.usage
|
|
2783
|
+
};
|
|
2784
|
+
return;
|
|
2785
|
+
}
|
|
2786
|
+
case "message_stop": {
|
|
2787
|
+
controller.enqueue({
|
|
2788
|
+
type: "finish",
|
|
2789
|
+
finishReason,
|
|
2790
|
+
usage,
|
|
2791
|
+
providerMetadata: {
|
|
2792
|
+
anthropic: {
|
|
2793
|
+
usage: rawUsage != null ? rawUsage : null,
|
|
2794
|
+
cacheCreationInputTokens,
|
|
2795
|
+
stopSequence,
|
|
2796
|
+
container
|
|
2744
2797
|
}
|
|
2745
|
-
}
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
}
|
|
2798
|
+
}
|
|
2799
|
+
});
|
|
2800
|
+
return;
|
|
2801
|
+
}
|
|
2802
|
+
case "error": {
|
|
2803
|
+
controller.enqueue({ type: "error", error: value.error });
|
|
2804
|
+
return;
|
|
2805
|
+
}
|
|
2806
|
+
default: {
|
|
2807
|
+
const _exhaustiveCheck = value;
|
|
2808
|
+
throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
|
|
2756
2809
|
}
|
|
2757
2810
|
}
|
|
2758
|
-
}
|
|
2759
|
-
)
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2811
|
+
}
|
|
2812
|
+
})
|
|
2813
|
+
);
|
|
2814
|
+
const [streamForFirstChunk, streamForConsumer] = transformedStream.tee();
|
|
2815
|
+
stream = streamForConsumer;
|
|
2816
|
+
const reader = streamForFirstChunk.getReader();
|
|
2817
|
+
(async () => {
|
|
2818
|
+
try {
|
|
2819
|
+
const { done } = await reader.read();
|
|
2820
|
+
if (!done) {
|
|
2821
|
+
await reader.cancel();
|
|
2822
|
+
}
|
|
2823
|
+
} catch (error) {
|
|
2824
|
+
try {
|
|
2825
|
+
await reader.cancel();
|
|
2826
|
+
} catch (e) {
|
|
2827
|
+
}
|
|
2828
|
+
} finally {
|
|
2829
|
+
reader.releaseLock();
|
|
2830
|
+
}
|
|
2831
|
+
})();
|
|
2832
|
+
return returnPromise.promise;
|
|
2763
2833
|
}
|
|
2764
2834
|
};
|
|
2765
2835
|
function getModelCapabilities(modelId) {
|