use serde::{Deserialize, Serialize};
use crate::types::audio::{LogProbProperties, TranscriptTextUsageTokens};
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum CreateSpeechResponseStreamEvent {
#[serde(rename = "speech.audio.delta")]
SpeechAudioDelta(SpeechAudioDeltaEvent),
#[serde(rename = "speech.audio.done")]
SpeechAudioDone(SpeechAudioDoneEvent),
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct SpeechAudioDeltaEvent {
pub audio: String,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct SpeechUsage {
pub input_tokens: u32,
pub output_tokens: u32,
pub total_tokens: u32,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct SpeechAudioDoneEvent {
pub usage: SpeechUsage,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct TranscriptionTextSegmentEvent {
pub id: String,
pub start: f32,
pub end: f32,
pub text: String,
pub speaker: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TranscriptionTextDeltaEvent {
pub delta: String,
pub logprobs: Option<Vec<LogProbProperties>>,
pub segment_id: Option<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TranscriptionTextDoneEvent {
pub text: String,
pub logprobs: Option<Vec<LogProbProperties>>,
pub usage: TranscriptTextUsageTokens,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type")]
pub enum CreateTranscriptionResponseStreamEvent {
#[serde(rename = "transcript.text.segment")]
TranscriptTextSegment(TranscriptionTextSegmentEvent),
#[serde(rename = "transcript.text.delta")]
TranscriptTextDelta(TranscriptionTextDeltaEvent),
#[serde(rename = "transcript.text.done")]
TranscriptTextDone(TranscriptionTextDoneEvent),
}
#[cfg(all(feature = "_api", not(target_family = "wasm")))]
pub type TranscriptionResponseStream = std::pin::Pin<
Box<
dyn futures::Stream<
Item = Result<CreateTranscriptionResponseStreamEvent, crate::error::OpenAIError>,
> + Send,
>,
>;
#[cfg(all(feature = "_api", not(target_family = "wasm")))]
pub type SpeechResponseStream = std::pin::Pin<
Box<
dyn futures::Stream<
Item = Result<CreateSpeechResponseStreamEvent, crate::error::OpenAIError>,
> + Send,
>,
>;
#[cfg(feature = "_api")]
macro_rules! impl_event_type {
($($ty:ty => $event_type:expr),* $(,)?) => {
$(
impl crate::traits::EventType for $ty {
fn event_type(&self) -> &'static str {
$event_type
}
}
)*
};
}
#[cfg(feature = "_api")]
impl_event_type! {
SpeechAudioDeltaEvent => "speech.audio.delta",
SpeechAudioDoneEvent => "speech.audio.done",
TranscriptionTextSegmentEvent => "transcript.text.segment",
TranscriptionTextDeltaEvent => "transcript.text.delta",
TranscriptionTextDoneEvent => "transcript.text.done",
}
#[cfg(feature = "_api")]
impl crate::traits::EventType for CreateSpeechResponseStreamEvent {
fn event_type(&self) -> &'static str {
match self {
CreateSpeechResponseStreamEvent::SpeechAudioDelta(event) => event.event_type(),
CreateSpeechResponseStreamEvent::SpeechAudioDone(event) => event.event_type(),
}
}
}
#[cfg(feature = "_api")]
impl crate::traits::EventType for CreateTranscriptionResponseStreamEvent {
fn event_type(&self) -> &'static str {
match self {
CreateTranscriptionResponseStreamEvent::TranscriptTextSegment(event) => {
event.event_type()
}
CreateTranscriptionResponseStreamEvent::TranscriptTextDelta(event) => {
event.event_type()
}
CreateTranscriptionResponseStreamEvent::TranscriptTextDone(event) => event.event_type(),
}
}
}