vex_api/
state.rs

1//! Application State
2//!
3//! Centralizes access to DB, Queue, and Auth/Monitoring services.
4
5use crate::a2a::handler::A2aState;
6use crate::auth::JwtAuth;
7use crate::tenant_rate_limiter::TenantRateLimiter;
8use std::sync::Arc;
9use vex_llm::{LlmProvider, Metrics};
10use vex_persist::StorageBackend;
11use vex_queue::{QueueBackend, WorkerPool};
12
13/// Application state shared across handlers
14#[derive(Clone)]
15pub struct AppState {
16    jwt_auth: JwtAuth,
17    rate_limiter: Arc<TenantRateLimiter>,
18    metrics: Arc<Metrics>,
19    db: Arc<dyn StorageBackend>,
20    queue: Arc<WorkerPool<dyn QueueBackend>>,
21    a2a_state: Arc<A2aState>,
22    llm: Arc<dyn LlmProvider>,
23    router: Option<Arc<vex_router::Router>>,
24}
25
26impl AppState {
27    /// Create new application state
28    #[allow(clippy::too_many_arguments)]
29    pub fn new(
30        jwt_auth: JwtAuth,
31        rate_limiter: Arc<TenantRateLimiter>,
32        metrics: Arc<Metrics>,
33        db: Arc<dyn StorageBackend>,
34        queue: Arc<WorkerPool<dyn QueueBackend>>,
35        a2a_state: Arc<A2aState>,
36        llm: Arc<dyn LlmProvider>,
37        router: Option<Arc<vex_router::Router>>,
38    ) -> Self {
39        Self {
40            jwt_auth,
41            rate_limiter,
42            metrics,
43            db,
44            queue,
45            a2a_state,
46            llm,
47            router,
48        }
49    }
50
51    /// Get JWT auth service
52    pub fn jwt_auth(&self) -> &JwtAuth {
53        &self.jwt_auth
54    }
55
56    /// Get rate limiter (cloned Arc for sharing)
57    pub fn rate_limiter(&self) -> Arc<TenantRateLimiter> {
58        self.rate_limiter.clone()
59    }
60
61    /// Get metrics collector (cloned Arc for sharing)
62    pub fn metrics(&self) -> Arc<Metrics> {
63        self.metrics.clone()
64    }
65
66    /// Get database backend (cloned Arc for sharing)
67    pub fn db(&self) -> Arc<dyn StorageBackend> {
68        self.db.clone()
69    }
70
71    /// Get queue worker pool (cloned Arc for sharing)
72    pub fn queue(&self) -> Arc<WorkerPool<dyn QueueBackend>> {
73        self.queue.clone()
74    }
75
76    /// Get A2A state (cloned Arc for sharing)
77    pub fn a2a_state(&self) -> Arc<A2aState> {
78        self.a2a_state.clone()
79    }
80
81    /// Get LLM provider (cloned Arc for sharing)
82    pub fn llm(&self) -> Arc<dyn LlmProvider> {
83        self.llm.clone()
84    }
85
86    /// Get Router specifically (cloned Arc for sharing)
87    pub fn router(&self) -> Option<Arc<vex_router::Router>> {
88        self.router.clone()
89    }
90}