| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 9 | ▶ | if (ref($body->{system}) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 2 | ▶ | my $txt = join('', map { ref($_) eq 'HASH' ? ($_->{text} // '') : "$_" } @{$body->{system}}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$_->{"text"} // ""
MC/DC0%0/1
$_->{"text"} // ""
$_->{"text"}
Truth table50%1/2
A
A= $_->{"text"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 2 | ▶ | push @messages, { role => 'system', content => $txt } if length $txt; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 7 | ▶ | push @messages, { role => 'system', content => "$body->{system}" } if length $body->{system}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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'} || []
MC/DC0%0/1
$body->{'messages'} || []
$body->{'messages'}
Truth table50%1/2
A
A= $body->{'messages'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 274 | ▶ | next unless ref($m) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 274 | ▶ | my $role = $m->{role} // 'user'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$m->{"role"} // "user"
MC/DC0%0/1
$m->{"role"} // "user"
$m->{"role"}
Truth table50%1/2
A
A= $m->{"role"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 28 | my $txt = _system_text($content); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 28 | ▶ | next unless length $txt; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 24 | ▶ | if (!$started) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 5 | ▶ | if (@messages) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
@messages and $messages[-1]{"role"} eq "user"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 202 | ▶ | _push_message(\@messages, \@pending, { role => $role, content => (defined($content) ? "$content" : '') }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 202 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 44 | ▶ | if (ref($content) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 60 | ▶ | my $type = $block->{type} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$block->{"type"} // ""
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table50%1/2
A
A= $block->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 60 | ▶ | if ($type eq 'text') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 15 | ▶ | push @text, ($block->{text} // ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$block->{"text"} // ""
MC/DC0%0/1
$block->{"text"} // ""
$block->{"text"}
Truth table50%1/2
A
A= $block->{"text"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 15 | ▶ | push @parts, { type => 'text', text => ($block->{text} // '') }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$block->{"text"} // ""
MC/DC0%0/1
$block->{"text"} // ""
$block->{"text"}
Truth table50%1/2
A
A= $block->{"text"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 15 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 45 | ▶ | if ($type eq 'image' && $role ne 'assistant') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$type eq "image" and $role ne "assistant"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 18 | ▶ | my $id = $block->{id} // ('toolu_' . int(rand(1_000_000))); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$block->{"id"} // 'toolu_' . int(rand 1000000)
MC/DC0%0/1
$block->{"id"} // 'toolu_' . int(rand 1000000)
$block->{"id"}
Truth table50%1/2
A
A= $block->{"id"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 18 | ▶ | my $name = $block->{name} // 'tool'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$block->{"name"} // "tool"
MC/DC0%0/1
$block->{"name"} // "tool"
$block->{"name"}
Truth table50%1/2
A
A= $block->{"name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 18 | ▶ | my $args = Langertha::Skeid::Protocol::encode_json_text_safe($block->{input} || {}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$block->{'input'} || {}
MC/DC0%0/1
$block->{'input'} || {}
$block->{'input'}
Truth table50%1/2
A
A= $block->{'input'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 19 | ▶ | my $tcid = $block->{tool_use_id} // $block->{id} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition40%2/5
($block->{"tool_use_id"} // $block->{"id"}) // ""
$block->{"tool_use_id"} // $block->{"id"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
ref $val eq "ARRAY" and grep {_is_image_block($_);} @$val
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 18 | ▶ | my $txt = ref($val) ? Langertha::Skeid::Protocol::encode_json_text_safe($val) : (defined($val) ? "$val" : ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 18 | my %msg = (role => 'assistant'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 18 | ▶ | $msg{content} = $text if length $text; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 18 | ▶ | $msg{tool_calls} = \@tool_calls if @tool_calls; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 18 | ▶ | $msg{content} = '' if !exists($msg{content}) && !exists($msg{tool_calls}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $msg{"content"} or exists $msg{"tool_calls"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 23 | ▶ | if (@tool_images) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$body->{"model"} // ""
MC/DC0%0/1
$body->{"model"} // ""
$body->{"model"}
Truth table50%1/2
A
A= $body->{"model"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 194 | ▶ | if (ref($body->{tools}) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 10 | my ($category, undef, $label) = Langertha::Tool->classify($tool, 'anthropic'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 10 | ▶ | next if $category eq 'function'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 4 | ▶ | Langertha::Skeid::Protocol::Refusal->refuse("tools[$i]: tool type '" . ($label // '') . "' ($category) is not supported: " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$label // ""
MC/DC0%0/1
$label // ""
$label
Truth table50%1/2
A
A= $label
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 1 | my $tc = Langertha::ToolChoice->from_hash($body->{tool_choice}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 1 | ▶ | if ($tc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | my $oai_tc = $tc->to_openai; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 1 | ▶ | $out{tool_choice} = $oai_tc if defined $oai_tc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 4 | ▶ | return '' unless ref($content) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 4 | ▶ | return join('', map { ref($_) eq 'HASH' ? ($_->{text} // '') : "$_" } @$content); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$_->{"text"} // ""
MC/DC0%0/1
$_->{"text"} // ""
$_->{"text"}
Truth table50%1/2
A
A= $_->{"text"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 4 | my $part = { type => 'text', text => $reminder }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 4 | ▶ | $in_front ? unshift(@{$msg->{content}}, $part) : push(@{$msg->{content}}, $part); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 8 | ▶ | my $text = $msg->{content} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$msg->{"content"} // ""
MC/DC0%0/1
$msg->{"content"} // ""
$msg->{"content"}
Truth table50%1/2
A
A= $msg->{"content"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 8 | ▶ | $msg->{content} = join("\n\n", grep { length } $in_front ? ($reminder, $text) : ($text, $reminder)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
@$pending and $msg->{"role"} ne "tool"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 6 | my $reminders = join("\n\n", splice(@$pending)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 6 | ▶ | if ($msg->{role} eq 'user') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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'
$block->{"type"} // ""
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 15 | ▶ | my $kind = $source->{type} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source->{"type"} // ""
MC/DC0%0/1
$source->{"type"} // ""
$source->{"type"}
Truth table50%1/2
A
A= $source->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
$kind eq "base64" and defined $source->{"data"} and not ref $source->{"data"}
$kind eq "base64" and defined $source->{"data"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%6/9
$kind eq "url" and defined $source->{"url"} and not ref $source->{"url"} and length $source->{"url"}
$kind eq "url" and defined $source->{"url"} and not ref $source->{"url"}
$kind eq "url" and defined $source->{"url"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
(ref $res->{'choices'} eq 'ARRAY' ? $res->{'choices'}[0] : {}) || {}
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] : {})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 105 | ▶ | my $msg = $choice->{message} || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$choice->{'message'} || {}
MC/DC0%0/1
$choice->{'message'} || {}
$choice->{'message'}
Truth table50%1/2
A
A= $choice->{'message'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 105 | ▶ | my $text = $msg->{content} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$msg->{"content"} // ""
MC/DC100%1/1
$msg->{"content"} // ""
$msg->{"content"}
Truth table100%2/2
A
A= $msg->{"content"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 105 | ▶ | my @calls = Langertha::ToolCall->extract('openai', $res || {}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$res || {}
MC/DC0%0/1
$res || {}
$res
Truth table50%1/2
A
A= $res
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 105 | ▶ | if (!@calls && length($text)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not @calls and length $text
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$choice->{"finish_reason"} // "stop"
$choice->{"finish_reason"}
Truth table50%1/2
A
A= $choice->{"finish_reason"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 105 | ▶ | my $stop_reason = ($fr eq 'tool_calls' || (@calls && $fr eq 'stop')) ? 'tool_use' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%6/6
$fr eq "tool_calls" or @calls and $fr eq "stop"
@calls and $fr eq "stop"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition81%9/11
$res->{"model"} // $default_model
+($res->{"usage"} || {})->{"prompt_tokens"} // 0
$res->{'usage'} || {}
+($res->{"usage"} || {})->{"completion_tokens"} // 0
$res->{'usage'} || {}
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
Truth table100%2/2
A
A= $res->{'usage'}
Truth table100%2/2
A
A= $res->{'usage'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$status // 500
$status
Truth table50%1/2
A
A= $status
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 73 | ▶ | return $ERROR_TYPE_FOR_STATUS{$status} // ($status >= 500 ? 'api_error' : 'invalid_request_error'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$ERROR_TYPE_FOR_STATUS{$status} // ($status >= 500 ? "api_error" : "invalid_request_error")
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}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $type and $KNOWN_ERROR_TYPE{$type}
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}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 60 | ▶ | type => 'error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$message // ""
MC/DC0%0/1
$message // ""
$message
Truth table50%1/2
A
A= $message
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||