|
| 1 | +import Foundation |
| 2 | + |
| 3 | +public struct SessionObject: Codable { |
| 4 | + /// The unique identifier for the session. |
| 5 | + public let id: String |
| 6 | + |
| 7 | + /// The object type, which is always "realtime.session". |
| 8 | + public let object: String |
| 9 | + |
| 10 | + /// The model used for this session. |
| 11 | + public let model: String |
| 12 | + |
| 13 | + /// The set of modalities the model can respond with. |
| 14 | + public let modalities: [Modality] |
| 15 | + |
| 16 | + /// The default system instructions (i.e. system message) prepended to model calls. |
| 17 | + public let instructions: String? |
| 18 | + |
| 19 | + /// The voice the model uses to respond. |
| 20 | + public let voice: String? |
| 21 | + |
| 22 | + /// The format of input audio. |
| 23 | + public let inputAudioFormat: AudioFormat? |
| 24 | + |
| 25 | + /// The format of output audio. |
| 26 | + public let outputAudioFormat: AudioFormat? |
| 27 | + |
| 28 | + /// Configuration for input audio transcription. |
| 29 | + public let inputAudioTranscription: InputAudioTranscription? |
| 30 | + |
| 31 | + /// Configuration for turn detection. |
| 32 | + public let turnDetection: TurnDetection? |
| 33 | + |
| 34 | + /// Tools (functions) available to the model. |
| 35 | + public let tools: [Tool] |
| 36 | + |
| 37 | + /// How the model chooses tools. |
| 38 | + public let toolChoice: ToolChoice |
| 39 | + |
| 40 | + /// Sampling temperature for the model. |
| 41 | + public let temperature: Double? |
| 42 | + |
| 43 | + /// Maximum number of output tokens for a single assistant response. |
| 44 | + public let maxResponseOutputTokens: MaxOutputTokens? |
| 45 | + |
| 46 | + /// The client secret containing the ephemeral API token. |
| 47 | + public let clientSecret: ClientSecret |
| 48 | + |
| 49 | + public enum AudioFormat: String, Codable { |
| 50 | + case pcm16 = "pcm16" |
| 51 | + case g711Ulaw = "g711_ulaw" |
| 52 | + case g711Alaw = "g711_alaw" |
| 53 | + } |
| 54 | + |
| 55 | + public struct InputAudioNoiseReduction: Codable { |
| 56 | + public let type: String? |
| 57 | + |
| 58 | + public init(type: String? = nil) { |
| 59 | + self.type = type |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public struct InputAudioTranscription: Codable { |
| 64 | + public let model: String? |
| 65 | + public let language: String? |
| 66 | + public let prompt: String? |
| 67 | + |
| 68 | + public init(model: String? = nil, language: String? = nil, prompt: String? = nil) { |
| 69 | + self.model = model |
| 70 | + self.language = language |
| 71 | + self.prompt = prompt |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public enum Modality: String, Codable { |
| 76 | + case text = "text" |
| 77 | + case audio = "audio" |
| 78 | + } |
| 79 | + |
| 80 | + public struct TurnDetection: Codable { |
| 81 | + public let createResponse: Bool? |
| 82 | + public let eagerness: TurnDetectionEagerness? |
| 83 | + public let interruptResponse: Bool? |
| 84 | + public let prefixPaddingMs: Int? |
| 85 | + public let silenceDurationMs: Int? |
| 86 | + public let threshold: Double? |
| 87 | + public let type: TurnDetectionType |
| 88 | + |
| 89 | + public enum TurnDetectionType: String, Codable, Sendable { |
| 90 | + case serverVad = "server_vad" |
| 91 | + case semanticVad = "semantic_vad" |
| 92 | + case none |
| 93 | + } |
| 94 | + |
| 95 | + public enum TurnDetectionEagerness: String, Codable, Sendable { |
| 96 | + case low |
| 97 | + case high |
| 98 | + case auto |
| 99 | + case medium |
| 100 | + } |
| 101 | + |
| 102 | + enum CodingKeys: String, CodingKey { |
| 103 | + case createResponse = "create_response" |
| 104 | + case eagerness |
| 105 | + case interruptResponse = "interrupt_response" |
| 106 | + case prefixPaddingMs = "prefix_padding_ms" |
| 107 | + case silenceDurationMs = "silence_duration_ms" |
| 108 | + case threshold |
| 109 | + case type |
| 110 | + } |
| 111 | + |
| 112 | + public init( |
| 113 | + type: TurnDetectionType, |
| 114 | + createResponse: Bool? = nil, |
| 115 | + eagerness: TurnDetectionEagerness? = nil, |
| 116 | + interruptResponse: Bool? = nil, |
| 117 | + prefixPaddingMs: Int? = nil, |
| 118 | + silenceDurationMs: Int? = nil, |
| 119 | + threshold: Double? = nil |
| 120 | + ) { |
| 121 | + self.type = type |
| 122 | + self.createResponse = createResponse |
| 123 | + self.eagerness = eagerness |
| 124 | + self.interruptResponse = interruptResponse |
| 125 | + self.prefixPaddingMs = prefixPaddingMs |
| 126 | + self.silenceDurationMs = silenceDurationMs |
| 127 | + self.threshold = threshold |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public struct ClientSecret: Codable { |
| 132 | + /// The ephemeral API token value. |
| 133 | + public let value: String |
| 134 | + |
| 135 | + /// The Unix timestamp (in seconds) when the token expires. |
| 136 | + public let expiresAt: Int |
| 137 | + |
| 138 | + private enum CodingKeys: String, CodingKey { |
| 139 | + case value |
| 140 | + case expiresAt = "expires_at" |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public enum MaxOutputTokens: Codable { |
| 145 | + case inf |
| 146 | + case int(Int) |
| 147 | + |
| 148 | + public init(from decoder: Decoder) throws { |
| 149 | + let container = try decoder.singleValueContainer() |
| 150 | + if let intValue = try? container.decode(Int.self) { |
| 151 | + self = .int(intValue) |
| 152 | + } else if let stringValue = try? container.decode(String.self), stringValue == "inf" { |
| 153 | + self = .inf |
| 154 | + } else { |
| 155 | + throw DecodingError.typeMismatch(MaxOutputTokens.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for MaxOutputTokens")) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + public func encode(to encoder: Encoder) throws { |
| 160 | + var container = encoder.singleValueContainer() |
| 161 | + switch self { |
| 162 | + case .inf: |
| 163 | + try container.encode("inf") |
| 164 | + case .int(let value): |
| 165 | + try container.encode(value) |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private enum CodingKeys: String, CodingKey { |
| 171 | + case id |
| 172 | + case object |
| 173 | + case model |
| 174 | + case modalities |
| 175 | + case instructions |
| 176 | + case voice |
| 177 | + case inputAudioFormat = "input_audio_format" |
| 178 | + case outputAudioFormat = "output_audio_format" |
| 179 | + case inputAudioTranscription = "input_audio_transcription" |
| 180 | + case turnDetection = "turn_detection" |
| 181 | + case tools |
| 182 | + case toolChoice = "tool_choice" |
| 183 | + case temperature |
| 184 | + case maxResponseOutputTokens = "max_response_output_tokens" |
| 185 | + case clientSecret = "client_secret" |
| 186 | + } |
| 187 | +} |
0 commit comments