@ai-sdk/anthropic 4.0.114 → 4.0.115

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/anthropic",
3
- "version": "4.0.114",
3
+ "version": "4.0.115",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -1160,6 +1160,206 @@ export const anthropicResponseSchema = lazySchema(() =>
1160
1160
  ),
1161
1161
  );
1162
1162
 
1163
+ function anthropicToolResultSchemas() {
1164
+ return [
1165
+ z.object({
1166
+ type: z.literal('mcp_tool_result'),
1167
+ tool_use_id: z.string(),
1168
+ is_error: z.boolean(),
1169
+ content: anthropicMcpToolResultContentSchema(),
1170
+ }),
1171
+ z.object({
1172
+ type: z.literal('web_fetch_tool_result'),
1173
+ tool_use_id: z.string(),
1174
+ caller: anthropicToolCallCallerSchema().optional(),
1175
+ content: z.union([
1176
+ z.object({
1177
+ type: z.literal('web_fetch_result'),
1178
+ url: z.string(),
1179
+ retrieved_at: z.string().nullable(),
1180
+ content: z.object({
1181
+ type: z.literal('document'),
1182
+ title: z.string().nullable(),
1183
+ citations: z.object({ enabled: z.boolean() }).optional(),
1184
+ source: z.union([
1185
+ z.object({
1186
+ type: z.literal('base64'),
1187
+ media_type: z.literal('application/pdf'),
1188
+ data: z.string(),
1189
+ }),
1190
+ z.object({
1191
+ type: z.literal('text'),
1192
+ media_type: z.literal('text/plain'),
1193
+ data: z.string(),
1194
+ }),
1195
+ ]),
1196
+ }),
1197
+ }),
1198
+ z.object({
1199
+ type: z.literal('web_fetch_tool_result_error'),
1200
+ error_code: z.string(),
1201
+ }),
1202
+ ]),
1203
+ }),
1204
+ z.object({
1205
+ type: z.literal('web_search_tool_result'),
1206
+ tool_use_id: z.string(),
1207
+ caller: anthropicToolCallCallerSchema().optional(),
1208
+ content: z.union([
1209
+ z.array(
1210
+ z.object({
1211
+ type: z.literal('web_search_result'),
1212
+ url: z.string(),
1213
+ title: z.string().nullable(),
1214
+ encrypted_content: z.string(),
1215
+ page_age: z.string().nullish(),
1216
+ }),
1217
+ ),
1218
+ z.object({
1219
+ type: z.literal('web_search_tool_result_error'),
1220
+ error_code: z.string(),
1221
+ }),
1222
+ ]),
1223
+ }),
1224
+ // code execution results for code_execution_20250522 tool:
1225
+ z.object({
1226
+ type: z.literal('code_execution_tool_result'),
1227
+ tool_use_id: z.string(),
1228
+ content: z.union([
1229
+ z.object({
1230
+ type: z.literal('code_execution_result'),
1231
+ stdout: z.string(),
1232
+ stderr: z.string(),
1233
+ return_code: z.number(),
1234
+ content: z
1235
+ .array(
1236
+ z.object({
1237
+ type: z.literal('code_execution_output'),
1238
+ file_id: z.string(),
1239
+ }),
1240
+ )
1241
+ .optional()
1242
+ .default([]),
1243
+ }),
1244
+ z.object({
1245
+ type: z.literal('encrypted_code_execution_result'),
1246
+ encrypted_stdout: z.string(),
1247
+ stderr: z.string(),
1248
+ return_code: z.number(),
1249
+ content: z
1250
+ .array(
1251
+ z.object({
1252
+ type: z.literal('code_execution_output'),
1253
+ file_id: z.string(),
1254
+ }),
1255
+ )
1256
+ .optional()
1257
+ .default([]),
1258
+ }),
1259
+ z.object({
1260
+ type: z.literal('code_execution_tool_result_error'),
1261
+ error_code: z.string(),
1262
+ }),
1263
+ ]),
1264
+ }),
1265
+ // bash code execution results for code_execution_20250825 tool:
1266
+ z.object({
1267
+ type: z.literal('bash_code_execution_tool_result'),
1268
+ tool_use_id: z.string(),
1269
+ content: z.discriminatedUnion('type', [
1270
+ z.object({
1271
+ type: z.literal('bash_code_execution_result'),
1272
+ content: z.array(
1273
+ z.object({
1274
+ type: z.literal('bash_code_execution_output'),
1275
+ file_id: z.string(),
1276
+ }),
1277
+ ),
1278
+ stdout: z.string(),
1279
+ stderr: z.string(),
1280
+ return_code: z.number(),
1281
+ }),
1282
+ z.object({
1283
+ type: z.literal('bash_code_execution_tool_result_error'),
1284
+ error_code: z.string(),
1285
+ }),
1286
+ ]),
1287
+ }),
1288
+ // text editor code execution results for code_execution_20250825 tool:
1289
+ z.object({
1290
+ type: z.literal('text_editor_code_execution_tool_result'),
1291
+ tool_use_id: z.string(),
1292
+ content: z.discriminatedUnion('type', [
1293
+ z.object({
1294
+ type: z.literal('text_editor_code_execution_tool_result_error'),
1295
+ error_code: z.string(),
1296
+ }),
1297
+ z.object({
1298
+ type: z.literal('text_editor_code_execution_view_result'),
1299
+ content: z.string(),
1300
+ file_type: z.string(),
1301
+ num_lines: z.number().nullable(),
1302
+ start_line: z.number().nullable(),
1303
+ total_lines: z.number().nullable(),
1304
+ }),
1305
+ z.object({
1306
+ type: z.literal('text_editor_code_execution_create_result'),
1307
+ is_file_update: z.boolean(),
1308
+ }),
1309
+ z.object({
1310
+ type: z.literal('text_editor_code_execution_str_replace_result'),
1311
+ lines: z.array(z.string()).nullable(),
1312
+ new_lines: z.number().nullable(),
1313
+ new_start: z.number().nullable(),
1314
+ old_lines: z.number().nullable(),
1315
+ old_start: z.number().nullable(),
1316
+ }),
1317
+ ]),
1318
+ }),
1319
+ // tool search tool results for tool_search_tool_regex_20251119 and tool_search_tool_bm25_20251119:
1320
+ z.object({
1321
+ type: z.literal('tool_search_tool_result'),
1322
+ tool_use_id: z.string(),
1323
+ content: z.union([
1324
+ z.object({
1325
+ type: z.literal('tool_search_tool_search_result'),
1326
+ tool_references: z.array(
1327
+ z.object({
1328
+ type: z.literal('tool_reference'),
1329
+ tool_name: z.string(),
1330
+ }),
1331
+ ),
1332
+ }),
1333
+ z.object({
1334
+ type: z.literal('tool_search_tool_result_error'),
1335
+ error_code: z.string(),
1336
+ }),
1337
+ ]),
1338
+ }),
1339
+ // advisor results for advisor_20260301:
1340
+ z.object({
1341
+ type: z.literal('advisor_tool_result'),
1342
+ tool_use_id: z.string(),
1343
+ content: z.discriminatedUnion('type', [
1344
+ z.object({
1345
+ type: z.literal('advisor_result'),
1346
+ text: z.string(),
1347
+ stop_reason: z.string().nullish(),
1348
+ }),
1349
+ z.object({
1350
+ type: z.literal('advisor_redacted_result'),
1351
+ encrypted_content: z.string(),
1352
+ stop_reason: z.string().nullish(),
1353
+ }),
1354
+ z.object({
1355
+ type: z.literal('advisor_tool_result_error'),
1356
+ error_code: z.string(),
1357
+ }),
1358
+ ]),
1359
+ }),
1360
+ ] as const;
1361
+ }
1362
+
1163
1363
  // limited version of the schema, focused on what is needed for the implementation
