openai_api_rs/realtime/
client_event.rs

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
use serde::{Deserialize, Serialize};
use tokio_tungstenite::tungstenite::Message;

use crate::realtime::types::{Item, Session};

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct SessionUpdate {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub event_id: Option<String>,
    pub session: Session,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct InputAudioBufferAppend {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub event_id: Option<String>,
    pub audio: String,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct InputAudioBufferCommit {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub event_id: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct InputAudioBufferClear {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub event_id: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct ConversationItemCreate {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub event_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub previous_item_id: Option<String>,
    pub item: Item,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct ConversationItemTruncate {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub event_id: Option<String>,
    pub item_id: String,
    pub content_index: u32,
    pub audio_end_ms: u32,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct ConversationItemDelete {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub event_id: Option<String>,
    pub item_id: String,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct ResponseCreate {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub event_id: Option<String>,
    pub response: Option<Session>,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct ResponseCancel {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub event_id: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ClientEvent {
    #[serde(rename = "session.update")]
    SessionUpdate(SessionUpdate),
    #[serde(rename = "input_audio_buffer.append")]
    InputAudioBufferAppend(InputAudioBufferAppend),
    #[serde(rename = "input_audio_buffer.commit")]
    InputAudioBufferCommit(InputAudioBufferCommit),
    #[serde(rename = "input_audio_buffer.clear")]
    InputAudioBufferClear(InputAudioBufferClear),
    #[serde(rename = "conversation.item.create")]
    ConversationItemCreate(ConversationItemCreate),
    #[serde(rename = "conversation.item.truncate")]
    ConversationItemTruncate(ConversationItemTruncate),
    #[serde(rename = "conversation.item.delete")]
    ConversationItemDelete(ConversationItemDelete),
    #[serde(rename = "response.create")]
    ResponseCreate(ResponseCreate),
    #[serde(rename = "response.cancel")]
    ResponseCancel(ResponseCancel),
}

impl From<ClientEvent> for Message {
    fn from(value: ClientEvent) -> Self {
        Message::Text(String::from(&value))
    }
}

impl From<&ClientEvent> for String {
    fn from(value: &ClientEvent) -> Self {
        serde_json::to_string(value).unwrap()
    }
}

impl From<ConversationItemCreate> for Message {
    fn from(value: ConversationItemCreate) -> Self {
        Self::from(ClientEvent::ConversationItemCreate(value))
    }
}

impl From<InputAudioBufferAppend> for Message {
    fn from(value: InputAudioBufferAppend) -> Self {
        Self::from(ClientEvent::InputAudioBufferAppend(value))
    }
}

impl From<InputAudioBufferCommit> for Message {
    fn from(value: InputAudioBufferCommit) -> Self {
        Self::from(ClientEvent::InputAudioBufferCommit(value))
    }
}

impl From<InputAudioBufferClear> for Message {
    fn from(value: InputAudioBufferClear) -> Self {
        Self::from(ClientEvent::InputAudioBufferClear(value))
    }
}

impl From<SessionUpdate> for Message {
    fn from(value: SessionUpdate) -> Self {
        Self::from(ClientEvent::SessionUpdate(value))
    }
}

impl From<ConversationItemTruncate> for Message {
    fn from(value: ConversationItemTruncate) -> Self {
        Self::from(ClientEvent::ConversationItemTruncate(value))
    }
}

impl From<ConversationItemDelete> for Message {
    fn from(value: ConversationItemDelete) -> Self {
        Self::from(ClientEvent::ConversationItemDelete(value))
    }
}

impl From<ResponseCreate> for Message {
    fn from(value: ResponseCreate) -> Self {
        Self::from(ClientEvent::ResponseCreate(value))
    }
}

impl From<ResponseCancel> for Message {
    fn from(value: ResponseCancel) -> Self {
        Self::from(ClientEvent::ResponseCancel(value))
    }
}