| 1 | package Langertha::Knarr::Protocol::Anthropic; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Anthropic-compatible wire protocol (/v1/messages) for Knarr | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 12 | our $VERSION = '1.100'; | |||||||||||||||||||||||||||
| 5 | 12 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use Time::HiRes qw( time ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use Langertha::Knarr::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use Langertha::Knarr::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 12 | with 'Langertha::Knarr::Protocol'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # --- Streaming model --- | ||||||||||||||||||||||||||||
| 14 | # Anthropic Messages SSE — multiple named events per stream: | ||||||||||||||||||||||||||||
| 15 | # event: message_start data: {"type":"message_start","message":{...}} | ||||||||||||||||||||||||||||
| 16 | # event: content_block_start data: {"type":"content_block_start","index":0, | ||||||||||||||||||||||||||||
| 17 | # "content_block":{"type":"text","text":""}} | ||||||||||||||||||||||||||||
| 18 | # event: content_block_delta data: {"type":"content_block_delta","index":0, | ||||||||||||||||||||||||||||
| 19 | # "delta":{"type":"text_delta","text":"Hi"}} | ||||||||||||||||||||||||||||
| 20 | # event: content_block_stop data: {"type":"content_block_stop","index":0} | ||||||||||||||||||||||||||||
| 21 | # event: message_delta data: {"type":"message_delta", | ||||||||||||||||||||||||||||
| 22 | # "delta":{"stop_reason":"end_turn"}, | ||||||||||||||||||||||||||||
| 23 | # "usage":{"output_tokens":N}} | ||||||||||||||||||||||||||||
| 24 | # event: message_stop data: {"type":"message_stop"} | ||||||||||||||||||||||||||||
| 25 | # So a single delta from the handler MUST translate into a content_block_delta. | ||||||||||||||||||||||||||||
| 26 | # We need the protocol to also be able to emit synthesized "start" and "stop" | ||||||||||||||||||||||||||||
| 27 | # frames around the stream — the runtime will call format_stream_open / _close. | ||||||||||||||||||||||||||||
| 28 | # ---------------------- | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 18 | ▶ | has _json => ( is => 'ro', default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | sub protocol_name { 'anthropic' } | ||||||||||||||||||||||||||
|
Subroutine
protocol_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub protocol_routes { | ||||||||||||||||||||||||||||
| 35 | return [ | ||||||||||||||||||||||||||||
| 36 | 14 | ▶ | { method => 'POST', path => '/v1/messages', action => 'chat' }, | ||||||||||||||||||||||||||
|
Subroutine
protocol_routes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | ]; | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 9 | ▶ | sub _msg_id { 'msg_' . int( time() * 1000 ) } | ||||||||||||||||||||||||||
|
Subroutine
_msg_id: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub parse_chat_request { | ||||||||||||||||||||||||||||
| 43 | 6 | ▶ | my ($self, $http_req, $body_ref) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse_chat_request: called CC=11 SCAR=26.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | 6 | ▶ | my $data = $self->_json->decode( $$body_ref || '{}' ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$$body_ref || '{}'
MC/DC0%0/1
$$body_ref || '{}'
$$body_ref
Truth table50%1/2
A
A= $$body_ref
| |||||||||||||||||||||||||||||
| 45 | # Anthropic puts system prompt outside messages. | ||||||||||||||||||||||||||||
| 46 | # system can be a string or an array of content blocks [{type:"text",text:"..."},...] | ||||||||||||||||||||||||||||
| 47 | 6 | my $system_raw = $data->{system}; | |||||||||||||||||||||||||||
| 48 | 6 | my $system_str; | |||||||||||||||||||||||||||
| 49 | 6 | ▶ | if (ref $system_raw eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | $system_str = join("\n", map { $_->{text} // '' } @$system_raw); | ||||||||||||||||||||||||||
|
Condition0%0/2
$_->{"text"} // ""
MC/DC0%0/1
$_->{"text"} // ""
$_->{"text"}
Truth table0%0/2
A
A= $_->{"text"}
| |||||||||||||||||||||||||||||
| 51 | } elsif (defined $system_raw) { | ||||||||||||||||||||||||||||
| 52 | 0 | $system_str = $system_raw; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 6 | my @msgs; | |||||||||||||||||||||||||||
| 55 | 6 | ▶ | push @msgs, { role => 'system', content => $system_str } if defined $system_str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 6 | ▶ | push @msgs, @{ $data->{messages} || [] }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | # Capture auth headers for passthrough | ||||||||||||||||||||||||||||
| 58 | 6 | my %fwd; | |||||||||||||||||||||||||||
| 59 | 6 | for my $h (qw( x-api-key anthropic-version authorization )) { | |||||||||||||||||||||||||||
| 60 | 18 | my $v = scalar $http_req->header($h); | |||||||||||||||||||||||||||
| 61 | 18 | ▶ | $fwd{$h} = $v if defined $v && length $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $v and length $v
MC/DC50%1/2
defined $v and length $v
defined $v
length $v
Truth table66%2/3
A and B
A= defined $vB= length $v
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | return Langertha::Knarr::Request->new( | ||||||||||||||||||||||||||||
| 64 | protocol => 'anthropic', | ||||||||||||||||||||||||||||
| 65 | raw => $data, | ||||||||||||||||||||||||||||
| 66 | model => $data->{model}, | ||||||||||||||||||||||||||||
| 67 | messages => \@msgs, | ||||||||||||||||||||||||||||
| 68 | stream => $data->{stream} ? 1 : 0, | ||||||||||||||||||||||||||||
| 69 | temperature => $data->{temperature}, | ||||||||||||||||||||||||||||
| 70 | max_tokens => $data->{max_tokens}, | ||||||||||||||||||||||||||||
| 71 | system => $system_str, | ||||||||||||||||||||||||||||
| 72 | tools => $data->{tools}, | ||||||||||||||||||||||||||||
| 73 | tool_choice => $data->{tool_choice}, | ||||||||||||||||||||||||||||
| 74 | 6 | ▶ | extra => { forward_headers => \%fwd }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sub format_chat_response { | ||||||||||||||||||||||||||||
| 79 | 7 | ▶ | my ($self, $response, $request) = @_; | ||||||||||||||||||||||||||
|
Subroutine
format_chat_response: called CC=9 SCAR=22.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 80 | 7 | my $r = Langertha::Knarr::Response->coerce($response); | |||||||||||||||||||||||||||
| 81 | 7 | my @blocks; | |||||||||||||||||||||||||||
| 82 | 7 | ▶ | push @blocks, { type => 'text', text => $r->content } if length $r->content; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 7 | push @blocks, map { $_->to_anthropic_block } @{ $r->tool_calls }; | |||||||||||||||||||||||||||
| 84 | 7 | ▶ | push @blocks, { type => 'text', text => '' } unless @blocks; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 7 | ▶ | my $stop_reason = $r->finish_reason | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$r->finish_reason // ($r->has_tool_calls ? "tool_use" : "end_turn")
MC/DC0%0/2
$r->finish_reason // ($r->has_tool_calls ? "tool_use" : "end_turn")
$r->finish_reason
($r->has_tool_calls ? "tool_use" : "end_turn")
Truth table66%2/3
A // B
A= $r->finish_reasonB= ($r->has_tool_calls ? "tool_use" : "end_turn")
| |||||||||||||||||||||||||||||
| 86 | // ( $r->has_tool_calls ? 'tool_use' : 'end_turn' ); | ||||||||||||||||||||||||||||
| 87 | 7 | ▶ | my $usage = $r->usage && $r->usage->can('to_anthropic_format') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$r->usage and $r->usage->can("to_anthropic_format")
MC/DC50%1/2
$r->usage and $r->usage->can("to_anthropic_format")
$r->usage
$r->usage->can("to_anthropic_format")
Truth table66%2/3
A and B
A= $r->usageB= $r->usage->can("to_anthropic_format")
| |||||||||||||||||||||||||||||
| 88 | ? $r->usage->to_anthropic_format | ||||||||||||||||||||||||||||
| 89 | : { input_tokens => 0, output_tokens => 0 }; | ||||||||||||||||||||||||||||
| 90 | 7 | ▶ | my $payload = { | ||||||||||||||||||||||||||
|
Condition60%3/5
($r->model // $request->model) // "unknown"
$r->model // $request->model
MC/DC0%0/2
($r->model // $request->model) // "unknown"
$r->model
$request->model
Truth table66%2/3
A // B
A= $r->modelB= $request->model
| |||||||||||||||||||||||||||||
| 91 | id => _msg_id(), | ||||||||||||||||||||||||||||
| 92 | type => 'message', | ||||||||||||||||||||||||||||
| 93 | role => 'assistant', | ||||||||||||||||||||||||||||
| 94 | model => $r->model // $request->model // 'unknown', | ||||||||||||||||||||||||||||
| 95 | content => \@blocks, | ||||||||||||||||||||||||||||
| 96 | stop_reason => $stop_reason, | ||||||||||||||||||||||||||||
| 97 | stop_sequence => undef, | ||||||||||||||||||||||||||||
| 98 | usage => $usage, | ||||||||||||||||||||||||||||
| 99 | }; | ||||||||||||||||||||||||||||
| 100 | 7 | return ( 200, { 'Content-Type' => 'application/json' }, $self->_json->encode($payload) ); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # Anthropic streaming uses named SSE events. We render full event blocks. | ||||||||||||||||||||||||||||
| 104 | sub _sse_event { | ||||||||||||||||||||||||||||
| 105 | 15 | ▶ | my ($self, $event, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_sse_event: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 106 | 15 | return "event: $event\ndata: " . $self->_json->encode($data) . "\n\n"; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub format_stream_open { | ||||||||||||||||||||||||||||
| 110 | 2 | ▶ | my ($self, $request) = @_; | ||||||||||||||||||||||||||
|
Subroutine
format_stream_open: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | 2 | my $id = _msg_id(); | |||||||||||||||||||||||||||
| 112 | 2 | ▶ | my $model = $request->model // 'unknown'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$request->model // "unknown"
MC/DC0%0/1
$request->model // "unknown"
$request->model
Truth table50%1/2
A
A= $request->model
| |||||||||||||||||||||||||||||
| 113 | 2 | return join( '', | |||||||||||||||||||||||||||
| 114 | $self->_sse_event( message_start => { | ||||||||||||||||||||||||||||
| 115 | type => 'message_start', | ||||||||||||||||||||||||||||
| 116 | message => { | ||||||||||||||||||||||||||||
| 117 | id => $id, type => 'message', role => 'assistant', | ||||||||||||||||||||||||||||
| 118 | content => [], model => $model, | ||||||||||||||||||||||||||||
| 119 | stop_reason => undef, stop_sequence => undef, | ||||||||||||||||||||||||||||
| 120 | usage => { input_tokens => 0, output_tokens => 0 }, | ||||||||||||||||||||||||||||
| 121 | }, | ||||||||||||||||||||||||||||
| 122 | }), | ||||||||||||||||||||||||||||
| 123 | $self->_sse_event( content_block_start => { | ||||||||||||||||||||||||||||
| 124 | type => 'content_block_start', | ||||||||||||||||||||||||||||
| 125 | index => 0, | ||||||||||||||||||||||||||||
| 126 | content_block => { type => 'text', text => '' }, | ||||||||||||||||||||||||||||
| 127 | }), | ||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | sub format_stream_chunk { | ||||||||||||||||||||||||||||
| 132 | 5 | ▶ | my ($self, $delta_text, $request) = @_; | ||||||||||||||||||||||||||
|
Subroutine
format_stream_chunk: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 133 | 5 | return $self->_sse_event( content_block_delta => { | |||||||||||||||||||||||||||
| 134 | type => 'content_block_delta', | ||||||||||||||||||||||||||||
| 135 | index => 0, | ||||||||||||||||||||||||||||
| 136 | delta => { type => 'text_delta', text => $delta_text }, | ||||||||||||||||||||||||||||
| 137 | }); | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | sub format_stream_close { | ||||||||||||||||||||||||||||
| 141 | 2 | ▶ | my ($self, $request) = @_; | ||||||||||||||||||||||||||
|
Subroutine
format_stream_close: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 142 | 2 | return join( '', | |||||||||||||||||||||||||||
| 143 | $self->_sse_event( content_block_stop => { type => 'content_block_stop', index => 0 } ), | ||||||||||||||||||||||||||||
| 144 | $self->_sse_event( message_delta => { | ||||||||||||||||||||||||||||
| 145 | type => 'message_delta', | ||||||||||||||||||||||||||||
| 146 | delta => { stop_reason => 'end_turn', stop_sequence => undef }, | ||||||||||||||||||||||||||||
| 147 | usage => { output_tokens => 0 }, | ||||||||||||||||||||||||||||
| 148 | }), | ||||||||||||||||||||||||||||
| 149 | $self->_sse_event( message_stop => { type => 'message_stop' } ), | ||||||||||||||||||||||||||||
| 150 | ); | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 2 | ▶ | sub format_stream_done { '' } | ||||||||||||||||||||||||||
|
Subroutine
format_stream_done: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 12 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 156 | 12 | 1; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =pod | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head1 NAME | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Langertha::Knarr::Protocol::Anthropic - Anthropic-compatible wire protocol (/v1/messages) for Knarr | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head1 VERSION | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | version 1.100 | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Implements the Anthropic Messages wire format on top of | ||||||||||||||||||||||||||||
| 175 | L<Langertha::Knarr::Protocol>. Loaded by default. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =over | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =item * C<POST /v1/messages> — sync and named-event SSE streaming | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =back | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | Streaming emits the full event sequence the Anthropic SDK expects: | ||||||||||||||||||||||||||||
| 184 | C<message_start>, C<content_block_start>, C<content_block_delta>×N, | ||||||||||||||||||||||||||||
| 185 | C<content_block_stop>, C<message_delta>, C<message_stop>. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head2 Issues | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 192 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head2 IRC | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 211 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =cut | ||||||||||||||||||||||||||||