rustvani 0.3.0

Voice AI framework for Rust — real-time speech pipelines with STT, LLM, TTS, and Dhara conversation flows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! Task coordination between agents: dispatch, streaming updates,
//! completion, and cancellation.
//!
//! A [`TaskContext`] is owned by each agent (constructed in
//! `BaseAgent::setup`). `dispatch()` sends a `TaskRequest` over the bus and
//! returns a [`TaskHandle`] the caller can await; incoming responses are
//! routed back through [`TaskContext::route_update`] by the agent's bus
//! message handler.

use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;

use serde_json::Value;
use tokio::sync::{mpsc, oneshot, Mutex};
use tokio::time::timeout;

use crate::error::{PipecatError, Result};

use super::bus::{AgentBus, BusMessage, BusPayload, TaskStatus};
use super::registry::AgentRegistry;

/// Default time `dispatch` waits for a target agent to become ready.
pub const DEFAULT_READY_TIMEOUT: Duration = Duration::from_secs(10);

// ---------------------------------------------------------------------------
// TaskUpdate
// ---------------------------------------------------------------------------

/// One update delivered to a [`TaskHandle`].
#[derive(Debug, Clone)]
pub enum TaskUpdate {
    /// Terminal response — resolves `await_completion`.
    Response {
        /// Terminal status.
        status: TaskStatus,
        /// Arbitrary JSON result.
        response: Option<Value>,
    },
    /// First chunk of a streaming reply.
    StreamStart {
        /// Arbitrary JSON chunk.
        data: Option<Value>,
    },
    /// Intermediate chunk of a streaming reply.
    StreamData {
        /// Arbitrary JSON chunk.
        data: Option<Value>,
    },
    /// Last chunk of a streaming reply.
    StreamEnd {
        /// Arbitrary JSON chunk.
        data: Option<Value>,
    },
    /// Non-terminal progress update.
    Update {
        /// Arbitrary JSON update.
        update: Option<Value>,
    },
}

// ---------------------------------------------------------------------------
// TaskResult
// ---------------------------------------------------------------------------

/// Terminal outcome of a dispatched task.
#[derive(Debug, Clone)]
pub struct TaskResult {
    /// Id of the task.
    pub task_id: String,
    /// Terminal status.
    pub status: TaskStatus,
    /// Arbitrary JSON result.
    pub response: Option<Value>,
}

// ---------------------------------------------------------------------------
// TaskHandle
// ---------------------------------------------------------------------------

/// Caller-side handle for a dispatched task.
pub struct TaskHandle {
    /// Id of the task.
    pub task_id: String,
    /// Name of the agent executing the task.
    pub target_agent: String,
    rx: mpsc::UnboundedReceiver<TaskUpdate>,
}

impl TaskHandle {
    /// Wait for the task to complete with an optional timeout.
    ///
    /// Non-terminal updates (stream chunks, progress) are skipped; the first
    /// `Response` resolves the call. Errors if the channel closes without a
    /// response (e.g. the dispatching agent's context was torn down).
    pub async fn await_completion(
        mut self,
        timeout_duration: Option<Duration>,
    ) -> Result<TaskResult> {
        let task_id = self.task_id.clone();
        loop {
            let update = match timeout_duration {
                Some(d) => timeout(d, self.rx.recv())
                    .await
                    .map_err(|_| PipecatError::pipeline("Task timeout"))?,
                None => self.rx.recv().await,
            };

            match update {
                Some(TaskUpdate::Response { status, response }) => {
                    return Ok(TaskResult {
                        task_id,
                        status,
                        response,
                    })
                }
                Some(_) => continue,
                None => return Err(PipecatError::pipeline("Task did not return a response")),
            }
        }
    }

