feat: allow starting a new chat
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user