super-voice-daemon/src/daemon/model_management/post_processor.rs
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-3.0-only |
2 | | //! The transcript post-processor's slot: stage 2. |
3 | | //! |
4 | | //! The post-processor is an ordinary backend model — discovered, instantiated, |
5 | | //! and driven exactly like a transcription model — that occupies its own slot |
6 | | //! ([`SuperSTTDaemon::post_processor`]) and is reached over `POST /v1/process`. |
7 | | //! It is selected independently of the transcription backend, so the two load |
8 | | //! and unload separately and a backend switch never disturbs it. Its loads |
9 | | //! and unloads are every stage's ([`super::stage`]). |
10 | | //! |
11 | | //! Everything here is best-effort by design: a post-processor that fails to |
12 | | //! load costs the user the cleanup, never the transcript (see |
13 | | //! [`SuperSTTDaemon::post_process_final`]). |
14 | | |
15 | | use log::{info, warn}; |
16 | | |
17 | | use crate::daemon::device_management::PipelineStage; |
18 | | use crate::daemon::types::SuperSTTDaemon; |
19 | | use super_engine_daemon::stage::{Load, StageExt}; |
20 | | use super_voice_shared::models::protocol::DaemonResponse; |
21 | | |
22 | | impl SuperSTTDaemon { |
23 | | /// Re-instantiate the loaded post-processor in place so a changed secret or |
24 | | /// option takes effect — the stage-2 twin of |
25 | | /// [`handle_reload_active_model`](SuperSTTDaemon::handle_reload_active_model). |
26 | | /// No-op when stage 2 is idle; rejected during an active recording. |
27 | 2 | pub async fn handle_reload_post_processor(&self) -> DaemonResponse { |
28 | 2 | if let Some(resp) = self.guard_model_mutation("reload the post-processor") { |
29 | 0 | return resp; |
30 | 2 | } |
31 | 2 | match self.stage(PipelineStage::PostProcessor).reload().await { |
32 | | Load::Unchanged => { |
33 | 2 | DaemonResponse::success().with_message("No post-processor to reload".to_string()) |
34 | | } |
35 | | Load::Loaded { .. } => { |
36 | 0 | DaemonResponse::success().with_message("Post-processor reloaded".to_string()) |
37 | | } |
38 | 0 | Load::Superseded => super::superseded("reload"), |
39 | | Load::Cancelled => { |
40 | 0 | DaemonResponse::error("Post-processor reload cancelled due to shutdown") |
41 | | } |
42 | | Load::Failed { error, .. } => { |
43 | 0 | warn!("Post-processor reload failed: {error:#}"); |
44 | 0 | DaemonResponse::error(&format!("Post-processor reload failed: {error:#}")) |
45 | | } |
46 | | } |
47 | 2 | } |
48 | | |
49 | | /// The backend serving the loaded post-processor, if one is loaded. |
50 | 54 | pub(in crate::daemon) async fn post_processor_source(&self) -> Option<String> { |
51 | 54 | self.post_processor |
52 | 54 | .read() |
53 | 54 | .await |
54 | | .as_ref() |
55 | 12 | .map(|l| l.definition.source.clone()) |
56 | 54 | } |
57 | | |
58 | | /// Drop the loaded post-processor, keeping the stage as it is, and |
59 | | /// announce that stage 2 is empty: for a change of backend, which records |
60 | | /// its own state. Nothing is announced when nothing was loaded — an event |
61 | | /// saying a stage went idle should mean it was running. |
62 | | /// |
63 | | /// Any load into the slot in flight finishes first: see |
64 | | /// [`ModelSlot::unload`](super_engine_daemon::model_slot::ModelSlot::unload). |
65 | 16 | pub(in crate::daemon) async fn unload_post_processor_announced(&self) { |
66 | 16 | let Some(unloaded) = self.post_processor.unload().await else { |
67 | 0 | return; |
68 | | }; |
69 | 16 | if unloaded.model.is_some() { |
70 | 1 | info!("Post-processor unloaded"0 ); |
71 | 1 | let loads_on_demand = self |
72 | 1 | .stage(PipelineStage::PostProcessor) |
73 | 1 | .loads_on_demand() |
74 | 1 | .await; |
75 | 1 | self.announce_stage_empty(PipelineStage::PostProcessor, loads_on_demand); |
76 | 15 | } |
77 | 16 | } |
78 | | |
79 | | /// Whether a post-processor is currently loaded. |
80 | 32 | pub(in crate::daemon) async fn post_processor_loaded(&self) -> bool { |
81 | 32 | self.post_processor.read().await.is_some() |
82 | 32 | } |
83 | | } |