blib/lib/Langertha/Knarr/Router.pm

filter: Statementstmt 62.5% 147 / 235 Branchbran 51.4% 70 / 136 Conditioncond 33.7% 31 / 92 MC/DCmcdc 25.9% 14 / 54 Subroutinesub 70.0% 21 / 30 Podpod 100% 6 / 6 totaltotal 52.2% 289 / 553 CC 111 SCAR 46.9
CC
111
Cov
54%
probe_capabilities_f
552.0
__ANON__
90.0
__ANON__
42.0
CRAP 108.8
« blib/lib/Langertha/Knarr/CLI/Cmd/Check.pm ↑ Summary blib/lib/Langertha/Knarr/Admin.pm »
1 package Langertha::Knarr::Router;
2 33 our $VERSION = '1.103';
3 # ABSTRACT: Model name to Langertha engine routing with caching
4 33 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use Scalar::Util qw( blessed refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use Future::Utils qw( fmap_void );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 33 ▶use Langertha ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 33 ▶use Langertha::Knarr::Handler::Passthrough;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 # Keys of a model config that describe that one model and therefore never
15 # pass to the models discovered through it (k31). Everything else is
16 # endpoint-level and inherited; see the POD above.
17 33 my @MODEL_SPECIFIC_KEYS = qw( model context_size );
18  
19 33 has config => (
20 is => 'ro',
21 required => 1,
22 );
23  
24  
25 has _engine_cache => (
26 is => 'ro',
27 51 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 33 );
29  
30 has _discovered_models => (
31 is => 'rw',
32 51 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 33 );
34  
35 33 has _discovery_done => (
36 is => 'rw',
37 default => 0,
38 );
39  
40 # Engine instances a capability probe was started on (k37), by refaddr: each
41 # cached instance is probed once, whether the probe succeeded or not.
42 has _probed => (
43 is => 'ro',
44 51 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 33 );
46  
47 # What a whole-catalogue probe learned, by endpoint key (k38): an instance
48 # that turns up on that endpoint later imports it instead of asking again.
49 has _catalogue_learned => (
50 is => 'ro',
51 51 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
52 33 );
53  
54 33 has capabilities_generation => (
55 is => 'rw',
56 default => 0,
57 );
58  
59  
60  
61 33 ▶sub resolve {
Subroutine resolve: called CC=15 SCAR=27.1
Pod documented
62 33 my ($self, $model_name, %opts) = @_;
63 33 ▶ my $named = defined $model_name && length $model_name ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $model_name && length $model_name ? : 25 8 33
Condition100%3/3
defined $model_name and length $model_name
!ll&&!rl&&r
7125
MC/DC100%2/2
defined $model_name and length $model_name
defined $model_name length $model_name
Truth table100%3/3
A and B
A= defined $model_nameB= length $model_name
ABresult
0X0
100
111
64 33 my $def;
65  
66 33 ▶ if ($named) {
Branch: 1 branch
BranchTrueFalseTotal
if ($named) 25 8 33
67 # Check explicit config first
68 25 $def = $self->config->models->{$model_name};
69  
70 # Check discovered models
71 25 ▶ unless ($def) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($def) 9 16 25
72 9 ▶ $self->_discover_models unless $self->_discovery_done;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_discovery_done 6 3 9
73 9 $def = $self->_discovered_models->{$model_name};
74 }
75 }
76  
77 # Fall back to default engine (skip_default allows caller to try passthrough first)
78 33 ▶ unless ($def) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($def) 13 20 33
79 13 ▶ return () if $opts{skip_default};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"skip_default"} 0 13 13
80 13 $def = $self->config->default_engine;
81 13 ▶ croak "No model specified" unless $named || $def;
Branch: 1 branch
BranchTrueFalseTotal
unless $named or $def 2 11 13
Condition100%3/3
$named or $def
l!l&&r!l&&!r
562
MC/DC100%2/2
$named or $def
$named $def
Truth table100%3/3
A or B
A= $namedB= $def
ABresult
1X1
011
000
82 # A named model replaces the default engine's model:; a request without
83 # one (A2A, ...) gets the default engine as configured (k42).
84 11 ▶ $def = { %$def, model => $model_name } if $def && $named;
Branch: 1 branch
BranchTrueFalseTotal
if $def and $named 4 7 11
Condition100%3/3
$def and $named
!ll&&!rl&&r
164
MC/DC100%2/2
$def and $named
$def $named
Truth table100%3/3
A and B
A= $defB= $named
ABresult
0X0
100
111
85 }
86  
87 31 ▶ croak "Model '$model_name' not configured and no default engine" unless $def;
Branch: 1 branch
BranchTrueFalseTotal
unless $def 1 30 31
88  
89 30 my $engine = $self->_get_engine($def, $model_name);
90 30 ▶ my $resolved_model = $def->{model} // $model_name;
Condition100%3/3
$def->{"model"} // $model_name
l!l&&r!l&&!r
2136
MC/DC100%2/2
$def->{"model"} // $model_name
$def->{"model"} $model_name
Truth table100%3/3
A // B
A= $def->{"model"}B= $model_name
ABresult
1X1
011
000
91 # No model: key means the engine was built without a model and the
92 # provider default answers; $resolved_model is only the alias then (k22).
93 30 ▶ my $alias_only = defined $def->{model} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $def->{'model'} ? : 21 9 30
94  
95 30 return ($engine, $resolved_model, $alias_only);
96 }
97  
98 0 ▶sub engine_for {
Subroutine engine_for: not called CC=1 SCAR=6.9
Pod documented
99 0 my ($self, $def, $model_name) = @_;
100 0 return $self->_get_engine( $def, $model_name );
101 }
102  
103  
104 146 ▶sub _get_engine {
Subroutine _get_engine: called CC=21 SCAR=33.3
105 146 my ($self, $def, $model_name) = @_;
106  
107 146 my $engine_class = $def->{engine};
108 146 my $cache_key = $self->_engine_cache_key($def);
109  
110 146 ▶ if (my $cached = $self->_engine_cache->{$cache_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cached = $self->_engine_cache->{$cache_key}) 7 139 146
111 7 return $cached;
112 }
113  
114 139 my $full_class = Langertha->resolve_engine_class($engine_class);
115  
116 139 my %args;
117  
118 # API key from env var if specified
119 139 ▶ if ($def->{api_key_env}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($def->{'api_key_env'}) { } 6 133 139
elsif ($def->{'api_key'}) { } 2 131 133
120 $args{api_key} = $ENV{$def->{api_key_env}}
121 6 ▶ // croak "Environment variable $def->{api_key_env} not set for engine $engine_class";
Condition50%1/2
$ENV{$def->{"api_key_env"}} // croak(qq[Environment variable $def->{"api_key_env"} not set for engine $engine_class])
l!l
60
MC/DC0%0/1
$ENV{$def->{"api_key_env"}} // croak(qq[Environment variable $def->{"api_key_env"} not set for engine $engine_class])
$ENV{$def->{"api_key_env"}}
Truth table50%1/2
A
A= $ENV{$def->{"api_key_env"}}
Aresult
11
00
122 } elsif ($def->{api_key}) {
123 2 $args{api_key} = $def->{api_key};
124 }
125  
126 139 ▶ $args{url} = $def->{url} if $def->{url};
Branch: 1 branch
BranchTrueFalseTotal
if $def->{"url"} 130 9 139
127 139 ▶ $args{model} = $def->{model} if $def->{model};
Branch: 1 branch
BranchTrueFalseTotal
if $def->{"model"} 20 119 139
128 139 ▶ $args{system_prompt} = $def->{system_prompt} if $def->{system_prompt};
Branch: 1 branch
BranchTrueFalseTotal
if $def->{"system_prompt"} 2 137 139
129 139 ▶ $args{temperature} = $def->{temperature} if defined $def->{temperature};
Branch: 1 branch
BranchTrueFalseTotal
if defined $def->{"temperature"} 2 137 139
130 139 ▶ $args{response_size} = $def->{response_size} if defined $def->{response_size};
Branch: 1 branch
BranchTrueFalseTotal
if defined $def->{"response_size"} 2 137 139
131 # context_size is operator intent: engines composing core's
132 # Role::ContextSize take it (and put it on their wire, e.g. Ollama's
133 # num_ctx); Config warned once at load about engines that cannot (k30).
134 $args{context_size} = $def->{context_size}
135 139 ▶ if defined $def->{context_size} && $full_class->can('context_size');
Branch: 1 branch
BranchTrueFalseTotal
if defined $def->{"context_size"} and $full_class->can("context_size") 2 137 139
Condition100%3/3
defined $def->{"context_size"} and $full_class->can("context_size")
!ll&&!rl&&r
13522
MC/DC100%2/2
defined $def->{"context_size"} and $full_class->can("context_size")
defined $def->{"context_size"} $full_class->can("context_size")
Truth table100%3/3
A and B
A= defined $def->{"context_size"}B= $full_class->can("context_size")
ABresult
0X0
100
111
136  
137 # A hanging upstream must not hold the client's request open forever
138 # (k35): the model config's own user_agent_timeout, else the global
139 # upstream_timeout. 0 leaves the engine without one.
140 139 ▶ my $timeout = $def->{user_agent_timeout} // $self->config->upstream_timeout;
Condition33%1/3
$def->{"user_agent_timeout"} // $self->config->upstream_timeout
l!l&&r!l&&!r
01390
MC/DC0%0/2
$def->{"user_agent_timeout"} // $self->config->upstream_timeout
$def->{"user_agent_timeout"} $self->config->upstream_timeout
Truth table33%1/3
A // B
A= $def->{"user_agent_timeout"}B= $self->config->upstream_timeout
ABresult
1X1
011
000
141 139 ▶ $args{user_agent_timeout} = $timeout
Branch: 1 branch
BranchTrueFalseTotal
if $timeout and $timeout > 0 and $full_class->can("user_agent_timeout") 19 120 139
Condition50%3/6
$timeout and $timeout > 0 and $full_class->can("user_agent_timeout")
!ll&&!rl&&r
012019
$timeout and $timeout > 0
!ll&&!rl&&r
00139
MC/DC33%1/3
$timeout and $timeout > 0 and $full_class->can("user_agent_timeout")
$timeout $timeout > 0 $full_class->can("user_agent_timeout")
Truth table50%2/4
A and B and C
A= $timeoutB= $timeout > 0C= $full_class->can("user_agent_timeout")
ABCresult
0XX0
10X0
1100
1111
142 if $timeout && $timeout > 0 && $full_class->can('user_agent_timeout');
143  
144 # Langfuse config from global config
145 139 my $langfuse = $self->config->langfuse;
146 139 ▶ if ($langfuse && %$langfuse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($langfuse and %$langfuse) 0 139 139
Condition33%1/3
$langfuse and %$langfuse
!ll&&!rl&&r
01390
MC/DC0%0/2
$langfuse and %$langfuse
$langfuse %$langfuse
Truth table33%1/3
A and B
A= $langfuseB= %$langfuse
ABresult
0X0
100
111
147 0 ▶ $args{langfuse_public_key} = $langfuse->{public_key} if $langfuse->{public_key};
Branch: 1 branch
BranchTrueFalseTotal
if $langfuse->{"public_key"} 0 0 0
148 0 ▶ $args{langfuse_secret_key} = $langfuse->{secret_key} if $langfuse->{secret_key};
Branch: 1 branch
BranchTrueFalseTotal
if $langfuse->{"secret_key"} 0 0 0
149 0 ▶ $args{langfuse_url} = $langfuse->{url} if $langfuse->{url};
Branch: 1 branch
BranchTrueFalseTotal
if $langfuse->{"url"} 0 0 0
150 }
151  
152 139 $log->debugf("Creating engine %s for model %s", $full_class, $model_name);
153 139 my $engine = $full_class->new(%args);
154  
155 139 $self->_engine_cache->{$cache_key} = $engine;
156 139 return $engine;
157 }
158  
159 146 ▶sub _engine_cache_key {
Subroutine _engine_cache_key: called CC=2 SCAR=6.9
160 146 my ($self, $def) = @_;
161 # The model is part of the key: chat_model is read-only on an engine, and
162 # core evaluates model-scoped capabilities against it, so each model needs
163 # its own instance (k20). context_size is read-only on the engine too, so
164 # two aliases of one model with different windows need two instances (k30);
165 # so is user_agent_timeout (k35).
166 return join('|', $self->_endpoint_key($def),
167 146 ▶ map { $def->{$_} // '' } qw( model context_size user_agent_timeout ));
Condition100%2/2
$def->{$_} // ""
l!l
30408
MC/DC100%1/1
$def->{$_} // ""
$def->{$_}
Truth table100%2/2
A
A= $def->{$_}
Aresult
11
00
168 }
169  
170 # One upstream endpoint: engine, url and API key variable. Discovery runs
171 # once per endpoint (k23).
172 262 ▶sub _endpoint_key {
Subroutine _endpoint_key: called CC=2 SCAR=6.9
173 262 my ($self, $def) = @_;
174 262 ▶ return join('|', map { $def->{$_} // '' } qw( engine url api_key_env ));
Condition100%2/2
$def->{$_} // ""
l!l
521265
MC/DC100%1/1
$def->{$_} // ""
$def->{$_}
Truth table100%2/2
A
A= $def->{$_}
Aresult
11
00
175 }
176  
177 38 ▶sub _discover_models {
Subroutine _discover_models: called CC=17 SCAR=29.3
178 38 my ($self) = @_;
179 38 ▶ return if $self->_discovery_done;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_discovery_done 0 38 38
180 38 $self->_discovery_done(1);
181  
182 38 ▶ return unless $self->config->auto_discover;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config->auto_discover 7 31 38
183  
184 31 my $models = $self->config->models;
185 31 my %seen_endpoints;
186  
187 # One model config per endpoint, in a fixed order: an id several endpoints
188 # list belongs to the first (k55). Endpoints that are a passthrough
189 # upstream come first -- their models may pass through as the client sent
190 # them -- then the others, each group by model config name.
191 31 my ( @upstream, @other );
192 31 for my $name (sort keys %$models) {
193 116 my $def = $models->{$name};
194 116 ▶ next if $seen_endpoints{ $self->_endpoint_key($def) }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_endpoints{$self->_endpoint_key($def)}++ 0 116 116
195 116 my $engine = eval { $self->_get_engine($def, $name) };
196 116 ▶ unless ($engine) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($engine) 0 116 116
197 0 $log->debugf("Model discovery skipped for %s: %s", $def->{engine}, $@);
198 0 next;
199 }
200 116 ▶ push @{ $self->_is_passthrough_upstream($engine) ? \@upstream : \@other },
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_passthrough_upstream($engine) ? : 15 101 116
201 [ $name, $def, $engine ];
202 }
203  
204 31 my %owner;
205 31 for my $endpoint (@upstream, @other) {
206 116 my ($name, $def, $engine) = @$endpoint;
207 116 my $engine_class = $def->{engine};
208 116 eval {
209 116 ▶ if ($engine->can('list_models')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($engine->can("list_models")) 116 0 116
210 116 $log->debugf("Discovering models from %s", $engine_class);
211 116 my $model_ids = $engine->list_models;
212 116 for my $id (@$model_ids) {
213 122 ▶ next if $models->{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $models->{$id} 0 122 122
214 122 ▶ if ( my $first = $owner{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $first = $owner{$id}) 84 38 122
215 84 ▶ $log->debugf("Discovered model %s also listed by %s, stays with %s",
Branch: 1 branch
BranchTrueFalseTotal
if $first ne $name 84 0 84
216 $id, $name, $first) if $first ne $name;
217 84 next;
218 }
219 38 ▶ next if $self->_discovered_models->{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_discovered_models->{$id} 0 38 38
220 38 $owner{$id} = $name;
221 38 my %inherited = %$def;
222 38 delete @inherited{@MODEL_SPECIFIC_KEYS};
223 38 ▶ $self->_discovered_models->{$id} = {
Branch: 1 branch
BranchTrueFalseTotal
$engine->can('url') && defined $engine->url ? : 34 4 38
Condition66%2/3
$engine->can("url") and defined $engine->url
!ll&&!rl&&r
4034
MC/DC50%1/2
$engine->can("url") and defined $engine->url
$engine->can("url") defined $engine->url
Truth table66%2/3
A and B
A= $engine->can("url")B= defined $engine->url
ABresult
0X0
100
111
224 %inherited,
225 model => $id,
226 discovered => 1,
227 # Where the model was listed: its passthrough upstream may
228 # serve it as is (k47).
229 ( $engine->can('url') && defined $engine->url
230 ? ( discovered_url => $engine->url ) : () ),
231 };
232 38 $log->debugf("Discovered model: %s (via %s)", $id, $engine_class);
233 }
234 }
235 };
236 116 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 116 116
237 0 $log->debugf("Model discovery skipped for %s: %s", $engine_class, $@);
238 }
239 }
240 }
241  
242 # The class that knows when two URLs are the same upstream (k54).
243 60 ▶sub _passthrough_class { 'Langertha::Knarr::Handler::Passthrough' }
Subroutine _passthrough_class: called CC=1 SCAR=0.0
244  
245 # True when the engine's URL is the passthrough upstream of some protocol.
246 116 ▶sub _is_passthrough_upstream {
Subroutine _is_passthrough_upstream: called CC=7 SCAR=19.6
247 116 my ($self, $engine) = @_;
248 116 ▶ my $url = $engine->can('url') ? $engine->url : undef;
Branch: 1 branch
BranchTrueFalseTotal
$engine->can('url') ? : 113 3 116
249 116 ▶ return 0 unless defined $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url 3 113 116
250 113 ▶ my $upstreams = $self->config->can('passthrough') ? $self->config->passthrough : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->config->can('passthrough') ? : 113 0 113
251 113 ▶ return 0 unless ref $upstreams eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $upstreams eq "HASH" 0 113 113
252 113 for my $base ( values %$upstreams ) {
253 60 ▶ return 1 if $self->_passthrough_class->same_upstream( $base, $url );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_passthrough_class->same_upstream($base, $url) 15 45 60
254 }
255 98 return 0;
256 }
257  
258  
259 4 ▶sub list_models {
Subroutine list_models: called CC=7 SCAR=20.4
Pod documented
260 4 my ($self) = @_;
261  
262 4 ▶ $self->_discover_models unless $self->_discovery_done;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_discovery_done 4 0 4
263  
264 4 my $configured = $self->config->models;
265 4 my $discovered = $self->_discovered_models;
266 4 my @models;
267  
268 4 for my $name (sort keys %$configured) {
269 push @models, {
270 id => $name,
271 engine => $configured->{$name}{engine},
272 6 ▶ model => $configured->{$name}{model} // $name,
Condition66%2/3
$configured->{$name}{"model"} // $name
l!l&&r!l&&!r
420
MC/DC0%0/2
$configured->{$name}{"model"} // $name
$configured->{$name}{"model"} $name
Truth table66%2/3
A // B
A= $configured->{$name}{"model"}B= $name
ABresult
1X1
011
000
273 source => 'configured',
274 };
275 }
276  
277 4 for my $name (sort keys %$discovered) {
278 2 ▶ next if $configured->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $configured->{$name} 0 2 2
279 push @models, {
280 id => $name,
281 engine => $discovered->{$name}{engine},
282 2 ▶ model => $discovered->{$name}{model} // $name,
Condition33%1/3
$discovered->{$name}{"model"} // $name
l!l&&r!l&&!r
200
MC/DC0%0/2
$discovered->{$name}{"model"} // $name
$discovered->{$name}{"model"} $name
Truth table33%1/3
A // B
A= $discovered->{$name}{"model"}B= $name
ABresult
1X1
011
000
283 source => 'discovered',
284 };
285 }
286  
287 4 return \@models;
288 }
289  
290  
291 0 ▶sub probe_capabilities_f {
Subroutine probe_capabilities_f: not called CC=23 SCAR=63.1
Pod documented
292 0 my ($self, %args) = @_;
293 0 my $config = $self->config;
294 0 ▶ return Future->done(0)
Branch: 1 branch
BranchTrueFalseTotal
unless not $config->can("probe_capabilities") or $config->probe_capabilities 0 0 0
Condition0%0/3
not $config->can("probe_capabilities") or $config->probe_capabilities
l!l&&r!l&&!r
000
MC/DC0%0/2
not $config->can("probe_capabilities") or $config->probe_capabilities
not $config->can("probe_capabilities") $config->probe_capabilities
Truth table0%0/3
not(A) or B
A= not $config->can("probe_capabilities")B= $config->probe_capabilities
ABresult
1X1
011
000
295 unless !$config->can('probe_capabilities') || $config->probe_capabilities;
296 0 my $loop = $args{loop};
297 my $timeout = $args{timeout}
298 0 ▶ // ( $config->can('probe_timeout') ? $config->probe_timeout : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$config->can('probe_timeout') ? : 0 0 0
Condition0%0/3
$args{"timeout"} // ($config->can("probe_timeout") ? $config->probe_timeout : 0)
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"timeout"} // ($config->can("probe_timeout") ? $config->probe_timeout : 0)
$args{"timeout"} ($config->can("probe_timeout") ? $config->probe_timeout : 0)
Truth table0%0/3
A // B
A= $args{"timeout"}B= ($config->can("probe_timeout") ? $config->probe_timeout : 0)
ABresult
1X1
011
000
299  
300 0 my ( @targets, %endpoint, @endpoint_order );
301 0 my $imported = 0;
302 0 for my $row (@{ $self->list_models }) {
303 0 my ($engine, $model, $alias_only) = eval { $self->resolve( $row->{id}, skip_default => 1 ) };
304 next unless blessed $engine && $engine->can('probe_model_capabilities_f')
305 && $engine->can('model_metadata_format')
306 0 ▶ && defined eval { $engine->model_metadata_format };
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($engine) and $engine->can("probe_model_capabilities_f") and $engine->can("model_metadata_format") and defined eval { do { $engine->model_metadata_format } } 0 0 0
Condition0%0/9
builtin::blessed($engine) and $engine->can("probe_model_capabilities_f") and $engine->can("model_metadata_format") and defined eval { do { $engine->model_metadata_format } }
!ll&&!rl&&r
000
builtin::blessed($engine) and $engine->can("probe_model_capabilities_f") and $engine->can("model_metadata_format")
!ll&&!rl&&r
000
builtin::blessed($engine) and $engine->can("probe_model_capabilities_f")
!ll&&!rl&&r
000
MC/DC0%0/4
builtin::blessed($engine) and $engine->can("probe_model_capabilities_f") and $engine->can("model_metadata_format") and defined eval { do { $engine->model_metadata_format } }
builtin::blessed($engine) $engine->can("probe_model_capabilities_f") $engine->can("model_metadata_format") defined eval { do { $engine->model_metadata_format } }
Truth table0%0/5
A and B and C and D
A= builtin::blessed($engine)B= $engine->can("probe_model_capabilities_f")C= $engine->can("model_metadata_format")D= defined eval { do { $engine->model_metadata_format } }
ABCDresult
0XXX0
10XX0
110X0
11100
11111
307 0 ▶ next if $self->_probed->{ refaddr $engine };
Branch: 1 branch
BranchTrueFalseTotal
if $self->_probed->{builtin::refaddr($engine)} 0 0 0
308 # An alias without model: key asks about the engine's own default (k22).
309 0 ▶ my $upstream = $alias_only ? eval { $engine->chat_model } : $model;
Branch: 1 branch
BranchTrueFalseTotal
$alias_only ? : 0 0 0
310 0 ▶ next unless defined $upstream && !ref $upstream && length $upstream;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $upstream and not ref $upstream and length $upstream 0 0 0
Condition0%0/6
defined $upstream and not ref $upstream and length $upstream
!ll&&!rl&&r
000
defined $upstream and not ref $upstream
!ll&&!rl&&r
000
MC/DC0%0/3
defined $upstream and not ref $upstream and length $upstream
defined $upstream not ref $upstream length $upstream
Truth table0%0/4
A and not(B) and C
A= defined $upstreamB= not ref $upstreamC= length $upstream
ABCresult
0XX0
10X0
1100
1111
311 0 $self->_probed->{ refaddr $engine } = 1;
312  
313 # A catalogue answers for every model of its endpoint: one request per
314 # endpoint, imported into the endpoint's other instances (k38).
315 0 ▶ my $def = $config->models->{ $row->{id} } // $self->_discovered_models->{ $row->{id} };
Condition0%0/3
$config->models->{$row->{"id"}} // $self->_discovered_models->{$row->{"id"}}
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->models->{$row->{"id"}} // $self->_discovered_models->{$row->{"id"}}
$config->models->{$row->{"id"}} $self->_discovered_models->{$row->{"id"}}
Truth table0%0/3
A // B
A= $config->models->{$row->{"id"}}B= $self->_discovered_models->{$row->{"id"}}
ABresult
1X1
011
000
316 0 ▶ if ( $def && $self->_shares_catalogue($engine) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def and $self->_shares_catalogue($engine)) 0 0 0
Condition0%0/3
$def and $self->_shares_catalogue($engine)
!ll&&!rl&&r
000
MC/DC0%0/2
$def and $self->_shares_catalogue($engine)
$def $self->_shares_catalogue($engine)
Truth table0%0/3
A and B
A= $defB= $self->_shares_catalogue($engine)
ABresult
0X0
100
111
317 0 my $key = $self->_endpoint_key($def);
318 0 ▶ if ( my $learned = $self->_catalogue_learned->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $learned = $self->_catalogue_learned->{$key}) 0 0 0
319 0 $engine->import_learned_capabilities($learned);
320 0 $imported++;
321 0 next;
322 }
323 0 ▶ push @endpoint_order, $key unless $endpoint{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $endpoint{$key} 0 0 0
324 0 push @{ $endpoint{$key} }, [ $engine, $upstream, $row->{id} ];
325 0 next;
326 }
327 0 push @targets, [ $engine, $upstream, $row->{id} ];
328 }
329 0 ▶ $self->capabilities_generation( $self->capabilities_generation + 1 ) if $imported;
Branch: 1 branch
BranchTrueFalseTotal
if $imported 0 0 0
330 0 my $covered = $imported + @targets;
331 0 $covered += @{ $endpoint{$_} } for @endpoint_order;
332 0 push @targets, map { [ $endpoint{$_}, undef, $_ ] } @endpoint_order;
333 0 ▶ return Future->done($covered) unless @targets;
Branch: 1 branch
BranchTrueFalseTotal
unless @targets 0 0 0
334  
335 0 ▶ return ( fmap_void {
Subroutine __ANON__: not called CC=9 SCAR=45.0
336 # Per model: [ $engine, $upstream, $id ]; per endpoint: [ \@members, undef, $key ].
337 0 my ($target, $upstream, $id) = @{ $_[0] };
338 0 my $shared = ref $target eq 'ARRAY';
339 0 ▶ my ($engine, @rest) = $shared ? map { $_->[0] } @$target : ( $target );
Branch: 1 branch
BranchTrueFalseTotal
$shared ? : 0 0 0
340 0 ▶ my $what = $shared ? "the catalogue behind $target->[0][2]" : "$id ($upstream)";
Branch: 1 branch
BranchTrueFalseTotal
$shared ? : 0 0 0
341 0 ▶ my $probe = eval { $engine->probe_model_capabilities_f(
Branch: 1 branch
BranchTrueFalseTotal
$shared ? : 0 0 0
Condition0%0/5
eval { do { $engine->probe_model_capabilities_f("models", $shared ? "all" : [$upstream]) } } // "Future"->fail($@ || "probe did not start")
l!l&&r!l&&!r
000
$@ || 'probe did not start'
l!l
00
MC/DC0%0/2
eval { do { $engine->probe_model_capabilities_f("models", $shared ? "all" : [$upstream]) } } // "Future"->fail($@ || "probe did not start")
eval { do { $engine->probe_model_capabilities_f("models", $shared ? "all" : [$upstream]) } } "Future"->fail($@ || "probe did not start")
MC/DC0%0/1
$@ || 'probe did not start'
$@
Truth table0%0/3
A // B
A= eval { do { $engine->probe_model_capabilities_f("models", $shared ? "all" : [$upstream]) } }B= "Future"->fail($@ || "probe did not start")
ABresult
1X1
011
000
Truth table0%0/2
A
A= $@
Aresult
11
00
342 models => ( $shared ? 'all' : [ $upstream ] ) ) }
343 // Future->fail( $@ || 'probe did not start' );
344 0 ▶ $probe = Future->wait_any( $probe,
Branch: 1 branch
BranchTrueFalseTotal
if $loop and $timeout and $timeout > 0 0 0 0
Condition0%0/6
$loop and $timeout and $timeout > 0
!ll&&!rl&&r
000
$loop and $timeout
!ll&&!rl&&r
000
MC/DC0%0/3
$loop and $timeout and $timeout > 0
$loop $timeout $timeout > 0
Truth table0%0/4
A and B and C
A= $loopB= $timeoutC= $timeout > 0
ABCresult
0XX0
10X0
1100
1111
345 $loop->delay_future( after => $timeout )
346 ->then_fail("capability probe timed out after ${timeout}s") )
347 if $loop && $timeout && $timeout > 0;
348 0 ▶ $probe->then(sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
349 0 my ($learned) = @_;
350 0 ▶ if ($shared) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shared) 0 0 0
351 0 $self->_catalogue_learned->{$id} = $learned;
352 0 $_->import_learned_capabilities($learned) for @rest;
353 $log->debugf( "Capability probe for %s: %d models learned, %d instances taught",
354 0 ▶ $what, scalar keys %{ $learned || {} }, 1 + @rest );
Condition0%0/2
$learned || {}
l!l
00
MC/DC0%0/1
$learned || {}
$learned
Truth table0%0/2
A
A= $learned
Aresult
11
00
355 0 return Future->done;
356 }
357 $log->debugf( "Capability probe for %s: %s", $what,
358 0 join( ', ', map { my $m = $_; map { "$m $_=$learned->{$m}{$_}" } sort keys %{ $learned->{$m} } }
359 0 ▶ sort keys %{ $learned || {} } ) || 'nothing learned' );
Condition0%0/4
join(', ', map({my $m = $_; map {"$m $_=$learned->{$m}{$_}";} sort(keys %{$$learned{$m};});} sort(keys %{$learned or {};}))) || 'nothing learned'
l!l
00
$learned || {}
l!l
00
MC/DC0%0/1
join(', ', map({my $m = $_; map {"$m $_=$learned->{$m}{$_}";} sort(keys %{$$learned{$m};});} sort(keys %{$learned or {};}))) || 'nothing learned'
join(', ', map({my $m = $_; map {"$m $_=$learned->{$m}{$_}";} sort(keys %{$$learned{$m};});} sort(keys %{$learned or {};})))
MC/DC0%0/1
$learned || {}
$learned
Truth table0%0/2
A
A= join(', ', map({my $m = $_; map {"$m $_=$learned->{$m}{$_}";} sort(keys %{$$learned{$m};});} sort(keys %{$learned or {};})))
Aresult
11
00
Truth table0%0/2
A
A= $learned
Aresult
11
00
360 0 Future->done;
361 0 ▶ })->else(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
362 0 my ($err) = @_;
363 0 ( my $text = "$err" ) =~ s/\s+\z//;
364 0 $log->warnf( "Capability probe for %s failed: %s", $what, $text );
365 0 Future->done;
366 0 ▶ })->on_ready(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
367 0 $self->capabilities_generation( $self->capabilities_generation + 1 );
368 0 });
369 } foreach => [ @targets ], concurrent => 4 ) # fmap consumes the array it is given
370 0 ▶ ->then(sub { Future->done($covered) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
371 }
372  
373 # True when the engine's metadata document is a catalogue AND the core can
374 # hand learned facts to another instance (k38). Either missing: per-instance
375 # probing as before (k37).
376 0 ▶sub _shares_catalogue {
Subroutine _shares_catalogue: not called CC=4 SCAR=30.0
377 0 my ($self, $engine) = @_;
378 0 ▶ return 0 unless $engine->can('import_learned_capabilities');
Branch: 1 branch
BranchTrueFalseTotal
unless $engine->can("import_learned_capabilities") 0 0 0
379 0 ▶ return 0 unless eval { require Langertha::ModelProbe; Langertha::ModelProbe->can('is_catalogue') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require Langertha::ModelProbe; "Langertha::ModelProbe"->can("is_catalogue") } } 0 0 0
380 0 ▶ return eval { Langertha::ModelProbe->is_catalogue( $engine->model_metadata_format ) } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { 'Langertha::ModelProbe'->is_catalogue($engine->model_metadata_format) } } ? : 0 0 0
381 }
382  
383 0 ▶sub is_passthrough_model {
Subroutine is_passthrough_model: not called CC=4 SCAR=30.0
Pod documented
384 0 my ($self, $model) = @_;
385 0 ▶ return 0 unless defined $model && length $model;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model and length $model 0 0 0
Condition0%0/3
defined $model and length $model
!ll&&!rl&&r
000
MC/DC0%0/2
defined $model and length $model
defined $model length $model
Truth table0%0/3
A and B
A= defined $modelB= length $model
ABresult
0X0
100
111
386 0 my @r = eval { $self->resolve($model, skip_default => 1) };
387 0 ▶ return @r ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
@r ? : 0 0 0
388 }
389  
390  
391 28 ▶sub discovered_url {
Subroutine discovered_url: called CC=6 SCAR=20.3
Pod documented
392 28 my ($self, $model) = @_;
393 28 ▶ return unless defined $model && length $model;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model and length $model 0 28 28
Condition33%1/3
defined $model and length $model
!ll&&!rl&&r
0028
MC/DC0%0/2
defined $model and length $model
defined $model length $model
Truth table33%1/3
A and B
A= defined $modelB= length $model
ABresult
0X0
100
111
394 28 ▶ return if $self->config->models->{$model};
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->models->{$model} 0 28 28
395 28 ▶ $self->_discover_models unless $self->_discovery_done;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_discovery_done 28 0 28
396 28 ▶ my $def = $self->_discovered_models->{$model} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $def = $self->_discovered_models->{$model} or return 28 0 28
397 28 return $def->{discovered_url};
398 }
399  
400  
401  
402 33 1;
403  
404 __END__
« blib/lib/Langertha/Knarr/CLI/Cmd/Check.pm ↑ Summary blib/lib/Langertha/Knarr/Admin.pm »