From 9175b7a247d5edd4bbeb6ba28ca6d5f7ab7c39b6 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Tue, 19 May 2026 07:25:34 -0700 Subject: [PATCH] =?UTF-8?q?feat(m2):=20add=20chat=20Tauri=20command=20?= =?UTF-8?q?=E2=80=94=20spawns=20Ollama=20stream,=20emits=20chat-token/done?= =?UTF-8?q?/error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/commands.rs | 49 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index cf96e71..42ad589 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -1,6 +1,6 @@ use crate::config::{MigrationConfig, NotificationLevel, RobinConfig, SourceOs}; use std::sync::Mutex; -use tauri::State; +use tauri::{Emitter, State}; pub struct AppState { pub config: Mutex, @@ -79,3 +79,50 @@ pub fn panel_opened() { pub fn panel_closed() { crate::notify::set_panel_open(false); } + +#[tauri::command] +pub async fn chat( + message: String, + state: State<'_, AppState>, + app_handle: tauri::AppHandle, +) -> Result<(), String> { + let (base_url, model, source_os, distro) = { + let cfg = state.config.lock().map_err(|e| e.to_string())?; + let base_url = cfg.ollama.base_url.clone(); + let model = cfg.ollama.model.clone(); + let (source_os, distro) = match cfg.migration.as_ref() { + Some(m) => { + let os = match m.source_os { + crate::config::SourceOs::Macos => "macOS", + crate::config::SourceOs::Windows => "Windows", + crate::config::SourceOs::Linux => "Linux", + crate::config::SourceOs::Unknown => "an unknown OS", + }; + (os.to_string(), m.distro.clone()) + } + None => ("an unknown OS".to_string(), "Linux".to_string()), + }; + (base_url, model, source_os, distro) + }; + + let system_prompt = crate::llm::build_system_prompt(&source_os, &distro); + let messages = vec![ + crate::llm::ChatMessage { + role: "system".into(), + content: system_prompt, + }, + crate::llm::ChatMessage { + role: "user".into(), + content: message, + }, + ]; + + tauri::async_runtime::spawn(async move { + if let Err(e) = crate::llm::chat_stream(&base_url, &model, messages, &app_handle).await { + log::error!("chat stream error: {e}"); + let _ = app_handle.emit("robin:chat-error", e.to_string()); + } + }); + + Ok(()) +}