blib/lib/Langertha/Knarr/Image.pm

filter: Statementstmt 98.2% 109 / 111 Branchbran 75.7% 50 / 66 Conditioncond 55.2% 53 / 96 MC/DCmcdc 16.3% 8 / 49 Subroutinesub 100% 20 / 20 Podpod 100% 6 / 6 totaltotal 70.6% 246 / 348 CC 70 SCAR 23.3
CC
70
Cov
78%
_from_anthropic_block
20.2
_ollama_message
18.3
_from_openai_part
13.0
CRAP 10.3
« blib/lib/Langertha/Knarr.pm ↑ Summary blib/lib/Langertha/Knarr/PassthroughUsage.pm »
1 package Langertha::Knarr::Image;
2 # ABSTRACT: Translate a client's image parts into Langertha::Content::Image objects
3 61 our $VERSION = '1.102';
4 61 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 61 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 61 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 61 ▶use MIME::Base64 qw( decode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 61 my @ALL_FORMATS = qw( openai anthropic gemini ollama responses lmstudio );
11  
12 61 my $translates;
13  
14 798 ▶sub translates {
Subroutine translates: called CC=6 SCAR=19.9
Pod documented
15 798 ▶ return $translates if defined $translates;
Branch: 1 branch
BranchTrueFalseTotal
if defined $translates 755 43 798
16 43 ▶ $translates = eval {
Condition50%1/2
eval { || 0
l!l
430
MC/DC0%0/1
eval { do { require Langertha::Content::Image; my $class = 'Langertha::Content::Image'; $class->can('to_ollama') && $class->can('to_responses') && $class->can('to_lmstudio') ? 1 : 0 } } || 0
eval { do { require Langertha::Content::Image; my $class = 'Langertha::Content::Image'; $class->can('to_ollama') && $class->can('to_responses') && $class->can('to_lmstudio') ? 1 : 0 } }
Truth table50%1/2
A
A= eval { do { require Langertha::Content::Image; my $class = 'Langertha::Content::Image'; $class->can('to_ollama') && $class->can('to_responses') && $class->can('to_lmstudio') ? 1 : 0 } }
Aresult
11
00
17 43 require Langertha::Content::Image;
18 43 my $class = 'Langertha::Content::Image';
19 43 ▶ ( $class->can('to_ollama') && $class->can('to_responses') && $class->can('to_lmstudio') ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$class->can('to_ollama') && $class->can('to_responses') && $class->can('to_lmstudio') ? : 43 0 43
Condition33%2/6
$class->can("to_ollama") and $class->can("to_responses") and $class->can("to_lmstudio")
!ll&&!rl&&r
0043
$class->can("to_ollama") and $class->can("to_responses")
!ll&&!rl&&r
0043
MC/DC0%0/3
$class->can("to_ollama") and $class->can("to_responses") and $class->can("to_lmstudio")
$class->can("to_ollama") $class->can("to_responses") $class->can("to_lmstudio")
Truth table25%1/4
A and B and C
A= $class->can("to_ollama")B= $class->can("to_responses")C= $class->can("to_lmstudio")
ABCresult
0XX0
10X0
1100
1111
20 } || 0;
21 43 return $translates;
22 }
23  
24  
25 111 ▶sub content_formats {
Subroutine content_formats: called CC=2 SCAR=7.2
Pod documented
26 111 my (@face_formats) = @_;
27 111 ▶ return translates() ? [@ALL_FORMATS] : [@face_formats];
Branch: 1 branch
BranchTrueFalseTotal
translates() ? : 111 0 111
28 }
29  
30  
31 264 ▶sub openai_messages { _map_messages( $_[0], \&_openai_message ) }
Subroutine openai_messages: called CC=1 SCAR=0.0
Pod documented
32 214 ▶sub anthropic_messages { _map_messages( $_[0], \&_anthropic_message ) }
Subroutine anthropic_messages: called CC=1 SCAR=0.0
Pod documented
33 207 ▶sub ollama_messages { _map_messages( $_[0], \&_ollama_message ) }
Subroutine ollama_messages: called CC=1 SCAR=0.0
Pod documented
34  
35  
36 685 ▶sub _map_messages {
Subroutine _map_messages: called CC=4 SCAR=15.6
37 685 my ( $messages, $code ) = @_;
38 685 ▶ return $messages unless translates() && ref $messages eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless translates() and ref $messages eq "ARRAY" 0 685 685
Condition33%1/3
translates() and ref $messages eq "ARRAY"
!ll&&!rl&&r
00685
MC/DC0%0/2
translates() and ref $messages eq "ARRAY"
translates() ref $messages eq "ARRAY"
Truth table33%1/3
A and B
A= translates()B= ref $messages eq "ARRAY"
ABresult
0X0
100
111
39 685 ▶ return [ map { ref $_ eq 'HASH' ? $code->($_) : $_ } @$messages ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 686 0 686
40 }
41  
42 264 ▶sub _openai_message {
Subroutine _openai_message: called CC=4 SCAR=13.9
43 264 my ($msg) = @_;
44 264 ▶ return $msg unless ref $msg->{content} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $msg->{"content"} eq "ARRAY" 255 9 264
45 9 my $found = 0;
46 my @parts = map {
47 18 my $img = _from_openai_part($_);
48 18 ▶ $img ? do { $found = 1; $img } : $_;
Branch: 1 branch
BranchTrueFalseTotal
$img ? : 9 9 18
49 9 } @{ $msg->{content} };
50 9 ▶ return $found ? { %$msg, content => _plain_text(\@parts) } : $msg;
Branch: 1 branch
BranchTrueFalseTotal
$found ? : 9 0 9
51 }
52  
53 18 ▶sub _from_openai_part {
Subroutine _from_openai_part: called CC=10 SCAR=25.6
54 18 my ($part) = @_;
55 18 ▶ return undef unless ref $part eq 'HASH' && ( $part->{type} // '' ) eq 'image_url';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $part eq "HASH" and ($part->{"type"} // "") eq "image_url" 9 9 18
Condition60%3/5
ref $part eq "HASH" and ($part->{"type"} // "") eq "image_url"
!ll&&!rl&&r
099
$part->{"type"} // ""
l!l
180
MC/DC0%0/2
ref $part eq "HASH" and ($part->{"type"} // "") eq "image_url"
ref $part eq "HASH" $part->{"type"}
Truth table33%1/3
A and B
A= ref $part eq "HASH"B= $part->{"type"}
ABresult
0X0
100
111
56 9 ▶ my $url = ref $part->{image_url} eq 'HASH' ? $part->{image_url}{url} : $part->{image_url};
Branch: 1 branch
BranchTrueFalseTotal
ref $part->{'image_url'} eq 'HASH' ? : 9 0 9
57 9 ▶ return undef unless defined $url && !ref $url && length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and not ref $url and length $url 0 9 9
Condition33%2/6
defined $url and not ref $url and length $url
!ll&&!rl&&r
009
defined $url and not ref $url
!ll&&!rl&&r
009
MC/DC0%0/3
defined $url and not ref $url and length $url
defined $url not ref $url length $url
Truth table25%1/4
A and not(B) and C
A= defined $urlB= not ref $urlC= length $url
ABCresult
0XX0
10X0
1100
1111
58 9 ▶ if ( $url =~ /\Adata:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /\Adata:/) 5 4 9
59 5 ▶ my ( $media_type, $b64 ) = _parse_data_url($url) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = _parse_data_url($url) or return undef 5 0 5
60 5 return Langertha::Content::Image->from_base64( $b64, media_type => $media_type );
61 }
62 # Not fetched here: core fetches only for engines that need inline data.
63 4 return Langertha::Content::Image->from_url($url);
64 }
65  
66 215 ▶sub _anthropic_message {
Subroutine _anthropic_message: called CC=4 SCAR=13.9
67 215 my ($msg) = @_;
68 215 ▶ return $msg unless ref $msg->{content} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $msg->{"content"} eq "ARRAY" 206 9 215
69 9 my $found = 0;
70 my @parts = map {
71 18 my $img = _from_anthropic_block($_);
72 18 ▶ $img ? do { $found = 1; $img } : $_;
Branch: 1 branch
BranchTrueFalseTotal
$img ? : 9 9 18
73 9 } @{ $msg->{content} };
74 9 ▶ return $found ? { %$msg, content => _plain_text(\@parts) } : $msg;
Branch: 1 branch
BranchTrueFalseTotal
$found ? : 9 0 9
75 }
76  
77 18 ▶sub _from_anthropic_block {
Subroutine _from_anthropic_block: called CC=13 SCAR=30.1
78 18 my ($block) = @_;
79 return undef unless ref $block eq 'HASH' && ( $block->{type} // '' ) eq 'image'
80 18 ▶ && ref $block->{source} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $block eq "HASH" and ($block->{"type"} // "") eq "image" and ref $block->{"source"} eq "HASH" 9 9 18
Condition62%5/8
ref $block eq "HASH" and ($block->{"type"} // "") eq "image" and ref $block->{"source"} eq "HASH"
!ll&&!rl&&r
909
ref $block eq "HASH" and ($block->{"type"} // "") eq "image"
!ll&&!rl&&r
099
$block->{"type"} // ""
l!l
180
MC/DC0%0/3
ref $block eq "HASH" and ($block->{"type"} // "") eq "image" and ref $block->{"source"} eq "HASH"
ref $block eq "HASH" $block->{"type"} ref $block->{"source"} eq "HASH"
Truth table25%1/4
A and B and C
A= ref $block eq "HASH"B= $block->{"type"}C= ref $block->{"source"} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
81 9 my $src = $block->{source};
82 9 ▶ my $type = $src->{type} // '';
Condition50%1/2
$src->{"type"} // ""
l!l
90
MC/DC0%0/1
$src->{"type"} // ""
$src->{"type"}
Truth table50%1/2
A
A= $src->{"type"}
Aresult
11
00
83 9 ▶ if ( $type eq 'base64' && defined $src->{data} && length $src->{data} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "base64" and defined $src->{"data"} and length $src->{"data"}) 6 3 9
Condition66%4/6
$type eq "base64" and defined $src->{"data"} and length $src->{"data"}
!ll&&!rl&&r
306
$type eq "base64" and defined $src->{"data"}
!ll&&!rl&&r
306
MC/DC33%1/3
$type eq "base64" and defined $src->{"data"} and length $src->{"data"}
$type eq "base64" defined $src->{"data"} length $src->{"data"}
Truth table50%2/4
A and B and C
A= $type eq "base64"B= defined $src->{"data"}C= length $src->{"data"}
ABCresult
0XX0
10X0
1100
1111
84 return Langertha::Content::Image->from_base64( $src->{data},
85 6 ▶ media_type => $src->{media_type} // _sniff_base64( $src->{data} ) );
Condition33%1/3
$src->{"media_type"} // _sniff_base64($src->{"data"})
l!l&&r!l&&!r
600
MC/DC0%0/2
$src->{"media_type"} // _sniff_base64($src->{"data"})
$src->{"media_type"} _sniff_base64($src->{"data"})
Truth table33%1/3
A // B
A= $src->{"media_type"}B= _sniff_base64($src->{"data"})
ABresult
1X1
011
000
86 }
87 3 ▶ if ( $type eq 'url' && defined $src->{url} && length $src->{url} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "url" and defined $src->{"url"} and length $src->{"url"}) 3 0 3
Condition33%2/6
$type eq "url" and defined $src->{"url"} and length $src->{"url"}
!ll&&!rl&&r
003
$type eq "url" and defined $src->{"url"}
!ll&&!rl&&r
003
MC/DC0%0/3
$type eq "url" and defined $src->{"url"} and length $src->{"url"}
$type eq "url" defined $src->{"url"} length $src->{"url"}
Truth table25%1/4
A and B and C
A= $type eq "url"B= defined $src->{"url"}C= length $src->{"url"}
ABCresult
0XX0
10X0
1100
1111
88 3 return Langertha::Content::Image->from_url( $src->{url} );
89 }
90 0 return undef;
91 }
92  
93 207 ▶sub _ollama_message {
Subroutine _ollama_message: called CC=15 SCAR=29.1
94 207 my ($msg) = @_;
95 207 ▶ return $msg unless ref $msg->{images} eq 'ARRAY' && @{ $msg->{images} };
Branch: 1 branch
BranchTrueFalseTotal
unless ref $msg->{"images"} eq "ARRAY" and @{$msg->{"images"};} 193 14 207
Condition66%2/3
ref $msg->{"images"} eq "ARRAY" and @{$msg->{"images"};}
!ll&&!rl&&r
193014
MC/DC50%1/2
ref $msg->{"images"} eq "ARRAY" and @{$msg->{"images"};}
ref $msg->{"images"} eq "ARRAY" @{$msg->{"images"};}
Truth table66%2/3
A and B
A= ref $msg->{"images"} eq "ARRAY"B= @{$msg->{"images"};}
ABresult
0X0
100
111
96 14 my @images;
97 14 for my $raw ( @{ $msg->{images} } ) {
98 14 ▶ return $msg unless defined $raw && !ref $raw && length $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw and not ref $raw and length $raw 0 14 14
Condition33%2/6
defined $raw and not ref $raw and length $raw
!ll&&!rl&&r
0014
defined $raw and not ref $raw
!ll&&!rl&&r
0014
MC/DC0%0/3
defined $raw and not ref $raw and length $raw
defined $raw not ref $raw length $raw
Truth table25%1/4
A and not(B) and C
A= defined $rawB= not ref $rawC= length $raw
ABCresult
0XX0
10X0
1100
1111
99 14 ▶ my ( $media_type, $b64 ) = $raw =~ /\Adata:/ ? _parse_data_url($raw) : ();
Branch: 1 branch
BranchTrueFalseTotal
$raw =~ /\Adata:/ ? : 1 13 14
100 14 ▶ return $msg if $raw =~ /\Adata:/ && !defined $b64;
Branch: 1 branch
BranchTrueFalseTotal
if $raw =~ /\Adata:/ and not defined $b64 0 14 14
Condition66%2/3
$raw =~ /\Adata:/ and not defined $b64
!ll&&!rl&&r
1310
MC/DC0%0/2
$raw =~ /\Adata:/ and not defined $b64
$raw =~ /\Adata:/ not defined $b64
Truth table66%2/3
A and not(B)
A= $raw =~ /\Adata:/B= not defined $b64
ABresult
0X0
100
111
101 14 ▶ $b64 //= $raw;
Condition66%2/3
$b64 //= $raw
l!l&&r!l&&!r
1130
MC/DC0%0/2
$b64 //= $raw
$b64 $raw
Truth table66%2/3
A //= B
A= $b64B= $raw
ABresult
1X1
011
000
102 14 ▶ push @images, Langertha::Content::Image->from_base64( $b64,
Condition66%2/3
$media_type // _sniff_base64($b64)
l!l&&r!l&&!r
1130
MC/DC0%0/2
$media_type // _sniff_base64($b64)
$media_type _sniff_base64($b64)
Truth table66%2/3
A // B
A= $media_typeB= _sniff_base64($b64)
ABresult
1X1
011
000
103 media_type => $media_type // _sniff_base64($b64) );
104 }
105 14 my $content = $msg->{content};
106 14 ▶ my @parts = ref $content eq 'ARRAY' ? @$content
Branch: 2 branches
BranchTrueFalseTotal
ref $content eq 'ARRAY' ? : 0 14 14
defined $content && length $content ? : 10 4 14
Condition66%2/3
defined $content and length $content
!ll&&!rl&&r
0410
MC/DC50%1/2
defined $content and length $content
defined $content length $content
Truth table66%2/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
107 : defined $content && length $content ? ($content)
108 : ();
109 14 my %out = %$msg;
110 14 delete $out{images};
111 14 return { %out, content => _plain_text( [ @parts, @images ] ) };
112 }
113  
114 # A bare { type => 'text', text => ... } part as a plain string: the one
115 # text form core renders in every content format.
116 32 ▶sub _plain_text {
Subroutine _plain_text: called CC=7 SCAR=20.1
117 32 my ($parts) = @_;
118 return [ map {
119 32 ( ref $_ eq 'HASH' && keys %$_ == 2 && ( $_->{type} // '' ) eq 'text'
120 60 ▶ && defined $_->{text} && !ref $_->{text} ) ? $_->{text} : $_
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' && keys %$_ == 2 && ($_->{'type'} // '') eq 'text' && defined $_->{'text'} && !ref($_->{'text'}) ? : 17 43 60
Condition71%10/14
ref $_ eq "HASH" and keys %$_ == 2 and ($_->{"type"} // "") eq "text" and defined $_->{"text"} and not ref $_->{"text"}
!ll&&!rl&&r
43017
ref $_ eq "HASH" and keys %$_ == 2 and ($_->{"type"} // "") eq "text" and defined $_->{"text"}
!ll&&!rl&&r
43017
ref $_ eq "HASH" and keys %$_ == 2 and ($_->{"type"} // "") eq "text"
!ll&&!rl&&r
43017
ref $_ eq "HASH" and keys %$_ == 2
!ll&&!rl&&r
42117
$_->{"type"} // ""
l!l
170
MC/DC40%2/5
ref $_ eq "HASH" and keys %$_ == 2 and ($_->{"type"} // "") eq "text" and defined $_->{"text"} and not ref $_->{"text"}
ref $_ eq "HASH" keys %$_ == 2 $_->{"type"} defined $_->{"text"} not ref $_->{"text"}
Truth table50%3/6
A and B and C and D and not(E)
A= ref $_ eq "HASH"B= keys %$_ == 2C= $_->{"type"}D= defined $_->{"text"}E= not ref $_->{"text"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
121 } @$parts ];
122 }
123  
124 # data:<media type>[;param...];base64,<payload>
125 6 ▶sub _parse_data_url {
Subroutine _parse_data_url: called CC=2 SCAR=7.1
126 6 my ($url) = @_;
127 6 ▶ return unless $url =~ m{\Adata:([^;,]+)((?:;[^;,]*)*);base64,(.+)\z}s;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ /\Adata:([^;,]+)((?:;[^;,]*)*);base64,(.+)\z/s 0 6 6
128 6 return ( $1, $3 );
129 }
130  
131 61 my @MAGIC = (
132 [ qr/\A\x89PNG/, 'image/png' ],
133 [ qr/\A\xFF\xD8\xFF/, 'image/jpeg' ],
134 [ qr/\AGIF8/, 'image/gif' ],
135 [ qr/\ARIFF.{4}WEBP/s, 'image/webp' ],
136 [ qr/\ABM/, 'image/bmp' ],
137 );
138  
139 # Media type from the payload's magic bytes; Ollama sends none. PNG when
140 # nothing matches.
141 13 ▶sub _sniff_base64 {
Subroutine _sniff_base64: called CC=3 SCAR=11.0
142 13 my ($b64) = @_;
143 13 my $head = decode_base64( substr( $b64, 0, 16 ) );
144 13 for my $m (@MAGIC) {
145 31 ▶ return $m->[1] if $head =~ $m->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $head =~ /$m->[0]/ 12 19 31
146 }
147 1 return 'image/png';
148 }
149  
150 84 ▶sub plain_messages {
Subroutine plain_messages: called CC=7 SCAR=20.0
Pod documented
151 84 my ($messages) = @_;
152 return $messages unless ref $messages eq 'ARRAY'
153 84 ▶ && grep { ref $_ eq 'HASH' && ref $_->{content} eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $messages eq "ARRAY" and grep {ref $_ eq "HASH" and ref $_->{"content"} eq "ARRAY" and grep {builtin::blessed($_);} @{$$_{"content"};};} @$messages 82 2 84
Condition100%3/3
ref $messages eq "ARRAY" and grep {ref $_ eq "HASH" and ref $_->{"content"} eq "ARRAY" and grep {builtin::blessed($_);} @{$$_{"content"};};} @$messages
!ll&&!rl&&r
20622
MC/DC100%2/2
ref $messages eq "ARRAY" and grep {ref $_ eq "HASH" and ref $_->{"content"} eq "ARRAY" and grep {builtin::blessed($_);} @{$$_{"content"};};} @$messages
ref $messages eq "ARRAY" grep {ref $_ eq "HASH" and ref $_->{"content"} eq "ARRAY" and grep {builtin::blessed($_);} @{$$_{"content"};};} @$messages
Truth table100%3/3
A and B
A= ref $messages eq "ARRAY"B= grep {ref $_ eq "HASH" and ref $_->{"content"} eq "ARRAY" and grep {builtin::blessed($_);} @{$$_{"content"};};} @$messages
ABresult
0X0
100
111
154 62 ▶ && grep { blessed $_ } @{ $_->{content} } } @$messages;
Condition66%4/6
ref $_ eq 'HASH' && ref $_->{'content'} eq 'ARRAY' && grep({builtin::blessed($_);} @{$$_{'content'};})
!ll&&!rl&&r
6002
ref $_ eq "HASH" and ref $_->{"content"} eq "ARRAY"
!ll&&!rl&&r
0602
MC/DC33%1/3
ref $_ eq 'HASH' && ref $_->{'content'} eq 'ARRAY' && grep({builtin::blessed($_);} @{$$_{'content'};})
ref $_ eq "HASH" ref $_->{"content"} eq "ARRAY" grep({builtin::blessed($_);} @{$$_{'content'};})
Truth table50%2/4
A and B && C
A= ref $_ eq "HASH"B= ref $_->{"content"} eq "ARRAY"C= grep({builtin::blessed($_);} @{$$_{'content'};})
ABCresult
0XX0
10X0
1100
1111
155 return [ map {
156 2 ( ref $_ eq 'HASH' && ref $_->{content} eq 'ARRAY' )
157 2 ▶ ? { %$_, content => [ map { _plain_part($_) } @{ $_->{content} } ] }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' && ref $_->{'content'} eq 'ARRAY' ? : 2 0 2
Condition33%1/3
ref $_ eq "HASH" and ref $_->{"content"} eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
ref $_ eq "HASH" and ref $_->{"content"} eq "ARRAY"
ref $_ eq "HASH" ref $_->{"content"} eq "ARRAY"
Truth table33%1/3
A and B
A= ref $_ eq "HASH"B= ref $_->{"content"} eq "ARRAY"
ABresult
0X0
100
111
158 : $_
159 } @$messages ];
160 }
161  
162  
163 6 ▶sub _plain_part {
Subroutine _plain_part: called CC=5 SCAR=16.6
164 6 my ($part) = @_;
165 6 ▶ return $part unless blessed $part;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($part) 2 4 6
166 4 ▶ if ( $part->isa('Langertha::Content::Image') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part->isa("Langertha::Content::Image")) 4 0 4
167 4 ▶ my $url = $part->has_base64
Branch: 1 branch
BranchTrueFalseTotal
$part->has_base64 ? : 2 2 4
Condition50%1/2
$part->media_type // "application/octet-stream"
l!l
20
MC/DC0%0/1
$part->media_type // "application/octet-stream"
$part->media_type
Truth table50%1/2
A
A= $part->media_type
Aresult
11
00
168 ? sprintf( 'data:%s;base64,%s', $part->media_type // 'application/octet-stream', $part->base64 )
169 : $part->url;
170 4 return { type => 'image_url', image_url => { url => $url } };
171 }
172 0 return "$part";
173 }
174  
175  
176 61 1;
177  
178 __END__
179  
180 =pod
181  
182 =encoding UTF-8
183  
184 =head1 NAME
185  
186 Langertha::Knarr::Image - Translate a client's image parts into Langertha::Content::Image objects
187  
188 =head1 VERSION
189  
190 version 1.102
191  
192 =head1 SYNOPSIS
193  
194 use Langertha::Knarr::Image;
195  
196 # In a protocol's parse_chat_request:
197 my $messages = Langertha::Knarr::Image::openai_messages( $data->{messages} );
198  
199 # Before a messages array is JSON-encoded for a trace or a log:
200 my $plain = Langertha::Knarr::Image::plain_messages($messages);
201  
202 =head1 DESCRIPTION
203  
204 Each client-facing protocol receives images in its own shape: OpenAI
205 C<image_url> content parts, Anthropic C<image> blocks, Ollama's per-message
206 C<images> array of raw base64. The engine behind the route may speak another
207 shape entirely. This module turns every face's image representation into
208 L<Langertha::Content::Image> objects inside the message C<content> array;
209 Langertha core then serializes them in the engine's own C<content_format>
210 (C<openai>, C<anthropic>, C<gemini>, C<ollama>, C<responses>, C<lmstudio>).
211  
212 Translation needs a core whose L<Langertha::Content::Image> serializes all of
213 those formats (C<to_ollama>, C<to_responses>, C<to_lmstudio>). On an older
214 core (Langertha 0.503) the translators return the messages untouched and
215 images keep passing through in the client's shape, as before.
216  
217 Only messages that carry an image change. In such a message a bare
218 C<< { type => 'text', text => ... } >> part becomes a plain string, which core
219 turns into the text part of whatever format the engine speaks; a text part
220 with any other key (C<cache_control>, ...) is kept as sent. The input arrays
221 are never modified; a changed message is a copy.
222  
223 An OpenAI C<image_url> part's C<detail> hint has no field on
224 L<Langertha::Content::Image> and is not carried.
225  
226 =head2 translates
227  
228 True when the installed core can serialize an image object into every
229 engine content format, i.e. when this module translates at all.
230  
231 =head2 content_formats
232  
233 image_content_formats => Langertha::Knarr::Image::content_formats('openai', 'gemini'),
234  
235 The engine content formats a protocol's images reach: every format when
236 L</translates>, else the given formats that read the protocol's own shape
237 untranslated.
238  
239 =head2 openai_messages
240  
241 =head2 anthropic_messages
242  
243 =head2 ollama_messages
244  
245 my $messages = Langertha::Knarr::Image::openai_messages( \@messages );
246  
247 Returns a new ArrayRef of messages with the face's image representation
248 replaced by L<Langertha::Content::Image> objects, or the given ArrayRef
249 itself when L</translates> is false.
250  
251 =head2 plain_messages
252  
253 my $plain = Langertha::Knarr::Image::plain_messages($messages);
254  
255 The messages with every image object replaced by its OpenAI
256 C<image_url> part (the URL as sent, or a C<data:> URL; nothing is fetched),
257 so a trace or request log can JSON-encode them. Returns the given ArrayRef
258 when it holds no object.
259  
260 =head1 SEE ALSO
261  
262 =over
263  
264 =item * L<Langertha::Content::Image> (Langertha core)
265  
266 =item * L<Langertha::Knarr::Protocol/manifest_endpoint> - C<image_content_formats>
267  
268 =back
269  
270 =head1 SUPPORT
271  
272 =head2 Issues
273  
274 Please report bugs and feature requests on GitHub at
275 L<https://github.com/Getty/langertha-knarr/issues>.
276  
277 =head2 IRC
278  
279 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
280  
281 =head1 CONTRIBUTING
282  
283 Contributions are welcome! Please fork the repository and submit a pull request.
284  
285 =head1 AUTHOR
286  
287 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
288  
289 =head1 COPYRIGHT AND LICENSE
290  
291 This software is copyright (c) 2026 by Torsten Raudssus.
292  
293 This is free software; you can redistribute it and/or modify it under
294 the same terms as the Perl 5 programming language system itself.
295  
296 =cut
« blib/lib/Langertha/Knarr.pm ↑ Summary blib/lib/Langertha/Knarr/PassthroughUsage.pm »