use crate::core::preview::automation::{control_path, ControlEndpoint};
use anyhow::{bail, Context, Result};
use clap::{Args, Subcommand};
use serde_json::{json, Value};
use std::path::PathBuf;
#[derive(Args, Debug)]
pub struct PreviewArgs {
#[command(subcommand)]
pub command: PreviewCommand,
}
#[derive(Subcommand, Debug)]
pub enum PreviewCommand {
List,
Downloads,
FetchDownload {
id: String,
#[arg(long, default_value = "default")]
profile: String,
},
CancelTransfer {
id: String,
#[arg(long, default_value = "default")]
profile: String,
},
CancelDownload {
id: String,
#[arg(long, default_value = "default")]
profile: String,
},
ForgetDownload {
id: String,
#[arg(long, default_value = "default")]
profile: String,
},
Open {
url: String,
#[arg(long, default_value = "default")]
profile: String,
},
Close { id: String },
Snapshot { id: String },
Reconnect { id: String },
Reopen { id: String },
Navigate { id: String, url: String },
Evaluate { id: String, expression: String },
Screenshot {
id: String,
output: PathBuf,
#[arg(long)]
full: bool,
},
Click { id: String, reference: String },
Fill {
id: String,
reference: String,
#[arg(allow_hyphen_values = true)]
value: String,
},
Scroll {
id: String,
#[arg(long, default_value_t = 0., allow_negative_numbers = true)]
x: f64,
#[arg(long, default_value_t = 600., allow_negative_numbers = true)]
y: f64,
},
}
pub(super) fn request(body: Value) -> Result<Value> {
let path = control_path();
let bytes = std::fs::read(path).context("Start AgentHub to use preview automation")?;
if bytes.len() > 8192 {
bail!("Invalid preview control configuration");
}
let endpoint: ControlEndpoint = serde_json::from_slice(&bytes)?;
crate::core::preview::automation::call(&endpoint, body)
.map_err(|error| anyhow::anyhow!(error.to_string()))
}
fn action(id: &str, value: Value) -> Result<Value> {
request(json!({"kind":"action","id":id,"action":value}))
}
pub fn run(args: &PreviewArgs, json_output: bool) -> Result<()> {
let value = match &args.command {
PreviewCommand::FetchDownload { id, profile } => {
request(json!({"kind":"fetch_download","id":id,"profile":profile}))?
}
PreviewCommand::CancelTransfer { id, profile } => {
request(json!({"kind":"cancel_transfer","id":id,"profile":profile}))?
}
PreviewCommand::Downloads => request(json!({"kind":"downloads"}))?,
PreviewCommand::CancelDownload { id, profile } => {
request(json!({"kind":"cancel_download","profile":profile,"id":id}))?
}
PreviewCommand::ForgetDownload { id, profile } => {
request(json!({"kind":"forget_download","profile":profile,"id":id}))?
}
PreviewCommand::List => request(json!({"kind":"list"}))?,
PreviewCommand::Open { url, profile } => {
request(json!({"kind":"open","url":url,"profile":profile}))?
}
PreviewCommand::Close { id } => request(json!({"kind":"close","id":id}))?,
PreviewCommand::Reopen { id } => request(json!({"kind":"reopen","id":id}))?,
PreviewCommand::Reconnect { id } => request(json!({"kind":"reconnect","id":id}))?,
PreviewCommand::Snapshot { id } => action(id, json!({"kind":"snapshot"}))?,
PreviewCommand::Navigate { id, url } => action(id, json!({"kind":"navigate","url":url}))?,
PreviewCommand::Evaluate { id, expression } => {
action(id, json!({"kind":"evaluate","expression":expression}))?
}
PreviewCommand::Click { id, reference } => {
action(id, json!({"kind":"click_ref","reference":reference}))?
}
PreviewCommand::Fill {
id,
reference,
value,
} => action(
id,
json!({"kind":"fill_ref","reference":reference,"value":value}),
)?,
PreviewCommand::Scroll { id, x, y } => {
if !x.is_finite() || !y.is_finite() {
bail!("Scroll coordinates must be finite");
}
action(
id,
json!({"kind":"evaluate","expression":format!("window.scrollBy({x},{y}); true")}),
)?
}
PreviewCommand::Screenshot { id, output, full } => {
use base64::Engine;
let result = action(id, json!({"kind":"screenshot","full":full}))?;
let data = result["data"].as_str().context("Screenshot unavailable")?;
let bytes = base64::engine::general_purpose::STANDARD.decode(data)?;
std::fs::write(output, bytes)?;
json!({"saved":output})
}
};
super::print_json(&value, json_output);
Ok(())
}