feat: allow starting a new chat

This commit is contained in:
2026-03-01 13:30:14 +02:00
parent b7f9ac043d
commit dc85276567
12 changed files with 98 additions and 93 deletions

View File

@@ -29,19 +29,19 @@ pub fn toggle_popup(app_handle: tauri::AppHandle) {
}
#[tauri::command]
pub async fn chat(
state: State<'_, AppState>,
prompt: String,
chat_id: Option<i64>,
) -> Result<Vec<Message>, String> {
pub async fn chat(state: State<'_, AppState>, prompt: String) -> Result<Vec<Message>, String> {
let mut client = state.grpc_client.lock().await;
let cid = state.current_chat_id.lock().await.clone();
let request = tonic::Request::new(ChatRequest {
chat_id: chat_id,
chat_id: cid,
text: Some(prompt),
});
match client.chat(request).await {
Ok(response) => {
let r = response.into_inner();
let mut cid = state.current_chat_id.lock().await;
*cid = Some(r.chat_id);
println!("CID={}", r.chat_id);
r.messages.iter().for_each(|m| {
if m.is_user {
println!(">>> {}", m.text)
@@ -66,19 +66,30 @@ pub async fn chat(
}
#[tauri::command]
pub async fn chat_history(
pub async fn set_chat_id(
state: State<'_, AppState>,
chat_id: Option<i64>,
) -> Result<MessageHistory, String> {
) -> Result<Option<i64>, String> {
let mut cid = state.current_chat_id.lock().await;
*cid = chat_id;
Ok(chat_id)
}
#[tauri::command]
pub async fn chat_history(state: State<'_, AppState>) -> Result<MessageHistory, String> {
let mut client = state.grpc_client.lock().await;
let chat_id = state.current_chat_id.lock().await.clone();
let result = client
.chat_history(ChatHistoryRequest { chat_id: chat_id })
.await;
match result {
Ok(response) => {
let r = response.into_inner();
let mut cid = state.current_chat_id.lock().await;
*cid = Some(r.chat_id);
println!("CID={}", r.chat_id);
Ok(MessageHistory {
chat_id: None,
chat_id: Some(r.chat_id),
history: r
.history
.iter()

View File

@@ -6,34 +6,35 @@ mod commands;
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState};
use tokio::sync::Mutex;
use commands::{chat, chat_history, daemon_state, toggle_dark_mode, toggle_popup};
use shared::ai::ai_daemon_client::AiDaemonClient;
use commands::{chat, chat_history, daemon_state, set_chat_id, toggle_dark_mode, toggle_popup};
use shared::ai::ai_service_client::AiServiceClient;
pub struct AppConfig {
dark_mode: bool,
}
pub struct AppState {
grpc_client: Mutex<AiDaemonClient<tonic::transport::Channel>>,
grpc_client: Mutex<AiServiceClient<tonic::transport::Channel>>,
config: Mutex<AppConfig>,
current_chat_id: Mutex<i32>,
current_chat_id: Mutex<Option<i64>>,
}
#[tokio::main]
async fn main() {
let channel = tonic::transport::Channel::from_static("http://[::1]:50051").connect_lazy();
let client = AiDaemonClient::new(channel);
let client = AiServiceClient::new(channel);
tauri::Builder::default()
.manage(AppState {
grpc_client: Mutex::new(client),
config: Mutex::new(AppConfig { dark_mode: true }),
current_chat_id: Mutex::new(-1),
current_chat_id: Mutex::new(None),
})
.plugin(tauri_plugin_global_shortcut::Builder::new().build())
.invoke_handler(tauri::generate_handler![
toggle_popup,
chat_history,
set_chat_id,
chat,
daemon_state,
toggle_dark_mode,

View File

@@ -9,7 +9,7 @@ use leptos_router::{
use wasm_bindgen::JsValue;
use crate::popup::PopupView;
use crate::{bridge::invoke, components::DarkModeToggle};
use crate::{bridge::invoke_js, components::DarkModeToggle};
use crate::{
bridge::invoke_typed,
components::{DaemonProvider, ThemeProvider},
@@ -34,7 +34,7 @@ fn Dashboard() -> impl IntoView {
let on_click = move |_ev: leptos::ev::MouseEvent| {
spawn_local(async move {
let empty_args = serde_wasm_bindgen::to_value(&serde_json::json!({})).unwrap();
invoke(TauriCommand::TogglePopup.as_str(), empty_args).await;
invoke_js(TauriCommand::TogglePopup, empty_args).await;
});
};
view! {

View File

@@ -4,12 +4,16 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = ["window", "__TAURI__", "core"])]
pub async fn invoke(cmd: &str, args: JsValue) -> JsValue;
async fn invoke(cmd: &str, args: JsValue) -> JsValue;
#[wasm_bindgen(js_namespace = ["window", "__TAURI__", "event"])]
pub async fn listen(event: &str, handler: &Closure<dyn FnMut(JsValue)>) -> JsValue;
}
pub async fn invoke_js(cmd: TauriCommand, args: JsValue) -> JsValue {
invoke(cmd.as_str(), args).await
}
pub async fn invoke_typed<T>(cmd: TauriCommand, args: JsValue) -> T
where
T: DeserializeOwned,

View File

@@ -5,7 +5,7 @@ use leptos::{component, prelude::*, reactive::spawn_local, view, IntoView};
use serde::{Deserialize, Serialize};
use wasm_bindgen::JsValue;
use crate::bridge::{event_handler, invoke, invoke_typed, listen};
use crate::bridge::{event_handler, invoke_js, invoke_typed, listen};
#[component]
pub fn DaemonProvider(children: ChildrenFn) -> impl IntoView {
@@ -88,7 +88,7 @@ pub fn ThemeProvider(children: Children) -> impl IntoView {
pub fn DarkModeToggle() -> impl IntoView {
let toggle_dark_mode = |_ev: leptos::ev::MouseEvent| {
spawn_local(async {
let _ = invoke(TauriCommand::ToggleDarkMode.as_str(), JsValue::UNDEFINED).await;
let _ = invoke_js(TauriCommand::ToggleDarkMode, JsValue::UNDEFINED).await;
});
};
view! {
@@ -100,8 +100,6 @@ pub fn DarkModeToggle() -> impl IntoView {
}
}
const DIALOG_BUTTON: &str = "primary-button p-3 m-3";
#[component]
pub fn ConfirmDialog(
is_open: ReadSignal<bool>,

View File

@@ -1,5 +1,5 @@
use crate::{
bridge::{invoke, invoke_typed},
bridge::{invoke_js, invoke_typed},
components::{ConfirmDialog, DaemonProvider, DarkModeToggle, ThemeProvider},
};
use feshared::{
@@ -30,11 +30,8 @@ pub fn Popup() -> impl IntoView {
use_context::<LocalResource<DaemonState>>().expect("No daemon connection context!");
let init_history = Action::new_local(|(): &()| async move {
let history: MessageHistory = invoke_typed(
TauriCommand::ChatHistory,
serde_wasm_bindgen::to_value(&serde_json::json!({"chat_id": 1})).unwrap(),
)
.await;
let history: MessageHistory =
invoke_typed(TauriCommand::ChatHistory, JsValue::UNDEFINED).await;
history
});
Effect::new(move |prev_status: Option<bool>| {
@@ -50,6 +47,18 @@ pub fn Popup() -> impl IntoView {
}
});
let set_chat_id_action = Action::new_local(|chat_id: &Option<i64>| {
let cid = chat_id.clone();
async move {
let result: Option<i64> = invoke_typed(
TauriCommand::SetChatId,
serde_wasm_bindgen::to_value(&serde_json::json!({"chat_id": cid})).unwrap(),
)
.await;
result
}
});
// Action that calls the chat action on the daemon
let prompt_action = Action::new_local(|prompt: &String| {
let prompt = prompt.clone();
@@ -88,17 +97,24 @@ pub fn Popup() -> impl IntoView {
let _ = window_event_listener(keydown, move |ev| {
if ev.key() == "Escape" {
spawn_local(async move {
let _ = invoke(TauriCommand::TogglePopup.as_str(), JsValue::UNDEFINED).await;
let _ = invoke_js(TauriCommand::TogglePopup, JsValue::UNDEFINED).await;
});
}
});
let (show_new_chat_confirm, set_new_chat_confirm) = signal(false);
let new_chat = move |_: ()| {
set_messages.set(Vec::<Message>::new());
// TODO add callback to the action, and clear chat and close dialog in the callback!
set_chat_id_action.dispatch(None);
set_new_chat_confirm.set(false);
};
view! {
<ConfirmDialog
is_open=show_new_chat_confirm
on_confirm=Callback::new(move |_| set_new_chat_confirm.set(false))
on_confirm=Callback::new(new_chat)
on_cancel=Callback::new(move |_| set_new_chat_confirm.set(false))
title="Open a new chat?".to_string()
message="Current chat is stored".to_string() />

View File

@@ -783,10 +783,6 @@
.field-sizing-fixed {
field-sizing: fixed;
}
.size-3 {
width: calc(var(--spacing) * 3);
height: calc(var(--spacing) * 3);
}
.size-3\.5 {
width: calc(var(--spacing) * 3.5);
height: calc(var(--spacing) * 3.5);
@@ -1293,9 +1289,6 @@
.justify-items-stretch {
justify-items: stretch;
}
.gap-1 {
gap: calc(var(--spacing) * 1);
}
.gap-1\.5 {
gap: calc(var(--spacing) * 1.5);
}
@@ -1575,9 +1568,6 @@
.bg-\[\#fbf0df\] {
background-color: #fbf0df;
}
.bg-blue-300 {
background-color: var(--color-blue-300);
}
.bg-green-600 {
background-color: var(--color-green-600);
}
@@ -2834,14 +2824,6 @@
line-height: var(--tw-leading, var(--text-sm--line-height));
}
}
.dark\:bg-black\/10 {
&:where(.dark, .dark *) {
background-color: color-mix(in srgb, #000 10%, transparent);
@supports (color: color-mix(in lab, red, red)) {
background-color: color-mix(in oklab, var(--color-black) 10%, transparent);
}
}
}
.dark\:bg-black\/30 {
&:where(.dark, .dark *) {
background-color: color-mix(in srgb, #000 30%, transparent);
@@ -2850,32 +2832,11 @@
}
}
}
.dark\:bg-gray-800 {
&:where(.dark, .dark *) {
background-color: var(--color-gray-800);
}
}
.dark\:bg-slate-800 {
&:where(.dark, .dark *) {
background-color: var(--color-slate-800);
}
}
.dark\:bg-white\/10 {
&:where(.dark, .dark *) {
background-color: color-mix(in srgb, #fff 10%, transparent);
@supports (color: color-mix(in lab, red, red)) {
background-color: color-mix(in oklab, var(--color-white) 10%, transparent);
}
}
}
.dark\:bg-white\/50 {
&:where(.dark, .dark *) {
background-color: color-mix(in srgb, #fff 50%, transparent);
@supports (color: color-mix(in lab, red, red)) {
background-color: color-mix(in oklab, var(--color-white) 50%, transparent);
}
}
}
.dark\:bg-zinc-900 {
&:where(.dark, .dark *) {
background-color: var(--color-zinc-900);