Skip to main content

tessera_command/
lib.rs

1//! `tesserax [--recipe <src>] [--model <dir | hf:<owner>/<name>[@<revision>]>] <verb>`.
2
3mod check;
4#[cfg(test)]
5mod check_test;
6mod fetch;
7mod machine;
8mod mcp;
9#[cfg(test)]
10mod mcp_test;
11mod models;
12#[cfg(test)]
13mod models_test;
14mod progress;
15mod serve;
16mod size;
17mod source;
18#[cfg(test)]
19mod source_test;
20mod surface;
21#[cfg(test)]
22mod surface_test;
23mod task;
24mod teleport;
25#[cfg(test)]
26mod teleport_test;
27mod version;
28
29use source::{Stop, split};
30use std::process::ExitCode;
31use tessera_frontend::serve::ranks::FLAGS as RANKS;
32use tessera_frontend::serve::start::flags;
33
34const USAGE: &str = "usage: tesserax [--recipe <dir | git+<url>#<commit>>] [--model <dir | hf:<owner>/<name>[@<revision>]>] <verb>
35  [--yes] [--max-tokens <n>] <prompt>...
36                 answer one prompt. With no model flag, the default model may
37                 pick a command for it instead, and asks first on a terminal.
38                 Without a terminal it refuses unless --yes is given
39  <command>      run one of the commands below
40  task <task>    run one of the tasks below on the model
41  models [--readme <path>]
42                 print the model architectures and the tested weights. --readme
43                 rewrites the Supported Models section of that file
44  mcp            serve the commands and tasks as MCP tools on stdin and stdout
45  size [--ctx <tokens>] [--slots <n>] [--boot]
46                 print the memory the model needs and this machine has, as
47                 JSON. --boot loads it and runs one step to measure
48  check-kernels [--arch <sm_XX>] [--max-spills <bytes>] [--file <path>] [--json]
49                 compile planned or standalone CUDA kernels offline with NVRTC and ptxas
50  teleport [--from <dir>] [--root <dir>] <user@host | local>...
51                 copy this build to other machines over ssh
52  --version      print version, commit, target and GPU features
53  --cli-json     print the commands, flags and words as JSON";
54
55const HELP: &str = include_str!("../HELP.md");
56
57fn run(given: &[String]) -> Result<(), Stop> {
58    if given.is_empty() {
59        print!("{HELP}");
60        return Ok(());
61    }
62    if given == ["--version"] {
63        println!("{}", version::line());
64        return Ok(());
65    }
66    if given == ["--cli-json"] {
67        println!("{:#}", surface::json());
68        return Ok(());
69    }
70    let (origin, args) = split(given)?;
71    let Some((verb, rest)) = args.split_first() else {
72        return Err(Stop::Usage);
73    };
74    match (verb.as_str(), task::find("", verb)) {
75        // Words a command refuses may still be a prompt.
76        (_, Some(command)) if origin.is_some() || (command.accepts)(rest) => {
77            (command.call)(origin, rest, &mut *task::terminal())
78        }
79        ("task", _) => task::run(origin, rest),
80        ("mcp", _) => mcp::run(origin, rest),
81        ("models", _) => models::run(rest),
82        ("teleport", _) => teleport::run(rest),
83        ("size", _) => size::run(origin, rest),
84        ("check-kernels", _) => check::run(origin, rest),
85        ("-h" | "--help" | "help", _) => Err(Stop::Usage),
86        // Other words are a prompt for the default model.
87        _ if origin.is_none() => task::route(&args),
88        _ => task::run(origin, &[&["ask".to_string()], args.as_slice()].concat()),
89    }
90}
91
92/// Prints why a run stopped and gives its exit code.
93fn finish(result: Result<(), Stop>) -> u8 {
94    match result {
95        Ok(()) => 0,
96        Err(Stop::Usage) => {
97            let default = source::default_model();
98            eprintln!("{USAGE}\nwith neither flag, the model is {default}");
99            eprintln!("{}", task::list());
100            eprintln!("serve flags: {} {RANKS}", flags());
101            2
102        }
103        Err(Stop::Tasks(message)) => {
104            if let Some(message) = message {
105                eprintln!("tesserax: {message}");
106            }
107            eprintln!("{}", task::list());
108            2
109        }
110        Err(Stop::Failed(e)) => {
111            eprintln!("tesserax: {e}");
112            if let Some(no_gpu) = tessera_frontend::frontend_settings::no_gpu() {
113                eprintln!("tesserax: {no_gpu}");
114            }
115            1
116        }
117    }
118}
119
120/// Runs the command line with the process arguments.
121pub fn main() -> ExitCode {
122    let given: Vec<String> = std::env::args().skip(1).collect();
123    ExitCode::from(finish(run(&given)))
124}