blib/lib/Langertha/Knarr/PassthroughUsage.pm

filter: Statementstmt 18.7% 15 / 80 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 22.2% 2 / 9 Podpod 100% 4 / 4 totaltotal 11.1% 21 / 189 CC 36 SCAR 48.1
CC
36
Cov
10%
_take
272.0
add_chunk
72.0
_read_line
42.0
CRAP 122.9
« blib/lib/Langertha/Knarr/Manifest.pm ↑ Summary blib/lib/Langertha/Knarr/CLI/Cmd/Check.pm »
1 package Langertha::Knarr::PassthroughUsage;
2 # ABSTRACT: Token usage and model read off a copy of a raw passthrough answer
3 1 our $VERSION = '1.103';
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 1 has max_line => ( is => 'ro', isa => 'Int', default => 65536 );
9  
10 1 has usage => ( is => 'ro', init_arg => undef, writer => '_set_usage' );
11  
12 1 has model => ( is => 'ro', init_arg => undef, writer => '_set_model' );
13  
14 1 has _json => ( is => 'ro', lazy => 1, builder => '_build__json' );
15 0 ▶sub _build__json { JSON::MaybeXS->new( utf8 => 1 ) }
Subroutine _build__json: not called CC=1 SCAR=6.9
16  
17 # The unfinished last line, and whether the line in progress was too long
18 # and is being skipped up to its end.
19 1 has _tail => ( is => 'rw', init_arg => undef, default => '' );
20 1 has _skipping => ( is => 'rw', init_arg => undef, default => 0 );
21  
22 0 ▶sub add_chunk {
Subroutine add_chunk: not called CC=8 SCAR=42.8
Pod documented
23 0 my ( $self, $data ) = @_;
24 0 ▶ return unless defined $data && length $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data and length $data 0 0 0
Condition0%0/3
defined $data and length $data
!ll&&!rl&&r
000
MC/DC0%0/2
defined $data and length $data
defined $data length $data
Truth table0%0/3
A and B
A= defined $dataB= length $data
ABresult
0X0
100
111
25 0 my $buf = $self->_tail . $data;
26 0 ▶ if ( $self->_skipping ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_skipping) 0 0 0
27 0 my $nl = index( $buf, "\n" );
28 0 ▶ if ( $nl < 0 ) { $self->_tail(''); return }
Branch: 1 branch
BranchTrueFalseTotal
if ($nl < 0) 0 0 0
29 0 substr( $buf, 0, $nl + 1, '' );
30 0 $self->_skipping(0);
31 }
32 0 my $end = rindex( $buf, "\n" );
33 0 ▶ if ( $end >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end >= 0) 0 0 0
34 0 $self->_read_line($_) for split /\n/, substr( $buf, 0, $end );
35 0 substr( $buf, 0, $end + 1, '' );
36 }
37 0 ▶ if ( length $buf > $self->max_line ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf > $self->max_line) 0 0 0
38 0 $self->_tail('');
39 0 $self->_skipping(1);
40 0 return;
41 }
42 0 $self->_tail($buf);
43 0 return;
44 }
45  
46  
47 0 ▶sub read_body {
Subroutine read_body: not called CC=5 SCAR=34.0
Pod documented
48 0 my ( $self, $body ) = @_;
49 0 ▶ return unless defined $body && length $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body and length $body 0 0 0
Condition0%0/3
defined $body and length $body
!ll&&!rl&&r
000
MC/DC0%0/2
defined $body and length $body
defined $body length $body
Truth table0%0/3
A and B
A= defined $bodyB= length $body
ABresult
0X0
100
111
50 0 ▶ if ( $body =~ /\A\s*\{/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /\A\s*\{/) 0 0 0
51 0 my $obj = eval { $self->_json->decode($body) };
52 0 ▶ return $self->_take($obj) if ref $obj eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $obj eq "HASH" 0 0 0
53 }
54 # A buffered stream (PSGI), or NDJSON.
55 0 $self->add_chunk($body);
56 0 $self->finish;
57 0 return;
58 }
59  
60  
61 0 ▶sub finish {
Subroutine finish: not called CC=3 SCAR=24.8
Pod documented
62 0 my ( $self ) = @_;
63 0 my $tail = $self->_tail;
64 0 $self->_tail('');
65 0 ▶ $self->_read_line($tail) if length $tail && !$self->_skipping;
Branch: 1 branch
BranchTrueFalseTotal
if length $tail and not $self->_skipping 0 0 0
Condition0%0/3
length $tail and not $self->_skipping
!ll&&!rl&&r
000
MC/DC0%0/2
length $tail and not $self->_skipping
length $tail not $self->_skipping
Truth table0%0/3
A and not(B)
A= length $tailB= not $self->_skipping
ABresult
0X0
100
111
66 0 $self->_skipping(0);
67 0 return;
68 }
69  
70  
71 0 ▶sub trace_args {
Subroutine trace_args: not called CC=3 SCAR=24.8
Pod documented
72 0 my ( $self ) = @_;
73 0 $self->finish;
74 return (
75 0 ▶ defined $self->usage ? ( usage => $self->usage ) : (),
Branch: 2 branches
BranchTrueFalseTotal
defined $self->usage ? : 0 0 0
defined $self->model ? : 0 0 0
76 defined $self->model ? ( model => $self->model ) : (),
77 );
78 }
79  
80  
81 # A line worth decoding carries a usage object or count, or names the
82 # model while none is known yet (OpenAI repeats it in every chunk, and
83 # sends "usage":null in every chunk but the last).
84 0 ▶sub _read_line {
Subroutine _read_line: not called CC=6 SCAR=37.4
85 0 my ( $self, $line ) = @_;
86 0 $line =~ s/\r\z//;
87 0 $line =~ s/\Adata:[ \t]*//;
88 0 ▶ return unless $line =~ /\A\s*\{/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\A\s*\{/ 0 0 0
89 0 ▶ return unless $line =~ /"(?:usage"\s*:\s*\{|prompt_eval_count"|eval_count")/
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /"(?:usage"\s*:\s*\{|prompt_eval_count"|eval_count")/ or not defined $self->model and index($line, "\"model\"") != -1 0 0 0
Condition0%0/6
$line =~ /"(?:usage"\s*:\s*\{|prompt_eval_count"|eval_count")/ or not defined $self->model and index($line, "\"model\"") != -1
l!l&&r!l&&!r
000
not defined $self->model and index($line, "\"model\"") != -1
!ll&&!rl&&r
000
MC/DC0%0/3
$line =~ /"(?:usage"\s*:\s*\{|prompt_eval_count"|eval_count")/ or not defined $self->model and index($line, "\"model\"") != -1
$line =~ /"(?:usage"\s*:\s*\{|prompt_eval_count"|eval_count")/ not defined $self->model index($line, "\"model\"") != -1
Truth table0%0/4
A or not(B) and C
A= $line =~ /"(?:usage"\s*:\s*\{|prompt_eval_count"|eval_count")/B= not defined $self->modelC= index($line, "\"model\"") != -1
ABCresult
1XX1
0111
00X0
0100
90 || ( !defined $self->model && index( $line, '"model"' ) >= 0 );
91 0 my $obj = eval { $self->_json->decode($line) };
92 0 ▶ $self->_take($obj) if ref $obj eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $obj eq "HASH" 0 0 0
93 0 return;
94 }
95  
96 0 ▶sub _take {
Subroutine _take: not called CC=16 SCAR=56.1
97 0 my ( $self, $obj ) = @_;
98 # Anthropic's message_start carries the message, model and usage inside.
99 0 ▶ my $msg = ref $obj->{message} eq 'HASH' ? $obj->{message} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $obj->{'message'} eq 'HASH' ? : 0 0 0
100 0 ▶ my $model = $obj->{model} // $msg->{model};
Condition0%0/3
$obj->{"model"} // $msg->{"model"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj->{"model"} // $msg->{"model"}
$obj->{"model"} $msg->{"model"}
Truth table0%0/3
A // B
A= $obj->{"model"}B= $msg->{"model"}
ABresult
1X1
011
000
101 0 ▶ $self->_set_model($model) if defined $model && !ref $model && length $model;
Branch: 1 branch
BranchTrueFalseTotal
if defined $model and not ref $model and length $model 0 0 0
Condition0%0/6
defined $model and not ref $model and length $model
!ll&&!rl&&r
000
defined $model and not ref $model
!ll&&!rl&&r
000
MC/DC0%0/3
defined $model and not ref $model and length $model
defined $model not ref $model length $model
Truth table0%0/4
A and not(B) and C
A= defined $modelB= not ref $modelC= length $model
ABCresult
0XX0
10X0
1100
1111
102  
103 0 ▶ my $type = $obj->{type} // '';
Condition0%0/2
$obj->{"type"} // ""
l!l
00
MC/DC0%0/1
$obj->{"type"} // ""
$obj->{"type"}
Truth table0%0/2
A
A= $obj->{"type"}
Aresult
11
00
104 0 ▶ if ( $type eq 'message_delta' && ref $obj->{usage} eq 'HASH' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'message_delta' and ref $obj->{'usage'} eq 'HASH') { } 0 0 0
elsif (ref $obj->{'usage'} eq 'HASH') { } 0 0 0
elsif (ref $msg->{'usage'} eq 'HASH') { } 0 0 0
elsif (defined $obj->{'prompt_eval_count'} or defined $obj->{'eval_count'}) { } 0 0 0
Condition0%0/3
$type eq "message_delta" and ref $obj->{"usage"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$type eq "message_delta" and ref $obj->{"usage"} eq "HASH"
$type eq "message_delta" ref $obj->{"usage"} eq "HASH"
Truth table0%0/3
A and B
A= $type eq "message_delta"B= ref $obj->{"usage"} eq "HASH"
ABresult
0X0
100
111
105 0 ▶ my %merged = %{ $self->usage // {} };
Condition0%0/2
$self->usage // {}
l!l
00
MC/DC0%0/1
$self->usage // {}
$self->usage
Truth table0%0/2
A
A= $self->usage
Aresult
11
00
106 0 my $delta = $obj->{usage};
107 0 $merged{$_} = $delta->{$_} for grep { defined $delta->{$_} } keys %$delta;
108 0 $self->_set_usage( \%merged );
109 }
110 elsif ( ref $obj->{usage} eq 'HASH' ) {
111 0 $self->_set_usage( $obj->{usage} );
112 }
113 elsif ( ref $msg->{usage} eq 'HASH' ) {
114 0 $self->_set_usage( $msg->{usage} );
115 }
116 ▶ elsif ( defined $obj->{prompt_eval_count} || defined $obj->{eval_count} ) {
Condition0%0/3
defined $obj->{"prompt_eval_count"} or defined $obj->{"eval_count"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $obj->{"prompt_eval_count"} or defined $obj->{"eval_count"}
defined $obj->{"prompt_eval_count"} defined $obj->{"eval_count"}
Truth table0%0/3
A or B
A= defined $obj->{"prompt_eval_count"}B= defined $obj->{"eval_count"}
ABresult
1X1
011
000
117 0 ▶ $self->_set_usage({ map { defined $obj->{$_} ? ( $_ => $obj->{$_} ) : () }
Branch: 1 branch
BranchTrueFalseTotal
defined $obj->{$_} ? : 0 0 0
118 qw( prompt_eval_count eval_count ) });
119 }
120 0 return;
121 }
122  
123 1 __PACKAGE__->meta->make_immutable;
124 1 1;
125  
126 __END__
« blib/lib/Langertha/Knarr/Manifest.pm ↑ Summary blib/lib/Langertha/Knarr/CLI/Cmd/Check.pm »