use std::path::Path;
use std::time::Duration;
use anyhow::Result;
use serde_json::json;
use trusty_code::cli_client::StdioRpcClient;
use trusty_code::cli_client::render::render_session_table;
use trusty_code::session::Session;
use super::tcode_exe;
pub async fn list(project: &Path) -> Result<()> {
let exe = tcode_exe::resolve()?;
let mut client = StdioRpcClient::spawn(&exe, project)?;
let result = client.call("session.list", json!({})).await?;
client.shutdown(Duration::from_secs(5)).await?;
let sessions: Vec<Session> = serde_json::from_value(
result
.get("sessions")
.cloned()
.unwrap_or(serde_json::Value::Null),
)
.map_err(|e| anyhow::anyhow!("tcode session list: malformed daemon response: {e}"))?;
println!("{}", render_session_table(&sessions));
Ok(())
}
pub async fn status(project: &Path, session_id: &str) -> Result<()> {
let exe = tcode_exe::resolve()?;
let mut client = StdioRpcClient::spawn(&exe, project)?;
let result = client
.call("session.status", json!({"session_id": session_id}))
.await;
client.shutdown(Duration::from_secs(5)).await?;
let result = result?;
let session: Session = serde_json::from_value(result)
.map_err(|e| anyhow::anyhow!("tcode session status: malformed daemon response: {e}"))?;
println!(
"{} {} agent={} task={}",
session.id,
session.status.as_str(),
session.agent.as_deref().unwrap_or("-"),
session.task
);
Ok(())
}