blib/lib/Langertha/ModelProbe.pm

filter: Statementstmt 94.9% 112 / 118 Branchbran 70.2% 52 / 74 Conditioncond 54.1% 39 / 72 MC/DCmcdc 36.5% 15 / 41 Subroutinesub 90.9% 20 / 22 Podpod 100% 9 / 9 totaltotal 73.5% 247 / 336 CC 121 SCAR 21.6
CC
121
Cov
77%
_extract_mistral
13.5
_extract_mistral
13.5
_extract_tsystems
11.2
CRAP 8.7
« blib/lib/Langertha/HTTP/ConnectCheck.pm ↑ Summary blib/lib/Langertha/Runtime/Knobs.pm »
1 package Langertha::ModelProbe;
2 # ABSTRACT: Reads model-scoped capability facts from a provider's own model metadata
3 195 our $VERSION = '0.505';
4 195 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 195 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 195 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 # The capabilities a probe may learn. Metadata that states more (Ollama's
10 # "tools" / "thinking", OpenRouter's supported_parameters, LM Studio's
11 # trained_for_tool_use) is deliberately not read yet: those flags drive the
12 # chat_f rewrite matrix (ADR 0005), image_input is advisory (ADR 0032).
13 195 my @PROBED_CAPABILITIES = qw( image_input );
14  
15 77 ▶sub probed_capabilities { return @PROBED_CAPABILITIES }
Subroutine probed_capabilities: called CC=1 SCAR=0.0
Pod documented
16  
17  
18 195 my %FORMAT = (
19 openrouter => { method => 'GET', extract => \&_extract_openrouter, catalogue => 1 },
20 mistral => { method => 'GET', extract => \&_extract_mistral, catalogue => 1 },
21 lmstudio => { method => 'GET', extract => \&_extract_lmstudio, catalogue => 1 },
22 tsystems => { method => 'GET', extract => \&_extract_tsystems, catalogue => 1 },
23 ollama => { method => 'POST', extract => \&_extract_ollama, per_model => 1 },
24 llamacpp => { method => 'GET', extract => \&_extract_llamacpp },
25 );
26  
27 263 ▶sub _format {
Subroutine _format: called CC=5 SCAR=23.8
28 0 ▶ my ( $class, $format ) = @_;
Subroutine _format: not called CC=5 SCAR=23.8
29 0 ▶ croak "Langertha::ModelProbe: format must be a string, not a reference"
Branch: 2 branches
BranchTrueFalseTotal
if ref $format 0 263 263
if ref $format 0 0 0
30 if ref $format;
31 croak "Langertha::ModelProbe: unknown model_metadata_format '"
32 . ( $format // '(undef)' ) . q{'}
33 0 ▶ unless defined $format && $FORMAT{$format};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $format and $FORMAT{$format} 1 262 263
unless defined $format and $FORMAT{$format} 0 0 0
Condition30%3/10
defined $format and $FORMAT{$format}
!ll&&!rl&&r
01262
$format // "(undef)"
l!l
10
defined $format and $FORMAT{$format}
!ll&&!rl&&r
000
$format // "(undef)"
l!l
00
MC/DC50%1/2
defined $format and $FORMAT{$format}
defined $format $FORMAT{$format}
MC/DC0%0/1
$format // "(undef)"
$format
MC/DC0%0/2
defined $format and $FORMAT{$format}
defined $format $FORMAT{$format}
MC/DC0%0/1
$format // "(undef)"
$format
Truth table66%2/3
A and B
A= defined $formatB= $FORMAT{$format}
ABresult
0X0
100
111
Truth table50%1/2
A
A= $format
Aresult
11
00
Truth table0%0/3
A and B
A= defined $formatB= $FORMAT{$format}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $format
Aresult
11
00
34 0 return $FORMAT{$format};
35 }
36  
37 65 ▶sub is_known_format {
Subroutine is_known_format: called CC=4 SCAR=23.7
Pod documented
38 0 ▶ my ( $class, $format ) = @_;
Subroutine is_known_format: not called CC=4 SCAR=23.7
Pod documented
39 0 ▶ return defined $format && !ref $format && exists $FORMAT{$format} ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
defined $format && !ref($format) && exists $FORMAT{$format} ? : 65 0 65
defined $format && !ref($format) && exists $FORMAT{$format} ? : 0 0 0
Condition16%2/12
defined $format and not ref $format and exists $FORMAT{$format}
!ll&&!rl&&r
0065
defined $format and not ref $format
!ll&&!rl&&r
0065
defined $format and not ref $format and exists $FORMAT{$format}
!ll&&!rl&&r
000
defined $format and not ref $format
!ll&&!rl&&r
000
MC/DC0%0/3
defined $format and not ref $format and exists $FORMAT{$format}
defined $format not ref $format exists $FORMAT{$format}
MC/DC0%0/3
defined $format and not ref $format and exists $FORMAT{$format}
defined $format not ref $format exists $FORMAT{$format}
Truth table25%1/4
A and not(B) and C
A= defined $formatB= not ref $formatC= exists $FORMAT{$format}
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and not(B) and C
A= defined $formatB= not ref $formatC= exists $FORMAT{$format}
ABCresult
0XX0
10X0
1100
1111
40 }
41  
42 59 ▶sub http_method { return $_[0]->_format( $_[1] )->{method} }
Subroutine http_method: called CC=1 SCAR=0.0
Pod documented
43  
44 59 ▶sub per_model { return $_[0]->_format( $_[1] )->{per_model} ? 1 : 0 }
Subroutine per_model: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_format($_[1])->{'per_model'} ? : 15 44 59
45  
46 13 ▶sub is_catalogue { return $_[0]->_format( $_[1] )->{catalogue} ? 1 : 0 }
Subroutine is_catalogue: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_format($_[1])->{'catalogue'} ? : 9 4 13
47  
48  
49 63 ▶sub extract {
Subroutine extract: called CC=3 SCAR=11.0
Pod documented
50 63 my ( $class, $format, $data, $models ) = @_;
51 63 my $spec = $class->_format($format);
52 62 ▶ return {} unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 1 61 62
53 61 ▶ my $facts = $spec->{extract}->( $data, [ grep { _id_ok($_) } @{ ref $models eq q{ARRAY} ? $models : [] } ] );
Branch: 1 branch
BranchTrueFalseTotal
ref $models eq 'ARRAY' ? : 61 0 61
54 61 return $facts;
55 }
56  
57  
58 91 ▶sub server_root_url {
Subroutine server_root_url: called CC=1 SCAR=0.0
Pod documented
59 91 my ( $class, $url ) = @_;
60 91 my $uri = URI->new($url);
61 91 my $path = $uri->path;
62 91 $path =~ s{/v1/?\z}{};
63 91 $path =~ s{/\z}{};
64 91 $uri->path($path);
65 91 return $uri->as_string;
66 }
67  
68  
69 69 ▶sub lookup_ids {
Subroutine lookup_ids: called CC=7 SCAR=19.5
Pod documented
70 69 my ( $class, $format, $model ) = @_;
71 69 $class->_format($format);
72 69 ▶ return () unless _id_ok($model);
Branch: 1 branch
BranchTrueFalseTotal
unless _id_ok($model) 0 69 69
73 69 my @ids = ($model);
74 69 ▶ if ( $format eq 'ollama' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'ollama') { } 10 59 69
elsif ($format eq 'openrouter') { } 21 38 59
75 # Ollama names without a tag mean :latest (llava == llava:latest).
76 10 ▶ if ( $model =~ /\A(.+):latest\z/ ) { push @ids, $1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($model =~ /\A(.+):latest\z/) { } 2 8 10
elsif (not $model =~ /:/) { } 7 1 8
77 7 elsif ( $model !~ /:/ ) { push @ids, "$model:latest" }
78 }
79 elsif ( $format eq 'openrouter' ) {
80 # A routing variant (:online, :free, :nitro, :floor, :thinking, ...) is
81 # the base model's capabilities; used only when the variant itself is
82 # not listed.
83 21 ▶ push @ids, $1 if $model =~ m{\A([^:]+/[^:]+):[A-Za-z0-9._-]+\z};
Branch: 1 branch
BranchTrueFalseTotal
if $model =~ m[\A([^:]+/[^:]+):[A-Za-z0-9._-]+\z] 5 16 21
84 }
85 69 return @ids;
86 }
87  
88  
89 # Only a non-empty plain string is a model id; a reference or an empty
90 # string is never a store key.
91 243 ▶sub _id_ok { return defined $_[0] && !ref $_[0] && length $_[0] ? 1 : 0 }
Subroutine _id_ok: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && !ref($_[0]) && length $_[0] ? : 240 3 243
Condition100%6/6
defined $_[0] and not ref $_[0] and length $_[0]
!ll&&!rl&&r
21240
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
11241
MC/DC100%3/3
defined $_[0] and not ref $_[0] and length $_[0]
defined $_[0] not ref $_[0] length $_[0]
Truth table100%4/4
A and not(B) and C
A= defined $_[0]B= not ref $_[0]C= length $_[0]
ABCresult
0XX0
10X0
1100
1111
92  
93 90 ▶sub _bool { return $_[0] ? 1 : 0 }
Subroutine _bool: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 50 40 90
94  
95 16 ▶sub _extract_openrouter {
Subroutine _extract_openrouter: called CC=9 SCAR=22.3
96 16 my ( $data ) = @_;
97 16 my %facts;
98 16 ▶ for my $model ( @{ ref $data->{data} eq 'ARRAY' ? $data->{data} : [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{'data'} eq 'ARRAY' ? : 16 0 16
99 25 ▶ next unless ref $model eq 'HASH' && ref $model->{architecture} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $model eq "HASH" and ref $model->{"architecture"} eq "HASH" 3 22 25
Condition66%2/3
ref $model eq "HASH" and ref $model->{"architecture"} eq "HASH"
!ll&&!rl&&r
0322
MC/DC50%1/2
ref $model eq "HASH" and ref $model->{"architecture"} eq "HASH"
ref $model eq "HASH" ref $model->{"architecture"} eq "HASH"
Truth table66%2/3
A and B
A= ref $model eq "HASH"B= ref $model->{"architecture"} eq "HASH"
ABresult
0X0
100
111
100 22 my $in = $model->{architecture}{input_modalities};
101 22 ▶ next unless ref $in eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $in eq "ARRAY" 0 22 22
102 22 ▶ my $vision = _bool( grep { defined && $_ eq 'image' } @$in );
Condition66%2/3
defined $_ && $_ eq 'image'
!ll&&!rl&&r
03111
MC/DC50%1/2
defined $_ && $_ eq 'image'
defined $_ $_ eq 'image'
Truth table66%2/3
A && B
A= defined $_B= $_ eq 'image'
ABresult
0X0
100
111
103 22 for my $id ( grep { _id_ok($_) } $model->{id}, $model->{canonical_slug} ) {
104 41 ▶ $facts{$id} //= { image_input => $vision };
Condition100%2/2
$facts{$id} //= {"image_input", $vision}
l!l
2021
MC/DC100%1/1
$facts{$id} //= {"image_input", $vision}
$facts{$id}
Truth table100%2/2
A
A= $facts{$id}
Aresult
11
00
105 }
106 }
107 16 return \%facts;
108 }
109  
110 6 ▶sub _extract_mistral {
Subroutine _extract_mistral: called CC=11 SCAR=26.1
111 6 my ( $data ) = @_;
112 6 my ( %by_id, %by_alias );
113 6 ▶ for my $model ( @{ ref $data->{data} eq 'ARRAY' ? $data->{data} : [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{'data'} eq 'ARRAY' ? : 6 0 6
114 17 ▶ next unless ref $model eq 'HASH' && ref $model->{capabilities} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $model eq "HASH" and ref $model->{"capabilities"} eq "HASH" 0 17 17
Condition33%1/3
ref $model eq "HASH" and ref $model->{"capabilities"} eq "HASH"
!ll&&!rl&&r
0017
MC/DC0%0/2
ref $model eq "HASH" and ref $model->{"capabilities"} eq "HASH"
ref $model eq "HASH" ref $model->{"capabilities"} eq "HASH"
Truth table33%1/3
A and B
A= ref $model eq "HASH"B= ref $model->{"capabilities"} eq "HASH"
ABresult
0X0
100
111
115 17 ▶ next unless exists $model->{capabilities}{vision};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $model->{"capabilities"}{"vision"} 0 17 17
116 17 my $fact = { image_input => _bool( $model->{capabilities}{vision} ) };
117 17 ▶ $by_id{ $model->{id} } = $fact if _id_ok( $model->{id} );
Branch: 1 branch
BranchTrueFalseTotal
if _id_ok($model->{"id"}) 17 0 17
118 17 ▶ for my $alias ( @{ ref $model->{aliases} eq 'ARRAY' ? $model->{aliases} : [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $model->{'aliases'} eq 'ARRAY' ? : 13 4 17
119 17 ▶ $by_alias{$alias} //= { %$fact } if _id_ok($alias);
Branch: 1 branch
BranchTrueFalseTotal
if _id_ok($alias) 17 0 17
Condition50%1/2
$by_alias{$alias} //= {%$fact}
l!l
017
MC/DC0%0/1
$by_alias{$alias} //= {%$fact}
$by_alias{$alias}
Truth table50%1/2
A
A= $by_alias{$alias}
Aresult
11
00
120 }
121 }
122 6 return { %by_alias, %by_id };
123 }
124  
125 10 ▶sub _extract_lmstudio {
Subroutine _extract_lmstudio: called CC=10 SCAR=23.6
126 10 my ( $data ) = @_;
127 10 my %facts;
128 10 ▶ for my $model ( @{ ref $data->{models} eq 'ARRAY' ? $data->{models} : [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{'models'} eq 'ARRAY' ? : 10 0 10
129 30 ▶ next unless ref $model eq 'HASH' && ref $model->{capabilities} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $model eq "HASH" and ref $model->{"capabilities"} eq "HASH" 10 20 30
Condition66%2/3
ref $model eq "HASH" and ref $model->{"capabilities"} eq "HASH"
!ll&&!rl&&r
01020
MC/DC50%1/2
ref $model eq "HASH" and ref $model->{"capabilities"} eq "HASH"
ref $model eq "HASH" ref $model->{"capabilities"} eq "HASH"
Truth table66%2/3
A and B
A= ref $model eq "HASH"B= ref $model->{"capabilities"} eq "HASH"
ABresult
0X0
100
111
130 20 ▶ next unless exists $model->{capabilities}{vision};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $model->{"capabilities"}{"vision"} 0 20 20
131 20 my $vision = _bool( $model->{capabilities}{vision} );
132 my @ids = ( $model->{key},
133 10 ▶ map { ref $_ eq 'HASH' ? $_->{id} : () }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 10 0 10
134 20 ▶ @{ ref $model->{loaded_instances} eq 'ARRAY' ? $model->{loaded_instances} : [] } );
Branch: 1 branch
BranchTrueFalseTotal
ref $model->{'loaded_instances'} eq 'ARRAY' ? : 20 0 20
135 20 ▶ $facts{$_} //= { image_input => $vision } for grep { _id_ok($_) } @ids;
Condition100%2/2
$facts{$_} //= {"image_input", $vision}
l!l
1020
MC/DC100%1/1
$facts{$_} //= {"image_input", $vision}
$facts{$_}
Truth table100%2/2
A
A= $facts{$_}
Aresult
11
00
136 }
137 10 return \%facts;
138 }
139  
140 4 ▶sub _extract_tsystems {
Subroutine _extract_tsystems: called CC=10 SCAR=24.1
141 4 my ( $data ) = @_;
142 4 my %facts;
143 4 ▶ for my $model ( @{ ref $data->{data} eq 'ARRAY' ? $data->{data} : [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{'data'} eq 'ARRAY' ? : 4 0 4
144 24 ▶ next unless ref $model eq 'HASH' && ref $model->{meta_data} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $model eq "HASH" and ref $model->{"meta_data"} eq "HASH" 4 20 24
Condition66%2/3
ref $model eq "HASH" and ref $model->{"meta_data"} eq "HASH"
!ll&&!rl&&r
0420
MC/DC50%1/2
ref $model eq "HASH" and ref $model->{"meta_data"} eq "HASH"
ref $model eq "HASH" ref $model->{"meta_data"} eq "HASH"
Truth table66%2/3
A and B
A= ref $model eq "HASH"B= ref $model->{"meta_data"} eq "HASH"
ABresult
0X0
100
111
145 20 my $in = $model->{meta_data}{input_modalities};
146 20 ▶ next unless ref $in eq 'ARRAY' && _id_ok( $model->{id} );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $in eq "ARRAY" and _id_ok($model->{"id"}) 8 12 20
Condition66%2/3
ref $in eq "ARRAY" and _id_ok($model->{"id"})
!ll&&!rl&&r
8012
MC/DC50%1/2
ref $in eq "ARRAY" and _id_ok($model->{"id"})
ref $in eq "ARRAY" _id_ok($model->{"id"})
Truth table66%2/3
A and B
A= ref $in eq "ARRAY"B= _id_ok($model->{"id"})
ABresult
0X0
100
111
147 12 ▶ my $vision = _bool( grep { defined && !ref && lc eq 'image' } @$in );
Condition50%3/6
defined $_ && !ref($_) && lc $_ eq 'image'
!ll&&!rl&&r
0128
defined $_ and not ref $_
!ll&&!rl&&r
0020
MC/DC33%1/3
defined $_ && !ref($_) && lc $_ eq 'image'
defined $_ not ref $_ lc $_ eq 'image'
Truth table50%2/4
A and not(B) && C
A= defined $_B= not ref $_C= lc $_ eq 'image'
ABCresult
0XX0
10X0
1100
1111
148 12 ▶ $facts{ $model->{id} } //= { image_input => $vision };
Condition50%1/2
$facts{$model->{"id"}} //= {"image_input", $vision}
l!l
012
MC/DC0%0/1
$facts{$model->{"id"}} //= {"image_input", $vision}
$facts{$model->{"id"}}
Truth table50%1/2
A
A= $facts{$model->{"id"}}
Aresult
11
00
149 }
150 4 return \%facts;
151 }
152  
153 19 ▶sub _extract_ollama {
Subroutine _extract_ollama: called CC=4 SCAR=14.0
154 19 my ( $data, $models ) = @_;
155 19 my $caps = $data->{capabilities};
156 19 ▶ return {} unless ref $caps eq 'ARRAY' && @$models;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $caps eq "ARRAY" and @$models 4 15 19
Condition66%2/3
ref $caps eq "ARRAY" and @$models
!ll&&!rl&&r
4015
MC/DC50%1/2
ref $caps eq "ARRAY" and @$models
ref $caps eq "ARRAY" @$models
Truth table66%2/3
A and B
A= ref $caps eq "ARRAY"B= @$models
ABresult
0X0
100
111
157 15 ▶ my $vision = _bool( grep { defined && $_ eq 'vision' } @$caps );
Condition66%2/3
defined $_ && $_ eq 'vision'
!ll&&!rl&&r
02010
MC/DC50%1/2
defined $_ && $_ eq 'vision'
defined $_ $_ eq 'vision'
Truth table66%2/3
A && B
A= defined $_B= $_ eq 'vision'
ABresult
0X0
100
111
158 15 return { map { $_ => { image_input => $vision } } @$models };
159 }
160  
161 6 ▶sub _extract_llamacpp {
Subroutine _extract_llamacpp: called CC=4 SCAR=14.0
162 6 my ( $data, $models ) = @_;
163 6 my $modalities = $data->{modalities};
164 6 ▶ return {} unless ref $modalities eq 'HASH' && exists $modalities->{vision} && @$models;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $modalities eq "HASH" and exists $modalities->{"vision"} and @$models 2 4 6
Condition66%4/6
ref $modalities eq "HASH" and exists $modalities->{"vision"} and @$models
!ll&&!rl&&r
204
ref $modalities eq "HASH" and exists $modalities->{"vision"}
!ll&&!rl&&r
204
MC/DC33%1/3
ref $modalities eq "HASH" and exists $modalities->{"vision"} and @$models
ref $modalities eq "HASH" exists $modalities->{"vision"} @$models
Truth table50%2/4
A and B and C
A= ref $modalities eq "HASH"B= exists $modalities->{"vision"}C= @$models
ABCresult
0XX0
10X0
1100
1111
165 4 my $vision = _bool( $modalities->{vision} );
166 4 return { map { $_ => { image_input => $vision } } @$models };
167 }
168  
169 195 __PACKAGE__->meta->make_immutable;
170  
171  
172 195 1;
173  
174 __END__
« blib/lib/Langertha/HTTP/ConnectCheck.pm ↑ Summary blib/lib/Langertha/Runtime/Knobs.pm »