mux-sesh 1.3.7 → 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.
- package/README.md +2 -2
- package/package.json +1 -1
- package/src/app.tsx +44 -37
- package/src/handlers/actions.ts +6 -5
- package/src/handlers/keyboard.ts +5 -2
- package/src/index.tsx +9 -0
- package/src/opencode/constants.ts +2 -0
- package/src/opencode/parser.ts +23 -47
- package/src/opencode/session.ts +120 -236
- package/src/opencode/sqlite.ts +432 -0
- package/src/opencode/stats.ts +10 -19
- package/src/opencode/tokens.ts +68 -95
- package/src/types/index.ts +7 -1
- package/src/ui/OpencodeStatsPanel.tsx +101 -81
- package/src/util/shutdown.ts +75 -0
package/README.md
CHANGED
|
@@ -229,7 +229,7 @@ MIT License - see [LICENSE](./LICENSE) for details.
|
|
|
229
229
|
## Acknowledgments
|
|
230
230
|
|
|
231
231
|
- Inspired by [ThePrimeagen's tmux-sessionizer](https://github.com/ThePrimeagen/.dotfiles/blob/master/bin/.local/scripts/tmux-sessionizer)
|
|
232
|
-
- Built with [OpenTUI](https://github.com/
|
|
232
|
+
- Built with [OpenTUI](https://github.com/anomalyco/opentui)
|
|
233
233
|
- UI design inspired by [nvim telescope plugin](https://github.com/nvim-telescope/telescope.nvim)
|
|
234
234
|
|
|
235
235
|
## Related Projects
|
|
@@ -239,5 +239,5 @@ MIT License - see [LICENSE](./LICENSE) for details.
|
|
|
239
239
|
---
|
|
240
240
|
|
|
241
241
|
<p align="center">
|
|
242
|
-
Made with using <a href="https://github.com/
|
|
242
|
+
Made with using <a href="https://github.com/anomalyco/opentui">OpenTUI</a> and <a href="https://bun.sh">Bun</a>
|
|
243
243
|
</p>
|
package/package.json
CHANGED
package/src/app.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useKeyboard } from '@opentui/react'
|
|
2
|
-
import { useState, useEffect, useRef
|
|
3
|
-
import { AppMode, ViewMode, type Item, type Config, type
|
|
2
|
+
import { useState, useEffect, useRef } from 'react'
|
|
3
|
+
import { AppMode, ViewMode, type Item, type Config, type OpencodeStatsState } from './types'
|
|
4
4
|
import { loadConfig } from './config'
|
|
5
5
|
import { listTmuxSessions } from './tmux'
|
|
6
6
|
import { getProjectItems } from './tmux/projects'
|
|
@@ -50,12 +50,21 @@ export function App() {
|
|
|
50
50
|
const [prefixActive, setPrefixActive] = useState(false)
|
|
51
51
|
const prefixTimeoutRef = useRef<NodeJS.Timeout | null>(null)
|
|
52
52
|
const textareaRef = useRef<any>(null)
|
|
53
|
-
const statsCache = useRef<Map<string, OpencodeSessionStats>>(new Map())
|
|
54
|
-
const [loadingStats, setLoadingStats] = useState(false)
|
|
55
53
|
const { columns } = useTerminalSize()
|
|
56
54
|
const [toastMessage, setToastMessage] = useState('')
|
|
57
55
|
const [toastVisible, setToastVisible] = useState(false)
|
|
58
56
|
|
|
57
|
+
function updateOpencodeState(sessionName: string, nextState: OpencodeStatsState) {
|
|
58
|
+
const applyState = (existingItems: Item[]) =>
|
|
59
|
+
existingItems.map(item =>
|
|
60
|
+
item.title === sessionName ? { ...item, opencodeState: nextState } : item
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
setSessionItems(applyState)
|
|
64
|
+
setAllItems(applyState)
|
|
65
|
+
setItems(applyState)
|
|
66
|
+
}
|
|
67
|
+
|
|
59
68
|
useEffect(() => {
|
|
60
69
|
async function init() {
|
|
61
70
|
const cfg = await loadConfig()
|
|
@@ -110,36 +119,27 @@ export function App() {
|
|
|
110
119
|
}
|
|
111
120
|
|
|
112
121
|
async function loadOpencodeStatsForSession(sessionName: string) {
|
|
113
|
-
|
|
114
|
-
return statsCache.current.get(sessionName)
|
|
115
|
-
}
|
|
122
|
+
updateOpencodeState(sessionName, { status: 'loading' })
|
|
116
123
|
|
|
117
|
-
setLoadingStats(true)
|
|
118
124
|
try {
|
|
119
125
|
const stats = await getOpencodeSessionStats(sessionName)
|
|
126
|
+
|
|
120
127
|
if (stats) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
setSessionItems(prevItems =>
|
|
124
|
-
prevItems.map(item =>
|
|
125
|
-
item.title === sessionName ? { ...item, opencodeStats: stats } : item
|
|
126
|
-
)
|
|
127
|
-
)
|
|
128
|
-
|
|
129
|
-
setAllItems(prevItems =>
|
|
130
|
-
prevItems.map(item =>
|
|
131
|
-
item.title === sessionName ? { ...item, opencodeStats: stats } : item
|
|
132
|
-
)
|
|
133
|
-
)
|
|
134
|
-
setItems(prevItems =>
|
|
135
|
-
prevItems.map(item =>
|
|
136
|
-
item.title === sessionName ? { ...item, opencodeStats: stats } : item
|
|
137
|
-
)
|
|
138
|
-
)
|
|
128
|
+
updateOpencodeState(sessionName, { status: 'ready', stats })
|
|
129
|
+
return stats
|
|
139
130
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
131
|
+
|
|
132
|
+
updateOpencodeState(sessionName, {
|
|
133
|
+
status: 'missing',
|
|
134
|
+
message: `No OpenCode stats found for '${sessionName}'`,
|
|
135
|
+
})
|
|
136
|
+
return null
|
|
137
|
+
} catch (error) {
|
|
138
|
+
const errorMessage = error instanceof Error ? error.message : `Failed to load stats for '${sessionName}'`
|
|
139
|
+
updateOpencodeState(sessionName, { status: 'error', message: errorMessage })
|
|
140
|
+
setMessage(errorMessage)
|
|
141
|
+
setTimeout(() => setMessage(''), 4000)
|
|
142
|
+
return null
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
|
|
@@ -154,6 +154,8 @@ export function App() {
|
|
|
154
154
|
(appMode === AppMode.Normal || appMode === AppMode.OpencodeManage)
|
|
155
155
|
? items.filter(item => item.isSession && item.title.startsWith('opencode-'))
|
|
156
156
|
: []
|
|
157
|
+
const selectedOpencodeSessionName =
|
|
158
|
+
appMode === AppMode.OpencodeManage ? opencodeSessions[opencodeCursor]?.title : undefined
|
|
157
159
|
|
|
158
160
|
async function handleKillSessionWrapper(sessionName: string) {
|
|
159
161
|
await actionKillSession(sessionName, {
|
|
@@ -295,15 +297,20 @@ export function App() {
|
|
|
295
297
|
}, [searchQuery, appMode, allItems])
|
|
296
298
|
|
|
297
299
|
useEffect(() => {
|
|
298
|
-
if (
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
300
|
+
if (!selectedOpencodeSessionName) {
|
|
301
|
+
return
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
void loadOpencodeStatsForSession(selectedOpencodeSessionName)
|
|
305
|
+
|
|
306
|
+
const interval = setInterval(() => {
|
|
307
|
+
void loadOpencodeStatsForSession(selectedOpencodeSessionName)
|
|
308
|
+
}, 2000)
|
|
309
|
+
|
|
310
|
+
return () => {
|
|
311
|
+
clearInterval(interval)
|
|
305
312
|
}
|
|
306
|
-
}, [
|
|
313
|
+
}, [selectedOpencodeSessionName])
|
|
307
314
|
|
|
308
315
|
const title =
|
|
309
316
|
appMode === AppMode.Search
|
package/src/handlers/actions.ts
CHANGED
|
@@ -8,14 +8,15 @@ import {
|
|
|
8
8
|
createNamedTmuxSession,
|
|
9
9
|
} from '../tmux/index'
|
|
10
10
|
import { isGitHubURL, cloneGitHubRepo } from '../util/github'
|
|
11
|
+
import { requestShutdown } from '../util/shutdown'
|
|
11
12
|
|
|
12
13
|
export async function handleSelect(item: Item) {
|
|
13
14
|
if (item.isSession) {
|
|
14
15
|
await switchTmuxSession(item.title)
|
|
15
|
-
|
|
16
|
+
await requestShutdown(0)
|
|
16
17
|
} else {
|
|
17
18
|
await createTmuxSession(item.title, item.path)
|
|
18
|
-
|
|
19
|
+
await requestShutdown(0)
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
22
|
|
|
@@ -71,16 +72,16 @@ export async function handleNewSessionSubmit(
|
|
|
71
72
|
const clonedPath = await cloneGitHubRepo(searchTerm, config)
|
|
72
73
|
const repoName = basename(clonedPath)
|
|
73
74
|
await createTmuxSession(repoName, clonedPath)
|
|
74
|
-
|
|
75
|
+
await requestShutdown(0)
|
|
75
76
|
} catch (error) {
|
|
76
77
|
throw new Error(`Error cloning repository: ${error}`)
|
|
77
78
|
}
|
|
78
79
|
} else if (items.length > 0 && cursor < items.length) {
|
|
79
80
|
const selectedItem = items[cursor]
|
|
80
81
|
await createTmuxSession(selectedItem.title, selectedItem.path)
|
|
81
|
-
|
|
82
|
+
await requestShutdown(0)
|
|
82
83
|
} else {
|
|
83
84
|
await createNamedTmuxSession(searchTerm)
|
|
84
|
-
|
|
85
|
+
await requestShutdown(0)
|
|
85
86
|
}
|
|
86
87
|
}
|
package/src/handlers/keyboard.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AppMode, ViewMode, KeybindMode } from '../types'
|
|
2
|
+
import { requestShutdown } from '../util/shutdown'
|
|
2
3
|
|
|
3
4
|
export interface KeyboardHandlerContext {
|
|
4
5
|
appMode: AppMode
|
|
@@ -46,7 +47,8 @@ export function handleNormalMode(
|
|
|
46
47
|
(isStandard && ((key.ctrl && key.name === 'q') || key.name === 'escape')) ||
|
|
47
48
|
(!isStandard && (key.name === 'q' || key.name === 'escape'))
|
|
48
49
|
) {
|
|
49
|
-
|
|
50
|
+
void requestShutdown(0)
|
|
51
|
+
return
|
|
50
52
|
}
|
|
51
53
|
|
|
52
54
|
// Handle search mode (i for vim, Ctrl+I for standard)
|
|
@@ -287,7 +289,8 @@ export function handleSearchMode(
|
|
|
287
289
|
|
|
288
290
|
// Ctrl+Q to quit (immediate, no prefix needed)
|
|
289
291
|
if (key.ctrl && key.name === 'q') {
|
|
290
|
-
|
|
292
|
+
void requestShutdown(0)
|
|
293
|
+
return
|
|
291
294
|
}
|
|
292
295
|
}
|
|
293
296
|
|
package/src/index.tsx
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
import { createCliRenderer } from '@opentui/core'
|
|
2
2
|
import { createRoot } from '@opentui/react'
|
|
3
3
|
import { App } from './app'
|
|
4
|
+
import { initializeShutdown, requestShutdown } from './util/shutdown'
|
|
4
5
|
|
|
5
6
|
const renderer = await createCliRenderer({
|
|
6
7
|
exitOnCtrlC: false,
|
|
7
8
|
})
|
|
8
9
|
|
|
10
|
+
initializeShutdown(renderer)
|
|
11
|
+
|
|
12
|
+
for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP'] as const) {
|
|
13
|
+
process.on(signal, () => {
|
|
14
|
+
void requestShutdown(signal === 'SIGINT' ? 130 : 0)
|
|
15
|
+
})
|
|
16
|
+
}
|
|
17
|
+
|
|
9
18
|
createRoot(renderer).render(<App />)
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { join } from 'path'
|
|
2
2
|
|
|
3
|
+
export const OPENCODE_DATA_DIR = join(process.env.HOME || '', '.local/share/opencode')
|
|
4
|
+
export const OPENCODE_DATABASE_PATH = join(OPENCODE_DATA_DIR, 'opencode.db')
|
|
3
5
|
export const OPENCODE_STORAGE = join(process.env.HOME || '', '.local/share/opencode/storage')
|
|
4
6
|
export const SESSION_STORAGE = join(OPENCODE_STORAGE, 'session')
|
|
5
7
|
export const MESSAGE_STORAGE = join(OPENCODE_STORAGE, 'message')
|
package/src/opencode/parser.ts
CHANGED
|
@@ -1,54 +1,30 @@
|
|
|
1
|
-
import { spawn } from 'bun'
|
|
2
1
|
import type { OpencodeStats } from './types'
|
|
2
|
+
import { getGlobalStats } from './sqlite'
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const lines = output.split('\n')
|
|
4
|
+
function formatTokenCount(value: number): string {
|
|
5
|
+
if (value >= 1_000_000_000) return `${(value / 1_000_000_000).toFixed(1)}B`
|
|
6
|
+
if (value >= 1_000_000) return `${(value / 1_000_000).toFixed(1)}M`
|
|
7
|
+
if (value >= 1_000) return `${(value / 1_000).toFixed(1)}K`
|
|
8
|
+
return value.toLocaleString()
|
|
9
|
+
}
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const match = line.match(/Sessions\s+(\d+)/)
|
|
16
|
-
if (match) stats.totalSessions = parseInt(match[1], 10)
|
|
17
|
-
}
|
|
18
|
-
if (line.includes('Messages')) {
|
|
19
|
-
const match = line.match(/Messages\s+([\d,]+)/)
|
|
20
|
-
if (match) stats.totalMessages = parseInt(match[1].replace(/,/g, ''), 10)
|
|
21
|
-
}
|
|
11
|
+
export async function getGlobalOpencodeStats(): Promise<OpencodeStats> {
|
|
12
|
+
const stats = await getGlobalStats()
|
|
22
13
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const match = line.match(/Cost\/Day\s+\$?([\d.]+)/)
|
|
29
|
-
if (match) stats.costPerDay = `$${match[1]}`
|
|
30
|
-
}
|
|
31
|
-
if (line.includes('Input') && !line.includes('Cost')) {
|
|
32
|
-
const match = line.match(/Input\s+([\d.]+[KMG]?)/)
|
|
33
|
-
if (match) stats.inputTokens = match[1]
|
|
34
|
-
}
|
|
35
|
-
if (line.includes('Output')) {
|
|
36
|
-
const match = line.match(/Output\s+([\d.]+[KMG]?)/)
|
|
37
|
-
if (match) stats.outputTokens = match[1]
|
|
38
|
-
}
|
|
39
|
-
if (line.includes('Cache Read')) {
|
|
40
|
-
const match = line.match(/Cache Read\s+([\d.]+[KMG]?)/)
|
|
41
|
-
if (match) stats.cacheRead = match[1]
|
|
42
|
-
}
|
|
43
|
-
if (line.includes('Cache Write')) {
|
|
44
|
-
const match = line.match(/Cache Write\s+([\d.]+[KMG]?)/)
|
|
45
|
-
if (match) stats.cacheWrite = match[1]
|
|
46
|
-
}
|
|
47
|
-
}
|
|
14
|
+
const totalCost = stats.totalCost
|
|
15
|
+
const oldestCreatedAt = stats.oldestCreatedAt ?? Date.now()
|
|
16
|
+
const newestUpdatedAt = stats.newestUpdatedAt ?? oldestCreatedAt
|
|
17
|
+
const daySpan = Math.max(1, Math.ceil((newestUpdatedAt - oldestCreatedAt) / 86_400_000))
|
|
18
|
+
const costPerDay = totalCost / daySpan
|
|
48
19
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
20
|
+
return {
|
|
21
|
+
totalSessions: stats.totalSessions,
|
|
22
|
+
totalMessages: stats.totalMessages,
|
|
23
|
+
totalCost: `$${totalCost.toFixed(2)}`,
|
|
24
|
+
costPerDay: `$${costPerDay.toFixed(2)}`,
|
|
25
|
+
inputTokens: formatTokenCount(stats.inputTokens),
|
|
26
|
+
outputTokens: formatTokenCount(stats.outputTokens),
|
|
27
|
+
cacheRead: formatTokenCount(stats.cacheRead),
|
|
28
|
+
cacheWrite: formatTokenCount(stats.cacheWrite),
|
|
53
29
|
}
|
|
54
30
|
}
|