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"
Instance Method Summary collapse
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
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 |
# File 'lib/clacky/media/openai_compat.rb', line 25 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 |