| 1 | package Langertha::Knarr::Handler::RequestLog; | ||||||||||||
| 2 | # ABSTRACT: Decorator handler that writes per-request JSON logs via Knarr::RequestLog | ||||||||||||
| 3 | 2 | our $VERSION = '1.100'; | |||||||||||
| 4 | 2 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use Future; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use Future::AsyncAwait; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use Langertha::Knarr::Stream; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use Langertha::Knarr::Response; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 2 | 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 | 2 | 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 | 2 | has request_log => ( | |||||||||||
| 26 | is => 'ro', | ||||||||||||
| 27 | required => 1, | ||||||||||||
| 28 | ); | ||||||||||||
| 29 | |||||||||||||
| 30 | sub _open { | ||||||||||||
| 31 | 2 | ▶ | my ($self, $request) = @_; | ||||||||||
|
Subroutine
_open: called CC=2 SCAR=7.2
| |||||||||||||
| 32 | 2 | ▶ | return $self->request_log->start_request( | ||||||||||
|
Condition50%1/2
$request->model // ""
MC/DC0%0/1
$request->model // ""
$request->model
Truth table50%1/2
A
A= $request->model
| |||||||||||||
| 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 | 1 | ▶ | async sub handle_chat_f { | ||||||||||
|
Subroutine
handle_chat_f: called CC=2 SCAR=7.8
Pod
undocumented
| |||||||||||||
| 48 | 1 | my ($self, $session, $request) = @_; | |||||||||||
| 49 | 1 | my $handle = $self->_open($request); | |||||||||||
| 50 | 1 | my $f = eval { $self->wrapped->handle_chat_f( $session, $request ) }; | |||||||||||
| 51 | 1 | ▶ | if ( my $err = $@ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | 0 | $self->request_log->end_request( $handle, error => "$err" ); | |||||||||||
| 53 | 0 | die $err; | |||||||||||
| 54 | } | ||||||||||||
| 55 | return await $f->then( sub { | ||||||||||||
| 56 | 1 | ▶ | my ($r) = @_; | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||
| 57 | 1 | my $resp = Langertha::Knarr::Response->coerce($r); | |||||||||||
| 58 | 1 | ▶ | $self->request_log->end_request( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | $handle, | ||||||||||||
| 60 | output => $resp->content, | ||||||||||||
| 61 | ( $resp->usage ? ( usage => $resp->usage ) : () ), | ||||||||||||
| 62 | ); | ||||||||||||
| 63 | 1 | return Future->done($r); | |||||||||||
| 64 | })->else( sub { | ||||||||||||
| 65 | 0 | ▶ | my ($err) = @_; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 66 | 0 | $self->request_log->end_request( $handle, error => "$err" ); | |||||||||||
| 67 | 0 | return Future->fail($err); | |||||||||||
| 68 | 1 | }); | |||||||||||
| 69 | 2 | } | |||||||||||
| 70 | |||||||||||||
| 71 | 1 | ▶ | async sub handle_stream_f { | ||||||||||
|
Subroutine
handle_stream_f: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||
| 72 | 1 | my ($self, $session, $request) = @_; | |||||||||||
| 73 | 1 | my $handle = $self->_open($request); | |||||||||||
| 74 | |||||||||||||
| 75 | 1 | my $upstream; | |||||||||||
| 76 | 1 | my $err = do { | |||||||||||
| 77 | 1 | local $@; | |||||||||||
| 78 | 1 | eval { $upstream = $self->wrapped->handle_stream_f( $session, $request )->get; }; | |||||||||||
| 79 | 1 | $@; | |||||||||||
| 80 | }; | ||||||||||||
| 81 | 1 | ▶ | if ($err) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | 0 | $self->request_log->end_request( $handle, error => "$err" ); | |||||||||||
| 83 | 0 | die $err; | |||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | 1 | my $accumulated = ''; | |||||||||||
| 87 | 1 | my $closed = 0; | |||||||||||
| 88 | |||||||||||||
| 89 | return Langertha::Knarr::Stream->new( | ||||||||||||
| 90 | source => sub { | ||||||||||||
| 91 | $upstream->next_chunk_f->then( sub { | ||||||||||||
| 92 | 4 | ▶ | my ($delta) = @_; | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||
| 93 | 4 | ▶ | if ( defined $delta ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | 3 | $accumulated .= $delta; | |||||||||||
| 95 | 3 | return Future->done($delta); | |||||||||||
| 96 | } | ||||||||||||
| 97 | 1 | ▶ | unless ( $closed ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 98 | 1 | $closed = 1; | |||||||||||
| 99 | 1 | $self->request_log->end_request( $handle, output => $accumulated ); | |||||||||||
| 100 | } | ||||||||||||
| 101 | 1 | return Future->done(undef); | |||||||||||
| 102 | })->else( sub { | ||||||||||||
| 103 | 0 | ▶ | my ($e) = @_; | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 104 | 0 | ▶ | unless ( $closed ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 105 | 0 | $closed = 1; | |||||||||||
| 106 | 0 | $self->request_log->end_request( $handle, error => "$e" ); | |||||||||||
| 107 | } | ||||||||||||
| 108 | 0 | return Future->fail($e); | |||||||||||
| 109 | 4 | ▶ | }); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 110 | }, | ||||||||||||
| 111 | 1 | ); | |||||||||||
| 112 | 2 | } | |||||||||||
| 113 | |||||||||||||
| 114 | 0 | ▶ | sub list_models { $_[0]->wrapped->list_models } | ||||||||||
|
Subroutine
list_models: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 115 | |||||||||||||
| 116 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 117 | 2 | 1; | |||||||||||
| 118 | |||||||||||||
| 119 | __END__ | ||||||||||||
| 120 | |||||||||||||
| 121 | =pod | ||||||||||||
| 122 | |||||||||||||
| 123 | =encoding UTF-8 | ||||||||||||
| 124 | |||||||||||||
| 125 | =head1 NAME | ||||||||||||
| 126 | |||||||||||||
| 127 | Langertha::Knarr::Handler::RequestLog - Decorator handler that writes per-request JSON logs via Knarr::RequestLog | ||||||||||||
| 128 | |||||||||||||
| 129 | =head1 VERSION | ||||||||||||
| 130 | |||||||||||||
| 131 | version 1.100 | ||||||||||||
| 132 | |||||||||||||
| 133 | =head1 SYNOPSIS | ||||||||||||
| 134 | |||||||||||||
| 135 | use Langertha::Knarr::RequestLog; | ||||||||||||
| 136 | use Langertha::Knarr::Handler::RequestLog; | ||||||||||||
| 137 | |||||||||||||
| 138 | my $rlog = Langertha::Knarr::RequestLog->new(config => $config); | ||||||||||||
| 139 | $handler = Langertha::Knarr::Handler::RequestLog->new( | ||||||||||||
| 140 | wrapped => $handler, | ||||||||||||
| 141 | request_log => $rlog, | ||||||||||||
| 142 | ); | ||||||||||||
| 143 | |||||||||||||
| 144 | =head1 DESCRIPTION | ||||||||||||
| 145 | |||||||||||||
| 146 | Decorator handler that writes a structured per-request log entry for | ||||||||||||
| 147 | every chat or stream request via L<Langertha::Knarr::RequestLog>. | ||||||||||||
| 148 | Sync requests log a single line with the result; streaming requests | ||||||||||||
| 149 | accumulate every delta and log one line with the assembled output | ||||||||||||
| 150 | when the stream closes. | ||||||||||||
| 151 | |||||||||||||
| 152 | C<knarr start> mounts this automatically when | ||||||||||||
| 153 | C<KNARR_LOG_FILE> / C<KNARR_LOG_DIR> (or the YAML C<logging:> section) | ||||||||||||
| 154 | is set. | ||||||||||||
| 155 | |||||||||||||
| 156 | =head2 wrapped | ||||||||||||
| 157 | |||||||||||||
| 158 | Required. The inner L<Langertha::Knarr::Handler> being decorated. | ||||||||||||
| 159 | |||||||||||||
| 160 | =head2 request_log | ||||||||||||
| 161 | |||||||||||||
| 162 | Required. A L<Langertha::Knarr::RequestLog> instance (or any object | ||||||||||||
| 163 | implementing C<start_request> / C<end_request>). | ||||||||||||
| 164 | |||||||||||||
| 165 | =head1 SUPPORT | ||||||||||||
| 166 | |||||||||||||
| 167 | =head2 Issues | ||||||||||||
| 168 | |||||||||||||
| 169 | Please report bugs and feature requests on GitHub at | ||||||||||||
| 170 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||||
| 171 | |||||||||||||
| 172 | =head2 IRC | ||||||||||||
| 173 | |||||||||||||
| 174 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||
| 175 | |||||||||||||
| 176 | =head1 CONTRIBUTING | ||||||||||||
| 177 | |||||||||||||
| 178 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||
| 179 | |||||||||||||
| 180 | =head1 AUTHOR | ||||||||||||
| 181 | |||||||||||||
| 182 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||
| 183 | |||||||||||||
| 184 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 185 | |||||||||||||
| 186 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||
| 187 | |||||||||||||
| 188 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 189 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 190 | |||||||||||||
| 191 | =cut | ||||||||||||