blib/lib/Langertha/Manifest.pm

filter: Statementstmt 100% 136 / 136 Branchbran 93.4% 43 / 46 Conditioncond 76.4% 13 / 17 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 26 / 26 Podpod 83.3% 10 / 12 totaltotal 94.7% 235 / 248 CC 35 SCAR 20.5
CC
35
Cov
96%
_from_hash
16.0
BUILD
12.0
from_hash
5.3
CRAP 7.8
« blib/lib/Langertha/Engine/Perplexity.pm ↑ Summary blib/lib/Langertha/ModelProbe.pm »
1 package Langertha::Manifest;
2 # ABSTRACT: Provider manifest (/.well-known/langertha.json) value object, parser and validator
3 8 our $VERSION = '0.503';
4 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Langertha::Manifest::Endpoint;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Langertha::Manifest::Auth;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Langertha::Manifest::Model;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 with 'Langertha::Manifest::Validation';
12  
13  
14 8 has provider_id => ( is => 'ro', isa => 'Str', required => 1 );
15 8 has issuer => ( is => 'ro', isa => 'Str', required => 1 );
16  
17 8 has endpoints => (
18 is => 'ro',
19 isa => 'ArrayRef[Langertha::Manifest::Endpoint]',
20 required => 1,
21 );
22  
23 has auth => (
24 is => 'ro',
25 isa => 'ArrayRef[Langertha::Manifest::Auth]',
26 5 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 8 );
28  
29 has models => (
30 is => 'ro',
31 isa => 'ArrayRef[Langertha::Manifest::Model]',
32 3 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 8 );
34  
35 # Stored as a private deep copy (plain JSON data only) and handed out as a
36 # fresh copy, so neither the caller's input nor a returned structure can
37 # mutate the immutable manifest.
38 has _extensions => (
39 is => 'ro',
40 isa => 'HashRef',
41 init_arg => 'extensions',
42 5 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 8 );
44  
45 78 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
46 78 my ( $orig, $class, @args ) = @_;
47 78 my $args = $class->$orig(@args);
48 78 ▶ if ( exists $args->{extensions} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$args{"extensions"}) 73 5 78
49 73 ▶ $class->_error('extensions: must be a JSON object') unless ref $args->{extensions} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$args{"extensions"} eq "HASH" 0 73 73
50 73 $args->{extensions} = $class->_json_clone( 'extensions', $args->{extensions} );
51 }
52 76 return $args;
53 8 };
54  
55 33 ▶sub extensions {
Subroutine extensions: called CC=1 SCAR=0.0
Pod documented
56 33 my ($self) = @_;
57 33 return $self->_json_clone( 'extensions', $self->_extensions );
58 }
59  
60  
61 8 ▶use constant SCHEMA_VERSION => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 8 ▶use constant KIND => 'langertha-provider';
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 1 ▶sub schema_version { return SCHEMA_VERSION }
Subroutine schema_version: called CC=1 SCAR=0.0
Pod documented
65 1 ▶sub kind { return KIND }
Subroutine kind: called CC=1 SCAR=0.0
Pod documented
66  
67  
68 76 ▶sub BUILD {
Subroutine BUILD: called CC=12 SCAR=24.8
Pod undocumented
69 76 my ($self) = @_;
70 76 ▶ $self->_error( q{provider_id must match [a-z0-9][a-z0-9._-]* (max 128), got '}
Branch: 1 branch
BranchTrueFalseTotal
unless $self->provider_id =~ /\A[a-z0-9][a-z0-9._-]{0,127}\z/ 2 74 76
71 . $self->_display( $self->provider_id ) . q{'} )
72 unless $self->provider_id =~ /\A[a-z0-9][a-z0-9._-]{0,127}\z/;
73 74 $self->_check_url( 'issuer', $self->issuer );
74 71 ▶ $self->_error('at least one endpoint is required') unless @{ $self->endpoints };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->endpoints;} 2 69 71
75  
76 69 my ( %auth, %endpoint, %model );
77 69 for my $entry ( @{ $self->auth } ) {
78 56 ▶ $self->_error( "duplicate auth id '" . $entry->id . q{'} ) if $auth{ $entry->id }++;
Branch: 1 branch
BranchTrueFalseTotal
if $auth{$entry->id}++ 1 55 56
79 }
80 68 for my $entry ( @{ $self->endpoints } ) {
81 81 ▶ $self->_error( "duplicate endpoint id '" . $entry->id . q{'} ) if $endpoint{ $entry->id }++;
Branch: 1 branch
BranchTrueFalseTotal
if $endpoint{$entry->id}++ 1 80 81
82 $self->_error( "endpoint '" . $entry->id . "': auth_ref '" . $entry->auth_ref
83 . q{' names no auth entry} )
84 80 ▶ if defined $entry->auth_ref && !$auth{ $entry->auth_ref };
Branch: 1 branch
BranchTrueFalseTotal
if defined $entry->auth_ref and not $auth{$entry->auth_ref} 1 79 80
Condition100%3/3
defined $entry->auth_ref and not $auth{$entry->auth_ref}
!ll&&!rl&&r
25541
MC/DC100%2/2
defined $entry->auth_ref and not $auth{$entry->auth_ref}
defined $entry->auth_ref not $auth{$entry->auth_ref}
Truth table100%3/3
A and not(B)
A= defined $entry->auth_refB= not $auth{$entry->auth_ref}
ABresult
0X0
100
111
85 }
86 66 for my $entry ( @{ $self->models } ) {
87 93 my $shown = $self->_display( $entry->id );
88 $self->_error( "model '$shown': endpoint_ref '" . $entry->endpoint_ref
89 . q{' names no endpoint} )
90 93 ▶ unless $endpoint{ $entry->endpoint_ref };
Branch: 1 branch
BranchTrueFalseTotal
unless $endpoint{$entry->endpoint_ref} 1 92 93
91 $self->_error( "duplicate model '$shown' on endpoint '" . $entry->endpoint_ref . q{'} )
92 92 ▶ if $model{ $entry->endpoint_ref }{ $entry->id }++;
Branch: 1 branch
BranchTrueFalseTotal
if $model{$entry->endpoint_ref}{$entry->id}++ 1 91 92
93 }
94 64 return;
95 }
96  
97 8 my $JSON = JSON::MaybeXS->new( utf8 => 1, canonical => 1 );
98  
99 10 ▶sub from_json {
Subroutine from_json: called CC=3 SCAR=11.0
Pod documented
100 10 my ( $class, $text ) = @_;
101 10 my $data = eval { $JSON->decode($text) };
102 10 ▶ unless ( defined $data || !$@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data or not $@) 1 9 10
Condition66%2/3
defined $data or not $@
l!l&&r!l&&!r
901
MC/DC50%1/2
defined $data or not $@
defined $data not $@
Truth table66%2/3
A or not(B)
A= defined $dataB= not $@
ABresult
1X1
011
000
103 1 ( my $err = $@ ) =~ s/\s+at \S+ line \d+\.?\s*\z//s;
104 1 croak "Langertha::Manifest: invalid JSON: $err";
105 }
106 9 return $class->from_hash($data);
107 }
108  
109  
110 107 ▶sub from_hash {
Subroutine from_hash: called CC=5 SCAR=16.6
Pod documented
111 107 my ( $class, $data ) = @_;
112 107 my $manifest = eval { $class->_from_hash($data) };
113 107 ▶ return $manifest if $manifest;
Branch: 1 branch
BranchTrueFalseTotal
if $manifest 30 77 107
114 77 my $error = $@;
115 77 ▶ my $message = blessed($error) && $error->can('message') ? $error->message : "$error";
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($error) && $error->can('message') ? : 0 77 77
Condition33%1/3
builtin::blessed($error) and $error->can("message")
!ll&&!rl&&r
7700
MC/DC0%0/2
builtin::blessed($error) and $error->can("message")
builtin::blessed($error) $error->can("message")
Truth table33%1/3
A and B
A= builtin::blessed($error)B= $error->can("message")
ABresult
0X0
100
111
116 77 $message =~ s/\s+\z//;
117 77 ▶ $message = "Langertha::Manifest: $message" unless $message =~ /\ALangertha::Manifest: /;
Branch: 1 branch
BranchTrueFalseTotal
unless $message =~ /\ALangertha::Manifest: / 0 77 77
118 77 croak $message;
119 }
120  
121 107 ▶sub _from_hash {
Subroutine _from_hash: called CC=16 SCAR=27.7
122 107 my ( $class, $data ) = @_;
123 # The version is checked before anything else: a document of another major
124 # version may legitimately carry fields v1 does not know, and the useful
125 # error then is "unsupported version", not "unknown field".
126 107 ▶ $class->_error('must be a JSON object') unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 2 105 107
127 105 my $version = $data->{schema_version};
128 105 ▶ $class->_error(q{field 'schema_version' is required}) unless defined $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 1 104 105
129 # A JSON number with a whole value (1, 1.0, 1e0 -- the same verdict on every
130 # JSON backend), never a string ("1"), checked before anything numifies it.
131 104 ▶ $class->_error('schema_version: must be an integer (a JSON number, not a string)')
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_is_whole_number($version) 10 94 104
132 unless $class->_is_whole_number($version);
133 94 ▶ $class->_error( "unsupported schema_version $version (this Langertha reads "
Branch: 1 branch
BranchTrueFalseTotal
unless $version == 1 4 90 94
134 . SCHEMA_VERSION . ')' )
135 unless $version == SCHEMA_VERSION;
136 90 $class->_check_fields( $data,
137 required => [qw( schema_version kind provider_id issuer endpoints )],
138 optional => [qw( auth models extensions )],
139 );
140 $class->_error( q{kind: must be '} . KIND . q{'} )
141 80 ▶ unless !ref $data->{kind} && $data->{kind} eq KIND;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $$data{"kind"} and $$data{"kind"} eq "langertha-provider" 1 79 80
Condition66%2/3
not ref $$data{"kind"} and $$data{"kind"} eq "langertha-provider"
!ll&&!rl&&r
0179
MC/DC50%1/2
not ref $$data{"kind"} and $$data{"kind"} eq "langertha-provider"
not ref $$data{"kind"} $$data{"kind"} eq "langertha-provider"
Truth table66%2/3
not(A) and B
A= not ref $$data{"kind"}B= $$data{"kind"} eq "langertha-provider"
ABresult
0X0
100
111
142 $class->_error('extensions: must be a JSON object')
143 79 ▶ if exists $data->{extensions} && ref $data->{extensions} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if exists $$data{"extensions"} and ref $$data{"extensions"} ne "HASH" 1 78 79
Condition100%3/3
exists $$data{"extensions"} and ref $$data{"extensions"} ne "HASH"
!ll&&!rl&&r
3751
MC/DC100%2/2
exists $$data{"extensions"} and ref $$data{"extensions"} ne "HASH"
exists $$data{"extensions"} ref $$data{"extensions"} ne "HASH"
Truth table100%3/3
A and B
A= exists $$data{"extensions"}B= ref $$data{"extensions"} ne "HASH"
ABresult
0X0
100
111
144  
145 78 my %parsed;
146 78 for my $section (
147 [ endpoints => 'Langertha::Manifest::Endpoint' ],
148 [ auth => 'Langertha::Manifest::Auth' ],
149 [ models => 'Langertha::Manifest::Model' ],
150 ) {
151 202 my ( $name, $entry_class ) = @$section;
152 202 ▶ next unless exists $data->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$data{$name} 4 198 202
153 198 ▶ $class->_error("$name: must be an array") unless ref $data->{$name} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$data{$name} eq "ARRAY" 1 197 198
154 197 my @entries = @{ $data->{$name} };
155 197 for my $i ( 0 .. $#entries ) {
156 226 my $entry = eval { $entry_class->from_hash( $entries[$i] ) };
157 226 ▶ $class->_rethrow( "${name}[$i]", $@ ) unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 33 193 226
158 193 push @{ $parsed{$name} }, $entry;
159 }
160 164 ▶ $parsed{$name} //= [];
Condition100%2/2
$parsed{$name} //= []
l!l
1622
MC/DC100%1/1
$parsed{$name} //= []
$parsed{$name}
Truth table100%2/2
A
A= $parsed{$name}
Aresult
11
00
161 }
162  
163 return $class->new(
164 provider_id => $class->_string( 'provider_id', $data->{provider_id} ),
165 issuer => $class->_string( 'issuer', $data->{issuer} ),
166 %parsed,
167 44 ▶ ( exists $data->{extensions} ? ( extensions => $data->{extensions} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
exists $$data{'extensions'} ? : 40 3 43
168 );
169 }
170  
171  
172 19 ▶sub endpoint {
Subroutine endpoint: called CC=1 SCAR=0.0
Pod documented
173 19 my ( $self, $id ) = @_;
174 19 my ($found) = grep { $_->id eq $id } @{ $self->endpoints };
175 19 return $found;
176 }
177  
178  
179 5 ▶sub auth_entry {
Subroutine auth_entry: called CC=1 SCAR=0.0
Pod documented
180 5 my ( $self, $id ) = @_;
181 5 my ($found) = grep { $_->id eq $id } @{ $self->auth };
182 5 return $found;
183 }
184  
185  
186 1 ▶sub models_for_endpoint {
Subroutine models_for_endpoint: called CC=1 SCAR=0.0
Pod documented
187 1 my ( $self, $id ) = @_;
188 1 return grep { $_->endpoint_ref eq $id } @{ $self->models };
189 }
190  
191  
192 29 ▶sub to_hash {
Subroutine to_hash: called CC=1 SCAR=0.0
Pod documented
193 29 my ($self) = @_;
194 return {
195 # + 0: a fresh number, never the shared constant (constant folding may
196 # have cached a string on it, and a JSON encoder would then emit "1").
197 schema_version => SCHEMA_VERSION + 0,
198 kind => KIND,
199 provider_id => $self->provider_id,
200 issuer => $self->issuer,
201 47 endpoints => [ map { $_->to_hash } @{ $self->endpoints } ],
202 21 auth => [ map { $_->to_hash } @{ $self->auth } ],
203 29 models => [ map { $_->to_hash } @{ $self->models } ],
204 extensions => $self->extensions,
205 };
206 }
207  
208  
209 16 ▶sub to_json {
Subroutine to_json: called CC=1 SCAR=0.0
Pod documented
210 16 my ($self) = @_;
211 16 return $JSON->encode( $self->to_hash );
212 }
213  
214  
215 1 ▶sub TO_JSON { shift->to_hash }
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod undocumented
216  
217 8 __PACKAGE__->meta->make_immutable;
218  
219  
220 8 1;
221  
222 __END__
223  
224 =pod
225  
226 =encoding UTF-8
227  
228 =head1 NAME
229  
230 Langertha::Manifest - Provider manifest (/.well-known/langertha.json) value object, parser and validator
231  
232 =head1 VERSION
233  
234 version 0.503
235  
236 =head1 SYNOPSIS
237  
238 use Langertha::Manifest;
239  
240 # Parse (a JSON string or an already-decoded hashref) -- validated or croaks
241 my $manifest = Langertha::Manifest->from_json($json_text);
242 my $manifest = Langertha::Manifest->from_hash(\%data);
243  
244 for my $model ( @{ $manifest->models } ) {
245 my $endpoint = $manifest->endpoint( $model->endpoint_ref );
246 next unless $endpoint->is_known_dialect;
247 printf "%s via %s at %s (tools: %s)\n", $model->id, $endpoint->dialect,
248 $endpoint->base_url, $model->supports('tools_native') ? 'yes' : 'no';
249 }
250  
251 # Serialize
252 my $hashref = $manifest->to_hash;
253 my $json = $manifest->to_json; # canonical, byte-stable
254  
255 # Build one from a configured engine
256 use Langertha::Manifest::Builder;
257 my $manifest = Langertha::Manifest::Builder->from_engine($engine);
258  
259 =head1 DESCRIPTION
260  
261 The data model of the provider manifest served at
262 C</.well-known/langertha.json>: a declarative description of what a provider
263 exposes — endpoints with their wire dialect, auth mechanisms, model ids and
264 declared capabilities. Langertha core owns the schema, these value objects,
265 the parser/validator and the L<Langertha::Manifest::Builder>; fetching,
266 trust, aliases and secret binding belong to the client (langertha-raider),
267 publishing to the servers (langertha-knarr, langertha-skeid). Core does no
268 network I/O here.
269  
270 Schema version 1 carries exactly: C<schema_version> (C<1>), C<kind>
271 (C<langertha-provider>), C<provider_id>, C<issuer>, C<endpoints>, C<auth>,
272 C<models> and C<extensions>. Validation is strict:
273  
274 =over
275  
276 =item * a field outside the schema is rejected — at the top level and in
277 every endpoint, auth and model entry;
278  
279 =item * a command-, code-, secret- or prompt-shaped field (C<command>,
280 C<exec>, C<engine_class>, C<api_key>, C<secret_path>, C<env>,
281 C<system_prompt>, C<mcp_servers>, C<tools>, …) is rejected explicitly with a
282 message saying why — a manifest never carries those;
283  
284 =item * URLs are printable-ASCII C<http>/C<https> without userinfo, query
285 or fragment (best effort: a secret embedded in the path itself cannot be
286 detected);
287  
288 =item * ids and model ids carry no control or format characters (a client
289 prints them);
290  
291 =item * ids are unique and every C<auth_ref> / C<endpoint_ref> resolves;
292  
293 =item * C<schema_version> must be a JSON number whose value is C<1> (C<1>,
294 C<1.0> and C<1e0> alike, on every JSON backend; the string C<"1"> is not); any other version
295 is rejected before anything else is checked.
296  
297 =back
298  
299 C<extensions> is inert: it must be an object of plain JSON data, and it is
300 kept and serialized exactly as given, never validated further or
301 interpreted by core.
302  
303 Values, unlike structure, are open: an unknown C<dialect>, auth C<type> or
304 capability name is accepted. C<is_known_dialect> / C<is_known_type> tell a
305 client whether it has an adapter; a client treats a capability it does not
306 know as absent.
307  
308 A manifest states what the provider B<claims>. It is not a probe result and
309 grants no local permission: a model claiming C<tools_native> does not
310 authorise running local tools.
311  
312 =head2 provider_id
313  
314 Stable provider slug, C<[a-z0-9][a-z0-9._-]*> (max 128).
315  
316 =head2 issuer
317  
318 Origin of the publisher, an C<http>/C<https> URL.
319  
320 =head2 endpoints
321  
322 ArrayRef of L<Langertha::Manifest::Endpoint>; at least one.
323  
324 =head2 auth
325  
326 ArrayRef of L<Langertha::Manifest::Auth>; may be empty.
327  
328 =head2 models
329  
330 ArrayRef of L<Langertha::Manifest::Model>; may be empty (a filtered
331 manifest can legitimately list none).
332  
333 =head2 extensions
334  
335 HashRef, inert: never validated beyond "plain JSON data" and never
336 interpreted by core; serialized exactly as given. It is deep-copied on
337 construction and every read returns a fresh copy, so mutating the input or
338 the returned structure does not change the manifest. A blessed object
339 (other than a JSON boolean) or a code reference in it is rejected.
340  
341 =head2 schema_version
342  
343 Always C<1>: the only schema version this Langertha reads and writes.
344  
345 =head2 kind
346  
347 Always C<langertha-provider>.
348  
349 =head2 from_json
350  
351 my $manifest = Langertha::Manifest->from_json($json_bytes);
352  
353 Decodes UTF-8 JSON text and hands it to L</from_hash>. Croaks on invalid
354 JSON and on every validation failure.
355  
356 =head2 from_hash
357  
358 my $manifest = Langertha::Manifest->from_hash(\%data);
359  
360 Validates a decoded manifest document and returns the object. Croaks with
361 C<Langertha::Manifest: E<lt>pathE<gt>: E<lt>reasonE<gt>> on the first
362 violation.
363  
364 =head2 endpoint
365  
366 my $endpoint = $manifest->endpoint('chat');
367  
368 The endpoint with that id, or C<undef>.
369  
370 =head2 auth_entry
371  
372 my $auth = $manifest->auth_entry( $endpoint->auth_ref );
373  
374 The auth entry with that id, or C<undef>.
375  
376 =head2 models_for_endpoint
377  
378 my @models = $manifest->models_for_endpoint('chat');
379  
380 The model entries served on that endpoint.
381  
382 =head2 to_hash
383  
384 The manifest as a plain Perl data structure, ready for any JSON encoder.
385 Capability values are JSON booleans; C<extensions> is returned as given.
386  
387 =head2 to_json
388  
389 UTF-8 JSON with sorted keys (canonical), so the output is byte-stable:
390 C<to_json> is a fixed point after one roundtrip
391 (C<< from_json($m->to_json)->to_json eq $m->to_json >>). Input that omitted
392 optional sections comes back with their defaults (C<auth>, C<models>,
393 C<extensions>, C<capabilities>) filled in, so the first serialization of
394 such input is not byte-identical to it.
395  
396 =head1 SEE ALSO
397  
398 =over
399  
400 =item * L<Langertha::Manifest::Builder> - Builds a manifest from configured engines
401  
402 =item * L<Langertha::Manifest::Endpoint>, L<Langertha::Manifest::Auth>, L<Langertha::Manifest::Model> - The entries
403  
404 =item * L<Langertha::Role::Capabilities> - The capability vocabulary
405  
406 =back
407  
408 =head1 SUPPORT
409  
410 =head2 Issues
411  
412 Please report bugs and feature requests on GitHub at
413 L<https://github.com/Getty/langertha/issues>.
414  
415 =head2 IRC
416  
417 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
418  
419 =head1 CONTRIBUTING
420  
421 Contributions are welcome! Please fork the repository and submit a pull request.
422  
423 =head1 AUTHOR
424  
425 Torsten Raudssus <getty@cpan.org>
426  
427 =head1 COPYRIGHT AND LICENSE
428  
429 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
430  
431 This is free software; you can redistribute it and/or modify it under
432 the same terms as the Perl 5 programming language system itself.
433  
434 =cut
« blib/lib/Langertha/Engine/Perplexity.pm ↑ Summary blib/lib/Langertha/ModelProbe.pm »