( msg: UserMessage | NormalizedUserMessage, breakdown: MessageBreakdown, toolUseIdToName: Map<string, string>, )
| 803 | } |
| 804 | |
| 805 | function processUserMessage( |
| 806 | msg: UserMessage | NormalizedUserMessage, |
| 807 | breakdown: MessageBreakdown, |
| 808 | toolUseIdToName: Map<string, string>, |
| 809 | ): void { |
| 810 | // Handle both string and array content |
| 811 | if (typeof msg.message.content === 'string') { |
| 812 | // Simple string content |
| 813 | const tokens = roughTokenCountEstimation(msg.message.content) |
| 814 | breakdown.userMessageTokens += tokens |
| 815 | return |
| 816 | } |
| 817 | |
| 818 | // Process each content block individually |
| 819 | for (const block of msg.message.content) { |
| 820 | const blockStr = jsonStringify(block) |
| 821 | const blockTokens = roughTokenCountEstimation(blockStr) |
| 822 | |
| 823 | if ('type' in block && block.type === 'tool_result') { |
| 824 | breakdown.toolResultTokens += blockTokens |
| 825 | const toolUseId = 'tool_use_id' in block ? block.tool_use_id : undefined |
| 826 | const toolName = |
| 827 | (toolUseId ? toolUseIdToName.get(toolUseId) : undefined) || 'unknown' |
| 828 | breakdown.toolResultsByType.set( |
| 829 | toolName, |
| 830 | (breakdown.toolResultsByType.get(toolName) || 0) + blockTokens, |
| 831 | ) |
| 832 | } else { |
| 833 | // Text blocks or other non-tool content |
| 834 | breakdown.userMessageTokens += blockTokens |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | function processAttachment( |
| 840 | msg: AttachmentMessage, |
no test coverage detected