1164
1364
  // this approach limits breakages when the API changes and increases efficiency
1165
1365
  export const anthropicChunkSchema = lazySchema(() =>
@@ -1178,7 +1378,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1178
1378
  cache_creation_input_tokens: z.number().nullish(),
1179
1379
  cache_read_input_tokens: z.number().nullish(),
1180
1380
  }),
1181
- // Programmatic tool calling: content may be pre-populated for deferred tool calls
1381
+ // Resumed responses may contain deferred tool calls and server tool results.
1182
1382
  content: z
1183
1383
  .array(
1184
1384
  z.discriminatedUnion('type', [
@@ -1190,6 +1390,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1190
1390
  caller: anthropicToolCallCallerSchema().optional(),
1191
1391
  toolset_name: z.string().nullish(),
1192
1392
  }),
1393
+ ...anthropicToolResultSchemas(),
1193
1394
  ]),
1194
1395
  )
1195
1396
  .nullish(),
@@ -1251,203 +1452,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1251
1452
  input: z.unknown(),
1252
1453
  server_name: z.string(),
1253
1454
  }),
1254
- z.object({
1255
- type: z.literal('mcp_tool_result'),
1256
- tool_use_id: z.string(),
1257
- is_error: z.boolean(),
1258
- content: anthropicMcpToolResultContentSchema(),
1259
- }),
1260
- z.object({
1261
- type: z.literal('web_fetch_tool_result'),
1262
- tool_use_id: z.string(),
1263
- caller: anthropicToolCallCallerSchema().optional(),
1264
- content: z.union([
1265
- z.object({
1266
- type: z.literal('web_fetch_result'),
1267
- url: z.string(),
1268
- retrieved_at: z.string().nullable(),
1269
- content: z.object({
1270
- type: z.literal('document'),
1271
- title: z.string().nullable(),
1272
- citations: z.object({ enabled: z.boolean() }).optional(),
1273
- source: z.union([
1274
- z.object({
1275
- type: z.literal('base64'),
1276
- media_type: z.literal('application/pdf'),
1277
- data: z.string(),
1278
- }),
1279
- z.object({
1280
- type: z.literal('text'),
1281
- media_type: z.literal('text/plain'),
1282
- data: z.string(),
1283
- }),
1284
- ]),
1285
- }),
1286
- }),
1287
- z.object({
1288
- type: z.literal('web_fetch_tool_result_error'),
1289
- error_code: z.string(),
1290
- }),
1291
- ]),
1292
- }),
1293
- z.object({
1294
- type: z.literal('web_search_tool_result'),
1295
- tool_use_id: z.string(),
1296
- caller: anthropicToolCallCallerSchema().optional(),
1297
- content: z.union([
1298
- z.array(
1299
- z.object({
1300
- type: z.literal('web_search_result'),
1301
- url: z.string(),
1302
- title: z.string().nullable(),
1303
- encrypted_content: z.string(),
1304
- page_age: z.string().nullish(),
1305
- }),
1306
- ),
1307
- z.object({
1308
- type: z.literal('web_search_tool_result_error'),
1309
- error_code: z.string(),
1310
- }),
1311
- ]),
1312
- }),
1313
- // code execution results for code_execution_20250522 tool:
1314
- z.object({
1315
- type: z.literal('code_execution_tool_result'),
1316
- tool_use_id: z.string(),
1317
- content: z.union([
1318
- z.object({
1319
- type: z.literal('code_execution_result'),
1320
- stdout: z.string(),
1321
- stderr: z.string(),
1322
- return_code: z.number(),
1323
- content: z
1324
- .array(
1325
- z.object({
1326
- type: z.literal('code_execution_output'),
1327
- file_id: z.string(),
1328
- }),
1329
- )
1330
- .optional()
1331
- .default([]),
1332
- }),
1333
- z.object({
1334
- type: z.literal('encrypted_code_execution_result'),
1335
- encrypted_stdout: z.string(),
1336
- stderr: z.string(),
1337
- return_code: z.number(),
1338
- content: z
1339
- .array(
1340
- z.object({
1341
- type: z.literal('code_execution_output'),
1342
- file_id: z.string(),
1343
- }),
1344
- )
1345
- .optional()
1346
- .default([]),
1347
- }),
1348
- z.object({
1349
- type: z.literal('code_execution_tool_result_error'),
1350
- error_code: z.string(),
1351
- }),
1352
- ]),
1353
- }),
1354
- // bash code execution results for code_execution_20250825 tool:
1355
- z.object({
1356
- type: z.literal('bash_code_execution_tool_result'),
1357
- tool_use_id: z.string(),
1358
- content: z.discriminatedUnion('type', [
1359
- z.object({
1360
- type: z.literal('bash_code_execution_result'),
1361
- content: z.array(
1362
- z.object({
1363
- type: z.literal('bash_code_execution_output'),
1364
- file_id: z.string(),
1365
- }),
1366
- ),
1367
- stdout: z.string(),
1368
- stderr: z.string(),
1369
- return_code: z.number(),
1370
- }),
1371
- z.object({
1372
- type: z.literal('bash_code_execution_tool_result_error'),
1373
- error_code: z.string(),
1374
- }),
1375
- ]),
1376
- }),
1377
- // text editor code execution results for code_execution_20250825 tool:
1378
- z.object({
1379
- type: z.literal('text_editor_code_execution_tool_result'),
1380
- tool_use_id: z.string(),
1381
- content: z.discriminatedUnion('type', [
1382
- z.object({
1383
- type: z.literal('text_editor_code_execution_tool_result_error'),
1384
- error_code: z.string(),
1385
- }),
1386
- z.object({
1387
- type: z.literal('text_editor_code_execution_view_result'),
1388
- content: z.string(),
1389
- file_type: z.string(),
1390
- num_lines: z.number().nullable(),
1391
- start_line: z.number().nullable(),
1392
- total_lines: z.number().nullable(),
1393
- }),
1394
- z.object({
1395
- type: z.literal('text_editor_code_execution_create_result'),
1396
- is_file_update: z.boolean(),
1397
- }),
1398
- z.object({
1399
- type: z.literal(
1400
- 'text_editor_code_execution_str_replace_result',
1401
- ),
1402
- lines: z.array(z.string()).nullable(),
1403
- new_lines: z.number().nullable(),
1404
- new_start: z.number().nullable(),
1405
- old_lines: z.number().nullable(),
1406
- old_start: z.number().nullable(),
1407
- }),
1408
- ]),
1409
- }),
1410
- // tool search tool results for tool_search_tool_regex_20251119 and tool_search_tool_bm25_20251119:
1411
- z.object({
1412
- type: z.literal('tool_search_tool_result'),
1413
- tool_use_id: z.string(),
1414
- content: z.union([
1415
- z.object({
1416
- type: z.literal('tool_search_tool_search_result'),
1417
- tool_references: z.array(
1418
- z.object({
1419
- type: z.literal('tool_reference'),
1420
- tool_name: z.string(),
1421
- }),
1422
- ),
1423
- }),
1424
- z.object({
1425
- type: z.literal('tool_search_tool_result_error'),
1426
- error_code: z.string(),
1427
- }),
1428
- ]),
1429
- }),
1430
- // advisor results for advisor_20260301:
1431
- z.object({
1432
- type: z.literal('advisor_tool_result'),
1433
- tool_use_id: z.string(),
1434
- content: z.discriminatedUnion('type', [
1435
- z.object({
1436
- type: z.literal('advisor_result'),
1437
- text: z.string(),
1438
- stop_reason: z.string().nullish(),
1439
- }),
1440
- z.object({
1441
- type: z.literal('advisor_redacted_result'),
1442
- encrypted_content: z.string(),
1443
- stop_reason: z.string().nullish(),
1444
- }),
1445
- z.object({
1446
- type: z.literal('advisor_tool_result_error'),
1447
- error_code: z.string(),
1448
- }),
1449
- ]),
1450
- }),
1455
+ ...anthropicToolResultSchemas(),
1451
1456
  anthropicFallbackContentSchema(),
