|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import type { SandboxConfig } from '@google/gemini-cli-core'; |
|
|
import { FatalSandboxError } from '@google/gemini-cli-core'; |
|
|
import commandExists from 'command-exists'; |
|
|
import * as os from 'node:os'; |
|
|
import { getPackageJson } from '../utils/package.js'; |
|
|
import type { Settings } from './settings.js'; |
|
|
|
|
|
|
|
|
|
|
|
interface SandboxCliArgs { |
|
|
sandbox?: boolean | string; |
|
|
sandboxImage?: string; |
|
|
} |
|
|
|
|
|
const VALID_SANDBOX_COMMANDS: ReadonlyArray<SandboxConfig['command']> = [ |
|
|
'docker', |
|
|
'podman', |
|
|
'sandbox-exec', |
|
|
]; |
|
|
|
|
|
function isSandboxCommand(value: string): value is SandboxConfig['command'] { |
|
|
return (VALID_SANDBOX_COMMANDS as readonly string[]).includes(value); |
|
|
} |
|
|
|
|
|
function getSandboxCommand( |
|
|
sandbox?: boolean | string, |
|
|
): SandboxConfig['command'] | '' { |
|
|
|
|
|
if (process.env['SANDBOX']) { |
|
|
return ''; |
|
|
} |
|
|
|
|
|
|
|
|
const environmentConfiguredSandbox = |
|
|
process.env['GEMINI_SANDBOX']?.toLowerCase().trim() ?? ''; |
|
|
sandbox = |
|
|
environmentConfiguredSandbox?.length > 0 |
|
|
? environmentConfiguredSandbox |
|
|
: sandbox; |
|
|
if (sandbox === '1' || sandbox === 'true') sandbox = true; |
|
|
else if (sandbox === '0' || sandbox === 'false' || !sandbox) sandbox = false; |
|
|
|
|
|
if (sandbox === false) { |
|
|
return ''; |
|
|
} |
|
|
|
|
|
if (typeof sandbox === 'string' && sandbox) { |
|
|
if (!isSandboxCommand(sandbox)) { |
|
|
throw new FatalSandboxError( |
|
|
`Invalid sandbox command '${sandbox}'. Must be one of ${VALID_SANDBOX_COMMANDS.join( |
|
|
', ', |
|
|
)}`, |
|
|
); |
|
|
} |
|
|
|
|
|
if (commandExists.sync(sandbox)) { |
|
|
return sandbox; |
|
|
} |
|
|
throw new FatalSandboxError( |
|
|
`Missing sandbox command '${sandbox}' (from GEMINI_SANDBOX)`, |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (os.platform() === 'darwin' && commandExists.sync('sandbox-exec')) { |
|
|
return 'sandbox-exec'; |
|
|
} else if (commandExists.sync('docker') && sandbox === true) { |
|
|
return 'docker'; |
|
|
} else if (commandExists.sync('podman') && sandbox === true) { |
|
|
return 'podman'; |
|
|
} |
|
|
|
|
|
|
|
|
if (sandbox === true) { |
|
|
throw new FatalSandboxError( |
|
|
'GEMINI_SANDBOX is true but failed to determine command for sandbox; ' + |
|
|
'install docker or podman or specify command in GEMINI_SANDBOX', |
|
|
); |
|
|
} |
|
|
|
|
|
return ''; |
|
|
} |
|
|
|
|
|
export async function loadSandboxConfig( |
|
|
settings: Settings, |
|
|
argv: SandboxCliArgs, |
|
|
): Promise<SandboxConfig | undefined> { |
|
|
const sandboxOption = argv.sandbox ?? settings.tools?.sandbox; |
|
|
const command = getSandboxCommand(sandboxOption); |
|
|
|
|
|
const packageJson = await getPackageJson(); |
|
|
const image = |
|
|
argv.sandboxImage ?? |
|
|
process.env['GEMINI_SANDBOX_IMAGE'] ?? |
|
|
packageJson?.config?.sandboxImageUri; |
|
|
|
|
|
return command && image ? { command, image } : undefined; |
|
|
} |
|
|
|