@@ -25,6 +25,9 @@ pub enum WsCommand {
2525 Video ( Vec < Vec < u8 > > ) ,
2626 EndResponse ,
2727 EndVad ,
28+ Choices ( String , Vec < String > ) ,
29+ DisplayText ( String ) ,
30+ Close ,
2831}
2932type WsTx = tokio:: sync:: mpsc:: UnboundedSender < WsCommand > ;
3033type WsRx = tokio:: sync:: mpsc:: UnboundedReceiver < WsCommand > ;
@@ -113,6 +116,7 @@ pub enum ClientMsg {
113116 AudioChunk ( Bytes ) ,
114117 Submit ,
115118 Text ( String ) ,
119+ Select ( usize ) ,
116120}
117121
118122pub struct ConnectConfig {
@@ -151,6 +155,11 @@ async fn process_socket_io(
151155
152156 match r {
153157 Some ( WsEvent :: Command ( cmd) ) => {
158+ if matches ! ( cmd, WsCommand :: Close ) {
159+ log:: info!( "Received Close command, closing websocket" ) ;
160+ return Ok ( ( ) ) ;
161+ }
162+
154163 if config. enable_opus {
155164 process_command_with_opus (
156165 socket,
@@ -184,6 +193,12 @@ async fn process_socket_io(
184193 . send ( ClientMsg :: Text ( input) )
185194 . await
186195 . map_err ( |_| anyhow:: anyhow!( "audio_tx closed" ) ) ?,
196+ ProcessMessageResult :: Select ( index) => {
197+ audio_tx
198+ . send ( ClientMsg :: Select ( index) )
199+ . await
200+ . map_err ( |_| anyhow:: anyhow!( "audio_tx closed" ) ) ?;
201+ }
187202 ProcessMessageResult :: Skip => { }
188203 ProcessMessageResult :: StartChat => {
189204 audio_tx
@@ -285,6 +300,18 @@ async fn process_command(ws: &mut WebSocket, cmd: WsCommand) -> anyhow::Result<(
285300 ws. send ( Message :: binary ( audio_chunk) ) . await ?;
286301 }
287302 }
303+ WsCommand :: Choices ( message, items) => {
304+ let choices =
305+ rmp_serde:: to_vec ( & crate :: protocol:: ServerEvent :: Choices { message, items } )
306+ . expect ( "Failed to serialize Choices ServerEvent" ) ;
307+ ws. send ( Message :: binary ( choices) ) . await ?;
308+ }
309+ WsCommand :: DisplayText ( text) => {
310+ let display_text =
311+ rmp_serde:: to_vec ( & crate :: protocol:: ServerEvent :: DisplayText { text } )
312+ . expect ( "Failed to serialize DisplayText ServerEvent" ) ;
313+ ws. send ( Message :: binary ( display_text) ) . await ?;
314+ }
288315 WsCommand :: EndAudio => {
289316 log:: trace!( "EndAudio" ) ;
290317 let end_audio = rmp_serde:: to_vec ( & crate :: protocol:: ServerEvent :: EndAudio )
@@ -306,6 +333,7 @@ async fn process_command(ws: &mut WebSocket, cmd: WsCommand) -> anyhow::Result<(
306333 . expect ( "Failed to serialize EndVad ServerEvent" ) ;
307334 ws. send ( Message :: binary ( end_vad) ) . await ?;
308335 }
336+ WsCommand :: Close => { }
309337 }
310338 Ok ( ( ) )
311339}
@@ -341,12 +369,23 @@ async fn process_command_with_opus(
341369 . expect ( "Failed to serialize ASR ServerEvent" ) ;
342370 ws. send ( Message :: binary ( asr) ) . await ?;
343371 }
344-
345372 WsCommand :: Action { action } => {
346373 let action = rmp_serde:: to_vec ( & crate :: protocol:: ServerEvent :: Action { action } )
347374 . expect ( "Failed to serialize Action ServerEvent" ) ;
348375 ws. send ( Message :: binary ( action) ) . await ?;
349376 }
377+ WsCommand :: Choices ( message, items) => {
378+ let choices =
379+ rmp_serde:: to_vec ( & crate :: protocol:: ServerEvent :: Choices { message, items } )
380+ . expect ( "Failed to serialize Choices ServerEvent" ) ;
381+ ws. send ( Message :: binary ( choices) ) . await ?;
382+ }
383+ WsCommand :: DisplayText ( text) => {
384+ let display_text =
385+ rmp_serde:: to_vec ( & crate :: protocol:: ServerEvent :: DisplayText { text } )
386+ . expect ( "Failed to serialize DisplayText ServerEvent" ) ;
387+ ws. send ( Message :: binary ( display_text) ) . await ?;
388+ }
350389 WsCommand :: StartAudio ( text) => {
351390 log:: trace!( "StartAudio: {text:?}" ) ;
352391 opus_encode
@@ -453,6 +492,7 @@ async fn process_command_with_opus(
453492 . expect ( "Failed to serialize EndVad ServerEvent" ) ;
454493 ws. send ( Message :: binary ( end_vad) ) . await ?;
455494 }
495+ WsCommand :: Close => { }
456496 }
457497 Ok ( ( ) )
458498}
@@ -461,6 +501,7 @@ enum ProcessMessageResult {
461501 Audio ( Bytes ) ,
462502 Submit ,
463503 Text ( String ) ,
504+ Select ( usize ) ,
464505 StartChat ,
465506 Close ,
466507 Skip ,
@@ -478,13 +519,16 @@ fn process_message(msg: Message) -> ProcessMessageResult {
478519 crate :: protocol:: ClientCommand :: Text { input } => {
479520 ProcessMessageResult :: Text ( input)
480521 }
522+ crate :: protocol:: ClientCommand :: Select { index } => {
523+ ProcessMessageResult :: Select ( index)
524+ }
481525 }
482526 } else {
483527 ProcessMessageResult :: Skip
484528 }
485529 }
486530 Message :: Binary ( d) => {
487- log:: debug !( "Received binary message of size: {}" , d. len( ) ) ;
531+ log:: trace !( "Received binary message of size: {}" , d. len( ) ) ;
488532 ProcessMessageResult :: Audio ( d)
489533 }
490534 Message :: Close ( c) => {
0 commit comments