feat: allow starting a new chat
This commit is contained in:
@@ -19,11 +19,11 @@ pub trait ChatRepository {
|
|||||||
&self,
|
&self,
|
||||||
text: &str,
|
text: &str,
|
||||||
is_user: &bool,
|
is_user: &bool,
|
||||||
chat_id: &i32,
|
chat_id: &i64,
|
||||||
) -> Result<ChatMessageData>;
|
) -> Result<ChatMessageData>;
|
||||||
async fn get_latest_messages(&self, chat_id: &i32, count: &i32)
|
async fn get_latest_messages(&self, chat_id: &i64, count: &i64)
|
||||||
-> Result<Vec<ChatMessageData>>;
|
-> Result<Vec<ChatMessageData>>;
|
||||||
async fn get_chat_ids(&self) -> Result<Box<[i32]>>;
|
async fn get_chat_ids(&self) -> Result<Box<[i64]>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SqliteChatRepository {
|
pub struct SqliteChatRepository {
|
||||||
@@ -62,7 +62,7 @@ impl ChatRepository for SqliteChatRepository {
|
|||||||
&self,
|
&self,
|
||||||
text: &str,
|
text: &str,
|
||||||
is_user: &bool,
|
is_user: &bool,
|
||||||
chat_id: &i32,
|
chat_id: &i64,
|
||||||
) -> Result<ChatMessageData> {
|
) -> Result<ChatMessageData> {
|
||||||
let result = sqlx::query_as::<_, ChatMessageData>(
|
let result = sqlx::query_as::<_, ChatMessageData>(
|
||||||
r#"
|
r#"
|
||||||
@@ -82,8 +82,8 @@ impl ChatRepository for SqliteChatRepository {
|
|||||||
|
|
||||||
async fn get_latest_messages(
|
async fn get_latest_messages(
|
||||||
&self,
|
&self,
|
||||||
chat_id: &i32,
|
chat_id: &i64,
|
||||||
count: &i32,
|
count: &i64,
|
||||||
) -> Result<Vec<ChatMessageData>> {
|
) -> Result<Vec<ChatMessageData>> {
|
||||||
// From all chat ids get the latest id.
|
// From all chat ids get the latest id.
|
||||||
let rows = sqlx::query(
|
let rows = sqlx::query(
|
||||||
@@ -116,15 +116,15 @@ impl ChatRepository for SqliteChatRepository {
|
|||||||
Ok(messages)
|
Ok(messages)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_chat_ids(&self) -> Result<Box<[i32]>> {
|
async fn get_chat_ids(&self) -> Result<Box<[i64]>> {
|
||||||
let rows = sqlx::query("SELECT DISTINCT(chat_id) FROM messages ORDER BY chat_id DESC")
|
let rows = sqlx::query("SELECT DISTINCT(chat_id) FROM messages ORDER BY chat_id DESC")
|
||||||
.fetch_all(&self.pool)
|
.fetch_all(&self.pool)
|
||||||
.await
|
.await
|
||||||
.inspect_err(|e| println!("sql error: {}", e))?;
|
.inspect_err(|e| println!("sql error: {}", e))?;
|
||||||
let ids: Vec<i32> = rows
|
let ids: Vec<i64> = rows
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|row| {
|
.map(|row| {
|
||||||
let i: i32 = row.get(0);
|
let i: i64 = row.get(0);
|
||||||
i
|
i
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use crate::chatpersistence::{ChatMessageData, ChatRepository};
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use genai::chat::{ChatMessage, ChatRequest};
|
use genai::chat::{ChatMessage, ChatRequest};
|
||||||
use genai::Client;
|
use genai::Client;
|
||||||
use shared::ai::ai_daemon_server::AiDaemon;
|
use shared::ai::ai_service_server::AiService;
|
||||||
use shared::ai::{
|
use shared::ai::{
|
||||||
ChatHistoryRequest, ChatHistoryResponse, ChatMessage as CMessage, ChatRequest as CRequest,
|
ChatHistoryRequest, ChatHistoryResponse, ChatMessage as CMessage, ChatRequest as CRequest,
|
||||||
ChatResponse as CResponse, DaemonStatusRequest, DaemonStatusResponse,
|
ChatResponse as CResponse, DaemonStatusRequest, DaemonStatusResponse,
|
||||||
@@ -25,10 +25,10 @@ impl DaemonServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tonic::async_trait]
|
#[tonic::async_trait]
|
||||||
impl AiDaemon for DaemonServer {
|
impl AiService for DaemonServer {
|
||||||
async fn chat(&self, request: Request<CRequest>) -> Result<Response<CResponse>, Status> {
|
async fn chat(&self, request: Request<CRequest>) -> Result<Response<CResponse>, Status> {
|
||||||
let r = request.into_inner();
|
let r = request.into_inner();
|
||||||
let chat_id = get_chat_id(self.repo.clone(), r.chat_id)
|
let chat_id = id_or_new(self.repo.clone(), r.chat_id)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| Status::new(Code::Internal, e.to_string()))?;
|
.map_err(|e| Status::new(Code::Internal, e.to_string()))?;
|
||||||
let mut messages = gather_history(self.repo.clone(), &chat_id)
|
let mut messages = gather_history(self.repo.clone(), &chat_id)
|
||||||
@@ -45,7 +45,7 @@ impl AiDaemon for DaemonServer {
|
|||||||
let user_message = message_to_dto(
|
let user_message = message_to_dto(
|
||||||
&self
|
&self
|
||||||
.repo
|
.repo
|
||||||
.save_message(r.text(), &true, &0)
|
.save_message(r.text(), &true, &chat_id)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| Status::new(Code::Internal, e.to_string()))?,
|
.map_err(|e| Status::new(Code::Internal, e.to_string()))?,
|
||||||
);
|
);
|
||||||
@@ -60,12 +60,12 @@ impl AiDaemon for DaemonServer {
|
|||||||
let ai_message = message_to_dto(
|
let ai_message = message_to_dto(
|
||||||
&self
|
&self
|
||||||
.repo
|
.repo
|
||||||
.save_message(response_text, &false, &0)
|
.save_message(response_text, &false, &chat_id)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| Status::new(Code::Internal, e.to_string()))?,
|
.map_err(|e| Status::new(Code::Internal, e.to_string()))?,
|
||||||
);
|
);
|
||||||
let response = CResponse {
|
let response = CResponse {
|
||||||
chat_id: 1,
|
chat_id: ai_message.chat_id,
|
||||||
messages: vec![user_message, ai_message],
|
messages: vec![user_message, ai_message],
|
||||||
};
|
};
|
||||||
return Ok(Response::new(response));
|
return Ok(Response::new(response));
|
||||||
@@ -75,7 +75,7 @@ impl AiDaemon for DaemonServer {
|
|||||||
&self,
|
&self,
|
||||||
request: Request<ChatHistoryRequest>,
|
request: Request<ChatHistoryRequest>,
|
||||||
) -> Result<Response<ChatHistoryResponse>, Status> {
|
) -> Result<Response<ChatHistoryResponse>, Status> {
|
||||||
let chat_id = get_chat_id(self.repo.clone(), request.into_inner().chat_id)
|
let chat_id = get_latest_chat_id(self.repo.clone(), request.into_inner().chat_id)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| Status::new(Code::Internal, e.to_string()))?;
|
.map_err(|e| Status::new(Code::Internal, e.to_string()))?;
|
||||||
let messages = self
|
let messages = self
|
||||||
@@ -85,7 +85,7 @@ impl AiDaemon for DaemonServer {
|
|||||||
.map_err(|e| Status::new(Code::Internal, e.to_string()))?;
|
.map_err(|e| Status::new(Code::Internal, e.to_string()))?;
|
||||||
|
|
||||||
let response = ChatHistoryResponse {
|
let response = ChatHistoryResponse {
|
||||||
chat_id: 1,
|
chat_id: chat_id,
|
||||||
history: messages.iter().map(|m| message_to_dto(m)).collect(),
|
history: messages.iter().map(|m| message_to_dto(m)).collect(),
|
||||||
};
|
};
|
||||||
Ok(Response::new(response))
|
Ok(Response::new(response))
|
||||||
@@ -107,6 +107,7 @@ impl AiDaemon for DaemonServer {
|
|||||||
pub fn message_to_dto(msg: &ChatMessageData) -> CMessage {
|
pub fn message_to_dto(msg: &ChatMessageData) -> CMessage {
|
||||||
CMessage {
|
CMessage {
|
||||||
id: msg.id,
|
id: msg.id,
|
||||||
|
chat_id: msg.chat_id,
|
||||||
text: msg.text.clone(),
|
text: msg.text.clone(),
|
||||||
is_user: msg.is_user,
|
is_user: msg.is_user,
|
||||||
}
|
}
|
||||||
@@ -114,7 +115,7 @@ pub fn message_to_dto(msg: &ChatMessageData) -> CMessage {
|
|||||||
|
|
||||||
async fn gather_history(
|
async fn gather_history(
|
||||||
repo: Arc<dyn ChatRepository + Send + Sync>,
|
repo: Arc<dyn ChatRepository + Send + Sync>,
|
||||||
chat_id: &i32,
|
chat_id: &i64,
|
||||||
) -> Result<Vec<ChatMessage>> {
|
) -> Result<Vec<ChatMessage>> {
|
||||||
let messages = repo.get_latest_messages(chat_id, &10).await?;
|
let messages = repo.get_latest_messages(chat_id, &10).await?;
|
||||||
Ok(messages
|
Ok(messages
|
||||||
@@ -126,12 +127,22 @@ async fn gather_history(
|
|||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_chat_id(
|
async fn get_latest_chat_id(
|
||||||
repo: Arc<dyn ChatRepository + Send + Sync>,
|
repo: Arc<dyn ChatRepository + Send + Sync>,
|
||||||
chat_id: Option<i64>,
|
chat_id: Option<i64>,
|
||||||
) -> Result<i32> {
|
) -> Result<i64> {
|
||||||
Ok(match chat_id {
|
Ok(match chat_id {
|
||||||
Some(i) => i as i32,
|
Some(i) => i,
|
||||||
None => repo.get_chat_ids().await?.get(0).copied().unwrap_or(0),
|
None => repo.get_chat_ids().await?.get(0).copied().unwrap_or(0),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn id_or_new(
|
||||||
|
repo: Arc<dyn ChatRepository + Send + Sync>,
|
||||||
|
chat_id: Option<i64>,
|
||||||
|
) -> Result<i64> {
|
||||||
|
Ok(match chat_id {
|
||||||
|
Some(i) => i,
|
||||||
|
None => repo.get_chat_ids().await?.get(0).copied().unwrap_or(0) + 1,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ mod daemongrpc;
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use genai::Client;
|
use genai::Client;
|
||||||
use shared::ai::ai_daemon_server::AiDaemonServer;
|
use shared::ai::ai_service_server::AiServiceServer;
|
||||||
use tonic::transport::Server;
|
use tonic::transport::Server;
|
||||||
|
|
||||||
use chatpersistence::SqliteChatRepository;
|
use chatpersistence::SqliteChatRepository;
|
||||||
@@ -24,7 +24,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.build_v1()?;
|
.build_v1()?;
|
||||||
println!("Started daemon at {}", addr_s);
|
println!("Started daemon at {}", addr_s);
|
||||||
Server::builder()
|
Server::builder()
|
||||||
.add_service(AiDaemonServer::new(daemon))
|
.add_service(AiServiceServer::new(daemon))
|
||||||
.add_service(reflection_service)
|
.add_service(reflection_service)
|
||||||
.serve(addr)
|
.serve(addr)
|
||||||
.await?;
|
.await?;
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ pub mod chatmessage {
|
|||||||
|
|
||||||
pub enum TauriCommand {
|
pub enum TauriCommand {
|
||||||
Chat,
|
Chat,
|
||||||
|
SetChatId,
|
||||||
ChatHistory,
|
ChatHistory,
|
||||||
DaemonState,
|
DaemonState,
|
||||||
ToggleDarkMode,
|
ToggleDarkMode,
|
||||||
@@ -27,6 +28,7 @@ pub mod chatmessage {
|
|||||||
match self {
|
match self {
|
||||||
TauriCommand::TogglePopup => "toggle_popup",
|
TauriCommand::TogglePopup => "toggle_popup",
|
||||||
TauriCommand::Chat => "chat",
|
TauriCommand::Chat => "chat",
|
||||||
|
TauriCommand::SetChatId => "set_chat_id",
|
||||||
TauriCommand::ChatHistory => "chat_history",
|
TauriCommand::ChatHistory => "chat_history",
|
||||||
TauriCommand::DaemonState => "daemon_state",
|
TauriCommand::DaemonState => "daemon_state",
|
||||||
TauriCommand::ToggleDarkMode => "toggle_dark_mode",
|
TauriCommand::ToggleDarkMode => "toggle_dark_mode",
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
syntax = "proto3";
|
syntax = "proto3";
|
||||||
package ai_daemon;
|
package ai_daemon;
|
||||||
|
|
||||||
service AiDaemon {
|
service AiService {
|
||||||
rpc Chat(ChatRequest) returns (ChatResponse);
|
rpc Chat(ChatRequest) returns (ChatResponse);
|
||||||
rpc ChatHistory(ChatHistoryRequest) returns (ChatHistoryResponse);
|
rpc ChatHistory(ChatHistoryRequest) returns (ChatHistoryResponse);
|
||||||
rpc DaemonStatus(DaemonStatusRequest) returns (DaemonStatusResponse);
|
rpc DaemonStatus(DaemonStatusRequest) returns (DaemonStatusResponse);
|
||||||
@@ -9,6 +9,7 @@ service AiDaemon {
|
|||||||
|
|
||||||
message ChatMessage {
|
message ChatMessage {
|
||||||
int64 id = 1;
|
int64 id = 1;
|
||||||
|
int64 chat_id = 2;
|
||||||
string text = 10;
|
string text = 10;
|
||||||
bool is_user = 20;
|
bool is_user = 20;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,19 +29,19 @@ pub fn toggle_popup(app_handle: tauri::AppHandle) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn chat(
|
pub async fn chat(state: State<'_, AppState>, prompt: String) -> Result<Vec<Message>, String> {
|
||||||
state: State<'_, AppState>,
|
|
||||||
prompt: String,
|
|
||||||
chat_id: Option<i64>,
|
|
||||||
) -> Result<Vec<Message>, String> {
|
|
||||||
let mut client = state.grpc_client.lock().await;
|
let mut client = state.grpc_client.lock().await;
|
||||||
|
let cid = state.current_chat_id.lock().await.clone();
|
||||||
let request = tonic::Request::new(ChatRequest {
|
let request = tonic::Request::new(ChatRequest {
|
||||||
chat_id: chat_id,
|
chat_id: cid,
|
||||||
text: Some(prompt),
|
text: Some(prompt),
|
||||||
});
|
});
|
||||||
match client.chat(request).await {
|
match client.chat(request).await {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let r = response.into_inner();
|
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| {
|
r.messages.iter().for_each(|m| {
|
||||||
if m.is_user {
|
if m.is_user {
|
||||||
println!(">>> {}", m.text)
|
println!(">>> {}", m.text)
|
||||||
@@ -66,19 +66,30 @@ pub async fn chat(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn chat_history(
|
pub async fn set_chat_id(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
chat_id: Option<i64>,
|
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 mut client = state.grpc_client.lock().await;
|
||||||
|
let chat_id = state.current_chat_id.lock().await.clone();
|
||||||
let result = client
|
let result = client
|
||||||
.chat_history(ChatHistoryRequest { chat_id: chat_id })
|
.chat_history(ChatHistoryRequest { chat_id: chat_id })
|
||||||
.await;
|
.await;
|
||||||
match result {
|
match result {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let r = response.into_inner();
|
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 {
|
Ok(MessageHistory {
|
||||||
chat_id: None,
|
chat_id: Some(r.chat_id),
|
||||||
history: r
|
history: r
|
||||||
.history
|
.history
|
||||||
.iter()
|
.iter()
|
||||||
|
|||||||
@@ -6,34 +6,35 @@ mod commands;
|
|||||||
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState};
|
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState};
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
use commands::{chat, chat_history, daemon_state, toggle_dark_mode, toggle_popup};
|
use commands::{chat, chat_history, daemon_state, set_chat_id, toggle_dark_mode, toggle_popup};
|
||||||
use shared::ai::ai_daemon_client::AiDaemonClient;
|
use shared::ai::ai_service_client::AiServiceClient;
|
||||||
|
|
||||||
pub struct AppConfig {
|
pub struct AppConfig {
|
||||||
dark_mode: bool,
|
dark_mode: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
grpc_client: Mutex<AiDaemonClient<tonic::transport::Channel>>,
|
grpc_client: Mutex<AiServiceClient<tonic::transport::Channel>>,
|
||||||
config: Mutex<AppConfig>,
|
config: Mutex<AppConfig>,
|
||||||
current_chat_id: Mutex<i32>,
|
current_chat_id: Mutex<Option<i64>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let channel = tonic::transport::Channel::from_static("http://[::1]:50051").connect_lazy();
|
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()
|
tauri::Builder::default()
|
||||||
.manage(AppState {
|
.manage(AppState {
|
||||||
grpc_client: Mutex::new(client),
|
grpc_client: Mutex::new(client),
|
||||||
config: Mutex::new(AppConfig { dark_mode: true }),
|
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())
|
.plugin(tauri_plugin_global_shortcut::Builder::new().build())
|
||||||
.invoke_handler(tauri::generate_handler![
|
.invoke_handler(tauri::generate_handler![
|
||||||
toggle_popup,
|
toggle_popup,
|
||||||
chat_history,
|
chat_history,
|
||||||
|
set_chat_id,
|
||||||
chat,
|
chat,
|
||||||
daemon_state,
|
daemon_state,
|
||||||
toggle_dark_mode,
|
toggle_dark_mode,
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use leptos_router::{
|
|||||||
use wasm_bindgen::JsValue;
|
use wasm_bindgen::JsValue;
|
||||||
|
|
||||||
use crate::popup::PopupView;
|
use crate::popup::PopupView;
|
||||||
use crate::{bridge::invoke, components::DarkModeToggle};
|
use crate::{bridge::invoke_js, components::DarkModeToggle};
|
||||||
use crate::{
|
use crate::{
|
||||||
bridge::invoke_typed,
|
bridge::invoke_typed,
|
||||||
components::{DaemonProvider, ThemeProvider},
|
components::{DaemonProvider, ThemeProvider},
|
||||||
@@ -34,7 +34,7 @@ fn Dashboard() -> impl IntoView {
|
|||||||
let on_click = move |_ev: leptos::ev::MouseEvent| {
|
let on_click = move |_ev: leptos::ev::MouseEvent| {
|
||||||
spawn_local(async move {
|
spawn_local(async move {
|
||||||
let empty_args = serde_wasm_bindgen::to_value(&serde_json::json!({})).unwrap();
|
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! {
|
||||||
|
|||||||
@@ -4,12 +4,16 @@ use wasm_bindgen::prelude::*;
|
|||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#[wasm_bindgen(js_namespace = ["window", "__TAURI__", "core"])]
|
#[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"])]
|
#[wasm_bindgen(js_namespace = ["window", "__TAURI__", "event"])]
|
||||||
pub async fn listen(event: &str, handler: &Closure<dyn FnMut(JsValue)>) -> JsValue;
|
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
|
pub async fn invoke_typed<T>(cmd: TauriCommand, args: JsValue) -> T
|
||||||
where
|
where
|
||||||
T: DeserializeOwned,
|
T: DeserializeOwned,
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use leptos::{component, prelude::*, reactive::spawn_local, view, IntoView};
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use wasm_bindgen::JsValue;
|
use wasm_bindgen::JsValue;
|
||||||
|
|
||||||
use crate::bridge::{event_handler, invoke, invoke_typed, listen};
|
use crate::bridge::{event_handler, invoke_js, invoke_typed, listen};
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn DaemonProvider(children: ChildrenFn) -> impl IntoView {
|
pub fn DaemonProvider(children: ChildrenFn) -> impl IntoView {
|
||||||
@@ -88,7 +88,7 @@ pub fn ThemeProvider(children: Children) -> impl IntoView {
|
|||||||
pub fn DarkModeToggle() -> impl IntoView {
|
pub fn DarkModeToggle() -> impl IntoView {
|
||||||
let toggle_dark_mode = |_ev: leptos::ev::MouseEvent| {
|
let toggle_dark_mode = |_ev: leptos::ev::MouseEvent| {
|
||||||
spawn_local(async {
|
spawn_local(async {
|
||||||
let _ = invoke(TauriCommand::ToggleDarkMode.as_str(), JsValue::UNDEFINED).await;
|
let _ = invoke_js(TauriCommand::ToggleDarkMode, JsValue::UNDEFINED).await;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
view! {
|
view! {
|
||||||
@@ -100,8 +100,6 @@ pub fn DarkModeToggle() -> impl IntoView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const DIALOG_BUTTON: &str = "primary-button p-3 m-3";
|
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn ConfirmDialog(
|
pub fn ConfirmDialog(
|
||||||
is_open: ReadSignal<bool>,
|
is_open: ReadSignal<bool>,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
bridge::{invoke, invoke_typed},
|
bridge::{invoke_js, invoke_typed},
|
||||||
components::{ConfirmDialog, DaemonProvider, DarkModeToggle, ThemeProvider},
|
components::{ConfirmDialog, DaemonProvider, DarkModeToggle, ThemeProvider},
|
||||||
};
|
};
|
||||||
use feshared::{
|
use feshared::{
|
||||||
@@ -30,11 +30,8 @@ pub fn Popup() -> impl IntoView {
|
|||||||
use_context::<LocalResource<DaemonState>>().expect("No daemon connection context!");
|
use_context::<LocalResource<DaemonState>>().expect("No daemon connection context!");
|
||||||
|
|
||||||
let init_history = Action::new_local(|(): &()| async move {
|
let init_history = Action::new_local(|(): &()| async move {
|
||||||
let history: MessageHistory = invoke_typed(
|
let history: MessageHistory =
|
||||||
TauriCommand::ChatHistory,
|
invoke_typed(TauriCommand::ChatHistory, JsValue::UNDEFINED).await;
|
||||||
serde_wasm_bindgen::to_value(&serde_json::json!({"chat_id": 1})).unwrap(),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
history
|
history
|
||||||
});
|
});
|
||||||
Effect::new(move |prev_status: Option<bool>| {
|
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
|
// Action that calls the chat action on the daemon
|
||||||
let prompt_action = Action::new_local(|prompt: &String| {
|
let prompt_action = Action::new_local(|prompt: &String| {
|
||||||
let prompt = prompt.clone();
|
let prompt = prompt.clone();
|
||||||
@@ -88,17 +97,24 @@ pub fn Popup() -> impl IntoView {
|
|||||||
let _ = window_event_listener(keydown, move |ev| {
|
let _ = window_event_listener(keydown, move |ev| {
|
||||||
if ev.key() == "Escape" {
|
if ev.key() == "Escape" {
|
||||||
spawn_local(async move {
|
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 (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! {
|
view! {
|
||||||
<ConfirmDialog
|
<ConfirmDialog
|
||||||
is_open=show_new_chat_confirm
|
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))
|
on_cancel=Callback::new(move |_| set_new_chat_confirm.set(false))
|
||||||
title="Open a new chat?".to_string()
|
title="Open a new chat?".to_string()
|
||||||
message="Current chat is stored".to_string() />
|
message="Current chat is stored".to_string() />
|
||||||
|
|||||||
@@ -783,10 +783,6 @@
|
|||||||
.field-sizing-fixed {
|
.field-sizing-fixed {
|
||||||
field-sizing: fixed;
|
field-sizing: fixed;
|
||||||
}
|
}
|
||||||
.size-3 {
|
|
||||||
width: calc(var(--spacing) * 3);
|
|
||||||
height: calc(var(--spacing) * 3);
|
|
||||||
}
|
|
||||||
.size-3\.5 {
|
.size-3\.5 {
|
||||||
width: calc(var(--spacing) * 3.5);
|
width: calc(var(--spacing) * 3.5);
|
||||||
height: calc(var(--spacing) * 3.5);
|
height: calc(var(--spacing) * 3.5);
|
||||||
@@ -1293,9 +1289,6 @@
|
|||||||
.justify-items-stretch {
|
.justify-items-stretch {
|
||||||
justify-items: stretch;
|
justify-items: stretch;
|
||||||
}
|
}
|
||||||
.gap-1 {
|
|
||||||
gap: calc(var(--spacing) * 1);
|
|
||||||
}
|
|
||||||
.gap-1\.5 {
|
.gap-1\.5 {
|
||||||
gap: calc(var(--spacing) * 1.5);
|
gap: calc(var(--spacing) * 1.5);
|
||||||
}
|
}
|
||||||
@@ -1575,9 +1568,6 @@
|
|||||||
.bg-\[\#fbf0df\] {
|
.bg-\[\#fbf0df\] {
|
||||||
background-color: #fbf0df;
|
background-color: #fbf0df;
|
||||||
}
|
}
|
||||||
.bg-blue-300 {
|
|
||||||
background-color: var(--color-blue-300);
|
|
||||||
}
|
|
||||||
.bg-green-600 {
|
.bg-green-600 {
|
||||||
background-color: var(--color-green-600);
|
background-color: var(--color-green-600);
|
||||||
}
|
}
|
||||||
@@ -2834,14 +2824,6 @@
|
|||||||
line-height: var(--tw-leading, var(--text-sm--line-height));
|
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 {
|
.dark\:bg-black\/30 {
|
||||||
&:where(.dark, .dark *) {
|
&:where(.dark, .dark *) {
|
||||||
background-color: color-mix(in srgb, #000 30%, transparent);
|
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 {
|
.dark\:bg-slate-800 {
|
||||||
&:where(.dark, .dark *) {
|
&:where(.dark, .dark *) {
|
||||||
background-color: var(--color-slate-800);
|
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 {
|
.dark\:bg-zinc-900 {
|
||||||
&:where(.dark, .dark *) {
|
&:where(.dark, .dark *) {
|
||||||
background-color: var(--color-zinc-900);
|
background-color: var(--color-zinc-900);
|
||||||
|
|||||||
Reference in New Issue
Block a user