blib/lib/Langertha/Knarr/Request.pm

filter: Statementstmt 100% 69 / 69 Branchbran 100% 26 / 26 Conditioncond 92.6% 38 / 41 MC/DCmcdc 88.4% 23 / 26 Subroutinesub 100% 22 / 22 Podpod 100% 3 / 3 totaltotal 96.7% 181 / 187 CC 28 SCAR 23.8
CC
28
Cov
98%
chat_f_args
22.0
_response_format_cap
4.0
forward_header_pairs
3.0
CRAP 10.8
« blib/lib/Langertha/Knarr/Manifest.pm ↑ Summary blib/lib/Langertha/Knarr.pm »
1 package Langertha::Knarr::Request;
2 # ABSTRACT: Normalized chat request shared across all Knarr protocols
3 61 our $VERSION = '1.102';
4 61 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 has model => (
8 is => 'ro',
9 isa => 'Maybe[Str]',
10 58 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
11 61 );
12  
13 has messages => (
14 is => 'ro',
15 isa => 'ArrayRef[HashRef]',
16 38 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 61 );
18  
19 61 has stream => (
20 is => 'ro',
21 isa => 'Bool',
22 default => 0,
23 );
24  
25 has temperature => (
26 is => 'ro',
27 isa => 'Maybe[Num]',
28 239 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 61 );
30  
31 has max_tokens => (
32 is => 'ro',
33 isa => 'Maybe[Int]',
34 446 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 61 );
36  
37 has reasoning_effort => (
38 is => 'ro',
39 isa => 'Maybe[Str]',
40 242 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 61 );
42  
43 has seed => (
44 is => 'ro',
45 isa => 'Maybe[Int]',
46 456 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 61 );
48  
49 has parallel_tool_use => (
50 is => 'ro',
51 isa => 'Maybe[Bool]',
52 662 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
53 61 );
54  
55 has prompt_cache_key => (
56 is => 'ro',
57 isa => 'Maybe[Str]',
58 663 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 61 );
60  
61 has tools => (
62 is => 'ro',
63 isa => 'Maybe[ArrayRef]',
64 238 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
65 61 );
66  
67 has tool_choice => (
68 is => 'ro',
69 445 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
70 61 );
71  
72 has response_format => (
73 is => 'ro',
74 451 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
75 61 );
76  
77 has system => (
78 is => 'ro',
79 isa => 'Maybe[Str]',
80 715 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
81 61 );
82  
83 has session_id => (
84 is => 'rw',
85 isa => 'Maybe[Str]',
86 587 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
87 61 );
88  
89 61 has protocol => (
90 is => 'ro',
91 isa => 'Str',
92 required => 1,
93 );
94  
95 has raw => (
96 is => 'ro',
97 isa => 'HashRef',
98 105 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
99 61 );
100  
101 has extra => (
102 is => 'ro',
103 isa => 'HashRef',
104 112 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
105 61 );
106  
107  
108 846 ▶sub forward_header_pairs {
Subroutine forward_header_pairs: called CC=3 SCAR=11.0
Pod documented
109 846 my ($self) = @_;
110 846 my $fwd = $self->extra->{forward_headers};
111 846 ▶ return map { [ @$_ ] } @$fwd if ref $fwd eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $fwd eq "ARRAY" 835 11 846
112 11 ▶ return map { [ $_, $fwd->{$_} ] } sort keys %$fwd if ref $fwd eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $fwd eq "HASH" 3 8 11
113 8 return;
114 }
115  
116  
117 127 ▶sub forward_header {
Subroutine forward_header: called CC=2 SCAR=7.0
Pod documented
118 127 my ($self, $name) = @_;
119 60 return map { $_->[1] }
120 127 ▶ grep { lc $_->[0] eq lc $name && defined $_->[1] } $self->forward_header_pairs;
Condition66%2/3
lc $_->[0] eq lc $name && defined $_->[1]
!ll&&!rl&&r
33060
MC/DC50%1/2
lc $_->[0] eq lc $name && defined $_->[1]
lc $_->[0] eq lc $name defined $_->[1]
Truth table66%2/3
A && B
A= lc $_->[0] eq lc $nameB= defined $_->[1]
ABresult
0X0
100
111
121 }
122  
123  
124 # Which capability flag a given response_format value requires. Only an
125 # explicit json_schema type needs the schema flag; json_object, Ollama's
126 # bare 'json' string and any raw-schema HashRef fall back to the object
127 # flag, which is the weaker of the two.
128 7 ▶sub _response_format_cap {
Subroutine _response_format_cap: called CC=4 SCAR=13.9
129 7 my ($rf) = @_;
130 return 'response_format_json_schema'
131 7 ▶ if ref($rf) eq 'HASH' && ( $rf->{type} // '' ) eq 'json_schema';
Branch: 1 branch
BranchTrueFalseTotal
if ref $rf eq "HASH" and ($rf->{"type"} // "") eq "json_schema" 2 5 7
Condition80%4/5
ref $rf eq "HASH" and ($rf->{"type"} // "") eq "json_schema"
!ll&&!rl&&r
232
$rf->{"type"} // ""
l!l
50
MC/DC50%1/2
ref $rf eq "HASH" and ($rf->{"type"} // "") eq "json_schema"
ref $rf eq "HASH" $rf->{"type"}
Truth table66%2/3
A and B
A= ref $rf eq "HASH"B= $rf->{"type"}
ABresult
0X0
100
111
132 5 return 'response_format_json_object';
133 }
134  
135 188 ▶sub chat_f_args {
Subroutine chat_f_args: called CC=22 SCAR=30.9
Pod documented
136 188 my ($self, $engine) = @_;
137 my $supports = $engine && $engine->can('supports')
138 255 ▶ ? sub { $engine->supports($_[0]) }
Subroutine __ANON__: called CC=1 SCAR=0.0
139 188 ▶ : sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$engine && $engine->can('supports') ? : 124 64 188
Condition66%2/3
$engine and $engine->can("supports")
!ll&&!rl&&r
064124
MC/DC50%1/2
$engine and $engine->can("supports")
$engine $engine->can("supports")
Truth table66%2/3
A and B
A= $engineB= $engine->can("supports")
ABresult
0X0
100
111
140 188 my $rf = $self->response_format;
141 188 ▶ my $tools_ok = $supports->('tools_native') || $supports->('tools_hermes');
Condition100%3/3
&$supports('tools_native') || &$supports('tools_hermes')
l!l&&r!l&&!r
110177
MC/DC100%2/2
&$supports('tools_native') || &$supports('tools_hermes')
&$supports('tools_native') &$supports('tools_hermes')
Truth table100%3/3
A || B
A= &$supports('tools_native')B= &$supports('tools_hermes')
ABresult
1X1
011
000
142 188 my @args = ( messages => $self->messages );
143 188 ▶ push @args, tools => $self->tools if $self->tools && $tools_ok;
Branch: 1 branch
BranchTrueFalseTotal
if $self->tools and $tools_ok 4 184 188
Condition100%3/3
$self->tools and $tools_ok
!ll&&!rl&&r
18224
MC/DC100%2/2
$self->tools and $tools_ok
$self->tools $tools_ok
Truth table100%3/3
A and B
A= $self->toolsB= $tools_ok
ABresult
0X0
100
111
144 188 ▶ push @args, tool_choice => $self->tool_choice if defined $self->tool_choice && $tools_ok;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->tool_choice and $tools_ok 4 184 188
Condition100%3/3
defined $self->tool_choice and $tools_ok
!ll&&!rl&&r
18224
MC/DC100%2/2
defined $self->tool_choice and $tools_ok
defined $self->tool_choice $tools_ok
Truth table100%3/3
A and B
A= defined $self->tool_choiceB= $tools_ok
ABresult
0X0
100
111
145 188 ▶ push @args, response_format => $rf if defined $rf && $supports->( _response_format_cap($rf) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rf and &$supports(_response_format_cap($rf)) 3 185 188
Condition100%3/3
defined $rf and &$supports(_response_format_cap($rf))
!ll&&!rl&&r
18143
MC/DC100%2/2
defined $rf and &$supports(_response_format_cap($rf))
defined $rf &$supports(_response_format_cap($rf))
Truth table100%3/3
A and B
A= defined $rfB= &$supports(_response_format_cap($rf))
ABresult
0X0
100
111
146 188 ▶ push @args, temperature => $self->temperature if defined $self->temperature && $supports->('temperature');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->temperature and &$supports("temperature") 4 184 188
Condition100%3/3
defined $self->temperature and &$supports("temperature")
!ll&&!rl&&r
18314
MC/DC100%2/2
defined $self->temperature and &$supports("temperature")
defined $self->temperature &$supports("temperature")
Truth table100%3/3
A and B
A= defined $self->temperatureB= &$supports("temperature")
ABresult
0X0
100
111
147 188 ▶ push @args, max_tokens => $self->max_tokens if defined $self->max_tokens && $supports->('response_size');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->max_tokens and &$supports("response_size") 36 152 188
Condition100%3/3
defined $self->max_tokens and &$supports("response_size")
!ll&&!rl&&r
1361636
MC/DC100%2/2
defined $self->max_tokens and &$supports("response_size")
defined $self->max_tokens &$supports("response_size")
Truth table100%3/3
A and B
A= defined $self->max_tokensB= &$supports("response_size")
ABresult
0X0
100
111
148 188 ▶ push @args, reasoning_effort => $self->reasoning_effort if defined $self->reasoning_effort && $supports->('reasoning_effort');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->reasoning_effort and &$supports("reasoning_effort") 2 186 188
Condition100%3/3
defined $self->reasoning_effort and &$supports("reasoning_effort")
!ll&&!rl&&r
18422
MC/DC100%2/2
defined $self->reasoning_effort and &$supports("reasoning_effort")
defined $self->reasoning_effort &$supports("reasoning_effort")
Truth table100%3/3
A and B
A= defined $self->reasoning_effortB= &$supports("reasoning_effort")
ABresult
0X0
100
111
149 188 ▶ push @args, seed => $self->seed if defined $self->seed && $supports->('seed');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->seed and &$supports("seed") 1 187 188
Condition100%3/3
defined $self->seed and &$supports("seed")
!ll&&!rl&&r
18611
MC/DC100%2/2
defined $self->seed and &$supports("seed")
defined $self->seed &$supports("seed")
Truth table100%3/3
A and B
A= defined $self->seedB= &$supports("seed")
ABresult
0X0
100
111
150 188 ▶ push @args, parallel_tool_use => $self->parallel_tool_use if defined $self->parallel_tool_use && $supports->('parallel_tool_use');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->parallel_tool_use and &$supports("parallel_tool_use") 2 186 188
Condition100%3/3
defined $self->parallel_tool_use and &$supports("parallel_tool_use")
!ll&&!rl&&r
18512
MC/DC100%2/2
defined $self->parallel_tool_use and &$supports("parallel_tool_use")
defined $self->parallel_tool_use &$supports("parallel_tool_use")
Truth table100%3/3
A and B
A= defined $self->parallel_tool_useB= &$supports("parallel_tool_use")
ABresult
0X0
100
111
151 188 ▶ push @args, prompt_cache_key => $self->prompt_cache_key if defined $self->prompt_cache_key && $supports->('prompt_cache_key');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->prompt_cache_key and &$supports("prompt_cache_key") 1 187 188
Condition100%3/3
defined $self->prompt_cache_key and &$supports("prompt_cache_key")
!ll&&!rl&&r
18611
MC/DC100%2/2
defined $self->prompt_cache_key and &$supports("prompt_cache_key")
defined $self->prompt_cache_key &$supports("prompt_cache_key")
Truth table100%3/3
A and B
A= defined $self->prompt_cache_keyB= &$supports("prompt_cache_key")
ABresult
0X0
100
111
152 188 return @args;
153 }
154  
155 61 __PACKAGE__->meta->make_immutable;
156 61 1;
157  
158 __END__
159  
160 =pod
161  
162 =encoding UTF-8
163  
164 =head1 NAME
165  
166 Langertha::Knarr::Request - Normalized chat request shared across all Knarr protocols
167  
168 =head1 VERSION
169  
170 version 1.102
171  
172 =head1 DESCRIPTION
173  
174 The normalized request shape that every L<Langertha::Knarr::Protocol>
175 parser produces and every L<Langertha::Knarr::Handler> receives.
176 Wire-protocol-specific quirks (OpenAI's C<choices>, Anthropic's
177 C<system> outside C<messages>, A2A's JSON-RPC envelope, etc.) are
178 handled by the protocol's C<parse_chat_request> and don't leak into
179 the handler API.
180  
181 The original wire-format body is preserved in L</raw> for handlers
182 (like L<Langertha::Knarr::Handler::Passthrough>) that need to forward
183 it verbatim.
184  
185 =head2 protocol
186  
187 Required. Short string identifying the parser that produced this
188 request: C<openai>, C<anthropic>, C<ollama>, C<a2a>, C<acp>, C<agui>.
189  
190 =head2 model
191  
192 Optional model id from the request body.
193  
194 =head2 messages
195  
196 ArrayRef of message hashes (C<< { role => ..., content => ... } >>).
197  
198 =head2 stream
199  
200 Boolean. Whether the client requested streaming.
201  
202 =head2 temperature, max_tokens, reasoning_effort, seed, parallel_tool_use, prompt_cache_key, tools, tool_choice, response_format, system
203  
204 Optional generation parameters and tool definitions, if the protocol
205 extracted them. C<tool_choice> and C<response_format> are passed to
206 L<Langertha::Engine> via C<chat_f> in their canonical form; Langertha
207 normalizes them to the target engine's wire format. C<reasoning_effort>
208 is the per-request reasoning effort (e.g. C<low>/C<medium>/C<high>),
209 capability-gated like the other generation parameters. The OpenAI face
210 carries it natively; the Anthropic face's C<thinking> and the Ollama face's
211 C<think> are mapped onto it by L<Langertha::Knarr::Reasoning>.
212  
213 C<seed>, C<parallel_tool_use> and C<prompt_cache_key> are further
214 per-request controls, each extracted by a protocol parser only where the
215 inbound wire format actually carries the field (OpenAI's top-level
216 C<seed> / C<parallel_tool_calls> / C<prompt_cache_key>, and Ollama's
217 C<options.seed>) and each capability-gated in L</chat_f_args>. Knarr only
218 validates and forwards them; Langertha does the honoring and places each
219 on the target engine's own wire.
220  
221 =head2 session_id
222  
223 Optional session id, used for per-session state. Pulled from
224 protocol-specific fields (e.g. OpenAI's C<user>, A2A's C<sessionId>,
225 or the C<x-session-id> header).
226  
227 =head2 raw
228  
229 The original decoded request body. Useful for passthrough handlers
230 that need to forward without re-encoding.
231  
232 =head2 extra
233  
234 Per-protocol scratch space (e.g. JSON-RPC id for A2A, run_id for ACP).
235 The Ollama parser records the path the client asked for as C<path>
236 (C</api/chat> or C</api/generate>): it picks the answer's shape, and
237 L<Langertha::Knarr::Handler::Passthrough> sends the request to the same path
238 upstream.
239  
240 The OpenAI, Anthropic and Ollama parsers record the client's auth headers
241 as C<forward_headers>, an ArrayRef of C<[ name, value ]> pairs with one pair
242 per header line the client sent, in its order (a header sent twice is two
243 pairs). L<Langertha::Knarr> takes its own proxy key out of every pair;
244 L<Langertha::Knarr::Handler::Passthrough> sends each pair upstream as a line
245 of its own. Read them with L</forward_header_pairs> and L</forward_header>.
246  
247 =head2 chat_f_args
248  
249 my @args = $request->chat_f_args($engine);
250 my $r = await $engine->chat_f(@args);
251  
252 Builds a named-argument list suitable for L<Langertha::Role::Chat/chat_f>.
253 Always includes C<messages>; conditionally adds C<tools>, C<tool_choice>,
254 C<response_format>, C<temperature>, C<max_tokens>, C<reasoning_effort>,
255 C<seed>, C<parallel_tool_use>, C<prompt_cache_key> when set on the request
256 B<and> the engine reports support for the matching capability via
257 C<< $engine->supports($cap) >>. Engines without C<supports()> get every
258 defined parameter — older Langertha versions accepted unknown args
259 silently.
260  
261 Per-request generation controls are handed to C<chat_f> as canonical
262 named arguments; Langertha extracts them as controls and places each on
263 the target engine's own wire (top-level C<reasoning_effort> on OpenAI,
264 C<output_config> plus C<thinking> on Anthropic, C<seed> under Ollama's
265 C<options>, OpenAI's C<parallel_tool_calls> / C<prompt_cache_key>, and so
266 on). The capability gate is strict: a control is forwarded only to an
267 engine whose wire actually advertises it, so e.g. a C<seed> is dropped
268 onto an engine that does not compose C<Langertha::Role::Seed> even where
269 its wire would technically accept the field.
270  
271 C<response_format> is gated on the capability matching the I<kind> of
272 format requested, because Langertha registers the two separately
273 (L<Langertha::Role::Capabilities>): an explicit
274 C<< { type => 'json_schema' } >> needs C<response_format_json_schema>,
275 while everything else — OpenAI's C<< { type => 'json_object' } >> and
276 Ollama's bare C<'json'> — needs only C<response_format_json_object>.
277 Gating both on the schema flag would drop a plain C<json_object>
278 request on an engine that can only do the loose form.
279  
280 C<tools> and C<tool_choice> are forwarded when the engine supports
281 either C<tools_native> or C<tools_hermes>. Hermes-wire engines
282 (NousResearch, AKI native) carry tools in the system prompt rather than
283 as a native C<tools> field, and Langertha's C<chat_f> renders them there
284 and handles C<tool_choice> per wire, so gating on C<tools_native> alone
285 would silently drop a client's tools on those backends.
286  
287 =head2 forward_header_pairs
288  
289 for my $pair ( $request->forward_header_pairs ) {
290 my ($name, $value) = @$pair;
291 ...
292 }
293  
294 The client headers recorded in C<< extra->{forward_headers} >> as a list of
295 C<[ name, value ]> pairs, in their order, repeats kept (copies: changing one
296 leaves the request alone). A C<forward_headers> hash, as a request built by
297 hand may carry, reads as one pair per key, sorted by name. Empty when the
298 request has none.
299  
300 =head2 forward_header
301  
302 my @values = $request->forward_header('authorization');
303  
304 Every value of one forwarded header, the name matched without regard to
305 case, in the order the client sent them. Empty when there is none.
306  
307 =head1 SUPPORT
308  
309 =head2 Issues
310  
311 Please report bugs and feature requests on GitHub at
312 L<https://github.com/Getty/langertha-knarr/issues>.
313  
314 =head2 IRC
315  
316 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
317  
318 =head1 CONTRIBUTING
319  
320 Contributions are welcome! Please fork the repository and submit a pull request.
321  
322 =head1 AUTHOR
323  
324 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
325  
326 =head1 COPYRIGHT AND LICENSE
327  
328 This software is copyright (c) 2026 by Torsten Raudssus.
329  
330 This is free software; you can redistribute it and/or modify it under
331 the same terms as the Perl 5 programming language system itself.
332  
333 =cut
« blib/lib/Langertha/Knarr/Manifest.pm ↑ Summary blib/lib/Langertha/Knarr.pm »