ai-agent 0.13.4

Idiomatic agent sdk inspired by the claude code source leak
Documentation
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Mutex;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachedSchema {
    #[serde(flatten)]
    pub tool: serde_json::Value,
    #[serde(default)]
    pub strict: Option<bool>,
    #[serde(default)]
    pub eager_input_streaming: Option<bool>,
}

static TOOL_SCHEMA_CACHE: Lazy<Mutex<HashMap<String, CachedSchema>>> =
    Lazy::new(|| Mutex::new(HashMap::new()));

pub fn get_tool_schema_cache() -> std::collections::HashMap<String, CachedSchema> {
    TOOL_SCHEMA_CACHE.lock().unwrap().clone()
}

pub fn clear_tool_schema_cache() {
    TOOL_SCHEMA_CACHE.lock().unwrap().clear();
}

pub fn put_tool_schema(name: String, schema: CachedSchema) {
    TOOL_SCHEMA_CACHE.lock().unwrap().insert(name, schema);
}

pub fn get_tool_schema(name: &str) -> Option<CachedSchema> {
    TOOL_SCHEMA_CACHE.lock().unwrap().get(name).cloned()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cache_operations() {
        clear_tool_schema_cache();

        let schema = CachedSchema {
            tool: serde_json::json!({"name": "test"}),
            strict: Some(true),
            eager_input_streaming: None,
        };

        put_tool_schema("test_tool".to_string(), schema.clone());
        let retrieved = get_tool_schema("test_tool");

        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().tool["name"], "test");
    }
}