( msg: UserMessage | NormalizedUserMessage, breakdown: MessageBreakdown, toolUseIdToName: Map<string, string>, )
| 820 | } |
| 821 | |
| 822 | function processUserMessage( |
| 823 | msg: UserMessage | NormalizedUserMessage, |
| 824 | breakdown: MessageBreakdown, |
| 825 | toolUseIdToName: Map<string, string>, |
| 826 | ): void { |
| 827 | // Handle both string and array content |
| 828 | if (typeof msg.message!.content === 'string') { |
| 829 | // Simple string content |
| 830 | const tokens = roughTokenCountEstimation(msg.message!.content) |
| 831 | breakdown.userMessageTokens += tokens |
| 832 | return |
| 833 | } |
| 834 | |
| 835 | // Process each content block individually |
| 836 | for (const block of msg.message!.content ?? []) { |
| 837 | const blockStr = jsonStringify(block) |
| 838 | const blockTokens = roughTokenCountEstimation(blockStr) |
| 839 | |
| 840 | if ('type' in block && block.type === 'tool_result') { |
| 841 | const toolUseId = 'tool_use_id' in block ? block.tool_use_id : undefined |
| 842 | const toolName = |
| 843 | (toolUseId ? toolUseIdToName.get(toolUseId) : undefined) || 'unknown' |
| 844 | breakdown.toolResultsByType.set( |
| 845 | toolName, |
| 846 | (breakdown.toolResultsByType.get(toolName) || 0) + blockTokens, |
| 847 | ) |
| 848 | } else { |
| 849 | // Text blocks or other non-tool content |
| 850 | breakdown.userMessageTokens += blockTokens |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | function processAttachment( |
| 856 | msg: AttachmentMessage, |
no test coverage detected