11import type { TuiPlugin , TuiPluginApi } from "@opencode-ai/plugin/tui"
22import type { BuiltinTuiPlugin } from "../builtins"
3- import { createMemo , Match , Show , Switch } from "solid-js"
3+ import { createMemo , createSignal , Match , onCleanup , Show , Switch } from "solid-js"
44import { abbreviateHome } from "../../runtime"
55import { useTuiPaths } from "../../context/runtime"
66import { useHomeSessionDestination } from "../../routes/home/session-destination"
7+ import { TokenRateMeter } from "../../util/token-rate"
8+ import { Locale } from "../../util/locale"
9+
10+ // Hide the live rate shortly after streaming stops (no part deltas).
11+ const ACTIVITY_TIMEOUT = 2500
712
813const id = "internal:home-footer"
914
@@ -24,6 +29,61 @@ function Directory(props: { api: TuiPluginApi }) {
2429 return < Show when = { dir ( ) } > { ( value ) => < text fg = { theme ( ) . textMuted } > { value ( ) } </ text > } </ Show >
2530}
2631
32+ function TokenRate ( props : { api : TuiPluginApi } ) {
33+ const theme = ( ) => props . api . theme . current
34+ const meter = new TokenRateMeter ( )
35+ let currentId : string | undefined
36+ let currentTokens = 0
37+ let lastActivityAt = 0
38+ const [ tick , setTick ] = createSignal ( 0 )
39+
40+ const unsubs = [
41+ props . api . event . on ( "message.updated" , ( evt ) => {
42+ const info = evt . properties . info
43+ if ( info . role !== "assistant" ) return
44+ const tokens = info . tokens . output + info . tokens . reasoning
45+ lastActivityAt = Date . now ( )
46+ if ( info . id !== currentId ) {
47+ currentId = info . id
48+ currentTokens = 0
49+ meter . reset ( )
50+ }
51+ if ( tokens >= currentTokens ) {
52+ currentTokens = tokens
53+ meter . add ( currentTokens , lastActivityAt )
54+ }
55+ } ) ,
56+ props . api . event . on ( "message.part.delta" , ( ) => {
57+ lastActivityAt = Date . now ( )
58+ } ) ,
59+ ]
60+ const timer = setInterval ( ( ) => setTick ( ( t ) => ( t + 1 ) % 1_000_000_000 ) , 1000 )
61+ onCleanup ( ( ) => {
62+ for ( const unsub of unsubs ) unsub ( )
63+ clearInterval ( timer )
64+ } )
65+
66+ const view = createMemo ( ( ) => {
67+ tick ( )
68+ const now = Date . now ( )
69+ if ( now - lastActivityAt > ACTIVITY_TIMEOUT ) return
70+ return {
71+ rate : meter . rate ( now ) ,
72+ total : currentTokens ,
73+ }
74+ } )
75+
76+ return (
77+ < Show when = { view ( ) } fallback = { < > </ > } >
78+ { ( v ) => (
79+ < text fg = { theme ( ) . textMuted } flexShrink = { 0 } >
80+ { Math . round ( v ( ) . rate ) } tok/s · { Locale . number ( v ( ) . total ) } tok
81+ </ text >
82+ ) }
83+ </ Show >
84+ )
85+ }
86+
2787function Mcp ( props : { api : TuiPluginApi } ) {
2888 const theme = ( ) => props . api . theme . current
2989 const list = createMemo ( ( ) => props . api . state . mcp ( ) )
@@ -74,6 +134,7 @@ function View(props: { api: TuiPluginApi }) {
74134 gap = { 2 }
75135 >
76136 < Directory api = { props . api } />
137+ < TokenRate api = { props . api } />
77138 < Mcp api = { props . api } />
78139 < box flexGrow = { 1 } />
79140 < Version api = { props . api } />
0 commit comments