-
Notifications
You must be signed in to change notification settings - Fork 415
Wrap STT server with actor #1495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,7 +5,7 @@ use bytes::Bytes; | |||||||||||||||||||||||||||||||||||||
| use futures_util::StreamExt; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| use owhisper_interface::{ControlMessage, MixedMessage, Word2}; | ||||||||||||||||||||||||||||||||||||||
| use ractor::{Actor, ActorName, ActorProcessingErr, ActorRef}; | ||||||||||||||||||||||||||||||||||||||
| use ractor::{Actor, ActorName, ActorProcessingErr, ActorRef, SupervisionEvent}; | ||||||||||||||||||||||||||||||||||||||
| use tauri_specta::Event; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| use crate::{manager::TranscriptManager, SessionEvent}; | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -27,6 +27,7 @@ pub struct ListenerArgs { | |||||||||||||||||||||||||||||||||||||
| pub struct ListenerState { | ||||||||||||||||||||||||||||||||||||||
| tx: tokio::sync::mpsc::Sender<MixedMessage<(Bytes, Bytes), ControlMessage>>, | ||||||||||||||||||||||||||||||||||||||
| rx_task: tokio::task::JoinHandle<()>, | ||||||||||||||||||||||||||||||||||||||
| shutdown_tx: Option<tokio::sync::oneshot::Sender<()>>, | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| pub struct ListenerActor; | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -47,8 +48,31 @@ impl Actor for ListenerActor { | |||||||||||||||||||||||||||||||||||||
| myself: ActorRef<Self::Msg>, | ||||||||||||||||||||||||||||||||||||||
| args: Self::Arguments, | ||||||||||||||||||||||||||||||||||||||
| ) -> Result<Self::State, ActorProcessingErr> { | ||||||||||||||||||||||||||||||||||||||
| let (tx, rx_task) = spawn_rx_task(args, myself).await.unwrap(); | ||||||||||||||||||||||||||||||||||||||
| Ok(ListenerState { tx, rx_task }) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| use tauri_plugin_local_stt::LocalSttPluginExt; | ||||||||||||||||||||||||||||||||||||||
| let _ = args.app.start_server(None).await; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| let (tx, rx_task, shutdown_tx) = spawn_rx_task(args, myself).await.unwrap(); | ||||||||||||||||||||||||||||||||||||||
| let state = ListenerState { | ||||||||||||||||||||||||||||||||||||||
| tx, | ||||||||||||||||||||||||||||||||||||||
| rx_task, | ||||||||||||||||||||||||||||||||||||||
| shutdown_tx: Some(shutdown_tx), | ||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Ok(state) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+56
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don’t unwrap
- let (tx, rx_task, shutdown_tx) = spawn_rx_task(args, myself).await.unwrap();
+ let (tx, rx_task, shutdown_tx) = spawn_rx_task(args, myself).await?;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| async fn post_stop( | ||||||||||||||||||||||||||||||||||||||
| &self, | ||||||||||||||||||||||||||||||||||||||
| _myself: ActorRef<Self::Msg>, | ||||||||||||||||||||||||||||||||||||||
| state: &mut Self::State, | ||||||||||||||||||||||||||||||||||||||
| ) -> Result<(), ActorProcessingErr> { | ||||||||||||||||||||||||||||||||||||||
| if let Some(shutdown_tx) = state.shutdown_tx.take() { | ||||||||||||||||||||||||||||||||||||||
| let _ = shutdown_tx.send(()); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| state.rx_task.abort(); | ||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||
yujonglee marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| async fn handle( | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -65,12 +89,21 @@ impl Actor for ListenerActor { | |||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| async fn post_stop( | ||||||||||||||||||||||||||||||||||||||
| async fn handle_supervisor_evt( | ||||||||||||||||||||||||||||||||||||||
| &self, | ||||||||||||||||||||||||||||||||||||||
| _myself: ActorRef<Self::Msg>, | ||||||||||||||||||||||||||||||||||||||
| state: &mut Self::State, | ||||||||||||||||||||||||||||||||||||||
| myself: ActorRef<Self::Msg>, | ||||||||||||||||||||||||||||||||||||||
| message: SupervisionEvent, | ||||||||||||||||||||||||||||||||||||||
| _state: &mut Self::State, | ||||||||||||||||||||||||||||||||||||||
| ) -> Result<(), ActorProcessingErr> { | ||||||||||||||||||||||||||||||||||||||
| state.rx_task.abort(); | ||||||||||||||||||||||||||||||||||||||
| tracing::info!("supervisor_event: {:?}", message); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| match message { | ||||||||||||||||||||||||||||||||||||||
| SupervisionEvent::ActorStarted(_) | SupervisionEvent::ProcessGroupChanged(_) => {} | ||||||||||||||||||||||||||||||||||||||
| SupervisionEvent::ActorTerminated(_, _, _) => {} | ||||||||||||||||||||||||||||||||||||||
| SupervisionEvent::ActorFailed(_cell, _) => { | ||||||||||||||||||||||||||||||||||||||
| myself.stop(None); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -82,10 +115,12 @@ async fn spawn_rx_task( | |||||||||||||||||||||||||||||||||||||
| ( | ||||||||||||||||||||||||||||||||||||||
| tokio::sync::mpsc::Sender<MixedMessage<(Bytes, Bytes), ControlMessage>>, | ||||||||||||||||||||||||||||||||||||||
| tokio::task::JoinHandle<()>, | ||||||||||||||||||||||||||||||||||||||
| tokio::sync::oneshot::Sender<()>, | ||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||
| ActorProcessingErr, | ||||||||||||||||||||||||||||||||||||||
| > { | ||||||||||||||||||||||||||||||||||||||
| let (tx, rx) = tokio::sync::mpsc::channel::<MixedMessage<(Bytes, Bytes), ControlMessage>>(32); | ||||||||||||||||||||||||||||||||||||||
| let (shutdown_tx, mut shutdown_rx) = tokio::sync::oneshot::channel::<()>(); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| let app = args.app.clone(); | ||||||||||||||||||||||||||||||||||||||
| let session_id = args.session_id.clone(); | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -109,7 +144,7 @@ async fn spawn_rx_task( | |||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| let rx_task = tokio::spawn(async move { | ||||||||||||||||||||||||||||||||||||||
| let outbound = tokio_stream::wrappers::ReceiverStream::new(rx); | ||||||||||||||||||||||||||||||||||||||
| let (listen_stream, _handle) = match client.from_realtime_audio(outbound).await { | ||||||||||||||||||||||||||||||||||||||
| let (listen_stream, handle) = match client.from_realtime_audio(outbound).await { | ||||||||||||||||||||||||||||||||||||||
| Ok(res) => res, | ||||||||||||||||||||||||||||||||||||||
| Err(e) => { | ||||||||||||||||||||||||||||||||||||||
| tracing::error!("listen_ws_connect_failed: {:?}", e); | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -122,8 +157,14 @@ async fn spawn_rx_task( | |||||||||||||||||||||||||||||||||||||
| let mut manager = TranscriptManager::with_unix_timestamp(session_start_ts_ms); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| loop { | ||||||||||||||||||||||||||||||||||||||
| match tokio::time::timeout(LISTEN_STREAM_TIMEOUT, listen_stream.next()).await { | ||||||||||||||||||||||||||||||||||||||
| Ok(Some(response)) => { | ||||||||||||||||||||||||||||||||||||||
| tokio::select! { | ||||||||||||||||||||||||||||||||||||||
| _ = &mut shutdown_rx => { | ||||||||||||||||||||||||||||||||||||||
| handle.finalize_with_text(serde_json::json!({"type": "Finalize"}).to_string().into()).await; | ||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| result = tokio::time::timeout(LISTEN_STREAM_TIMEOUT, listen_stream.next()) => { | ||||||||||||||||||||||||||||||||||||||
| match result { | ||||||||||||||||||||||||||||||||||||||
| Ok(Some(response)) => { | ||||||||||||||||||||||||||||||||||||||
| let diff = manager.append(response.clone()); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| let partial_words_by_channel: HashMap<usize, Vec<Word2>> = diff | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -179,21 +220,23 @@ async fn spawn_rx_task( | |||||||||||||||||||||||||||||||||||||
| .emit(&app) | ||||||||||||||||||||||||||||||||||||||
| .unwrap(); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Ok(None) => { | ||||||||||||||||||||||||||||||||||||||
| tracing::info!("listen_stream_ended"); | ||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Err(_) => { | ||||||||||||||||||||||||||||||||||||||
| tracing::info!("listen_stream_timeout"); | ||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||
| Ok(None) => { | ||||||||||||||||||||||||||||||||||||||
| tracing::info!("listen_stream_ended"); | ||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Err(_) => { | ||||||||||||||||||||||||||||||||||||||
| tracing::info!("listen_stream_timeout"); | ||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| myself.stop(None); | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Ok((tx, rx_task)) | ||||||||||||||||||||||||||||||||||||||
| Ok((tx, rx_task, shutdown_tx)) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| async fn update_session<R: tauri::Runtime>( | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.