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