| 1 | package Langertha::Knarr::Handler::RequestLog; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Decorator handler that writes per-request JSON logs via Knarr::RequestLog | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 6 | our $VERSION = '1.103'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use Future; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Langertha::Knarr::Stream; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Langertha::Knarr::Response; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 6 | 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 | 6 | 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 | 6 | has request_log => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 6 | ▶ | sub _open { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_open: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 6 | my ($self, $request) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 6 | ▶ | 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 | 5 | ▶ | async sub handle_chat_f { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
handle_chat_f: called CC=2 SCAR=7.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 5 | my ($self, $session, $request) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | my $handle = $self->_open($request); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 5 | my $f = eval { $self->wrapped->handle_chat_f( $session, $request ) }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 5 | ▶ | if ( my $err = $@ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | })->else( sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | my ($err) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | $self->request_log->end_request( $handle, error => "$err" ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # The whole failure: a timeout's category rides along (k36). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | return Future->fail(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 5 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 6 | } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | ▶ | async sub handle_stream_f { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
handle_stream_f: called CC=2 SCAR=7.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | my ($self, $session, $request) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | my $handle = $self->_open($request); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | my $upstream; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | my $err = do { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | eval { $upstream = $self->wrapped->handle_stream_f( $session, $request )->get; }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | $@; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | ▶ | if ($err) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | $self->request_log->end_request( $handle, error => "$err" ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | die $err; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | my $accumulated = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | my $closed = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | return Langertha::Knarr::Stream->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | upstream => $upstream, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | ▶ | source => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 4 | ▶ | $upstream->next_chunk_f->then( sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=21.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 4 | my ($delta) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | ▶ | if ( defined $delta ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | $accumulated .= $delta; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 3 | return Future->done($delta); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | ▶ | unless ( $closed ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | $closed = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | # The complete tool calls, known once the stream is exhausted (k19). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | ▶ | $self->request_log->end_request( | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%2/6
$upstream->can("has_tool_calls") and $upstream->has_tool_calls
$upstream->can("usage") and $upstream->usage
MC/DC0%0/2
$upstream->can("has_tool_calls") and $upstream->has_tool_calls
$upstream->can("has_tool_calls")
$upstream->has_tool_calls
MC/DC0%0/2
$upstream->can("usage") and $upstream->usage
$upstream->can("usage")
$upstream->usage
Truth table33%1/3
A and B
A= $upstream->can("has_tool_calls")B= $upstream->has_tool_calls
Truth table33%1/3
A and B
A= $upstream->can("usage")B= $upstream->usage
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 1 | return Future->done(undef); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | })->else( sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | my ($e) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | unless ( $closed ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | $closed = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $self->request_log->end_request( $handle, error => "$e" ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | return Future->fail(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 4 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 6 | } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub list_models { $_[0]->wrapped->list_models } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_models: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 6 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||