blib/lib/Langertha/Raider/Provider/Activation.pm

filter: Statementstmt 98.1% 106 / 108 Branchbran 92.3% 48 / 52 Conditioncond 50.0% 8 / 16 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 15 / 15 Podpod 75.0% 6 / 8 totaltotal 88.5% 185 / 209 CC 33 SCAR 25.4
CC
33
Cov
92%
activate_f
21.7
choose_model
11.0
engine_for_dialect
3.0
CRAP 12.7
« blib/lib/Langertha/Raider/Detect.pm ↑ Summary blib/lib/Langertha/Raider/Hall/CLI.pm »
1 package Langertha::Raider::Provider::Activation;
2 # ABSTRACT: Internal activation of a provider manifest's endpoint as the engine of one raider run
3 56 our $VERSION = '0.503';
4 56 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 56 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 56 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 56 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 56 ▶use Langertha::Manifest;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 56 ▶use Langertha::Manifest::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 56 ▶use Langertha::Raider::Provider::Fetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13  
14 56 has fetch => (
15 is => 'ro',
16 isa => 'Langertha::Raider::Provider::Fetch',
17 required => 1,
18 );
19  
20 42 ▶sub manifest_class { 'Langertha::Manifest' }
Subroutine manifest_class: called CC=1 SCAR=0.0
Pod undocumented
21  
22 34 ▶sub builder_class { 'Langertha::Manifest::Builder' }
Subroutine builder_class: called CC=1 SCAR=0.0
Pod undocumented
23  
24 # The adapter: manifest dialect => raider engine name (the .raider.yml
25 # engine / -o name). The Langertha engine class comes from core
26 # (Langertha::Manifest::Builder->engine_class_for_dialect); each class takes
27 # the endpoint's base_url as its url. anthropic-compat is the bare
28 # Anthropic-compatible base: no native structured output, so response_format
29 # goes through the synthetic tool and a forced tool_choice (ADR 0007, update
30 # of 2026-09-25) -- t/69 holds that core still answers AnthropicBase.
31 56 my %ENGINE_NAME_FOR_DIALECT = (
32 'openai-chat' => 'openai',
33 'responses' => 'responses',
34 'perplexity-agent' => 'perplexity-agent',
35 'anthropic' => 'anthropic',
36 'anthropic-compat' => 'anthropic-compat',
37 'gemini' => 'gemini',
38 'ollama' => 'ollama',
39 'aki' => 'aki'
40 );
41  
42 # Dialects Langertha knows but raider cannot run on, and why.
43 56 my %UNSUPPORTED_DIALECT = (
44 'lmstudio' => "Langertha's native LM Studio engine has no tool calling, which raider needs",
45 );
46  
47  
48 35 ▶sub engine_for_dialect {
Subroutine engine_for_dialect: called CC=3 SCAR=11.0
Pod documented
49 35 my ( $self, $dialect ) = @_;
50 35 ▶ my $name = $ENGINE_NAME_FOR_DIALECT{$dialect} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $name = $ENGINE_NAME_FOR_DIALECT{$dialect} or return 34 1 35
51 34 ▶ my $class = $self->builder_class->engine_class_for_dialect($dialect) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $class = $self->builder_class->engine_class_for_dialect($dialect) or return 34 0 34
52 34 return ( $name, $class );
53 }
54  
55 3 ▶sub mapped_dialects {
Subroutine mapped_dialects: called CC=1 SCAR=0.0
Pod documented
56 3 my ( $self ) = @_;
57 3 return sort keys %ENGINE_NAME_FOR_DIALECT;
58 }
59  
60 33 ▶sub unsupported_dialect {
Subroutine unsupported_dialect: called CC=1 SCAR=0.0
Pod documented
61 33 my ( $self, $dialect ) = @_;
62 33 return $UNSUPPORTED_DIALECT{$dialect};
63 }
64  
65 1 ▶sub unsupported_dialects {
Subroutine unsupported_dialects: called CC=1 SCAR=0.0
Pod documented
66 1 my ( $self ) = @_;
67 1 return sort keys %UNSUPPORTED_DIALECT;
68 }
69  
70  
71 47 ▶async sub activate_f {
Subroutine activate_f: called CC=21 SCAR=30.8
Pod documented
72 47 my ( $self, $target, %opt ) = @_;
73 47 my $fetch = $self->fetch;
74 47 my $url = eval { $fetch->target_url($target) };
75 47 ▶ unless (defined $url) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $url) 2 45 47
76 2 ( my $error = $@ ) =~ s/ at \S+ line \d+\.?\n?\z//s;
77 2 return { status => 'usage', error => $error };
78 }
79 45 my $report = await $fetch->fetch_f($url);
80 return { status => $report->{status}, error => $report->{error} }
81 45 ▶ unless $report->{status} eq 'completed';
Branch: 1 branch
BranchTrueFalseTotal
unless $$report{"status"} eq "completed" 3 42 45
82 42 my $manifest = eval { $self->manifest_class->from_json($report->{body}) };
83 42 ▶ unless ($manifest) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($manifest) 0 42 42
84 0 ( my $error = $@ ) =~ s/ at \S+ line \d+\.?\n?\z//s;
85 0 return { status => 'failed', error => 'not a valid provider manifest: '.$error };
86 }
87 42 ▶ my $origin = $fetch->origin($report->{final_url} // $url);
Condition33%1/3
$$report{"final_url"} // $url
l!l&&r!l&&!r
4200
MC/DC0%0/2
$$report{"final_url"} // $url
$$report{"final_url"} $url
Truth table33%1/3
A // B
A= $$report{"final_url"}B= $url
ABresult
1X1
011
000
88  
89 42 my ( $model, $refusal ) = $self->choose_model($manifest, $opt{model});
90 42 ▶ return $refusal if $refusal;
Branch: 1 branch
BranchTrueFalseTotal
if $refusal 7 35 42
91 35 my $endpoint = $manifest->endpoint($model->endpoint_ref);
92 35 my $what = "endpoint '".$endpoint->id."'";
93 35 my $dialect = $endpoint->dialect;
94  
95 35 ▶ return { status => 'failed', error => $what.": dialect '".$dialect."' is unknown to this raider (no adapter for it)" }
Branch: 1 branch
BranchTrueFalseTotal
unless $endpoint->is_known_dialect 3 32 35
96 unless $endpoint->is_known_dialect;
97 32 ▶ if ( my $why = $self->unsupported_dialect($dialect) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $why = $self->unsupported_dialect($dialect)) 1 31 32
98 1 return { status => 'failed', error => $what.": dialect '".$dialect."' is not supported: ".$why };
99 }
100 31 my ( $engine_name, $engine_class ) = $self->engine_for_dialect($dialect);
101 31 ▶ return { status => 'failed', error => $what.": dialect '".$dialect."' has no engine in this raider" }
Branch: 1 branch
BranchTrueFalseTotal
unless $engine_class 0 31 31
102 unless $engine_class;
103  
104 31 my $base = URI->new($endpoint->base_url);
105 31 ▶ return { status => 'refused', error => $what.': base_url '.$endpoint->base_url.' is not https' }
Branch: 1 branch
BranchTrueFalseTotal
unless lc($base->scheme // "") eq "https" 1 30 31
Condition50%1/2
$base->scheme // ""
l!l
310
MC/DC0%0/1
$base->scheme // ""
$base->scheme
Truth table50%1/2
A
A= $base->scheme
Aresult
11
00
106 unless lc( $base->scheme // '' ) eq 'https';
107 30 ▶ my $endpoint_origin = $fetch->origin($base) // '';
Condition50%1/2
$fetch->origin($base) // ""
l!l
300
MC/DC0%0/1
$fetch->origin($base) // ""
$fetch->origin($base)
Truth table50%1/2
A
A= $fetch->origin($base)
Aresult
11
00
108 30 ▶ return { status => 'refused', error => $what.': base_url '.$endpoint->base_url
Branch: 1 branch
BranchTrueFalseTotal
unless $endpoint_origin eq $origin 2 28 30
109 .' is not of the origin the manifest came from ('.$origin.'); no credential goes to another origin' }
110 unless $endpoint_origin eq $origin;
111  
112 28 my $auth;
113 28 ▶ if ( defined $endpoint->auth_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $endpoint->auth_ref) 26 2 28
114 26 $auth = $manifest->auth_entry($endpoint->auth_ref);
115 26 ▶ return { status => 'failed', error => $what.": auth '".$auth->id."' has type '".$auth->type
Branch: 1 branch
BranchTrueFalseTotal
unless $auth->is_known_type 1 25 26
116 ."', which this raider cannot supply" }
117 unless $auth->is_known_type;
118 return { status => 'usage', error => $what." needs an API key (auth '".$auth->id."', type "
119 .$auth->type.'); pass it with -k KEY' }
120 25 ▶ unless $opt{has_api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"has_api_key"} 2 23 25
121 }
122  
123 25 my $checked = await $fetch->check_host_f($base->host);
124 25 ▶ return { status => $checked->{status}, error => $what.': '.$checked->{error} } if $checked->{status};
Branch: 1 branch
BranchTrueFalseTotal
if $$checked{"status"} 2 23 25
125 # The engine connects to this address, not to the name (k141). Every
126 # address passed the policy above; the first is the one the system
127 # prefers, and the one the manifest fetch tries first.
128 23 my ( $connect_address ) = @{ $checked->{addresses} };
129 23 ▶ return { status => 'failed', error => $what.': '.$base->host.' resolves first to '.$connect_address
Branch: 1 branch
BranchTrueFalseTotal
if $connect_address =~ /%/ 1 22 23
130 .', a scoped address the engine connection cannot be pinned to' }
131 if $connect_address =~ /%/;
132  
133 22 my @warnings;
134 22 ▶ push @warnings, "model '".$model->id."' does not declare tools_native; raider works through tool calls"
Branch: 1 branch
BranchTrueFalseTotal
unless $model->supports("tools_native") 2 20 22
135 unless $model->supports('tools_native');
136  
137 return {
138 status => 'completed',
139 provider_id => $manifest->provider_id,
140 manifest_url => $report->{final_url} // $url,
141 endpoint => $endpoint->id,
142 dialect => $dialect,
143 engine_name => $engine_name,
144 engine_class => $engine_class,
145 url => $endpoint->base_url,
146 model => $model->id,
147 auth => $auth ? $auth->id : undef,
148 addresses => $checked->{addresses},
149 22 ▶ connect_address => $connect_address,
Branch: 1 branch
BranchTrueFalseTotal
$auth ? : 20 2 22
Condition33%1/3
$$report{"final_url"} // $url
l!l&&r!l&&!r
2200
MC/DC0%0/2
$$report{"final_url"} // $url
$$report{"final_url"} $url
Truth table33%1/3
A // B
A= $$report{"final_url"}B= $url
ABresult
1X1
011
000
150 warnings => \@warnings,
151 };
152 56 }
153  
154  
155 42 ▶sub choose_model {
Subroutine choose_model: called CC=11 SCAR=24.0
Pod documented
156 42 my ( $self, $manifest, $requested ) = @_;
157 42 my @models = @{ $manifest->models };
158 42 my %seen;
159 42 my @ids = grep { !$seen{$_}++ } map { $_->id } @models;
160 42 ▶ my $listed = @ids ? ' (models: '.join(', ', @ids).')' : ' (it lists none)';
Branch: 1 branch
BranchTrueFalseTotal
@ids ? : 40 2 42
161 42 my $id;
162 42 ▶ if ( defined $requested && length $requested ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $requested and length $requested) { } 9 33 42
Condition66%2/3
defined $requested and length $requested
!ll&&!rl&&r
3309
MC/DC50%1/2
defined $requested and length $requested
defined $requested length $requested
Truth table66%2/3
A and B
A= defined $requestedB= length $requested
ABresult
0X0
100
111
163 return ( undef, { status => 'usage', error => "model '".$requested."' is not in the manifest of "
164 .$manifest->provider_id.$listed } )
165 9 ▶ unless $seen{$requested};
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$requested} 3 6 9
166 6 $id = $requested;
167 }
168 else {
169 33 ▶ return ( undef, { status => 'failed', error => 'the manifest of '.$manifest->provider_id.' lists no models' } )
Branch: 1 branch
BranchTrueFalseTotal
unless @ids 1 32 33
170 unless @ids;
171 32 ▶ return ( undef, { status => 'usage', error => 'the manifest of '.$manifest->provider_id
Branch: 1 branch
BranchTrueFalseTotal
if @ids > 1 2 30 32
172 .' lists several models; choose one with -m MODEL'.$listed } )
173 if @ids > 1;
174 30 ( $id ) = @ids;
175 }
176 36 my @candidates = grep { $_->id eq $id } @models;
177 36 ▶ return ( $candidates[0] ) if @candidates == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @candidates == 1 34 2 36
178 my @usable = grep {
179 2 my $endpoint = $manifest->endpoint($_->endpoint_ref);
180 4 ▶ $endpoint->is_known_dialect && $self->engine_for_dialect($endpoint->dialect);
Condition66%2/3
$endpoint->is_known_dialect && $self->engine_for_dialect($endpoint->dialect)
!ll&&!rl&&r
103
MC/DC50%1/2
$endpoint->is_known_dialect && $self->engine_for_dialect($endpoint->dialect)
$endpoint->is_known_dialect $self->engine_for_dialect($endpoint->dialect)
Truth table66%2/3
A && B
A= $endpoint->is_known_dialectB= $self->engine_for_dialect($endpoint->dialect)
ABresult
0X0
100
111
181 } @candidates;
182 2 ▶ return ( $usable[0] ) if @usable == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @usable == 1 1 1 2
183 1 ▶ return ( $candidates[0] ) unless @usable;
Branch: 1 branch
BranchTrueFalseTotal
unless @usable 0 1 1
184 return ( undef, { status => 'failed', error => "model '".$id."' is offered on several endpoints ("
185 1 .join(', ', map { $_->endpoint_ref.': '.$manifest->endpoint($_->endpoint_ref)->dialect } @usable)
186 .'); raider does not choose between them' } );
187 }
188  
189 56 __PACKAGE__->meta->make_immutable;
190  
191 56 1;
192  
193 __END__
194  
195 =pod
196  
197 =encoding UTF-8
198  
199 =head1 NAME
200  
201 Langertha::Raider::Provider::Activation - Internal activation of a provider manifest's endpoint as the engine of one raider run
202  
203 =head1 VERSION
204  
205 version 0.503
206  
207 =head1 SYNOPSIS
208  
209 # Internal to Langertha-Raider -- no API promise.
210 my $activation = Langertha::Raider::Provider::Activation->new(
211 fetch => Langertha::Raider::Provider::Fetch->new( allow_internal => 0 ),
212 );
213 my $got = $activation->activate_f('provider.example',
214 model => 'example-model', has_api_key => 1)->get;
215 # { status => 'completed', engine_name => 'openai',
216 # engine_class => 'Langertha::Engine::OpenAI',
217 # url => 'https://provider.example/v1', model => 'example-model', ... }
218 # { status => 'usage' | 'refused' | 'failed', error => ... }
219  
220 =head1 DESCRIPTION
221  
222 B<Internal module.> Its interface may change without notice.
223  
224 C<raider --provider>: turns what a provider manifest (ADR 0007) claims
225 into the engine of one run. Nothing is stored and nothing is trusted
226 beyond this run: the release is the target named on the command line,
227 as with C<-o url=>.
228  
229 =over
230  
231 =item 1. The manifest is fetched with L<Langertha::Raider::Provider::Fetch>
232 and validated with L<Langertha::Manifest>.
233  
234 =item 2. B<The model.> The requested model must be a model id of the
235 manifest. Without one, the manifest must list exactly one model id.
236  
237 =item 3. B<The endpoint> is the one the model is listed on. A model listed
238 on several endpoints takes the one whose dialect raider has an adapter
239 for; when that is more than one, raider does not choose.
240  
241 =item 4. B<The dialect> maps to a L<Langertha> engine class
242 (L</engine_for_dialect>). An unknown dialect, or one raider cannot use
243 (L</unsupported_dialect>), is an error -- never a guess.
244  
245 =item 5. B<The origin.> The endpoint's C<base_url> must be C<https> and of
246 the same origin as the manifest, so a credential never goes to an origin
247 the command line did not name. Its host is resolved and checked again,
248 under the same address policy as the fetch (C<--allow-internal> releases
249 both): every address it resolves to must pass, as for the manifest.
250  
251 =item 6. B<The connection.> The engine connects to the first of those
252 checked addresses (C<connect_address>, Langertha ADR 0037), never to the
253 name resolved once more, so an answer that changes after the check (DNS
254 rebinding) cannot send the request and its credential elsewhere. An
255 address literal stands for itself. TLS still verifies the certificate
256 against the host name. Unlike the manifest fetch, the engine has no second
257 address to fall back to; an address with a scope (C<fe80::1%eth0>) cannot
258 be pinned and is an error.
259  
260 =item 7. B<The credential> is the caller's: an endpoint with an
261 C<auth_ref> needs one (C<has_api_key>), of an auth type raider knows.
262  
263 =back
264  
265 A model that does not declare C<tools_native> is a warning, not an error.
266  
267 =head2 fetch
268  
269 The L<Langertha::Raider::Provider::Fetch> the manifest comes through; its
270 C<allow_internal> also releases the endpoint's host. Required.
271  
272 =head2 engine_for_dialect
273  
274 my ( $name, $class ) = $activation->engine_for_dialect('openai-chat');
275 # ( 'openai', 'Langertha::Engine::OpenAI' )
276  
277 The raider engine name and the L<Langertha> engine class of a manifest
278 dialect; the empty list for a dialect raider has no adapter for. Also
279 callable on the class.
280  
281 =head2 mapped_dialects
282  
283 The dialects L</engine_for_dialect> maps, sorted. Also callable on the
284 class.
285  
286 =head2 unsupported_dialect
287  
288 my $why = $activation->unsupported_dialect('lmstudio');
289  
290 Why raider cannot use a dialect Langertha knows, or C<undef>. Also callable
291 on the class.
292  
293 =head2 unsupported_dialects
294  
295 The dialects L</unsupported_dialect> names, sorted. Also callable on the
296 class.
297  
298 =head2 activate_f
299  
300 my $got = await $activation->activate_f($target,
301 model => $model_or_undef, has_api_key => $bool);
302  
303 Resolves to a hash. C<status> is C<completed>, C<usage> (the command line
304 names a target that is none, a model the manifest does not list, no model
305 where it lists several, or no key where the endpoint needs one),
306 C<refused> (the fetch policy, an endpoint that is not C<https> or not of
307 the manifest's origin, or an endpoint address the policy refuses) or
308 C<failed> (the fetch, an invalid manifest, a dialect or auth type raider
309 cannot use, a manifest without models). Apart from C<completed>, C<error>
310 says why.
311  
312 A C<completed> activation carries C<provider_id>, C<manifest_url>,
313 C<endpoint> (its id), C<dialect>, C<engine_name>, C<engine_class>, C<url>
314 (the endpoint's C<base_url>), C<model>, C<auth> (the auth id, or C<undef>),
315 C<addresses> (of the endpoint host, all checked), C<connect_address> (the
316 one of them the engine connects to) and C<warnings>. It never carries a
317 credential.
318  
319 =head2 choose_model
320  
321 my ( $model, $refusal ) = $activation->choose_model($manifest, $requested);
322  
323 The L<Langertha::Manifest::Model> entry the run uses (see L</DESCRIPTION>),
324 or C<undef> and the C<usage> or C<failed> result that says why not.
325  
326 =head1 SEE ALSO
327  
328 =over
329  
330 =item * L<Langertha::Raider::Provider::Fetch>
331  
332 =item * L<Langertha::Raider::EngineResolver> -- builds the engine from the activation
333  
334 =item * L<Langertha::Manifest::Builder> -- core's dialect => engine class table this one asks
335  
336 =back
337  
338 =head1 SUPPORT
339  
340 =head2 Issues
341  
342 Please report bugs and feature requests on GitHub at
343 L<https://github.com/Getty/langertha-raider/issues>.
344  
345 =head2 IRC
346  
347 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
348  
349 =head1 CONTRIBUTING
350  
351 Contributions are welcome! Please fork the repository and submit a pull request.
352  
353 =head1 AUTHOR
354  
355 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
356  
357 =head1 COPYRIGHT AND LICENSE
358  
359 This software is copyright (c) 2026 by Torsten Raudssus.
360  
361 This is free software; you can redistribute it and/or modify it under
362 the same terms as the Perl 5 programming language system itself.
363  
364 =cut
« blib/lib/Langertha/Raider/Detect.pm ↑ Summary blib/lib/Langertha/Raider/Hall/CLI.pm »