use std::collections::VecDeque;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use async_trait::async_trait;
use futures::Stream;
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use tokio::sync::Notify;
use crate::audio::{AudioChunk, AudioFormat};
use crate::config::RealtimeConfig;
use crate::error::Result;
use crate::events::{ClientEvent, ServerEvent, ToolResponse};
use crate::model::RealtimeModel;
use crate::session::{BoxedSession, ContextMutationOutcome, RealtimeSession};
pub const DEFAULT_IDLE_TIMEOUT: Duration = Duration::from_millis(500);
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "on", rename_all = "snake_case")]
pub enum ScriptedStep {
Connect {
events: Vec<ServerEvent>,
},
Text {
events: Vec<ServerEvent>,
},
Audio {
events: Vec<ServerEvent>,
},
ToolOutput {
events: Vec<ServerEvent>,
},
Response {
events: Vec<ServerEvent>,
},
}
impl ScriptedStep {
fn trigger(&self) -> Trigger {
match self {
Self::Connect { .. } => Trigger::Connect,
Self::Text { .. } => Trigger::Text,
Self::Audio { .. } => Trigger::Audio,
Self::ToolOutput { .. } => Trigger::ToolOutput,
Self::Response { .. } => Trigger::Response,
}
}
fn into_events(self) -> Vec<ServerEvent> {
match self {
Self::Connect { events }
| Self::Text { events }
| Self::Audio { events }
| Self::ToolOutput { events }
| Self::Response { events } => events,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct ScriptedReply {
pub events: Vec<ServerEvent>,
}
impl ScriptedReply {
pub fn new(events: Vec<ServerEvent>) -> Self {
Self { events }
}
pub fn on_connect(self) -> ScriptedStep {
ScriptedStep::Connect { events: self.events }
}
pub fn on_text(self) -> ScriptedStep {
ScriptedStep::Text { events: self.events }
}
pub fn on_audio(self) -> ScriptedStep {
ScriptedStep::Audio { events: self.events }
}
pub fn on_tool_output(self) -> ScriptedStep {
ScriptedStep::ToolOutput { events: self.events }
}
pub fn on_response(self) -> ScriptedStep {
ScriptedStep::Response { events: self.events }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Trigger {
Connect,
Text,
Audio,
ToolOutput,
Response,
}
#[derive(Debug, Default)]
pub struct ScriptedLog {
texts: Mutex<Vec<String>>,
tool_outputs: Mutex<Vec<ToolResponse>>,
audio_chunks: Mutex<usize>,
responses_requested: Mutex<usize>,
interrupts: Mutex<usize>,
configs: Mutex<Vec<RealtimeConfig>>,
}
impl ScriptedLog {
pub fn texts(&self) -> Vec<String> {
self.texts.lock().clone()
}
pub fn tool_outputs(&self) -> Vec<ToolResponse> {
self.tool_outputs.lock().clone()
}
pub fn audio_chunks(&self) -> usize {
*self.audio_chunks.lock()
}
pub fn responses_requested(&self) -> usize {
*self.responses_requested.lock()
}
pub fn interrupts(&self) -> usize {
*self.interrupts.lock()
}
pub fn configs(&self) -> Vec<RealtimeConfig> {
self.configs.lock().clone()
}
}
#[derive(Debug, Clone)]
pub struct ScriptedRealtimeModel {
script: Arc<Vec<ScriptedStep>>,
model_id: String,
idle_timeout: Duration,
log: Arc<ScriptedLog>,
}
impl ScriptedRealtimeModel {
pub fn new(script: Vec<ScriptedStep>) -> Self {
Self {
script: Arc::new(script),
model_id: "scripted-realtime".to_string(),
idle_timeout: DEFAULT_IDLE_TIMEOUT,
log: Arc::new(ScriptedLog::default()),
}
}
#[must_use]
pub fn with_model_id(mut self, model_id: impl Into<String>) -> Self {
self.model_id = model_id.into();
self
}
#[must_use]
pub fn with_idle_timeout(mut self, timeout: Duration) -> Self {
self.idle_timeout = timeout;
self
}
pub fn log(&self) -> Arc<ScriptedLog> {
self.log.clone()
}
}
#[async_trait]
impl RealtimeModel for ScriptedRealtimeModel {
fn provider(&self) -> &str {
"scripted"
}
fn model_id(&self) -> &str {
&self.model_id
}
fn supports_realtime(&self) -> bool {
true
}
fn supported_input_formats(&self) -> Vec<AudioFormat> {
vec![AudioFormat::pcm16_16khz(), AudioFormat::pcm16_24khz()]
}
fn supported_output_formats(&self) -> Vec<AudioFormat> {
vec![AudioFormat::pcm16_24khz()]
}
fn available_voices(&self) -> Vec<&str> {
vec!["default"]
}
async fn connect(&self, config: RealtimeConfig) -> Result<BoxedSession> {
self.log.configs.lock().push(config);
let session = ScriptedSession {
id: format!("scripted-{}", uuid::Uuid::new_v4()),
script: Mutex::new(self.script.iter().cloned().collect()),
queue: Mutex::new(VecDeque::new()),
notify: Notify::new(),
closed: Mutex::new(false),
idle_timeout: self.idle_timeout,
log: self.log.clone(),
};
session.fire(Trigger::Connect);
Ok(Box::new(session))
}
}
struct ScriptedSession {
id: String,
script: Mutex<VecDeque<ScriptedStep>>,
queue: Mutex<VecDeque<ServerEvent>>,
notify: Notify,
closed: Mutex<bool>,
idle_timeout: Duration,
log: Arc<ScriptedLog>,
}
impl ScriptedSession {
fn fire(&self, trigger: Trigger) {
let step = {
let mut script = self.script.lock();
match script.front() {
Some(step) if step.trigger() == trigger => script.pop_front(),
_ => None,
}
};
if let Some(step) = step {
self.queue.lock().extend(step.into_events());
self.notify.notify_waiters();
self.notify.notify_one();
}
}
}
#[async_trait]
impl RealtimeSession for ScriptedSession {
fn session_id(&self) -> &str {
&self.id
}
fn is_connected(&self) -> bool {
!*self.closed.lock()
}
async fn send_audio(&self, _audio: &AudioChunk) -> Result<()> {
*self.log.audio_chunks.lock() += 1;
self.fire(Trigger::Audio);
Ok(())
}
async fn send_audio_base64(&self, _audio_base64: &str) -> Result<()> {
*self.log.audio_chunks.lock() += 1;
self.fire(Trigger::Audio);
Ok(())
}
async fn send_text(&self, text: &str) -> Result<()> {
self.log.texts.lock().push(text.to_string());
self.fire(Trigger::Text);
Ok(())
}
async fn send_tool_response(&self, response: ToolResponse) -> Result<()> {
self.log.tool_outputs.lock().push(response);
self.fire(Trigger::ToolOutput);
Ok(())
}
async fn send_tool_output(&self, response: ToolResponse) -> Result<()> {
self.log.tool_outputs.lock().push(response);
self.fire(Trigger::ToolOutput);
Ok(())
}
async fn commit_audio(&self) -> Result<()> {
self.fire(Trigger::Audio);
Ok(())
}
async fn clear_audio(&self) -> Result<()> {
Ok(())
}
async fn create_response(&self) -> Result<()> {
*self.log.responses_requested.lock() += 1;
self.fire(Trigger::Response);
Ok(())
}
async fn interrupt(&self) -> Result<()> {
*self.log.interrupts.lock() += 1;
Ok(())
}
async fn send_event(&self, _event: ClientEvent) -> Result<()> {
Ok(())
}
async fn next_event(&self) -> Option<Result<ServerEvent>> {
loop {
if let Some(event) = self.queue.lock().pop_front() {
return Some(Ok(event));
}
if *self.closed.lock() {
return None;
}
let waited = tokio::time::timeout(self.idle_timeout, self.notify.notified()).await;
if waited.is_err() && self.queue.lock().is_empty() {
return None;
}
}
}
fn events(&self) -> Pin<Box<dyn Stream<Item = Result<ServerEvent>> + Send + '_>> {
Box::pin(async_stream::stream! {
while let Some(event) = self.next_event().await {
yield event;
}
})
}
async fn close(&self) -> Result<()> {
*self.closed.lock() = true;
self.notify.notify_waiters();
self.notify.notify_one();
Ok(())
}
async fn mutate_context(&self, _config: RealtimeConfig) -> Result<ContextMutationOutcome> {
Ok(ContextMutationOutcome::Applied)
}
}
pub fn response_created() -> ServerEvent {
ServerEvent::ResponseCreated {
event_id: format!("evt-{}", uuid::Uuid::new_v4()),
response: json!({ "id": "resp-scripted" }),
}
}
pub fn response_done(response: Value) -> ServerEvent {
ServerEvent::ResponseDone { event_id: format!("evt-{}", uuid::Uuid::new_v4()), response }
}
pub fn function_call(call_id: &str, name: &str, args: Value) -> ServerEvent {
ServerEvent::FunctionCallDone {
event_id: format!("evt-{}", uuid::Uuid::new_v4()),
response_id: "resp-scripted".to_string(),
item_id: format!("item-{call_id}"),
output_index: 0,
call_id: call_id.to_string(),
name: name.to_string(),
arguments: args.to_string(),
}
}
pub fn transcript_delta(text: &str) -> ServerEvent {
ServerEvent::TranscriptDelta {
event_id: format!("evt-{}", uuid::Uuid::new_v4()),
response_id: "resp-scripted".to_string(),
item_id: "item-scripted".to_string(),
output_index: 0,
content_index: 0,
delta: text.to_string(),
}
}
pub fn text_delta(text: &str) -> ServerEvent {
ServerEvent::TextDelta {
event_id: format!("evt-{}", uuid::Uuid::new_v4()),
response_id: "resp-scripted".to_string(),
item_id: "item-scripted".to_string(),
output_index: 0,
content_index: 0,
delta: text.to_string(),
}
}
pub fn input_transcript(text: &str) -> ServerEvent {
ServerEvent::InputTranscriptCompleted {
item_id: format!("input-{}", uuid::Uuid::new_v4()),
content_index: 0,
transcript: text.to_string(),
}
}
pub fn spoken(text: &str) -> Vec<ServerEvent> {
vec![
response_created(),
transcript_delta(text),
response_done(json!({ "id": "resp-scripted" })),
]
}
pub fn calls(call_id: &str, name: &str, args: Value) -> Vec<ServerEvent> {
vec![
response_created(),
function_call(call_id, name, args),
response_done(json!({ "id": "resp-scripted" })),
]
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn steps_fire_in_order_on_their_triggers() {
let model = ScriptedRealtimeModel::new(vec![
ScriptedReply::new(vec![response_created()]).on_connect(),
ScriptedReply::new(spoken("one")).on_text(),
ScriptedReply::new(spoken("two")).on_tool_output(),
])
.with_idle_timeout(Duration::from_millis(20));
let session = model.connect(RealtimeConfig::default()).await.unwrap();
session
.send_tool_output(ToolResponse { call_id: "c".into(), output: json!({}) })
.await
.unwrap();
session.send_text("go").await.unwrap();
session
.send_tool_output(ToolResponse { call_id: "c".into(), output: json!({}) })
.await
.unwrap();
let mut transcript = String::new();
let mut count = 0;
while let Some(Ok(event)) = session.next_event().await {
count += 1;
if let ServerEvent::TranscriptDelta { delta, .. } = event {
transcript.push_str(&delta);
}
}
assert_eq!(transcript, "onetwo");
assert_eq!(count, 1 + 3 + 3);
assert_eq!(model.log().tool_outputs().len(), 2);
assert_eq!(model.log().texts(), vec!["go".to_string()]);
}
#[test]
fn a_script_round_trips_through_json() {
let step =
ScriptedReply::new(calls("call-1", "bash", json!({ "command": "ls" }))).on_text();
let text = serde_json::to_string(&[step]).unwrap();
let parsed: Vec<ScriptedStep> = serde_json::from_str(&text).unwrap();
assert!(matches!(&parsed[0], ScriptedStep::Text { events } if events.len() == 3));
}
}