use std::ffi::{OsStr, OsString};
use std::path::Path;
use std::process::{Command, Stdio};
use ag_protocol::{SchemaRequiredPolicy, protocol_output_schema};
use super::availability;
use super::backend::{AgentBackend, AgentBackendError, BuildCommandRequest};
use super::prompt::{self as shared_prompt, CliPromptAccessRootMode};
use crate::model::reasoning;
const ANTIGRAVITY_PRINT_TIMEOUT: &str = "1h";
pub(super) struct AntigravityBackend {
path_value: Option<OsString>,
validate_cached_cli: fn(Option<&OsStr>) -> Result<(), String>,
}
impl AntigravityBackend {
pub(super) fn new() -> Self {
Self {
path_value: std::env::var_os("PATH"),
validate_cached_cli: availability::ensure_cached_antigravity_cli_supported_on_path,
}
}
}
impl AgentBackend for AntigravityBackend {
fn setup(&self, _folder: &Path) -> Result<(), AgentBackendError> {
(self.validate_cached_cli)(self.path_value.as_deref()).map_err(AgentBackendError::Setup)
}
fn build_command<'request>(
&'request self,
request: BuildCommandRequest<'request>,
) -> Result<Command, AgentBackendError> {
super::execution_policy::validate(
ag_session::AgentKind::Antigravity,
request.execution_policy,
)?;
(self.validate_cached_cli)(self.path_value.as_deref())
.map_err(AgentBackendError::CommandBuild)?;
let BuildCommandRequest {
attachments,
folder,
main_checkout_root: _main_checkout_root,
model,
permission_mode,
prompt: _prompt,
request_kind,
replay_transcript: _replay_transcript,
reasoning_level,
..
} = request;
let mut command = Command::new("agy");
shared_prompt::append_cli_prompt_access_directories(
&mut command,
folder,
attachments,
CliPromptAccessRootMode::WorkspaceThenAttachments,
);
command.arg("--sandbox");
if permission_mode.is_read_only() {
command
.arg("--settings")
.arg(read_only_settings().to_string());
} else {
command.arg("--dangerously-skip-permissions");
}
command
.arg("--print-timeout")
.arg(ANTIGRAVITY_PRINT_TIMEOUT)
.arg("--model")
.arg(model);
if let Some(effort) = reasoning::antigravity(model, reasoning_level) {
command.arg("--effort").arg(effort);
}
command
.arg("--input-format")
.arg("stream-json")
.arg("--output-format")
.arg("stream-json")
.arg("--json-schema")
.arg(
protocol_output_schema(
request_kind.protocol_profile(),
SchemaRequiredPolicy::MinimumProtocolKeys,
)
.to_string(),
)
.current_dir(folder)
.stdout(Stdio::piped())
.stderr(Stdio::piped());
Ok(command)
}
}
fn read_only_settings() -> serde_json::Value {
serde_json::json!({
"permissions": {
"deny": ["command(*)", "write_file(*)"],
},
})
}
#[cfg(test)]
#[path = "antigravity_test.rs"]
mod tests;