Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / infra / agents / agentd / src / state.rs
state.rs53 lines · 1.4 KiBRaw
| 1 | //! Shared application state: config + store + auth + runner + OpenRouter. |
| 2 | |
| 3 | use std::sync::Arc; |
| 4 | |
| 5 | use tokio::sync::broadcast; |
| 6 | |
| 7 | use crate::agent::DriverCtx; |
| 8 | use crate::auth::Auth; |
| 9 | use crate::config::Config; |
| 10 | use crate::models::Event; |
| 11 | use crate::openrouter::OpenRouter; |
| 12 | use crate::runner::Runner; |
| 13 | use crate::store::Store; |
| 14 | |
| 15 | #[derive(Clone)] |
| 16 | pub struct AppState { |
| 17 | pub config: Arc<Config>, |
| 18 | pub store: Store, |
| 19 | pub auth: Auth, |
| 20 | pub runner: Runner, |
| 21 | pub openrouter: OpenRouter, |
| 22 | pub events_tx: broadcast::Sender<(i64, Event)>, |
| 23 | } |
| 24 | |
| 25 | impl AppState { |
| 26 | pub fn new(config: Config, store: Store) -> AppState { |
| 27 | let auth = Auth::new(store.clone(), config.password_hash.clone()); |
| 28 | let runner = Runner::new(config.runner_url.clone()); |
| 29 | let openrouter = OpenRouter::new( |
| 30 | config.openrouter_base_url.clone(), |
| 31 | config.openrouter_api_key.clone(), |
| 32 | ); |
| 33 | let (events_tx, _) = broadcast::channel(1024); |
| 34 | AppState { |
| 35 | config: Arc::new(config), |
| 36 | store, |
| 37 | auth, |
| 38 | runner, |
| 39 | openrouter, |
| 40 | events_tx, |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | pub fn driver(&self) -> DriverCtx { |
| 45 | DriverCtx { |
| 46 | config: self.config.clone(), |
| 47 | store: self.store.clone(), |
| 48 | runner: self.runner.clone(), |
| 49 | openrouter: self.openrouter.clone(), |
| 50 | events_tx: self.events_tx.clone(), |
| 51 | } |
| 52 | } |
| 53 | } |