mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-03-24 21:10:43 -07:00
feat(workspaces): implement foreground, background, and iterm2 UI targets
This commit is contained in:
@@ -27,11 +27,11 @@ export async function runOrchestrator(args: string[], env: NodeJS.ProcessEnv = p
|
||||
const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
|
||||
const config = settings.workspace;
|
||||
if (!config) {
|
||||
console.error('❌ Deep Review configuration not found.');
|
||||
console.error('❌ Workspace configuration not found.');
|
||||
return 1;
|
||||
}
|
||||
|
||||
const { projectId, zone } = config;
|
||||
const { projectId, zone, remoteWorkDir } = config;
|
||||
const targetVM = `gcli-workspace-${env.USER || 'mattkorwel'}`;
|
||||
const provider = ProviderFactory.getProvider({ projectId, zone, instanceName: targetVM });
|
||||
|
||||
@@ -40,27 +40,24 @@ export async function runOrchestrator(args: string[], env: NodeJS.ProcessEnv = p
|
||||
|
||||
// Use Absolute Container Paths
|
||||
const containerHome = '/home/node';
|
||||
const remoteWorkDir = `${containerHome}/dev/main`;
|
||||
const remotePolicyPath = `${containerHome}/.gemini/policies/workspace-policy.toml`;
|
||||
const persistentScripts = `${containerHome}/.workspace/scripts`;
|
||||
const persistentScripts = `${containerHome}/.workspaces/scripts`;
|
||||
const sessionName = `workspace-${prNumber}-${action}`;
|
||||
const remoteWorktreeDir = `${containerHome}/dev/worktrees/${sessionName}`;
|
||||
|
||||
// 3. Remote Context Setup
|
||||
console.log(`🚀 Preparing remote environment for ${action} on #${prNumber}...`);
|
||||
|
||||
// Check if worktree exists
|
||||
const check = await provider.getExecOutput(`ls -d ${remoteWorktreeDir}/.git`, { wrapContainer: 'maintainer-worker' });
|
||||
|
||||
if (check.status !== 0) {
|
||||
console.log(' - Provisioning isolated git worktree...');
|
||||
// Only re-own the worktrees directory, NOT the entire home dir or scripts
|
||||
await provider.exec(`sudo docker exec -u root maintainer-worker mkdir -p ${containerHome}/dev/worktrees && sudo docker exec -u root maintainer-worker chown -R node:node ${containerHome}/dev/worktrees`);
|
||||
|
||||
const setupCmd = `
|
||||
git config --global --add safe.directory ${remoteWorkDir} && \
|
||||
git config --global --add safe.directory ${containerHome}/dev/main && \
|
||||
mkdir -p ${containerHome}/dev/worktrees && \
|
||||
cd ${remoteWorkDir} && \
|
||||
cd ${containerHome}/dev/main && \
|
||||
git fetch upstream pull/${prNumber}/head && \
|
||||
git worktree add -f ${remoteWorktreeDir} FETCH_HEAD
|
||||
`;
|
||||
@@ -71,27 +68,69 @@ export async function runOrchestrator(args: string[], env: NodeJS.ProcessEnv = p
|
||||
|
||||
// 4. Execution Logic
|
||||
const remoteWorker = `tsx ${persistentScripts}/entrypoint.ts ${prNumber} . ${remotePolicyPath} ${action}`;
|
||||
const remoteTmuxCmd = `tmux attach-session -t ${sessionName} 2>/dev/null || tmux new-session -s ${sessionName} -n 'workspace' 'cd ${remoteWorktreeDir} && ${remoteWorker}; exec $SHELL'`;
|
||||
const containerWrap = `sudo docker exec -it maintainer-worker sh -c ${q(remoteTmuxCmd)}`;
|
||||
|
||||
const finalSSH = provider.getRunCommand(containerWrap, { interactive: true });
|
||||
|
||||
// tmux command inside container
|
||||
const remoteTmuxCmd = `tmux attach-session -t ${sessionName} 2>/dev/null || tmux new-session -s ${sessionName} -n 'workspace' 'cd ${remoteWorktreeDir} && ${remoteWorker}; exec $SHELL'`;
|
||||
|
||||
const terminalTarget = config.terminalTarget || 'tab';
|
||||
const isWithinGemini = !!env.GEMINI_CLI || !!env.GEMINI_SESSION_ID || !!env.GCLI_SESSION_ID;
|
||||
const forceMainTerminal = true; // For debugging
|
||||
|
||||
if (!forceMainTerminal && isWithinGemini && env.TERM_PROGRAM === 'iTerm.app') {
|
||||
const tempCmdPath = path.join(process.env.TMPDIR || '/tmp', `workspace-ssh-${prNumber}.sh`);
|
||||
fs.writeFileSync(tempCmdPath, `#!/bin/bash\n${finalSSH}\nrm "$0"`, { mode: 0o755 });
|
||||
const appleScript = `on run argv\ntell application "iTerm"\ntell current window\nset newTab to (create tab with default profile)\ntell current session of newTab\nwrite text (item 1 of argv) & return\nend tell\nend tell\nactivate\nend tell\nend run`;
|
||||
spawnSync('osascript', ['-', tempCmdPath], { input: appleScript });
|
||||
console.log(`✅ iTerm2 tab opened.`);
|
||||
return 0;
|
||||
// Handle different UI targets
|
||||
switch (terminalTarget) {
|
||||
case 'background':
|
||||
console.log(`📡 Job #${prNumber} starting in background (session: ${sessionName}).`);
|
||||
// Remove -it for background launch
|
||||
const bgWrap = `sudo docker exec maintainer-worker sh -c ${q(remoteTmuxCmd)}`;
|
||||
await provider.exec(bgWrap);
|
||||
console.log(`✅ Job is running. Attach anytime with: npm run workspace:attach ${prNumber} ${action}`);
|
||||
return 0;
|
||||
|
||||
case 'tab':
|
||||
case 'window':
|
||||
if (isWithinGemini && env.TERM_PROGRAM === 'iTerm.app') {
|
||||
const containerWrap = `sudo docker exec -it maintainer-worker sh -c ${q(remoteTmuxCmd)}`;
|
||||
const finalSSH = provider.getRunCommand(containerWrap, { interactive: true });
|
||||
const tempCmdPath = path.join(process.env.TMPDIR || '/tmp', `workspace-ssh-${prNumber}.sh`);
|
||||
fs.writeFileSync(tempCmdPath, `#!/bin/bash\n${finalSSH}\nrm "$0"`, { mode: 0o755 });
|
||||
|
||||
const appleScript = terminalTarget === 'window' ? `
|
||||
on run argv
|
||||
tell application "iTerm"
|
||||
set newWindow to (create window with default profile)
|
||||
tell current session of newWindow
|
||||
write text (item 1 of argv) & return
|
||||
end tell
|
||||
activate
|
||||
end tell
|
||||
end run
|
||||
` : `
|
||||
on run argv
|
||||
tell application "iTerm"
|
||||
tell current window
|
||||
set newTab to (create tab with default profile)
|
||||
tell current session of newTab
|
||||
write text (item 1 of argv) & return
|
||||
end tell
|
||||
end tell
|
||||
activate
|
||||
end tell
|
||||
end run
|
||||
`;
|
||||
spawnSync('osascript', ['-', tempCmdPath], { input: appleScript });
|
||||
console.log(`✅ iTerm2 ${terminalTarget} opened for job #${prNumber}.`);
|
||||
return 0;
|
||||
}
|
||||
// Fallthrough to foreground if not in iTerm
|
||||
console.log(' ⚠️ iTerm2 not detected or not in Gemini. Falling back to foreground...');
|
||||
|
||||
case 'foreground':
|
||||
default:
|
||||
console.log(`📡 Connecting to session ${sessionName}...`);
|
||||
const fgWrap = `sudo docker exec -it maintainer-worker sh -c ${q(remoteTmuxCmd)}`;
|
||||
const fgSSH = provider.getRunCommand(fgWrap, { interactive: true });
|
||||
spawnSync(fgSSH, { stdio: 'inherit', shell: true });
|
||||
return 0;
|
||||
}
|
||||
|
||||
console.log(`📡 Connecting to session ${sessionName}...`);
|
||||
spawnSync(finalSSH, { stdio: 'inherit', shell: true });
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
|
||||
@@ -64,8 +64,8 @@ and full builds) to a dedicated, high-performance GCP worker.
|
||||
const zone = await prompt('Compute Zone', env.WORKSPACE_ZONE || 'us-west1-a',
|
||||
'The physical location of your worker. us-west1-a is the team default.');
|
||||
|
||||
const terminalTarget = await prompt('Terminal UI Target (tab or window)', env.WORKSPACE_TERM_TARGET || 'tab',
|
||||
'When a job starts, should it open in a new iTerm2 tab or a completely new window?');
|
||||
const terminalTarget = await prompt('Terminal UI Target (foreground, background, tab, window)', env.WORKSPACE_TERM_TARGET || 'tab',
|
||||
'When you start a job in gemini-cli, should it run as a foreground shell, background shell (no attach), new iterm2 tab, or new iterm2 window?');
|
||||
|
||||
// 2. Repository Discovery (Dynamic)
|
||||
console.log('\n🔍 Detecting repository origins...');
|
||||
|
||||
Reference in New Issue
Block a user