lib/Langertha/Skeid/Protocol/Anthropic/Stream.pm

filter: Statementstmt 100% 95 / 95 Branchbran 82.6% 38 / 46 Conditioncond 71.4% 35 / 49 MC/DCmcdc 30.7% 8 / 26 Subroutinesub 100% 13 / 13 Podpod 100% 8 / 8 totaltotal 83.1% 197 / 237 CC 46 SCAR 30.5
CC
46
Cov
88%
delta
33.6
finish
11.3
new
3.3
CRAP 21.1
« blib/lib/Langertha/Skeid/KeyBroker/OpenBao.pm ↑ Summary lib/Langertha/Skeid/Protocol/Ollama.pm »
1 package Langertha::Skeid::Protocol::Anthropic::Stream;
2 89 our $VERSION = '0.004';
3 # ABSTRACT: Rewrites an OpenAI SSE stream as Anthropic streaming events
4 89 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 89 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 89 ▶use Langertha::Skeid::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 89 ▶use Langertha::Skeid::Protocol::Anthropic;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10  
11 78 ▶sub new {
Subroutine new: called CC=3 SCAR=12.0
Pod documented
12 78 my ($class, %args) = @_;
13 return bless {
14 model => ($args{model} // ''),
15 78 ▶ message_id => ($args{message_id} // ('msg_' . int(time * 1000))),
Condition50%2/4
$args{"model"} // ""
l!l
780
$args{"message_id"} // 'msg_' . int(time * 1000)
l!l
078
MC/DC0%0/1
$args{"model"} // ""
$args{"model"}
MC/DC0%0/1
$args{"message_id"} // 'msg_' . int(time * 1000)
$args{"message_id"}
Truth table50%1/2
A
A= $args{"model"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"message_id"}
Aresult
11
00
16 started => 0,
17 finished => 0,
18 errored => 0,
19 output_tokens => 0,
20 input_tokens => 0,
21 stop_reason => undef,
22 finish_reason => undef,
23 text_bytes => 0,
24  
25 # Content-block state. The OpenAI stream numbers parallel tool calls by their position
26 # in tool_calls[i].index; Anthropic numbers content blocks by their own order of
27 # appearance. The two numberings are different, so the translator keeps a map:
28 # tool_blocks keyed by the OpenAI index, each value an {anthropic_index, id, name,
29 # partial_json} record. open_blocks lists the Anthropic indices still awaiting a
30 # content_block_stop; next_index is the Anthropic index the next new block will get.
31 next_index => 0,
32 text_index => undef,
33 text_started => 0,
34 tool_blocks => {},
35 open_blocks => [],
36 }, $class;
37 }
38  
39  
40 58 ▶sub content_type { 'text/event-stream' }
Subroutine content_type: called CC=1 SCAR=0.0
Pod documented
41  
42 427 ▶sub _event {
Subroutine _event: called CC=1 SCAR=0.0
43 427 my ($name, $payload) = @_;
44 427 return "event: $name\ndata: " . Langertha::Skeid::Protocol::encode_json_safe($payload) . "\n\n";
45 }
46  
47  
48 65 ▶sub start {
Subroutine start: called CC=2 SCAR=7.0
Pod documented
49 65 my ($self) = @_;
50 65 ▶ return '' if $self->{started};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"started"} 0 65 65
51 65 $self->{started} = 1;
52  
53 # The SSE event name and the "type" inside its data must agree: the official SDKs dispatch on
54 # the data field, not on the event line, so a message_start carrying type "message" is a
55 # frame they do not recognise as the start of anything.
56 return _event('message_start', {
57 type => 'message_start',
58 message => {
59 id => $self->{message_id},
60 type => 'message',
61 role => 'assistant',
62 model => $self->{model},
63 65 content => [],
64 stop_reason => undef,
65 stop_sequence => undef,
66 usage => { input_tokens => 0, output_tokens => 0 },
67 },
68 });
69 }
70  
71  
72 201 ▶sub delta {
Subroutine delta: called CC=32 SCAR=35.1
Pod documented
73 201 my ($self, $chunk) = @_;
74 201 ▶ return '' unless ref($chunk) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $chunk eq "HASH" 0 201 201
75  
76 201 ▶ return '' if $self->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} 0 201 201
77  
78 # Some servers report a failure inside an open stream as a chunk carrying an OpenAI error
79 # object instead of choices. The HTTP status is already 200, so it can only reach an
80 # Anthropic client in-band, as an error event -- and nothing after it (core karr #224).
81 201 ▶ if (ref($chunk->{error}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $chunk->{"error"} eq "HASH") 3 198 201
82 3 ▶ my $message = $chunk->{error}{message} // 'upstream error';
Condition50%1/2
$chunk->{"error"}{"message"} // "upstream error"
l!l
30
MC/DC0%0/1
$chunk->{"error"}{"message"} // "upstream error"
$chunk->{"error"}{"message"}
Truth table50%1/2
A
A= $chunk->{"error"}{"message"}
Aresult
11
00
83 3 return $self->error_event(500, "Upstream error: $message", $chunk->{error}{type});
84 }
85  
86 198 my $out = '';
87 198 ▶ $out .= $self->start unless $self->{started};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"started"} 56 142 198
88  
89 # Usage can arrive on any chunk and usually arrives on the last one, which is also the
90 # chunk that has no content. Recorded whenever seen, reported at the end.
91 198 ▶ if (my $usage = $chunk->{usage}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $usage = $chunk->{"usage"}) 78 120 198
92 78 ▶ $self->{input_tokens} = 0 + ($usage->{prompt_tokens} // $usage->{input_tokens} // $self->{input_tokens});
Condition83%5/6
($usage->{"prompt_tokens"} // $usage->{"input_tokens"}) // $self->{"input_tokens"}
l!l&&r!l&&!r
7530
$usage->{"prompt_tokens"} // $usage->{"input_tokens"}
l!l&&r!l&&!r
7233
MC/DC0%0/3
($usage->{"prompt_tokens"} // $usage->{"input_tokens"}) // $self->{"input_tokens"}
$usage->{"prompt_tokens"} $usage->{"input_tokens"} $self->{"input_tokens"}
Truth table75%3/4
A // B // C
A= $usage->{"prompt_tokens"}B= $usage->{"input_tokens"}C= $self->{"input_tokens"}
ABCresult
1XX1
01X1
0011
0000
93 78 ▶ $self->{output_tokens} = 0 + ($usage->{completion_tokens} // $usage->{output_tokens} // $self->{output_tokens});
Condition83%5/6
($usage->{"completion_tokens"} // $usage->{"output_tokens"}) // $self->{"output_tokens"}
l!l&&r!l&&!r
7530
$usage->{"completion_tokens"} // $usage->{"output_tokens"}
l!l&&r!l&&!r
7233
MC/DC0%0/3
($usage->{"completion_tokens"} // $usage->{"output_tokens"}) // $self->{"output_tokens"}
$usage->{"completion_tokens"} $usage->{"output_tokens"} $self->{"output_tokens"}
Truth table75%3/4
A // B // C
A= $usage->{"completion_tokens"}B= $usage->{"output_tokens"}C= $self->{"output_tokens"}
ABCresult
1XX1
01X1
0011
0000
94 }
95  
96 198 ▶ my $choice = (ref($chunk->{choices}) eq 'ARRAY' ? $chunk->{choices}[0] : undef) || {};
Branch: 1 branch
BranchTrueFalseTotal
ref $chunk->{'choices'} eq 'ARRAY' ? : 198 0 198
Condition50%1/2
(ref $chunk->{'choices'} eq 'ARRAY' ? $chunk->{'choices'}[0] : undef) || {}
l!l
1980
MC/DC0%0/1
(ref $chunk->{'choices'} eq 'ARRAY' ? $chunk->{'choices'}[0] : undef) || {}
(ref $chunk->{'choices'} eq 'ARRAY' ? $chunk->{'choices'}[0] : undef)
Truth table50%1/2
A
A= (ref $chunk->{'choices'} eq 'ARRAY' ? $chunk->{'choices'}[0] : undef)
Aresult
11
00
97 198 ▶ if (defined(my $reason = $choice->{finish_reason})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $reason = $choice->{"finish_reason"})) 40 158 198
98 40 $self->{finish_reason} = $reason;
99 40 ▶ $self->{stop_reason} = $reason eq 'tool_calls' ? 'tool_use'
Branch: 2 branches
BranchTrueFalseTotal
$reason eq 'tool_calls' ? : 10 30 40
$reason eq 'length' ? : 2 28 30
100 : $reason eq 'length' ? 'max_tokens'
101 : 'end_turn';
102 }
103  
104 198 ▶ my $delta = $choice->{delta} || {};
Condition100%2/2
$choice->{'delta'} || {}
l!l
18513
MC/DC100%1/1
$choice->{'delta'} || {}
$choice->{'delta'}
Truth table100%2/2
A
A= $choice->{'delta'}
Aresult
11
00
105  
106 # Text content. The first text delta opens a text block at the next free Anthropic index;
107 # later deltas extend it.
108 198 my $text = $delta->{content};
109 198 ▶ if (defined($text) && length($text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text and length $text) 98 100 198
Condition100%3/3
defined $text and length $text
!ll&&!rl&&r
99198
MC/DC100%2/2
defined $text and length $text
defined $text length $text
Truth table100%3/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
110 98 ▶ if (!$self->{text_started}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"text_started"}) 57 41 98
111 57 $self->{text_index} = $self->{next_index}++;
112 57 $self->{text_started} = 1;
113 57 push @{$self->{open_blocks}}, $self->{text_index};
114 $out .= _event('content_block_start', {
115 type => 'content_block_start',
116 index => $self->{text_index},
117 57 content_block => { type => 'text', text => '' },
118 });
119 }
120 98 $self->{text_bytes} += Langertha::Skeid::Protocol::utf8_length($text);
121 $out .= _event('content_block_delta', {
122 type => 'content_block_delta',
123 index => $self->{text_index},
124 98 delta => { type => 'text_delta', text => $text },
125 });
126 }
127  
128 # Tool calls. OpenAI tracks parallel calls by tool_calls[i].index; Anthropic tracks content
129 # blocks by their own order. The first time we see a new tool_calls index we allocate an
130 # Anthropic block; later arguments-only deltas for the same index extend it with
131 # partial_json.
132 198 ▶ if (ref($delta->{tool_calls}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $delta->{"tool_calls"} eq "ARRAY") 28 170 198
133 28 for my $call (@{$delta->{tool_calls}}) {
134 28 ▶ next unless ref($call) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $call eq "HASH" 0 28 28
135 28 ▶ my $tool_idx = $call->{index} // 0;
Condition50%1/2
$call->{"index"} // 0
l!l
280
MC/DC0%0/1
$call->{"index"} // 0
$call->{"index"}
Truth table50%1/2
A
A= $call->{"index"}
Aresult
11
00
136  
137 # Lazily allocate a content block for this OpenAI tool_call index. The do block
138 # returns a hashref; ||= stores it under tool_blocks{$tool_idx} and binds $block to
139 # the same ref.
140 28 ▶ my $block = $self->{tool_blocks}{$tool_idx} ||= do {
Condition66%2/3
$self->{"tool_blocks"}{$tool_idx} ||= do { my $anthropic_index = $self->{"next_index"}++; push @{$$self{"open_blocks"};}, $anthropic_index; +{"anthropic_index", $anthropic_index, "id", $call->{"id"} // "", "name", $call->{"function"}{"name"} // "", "partial_json", "", "emitted_start", 0} }
l!l&&r!l&&!r
15130
MC/DC0%0/2
$self->{"tool_blocks"}{$tool_idx} ||= do { my $anthropic_index = $self->{"next_index"}++; push @{$$self{"open_blocks"};}, $anthropic_index; +{"anthropic_index", $anthropic_index, "id", $call->{"id"} // "", "name", $call->{"function"}{"name"} // "", "partial_json", "", "emitted_start", 0} }
$self->{"tool_blocks"}{$tool_idx} do { my $anthropic_index = $self->{"next_index"}++; push @{$$self{"open_blocks"};}, $anthropic_index; +{"anthropic_index", $anthropic_index, "id", $call->{"id"} // "", "name", $call->{"function"}{"name"} // "", "partial_json", "", "emitted_start", 0} }
Truth table66%2/3
A ||= B
A= $self->{"tool_blocks"}{$tool_idx}B= do { my $anthropic_index = $self->{"next_index"}++; push @{$$self{"open_blocks"};}, $anthropic_index; +{"anthropic_index", $anthropic_index, "id", $call->{"id"} // "", "name", $call->{"function"}{"name"} // "", "partial_json", "", "emitted_start", 0} }
ABresult
1X1
011
000
141 13 my $anthropic_index = $self->{next_index}++;
142 13 push @{$self->{open_blocks}}, $anthropic_index;
143 {
144 anthropic_index => $anthropic_index,
145 id => ($call->{id} // ''),
146 13 ▶ name => ($call->{function}{name} // ''),
Condition50%2/4
$call->{"id"} // ""
l!l
130
$call->{"function"}{"name"} // ""
l!l
130
MC/DC0%0/1
$call->{"id"} // ""
$call->{"id"}
MC/DC0%0/1
$call->{"function"}{"name"} // ""
$call->{"function"}{"name"}
Truth table50%1/2
A
A= $call->{"id"}
Aresult
11
00
Truth table50%1/2
A
A= $call->{"function"}{"name"}
Aresult
11
00
147 partial_json => '',
148 emitted_start => 0,
149 };
150 };
151  
152 # The OpenAI convention is the first chunk for a call carries id+name and later
153 # chunks carry only arguments; tolerate either order, fill in whatever this chunk
154 # brought.
155 28 ▶ $block->{id} = $call->{id} if defined $call->{id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $call->{"id"} 13 15 28
156 28 ▶ $block->{name} = $call->{function}{name} if defined $call->{function}{name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $call->{"function"}{"name"} 13 15 28
157  
158 28 ▶ unless ($block->{emitted_start}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($block->{"emitted_start"}) 13 15 28
159 13 $block->{emitted_start} = 1;
160 $out .= _event('content_block_start', {
161 type => 'content_block_start',
162 index => $block->{anthropic_index},
163 content_block => {
164 type => 'tool_use',
165 id => $block->{id},
166 name => $block->{name},
167 13 input => {},
168 },
169 });
170 }
171  
172 28 my $args = $call->{function}{arguments};
173 28 ▶ if (defined($args) && length($args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args and length $args) 20 8 28
Condition66%2/3
defined $args and length $args
!ll&&!rl&&r
0820
MC/DC50%1/2
defined $args and length $args
defined $args length $args
Truth table66%2/3
A and B
A= defined $argsB= length $args
ABresult
0X0
100
111
174 20 $block->{partial_json} .= $args;
175 $out .= _event('content_block_delta', {
176 type => 'content_block_delta',
177 index => $block->{anthropic_index},
178 20 delta => { type => 'input_json_delta', partial_json => $args },
179 });
180 }
181 }
182 }
183  
184 198 return $out;
185 }
186  
187  
188 61 ▶sub finish {
Subroutine finish: called CC=11 SCAR=24.2
Pod documented
189 61 my ($self, %args) = @_;
190 61 ▶ return '' if $self->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} 8 53 61
191  
192 53 my $out = '';
193 53 ▶ $out .= $self->start unless $self->{started};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"started"} 0 53 53
194 53 $self->{finished} = 1;
195  
196 53 for my $idx (@{$self->{open_blocks}}) {
197 58 $out .= _event('content_block_stop', {
198 type => 'content_block_stop',
199 index => $idx,
200 });
201 }
202 53 @{$self->{open_blocks}} = ();
203  
204 # A stream that emitted tool_use blocks and then finished with 'stop' (gpt-oss on
205 # vLLM-style servers) ends with stop_reason tool_use, the same rule as the non-streaming
206 # translation: tool calls present wins over 'stop' (core k248).
207 53 my $stop_reason = $args{stop_reason};
208 53 ▶ if (!defined $stop_reason) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $stop_reason) 53 0 53
209 53 ▶ my $finish_reason = $self->{finish_reason} // 'stop';
Condition100%2/2
$self->{"finish_reason"} // "stop"
l!l
3815
MC/DC100%1/1
$self->{"finish_reason"} // "stop"
$self->{"finish_reason"}
Truth table100%2/2
A
A= $self->{"finish_reason"}
Aresult
11
00
210 53 ▶ $stop_reason = (%{$self->{tool_blocks}} && $finish_reason eq 'stop')
Condition100%2/2
$self->{"stop_reason"} // "end_turn"
l!l
3715
MC/DC100%1/1
$self->{"stop_reason"} // "end_turn"
$self->{"stop_reason"}
Truth table100%2/2
A
A= $self->{"stop_reason"}
Aresult
11
00
211 ? 'tool_use'
212 53 ▶ : ($self->{stop_reason} // 'end_turn');
Branch: 1 branch
BranchTrueFalseTotal
%{$self->{'tool_blocks'};} && $finish_reason eq 'stop' ? : 1 52 53
Condition100%3/3
%{$self->{"tool_blocks"};} and $finish_reason eq "stop"
!ll&&!rl&&r
41111
MC/DC100%2/2
%{$self->{"tool_blocks"};} and $finish_reason eq "stop"
%{$self->{"tool_blocks"};} $finish_reason eq "stop"
Truth table100%3/3
A and B
A= %{$self->{"tool_blocks"};}B= $finish_reason eq "stop"
ABresult
0X0
100
111
213 }
214  
215 $out .= _event('message_delta', {
216 type => 'message_delta',
217 delta => {
218 stop_reason => $stop_reason,
219 stop_sequence => undef,
220 },
221 53 ▶ usage => { output_tokens => 0 + ($args{output_tokens} // $self->{output_tokens} // 0) },
Condition40%2/5
($args{"output_tokens"} // $self->{"output_tokens"}) // 0
l!l
530
$args{"output_tokens"} // $self->{"output_tokens"}
l!l&&r!l&&!r
0530
MC/DC0%0/2
($args{"output_tokens"} // $self->{"output_tokens"}) // 0
$args{"output_tokens"} $self->{"output_tokens"}
Truth table33%1/3
A // B
A= $args{"output_tokens"}B= $self->{"output_tokens"}
ABresult
1X1
011
000
222 });
223  
224 53 $out .= _event('message_stop', { type => 'message_stop' });
225 53 return $out;
226 }
227  
228  
229 10 ▶sub error_event {
Subroutine error_event: called CC=2 SCAR=7.0
Pod documented
230 10 my ($self, $status, $message, $type) = @_;
231 10 ▶ return '' if $self->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} 0 10 10
232 10 $self->{finished} = 1;
233 10 $self->{errored} = 1;
234 10 return _event('error', Langertha::Skeid::Protocol::Anthropic->error_body($status, $message, $type));
235 }
236  
237  
238 54 ▶sub errored { $_[0]->{errored} }
Subroutine errored: called CC=1 SCAR=0.0
Pod documented
239  
240  
241 64 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
Pod documented
242 64 my ($self) = @_;
243 64 return ($self->{input_tokens}, $self->{output_tokens}, $self->{text_bytes});
244 }
245  
246  
247 89 1;
248  
249 __END__
« blib/lib/Langertha/Skeid/KeyBroker/OpenBao.pm ↑ Summary lib/Langertha/Skeid/Protocol/Ollama.pm »