| 1 | package Langertha::Knarr::Image; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Translate a client's image parts into Langertha::Content::Image objects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 15 | our $VERSION = '1.103'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use MIME::Base64 qw( decode_base64 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 15 | my @ALL_FORMATS = qw( openai anthropic gemini ollama responses lmstudio ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 15 | my $translates; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 79 | ▶ | sub translates { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
translates: called CC=6 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 79 | ▶ | return $translates if defined $translates; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | ▶ | $translates = eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
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
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
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 4 | require Langertha::Content::Image; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 4 | my $class = 'Langertha::Content::Image'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | ▶ | ( $class->can('to_ollama') && $class->can('to_responses') && $class->can('to_lmstudio') ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$class->can("to_ollama") and $class->can("to_responses") and $class->can("to_lmstudio")
$class->can("to_ollama") and $class->can("to_responses")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | } || 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 4 | return $translates; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | ▶ | sub content_formats { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
content_formats: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | my (@face_formats) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 3 | ▶ | return translates() ? [@ALL_FORMATS] : [@face_formats]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 13 | ▶ | sub openai_messages { _map_messages( $_[0], \&_openai_message ) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
openai_messages: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 34 | ▶ | sub anthropic_messages { _map_messages( $_[0], \&_anthropic_message ) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
anthropic_messages: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 28 | ▶ | sub ollama_messages { _map_messages( $_[0], \&_ollama_message ) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ollama_messages: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 75 | ▶ | sub _map_messages { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_map_messages: called CC=4 SCAR=15.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 75 | my ( $messages, $code ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 75 | ▶ | return $messages unless translates() && ref $messages eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
translates() and ref $messages eq "ARRAY"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 75 | ▶ | return [ map { ref $_ eq 'HASH' ? $code->($_) : $_ } @$messages ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 13 | ▶ | sub _openai_message { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_openai_message: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 13 | my ($msg) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 13 | ▶ | return $msg unless ref $msg->{content} eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 9 | } @{ $msg->{content} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 9 | ▶ | return $found ? { %$msg, content => _plain_text(\@parts) } : $msg; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition60%3/5
ref $part eq "HASH" and ($part->{"type"} // "") eq "image_url"
$part->{"type"} // ""
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 9 | ▶ | my $url = ref $part->{image_url} eq 'HASH' ? $part->{image_url}{url} : $part->{image_url}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 9 | ▶ | return undef unless defined $url && !ref $url && length $url; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $url and not ref $url and length $url
defined $url and not ref $url
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 9 | ▶ | if ( $url =~ /\Adata:/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 5 | ▶ | my ( $media_type, $b64 ) = _parse_data_url($url) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 35 | ▶ | sub _anthropic_message { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_anthropic_message: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 35 | my ($msg) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 35 | ▶ | return $msg unless ref $msg->{content} eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 9 | } @{ $msg->{content} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 9 | ▶ | return $found ? { %$msg, content => _plain_text(\@parts) } : $msg; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition62%5/8
ref $block eq "HASH" and ($block->{"type"} // "") eq "image" and ref $block->{"source"} eq "HASH"
ref $block eq "HASH" and ($block->{"type"} // "") eq "image"
$block->{"type"} // ""
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 9 | my $src = $block->{source}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 9 | ▶ | my $type = $src->{type} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$src->{"type"} // ""
MC/DC0%0/1
$src->{"type"} // ""
$src->{"type"}
Truth table50%1/2
A
A= $src->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 9 | ▶ | if ( $type eq 'base64' && defined $src->{data} && length $src->{data} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$type eq "base64" and defined $src->{"data"} and length $src->{"data"}
$type eq "base64" and defined $src->{"data"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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"})
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"})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | ▶ | if ( $type eq 'url' && defined $src->{url} && length $src->{url} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$type eq "url" and defined $src->{"url"} and length $src->{"url"}
$type eq "url" and defined $src->{"url"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | return Langertha::Content::Image->from_url( $src->{url} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 28 | ▶ | sub _ollama_message { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ollama_message: called CC=15 SCAR=29.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 28 | my ($msg) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 28 | ▶ | return $msg unless ref $msg->{images} eq 'ARRAY' && @{ $msg->{images} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $msg->{"images"} eq "ARRAY" and @{$msg->{"images"};}
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"};}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
defined $raw and not ref $raw and length $raw
defined $raw and not ref $raw
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 14 | ▶ | my ( $media_type, $b64 ) = $raw =~ /\Adata:/ ? _parse_data_url($raw) : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 14 | ▶ | return $msg if $raw =~ /\Adata:/ && !defined $b64; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$raw =~ /\Adata:/ and not defined $b64
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 14 | ▶ | $b64 //= $raw; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$b64 //= $raw
MC/DC0%0/2
$b64 //= $raw
$b64
$raw
Truth table66%2/3
A //= B
A= $b64B= $raw
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 14 | ▶ | push @images, Langertha::Content::Image->from_base64( $b64, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$media_type // _sniff_base64($b64)
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $content and length $content
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition71%10/14
ref $_ eq "HASH" and keys %$_ == 2 and ($_->{"type"} // "") eq "text" and defined $_->{"text"} and not ref $_->{"text"}
ref $_ eq "HASH" and keys %$_ == 2 and ($_->{"type"} // "") eq "text" and defined $_->{"text"}
ref $_ eq "HASH" and keys %$_ == 2 and ($_->{"type"} // "") eq "text"
ref $_ eq "HASH" and keys %$_ == 2
$_->{"type"} // ""
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 6 | return ( $1, $3 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 15 | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | return 'image/png'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 27 | ▶ | sub plain_messages { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
plain_messages: called CC=7 SCAR=20.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 27 | my ($messages) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | return $messages unless ref $messages eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 27 | ▶ | && grep { ref $_ eq 'HASH' && ref $_->{content} eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $messages eq "ARRAY" and grep {ref $_ eq "HASH" and ref $_->{"content"} eq "ARRAY" and grep {builtin::blessed($_);} @{$$_{"content"};};} @$messages
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 17 | ▶ | && grep { blessed $_ } @{ $_->{content} } } @$messages; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
ref $_ eq 'HASH' && ref $_->{'content'} eq 'ARRAY' && grep({builtin::blessed($_);} @{$$_{'content'};})
ref $_ eq "HASH" and ref $_->{"content"} eq "ARRAY"
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'};})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | return [ map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 2 | ( ref $_ eq 'HASH' && ref $_->{content} eq 'ARRAY' ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 2 | ▶ | ? { %$_, content => [ map { _plain_part($_) } @{ $_->{content} } ] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $_ eq "HASH" and ref $_->{"content"} eq "ARRAY"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 4 | ▶ | if ( $part->isa('Langertha::Content::Image') ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 4 | ▶ | my $url = $part->has_base64 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$part->media_type // "application/octet-stream"
MC/DC0%0/1
$part->media_type // "application/octet-stream"
$part->media_type
Truth table50%1/2
A
A= $part->media_type
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 15 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||