blib/lib/Langertha/Knarr/Manifest.pm

filter: Statementstmt 97.1% 102 / 105 Branchbran 77.5% 31 / 40 Conditioncond 58.3% 21 / 36 MC/DCmcdc 27.2% 6 / 22 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 79.8% 170 / 213 CC 38 SCAR 24.0
CC
38
Cov
85%
_model_entries
14.3
build
14.3
_router_capabilities
9.1
CRAP 11.0
« blib/lib/Langertha/Knarr/Router.pm ↑ Summary blib/lib/Langertha/Knarr/Request.pm »
1 package Langertha::Knarr::Manifest;
2 # ABSTRACT: Build Knarr's provider manifest (/.well-known/langertha.json) from its exposed model surface
3 50 our $VERSION = '1.102';
4 50 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 50 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 50 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 50 has knarr => (
10 is => 'ro',
11 required => 1,
12 weak_ref => 1,
13 );
14  
15 50 has _cache => (
16 is => 'rw',
17 );
18  
19 50 my $AVAILABLE;
20  
21 39 ▶sub available {
Subroutine available: called CC=3 SCAR=11.0
Pod documented
22 39 ▶ $AVAILABLE //= eval { require Langertha::Manifest::Builder; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Langertha::Manifest::Builder; 1 } } ? : 3 1 4
Condition66%2/3
$AVAILABLE //= eval {
l!l&&r!l&&!r
3540
MC/DC0%0/2
$AVAILABLE //= eval { do { require Langertha::Manifest::Builder; 1 } } ? 1 : 0
$AVAILABLE eval { do { require Langertha::Manifest::Builder; 1 } } ? 1 : 0
Truth table66%2/3
A //= B
A= $AVAILABLEB= eval { do { require Langertha::Manifest::Builder; 1 } } ? 1 : 0
ABresult
1X1
011
000
23 39 return $AVAILABLE;
24 }
25  
26  
27 36 ▶sub build {
Subroutine build: called CC=14 SCAR=26.6
Pod documented
28 36 my ( $self, $base_url ) = @_;
29 36 $base_url =~ s{/+\z}{};
30 36 my $knarr = $self->knarr;
31  
32 36 my $builder = Langertha::Manifest::Builder->new(
33 provider_id => 'knarr',
34 issuer => _origin_of($base_url),
35 );
36  
37 36 my $auth_ref;
38 36 ▶ if ( defined $knarr->auth_token && length $knarr->auth_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $knarr->auth_token and length $knarr->auth_token) 8 28 36
Condition66%2/3
defined $knarr->auth_token and length $knarr->auth_token
!ll&&!rl&&r
2808
MC/DC50%1/2
defined $knarr->auth_token and length $knarr->auth_token
defined $knarr->auth_token length $knarr->auth_token
Truth table66%2/3
A and B
A= defined $knarr->auth_tokenB= length $knarr->auth_token
ABresult
0X0
100
111
39 8 $auth_ref = 'api';
40 8 $builder->add_auth( id => $auth_ref, type => 'api_key' );
41 }
42  
43 36 my %allowed = map { $_ => 1 } Langertha::Manifest::Builder->model_capabilities;
44 36 my $entries = $self->_model_entries;
45 36 my %seen;
46 36 for my $proto ( @{ $knarr->_protocol_objects } ) {
47 216 ▶ my $spec = $proto->manifest_endpoint or next;
Branch: 1 branch
BranchTrueFalseTotal
my $spec = $proto->manifest_endpoint or next 108 108 216
48 108 my $endpoint_id = $proto->protocol_name;
49 108 ▶ next if $seen{$endpoint_id}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$endpoint_id}++ 0 108 108
50 $builder->add_endpoint(
51 id => $endpoint_id,
52 dialect => $spec->{dialect},
53 108 ▶ base_url => $base_url . ( $spec->{path} // '' ),
Branch: 1 branch
BranchTrueFalseTotal
defined $auth_ref ? : 24 84 108
Condition50%1/2
$spec->{"path"} // ""
l!l
1080
MC/DC0%0/1
$spec->{"path"} // ""
$spec->{"path"}
Truth table50%1/2
A
A= $spec->{"path"}
Aresult
11
00
54 ( defined $auth_ref ? ( auth_ref => $auth_ref ) : () ),
55 );
56 108 ▶ my %forwarded = map { $_ => 1 } grep { $allowed{$_} } @{ $spec->{capabilities} || [] };
Condition50%1/2
$spec->{'capabilities'} || []
l!l
1080
MC/DC0%0/1
$spec->{'capabilities'} || []
$spec->{'capabilities'}
Truth table50%1/2
A
A= $spec->{'capabilities'}
Aresult
11
00
57 108 ▶ my %image_format = map { $_ => 1 } @{ $spec->{image_content_formats} || [] };
Condition50%1/2
$spec->{'image_content_formats'} || []
l!l
1080
MC/DC0%0/1
$spec->{'image_content_formats'} || []
$spec->{'image_content_formats'}
Truth table50%1/2
A
A= $spec->{'image_content_formats'}
Aresult
11
00
58 108 for my $entry (@$entries) {
59 306 my %caps = map { $_ => 1 } grep { $forwarded{$_} } keys %{ $entry->{capabilities} };
60 # Only an engine whose content format reads the protocol's image
61 # parts gets them (k32); with translation (k33) that is every format.
62 delete $caps{image_input}
63 306 ▶ unless defined $entry->{content_format} && $image_format{ $entry->{content_format} };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry->{"content_format"} and $image_format{$entry->{"content_format"}} 69 237 306
Condition66%2/3
defined $entry->{"content_format"} and $image_format{$entry->{"content_format"}}
!ll&&!rl&&r
690237
MC/DC50%1/2
defined $entry->{"content_format"} and $image_format{$entry->{"content_format"}}
defined $entry->{"content_format"} $image_format{$entry->{"content_format"}}
Truth table66%2/3
A and B
A= defined $entry->{"content_format"}B= $image_format{$entry->{"content_format"}}
ABresult
0X0
100
111
64 eval {
65 $builder->add_model(
66 id => $entry->{id},
67 306 endpoint_ref => $endpoint_id,
68 capabilities => \%caps,
69 );
70 306 1;
71 306 ▶ } or $log->debugf( "Manifest: model %s not published: %s", $entry->{id}, $@ );
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $builder->add_model("id", $entry->{"id"}, "endpoint_ref", $endpoint_id, "capabilities", \%caps); 1 } } or $Langertha::Knarr::Manifest::log->debugf("Manifest: model %s not published: %s", $entry->{"id"}, $@) 306 0 306
72 }
73 }
74  
75 36 return $builder->manifest;
76 }
77  
78  
79 # The listed surface, one entry per model id with the capabilities of the
80 # engine that serves it. Cached until the listing changes or a capability
81 # probe finishes.
82 36 ▶sub _model_entries {
Subroutine _model_entries: called CC=14 SCAR=26.6
83 36 my ($self) = @_;
84 36 my $knarr = $self->knarr;
85 36 my $router = $knarr->router;
86 36 ▶ my $listed = $router ? $router->list_models : $knarr->handler->list_models;
Branch: 1 branch
BranchTrueFalseTotal
$router ? : 14 22 36
87 36 ▶ my @rows = map { ref $_ eq 'HASH' ? $_ : { id => "$_" } } @{ $listed || [] };
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 109 0 109
Condition50%1/2
$listed || []
l!l
360
MC/DC0%0/1
$listed || []
$listed
Truth table50%1/2
A
A= $listed
Aresult
11
00
88  
89 36 ▶ my $key = join "\0", map { join "\1", map { $_ // '' } @{$_}{qw( id engine model )} } @rows;
Condition100%2/2
$_ // ""
l!l
28146
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
90 # A capability probe (k37) can change what an engine supports.
91 36 ▶ $key .= "\2" . $router->capabilities_generation
Branch: 1 branch
BranchTrueFalseTotal
if $router and $router->can("capabilities_generation") 14 22 36
Condition66%2/3
$router and $router->can("capabilities_generation")
!ll&&!rl&&r
22014
MC/DC50%1/2
$router and $router->can("capabilities_generation")
$router $router->can("capabilities_generation")
Truth table66%2/3
A and B
A= $routerB= $router->can("capabilities_generation")
ABresult
0X0
100
111
92 if $router && $router->can('capabilities_generation');
93 36 my $cache = $self->_cache;
94 36 ▶ return $cache->{entries} if $cache && $cache->{key} eq $key;
Branch: 1 branch
BranchTrueFalseTotal
if $cache and $cache->{"key"} eq $key 23 13 36
Condition100%3/3
$cache and $cache->{"key"} eq $key
!ll&&!rl&&r
9423
MC/DC100%2/2
$cache and $cache->{"key"} eq $key
$cache $cache->{"key"} eq $key
Truth table100%3/3
A and B
A= $cacheB= $cache->{"key"} eq $key
ABresult
0X0
100
111
95  
96 13 my @entries;
97 13 for my $row (@rows) {
98 53 my $id = $row->{id};
99 53 ▶ next unless defined $id && length $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and length $id 0 53 53
Condition33%1/3
defined $id and length $id
!ll&&!rl&&r
0053
MC/DC0%0/2
defined $id and length $id
defined $id length $id
Truth table33%1/3
A and B
A= defined $idB= length $id
ABresult
0X0
100
111
100 53 ▶ my ( $caps, $content_format ) = $router
Branch: 1 branch
BranchTrueFalseTotal
$router ? : 48 5 53
101 ? _router_capabilities( $router, $id ) : ( { chat => 1 } );
102 53 ▶ next unless $caps;
Branch: 1 branch
BranchTrueFalseTotal
unless $caps 5 48 53
103 48 push @entries, { id => $id, capabilities => $caps, content_format => $content_format };
104 }
105 13 $self->_cache( { key => $key, entries => \@entries } );
106 13 return \@entries;
107 }
108  
109 # Capabilities of the engine serving $id, for the upstream model it sends,
110 # through core's Builder: model-scoped (ADR 0019) and filtered to the public
111 # allowlist. A model the router cannot build an engine for (e.g. its key
112 # variable is unset) is not servable and not published.
113 48 ▶sub _router_capabilities {
Subroutine _router_capabilities: called CC=8 SCAR=22.1
114 48 my ( $router, $id ) = @_;
115 48 my ( $engine, $model, $alias_only ) = eval { $router->resolve( $id, skip_default => 1 ) };
116 48 ▶ unless ($engine) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($engine) 5 43 48
117 5 ▶ $log->debugf( "Manifest: model %s not published: %s", $id, $@ || 'unresolved' );
Condition50%1/2
$@ || 'unresolved'
l!l
50
MC/DC0%0/1
$@ || 'unresolved'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
118 5 return;
119 }
120 # An alias without model: key sends the engine's own default (k22).
121 43 ▶ my $upstream = $alias_only ? eval { $engine->chat_model } : $model;
Branch: 1 branch
BranchTrueFalseTotal
$alias_only ? : 0 43 43
122 43 ▶ $upstream = $id unless defined $upstream && length $upstream;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $upstream and length $upstream 0 43 43
Condition33%1/3
defined $upstream and length $upstream
!ll&&!rl&&r
0043
MC/DC0%0/2
defined $upstream and length $upstream
defined $upstream length $upstream
Truth table33%1/3
A and B
A= defined $upstreamB= length $upstream
ABresult
0X0
100
111
123  
124 43 my $caps = eval {
125 43 my $probe = Langertha::Manifest::Builder->new(
126 provider_id => 'probe',
127 issuer => 'http://probe.invalid',
128 );
129 # base_url, dialect and auth are given so nothing of the engine but its
130 # capabilities is read; the probe manifest is thrown away.
131 43 $probe->add_engine( $engine,
132 endpoint_id => 'probe',
133 base_url => 'http://probe.invalid',
134 dialect => 'probe',
135 auth => 'none',
136 models => [$upstream],
137 );
138 43 my ($entry) = @{ $probe->manifest->models };
139 43 +{ map { $_ => 1 } grep { $entry->supports($_) } keys %{ $entry->capabilities } };
140 };
141 43 ▶ unless ($caps) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($caps) 0 43 43
142 # Not a Langertha chat engine: it answers chat, nothing more is known.
143 0 $log->debugf( "Manifest: no capabilities for %s: %s", $id, $@ );
144 0 $caps = { chat => 1 };
145 }
146 # The message-content shape the engine puts on its wire (openai,
147 # anthropic, gemini, ollama, ...); decides which protocol's image parts
148 # reach it intact.
149 43 ▶ my $content_format = $engine->can('content_format') ? eval { $engine->content_format } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$engine->can('content_format') ? : 43 0 43
150 43 return ( $caps, $content_format );
151 }
152  
153 36 ▶sub _origin_of {
Subroutine _origin_of: called CC=3 SCAR=12.0
154 36 my ($url) = @_;
155 36 my $uri = URI->new($url);
156 36 ▶ return $url unless $uri->can('authority') && defined $uri->authority;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->can("authority") and defined $uri->authority 0 36 36
Condition33%1/3
$uri->can("authority") and defined $uri->authority
!ll&&!rl&&r
0036
MC/DC0%0/2
$uri->can("authority") and defined $uri->authority
$uri->can("authority") defined $uri->authority
Truth table33%1/3
A and B
A= $uri->can("authority")B= defined $uri->authority
ABresult
0X0
100
111
157 36 return $uri->scheme . '://' . $uri->authority;
158 }
159  
160  
161 50 __PACKAGE__->meta->make_immutable;
162 50 1;
163  
164 __END__
165  
166 =pod
167  
168 =encoding UTF-8
169  
170 =head1 NAME
171  
172 Langertha::Knarr::Manifest - Build Knarr's provider manifest (/.well-known/langertha.json) from its exposed model surface
173  
174 =head1 VERSION
175  
176 version 1.102
177  
178 =head1 SYNOPSIS
179  
180 # Used by Langertha::Knarr itself; see Langertha::Knarr/MANIFEST.
181 my $manifest = Langertha::Knarr::Manifest->new( knarr => $knarr );
182 if ( Langertha::Knarr::Manifest->available ) {
183 print $manifest->build('https://knarr.example')->to_json;
184 }
185  
186 =head1 DESCRIPTION
187  
188 Builds the L<Langertha::Manifest> that L<Langertha::Knarr> serves at
189 C</.well-known/langertha.json>, with L<Langertha::Manifest::Builder> from
190 Langertha core.
191  
192 Only what Knarr B<exposes> is published, never its configuration:
193  
194 =over
195  
196 =item * one endpoint per loaded protocol that has a manifest dialect
197 (L<Langertha::Knarr::Protocol/manifest_endpoint>), under the public base
198 URL;
199  
200 =item * one model entry per model id Knarr lists (configured aliases and
201 C<auto_discover> results, as C<GET /v1/models> shows them) on each of those
202 endpoints;
203  
204 =item * per model, the capabilities of the engine that serves it, evaluated
205 for its upstream model by the core Builder (model-scoped, public allowlist
206 only) and narrowed to what the endpoint's protocol actually forwards;
207 C<image_input> only where the engine's C<content_format> reads the
208 protocol's image parts (C<image_content_formats> in
209 L<Langertha::Knarr::Protocol/manifest_endpoint>: every format when Knarr
210 translates images, see L<Langertha::Knarr::Image>);
211  
212 =item * an C<api_key> auth entry when Knarr requires its
213 L<Langertha::Knarr/auth_token>.
214  
215 =back
216  
217 Upstream URLs, API keys, C<api_key_env> names, engine classes, upstream
218 model names and passthrough targets never enter the document. Without a
219 router (a single custom handler), model entries claim only C<chat>.
220  
221 The model part is cached and rebuilt when the listed model surface changes
222 (for example once auto-discovery has run) or a capability probe
223 (L<Langertha::Knarr::Router/probe_capabilities_f>) has finished, so learned
224 facts such as C<image_input> appear without a restart; the envelope around it is built
225 per request, because the base URL can come from the request.
226  
227 =head2 knarr
228  
229 Required (weak). The L<Langertha::Knarr> whose surface is published.
230  
231 =head2 available
232  
233 Langertha::Knarr::Manifest->available;
234  
235 True when the installed Langertha has L<Langertha::Manifest::Builder>.
236 Checked once per process; the manifest is a runtime-optional feature.
237  
238 =head2 build
239  
240 my $manifest = $knarr_manifest->build($public_base_url);
241  
242 Returns a validated L<Langertha::Manifest> for the given public base URL.
243 Croaks when the URL is not a valid manifest URL. Call only when
244 L</available> is true.
245  
246 =head1 SEE ALSO
247  
248 =over
249  
250 =item * L<Langertha::Knarr/MANIFEST>
251  
252 =item * L<Langertha::Manifest>, L<Langertha::Manifest::Builder> (Langertha core)
253  
254 =back
255  
256 =head1 SUPPORT
257  
258 =head2 Issues
259  
260 Please report bugs and feature requests on GitHub at
261 L<https://github.com/Getty/langertha-knarr/issues>.
262  
263 =head2 IRC
264  
265 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
266  
267 =head1 CONTRIBUTING
268  
269 Contributions are welcome! Please fork the repository and submit a pull request.
270  
271 =head1 AUTHOR
272  
273 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
274  
275 =head1 COPYRIGHT AND LICENSE
276  
277 This software is copyright (c) 2026 by Torsten Raudssus.
278  
279 This is free software; you can redistribute it and/or modify it under
280 the same terms as the Perl 5 programming language system itself.
281  
282 =cut
« blib/lib/Langertha/Knarr/Router.pm ↑ Summary blib/lib/Langertha/Knarr/Request.pm »