Skip to main content

roboticus_api/api/
state.rs

1//! Domain-scoped state traits for `AppState` decomposition.
2//!
3//! Phase 1 of the AppState god-object decomposition: define traits that
4//! declare which domain surfaces a function actually needs. Functions
5//! should accept `impl HasInference + HasAgent` instead of `&AppState`
6//! when they only need those two domains.
7//!
8//! Phase 2 (future): extract sub-states from AppState and implement
9//! these traits on the narrower types, enabling true separation.
10
11use std::sync::Arc;
12use tokio::sync::RwLock;
13
14use roboticus_agent::approvals::ApprovalManager;
15use roboticus_agent::capability::CapabilityRegistry;
16use roboticus_agent::policy::PolicyEngine;
17use roboticus_agent::retrieval::MemoryRetriever;
18use roboticus_agent::subagents::SubagentRegistry;
19use roboticus_agent::tools::ToolRegistry;
20use roboticus_channels::router::ChannelRouter;
21use roboticus_core::RoboticusConfig;
22use roboticus_db::Database;
23use roboticus_llm::LlmService;
24use roboticus_llm::semantic_classifier::SemanticClassifier;
25use roboticus_plugin_sdk::registry::PluginRegistry;
26use roboticus_wallet::WalletService;
27
28/// Core infrastructure: database, config, HMAC, rate limiting.
29pub trait HasCore {
30    fn db(&self) -> &Database;
31    fn config(&self) -> &Arc<RwLock<RoboticusConfig>>;
32    fn hmac_secret(&self) -> &[u8];
33}
34
35/// LLM inference: model routing, quality tracking, latency, caching.
36pub trait HasInference {
37    fn llm(&self) -> &Arc<RwLock<LlmService>>;
38    fn semantic_classifier(&self) -> &Arc<SemanticClassifier>;
39}
40
41/// Agent tools: tool registry, capabilities, policy, approvals, plugins.
42pub trait HasAgent {
43    fn tools(&self) -> &Arc<ToolRegistry>;
44    fn capabilities(&self) -> &Arc<CapabilityRegistry>;
45    fn policy_engine(&self) -> &Arc<PolicyEngine>;
46    fn approvals(&self) -> &Arc<ApprovalManager>;
47    fn registry(&self) -> &Arc<SubagentRegistry>;
48    fn plugins(&self) -> &Arc<PluginRegistry>;
49}
50
51/// Memory and retrieval: memory retriever, ANN index, obsidian vault.
52pub trait HasMemory {
53    fn retriever(&self) -> &Arc<MemoryRetriever>;
54    fn ann_index(&self) -> &roboticus_db::ann::AnnIndex;
55}
56
57/// Channel adapters: router and per-platform adapters.
58pub trait HasChannels {
59    fn channel_router(&self) -> &Arc<ChannelRouter>;
60}
61
62/// Wallet and financial services.
63pub trait HasWallet {
64    fn wallet(&self) -> &Arc<WalletService>;
65}
66
67// ── AppState implementations ─────────────────────────────────────────
68
69use super::routes::AppState;
70
71impl HasCore for AppState {
72    fn db(&self) -> &roboticus_db::Database {
73        &self.db
74    }
75    fn config(&self) -> &Arc<RwLock<roboticus_core::RoboticusConfig>> {
76        &self.config
77    }
78    fn hmac_secret(&self) -> &[u8] {
79        &self.hmac_secret
80    }
81}
82
83impl HasInference for AppState {
84    fn llm(&self) -> &Arc<RwLock<roboticus_llm::LlmService>> {
85        &self.llm
86    }
87    fn semantic_classifier(&self) -> &Arc<roboticus_llm::semantic_classifier::SemanticClassifier> {
88        &self.semantic_classifier
89    }
90}
91
92impl HasAgent for AppState {
93    fn tools(&self) -> &Arc<roboticus_agent::tools::ToolRegistry> {
94        &self.tools
95    }
96    fn capabilities(&self) -> &Arc<roboticus_agent::capability::CapabilityRegistry> {
97        &self.capabilities
98    }
99    fn policy_engine(&self) -> &Arc<roboticus_agent::policy::PolicyEngine> {
100        &self.policy_engine
101    }
102    fn approvals(&self) -> &Arc<roboticus_agent::approvals::ApprovalManager> {
103        &self.approvals
104    }
105    fn registry(&self) -> &Arc<roboticus_agent::subagents::SubagentRegistry> {
106        &self.registry
107    }
108    fn plugins(&self) -> &Arc<roboticus_plugin_sdk::registry::PluginRegistry> {
109        &self.plugins
110    }
111}
112
113impl HasMemory for AppState {
114    fn retriever(&self) -> &Arc<roboticus_agent::retrieval::MemoryRetriever> {
115        &self.retriever
116    }
117    fn ann_index(&self) -> &roboticus_db::ann::AnnIndex {
118        &self.ann_index
119    }
120}
121
122impl HasChannels for AppState {
123    fn channel_router(&self) -> &Arc<roboticus_channels::router::ChannelRouter> {
124        &self.channel_router
125    }
126}
127
128impl HasWallet for AppState {
129    fn wallet(&self) -> &Arc<roboticus_wallet::WalletService> {
130        &self.wallet
131    }
132}