Class: Clacky::Media::OpenAICompat
- Defined in:
- lib/clacky/media/openai_compat.rb
Overview
OpenAI-compatible image generation provider.
Talks to POST <base_url>/images/generations with the standard OpenAI request shape. Handles three providers under one class because they all expose the same endpoint: OpenAI, OpenRouter, and the openclacky platform gateway. Provider-specific quirks (model id naming, billing) live in PRESETS, not here.
Constant Summary collapse
- ASPECT_TO_SIZE =
{ "landscape" => "1536x1024", "square" => "1024x1024", "portrait" => "1024x1536" }.freeze
- DEFAULT_ASPECT =
"landscape"- VIDEO_ASPECTS =
Video aspect ratios accepted by the gateway’s /videos/generations endpoint. The human-friendly labels map straight through; the gateway normalises to Veo’s “16:9” / “9:16” internally.
%w[landscape portrait].freeze
- DEFAULT_VIDEO_DURATION =
8
Instance Method Summary collapse
- #generate_image(prompt:, aspect_ratio: DEFAULT_ASPECT, output_dir: nil, n: 1, **_kwargs) ⇒ Object
- #generate_speech(input:, voice: nil, output_dir: nil, **_kwargs) ⇒ Object
- #generate_video(prompt:, aspect_ratio: DEFAULT_ASPECT, duration_seconds: nil, output_dir: nil, image: nil, **_kwargs) ⇒ Object
Methods inherited from Base
Constructor Details
This class inherits a constructor from Clacky::Media::Base
Instance Method Details
#generate_image(prompt:, aspect_ratio: DEFAULT_ASPECT, output_dir: nil, n: 1, **_kwargs) ⇒ Object
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 |
# File 'lib/clacky/media/openai_compat.rb', line 31 def generate_image(prompt:, aspect_ratio: DEFAULT_ASPECT, output_dir: nil, n: 1, **_kwargs) provider_id = Clacky::Providers.find_by_base_url(@base_url) || "custom" aspect = ASPECT_TO_SIZE.key?(aspect_ratio) ? aspect_ratio : DEFAULT_ASPECT size = ASPECT_TO_SIZE[aspect] if prompt.to_s.strip.empty? return error_response( error: "Prompt is required and must be a non-empty string", error_type: "invalid_argument", provider: provider_id, aspect_ratio: aspect ) end if @api_key.to_s.empty? return error_response( error: "api_key not configured for image model '#{@model}'", error_type: "auth_required", provider: provider_id, prompt: prompt, aspect_ratio: aspect ) end payload = { model: @model, n: n } if gemini_family?(@model) # Gemini image models (routed via openclacky / openrouter gateway) # don't accept the OpenAI `size` parameter — they infer aspect from # the prompt text. Embedding a hint keeps the user's aspect choice # honoured without breaking the gateway request validator. payload[:prompt] = "#{prompt}\n\n[aspect: #{aspect}]" else payload[:prompt] = prompt payload[:size] = size end begin response = connection.post("images/generations") do |req| req.headers["Content-Type"] = "application/json" req.headers["Authorization"] = "Bearer #{@api_key}" req.body = JSON.generate(payload) end rescue Faraday::Error => e return error_response( error: "HTTP request failed: #{e.}", error_type: "network_error", provider: provider_id, prompt: prompt, aspect_ratio: aspect ) end unless response.success? return error_response( error: "Upstream #{response.status}: #{truncate(response.body, 500)}", error_type: "api_error", provider: provider_id, prompt: prompt, aspect_ratio: aspect ) end body = parse_json(response.body) return error_response( error: "Invalid JSON response from upstream", error_type: "invalid_response", provider: provider_id, prompt: prompt, aspect_ratio: aspect ) unless body.is_a?(Hash) data = body["data"] || [] first = data.first if first.nil? return error_response( error: "Upstream returned no image data", error_type: "empty_response", provider: provider_id, prompt: prompt, aspect_ratio: aspect ) end image_ref = if first["b64_json"] save_b64_image(first["b64_json"], output_dir: output_dir || Dir.pwd, prefix: "img") elsif first["url"] first["url"] end if image_ref.nil? return error_response( error: "Response contained neither b64_json nor url", error_type: "empty_response", provider: provider_id, prompt: prompt, aspect_ratio: aspect ) end success_response( image: image_ref, prompt: prompt, aspect_ratio: aspect, provider: provider_id, extra: { "size" => size, "usage" => body["usage"], "cost_usd" => body["cost_usd"] }.compact ) end |
#generate_speech(input:, voice: nil, output_dir: nil, **_kwargs) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/clacky/media/openai_compat.rb', line 211 def generate_speech(input:, voice: nil, output_dir: nil, **_kwargs) provider_id = Clacky::Providers.find_by_base_url(@base_url) || "custom" if input.to_s.strip.empty? return audio_error_response( error: "input is required and must be a non-empty string", error_type: "invalid_argument", provider: provider_id, voice: voice.to_s ) end if @api_key.to_s.empty? return audio_error_response( error: "api_key not configured for audio model '#{@model}'", error_type: "auth_required", provider: provider_id, input: input, voice: voice.to_s ) end payload = { model: @model, input: input } payload[:voice] = voice if voice && !voice.to_s.strip.empty? begin response = audio_connection.post("audio/speech") do |req| req.headers["Content-Type"] = "application/json" req.headers["Authorization"] = "Bearer #{@api_key}" req.body = JSON.generate(payload) end rescue Faraday::Error => e return audio_error_response( error: "HTTP request failed: #{e.}", error_type: "network_error", provider: provider_id, input: input, voice: voice.to_s ) end unless response.success? return audio_error_response( error: "Upstream #{response.status}: #{truncate(response.body, 500)}", error_type: "api_error", provider: provider_id, input: input, voice: voice.to_s ) end body = parse_json(response.body) return audio_error_response( error: "Invalid JSON response from upstream", error_type: "invalid_response", provider: provider_id, input: input, voice: voice.to_s ) unless body.is_a?(Hash) first = (body["data"] || []).first if first.nil? || first["b64_json"].to_s.empty? return audio_error_response( error: "Upstream returned no audio data", error_type: "empty_response", provider: provider_id, input: input, voice: voice.to_s ) end ext = case first["mime_type"].to_s when "audio/mpeg", "audio/mp3" then "mp3" when "audio/ogg" then "ogg" else "wav" end path = save_b64_audio(first["b64_json"], output_dir: output_dir || Dir.pwd, prefix: "tts", extension: ext) audio_success_response( audio: path, input: input, voice: body["voice"] || voice.to_s, provider: provider_id, extra: { "mime_type" => first["mime_type"], "usage" => body["usage"], "cost_usd" => body["cost_usd"] }.compact ) end |
#generate_video(prompt:, aspect_ratio: DEFAULT_ASPECT, duration_seconds: nil, output_dir: nil, image: nil, **_kwargs) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/clacky/media/openai_compat.rb', line 144 def generate_video(prompt:, aspect_ratio: DEFAULT_ASPECT, duration_seconds: nil, output_dir: nil, image: nil, **_kwargs) provider_id = Clacky::Providers.find_by_base_url(@base_url) || "custom" aspect = VIDEO_ASPECTS.include?(aspect_ratio) ? aspect_ratio : DEFAULT_ASPECT duration = duration_seconds.to_i duration = DEFAULT_VIDEO_DURATION if duration <= 0 if prompt.to_s.strip.empty? return video_error_response( error: "Prompt is required and must be a non-empty string", error_type: "invalid_argument", provider: provider_id, aspect_ratio: aspect ) end if @api_key.to_s.empty? return video_error_response( error: "api_key not configured for video model '#{@model}'", error_type: "auth_required", provider: provider_id, prompt: prompt, aspect_ratio: aspect ) end payload = { model: @model, prompt: prompt, aspect_ratio: aspect, duration_seconds: duration } payload[:image] = image if image.is_a?(Hash) && image["b64_json"] begin response = video_connection.post("videos/generations") do |req| req.headers["Content-Type"] = "application/json" req.headers["Authorization"] = "Bearer #{@api_key}" req.body = JSON.generate(payload) end rescue Faraday::Error => e return video_error_response( error: "HTTP request failed: #{e.}", error_type: "network_error", provider: provider_id, prompt: prompt, aspect_ratio: aspect ) end unless response.success? return video_error_response( error: "Upstream #{response.status}: #{truncate(response.body, 500)}", error_type: "api_error", provider: provider_id, prompt: prompt, aspect_ratio: aspect ) end body = parse_json(response.body) return video_error_response( error: "Invalid JSON response from upstream", error_type: "invalid_response", provider: provider_id, prompt: prompt, aspect_ratio: aspect ) unless body.is_a?(Hash) first = (body["data"] || []).first if first.nil? || first["b64_json"].to_s.empty? return video_error_response( error: "Upstream returned no video data", error_type: "empty_response", provider: provider_id, prompt: prompt, aspect_ratio: aspect ) end path = save_b64_video(first["b64_json"], output_dir: output_dir || Dir.pwd, prefix: "vid") video_success_response( video: path, prompt: prompt, aspect_ratio: aspect, provider: provider_id, extra: { "duration_seconds" => duration, "usage" => body["usage"], "cost_usd" => body["cost_usd"] }.compact ) end |