lib/Langertha/Skeid/Protocol/Anthropic.pm

filter: Statementstmt 100% 183 / 183 Branchbran 85.4% 106 / 124 Conditioncond 66.3% 71 / 107 MC/DCmcdc 33.3% 18 / 54 Subroutinesub 100% 17 / 17 Podpod 100% 5 / 5 totaltotal 81.6% 400 / 490 CC 112 SCAR 37.1
CC
112
Cov
87%
request_to_openai
71.6
response_from_openai
22.5
_image_part
11.4
CRAP 40.9
« lib/Langertha/Skeid/UsageStore.pm ↑ Summary lib/Langertha/Skeid.pm »
1 package Langertha::Skeid::Protocol::Anthropic;
2 116 our $VERSION = '0.004';
3 # ABSTRACT: Translate between the Anthropic Messages format and the upstream OpenAI call
4 116 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 116 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 116 ▶use Langertha::Skeid::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 116 ▶use Langertha::Skeid::Protocol::Refusal;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 116 ▶use Langertha::Tool;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 116 ▶use Langertha::ToolCall;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 116 ▶use Langertha::ToolChoice;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # What a tool message says when its tool_result held images only.
13 116 my $TOOL_IMAGES_MOVED = 'The tool result is the images in the next message.';
14  
15  
16 198 ▶sub request_to_openai {
Subroutine request_to_openai: called CC=63 SCAR=42.7
Pod documented
17 198 my ($class, $body) = @_;
18 198 my @messages;
19  
20 198 ▶ if (defined $body->{system}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body->{"system"}) 9 189 198
21 9 ▶ if (ref($body->{system}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $body->{'system'} eq 'ARRAY') { } 2 7 9
22 2 ▶ my $txt = join('', map { ref($_) eq 'HASH' ? ($_->{text} // '') : "$_" } @{$body->{system}});
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 2 0 2
Condition50%1/2
$_->{"text"} // ""
l!l
20
MC/DC0%0/1
$_->{"text"} // ""
$_->{"text"}
Truth table50%1/2
A
A= $_->{"text"}
Aresult
11
00
23 2 ▶ push @messages, { role => 'system', content => $txt } if length $txt;
Branch: 1 branch
BranchTrueFalseTotal
if length $txt 1 1 2
24 } else {
25 7 ▶ push @messages, { role => 'system', content => "$body->{system}" } if length $body->{system};
Branch: 1 branch
BranchTrueFalseTotal
if length $body->{"system"} 6 1 7
26 }
27 }
28  
29 198 my $started; # a message that is not a system message has been seen
30 my @pending; # reminders waiting for the next user message
31  
32 198 ▶ for my $m (@{$body->{messages} || []}) {
Condition50%1/2
$body->{'messages'} || []
l!l
1980
MC/DC0%0/1
$body->{'messages'} || []
$body->{'messages'}
Truth table50%1/2
A
A= $body->{'messages'}
Aresult
11
00
33 274 ▶ next unless ref($m) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $m eq "HASH" 0 274 274
34 274 ▶ my $role = $m->{role} // 'user';
Condition50%1/2
$m->{"role"} // "user"
l!l
2740
MC/DC0%0/1
$m->{"role"} // "user"
$m->{"role"}
Truth table50%1/2
A
A= $m->{"role"}
Aresult
11
00
35 274 my $content = $m->{content};
36  
37 # A system message is never forwarded where it stands: a chat template wants its system
38 # message first. Before the first turn it is part of the one leading system message; after
39 # it, text of a user turn -- not of the leading one, whose prefix has to stay the same from
40 # turn to turn for the node's prefix cache.
41 274 ▶ if ($role eq 'system') {
Branch: 1 branch
BranchTrueFalseTotal
if ($role eq "system") 28 246 274
42 28 my $txt = _system_text($content);
43 28 ▶ next unless length $txt;
Branch: 1 branch
BranchTrueFalseTotal
unless length $txt 4 24 28
44 24 ▶ if (!$started) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($started) 5 19 24
45 5 ▶ if (@messages) {
Branch: 1 branch
BranchTrueFalseTotal
if (@messages) { } 4 1 5
46 4 $messages[0]{content} = join("\n\n", grep { length } $messages[0]{content}, $txt);
47 } else {
48 1 push @messages, { role => 'system', content => $txt };
49 }
50 5 next;
51 }
52 19 my $reminder = "<system-reminder>\n" . $txt . "\n</system-reminder>";
53 19 ▶ if (@messages && $messages[-1]{role} eq 'user') {
Branch: 1 branch
BranchTrueFalseTotal
if (@messages and $messages[-1]{'role'} eq 'user') { } 8 11 19
Condition66%2/3
@messages and $messages[-1]{"role"} eq "user"
!ll&&!rl&&r
0118
MC/DC50%1/2
@messages and $messages[-1]{"role"} eq "user"
@messages $messages[-1]{"role"} eq "user"
Truth table66%2/3
A and B
A= @messagesB= $messages[-1]{"role"} eq "user"
ABresult
0X0
100
111
54 8 _add_reminder($messages[-1], $reminder);
55 } else {
56 11 push @pending, $reminder;
57 }
58 19 next;
59 }
60 246 $started = 1;
61  
62 246 ▶ if (!ref($content)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $content) 202 44 246
63 202 ▶ _push_message(\@messages, \@pending, { role => $role, content => (defined($content) ? "$content" : '') });
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 202 0 202
64 202 next;
65 }
66  
67 44 ▶ if (ref($content) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq "ARRAY") 44 0 44
68 44 my @text;
69 my @parts; # text and image parts in client order, used once an image appears
70 44 my $has_image;
71 44 my @tool_calls;
72 44 my @tool_images; # image parts lifted out of this message's tool_result blocks
73  
74 44 for my $block (@$content) {
75 60 ▶ next unless ref($block) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $block eq "HASH" 0 60 60
76 60 ▶ my $type = $block->{type} // '';
Condition50%1/2
$block->{"type"} // ""
l!l
600
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table50%1/2
A
A= $block->{"type"}
Aresult
11
00
77  
78 60 ▶ if ($type eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "text") 15 45 60
79 15 ▶ push @text, ($block->{text} // '');
Condition50%1/2
$block->{"text"} // ""
l!l
150
MC/DC0%0/1
$block->{"text"} // ""
$block->{"text"}
Truth table50%1/2
A
A= $block->{"text"}
Aresult
11
00
80 15 ▶ push @parts, { type => 'text', text => ($block->{text} // '') };
Condition50%1/2
$block->{"text"} // ""
l!l
150
MC/DC0%0/1
$block->{"text"} // ""
$block->{"text"}
Truth table50%1/2
A
A= $block->{"text"}
Aresult
11
00
81 15 next;
82 }
83  
84 45 ▶ if ($type eq 'image' && $role ne 'assistant') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "image" and $role ne "assistant") 8 37 45
Condition66%2/3
$type eq "image" and $role ne "assistant"
!ll&&!rl&&r
3708
MC/DC50%1/2
$type eq "image" and $role ne "assistant"
$type eq "image" $role ne "assistant"
Truth table66%2/3
A and B
A= $type eq "image"B= $role ne "assistant"
ABresult
0X0
100
111
85 8 push @parts, _image_part($block);
86 6 $has_image = 1;
87 6 next;
88 }
89  
90 37 ▶ if ($type eq 'tool_use') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tool_use") 18 19 37
91 18 ▶ my $id = $block->{id} // ('toolu_' . int(rand(1_000_000)));
Condition50%1/2
$block->{"id"} // 'toolu_' . int(rand 1000000)
l!l
180
MC/DC0%0/1
$block->{"id"} // 'toolu_' . int(rand 1000000)
$block->{"id"}
Truth table50%1/2
A
A= $block->{"id"}
Aresult
11
00
92 18 ▶ my $name = $block->{name} // 'tool';
Condition50%1/2
$block->{"name"} // "tool"
l!l
180
MC/DC0%0/1
$block->{"name"} // "tool"
$block->{"name"}
Truth table50%1/2
A
A= $block->{"name"}
Aresult
11
00
93 18 ▶ my $args = Langertha::Skeid::Protocol::encode_json_text_safe($block->{input} || {});
Condition50%1/2
$block->{'input'} || {}
l!l
180
MC/DC0%0/1
$block->{'input'} || {}
$block->{'input'}
Truth table50%1/2
A
A= $block->{'input'}
Aresult
11
00
94 18 push @tool_calls, {
95 id => $id,
96 type => 'function',
97 function => {
98 name => $name,
99 arguments => $args,
100 },
101 };
102 18 next;
103 }
104  
105 19 ▶ if ($type eq 'tool_result') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tool_result") 19 0 19
106 19 ▶ my $tcid = $block->{tool_use_id} // $block->{id} // '';
Condition40%2/5
($block->{"tool_use_id"} // $block->{"id"}) // ""
l!l
190
$block->{"tool_use_id"} // $block->{"id"}
l!l&&r!l&&!r
1900
MC/DC0%0/2
($block->{"tool_use_id"} // $block->{"id"}) // ""
$block->{"tool_use_id"} $block->{"id"}
Truth table33%1/3
A // B
A= $block->{"tool_use_id"}B= $block->{"id"}
ABresult
1X1
011
000
107 19 my $val = $block->{content};
108 # An OpenAI tool message carries no image parts. Its images move to the one user
109 # message that follows this run of tool messages; the tool message keeps the rest.
110 19 ▶ if (ref($val) eq 'ARRAY' && grep { _is_image_block($_) } @$val) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "ARRAY" and grep {_is_image_block($_);} @$val) 7 12 19
Condition100%3/3
ref $val eq "ARRAY" and grep {_is_image_block($_);} @$val
!ll&&!rl&&r
757
MC/DC100%2/2
ref $val eq "ARRAY" and grep {_is_image_block($_);} @$val
ref $val eq "ARRAY" grep {_is_image_block($_);} @$val
Truth table100%3/3
A and B
A= ref $val eq "ARRAY"B= grep {_is_image_block($_);} @$val
ABresult
0X0
100
111
111 push @tool_images, { type => 'text', text => "Images from tool result $tcid:" },
112 7 map { _image_part($_) } grep { _is_image_block($_) } @$val;
113 6 my @rest = grep { !_is_image_block($_) } @$val;
114 6 ▶ $val = @rest ? \@rest : $TOOL_IMAGES_MOVED;
Branch: 1 branch
BranchTrueFalseTotal
@rest ? : 5 1 6
115 }
116 18 ▶ my $txt = ref($val) ? Langertha::Skeid::Protocol::encode_json_text_safe($val) : (defined($val) ? "$val" : '');
Branch: 2 branches
BranchTrueFalseTotal
ref $val ? : 10 8 18
defined $val ? : 8 0 8
117 18 _push_message(\@messages, \@pending, {
118 role => 'tool',
119 tool_call_id => $tcid,
120 content => $txt,
121 });
122 18 next;
123 }
124 }
125  
126 41 my $text = join('', @text);
127 41 ▶ if ($role eq 'assistant') {
Branch: 1 branch
BranchTrueFalseTotal
if ($role eq 'assistant') { } 18 23 41
128 18 my %msg = (role => 'assistant');
129 18 ▶ $msg{content} = $text if length $text;
Branch: 1 branch
BranchTrueFalseTotal
if length $text 2 16 18
130 18 ▶ $msg{tool_calls} = \@tool_calls if @tool_calls;
Branch: 1 branch
BranchTrueFalseTotal
if @tool_calls 17 1 18
131 18 ▶ $msg{content} = '' if !exists($msg{content}) && !exists($msg{tool_calls});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $msg{"content"} or exists $msg{"tool_calls"} 0 18 18
Condition66%2/3
exists $msg{"content"} or exists $msg{"tool_calls"}
l!l&&r!l&&!r
2160
MC/DC0%0/2
exists $msg{"content"} or exists $msg{"tool_calls"}
exists $msg{"content"} exists $msg{"tool_calls"}
Truth table66%2/3
A or B
A= exists $msg{"content"}B= exists $msg{"tool_calls"}
ABresult
1X1
011
000
132 18 _push_message(\@messages, \@pending, \%msg);
133 } else {
134 # The tool-result images and the client's own content of this one Anthropic message
135 # are one user message (content arrays concatenated); never merged across messages.
136 23 ▶ my @client = $has_image ? @parts : length($text) ? ({ type => 'text', text => $text }) : ();
Branch: 2 branches
BranchTrueFalseTotal
$has_image ? : 5 18 23
length $text ? : 7 11 18
137 23 ▶ if (@tool_images) {
Branch: 3 branches
BranchTrueFalseTotal
if (@tool_images) { } 5 18 23
elsif ($has_image) { } 5 13 18
elsif (length $text) { } 3 10 13
138 5 _push_message(\@messages, \@pending, { role => 'user', content => [ @tool_images, @client ] });
139 } elsif ($has_image) {
140 5 _push_message(\@messages, \@pending, { role => $role, content => \@parts });
141 } elsif (length $text) {
142 3 _push_message(\@messages, \@pending, { role => $role, content => $text });
143 }
144 }
145 }
146 }
147  
148 # Reminders no user message came for end the conversation as one.
149 194 ▶ push @messages, { role => 'user', content => join("\n\n", @pending) } if @pending;
Branch: 1 branch
BranchTrueFalseTotal
if @pending 3 191 194
150  
151 my %out = (
152 model => ($body->{model} // ''),
153 messages => \@messages,
154 (defined($body->{max_tokens}) ? (max_tokens => 0 + $body->{max_tokens}) : ()),
155 (defined($body->{temperature}) ? (temperature => 0 + $body->{temperature}) : ()),
156 194 ▶ (defined($body->{top_p}) ? (top_p => 0 + $body->{top_p}) : ()),
Branch: 3 branches
BranchTrueFalseTotal
defined $body->{'max_tokens'} ? : 193 1 194
defined $body->{'temperature'} ? : 1 193 194
defined $body->{'top_p'} ? : 0 194 194
Condition50%1/2
$body->{"model"} // ""
l!l
1940
MC/DC0%0/1
$body->{"model"} // ""
$body->{"model"}
Truth table50%1/2
A
A= $body->{"model"}
Aresult
11
00
157 );
158  
159 194 ▶ if (ref($body->{tools}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $body->{"tools"} eq "ARRAY") 8 186 194
160 # Skeid forwards one OpenAI chat call (ADR 0001); a provider built-in has no function shape
161 # there and nothing downstream would run it. from_list croaks on one (Langertha k210), so
162 # ask the non-croaking classifier first and refuse with a message the client can act on.
163 # Non-hash entries are left to from_list, which skips them.
164 8 my $i = -1;
165 8 for my $tool (@{$body->{tools}}) {
166 10 $i++;
167 10 ▶ next unless ref($tool) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tool eq "HASH" 0 10 10
168 10 my ($category, undef, $label) = Langertha::Tool->classify($tool, 'anthropic');
169 10 ▶ next if $category eq 'function';
Branch: 1 branch
BranchTrueFalseTotal
if $category eq "function" 6 4 10
170 4 ▶ Langertha::Skeid::Protocol::Refusal->refuse("tools[$i]: tool type '" . ($label // '') . "' ($category) is not supported: "
Condition50%1/2
$label // ""
l!l
40
MC/DC0%0/1
$label // ""
$label
Truth table50%1/2
A
A= $label
Aresult
11
00
171 . "skeid does not forward provider built-in tools, only function tools");
172 }
173 4 my $tools = Langertha::Tool->from_list($body->{tools});
174 4 $out{tools} = [ map { $_->to_openai } @$tools ];
175 }
176  
177 190 ▶ if (defined $body->{tool_choice}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body->{"tool_choice"}) 1 189 190
178 1 my $tc = Langertha::ToolChoice->from_hash($body->{tool_choice});
179 1 ▶ if ($tc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tc) 1 0 1
180 1 my $oai_tc = $tc->to_openai;
181 1 ▶ $out{tool_choice} = $oai_tc if defined $oai_tc;
Branch: 1 branch
BranchTrueFalseTotal
if defined $oai_tc 1 0 1
182 }
183 }
184  
185 190 return \%out;
186 }
187  
188 # The text of a system message: a string, or the text blocks of a block list joined like the
189 # top-level system.
190 28 ▶sub _system_text {
Subroutine _system_text: called CC=6 SCAR=19.0
191 28 my ($content) = @_;
192 28 ▶ return defined($content) ? "$content" : '' unless ref($content);
Branch: 2 branches
BranchTrueFalseTotal
unless ref $content 24 4 28
defined $content ? : 24 0 24
193 4 ▶ return '' unless ref($content) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $content eq "ARRAY" 0 4 4
194 4 ▶ return join('', map { ref($_) eq 'HASH' ? ($_->{text} // '') : "$_" } @$content);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 5 0 5
Condition50%1/2
$_->{"text"} // ""
l!l
50
MC/DC0%0/1
$_->{"text"} // ""
$_->{"text"}
Truth table50%1/2
A
A= $_->{"text"}
Aresult
11
00
195 }
196  
197 # Adds a reminder to a user message, behind what it says or in front of it: text joined by a
198 # blank line, or a text part of its own when the content is an array of parts.
199 12 ▶sub _add_reminder {
Subroutine _add_reminder: called CC=5 SCAR=16.1
200 12 my ($msg, $reminder, $in_front) = @_;
201 12 ▶ if (ref($msg->{content}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msg->{"content"} eq "ARRAY") 4 8 12
202 4 my $part = { type => 'text', text => $reminder };
203 4 ▶ $in_front ? unshift(@{$msg->{content}}, $part) : push(@{$msg->{content}}, $part);
Branch: 1 branch
BranchTrueFalseTotal
$in_front ? : 2 2 4
204 4 return;
205 }
206 8 ▶ my $text = $msg->{content} // '';
Condition50%1/2
$msg->{"content"} // ""
l!l
80
MC/DC0%0/1
$msg->{"content"} // ""
$msg->{"content"}
Truth table50%1/2
A
A= $msg->{"content"}
Aresult
11
00
207 8 ▶ $msg->{content} = join("\n\n", grep { length } $in_front ? ($reminder, $text) : ($text, $reminder));
Branch: 1 branch
BranchTrueFalseTotal
$in_front ? : 2 6 8
208 }
209  
210 # Adds a translated message. Waiting reminders go in front of a user message's own content; a
211 # tool message leaves them waiting, so nothing comes between an assistant's tool calls and
212 # their answers; anything else gets them as a user message in front of it.
213 251 ▶sub _push_message {
Subroutine _push_message: called CC=4 SCAR=13.9
214 251 my ($messages, $pending, $msg) = @_;
215 251 ▶ if (@$pending && $msg->{role} ne 'tool') {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pending and $msg->{"role"} ne "tool") 6 245 251
Condition100%3/3
@$pending and $msg->{"role"} ne "tool"
!ll&&!rl&&r
24236
MC/DC100%2/2
@$pending and $msg->{"role"} ne "tool"
@$pending $msg->{"role"} ne "tool"
Truth table100%3/3
A and B
A= @$pendingB= $msg->{"role"} ne "tool"
ABresult
0X0
100
111
216 6 my $reminders = join("\n\n", splice(@$pending));
217 6 ▶ if ($msg->{role} eq 'user') {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->{'role'} eq 'user') { } 4 2 6
218 4 _add_reminder($msg, $reminders, 1);
219 } else {
220 2 push @$messages, { role => 'user', content => $reminders };
221 }
222 }
223 251 push @$messages, $msg;
224 }
225  
226 40 ▶sub _is_image_block {
Subroutine _is_image_block: called CC=3 SCAR=11.7
227 40 my ($block) = @_;
228 40 ▶ return ref($block) eq 'HASH' && ($block->{type} // '') eq 'image';
Condition60%3/5
ref $block eq 'HASH' && ($block->{'type'} // '') eq 'image'
!ll&&!rl&&r
02020
$block->{"type"} // ""
l!l
400
MC/DC0%0/2
ref $block eq 'HASH' && ($block->{'type'} // '') eq 'image'
ref $block eq 'HASH' $block->{"type"}
Truth table33%1/3
A && B
A= ref $block eq 'HASH'B= $block->{"type"}
ABresult
0X0
100
111
229 }
230  
231 # An Anthropic image block as an OpenAI image_url part: a base64 source becomes a data URL
232 # with its media_type, a url source is passed as the URL. Any other source (a Files API
233 # file_id) has no OpenAI-chat equivalent and is refused like a built-in tool.
234 15 ▶sub _image_part {
Subroutine _image_part: called CC=10 SCAR=24.3
235 15 my ($block) = @_;
236 15 ▶ my $source = ref($block->{source}) eq 'HASH' ? $block->{source} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $block->{'source'} eq 'HASH' ? : 15 0 15
237 15 ▶ my $kind = $source->{type} // '';
Condition50%1/2
$source->{"type"} // ""
l!l
150
MC/DC0%0/1
$source->{"type"} // ""
$source->{"type"}
Truth table50%1/2
A
A= $source->{"type"}
Aresult
11
00
238 return Langertha::Skeid::Protocol::image_url_part(undef, $source->{data}, $source->{media_type})
239 15 ▶ if $kind eq 'base64' && defined($source->{data}) && !ref($source->{data});
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "base64" and defined $source->{"data"} and not ref $source->{"data"} 10 5 15
Condition66%4/6
$kind eq "base64" and defined $source->{"data"} and not ref $source->{"data"}
!ll&&!rl&&r
5010
$kind eq "base64" and defined $source->{"data"}
!ll&&!rl&&r
5010
MC/DC33%1/3
$kind eq "base64" and defined $source->{"data"} and not ref $source->{"data"}
$kind eq "base64" defined $source->{"data"} not ref $source->{"data"}
Truth table50%2/4
A and B and not(C)
A= $kind eq "base64"B= defined $source->{"data"}C= not ref $source->{"data"}
ABCresult
0XX0
10X0
1100
1111
240 return Langertha::Skeid::Protocol::image_url_part($source->{url})
241 5 ▶ if $kind eq 'url' && defined($source->{url}) && !ref($source->{url}) && length($source->{url});
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "url" and defined $source->{"url"} and not ref $source->{"url"} and length $source->{"url"} 2 3 5
Condition66%6/9
$kind eq "url" and defined $source->{"url"} and not ref $source->{"url"} and length $source->{"url"}
!ll&&!rl&&r
302
$kind eq "url" and defined $source->{"url"} and not ref $source->{"url"}
!ll&&!rl&&r
302
$kind eq "url" and defined $source->{"url"}
!ll&&!rl&&r
302
MC/DC25%1/4
$kind eq "url" and defined $source->{"url"} and not ref $source->{"url"} and length $source->{"url"}
$kind eq "url" defined $source->{"url"} not ref $source->{"url"} length $source->{"url"}
Truth table40%2/5
A and B and not(C) and D
A= $kind eq "url"B= defined $source->{"url"}C= not ref $source->{"url"}D= length $source->{"url"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
242 3 Langertha::Skeid::Protocol::Refusal->refuse("image source type '$kind' is not supported: skeid forwards base64 and url images only");
243 }
244  
245  
246 105 ▶sub response_from_openai {
Subroutine response_from_openai: called CC=21 SCAR=31.1
Pod documented
247 105 my ($class, $res, $default_model) = @_;
248  
249 105 ▶ my $choice = (ref($res->{choices}) eq 'ARRAY' ? $res->{choices}[0] : {}) || {};
Branch: 1 branch
BranchTrueFalseTotal
ref $res->{'choices'} eq 'ARRAY' ? : 105 0 105
Condition50%1/2
(ref $res->{'choices'} eq 'ARRAY' ? $res->{'choices'}[0] : {}) || {}
l!l
1050
MC/DC0%0/1
(ref $res->{'choices'} eq 'ARRAY' ? $res->{'choices'}[0] : {}) || {}
(ref $res->{'choices'} eq 'ARRAY' ? $res->{'choices'}[0] : {})
Truth table50%1/2
A
A= (ref $res->{'choices'} eq 'ARRAY' ? $res->{'choices'}[0] : {})
Aresult
11
00
250 105 ▶ my $msg = $choice->{message} || {};
Condition50%1/2
$choice->{'message'} || {}
l!l
1050
MC/DC0%0/1
$choice->{'message'} || {}
$choice->{'message'}
Truth table50%1/2
A
A= $choice->{'message'}
Aresult
11
00
251 105 ▶ my $text = $msg->{content} // '';
Condition100%2/2
$msg->{"content"} // ""
l!l
1023
MC/DC100%1/1
$msg->{"content"} // ""
$msg->{"content"}
Truth table100%2/2
A
A= $msg->{"content"}
Aresult
11
00
252 105 ▶ my @calls = Langertha::ToolCall->extract('openai', $res || {});
Condition50%1/2
$res || {}
l!l
1050
MC/DC0%0/1
$res || {}
$res
Truth table50%1/2
A
A= $res
Aresult
11
00
253  
254 105 ▶ if (!@calls && length($text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @calls and length $text) 100 5 105
Condition66%2/3
not @calls and length $text
!ll&&!rl&&r
50100
MC/DC50%1/2
not @calls and length $text
not @calls length $text
Truth table66%2/3
not(A) and B
A= not @callsB= length $text
ABresult
0X0
100
111
255 100 my ($clean, $extracted) = Langertha::ToolCall->extract_hermes_from_text($text);
256 100 $text = $clean;
257 100 @calls = @$extracted;
258 }
259  
260 105 my @content;
261 105 ▶ push @content, { type => 'text', text => $text } if length $text;
Branch: 1 branch
BranchTrueFalseTotal
if length $text 101 4 105
262 105 my $i = 0;
263 105 for my $call (@calls) {
264 6 $i++;
265 6 push @content, $call->to_anthropic_block( fallback_id => "toolu_skeid_$i" );
266 }
267  
268 # gpt-oss on vLLM-style servers (seen live on AKI.IO) answers a tool call with finish_reason
269 # 'stop'. The reply carries tool_use blocks, and an Anthropic client only runs them when
270 # stop_reason says tool_use, so tool calls present wins over 'stop' -- the rule core applies
271 # to Response.finish_reason (core k248). 'length' and the rest keep their mapping.
272 105 ▶ my $fr = $choice->{finish_reason} // 'stop';
Condition50%1/2
$choice->{"finish_reason"} // "stop"
l!l
1050
MC/DC0%0/1
$choice->{"finish_reason"} // "stop"
$choice->{"finish_reason"}
Truth table50%1/2
A
A= $choice->{"finish_reason"}
Aresult
11
00
273 105 ▶ my $stop_reason = ($fr eq 'tool_calls' || (@calls && $fr eq 'stop')) ? 'tool_use'
Branch: 2 branches
BranchTrueFalseTotal
$fr eq 'tool_calls' || @calls && $fr eq 'stop' ? : 5 100 105
$fr eq 'length' ? : 1 99 100
Condition100%6/6
$fr eq "tool_calls" or @calls and $fr eq "stop"
l!l&&r!l&&!r
32100
@calls and $fr eq "stop"
!ll&&!rl&&r
9912
MC/DC100%3/3
$fr eq "tool_calls" or @calls and $fr eq "stop"
$fr eq "tool_calls" @calls $fr eq "stop"
Truth table100%4/4
A or B and C
A= $fr eq "tool_calls"B= @callsC= $fr eq "stop"
ABCresult
1XX1
0111
00X0
0100
274 : $fr eq 'length' ? 'max_tokens'
275 : 'end_turn';
276  
277 return {
278 id => ($res->{id} ? ('msg_' . $res->{id}) : ('msg_' . int(time * 1000))),
279 type => 'message',
280 role => 'assistant',
281 model => ($res->{model} // $default_model),
282 content => \@content,
283 stop_reason => $stop_reason,
284 stop_sequence => undef,
285 usage => {
286 input_tokens => 0 + (($res->{usage} || {})->{prompt_tokens} // 0),
287 105 ▶ output_tokens => 0 + (($res->{usage} || {})->{completion_tokens} // 0),
Branch: 1 branch
BranchTrueFalseTotal
$res->{'id'} ? : 105 0 105
Condition81%9/11
$res->{"model"} // $default_model
l!l&&r!l&&!r
10500
+($res->{"usage"} || {})->{"prompt_tokens"} // 0
l!l
1023
$res->{'usage'} || {}
l!l
1041
+($res->{"usage"} || {})->{"completion_tokens"} // 0
l!l
1023
$res->{'usage'} || {}
l!l
1041
MC/DC0%0/2
$res->{"model"} // $default_model
$res->{"model"} $default_model
MC/DC100%1/1
+($res->{"usage"} || {})->{"prompt_tokens"} // 0
$res->{'usage'}
MC/DC100%1/1
+($res->{"usage"} || {})->{"completion_tokens"} // 0
$res->{'usage'}
Truth table33%1/3
A // B
A= $res->{"model"}B= $default_model
ABresult
1X1
011
000
Truth table100%2/2
A
A= $res->{'usage'}
Aresult
11
00
Truth table100%2/2
A
A= $res->{'usage'}
Aresult
11
00
288 },
289 };
290 }
291  
292  
293 116 my %ERROR_TYPE_FOR_STATUS = (
294 400 => 'invalid_request_error',
295 401 => 'authentication_error',
296 402 => 'billing_error',
297 403 => 'permission_error',
298 404 => 'not_found_error',
299 409 => 'conflict_error',
300 413 => 'request_too_large',
301 429 => 'rate_limit_error',
302 500 => 'api_error',
303 504 => 'timeout_error',
304 529 => 'overloaded_error',
305 );
306  
307 116 my %KNOWN_ERROR_TYPE = map { $_ => 1 } values %ERROR_TYPE_FOR_STATUS;
308  
309 73 ▶sub error_type_for_status {
Subroutine error_type_for_status: called CC=4 SCAR=13.9
Pod documented
310 73 my ($class, $status) = @_;
311 73 ▶ $status = 0 + ($status // 500);
Condition50%1/2
$status // 500
l!l
730
MC/DC0%0/1
$status // 500
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
312 73 ▶ return $ERROR_TYPE_FOR_STATUS{$status} // ($status >= 500 ? 'api_error' : 'invalid_request_error');
Branch: 1 branch
BranchTrueFalseTotal
$status >= 500 ? : 18 1 19
Condition100%2/2
$ERROR_TYPE_FOR_STATUS{$status} // ($status >= 500 ? "api_error" : "invalid_request_error")
l!l
5419
MC/DC100%1/1
$ERROR_TYPE_FOR_STATUS{$status} // ($status >= 500 ? "api_error" : "invalid_request_error")
$ERROR_TYPE_FOR_STATUS{$status}
Truth table100%2/2
A
A= $ERROR_TYPE_FOR_STATUS{$status}
Aresult
11
00
313 }
314  
315  
316 60 ▶sub error_body {
Subroutine error_body: called CC=4 SCAR=13.9
Pod documented
317 60 my ($class, $status, $message, $type) = @_;
318 $type = $class->error_type_for_status($status)
319 60 ▶ unless defined($type) && $KNOWN_ERROR_TYPE{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and $KNOWN_ERROR_TYPE{$type} 59 1 60
Condition100%3/3
defined $type and $KNOWN_ERROR_TYPE{$type}
!ll&&!rl&&r
5721
MC/DC100%2/2
defined $type and $KNOWN_ERROR_TYPE{$type}
defined $type $KNOWN_ERROR_TYPE{$type}
Truth table100%3/3
A and B
A= defined $typeB= $KNOWN_ERROR_TYPE{$type}
ABresult
0X0
100
111
320 return {
321 60 ▶ type => 'error',
Condition50%1/2
$message // ""
l!l
600
MC/DC0%0/1
$message // ""
$message
Truth table50%1/2
A
A= $message
Aresult
11
00
322 error => {
323 type => $type,
324 message => ($message // ''),
325 },
326 };
327 }
328  
329  
330 19 ▶sub manifest_endpoint {
Subroutine manifest_endpoint: called CC=1 SCAR=0.0
Pod documented
331 return {
332 19 dialect => 'anthropic-compat',
333 path => '',
334 capabilities => [qw(
335 chat streaming system_prompt
336 tools_native tools_hermes
337 tool_choice_auto tool_choice_any tool_choice_none tool_choice_named
338 temperature response_size
339 image_input
340 )],
341 };
342 }
343  
344  
345 116 1;
346  
347 __END__
« lib/Langertha/Skeid/UsageStore.pm ↑ Summary lib/Langertha/Skeid.pm »