roboticus-api 0.11.4

HTTP routes, WebSocket, auth, rate limiting, and dashboard for the Roboticus agent runtime
Documentation
//! Domain-scoped state traits for `AppState` decomposition.
//!
//! Phase 1 of the AppState god-object decomposition: define traits that
//! declare which domain surfaces a function actually needs. Functions
//! should accept `impl HasInference + HasAgent` instead of `&AppState`
//! when they only need those two domains.
//!
//! Phase 2 (future): extract sub-states from AppState and implement
//! these traits on the narrower types, enabling true separation.

use std::sync::Arc;
use tokio::sync::RwLock;

use roboticus_agent::approvals::ApprovalManager;
use roboticus_agent::capability::CapabilityRegistry;
use roboticus_agent::policy::PolicyEngine;
use roboticus_agent::retrieval::MemoryRetriever;
use roboticus_agent::subagents::SubagentRegistry;
use roboticus_agent::tools::ToolRegistry;
use roboticus_channels::router::ChannelRouter;
use roboticus_core::RoboticusConfig;
use roboticus_db::Database;
use roboticus_llm::LlmService;
use roboticus_llm::semantic_classifier::SemanticClassifier;
use roboticus_plugin_sdk::registry::PluginRegistry;
use roboticus_wallet::WalletService;

/// Core infrastructure: database, config, HMAC, rate limiting.
pub trait HasCore {
    fn db(&self) -> &Database;
    fn config(&self) -> &Arc<RwLock<RoboticusConfig>>;
    fn hmac_secret(&self) -> &[u8];
}

/// LLM inference: model routing, quality tracking, latency, caching.
pub trait HasInference {
    fn llm(&self) -> &Arc<RwLock<LlmService>>;
    fn semantic_classifier(&self) -> &Arc<SemanticClassifier>;
}

/// Agent tools: tool registry, capabilities, policy, approvals, plugins.
pub trait HasAgent {
    fn tools(&self) -> &Arc<ToolRegistry>;
    fn capabilities(&self) -> &Arc<CapabilityRegistry>;
    fn policy_engine(&self) -> &Arc<PolicyEngine>;
    fn approvals(&self) -> &Arc<ApprovalManager>;
    fn registry(&self) -> &Arc<SubagentRegistry>;
    fn plugins(&self) -> &Arc<PluginRegistry>;
}

/// Memory and retrieval: memory retriever, ANN index, obsidian vault.
pub trait HasMemory {
    fn retriever(&self) -> &Arc<MemoryRetriever>;
    fn ann_index(&self) -> &roboticus_db::ann::AnnIndex;
}

/// Channel adapters: router and per-platform adapters.
pub trait HasChannels {
    fn channel_router(&self) -> &Arc<ChannelRouter>;
}

/// Wallet and financial services.
pub trait HasWallet {
    fn wallet(&self) -> &Arc<WalletService>;
}

// ── AppState implementations ─────────────────────────────────────────

use super::routes::AppState;

impl HasCore for AppState {
    fn db(&self) -> &roboticus_db::Database {
        &self.db
    }
    fn config(&self) -> &Arc<RwLock<roboticus_core::RoboticusConfig>> {
        &self.config
    }
    fn hmac_secret(&self) -> &[u8] {
        &self.hmac_secret
    }
}

impl HasInference for AppState {
    fn llm(&self) -> &Arc<RwLock<roboticus_llm::LlmService>> {
        &self.llm
    }
    fn semantic_classifier(&self) -> &Arc<roboticus_llm::semantic_classifier::SemanticClassifier> {
        &self.semantic_classifier
    }
}

impl HasAgent for AppState {
    fn tools(&self) -> &Arc<roboticus_agent::tools::ToolRegistry> {
        &self.tools
    }
    fn capabilities(&self) -> &Arc<roboticus_agent::capability::CapabilityRegistry> {
        &self.capabilities
    }
    fn policy_engine(&self) -> &Arc<roboticus_agent::policy::PolicyEngine> {
        &self.policy_engine
    }
    fn approvals(&self) -> &Arc<roboticus_agent::approvals::ApprovalManager> {
        &self.approvals
    }
    fn registry(&self) -> &Arc<roboticus_agent::subagents::SubagentRegistry> {
        &self.registry
    }
    fn plugins(&self) -> &Arc<roboticus_plugin_sdk::registry::PluginRegistry> {
        &self.plugins
    }
}

impl HasMemory for AppState {
    fn retriever(&self) -> &Arc<roboticus_agent::retrieval::MemoryRetriever> {
        &self.retriever
    }
    fn ann_index(&self) -> &roboticus_db::ann::AnnIndex {
        &self.ann_index
    }
}

impl HasChannels for AppState {
    fn channel_router(&self) -> &Arc<roboticus_channels::router::ChannelRouter> {
        &self.channel_router
    }
}

impl HasWallet for AppState {
    fn wallet(&self) -> &Arc<roboticus_wallet::WalletService> {
        &self.wallet
    }
}