@@ -2,9 +2,12 @@ use crate::common;
22use crate :: common:: assistant:: ChatRequestMessage ;
33use crate :: common:: http:: GetResponse ;
44use crate :: server:: http_client:: HttpClient ;
5+ use futures_util:: TryStreamExt ;
6+ use http:: Method ;
57use serde_json:: Value ;
68use std:: collections:: HashMap ;
7- use tauri:: { AppHandle , Runtime } ;
9+ use tauri:: { AppHandle , Emitter , Runtime } ;
10+ use tokio:: io:: AsyncBufReadExt ;
811
912#[ tauri:: command]
1013pub async fn chat_history < R : Runtime > (
@@ -143,8 +146,8 @@ pub async fn new_chat<R: Runtime>(
143146
144147 log:: debug!( "New chat response: {}" , & body_text) ;
145148
146- let chat_response: GetResponse =
147- serde_json :: from_str ( & body_text ) . map_err ( |e| format ! ( "Failed to parse response JSON: {}" , e) ) ?;
149+ let chat_response: GetResponse = serde_json :: from_str ( & body_text )
150+ . map_err ( |e| format ! ( "Failed to parse response JSON: {}" , e) ) ?;
148151
149152 if chat_response. result != "created" {
150153 return Err ( format ! ( "Unexpected result: {}" , chat_response. result) ) ;
@@ -178,8 +181,8 @@ pub async fn send_message<R: Runtime>(
178181 query_params,
179182 Some ( body) ,
180183 )
181- . await
182- . map_err ( |e| format ! ( "Error cancel session: {}" , e) ) ?;
184+ . await
185+ . map_err ( |e| format ! ( "Error cancel session: {}" , e) ) ?;
183186
184187 common:: http:: get_response_body_text ( response) . await
185188}
@@ -221,8 +224,8 @@ pub async fn update_session_chat(
221224 None ,
222225 Some ( reqwest:: Body :: from ( serde_json:: to_string ( & body) . unwrap ( ) ) ) ,
223226 )
224- . await
225- . map_err ( |e| format ! ( "Error updating session: {}" , e) ) ?;
227+ . await
228+ . map_err ( |e| format ! ( "Error updating session: {}" , e) ) ?;
226229
227230 Ok ( response. status ( ) . is_success ( ) )
228231}
@@ -250,11 +253,56 @@ pub async fn assistant_search<R: Runtime>(
250253 None ,
251254 Some ( reqwest:: Body :: from ( body. to_string ( ) ) ) ,
252255 )
253- . await
254- . map_err ( |e| format ! ( "Error searching assistants: {}" , e) ) ?;
256+ . await
257+ . map_err ( |e| format ! ( "Error searching assistants: {}" , e) ) ?;
255258
256259 response
257260 . json :: < Value > ( )
258261 . await
259262 . map_err ( |err| err. to_string ( ) )
260263}
264+
265+ #[ tauri:: command]
266+ pub async fn ask_ai < R : Runtime > (
267+ app_handle : AppHandle < R > ,
268+ message : String ,
269+ server_id : String ,
270+ assistant_id : String ,
271+ client_id : String ,
272+ ) -> Result < ( ) , String > {
273+ let body = serde_json:: json!( { "message" : message } ) ;
274+
275+ let path = format ! ( "/assistant/{}/_ask" , assistant_id) ;
276+
277+ println ! ( "Sending request to {}" , & path) ;
278+
279+ let response = HttpClient :: send_request (
280+ server_id. as_str ( ) ,
281+ Method :: POST ,
282+ path. as_str ( ) ,
283+ None ,
284+ None ,
285+ Some ( reqwest:: Body :: from ( body. to_string ( ) ) ) ,
286+ )
287+ . await ?;
288+
289+ if !response. status ( ) . is_success ( ) {
290+ return Err ( format ! ( "Request Failed: {}" , response. status( ) ) ) ;
291+ }
292+
293+ let stream = response. bytes_stream ( ) ;
294+ let reader = tokio_util:: io:: StreamReader :: new (
295+ stream. map_err ( |e| std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , e) ) ,
296+ ) ;
297+ let mut lines = tokio:: io:: BufReader :: new ( reader) . lines ( ) ;
298+
299+ while let Ok ( Some ( line) ) = lines. next_line ( ) . await {
300+ dbg ! ( "Received line: {}" , & line) ;
301+
302+ let _ = app_handle. emit ( & client_id, line) . map_err ( |err| {
303+ println ! ( "Failed to emit: {:?}" , err) ;
304+ } ) ;
305+ }
306+
307+ Ok ( ( ) )
308+ }
0 commit comments