    /// Stream all updates until a `Response` is received.
    ///
    /// Returns the collected non-terminal updates and the terminal result.
    pub async fn stream_updates(
        self,
        timeout_duration: Option<Duration>,
    ) -> Result<(Vec<TaskUpdate>, TaskResult)> {
        let mut rx = self.rx;
        let task_id = self.task_id.clone();
        let mut updates = Vec::new();

        loop {
            let update = match timeout_duration {
                Some(d) => timeout(d, rx.recv())
                    .await
                    .map_err(|_| PipecatError::pipeline("Task stream timeout"))?,
                None => rx.recv().await,
            };

            match update {
                Some(TaskUpdate::Response { status, response }) => {
                    return Ok((
                        updates,
                        TaskResult {
                            task_id,
                            status,
                            response,
                        },
                    ));
                }
                Some(u) => updates.push(u),
                None => {
                    return Err(PipecatError::pipeline(
                        "Task stream closed without response",
                    ))
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------
// TaskContext
// ---------------------------------------------------------------------------

/// Async callback invoked for each routed update of a watched task.
pub type UpdateHandler =
    Arc<dyn Fn(TaskUpdate) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>;

/// Per-agent task coordination state: pending handles awaiting responses
/// and registered update handlers.
pub struct TaskContext {
    bus: Arc<dyn AgentBus>,
    registry: Arc<AgentRegistry>,
    pending: Mutex<HashMap<String, mpsc::UnboundedSender<TaskUpdate>>>,
    update_handlers: Mutex<HashMap<String, Vec<UpdateHandler>>>,
    // TODO(job-groups): multi-target fan-out with cancel_on_error would
    // track group membership here, keyed alongside `pending`.
}

impl TaskContext {
    /// Create a task context bound to a bus and the agent registry.
    ///
    /// The registry lets [`TaskContext::dispatch`] gate on target readiness
    /// instead of silently dropping requests to agents that have not
    /// subscribed yet.
    pub fn new(bus: Arc<dyn AgentBus>, registry: Arc<AgentRegistry>) -> Self {
        Self {
            bus,
            registry,
            pending: Mutex::new(HashMap::new()),
            update_handlers: Mutex::new(HashMap::new()),
        }
    }

    /// Dispatch a task to a target agent with the default ready timeout
    /// ([`DEFAULT_READY_TIMEOUT`]).
    ///
    /// Thin wrapper around [`TaskContext::dispatch_with`].
    pub async fn dispatch(
        &self,
        source: &str,
        target: &str,
        task_name: Option<String>,
        payload: Option<Value>,
    ) -> Result<TaskHandle> {
        self.dispatch_with(
            source,
            target,
            task_name,
            payload,
            Some(DEFAULT_READY_TIMEOUT),
        )
        .await
    }

    /// Dispatch a task to a target agent.
    ///
    /// If the target is not yet in the registry, waits up to `ready_timeout`
    /// for it to announce readiness (no polling — resolved from a registry
    /// watch). `None` skips the gate entirely and sends immediately.
    /// Returns an error if the timeout elapses before the target is ready.
    pub async fn dispatch_with(
        &self,
        source: &str,
        target: &str,
        task_name: Option<String>,
        payload: Option<Value>,
        ready_timeout: Option<Duration>,
    ) -> Result<TaskHandle> {
        if let Some(wait) = ready_timeout {
            self.await_target_ready(target, wait).await?;
        }

        let task_id = uuid::Uuid::new_v4().to_string();
        let (tx, rx) = mpsc::unbounded_channel();

        {
            let mut pending = self.pending.lock().await;
            pending.insert(task_id.clone(), tx);
        }

        let msg = BusMessage::new(
            source.to_string(),
            Some(target.to_string()),
            BusPayload::TaskRequest {
                task_id: task_id.clone(),
                task_name,
                payload,
            },
        );

        self.bus.send(msg).await;

        Ok(TaskHandle {
            task_id,
            target_agent: target.to_string(),
            rx,
        })
    }

    /// Wait (without polling) until `target` appears in the registry.
    async fn await_target_ready(&self, target: &str, wait: Duration) -> Result<()> {
        if self.registry.get(target).await.is_some() {
            return Ok(());
        }

        // Register a watch that fires a oneshot when the agent registers.
        // The watch also fires immediately if the agent registered between
        // the check above and the watch installation, so there is no race.
        let (ready_tx, ready_rx) = oneshot::channel::<()>();
        let ready_tx = Arc::new(std::sync::Mutex::new(Some(ready_tx)));
        self.registry
            .watch(
                target,
                Arc::new(move |_info| {
                    let ready_tx = ready_tx.clone();
                    Box::pin(async move {
                        if let Some(tx) = ready_tx.lock().unwrap().take() {
                            let _ = tx.send(());
                        }
                    })
                }),
            )
            .await;

        timeout(wait, ready_rx)
            .await
            .map_err(|_| PipecatError::pipeline(format!("Target agent '{}' not ready", target)))?
            .map_err(|_| PipecatError::pipeline(format!("Target agent '{}' not ready", target)))?;
        Ok(())
    }

    /// Drain all pending task handles and registered update handlers.
    ///
    /// Dropping the senders makes every outstanding
    /// [`TaskHandle::await_completion`] / [`TaskHandle::stream_updates`]
    /// resolve with an error instead of hanging forever. Called from agent
    /// lifecycle cleanup when the owning agent ends or is cancelled.
    pub async fn fail_all_pending(&self, reason: &str) {
        let pending: HashMap<_, _> = {
            let mut guard = self.pending.lock().await;
            guard.drain().collect()
        };
        if !pending.is_empty() {
            log::debug!(
                "TaskContext: failing {} pending task(s): {}",
                pending.len(),
                reason
            );
        }
        drop(pending); // dropping the senders closes the receivers
        self.update_handlers.lock().await.clear();
    }

    /// Register a handler for task updates without awaiting completion.
    pub async fn on_update(&self, task_id: &str, handler: UpdateHandler) {
        let mut handlers = self.update_handlers.lock().await;
        handlers
            .entry(task_id.to_string())
            .or_default()
            .push(handler);
    }

    /// Route an incoming task update to pending handles and registered
    /// handlers. The `pending` lock is never held while user handlers run.
    pub async fn route_update(&self, task_id: &str, update: TaskUpdate) {
        // TODO(job-groups): group bookkeeping (cancel_on_error) hooks in here.
        // Send to pending channel.
        {
            let mut pending = self.pending.lock().await;
            if let Some(tx) = pending.get(task_id) {
                let is_final = matches!(update, TaskUpdate::Response { .. });
                let _ = tx.send(update.clone());
                if is_final {
                    pending.remove(task_id);
                }
            }
        }

        // Fire registered handlers (lock released above).
        let handlers: Vec<UpdateHandler> = {
            let h = self.update_handlers.lock().await;
            h.get(task_id).cloned().unwrap_or_default()
        };
        for handler in handlers {
            handler(update.clone()).await;
        }
    }

    /// Send the first chunk of a streaming task reply.
    pub async fn stream_start(
        &self,
        source: &str,
        target: &str,
        task_id: String,
        data: Option<Value>,
    ) {
        let msg = BusMessage::new(
            source.to_string(),
            Some(target.to_string()),
            BusPayload::TaskStreamStart { task_id, data },
        );
        self.bus.send(msg).await;
    }

    /// Send a streaming data chunk for a task.
    pub async fn stream_data(
        &self,
        source: &str,
        target: &str,
        task_id: String,
        data: Option<Value>,
    ) {
        let msg = BusMessage::new(
            source.to_string(),
            Some(target.to_string()),
            BusPayload::TaskStreamData { task_id, data },
        );
        self.bus.send(msg).await;
    }

    /// Send the last chunk of a streaming task reply.
    pub async fn stream_end(
        &self,
        source: &str,
        target: &str,
        task_id: String,
        data: Option<Value>,
    ) {
        let msg = BusMessage::new(
            source.to_string(),
            Some(target.to_string()),
            BusPayload::TaskStreamEnd { task_id, data },
        );
        self.bus.send(msg).await;
    }

    /// Mark a task as complete and send the terminal response.
    pub async fn complete_task(
        &self,
        source: &str,
        target: &str,
        task_id: String,
        status: TaskStatus,
        response: Option<Value>,
    ) {
        let msg = BusMessage::new(
            source.to_string(),
            Some(target.to_string()),
            BusPayload::TaskResponse {
                task_id,
                status,
                response,
            },
        );
        self.bus.send(msg).await;
    }

    /// Request cancellation of a task running on another agent.
    pub async fn cancel_task(
        &self,
        source: &str,
        target: &str,
        task_id: String,
        reason: Option<String>,
    ) {
        let msg = BusMessage::new(
            source.to_string(),
            Some(target.to_string()),
            BusPayload::TaskCancel { task_id, reason },
        );
        self.bus.send(msg).await;
    }

    /// Send an urgent terminal response (system priority).
    pub async fn urgent_response(
        &self,
        source: &str,
        target: &str,
        task_id: String,
        status: TaskStatus,
        response: Option<Value>,
    ) {
        let msg = BusMessage::new(
            source.to_string(),
            Some(target.to_string()),
            BusPayload::TaskResponseUrgent {
                task_id,
                status,
                response,
            },
        );
        self.bus.send(msg).await;
    }

    /// Send an urgent progress update (system priority).
    pub async fn urgent_update(
        &self,
        source: &str,
        target: &str,
        task_id: String,
        update: Option<Value>,
    ) {
        let msg = BusMessage::new(
            source.to_string(),
            Some(target.to_string()),
            BusPayload::TaskUpdateUrgent { task_id, update },
        );
        self.bus.send(msg).await;
    }
}