use anyhow::{anyhow, Context, Result};
use serde_json::{json, Value};
use crate::api::Client;
use crate::config::Config;
pub async fn run(
cfg: &Config,
cmd: String,
text: Vec<String>,
raw_json: Option<String>,
) -> Result<()> {
let client = Client::new(cfg)?;
let args: Value = if let Some(j) = raw_json {
serde_json::from_str(&j).context("parsing --json")?
} else if text.is_empty() {
json!({})
} else {
json!({ "text": text.join(" ") })
};
let body = json!({ "cmd": cmd, "args": args });
let resp: Value = client.post_json("/@", &body).await?;
println!("{}", serde_json::to_string_pretty(&resp)?);
if let Some(ok) = resp.get("success").and_then(Value::as_bool) {
if !ok {
return Err(anyhow!(
"skill returned error: {}",
resp.get("error").and_then(Value::as_str).unwrap_or("(unspecified)")
));
}
}
Ok(())
}