| 1 | package Langertha::Knarr::Protocol; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Role for Knarr wire protocols (OpenAI, Anthropic, Ollama, A2A, ACP, AG-UI) | ||||||||||||||||||||||||||||
| 3 | 50 | our $VERSION = '1.102'; | |||||||||||||||||||||||||||
| 4 | 50 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 50 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 50 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # Identifier (e.g. 'openai', 'anthropic', 'ollama'). | ||||||||||||||||||||||||||||
| 10 | 50 | requires 'protocol_name'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # Returns arrayref of route specs: | ||||||||||||||||||||||||||||
| 13 | # [ { method => 'POST', path => '/v1/chat/completions', action => 'chat' }, ... ] | ||||||||||||||||||||||||||||
| 14 | 50 | requires 'protocol_routes'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # parse_chat_request($http_req, $body_ref) -> Langertha::Knarr::Request | ||||||||||||||||||||||||||||
| 17 | 50 | requires 'parse_chat_request'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # format_chat_response($response, $request) -> ($status, \%headers, $body) | ||||||||||||||||||||||||||||
| 20 | 50 | requires 'format_chat_response'; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # format_stream_chunk($chunk, $request) -> string (raw bytes for the wire) | ||||||||||||||||||||||||||||
| 23 | # Default: SSE-style "data: {...}\n\n" — protocols may override (Ollama uses NDJSON). | ||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub format_stream_chunk { | ||||||||||||||||||||||||||
|
Subroutine
format_stream_chunk: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 0 | my ($self, $chunk_json) = @_; | |||||||||||||||||||||||||||
| 26 | 0 | return "data: $chunk_json\n\n"; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 50 | ▶ | sub format_stream_done { | ||||||||||||||||||||||||||
|
Subroutine
format_stream_done: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 50 | my ($self) = @_; | |||||||||||||||||||||||||||
| 31 | 50 | return "data: [DONE]\n\n"; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Optional lifecycle hooks for protocols that need to frame the stream | ||||||||||||||||||||||||||||
| 35 | # (Anthropic message_start/stop, A2A status events, ACP run.created, AGUI RUN_STARTED). | ||||||||||||||||||||||||||||
| 36 | # Default: empty — protocols like OpenAI / Ollama don't need them. | ||||||||||||||||||||||||||||
| 37 | 129 | ▶ | sub format_stream_open { '' } | ||||||||||||||||||||||||||
|
Subroutine
format_stream_open: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 63 | ▶ | sub format_stream_close { '' } | ||||||||||||||||||||||||||
|
Subroutine
format_stream_close: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # An error answer in the protocol's own shape; default is the OpenAI wire's. | ||||||||||||||||||||||||||||
| 41 | 19 | ▶ | sub format_error_response { | ||||||||||||||||||||||||||
|
Subroutine
format_error_response: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 19 | my ($self, $status, $message) = @_; | |||||||||||||||||||||||||||
| 43 | 19 | return ( $status, { 'Content-Type' => 'application/json' }, | |||||||||||||||||||||||||||
| 44 | JSON::MaybeXS->new( utf8 => 1, canonical => 1 )->encode({ error => { message => "$message" } }) ); | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # The frame that marks a stream as failed once its headers are out; '' | ||||||||||||||||||||||||||||
| 48 | # when the protocol has none. | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub format_stream_error { '' } | ||||||||||||||||||||||||||
|
Subroutine
format_stream_error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Content-Type for streaming responses. Default is SSE; Ollama overrides. | ||||||||||||||||||||||||||||
| 52 | 186 | ▶ | sub stream_content_type { 'text/event-stream' } | ||||||||||||||||||||||||||
|
Subroutine
stream_content_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # Manifest publication (k14): undef = not in the provider manifest. | ||||||||||||||||||||||||||||
| 55 | 108 | ▶ | sub manifest_endpoint { undef } | ||||||||||||||||||||||||||
|
Subroutine
manifest_endpoint: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # format_models_response(\@models) -> ($status, \%headers, $body) | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub format_models_response { | ||||||||||||||||||||||||||
|
Subroutine
format_models_response: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 0 | my ($self, $models) = @_; | |||||||||||||||||||||||||||
| 60 | 0 | return ( 200, { 'Content-Type' => 'application/json' }, '{"data":[]}' ); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # The client's headers of the given names as [ name, value ] pairs for a | ||||||||||||||||||||||||||||
| 64 | # Request's forward_headers: one pair per line the client sent, repeats in | ||||||||||||||||||||||||||||
| 65 | # their order, read with a list-context ->header (k60). A request object | ||||||||||||||||||||||||||||
| 66 | # that has no ->header gives none. | ||||||||||||||||||||||||||||
| 67 | 685 | ▶ | sub _forward_headers { | ||||||||||||||||||||||||||
|
Subroutine
_forward_headers: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 68 | 685 | my ($self, $http_req, @names) = @_; | |||||||||||||||||||||||||||
| 69 | 685 | ▶ | return [] unless Scalar::Util::blessed($http_req) && $http_req->can('header'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($http_req) and $http_req->can("header")
MC/DC50%1/2
builtin::blessed($http_req) and $http_req->can("header")
builtin::blessed($http_req)
$http_req->can("header")
Truth table66%2/3
A and B
A= builtin::blessed($http_req)B= $http_req->can("header")
| |||||||||||||||||||||||||||||
| 70 | 684 | my @pairs; | |||||||||||||||||||||||||||
| 71 | 684 | for my $name (@names) { | |||||||||||||||||||||||||||
| 72 | 1112 | ▶ | push @pairs, map { [ $name, $_ ] } grep { defined && length } $http_req->header($name); | ||||||||||||||||||||||||||
|
Condition66%2/3
defined $_ && length $_
MC/DC50%1/2
defined $_ && length $_
defined $_
length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 684 | return \@pairs; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 50 | 1; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | __END__ | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =pod | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head1 NAME | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | Langertha::Knarr::Protocol - Role for Knarr wire protocols (OpenAI, Anthropic, Ollama, A2A, ACP, AG-UI) | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head1 VERSION | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | version 1.102 | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | The role every Knarr wire protocol must consume. A protocol declares | ||||||||||||||||||||||||||||
| 96 | its routes, parses incoming HTTP bodies into a normalized | ||||||||||||||||||||||||||||
| 97 | L<Langertha::Knarr::Request>, and formats outgoing | ||||||||||||||||||||||||||||
| 98 | L<Langertha::Knarr::Stream> chunks back into the protocol-native wire | ||||||||||||||||||||||||||||
| 99 | format. The Knarr core dispatches each request to the right handler | ||||||||||||||||||||||||||||
| 100 | via the matched protocol's parser/formatter. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | Knarr ships with six concrete protocols, all loaded by default: | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =over | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =item * L<Langertha::Knarr::Protocol::OpenAI> — C</v1/chat/completions>, SSE | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =item * L<Langertha::Knarr::Protocol::Anthropic> — C</v1/messages>, named SSE events | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =item * L<Langertha::Knarr::Protocol::Ollama> — C</api/chat>, NDJSON streaming | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =item * L<Langertha::Knarr::Protocol::A2A> — Google Agent2Agent JSON-RPC | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =item * L<Langertha::Knarr::Protocol::ACP> — IBM/BeeAI Agent Communication Protocol | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =item * L<Langertha::Knarr::Protocol::AGUI> — CopilotKit AG-UI event protocol | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =back | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head2 protocol_name | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | Required. Returns a short string identifier (e.g. C<'openai'>). | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head2 protocol_routes | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Required. Returns an arrayref of route specs of the form | ||||||||||||||||||||||||||||
| 127 | C<< { method => 'POST', path => '/v1/chat/completions', action => 'chat' } >>. | ||||||||||||||||||||||||||||
| 128 | Action names map to C<_action_*> methods on the Knarr core. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head2 parse_chat_request | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | my $req = $proto->parse_chat_request($http_request, \$body); | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Required. Returns a L<Langertha::Knarr::Request>. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head2 format_chat_response | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | my ($status, \%headers, $body) = $proto->format_chat_response($response, $request); | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | Required. Returns the HTTP response triple for sync mode. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head2 format_stream_open / format_stream_chunk / format_stream_close / format_stream_done | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Lifecycle hooks for streaming responses. Defaults are no-ops where the | ||||||||||||||||||||||||||||
| 145 | protocol doesn't need framing — Anthropic/A2A/ACP/AG-UI override these | ||||||||||||||||||||||||||||
| 146 | to emit their named events around the chunk stream. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | C<format_stream_close> and C<format_stream_done> are called as | ||||||||||||||||||||||||||||
| 149 | C<($request, $finish_reason, $tool_calls, $usage)>: the second argument is the | ||||||||||||||||||||||||||||
| 150 | backend's terminal finish reason from L<Langertha::Knarr::Stream/finish_reason>, | ||||||||||||||||||||||||||||
| 151 | verbatim and possibly C<undef> (always C<undef> after a stream error). | ||||||||||||||||||||||||||||
| 152 | A protocol maps it into its own vocabulary; Anthropic puts it on | ||||||||||||||||||||||||||||
| 153 | C<message_delta>, OpenAI on a terminal chunk, Ollama on C<done_reason>. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | The third argument is an ArrayRef of complete L<Langertha::ToolCall> | ||||||||||||||||||||||||||||
| 156 | objects from L<Langertha::Knarr::Stream/tool_calls>, empty when the | ||||||||||||||||||||||||||||
| 157 | backend emitted none (and absent after a stream error). A protocol that | ||||||||||||||||||||||||||||
| 158 | can carry tool calls emits them before its terminal frames: Anthropic | ||||||||||||||||||||||||||||
| 159 | as C<tool_use> content blocks, OpenAI as one C<delta.tool_calls> chunk, | ||||||||||||||||||||||||||||
| 160 | Ollama as C<message.tool_calls> on the done line. | ||||||||||||||||||||||||||||
| 161 | The fourth argument is the stream's token usage from | ||||||||||||||||||||||||||||
| 162 | L<Langertha::Knarr::Stream/usage>, a L<Langertha::Usage>, or C<undef> when | ||||||||||||||||||||||||||||
| 163 | the backend reported none (and after a stream error). Anthropic reports it | ||||||||||||||||||||||||||||
| 164 | on C<message_delta>, Ollama as C<prompt_eval_count> / C<eval_count> on the | ||||||||||||||||||||||||||||
| 165 | done line, OpenAI in a C<usage> chunk after the terminal one when the client | ||||||||||||||||||||||||||||
| 166 | asked for it with C<stream_options.include_usage>. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head2 format_error_response | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | my ($status, \%headers, $body) = $proto->format_error_response( 504, 'upstream timed out' ); | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | An error answer in this protocol's shape. Default: the OpenAI wire's | ||||||||||||||||||||||||||||
| 173 | C<{"error":{"message":...}}>; Anthropic answers | ||||||||||||||||||||||||||||
| 174 | C<{"type":"error","error":{"type":...,"message":...}}>, Ollama a plain | ||||||||||||||||||||||||||||
| 175 | C<{"error":"..."}>. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head2 format_stream_error | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | my $bytes = $proto->format_stream_error( 504, 'upstream timed out' ); | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | The frame that tells a client its stream failed after the response | ||||||||||||||||||||||||||||
| 182 | headers went out, or C<''> (the default) when the protocol has none. | ||||||||||||||||||||||||||||
| 183 | OpenAI sends C<data: {"error":{...}}>, Anthropic an C<event: error>, | ||||||||||||||||||||||||||||
| 184 | Ollama an C<{"error":"..."}> line. The raw passthrough writes it before | ||||||||||||||||||||||||||||
| 185 | closing a stream the upstream stopped feeding, and so does a routed stream | ||||||||||||||||||||||||||||
| 186 | whose handler failed with an upstream timeout. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =head2 stream_content_type | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Returns the HTTP C<Content-Type> for streaming responses. Default | ||||||||||||||||||||||||||||
| 191 | C<text/event-stream>; Ollama overrides to C<application/x-ndjson>. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head2 manifest_endpoint | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | my $spec = $proto->manifest_endpoint; | ||||||||||||||||||||||||||||
| 196 | # { dialect => 'openai-chat', path => '/v1', capabilities => [ ... ], | ||||||||||||||||||||||||||||
| 197 | # image_content_formats => [ 'openai', 'gemini' ] } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | How this protocol appears in the provider manifest | ||||||||||||||||||||||||||||
| 200 | (C</.well-known/langertha.json>, see L<Langertha::Knarr/MANIFEST>), or | ||||||||||||||||||||||||||||
| 201 | C<undef> (the default) when it has no manifest dialect and is not | ||||||||||||||||||||||||||||
| 202 | published. C<dialect> is a Langertha manifest dialect, C<path> is appended | ||||||||||||||||||||||||||||
| 203 | to the public base URL to form the endpoint's C<base_url>, and | ||||||||||||||||||||||||||||
| 204 | C<capabilities> lists the capability flags this protocol's | ||||||||||||||||||||||||||||
| 205 | L</parse_chat_request> actually carries to the engine. A published model | ||||||||||||||||||||||||||||
| 206 | claims a capability on this endpoint only when its engine has it B<and> | ||||||||||||||||||||||||||||
| 207 | it is in this list. | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | C<image_input> additionally needs C<image_content_formats>: the engine | ||||||||||||||||||||||||||||
| 210 | C<content_format>s (C<openai>, C<anthropic>, C<gemini>, C<ollama>, ...) | ||||||||||||||||||||||||||||
| 211 | that read this protocol's image parts; a model is published with | ||||||||||||||||||||||||||||
| 212 | C<image_input> on this endpoint only when its engine's content format is | ||||||||||||||||||||||||||||
| 213 | listed. The OpenAI, Anthropic and Ollama protocols translate their image | ||||||||||||||||||||||||||||
| 214 | parts into L<Langertha::Content::Image> objects and list every format | ||||||||||||||||||||||||||||
| 215 | (L<Langertha::Knarr::Image>); on a core too old for that the parts pass | ||||||||||||||||||||||||||||
| 216 | through untranslated and only the formats that read the protocol's own | ||||||||||||||||||||||||||||
| 217 | shape are listed. | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =head2 Issues | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 224 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head2 IRC | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 243 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =cut | ||||||||||||||||||||||||||||