feat: enhance chat and settings functionality with debugging and new features

- Integrated useRouter for navigation in chat and settings pages, improving user experience.
- Added detailed debug logging for web context generation and chat payloads to assist in troubleshooting.
- Updated settings page to include a hardware dashboard and enhanced sources configuration management.
- Implemented auto-start feature for the application and improved task management with new scheduled task options.
- Refined user interface components for better accessibility and usability across chat and settings pages.
This commit is contained in:
Evandro Fonseca Junior committed 2025-11-20 05:25:34 -03:00
1 parent 8834ef2b4f
commit a44d1bba44
18 files changed
+1692 -223

No files matched your search

+73 -6
View File
@@ -119,6 +119,7 @@ dependencies = [
"sysinfo",
"tauri",
"tauri-build",
"tauri-plugin-autostart",
"tauri-plugin-log",
"tauri-plugin-notification",
"tauri-plugin-shell",
@@ -127,6 +128,7 @@ dependencies = [
"url",
"urlencoding",
"uuid",
"walkdir",
"zip",
]
@@ -296,6 +298,17 @@ version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
[[package]]
name = "auto-launch"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f012b8cc0c850f34117ec8252a44418f2e34a2cf501de89e29b241ae5f79471"
dependencies = [
"dirs 4.0.0",
"thiserror 1.0.69",
"winreg 0.10.1",
]
[[package]]
name = "auto_generate_cdp"
version = "0.4.5"
@@ -1003,13 +1016,33 @@ dependencies = [
"subtle",
]
[[package]]
name = "dirs"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059"
dependencies = [
"dirs-sys 0.3.7",
]
[[package]]
name = "dirs"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3e8aa94d75141228480295a7d0e7feb620b1a5ad9f12bc40be62411e38cce4e"
dependencies = [
"dirs-sys",
"dirs-sys 0.5.0",
]
[[package]]
name = "dirs-sys"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6"
dependencies = [
"libc",
"redox_users 0.4.6",
"winapi",
]
[[package]]
@@ -1020,7 +1053,7 @@ checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab"
dependencies = [
"libc",
"option-ext",
"redox_users",
"redox_users 0.5.2",
"windows-sys 0.61.2",
]
@@ -3772,6 +3805,17 @@ dependencies = [
"bitflags 2.10.0",
]
[[package]]
name = "redox_users"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43"
dependencies = [
"getrandom 0.2.16",
"libredox",
"thiserror 1.0.69",
]
[[package]]
name = "redox_users"
version = "0.5.2"
@@ -4840,7 +4884,7 @@ dependencies = [
"anyhow",
"bytes",
"cookie",
"dirs",
"dirs 6.0.0",
"dunce",
"embed_plist",
"getrandom 0.3.4",
@@ -4890,7 +4934,7 @@ checksum = "87d6f8cafe6a75514ce5333f115b7b1866e8e68d9672bf4ca89fc0f35697ea9d"
dependencies = [
"anyhow",
"cargo_toml",
"dirs",
"dirs 6.0.0",
"glob",
"heck 0.5.0",
"json-patch",
@@ -4962,6 +5006,20 @@ dependencies = [
"walkdir",
]
[[package]]
name = "tauri-plugin-autostart"
version = "2.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "459383cebc193cdd03d1ba4acc40f2c408a7abce419d64bdcd2d745bc2886f70"
dependencies = [
"auto-launch",
"serde",
"serde_json",
"tauri",
"tauri-plugin",
"thiserror 2.0.17",
]
[[package]]
name = "tauri-plugin-log"
version = "2.7.1"
@@ -5514,7 +5572,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3d5572781bee8e3f994d7467084e1b1fd7a93ce66bd480f8156ba89dee55a2b"
dependencies = [
"crossbeam-channel",
"dirs",
"dirs 6.0.0",
"libappindicator",
"muda",
"objc2 0.6.3",
@@ -6526,6 +6584,15 @@ dependencies = [
"memchr",
]
[[package]]
name = "winreg"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d"
dependencies = [
"winapi",
]
[[package]]
name = "winreg"
version = "0.50.0"
@@ -6574,7 +6641,7 @@ dependencies = [
"block2 0.6.2",
"cookie",
"crossbeam-channel",
"dirs",
"dirs 6.0.0",
"dpi",
"dunce",
"gdkx11",
+2
View File
@@ -42,3 +42,5 @@ rand = "0.8"
tokio-cron-scheduler = "0.9"
futures-util = "0.3"
regex = "1.10"
walkdir = "2.5"
tauri-plugin-autostart = "2"
+310 -13
View File
@@ -16,11 +16,14 @@ mod scheduler;
mod ollama_client;
mod task_executor;
mod scheduler_loop;
mod sources_config;
mod system_monitor;
use web_scraper::{ScrapedContent, create_browser, search_and_scrape, search_and_scrape_with_config, scrape_url, SearchConfig};
use headless_chrome::Browser;
use scheduler::{SentinelTask, SchedulerService, SchedulerState, TaskAction};
use ollama_client::OllamaClient;
use sources_config::{SourcesConfig, load_sources_config, save_sources_config};
use system_monitor::{SystemStats, SystemMonitorState};
#[cfg(target_os = "windows")]
use std::os::windows::process::CommandExt;
@@ -63,8 +66,10 @@ struct SystemSpecs {
os_name: String,
}
// SystemStats movido para system_monitor.rs
// Mantendo apenas para compatibilidade com start_system_monitor
#[derive(serde::Serialize, Clone)]
struct SystemStats {
struct LegacySystemStats {
cpu_usage: f32,
memory_used: u64,
memory_total: u64,
@@ -443,7 +448,7 @@ fn start_system_monitor(window: Window) {
let memory_used = sys.used_memory();
let memory_total = sys.total_memory();
let stats = SystemStats {
let stats = LegacySystemStats {
cpu_usage,
memory_used,
memory_total,
@@ -1237,13 +1242,117 @@ fn reset_browser(state: State<'_, BrowserState>) -> Result<(), String> {
Ok(())
}
/// Cleanup explícito do browser (chamado ao fechar app)
/// Força o encerramento apenas de processos Chrome/Chromium headless criados pelo app
/// Seguro: não mata o navegador pessoal do usuário
#[command]
fn cleanup_browser(state: State<'_, BrowserState>) -> Result<(), String> {
let mut browser_opt = state.lock().map_err(|e| format!("Erro ao acessar estado do browser: {}", e))?;
*browser_opt = None;
log::info!("Browser cleanup executado");
Ok(())
fn force_kill_browser() -> Result<u32, String> {
let mut system = System::new_all();
system.refresh_all();
let mut killed_count = 0;
let process_names = vec!["chrome", "chromium", "chromedriver", "headless_shell"];
for (pid, process) in system.processes() {
let name = process.name().to_string_lossy().to_lowercase();
// Verifica se o nome do processo corresponde
if !process_names.iter().any(|&pn| name.contains(pn)) {
continue;
}
// SAFE KILL: Estratégia conservadora para identificar processos headless
// No Windows, tentamos usar wmic para obter a linha de comando completa
#[cfg(target_os = "windows")]
let is_headless = {
use std::process::Command;
// Tenta obter a linha de comando do processo via wmic
let cmd_output = Command::new("wmic")
.args(&["process", "where", &format!("ProcessId={}", pid), "get", "CommandLine", "/format:list"])
.output();
if let Ok(output) = cmd_output {
if let Ok(cmd_str) = String::from_utf8(output.stdout) {
let cmd_lower = cmd_str.to_lowercase();
// Só mata se tiver flags muito específicas de headless
cmd_lower.contains("--headless")
|| cmd_lower.contains("--remote-debugging-port")
|| (cmd_lower.contains("--disable-gpu") && cmd_lower.contains("--no-sandbox"))
} else {
false // Se não conseguir ler, não mata (seguro)
}
} else {
// Se wmic falhar, usa heurística conservadora: só mata se o nome for muito específico
name.contains("headless_shell") || name.contains("chromedriver")
}
};
#[cfg(not(target_os = "windows"))]
let is_headless = {
// No Linux/Mac, tenta ler /proc/PID/cmdline
use std::fs;
if let Ok(cmdline) = fs::read_to_string(format!("/proc/{}/cmdline", pid)) {
let cmd_lower = cmdline.to_lowercase();
cmd_lower.contains("--headless")
|| cmd_lower.contains("--remote-debugging-port")
|| (cmd_lower.contains("--disable-gpu") && cmd_lower.contains("--no-sandbox"))
} else {
// Se não conseguir ler, usa heurística conservadora
name.contains("headless_shell") || name.contains("chromedriver")
}
};
if !is_headless {
log::debug!("Ignorando processo Chrome não-headless: PID {} ({})", pid, name);
continue;
}
// Processo identificado como headless - pode matar com segurança
#[cfg(target_os = "windows")]
{
use std::process::Command;
match Command::new("taskkill")
.args(&["/F", "/PID", &pid.to_string()])
.output()
{
Ok(output) => {
if output.status.success() {
killed_count += 1;
log::info!("Processo Chrome headless encerrado: PID {} ({})", pid, name);
}
}
Err(e) => {
log::warn!("Erro ao encerrar processo {}: {}", pid, e);
}
}
}
#[cfg(not(target_os = "windows"))]
{
use std::process::Command;
match Command::new("kill")
.args(&["-9", &pid.to_string()])
.output()
{
Ok(output) => {
if output.status.success() {
killed_count += 1;
log::info!("Processo Chrome headless encerrado: PID {} ({})", pid, name);
}
}
Err(e) => {
log::warn!("Erro ao encerrar processo {}: {}", pid, e);
}
}
}
}
if killed_count > 0 {
log::info!("Total de {} processos Chrome headless encerrados (seguro)", killed_count);
} else {
log::info!("Nenhum processo Chrome headless encontrado para encerrar");
}
Ok(killed_count)
}
// ========== Storage Management Commands ==========
@@ -1272,7 +1381,6 @@ async fn export_chat_sessions(app_handle: AppHandle) -> Result<String, String> {
let entries = fs::read_dir(&chats_dir)
.map_err(|e| format!("Failed to read chats dir: {}", e))?;
let mut file_count = 0;
for entry in entries {
let entry = entry.map_err(|e| format!("Failed to read entry: {}", e))?;
let path = entry.path();
@@ -1289,8 +1397,6 @@ async fn export_chat_sessions(app_handle: AppHandle) -> Result<String, String> {
.map_err(|e| format!("Failed to add file to ZIP: {}", e))?;
zip.write_all(file_content.as_bytes())
.map_err(|e| format!("Failed to write file to ZIP: {}", e))?;
file_count += 1;
}
}
@@ -1332,6 +1438,186 @@ fn get_app_data_dir(app_handle: AppHandle) -> Result<String, String> {
Ok(format!("{}", app_data_dir.display()))
}
// ========== Sources Config Commands ==========
/// Carrega a configuração de fontes de busca
#[command]
fn load_sources_config_command(app_handle: AppHandle) -> Result<SourcesConfig, String> {
load_sources_config(&app_handle)
}
/// Salva a configuração de fontes de busca
#[command]
fn save_sources_config_command(app_handle: AppHandle, config: SourcesConfig) -> Result<(), String> {
save_sources_config(&app_handle, config)
}
// ========== Export & Backup Commands ==========
/// Exporta todos os dados do app (chats, tasks, sources, settings) para um arquivo ZIP
#[command]
async fn export_all_data(app_handle: AppHandle) -> Result<String, String> {
use walkdir::WalkDir;
let app_data_dir = app_handle.path().app_data_dir()
.map_err(|e| format!("Failed to get app data dir: {}", e))?;
// Criar nome do arquivo com timestamp
let timestamp = Utc::now().format("%Y%m%d_%H%M%S");
let zip_path = app_data_dir.join(format!("ollahub_backup_{}.zip", timestamp));
// Criar arquivo ZIP
let file = fs::File::create(&zip_path)
.map_err(|e| format!("Failed to create ZIP file: {}", e))?;
let mut zip = ZipWriter::new(file);
let options = FileOptions::default()
.compression_method(CompressionMethod::Deflated)
.unix_permissions(0o755);
// 1. Adicionar pasta chats/ inteira
let chats_dir = get_chats_dir(&app_handle)?;
if chats_dir.exists() {
for entry in WalkDir::new(&chats_dir) {
let entry = entry.map_err(|e| format!("Failed to read directory entry: {}", e))?;
let path = entry.path();
if path.is_file() {
// Obter caminho relativo a partir de chats_dir
let relative_path = path.strip_prefix(&chats_dir)
.map_err(|e| format!("Failed to get relative path: {}", e))?;
// Construir caminho no ZIP como "chats/nome_arquivo.json"
let zip_path = format!("chats/{}", relative_path.to_string_lossy().replace('\\', "/"));
let file_content = fs::read(path)
.map_err(|e| format!("Failed to read file {:?}: {}", path, e))?;
zip.start_file(zip_path, options)
.map_err(|e| format!("Failed to add file to ZIP: {}", e))?;
zip.write_all(&file_content)
.map_err(|e| format!("Failed to write file to ZIP: {}", e))?;
}
}
}
// 2. Adicionar tasks.json
let tasks_file = app_data_dir.join("tasks.json");
if tasks_file.exists() {
let tasks_content = fs::read_to_string(&tasks_file)
.map_err(|e| format!("Failed to read tasks.json: {}", e))?;
zip.start_file("tasks.json", options)
.map_err(|e| format!("Failed to add tasks.json to ZIP: {}", e))?;
zip.write_all(tasks_content.as_bytes())
.map_err(|e| format!("Failed to write tasks.json to ZIP: {}", e))?;
}
// 3. Adicionar sources.json
let sources_file = app_data_dir.join("sources.json");
if sources_file.exists() {
let sources_content = fs::read_to_string(&sources_file)
.map_err(|e| format!("Failed to read sources.json: {}", e))?;
zip.start_file("sources.json", options)
.map_err(|e| format!("Failed to add sources.json to ZIP: {}", e))?;
zip.write_all(sources_content.as_bytes())
.map_err(|e| format!("Failed to write sources.json to ZIP: {}", e))?;
} else {
// Se não existir, criar um sources.json padrão no ZIP
let default_config = SourcesConfig::default();
let default_json = serde_json::to_string_pretty(&default_config)
.map_err(|e| format!("Failed to serialize default sources config: {}", e))?;
zip.start_file("sources.json", options)
.map_err(|e| format!("Failed to add default sources.json to ZIP: {}", e))?;
zip.write_all(default_json.as_bytes())
.map_err(|e| format!("Failed to write default sources.json to ZIP: {}", e))?;
}
// 4. Adicionar settings.json (se existir)
let settings_file = app_data_dir.join("settings.json");
if settings_file.exists() {
let settings_content = fs::read_to_string(&settings_file)
.map_err(|e| format!("Failed to read settings.json: {}", e))?;
zip.start_file("settings.json", options)
.map_err(|e| format!("Failed to add settings.json to ZIP: {}", e))?;
zip.write_all(settings_content.as_bytes())
.map_err(|e| format!("Failed to write settings.json to ZIP: {}", e))?;
}
// Finalizar ZIP
zip.finish()
.map_err(|e| format!("Failed to finalize ZIP: {}", e))?;
log::info!("Backup completo exportado para: {}", zip_path.display());
Ok(format!("{}", zip_path.display()))
}
// ========== Logs Commands ==========
/// Obtém as últimas N linhas dos logs do sistema
#[command]
fn get_recent_logs(app_handle: AppHandle, lines: usize) -> Result<Vec<String>, String> {
// O tauri-plugin-log geralmente salva logs em app_data_dir/logs/
let app_data_dir = app_handle.path().app_data_dir()
.map_err(|e| format!("Failed to get app data dir: {}", e))?;
let logs_dir = app_data_dir.join("logs");
// Se o diretório de logs não existir, retornar vazio
if !logs_dir.exists() {
return Ok(Vec::new());
}
// Procurar pelo arquivo de log mais recente
let mut log_files: Vec<_> = fs::read_dir(&logs_dir)
.map_err(|e| format!("Failed to read logs directory: {}", e))?
.filter_map(|entry| {
entry.ok().and_then(|e| {
let path = e.path();
if path.is_file() && path.extension().and_then(|s| s.to_str()) == Some("log") {
Some((path, e.metadata().ok()?.modified().ok()?))
} else {
None
}
})
})
.collect();
// Ordenar por data de modificação (mais recente primeiro)
log_files.sort_by(|a, b| b.1.cmp(&a.1));
// Ler o arquivo mais recente
if let Some((log_file_path, _)) = log_files.first() {
let content = fs::read_to_string(log_file_path)
.map_err(|e| format!("Failed to read log file: {}", e))?;
// Dividir em linhas e pegar as últimas N
let all_lines: Vec<String> = content.lines().map(|s| s.to_string()).collect();
let total_lines = all_lines.len();
let start = if total_lines > lines { total_lines - lines } else { 0 };
Ok(all_lines[start..].to_vec())
} else {
Ok(Vec::new())
}
}
// ========== System Monitor Commands ==========
/// Obtém estatísticas do sistema em tempo real
#[command]
fn get_system_stats(
monitor_state: State<'_, Arc<Mutex<SystemMonitorState>>>,
) -> Result<SystemStats, String> {
let mut monitor = monitor_state.lock()
.map_err(|e| format!("Failed to lock monitor state: {}", e))?;
Ok(monitor.get_stats())
}
// ========== Task Scheduler Commands ==========
#[command]
@@ -1447,7 +1733,8 @@ pub fn run() {
let scheduler_clone = scheduler_state.clone();
// BrowserState não é mais necessário - o scheduler criará o browser quando necessário
tokio::spawn(async move {
// Usar o runtime async do Tauri ao invés de tokio::spawn
tauri::async_runtime::spawn(async move {
if let Err(e) = scheduler_loop::start_scheduler_loop(
app_handle,
scheduler_clone,
@@ -1461,6 +1748,10 @@ pub fn run() {
// Adicionar scheduler ao manage
app.manage(scheduler_state.clone());
// Inicializar System Monitor State
let monitor_state: Arc<Mutex<SystemMonitorState>> = Arc::new(Mutex::new(SystemMonitorState::new()));
app.manage(monitor_state);
Ok(())
})
.manage(Arc::new(Mutex::new(None::<Arc<Browser>>)) as BrowserState)
@@ -1495,9 +1786,15 @@ pub fn run() {
search_and_extract_content,
extract_url_content,
reset_browser,
force_kill_browser,
export_chat_sessions,
export_all_data,
clear_chat_history,
get_app_data_dir,
load_sources_config_command,
save_sources_config_command,
get_recent_logs,
get_system_stats,
create_task,
list_tasks,
update_task,
+1 -2
View File
@@ -1,5 +1,4 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
/// Mensagem para o Ollama API
#[derive(Debug, Serialize)]
@@ -97,7 +96,7 @@ impl OllamaClient {
};
let url = format!("{}/api/chat", self.base_url);
let mut response = self.client
let response = self.client
.post(&url)
.json(&request)
.send()
+2 -2
View File
@@ -6,7 +6,6 @@ use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::Mutex;
use tauri::{AppHandle, Manager};
use uuid::Uuid;
/// Tipos de ações que uma task pode executar
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
@@ -49,7 +48,8 @@ pub type SchedulerState = Arc<Mutex<SchedulerService>>;
pub struct SchedulerService {
tasks: HashMap<String, SentinelTask>,
tasks_file: PathBuf,
app_handle: Option<AppHandle>,
#[allow(dead_code)]
app_handle: Option<AppHandle>, // Mantido para uso futuro (notificações, etc)
}
impl SchedulerService {
+1 -1
View File
@@ -1,4 +1,4 @@
use crate::scheduler::{SchedulerService, SchedulerState};
use crate::scheduler::SchedulerState;
use crate::task_executor::execute_task;
use tokio_cron_scheduler::{Job, JobScheduler};
use std::sync::Arc;
+154
View File
@@ -0,0 +1,154 @@
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
use tauri::{AppHandle, Manager};
use chrono::Utc;
/// Categoria de fonte de busca
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SourceCategory {
pub id: String,
pub name: String,
pub base_sites: Vec<String>,
pub enabled: bool,
}
/// Configuração completa de fontes de busca
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SourcesConfig {
#[serde(default = "default_version")]
pub version: u32,
pub categories: Vec<SourceCategory>,
#[serde(default = "default_last_updated")]
pub last_updated: String,
}
fn default_version() -> u32 {
1
}
fn default_last_updated() -> String {
Utc::now().to_rfc3339()
}
impl Default for SourcesConfig {
fn default() -> Self {
Self {
version: 1,
last_updated: Utc::now().to_rfc3339(),
categories: vec![
SourceCategory {
id: "academico".to_string(),
name: "Acadêmico".to_string(),
base_sites: vec![
"scholar.google.com".to_string(),
"arxiv.org".to_string(),
"pubmed.ncbi.nlm.nih.gov".to_string(),
"ieee.org".to_string(),
"acm.org".to_string(),
],
enabled: true,
},
SourceCategory {
id: "tech".to_string(),
name: "Tech".to_string(),
base_sites: vec![
"github.com".to_string(),
"stackoverflow.com".to_string(),
"dev.to".to_string(),
"medium.com".to_string(),
"reddit.com/r/programming".to_string(),
],
enabled: true,
},
SourceCategory {
id: "news".to_string(),
name: "News".to_string(),
base_sites: vec![
"news.ycombinator.com".to_string(),
"techcrunch.com".to_string(),
"theverge.com".to_string(),
"arstechnica.com".to_string(),
],
enabled: true,
},
SourceCategory {
id: "financeiro".to_string(),
name: "Financeiro".to_string(),
base_sites: vec![
"bloomberg.com".to_string(),
"reuters.com".to_string(),
"financialtimes.com".to_string(),
"wsj.com".to_string(),
],
enabled: true,
},
],
}
}
}
/// Helper para obter o caminho do arquivo sources.json
pub fn get_sources_config_path(app_handle: &AppHandle) -> Result<PathBuf, String> {
let app_data_dir = app_handle
.path()
.app_data_dir()
.map_err(|e| format!("Failed to get app data dir: {}", e))?;
Ok(app_data_dir.join("sources.json"))
}
/// Carrega a configuração de fontes do arquivo
/// Se o arquivo não existir, retorna uma configuração padrão robusta
pub fn load_sources_config(app_handle: &AppHandle) -> Result<SourcesConfig, String> {
let config_path = get_sources_config_path(app_handle)?;
// Se o arquivo não existir, retornar Default
if !config_path.exists() {
log::info!("sources.json não encontrado, usando configuração padrão");
return Ok(SourcesConfig::default());
}
// Tentar ler e parsear o arquivo
let content = fs::read_to_string(&config_path)
.map_err(|e| format!("Failed to read sources.json: {}", e))?;
let config: SourcesConfig = serde_json::from_str(&content)
.map_err(|e| format!("Failed to parse sources.json: {}", e))?;
Ok(config)
}
/// Salva a configuração de fontes no arquivo
pub fn save_sources_config(app_handle: &AppHandle, config: SourcesConfig) -> Result<(), String> {
let config_path = get_sources_config_path(app_handle)?;
// Garantir que o diretório pai existe
if let Some(parent) = config_path.parent() {
if !parent.exists() {
fs::create_dir_all(parent)
.map_err(|e| format!("Failed to create config directory: {}", e))?;
}
}
// Atualizar timestamp
let mut config_to_save = config;
config_to_save.last_updated = Utc::now().to_rfc3339();
// Serializar e escrever
let json = serde_json::to_string_pretty(&config_to_save)
.map_err(|e| format!("Failed to serialize sources config: {}", e))?;
// Escrever em arquivo temporário primeiro, depois renomear (atomic write)
let temp_path = config_path.with_extension("json.tmp");
fs::write(&temp_path, json)
.map_err(|e| format!("Failed to write temp sources config file: {}", e))?;
// Renomear atomicamente
fs::rename(&temp_path, &config_path)
.map_err(|e| format!("Failed to rename temp file to sources.json: {}", e))?;
log::info!("Sources config salvo com sucesso em {:?}", config_path);
Ok(())
}
+153
View File
@@ -0,0 +1,153 @@
use serde::Serialize;
use sysinfo::System;
/// Estatísticas do sistema em tempo real
#[derive(Serialize, Clone, Debug)]
pub struct SystemStats {
pub cpu_usage: f32,
pub ram_used: u64,
pub ram_total: u64,
pub ram_percent: f32,
pub gpu_name: Option<String>,
pub uptime: u64,
pub processes_count: usize,
pub cpu_name: String,
}
/// Estado persistente do sistema para cálculo de CPU
pub struct SystemMonitorState {
system: System,
last_cpu_check: std::time::Instant,
}
impl SystemMonitorState {
pub fn new() -> Self {
let mut system = System::new_all();
system.refresh_all();
Self {
system,
last_cpu_check: std::time::Instant::now(),
}
}
pub fn get_stats(&mut self) -> SystemStats {
// Refresh system info
self.system.refresh_all();
// Refresh CPU para cálculo preciso
self.system.refresh_cpu_all();
// Pequeno delay para cálculo preciso de CPU
std::thread::sleep(std::time::Duration::from_millis(100));
self.system.refresh_cpu_all();
// CPU usage global
let cpu_usage = self.system.global_cpu_usage();
// RAM
let ram_total = self.system.total_memory();
let ram_used = self.system.used_memory();
let ram_percent = if ram_total > 0 {
(ram_used as f32 / ram_total as f32) * 100.0
} else {
0.0
};
// CPU Name
let cpu_name = self.system
.cpus()
.first()
.map(|cpu| cpu.name().to_string())
.unwrap_or_else(|| "Unknown CPU".to_string());
// GPU Name (tentativa básica - sysinfo não tem suporte direto)
let gpu_name = detect_gpu_name();
// Uptime do sistema (em segundos desde o boot)
let boot_time = System::boot_time();
let uptime = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
.saturating_sub(boot_time);
// Contagem de processos
let processes_count = self.system.processes().len();
SystemStats {
cpu_usage,
ram_used,
ram_total,
ram_percent,
gpu_name,
uptime,
processes_count,
cpu_name,
}
}
}
/// Tenta detectar o nome da GPU (implementação básica)
fn detect_gpu_name() -> Option<String> {
#[cfg(target_os = "windows")]
{
use std::process::Command;
// Tenta usar wmic para detectar GPU no Windows
if let Ok(output) = Command::new("wmic")
.args(&["path", "win32_VideoController", "get", "name"])
.output()
{
if let Ok(stdout) = String::from_utf8(output.stdout) {
for line in stdout.lines() {
let line = line.trim();
if !line.is_empty() && line != "Name" {
return Some(line.to_string());
}
}
}
}
}
#[cfg(target_os = "linux")]
{
use std::process::Command;
// Tenta usar lspci no Linux
if let Ok(output) = Command::new("lspci")
.args(&["-v", "-s", "$(lspci | grep VGA | cut -d' ' -f1)"])
.output()
{
if let Ok(stdout) = String::from_utf8(output.stdout) {
for line in stdout.lines() {
if line.contains("VGA") || line.contains("Display") {
return Some(line.to_string());
}
}
}
}
}
#[cfg(target_os = "macos")]
{
use std::process::Command;
// Tenta usar system_profiler no macOS
if let Ok(output) = Command::new("system_profiler")
.args(&["SPDisplaysDataType"])
.output()
{
if let Ok(stdout) = String::from_utf8(output.stdout) {
for line in stdout.lines() {
if line.contains("Chipset Model") {
return Some(line.replace("Chipset Model:", "").trim().to_string());
}
}
}
}
}
None
}
-2
View File
@@ -103,7 +103,6 @@ async fn execute_search_and_summarize(
// 5. Salvar como sessão de chat
let session_id = uuid::Uuid::new_v4().to_string();
let now = Utc::now();
let messages = vec![
Message {
@@ -176,7 +175,6 @@ fn save_task_session_internal(
) -> Result<(), String> {
let chats_dir = get_chats_dir(app_handle)?;
let file_path = chats_dir.join(format!("{}.json", session_id));
let file_path = chats_dir.join(format!("{}.json", session_id));
let now = Utc::now();