|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('node:fs'); |
| 4 | +const path = require('node:path'); |
| 5 | + |
| 6 | +const { TOOL_NAME } = require('../context'); |
| 7 | +const { run } = require('../core/runtime'); |
| 8 | +const { shellQuote } = require('./launch'); |
| 9 | + |
| 10 | +const DEFAULT_AGENT_TERMINAL = 'kitty'; |
| 11 | +const SUPPORTED_AGENT_TERMINALS = new Set(['kitty', 'none']); |
| 12 | + |
| 13 | +function normalizeAgentTerminal(value) { |
| 14 | + const terminal = String(value || DEFAULT_AGENT_TERMINAL).trim().toLowerCase(); |
| 15 | + return terminal || DEFAULT_AGENT_TERMINAL; |
| 16 | +} |
| 17 | + |
| 18 | +function sanitizeFileSegment(value) { |
| 19 | + return String(value || 'agents') |
| 20 | + .replace(/[^a-zA-Z0-9._-]+/g, '__') |
| 21 | + .replace(/^_+|_+$/g, '') |
| 22 | + .slice(0, 120) || 'agents'; |
| 23 | +} |
| 24 | + |
| 25 | +function terminalSessionDir(repoRoot) { |
| 26 | + return path.join(repoRoot, '.guardex', 'agents', 'terminals'); |
| 27 | +} |
| 28 | + |
| 29 | +function terminalSessionFilePath(repoRoot, sessions, terminal = DEFAULT_AGENT_TERMINAL) { |
| 30 | + const firstSession = sessions[0] || {}; |
| 31 | + const sessionId = sanitizeFileSegment(firstSession.id || firstSession.branch || 'agents'); |
| 32 | + return path.join(terminalSessionDir(repoRoot), `${sessionId}-${sessions.length}.${terminal}-session`); |
| 33 | +} |
| 34 | + |
| 35 | +function sessionTitle(session, index) { |
| 36 | + const branch = String(session.branch || session.id || `agent-${index + 1}`); |
| 37 | + const leaf = branch.split('/').filter(Boolean).pop() || branch; |
| 38 | + return `${index + 1}: ${session.agent || 'agent'} ${leaf}`; |
| 39 | +} |
| 40 | + |
| 41 | +function buildKittySession(sessions) { |
| 42 | + const lines = ['# Generated by gx agents start.']; |
| 43 | + sessions.forEach((session, index) => { |
| 44 | + const title = sessionTitle(session, index); |
| 45 | + lines.push( |
| 46 | + '', |
| 47 | + `new_tab ${shellQuote(title)}`, |
| 48 | + `cd ${shellQuote(session.worktreePath)}`, |
| 49 | + `launch --title ${shellQuote(title)} sh -lc ${shellQuote(session.launchCommand)}`, |
| 50 | + ); |
| 51 | + }); |
| 52 | + return `${lines.join('\n')}\n`; |
| 53 | +} |
| 54 | + |
| 55 | +function writeKittySessionFile(repoRoot, sessions) { |
| 56 | + const filePath = terminalSessionFilePath(repoRoot, sessions, 'kitty'); |
| 57 | + fs.mkdirSync(path.dirname(filePath), { recursive: true }); |
| 58 | + fs.writeFileSync(filePath, buildKittySession(sessions), { encoding: 'utf8', mode: 0o600 }); |
| 59 | + return filePath; |
| 60 | +} |
| 61 | + |
| 62 | +function recoveryLines(sessionFilePath, reason) { |
| 63 | + const detail = reason ? `: ${reason}` : '.'; |
| 64 | + return [ |
| 65 | + `[${TOOL_NAME}] Kitty terminal not launched${detail}`, |
| 66 | + `[${TOOL_NAME}] Kitty session file: ${sessionFilePath}`, |
| 67 | + `[${TOOL_NAME}] Recovery: kitty --detach --session ${shellQuote(sessionFilePath)}`, |
| 68 | + `[${TOOL_NAME}] Agent lanes are intact; run the recovery command when Kitty is available.`, |
| 69 | + '', |
| 70 | + ].join('\n'); |
| 71 | +} |
| 72 | + |
| 73 | +function resultReason(result, fallback) { |
| 74 | + if (result?.error?.message) return result.error.message; |
| 75 | + if (typeof result?.status === 'number') return `${fallback} exited ${result.status}`; |
| 76 | + return fallback; |
| 77 | +} |
| 78 | + |
| 79 | +function launchAgentTerminal(repoRoot, sessions, options = {}) { |
| 80 | + const terminal = normalizeAgentTerminal(options.terminal); |
| 81 | + if (terminal === 'none' || !Array.isArray(sessions) || sessions.length === 0) { |
| 82 | + return { status: 'skipped', stdout: '', stderr: '', sessionFilePath: '' }; |
| 83 | + } |
| 84 | + if (!SUPPORTED_AGENT_TERMINALS.has(terminal)) { |
| 85 | + return { |
| 86 | + status: 'unsupported', |
| 87 | + stdout: '', |
| 88 | + stderr: `[${TOOL_NAME}] Unsupported agent terminal '${terminal}'. Supported terminals: kitty, none.\n`, |
| 89 | + sessionFilePath: '', |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + const sessionFilePath = writeKittySessionFile(repoRoot, sessions); |
| 94 | + const runner = options.runner || run; |
| 95 | + if (typeof runner !== 'function') { |
| 96 | + return { |
| 97 | + status: 'missing', |
| 98 | + stdout: '', |
| 99 | + stderr: recoveryLines(sessionFilePath, 'terminal runner unavailable'), |
| 100 | + sessionFilePath, |
| 101 | + }; |
| 102 | + } |
| 103 | + const kittyBin = options.kittyBin || process.env.GUARDEX_KITTY_BIN || 'kitty'; |
| 104 | + const probe = runner(kittyBin, ['--version'], { cwd: repoRoot, stdio: 'pipe' }); |
| 105 | + if (probe?.error || probe?.status !== 0) { |
| 106 | + return { |
| 107 | + status: 'missing', |
| 108 | + stdout: '', |
| 109 | + stderr: recoveryLines(sessionFilePath, resultReason(probe, `${kittyBin} --version`)), |
| 110 | + sessionFilePath, |
| 111 | + }; |
| 112 | + } |
| 113 | + |
| 114 | + const launch = runner(kittyBin, ['--detach', '--session', sessionFilePath], { cwd: repoRoot, stdio: 'ignore' }); |
| 115 | + if (launch?.error || launch?.status !== 0) { |
| 116 | + return { |
| 117 | + status: 'failed', |
| 118 | + stdout: '', |
| 119 | + stderr: recoveryLines(sessionFilePath, resultReason(launch, `${kittyBin} --detach`)), |
| 120 | + sessionFilePath, |
| 121 | + }; |
| 122 | + } |
| 123 | + |
| 124 | + return { |
| 125 | + status: 'launched', |
| 126 | + stdout: `[${TOOL_NAME}] Kitty agent terminal: ${sessionFilePath}\n`, |
| 127 | + stderr: '', |
| 128 | + sessionFilePath, |
| 129 | + }; |
| 130 | +} |
| 131 | + |
| 132 | +module.exports = { |
| 133 | + DEFAULT_AGENT_TERMINAL, |
| 134 | + buildKittySession, |
| 135 | + launchAgentTerminal, |
| 136 | + normalizeAgentTerminal, |
| 137 | + recoveryLines, |
| 138 | + terminalSessionFilePath, |
| 139 | + writeKittySessionFile, |
| 140 | +}; |
0 commit comments