(block: ToolResultBlockParam)
| 136 | |
| 137 | // Helper to calculate tool result tokens |
| 138 | function calculateToolResultTokens(block: ToolResultBlockParam): number { |
| 139 | if (!block.content) { |
| 140 | return 0 |
| 141 | } |
| 142 | |
| 143 | if (typeof block.content === 'string') { |
| 144 | return roughTokenCountEstimation(block.content) |
| 145 | } |
| 146 | |
| 147 | // Array of TextBlockParam | ImageBlockParam | DocumentBlockParam |
| 148 | return block.content.reduce((sum, item) => { |
| 149 | if (item.type === 'text') { |
| 150 | return sum + roughTokenCountEstimation(item.text) |
| 151 | } else if (item.type === 'image' || item.type === 'document') { |
| 152 | // Images/documents are approximately 2000 tokens regardless of format |
| 153 | return sum + IMAGE_MAX_TOKEN_SIZE |
| 154 | } |
| 155 | return sum |
| 156 | }, 0) |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Estimate token count for messages by extracting text content |
no test coverage detected