botserver/src/shared/state.rs
Rodrigo Rodriguez (Pragmatismo) 14ab06b9f1 refactor: inject AppState into automation & bot, drop Default impl
- Updated `execute_compact_prompt` to accept an `Arc<AppState>` instead of creating a new default state, enabling proper state sharing across tasks.
- Adjusted bot orchestration to clone and pass the existing `AppState` to the automation task, ensuring the same connection and configuration are used.
- Removed the `Default` implementation for `AppState`, preventing accidental creation of a default state with hard‑coded DB connections and services.
- Modified `BotOrchestrator::default` to panic, enforcing explicit construction via `BotOrchestrator::new(state)` for clearer dependency injection.

These changes improve testability, avoid hidden side‑effects from default state initialization, and ensure consistent use of the application state throughout the system.
2025-11-07 16:13:47 -03:00

47 lines
1.8 KiB
Rust

use crate::channels::{ChannelAdapter, VoiceAdapter, WebChannelAdapter};
use crate::config::AppConfig;
use crate::llm::LLMProvider;
use crate::session::SessionManager;
use diesel::{Connection, PgConnection};
use aws_sdk_s3::Client as S3Client;
use redis::Client as RedisClient;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::Mutex;
use tokio::sync::mpsc;
use crate::shared::models::BotResponse;
use crate::auth::AuthService;
pub struct AppState {
pub drive: Option<S3Client>,
pub cache: Option<Arc<RedisClient>>,
pub bucket_name: String,
pub config: Option<AppConfig>,
pub conn: Arc<Mutex<PgConnection>>,
pub session_manager: Arc<tokio::sync::Mutex<SessionManager>>,
pub llm_provider: Arc<dyn LLMProvider>,
pub auth_service: Arc<tokio::sync::Mutex<AuthService>>,
pub channels: Arc<Mutex<HashMap<String, Arc<dyn ChannelAdapter>>>>,
pub response_channels: Arc<tokio::sync::Mutex<HashMap<String, mpsc::Sender<BotResponse>>>>,
pub web_adapter: Arc<WebChannelAdapter>,
pub voice_adapter: Arc<VoiceAdapter>,
}
impl Clone for AppState {
fn clone(&self) -> Self {
Self {
drive: self.drive.clone(),
bucket_name: self.bucket_name.clone(),
config: self.config.clone(),
conn: Arc::clone(&self.conn),
cache: self.cache.clone(),
session_manager: Arc::clone(&self.session_manager),
llm_provider: Arc::clone(&self.llm_provider),
auth_service: Arc::clone(&self.auth_service),
channels: Arc::clone(&self.channels),
response_channels: Arc::clone(&self.response_channels),
web_adapter: Arc::clone(&self.web_adapter),
voice_adapter: Arc::clone(&self.voice_adapter),
}
}
}