1452
1457
  ]),
1453
1458
  }),
@@ -2390,12 +2390,16 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
2390
2390
  controller.enqueue({ type: 'stream-start', warnings });
2391
2391
  },
2392
2392
 
2393
- transform(chunk, controller) {
2393
+ transform: function transformChunk(
2394
+ chunk,
2395
+ controller,
2396
+ emitRawChunks = true,
2397
+ ) {
2394
2398
  if (hasInvalidMessageSequence) {
2395
2399
  return;
2396
2400
  }
2397
2401
 
2398
- if (options.includeRawChunks) {
2402
+ if (options.includeRawChunks && emitRawChunks) {
2399
2403
  controller.enqueue({ type: 'raw', rawValue: chunk.rawValue });
2400
2404
  }
2401
2405
 
@@ -3383,8 +3387,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
3383
3387
  },
3384
3388
  });
3385
3389
 
3386
- // Programmatic tool calling: process pre-populated content blocks
3387
- // (for deferred tool calls, content may be in message_start)
3390
+ // Resumed responses may include deferred tool calls and server tool results.
3388
3391
  if (value.message.content != null) {
3389
3392
  for (
3390
3393
  let contentIndex = 0;
@@ -3426,6 +3429,21 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
3426
3429
  },
3427
3430
  }),
3428
3431
  });
3432
+ } else {
3433
+ // Reuse the block handler without emitting a synthetic raw chunk.
3434
+ transformChunk(
3435
+ {
3436
+ success: true,
3437
+ value: {
3438
+ type: 'content_block_start',
3439
+ index: contentIndex,
3440
+ content_block: part,
3441
+ },
3442
+ rawValue: part,
3443
+ },
3444
+ controller,
3445
+ false,
3446
+ );
3429
3447
  }
3430
3448
  }
3431
3449
  }