import * as pty from 'node-pty'; import type { IPty } from 'node-pty'; export interface AttachPtyOptions { sessionName: string; windowName: string; projectRoot: string; cols: number; rows: number; tmuxConfPath: string; } function cleanEnv(): { [key: string]: string } { const out: { [key: string]: string } = {}; for (const [k, v] of Object.entries(process.env)) { if (typeof v === 'string') out[k] = v; } out['TERM'] = 'screen-256color'; return out; } // Spawns a tmux client attached to the given session+window. `-d` detaches any // other client so a browser refresh takes over the same window without // duplicate input. tmux server (and the window) persists across PTY exits. export function attachPty(opts: AttachPtyOptions): IPty { return pty.spawn( 'tmux', [ '-f', opts.tmuxConfPath, 'attach-session', '-d', '-t', `${opts.sessionName}:${opts.windowName}`, ], { name: 'xterm-256color', cols: opts.cols, rows: opts.rows, cwd: opts.projectRoot, env: cleanEnv(), }, ); }