blib/lib/Langertha/Knarr/Stream.pm

filter: Statementstmt 68.5% 85 / 124 Branchbran 28.5% 20 / 70 Conditioncond 15.0% 6 / 40 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 64.0% 16 / 25 Podpod 90.0% 9 / 10 totaltotal 46.2% 136 / 294 CC 50 SCAR 24.3
CC
50
Cov
47%
finish_reason
30.0
__ANON__
20.0
__ANON__
20.0
CRAP 11.3
« blib/lib/Langertha/Knarr/Request.pm ↑ Summary blib/lib/Langertha/Knarr/Image.pm »
1 package Langertha::Knarr::Stream;
2 # ABSTRACT: Async chunk iterator returned by streaming Knarr handlers
3 46 our $VERSION = '1.103';
4 46 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 46 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 46 ▶use Scalar::Util qw( blessed weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 46 ▶use Langertha::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 2 ▶sub from_callback {
Subroutine from_callback: called CC=1 SCAR=2.7
Pod documented
11 2 my ($class, $setup) = @_;
12 2 my @queue;
13 my $pending;
14 2 my $finished = 0;
15 2 my @error;
16  
17 10 ▶ my $deliver = sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
18 10 my ($v) = @_;
19 0 ▶ if ( $pending ) { my $p = $pending; $pending = undef; $p->done($v) }
Branch: 1 branch
BranchTrueFalseTotal
if ($pending) { } 0 10 10
20 10 else { push @queue, $v }
21 2 };
22  
23 8 ▶ my $emit = sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
24 8 my ($chunk) = @_;
25 8 ▶ return unless defined $chunk && length $chunk;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chunk and length $chunk 0 8 8
Condition33%1/3
defined $chunk and length $chunk
!ll&&!rl&&r
008
MC/DC0%0/2
defined $chunk and length $chunk
defined $chunk length $chunk
Truth table33%1/3
A and B
A= defined $chunkB= length $chunk
ABresult
0X0
100
111
26 8 $deliver->($chunk);
27 2 };
28 2 ▶ my $done = sub { $finished = 1; $deliver->(undef) };
Subroutine __ANON__: called CC=1 SCAR=0.0
29 # The whole failure is kept, not just its message: a timeout's category
30 # (the second value, k36) has to reach the protocol. A read already
31 # waiting gets the failure too, not an undef that would end the stream
32 # as if it were complete. With no read waiting, the failure comes after
33 # the chunks already queued (the source reads it once they are gone),
34 # never an undef: a producer that fails before the first read -- an
35 # engine that refuses at once -- must not look like an empty, complete
36 # stream (k78).
37 0 ▶ my $fail = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
38 0 ▶ @error = ( $_[0] // 'unknown error', @_[ 1 .. $#_ ] );
Condition0%0/2
$_[0] // "unknown error"
l!l
00
MC/DC0%0/1
$_[0] // "unknown error"
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
39 0 $finished = 1;
40 0 ▶ return $deliver->(undef) unless $error[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $error[0] 0 0 0
41 0 ▶ if ( $pending ) { my $p = $pending; $pending = undef; $p->fail(@error) }
Branch: 1 branch
BranchTrueFalseTotal
if ($pending) 0 0 0
42 2 };
43  
44 my $stream = $class->new(
45 10 ▶ source => sub {
Subroutine __ANON__: called CC=4 SCAR=23.7
46 10 ▶ if ( @queue ) { return Future->done( shift @queue ) }
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 10 0 10
47 0 ▶ if ( $finished ) { return $error[0] ? Future->fail(@error) : Future->done(undef) }
Branch: 2 branches
BranchTrueFalseTotal
if ($finished) 0 0 0
$error[0] ? : 0 0 0
48 0 $pending = Future->new;
49 0 return $pending;
50 },
51 2 );
52  
53 # The producer may outlive a consumer that dropped the stream; it must
54 # not keep the stream alive for it.
55 2 my $weak = $stream;
56 2 weaken $weak;
57 0 ▶ my $finish = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
58 0 my ($reason) = @_;
59 0 ▶ $weak->finish_reason($reason) if $weak && defined $reason;
Branch: 1 branch
BranchTrueFalseTotal
if $weak and defined $reason 0 0 0
Condition0%0/3
$weak and defined $reason
!ll&&!rl&&r
000
MC/DC0%0/2
$weak and defined $reason
$weak defined $reason
Truth table0%0/3
A and B
A= $weakB= defined $reason
ABresult
0X0
100
111
60 2 };
61 0 ▶ my $tool_call = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
62 0 my @calls = grep { defined } @_;
63 0 ▶ $weak->tool_calls( [ @{ $weak->_tool_calls // [] }, @calls ] ) if $weak && @calls;
Branch: 1 branch
BranchTrueFalseTotal
if $weak and @calls 0 0 0
Condition0%0/5
$weak and @calls
!ll&&!rl&&r
000
$weak->_tool_calls // []
l!l
00
MC/DC0%0/2
$weak and @calls
$weak @calls
MC/DC0%0/1
$weak->_tool_calls // []
$weak->_tool_calls
Truth table0%0/3
A and B
A= $weakB= @calls
ABresult
0X0
100
111
Truth table0%0/2
A
A= $weak->_tool_calls
Aresult
11
00
64 2 };
65 # Every dialect reports cumulative usage, the last report being the
66 # stream's totals -- not always on the final chunk: OpenAI's include_usage
67 # frame comes after it.
68 0 ▶ my $usage = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
69 0 my ($u) = @_;
70 0 ▶ $weak->usage($u) if $weak && $weak->_usable_usage($u);
Branch: 1 branch
BranchTrueFalseTotal
if $weak and $weak->_usable_usage($u) 0 0 0
Condition0%0/3
$weak and $weak->_usable_usage($u)
!ll&&!rl&&r
000
MC/DC0%0/2
$weak and $weak->_usable_usage($u)
$weak $weak->_usable_usage($u)
Truth table0%0/3
A and B
A= $weakB= $weak->_usable_usage($u)
ABresult
0X0
100
111
71 2 };
72  
73 0 ▶ my $model = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
74 0 my ($name) = @_;
75 0 ▶ $weak->upstream_model($name) if $weak && defined $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
if $weak and defined $name and length $name 0 0 0
Condition0%0/6
$weak and defined $name and length $name
!ll&&!rl&&r
000
$weak and defined $name
!ll&&!rl&&r
000
MC/DC0%0/3
$weak and defined $name and length $name
$weak defined $name length $name
Truth table0%0/4
A and B and C
A= $weakB= defined $nameC= length $name
ABCresult
0XX0
10X0
1100
1111
76 2 };
77  
78 2 $setup->($emit, $done, $fail, $finish, $tool_call, $usage, $model);
79  
80 2 return $stream;
81 }
82  
83 # Two ways to construct:
84 # 1) generator => sub { ... } — sync coderef returning next string or undef
85 # 2) source => sub { ... } — coderef returning a Future[string|undef]
86 46 has generator => ( is => 'ro', isa => 'Maybe[CodeRef]' );
87 46 has source => ( is => 'ro', isa => 'Maybe[CodeRef]' );
88 46 has upstream => ( is => 'ro', isa => 'Maybe[Object]' );
89  
90 46 has _finish_reason => (
91 is => 'rw',
92 isa => 'Maybe[Str]',
93 init_arg => 'finish_reason',
94 );
95  
96 0 ▶sub finish_reason {
Subroutine finish_reason: not called CC=5 SCAR=34.0
Pod documented
97 0 my $self = shift;
98 0 ▶ return $self->_finish_reason(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
99 0 my $own = $self->_finish_reason;
100 0 ▶ return $own if defined $own;
Branch: 1 branch
BranchTrueFalseTotal
if defined $own 0 0 0
101 0 my $up = $self->upstream;
102 0 ▶ return $up && $up->can('finish_reason') ? $up->finish_reason : undef;
Branch: 1 branch
BranchTrueFalseTotal
$up && $up->can('finish_reason') ? : 0 0 0
Condition0%0/3
$up and $up->can("finish_reason")
!ll&&!rl&&r
000
MC/DC0%0/2
$up and $up->can("finish_reason")
$up $up->can("finish_reason")
Truth table0%0/3
A and B
A= $upB= $up->can("finish_reason")
ABresult
0X0
100
111
103 }
104  
105 46 has _tool_calls => (
106 is => 'rw',
107 isa => 'Maybe[ArrayRef]',
108 init_arg => 'tool_calls',
109 );
110  
111 4 ▶sub tool_calls {
Subroutine tool_calls: called CC=6 SCAR=20.5
Pod documented
112 4 my $self = shift;
113 4 ▶ return $self->_tool_calls(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4 4
114 4 my $own = $self->_tool_calls;
115 4 ▶ return $own if $own && @$own;
Branch: 1 branch
BranchTrueFalseTotal
if $own and @$own 0 4 4
Condition33%1/3
$own and @$own
!ll&&!rl&&r
400
MC/DC0%0/2
$own and @$own
$own @$own
Truth table33%1/3
A and B
A= $ownB= @$own
ABresult
0X0
100
111
116 4 my $up = $self->upstream;
117 4 ▶ return $up->tool_calls if $up && $up->can('tool_calls');
Branch: 1 branch
BranchTrueFalseTotal
if $up and $up->can("tool_calls") 0 4 4
Condition33%1/3
$up and $up->can("tool_calls")
!ll&&!rl&&r
400
MC/DC0%0/2
$up and $up->can("tool_calls")
$up $up->can("tool_calls")
Truth table33%1/3
A and B
A= $upB= $up->can("tool_calls")
ABresult
0X0
100
111
118 4 return [];
119 }
120  
121 4 ▶sub has_tool_calls { scalar @{ $_[0]->tool_calls } > 0 }
Subroutine has_tool_calls: called CC=1 SCAR=0.0
Pod documented
122  
123 46 has _usage => (
124 is => 'rw',
125 isa => 'Maybe[Object]',
126 init_arg => undef,
127 );
128  
129 4 ▶sub usage {
Subroutine usage: called CC=7 SCAR=26.4
Pod documented
130 4 my $self = shift;
131 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 4 4
132 0 my ($u) = @_;
133 0 ▶ return $self->_usage( ref $u eq 'HASH' ? ( %$u ? Langertha::Usage->from_hash($u) : undef ) : $u );
Branch: 2 branches
BranchTrueFalseTotal
ref $u eq 'HASH' ? : 0 0 0
%$u ? : 0 0 0
134 }
135 4 my $own = $self->_usage;
136 4 ▶ return $own if $own;
Branch: 1 branch
BranchTrueFalseTotal
if $own 0 4 4
137 4 my $up = $self->upstream;
138 4 ▶ return $up && $up->can('usage') ? $up->usage : undef;
Branch: 1 branch
BranchTrueFalseTotal
$up && $up->can('usage') ? : 0 4 4
Condition33%1/3
$up and $up->can("usage")
!ll&&!rl&&r
400
MC/DC0%0/2
$up and $up->can("usage")
$up $up->can("usage")
Truth table33%1/3
A and B
A= $upB= $up->can("usage")
ABresult
0X0
100
111
139 }
140  
141 46 has _model => (
142 is => 'rw',
143 isa => 'Maybe[Str]',
144 init_arg => 'model',
145 );
146  
147 4 ▶sub model {
Subroutine model: called CC=5 SCAR=17.0
Pod documented
148 4 my $self = shift;
149 4 ▶ return $self->_model(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 3 4
150 3 my $own = $self->_model;
151 3 ▶ return $own if defined $own;
Branch: 1 branch
BranchTrueFalseTotal
if defined $own 0 3 3
152 3 my $up = $self->upstream;
153 3 ▶ return $up && $up->can('model') ? $up->model : undef;
Branch: 1 branch
BranchTrueFalseTotal
$up && $up->can('model') ? : 0 3 3
Condition33%1/3
$up and $up->can("model")
!ll&&!rl&&r
300
MC/DC0%0/2
$up and $up->can("model")
$up $up->can("model")
Truth table33%1/3
A and B
A= $upB= $up->can("model")
ABresult
0X0
100
111
154 }
155  
156 46 has _upstream_model => (
157 is => 'rw',
158 isa => 'Maybe[Str]',
159 init_arg => 'upstream_model',
160 );
161  
162 3 ▶sub upstream_model {
Subroutine upstream_model: called CC=5 SCAR=17.8
Pod documented
163 3 my $self = shift;
164 3 ▶ return $self->_upstream_model(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 3 3
165 3 my $own = $self->_upstream_model;
166 3 ▶ return $own if defined $own;
Branch: 1 branch
BranchTrueFalseTotal
if defined $own 0 3 3
167 3 my $up = $self->upstream;
168 3 ▶ return $up && $up->can('upstream_model') ? $up->upstream_model : undef;
Branch: 1 branch
BranchTrueFalseTotal
$up && $up->can('upstream_model') ? : 0 3 3
Condition33%1/3
$up and $up->can("upstream_model")
!ll&&!rl&&r
300
MC/DC0%0/2
$up and $up->can("upstream_model")
$up $up->can("upstream_model")
Truth table33%1/3
A and B
A= $upB= $up->can("upstream_model")
ABresult
0X0
100
111
169 }
170  
171 0 ▶sub _usable_usage {
Subroutine _usable_usage: not called CC=4 SCAR=30.0
172 0 my ($self, $u) = @_;
173 0 ▶ return ref $u eq 'HASH' ? ( %$u ? 1 : 0 ) : blessed($u) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
ref $u eq 'HASH' ? : 0 0 0
%$u ? : 0 0 0
builtin::blessed($u) ? : 0 0 0
174 }
175  
176 19 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.2
Pod undocumented
177 19 my ($self, $args) = @_;
178 19 ▶ $self->usage( $args->{usage} ) if defined $args->{usage};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"usage"} 0 19 19
179 }
180  
181 45 ▶sub next_chunk_f {
Subroutine next_chunk_f: called CC=3 SCAR=11.2
Pod documented
182 45 my ($self) = @_;
183 45 ▶ if ( my $g = $self->generator ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = $self->generator) 13 32 45
184 13 my $v = $g->();
185 13 return Future->done($v);
186 }
187 32 ▶ if ( my $s = $self->source ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $self->source) 32 0 32
188 32 return $s->();
189 }
190 0 return Future->done(undef);
191 }
192  
193 # Convenience: build a stream from a fixed list of chunks
194 0 ▶sub from_list {
Subroutine from_list: not called CC=1 SCAR=6.9
Pod documented
195 0 my ($class, @chunks) = @_;
196 0 my @queue = @chunks;
197 0 ▶ return $class->new( generator => sub { @queue ? shift @queue : undef } );
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
@queue ? : 0 0 0
198 }
199  
200 46 __PACKAGE__->meta->make_immutable;
201 46 1;
202  
203 __END__
« blib/lib/Langertha/Knarr/Request.pm ↑ Summary blib/lib/Langertha/Knarr/Image.pm »