|
| 1 | +import { createHash, randomUUID } from 'node:crypto'; |
| 2 | +import * as fs from 'node:fs'; |
| 3 | +import * as os from 'node:os'; |
| 4 | +import * as path from 'node:path'; |
| 5 | + |
| 6 | +interface TmuxPaneRegistration { |
| 7 | + version: 1; |
| 8 | + sessionId: string; |
| 9 | + paneId: string; |
| 10 | + ownerPid: number; |
| 11 | + updatedAt: number; |
| 12 | +} |
| 13 | + |
| 14 | +export const TMUX_PANE_REGISTRATION_TTL_MS = 30_000; |
| 15 | + |
| 16 | +function dataDir(): string { |
| 17 | + return ( |
| 18 | + process.env.XDG_DATA_HOME ?? path.join(os.homedir(), '.local', 'share') |
| 19 | + ); |
| 20 | +} |
| 21 | + |
| 22 | +function sessionScope(sessionId: string): string { |
| 23 | + return createHash('sha256').update(sessionId).digest('hex').slice(0, 24); |
| 24 | +} |
| 25 | + |
| 26 | +export function getTmuxPaneRegistrationPath(sessionId: string): string { |
| 27 | + return path.join( |
| 28 | + dataDir(), |
| 29 | + 'opencode', |
| 30 | + 'storage', |
| 31 | + 'oh-my-opencode-slim', |
| 32 | + 'tmux-panes', |
| 33 | + `${sessionScope(sessionId)}.json`, |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +function isPaneId(value: unknown): value is string { |
| 38 | + return typeof value === 'string' && /^%\d+$/.test(value); |
| 39 | +} |
| 40 | + |
| 41 | +export function recordTmuxPane( |
| 42 | + sessionId: string, |
| 43 | + paneId: string, |
| 44 | + ownerPid = process.pid, |
| 45 | +): boolean { |
| 46 | + if (!sessionId || !isPaneId(paneId)) return false; |
| 47 | + |
| 48 | + const registration: TmuxPaneRegistration = { |
| 49 | + version: 1, |
| 50 | + sessionId, |
| 51 | + paneId, |
| 52 | + ownerPid, |
| 53 | + updatedAt: Date.now(), |
| 54 | + }; |
| 55 | + |
| 56 | + try { |
| 57 | + const filePath = getTmuxPaneRegistrationPath(sessionId); |
| 58 | + fs.mkdirSync(path.dirname(filePath), { recursive: true }); |
| 59 | + const tmpPath = `${filePath}.${process.pid}.${randomUUID()}.tmp`; |
| 60 | + try { |
| 61 | + fs.writeFileSync(tmpPath, `${JSON.stringify(registration)}\n`); |
| 62 | + fs.renameSync(tmpPath, filePath); |
| 63 | + return true; |
| 64 | + } finally { |
| 65 | + try { |
| 66 | + if (fs.existsSync(tmpPath)) fs.unlinkSync(tmpPath); |
| 67 | + } catch { |
| 68 | + // Best-effort state cleanup. |
| 69 | + } |
| 70 | + } |
| 71 | + } catch { |
| 72 | + return false; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +export function readTmuxPane( |
| 77 | + sessionId: string, |
| 78 | + now = Date.now(), |
| 79 | +): string | undefined { |
| 80 | + try { |
| 81 | + const parsed = JSON.parse( |
| 82 | + fs.readFileSync(getTmuxPaneRegistrationPath(sessionId), 'utf8'), |
| 83 | + ) as Partial<TmuxPaneRegistration>; |
| 84 | + if ( |
| 85 | + parsed.version !== 1 || |
| 86 | + parsed.sessionId !== sessionId || |
| 87 | + !isPaneId(parsed.paneId) || |
| 88 | + typeof parsed.updatedAt !== 'number' || |
| 89 | + now - parsed.updatedAt > TMUX_PANE_REGISTRATION_TTL_MS |
| 90 | + ) { |
| 91 | + return undefined; |
| 92 | + } |
| 93 | + return parsed.paneId; |
| 94 | + } catch { |
| 95 | + return undefined; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +export function removeTmuxPane( |
| 100 | + sessionId: string, |
| 101 | + paneId: string, |
| 102 | + ownerPid = process.pid, |
| 103 | +): void { |
| 104 | + try { |
| 105 | + const filePath = getTmuxPaneRegistrationPath(sessionId); |
| 106 | + const parsed = JSON.parse( |
| 107 | + fs.readFileSync(filePath, 'utf8'), |
| 108 | + ) as Partial<TmuxPaneRegistration>; |
| 109 | + if (parsed.paneId === paneId && parsed.ownerPid === ownerPid) { |
| 110 | + fs.unlinkSync(filePath); |
| 111 | + } |
| 112 | + } catch { |
| 113 | + // Registration may already be gone or replaced by another TUI. |
| 114 | + } |
| 115 | +} |
0 commit comments