1- import { readFile } from 'node:fs/promises' ;
21import process from 'node:process' ;
32import getStdin from 'get-stdin' ;
43import { define } from 'gunshi' ;
54import pc from 'picocolors' ;
5+ import { CONTEXT_USAGE_THRESHOLDS } from '../_consts.ts' ;
66import { calculateBurnRate } from '../_session-blocks.ts' ;
77import { sharedArgs } from '../_shared-args.ts' ;
88import { statuslineHookJsonSchema } from '../_types.ts' ;
99import { formatCurrency } from '../_utils.ts' ;
1010import { calculateTotals } from '../calculate-cost.ts' ;
11- import { getClaudePaths , loadDailyUsageData , loadSessionBlockData , loadSessionUsageById } from '../data-loader.ts' ;
11+ import { calculateContextTokens , getClaudePaths , loadDailyUsageData , loadSessionBlockData , loadSessionUsageById } from '../data-loader.ts' ;
1212import { log , logger } from '../logger.ts' ;
1313
1414/**
@@ -26,79 +26,6 @@ function formatRemainingTime(remaining: number): string {
2626 return `${ remainingMins } m left` ;
2727}
2828
29- /**
30- * Interface for transcript structure
31- */
32- type TranscriptUsage = {
33- input_tokens ?: number ;
34- } ;
35-
36- type TranscriptMessage = {
37- usage ?: TranscriptUsage ;
38- } ;
39-
40- type TranscriptData = {
41- messages ?: TranscriptMessage [ ] ;
42- usage ?: TranscriptUsage ;
43- } ;
44-
45- /**
46- * Calculate context tokens from transcript file
47- * @param transcriptPath - Path to the transcript JSON file
48- * @returns Object with context tokens info or null if unavailable
49- */
50- async function calculateContextTokens ( transcriptPath : string ) : Promise < {
51- totalInputTokens : number ;
52- percentage : number ;
53- contextLimit : number ;
54- } | null > {
55- let transcript : TranscriptData ;
56- try {
57- const content = await readFile ( transcriptPath , 'utf-8' ) ;
58- transcript = JSON . parse ( content ) as TranscriptData ;
59- }
60- catch ( error ) {
61- logger . debug ( 'Failed to read transcript file:' , error ) ;
62- return null ;
63- }
64-
65- // Calculate total input tokens from all messages with usage information
66- let totalInputTokens = 0 ;
67- const contextLimit = 200000 ; // Default Claude 4 context limit
68- let foundUsage = false ;
69-
70- // Look for usage information in the transcript
71- if ( transcript . messages != null && Array . isArray ( transcript . messages ) ) {
72- for ( const message of transcript . messages ) {
73- if ( message . usage ?. input_tokens != null ) {
74- totalInputTokens = Math . max ( totalInputTokens , message . usage . input_tokens ) ;
75- foundUsage = true ;
76- }
77- }
78- }
79-
80- // If no usage found in messages, check if there's a direct usage field
81- if ( ! foundUsage && transcript . usage ?. input_tokens != null ) {
82- totalInputTokens = transcript . usage . input_tokens ;
83- foundUsage = true ;
84- }
85-
86- // If still no usage found, return null
87- if ( ! foundUsage ) {
88- logger . debug ( 'No usage information found in transcript' ) ;
89- return null ;
90- }
91-
92- // Calculate percentage
93- const percentage = Math . round ( ( totalInputTokens / contextLimit ) * 100 ) ;
94-
95- return {
96- totalInputTokens,
97- percentage,
98- contextLimit,
99- } ;
100- }
101-
10229export const statuslineCommand = define ( {
10330 name : 'statusline' ,
10431 description : 'Display compact status line for Claude Code hooks (Beta)' ,
@@ -240,20 +167,15 @@ export const statuslineCommand = define({
240167 try {
241168 const contextData = await calculateContextTokens ( hookData . transcript_path ) ;
242169 if ( contextData != null ) {
243- // Format context percentage with color coding
244- let coloredPercentage = `${ contextData . percentage } %` ;
245- if ( contextData . percentage < 50 ) {
246- coloredPercentage = pc . green ( `${ contextData . percentage } %` ) ;
247- }
248- else if ( contextData . percentage < 80 ) {
249- coloredPercentage = pc . yellow ( `${ contextData . percentage } %` ) ;
250- }
251- else {
252- coloredPercentage = pc . red ( `${ contextData . percentage } %` ) ;
253- }
170+ // Format context percentage with color coding using constants
171+ const coloredPercentage = contextData . percentage < CONTEXT_USAGE_THRESHOLDS . LOW
172+ ? pc . green ( `${ contextData . percentage } %` )
173+ : contextData . percentage < CONTEXT_USAGE_THRESHOLDS . MEDIUM
174+ ? pc . yellow ( `${ contextData . percentage } %` )
175+ : pc . red ( `${ contextData . percentage } %` ) ;
254176
255177 // Format token count with thousand separators
256- const tokenDisplay = contextData . totalInputTokens . toLocaleString ( ) ;
178+ const tokenDisplay = contextData . maxInputTokens . toLocaleString ( ) ;
257179 contextInfo = ` | 🧠 ${ tokenDisplay } (${ coloredPercentage } )` ;
258180 }
259181 }
0 commit comments