| 1 | package Langertha::Content::Image; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Canonical image content block with cross-provider conversion (OpenAI / Anthropic / Gemini) | ||||||||||||||||||||||||||||
| 3 | 1 | our $VERSION = '0.502'; | |||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use MIME::Base64 qw( encode_base64 decode_base64 ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | with 'Langertha::Content'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | has url => ( | |||||||||||||||||||||||||||
| 12 | is => 'ro', | ||||||||||||||||||||||||||||
| 13 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||
| 14 | predicate => 'has_url', | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | has base64 => ( | |||||||||||||||||||||||||||
| 19 | is => 'rw', | ||||||||||||||||||||||||||||
| 20 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||
| 21 | predicate => 'has_base64', | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | has media_type => ( | |||||||||||||||||||||||||||
| 26 | is => 'rw', | ||||||||||||||||||||||||||||
| 27 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||
| 28 | predicate => 'has_media_type', | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | sub BUILD { | ||||||||||||||||||||||||||||
| 33 | 14 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | 14 | ▶ | croak "Langertha::Content::Image requires url, base64, or data" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->has_url or $self->has_base64
MC/DC0%0/2
$self->has_url or $self->has_base64
$self->has_url
$self->has_base64
Truth table66%2/3
A or B
A= $self->has_urlB= $self->has_base64
| |||||||||||||||||||||||||||||
| 35 | unless $self->has_url || $self->has_base64; | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # --- Constructors --- | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub from_url { | ||||||||||||||||||||||||||||
| 41 | 7 | ▶ | my ( $class, $url, %extra ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_url: called CC=5 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 7 | ▶ | croak "from_url requires a URL" unless defined $url && length $url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $url and length $url
MC/DC0%0/2
defined $url and length $url
defined $url
length $url
Truth table33%1/3
A and B
A= defined $urlB= length $url
| |||||||||||||||||||||||||||||
| 43 | 7 | ▶ | my $media_type = $extra{media_type} // _sniff_media_type($url); | ||||||||||||||||||||||||||
|
Condition66%2/3
$extra{"media_type"} // _sniff_media_type($url)
MC/DC0%0/2
$extra{"media_type"} // _sniff_media_type($url)
$extra{"media_type"}
_sniff_media_type($url)
Truth table66%2/3
A // B
A= $extra{"media_type"}B= _sniff_media_type($url)
| |||||||||||||||||||||||||||||
| 44 | 7 | ▶ | return $class->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | url => $url, | ||||||||||||||||||||||||||||
| 46 | ( defined $media_type ? ( media_type => $media_type ) : () ), | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub from_file { | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | my ( $class, $path, %extra ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_file: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | croak "from_file requires a path" unless defined $path && length $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $path and length $path
MC/DC0%0/2
defined $path and length $path
defined $path
length $path
Truth table0%0/3
A and B
A= defined $pathB= length $path
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | croak "from_file: $path not found" unless -f $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | open my $fh, '<:raw', $path or croak "open $path: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | my $bytes = do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 57 | 0 | close $fh; | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $media_type = $extra{media_type} // _sniff_media_type($path); | ||||||||||||||||||||||||||
|
Condition0%0/3
$extra{"media_type"} // _sniff_media_type($path)
MC/DC0%0/2
$extra{"media_type"} // _sniff_media_type($path)
$extra{"media_type"}
_sniff_media_type($path)
Truth table0%0/3
A // B
A= $extra{"media_type"}B= _sniff_media_type($path)
| |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | croak "from_file: cannot determine media_type for $path" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | unless defined $media_type; | ||||||||||||||||||||||||||||
| 61 | 0 | return $class->new( | |||||||||||||||||||||||||||
| 62 | base64 => encode_base64($bytes, ''), | ||||||||||||||||||||||||||||
| 63 | media_type => $media_type, | ||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub from_data { | ||||||||||||||||||||||||||||
| 69 | 1 | ▶ | my ( $class, $bytes, %extra ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_data: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | croak "from_data requires bytes" unless defined $bytes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 1 | ▶ | croak "from_data requires media_type" unless defined $extra{media_type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | return $class->new( | ||||||||||||||||||||||||||||
| 73 | base64 => encode_base64($bytes, ''), | ||||||||||||||||||||||||||||
| 74 | media_type => $extra{media_type}, | ||||||||||||||||||||||||||||
| 75 | 1 | ); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | sub from_base64 { | ||||||||||||||||||||||||||||
| 80 | 5 | ▶ | my ( $class, $b64, %extra ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_base64: called CC=4 SCAR=15.6
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 5 | ▶ | croak "from_base64 requires a base64 string" unless defined $b64 && length $b64; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $b64 and length $b64
MC/DC0%0/2
defined $b64 and length $b64
defined $b64
length $b64
Truth table33%1/3
A and B
A= defined $b64B= length $b64
| |||||||||||||||||||||||||||||
| 82 | 5 | ▶ | croak "from_base64 requires media_type" unless defined $extra{media_type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | return $class->new( | ||||||||||||||||||||||||||||
| 84 | base64 => $b64, | ||||||||||||||||||||||||||||
| 85 | media_type => $extra{media_type}, | ||||||||||||||||||||||||||||
| 86 | 5 | ); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # --- Base64 materialization --- | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub ensure_base64 { | ||||||||||||||||||||||||||||
| 93 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
ensure_base64: called CC=7 SCAR=37.1
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 2 | ▶ | return $self->base64 if $self->has_base64; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | croak "ensure_base64: no url to fetch" unless $self->has_url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | require LWP::UserAgent; | |||||||||||||||||||||||||||
| 98 | 0 | my $ua = LWP::UserAgent->new( | |||||||||||||||||||||||||||
| 99 | agent => 'Langertha-Content-Image/'.$VERSION, | ||||||||||||||||||||||||||||
| 100 | timeout => 30, | ||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||
| 102 | 0 | my $response = $ua->get($self->url); | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | croak "ensure_base64: failed to fetch ".$self->url.": ".$response->status_line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | unless $response->is_success; | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | $self->base64(encode_base64($response->decoded_content(charset => 'none'), '')); | |||||||||||||||||||||||||||
| 107 | 0 | ▶ | unless ($self->has_media_type) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | my $ct = $response->header('Content-Type') // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$response->header("Content-Type") // ""
MC/DC0%0/1
$response->header("Content-Type") // ""
$response->header("Content-Type")
Truth table0%0/2
A
A= $response->header("Content-Type")
| |||||||||||||||||||||||||||||
| 109 | 0 | $ct =~ s/;.*$//; | |||||||||||||||||||||||||||
| 110 | 0 | $ct =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | $self->media_type($ct) if length $ct; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 0 | return $self->base64; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # --- Serializers --- | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub to_openai { | ||||||||||||||||||||||||||||
| 120 | 3 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_openai: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 3 | ▶ | my $url = $self->has_url | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->media_type // "application/octet-stream"
MC/DC0%0/1
$self->media_type // "application/octet-stream"
$self->media_type
Truth table50%1/2
A
A= $self->media_type
| |||||||||||||||||||||||||||||
| 122 | ? $self->url | ||||||||||||||||||||||||||||
| 123 | : sprintf('data:%s;base64,%s', | ||||||||||||||||||||||||||||
| 124 | ($self->media_type // 'application/octet-stream'), | ||||||||||||||||||||||||||||
| 125 | $self->base64, | ||||||||||||||||||||||||||||
| 126 | ); | ||||||||||||||||||||||||||||
| 127 | 3 | return { type => 'image_url', image_url => { url => $url } }; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | sub to_anthropic { | ||||||||||||||||||||||||||||
| 132 | 4 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_anthropic: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 4 | ▶ | if ($self->has_url) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | return { | ||||||||||||||||||||||||||||
| 135 | 2 | type => 'image', | |||||||||||||||||||||||||||
| 136 | source => { type => 'url', url => $self->url }, | ||||||||||||||||||||||||||||
| 137 | }; | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 2 | ▶ | croak "to_anthropic: base64 image requires media_type" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | unless $self->has_media_type; | ||||||||||||||||||||||||||||
| 141 | return { | ||||||||||||||||||||||||||||
| 142 | 1 | type => 'image', | |||||||||||||||||||||||||||
| 143 | source => { | ||||||||||||||||||||||||||||
| 144 | type => 'base64', | ||||||||||||||||||||||||||||
| 145 | media_type => $self->media_type, | ||||||||||||||||||||||||||||
| 146 | data => $self->base64, | ||||||||||||||||||||||||||||
| 147 | }, | ||||||||||||||||||||||||||||
| 148 | }; | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | sub to_gemini { | ||||||||||||||||||||||||||||
| 153 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_gemini: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | 2 | $self->ensure_base64; | |||||||||||||||||||||||||||
| 155 | 2 | ▶ | croak "to_gemini: image requires media_type" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | unless $self->has_media_type; | ||||||||||||||||||||||||||||
| 157 | return { | ||||||||||||||||||||||||||||
| 158 | 2 | inline_data => { | |||||||||||||||||||||||||||
| 159 | mime_type => $self->media_type, | ||||||||||||||||||||||||||||
| 160 | data => $self->base64, | ||||||||||||||||||||||||||||
| 161 | }, | ||||||||||||||||||||||||||||
| 162 | }; | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # --- Helpers --- | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 1 | my %EXT_MAP = ( | |||||||||||||||||||||||||||
| 169 | jpg => 'image/jpeg', | ||||||||||||||||||||||||||||
| 170 | jpeg => 'image/jpeg', | ||||||||||||||||||||||||||||
| 171 | png => 'image/png', | ||||||||||||||||||||||||||||
| 172 | gif => 'image/gif', | ||||||||||||||||||||||||||||
| 173 | webp => 'image/webp', | ||||||||||||||||||||||||||||
| 174 | bmp => 'image/bmp', | ||||||||||||||||||||||||||||
| 175 | svg => 'image/svg+xml', | ||||||||||||||||||||||||||||
| 176 | heic => 'image/heic', | ||||||||||||||||||||||||||||
| 177 | heif => 'image/heif', | ||||||||||||||||||||||||||||
| 178 | ); | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | sub _sniff_media_type { | ||||||||||||||||||||||||||||
| 181 | 6 | ▶ | my ($path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_sniff_media_type: called CC=3 SCAR=11.8
| |||||||||||||||||||||||||||||
| 182 | 6 | ▶ | return undef unless defined $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 6 | ( my $clean = $path ) =~ s/[?#].*$//; | |||||||||||||||||||||||||||
| 184 | 6 | ▶ | if ( $clean =~ /\.([a-zA-Z0-9]+)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 6 | return $EXT_MAP{ lc $1 }; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | 0 | return undef; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 1 | 1; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | __END__ | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =pod | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =head1 NAME | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | Langertha::Content::Image - Canonical image content block with cross-provider conversion (OpenAI / Anthropic / Gemini) | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head1 VERSION | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | version 0.502 | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | use Langertha::Content::Image; | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # From a remote URL | ||||||||||||||||||||||||||||
| 214 | my $img = Langertha::Content::Image->from_url('https://example.com/cat.jpg'); | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # From a local file (media_type sniffed from extension) | ||||||||||||||||||||||||||||
| 217 | my $img = Langertha::Content::Image->from_file('/tmp/cat.png'); | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | # From raw bytes | ||||||||||||||||||||||||||||
| 220 | my $img = Langertha::Content::Image->from_data($bytes, media_type => 'image/jpeg'); | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | # From an existing base64 string | ||||||||||||||||||||||||||||
| 223 | my $img = Langertha::Content::Image->from_base64($b64, media_type => 'image/png'); | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | # Embed in a chat message — Langertha::Role::Chat converts per engine | ||||||||||||||||||||||||||||
| 226 | my $response = $engine->simple_chat_f({ | ||||||||||||||||||||||||||||
| 227 | role => 'user', | ||||||||||||||||||||||||||||
| 228 | content => [ 'What is in this image?', $img ], | ||||||||||||||||||||||||||||
| 229 | }); | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | Provider-neutral image block. Carries either a remote URL, a base64 payload, | ||||||||||||||||||||||||||||
| 234 | or both, plus an IANA C<media_type>. Serializes to the three dominant | ||||||||||||||||||||||||||||
| 235 | vision-chat wire formats: | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =over | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =item * OpenAI chat completions — C<{ type => 'image_url', image_url => { url => ... } }> | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =item * Anthropic messages — C<{ type => 'image', source => { type => 'url' | 'base64', ... } }> | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =item * Google Gemini — C<{ inline_data => { mime_type => ..., data => <base64> } }> | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =back | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | Gemini requires base64, so C<to_gemini> will transparently download a | ||||||||||||||||||||||||||||
| 248 | remote URL on first call (cached on the object). | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | =head2 url | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | Remote HTTP(S) URL of the image. May be passed through directly (OpenAI, | ||||||||||||||||||||||||||||
| 253 | Anthropic) or auto-downloaded and base64-encoded (Gemini). | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =head2 base64 | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | The base64-encoded image payload (no C<data:> URL prefix). Can be supplied | ||||||||||||||||||||||||||||
| 258 | at construction, or populated lazily when a provider that requires inline | ||||||||||||||||||||||||||||
| 259 | data (Gemini) is targeted. | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =head2 media_type | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | IANA media type (C<image/jpeg>, C<image/png>, C<image/gif>, C<image/webp>). | ||||||||||||||||||||||||||||
| 264 | Required for base64 payloads on Anthropic and Gemini. Sniffed from the file | ||||||||||||||||||||||||||||
| 265 | extension by C<from_file> and from the URL path by C<from_url>. | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =head2 from_url | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | my $img = Langertha::Content::Image->from_url($url); | ||||||||||||||||||||||||||||
| 270 | my $img = Langertha::Content::Image->from_url($url, media_type => 'image/jpeg'); | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | Builds an image block referencing a remote URL. Media type is sniffed from | ||||||||||||||||||||||||||||
| 273 | the URL extension when not provided. | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =head2 from_file | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | my $img = Langertha::Content::Image->from_file('/tmp/cat.png'); | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | Reads a local file, base64-encodes it, and sniffs the media type from the | ||||||||||||||||||||||||||||
| 280 | extension (unless C<media_type> is passed). | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | =head2 from_data | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | my $img = Langertha::Content::Image->from_data($bytes, media_type => 'image/jpeg'); | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | Builds an image block from raw bytes. C<media_type> is required. | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =head2 from_base64 | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | my $img = Langertha::Content::Image->from_base64($b64, media_type => 'image/png'); | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | Builds an image block from an existing base64 string. | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | =head2 ensure_base64 | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | my $b64 = $img->ensure_base64; | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | Returns the base64 payload, fetching the URL over HTTP if necessary. | ||||||||||||||||||||||||||||
| 299 | Populates C<media_type> from the response C<Content-Type> header when the | ||||||||||||||||||||||||||||
| 300 | image was URL-only. Caches the result on the object. | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | =head2 to_openai | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | my $block = $img->to_openai; | ||||||||||||||||||||||||||||
| 305 | # { type => 'image_url', image_url => { url => ... } } | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | Serializes to the OpenAI chat-completions image block. Uses the URL when | ||||||||||||||||||||||||||||
| 308 | available, otherwise emits a C<data:> URL from the base64 payload. | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | =head2 to_anthropic | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | my $block = $img->to_anthropic; | ||||||||||||||||||||||||||||
| 313 | # { type => 'image', source => { type => 'url', url => ... } } | ||||||||||||||||||||||||||||
| 314 | # or | ||||||||||||||||||||||||||||
| 315 | # { type => 'image', source => { type => 'base64', media_type => ..., data => ... } } | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | Serializes to the Anthropic messages image block. Prefers a URL source | ||||||||||||||||||||||||||||
| 318 | when available; otherwise emits an inline base64 source (C<media_type> | ||||||||||||||||||||||||||||
| 319 | required). | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | =head2 to_gemini | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | my $block = $img->to_gemini; | ||||||||||||||||||||||||||||
| 324 | # { inline_data => { mime_type => ..., data => <base64> } } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | Serializes to the Gemini C<inlineData> part. Auto-downloads URL-only | ||||||||||||||||||||||||||||
| 327 | images because Gemini has no URL-fetching equivalent. | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | =over | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | =item * L<Langertha::Content> - Base role this class implements | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | =item * L<Langertha::Role::Chat> - Normalizes content blocks per engine during C<chat_messages> | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | =item * L<Langertha::ToolChoice> - Sibling value object for tool_choice normalization | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | =back | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | =head2 Issues | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 346 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | =head2 IRC | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 365 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | =cut | ||||||||||||||||||||||||||||