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