2025-10-07 09:08:53 -03:00
|
|
|
#![allow(dead_code)]
|
|
|
|
|
|
2025-10-06 19:12:13 -03:00
|
|
|
use actix_cors::Cors;
|
2025-10-07 07:16:03 -03:00
|
|
|
use actix_web::middleware::Logger;
|
2025-10-06 20:06:43 -03:00
|
|
|
use actix_web::{web, App, HttpServer};
|
2025-10-06 19:12:13 -03:00
|
|
|
use dotenv::dotenv;
|
|
|
|
|
use log::info;
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
2025-10-06 10:30:17 -03:00
|
|
|
mod auth;
|
|
|
|
|
mod automation;
|
|
|
|
|
mod basic;
|
|
|
|
|
mod bot;
|
|
|
|
|
mod channels;
|
|
|
|
|
mod chart;
|
|
|
|
|
mod config;
|
|
|
|
|
mod context;
|
|
|
|
|
mod email;
|
|
|
|
|
mod file;
|
|
|
|
|
mod llm;
|
2025-10-06 20:06:43 -03:00
|
|
|
mod llm_legacy;
|
2025-10-06 10:30:17 -03:00
|
|
|
mod org;
|
|
|
|
|
mod session;
|
|
|
|
|
mod shared;
|
|
|
|
|
mod tools;
|
|
|
|
|
mod web_automation;
|
|
|
|
|
mod whatsapp;
|
|
|
|
|
|
2025-10-07 07:16:03 -03:00
|
|
|
use crate::automation::AutomationService;
|
2025-10-06 20:06:43 -03:00
|
|
|
use crate::bot::{
|
|
|
|
|
create_session, get_session_history, get_sessions, index, set_mode_handler, static_files,
|
|
|
|
|
voice_start, voice_stop, websocket_handler, whatsapp_webhook, whatsapp_webhook_verify,
|
|
|
|
|
};
|
|
|
|
|
use crate::channels::{VoiceAdapter, WebChannelAdapter};
|
|
|
|
|
use crate::config::AppConfig;
|
2025-10-07 07:16:03 -03:00
|
|
|
use crate::email::{
|
|
|
|
|
get_emails, get_latest_email_from, list_emails, save_click, save_draft, send_email,
|
|
|
|
|
};
|
2025-10-06 20:49:38 -03:00
|
|
|
use crate::file::{list_file, upload_file};
|
2025-10-07 07:16:03 -03:00
|
|
|
use crate::llm_legacy::llm_generic::generic_chat_completions;
|
|
|
|
|
use crate::llm_legacy::llm_local::{
|
|
|
|
|
chat_completions_local, embeddings_local, ensure_llama_servers_running,
|
|
|
|
|
};
|
2025-10-06 20:49:38 -03:00
|
|
|
use crate::shared::AppState;
|
2025-10-06 20:06:43 -03:00
|
|
|
use crate::whatsapp::WhatsAppAdapter;
|
2025-10-06 10:30:17 -03:00
|
|
|
|
2025-10-06 19:12:13 -03:00
|
|
|
#[actix_web::main]
|
2025-10-06 10:30:17 -03:00
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
|
|
dotenv().ok();
|
2025-10-07 07:16:03 -03:00
|
|
|
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
|
2025-10-06 19:12:13 -03:00
|
|
|
|
2025-10-07 07:16:03 -03:00
|
|
|
info!("Starting General Bots 6.0...");
|
2025-10-06 10:30:17 -03:00
|
|
|
|
|
|
|
|
let config = AppConfig::from_env();
|
2025-10-06 20:49:38 -03:00
|
|
|
|
|
|
|
|
// Main database pool (required)
|
2025-10-06 20:06:43 -03:00
|
|
|
let db_pool = match sqlx::postgres::PgPool::connect(&config.database_url()).await {
|
|
|
|
|
Ok(pool) => {
|
|
|
|
|
info!("Connected to main database");
|
|
|
|
|
pool
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
log::error!("Failed to connect to main database: {}", e);
|
|
|
|
|
return Err(std::io::Error::new(
|
|
|
|
|
std::io::ErrorKind::ConnectionRefused,
|
|
|
|
|
format!("Database connection failed: {}", e),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-10-06 10:30:17 -03:00
|
|
|
|
2025-10-06 20:49:38 -03:00
|
|
|
// Optional custom database pool
|
2025-10-07 07:16:03 -03:00
|
|
|
let db_custom_pool = match sqlx::postgres::PgPool::connect(&config.database_custom_url()).await
|
|
|
|
|
{
|
|
|
|
|
Ok(pool) => {
|
|
|
|
|
info!("Connected to custom database");
|
|
|
|
|
Some(pool)
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
log::warn!("Failed to connect to custom database: {}", e);
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-10-06 10:30:17 -03:00
|
|
|
|
2025-10-06 20:49:38 -03:00
|
|
|
// Optional Redis client
|
2025-10-06 20:06:43 -03:00
|
|
|
let redis_client = match redis::Client::open("redis://127.0.0.1/") {
|
|
|
|
|
Ok(client) => {
|
|
|
|
|
info!("Connected to Redis");
|
|
|
|
|
Some(Arc::new(client))
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
log::warn!("Failed to connect to Redis: {}", e);
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-10-06 10:30:17 -03:00
|
|
|
|
2025-10-07 07:16:03 -03:00
|
|
|
// Initialize MinIO client
|
|
|
|
|
let minio_client = file::init_minio(&config)
|
|
|
|
|
.await
|
|
|
|
|
.expect("Failed to initialize Minio");
|
|
|
|
|
|
|
|
|
|
// Initialize browser pool
|
|
|
|
|
let browser_pool = Arc::new(web_automation::BrowserPool::new(
|
|
|
|
|
"chrome".to_string(),
|
|
|
|
|
2,
|
|
|
|
|
"headless".to_string(),
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
// Initialize LLM servers
|
|
|
|
|
ensure_llama_servers_running()
|
|
|
|
|
.await
|
|
|
|
|
.expect("Failed to initialize LLM local server.");
|
2025-10-06 10:30:17 -03:00
|
|
|
|
2025-10-07 07:16:03 -03:00
|
|
|
web_automation::initialize_browser_pool()
|
|
|
|
|
.await
|
|
|
|
|
.expect("Failed to initialize browser pool");
|
|
|
|
|
|
|
|
|
|
// Initialize services from new architecture
|
2025-10-06 20:06:43 -03:00
|
|
|
let auth_service = auth::AuthService::new(db_pool.clone(), redis_client.clone());
|
|
|
|
|
let session_manager = session::SessionManager::new(db_pool.clone(), redis_client.clone());
|
2025-10-06 20:49:38 -03:00
|
|
|
|
2025-10-06 19:12:13 -03:00
|
|
|
let tool_manager = tools::ToolManager::new();
|
2025-10-06 20:06:43 -03:00
|
|
|
let llm_provider = Arc::new(llm::MockLLMProvider::new());
|
2025-10-06 20:49:38 -03:00
|
|
|
|
|
|
|
|
let orchestrator =
|
|
|
|
|
bot::BotOrchestrator::new(session_manager, tool_manager, llm_provider, auth_service);
|
2025-10-06 10:30:17 -03:00
|
|
|
|
2025-10-06 20:06:43 -03:00
|
|
|
let web_adapter = Arc::new(WebChannelAdapter::new());
|
|
|
|
|
let voice_adapter = Arc::new(VoiceAdapter::new(
|
2025-10-06 19:12:13 -03:00
|
|
|
"https://livekit.example.com".to_string(),
|
|
|
|
|
"api_key".to_string(),
|
|
|
|
|
"api_secret".to_string(),
|
2025-10-06 10:30:17 -03:00
|
|
|
));
|
2025-10-06 20:49:38 -03:00
|
|
|
|
2025-10-06 20:06:43 -03:00
|
|
|
let whatsapp_adapter = Arc::new(WhatsAppAdapter::new(
|
2025-10-06 19:12:13 -03:00
|
|
|
"whatsapp_token".to_string(),
|
|
|
|
|
"phone_number_id".to_string(),
|
|
|
|
|
"verify_token".to_string(),
|
|
|
|
|
));
|
2025-10-06 20:49:38 -03:00
|
|
|
|
2025-10-06 20:06:43 -03:00
|
|
|
let tool_api = Arc::new(tools::ToolApi::new());
|
2025-10-06 10:30:17 -03:00
|
|
|
|
2025-10-07 07:16:03 -03:00
|
|
|
// Create unified app state
|
2025-10-06 19:12:13 -03:00
|
|
|
let app_state = AppState {
|
2025-10-07 07:16:03 -03:00
|
|
|
minio_client: Some(minio_client),
|
2025-10-06 20:06:43 -03:00
|
|
|
config: Some(config.clone()),
|
|
|
|
|
db: Some(db_pool.clone()),
|
2025-10-07 07:16:03 -03:00
|
|
|
db_custom: db_custom_pool.clone(),
|
|
|
|
|
browser_pool: browser_pool.clone(),
|
2025-10-06 20:06:43 -03:00
|
|
|
orchestrator: Arc::new(orchestrator),
|
2025-10-06 10:30:17 -03:00
|
|
|
web_adapter,
|
|
|
|
|
voice_adapter,
|
|
|
|
|
whatsapp_adapter,
|
2025-10-06 19:12:13 -03:00
|
|
|
tool_api,
|
|
|
|
|
};
|
2025-10-06 10:30:17 -03:00
|
|
|
|
2025-10-07 07:16:03 -03:00
|
|
|
// Start automation service in background
|
|
|
|
|
let automation_state = app_state.clone();
|
|
|
|
|
let automation = AutomationService::new(automation_state, "src/prompts");
|
|
|
|
|
let _automation_handle = automation.spawn();
|
|
|
|
|
|
2025-10-06 20:49:38 -03:00
|
|
|
info!(
|
|
|
|
|
"Starting server on {}:{}",
|
|
|
|
|
config.server.host, config.server.port
|
|
|
|
|
);
|
2025-10-06 10:30:17 -03:00
|
|
|
|
|
|
|
|
HttpServer::new(move || {
|
2025-10-06 19:12:13 -03:00
|
|
|
let cors = Cors::default()
|
|
|
|
|
.allow_any_origin()
|
|
|
|
|
.allow_any_method()
|
|
|
|
|
.allow_any_header()
|
|
|
|
|
.max_age(3600);
|
|
|
|
|
|
2025-10-06 10:30:17 -03:00
|
|
|
App::new()
|
2025-10-06 19:12:13 -03:00
|
|
|
.wrap(cors)
|
2025-10-07 07:16:03 -03:00
|
|
|
.wrap(Logger::default())
|
|
|
|
|
.wrap(Logger::new("HTTP REQUEST: %a %{User-Agent}i"))
|
2025-10-06 20:06:43 -03:00
|
|
|
.app_data(web::Data::new(app_state.clone()))
|
2025-10-07 07:16:03 -03:00
|
|
|
// Legacy services
|
|
|
|
|
.service(upload_file)
|
|
|
|
|
.service(list_file)
|
|
|
|
|
.service(save_click)
|
|
|
|
|
.service(get_emails)
|
|
|
|
|
.service(list_emails)
|
|
|
|
|
.service(send_email)
|
|
|
|
|
.service(chat_completions_local)
|
|
|
|
|
.service(save_draft)
|
|
|
|
|
.service(generic_chat_completions)
|
|
|
|
|
.service(embeddings_local)
|
|
|
|
|
.service(get_latest_email_from)
|
|
|
|
|
// New bot services
|
2025-10-06 20:06:43 -03:00
|
|
|
.service(index)
|
|
|
|
|
.service(static_files)
|
|
|
|
|
.service(websocket_handler)
|
|
|
|
|
.service(whatsapp_webhook_verify)
|
|
|
|
|
.service(whatsapp_webhook)
|
|
|
|
|
.service(voice_start)
|
|
|
|
|
.service(voice_stop)
|
|
|
|
|
.service(create_session)
|
|
|
|
|
.service(get_sessions)
|
|
|
|
|
.service(get_session_history)
|
|
|
|
|
.service(set_mode_handler)
|
2025-10-06 10:30:17 -03:00
|
|
|
})
|
2025-10-06 20:06:43 -03:00
|
|
|
.bind((config.server.host.clone(), config.server.port))?
|
2025-10-06 10:30:17 -03:00
|
|
|
.run()
|
|
|
|
|
.await
|
|
|
|
|
}
|