Class: Kward::Tools::GenerateImage
- Defined in:
- lib/kward/tools/generate_image.rb
Overview
Generates a preview, never a workspace file. The frontend owns export.
Constant Summary collapse
- NAME =
"generate_image".freeze
- MAX_PROMPT_BYTES =
16_384
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #call(args, conversation, cancellation: nil, provider: nil, turn_id: nil) ⇒ Object
-
#initialize(client:, images:) ⇒ GenerateImage
constructor
A new instance of GenerateImage.
Methods inherited from Base
Constructor Details
#initialize(client:, images:) ⇒ GenerateImage
Returns a new instance of GenerateImage.
12 13 14 15 16 17 |
# File 'lib/kward/tools/generate_image.rb', line 12 def initialize(client:, images:) super(NAME, "Generate one PNG when the user asks to create an image. Describe what to draw, not a destination. Kward presents the preview and the user decides whether and where to save it.", properties: { "prompt" => { type: "string", description: "What to draw." } }, required: ["prompt"]) @client = client @images = images end |
Instance Method Details
#call(args, conversation, cancellation: nil, provider: nil, turn_id: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/kward/tools/generate_image.rb', line 19 def call(args, conversation, cancellation: nil, provider: nil, turn_id: nil) prompt = args["prompt"] || args[:prompt] unless prompt.is_a?(String) && !prompt.strip.empty? && prompt.bytesize <= MAX_PROMPT_BYTES return "Error: provide a non-empty image prompt of at most #{MAX_PROMPT_BYTES} bytes." end return "Error: image generation requires a Codex turn." unless provider == "Codex" bytes = @client.generate_codex_image(prompt, turn_id: turn_id, cancellation: cancellation) cancellation&.raise_if_cancelled! image = @images.add(conversation, bytes, cancellation: cancellation) yield image if block_given? "Image generated. The preview is ready for the user to save or discard. No project file was written." rescue CodexImages::Error, GeneratedImages::Error => e "Error: #{e.}" end |