| 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
Condition0%0/3
defined $data and length $data
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
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | my $buf = $self->_tail . $data; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | if ( $self->_skipping ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | my $nl = index( $buf, "\n" ); | |||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | if ( $nl < 0 ) { $self->_tail(''); return } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $body and length $body
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
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if ( $body =~ /\A\s*\{/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my $obj = eval { $self->_json->decode($body) }; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | return $self->_take($obj) if ref $obj eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
length $tail and not $self->_skipping
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | return unless $line =~ /"(?:usage"\s*:\s*\{|prompt_eval_count"|eval_count")/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$line =~ /"(?:usage"\s*:\s*\{|prompt_eval_count"|eval_count")/ or not defined $self->model and index($line, "\"model\"") != -1
not defined $self->model and index($line, "\"model\"") != -1
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | my $model = $obj->{model} // $msg->{model}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$obj->{"model"} // $msg->{"model"}
MC/DC0%0/2
$obj->{"model"} // $msg->{"model"}
$obj->{"model"}
$msg->{"model"}
Truth table0%0/3
A // B
A= $obj->{"model"}B= $msg->{"model"}
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | $self->_set_model($model) if defined $model && !ref $model && length $model; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $model and not ref $model and length $model
defined $model and not ref $model
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
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | my $type = $obj->{type} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$obj->{"type"} // ""
MC/DC0%0/1
$obj->{"type"} // ""
$obj->{"type"}
Truth table0%0/2
A
A= $obj->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ( $type eq 'message_delta' && ref $obj->{usage} eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition0%0/3
$type eq "message_delta" and ref $obj->{"usage"} eq "HASH"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | my %merged = %{ $self->usage // {} }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->usage // {}
MC/DC0%0/1
$self->usage // {}
$self->usage
Truth table0%0/2
A
A= $self->usage
| |||||||||||||||||||||||||||||||||||||||||||
| 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"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | $self->_set_usage({ map { defined $obj->{$_} ? ( $_ => $obj->{$_} ) : () } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||