@ai-sdk/anthropic 2.0.49 → 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 +8 -0
- package/dist/index.js +433 -382
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +435 -382
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +432 -381
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +434 -381
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -11,16 +11,18 @@ import {
|
|
|
11
11
|
} from "@ai-sdk/provider-utils";
|
|
12
12
|
|
|
13
13
|
// src/version.ts
|
|
14
|
-
var VERSION = true ? "2.0.
|
|
14
|
+
var VERSION = true ? "2.0.50" : "0.0.0-test";
|
|
15
15
|
|
|
16
16
|
// src/anthropic-messages-language-model.ts
|
|
17
17
|
import {
|
|
18
|
+
APICallError,
|
|
18
19
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
19
20
|
} from "@ai-sdk/provider";
|
|
20
21
|
import {
|
|
21
22
|
combineHeaders,
|
|
22
23
|
createEventSourceResponseHandler,
|
|
23
24
|
createJsonResponseHandler,
|
|
25
|
+
DelayedPromise,
|
|
24
26
|
generateId,
|
|
25
27
|
parseProviderOptions as parseProviderOptions2,
|
|
26
28
|
postJsonToApi,
|
|
@@ -2338,8 +2340,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
2338
2340
|
});
|
|
2339
2341
|
const citationDocuments = this.extractCitationDocuments(options.prompt);
|
|
2340
2342
|
const body = { ...args, stream: true };
|
|
2343
|
+
const url = this.buildRequestUrl(true);
|
|
2341
2344
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
2342
|
-
url
|
|
2345
|
+
url,
|
|
2343
2346
|
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
2344
2347
|
body: this.transformRequestBody(body),
|
|
2345
2348
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
@@ -2362,441 +2365,491 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
2362
2365
|
let container = null;
|
|
2363
2366
|
let blockType = void 0;
|
|
2364
2367
|
const generateId3 = this.generateId;
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2368
|
+
let isFirstChunk = true;
|
|
2369
|
+
let stream = void 0;
|
|
2370
|
+
const returnPromise = new DelayedPromise();
|
|
2371
|
+
const transformedStream = response.pipeThrough(
|
|
2372
|
+
new TransformStream({
|
|
2373
|
+
start(controller) {
|
|
2374
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
2375
|
+
},
|
|
2376
|
+
async flush() {
|
|
2377
|
+
if (returnPromise.isPending()) {
|
|
2378
|
+
returnPromise.resolve({
|
|
2379
|
+
stream,
|
|
2380
|
+
request: { body },
|
|
2381
|
+
response: { headers: responseHeaders }
|
|
2382
|
+
});
|
|
2383
|
+
}
|
|
2384
|
+
},
|
|
2385
|
+
transform(chunk, controller) {
|
|
2386
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
2387
|
+
if (options.includeRawChunks) {
|
|
2388
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
2389
|
+
}
|
|
2390
|
+
if (!chunk.success) {
|
|
2391
|
+
controller.enqueue({ type: "error", error: chunk.error });
|
|
2392
|
+
return;
|
|
2393
|
+
}
|
|
2394
|
+
if (isFirstChunk) {
|
|
2395
|
+
if (chunk.value.type === "error") {
|
|
2396
|
+
returnPromise.reject(
|
|
2397
|
+
new APICallError({
|
|
2398
|
+
message: chunk.value.error.message,
|
|
2399
|
+
url,
|
|
2400
|
+
requestBodyValues: body,
|
|
2401
|
+
statusCode: chunk.value.error.type === "overloaded_error" ? 529 : 500,
|
|
2402
|
+
responseHeaders,
|
|
2403
|
+
responseBody: JSON.stringify(chunk.value.error),
|
|
2404
|
+
isRetryable: chunk.value.error.type === "overloaded_error"
|
|
2405
|
+
})
|
|
2406
|
+
);
|
|
2407
|
+
controller.terminate();
|
|
2408
|
+
return;
|
|
2375
2409
|
}
|
|
2376
|
-
|
|
2377
|
-
|
|
2410
|
+
isFirstChunk = false;
|
|
2411
|
+
returnPromise.resolve({
|
|
2412
|
+
stream,
|
|
2413
|
+
request: { body },
|
|
2414
|
+
response: { headers: responseHeaders }
|
|
2415
|
+
});
|
|
2416
|
+
}
|
|
2417
|
+
const value = chunk.value;
|
|
2418
|
+
switch (value.type) {
|
|
2419
|
+
case "ping": {
|
|
2378
2420
|
return;
|
|
2379
2421
|
}
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
type: "reasoning-start",
|
|
2409
|
-
id: String(value.index),
|
|
2410
|
-
providerMetadata: {
|
|
2411
|
-
anthropic: {
|
|
2412
|
-
redactedData: value.content_block.data
|
|
2413
|
-
}
|
|
2422
|
+
case "content_block_start": {
|
|
2423
|
+
const contentBlockType = value.content_block.type;
|
|
2424
|
+
blockType = contentBlockType;
|
|
2425
|
+
switch (contentBlockType) {
|
|
2426
|
+
case "text": {
|
|
2427
|
+
contentBlocks[value.index] = { type: "text" };
|
|
2428
|
+
controller.enqueue({
|
|
2429
|
+
type: "text-start",
|
|
2430
|
+
id: String(value.index)
|
|
2431
|
+
});
|
|
2432
|
+
return;
|
|
2433
|
+
}
|
|
2434
|
+
case "thinking": {
|
|
2435
|
+
contentBlocks[value.index] = { type: "reasoning" };
|
|
2436
|
+
controller.enqueue({
|
|
2437
|
+
type: "reasoning-start",
|
|
2438
|
+
id: String(value.index)
|
|
2439
|
+
});
|
|
2440
|
+
return;
|
|
2441
|
+
}
|
|
2442
|
+
case "redacted_thinking": {
|
|
2443
|
+
contentBlocks[value.index] = { type: "reasoning" };
|
|
2444
|
+
controller.enqueue({
|
|
2445
|
+
type: "reasoning-start",
|
|
2446
|
+
id: String(value.index),
|
|
2447
|
+
providerMetadata: {
|
|
2448
|
+
anthropic: {
|
|
2449
|
+
redactedData: value.content_block.data
|
|
2414
2450
|
}
|
|
2415
|
-
}
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2451
|
+
}
|
|
2452
|
+
});
|
|
2453
|
+
return;
|
|
2454
|
+
}
|
|
2455
|
+
case "tool_use": {
|
|
2456
|
+
contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
|
|
2457
|
+
type: "tool-call",
|
|
2458
|
+
toolCallId: value.content_block.id,
|
|
2459
|
+
toolName: value.content_block.name,
|
|
2460
|
+
input: "",
|
|
2461
|
+
firstDelta: true
|
|
2462
|
+
};
|
|
2463
|
+
controller.enqueue(
|
|
2464
|
+
usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
|
|
2465
|
+
type: "tool-input-start",
|
|
2466
|
+
id: value.content_block.id,
|
|
2467
|
+
toolName: value.content_block.name
|
|
2468
|
+
}
|
|
2469
|
+
);
|
|
2470
|
+
return;
|
|
2471
|
+
}
|
|
2472
|
+
case "server_tool_use": {
|
|
2473
|
+
if ([
|
|
2474
|
+
"web_fetch",
|
|
2475
|
+
"web_search",
|
|
2476
|
+
// code execution 20250825:
|
|
2477
|
+
"code_execution",
|
|
2478
|
+
// code execution 20250825 text editor:
|
|
2479
|
+
"text_editor_code_execution",
|
|
2480
|
+
// code execution 20250825 bash:
|
|
2481
|
+
"bash_code_execution"
|
|
2482
|
+
].includes(value.content_block.name)) {
|
|
2483
|
+
contentBlocks[value.index] = {
|
|
2420
2484
|
type: "tool-call",
|
|
2421
2485
|
toolCallId: value.content_block.id,
|
|
2422
2486
|
toolName: value.content_block.name,
|
|
2423
2487
|
input: "",
|
|
2488
|
+
providerExecuted: true,
|
|
2424
2489
|
firstDelta: true
|
|
2425
2490
|
};
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
);
|
|
2433
|
-
return;
|
|
2434
|
-
}
|
|
2435
|
-
case "server_tool_use": {
|
|
2436
|
-
if ([
|
|
2437
|
-
"web_fetch",
|
|
2438
|
-
"web_search",
|
|
2439
|
-
// code execution 20250825:
|
|
2440
|
-
"code_execution",
|
|
2441
|
-
// code execution 20250825 text editor:
|
|
2442
|
-
"text_editor_code_execution",
|
|
2443
|
-
// code execution 20250825 bash:
|
|
2444
|
-
"bash_code_execution"
|
|
2445
|
-
].includes(value.content_block.name)) {
|
|
2446
|
-
contentBlocks[value.index] = {
|
|
2447
|
-
type: "tool-call",
|
|
2448
|
-
toolCallId: value.content_block.id,
|
|
2449
|
-
toolName: value.content_block.name,
|
|
2450
|
-
input: "",
|
|
2451
|
-
providerExecuted: true,
|
|
2452
|
-
firstDelta: true
|
|
2453
|
-
};
|
|
2454
|
-
const mappedToolName = value.content_block.name === "text_editor_code_execution" || value.content_block.name === "bash_code_execution" ? "code_execution" : value.content_block.name;
|
|
2455
|
-
controller.enqueue({
|
|
2456
|
-
type: "tool-input-start",
|
|
2457
|
-
id: value.content_block.id,
|
|
2458
|
-
toolName: mappedToolName,
|
|
2459
|
-
providerExecuted: true
|
|
2460
|
-
});
|
|
2461
|
-
}
|
|
2462
|
-
return;
|
|
2491
|
+
const mappedToolName = value.content_block.name === "text_editor_code_execution" || value.content_block.name === "bash_code_execution" ? "code_execution" : value.content_block.name;
|
|
2492
|
+
controller.enqueue({
|
|
2493
|
+
type: "tool-input-start",
|
|
2494
|
+
id: value.content_block.id,
|
|
2495
|
+
toolName: mappedToolName,
|
|
2496
|
+
providerExecuted: true
|
|
2497
|
+
});
|
|
2463
2498
|
}
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2499
|
+
return;
|
|
2500
|
+
}
|
|
2501
|
+
case "web_fetch_tool_result": {
|
|
2502
|
+
const part = value.content_block;
|
|
2503
|
+
if (part.content.type === "web_fetch_result") {
|
|
2504
|
+
controller.enqueue({
|
|
2505
|
+
type: "tool-result",
|
|
2506
|
+
toolCallId: part.tool_use_id,
|
|
2507
|
+
toolName: "web_fetch",
|
|
2508
|
+
result: {
|
|
2509
|
+
type: "web_fetch_result",
|
|
2510
|
+
url: part.content.url,
|
|
2511
|
+
retrievedAt: part.content.retrieved_at,
|
|
2512
|
+
content: {
|
|
2513
|
+
type: part.content.content.type,
|
|
2514
|
+
title: part.content.content.title,
|
|
2515
|
+
citations: part.content.content.citations,
|
|
2516
|
+
source: {
|
|
2517
|
+
type: part.content.content.source.type,
|
|
2518
|
+
mediaType: part.content.content.source.media_type,
|
|
2519
|
+
data: part.content.content.source.data
|
|
2484
2520
|
}
|
|
2485
|
-
}
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
}
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
}
|
|
2501
|
-
return;
|
|
2521
|
+
}
|
|
2522
|
+
},
|
|
2523
|
+
providerExecuted: true
|
|
2524
|
+
});
|
|
2525
|
+
} else if (part.content.type === "web_fetch_tool_result_error") {
|
|
2526
|
+
controller.enqueue({
|
|
2527
|
+
type: "tool-result",
|
|
2528
|
+
toolCallId: part.tool_use_id,
|
|
2529
|
+
toolName: "web_fetch",
|
|
2530
|
+
isError: true,
|
|
2531
|
+
result: {
|
|
2532
|
+
type: "web_fetch_tool_result_error",
|
|
2533
|
+
errorCode: part.content.error_code
|
|
2534
|
+
},
|
|
2535
|
+
providerExecuted: true
|
|
2536
|
+
});
|
|
2502
2537
|
}
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
pageAge: (_a2 = result.page_age) != null ? _a2 : null,
|
|
2516
|
-
encryptedContent: result.encrypted_content,
|
|
2517
|
-
type: result.type
|
|
2518
|
-
};
|
|
2519
|
-
}),
|
|
2520
|
-
providerExecuted: true
|
|
2521
|
-
});
|
|
2522
|
-
for (const result of part.content) {
|
|
2523
|
-
controller.enqueue({
|
|
2524
|
-
type: "source",
|
|
2525
|
-
sourceType: "url",
|
|
2526
|
-
id: generateId3(),
|
|
2538
|
+
return;
|
|
2539
|
+
}
|
|
2540
|
+
case "web_search_tool_result": {
|
|
2541
|
+
const part = value.content_block;
|
|
2542
|
+
if (Array.isArray(part.content)) {
|
|
2543
|
+
controller.enqueue({
|
|
2544
|
+
type: "tool-result",
|
|
2545
|
+
toolCallId: part.tool_use_id,
|
|
2546
|
+
toolName: "web_search",
|
|
2547
|
+
result: part.content.map((result) => {
|
|
2548
|
+
var _a2;
|
|
2549
|
+
return {
|
|
2527
2550
|
url: result.url,
|
|
2528
2551
|
title: result.title,
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
controller.enqueue({
|
|
2538
|
-
type: "tool-result",
|
|
2539
|
-
toolCallId: part.tool_use_id,
|
|
2540
|
-
toolName: "web_search",
|
|
2541
|
-
isError: true,
|
|
2542
|
-
result: {
|
|
2543
|
-
type: "web_search_tool_result_error",
|
|
2544
|
-
errorCode: part.content.error_code
|
|
2545
|
-
},
|
|
2546
|
-
providerExecuted: true
|
|
2547
|
-
});
|
|
2548
|
-
}
|
|
2549
|
-
return;
|
|
2550
|
-
}
|
|
2551
|
-
// code execution 20250522:
|
|
2552
|
-
case "code_execution_tool_result": {
|
|
2553
|
-
const part = value.content_block;
|
|
2554
|
-
if (part.content.type === "code_execution_result") {
|
|
2555
|
-
controller.enqueue({
|
|
2556
|
-
type: "tool-result",
|
|
2557
|
-
toolCallId: part.tool_use_id,
|
|
2558
|
-
toolName: "code_execution",
|
|
2559
|
-
result: {
|
|
2560
|
-
type: part.content.type,
|
|
2561
|
-
stdout: part.content.stdout,
|
|
2562
|
-
stderr: part.content.stderr,
|
|
2563
|
-
return_code: part.content.return_code
|
|
2564
|
-
},
|
|
2565
|
-
providerExecuted: true
|
|
2566
|
-
});
|
|
2567
|
-
} else if (part.content.type === "code_execution_tool_result_error") {
|
|
2552
|
+
pageAge: (_a2 = result.page_age) != null ? _a2 : null,
|
|
2553
|
+
encryptedContent: result.encrypted_content,
|
|
2554
|
+
type: result.type
|
|
2555
|
+
};
|
|
2556
|
+
}),
|
|
2557
|
+
providerExecuted: true
|
|
2558
|
+
});
|
|
2559
|
+
for (const result of part.content) {
|
|
2568
2560
|
controller.enqueue({
|
|
2569
|
-
type: "
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2561
|
+
type: "source",
|
|
2562
|
+
sourceType: "url",
|
|
2563
|
+
id: generateId3(),
|
|
2564
|
+
url: result.url,
|
|
2565
|
+
title: result.title,
|
|
2566
|
+
providerMetadata: {
|
|
2567
|
+
anthropic: {
|
|
2568
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
2569
|
+
}
|
|
2570
|
+
}
|
|
2578
2571
|
});
|
|
2579
2572
|
}
|
|
2580
|
-
|
|
2573
|
+
} else {
|
|
2574
|
+
controller.enqueue({
|
|
2575
|
+
type: "tool-result",
|
|
2576
|
+
toolCallId: part.tool_use_id,
|
|
2577
|
+
toolName: "web_search",
|
|
2578
|
+
isError: true,
|
|
2579
|
+
result: {
|
|
2580
|
+
type: "web_search_tool_result_error",
|
|
2581
|
+
errorCode: part.content.error_code
|
|
2582
|
+
},
|
|
2583
|
+
providerExecuted: true
|
|
2584
|
+
});
|
|
2581
2585
|
}
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
+
return;
|
|
2587
|
+
}
|
|
2588
|
+
// code execution 20250522:
|
|
2589
|
+
case "code_execution_tool_result": {
|
|
2590
|
+
const part = value.content_block;
|
|
2591
|
+
if (part.content.type === "code_execution_result") {
|
|
2586
2592
|
controller.enqueue({
|
|
2587
2593
|
type: "tool-result",
|
|
2588
2594
|
toolCallId: part.tool_use_id,
|
|
2589
2595
|
toolName: "code_execution",
|
|
2590
|
-
result:
|
|
2596
|
+
result: {
|
|
2597
|
+
type: part.content.type,
|
|
2598
|
+
stdout: part.content.stdout,
|
|
2599
|
+
stderr: part.content.stderr,
|
|
2600
|
+
return_code: part.content.return_code
|
|
2601
|
+
},
|
|
2602
|
+
providerExecuted: true
|
|
2603
|
+
});
|
|
2604
|
+
} else if (part.content.type === "code_execution_tool_result_error") {
|
|
2605
|
+
controller.enqueue({
|
|
2606
|
+
type: "tool-result",
|
|
2607
|
+
toolCallId: part.tool_use_id,
|
|
2608
|
+
toolName: "code_execution",
|
|
2609
|
+
isError: true,
|
|
2610
|
+
result: {
|
|
2611
|
+
type: "code_execution_tool_result_error",
|
|
2612
|
+
errorCode: part.content.error_code
|
|
2613
|
+
},
|
|
2591
2614
|
providerExecuted: true
|
|
2592
2615
|
});
|
|
2593
|
-
return;
|
|
2594
|
-
}
|
|
2595
|
-
default: {
|
|
2596
|
-
const _exhaustiveCheck = contentBlockType;
|
|
2597
|
-
throw new Error(
|
|
2598
|
-
`Unsupported content block type: ${_exhaustiveCheck}`
|
|
2599
|
-
);
|
|
2600
2616
|
}
|
|
2617
|
+
return;
|
|
2618
|
+
}
|
|
2619
|
+
// code execution 20250825:
|
|
2620
|
+
case "bash_code_execution_tool_result":
|
|
2621
|
+
case "text_editor_code_execution_tool_result": {
|
|
2622
|
+
const part = value.content_block;
|
|
2623
|
+
controller.enqueue({
|
|
2624
|
+
type: "tool-result",
|
|
2625
|
+
toolCallId: part.tool_use_id,
|
|
2626
|
+
toolName: "code_execution",
|
|
2627
|
+
result: part.content,
|
|
2628
|
+
providerExecuted: true
|
|
2629
|
+
});
|
|
2630
|
+
return;
|
|
2631
|
+
}
|
|
2632
|
+
default: {
|
|
2633
|
+
const _exhaustiveCheck = contentBlockType;
|
|
2634
|
+
throw new Error(
|
|
2635
|
+
`Unsupported content block type: ${_exhaustiveCheck}`
|
|
2636
|
+
);
|
|
2601
2637
|
}
|
|
2602
2638
|
}
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2639
|
+
}
|
|
2640
|
+
case "content_block_stop": {
|
|
2641
|
+
if (contentBlocks[value.index] != null) {
|
|
2642
|
+
const contentBlock = contentBlocks[value.index];
|
|
2643
|
+
switch (contentBlock.type) {
|
|
2644
|
+
case "text": {
|
|
2645
|
+
controller.enqueue({
|
|
2646
|
+
type: "text-end",
|
|
2647
|
+
id: String(value.index)
|
|
2648
|
+
});
|
|
2649
|
+
break;
|
|
2650
|
+
}
|
|
2651
|
+
case "reasoning": {
|
|
2652
|
+
controller.enqueue({
|
|
2653
|
+
type: "reasoning-end",
|
|
2654
|
+
id: String(value.index)
|
|
2655
|
+
});
|
|
2656
|
+
break;
|
|
2657
|
+
}
|
|
2658
|
+
case "tool-call":
|
|
2659
|
+
if (!usesJsonResponseTool) {
|
|
2608
2660
|
controller.enqueue({
|
|
2609
|
-
type: "
|
|
2610
|
-
id:
|
|
2661
|
+
type: "tool-input-end",
|
|
2662
|
+
id: contentBlock.toolCallId
|
|
2611
2663
|
});
|
|
2612
|
-
|
|
2613
|
-
}
|
|
2614
|
-
case "reasoning": {
|
|
2664
|
+
const toolName = contentBlock.toolName === "text_editor_code_execution" || contentBlock.toolName === "bash_code_execution" ? "code_execution" : contentBlock.toolName;
|
|
2615
2665
|
controller.enqueue({
|
|
2616
|
-
type: "
|
|
2617
|
-
|
|
2666
|
+
type: "tool-call",
|
|
2667
|
+
toolCallId: contentBlock.toolCallId,
|
|
2668
|
+
toolName,
|
|
2669
|
+
input: contentBlock.input,
|
|
2670
|
+
providerExecuted: contentBlock.providerExecuted
|
|
2618
2671
|
});
|
|
2619
|
-
break;
|
|
2620
2672
|
}
|
|
2621
|
-
|
|
2622
|
-
if (!usesJsonResponseTool) {
|
|
2623
|
-
controller.enqueue({
|
|
2624
|
-
type: "tool-input-end",
|
|
2625
|
-
id: contentBlock.toolCallId
|
|
2626
|
-
});
|
|
2627
|
-
const toolName = contentBlock.toolName === "text_editor_code_execution" || contentBlock.toolName === "bash_code_execution" ? "code_execution" : contentBlock.toolName;
|
|
2628
|
-
controller.enqueue({
|
|
2629
|
-
type: "tool-call",
|
|
2630
|
-
toolCallId: contentBlock.toolCallId,
|
|
2631
|
-
toolName,
|
|
2632
|
-
input: contentBlock.input,
|
|
2633
|
-
providerExecuted: contentBlock.providerExecuted
|
|
2634
|
-
});
|
|
2635
|
-
}
|
|
2636
|
-
break;
|
|
2637
|
-
}
|
|
2638
|
-
delete contentBlocks[value.index];
|
|
2673
|
+
break;
|
|
2639
2674
|
}
|
|
2640
|
-
|
|
2641
|
-
return;
|
|
2675
|
+
delete contentBlocks[value.index];
|
|
2642
2676
|
}
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
type: "text-delta",
|
|
2652
|
-
id: String(value.index),
|
|
2653
|
-
delta: value.delta.text
|
|
2654
|
-
});
|
|
2677
|
+
blockType = void 0;
|
|
2678
|
+
return;
|
|
2679
|
+
}
|
|
2680
|
+
case "content_block_delta": {
|
|
2681
|
+
const deltaType = value.delta.type;
|
|
2682
|
+
switch (deltaType) {
|
|
2683
|
+
case "text_delta": {
|
|
2684
|
+
if (usesJsonResponseTool) {
|
|
2655
2685
|
return;
|
|
2656
2686
|
}
|
|
2657
|
-
|
|
2687
|
+
controller.enqueue({
|
|
2688
|
+
type: "text-delta",
|
|
2689
|
+
id: String(value.index),
|
|
2690
|
+
delta: value.delta.text
|
|
2691
|
+
});
|
|
2692
|
+
return;
|
|
2693
|
+
}
|
|
2694
|
+
case "thinking_delta": {
|
|
2695
|
+
controller.enqueue({
|
|
2696
|
+
type: "reasoning-delta",
|
|
2697
|
+
id: String(value.index),
|
|
2698
|
+
delta: value.delta.thinking
|
|
2699
|
+
});
|
|
2700
|
+
return;
|
|
2701
|
+
}
|
|
2702
|
+
case "signature_delta": {
|
|
2703
|
+
if (blockType === "thinking") {
|
|
2658
2704
|
controller.enqueue({
|
|
2659
2705
|
type: "reasoning-delta",
|
|
2660
2706
|
id: String(value.index),
|
|
2661
|
-
delta:
|
|
2707
|
+
delta: "",
|
|
2708
|
+
providerMetadata: {
|
|
2709
|
+
anthropic: {
|
|
2710
|
+
signature: value.delta.signature
|
|
2711
|
+
}
|
|
2712
|
+
}
|
|
2662
2713
|
});
|
|
2663
|
-
return;
|
|
2664
2714
|
}
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
providerMetadata: {
|
|
2672
|
-
anthropic: {
|
|
2673
|
-
signature: value.delta.signature
|
|
2674
|
-
}
|
|
2675
|
-
}
|
|
2676
|
-
});
|
|
2677
|
-
}
|
|
2715
|
+
return;
|
|
2716
|
+
}
|
|
2717
|
+
case "input_json_delta": {
|
|
2718
|
+
const contentBlock = contentBlocks[value.index];
|
|
2719
|
+
let delta = value.delta.partial_json;
|
|
2720
|
+
if (delta.length === 0) {
|
|
2678
2721
|
return;
|
|
2679
2722
|
}
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
let delta = value.delta.partial_json;
|
|
2683
|
-
if (delta.length === 0) {
|
|
2723
|
+
if (usesJsonResponseTool) {
|
|
2724
|
+
if ((contentBlock == null ? void 0 : contentBlock.type) !== "text") {
|
|
2684
2725
|
return;
|
|
2685
2726
|
}
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
});
|
|
2695
|
-
} else {
|
|
2696
|
-
if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
|
|
2697
|
-
return;
|
|
2698
|
-
}
|
|
2699
|
-
if (contentBlock.firstDelta && (contentBlock.toolName === "bash_code_execution" || contentBlock.toolName === "text_editor_code_execution")) {
|
|
2700
|
-
delta = `{"type": "${contentBlock.toolName}",${delta.substring(1)}`;
|
|
2701
|
-
}
|
|
2702
|
-
controller.enqueue({
|
|
2703
|
-
type: "tool-input-delta",
|
|
2704
|
-
id: contentBlock.toolCallId,
|
|
2705
|
-
delta
|
|
2706
|
-
});
|
|
2707
|
-
contentBlock.input += delta;
|
|
2708
|
-
contentBlock.firstDelta = false;
|
|
2727
|
+
controller.enqueue({
|
|
2728
|
+
type: "text-delta",
|
|
2729
|
+
id: String(value.index),
|
|
2730
|
+
delta
|
|
2731
|
+
});
|
|
2732
|
+
} else {
|
|
2733
|
+
if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
|
|
2734
|
+
return;
|
|
2709
2735
|
}
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
case "citations_delta": {
|
|
2713
|
-
const citation = value.delta.citation;
|
|
2714
|
-
const source = createCitationSource(
|
|
2715
|
-
citation,
|
|
2716
|
-
citationDocuments,
|
|
2717
|
-
generateId3
|
|
2718
|
-
);
|
|
2719
|
-
if (source) {
|
|
2720
|
-
controller.enqueue(source);
|
|
2736
|
+
if (contentBlock.firstDelta && (contentBlock.toolName === "bash_code_execution" || contentBlock.toolName === "text_editor_code_execution")) {
|
|
2737
|
+
delta = `{"type": "${contentBlock.toolName}",${delta.substring(1)}`;
|
|
2721
2738
|
}
|
|
2722
|
-
|
|
2739
|
+
controller.enqueue({
|
|
2740
|
+
type: "tool-input-delta",
|
|
2741
|
+
id: contentBlock.toolCallId,
|
|
2742
|
+
delta
|
|
2743
|
+
});
|
|
2744
|
+
contentBlock.input += delta;
|
|
2745
|
+
contentBlock.firstDelta = false;
|
|
2723
2746
|
}
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2747
|
+
return;
|
|
2748
|
+
}
|
|
2749
|
+
case "citations_delta": {
|
|
2750
|
+
const citation = value.delta.citation;
|
|
2751
|
+
const source = createCitationSource(
|
|
2752
|
+
citation,
|
|
2753
|
+
citationDocuments,
|
|
2754
|
+
generateId3
|
|
2755
|
+
);
|
|
2756
|
+
if (source) {
|
|
2757
|
+
controller.enqueue(source);
|
|
2729
2758
|
}
|
|
2759
|
+
return;
|
|
2760
|
+
}
|
|
2761
|
+
default: {
|
|
2762
|
+
const _exhaustiveCheck = deltaType;
|
|
2763
|
+
throw new Error(
|
|
2764
|
+
`Unsupported delta type: ${_exhaustiveCheck}`
|
|
2765
|
+
);
|
|
2730
2766
|
}
|
|
2731
2767
|
}
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
} : null
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
|
|
2780
|
-
|
|
2768
|
+
}
|
|
2769
|
+
case "message_start": {
|
|
2770
|
+
usage.inputTokens = value.message.usage.input_tokens;
|
|
2771
|
+
usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
|
|
2772
|
+
rawUsage = {
|
|
2773
|
+
...value.message.usage
|
|
2774
|
+
};
|
|
2775
|
+
cacheCreationInputTokens = (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null;
|
|
2776
|
+
controller.enqueue({
|
|
2777
|
+
type: "response-metadata",
|
|
2778
|
+
id: (_d = value.message.id) != null ? _d : void 0,
|
|
2779
|
+
modelId: (_e = value.message.model) != null ? _e : void 0
|
|
2780
|
+
});
|
|
2781
|
+
return;
|
|
2782
|
+
}
|
|
2783
|
+
case "message_delta": {
|
|
2784
|
+
usage.outputTokens = value.usage.output_tokens;
|
|
2785
|
+
usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
|
|
2786
|
+
finishReason = mapAnthropicStopReason({
|
|
2787
|
+
finishReason: value.delta.stop_reason,
|
|
2788
|
+
isJsonResponseFromTool: usesJsonResponseTool
|
|
2789
|
+
});
|
|
2790
|
+
stopSequence = (_h = value.delta.stop_sequence) != null ? _h : null;
|
|
2791
|
+
container = value.delta.container != null ? {
|
|
2792
|
+
expiresAt: value.delta.container.expires_at,
|
|
2793
|
+
id: value.delta.container.id,
|
|
2794
|
+
skills: (_j = (_i = value.delta.container.skills) == null ? void 0 : _i.map((skill) => ({
|
|
2795
|
+
type: skill.type,
|
|
2796
|
+
skillId: skill.skill_id,
|
|
2797
|
+
version: skill.version
|
|
2798
|
+
}))) != null ? _j : null
|
|
2799
|
+
} : null;
|
|
2800
|
+
rawUsage = {
|
|
2801
|
+
...rawUsage,
|
|
2802
|
+
...value.usage
|
|
2803
|
+
};
|
|
2804
|
+
return;
|
|
2805
|
+
}
|
|
2806
|
+
case "message_stop": {
|
|
2807
|
+
controller.enqueue({
|
|
2808
|
+
type: "finish",
|
|
2809
|
+
finishReason,
|
|
2810
|
+
usage,
|
|
2811
|
+
providerMetadata: {
|
|
2812
|
+
anthropic: {
|
|
2813
|
+
usage: rawUsage != null ? rawUsage : null,
|
|
2814
|
+
cacheCreationInputTokens,
|
|
2815
|
+
stopSequence,
|
|
2816
|
+
container
|
|
2781
2817
|
}
|
|
2782
|
-
}
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
}
|
|
2818
|
+
}
|
|
2819
|
+
});
|
|
2820
|
+
return;
|
|
2821
|
+
}
|
|
2822
|
+
case "error": {
|
|
2823
|
+
controller.enqueue({ type: "error", error: value.error });
|
|
2824
|
+
return;
|
|
2825
|
+
}
|
|
2826
|
+
default: {
|
|
2827
|
+
const _exhaustiveCheck = value;
|
|
2828
|
+
throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
|
|
2793
2829
|
}
|
|
2794
2830
|
}
|
|
2795
|
-
}
|
|
2796
|
-
)
|
|
2797
|
-
|
|
2798
|
-
|
|
2799
|
-
|
|
2831
|
+
}
|
|
2832
|
+
})
|
|
2833
|
+
);
|
|
2834
|
+
const [streamForFirstChunk, streamForConsumer] = transformedStream.tee();
|
|
2835
|
+
stream = streamForConsumer;
|
|
2836
|
+
const reader = streamForFirstChunk.getReader();
|
|
2837
|
+
(async () => {
|
|
2838
|
+
try {
|
|
2839
|
+
const { done } = await reader.read();
|
|
2840
|
+
if (!done) {
|
|
2841
|
+
await reader.cancel();
|
|
2842
|
+
}
|
|
2843
|
+
} catch (error) {
|
|
2844
|
+
try {
|
|
2845
|
+
await reader.cancel();
|
|
2846
|
+
} catch (e) {
|
|
2847
|
+
}
|
|
2848
|
+
} finally {
|
|
2849
|
+
reader.releaseLock();
|
|
2850
|
+
}
|
|
2851
|
+
})();
|
|
2852
|
+
return returnPromise.promise;
|
|
2800
2853
|
}
|
|
2801
2854
|
};
|
|
2802
2855
|
function getModelCapabilities(modelId) {
|