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