(state: SessionState, messages: WithParts[])
| 7 | import { getLastUserMessage } from "./messages/query" |
| 8 | |
| 9 | export 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 | |
| 40 | export function getCurrentParams( |
| 41 | state: SessionState, |
no outgoing calls
no test coverage detected