Class: PiAgent::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/pi_agent/image.rb

Overview

An image attachment for a prompt/steer/follow_up message.

Serializes to the pi RPC ImageContent shape:

{ "type" => "image", "data" => <base64>, "mimeType" => <mime> }

The pi prompt commands accept image attachments only; other file types are not part of the prompt wire protocol.

Constant Summary collapse

MIME_BY_EXTENSION =
{
  ".png" => "image/png",
  ".jpg" => "image/jpeg",
  ".jpeg" => "image/jpeg",
  ".gif" => "image/gif",
  ".webp" => "image/webp"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, mime_type:) ⇒ Image

‘data` must already be base64-encoded image data.



39
40
41
42
# File 'lib/pi_agent/image.rb', line 39

def initialize(data:, mime_type:)
  @data = data
  @mime_type = mime_type
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



22
23
24
# File 'lib/pi_agent/image.rb', line 22

def data
  @data
end

#mime_typeObject (readonly)

Returns the value of attribute mime_type.



22
23
24
# File 'lib/pi_agent/image.rb', line 22

def mime_type
  @mime_type
end

Class Method Details

.from_bytes(bytes, mime_type:) ⇒ Object

Build from raw binary image bytes.



34
35
36
# File 'lib/pi_agent/image.rb', line 34

def self.from_bytes(bytes, mime_type:)
  new(data: Base64.strict_encode64(bytes), mime_type: mime_type)
end

.from_file(path) ⇒ Object

Build from a file on disk. MIME type is inferred from the extension.

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
# File 'lib/pi_agent/image.rb', line 25

def self.from_file(path)
  ext = File.extname(path).downcase
  mime = MIME_BY_EXTENSION[ext]
  raise ArgumentError, "Unsupported image extension #{ext.inspect} (#{path})" unless mime

  from_bytes(File.binread(path), mime_type: mime)
end

Instance Method Details

#to_hObject



44
45
46
# File 'lib/pi_agent/image.rb', line 44

def to_h
  { "type" => "image", "data" => @data, "mimeType" => @mime_type }
end