use std::io::{BufRead, IsTerminal};
use std::process::{Command, Stdio};
use std::sync::OnceLock;
use std::time::Duration;
use anyhow::Context;
use indicatif::{ProgressBar, ProgressStyle};
use serde::Deserialize;
const DEFAULT_HOST: &str = "127.0.0.1";
const DEFAULT_PORT: u16 = 17434;
static PARSED_HOST: OnceLock<(String, String, u16)> = OnceLock::new();
fn parsed_host() -> &'static (String, String, u16) {
PARSED_HOST.get_or_init(|| parse_host(std::env::var("LLMMAN_HOST").ok().as_deref()))
}
fn parse_host(value: Option<&str>) -> (String, String, u16) {
let raw = value.unwrap_or("");
let trimmed = raw.trim().trim_matches(|c| c == '"' || c == '\'');
let mut default_port = DEFAULT_PORT;
let (scheme, hostport) = match trimmed.split_once("://") {
Some((scheme, rest)) => {
let scheme = scheme.to_ascii_lowercase();
match scheme.as_str() {
"http" => default_port = 80,
"https" => default_port = 443,
_ => {}
}
(scheme, rest)
}
None => ("http".to_string(), trimmed),
};
let hostport = hostport.split_once('/').map_or(hostport, |(h, _)| h);
let (host, port) = split_host_port(hostport);
let port = port
.and_then(|p| p.parse::<u16>().ok())
.unwrap_or(default_port);
let host = host.unwrap_or_else(|| DEFAULT_HOST.to_string());
(scheme, host, port)
}
fn split_host_port(hostport: &str) -> (Option<String>, Option<&str>) {
if hostport.is_empty() {
return (None, None);
}
if let Some(rest) = hostport.strip_prefix('[') {
return match rest.split_once(']') {
Some((host, after)) => {
let port = after.strip_prefix(':').filter(|p| !p.is_empty());
(Some(host.to_string()), port)
}
None => (Some(hostport.to_string()), None), };
}
match hostport.rsplit_once(':') {
Some((host, port)) if !host.contains(':') && !port.is_empty() => {
(Some(host.to_string()), Some(port))
}
_ => (Some(hostport.to_string()), None),
}
}
fn format_host_port(host: &str, port: u16) -> String {
if host.parse::<std::net::Ipv6Addr>().is_ok() {
format!("[{host}]:{port}")
} else {
format!("{host}:{port}")
}
}
pub fn server() -> String {
format!("{}://{}", scheme(), connect_addr())
}
fn scheme() -> &'static str {
if parsed_host().0 == "https" {
"https"
} else {
"http"
}
}
pub fn tls_scheme() -> bool {
scheme() == "https"
}
pub fn connects_securely() -> bool {
connects_over_loopback() || tls_scheme()
}
pub fn client_builder() -> anyhow::Result<reqwest::blocking::ClientBuilder> {
let mut builder =
reqwest::blocking::Client::builder().default_headers(crate::auth::client_headers()?);
for cert in crate::auth::tls_ca()? {
builder = builder.add_root_certificate(cert);
}
Ok(builder)
}
pub fn client() -> anyhow::Result<reqwest::blocking::Client> {
client_builder()?.build().context("build http client")
}
pub fn async_client_builder() -> anyhow::Result<reqwest::ClientBuilder> {
Ok(crate::auth::trusted_client()?.default_headers(crate::auth::client_headers()?))
}
pub fn peer_url(spec: &str) -> String {
let (scheme, host, port) = parse_host(Some(spec));
format!("{scheme}://{}", format_host_port(&host, port))
}
pub fn bind_addr() -> String {
let (_, host, port) = parsed_host();
format_host_port(host, *port)
}
fn connect_addr() -> String {
let (_, host, port) = parsed_host();
format_host_port(connectable_host(host), *port)
}
fn connectable_host(host: &str) -> &str {
match host.parse::<std::net::IpAddr>() {
Ok(ip) if ip.is_unspecified() => {
if ip.is_ipv4() {
"127.0.0.1"
} else {
"::1"
}
}
_ => host,
}
}
pub fn server_alive() -> bool {
use std::net::ToSocketAddrs;
let Ok(mut addrs) = connect_addr().to_socket_addrs() else {
return false;
};
addrs.any(|addr| std::net::TcpStream::connect_timeout(&addr, Duration::from_secs(2)).is_ok())
}
#[derive(Deserialize, Default)]
struct DaemonIdentity {
#[serde(default)]
version: Option<String>,
#[serde(default)]
exe: Option<String>,
#[serde(default)]
pid: Option<u32>,
}
fn host_is_local() -> bool {
let (_, host, _) = parsed_host();
is_local_host(host)
}
pub fn reachable_only_locally() -> bool {
host_reachable_only_locally(&parsed_host().1)
}
fn host_reachable_only_locally(host: &str) -> bool {
if host.eq_ignore_ascii_case("localhost") {
return true;
}
host.parse::<std::net::IpAddr>()
.is_ok_and(|ip| ip.is_loopback())
}
pub fn connects_over_loopback() -> bool {
host_connects_over_loopback(&parsed_host().1)
}
fn host_connects_over_loopback(host: &str) -> bool {
is_local_host(connectable_host(host))
}
fn is_local_host(host: &str) -> bool {
if host.eq_ignore_ascii_case("localhost") {
return true;
}
host.parse::<std::net::IpAddr>()
.is_ok_and(|ip| ip.is_loopback() || ip.is_unspecified())
}
fn stale_daemon() -> Option<DaemonIdentity> {
let resp = client_builder()
.ok()?
.timeout(Some(Duration::from_secs(2)))
.build()
.ok()?
.get(format!("{}/api/version", server()))
.send()
.ok()?;
if !resp.status().is_success() {
return None;
}
let identity: DaemonIdentity = resp.json().ok()?;
match (&identity.version, &identity.exe) {
(Some(version), Some(exe))
if version == env!("LLMMAN_VERSION") && std::path::Path::new(exe).exists() =>
{
None
}
_ => Some(identity),
}
}
fn stop_stale_daemon(identity: &DaemonIdentity) -> anyhow::Result<()> {
eprintln!("stopping stale llmman serve daemon (superseded by this install); restarting");
kill_daemon(identity, false);
if wait_for_port_free() {
return Ok(());
}
kill_daemon(identity, true);
if wait_for_port_free() {
return Ok(());
}
anyhow::bail!(
"a stale llmman serve daemon is still holding {} after being asked to stop; \
stop it manually (e.g. pkill -f 'llmman serve') and retry",
bind_addr()
)
}
fn wait_for_port_free() -> bool {
for _ in 0..50 {
if !server_alive() {
return true;
}
std::thread::sleep(Duration::from_millis(100));
}
false
}
#[cfg(unix)]
fn kill_daemon(identity: &DaemonIdentity, force: bool) {
let signal = if force { libc::SIGKILL } else { libc::SIGTERM };
match identity.pid {
Some(pid) => {
if !kill_group(pid, signal) {
if let Some(pid) = i32::try_from(pid).ok().filter(|pid| *pid > 1) {
unsafe { libc::kill(pid, signal) };
}
}
}
None => {
let _ = Command::new("pkill")
.args([if force { "-KILL" } else { "-TERM" }, "-f", "llmman serve"])
.status();
}
}
}
#[cfg(windows)]
fn kill_daemon(identity: &DaemonIdentity, _force: bool) {
match identity.pid {
Some(pid) => {
let _ = Command::new("taskkill")
.args(["/PID", &pid.to_string(), "/T", "/F"])
.status();
}
None => {
let _ = Command::new("powershell")
.args([
"-NoProfile",
"-Command",
"Get-CimInstance Win32_Process -Filter \"Name='llmman.exe'\" | \
Where-Object { $_.CommandLine -match ' serve' } | \
ForEach-Object { Stop-Process -Id $_.ProcessId -Force }",
])
.status();
}
}
}
pub fn ensure_server(preload_model: &str) -> anyhow::Result<()> {
if server_alive() {
if !host_is_local() {
return Ok(());
}
match stale_daemon() {
None => return Ok(()),
Some(identity) => stop_stale_daemon(&identity)?,
}
} else if !host_is_local() {
anyhow::bail!(
"LLMMAN_HOST={} is unreachable, and llmman can't start a daemon on a remote host",
server()
);
}
let exe = std::env::current_exe().context("could not resolve own executable")?;
let log_path = crate::default_store()
.ok()
.and_then(|store| store.parent().map(|p| p.join("serve.log")));
if let Some(p) = log_path.as_ref().and_then(|p| p.parent()) {
let _ = std::fs::create_dir_all(p);
}
if let Some(p) = &log_path {
rotate_log(p);
}
let mut cmd = Command::new(&exe);
cmd.arg("serve");
if !preload_model.is_empty() {
cmd.arg(preload_model);
}
cmd.stdin(Stdio::null());
match log_path
.as_ref()
.and_then(|p| std::fs::File::create(p).ok())
.and_then(|f| f.try_clone().ok().map(|f2| (f, f2)))
{
Some((out, err)) => {
cmd.stdout(out);
cmd.stderr(err);
}
None => {
cmd.stdout(Stdio::null());
cmd.stderr(Stdio::null());
}
}
detach(&mut cmd);
let mut child = cmd.spawn().context("spawn llmman serve")?;
let mut idle_since = std::time::Instant::now();
let mut fetch_notice = FetchNotice::default();
loop {
std::thread::sleep(Duration::from_millis(500));
if server_alive() {
bail_if_exited(&mut child, log_path.as_deref())?;
return Ok(());
}
bail_if_exited(&mut child, log_path.as_deref())?;
match crate::llama_release::download_status() {
Some(status) => {
fetch_notice.update(&status);
idle_since = std::time::Instant::now();
}
None if idle_since.elapsed() >= STARTUP_BUDGET => break,
None => {}
}
}
drop(fetch_notice);
if server_alive() {
bail_if_exited(&mut child, log_path.as_deref())?;
return Ok(());
}
bail_if_exited(&mut child, log_path.as_deref())?;
if crate::llama_release::download_in_progress() {
anyhow::bail!(
"llmman serve did not start within {}s: startup is still fetching an inference \
backend (llama.cpp, or mlx-lm on macOS). The daemon was left running \
so it can finish; retry this command once it does{}",
STARTUP_BUDGET.as_secs(),
log_tail(log_path.as_deref())
);
}
stop_group(child.id());
let _ = child.wait();
anyhow::bail!(
"llmman serve did not start within {}s and was stopped; run 'llmman serve' in the \
foreground to watch what startup is doing{}",
STARTUP_BUDGET.as_secs(),
log_tail(log_path.as_deref())
)
}
const STARTUP_BUDGET: Duration = Duration::from_secs(60);
#[derive(Default)]
struct FetchNotice {
spinner: Option<ProgressBar>,
announced: bool,
}
impl FetchNotice {
fn update(&mut self, status: &str) {
let message = fetch_message(status);
if !std::io::stderr().is_terminal() {
if !self.announced {
eprintln!("[llmman] {message}");
self.announced = true;
}
return;
}
self.spinner
.get_or_insert_with(|| crate::fmt::braille_spinner("{spinner} {msg}"))
.set_message(message);
}
}
impl Drop for FetchNotice {
fn drop(&mut self) {
if let Some(pb) = self.spinner.take() {
pb.finish_and_clear();
}
}
}
fn fetch_message(status: &str) -> String {
const PREFIX: &str = "first run: setting up the llmman daemon's inference backends (one-time)";
match status.trim() {
"" => PREFIX.to_string(),
status => format!("{PREFIX}: {status}"),
}
}
#[cfg(unix)]
fn kill_group(pid: u32, signal: libc::c_int) -> bool {
let Ok(pid) = i32::try_from(pid) else {
return false;
};
if pid <= 1 {
return false;
}
unsafe { libc::kill(-pid, signal) == 0 }
}
#[cfg(unix)]
fn signal_group(pid: u32, force: bool) -> bool {
kill_group(pid, if force { libc::SIGKILL } else { libc::SIGTERM })
}
#[cfg(windows)]
fn signal_group(pid: u32, force: bool) -> bool {
let mut args = vec!["/PID".to_string(), pid.to_string(), "/T".to_string()];
if force {
args.push("/F".to_string());
}
let _ = Command::new("taskkill")
.args(&args)
.stdout(Stdio::null())
.stderr(Stdio::null())
.status();
true
}
fn stop_group(pid: u32) {
if signal_group(pid, false) {
std::thread::sleep(Duration::from_millis(500));
signal_group(pid, true);
}
}
fn bail_if_exited(
child: &mut std::process::Child,
log_path: Option<&std::path::Path>,
) -> anyhow::Result<()> {
let pid = child.id();
let Some(status) = child.try_wait().context("wait on llmman serve")? else {
return Ok(());
};
if server_alive() {
return Ok(());
}
stop_group(pid);
anyhow::bail!(
"llmman serve exited during startup ({status}){}",
log_tail(log_path)
)
}
fn log_tail(log_path: Option<&std::path::Path>) -> String {
let Some(path) = log_path else {
return String::new();
};
let Ok(contents) = std::fs::read_to_string(path) else {
return format!(" (see {})", path.display());
};
let lines: Vec<&str> = contents.lines().filter(|l| !l.trim().is_empty()).collect();
if lines.is_empty() {
return format!(" (see {})", path.display());
}
let tail = &lines[lines.len().saturating_sub(5)..];
format!("; last lines of {}:\n{}", path.display(), tail.join("\n"))
}
#[cfg(unix)]
fn detach(cmd: &mut Command) {
use std::os::unix::process::CommandExt;
cmd.process_group(0);
}
#[cfg(windows)]
fn detach(cmd: &mut Command) {
use std::os::windows::process::CommandExt;
const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
cmd.creation_flags(CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW);
}
const MAX_LOG_FILES: u32 = 5;
fn rotate_log(path: &std::path::Path) {
if !path.exists() {
return;
}
let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or("serve");
let ext = path.extension().and_then(|s| s.to_str()).unwrap_or("log");
let dir = path.parent().unwrap_or_else(|| std::path::Path::new("."));
for i in (1..=MAX_LOG_FILES).rev() {
let older = dir.join(format!("{stem}-{i}.{ext}"));
let newer = if i == 1 {
path.to_path_buf()
} else {
dir.join(format!("{stem}-{}.{ext}", i - 1))
};
if newer.exists() {
if older.exists() {
let _ = std::fs::remove_file(&older);
}
let _ = std::fs::rename(&newer, &older);
}
}
}
#[cfg(windows)]
mod win_handles {
use std::ffi::c_void;
#[allow(non_snake_case)]
extern "system" {
fn GetStdHandle(nStdHandle: i32) -> *mut c_void;
fn SetHandleInformation(hObject: *mut c_void, dwMask: u32, dwFlags: u32) -> i32;
}
const STD_INPUT_HANDLE: i32 = -10;
const STD_OUTPUT_HANDLE: i32 = -11;
const STD_ERROR_HANDLE: i32 = -12;
const HANDLE_FLAG_INHERIT: u32 = 0x0000_0001;
pub fn disable_std_handle_inheritance() {
static INIT: std::sync::Once = std::sync::Once::new();
INIT.call_once(|| {
for which in [STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE] {
unsafe {
let handle = GetStdHandle(which);
if !handle.is_null() && handle != usize::MAX as *mut c_void {
let _ = SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0);
}
}
}
});
}
}
pub fn disable_std_handle_inheritance() {
#[cfg(windows)]
win_handles::disable_std_handle_inheritance();
}
#[derive(Deserialize)]
struct ProgressLine {
status: Option<String>,
error: Option<String>,
#[serde(default)]
notice: Option<String>,
#[serde(default)]
digest: Option<String>,
#[serde(default)]
total: Option<u64>,
#[serde(default)]
completed: Option<u64>,
}
fn progress_bar_style() -> ProgressStyle {
ProgressStyle::with_template(
"{msg:<20} [{bar:32.cyan/blue}] {bytes:>10}/{total_bytes:<10} {bytes_per_sec:>12}",
)
.unwrap_or_else(|_| ProgressStyle::default_bar())
.progress_chars("=> ")
}
pub fn stream_progress(path: &str, reference: &str) -> anyhow::Result<()> {
stream_progress_with(path, reference).map(|_| ())
}
pub fn stream_progress_with(path: &str, reference: &str) -> anyhow::Result<Option<String>> {
let body = serde_json::json!({"model": reference});
let client = client_builder()?
.timeout(None) .build()
.context("build http client")?;
let resp = client
.post(format!("{}{path}", server()))
.json(&body)
.send()
.with_context(|| format!("request {path} for {reference}"))?;
if !resp.status().is_success() {
let status = resp.status();
let body = resp.text().unwrap_or_default();
anyhow::bail!("{path} {reference}: server returned {status}: {body}");
}
let mut saw_success = false;
let mut pushed_digest: Option<String> = None;
let mut last_status = String::new();
let mut bar: Option<ProgressBar> = None;
let mut printed_instant_complete = false;
for line in std::io::BufReader::new(resp).lines() {
let line = line.context("read response stream")?;
let line = line.trim();
if line.is_empty() {
continue;
}
let Ok(msg) = serde_json::from_str::<ProgressLine>(line) else {
continue; };
if let Some(d) = msg.digest.filter(|d| !d.is_empty()) {
pushed_digest = Some(d);
continue;
}
if let Some(notice) = msg.notice.filter(|n| !n.is_empty()) {
match &bar {
Some(b) => b.suspend(|| eprintln!("[llmman] {notice}")),
None => eprintln!("[llmman] {notice}"),
}
continue;
}
if let Some(err) = msg.error.filter(|e| !e.is_empty()) {
if let Some(b) = bar.take() {
b.abandon(); }
if err.contains(reference) {
anyhow::bail!("{err}");
}
anyhow::bail!("{reference}: {err}");
}
if let Some(total) = msg.total.filter(|&t| t > 0) {
let completed = msg.completed.unwrap_or(0).min(total);
if bar.is_none() && completed * 100 >= total * 99 {
if !printed_instant_complete {
println!(
"Already have {reference} ({})",
crate::fmt::human_size(total)
);
printed_instant_complete = true;
}
last_status = "pulling".to_string(); continue;
}
let pb = bar.get_or_insert_with(|| {
let pb = ProgressBar::new(total);
pb.set_style(progress_bar_style());
pb.set_message(reference.to_string());
pb
});
pb.set_length(total);
pb.set_position(completed);
continue;
}
if let Some(b) = bar.take() {
b.finish_and_clear();
}
if let Some(status) = msg.status {
if !status.is_empty() && status != last_status {
println!("{status}");
last_status = status;
}
saw_success = last_status == "success";
}
}
if let Some(b) = bar.take() {
b.finish_and_clear();
}
if !saw_success {
anyhow::bail!("{reference}: stream ended without a success status");
}
Ok(pushed_digest)
}
#[derive(Debug, Default, Deserialize)]
pub struct ShowResponse {
#[serde(default)]
pub capabilities: Vec<String>,
#[serde(default)]
pub template: Option<String>,
}
impl ShowResponse {
pub fn thinking_controls(&self) -> Option<crate::chat_template::ThinkingControls> {
self.template
.as_deref()
.map(crate::chat_template::thinking_controls)
}
pub fn multimodal(&self) -> bool {
self.capabilities
.iter()
.any(|c| c == crate::modelpack::CAPABILITY_VISION || c == "audio")
}
pub fn vision(&self) -> bool {
self.capabilities
.iter()
.any(|c| c == crate::modelpack::CAPABILITY_VISION)
}
pub fn image(&self) -> bool {
self.capabilities
.iter()
.any(|c| c == crate::modelpack::CAPABILITY_IMAGE)
}
}
pub fn show(reference: &str) -> anyhow::Result<Option<ShowResponse>> {
let resp = client()?
.post(format!("{}/api/show", server()))
.json(&serde_json::json!({"model": reference}))
.send()
.with_context(|| format!("request /api/show for {reference}"))?;
if resp.status() == reqwest::StatusCode::NOT_FOUND {
return Ok(None);
}
if !resp.status().is_success() {
let body = resp.text().unwrap_or_default();
anyhow::bail!("{}", api_error(&body).unwrap_or(body));
}
let body = resp
.json::<ShowResponse>()
.with_context(|| format!("decode /api/show for {reference}"))?;
Ok(Some(body))
}
pub fn ensure_model_pulled(reference: &str) -> anyhow::Result<ShowResponse> {
if let Some(info) = show(reference)? {
return Ok(info);
}
stream_progress("/api/pull", reference)?;
show(reference)?.ok_or_else(|| anyhow::anyhow!("{reference}: not found after pull"))
}
pub fn push(reference: &str) -> anyhow::Result<Option<String>> {
stream_progress_with("/api/push", reference)
}
pub fn unload(reference: &str) -> anyhow::Result<bool> {
let resp = client()?
.post(format!("{}/api/generate", server()))
.json(&serde_json::json!({"model": reference, "keep_alive": 0}))
.send()
.with_context(|| format!("request /api/generate (unload) for {reference}"))?;
let status = resp.status();
if status.is_success() {
return Ok(true);
}
let body = resp.text().unwrap_or_default();
if status == reqwest::StatusCode::NOT_FOUND && is_model_not_found_body(&body, reference) {
return Ok(false);
}
anyhow::bail!("stop {reference}: server returned {status}: {body}");
}
pub(crate) fn is_model_not_found_body(body: &str, reference: &str) -> bool {
let expected = format!("model '{}' not found", crate::hybrid::local_half(reference));
api_error(body).is_some_and(|e| e == expected)
}
pub fn get_json<T: serde::de::DeserializeOwned>(path: &str) -> anyhow::Result<T> {
let resp = client()?
.get(format!("{}{path}", server()))
.send()
.with_context(|| format!("request {path}"))?;
if !resp.status().is_success() {
let status = resp.status();
let body = resp.text().unwrap_or_default();
anyhow::bail!(
"{}",
api_error(&body).unwrap_or(format!("{path}: server returned {status}: {body}"))
);
}
resp.json()
.with_context(|| format!("parse response from {path}"))
}
pub fn api_error(body: &str) -> Option<String> {
serde_json::from_str::<serde_json::Value>(body)
.ok()?
.get("error")?
.as_str()
.map(str::to_string)
.filter(|e| !e.is_empty())
}
#[derive(Debug, Deserialize)]
pub struct ProviderSummary {
pub id: String,
pub name: String,
#[serde(default)]
pub key_env: Option<String>,
pub key_set: bool,
#[serde(default)]
pub key_usable: bool,
#[serde(default)]
pub key_optional: bool,
pub models: usize,
}
#[derive(Debug, Deserialize)]
struct ProvidersResponse {
providers: Vec<ProviderSummary>,
}
impl ProviderSummary {
pub fn key_here(&self) -> bool {
crate::providers::key_for(&self.id, self.key_env.as_deref()).is_some()
}
}
pub fn providers() -> anyhow::Result<Vec<ProviderSummary>> {
Ok(get_json::<ProvidersResponse>("/llmman/providers")?.providers)
}
#[derive(Debug, Deserialize)]
pub struct ProviderDetail {
pub id: String,
pub name: String,
pub base_url: String,
#[serde(default)]
pub key_env: Option<String>,
pub key_set: bool,
#[serde(default)]
pub key_usable: bool,
#[serde(default)]
pub key_optional: bool,
pub models: Vec<ProviderModel>,
}
#[derive(Debug, Deserialize)]
pub struct ProviderModel {
pub id: String,
#[serde(default)]
pub cost: Option<ModelCost>,
}
#[derive(Debug, Clone, Copy, Deserialize)]
pub struct ModelCost {
pub input: f64,
pub output: f64,
}
impl ProviderDetail {
pub fn api_key(&self) -> Option<String> {
crate::providers::key_for(&self.id, self.key_env.as_deref())
}
pub fn model_ids(&self) -> Vec<&str> {
self.models.iter().map(|m| m.id.as_str()).collect()
}
pub fn key_hint(&self) -> String {
crate::providers::key_hint(&self.id, self.key_env.as_deref())
}
pub fn client_key(&self) -> Option<String> {
let key = self.api_key();
if key.is_some() && self.base_url.starts_with("http://") {
eprintln!(
"[llmman] warning: the API key for {} goes to {} over plain http",
self.name, self.base_url
);
}
key
}
pub fn warn_unlisted(&self, model: &str) {
if self.key_optional && self.models.is_empty() {
return;
}
if !self.models.iter().any(|m| m.id == model) {
eprintln!(
"[llmman] warning: {} does not list model {model:?}\n{}",
self.name,
crate::providers::example_models(&self.name, &self.model_ids())
);
}
}
pub fn daemon_key_usable(&self) -> bool {
self.key_usable
}
}
pub fn provider(id: &str) -> anyhow::Result<ProviderDetail> {
get_json(&format!("/llmman/providers/{}", encode_path_segment(id)))
}
fn encode_path_segment(segment: &str) -> String {
let mut out = String::with_capacity(segment.len());
for byte in segment.bytes() {
match byte {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => {
out.push(byte as char)
}
other => out.push_str(&format!("%{other:02X}")),
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wildcard_binds_are_locally_reachable_but_not_only_locally() {
for host in ["127.0.0.1", "::1", "localhost", "LOCALHOST"] {
assert!(host_connects_over_loopback(host), "{host}");
assert!(host_reachable_only_locally(host), "{host}");
}
for wildcard in ["0.0.0.0", "::", "0:0:0:0:0:0:0:0"] {
assert!(host_connects_over_loopback(wildcard), "{wildcard}");
assert!(!host_reachable_only_locally(wildcard), "{wildcard}");
}
for remote in ["192.168.1.50", "10.0.0.1", "example.com"] {
assert!(!host_connects_over_loopback(remote), "{remote}");
assert!(!host_reachable_only_locally(remote), "{remote}");
}
}
#[test]
fn encode_path_segment_keeps_a_typo_inside_one_segment() {
assert_eq!(encode_path_segment("openrouter"), "openrouter");
assert_eq!(
encode_path_segment("together-ai_1.0~x"),
"together-ai_1.0~x"
);
assert_eq!(encode_path_segment("a/b"), "a%2Fb");
assert_eq!(encode_path_segment("../api/tags"), "..%2Fapi%2Ftags");
assert_eq!(encode_path_segment("open ai"), "open%20ai");
assert_eq!(encode_path_segment("a?b#c"), "a%3Fb%23c");
assert_eq!(encode_path_segment("é"), "%C3%A9");
}
#[test]
fn daemon_key_usable_is_the_daemons_own_answer() {
let body = |extra: &str| {
serde_json::from_str::<ProviderDetail>(&format!(
r#"{{"id":"openai","name":"OpenAI","base_url":"https://api.openai.com/v1",
"key_env":"LLMMAN_TEST_PROVIDER_KEY_UNSET","models":[]{extra}}}"#
))
.expect("parses")
};
assert!(body(r#","key_set":true,"key_usable":true"#).daemon_key_usable());
assert!(!body(r#","key_set":true,"key_usable":false"#).daemon_key_usable());
assert!(!body(r#","key_set":true"#).daemon_key_usable());
}
#[test]
fn api_error_unwraps_the_daemons_own_message() {
assert_eq!(
api_error(r#"{"error":"unknown provider \"togethr\""}"#).as_deref(),
Some("unknown provider \"togethr\"")
);
assert_eq!(api_error(""), None);
assert_eq!(api_error("Not Found"), None);
assert_eq!(api_error(r#"{"error":""}"#), None);
assert_eq!(api_error(r#"{"detail":"nope"}"#), None);
}
#[test]
fn parse_host_defaults_when_unset_or_blank() {
assert_eq!(
parse_host(None),
("http".to_string(), "127.0.0.1".to_string(), 17434)
);
assert_eq!(
parse_host(Some(" ")),
("http".to_string(), "127.0.0.1".to_string(), 17434)
);
}
#[test]
fn parse_host_accepts_a_bare_host() {
assert_eq!(
parse_host(Some("0.0.0.0")),
("http".to_string(), "0.0.0.0".to_string(), 17434)
);
}
#[test]
fn parse_host_accepts_a_bare_host_and_port() {
assert_eq!(
parse_host(Some("0.0.0.0:8080")),
("http".to_string(), "0.0.0.0".to_string(), 8080)
);
}
#[test]
fn parse_host_accepts_an_explicit_scheme() {
assert_eq!(
parse_host(Some("https://example.com:9999")),
("https".to_string(), "example.com".to_string(), 9999)
);
}
#[test]
fn parse_host_shifts_default_port_for_http_and_https_schemes() {
assert_eq!(
parse_host(Some("http://example.com")),
("http".to_string(), "example.com".to_string(), 80)
);
assert_eq!(
parse_host(Some("https://example.com")),
("https".to_string(), "example.com".to_string(), 443)
);
}
#[test]
fn parse_host_keeps_default_port_for_other_schemes() {
assert_eq!(
parse_host(Some("grpc://example.com")),
("grpc".to_string(), "example.com".to_string(), 17434)
);
}
#[test]
fn parse_host_accepts_a_bracketed_ipv6_literal_with_port() {
assert_eq!(
parse_host(Some("[::1]:8080")),
("http".to_string(), "::1".to_string(), 8080)
);
}
#[test]
fn parse_host_accepts_a_bare_ipv6_literal_with_no_port() {
assert_eq!(
parse_host(Some("::1")),
("http".to_string(), "::1".to_string(), 17434)
);
}
#[test]
fn parse_host_falls_back_to_default_port_when_unparseable() {
assert_eq!(
parse_host(Some("example.com:not-a-port")),
("http".to_string(), "example.com".to_string(), 17434)
);
assert_eq!(
parse_host(Some("example.com:99999")),
("http".to_string(), "example.com".to_string(), 17434)
);
}
#[test]
fn parse_host_trims_whitespace_and_surrounding_quotes() {
assert_eq!(
parse_host(Some(" \"0.0.0.0:8080\" ")),
("http".to_string(), "0.0.0.0".to_string(), 8080)
);
}
#[test]
fn parse_host_drops_a_trailing_path() {
assert_eq!(
parse_host(Some("example.com:8080/some/path")),
("http".to_string(), "example.com".to_string(), 8080)
);
}
#[test]
fn peer_url_spells_a_peer_the_way_llmman_host_is_spelled() {
assert_eq!(peer_url("asahi"), "http://asahi:17434");
assert_eq!(peer_url("spark:8080"), "http://spark:8080");
assert_eq!(peer_url("https://x.example/"), "https://x.example:443");
assert_eq!(peer_url("[fe80::1]"), "http://[fe80::1]:17434");
}
#[test]
fn format_host_port_brackets_ipv6_literals() {
assert_eq!(format_host_port("::1", 17434), "[::1]:17434");
assert_eq!(format_host_port("127.0.0.1", 17434), "127.0.0.1:17434");
assert_eq!(format_host_port("example.com", 17434), "example.com:17434");
}
#[test]
fn connectable_host_rewrites_wildcard_hosts_to_loopback() {
assert_eq!(connectable_host("0.0.0.0"), "127.0.0.1");
assert_eq!(connectable_host("::"), "::1");
assert_eq!(connectable_host("0:0:0:0:0:0:0:0"), "::1"); assert_eq!(connectable_host("example.com"), "example.com");
}
#[test]
fn is_local_host_only_accepts_loopback_and_wildcard_hosts() {
for host in [
"127.0.0.1",
"::1",
"0:0:0:0:0:0:0:1", "localhost",
"0.0.0.0",
"::",
] {
assert!(is_local_host(host), "{host} should be local");
}
assert!(!is_local_host("example.com"));
assert!(!is_local_host("192.168.1.5"));
}
#[test]
fn log_tail_none_path_is_empty() {
assert_eq!(log_tail(None), "");
}
#[test]
fn fetch_message_carries_the_daemon_status_when_present() {
let bare = fetch_message("");
assert!(bare.contains("first run"), "got: {bare}");
assert!(bare.contains("backends"), "got: {bare}");
assert!(!bare.ends_with(':'), "got: {bare}");
assert_eq!(fetch_message(" \n"), bare);
let with = fetch_message("downloading llama.tar.gz: 12 MB / 40 MB (30%)\n");
assert!(with.starts_with(&bare), "got: {with}");
assert!(
with.ends_with(": downloading llama.tar.gz: 12 MB / 40 MB (30%)"),
"got: {with}"
);
}
#[test]
fn log_tail_missing_file_points_at_path() {
let path =
std::env::temp_dir().join(format!("llmman-log-tail-missing-{}", std::process::id()));
let _ = std::fs::remove_file(&path);
let tail = log_tail(Some(&path));
assert!(tail.contains("see "), "got: {tail}");
assert!(tail.contains(&path.display().to_string()), "got: {tail}");
}
#[test]
fn log_tail_returns_last_five_nonempty_lines() {
let path = std::env::temp_dir().join(format!("llmman-log-tail-{}", std::process::id()));
std::fs::write(&path, "one\ntwo\n\nthree\nfour\nfive\nsix\n").unwrap();
let tail = log_tail(Some(&path));
std::fs::remove_file(&path).unwrap();
assert!(tail.contains("last lines of"), "got: {tail}");
let excerpt = tail.split_once('\n').map(|(_, rest)| rest).unwrap_or("");
assert_eq!(excerpt, "two\nthree\nfour\nfive\nsix", "got: {tail}");
}
#[cfg(unix)]
#[test]
fn kill_group_refuses_pids_that_would_signal_everything() {
assert!(!kill_group(0, 0), "pid 0 = the caller's own process group");
assert!(!kill_group(1, 0), "pid 1 = every process the user owns");
assert!(!kill_group(u32::MAX, 0));
}
#[cfg(unix)]
#[test]
fn kill_group_reports_missing_group_as_nothing_signalled() {
let mut child = Command::new("sleep")
.arg("30")
.spawn()
.expect("spawn child");
let signalled = kill_group(child.id(), 0);
let _ = child.kill();
let _ = child.wait();
assert!(!signalled, "an empty group reported as signalled");
}
#[cfg(unix)]
#[test]
fn kill_group_kills_only_its_own_group() {
use std::os::unix::process::CommandExt;
let mut target = Command::new("sleep");
target.arg("30").process_group(0); let mut target = target.spawn().expect("spawn target");
let mut control = Command::new("sleep")
.arg("30")
.spawn()
.expect("spawn control");
assert!(
kill_group(target.id(), libc::SIGKILL),
"a live group should report as signalled"
);
let deadline = std::time::Instant::now() + Duration::from_secs(5);
loop {
match target.try_wait().expect("poll target") {
Some(_) => break,
None if std::time::Instant::now() >= deadline => {
let _ = target.kill();
let _ = control.kill();
panic!("target process group survived SIGKILL");
}
None => std::thread::sleep(Duration::from_millis(20)),
}
}
let survived = control.try_wait().expect("poll control").is_none();
let _ = control.kill();
let _ = control.wait();
assert!(
survived,
"a process outside the targeted group was signalled too"
);
}
#[test]
fn only_the_not_found_error_envelope_for_that_model_means_a_missing_model() {
let m = "docker.io/ai/m";
assert!(is_model_not_found_body(
r#"{"error":"model 'docker.io/ai/m' not found"}"#,
m
));
assert!(is_model_not_found_body(
r#"{"error":"model 'gemma4' not found"}"#,
"llmman.hybrid/gemma4,anthropic/claude-sonnet-4-5"
));
assert!(!is_model_not_found_body(
r#"{"error":"model 'other' not found"}"#,
m
));
assert!(!is_model_not_found_body(r#"{"error":"no route"}"#, m));
assert!(!is_model_not_found_body("<html>404 Not Found</html>", m));
assert!(!is_model_not_found_body("", m));
assert!(!is_model_not_found_body(
r#"{"message":"model 'docker.io/ai/m' not found"}"#,
m
));
}
}