|
| 1 | + |
| 2 | +import Foundation |
| 3 | + |
| 4 | +public enum ClientEvent { |
| 5 | + public struct SessionUpdateEvent: Encodable { |
| 6 | + /// Optional client-generated ID used to identify this event. |
| 7 | + var event_id: String? |
| 8 | + /// Session configuration to update. |
| 9 | + var session: Session |
| 10 | + |
| 11 | + private let type = "session.update" |
| 12 | + } |
| 13 | + |
| 14 | + public struct InputAudioBufferAppendEvent: Encodable { |
| 15 | + /// Optional client-generated ID used to identify this event. |
| 16 | + var event_id: String? |
| 17 | + /// Base64-encoded audio bytes. |
| 18 | + var audio: String |
| 19 | + |
| 20 | + private let type = "input_audio_buffer.append" |
| 21 | + } |
| 22 | + |
| 23 | + public struct InputAudioBufferCommitEvent: Encodable { |
| 24 | + /// Optional client-generated ID used to identify this event. |
| 25 | + var event_id: String? |
| 26 | + |
| 27 | + private let type = "input_audio_buffer.commit" |
| 28 | + } |
| 29 | + |
| 30 | + public struct InputAudioBufferClearEvent: Encodable { |
| 31 | + /// Optional client-generated ID used to identify this event. |
| 32 | + var event_id: String? |
| 33 | + |
| 34 | + private let type = "input_audio_buffer.clear" |
| 35 | + } |
| 36 | + |
| 37 | + public struct ConversationItemCreateEvent: Encodable { |
| 38 | + /// Optional client-generated ID used to identify this event. |
| 39 | + var event_id: String? |
| 40 | + /// The ID of the preceding item after which the new item will be inserted. |
| 41 | + var previous_item_id: String? |
| 42 | + /// The item to add to the conversation. |
| 43 | + var item: Item |
| 44 | + |
| 45 | + private let type = "conversation.item.create" |
| 46 | + } |
| 47 | + |
| 48 | + public struct ConversationItemTruncateEvent: Encodable { |
| 49 | + /// Optional client-generated ID used to identify this event. |
| 50 | + var event_id: String? |
| 51 | + /// The ID of the assistant message item to truncate. |
| 52 | + var item_id: String? |
| 53 | + /// The index of the content part to truncate. |
| 54 | + var content_index: Int |
| 55 | + /// Inclusive duration up to which audio is truncated, in milliseconds. |
| 56 | + var audio_end_ms: Int |
| 57 | + |
| 58 | + private let type = "conversation.item.truncate" |
| 59 | + } |
| 60 | + |
| 61 | + public struct ConversationItemDeleteEvent: Encodable { |
| 62 | + /// Optional client-generated ID used to identify this event. |
| 63 | + var event_id: String? |
| 64 | + /// The ID of the assistant message item to truncate. |
| 65 | + var item_id: String? |
| 66 | + /// The index of the content part to truncate. |
| 67 | + var content_index: Int |
| 68 | + /// Inclusive duration up to which audio is truncated, in milliseconds. |
| 69 | + var audio_end_ms: Int |
| 70 | + |
| 71 | + private let type = "conversation.item.delete" |
| 72 | + } |
| 73 | + |
| 74 | + public struct ResponseCreateEvent: Encodable { |
| 75 | + /// Optional client-generated ID used to identify this event. |
| 76 | + var event_id: String? |
| 77 | + /// Configuration for the response. |
| 78 | + var response: Response.Config? |
| 79 | + |
| 80 | + private let type = "response.create" |
| 81 | + } |
| 82 | + |
| 83 | + public struct ResponseCancelEvent: Encodable { |
| 84 | + /// Optional client-generated ID used to identify this event. |
| 85 | + var event_id: String? |
| 86 | + |
| 87 | + private let type = "response.cancel" |
| 88 | + } |
| 89 | + |
| 90 | + /// Send this event to update the session’s default configuration. |
| 91 | + case updateSession(SessionUpdateEvent) |
| 92 | + /// Send this event to append audio bytes to the input audio buffer. |
| 93 | + case appendInputAudioBuffer(InputAudioBufferAppendEvent) |
| 94 | + /// Send this event to commit audio bytes to a user message. |
| 95 | + case commitInputAudioBuffer(InputAudioBufferCommitEvent) |
| 96 | + /// Send this event to clear the audio bytes in the buffer. |
| 97 | + case clearInputAudioBuffer(InputAudioBufferClearEvent) |
| 98 | + /// Send this event when adding an item to the conversation. |
| 99 | + case createConversationItem(ConversationItemCreateEvent) |
| 100 | + /// Send this event when you want to truncate a previous assistant message’s audio. |
| 101 | + case truncateConversationItem(ConversationItemTruncateEvent) |
| 102 | + /// Send this event when you want to remove any item from the conversation history. |
| 103 | + case deleteConversationItem(ConversationItemDeleteEvent) |
| 104 | + /// Send this event to trigger a response generation. |
| 105 | + case createResponse(ResponseCreateEvent) |
| 106 | + /// Send this event to cancel an in-progress response. |
| 107 | + case cancelResponse(ResponseCancelEvent) |
| 108 | +} |
| 109 | + |
| 110 | +extension ClientEvent { |
| 111 | + public static func updateSession(id: String? = nil, _ session: Session) -> Self { |
| 112 | + .updateSession(SessionUpdateEvent(event_id: id, session: session)) |
| 113 | + } |
| 114 | + |
| 115 | + public static func appendInputAudioBuffer(id: String? = nil, encoding audio: Data) -> Self { |
| 116 | + .appendInputAudioBuffer(InputAudioBufferAppendEvent(event_id: id, audio: audio.base64EncodedString())) |
| 117 | + } |
| 118 | + |
| 119 | + public static func commitInputAudioBuffer(id: String? = nil) -> Self { |
| 120 | + .commitInputAudioBuffer(InputAudioBufferCommitEvent(event_id: id)) |
| 121 | + } |
| 122 | + |
| 123 | + public static func clearInputAudioBuffer(id: String? = nil) -> Self { |
| 124 | + .clearInputAudioBuffer(InputAudioBufferClearEvent(event_id: id)) |
| 125 | + } |
| 126 | + |
| 127 | + public static func createConversationItem(id: String? = nil, previous previousID: String? = nil, _ item: Item) -> Self { |
| 128 | + .createConversationItem(ConversationItemCreateEvent(event_id: id, previous_item_id: previousID, item: item)) |
| 129 | + } |
| 130 | + |
| 131 | + public static func truncateConversationItem(id event_id: String? = nil, for id: String? = nil, at index: Int, at_audio audio_index: Int) -> Self { |
| 132 | + .truncateConversationItem(ConversationItemTruncateEvent(event_id: event_id, item_id: id, content_index: index, audio_end_ms: audio_index)) |
| 133 | + } |
| 134 | + |
| 135 | + public static func deleteConversationItem(id event_id: String? = nil, for id: String? = nil, at index: Int, at_audio audio_index: Int) -> Self { |
| 136 | + .deleteConversationItem(ConversationItemDeleteEvent(event_id: event_id, item_id: id, content_index: index, audio_end_ms: audio_index)) |
| 137 | + } |
| 138 | + |
| 139 | + public static func createResponse(id: String? = nil, _ response: Response.Config? = nil) -> Self { |
| 140 | + .createResponse(ResponseCreateEvent(event_id: id, response: response)) |
| 141 | + } |
| 142 | + |
| 143 | + public static func cancelResponse(id: String? = nil) -> Self { |
| 144 | + .cancelResponse(ResponseCancelEvent(event_id: id)) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +extension ClientEvent: Encodable { |
| 149 | + private enum CodingKeys: String, CodingKey { |
| 150 | + case type |
| 151 | + } |
| 152 | + |
| 153 | + public func encode(to encoder: Encoder) throws { |
| 154 | + switch self { |
| 155 | + case let .updateSession(event): |
| 156 | + try event.encode(to: encoder) |
| 157 | + case let .appendInputAudioBuffer(event): |
| 158 | + try event.encode(to: encoder) |
| 159 | + case let .commitInputAudioBuffer(event): |
| 160 | + try event.encode(to: encoder) |
| 161 | + case let .clearInputAudioBuffer(event): |
| 162 | + try event.encode(to: encoder) |
| 163 | + case let .createConversationItem(event): |
| 164 | + try event.encode(to: encoder) |
| 165 | + case let .truncateConversationItem(event): |
| 166 | + try event.encode(to: encoder) |
| 167 | + case let .deleteConversationItem(event): |
| 168 | + try event.encode(to: encoder) |
| 169 | + case let .createResponse(event): |
| 170 | + try event.encode(to: encoder) |
| 171 | + case let .cancelResponse(event): |
| 172 | + try event.encode(to: encoder) |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments