blib/lib/Langertha/Role/Streaming.pm

filter: Statementstmt 55.7% 58 / 104 Branchbran 38.1% 32 / 84 Conditioncond 16.6% 3 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 70.0% 7 / 10 Podpod 100% 6 / 6 totaltotal 45.3% 106 / 234 CC 51 SCAR 38.3
CC
51
Cov
45%
process_stream_data
69.3
process_stream_data
69.3
parse_sse_line
22.9
CRAP 46.0
« blib/lib/Langertha/ToolChoice.pm ↑ Summary blib/lib/Langertha/Output/Tools.pm »
1 package Langertha::Role::Streaming;
2 # ABSTRACT: Role for streaming support
3 193 our $VERSION = '0.505';
4 193 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 193 requires qw(
7 json
8 parse_stream_chunk
9 stream_format
10 );
11  
12 193 ▶use Langertha::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 193 ▶use Langertha::Stream::Chunk;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Per-stream parse state for the dialect parsers that assemble a tool call
16 # from fragments spread over several events (Chat-Completions delta.tool_calls,
17 # Anthropic input_json_delta -- karr k221). Both stream paths hand
18 # parse_stream_chunk a fresh HashRef per stream as its third argument
19 # (process_stream_data below, Role::Chat::chat_stream_realtime_f), so two
20 # streams on one engine never share fragments. This engine-wide HashRef is only
21 # the fallback for a caller that invokes parse_stream_chunk (or
22 # _process_stream_buffer) directly without one.
23 has _stream_parse_state => (
24 is => 'ro',
25 isa => 'HashRef',
26 init_arg => undef,
27 4401 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 193 );
29  
30  
31 393 ▶sub parse_sse_line {
Subroutine parse_sse_line: called CC=8 SCAR=31.3
Pod documented
32 0 ▶ my ($self, $line) = @_;
Subroutine parse_sse_line: not called CC=8 SCAR=31.3
Pod documented
33  
34 0 ▶ return undef if !defined $line || $line eq '';
Branch: 2 branches
BranchTrueFalseTotal
if not defined $line or $line eq "" 0 393 393
if not defined $line or $line eq "" 0 0 0
Condition16%1/6
not defined $line or $line eq ""
l!l&&r!l&&!r
00393
not defined $line or $line eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $line or $line eq ""
not defined $line $line eq ""
MC/DC0%0/2
not defined $line or $line eq ""
not defined $line $line eq ""
Truth table33%1/3
not(A) or B
A= not defined $lineB= $line eq ""
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not defined $lineB= $line eq ""
ABresult
1X1
011
000
35 0 ▶ return undef if $line =~ /^:\s*/; # SSE comment
Branch: 2 branches
BranchTrueFalseTotal
if $line =~ /^:\s*/ 2 391 393
if $line =~ /^:\s*/ 0 0 0
36  
37 0 ▶ if ($line =~ /^data:\s*(.*)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^data:\s*(.*)$/) 250 141 391
if ($line =~ /^data:\s*(.*)$/) 0 0 0
38 0 my $data = $1;
39 0 ▶ return { type => 'done' } if $data eq '[DONE]';
Branch: 2 branches
BranchTrueFalseTotal
if $data eq "[DONE]" 17 233 250
if $data eq "[DONE]" 0 0 0
40 0 ▶ return undef if $data eq '';
Branch: 2 branches
BranchTrueFalseTotal
if $data eq "" 0 233 233
if $data eq "" 0 0 0
41 0 return { type => 'data', data => $self->json->decode($data) };
42 }
43  
44 0 ▶ if ($line =~ /^event:\s*(.*)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^event:\s*(.*)$/) 141 0 141
if ($line =~ /^event:\s*(.*)$/) 0 0 0
45 0 return { type => 'event', event => $1 };
46 }
47  
48 0 return undef;
49 }
50  
51  
52 15 ▶sub parse_ndjson_line {
Subroutine parse_ndjson_line: called CC=3 SCAR=17.3
Pod documented
53 0 ▶ my ($self, $line) = @_;
Subroutine parse_ndjson_line: not called CC=3 SCAR=17.3
Pod documented
54  
55 0 ▶ return undef if !defined $line || $line eq '';
Branch: 2 branches
BranchTrueFalseTotal
if not defined $line or $line eq "" 0 15 15
if not defined $line or $line eq "" 0 0 0
Condition16%1/6
not defined $line or $line eq ""
l!l&&r!l&&!r
0015
not defined $line or $line eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $line or $line eq ""
not defined $line $line eq ""
MC/DC0%0/2
not defined $line or $line eq ""
not defined $line $line eq ""
Truth table33%1/3
not(A) or B
A= not defined $lineB= $line eq ""
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not defined $lineB= $line eq ""
ABresult
1X1
011
000
56  
57 0 my $data = $self->json->decode($line);
58 0 return { type => 'data', data => $data };
59 }
60  
61  
62 55 ▶sub process_stream_data {
Subroutine process_stream_data: called CC=17 SCAR=42.4
Pod documented
63 0 ▶ my ($self, $data, $chunk_callback) = @_;
Subroutine process_stream_data: not called CC=17 SCAR=42.4
Pod documented
64  
65 0 my @chunks;
66 0 my $format = $self->stream_format;
67 0 my $buffer = '';
68 0 my $current_event = undef;
69 0 my %state; # this stream's parse state, see _stream_parse_state
70  
71 0 my @lines = split /\r?\n/, $data;
72  
73 0 for my $line (@lines) {
74 0 ▶ if ($format eq 'sse') {
Branch: 4 branches
BranchTrueFalseTotal
if ($format eq 'sse') { } 593 15 608
elsif ($format eq 'ndjson') { } 15 0 15
if ($format eq 'sse') { } 0 0 0
elsif ($format eq 'ndjson') { } 0 0 0
75 0 ▶ if ($line eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($line eq "") 200 393 593
if ($line eq "") 0 0 0
76 # Empty line marks end of SSE event
77 0 next;
78 }
79  
80 0 my $parsed = $self->parse_sse_line($line);
81 0 ▶ next unless $parsed;
Branch: 2 branches
BranchTrueFalseTotal
unless $parsed 2 391 393
unless $parsed 0 0 0
82  
83 0 ▶ if ($parsed->{type} eq 'event') {
Branch: 6 branches
BranchTrueFalseTotal
if ($parsed->{'type'} eq 'event') { } 141 250 391
elsif ($parsed->{'type'} eq 'done') { } 17 233 250
elsif ($parsed->{'type'} eq 'data') { } 233 0 233
if ($$parsed{'type'} eq 'event') { } 0 0 0
elsif ($$parsed{'type'} eq 'done') { } 0 0 0
elsif ($$parsed{'type'} eq 'data') { } 0 0 0
84 0 $current_event = $parsed->{event};
85 } elsif ($parsed->{type} eq 'done') {
86 # Stream complete
87 0 last;
88 } elsif ($parsed->{type} eq 'data') {
89 0 my $chunk = $self->parse_stream_chunk($parsed->{data}, $current_event, \%state);
90 0 ▶ if ($chunk) {
Branch: 2 branches
BranchTrueFalseTotal
if ($chunk) 146 84 230
if ($chunk) 0 0 0
91 0 push @chunks, $chunk;
92 0 ▶ $chunk_callback->($chunk) if $chunk_callback;
Branch: 2 branches
BranchTrueFalseTotal
if $chunk_callback 11 135 146
if $chunk_callback 0 0 0
93 }
94 0 $current_event = undef;
95 }
96 } elsif ($format eq 'ndjson') {
97 0 ▶ next if $line eq '';
Branch: 2 branches
BranchTrueFalseTotal
if $line eq "" 0 15 15
if $line eq "" 0 0 0
98  
99 0 my $parsed = $self->parse_ndjson_line($line);
100 0 ▶ next unless $parsed && $parsed->{type} eq 'data';
Branch: 2 branches
BranchTrueFalseTotal
unless $parsed and $parsed->{"type"} eq "data" 0 15 15
unless $parsed and $$parsed{"type"} eq "data" 0 0 0
Condition16%1/6
$parsed and $parsed->{"type"} eq "data"
!ll&&!rl&&r
0015
$parsed and $$parsed{"type"} eq "data"
!ll&&!rl&&r
000
MC/DC0%0/2
$parsed and $parsed->{"type"} eq "data"
$parsed $parsed->{"type"} eq "data"
MC/DC0%0/2
$parsed and $$parsed{"type"} eq "data"
$parsed $$parsed{"type"} eq "data"
Truth table33%1/3
A and B
A= $parsedB= $parsed->{"type"} eq "data"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $parsedB= $$parsed{"type"} eq "data"
ABresult
0X0
100
111
101  
102 0 my $chunk = $self->parse_stream_chunk($parsed->{data}, undef, \%state);
103 0 ▶ if ($chunk) {
Branch: 2 branches
BranchTrueFalseTotal
if ($chunk) 14 0 14
if ($chunk) 0 0 0
104 0 push @chunks, $chunk;
105 0 ▶ $chunk_callback->($chunk) if $chunk_callback;
Branch: 2 branches
BranchTrueFalseTotal
if $chunk_callback 0 14 14
if $chunk_callback 0 0 0
106 }
107 }
108 }
109  
110 # The body is complete: let the dialect report what it could not finish.
111 0 ▶ $self->_finish_stream_state(\%state) if $self->can('_finish_stream_state');
Branch: 2 branches
BranchTrueFalseTotal
if $self->can("_finish_stream_state") 27 24 51
if $self->can("_finish_stream_state") 0 0 0
112  
113 0 return \@chunks;
114 }
115  
116  
117  
118 193 1;
119  
120 __END__
« blib/lib/Langertha/ToolChoice.pm ↑ Summary blib/lib/Langertha/Output/Tools.pm »