MCPcopy Index your code
hub / github.com/Opencode-DCP/opencode-dynamic-context-pruning / getCurrentTokenUsage

Function getCurrentTokenUsage

lib/token-utils.ts:9–38  ·  view source on GitHub ↗
(state: SessionState, messages: WithParts[])

Source from the content-addressed store, hash-verified

7import { getLastUserMessage } from "./messages/query"
8
9export function getCurrentTokenUsage(state: SessionState, messages: WithParts[]): number {
10 for (let i = messages.length - 1; i >= 0; i--) {
11 const msg = messages[i]
12 if (msg.info.role !== "assistant") {
13 continue
14 }
15
16 const assistantInfo = msg.info as AssistantMessage
17 if ((assistantInfo.tokens?.output || 0) <= 0) {
18 continue
19 }
20
21 if (
22 state.lastCompaction > 0 &&
23 (msg.info.time.created < state.lastCompaction ||
24 (msg.info.summary === true && msg.info.time.created === state.lastCompaction))
25 ) {
26 return 0
27 }
28
29 const input = assistantInfo.tokens?.input || 0
30 const output = assistantInfo.tokens?.output || 0
31 const reasoning = assistantInfo.tokens?.reasoning || 0
32 const cacheRead = assistantInfo.tokens?.cache?.read || 0
33 const cacheWrite = assistantInfo.tokens?.cache?.write || 0
34 return input + output + reasoning + cacheRead + cacheWrite
35 }
36
37 return 0
38}
39
40export function getCurrentParams(
41 state: SessionState,

Callers 2

isContextOverLimitsFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected