mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-20 10:10:56 -07:00
184 lines
7.8 KiB
TypeScript
184 lines
7.8 KiB
TypeScript
import { spawnSync } from 'child_process';
|
|
import path from 'path';
|
|
import fs from 'fs';
|
|
import { fileURLToPath } from 'url';
|
|
import readline from 'readline';
|
|
import { ProviderFactory } from './providers/ProviderFactory.ts';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
const REPO_ROOT = path.resolve(__dirname, '../../../..');
|
|
|
|
async function prompt(question: string, defaultValue: string, explanation?: string): Promise<string> {
|
|
const autoAccept = process.argv.includes('--yes') || process.argv.includes('-y');
|
|
if (autoAccept && defaultValue) return defaultValue;
|
|
|
|
if (explanation) {
|
|
console.log(`\n📖 ${explanation}`);
|
|
}
|
|
|
|
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
return new Promise((resolve) => {
|
|
rl.question(`❓ ${question} (default: ${defaultValue}, <Enter> to use default): `, (answer) => {
|
|
rl.close();
|
|
resolve(answer.trim() || defaultValue);
|
|
});
|
|
});
|
|
}
|
|
|
|
async function confirm(question: string): Promise<boolean> {
|
|
const autoAccept = process.argv.includes('--yes') || process.argv.includes('-y');
|
|
if (autoAccept) return true;
|
|
|
|
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
return new Promise((resolve) => {
|
|
rl.question(`❓ ${question} (y/n): `, (answer) => {
|
|
rl.close();
|
|
resolve(answer.trim().toLowerCase() === 'y');
|
|
});
|
|
});
|
|
}
|
|
|
|
export async function runSetup(env: NodeJS.ProcessEnv = process.env) {
|
|
console.log(`
|
|
================================================================================
|
|
🚀 GEMINI CLI: HIGH-PERFORMANCE OFFLOAD SYSTEM
|
|
================================================================================
|
|
The offload system allows you to delegate heavy tasks (PR reviews, agentic fixes,
|
|
and full builds) to a dedicated, high-performance GCP worker.
|
|
================================================================================
|
|
`);
|
|
|
|
console.log('📝 PHASE 1: CONFIGURATION');
|
|
console.log('--------------------------------------------------------------------------------');
|
|
|
|
// 1. Project Identity
|
|
const defaultProject = env.GOOGLE_CLOUD_PROJECT || env.OFFLOAD_PROJECT || '';
|
|
const projectId = await prompt('GCP Project ID', defaultProject,
|
|
'The GCP Project where your offload worker will live. Your personal project is recommended.');
|
|
|
|
if (!projectId) {
|
|
console.error('❌ Project ID is required. Set GOOGLE_CLOUD_PROJECT or enter it manually.');
|
|
return 1;
|
|
}
|
|
|
|
const zone = await prompt('Compute Zone', env.OFFLOAD_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.OFFLOAD_TERM_TARGET || 'tab',
|
|
'When a job starts, should it open in a new iTerm2 tab or a completely new window?');
|
|
|
|
// 2. Repository Discovery
|
|
console.log('\n🔍 Detecting repository origins...');
|
|
const repoInfoRes = spawnSync('gh', ['repo', 'view', '--json', 'nameWithOwner,parent,isFork'], { stdio: 'pipe' });
|
|
let upstreamRepo = 'google-gemini/gemini-cli';
|
|
let userFork = upstreamRepo;
|
|
|
|
if (repoInfoRes.status === 0) {
|
|
try {
|
|
const repoInfo = JSON.parse(repoInfoRes.stdout.toString());
|
|
if (repoInfo.isFork && repoInfo.parent) {
|
|
upstreamRepo = repoInfo.parent.nameWithOwner;
|
|
userFork = repoInfo.nameWithOwner;
|
|
console.log(` ✅ Detected Fork: ${userFork} (Upstream: ${upstreamRepo})`);
|
|
} else {
|
|
console.log(` ✅ Working on Upstream: ${upstreamRepo}`);
|
|
}
|
|
} catch (e) {}
|
|
}
|
|
|
|
// 3. Security & Auth
|
|
let githubToken = env.OFFLOAD_GH_TOKEN || '';
|
|
if (!githubToken) {
|
|
const shouldGenToken = await confirm('\nGenerate a scoped GitHub token for the remote agent? (Highly Recommended)');
|
|
if (shouldGenToken) {
|
|
const baseUrl = 'https://github.com/settings/personal-access-tokens/new';
|
|
const name = `Offload-${env.USER}`;
|
|
const repoParams = userFork !== upstreamRepo
|
|
? `&repositories[]=${encodeURIComponent(upstreamRepo)}&repositories[]=${encodeURIComponent(userFork)}`
|
|
: `&repositories[]=${encodeURIComponent(upstreamRepo)}`;
|
|
|
|
const magicLink = `${baseUrl}?name=${encodeURIComponent(name)}&description=Gemini+CLI+Offload+Worker${repoParams}&contents=write&pull_requests=write&metadata=read`;
|
|
const terminalLink = `\u001b]8;;${magicLink}\u0007${magicLink}\u001b]8;;\u0007`;
|
|
|
|
console.log(`\n🔐 ACTION REQUIRED: Create a token with "Read/Write" access to contents & PRs:`);
|
|
console.log(`\n${terminalLink}\n`);
|
|
|
|
githubToken = await prompt('Paste Scoped Token', '');
|
|
}
|
|
} else {
|
|
console.log(' ✅ Using GitHub token from environment (OFFLOAD_GH_TOKEN).');
|
|
}
|
|
|
|
// 4. Save Confirmed State
|
|
const targetVM = `gcli-offload-${env.USER || 'mattkorwel'}`;
|
|
const settingsPath = path.join(REPO_ROOT, '.gemini/offload/settings.json');
|
|
if (!fs.existsSync(path.dirname(settingsPath))) fs.mkdirSync(path.dirname(settingsPath), { recursive: true });
|
|
|
|
const settings = {
|
|
deepReview: {
|
|
projectId, zone, terminalTarget,
|
|
userFork, upstreamRepo,
|
|
remoteHost: 'gcli-worker',
|
|
remoteWorkDir: '~/dev/main',
|
|
useContainer: true
|
|
}
|
|
};
|
|
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2));
|
|
console.log(`\n✅ Configuration saved to ${settingsPath}`);
|
|
|
|
// Transition to Execution
|
|
const provider = ProviderFactory.getProvider({ projectId, zone, instanceName: targetVM });
|
|
|
|
console.log('\n🏗️ PHASE 2: INFRASTRUCTURE');
|
|
console.log('--------------------------------------------------------------------------------');
|
|
console.log(` - Verifying access and finding worker ${targetVM}...`);
|
|
let status = await provider.getStatus();
|
|
|
|
if (status.status === 'UNKNOWN' || status.status === 'ERROR') {
|
|
const shouldProvision = await confirm(`❌ Worker ${targetVM} not found. Provision it now?`);
|
|
if (!shouldProvision) return 1;
|
|
|
|
const provisionRes = await provider.provision();
|
|
if (provisionRes !== 0) return 1;
|
|
status = await provider.getStatus();
|
|
}
|
|
|
|
if (status.status !== 'RUNNING') {
|
|
console.log(' - Waking up worker...');
|
|
await provider.ensureReady();
|
|
}
|
|
|
|
console.log('\n🚀 PHASE 3: REMOTE INITIALIZATION');
|
|
console.log('--------------------------------------------------------------------------------');
|
|
const setupRes = await provider.setup({ projectId, zone, dnsSuffix: '.internal.gcpnode.com' });
|
|
if (setupRes !== 0) return setupRes;
|
|
|
|
const persistentScripts = `~/.offload/scripts`;
|
|
|
|
console.log(`\n📦 Synchronizing Logic & Credentials...`);
|
|
await provider.exec(`mkdir -p ~/dev/main ~/.gemini/policies ~/.offload/scripts`);
|
|
await provider.sync('.gemini/skills/offload/scripts/', `${persistentScripts}/`, { delete: true });
|
|
await provider.sync('.gemini/skills/offload/policy.toml', `~/.gemini/policies/offload-policy.toml`);
|
|
|
|
if (fs.existsSync(path.join(env.HOME || '', '.gemini/google_accounts.json'))) {
|
|
await provider.sync(path.join(env.HOME || '', '.gemini/google_accounts.json'), `~/.gemini/google_accounts.json`);
|
|
}
|
|
|
|
if (githubToken) {
|
|
await provider.exec(`mkdir -p ~/.offload && echo ${githubToken} > ~/.offload/.gh_token && chmod 600 ~/.offload/.gh_token`);
|
|
}
|
|
|
|
// Final Repo Sync
|
|
console.log(`🚀 Finalizing Remote Repository (${userFork})...`);
|
|
const repoUrl = `https://github.com/${userFork}.git`;
|
|
const cloneCmd = `rm -rf ~/dev/main && git clone --filter=blob:none ${repoUrl} ~/dev/main && cd ~/dev/main && git remote add upstream https://github.com/${upstreamRepo}.git && git fetch upstream`;
|
|
await provider.exec(cloneCmd);
|
|
|
|
console.log('\n✨ ALL SYSTEMS GO! Your offload environment is ready.');
|
|
return 0;
|
|
}
|
|
|
|
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
runSetup().catch(console.error);
|
|
}
|