Skip to main content

netget/protocol/
easy_registry.rs

1//! Easy protocol registry
2//!
3//! This module provides a centralized registry for "Easy" protocols that act as
4//! translation layers between network events and simplified LLM prompts.
5
6use crate::llm::actions::Easy;
7use std::collections::HashMap;
8use std::sync::{Arc, LazyLock};
9
10/// Global easy protocol registry mapping protocol names to easy protocol implementations
11pub struct EasyRegistry {
12    /// Maps easy protocol name (e.g., "http-easy") to easy protocol implementation
13    protocols: HashMap<String, Arc<dyn Easy>>,
14    /// Maps underlying protocol event type IDs to easy protocol name
15    /// Used for routing events: e.g., "http_request" -> "http-easy"
16    event_routing: HashMap<String, String>,
17}
18
19impl EasyRegistry {
20    /// Create a new easy protocol registry
21    fn new() -> Self {
22        let mut registry = Self {
23            protocols: HashMap::new(),
24            event_routing: HashMap::new(),
25        };
26
27        // Register every easy protocol this platform builds
28        registry.register_protocols();
29        registry.build_event_routing();
30
31        registry
32    }
33
34    /// Register all available easy protocols based on compiled features
35    fn register_protocols(&mut self) {
36        self.register(Arc::new(crate::easy::http::HttpEasyProtocol));
37    }
38
39    /// Register a new easy protocol
40    #[allow(dead_code)]
41    fn register(&mut self, protocol: Arc<dyn Easy>) {
42        let name = protocol.protocol_name().to_string();
43        self.protocols.insert(name, protocol);
44    }
45
46    /// Build event routing map from event type IDs to easy protocol names
47    fn build_event_routing(&mut self) {
48        for (protocol_name, protocol) in &self.protocols {
49            for event_type_id in protocol.get_handled_event_type_ids() {
50                self.event_routing
51                    .insert(event_type_id.to_string(), protocol_name.clone());
52            }
53        }
54    }
55
56    /// Get a protocol by name (e.g., "http-easy")
57    pub fn get_by_name(&self, name: &str) -> Option<Arc<dyn Easy>> {
58        self.protocols.get(name).cloned()
59    }
60
61    /// Get easy protocol that handles a specific event type ID
62    ///
63    /// Returns None if no easy protocol is registered to handle this event type.
64    /// Used by EventHandler to route events to easy protocols.
65    pub fn get_by_event_type(&self, event_type_id: &str) -> Option<Arc<dyn Easy>> {
66        self.event_routing
67            .get(event_type_id)
68            .and_then(|protocol_name| self.get_by_name(protocol_name))
69    }
70
71    /// Get all registered easy protocol names
72    pub fn get_all_names(&self) -> Vec<String> {
73        self.protocols.keys().cloned().collect()
74    }
75
76    /// Get all registered easy protocols
77    pub fn get_all(&self) -> Vec<Arc<dyn Easy>> {
78        self.protocols.values().cloned().collect()
79    }
80}
81
82/// Global easy protocol registry instance
83pub static EASY_REGISTRY: LazyLock<EasyRegistry> = LazyLock::new(EasyRegistry::new);