Langertha/Role/Streaming.pm

filter: Statementstmt 98.1% 54 / 55 Branchbran 75.0% 30 / 40 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 6 / 6 Podpod 100% 3 / 3 totaltotal 80.6% 96 / 119 CC 25 SCAR 24.6
CC
25
Cov
84%
process_stream_data
16.7
parse_sse_line
8.8
parse_ndjson_line
3.3
CRAP 11.7
« Langertha/Role/OpenAICompatible.pm ↑ Summary Langertha/Role/ResponseFormat.pm »
1 package Langertha::Role::Streaming;
2 # ABSTRACT: Role for streaming support
3 33 our $VERSION = '0.502';
4 33 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 33 requires qw(
7 json
8 parse_stream_chunk
9 stream_format
10 );
11  
12 33 ▶use Langertha::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 33 ▶use Langertha::Stream::Chunk;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 sub parse_sse_line {
17 22 ▶ my ($self, $line) = @_;
Subroutine parse_sse_line: called CC=8 SCAR=21.7
Pod documented
18  
19 22 ▶ return undef if !defined $line || $line eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $line or $line eq "" 0 22 22
Condition33%1/3
not defined $line or $line eq ""
l!l&&r!l&&!r
0022
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
20 22 ▶ return undef if $line =~ /^:\s*/; # SSE comment
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^:\s*/ 1 21 22
21  
22 21 ▶ if ($line =~ /^data:\s*(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^data:\s*(.*)$/) 15 6 21
23 15 my $data = $1;
24 15 ▶ return { type => 'done' } if $data eq '[DONE]';
Branch: 1 branch
BranchTrueFalseTotal
if $data eq "[DONE]" 2 13 15
25 13 ▶ return undef if $data eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $data eq "" 0 13 13
26 13 return { type => 'data', data => $self->json->decode($data) };
27 }
28  
29 6 ▶ if ($line =~ /^event:\s*(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^event:\s*(.*)$/) 6 0 6
30 6 return { type => 'event', event => $1 };
31 }
32  
33 0 return undef;
34 }
35  
36  
37 sub parse_ndjson_line {
38 3 ▶ my ($self, $line) = @_;
Subroutine parse_ndjson_line: called CC=3 SCAR=12.0
Pod documented
39  
40 3 ▶ return undef if !defined $line || $line eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $line or $line eq "" 0 3 3
Condition33%1/3
not defined $line or $line eq ""
l!l&&r!l&&!r
003
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
41  
42 3 my $data = $self->json->decode($line);
43 3 return { type => 'data', data => $data };
44 }
45  
46  
47 sub process_stream_data {
48 5 ▶ my ($self, $data, $chunk_callback) = @_;
Subroutine process_stream_data: called CC=16 SCAR=28.2
Pod documented
49  
50 5 my @chunks;
51 5 my $format = $self->stream_format;
52 5 my $buffer = '';
53 5 my $current_event = undef;
54  
55 5 my @lines = split /\r?\n/, $data;
56  
57 5 for my $line (@lines) {
58 36 ▶ if ($format eq 'sse') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'sse') { } 33 3 36
elsif ($format eq 'ndjson') { } 3 0 3
59 33 ▶ if ($line eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "") 11 22 33
60 # Empty line marks end of SSE event
61 11 next;
62 }
63  
64 22 my $parsed = $self->parse_sse_line($line);
65 22 ▶ next unless $parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed 1 21 22
66  
67 21 ▶ if ($parsed->{type} eq 'event') {
Branch: 3 branches
BranchTrueFalseTotal
if ($$parsed{'type'} eq 'event') { } 6 15 21
elsif ($$parsed{'type'} eq 'done') { } 2 13 15
elsif ($$parsed{'type'} eq 'data') { } 13 0 13
68 6 $current_event = $parsed->{event};
69 } elsif ($parsed->{type} eq 'done') {
70 # Stream complete
71 2 last;
72 } elsif ($parsed->{type} eq 'data') {
73 13 my $chunk = $self->parse_stream_chunk($parsed->{data}, $current_event);
74 13 ▶ if ($chunk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk) 11 2 13
75 11 push @chunks, $chunk;
76 11 ▶ $chunk_callback->($chunk) if $chunk_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_callback 3 8 11
77 }
78 13 $current_event = undef;
79 }
80 } elsif ($format eq 'ndjson') {
81 3 ▶ next if $line eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 0 3 3
82  
83 3 my $parsed = $self->parse_ndjson_line($line);
84 3 ▶ next unless $parsed && $parsed->{type} eq 'data';
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed and $$parsed{"type"} eq "data" 0 3 3
Condition33%1/3
$parsed and $$parsed{"type"} eq "data"
!ll&&!rl&&r
003
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
85  
86 3 my $chunk = $self->parse_stream_chunk($parsed->{data});
87 3 ▶ if ($chunk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk) 3 0 3
88 3 push @chunks, $chunk;
89 3 ▶ $chunk_callback->($chunk) if $chunk_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_callback 0 3 3
90 }
91 }
92 }
93  
94 5 return \@chunks;
95 }
96  
97  
98  
99 33 1;
100  
101 __END__
102  
103 =pod
104  
105 =encoding UTF-8
106  
107 =head1 NAME
108  
109 Langertha::Role::Streaming - Role for streaming support
110  
111 =head1 VERSION
112  
113 version 0.502
114  
115 =head1 SYNOPSIS
116  
117 # Synchronous streaming via Role::HTTP
118 my $chunks = $engine->execute_streaming_request($request, sub {
119 my ($chunk) = @_;
120 print $chunk->content;
121 });
122  
123 # Streaming with iterator
124 my $stream = $engine->simple_chat_stream_iterator('Tell me a story');
125 while (my $chunk = $stream->next) {
126 print $chunk->content;
127 }
128  
129 =head1 DESCRIPTION
130  
131 Provides stream parsing for server-sent events (SSE) and newline-delimited JSON
132 (NDJSON) streaming responses from LLM APIs. Engines composing this role must
133 implement C<parse_stream_chunk> and C<stream_format>. Works together with
134 L<Langertha::Role::HTTP> for synchronous streaming and L<Langertha::Role::Chat>
135 for the higher-level streaming API.
136  
137 =head2 parse_sse_line
138  
139 my $parsed = $engine->parse_sse_line($line);
140  
141 Parses a single Server-Sent Events line. Returns C<undef> for empty lines and
142 SSE comments. Returns a HashRef with C<type> set to C<'done'>, C<'data'>, or
143 C<'event'>. Data lines have their JSON decoded under the C<data> key.
144  
145 =head2 parse_ndjson_line
146  
147 my $parsed = $engine->parse_ndjson_line($line);
148  
149 Parses a single newline-delimited JSON line. Returns C<undef> for empty lines.
150 Returns a HashRef with C<type =E<gt> 'data'> and the decoded C<data>.
151  
152 =head2 process_stream_data
153  
154 my $chunks = $engine->process_stream_data($raw_body, $chunk_callback);
155 my $chunks = $engine->process_stream_data($raw_body);
156  
157 Parses a complete streaming response body according to the engine's
158 C<stream_format> (C<'sse'> or C<'ndjson'>). Calls C<parse_stream_chunk> on
159 each data event and optionally calls C<$chunk_callback> with each resulting
160 L<Langertha::Stream::Chunk>. Returns an ArrayRef of all chunks.
161  
162 =head1 SEE ALSO
163  
164 =over
165  
166 =item * L<Langertha::Role::Chat> - Chat streaming methods (uses this role)
167  
168 =item * L<Langertha::Role::HTTP> - HTTP execution of streaming requests
169  
170 =item * L<Langertha::Stream> - Stream iterator object
171  
172 =item * L<Langertha::Stream::Chunk> - Individual stream chunk
173  
174 =back
175  
176 =head1 SUPPORT
177  
178 =head2 Issues
179  
180 Please report bugs and feature requests on GitHub at
181 L<https://github.com/Getty/langertha/issues>.
182  
183 =head2 IRC
184  
185 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
186  
187 =head1 CONTRIBUTING
188  
189 Contributions are welcome! Please fork the repository and submit a pull request.
190  
191 =head1 AUTHOR
192  
193 Torsten Raudssus <getty@cpan.org>
194  
195 =head1 COPYRIGHT AND LICENSE
196  
197 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
198  
199 This is free software; you can redistribute it and/or modify it under
200 the same terms as the Perl 5 programming language system itself.
201  
202 =cut
« Langertha/Role/OpenAICompatible.pm ↑ Summary Langertha/Role/ResponseFormat.pm »