use crate::{
realtime_conversation::RealtimeConversationItem,
realtime_events::{ClientEvent, SessionConfig},
realtime_response::RealtimeResponseCreateParams,
};
impl ClientEvent {
pub fn session_update(event_id: Option<String>, session: SessionConfig) -> Self {
Self::SessionUpdate {
event_id,
session: Box::new(session),
}
}
pub fn conversation_item_create(
event_id: Option<String>,
previous_item_id: Option<String>,
item: RealtimeConversationItem,
) -> Self {
Self::ConversationItemCreate {
event_id,
previous_item_id,
item,
}
}
pub fn conversation_item_delete(event_id: Option<String>, item_id: impl Into<String>) -> Self {
Self::ConversationItemDelete {
event_id,
item_id: item_id.into(),
}
}
pub fn conversation_item_retrieve(
event_id: Option<String>,
item_id: impl Into<String>,
) -> Self {
Self::ConversationItemRetrieve {
event_id,
item_id: item_id.into(),
}
}
pub fn conversation_item_truncate(
event_id: Option<String>,
item_id: impl Into<String>,
content_index: u32,
audio_end_ms: u32,
) -> Self {
Self::ConversationItemTruncate {
event_id,
item_id: item_id.into(),
content_index,
audio_end_ms,
}
}
pub fn input_audio_buffer_append(event_id: Option<String>, audio: impl Into<String>) -> Self {
Self::InputAudioBufferAppend {
event_id,
audio: audio.into(),
}
}
pub fn input_audio_buffer_clear(event_id: Option<String>) -> Self {
Self::InputAudioBufferClear { event_id }
}
pub fn input_audio_buffer_commit(event_id: Option<String>) -> Self {
Self::InputAudioBufferCommit { event_id }
}
pub fn output_audio_buffer_clear(event_id: Option<String>) -> Self {
Self::OutputAudioBufferClear { event_id }
}
pub fn response_cancel(event_id: Option<String>, response_id: Option<String>) -> Self {
Self::ResponseCancel {
event_id,
response_id,
}
}
pub fn response_create(
event_id: Option<String>,
response: Option<RealtimeResponseCreateParams>,
) -> Self {
Self::ResponseCreate {
event_id,
response: response.map(Box::new),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::realtime_session::{
OutputModality, RealtimeSessionCreateRequest, RealtimeSessionType,
};
#[test]
fn test_session_update() {
let config = SessionConfig::Realtime(Box::new(RealtimeSessionCreateRequest {
r#type: RealtimeSessionType::Realtime,
output_modalities: Some(vec![OutputModality::Audio]),
model: None,
instructions: None,
audio: None,
include: None,
tracing: None,
tools: None,
tool_choice: None,
max_output_tokens: None,
truncation: None,
prompt: None,
}));
let event = ClientEvent::session_update(Some("evt_1".into()), config);
assert_eq!(event.event_type(), "session.update");
}
#[test]
fn test_response_create_empty() {
let event = ClientEvent::response_create(None, None);
assert_eq!(event.event_type(), "response.create");
}
}