Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / infra / agents / agentd / src / state.rs

state.rs53 lines · 1.4 KiBRaw
1//! Shared application state: config + store + auth + runner + OpenRouter.
2
3use std::sync::Arc;
4
5use tokio::sync::broadcast;
6
7use crate::agent::DriverCtx;
8use crate::auth::Auth;
9use crate::config::Config;
10use crate::models::Event;
11use crate::openrouter::OpenRouter;
12use crate::runner::Runner;
13use crate::store::Store;
14
15#[derive(Clone)]
16pub 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
25impl 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}