mux-sesh 1.3.7 → 1.4.1
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 +46 -38
- 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/ui/SearchInput.tsx +19 -1
- 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,7 @@
|
|
|
1
|
+
import type { TextareaRenderable } from '@opentui/core'
|
|
1
2
|
import { useKeyboard } from '@opentui/react'
|
|
2
|
-
import { useState, useEffect, useRef
|
|
3
|
-
import { AppMode, ViewMode, type Item, type Config, type
|
|
3
|
+
import { useState, useEffect, useRef } from 'react'
|
|
4
|
+
import { AppMode, ViewMode, type Item, type Config, type OpencodeStatsState } from './types'
|
|
4
5
|
import { loadConfig } from './config'
|
|
5
6
|
import { listTmuxSessions } from './tmux'
|
|
6
7
|
import { getProjectItems } from './tmux/projects'
|
|
@@ -49,13 +50,22 @@ export function App() {
|
|
|
49
50
|
const [config, setConfig] = useState<Config | null>(null)
|
|
50
51
|
const [prefixActive, setPrefixActive] = useState(false)
|
|
51
52
|
const prefixTimeoutRef = useRef<NodeJS.Timeout | null>(null)
|
|
52
|
-
const textareaRef = useRef<
|
|
53
|
-
const statsCache = useRef<Map<string, OpencodeSessionStats>>(new Map())
|
|
54
|
-
const [loadingStats, setLoadingStats] = useState(false)
|
|
53
|
+
const textareaRef = useRef<TextareaRenderable | null>(null)
|
|
55
54
|
const { columns } = useTerminalSize()
|
|
56
55
|
const [toastMessage, setToastMessage] = useState('')
|
|
57
56
|
const [toastVisible, setToastVisible] = useState(false)
|
|
58
57
|
|
|
58
|
+
function updateOpencodeState(sessionName: string, nextState: OpencodeStatsState) {
|
|
59
|
+
const applyState = (existingItems: Item[]) =>
|
|
60
|
+
existingItems.map(item =>
|
|
61
|
+
item.title === sessionName ? { ...item, opencodeState: nextState } : item
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
setSessionItems(applyState)
|
|
65
|
+
setAllItems(applyState)
|
|
66
|
+
setItems(applyState)
|
|
67
|
+
}
|
|
68
|
+
|
|
59
69
|
useEffect(() => {
|
|
60
70
|
async function init() {
|
|
61
71
|
const cfg = await loadConfig()
|
|
@@ -110,36 +120,27 @@ export function App() {
|
|
|
110
120
|
}
|
|
111
121
|
|
|
112
122
|
async function loadOpencodeStatsForSession(sessionName: string) {
|
|
113
|
-
|
|
114
|
-
return statsCache.current.get(sessionName)
|
|
115
|
-
}
|
|
123
|
+
updateOpencodeState(sessionName, { status: 'loading' })
|
|
116
124
|
|
|
117
|
-
setLoadingStats(true)
|
|
118
125
|
try {
|
|
119
126
|
const stats = await getOpencodeSessionStats(sessionName)
|
|
127
|
+
|
|
120
128
|
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
|
-
)
|
|
129
|
+
updateOpencodeState(sessionName, { status: 'ready', stats })
|
|
130
|
+
return stats
|
|
139
131
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
132
|
+
|
|
133
|
+
updateOpencodeState(sessionName, {
|
|
134
|
+
status: 'missing',
|
|
135
|
+
message: `No OpenCode stats found for '${sessionName}'`,
|
|
136
|
+
})
|
|
137
|
+
return null
|
|
138
|
+
} catch (error) {
|
|
139
|
+
const errorMessage = error instanceof Error ? error.message : `Failed to load stats for '${sessionName}'`
|
|
140
|
+
updateOpencodeState(sessionName, { status: 'error', message: errorMessage })
|
|
141
|
+
setMessage(errorMessage)
|
|
142
|
+
setTimeout(() => setMessage(''), 4000)
|
|
143
|
+
return null
|
|
143
144
|
}
|
|
144
145
|
}
|
|
145
146
|
|
|
@@ -154,6 +155,8 @@ export function App() {
|
|
|
154
155
|
(appMode === AppMode.Normal || appMode === AppMode.OpencodeManage)
|
|
155
156
|
? items.filter(item => item.isSession && item.title.startsWith('opencode-'))
|
|
156
157
|
: []
|
|
158
|
+
const selectedOpencodeSessionName =
|
|
159
|
+
appMode === AppMode.OpencodeManage ? opencodeSessions[opencodeCursor]?.title : undefined
|
|
157
160
|
|
|
158
161
|
async function handleKillSessionWrapper(sessionName: string) {
|
|
159
162
|
await actionKillSession(sessionName, {
|
|
@@ -295,15 +298,20 @@ export function App() {
|
|
|
295
298
|
}, [searchQuery, appMode, allItems])
|
|
296
299
|
|
|
297
300
|
useEffect(() => {
|
|
298
|
-
if (
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
301
|
+
if (!selectedOpencodeSessionName) {
|
|
302
|
+
return
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
void loadOpencodeStatsForSession(selectedOpencodeSessionName)
|
|
306
|
+
|
|
307
|
+
const interval = setInterval(() => {
|
|
308
|
+
void loadOpencodeStatsForSession(selectedOpencodeSessionName)
|
|
309
|
+
}, 2000)
|
|
310
|
+
|
|
311
|
+
return () => {
|
|
312
|
+
clearInterval(interval)
|
|
305
313
|
}
|
|
306
|
-
}, [
|
|
314
|
+
}, [selectedOpencodeSessionName])
|
|
307
315
|
|
|
308
316
|
const title =
|
|
309
317
|
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
|
}
|