tessera-command 0.4.21

The tesserax command: opens a recipe, then serves, fetches or runs tasks
Documentation
use super::route::{argv, brief, knobs, prompt, schema, tools};
use crate::source::{Origin, builtins, catalog, split};
use serde_json::json;
use tessera_frontend::recipe::Catalog;

/// Tools come from the entry list, and picks become commands.
#[test]
fn picks_become_commands() {
    let catalog = catalog();
    let known = Catalog::embedded().unwrap();
    assert_eq!(catalog.len(), builtins().len());
    let tools = tools(&catalog);
    let names: Vec<&str> = tools.iter().map(|t| t.name).collect();
    assert_eq!(names, ["serve", "fetch", "machine", "embed", "image"]);
    // A tool without a model has one branch.
    let branches = schema(&catalog, &tools)["anyOf"].as_array().map(Vec::len);
    assert_eq!(branches, Some(2 * tools.len()));
    assert!(prompt(&catalog, &known, &tools, "hi").ends_with("Request: hi\n"));
    let image = json!({ "tool": "image", "model": "flux2-klein", "prompt": "a fox", "n": 2 });
    let found = |words: &str| {
        let id = format!("hf:found/{}", words.replace(' ', "-"));
        Ok(vec!["--model".to_string(), id])
    };
    let args = argv(&image, &catalog, found)
        .ok()
        .flatten()
        .unwrap_or_default();
    assert_eq!(
        args,
        [
            "--recipe",
            "flux2-klein",
            "task",
            "image",
            "--n",
            "2",
            "a fox"
        ]
    );
    let given: Vec<String> = args.iter().map(String::from).collect();
    let origin = split(&given).ok().and_then(|(origin, _)| origin);
    assert!(matches!(origin, Some(Origin::Builtin("flux2-klein"))));
    let hub = json!({ "tool": "serve", "hub": "qwen 27b", "knobs": { "profile": "1" } });
    assert_eq!(knobs(&hub), [("profile".to_string(), "1".to_string())]);
    let args = argv(&hub, &catalog, found)
        .ok()
        .flatten()
        .unwrap_or_default();
    assert_eq!(args, ["--model", "hf:found/qwen-27b", "serve"]);
    let kind = json!({ "tool": "fetch", "hub": "the image model" });
    let args = argv(&kind, &catalog, found)
        .ok()
        .flatten()
        .unwrap_or_default();
    assert_eq!(args, ["--recipe", "flux2-klein", "fetch"]);
    let named = json!({ "tool": "fetch", "hub": "flux2-klein" });
    let args = argv(&named, &catalog, found).ok().flatten();
    assert_eq!(
        args.unwrap_or_default(),
        ["--recipe", "flux2-klein", "fetch"]
    );
    let machine = argv(&json!({ "tool": "machine" }), &catalog, found);
    assert_eq!(machine.ok().flatten().unwrap_or_default(), ["machine"]);
    let reply = argv(&json!({ "tool": "reply" }), &catalog, found);
    assert!(matches!(reply, Ok(None)));
    // Long lists reach the model cut, with a count.
    let vector = json!({ "records": [{ "embedding": vec![0.5; 768] }] });
    let cut = brief(&vector)["records"][0]["embedding"].clone();
    assert_eq!(cut.as_array().map(Vec::len), Some(9));
    assert_eq!(cut[8], json!("760 more, 768 in all"));
}

/// Built-in recipes match the embedded catalog.
#[test]
fn builtins_match_the_catalog() {
    let known = Catalog::embedded().unwrap();
    for recipe in catalog() {
        let family = known.find(&recipe.name).unwrap();
        let pick = &family.picks[0].recipe().unwrap();
        assert_eq!(pick.weights, recipe.weights, "{}", recipe.name);
        assert_eq!(pick.serve, recipe.serve, "{}", recipe.name);
        assert_eq!(pick.config, recipe.config, "{}", recipe.name);
    }
}