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-11 12:29:03 -03:00
|
|
|
use dotenvy::dotenv;
|
2025-10-06 19:12:13 -03:00
|
|
|
use log::info;
|
2025-10-12 20:12:49 -03:00
|
|
|
use std::collections::HashMap;
|
2025-10-11 20:02:14 -03:00
|
|
|
use std::sync::{Arc, Mutex};
|
2025-10-06 19:12:13 -03:00
|
|
|
|
2025-10-06 10:30:17 -03:00
|
|
|
mod auth;
|
|
|
|
|
mod automation;
|
|
|
|
|
mod basic;
|
|
|
|
|
mod bot;
|
|
|
|
|
mod channels;
|
|
|
|
|
mod config;
|
|
|
|
|
mod context;
|
2025-10-07 10:53:09 -03:00
|
|
|
#[cfg(feature = "email")]
|
2025-10-06 10:30:17 -03:00
|
|
|
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 whatsapp;
|
|
|
|
|
|
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 10:53:09 -03:00
|
|
|
#[cfg(feature = "email")]
|
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-11 20:02:14 -03:00
|
|
|
use crate::file::upload_file;
|
2025-10-07 07:16:03 -03:00
|
|
|
use crate::llm_legacy::llm_local::{
|
|
|
|
|
chat_completions_local, embeddings_local, ensure_llama_servers_running,
|
|
|
|
|
};
|
2025-10-11 20:02:14 -03:00
|
|
|
use crate::shared::state::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
|
|
|
|
2025-10-11 20:02:14 -03:00
|
|
|
let cfg = AppConfig::from_env();
|
|
|
|
|
let config = std::sync::Arc::new(cfg.clone());
|
2025-10-06 20:49:38 -03:00
|
|
|
|
2025-10-11 20:02:14 -03:00
|
|
|
let db_pool = match diesel::Connection::establish(&cfg.database_url()) {
|
2025-10-11 12:29:03 -03:00
|
|
|
Ok(conn) => {
|
2025-10-06 20:06:43 -03:00
|
|
|
info!("Connected to main database");
|
2025-10-11 12:29:03 -03:00
|
|
|
Arc::new(Mutex::new(conn))
|
2025-10-06 20:06:43 -03:00
|
|
|
}
|
|
|
|
|
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-11 20:02:14 -03:00
|
|
|
let custom_db_url = format!(
|
|
|
|
|
"postgres://{}:{}@{}:{}/{}",
|
|
|
|
|
cfg.database_custom.username,
|
|
|
|
|
cfg.database_custom.password,
|
|
|
|
|
cfg.database_custom.server,
|
|
|
|
|
cfg.database_custom.port,
|
|
|
|
|
cfg.database_custom.database
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let db_custom_pool = match diesel::Connection::establish(&custom_db_url) {
|
|
|
|
|
Ok(conn) => {
|
|
|
|
|
info!("Connected to custom database using constructed URL");
|
|
|
|
|
Arc::new(Mutex::new(conn))
|
|
|
|
|
}
|
|
|
|
|
Err(e2) => {
|
|
|
|
|
log::error!("Failed to connect to custom database: {}", e2);
|
|
|
|
|
return Err(std::io::Error::new(
|
|
|
|
|
std::io::ErrorKind::ConnectionRefused,
|
|
|
|
|
format!("Custom Database connection failed: {}", e2),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ensure_llama_servers_running()
|
|
|
|
|
.await
|
|
|
|
|
.expect("Failed to initialize LLM local server.");
|
|
|
|
|
|
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-11 20:02:14 -03:00
|
|
|
let tool_manager = Arc::new(tools::ToolManager::new());
|
2025-10-12 20:54:42 -03:00
|
|
|
let llama_url =
|
|
|
|
|
std::env::var("LLM_URL").unwrap_or_else(|_| "http://localhost:8081".to_string());
|
|
|
|
|
|
2025-10-12 20:12:49 -03:00
|
|
|
let llm_provider = Arc::new(crate::llm::OpenAIClient::new(
|
2025-10-12 17:41:41 -03:00
|
|
|
"empty".to_string(),
|
2025-10-12 20:54:42 -03:00
|
|
|
Some(llama_url.clone()),
|
2025-10-12 17:41:41 -03:00
|
|
|
));
|
2025-10-06 20:49:38 -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-12 20:12:49 -03:00
|
|
|
let session_manager = Arc::new(tokio::sync::Mutex::new(session::SessionManager::new(
|
|
|
|
|
diesel::Connection::establish(&cfg.database_url()).unwrap(),
|
|
|
|
|
redis_client.clone(),
|
|
|
|
|
)));
|
|
|
|
|
|
|
|
|
|
let auth_service = Arc::new(tokio::sync::Mutex::new(auth::AuthService::new(
|
|
|
|
|
diesel::Connection::establish(&cfg.database_url()).unwrap(),
|
|
|
|
|
redis_client.clone(),
|
|
|
|
|
)));
|
|
|
|
|
|
|
|
|
|
let app_state = Arc::new(AppState {
|
2025-10-11 12:29:03 -03:00
|
|
|
s3_client: None,
|
2025-10-11 20:02:14 -03:00
|
|
|
config: Some(cfg.clone()),
|
|
|
|
|
conn: db_pool.clone(),
|
|
|
|
|
custom_conn: db_custom_pool.clone(),
|
2025-10-11 12:29:03 -03:00
|
|
|
redis_client: redis_client.clone(),
|
2025-10-12 20:12:49 -03:00
|
|
|
session_manager: session_manager.clone(),
|
|
|
|
|
tool_manager: tool_manager.clone(),
|
|
|
|
|
llm_provider: llm_provider.clone(),
|
|
|
|
|
auth_service: auth_service.clone(),
|
|
|
|
|
channels: Arc::new(Mutex::new(HashMap::new())),
|
|
|
|
|
response_channels: Arc::new(tokio::sync::Mutex::new(HashMap::new())),
|
|
|
|
|
web_adapter: web_adapter.clone(),
|
|
|
|
|
voice_adapter: voice_adapter.clone(),
|
|
|
|
|
whatsapp_adapter: whatsapp_adapter.clone(),
|
|
|
|
|
tool_api: tool_api.clone(),
|
|
|
|
|
});
|
2025-10-06 10:30:17 -03:00
|
|
|
|
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-11 20:02:14 -03:00
|
|
|
let app_state_clone = app_state.clone();
|
|
|
|
|
|
2025-10-11 12:29:03 -03:00
|
|
|
let mut app = 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-12 20:12:49 -03:00
|
|
|
.app_data(web::Data::from(app_state_clone));
|
2025-10-11 20:02:14 -03:00
|
|
|
|
|
|
|
|
app = app
|
2025-10-07 07:16:03 -03:00
|
|
|
.service(upload_file)
|
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)
|
2025-10-11 20:02:14 -03:00
|
|
|
.service(set_mode_handler)
|
|
|
|
|
.service(chat_completions_local)
|
|
|
|
|
.service(embeddings_local);
|
2025-10-07 10:53:09 -03:00
|
|
|
|
|
|
|
|
#[cfg(feature = "email")]
|
|
|
|
|
{
|
|
|
|
|
app = app
|
|
|
|
|
.service(get_latest_email_from)
|
|
|
|
|
.service(get_emails)
|
|
|
|
|
.service(list_emails)
|
|
|
|
|
.service(send_email)
|
2025-10-11 20:02:14 -03:00
|
|
|
.service(save_draft)
|
|
|
|
|
.service(save_click);
|
2025-10-07 10:53:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app
|
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
|
|
|
|
|
}
|