initial work on message history and data transfer

This commit is contained in:
2026-02-09 20:57:47 +02:00
parent e878b8120b
commit e63fd76d2f
6 changed files with 149 additions and 49 deletions

View File

@@ -2,13 +2,14 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use feshared::chatmessage::{Message, MessageHistory};
use shared::ai::{ai_daemon_client::AiDaemonClient, PromptRequest};
use shared::ai::{ai_daemon_client::AiDaemonClient, ChatRequest, PromptRequest};
use tauri::{Emitter, Manager, State};
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState};
use tokio::sync::Mutex;
struct AppState {
grpc_client: Mutex<AiDaemonClient<tonic::transport::Channel>>,
current_chat: Mutex<Option<i64>>,
}
#[tauri::command]
@@ -41,6 +42,40 @@ async fn prompt_llm(state: State<'_, AppState>, prompt: String) -> Result<String
}
}
#[tauri::command]
async fn chat(
state: State<'_, AppState>,
prompt: String,
chat_id: Option<i64>,
) -> Result<Vec<Message>, String> {
let mut client = state.grpc_client.lock().await;
let request = tonic::Request::new(ChatRequest {
chat_id: chat_id,
text: Some(prompt),
});
match client.chat(request).await {
Ok(response) => {
let r = response.into_inner();
r.messages.iter().for_each(|m| {
if m.is_user {
println!(">>> {}", m.text)
} else {
println!("<<< {}", m.text)
}
});
Ok(r.messages
.iter()
.map(|msg| Message {
id: msg.id,
text: msg.text.clone(),
is_user: msg.is_user,
})
.collect())
}
Err(e) => Err(format!("gRPC error: {}", e)),
}
}
#[tauri::command]
async fn chat_history(
state: State<'_, AppState>,
@@ -48,14 +83,21 @@ async fn chat_history(
) -> Result<MessageHistory, String> {
let history = MessageHistory {
chat_id: match chat_id {
Some(id) => id,
None => -1,
Some(_) => chat_id,
None => Some(-1),
},
history: vec![Message {
id: 1,
text: String::from("asd"),
is_user: false,
}],
history: vec![
Message {
id: 1,
text: String::from("asd"),
is_user: false,
},
Message {
id: 2,
text: String::from("yeah!!!!"),
is_user: true,
},
],
};
Ok(history)
}
@@ -72,12 +114,14 @@ async fn main() {
tauri::Builder::default()
.manage(AppState {
grpc_client: Mutex::new(client),
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_history,
chat,
])
.setup(|app| {
/* Auto-hide popup when focus is lost