diff --git a/crates/localllm-cli/src/main.rs b/crates/localllm-cli/src/main.rs index a8cec6e..ab8772e 100644 --- a/crates/localllm-cli/src/main.rs +++ b/crates/localllm-cli/src/main.rs @@ -445,8 +445,84 @@ fn render_execution_preview(cli: &Cli) -> String { } } -fn render_backend_response(cli: &Cli, _status_code: u16, _body: &str) -> String { - format!("LIVE {}", render_command_summary(cli)) +fn render_backend_response(cli: &Cli, status_code: u16, body: &str) -> String { + let preview = backend_response_preview(cli, status_code, body); + + match (cli.format, preview) { + (OutputFormat::Text, BackendResponsePreview::SummaryOnly { command }) => { + format!("LIVE {}", command) + } + ( + OutputFormat::Text, + BackendResponsePreview::Health { + status, + model_loaded, + model_id, + uptime_secs, + }, + ) => format!( + "STATUS status={} model_loaded={} model_id={} uptime_secs={}", + status, + model_loaded, + model_id.unwrap_or_else(|| "none".to_owned()), + uptime_secs + ), + (OutputFormat::Text, BackendResponsePreview::Models { count, ids }) => { + format!("MODELS count={} ids={}", count, ids.join(",")) + } + ( + OutputFormat::Text, + BackendResponsePreview::Throughput { + model, + total_tokens, + tokens_per_second, + }, + ) => format!( + "THROUGHPUT model={} total_tokens={} tokens_per_second={}", + model, total_tokens, tokens_per_second + ), + (OutputFormat::Json, BackendResponsePreview::SummaryOnly { command }) => { + format!(r#"{{"kind":"summary","command":"{}"}}"#, command) + } + ( + OutputFormat::Json, + BackendResponsePreview::Health { + status, + model_loaded, + model_id, + uptime_secs, + }, + ) => format!( + r#"{{"kind":"health","status":"{}","model_loaded":{},"model_id":{},"uptime_secs":{}}}"#, + status, + model_loaded, + render_json_optional_string(model_id.as_deref()), + uptime_secs + ), + (OutputFormat::Json, BackendResponsePreview::Models { count, ids }) => format!( + r#"{{"kind":"models","count":{},"ids":{}}}"#, + count, + serde_json::to_string(&ids).unwrap_or_else(|_| "[]".to_owned()) + ), + ( + OutputFormat::Json, + BackendResponsePreview::Throughput { + model, + total_tokens, + tokens_per_second, + }, + ) => format!( + r#"{{"kind":"throughput","model":"{}","total_tokens":{},"tokens_per_second":{}}}"#, + model, total_tokens, tokens_per_second + ), + } +} + +fn render_json_optional_string(value: Option<&str>) -> String { + match value { + Some(value) => format!(r#""{}""#, value), + None => "null".to_owned(), + } } fn render_json_body(body: Option<&str>) -> String {