Knarr/Request.pm

filter: Statementstmt 97.5% 40 / 41 Branchbran 100% 12 / 12 Conditioncond 93.3% 14 / 15 MC/DCmcdc 90.0% 9 / 10 Subroutinesub 93.3% 14 / 15 Podpod 100% 1 / 1 totaltotal 95.7% 90 / 94 CC 12 SCAR 18.1
CC
12
Cov
97%
chat_f_args
12.0
__ANON__
1.0
BEGIN
1.0
CRAP 6.1
« Knarr/Handler/Router.pm ↑ Summary Knarr/Handler/Engine.pm »
1 package Langertha::Knarr::Request;
2 # ABSTRACT: Normalized chat request shared across all Knarr protocols
3 18 our $VERSION = '1.100';
4 18 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 has model => (
8 is => 'ro',
9 isa => 'Maybe[Str]',
10 7 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
11 18 );
12  
13 has messages => (
14 is => 'ro',
15 isa => 'ArrayRef[HashRef]',
16 7 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 18 );
18  
19 18 has stream => (
20 is => 'ro',
21 isa => 'Bool',
22 default => 0,
23 );
24  
25 has temperature => (
26 is => 'ro',
27 isa => 'Maybe[Num]',
28 27 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 18 );
30  
31 has max_tokens => (
32 is => 'ro',
33 isa => 'Maybe[Int]',
34 34 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 18 );
36  
37 has tools => (
38 is => 'ro',
39 isa => 'Maybe[ArrayRef]',
40 27 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 18 );
42  
43 has tool_choice => (
44 is => 'ro',
45 34 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
46 18 );
47  
48 has response_format => (
49 is => 'ro',
50 34 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
51 18 );
52  
53 has system => (
54 is => 'ro',
55 isa => 'Maybe[Str]',
56 59 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 18 );
58  
59 has session_id => (
60 is => 'rw',
61 isa => 'Maybe[Str]',
62 37 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
63 18 );
64  
65 18 has protocol => (
66 is => 'ro',
67 isa => 'Str',
68 required => 1,
69 );
70  
71 has raw => (
72 is => 'ro',
73 isa => 'HashRef',
74 21 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
75 18 );
76  
77 has extra => (
78 is => 'ro',
79 isa => 'HashRef',
80 28 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
81 18 );
82  
83  
84 sub chat_f_args {
85 6 ▶ my ($self, $engine) = @_;
Subroutine chat_f_args: called CC=12 SCAR=24.9
Pod documented
86 my $supports = $engine && $engine->can('supports')
87 8 ▶ ? sub { $engine->supports($_[0]) }
Subroutine __ANON__: called CC=1 SCAR=0.0
88 6 ▶ : sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
$engine && $engine->can('supports') ? : 3 3 6
Condition66%2/3
$engine and $engine->can("supports")
!ll&&!rl&&r
033
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
89 6 my @args = ( messages => $self->messages );
90 6 ▶ push @args, tools => $self->tools if $self->tools && $supports->('tools_native');
Branch: 1 branch
BranchTrueFalseTotal
if $self->tools and &$supports("tools_native") 1 5 6
Condition100%3/3
$self->tools and &$supports("tools_native")
!ll&&!rl&&r
411
MC/DC100%2/2
$self->tools and &$supports("tools_native")
$self->tools &$supports("tools_native")
Truth table100%3/3
A and B
A= $self->toolsB= &$supports("tools_native")
ABresult
0X0
100
111
91 6 ▶ push @args, tool_choice => $self->tool_choice if defined $self->tool_choice && $supports->('tools_native');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->tool_choice and &$supports("tools_native") 1 5 6
Condition100%3/3
defined $self->tool_choice and &$supports("tools_native")
!ll&&!rl&&r
411
MC/DC100%2/2
defined $self->tool_choice and &$supports("tools_native")
defined $self->tool_choice &$supports("tools_native")
Truth table100%3/3
A and B
A= defined $self->tool_choiceB= &$supports("tools_native")
ABresult
0X0
100
111
92 6 ▶ push @args, response_format => $self->response_format if defined $self->response_format;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->response_format 1 5 6
93 6 ▶ push @args, temperature => $self->temperature if defined $self->temperature && $supports->('temperature');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->temperature and &$supports("temperature") 1 5 6
Condition100%3/3
defined $self->temperature and &$supports("temperature")
!ll&&!rl&&r
411
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
94 6 ▶ 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") 1 5 6
Condition100%3/3
defined $self->max_tokens and &$supports("response_size")
!ll&&!rl&&r
411
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
95 6 return @args;
96 }
97  
98 18 __PACKAGE__->meta->make_immutable;
99 18 1;
100  
101 __END__
102  
103 =pod
104  
105 =encoding UTF-8
106  
107 =head1 NAME
108  
109 Langertha::Knarr::Request - Normalized chat request shared across all Knarr protocols
110  
111 =head1 VERSION
112  
113 version 1.100
114  
115 =head1 DESCRIPTION
116  
117 The normalized request shape that every L<Langertha::Knarr::Protocol>
118 parser produces and every L<Langertha::Knarr::Handler> receives.
119 Wire-protocol-specific quirks (OpenAI's C<choices>, Anthropic's
120 C<system> outside C<messages>, A2A's JSON-RPC envelope, etc.) are
121 handled by the protocol's C<parse_chat_request> and don't leak into
122 the handler API.
123  
124 The original wire-format body is preserved in L</raw> for handlers
125 (like L<Langertha::Knarr::Handler::Passthrough>) that need to forward
126 it verbatim.
127  
128 =head2 protocol
129  
130 Required. Short string identifying the parser that produced this
131 request: C<openai>, C<anthropic>, C<ollama>, C<a2a>, C<acp>, C<agui>.
132  
133 =head2 model
134  
135 Optional model id from the request body.
136  
137 =head2 messages
138  
139 ArrayRef of message hashes (C<< { role => ..., content => ... } >>).
140  
141 =head2 stream
142  
143 Boolean. Whether the client requested streaming.
144  
145 =head2 temperature, max_tokens, tools, tool_choice, response_format, system
146  
147 Optional generation parameters and tool definitions, if the protocol
148 extracted them. C<tool_choice> and C<response_format> are passed to
149 L<Langertha::Engine> via C<chat_f> in their canonical form; Langertha
150 normalizes them to the target engine's wire format.
151  
152 =head2 session_id
153  
154 Optional session id, used for per-session state. Pulled from
155 protocol-specific fields (e.g. OpenAI's C<user>, A2A's C<sessionId>,
156 or the C<x-session-id> header).
157  
158 =head2 raw
159  
160 The original decoded request body. Useful for passthrough handlers
161 that need to forward without re-encoding.
162  
163 =head2 extra
164  
165 Per-protocol scratch space (e.g. JSON-RPC id for A2A, run_id for ACP).
166  
167 =head2 chat_f_args
168  
169 my @args = $request->chat_f_args($engine);
170 my $r = await $engine->chat_f(@args);
171  
172 Builds a named-argument list suitable for L<Langertha::Role::Chat/chat_f>.
173 Always includes C<messages>; conditionally adds C<tools>, C<tool_choice>,
174 C<response_format>, C<temperature>, C<max_tokens> when set on the request
175 B<and> the engine reports support for the matching capability via
176 C<< $engine->supports($cap) >>. Engines without C<supports()> get every
177 defined parameter — older Langertha versions accepted unknown args
178 silently.
179  
180 =head1 SUPPORT
181  
182 =head2 Issues
183  
184 Please report bugs and feature requests on GitHub at
185 L<https://github.com/Getty/langertha-knarr/issues>.
186  
187 =head2 IRC
188  
189 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
190  
191 =head1 CONTRIBUTING
192  
193 Contributions are welcome! Please fork the repository and submit a pull request.
194  
195 =head1 AUTHOR
196  
197 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
198  
199 =head1 COPYRIGHT AND LICENSE
200  
201 This software is copyright (c) 2026 by Torsten Raudssus.
202  
203 This is free software; you can redistribute it and/or modify it under
204 the same terms as the Perl 5 programming language system itself.
205  
206 =cut
« Knarr/Handler/Router.pm ↑ Summary Knarr/Handler/Engine.pm »