mux-sesh 1.3.6 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  import { useEffect, useState } from 'react'
2
- import { getGlobalOpencodeStats, formatSessionAge } from '../opencode'
2
+ import { formatSessionAge } from '../opencode'
3
+ import { getGlobalOpencodeStats } from '../opencode/parser'
3
4
  import type { OpencodeStats } from '../opencode'
4
5
  import { colors, detailPanelStyle } from '../styles/theme'
5
6
  import type { Item } from '../types'
@@ -8,26 +9,54 @@ interface Props {
8
9
  selectedItem?: Item
9
10
  }
10
11
 
12
+ type GlobalStatsState =
13
+ | { status: 'loading' }
14
+ | { status: 'ready'; stats: OpencodeStats }
15
+ | { status: 'error'; message: string }
16
+
17
+ function formatNumber(value: number): string {
18
+ return value.toLocaleString()
19
+ }
20
+
11
21
  export default function OpencodeStatsPanel({ selectedItem }: Props) {
12
- const [globalStats, setGlobalStats] = useState<OpencodeStats | null>(null)
22
+ const [globalStatsState, setGlobalStatsState] = useState<GlobalStatsState>({ status: 'loading' })
13
23
 
14
24
  useEffect(() => {
15
- getGlobalOpencodeStats().then(setGlobalStats)
25
+ let disposed = false
26
+
27
+ async function loadGlobalStats() {
28
+ try {
29
+ const stats = await getGlobalOpencodeStats()
30
+ if (!disposed) {
31
+ setGlobalStatsState({ status: 'ready', stats })
32
+ }
33
+ } catch (error) {
34
+ const message = error instanceof Error ? error.message : 'Failed to load global OpenCode stats'
35
+ if (!disposed) {
36
+ setGlobalStatsState({ status: 'error', message })
37
+ }
38
+ }
39
+ }
40
+
41
+ void loadGlobalStats()
42
+
43
+ const interval = setInterval(() => {
44
+ void loadGlobalStats()
45
+ }, 5000)
46
+
47
+ return () => {
48
+ disposed = true
49
+ clearInterval(interval)
50
+ }
16
51
  }, [])
17
52
 
18
- const sessionStats = selectedItem?.opencodeStats
19
-
20
- const formatNumber = (num: number): string => {
21
- return num.toLocaleString()
22
- }
23
-
24
- const lastMessageTokens = sessionStats?.lastMessageTokens || 0
25
- const contextLimit = sessionStats?.contextLimit || 200000
53
+ const sessionState = selectedItem?.opencodeState
54
+ const sessionStats = sessionState?.status === 'ready' ? sessionState.stats : null
55
+ const lastMessageTokens = sessionStats?.lastMessageTokens ?? 0
56
+ const contextLimit = sessionStats?.contextLimit ?? 200000
26
57
  const contextPercentage =
27
- lastMessageTokens > 0 && contextLimit > 0
28
- ? Math.round((lastMessageTokens / contextLimit) * 100)
29
- : 0
30
- const sessionCost = sessionStats?.sessionTotalCost || 0
58
+ lastMessageTokens > 0 && contextLimit > 0 ? Math.round((lastMessageTokens / contextLimit) * 100) : 0
59
+ const sessionCost = sessionStats?.sessionTotalCost ?? 0
31
60
 
32
61
  const panelStyle = {
33
62
  ...detailPanelStyle,
@@ -38,20 +67,17 @@ export default function OpencodeStatsPanel({ selectedItem }: Props) {
38
67
 
39
68
  return (
40
69
  <box style={panelStyle}>
41
- {/* Header */}
42
70
  <box style={{ alignItems: 'center', justifyContent: 'center', marginBottom: 2 }}>
43
71
  <text style={{ fg: colors.primary, marginBottom: 1 }}> OpenCode Stats</text>
44
72
  </box>
45
73
 
46
74
  <box style={{ flexDirection: 'row', marginBottom: 2 }}>
47
- {/* Current Session Column */}
48
- {sessionStats ? (
49
- sessionStats.lastMessageTokens !== undefined ? (
50
- <box style={{ flexDirection: 'column', marginLeft: 2, width: '50%' }}>
51
- <text style={{ fg: colors.key, marginBottom: 1 }}>Current Session</text>
75
+ <box style={{ flexDirection: 'column', marginLeft: 2, width: '50%' }}>
76
+ <text style={{ fg: colors.key, marginBottom: 1 }}>Current Session</text>
77
+ {sessionState?.status === 'ready' ? (
78
+ <>
52
79
  <text>
53
- Messages:{' '}
54
- <span style={{ fg: colors.active }}>{sessionStats.messageCount || 0}</span>
80
+ Messages: <span style={{ fg: colors.active }}>{sessionState.stats.messageCount ?? 0}</span>
55
81
  </text>
56
82
  <text>
57
83
  Cost: <span style={{ fg: colors.action }}>${sessionCost.toFixed(2)}</span>
@@ -60,56 +86,59 @@ export default function OpencodeStatsPanel({ selectedItem }: Props) {
60
86
  Context: <span style={{ fg: colors.text }}>{formatNumber(lastMessageTokens)}</span>
61
87
  <span style={{ fg: colors.action }}>/{contextPercentage}%</span>
62
88
  </text>
63
- </box>
89
+ </>
90
+ ) : sessionState?.status === 'error' ? (
91
+ <text style={{ fg: colors.action }}>{sessionState.message}</text>
92
+ ) : sessionState?.status === 'missing' ? (
93
+ <text style={{ fg: colors.inactive }}>{sessionState.message}</text>
64
94
  ) : (
65
- <box style={{ flexDirection: 'column', marginLeft: 2, width: '50%' }}>
66
- <text style={{ fg: colors.inactive }}>Loading stats...</text>
67
- </box>
68
- )
69
- ) : (
70
- <box style={{ flexDirection: 'column', marginLeft: 2, width: '50%' }}>
71
95
  <text style={{ fg: colors.inactive }}>Loading stats...</text>
72
- </box>
73
- )}
96
+ )}
97
+ </box>
74
98
 
75
- {/* Global Overview Column */}
76
- {globalStats && (
77
- <box style={{ flexDirection: 'column', marginLeft: 2 }}>
78
- <text style={{ fg: colors.key, marginBottom: 1 }}>Global Overview</text>
79
- <text>
80
- Sessions: <span style={{ fg: colors.active }}>{globalStats.totalSessions || 0}</span>
81
- </text>
82
- <text>
83
- Messages: <span style={{ fg: colors.active }}>{globalStats.totalMessages || 0}</span>
84
- </text>
85
- <text>
86
- Total Cost:{' '}
87
- <span style={{ fg: colors.action }}>{globalStats.totalCost || '$0.00'}</span>
88
- </text>
89
- <text>
90
- Cost/Day: <span style={{ fg: colors.text }}>{globalStats.costPerDay || '$0.00'}</span>
91
- </text>
92
-
93
- <text style={{ fg: colors.key, marginTop: 1, marginBottom: 1 }}>Token Usage</text>
94
- <text>
95
- Input: <span style={{ fg: colors.text }}>{globalStats.inputTokens || '0'}</span>
96
- </text>
97
- <text>
98
- Output: <span style={{ fg: colors.text }}>{globalStats.outputTokens || '0'}</span>
99
- </text>
100
- <text>
101
- Cache Read:{' '}
102
- <span style={{ fg: colors.inactive }}>{globalStats.cacheRead || '0'}</span>
103
- </text>
104
- <text>
105
- Cache Write:{' '}
106
- <span style={{ fg: colors.inactive }}>{globalStats.cacheWrite || '0'}</span>
107
- </text>
108
- </box>
109
- )}
99
+ <box style={{ flexDirection: 'column', marginLeft: 2 }}>
100
+ <text style={{ fg: colors.key, marginBottom: 1 }}>Global Overview</text>
101
+ {globalStatsState.status === 'ready' ? (
102
+ <>
103
+ <text>
104
+ Sessions: <span style={{ fg: colors.active }}>{globalStatsState.stats.totalSessions ?? 0}</span>
105
+ </text>
106
+ <text>
107
+ Messages: <span style={{ fg: colors.active }}>{globalStatsState.stats.totalMessages ?? 0}</span>
108
+ </text>
109
+ <text>
110
+ Total Cost:{' '}
111
+ <span style={{ fg: colors.action }}>{globalStatsState.stats.totalCost ?? '$0.00'}</span>
112
+ </text>
113
+ <text>
114
+ Cost/Day:{' '}
115
+ <span style={{ fg: colors.text }}>{globalStatsState.stats.costPerDay ?? '$0.00'}</span>
116
+ </text>
117
+
118
+ <text style={{ fg: colors.key, marginTop: 1, marginBottom: 1 }}>Token Usage</text>
119
+ <text>
120
+ Input: <span style={{ fg: colors.text }}>{globalStatsState.stats.inputTokens ?? '0'}</span>
121
+ </text>
122
+ <text>
123
+ Output: <span style={{ fg: colors.text }}>{globalStatsState.stats.outputTokens ?? '0'}</span>
124
+ </text>
125
+ <text>
126
+ Cache Read:{' '}
127
+ <span style={{ fg: colors.inactive }}>{globalStatsState.stats.cacheRead ?? '0'}</span>
128
+ </text>
129
+ <text>
130
+ Cache Write:{' '}
131
+ <span style={{ fg: colors.inactive }}>{globalStatsState.stats.cacheWrite ?? '0'}</span>
132
+ </text>
133
+ </>
134
+ ) : globalStatsState.status === 'error' ? (
135
+ <text style={{ fg: colors.action }}>{globalStatsState.message}</text>
136
+ ) : (
137
+ <text style={{ fg: colors.inactive }}>Loading stats...</text>
138
+ )}
139
+ </box>
110
140
  </box>
111
141
 
112
- {/* Selected Session Stats */}
113
142
  {sessionStats && (
114
143
  <>
115
144
  <box style={{ alignItems: 'center', justifyContent: 'center', marginTop: 1 }}>
@@ -127,7 +156,6 @@ export default function OpencodeStatsPanel({ selectedItem }: Props) {
127
156
  <text style={{ marginBottom: 1, fg: colors.fileTree }}>{sessionStats.directory}</text>
128
157
  )}
129
158
 
130
- {/* Show session count for aggregated stats */}
131
159
  {sessionStats.sessionCount !== undefined && (
132
160
  <text>
133
161
  Sessions: <span style={{ fg: colors.active }}>{sessionStats.sessionCount}</span>
@@ -142,12 +170,10 @@ export default function OpencodeStatsPanel({ selectedItem }: Props) {
142
170
 
143
171
  {sessionStats.filesModified !== undefined && sessionStats.filesModified > 0 && (
144
172
  <text>
145
- Files Modified:{' '}
146
- <span style={{ fg: colors.action }}>{sessionStats.filesModified}</span>
173
+ Files Modified: <span style={{ fg: colors.action }}>{sessionStats.filesModified}</span>
147
174
  </text>
148
175
  )}
149
176
 
150
- {/* Show most recent session title for aggregated stats */}
151
177
  {sessionStats.mostRecentTitle && (
152
178
  <text style={{ marginTop: 1 }}>
153
179
  <span style={{ fg: colors.key }}>Most Recent:</span> {sessionStats.mostRecentTitle}
@@ -156,26 +182,20 @@ export default function OpencodeStatsPanel({ selectedItem }: Props) {
156
182
 
157
183
  {sessionStats.createdAt && (
158
184
  <text style={{ marginTop: 1 }}>
159
- Created:{' '}
160
- <span style={{ fg: colors.inactive }}>
161
- {formatSessionAge(sessionStats.createdAt)}
162
- </span>
185
+ Created: <span style={{ fg: colors.inactive }}>{formatSessionAge(sessionStats.createdAt)}</span>
163
186
  </text>
164
187
  )}
165
188
 
166
189
  {sessionStats.updatedAt && (
167
190
  <text>
168
- Updated:{' '}
169
- <span style={{ fg: colors.inactive }}>
170
- {formatSessionAge(sessionStats.updatedAt)}
171
- </span>
191
+ Updated: <span style={{ fg: colors.inactive }}>{formatSessionAge(sessionStats.updatedAt)}</span>
172
192
  </text>
173
193
  )}
174
194
  </box>
175
195
  </>
176
196
  )}
177
197
 
178
- {!sessionStats && (
198
+ {!selectedItem && (
179
199
  <box style={{ marginTop: 2, marginLeft: 2 }}>
180
200
  <text style={{ fg: colors.inactive }}>No session data found</text>
181
201
  </box>
@@ -0,0 +1,75 @@
1
+ import type { CliRenderer } from '@opentui/core'
2
+
3
+ const TERMINAL_RESET_SEQUENCE = [
4
+ '\u001b[?1000l',
5
+ '\u001b[?1002l',
6
+ '\u001b[?1003l',
7
+ '\u001b[?1005l',
8
+ '\u001b[?1006l',
9
+ '\u001b[?1015l',
10
+ '\u001b[?1049l',
11
+ '\u001b[?2004l',
12
+ '\u001b[?25h',
13
+ ].join('')
14
+
15
+ const state: {
16
+ renderer?: CliRenderer
17
+ exiting?: Promise<never>
18
+ listenersInstalled?: boolean
19
+ } = {}
20
+
21
+ function resetTerminalModes(): void {
22
+ if (process.stdout.isTTY) {
23
+ process.stdout.write(TERMINAL_RESET_SEQUENCE)
24
+ }
25
+
26
+ if (process.stdin.isTTY && typeof process.stdin.setRawMode === 'function') {
27
+ process.stdin.setRawMode(false)
28
+ }
29
+
30
+ process.stdin.pause()
31
+ }
32
+
33
+ function destroyRenderer(): void {
34
+ const renderer = state.renderer
35
+ if (!renderer || renderer.isDestroyed) {
36
+ return
37
+ }
38
+
39
+ try {
40
+ renderer.useMouse = false
41
+ renderer.disableKittyKeyboard()
42
+ renderer.setTerminalTitle('')
43
+ renderer.destroy()
44
+ } catch (error) {
45
+ console.error('Failed during renderer shutdown:', error)
46
+ }
47
+ }
48
+
49
+ export function initializeShutdown(renderer: CliRenderer): void {
50
+ state.renderer = renderer
51
+
52
+ if (state.listenersInstalled) {
53
+ return
54
+ }
55
+
56
+ process.once('exit', () => {
57
+ resetTerminalModes()
58
+ })
59
+
60
+ state.listenersInstalled = true
61
+ }
62
+
63
+ export function requestShutdown(code = 0): Promise<never> {
64
+ if (state.exiting) {
65
+ return state.exiting
66
+ }
67
+
68
+ state.exiting = (async () => {
69
+ destroyRenderer()
70
+ resetTerminalModes()
71
+ process.exit(code)
72
+ })()
73
+
74
+ return state.exiting
75
+ }