Langertha/Stream.pm

filter: Statementstmt 100% 35 / 35 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 6 / 6 totaltotal 98.2% 56 / 57 CC 5 SCAR 6.1
CC
5
Cov
98%
each
3.0
next
2.0
collect
2.0
CRAP 1.8
« Langertha/Spec/OpenAI.pm ↑ Summary Langertha/Role/StaticModels.pm »
1 package Langertha::Stream;
2 # ABSTRACT: Iterator for streaming responses
3 34 our $VERSION = '0.502';
4 34 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 34 has chunks => (
9 is => 'ro',
10 isa => 'ArrayRef[Langertha::Stream::Chunk]',
11 required => 1,
12 );
13  
14  
15 34 has _position => (
16 is => 'rw',
17 isa => 'Int',
18 default => 0,
19 );
20  
21 sub next {
22 13 ▶ my ($self) = @_;
Subroutine next: called CC=2 SCAR=6.9
Pod documented
23 13 my $pos = $self->_position;
24 13 ▶ return undef if $pos >= scalar @{$self->chunks};
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= scalar @{$self->chunks;} 3 10 13
25 10 $self->_position($pos + 1);
26 10 return $self->chunks->[$pos];
27 }
28  
29  
30 sub has_next {
31 5 ▶ my ($self) = @_;
Subroutine has_next: called CC=1 SCAR=0.0
Pod documented
32 5 return $self->_position < scalar @{$self->chunks};
33 }
34  
35  
36 sub collect {
37 1 ▶ my ($self) = @_;
Subroutine collect: called CC=2 SCAR=6.9
Pod documented
38 1 my @remaining;
39 1 ▶ while (my $chunk = $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chunk = $self->next) 3 1 4
40 3 push @remaining, $chunk;
41 }
42 1 return @remaining;
43 }
44  
45  
46 sub content {
47 1 ▶ my ($self) = @_;
Subroutine content: called CC=1 SCAR=0.0
Pod documented
48 1 return join('', map { $_->content } @{$self->chunks});
49 }
50  
51  
52 sub each {
53 1 ▶ my ($self, $callback) = @_;
Subroutine each: called CC=3 SCAR=11.0
Pod documented
54 1 ▶ croak "each() requires a callback" unless ref $callback eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback eq "CODE" 0 1 1
55 1 ▶ while (my $chunk = $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chunk = $self->next) 3 1 4
56 3 $callback->($chunk);
57 }
58 }
59  
60  
61 sub reset {
62 2 ▶ my ($self) = @_;
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
63 2 $self->_position(0);
64 }
65  
66  
67  
68 34 __PACKAGE__->meta->make_immutable;
69  
70 34 1;
71  
72 __END__
73  
74 =pod
75  
76 =encoding UTF-8
77  
78 =head1 NAME
79  
80 Langertha::Stream - Iterator for streaming responses
81  
82 =head1 VERSION
83  
84 version 0.502
85  
86 =head1 SYNOPSIS
87  
88 my $stream = $engine->simple_chat_stream_iterator('Tell me a story');
89  
90 # Iterate chunk by chunk
91 while (my $chunk = $stream->next) {
92 print $chunk->content;
93 }
94  
95 # Or use the callback form
96 $stream->reset;
97 $stream->each(sub {
98 my ($chunk) = @_;
99 print $chunk->content;
100 });
101  
102 # Collect all remaining chunks
103 my @chunks = $stream->collect;
104  
105 # Get complete content as a string
106 my $full_text = $stream->content;
107  
108 =head1 DESCRIPTION
109  
110 An iterator object wrapping an array of L<Langertha::Stream::Chunk> objects
111 returned from a streaming LLM response. Created by
112 L<Langertha::Role::Chat/simple_chat_stream_iterator>.
113  
114 The iterator maintains a position cursor so you can step through chunks one
115 at a time with C<next>, consume them all with C<collect> or C<each>, and
116 start over with C<reset>.
117  
118 =head2 chunks
119  
120 ArrayRef of L<Langertha::Stream::Chunk> objects comprising the full streaming
121 response. Required.
122  
123 =head2 next
124  
125 while (my $chunk = $stream->next) {
126 print $chunk->content;
127 }
128  
129 Returns the next L<Langertha::Stream::Chunk> and advances the cursor, or
130 C<undef> when all chunks have been consumed.
131  
132 =head2 has_next
133  
134 if ($stream->has_next) { ... }
135  
136 Returns true if there are more chunks to iterate over.
137  
138 =head2 collect
139  
140 my @chunks = $stream->collect;
141  
142 Returns all remaining chunks as a list and advances the cursor to the end.
143  
144 =head2 content
145  
146 my $text = $stream->content;
147  
148 Returns the concatenated C<content> of all chunks in the stream as a single
149 string, regardless of the current cursor position.
150  
151 =head2 each
152  
153 $stream->each(sub {
154 my ($chunk) = @_;
155 print $chunk->content;
156 });
157  
158 Iterates over all remaining chunks, calling C<$callback> with each
159 L<Langertha::Stream::Chunk>. Dies if no callback is provided.
160  
161 =head2 reset
162  
163 $stream->reset;
164  
165 Resets the cursor to the beginning so the stream can be iterated again.
166  
167 =head1 SEE ALSO
168  
169 =over
170  
171 =item * L<Langertha::Stream::Chunk> - A single streaming chunk
172  
173 =item * L<Langertha::Role::Chat> - Provides C<simple_chat_stream_iterator> that returns this object
174  
175 =item * L<Langertha::Role::Streaming> - Stream parsing (SSE / NDJSON)
176  
177 =back
178  
179 =head1 SUPPORT
180  
181 =head2 Issues
182  
183 Please report bugs and feature requests on GitHub at
184 L<https://github.com/Getty/langertha/issues>.
185  
186 =head2 IRC
187  
188 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
189  
190 =head1 CONTRIBUTING
191  
192 Contributions are welcome! Please fork the repository and submit a pull request.
193  
194 =head1 AUTHOR
195  
196 Torsten Raudssus <getty@cpan.org>
197  
198 =head1 COPYRIGHT AND LICENSE
199  
200 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
201  
202 This is free software; you can redistribute it and/or modify it under
203 the same terms as the Perl 5 programming language system itself.
204  
205 =cut
« Langertha/Spec/OpenAI.pm ↑ Summary Langertha/Role/StaticModels.pm »