blib/lib/Langertha/Knarr/Handler/RequestLog.pm

filter: Statementstmt 92.9% 66 / 71 Branchbran 77.7% 14 / 18 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 3 totaltotal 84.0% 100 / 119 CC 13 SCAR 11.7
CC
13
Cov
88%
__ANON__
7.1
__ANON__
3.0
_open
2.1
CRAP 3.2
« blib/lib/Langertha/Knarr/Protocol/ACP.pm ↑ Summary blib/lib/Langertha/Knarr/PSGI/FakeReq.pm »
1 package Langertha::Knarr::Handler::RequestLog;
2 # ABSTRACT: Decorator handler that writes per-request JSON logs via Knarr::RequestLog
3 8 our $VERSION = '1.102';
4 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Langertha::Knarr::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Langertha::Knarr::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 with 'Langertha::Knarr::Handler';
11  
12  
13 # Wraps an inner handler with structured per-request logging. Same shape
14 # as Knarr::Handler::Tracing — opens a log handle on each request and
15 # closes it with the assistant text once the inner handler resolves.
16  
17 8 has wrapped => (
18 is => 'ro',
19 required => 1,
20 );
21  
22 # Anything implementing start_request(%opts) → $handle / end_request($handle, %opts).
23 # In production this is a Langertha::Knarr::RequestLog instance; tests can
24 # pass a mock that records calls.
25 8 has request_log => (
26 is => 'ro',
27 required => 1,
28 );
29  
30 46 ▶sub _open {
Subroutine _open: called CC=2 SCAR=7.2
31 46 my ($self, $request) = @_;
32 46 ▶ return $self->request_log->start_request(
Condition50%1/2
$request->model // ""
l!l
460
MC/DC0%0/1
$request->model // ""
$request->model
Truth table50%1/2
A
A= $request->model
Aresult
11
00
33 model => ( $request->model // '' ),
34 format => $request->protocol,
35 engine => ref( $self->wrapped ),
36 path => '',
37 stream => $request->stream,
38 messages => $request->messages,
39 params => {
40 temperature => $request->temperature,
41 max_tokens => $request->max_tokens,
42 tools => $request->tools,
43 },
44 );
45 }
46  
47 18 ▶async sub handle_chat_f {
Subroutine handle_chat_f: called CC=2 SCAR=7.0
Pod undocumented
48 18 my ($self, $session, $request) = @_;
49 18 my $handle = $self->_open($request);
50 18 my $f = eval { $self->wrapped->handle_chat_f( $session, $request ) };
51 18 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 18 18
52 0 $self->request_log->end_request( $handle, error => "$err" );
53 0 die $err;
54 }
55 5 ▶ return await $f->then( sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
56 5 my ($r) = @_;
57 5 my $resp = Langertha::Knarr::Response->coerce($r);
58 5 ▶ $self->request_log->end_request(
Branch: 2 branches
BranchTrueFalseTotal
$resp->usage ? : 1 4 5
$resp->has_tool_calls ? : 1 4 5
59 $handle,
60 output => $resp->content,
61 ( $resp->usage ? ( usage => $resp->usage ) : () ),
62 ( $resp->has_tool_calls ? ( tool_calls => $resp->tool_calls ) : () ),
63 );
64 5 return Future->done($r);
65 13 ▶ })->else( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
66 13 my ($err) = @_;
67 13 $self->request_log->end_request( $handle, error => "$err" );
68 # The whole failure: a timeout's category rides along (k36).
69 13 return Future->fail(@_);
70 18 });
71 8 }
72  
73 28 ▶async sub handle_stream_f {
Subroutine handle_stream_f: called CC=2 SCAR=7.0
Pod undocumented
74 28 my ($self, $session, $request) = @_;
75 28 my $handle = $self->_open($request);
76  
77 28 my $upstream;
78 28 my $err = do {
79 28 local $@;
80 28 eval { $upstream = $self->wrapped->handle_stream_f( $session, $request )->get; };
81 28 $@;
82 };
83 28 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 28 28
84 0 $self->request_log->end_request( $handle, error => "$err" );
85 0 die $err;
86 }
87  
88 28 my $accumulated = '';
89 28 my $closed = 0;
90  
91 return Langertha::Knarr::Stream->new(
92 upstream => $upstream,
93 46 ▶ source => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
94 21 ▶ $upstream->next_chunk_f->then( sub {
Subroutine __ANON__: called CC=7 SCAR=19.6
95 21 my ($delta) = @_;
96 21 ▶ if ( defined $delta ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $delta) 18 3 21
97 18 $accumulated .= $delta;
98 18 return Future->done($delta);
99 }
100 3 ▶ unless ( $closed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($closed) 3 0 3
101 3 $closed = 1;
102 # The complete tool calls, known once the stream is exhausted (k19).
103 3 ▶ $self->request_log->end_request(
Branch: 2 branches
BranchTrueFalseTotal
$upstream->can('has_tool_calls') && $upstream->has_tool_calls ? : 1 2 3
$upstream->can('usage') && $upstream->usage ? : 1 2 3
Condition66%4/6
$upstream->can("has_tool_calls") and $upstream->has_tool_calls
!ll&&!rl&&r
021
$upstream->can("usage") and $upstream->usage
!ll&&!rl&&r
021
MC/DC50%1/2
$upstream->can("has_tool_calls") and $upstream->has_tool_calls
$upstream->can("has_tool_calls") $upstream->has_tool_calls
MC/DC50%1/2
$upstream->can("usage") and $upstream->usage
$upstream->can("usage") $upstream->usage
Truth table66%2/3
A and B
A= $upstream->can("has_tool_calls")B= $upstream->has_tool_calls
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= $upstream->can("usage")B= $upstream->usage
ABresult
0X0
100
111
104 $handle,
105 output => $accumulated,
106 ( $upstream->can('has_tool_calls') && $upstream->has_tool_calls
107 ? ( tool_calls => $upstream->tool_calls ) : () ),
108 ( $upstream->can('usage') && $upstream->usage
109 ? ( usage => $upstream->usage ) : () ),
110 );
111 }
112 3 return Future->done(undef);
113 25 ▶ })->else( sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
114 25 my ($e) = @_;
115 25 ▶ unless ( $closed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($closed) 25 0 25
116 25 $closed = 1;
117 25 $self->request_log->end_request( $handle, error => "$e" );
118 }
119 25 return Future->fail(@_);
120 46 });
121 },
122 28 );
123 8 }
124  
125 0 ▶sub list_models { $_[0]->wrapped->list_models }
Subroutine list_models: not called CC=1 SCAR=6.9
Pod undocumented
126  
127 8 __PACKAGE__->meta->make_immutable;
128 8 1;
129  
130 __END__
131  
132 =pod
133  
134 =encoding UTF-8
135  
136 =head1 NAME
137  
138 Langertha::Knarr::Handler::RequestLog - Decorator handler that writes per-request JSON logs via Knarr::RequestLog
139  
140 =head1 VERSION
141  
142 version 1.102
143  
144 =head1 SYNOPSIS
145  
146 use Langertha::Knarr::RequestLog;
147 use Langertha::Knarr::Handler::RequestLog;
148  
149 my $rlog = Langertha::Knarr::RequestLog->new(config => $config);
150 $handler = Langertha::Knarr::Handler::RequestLog->new(
151 wrapped => $handler,
152 request_log => $rlog,
153 );
154  
155 =head1 DESCRIPTION
156  
157 Decorator handler that writes a structured per-request log entry for
158 every chat or stream request via L<Langertha::Knarr::RequestLog>.
159 Sync requests log a single line with the result; streaming requests
160 accumulate every delta and log one line with the assembled output
161 when the stream closes.
162  
163 C<knarr start> mounts this automatically when
164 C<KNARR_LOG_FILE> / C<KNARR_LOG_DIR> (or the YAML C<logging:> section)
165 is set.
166  
167 =head2 wrapped
168  
169 Required. The inner L<Langertha::Knarr::Handler> being decorated.
170  
171 =head2 request_log
172  
173 Required. A L<Langertha::Knarr::RequestLog> instance (or any object
174 implementing C<start_request> / C<end_request>).
175  
176 =head1 SUPPORT
177  
178 =head2 Issues
179  
180 Please report bugs and feature requests on GitHub at
181 L<https://github.com/Getty/langertha-knarr/issues>.
182  
183 =head2 IRC
184  
185 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
186  
187 =head1 CONTRIBUTING
188  
189 Contributions are welcome! Please fork the repository and submit a pull request.
190  
191 =head1 AUTHOR
192  
193 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
194  
195 =head1 COPYRIGHT AND LICENSE
196  
197 This software is copyright (c) 2026 by Torsten Raudssus.
198  
199 This is free software; you can redistribute it and/or modify it under
200 the same terms as the Perl 5 programming language system itself.
201  
202 =cut
« blib/lib/Langertha/Knarr/Protocol/ACP.pm ↑ Summary blib/lib/Langertha/Knarr/PSGI/FakeReq.pm »