| 1 | package Langertha::Knarr::Protocol; | ||
| 2 | # ABSTRACT: Role for Knarr wire protocols (OpenAI, Anthropic, Ollama, A2A, ACP, AG-UI) | ||
| 3 | 13 | our $VERSION = '1.100'; | |
| 4 | 13 | ▶ | use Moose::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | |||
| 7 | # Identifier (e.g. 'openai', 'anthropic', 'ollama'). | ||
| 8 | 13 | requires 'protocol_name'; | |
| 9 | |||
| 10 | # Returns arrayref of route specs: | ||
| 11 | # [ { method => 'POST', path => '/v1/chat/completions', action => 'chat' }, ... ] | ||
| 12 | 13 | requires 'protocol_routes'; | |
| 13 | |||
| 14 | # parse_chat_request($http_req, $body_ref) -> Langertha::Knarr::Request | ||
| 15 | 13 | requires 'parse_chat_request'; | |
| 16 | |||
| 17 | # format_chat_response($response, $request) -> ($status, \%headers, $body) | ||
| 18 | 13 | requires 'format_chat_response'; | |
| 19 | |||
| 20 | # format_stream_chunk($chunk, $request) -> string (raw bytes for the wire) | ||
| 21 | # Default: SSE-style "data: {...}\n\n" — protocols may override (Ollama uses NDJSON). | ||
| 22 | sub format_stream_chunk { | ||
| 23 | 0 | ▶ | my ($self, $chunk_json) = @_; |
|
Subroutine
format_stream_chunk: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 24 | 0 | return "data: $chunk_json\n\n"; | |
| 25 | } | ||
| 26 | |||
| 27 | sub format_stream_done { | ||
| 28 | 6 | ▶ | my ($self) = @_; |
|
Subroutine
format_stream_done: called CC=1 SCAR=0.0
Pod
documented
| |||
| 29 | 6 | return "data: [DONE]\n\n"; | |
| 30 | } | ||
| 31 | |||
| 32 | # Optional lifecycle hooks for protocols that need to frame the stream | ||
| 33 | # (Anthropic message_start/stop, A2A status events, ACP run.created, AGUI RUN_STARTED). | ||
| 34 | # Default: empty — protocols like OpenAI / Ollama don't need them. | ||
| 35 | 11 | ▶ | sub format_stream_open { '' } |
|
Subroutine
format_stream_open: called CC=1 SCAR=0.0
Pod
documented
| |||
| 36 | 11 | ▶ | sub format_stream_close { '' } |
|
Subroutine
format_stream_close: called CC=1 SCAR=0.0
Pod
documented
| |||
| 37 | |||
| 38 | # Content-Type for streaming responses. Default is SSE; Ollama overrides. | ||
| 39 | 19 | ▶ | sub stream_content_type { 'text/event-stream' } |
|
Subroutine
stream_content_type: called CC=1 SCAR=0.0
Pod
documented
| |||
| 40 | |||
| 41 | # format_models_response(\@models) -> ($status, \%headers, $body) | ||
| 42 | sub format_models_response { | ||
| 43 | 0 | ▶ | my ($self, $models) = @_; |
|
Subroutine
format_models_response: not called CC=1 SCAR=6.9
Pod
undocumented
| |||
| 44 | 0 | return ( 200, { 'Content-Type' => 'application/json' }, '{"data":[]}' ); | |
| 45 | } | ||
| 46 | |||
| 47 | 13 | 1; | |
| 48 | |||
| 49 | __END__ | ||
| 50 | |||
| 51 | =pod | ||
| 52 | |||
| 53 | =encoding UTF-8 | ||
| 54 | |||
| 55 | =head1 NAME | ||
| 56 | |||
| 57 | Langertha::Knarr::Protocol - Role for Knarr wire protocols (OpenAI, Anthropic, Ollama, A2A, ACP, AG-UI) | ||
| 58 | |||
| 59 | =head1 VERSION | ||
| 60 | |||
| 61 | version 1.100 | ||
| 62 | |||
| 63 | =head1 DESCRIPTION | ||
| 64 | |||
| 65 | The role every Knarr wire protocol must consume. A protocol declares | ||
| 66 | its routes, parses incoming HTTP bodies into a normalized | ||
| 67 | L<Langertha::Knarr::Request>, and formats outgoing | ||
| 68 | L<Langertha::Knarr::Stream> chunks back into the protocol-native wire | ||
| 69 | format. The Knarr core dispatches each request to the right handler | ||
| 70 | via the matched protocol's parser/formatter. | ||
| 71 | |||
| 72 | Knarr ships with six concrete protocols, all loaded by default: | ||
| 73 | |||
| 74 | =over | ||
| 75 | |||
| 76 | =item * L<Langertha::Knarr::Protocol::OpenAI> — C</v1/chat/completions>, SSE | ||
| 77 | |||
| 78 | =item * L<Langertha::Knarr::Protocol::Anthropic> — C</v1/messages>, named SSE events | ||
| 79 | |||
| 80 | =item * L<Langertha::Knarr::Protocol::Ollama> — C</api/chat>, NDJSON streaming | ||
| 81 | |||
| 82 | =item * L<Langertha::Knarr::Protocol::A2A> — Google Agent2Agent JSON-RPC | ||
| 83 | |||
| 84 | =item * L<Langertha::Knarr::Protocol::ACP> — IBM/BeeAI Agent Communication Protocol | ||
| 85 | |||
| 86 | =item * L<Langertha::Knarr::Protocol::AGUI> — CopilotKit AG-UI event protocol | ||
| 87 | |||
| 88 | =back | ||
| 89 | |||
| 90 | =head2 protocol_name | ||
| 91 | |||
| 92 | Required. Returns a short string identifier (e.g. C<'openai'>). | ||
| 93 | |||
| 94 | =head2 protocol_routes | ||
| 95 | |||
| 96 | Required. Returns an arrayref of route specs of the form | ||
| 97 | C<< { method => 'POST', path => '/v1/chat/completions', action => 'chat' } >>. | ||
| 98 | Action names map to C<_action_*> methods on the Knarr core. | ||
| 99 | |||
| 100 | =head2 parse_chat_request | ||
| 101 | |||
| 102 | my $req = $proto->parse_chat_request($http_request, \$body); | ||
| 103 | |||
| 104 | Required. Returns a L<Langertha::Knarr::Request>. | ||
| 105 | |||
| 106 | =head2 format_chat_response | ||
| 107 | |||
| 108 | my ($status, \%headers, $body) = $proto->format_chat_response($response, $request); | ||
| 109 | |||
| 110 | Required. Returns the HTTP response triple for sync mode. | ||
| 111 | |||
| 112 | =head2 format_stream_open / format_stream_chunk / format_stream_close / format_stream_done | ||
| 113 | |||
| 114 | Lifecycle hooks for streaming responses. Defaults are no-ops where the | ||
| 115 | protocol doesn't need framing — Anthropic/A2A/ACP/AG-UI override these | ||
| 116 | to emit their named events around the chunk stream. | ||
| 117 | |||
| 118 | =head2 stream_content_type | ||
| 119 | |||
| 120 | Returns the HTTP C<Content-Type> for streaming responses. Default | ||
| 121 | C<text/event-stream>; Ollama overrides to C<application/x-ndjson>. | ||
| 122 | |||
| 123 | =head1 SUPPORT | ||
| 124 | |||
| 125 | =head2 Issues | ||
| 126 | |||
| 127 | Please report bugs and feature requests on GitHub at | ||
| 128 | L<https://github.com/Getty/langertha-knarr/issues>. | ||
| 129 | |||
| 130 | =head2 IRC | ||
| 131 | |||
| 132 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||
| 133 | |||
| 134 | =head1 CONTRIBUTING | ||
| 135 | |||
| 136 | Contributions are welcome! Please fork the repository and submit a pull request. | ||
| 137 | |||
| 138 | =head1 AUTHOR | ||
| 139 | |||
| 140 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||
| 141 | |||
| 142 | =head1 COPYRIGHT AND LICENSE | ||
| 143 | |||
| 144 | This software is copyright (c) 2026 by Torsten Raudssus. | ||
| 145 | |||
| 146 | This is free software; you can redistribute it and/or modify it under | ||
| 147 | the same terms as the Perl 5 programming language system itself. | ||
| 148 | |||
| 149 | =cut | ||