Cotton code
84 lines · 3.1 KB · RustRaw
//! `cotton-session-host statusline` — the status line command, and the one//! place an account's usage windows arrive.//!//! The agent renders whatever this prints at the bottom of its own TUI, so the//! command has two jobs and must not trade one for the other: forward the JSON//! to the daemon (this is where `rate_limits.five_hour` and//! `rate_limits.seven_day` come from), and still print a status line, because//! a hosted session whose status line went blank is a session that looks//! broken to the person looking at the terminal.//!//! **It prints what the payload actually carries.** A payload with no context//! figure gets a line with the model alone rather than `0%` — a zero there//! reads as "no context used", which is the opposite of "we were not told".use std::path::Path;use std::time::Duration;use tokio::io::AsyncWriteExt as _;use tokio::net::UnixStream;use crate::protocol::{line, Request};/// How long the forward is given before the status line is printed anyway.////// The agent draws its screen around this command; a daemon that is wedged/// must not freeze the person's terminal.pub const FORWARD_TIMEOUT: Duration = Duration::from_millis(500);/// The message this command sends.pub fn request(stdin: &str) -> Request {    let payload: serde_json::Value =        serde_json::from_str(stdin.trim()).unwrap_or(serde_json::Value::Null);    let session_id = payload        .get("session_id")        .and_then(serde_json::Value::as_str)        .map(str::to_string);    Request::Statusline {        session_id,        payload,    }}/// The one line the agent draws.////// `model · N% ctx`, and only the halves the payload has.pub fn status_line(payload: &serde_json::Value) -> String {    let model = payload        .get("model")        .and_then(|model| model.get("display_name"))        .and_then(serde_json::Value::as_str);    let context = payload        .get("context_window")        .and_then(|window| window.get("used_percentage"))        .and_then(serde_json::Value::as_f64)        .map(|pct| format!("{}% ctx", pct.round() as i64));    match (model, context) {        (Some(model), Some(context)) => format!("{model} · {context}"),        (Some(model), None) => model.to_string(),        (None, Some(context)) => context,        // Nothing to say about a payload with neither. An empty status line is        // the agent's own default, and inventing a word for it would put a        // sentence on screen that describes nothing.        (None, None) => String::new(),    }}/// Forward the payload and answer the line to print.pub async fn run(socket: &Path, stdin: &str) -> String {    let payload: serde_json::Value =        serde_json::from_str(stdin.trim()).unwrap_or(serde_json::Value::Null);    let _ = tokio::time::timeout(FORWARD_TIMEOUT, async {        let stream = UnixStream::connect(socket).await.ok()?;        let (_, mut write) = stream.into_split();        let text = line(&request(stdin)).ok()?;        write.write_all(text.as_bytes()).await.ok()?;        write.flush().await.ok()    })    .await;    status_line(&payload)}#[cfg(test)]#[path = "statusline_tests.rs"]mod statusline_tests;