Skip to main content

mojentic/realtime/
broker.rs

1//! Realtime voice broker — sibling to [`crate::llm::broker::LlmBroker`].
2
3use crate::llm::tools::{ParallelToolRunner, ToolRunner};
4use crate::realtime::config::RealtimeVoiceConfig;
5use crate::realtime::gateway::RealtimeVoiceGateway;
6use crate::realtime::session::RealtimeSession;
7use crate::realtime::transport::TransportError;
8use crate::tracer::TracerSystem;
9use std::sync::Arc;
10use uuid::Uuid;
11
12/// Long-lived broker that opens duplex realtime sessions against a
13/// gateway. Reusable across many concurrent sessions; the
14/// [`RealtimeSession`] returned by [`connect`] owns the socket
15/// lifetime.
16pub struct RealtimeVoiceBroker {
17    model: String,
18    gateway: Arc<dyn RealtimeVoiceGateway>,
19    config: RealtimeVoiceConfig,
20    tracer: Option<Arc<TracerSystem>>,
21    tool_runner: Arc<dyn ToolRunner>,
22}
23
24impl RealtimeVoiceBroker {
25    pub fn new(
26        model: impl Into<String>,
27        gateway: Arc<dyn RealtimeVoiceGateway>,
28        config: RealtimeVoiceConfig,
29        tracer: Option<Arc<TracerSystem>>,
30        tool_runner: Option<Arc<dyn ToolRunner>>,
31    ) -> Self {
32        Self {
33            model: model.into(),
34            gateway,
35            config,
36            tracer,
37            tool_runner: tool_runner.unwrap_or_else(|| Arc::new(ParallelToolRunner::default())),
38        }
39    }
40
41    /// Open a new realtime session.
42    ///
43    /// Returns the session handle ready to drive: text in / events out
44    /// flow through it. The initial ``session.update`` is sent in the
45    /// background by the session driver before the first event lands.
46    pub async fn connect(&self) -> Result<RealtimeSession, TransportError> {
47        let correlation_id = Uuid::new_v4().to_string();
48        let gateway_session =
49            self.gateway.open(&self.model, &self.config, Some(&correlation_id)).await?;
50
51        Ok(RealtimeSession::run(
52            gateway_session,
53            self.config.clone(),
54            self.tool_runner.clone(),
55            self.tracer.clone(),
56            correlation_id,
57        )
58        .await)
59    }
60}