| 1 | package Langertha::Skeid::Protocol::Anthropic; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 70 | our $VERSION = '0.003'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Translate between the Anthropic Messages format and the upstream OpenAI call | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 70 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 70 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 70 | ▶ | use Langertha::Skeid::Protocol; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 70 | ▶ | use Langertha::Skeid::Protocol::Refusal; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 70 | ▶ | use Langertha::Tool; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 70 | ▶ | use Langertha::ToolCall; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 70 | ▶ | 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 | 70 | my $TOOL_IMAGES_MOVED = 'The tool result is the images in the next message.'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 124 | ▶ | sub request_to_openai { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
request_to_openai: called CC=53 SCAR=43.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 124 | my ($class, $body) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 124 | my @messages; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 124 | ▶ | if (defined $body->{system}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | ▶ | if (ref($body->{system}) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | my $txt = join('', map { ref($_) eq 'HASH' ? ($_->{text} // '') : "$_" } @{$body->{system}}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$_->{"text"} // ""
MC/DC0%0/1
$_->{"text"} // ""
$_->{"text"}
Truth table0%0/2
A
A= $_->{"text"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | push @messages, { role => 'system', content => $txt } if length $txt; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | push @messages, { role => 'system', content => "$body->{system}" }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 124 | ▶ | 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'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 140 | ▶ | next unless ref($m) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 140 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 140 | my $content = $m->{content}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 140 | ▶ | if (!ref($content)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 117 | ▶ | push @messages, { role => $role, content => (defined($content) ? "$content" : '') }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 117 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 23 | ▶ | if (ref($content) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 23 | my @text; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | my @parts; # text and image parts in client order, used once an image appears | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 23 | my $has_image; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 23 | my @tool_calls; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 23 | my @tool_images; # image parts lifted out of this message's tool_result blocks | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 23 | for my $block (@$content) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 32 | ▶ | next unless ref($block) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 32 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 32 | ▶ | if ($type eq 'text') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 6 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 6 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 6 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 26 | ▶ | 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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 5 | push @parts, _image_part($block); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 3 | $has_image = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 3 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 21 | ▶ | if ($type eq 'tool_use') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 10 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 10 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 10 | ▶ | 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'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 10 | push @tool_calls, { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | id => $id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | type => 'function', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | function => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | name => $name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | arguments => $args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 10 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 11 | ▶ | if ($type eq 'tool_result') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 11 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 11 | my $val = $block->{content}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # An OpenAI tool message carries no image parts. Its images move to the one user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # message that follows this run of tool messages; the tool message keeps the rest. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 11 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | push @tool_images, { type => 'text', text => "Images from tool result $tcid:" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | map { _image_part($_) } grep { _is_image_block($_) } @$val; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 2 | my @rest = grep { !_is_image_block($_) } @$val; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | ▶ | $val = @rest ? \@rest : $TOOL_IMAGES_MOVED; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 10 | ▶ | my $txt = ref($val) ? Langertha::Skeid::Protocol::encode_json_text_safe($val) : (defined($val) ? "$val" : ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 10 | push @messages, { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | role => 'tool', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | tool_call_id => $tcid, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | content => $txt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 10 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 20 | ▶ | push @messages, { role => 'user', content => \@tool_images } if @tool_images; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 20 | my $text = join('', @text); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 20 | ▶ | if ($role eq 'assistant') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 9 | my %msg = (role => 'assistant'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 9 | ▶ | $msg{content} = $text if length $text; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 9 | ▶ | $msg{tool_calls} = \@tool_calls if @tool_calls; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 9 | ▶ | $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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 9 | push @messages, \%msg; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } elsif ($has_image) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 2 | push @messages, { role => $role, content => \@parts }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | } elsif (length $text) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 2 | push @messages, { role => $role, content => $text }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | my %out = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | model => ($body->{model} // ''), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | messages => \@messages, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | (defined($body->{max_tokens}) ? (max_tokens => 0 + $body->{max_tokens}) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | (defined($body->{temperature}) ? (temperature => 0 + $body->{temperature}) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 120 | ▶ | (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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 120 | ▶ | if (ref($body->{tools}) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | # Skeid forwards one OpenAI chat call (ADR 0001); a provider built-in has no function shape | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | # there and nothing downstream would run it. from_list croaks on one (Langertha k210), so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | # ask the non-croaking classifier first and refuse with a message the client can act on. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | # Non-hash entries are left to from_list, which skips them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 6 | my $i = -1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 6 | for my $tool (@{$body->{tools}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 8 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 8 | ▶ | next unless ref($tool) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 8 | my ($category, undef, $label) = Langertha::Tool->classify($tool, 'anthropic'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 8 | ▶ | next if $category eq 'function'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | . "skeid does not forward provider built-in tools, only function tools"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 2 | my $tools = Langertha::Tool->from_list($body->{tools}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 2 | $out{tools} = [ map { $_->to_openai } @$tools ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 116 | ▶ | if (defined $body->{tool_choice}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 1 | my $tc = Langertha::ToolChoice->from_hash($body->{tool_choice}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 1 | ▶ | if ($tc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | my $oai_tc = $tc->to_openai; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | ▶ | $out{tool_choice} = $oai_tc if defined $oai_tc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 116 | return \%out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 16 | ▶ | sub _is_image_block { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_image_block: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 16 | my ($block) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 16 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | # An Anthropic image block as an OpenAI image_url part: a base64 source becomes a data URL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | # with its media_type, a url source is passed as the URL. Any other source (a Files API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | # file_id) has no OpenAI-chat equivalent and is refused like a built-in tool. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 8 | ▶ | sub _image_part { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_image_part: called CC=10 SCAR=24.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 8 | my ($block) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 8 | ▶ | my $source = ref($block->{source}) eq 'HASH' ? $block->{source} : {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 8 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | return Langertha::Skeid::Protocol::image_url_part(undef, $source->{data}, $source->{media_type}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 8 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | return Langertha::Skeid::Protocol::image_url_part($source->{url}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 3 | Langertha::Skeid::Protocol::Refusal->refuse("image source type '$kind' is not supported: skeid forwards base64 and url images only"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 37 | ▶ | sub response_from_openai { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
response_from_openai: called CC=21 SCAR=31.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 37 | my ($class, $res, $default_model) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 37 | ▶ | 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] : {})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 37 | ▶ | 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'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 37 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 37 | ▶ | my @calls = Langertha::ToolCall->extract('openai', $res || {}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$res || {}
MC/DC0%0/1
$res || {}
$res
Truth table50%1/2
A
A= $res
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 37 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 32 | my ($clean, $extracted) = Langertha::ToolCall->extract_hermes_from_text($text); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 32 | $text = $clean; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 32 | @calls = @$extracted; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 37 | my @content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 37 | ▶ | push @content, { type => 'text', text => $text } if length $text; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 37 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 37 | for my $call (@calls) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 6 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 6 | push @content, $call->to_anthropic_block( fallback_id => "toolu_skeid_$i" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | # gpt-oss on vLLM-style servers (seen live on AKI.IO) answers a tool call with finish_reason | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | # 'stop'. The reply carries tool_use blocks, and an Anthropic client only runs them when | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | # stop_reason says tool_use, so tool calls present wins over 'stop' -- the rule core applies | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | # to Response.finish_reason (core k248). 'length' and the rest keep their mapping. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 37 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 37 | ▶ | 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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | : $fr eq 'length' ? 'max_tokens' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | : 'end_turn'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | id => ($res->{id} ? ('msg_' . $res->{id}) : ('msg_' . int(time * 1000))), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | type => 'message', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | role => 'assistant', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | model => ($res->{model} // $default_model), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | content => \@content, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | stop_reason => $stop_reason, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | stop_sequence => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | usage => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | input_tokens => 0 + (($res->{usage} || {})->{prompt_tokens} // 0), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 37 | ▶ | output_tokens => 0 + (($res->{usage} || {})->{completion_tokens} // 0), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition63%7/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/DC0%0/1
+($res->{"usage"} || {})->{"prompt_tokens"} // 0
$res->{'usage'}
MC/DC0%0/1
+($res->{"usage"} || {})->{"completion_tokens"} // 0
$res->{'usage'}
Truth table33%1/3
A // B
A= $res->{"model"}B= $default_model
Truth table50%1/2
A
A= $res->{'usage'}
Truth table50%1/2
A
A= $res->{'usage'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 70 | my %ERROR_TYPE_FOR_STATUS = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 400 => 'invalid_request_error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 401 => 'authentication_error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 402 => 'billing_error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 403 => 'permission_error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 404 => 'not_found_error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 409 => 'conflict_error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 413 => 'request_too_large', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 429 => 'rate_limit_error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 500 => 'api_error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 504 => 'timeout_error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 529 => 'overloaded_error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 70 | my %KNOWN_ERROR_TYPE = map { $_ => 1 } values %ERROR_TYPE_FOR_STATUS; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 66 | ▶ | sub error_type_for_status { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error_type_for_status: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 66 | my ($class, $status) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 66 | ▶ | $status = 0 + ($status // 500); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$status // 500
MC/DC0%0/1
$status // 500
$status
Truth table50%1/2
A
A= $status
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 66 | ▶ | return $ERROR_TYPE_FOR_STATUS{$status} // ($status >= 500 ? 'api_error' : 'invalid_request_error'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ERROR_TYPE_FOR_STATUS{$status} // ($status >= 500 ? "api_error" : "invalid_request_error")
MC/DC0%0/2
$ERROR_TYPE_FOR_STATUS{$status} // ($status >= 500 ? "api_error" : "invalid_request_error")
$ERROR_TYPE_FOR_STATUS{$status}
($status >= 500 ? "api_error" : "invalid_request_error")
Truth table66%2/3
A // B
A= $ERROR_TYPE_FOR_STATUS{$status}B= ($status >= 500 ? "api_error" : "invalid_request_error")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 53 | ▶ | sub error_body { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error_body: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 53 | my ($class, $status, $message, $type) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | $type = $class->error_type_for_status($status) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 53 | ▶ | 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}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 53 | ▶ | type => 'error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$message // ""
MC/DC0%0/1
$message // ""
$message
Truth table50%1/2
A
A= $message
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | error => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | type => $type, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | message => ($message // ''), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 18 | ▶ | sub manifest_endpoint { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
manifest_endpoint: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 18 | dialect => 'anthropic-compat', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | path => '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | capabilities => [qw( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | chat streaming system_prompt | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | tools_native tools_hermes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | tool_choice_auto tool_choice_any tool_choice_none tool_choice_named | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | temperature response_size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | image_input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | )], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 70 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | Langertha::Skeid::Protocol::Anthropic - Translate between the Anthropic Messages format and the upstream OpenAI call | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | version 0.003 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | Serves C<POST /v1/messages>. Anthropic-specific field names live here and nowhere else in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | Skeid. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | Tool shapes are not translated by hand — L<Langertha::Tool>, L<Langertha::ToolCall> and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | L<Langertha::ToolChoice> own what a tool looks like in each dialect, including recovering | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | Hermes-style C<< <tool_call> >> blocks from plain text. A format Langertha cannot express is a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | Langertha ticket, not a parser here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | Streaming is translated, not refused: C<stream: true> is rewritten to an OpenAI stream with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | C<stream_options.include_usage> set, so the token counts an Anthropic client reads from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | C<message_delta> actually arrive; the response is re-emitted at the client edge as the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | Anthropic event protocol — C<message_start>, C<content_block_start>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | C<content_block_delta>, C<content_block_stop>, C<message_delta>, C<message_stop>. The same | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | single upstream call (ADR 0001) carries the load; the format-specific framing is the only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | thing that changes between the wire Skeid speaks and the wire the client reads. See | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | L<Langertha::Skeid::Protocol::Anthropic::Stream> for the per-chunk rewrite and the usage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | accumulator, and F<t/31-stream-translation.t> for what the wire looks like end-to-end. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | C<< finish_reason >> mapping is the same as the non-streaming path: C<tool_calls> becomes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | C<tool_use>, C<length> becomes C<max_tokens>, anything else becomes C<end_turn> -- and a reply | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | that carries tool calls but finished with C<stop> reports C<tool_use>. Tool calls in a stream | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | are re-emitted as C<tool_use> content blocks with C<input_json_delta> deltas, one block per | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | parallel call. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | =head2 request_to_openai | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | my $openai_body = Langertha::Skeid::Protocol::Anthropic->request_to_openai($body); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | Turns an Anthropic Messages request into the OpenAI chat-completions body Skeid forwards: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | C<model>, the messages, and C<max_tokens>, C<temperature> and C<top_p> when given. Nothing else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | of the request is carried -- C<stop_sequences>, C<metadata>, C<thinking> and C<cache_control> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | included; C<stream> is set by the proxy. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | C<system> (a string or a block array) becomes a leading system message. Content block arrays | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | fold to text, unless a user message carries an C<image> block: then its text and image blocks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | become an OpenAI content array in the order the client sent them, text as C<text> parts and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | each image as an C<image_url> part -- a C<base64> source as a C<data:> URL with its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | C<media_type>, a C<url> source as that URL. An image source of any other type (a Files API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | C<file_id>) makes it die, answered as a C<400>. C<tool_use> blocks become C<tool_calls> on the assistant message; C<tool_result> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | blocks become their own C<role => 'tool'> message carrying C<tool_call_id>, which is why a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | single Anthropic message can expand into several OpenAI ones. A structured C<tool_result> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | content is sent as its JSON text. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | An OpenAI tool message cannot carry images, so the C<image> blocks of a C<tool_result> are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | lifted out of it: its tool message keeps the other blocks as JSON text (or, when there are none, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | a sentence saying the result is the images in the next message), and right after the run of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | tool messages one user message follows with the images of every C<tool_result> in that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | Anthropic message, each result's images as C<image_url> parts after a text part | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | C<Images from tool result E<lt>tool_use_idE<gt>:>. The user message goes after all the tool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | messages because an OpenAI conversation wants the answers to an assistant's tool calls | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | directly after it. The client's own text and images in the same message follow as their own | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | user message. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | Only function tools are translated. A provider built-in in C<tools> (C<web_search_20250305>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | C<bash_20250124>, C<text_editor_*>, C<computer_*>, C<mcp_toolset>, ...) or a definition | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | L<Langertha::Tool/classify> does not recognise, and an image source that is neither base64 nor a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | url, make it throw a L<Langertha::Skeid::Protocol::Refusal> with a one-line message naming the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | tool type and its category; the proxy answers that as a C<400 invalid_request_error> carrying | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | the message. Any other failure to translate the request is answered as a C<400> with the fixed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | text C<Invalid request>: that exception's text can quote the request, so it is neither sent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | nor logged. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | =head2 response_from_openai | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | my $anthropic = Langertha::Skeid::Protocol::Anthropic->response_from_openai($res, $model); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | Turns the upstream OpenAI response into an Anthropic message. Content becomes a C<text> block, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | tool calls become C<tool_use> blocks, and C<finish_reason> maps C<tool_calls> to C<tool_use>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | C<length> to C<max_tokens>, everything else to C<end_turn> -- except that a reply carrying tool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | calls with C<stop> (gpt-oss on vLLM-style servers) reports C<tool_use>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | C<$model> is the model the client asked for, used only when the upstream omits it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | =head2 error_type_for_status | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | my $type = Langertha::Skeid::Protocol::Anthropic->error_type_for_status(429); # rate_limit_error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | The C<error.type> an Anthropic client expects for an HTTP status, from Anthropic's Messages API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | error reference: 400 C<invalid_request_error>, 401 C<authentication_error>, 402 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | C<billing_error>, 403 C<permission_error>, 404 C<not_found_error>, 409 C<conflict_error>, 413 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | C<request_too_large>, 429 C<rate_limit_error>, 500 C<api_error>, 504 C<timeout_error>, 529 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | C<overloaded_error>. A | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | status the reference does not list falls back by class: any other 5xx (Skeid's own 502 and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 503) is C<api_error>, any other 4xx C<invalid_request_error>. The SDKs choose their exception | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | class from the HTTP status first, so the fallback only decides the type string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | =head2 error_body | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | my $body = Langertha::Skeid::Protocol::Anthropic->error_body(429, 'Timed out waiting ...'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | my $body = Langertha::Skeid::Protocol::Anthropic->error_body(500, $message, $upstream_type); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | The Anthropic error envelope, C<< { type => 'error', error => { type, message } } >>, with the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | type taken from L</error_type_for_status>. An optional third argument, an error type the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | upstream reported, wins when it is one of the types in that table -- an OpenAI-dialect upstream | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | often spells a rate limit or a bad request the same way -- and is ignored otherwise, so a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | foreign type such as C<server_error> never reaches an Anthropic client. Every error Skeid answers on C</v1/messages> is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | rendered from this -- also the C<data> of a mid-stream C<event: error> frame (see | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | L<Langertha::Skeid::Protocol::Anthropic::Stream/error_event>) -- so an Anthropic SDK can parse | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | it and raise the matching exception. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | =head2 manifest_endpoint | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | my $spec = Langertha::Skeid::Protocol::Anthropic->manifest_endpoint; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | # { dialect => 'anthropic-compat', path => '', capabilities => [ ... ] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | How this face appears in the provider manifest (skeid #29): C<anthropic-compat> at the public | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | root, and the capability flags L</request_to_openai> actually carries to the upstream -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | C<system>, function C<tools>, C<tool_choice> (auto, any, none, a named tool), C<max_tokens>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | C<temperature>, C<stream> and C<image> blocks (C<image_input>). A model is published here | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | only with the capabilities declared for it that are in this list. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | It is C<anthropic-compat>, not C<anthropic>: C<output_config.format> is not translated, so a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | client takes the synthetic-tool path for structured output, which is what that dialect tells | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | it. Not carried, so never claimed: structured output, C<thinking> (reasoning), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | C<cache_control> (prompt cache), and C<disable_parallel_tool_use>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | L<Langertha::Skeid::Protocol::Anthropic::Stream>, L<Langertha::Skeid::Protocol>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | L<Langertha::Skeid::Proxy> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | L<https://github.com/Getty/langertha-skeid/issues>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||