use crate::design::Json;
use std::io::{Read, Write};
use std::net::TcpStream;
#[derive(Clone, Debug, PartialEq)]
pub struct Call {
pub method: &'static str,
pub op: &'static str,
pub tool: &'static str,
pub args: Json,
}
impl Call {
fn new(method: &'static str, op: &'static str, tool: &'static str) -> Call {
Call { method, op, tool, args: Json::obj() }
}
fn arg(mut self, key: &str, value: impl Into<Json>) -> Call {
self.args.set(key, value);
self
}
pub fn paths<I: IntoIterator<Item = S>, S: Into<String>>(self, paths: I) -> Call {
let list: Vec<Json> = paths.into_iter().map(|p| Json::Str(p.into())).collect();
self.arg("paths", Json::Arr(list))
}
pub fn remote(self, name: &str) -> Call {
self.arg("remote", name)
}
pub fn branch(self, name: &str) -> Call {
self.arg("branch", name)
}
pub fn account(self, name: &str) -> Call {
self.arg("account", name)
}
pub fn path(self, path: &str) -> Call {
self.arg("path", path)
}
pub fn commit_id(self, oid: &str) -> Call {
self.arg("commit", oid)
}
pub fn force(self) -> Call {
self.arg("force", true)
}
pub fn platform(&self, home: &str, folder: &str) -> (&'static str, String, Option<String>) {
let mut query = format!("folder={}", encode(folder));
if self.method == "GET" {
if let Json::Obj(entries) = &self.args {
for (k, v) in entries {
let value = v.as_str().map(str::to_string).unwrap_or_else(|| v.text());
query.push_str(&format!("&{}={}", encode(k), encode(&value)));
}
}
}
let path = format!("/accounts/{}/git/{}?{}", encode(home), self.op, query);
(self.method, path, if self.method == "POST" { Some(self.args.text()) } else { None })
}
}
fn encode(s: &str) -> String {
s.bytes()
.map(|b| match b {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' | b'/' => (b as char).to_string(),
_ => format!("%{:02X}", b),
})
.collect()
}
pub fn status() -> Call {
Call::new("GET", "status", "git_status")
}
pub fn log() -> Call {
Call::new("GET", "log", "git_log")
}
pub fn diff() -> Call {
Call::new("GET", "diff", "git_diff")
}
pub fn remotes() -> Call {
Call::new("GET", "remotes", "git_remotes")
}
pub fn commit(message: &str) -> Call {
Call::new("POST", "commit", "git_commit").arg("message", message)
}
pub fn checkpoint() -> Call {
Call::new("POST", "checkpoint", "git_checkpoint")
}
pub fn branch(name: &str) -> Call {
Call::new("POST", "branch", "git_create_branch").arg("name", name)
}
pub fn switch(name: &str) -> Call {
Call::new("POST", "switch", "git_switch").arg("name", name)
}
pub fn checkout(rev: &str) -> Call {
Call::new("POST", "checkout", "git_checkout").arg("rev", rev)
}
pub fn merge(from: &str) -> Call {
Call::new("POST", "merge", "git_merge").arg("from", from)
}
pub fn remote_add(name: &str, url: &str) -> Call {
Call::new("POST", "remote/add", "git_remote_add").arg("name", name).arg("url", url)
}
pub fn remote_remove(name: &str) -> Call {
Call::new("POST", "remote/remove", "git_remote_remove").arg("name", name)
}
pub fn fetch() -> Call {
Call::new("POST", "fetch", "git_fetch")
}
pub fn pull() -> Call {
Call::new("POST", "pull", "git_pull")
}
pub fn push() -> Call {
Call::new("POST", "push", "git_push")
}
pub fn fork(to: &str) -> Call {
Call::new("POST", "fork", "git_fork").arg("to", to)
}
#[derive(Clone, Debug)]
pub struct Host {
pub addr: String,
pub path: String,
pub token: String,
pub credential: Option<String>,
}
impl Host {
pub fn from_env() -> Result<Host, String> {
let dir = std::env::var("COMMANDAGI_USER_DIR")
.ok()
.or_else(|| std::env::var("HOME").ok().map(|h| format!("{h}/.commandagi")))
.ok_or("no home folder: set COMMANDAGI_USER_DIR")?;
let file = format!("{dir}/mcp.json");
let text = std::fs::read_to_string(&file).map_err(|e| format!("{file}: {e} (is the CommandAGI host running?)"))?;
let info = Json::parse(&text)?;
let url = info.get("url").and_then(Json::as_str).ok_or(format!("{file} names no url"))?;
let token = info.get("token").and_then(Json::as_str).ok_or(format!("{file} names no token"))?;
let mut host = Host::at(url, token)?;
host.credential = std::env::var("COMMANDAGI_CREDENTIAL").ok().filter(|c| !c.is_empty());
Ok(host)
}
pub fn at(url: &str, token: &str) -> Result<Host, String> {
let rest = url.strip_prefix("http://").ok_or(format!("{url}: the host's endpoint is http on the loopback"))?;
let (addr, path) = match rest.find('/') {
Some(i) => (&rest[..i], &rest[i..]),
None => (rest, "/"),
};
Ok(Host { addr: addr.to_string(), path: path.to_string(), token: token.to_string(), credential: None })
}
pub fn send(&self, call: &Call, folder: &str) -> Result<Json, String> {
let mut args = call.args.clone();
args.set("folder", folder);
let body = Json::obj()
.with("jsonrpc", "2.0")
.with("id", 1i64)
.with("method", "tools/call")
.with("params", Json::obj().with("name", call.tool).with("arguments", args))
.text();
let answer = Json::parse(&self.post(&body)?)?;
if let Some(e) = answer.get("error") {
return Err(e.get("message").and_then(Json::as_str).unwrap_or("the host refused the call").to_string());
}
let result = answer.get("result").ok_or("the host answered no result")?;
let text = match result.get("content") {
Some(Json::Arr(parts)) => parts.iter().filter_map(|p| p.get("text").and_then(Json::as_str)).collect::<Vec<_>>().join("\n"),
_ => String::new(),
};
if result.get("isError") == Some(&Json::Bool(true)) {
return Err(text);
}
Ok(Json::parse(&text).unwrap_or(Json::Str(text)))
}
fn post(&self, body: &str) -> Result<String, String> {
let mut stream = TcpStream::connect(&self.addr).map_err(|e| format!("{}: {e} (is the CommandAGI host running?)", self.addr))?;
let credential = self.credential.as_ref().map(|c| format!("x-commandagi-credential: {c}\r\n")).unwrap_or_default();
let request = format!(
"POST {} HTTP/1.1\r\nHost: {}\r\nAuthorization: Bearer {}\r\n{}Content-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
self.path,
self.addr,
self.token,
credential,
body.len(),
body
);
stream.write_all(request.as_bytes()).map_err(|e| e.to_string())?;
let mut raw = Vec::new();
stream.read_to_end(&mut raw).map_err(|e| e.to_string())?;
let split = raw.windows(4).position(|w| w == b"\r\n\r\n").ok_or("the host's answer has no head")?;
let head = String::from_utf8_lossy(&raw[..split]).to_string();
let mut payload = raw[split + 4..].to_vec();
if head.to_ascii_lowercase().contains("transfer-encoding: chunked") {
payload = unchunk(&payload)?;
}
let status = head.split_whitespace().nth(1).unwrap_or("");
let text = String::from_utf8(payload).map_err(|e| e.to_string())?;
if status != "200" {
return Err(format!("the host answered {status}: {text}"));
}
Ok(text)
}
}
fn unchunk(mut data: &[u8]) -> Result<Vec<u8>, String> {
let mut out = Vec::new();
loop {
let line = data.windows(2).position(|w| w == b"\r\n").ok_or("a chunk without its size")?;
let size = usize::from_str_radix(String::from_utf8_lossy(&data[..line]).split(';').next().unwrap_or("").trim(), 16).map_err(|e| e.to_string())?;
data = &data[line + 2..];
if size == 0 {
return Ok(out);
}
if data.len() < size + 2 {
return Err("a chunk cut short".into());
}
out.extend_from_slice(&data[..size]);
data = &data[size + 2..];
}
}