blib/lib/Langertha/Manifest/Builder.pm

filter: Statementstmt 95.3% 164 / 172 Branchbran 75.6% 59 / 78 Conditioncond 51.9% 27 / 52 MC/DCmcdc 23.3% 7 / 30 Subroutinesub 96.4% 27 / 28 Podpod 100% 9 / 9 totaltotal 79.4% 293 / 369 CC 61 SCAR 27.4
CC
61
Cov
83%
add_engine
37.3
_reraise
12.0
_auth_type_for
8.1
CRAP 15.5
« blib/lib/Langertha/Role/Chat.pm ↑ Summary blib/lib/Langertha/Role/ReasoningEffort.pm »
1 package Langertha::Manifest::Builder;
2 # ABSTRACT: Build a provider manifest from configured engines (offline, never copies a secret)
3 6 our $VERSION = '0.503';
4 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Module::Runtime qw( use_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Langertha::Manifest;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 6 has provider_id => (
13 is => 'ro',
14 isa => 'Str',
15 writer => '_set_provider_id',
16 predicate => 'has_provider_id',
17 );
18  
19 6 has issuer => (
20 is => 'ro',
21 isa => 'Str',
22 writer => '_set_issuer',
23 predicate => 'has_issuer',
24 );
25  
26 35 ▶has extensions => ( is => 'ro', isa => 'HashRef', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
27  
28 35 ▶has _endpoints => ( is => 'ro', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
29 35 ▶has _auth => ( is => 'ro', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
30 35 ▶has _models => ( is => 'ro', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
31  
32  
33 # The capabilities a model entry may claim: exactly those that describe a
34 # CHAT CALL to that model at that endpoint -- what the request may carry and
35 # what the reply can be. Everything else engine_capabilities reports is left
36 # out on purpose:
37 # - other operations of the engine (embedding, transcription,
38 # image_generation) are not facts about a chat model;
39 # - client-side or server-management features (runtime_metrics is
40 # Langertha's own Prometheus scrape, prefix_caching the self-hosted
41 # cache knobs, keep_alive Ollama's model residency, cached_content
42 # Gemini's cache-resource lifecycle, context_size Ollama's server-side
43 # num_ctx allocation) are not provider claims about a model either, and
44 # would be wrong behind a proxy's public URL.
45 # A capability added to %ROLE_TO_CAPS later is NOT published until it is
46 # added here (t/96_manifest_builder.t forces that decision).
47 6 my @MODEL_CAPABILITIES = qw(
48 chat
49 streaming
50 tools_native tools_hermes
51 tool_choice_auto tool_choice_any tool_choice_none tool_choice_named
52 parallel_tool_use
53 response_format_json_object response_format_json_schema
54 reasoning_effort thinking_budget
55 temperature seed
56 system_prompt response_size
57 prompt_cache prompt_cache_key
58 server_tools
59 image_input
60 );
61 6 my %MODEL_CAPABILITY = map { $_ => 1 } @MODEL_CAPABILITIES;
62  
63 1 ▶sub model_capabilities { return @MODEL_CAPABILITIES }
Subroutine model_capabilities: called CC=1 SCAR=0.0
Pod documented
64  
65  
66 # Most specific first: OpenAIResponses isa OpenAI isa OpenAIBase. A code
67 # value decides within a family.
68 my @DIALECT_BY_CLASS = (
69 [ 'Langertha::Engine::Perplexity' => 'perplexity-agent' ],
70 [ 'Langertha::Engine::OpenAIResponses' => 'responses' ],
71 [ 'Langertha::Engine::OpenAIBase' => 'openai-chat' ],
72 # Same Messages envelope, two wire variants: first-party Anthropic sends
73 # structured output as native output_config.format, the /anthropic shims
74 # emulate it with a synthetic tool + forced tool_choice. The engine's own
75 # predicate (Role::AnthropicCompatible) tells them apart.
76 11 ▶ [ 'Langertha::Engine::AnthropicBase' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
77 11 ▶ $_[0]->_native_structured_output ? 'anthropic' : 'anthropic-compat' } ],
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_native_structured_output ? : 5 6 11
78 [ 'Langertha::Engine::Gemini' => 'gemini' ],
79 [ 'Langertha::Engine::Ollama' => 'ollama' ],
80 [ 'Langertha::Engine::AKI' => 'aki' ],
81 [ 'Langertha::Engine::LMStudio' => 'lmstudio' ],
82 );
83  
84 52 ▶sub dialect_for_engine {
Subroutine dialect_for_engine: called CC=4 SCAR=13.9
Pod documented
85 52 my ( $class, $engine ) = @_;
86 52 for my $row (@DIALECT_BY_CLASS) {
87 184 my ( $isa, $dialect ) = @$row;
88 184 ▶ next unless $engine->isa($isa);
Branch: 1 branch
BranchTrueFalseTotal
unless $engine->isa($isa) 132 52 184
89 52 ▶ return ref $dialect eq 'CODE' ? $dialect->($engine) : $dialect;
Branch: 1 branch
BranchTrueFalseTotal
ref $dialect eq 'CODE' ? : 11 41 52
90 }
91 0 return undef;
92 }
93  
94  
95 # The inverse of @DIALECT_BY_CLASS: per dialect the one generic class that
96 # takes the endpoint's base_url as its `url` (not a vendor subclass).
97 6 my %ENGINE_CLASS_BY_DIALECT = (
98 'openai-chat' => 'Langertha::Engine::OpenAI',
99 'responses' => 'Langertha::Engine::OpenAIResponses',
100 'perplexity-agent' => 'Langertha::Engine::Perplexity',
101 'anthropic' => 'Langertha::Engine::Anthropic',
102 'anthropic-compat' => 'Langertha::Engine::AnthropicBase',
103 'gemini' => 'Langertha::Engine::Gemini',
104 'ollama' => 'Langertha::Engine::Ollama',
105 'aki' => 'Langertha::Engine::AKI',
106 'lmstudio' => 'Langertha::Engine::LMStudio',
107 );
108  
109 20 ▶sub engine_class_for_dialect {
Subroutine engine_class_for_dialect: called CC=3 SCAR=11.0
Pod documented
110 20 my ( $class, $dialect ) = @_;
111 20 ▶ return undef unless defined $dialect;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dialect 1 19 20
112 19 ▶ my $engine_class = $ENGINE_CLASS_BY_DIALECT{$dialect} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $engine_class = $ENGINE_CLASS_BY_DIALECT{$dialect} or return undef 18 1 19
113 18 return use_module($engine_class);
114 }
115  
116  
117 # Validation errors from the value objects are "...\n" strings without a
118 # location; re-raise them from the caller's perspective.
119 0 ▶sub _reraise {
Subroutine _reraise: not called CC=3 SCAR=24.8
120 0 my ($error) = @_;
121 0 ▶ my $message = blessed($error) && $error->can('message') ? $error->message : "$error";
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($error) && $error->can('message') ? : 0 0 0
Condition0%0/3
builtin::blessed($error) and $error->can("message")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($error) and $error->can("message")
builtin::blessed($error) $error->can("message")
Truth table0%0/3
A and B
A= builtin::blessed($error)B= $error->can("message")
ABresult
0X0
100
111
122 0 $message =~ s/\s+\z//;
123 0 croak $message;
124 }
125  
126 31 ▶sub from_engine {
Subroutine from_engine: called CC=2 SCAR=6.9
Pod documented
127 31 my ( $class, $engine, %opt ) = @_;
128 my $self = $class->new(
129 31 ▶ map { exists $opt{$_} ? ( $_ => delete $opt{$_} ) : () } qw( provider_id issuer extensions )
Branch: 1 branch
BranchTrueFalseTotal
exists $opt{$_} ? : 2 91 93
130 );
131 31 $self->add_engine( $engine, %opt );
132 28 return $self->manifest;
133 }
134  
135  
136 38 ▶sub add_engine {
Subroutine add_engine: called CC=31 SCAR=36.2
Pod documented
137 38 my ( $self, $engine, %opt ) = @_;
138 38 ▶ croak 'Langertha::Manifest::Builder: add_engine needs a chat engine (an object'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($engine) and $engine->can("does") and $engine->does("Langertha::Role::Chat") 2 36 38
Condition50%4/8
builtin::blessed($engine) and $engine->can("does") and $engine->does("Langertha::Role::Chat")
!ll&&!rl&&r
0236
builtin::blessed($engine) and $engine->can("does")
!ll&&!rl&&r
0038
ref $engine || 'a non-object'
l!l
20
MC/DC33%1/3
builtin::blessed($engine) and $engine->can("does") and $engine->does("Langertha::Role::Chat")
builtin::blessed($engine) $engine->can("does") $engine->does("Langertha::Role::Chat")
MC/DC0%0/1
ref $engine || 'a non-object'
ref $engine
Truth table50%2/4
A and B and C
A= builtin::blessed($engine)B= $engine->can("does")C= $engine->does("Langertha::Role::Chat")
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= ref $engine
Aresult
11
00
139 . ' composing Langertha::Role::Chat); ' . ( ref($engine) || 'a non-object' ) . ' is not one'
140 unless blessed($engine) && $engine->can('does') && $engine->does('Langertha::Role::Chat');
141  
142 36 ▶ my $dialect = $opt{dialect} // $self->dialect_for_engine($engine);
Condition33%1/3
$opt{"dialect"} // $self->dialect_for_engine($engine)
l!l&&r!l&&!r
0360
MC/DC0%0/2
$opt{"dialect"} // $self->dialect_for_engine($engine)
$opt{"dialect"} $self->dialect_for_engine($engine)
Truth table33%1/3
A // B
A= $opt{"dialect"}B= $self->dialect_for_engine($engine)
ABresult
1X1
011
000
143 36 ▶ croak 'Langertha::Manifest::Builder: ' . ref($engine) . ' has no manifest dialect;'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dialect 0 36 36
144 . ' pass dialect => ... to name one'
145 unless defined $dialect;
146  
147 36 ▶ my $base_url = $opt{base_url} // ( $engine->can('url') ? $engine->url : undef );
Branch: 1 branch
BranchTrueFalseTotal
$engine->can('url') ? : 33 0 33
Condition66%2/3
$opt{"base_url"} // ($engine->can("url") ? $engine->url : undef)
l!l&&r!l&&!r
3330
MC/DC0%0/2
$opt{"base_url"} // ($engine->can("url") ? $engine->url : undef)
$opt{"base_url"} ($engine->can("url") ? $engine->url : undef)
Truth table66%2/3
A // B
A= $opt{"base_url"}B= ($engine->can("url") ? $engine->url : undef)
ABresult
1X1
011
000
148 36 ▶ croak 'Langertha::Manifest::Builder: ' . ref($engine) . ' has no url; pass base_url => ...'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base_url 0 36 36
149 unless defined $base_url;
150  
151 # Everything lazy is read from a clone, so the caller's engine keeps its
152 # unbuilt slots (and a model-less engine with `models` given never runs its
153 # croaking default_model).
154 36 my $probe = _capability_clone($engine);
155  
156 36 my @model_ids;
157 36 ▶ if ( $opt{models} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'models'}) { } 24 12 36
158 24 @model_ids = @{ $opt{models} };
159 }
160 else {
161 12 my $model_id = eval { $probe->chat_model };
162 12 ▶ croak 'Langertha::Manifest::Builder: cannot determine a model for ' . ref($engine)
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 11 12
163 . '; pass models => [...]'
164 if $@;
165 # "default" is the self-hosted engines' placeholder for "whatever the
166 # server loaded" -- not a model id worth publishing.
167 11 ▶ @model_ids = grep { defined && length && $_ ne 'default' } $model_id;
Condition50%3/6
defined $_ && length $_ && $_ ne 'default'
!ll&&!rl&&r
0110
defined $_ and length $_
!ll&&!rl&&r
0011
MC/DC33%1/3
defined $_ && length $_ && $_ ne 'default'
defined $_ length $_ $_ ne 'default'
Truth table50%2/4
A and B && C
A= defined $_B= length $_C= $_ ne 'default'
ABCresult
0XX0
10X0
1100
1111
168 carp 'Langertha::Manifest::Builder: ' . ref($engine) . ' has only the placeholder model '
169 11 ▶ . q{'} . ( $model_id // '' ) . q{'} . "; endpoint '" . ( $opt{endpoint_id} // 'chat' )
Branch: 1 branch
BranchTrueFalseTotal
unless @model_ids 1 10 11
Condition50%2/4
$model_id // ""
l!l
10
$opt{"endpoint_id"} // "chat"
l!l
01
MC/DC0%0/1
$model_id // ""
$model_id
MC/DC0%0/1
$opt{"endpoint_id"} // "chat"
$opt{"endpoint_id"}
Truth table50%1/2
A
A= $model_id
Aresult
11
00
Truth table50%1/2
A
A= $opt{"endpoint_id"}
Aresult
11
00
170 . q{' is published without models (pass models => [...] to list them)}
171 unless @model_ids;
172 }
173  
174 35 ▶ my $auth_type = $opt{auth} // _auth_type_for($probe);
Condition66%2/3
$opt{"auth"} // _auth_type_for($probe)
l!l&&r!l&&!r
1340
MC/DC0%0/2
$opt{"auth"} // _auth_type_for($probe)
$opt{"auth"} _auth_type_for($probe)
Truth table66%2/3
A // B
A= $opt{"auth"}B= _auth_type_for($probe)
ABresult
1X1
011
000
175 35 my ( $auth_ref, $new_auth );
176 35 ▶ if ( $auth_type ne 'none' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_type ne "none") 28 7 35
177 28 ▶ $auth_ref = $opt{auth_id} // 'api';
Condition50%1/2
$opt{"auth_id"} // "api"
l!l
028
MC/DC0%0/1
$opt{"auth_id"} // "api"
$opt{"auth_id"}
Truth table50%1/2
A
A= $opt{"auth_id"}
Aresult
11
00
178 28 my ($existing) = grep { $_->id eq $auth_ref } @{ $self->_auth };
179 28 ▶ if ($existing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing) { } 2 26 28
180 2 ▶ croak "Langertha::Manifest::Builder: auth id '$auth_ref' already has type '"
Branch: 1 branch
BranchTrueFalseTotal
unless $existing->type eq $auth_type 0 2 2
181 . $existing->type . q{'}
182 unless $existing->type eq $auth_type;
183 }
184 else {
185 26 ▶ $new_auth = eval { Langertha::Manifest::Auth->new( id => $auth_ref, type => $auth_type ) }
Branch: 1 branch
BranchTrueFalseTotal
$new_auth = eval { do { "Langertha::Manifest::Auth"->new("id", $auth_ref, "type", $auth_type) } } or _reraise($@) 26 0 26
186 or _reraise($@);
187 }
188 }
189  
190 35 ▶ my $endpoint_id = $opt{endpoint_id} // 'chat';
Condition100%2/2
$opt{"endpoint_id"} // "chat"
l!l
530
MC/DC100%1/1
$opt{"endpoint_id"} // "chat"
$opt{"endpoint_id"}
Truth table100%2/2
A
A= $opt{"endpoint_id"}
Aresult
11
00
191 35 ▶ my $endpoint = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $endpoint = eval { do { "Langertha::Manifest::Endpoint"->new("id", $endpoint_id, "dialect", $dialect, "base_url", $base_url, defined $auth_ref ? ("auth_ref", $auth_ref) : ()) } } or _reraise($@) 35 0 35
192 35 ▶ Langertha::Manifest::Endpoint->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $auth_ref ? : 28 7 35
193 id => $endpoint_id,
194 dialect => $dialect,
195 base_url => $base_url,
196 ( defined $auth_ref ? ( auth_ref => $auth_ref ) : () ),
197 );
198 } or _reraise($@);
199 35 $self->_check_new_endpoint($endpoint);
200  
201 33 my @models;
202 33 for my $model_id (@model_ids) {
203 44 ▶ my $model = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $model = eval { do { "Langertha::Manifest::Model"->new("id", $model_id, "endpoint_ref", $endpoint_id, "capabilities", _capabilities_for($engine, $model_id)) } } or _reraise($@) 44 0 44
204 44 Langertha::Manifest::Model->new(
205 id => $model_id,
206 endpoint_ref => $endpoint_id,
207 capabilities => _capabilities_for( $engine, $model_id ),
208 );
209 } or _reraise($@);
210 44 $self->_check_new_model( $model, @models );
211 43 push @models, $model;
212 }
213  
214 # All checks passed: commit. A croak above leaves the builder unchanged.
215 32 ▶ push @{ $self->_auth }, $new_auth if $new_auth;
Branch: 1 branch
BranchTrueFalseTotal
if $new_auth 24 8 32
216 32 push @{ $self->_endpoints }, $endpoint;
217 32 push @{ $self->_models }, @models;
218 32 ▶ $self->_set_provider_id( _provider_id_for( ref $engine ) ) unless $self->has_provider_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_provider_id 29 3 32
219 32 ▶ $self->_set_issuer( _origin_of($base_url) ) unless $self->has_issuer;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_issuer 29 3 32
220 32 return $self;
221 }
222  
223  
224 37 ▶sub _check_new_endpoint {
Subroutine _check_new_endpoint: called CC=2 SCAR=6.9
225 37 my ( $self, $endpoint ) = @_;
226 croak "Langertha::Manifest::Builder: duplicate endpoint id '" . $endpoint->id . q{'}
227 37 ▶ if grep { $_->id eq $endpoint->id } @{ $self->_endpoints };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->id eq $endpoint->id;} @{$self->_endpoints;} 2 35 37
228 35 return;
229 }
230  
231 46 ▶sub _check_new_model {
Subroutine _check_new_model: called CC=3 SCAR=11.0
232 46 my ( $self, $model, @pending ) = @_;
233 croak "Langertha::Manifest::Builder: duplicate model '" . $model->id . "' on endpoint '"
234 . $model->endpoint_ref . q{'}
235 18 ▶ if grep { $_->id eq $model->id && $_->endpoint_ref eq $model->endpoint_ref }
Condition100%3/3
$_->id eq $model->id && $_->endpoint_ref eq $model->endpoint_ref
!ll&&!rl&&r
1152
MC/DC100%2/2
$_->id eq $model->id && $_->endpoint_ref eq $model->endpoint_ref
$_->id eq $model->id $_->endpoint_ref eq $model->endpoint_ref
Truth table100%3/3
A && B
A= $_->id eq $model->idB= $_->endpoint_ref eq $model->endpoint_ref
ABresult
0X0
100
111
236 46 ▶ @{ $self->_models }, @pending;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->id eq $model->id and $_->endpoint_ref eq $model->endpoint_ref;} @{$self->_models;}, @pending 2 44 46
237 44 return;
238 }
239  
240 2 ▶sub add_endpoint {
Subroutine add_endpoint: called CC=2 SCAR=7.0
Pod documented
241 2 my ( $self, %args ) = @_;
242 2 ▶ my $endpoint = eval { Langertha::Manifest::Endpoint->new(%args) } or _reraise($@);
Branch: 1 branch
BranchTrueFalseTotal
my $endpoint = eval { do { "Langertha::Manifest::Endpoint"->new(%args) } } or _reraise($@) 2 0 2
243 2 $self->_check_new_endpoint($endpoint);
244 2 push @{ $self->_endpoints }, $endpoint;
245 2 return $self;
246 }
247  
248  
249 1 ▶sub add_auth {
Subroutine add_auth: called CC=3 SCAR=12.6
Pod documented
250 1 my ( $self, %args ) = @_;
251 1 ▶ my $auth = eval { Langertha::Manifest::Auth->new(%args) } or _reraise($@);
Branch: 1 branch
BranchTrueFalseTotal
my $auth = eval { do { "Langertha::Manifest::Auth"->new(%args) } } or _reraise($@) 1 0 1
252 croak "Langertha::Manifest::Builder: duplicate auth id '" . $auth->id . q{'}
253 1 ▶ if grep { $_->id eq $auth->id } @{ $self->_auth };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->id eq $auth->id;} @{$self->_auth;} 1 0 1
254 0 push @{ $self->_auth }, $auth;
255 0 return $self;
256 }
257  
258  
259 2 ▶sub add_model {
Subroutine add_model: called CC=2 SCAR=7.0
Pod documented
260 2 my ( $self, %args ) = @_;
261 2 ▶ my $model = eval { Langertha::Manifest::Model->new(%args) } or _reraise($@);
Branch: 1 branch
BranchTrueFalseTotal
my $model = eval { do { "Langertha::Manifest::Model"->new(%args) } } or _reraise($@) 2 0 2
262 2 $self->_check_new_model($model);
263 1 push @{ $self->_models }, $model;
264 1 return $self;
265 }
266  
267  
268 33 ▶sub manifest {
Subroutine manifest: called CC=4 SCAR=14.2
Pod documented
269 33 my ($self) = @_;
270 33 ▶ croak 'Langertha::Manifest::Builder: no provider_id (add an engine or pass provider_id)'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_provider_id 0 33 33
271 unless $self->has_provider_id;
272 33 ▶ croak 'Langertha::Manifest::Builder: no issuer (add an engine or pass issuer)'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_issuer 0 33 33
273 unless $self->has_issuer;
274 33 ▶ my $manifest = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $manifest = eval { do { "Langertha::Manifest"->new("provider_id", $self->provider_id, "issuer", $self->issuer, "endpoints", [@{$self->_endpoints;}], "auth", [@{$self->_auth;}], "models", [@{$self->_models;}], "extensions", $self->extensions) } } or _reraise($@) 33 0 33
275 Langertha::Manifest->new(
276 provider_id => $self->provider_id,
277 issuer => $self->issuer,
278 33 endpoints => [ @{ $self->_endpoints } ],
279 33 auth => [ @{ $self->_auth } ],
280 33 models => [ @{ $self->_models } ],
281 extensions => $self->extensions,
282 );
283 } or _reraise($@);
284 33 return $manifest;
285 }
286  
287  
288 29 ▶sub _provider_id_for {
Subroutine _provider_id_for: called CC=1 SCAR=0.0
289 29 my ($class_name) = @_;
290 29 ( my $name = $class_name ) =~ s/\A.*::Engine:://;
291 29 $name =~ s/\A.*:://;
292 29 return lc $name;
293 }
294  
295 29 ▶sub _origin_of {
Subroutine _origin_of: called CC=4 SCAR=14.3
296 29 my ($url) = @_;
297 29 my $uri = URI->new($url);
298 29 ▶ return $url unless $uri->can('host') && $uri->can('port');
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->can("host") and $uri->can("port") 0 29 29
Condition33%1/3
$uri->can("host") and $uri->can("port")
!ll&&!rl&&r
0029
MC/DC0%0/2
$uri->can("host") and $uri->can("port")
$uri->can("host") $uri->can("port")
Truth table33%1/3
A and B
A= $uri->can("host")B= $uri->can("port")
ABresult
0X0
100
111
299 29 my $origin = $uri->scheme . '://' . $uri->host;
300 29 ▶ $origin .= ':' . $uri->port if $uri->port != $uri->default_port;
Branch: 1 branch
BranchTrueFalseTotal
if $uri->port != $uri->default_port 17 12 29
301 29 return $origin;
302 }
303  
304 # $probe is a clone: reading the lazy api_key builds it there, not on the
305 # caller's engine.
306 34 ▶sub _auth_type_for {
Subroutine _auth_type_for: called CC=7 SCAR=21.0
307 34 my ($probe) = @_;
308 34 ▶ return 'api_key' if $probe->can('api_key_required') && $probe->api_key_required;
Branch: 1 branch
BranchTrueFalseTotal
if $probe->can("api_key_required") and $probe->api_key_required 21 13 34
Condition66%2/3
$probe->can("api_key_required") and $probe->api_key_required
!ll&&!rl&&r
01321
MC/DC50%1/2
$probe->can("api_key_required") and $probe->api_key_required
$probe->can("api_key_required") $probe->api_key_required
Truth table66%2/3
A and B
A= $probe->can("api_key_required")B= $probe->api_key_required
ABresult
0X0
100
111
309 # Optional key: announce the mechanism only when one is configured. Only
310 # definedness is inspected; the value is dropped on the spot.
311 13 ▶ return 'none'
Branch: 1 branch
BranchTrueFalseTotal
unless $probe->can("api_key_env") and defined $probe->api_key_env and $probe->can("api_key") 0 13 13
Condition33%2/6
$probe->can("api_key_env") and defined $probe->api_key_env and $probe->can("api_key")
!ll&&!rl&&r
0013
$probe->can("api_key_env") and defined $probe->api_key_env
!ll&&!rl&&r
0013
MC/DC0%0/3
$probe->can("api_key_env") and defined $probe->api_key_env and $probe->can("api_key")
$probe->can("api_key_env") defined $probe->api_key_env $probe->can("api_key")
Truth table25%1/4
A and B and C
A= $probe->can("api_key_env")B= defined $probe->api_key_envC= $probe->can("api_key")
ABCresult
0XX0
10X0
1100
1111
312 unless $probe->can('api_key_env') && defined $probe->api_key_env && $probe->can('api_key');
313 13 my $configured = defined( scalar eval { $probe->api_key } );
314 13 ▶ return $configured ? 'api_key' : 'none';
Branch: 1 branch
BranchTrueFalseTotal
$configured ? : 7 6 13
315 }
316  
317 44 ▶sub _capabilities_for {
Subroutine _capabilities_for: called CC=2 SCAR=7.0
318 44 my ( $engine, $model_id ) = @_;
319 # engine_capabilities is model-scoped (ADR 0019 layer 3 and model-aware
320 # `around engine_capabilities`, e.g. Gemini): evaluate it on an in-memory
321 # clone whose chat_model is this model. The caller's engine is not read
322 # for its chat_model and not touched.
323 44 my $probe = _capability_clone( $engine, chat_model => $model_id );
324 44 my $caps = $probe->engine_capabilities;
325 44 ▶ return { map { $_ => 1 } grep { $caps->{$_} && $MODEL_CAPABILITY{$_} } keys %$caps };
Condition66%2/3
$$caps{$_} && $MODEL_CAPABILITY{$_}
!ll&&!rl&&r
072620
MC/DC50%1/2
$$caps{$_} && $MODEL_CAPABILITY{$_}
$$caps{$_} $MODEL_CAPABILITY{$_}
Truth table66%2/3
A && B
A= $$caps{$_}B= $MODEL_CAPABILITY{$_}
ABresult
0X0
100
111
326 }
327  
328 # clone_object copies every slot already set, so a tool_wire_format the
329 # builder made for the source engine's chat_model would ride along into a
330 # probe for another model (karr k251). The probe drops a builder-made tag and
331 # resolves it again for its own chat_model; a constructor tag is kept.
332 80 ▶sub _capability_clone {
Subroutine _capability_clone: called CC=2 SCAR=7.0
333 80 my ( $engine, %params ) = @_;
334 80 my $probe = $engine->meta->clone_object( $engine, %params );
335 80 ▶ $probe->_reset_derived_tool_wire_format if $probe->can('_reset_derived_tool_wire_format');
Branch: 1 branch
BranchTrueFalseTotal
if $probe->can("_reset_derived_tool_wire_format") 80 0 80
336 80 return $probe;
337 }
338  
339 6 __PACKAGE__->meta->make_immutable;
340  
341  
342 6 1;
343  
344 __END__
345  
346 =pod
347  
348 =encoding UTF-8
349  
350 =head1 NAME
351  
352 Langertha::Manifest::Builder - Build a provider manifest from configured engines (offline, never copies a secret)
353  
354 =head1 VERSION
355  
356 version 0.503
357  
358 =head1 SYNOPSIS
359  
360 use Langertha::Manifest::Builder;
361  
362 # One engine, one endpoint
363 my $manifest = Langertha::Manifest::Builder->from_engine(
364 Langertha::Engine::vLLM->new( url => 'http://gpu01:8000/v1', model => 'qwen3' ),
365 );
366 print $manifest->to_json;
367  
368 # A proxy exposing several protocol endpoints under its public URL
369 my $builder = Langertha::Manifest::Builder->new(
370 provider_id => 'my-knarr',
371 issuer => 'https://knarr.example',
372 );
373 $builder->add_engine( $openrouter_engine, # no model of its own needed
374 endpoint_id => 'openai', base_url => 'https://knarr.example/v1',
375 models => [ 'gpt-5.6', 'local-qwen' ] );
376 $builder->add_endpoint( id => 'ollama', dialect => 'ollama',
377 base_url => 'https://knarr.example' );
378 $builder->add_model( id => 'local-qwen', endpoint_ref => 'ollama',
379 capabilities => { chat => 1, streaming => 1 } );
380 my $manifest = $builder->manifest;
381  
382 =head1 DESCRIPTION
383  
384 Maps configured Langertha chat engines into a L<Langertha::Manifest>.
385 Everything is read from the engine object: no network I/O happens (in
386 particular C<list_models> is never called), and the caller's engine is not
387 touched — lazy attributes (C<model>, C<chat_model>, C<api_key>) are
388 evaluated on in-memory clones, never on the engine itself.
389  
390 =over
391  
392 =item * B<dialect> — from the engine family, most specific class first:
393 L<Langertha::Engine::Perplexity> → C<perplexity-agent>,
394 L<Langertha::Engine::OpenAIResponses> → C<responses>,
395 L<Langertha::Engine::OpenAIBase> → C<openai-chat>,
396 L<Langertha::Engine::AnthropicBase> → C<anthropic> when the engine emits
397 first-party native structured output (C<output_config.format>), otherwise
398 C<anthropic-compat> (the C</anthropic> shims: AKIAnthropic,
399 MiniMaxAnthropic, MoonshotAnthropic, LMStudioAnthropic),
400 L<Langertha::Engine::Gemini> → C<gemini>, L<Langertha::Engine::Ollama> →
401 C<ollama>, L<Langertha::Engine::AKI> → C<aki>, L<Langertha::Engine::LMStudio>
402 → C<lmstudio>. An engine that is not a chat engine (e.g. the
403 transcription-only L<Langertha::Engine::Whisper>) croaks.
404  
405 =item * B<base_url> — the engine's C<url> (override with C<base_url> to
406 publish a public URL instead of an internal one).
407  
408 =item * B<auth> — from the engine class's C<api_key_required> /
409 C<api_key_env>: a required key yields an C<api_key> auth entry; an optional
410 key only when one is configured; no key, no entry. Only the I<definedness>
411 of the key is looked at — its value never enters the manifest.
412  
413 =item * B<models> — C<models =E<gt> [...]> when given. Otherwise the
414 engine's configured model; a placeholder id (C<default>, which the
415 self-hosted engines use for "whatever the server loaded", or an empty id)
416 is skipped rather than published (with a warning, since the endpoint then
417 lists no models), and an engine with no model at all
418 croaks asking for C<models>.
419  
420 =item * B<capabilities> — C<engine_capabilities> evaluated B<per model>
421 (on a clone with C<chat_model> set to that model id, so model-scoped
422 corrections apply), then filtered to L</model_capabilities>: only flags that
423 describe a chat call to that model at that endpoint. Names are exactly the
424 registry's (L<Langertha::Role::Capabilities>); the Builder adds none.
425  
426 =back
427  
428 Known v1 limitations: engine-class facts that the capability flags cannot
429 express are not in the manifest — the Groq/Cerebras refusal of C<tools> plus
430 C<response_format> in one request (ADR 0024) and OpenAI's temperature gate
431 under active reasoning (ADR 0025).
432  
433 =head2 provider_id
434  
435 The manifest's C<provider_id>. When not given, the first L</add_engine>
436 derives it from the engine class (C<Langertha::Engine::vLLM> → C<vllm>).
437  
438 =head2 issuer
439  
440 The manifest's C<issuer>. When not given, the first L</add_engine> derives
441 it from the origin of the engine's URL.
442  
443 =head2 extensions
444  
445 HashRef passed into the manifest's C<extensions> (deep-copied there).
446  
447 =head2 model_capabilities
448  
449 my @names = Langertha::Manifest::Builder->model_capabilities;
450  
451 The allowlist of capability names a Builder-made model entry may claim —
452 the flags that describe a chat call to that model: C<chat>, C<streaming>,
453 the tool flags (C<tools_native>, C<tools_hermes>, C<tool_choice_auto>,
454 C<tool_choice_any>, C<tool_choice_none>, C<tool_choice_named>,
455 C<parallel_tool_use>), structured output (C<response_format_json_object>,
456 C<response_format_json_schema>), reasoning (C<reasoning_effort>,
457 C<thinking_budget>), sampling and request controls (C<temperature>,
458 C<seed>, C<system_prompt>, C<response_size>) and the
459 request-side prompt-cache controls (C<prompt_cache>, C<prompt_cache_key>),
460 C<server_tools> (the wire accepts provider-native server-side tools; I<which>
461 types is not published), and C<image_input> (the model sees an image part;
462 model-scoped, see L<Langertha::Role::ImageInput>).
463  
464 Engine-level and client-side flags are never published on a model:
465 C<embedding>, C<transcription>, C<image_generation>, C<runtime_metrics>,
466 C<prefix_caching>, C<keep_alive>, C<cached_content>, C<context_size> (Ollama's
467 server-side C<num_ctx> allocation, like C<keep_alive>).
468  
469 This filters only what the Builder B<emits>. A parsed manifest accepts any
470 capability name (L<Langertha::Manifest::Model/supports>).
471  
472 =head2 dialect_for_engine
473  
474 my $dialect = Langertha::Manifest::Builder->dialect_for_engine($engine);
475  
476 The manifest dialect of an engine (see L</DESCRIPTION>), or C<undef> when
477 its family has none.
478  
479 =head2 engine_class_for_dialect
480  
481 my $engine_class = Langertha::Manifest::Builder->engine_class_for_dialect('openai-chat');
482 my $engine = $engine_class->new( url => $endpoint->base_url, api_key => $key );
483  
484 The inverse of L</dialect_for_engine>: the generic engine class that speaks
485 a manifest dialect, loaded and ready for C<new>, or C<undef> for an unknown
486 (or undefined) dialect. It never croaks on an unknown dialect.
487  
488 Each dialect maps to the one class that takes the endpoint's C<base_url> as
489 its C<url>, not to a vendor subclass: C<openai-chat> is
490 L<Langertha::Engine::OpenAI> (the ~25 OpenAI-compatible engines share that
491 wire), C<responses> L<Langertha::Engine::OpenAIResponses>, C<perplexity-agent>
492 L<Langertha::Engine::Perplexity>, C<anthropic> L<Langertha::Engine::Anthropic>,
493 C<anthropic-compat> L<Langertha::Engine::AnthropicBase> (the C</anthropic>
494 shims), C<gemini> L<Langertha::Engine::Gemini>, C<ollama>
495 L<Langertha::Engine::Ollama>, C<aki> L<Langertha::Engine::AKI> and
496 C<lmstudio> L<Langertha::Engine::LMStudio>. Every dialect of
497 L<Langertha::Manifest::Endpoint/known_dialects> has a class, and a test holds
498 the round trip: C<dialect_for_engine> of that class gives the dialect back.
499  
500 =head2 from_engine
501  
502 my $manifest = Langertha::Manifest::Builder->from_engine( $engine, %options );
503  
504 Shortcut: a builder with C<provider_id> / C<issuer> / C<extensions> from
505 C<%options>, one L</add_engine> with the rest, then L</manifest>.
506  
507 =head2 add_engine
508  
509 $builder->add_engine( $engine,
510 endpoint_id => 'chat', # default 'chat'
511 base_url => $public_url, # default $engine->url
512 models => [ ... ], # default: the engine's model (placeholders skipped)
513 auth => 'api_key', # or 'none'; default from the engine class
514 auth_id => 'api', # default 'api' (shared across engines)
515 dialect => 'openai-chat', # default from the engine family
516 );
517  
518 Adds one endpoint for the chat engine, its auth entry (if any) and one
519 model entry per model id. Croaks for a non-chat engine, for a chat engine
520 without a manifest dialect unless C<dialect> is given, for a model-less
521 engine unless C<models> is given, and on a duplicate endpoint id or
522 C<(model, endpoint)> pair. Atomic: on a croak the builder is unchanged.
523 Returns the builder.
524  
525 =head2 add_endpoint
526  
527 $builder->add_endpoint( id => ..., dialect => ..., base_url => ..., auth_ref => ... );
528  
529 Adds an endpoint that is not an engine (a proxy's own protocol route).
530 Croaks on a duplicate id.
531  
532 =head2 add_auth
533  
534 $builder->add_auth( id => 'api', type => 'api_key' );
535  
536 Adds an auth entry. Croaks on a duplicate id.
537  
538 =head2 add_model
539  
540 $builder->add_model( id => ..., endpoint_ref => ..., capabilities => { ... } );
541  
542 Adds a model entry as given (no capability filtering: the caller states the
543 claim). Croaks on a duplicate C<(id, endpoint_ref)> pair.
544  
545 =head2 manifest
546  
547 Returns the validated L<Langertha::Manifest>.
548  
549 =head1 SEE ALSO
550  
551 =over
552  
553 =item * L<Langertha::Manifest> - The manifest value object and validator
554  
555 =item * L<Langertha::Role::Capabilities> - Where the capability names come from
556  
557 =back
558  
559 =head1 SUPPORT
560  
561 =head2 Issues
562  
563 Please report bugs and feature requests on GitHub at
564 L<https://github.com/Getty/langertha/issues>.
565  
566 =head2 IRC
567  
568 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
569  
570 =head1 CONTRIBUTING
571  
572 Contributions are welcome! Please fork the repository and submit a pull request.
573  
574 =head1 AUTHOR
575  
576 Torsten Raudssus <getty@cpan.org>
577  
578 =head1 COPYRIGHT AND LICENSE
579  
580 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
581  
582 This is free software; you can redistribute it and/or modify it under
583 the same terms as the Perl 5 programming language system itself.
584  
585 =cut
« blib/lib/Langertha/Role/Chat.pm ↑ Summary blib/lib/Langertha/Role/ReasoningEffort.pm »