(
messages: { message: AssistantMessage | NormalizedUserMessage }[],
)
| 1371 | * `AssistantMessage | NormalizedUserMessage`. |
| 1372 | */ |
| 1373 | export function buildSubagentLookups( |
| 1374 | messages: { message: AssistantMessage | NormalizedUserMessage }[], |
| 1375 | ): { lookups: MessageLookups; inProgressToolUseIDs: Set<string> } { |
| 1376 | const toolUseByToolUseID = new Map<string, ToolUseBlockParam>() |
| 1377 | const resolvedToolUseIDs = new Set<string>() |
| 1378 | const toolResultByToolUseID = new Map< |
| 1379 | string, |
| 1380 | NormalizedUserMessage & { type: 'user' } |
| 1381 | >() |
| 1382 | |
| 1383 | for (const { message: msg } of messages) { |
| 1384 | if (msg.type === 'assistant') { |
| 1385 | for (const content of msg.message.content) { |
| 1386 | if (content.type === 'tool_use') { |
| 1387 | toolUseByToolUseID.set(content.id, content as ToolUseBlockParam) |
| 1388 | } |
| 1389 | } |
| 1390 | } else if (msg.type === 'user') { |
| 1391 | for (const content of msg.message.content) { |
| 1392 | if (content.type === 'tool_result') { |
| 1393 | resolvedToolUseIDs.add(content.tool_use_id) |
| 1394 | toolResultByToolUseID.set(content.tool_use_id, msg) |
| 1395 | } |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | const inProgressToolUseIDs = new Set<string>() |
| 1401 | for (const id of toolUseByToolUseID.keys()) { |
| 1402 | if (!resolvedToolUseIDs.has(id)) { |
| 1403 | inProgressToolUseIDs.add(id) |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | return { |
| 1408 | lookups: { |
| 1409 | ...EMPTY_LOOKUPS, |
| 1410 | toolUseByToolUseID, |
| 1411 | resolvedToolUseIDs, |
| 1412 | toolResultByToolUseID, |
| 1413 | }, |
| 1414 | inProgressToolUseIDs, |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | /** |
| 1419 | * Get sibling tool use IDs using pre-computed lookup. O(1). |
no test coverage detected