use std::sync::Arc;
use sea_orm::DatabaseConnection;
use crate::{
llm_tools::LlmToolsCapability, plugins::filesystem::storage::DynFilestore,
rune_env::RuneEnvCapability,
};
use super::config::LlmAssistantConfig;
use super::email_listener::EmailListenerHandle;
use super::genai::GenaiClient;
use super::live_turn::LiveTurns;
use super::preferences::{resolved_api_key, resolved_chat_model};
#[derive(Clone)]
pub struct EmailAutomationDeps {
pub store: Arc<DynFilestore>,
pub tools: Arc<LlmToolsCapability>,
pub rune_env: Arc<RuneEnvCapability>,
}
#[derive(Clone)]
pub struct LlmAssistantState {
pub db: DatabaseConnection,
pub config: LlmAssistantConfig,
pub genai: GenaiClient,
pub live_turns: LiveTurns,
pub email_automation: EmailAutomationDeps,
pub email_listener: EmailListenerHandle,
}
impl LlmAssistantState {
pub fn new(
db: DatabaseConnection,
config: LlmAssistantConfig,
email_automation: EmailAutomationDeps,
) -> Self {
let chat_model = config.chat_model.clone();
Self {
db,
config,
genai: GenaiClient::new(String::new(), chat_model),
live_turns: LiveTurns::new(),
email_automation,
email_listener: super::email_listener::new_handle(),
}
}
pub fn bind_email_listener(self) -> Self {
let state = Arc::new(self);
state.email_listener.bind(Arc::clone(&state));
Arc::try_unwrap(state).unwrap_or_else(|arc| (*arc).clone())
}
pub async fn genai_with_key(&self) -> Result<GenaiClient, sea_orm::DbErr> {
let key = resolved_api_key(&self.db).await?;
let model = resolved_chat_model(&self.db, &self.config.chat_model).await?;
Ok(self.genai.with_api_key(key).with_model(model))
}
}