Skip to main content

weavatrix_rust/operations/catalog/
mod.rs

1use blazingly_json::{Map, Value, json};
2use serde::Serialize;
3
4mod definitions;
5mod profile;
6mod schema;
7mod validation;
8
9pub use profile::ToolProfile;
10
11#[derive(Debug, Clone, Serialize)]
12#[serde(rename_all = "camelCase")]
13pub struct ToolDefinition {
14    pub name: &'static str,
15    pub description: &'static str,
16    pub input_schema: Value,
17}
18
19#[must_use]
20pub fn catalog() -> Vec<ToolDefinition> {
21    definitions::SPECS
22        .iter()
23        .filter(|spec| capability_is_compiled(spec.name))
24        .map(|spec| tool(spec.name, spec.description, spec.required))
25        .collect()
26}
27
28#[must_use]
29pub fn catalog_for_profile(profile: ToolProfile) -> Vec<ToolDefinition> {
30    catalog()
31        .into_iter()
32        .filter(|tool| profile.allows(tool.name))
33        .collect()
34}
35
36#[allow(clippy::needless_bool)]
37fn capability_is_compiled(tool: &str) -> bool {
38    if tool == "find_duplicates" {
39        cfg!(feature = "clone")
40    } else if matches!(
41        tool,
42        "change_impact" | "git_history" | "cross_repo_git" | "verified_change" | "graph_diff"
43    ) {
44        cfg!(feature = "git")
45    } else if tool == "search_code" {
46        cfg!(feature = "search")
47    } else if matches!(tool, "semantic_link" | "seo_link_suggestions") {
48        cfg!(feature = "semantic")
49    } else if tool == "vector_search" {
50        cfg!(feature = "vector")
51    } else if tool == "memory_context" {
52        cfg!(feature = "memory")
53    } else {
54        true
55    }
56}
57
58fn tool(
59    tool_name: &'static str,
60    description: &'static str,
61    required: &[&'static str],
62) -> ToolDefinition {
63    let mut properties = Map::from_iter([(
64        "output_format".to_owned(),
65        json!({
66            "type": "string",
67            "enum": ["text", "json"],
68            "default": "json"
69        }),
70    )]);
71    for name in schema::optional_fields(tool_name) {
72        properties.insert((*name).to_owned(), schema::field_schema(tool_name, name));
73    }
74    for name in required {
75        let schema = schema::field_schema(tool_name, name);
76        properties.insert((*name).to_owned(), schema);
77    }
78    let required = required
79        .iter()
80        .map(|value| json!(value))
81        .collect::<Vec<_>>();
82    ToolDefinition {
83        name: tool_name,
84        description,
85        input_schema: json!({
86            "type": "object",
87            "additionalProperties": true,
88            "properties": properties,
89            "required": required
90        }),
91    }
92}