feat: storing chat messages and fetching latest messages

This commit is contained in:
2026-02-13 21:34:35 +02:00
parent e63fd76d2f
commit 3ce2fa3841
4 changed files with 107 additions and 78 deletions

View File

@@ -2,10 +2,13 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use feshared::chatmessage::{Message, MessageHistory};
use shared::ai::{ai_daemon_client::AiDaemonClient, ChatRequest, PromptRequest};
use shared::ai::{
ai_daemon_client::AiDaemonClient, ChatHistoryRequest, ChatRequest, PromptRequest,
};
use tauri::{Emitter, Manager, State};
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState};
use tokio::sync::Mutex;
use tonic::{client, Response};
struct AppState {
grpc_client: Mutex<AiDaemonClient<tonic::transport::Channel>>,
@@ -31,17 +34,6 @@ fn toggle_popup(app_handle: tauri::AppHandle) {
}
}
#[tauri::command]
async fn prompt_llm(state: State<'_, AppState>, prompt: String) -> Result<String, String> {
println!(">>>> {}", prompt);
let mut client = state.grpc_client.lock().await;
let request = tonic::Request::new(PromptRequest { prompt });
match client.prompt(request).await {
Ok(response) => Ok(response.into_inner().response),
Err(e) => Err(format!("gRPC error: {}", e)),
}
}
#[tauri::command]
async fn chat(
state: State<'_, AppState>,
@@ -72,7 +64,10 @@ async fn chat(
})
.collect())
}
Err(e) => Err(format!("gRPC error: {}", e)),
Err(e) => {
println!("gRPC error: {}", e);
Err(format!("gRPC error: {}", e))
}
}
}
@@ -81,25 +76,28 @@ async fn chat_history(
state: State<'_, AppState>,
chat_id: Option<i64>,
) -> Result<MessageHistory, String> {
let history = MessageHistory {
chat_id: match chat_id {
Some(_) => chat_id,
None => Some(-1),
},
history: vec![
Message {
id: 1,
text: String::from("asd"),
is_user: false,
},
Message {
id: 2,
text: String::from("yeah!!!!"),
is_user: true,
},
],
};
Ok(history)
let mut client = state.grpc_client.lock().await;
let result = client
.chat_history(ChatHistoryRequest { chat_id: None })
.await;
match result {
Ok(response) => {
let r = response.into_inner();
Ok(MessageHistory {
chat_id: None,
history: r
.history
.iter()
.map(|m| Message {
id: m.id,
is_user: m.is_user,
text: m.text.clone(),
})
.collect(),
})
}
Err(e) => Err(format!("gRPC error: {e}")),
}
}
#[tokio::main]
@@ -117,12 +115,7 @@ async fn main() {
current_chat: Mutex::new(None),
})
.plugin(tauri_plugin_global_shortcut::Builder::new().build())
.invoke_handler(tauri::generate_handler![
toggle_popup,
prompt_llm,
chat_history,
chat,
])
.invoke_handler(tauri::generate_handler![toggle_popup, chat_history, chat,])
.setup(|app| {
/* Auto-hide popup when focus is lost
if let Some(window) = app.get_webview_window("popup") {