use serde::Serialize;
use serde_json::Value;
use lean_ctx_cloud::models_access::CatalogRead;
use super::routes::{Method, Project, Route};
use super::{Answer, Failure, Via};
pub(crate) const KIND: &str = "cloud.models.catalog";
pub(crate) const ROUTES: &[Route] = &[Route {
kind: KIND,
method: Method::Get,
path: "/v1/local/cloud/models/catalog",
cli: &["cloud", "models", "catalog", "--json"],
action: &["catalog"],
params: &[],
project: Project::None,
names: None,
}];
pub(crate) const KINDS: &[&str] = &[KIND];
pub(crate) const COMMANDS: &[(&str, &[&str])] = &[("cloud.models", &["catalog"])];
const READ_TOOL: &str = "ctx_knowledge";
#[derive(Debug, Serialize, PartialEq)]
pub(crate) struct CatalogDoc {
pub catalog: Value,
pub verified: Option<bool>,
pub models_subscription: bool,
pub gateway_base_url: String,
}
pub(crate) fn build(read: CatalogRead) -> CatalogDoc {
CatalogDoc {
catalog: read.document,
verified: read.verified,
models_subscription: read.models_subscription,
gateway_base_url: read.gateway_base_url,
}
}
fn read() -> Result<CatalogDoc, Failure> {
Ok(build(lean_ctx_cloud::models_access::read_catalog()?))
}
pub(crate) fn answer(kind: &str, _args: &[String], via: Via) -> Answer {
match kind {
KIND => super::answer_released(READ_TOOL, "", kind, via, read),
other => Answer::failure(&Failure::invalid(format!("unknown kind {other}"))),
}
}
pub(crate) fn try_run(command: &str, rest: &[String]) -> bool {
if !super::wants_json(rest) {
return false;
}
let words: Vec<&str> = rest
.iter()
.map(String::as_str)
.filter(|w| !w.starts_with("--"))
.collect();
let taken = match command {
"cloud" => words == ["models", "catalog"],
"models" => words.is_empty(),
_ => false,
};
if taken {
super::print(&answer(KIND, rest, Via::Cli));
}
taken
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn words(w: &[&str]) -> Vec<String> {
w.iter().map(|s| (*s).to_owned()).collect()
}
#[test]
fn the_catalog_is_dispatched_routed_and_listed_without_a_project() {
assert!(super::super::part_of(KIND).is_some(), "{KIND} has no part");
let route = super::super::routes::all()
.find(|r| r.kind == KIND)
.expect("routed");
assert!(matches!(route.method, Method::Get));
assert!(matches!(route.project, Project::None));
assert!(
super::super::routes::table_json()
.to_string()
.contains("/v1/local/cloud/models/catalog")
);
}
#[test]
fn the_cloud_document_reaches_the_reader_unchanged() {
let catalog = json!({
"schema": "leanctx.cloud.model-catalog/v1",
"catalog_version": "1",
"price_version": "1",
"models": [],
"direct": [],
"key_id": "0123456789abcdef",
"signature": "00"
});
let doc = build(CatalogRead {
document: catalog.clone(),
verified: Some(false),
models_subscription: true,
gateway_base_url: "https://cloud.example.com/gateway/v1".into(),
});
let text = super::super::document(KIND, &doc);
let value: Value = serde_json::from_str(&text).unwrap();
assert_eq!(value["contract"], "local-read/1");
assert_eq!(value["kind"], KIND);
assert_eq!(value["catalog"], catalog);
assert_eq!(value["verified"], false);
assert_eq!(value["models_subscription"], true);
assert_eq!(
value["gateway_base_url"],
"https://cloud.example.com/gateway/v1"
);
let unsigned = build(CatalogRead {
document: json!({}),
verified: None,
models_subscription: false,
gateway_base_url: String::new(),
});
assert!(serde_json::to_value(unsigned).unwrap()["verified"].is_null());
}
#[test]
fn without_a_connection_the_answer_is_not_connected() {
let _data = lean_ctx::core::data_dir::isolated_data_dir();
let failure = read().expect_err("no instance here");
assert_eq!(failure.code, super::super::Code::NotConnected);
}
#[test]
fn only_the_catalog_command_lines_are_taken() {
assert!(
!try_run("cloud", &words(&["models", "catalog"])),
"needs --json"
);
assert!(!try_run("models", &words(&["help"])));
assert!(!try_run("cloud", &words(&["billing", "credit", "--json"])));
assert!(!try_run("cloud", &words(&["models", "--json"])));
assert!(!try_run("models", &words(&["list", "--json"])));
}
}