MCPcopy Index your code
hub / github.com/codeaashu/claude-code / analyzeContext

Function analyzeContext

src/utils/contextAnalysis.ts:27–97  ·  view source on GitHub ↗
(messages: Message[])

Source from the content-addressed store, hash-verified

25}
26
27export 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) => {

Callers 1

compactConversationFunction · 0.85

Calls 5

normalizeMessagesForAPIFunction · 0.85
processBlockFunction · 0.85
forEachMethod · 0.80
getMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected