1- import { createMemo , Match , onCleanup , onMount , Show , Switch } from "solid-js"
1+ import { createEffect , createMemo , Match , on , onCleanup , onMount , Show , Switch } from "solid-js"
22import { useTheme } from "../../context/theme"
33import { useSync } from "../../context/sync"
44import { useDirectory } from "../../context/directory"
55import { useConnected } from "../../component/dialog-model"
66import { createStore } from "solid-js/store"
77import { useRoute } from "../../context/route"
8+ import type { AssistantMessage } from "@opencode-ai/sdk/v2"
9+ import { formatTokenCount , formatTps } from "../../util/tokens"
810
911export function Footer ( ) {
1012 const { theme } = useTheme ( )
@@ -20,12 +22,95 @@ export function Footer() {
2022 const directory = useDirectory ( )
2123 const connected = useConnected ( )
2224
25+ // Compute cumulative token stats for the current session
26+ const tokenStats = createMemo ( ( ) => {
27+ if ( route . data . type !== "session" ) return null
28+ const messages = sync . data . message [ route . data . sessionID ] ?? [ ]
29+ let inputTokens = 0
30+ let outputTokens = 0
31+ for ( const msg of messages ) {
32+ if ( msg . role !== "assistant" ) continue
33+ const am = msg as AssistantMessage
34+ if ( ! am . tokens ) continue
35+ inputTokens += am . tokens . input ?? 0
36+ outputTokens += am . tokens . output ?? 0
37+ }
38+ if ( inputTokens === 0 && outputTokens === 0 ) return null
39+ return { inputTokens, outputTokens }
40+ } )
41+
42+ // Track streaming tokens-per-second
43+ const [ tpsStore , setTpsStore ] = createStore ( {
44+ /** Current or last-computed TPS rate */
45+ tps : 0 ,
46+ /** Whether to show TPS in the footer (true during streaming + 3s after) */
47+ showTps : false ,
48+ } )
49+
50+ // Internal tracking for TPS calculation (not reactive)
51+ let prevOutputTokens = 0
52+ let prevTimestamp = 0
53+ let tpsTimeout : ReturnType < typeof setTimeout > | undefined
54+
55+ const sessionStatus = createMemo ( ( ) => {
56+ if ( route . data . type !== "session" ) return undefined
57+ return sync . data . session_status [ route . data . sessionID ]
58+ } )
59+
60+ const isStreaming = createMemo ( ( ) => sessionStatus ( ) ?. type === "busy" )
61+
62+ // Find the last assistant message's output token count (reactive)
63+ const lastAssistantOutputTokens = createMemo ( ( ) => {
64+ if ( route . data . type !== "session" ) return 0
65+ const messages = sync . data . message [ route . data . sessionID ] ?? [ ]
66+ for ( let i = messages . length - 1 ; i >= 0 ; i -- ) {
67+ const msg = messages [ i ]
68+ if ( msg . role === "assistant" ) {
69+ return ( msg as AssistantMessage ) . tokens ?. output ?? 0
70+ }
71+ }
72+ return 0
73+ } )
74+
75+ // Update TPS when output tokens change during streaming (effect, not memo)
76+ createEffect (
77+ on ( [ isStreaming , lastAssistantOutputTokens ] , ( [ streaming , outputTokens ] ) => {
78+ if ( ! streaming || outputTokens === 0 ) return
79+
80+ const now = Date . now ( )
81+ if ( prevOutputTokens > 0 && prevTimestamp > 0 && outputTokens > prevOutputTokens ) {
82+ const elapsed = ( now - prevTimestamp ) / 1000
83+ if ( elapsed > 0 ) {
84+ setTpsStore ( "tps" , ( outputTokens - prevOutputTokens ) / elapsed )
85+ setTpsStore ( "showTps" , true )
86+ }
87+ }
88+ prevOutputTokens = outputTokens
89+ prevTimestamp = now
90+ } ) ,
91+ )
92+
93+ // When streaming stops, keep TPS visible for 3 seconds then hide
94+ createEffect (
95+ on ( isStreaming , ( streaming ) => {
96+ if ( ! streaming && tpsStore . showTps ) {
97+ clearTimeout ( tpsTimeout )
98+ tpsTimeout = setTimeout ( ( ) => {
99+ setTpsStore ( "showTps" , false )
100+ prevOutputTokens = 0
101+ prevTimestamp = 0
102+ } , 3000 )
103+ }
104+ } ) ,
105+ )
106+
107+ onCleanup ( ( ) => clearTimeout ( tpsTimeout ) )
108+
23109 const [ store , setStore ] = createStore ( {
24110 welcome : false ,
25111 } )
26112
27113 onMount ( ( ) => {
28- // Track all timeouts to ensure proper cleanup
29114 const timeouts : ReturnType < typeof setTimeout > [ ] = [ ]
30115
31116 function tick ( ) {
@@ -60,6 +145,19 @@ export function Footer() {
60145 </ text >
61146 </ Match >
62147 < Match when = { connected ( ) } >
148+ < Show when = { tokenStats ( ) } >
149+ { ( stats ) => (
150+ < text fg = { theme . textMuted } >
151+ < Show when = { tpsStore . showTps && tpsStore . tps > 0 } >
152+ < span style = { { fg : isStreaming ( ) ? theme . text : theme . textMuted } } >
153+ { formatTps ( tpsStore . tps ) } tok/s
154+ </ span >
155+ { " · " }
156+ </ Show >
157+ { formatTokenCount ( stats ( ) . inputTokens ) } in · { formatTokenCount ( stats ( ) . outputTokens ) } out
158+ </ text >
159+ ) }
160+ </ Show >
63161 < Show when = { permissions ( ) . length > 0 } >
64162 < text fg = { theme . warning } >
65163 < span style = { { fg : theme . warning } } > △</ span > { permissions ( ) . length } Permission
0 commit comments