(messages: Message[])
| 27 | } |
| 28 | |
| 29 | export function analyzeContext(messages: Message[]): TokenStats { |
| 30 | const stats: TokenStats = { |
| 31 | toolRequests: new Map(), |
| 32 | toolResults: new Map(), |
| 33 | humanMessages: 0, |
| 34 | assistantMessages: 0, |
| 35 | localCommandOutputs: 0, |
| 36 | other: 0, |
| 37 | attachments: new Map(), |
| 38 | duplicateFileReads: new Map(), |
| 39 | total: 0, |
| 40 | } |
| 41 | |
| 42 | const toolIdsToToolNames = new Map<string, string>() |
| 43 | const readToolIdToFilePath = new Map<string, string>() |
| 44 | const fileReadStats = new Map< |
| 45 | string, |
| 46 | { count: number; totalTokens: number } |
| 47 | >() |
| 48 | |
| 49 | messages.forEach(msg => { |
| 50 | if (msg.type === 'attachment') { |
| 51 | const type = msg.attachment.type || 'unknown' |
| 52 | stats.attachments.set(type, (stats.attachments.get(type) || 0) + 1) |
| 53 | } |
| 54 | }) |
| 55 | |
| 56 | const normalizedMessages = normalizeMessagesForAPI(messages) |
| 57 | normalizedMessages.forEach(msg => { |
| 58 | const { content } = msg.message |
| 59 | |
| 60 | // Not sure if this path is still used, but adding as a fallback |
| 61 | if (typeof content === 'string') { |
| 62 | const tokens = countTokens(content) |
| 63 | stats.total += tokens |
| 64 | // Check if this is a local command output |
| 65 | if (msg.type === 'user' && content.includes('local-command-stdout')) { |
| 66 | stats.localCommandOutputs += tokens |
| 67 | } else { |
| 68 | stats[msg.type === 'user' ? 'humanMessages' : 'assistantMessages'] += |
| 69 | tokens |
| 70 | } |
| 71 | } else { |
| 72 | content.forEach(block => |
| 73 | processBlock( |
| 74 | block, |
| 75 | msg, |
| 76 | stats, |
| 77 | toolIdsToToolNames, |
| 78 | readToolIdToFilePath, |
| 79 | fileReadStats, |
| 80 | ), |
| 81 | ) |
| 82 | } |
| 83 | }) |
| 84 | |
| 85 | // Calculate duplicate file reads |
| 86 | fileReadStats.forEach((data, path) => { |
no test coverage detected