| 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
Condition16%1/6
not defined $line or $line eq ""
not defined $line or $line eq ""
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 ""
Truth table0%0/3
not(A) or B
A= not defined $lineB= $line eq ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | return undef if $line =~ /^:\s*/; # SSE comment | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | if ($line =~ /^data:\s*(.*)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | my $data = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | return { type => 'done' } if $data eq '[DONE]'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | return undef if $data eq ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | return { type => 'data', data => $self->json->decode($data) }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if ($line =~ /^event:\s*(.*)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition16%1/6
not defined $line or $line eq ""
not defined $line or $line eq ""
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 ""
Truth table0%0/3
not(A) or B
A= not defined $lineB= $line eq ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ($line eq '') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ($parsed->{type} eq 'event') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | push @chunks, $chunk; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | $chunk_callback->($chunk) if $chunk_callback; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | $current_event = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } elsif ($format eq 'ndjson') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | next if $line eq ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my $parsed = $self->parse_ndjson_line($line); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | next unless $parsed && $parsed->{type} eq 'data'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition16%1/6
$parsed and $parsed->{"type"} eq "data"
$parsed and $$parsed{"type"} eq "data"
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"
Truth table0%0/3
A and B
A= $parsedB= $$parsed{"type"} eq "data"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my $chunk = $self->parse_stream_chunk($parsed->{data}, undef, \%state); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ($chunk) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | push @chunks, $chunk; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | $chunk_callback->($chunk) if $chunk_callback; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | return \@chunks; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 193 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||