| 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"} // ""
$args{"message_id"} // 'msg_' . int(time * 1000)
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"}
Truth table50%1/2
A
A= $args{"message_id"}
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | 201 | ▶ | return '' if $self->{finished}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 82 | 3 | ▶ | my $message = $chunk->{error}{message} // 'upstream error'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$chunk->{"error"}{"message"} // "upstream error"
MC/DC0%0/1
$chunk->{"error"}{"message"} // "upstream error"
$chunk->{"error"}{"message"}
Truth table50%1/2
A
A= $chunk->{"error"}{"message"}
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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"}
$usage->{"prompt_tokens"} // $usage->{"input_tokens"}
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"}
| |||||||||||||||||||||||||||||||||||
| 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"}
$usage->{"completion_tokens"} // $usage->{"output_tokens"}
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"}
| |||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||
| 96 | 198 | ▶ | my $choice = (ref($chunk->{choices}) eq 'ARRAY' ? $chunk->{choices}[0] : undef) || {}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
(ref $chunk->{'choices'} eq 'ARRAY' ? $chunk->{'choices'}[0] : undef) || {}
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)
| |||||||||||||||||||||||||||||||||||
| 97 | 198 | ▶ | if (defined(my $reason = $choice->{finish_reason})) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 98 | 40 | $self->{finish_reason} = $reason; | |||||||||||||||||||||||||||||||||
| 99 | 40 | ▶ | $self->{stop_reason} = $reason eq 'tool_calls' ? 'tool_use' | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 100 | : $reason eq 'length' ? 'max_tokens' | ||||||||||||||||||||||||||||||||||
| 101 | : 'end_turn'; | ||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||
| 104 | 198 | ▶ | my $delta = $choice->{delta} || {}; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$choice->{'delta'} || {}
MC/DC100%1/1
$choice->{'delta'} || {}
$choice->{'delta'}
Truth table100%2/2
A
A= $choice->{'delta'}
| |||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $text and length $text
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
| |||||||||||||||||||||||||||||||||||
| 110 | 98 | ▶ | if (!$self->{text_started}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 133 | 28 | for my $call (@{$delta->{tool_calls}}) { | |||||||||||||||||||||||||||||||||
| 134 | 28 | ▶ | next unless ref($call) eq 'HASH'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 135 | 28 | ▶ | my $tool_idx = $call->{index} // 0; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$call->{"index"} // 0
MC/DC0%0/1
$call->{"index"} // 0
$call->{"index"}
Truth table50%1/2
A
A= $call->{"index"}
| |||||||||||||||||||||||||||||||||||
| 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}
}
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}
}
| |||||||||||||||||||||||||||||||||||
| 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"} // ""
$call->{"function"}{"name"} // ""
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"}
Truth table50%1/2
A
A= $call->{"function"}{"name"}
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 156 | 28 | ▶ | $block->{name} = $call->{function}{name} if defined $call->{function}{name}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||
| 158 | 28 | ▶ | unless ($block->{emitted_start}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $args and length $args
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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||
| 192 | 53 | my $out = ''; | |||||||||||||||||||||||||||||||||
| 193 | 53 | ▶ | $out .= $self->start unless $self->{started}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 209 | 53 | ▶ | my $finish_reason = $self->{finish_reason} // 'stop'; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"finish_reason"} // "stop"
MC/DC100%1/1
$self->{"finish_reason"} // "stop"
$self->{"finish_reason"}
Truth table100%2/2
A
A= $self->{"finish_reason"}
| |||||||||||||||||||||||||||||||||||
| 210 | 53 | ▶ | $stop_reason = (%{$self->{tool_blocks}} && $finish_reason eq 'stop') | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"stop_reason"} // "end_turn"
MC/DC100%1/1
$self->{"stop_reason"} // "end_turn"
$self->{"stop_reason"}
Truth table100%2/2
A
A= $self->{"stop_reason"}
| |||||||||||||||||||||||||||||||||||
| 211 | ? 'tool_use' | ||||||||||||||||||||||||||||||||||
| 212 | 53 | ▶ | : ($self->{stop_reason} // 'end_turn'); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
%{$self->{"tool_blocks"};} and $finish_reason eq "stop"
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"
| |||||||||||||||||||||||||||||||||||
| 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
$args{"output_tokens"} // $self->{"output_tokens"}
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"}
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||