| 1 | package Langertha::Knarr::Protocol::A2A; | ||
| 2 | # ABSTRACT: Google Agent2Agent (A2A) wire protocol for Knarr | ||
| 3 | |||
| 4 | our $VERSION = '1.103'; | ||
| 5 | 2 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use Carp qw( croak ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use JSON::MaybeXS; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use Data::UUID; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 2 | ▶ | use Time::HiRes qw( time ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | ▶ | use Langertha::Knarr::Request; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 2 | ▶ | use Langertha::Knarr::Response; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 0 | ▶ | use Langertha::Knarr (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | with 'Langertha::Knarr::Protocol'; | ||
| 15 | |||
| 16 | # --- A2A overview --- | ||
| 17 | # Discovery: GET /.well-known/agent.json -> AgentCard JSON | ||
| 18 | # Method bus: POST / body: JSON-RPC 2.0 envelope | ||
| 19 | # methods: "tasks/send", "tasks/sendSubscribe", "tasks/get", | ||
| 20 | # "tasks/cancel", "tasks/pushNotification/set", ... | ||
| 21 | # | ||
| 22 | # tasks/send (sync): returns Task JSON in JSON-RPC result | ||
| 23 | # tasks/sendSubscribe (streaming): returns SSE stream of JSON-RPC responses | ||
| 24 | # Each event is a full JSON-RPC response wrapping a TaskStatusUpdateEvent or | ||
| 25 | # TaskArtifactUpdateEvent. Final event has status.state = "completed". | ||
| 26 | # | ||
| 27 | # Streaming model on the wire: | ||
| 28 | # data: {"jsonrpc":"2.0","id":<reqid>,"result":{"id":"<task>","status":{"state":"working","message":{...}}, "final":false}}\n\n | ||
| 29 | # data: {"jsonrpc":"2.0","id":<reqid>,"result":{"id":"<task>","artifact":{"parts":[{"type":"text","text":"Hi"}], "index":0,"append":true}, "final":false}}\n\n | ||
| 30 | # data: {"jsonrpc":"2.0","id":<reqid>,"result":{"id":"<task>","status":{"state":"completed"},"final":true}}\n\n | ||
| 31 | # --------------------------------------------------------------------------- | ||
| 32 | |||
| 33 | has _json => ( is => 'ro', default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) } ); | ||
| 34 | has _uuid => ( is => 'ro', default => sub { Data::UUID->new } ); | ||
| 35 | |||
| 36 | has agent_name => ( | ||
| 37 | is => 'ro', | ||
| 38 | isa => 'Str', | ||
| 39 | default => 'Langertha Knarr Agent', | ||
| 40 | ); | ||
| 41 | |||
| 42 | has agent_description => ( | ||
| 43 | is => 'ro', | ||
| 44 | isa => 'Str', | ||
| 45 | default => 'LLM agent served through Langertha Knarr', | ||
| 46 | ); | ||
| 47 | |||
| 48 | has agent_card => ( | ||
| 49 | is => 'ro', | ||
| 50 | isa => 'HashRef', | ||
| 51 | lazy => 1, | ||
| 52 | builder => '_build_agent_card', | ||
| 53 | ); | ||
| 54 | |||
| 55 | sub _build_agent_card { | ||
| 56 | my ($self) = @_; | ||
| 57 | return { | ||
| 58 | name => $self->agent_name, | ||
| 59 | description => $self->agent_description, | ||
| 60 | url => '/', | ||
| 61 | # Knarr's own version, as knarr --version prints it; 'dev' only for a | ||
| 62 | # tree whose Langertha::Knarr carries no $VERSION. | ||
| 63 | version => $Langertha::Knarr::VERSION // 'dev', | ||
| 64 | capabilities => { | ||
| 65 | streaming => JSON::MaybeXS::true(), | ||
| 66 | pushNotifications => JSON::MaybeXS::false(), | ||
| 67 | stateTransitionHistory => JSON::MaybeXS::false(), | ||
| 68 | }, | ||
| 69 | defaultInputModes => ['text'], | ||
| 70 | defaultOutputModes => ['text'], | ||
| 71 | skills => [], | ||
| 72 | }; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub protocol_name { 'a2a' } | ||
| 76 | |||
| 77 | sub protocol_routes { | ||
| 78 | return [ | ||
| 79 | { method => 'GET', path => '/.well-known/agent.json', action => 'a2a_card' }, | ||
| 80 | { method => 'POST', path => '/', action => 'chat' }, | ||
| 81 | ]; | ||
| 82 | } | ||
| 83 | |||
| 84 | # A2A puts everything through JSON-RPC; we surface a normalized Knarr | ||
| 85 | # request from the "tasks/send" or "tasks/sendSubscribe" method params. | ||
| 86 | sub parse_chat_request { | ||
| 87 | my ($self, $http_req, $body_ref) = @_; | ||
| 88 | my $env = $self->_json->decode( $$body_ref || '{}' ); | ||
| 89 | # JSON that is no object (null would pass as an empty request): -32600. | ||
| 90 | croak('request is not a JSON object') unless ref $env eq 'HASH'; | ||
| 91 | my $method = $env->{method} // ''; | ||
| 92 | my $params = $env->{params} // {}; | ||
| 93 | my $stream = $method eq 'tasks/sendSubscribe' ? 1 : 0; | ||
| 94 | |||
| 95 | my $msg = $params->{message} // {}; | ||
| 96 | my @parts = @{ $msg->{parts} || [] }; | ||
| 97 | my $text = join '', map { ($_->{type} // '') eq 'text' ? ($_->{text} // '') : '' } @parts; | ||
| 98 | |||
| 99 | return Langertha::Knarr::Request->new( | ||
| 100 | protocol => 'a2a', | ||
| 101 | raw => $env, | ||
| 102 | messages => [ { role => 'user', content => $text } ], | ||
| 103 | stream => $stream, | ||
| 104 | session_id => $params->{sessionId} // $params->{id}, | ||
| 105 | extra => { | ||
| 106 | jsonrpc_id => $env->{id}, | ||
| 107 | task_id => $params->{id} // $self->_uuid->create_str, | ||
| 108 | a2a_method => $method, | ||
| 109 | }, | ||
| 110 | ); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub format_chat_response { | ||
| 114 | my ($self, $response, $request) = @_; | ||
| 115 | my $content = Langertha::Knarr::Response->coerce($response)->content; | ||
| 116 | my $task = { | ||
| 117 | id => $request->extra->{task_id}, | ||
| 118 | sessionId => $request->session_id, | ||
| 119 | status => { | ||
| 120 | state => 'completed', | ||
| 121 | message => { | ||
| 122 | role => 'agent', | ||
| 123 | parts => [ { type => 'text', text => $content } ], | ||
| 124 | }, | ||
| 125 | }, | ||
| 126 | artifacts => [ | ||
| 127 | { parts => [ { type => 'text', text => $content } ], index => 0, append => JSON::MaybeXS::false() }, | ||
| 128 | ], | ||
| 129 | }; | ||
| 130 | my $envelope = { | ||
| 131 | jsonrpc => '2.0', | ||
| 132 | id => $request->extra->{jsonrpc_id}, | ||
| 133 | result => $task, | ||
| 134 | }; | ||
| 135 | return ( 200, { 'Content-Type' => 'application/json' }, $self->_json->encode($envelope) ); | ||
| 136 | } | ||
| 137 | |||
| 138 | # Card endpoint helper — called via a custom action. | ||
| 139 | sub format_agent_card { | ||
| 140 | my ($self) = @_; | ||
| 141 | return ( 200, { 'Content-Type' => 'application/json' }, $self->_json->encode($self->agent_card) ); | ||
| 142 | } | ||
| 143 | |||
| 144 | sub _rpc_event { | ||
| 145 | my ($self, $request, $result) = @_; | ||
| 146 | my $env = { | ||
| 147 | jsonrpc => '2.0', | ||
| 148 | id => $request->extra->{jsonrpc_id}, | ||
| 149 | result => $result, | ||
| 150 | }; | ||
| 151 | return "data: " . $self->_json->encode($env) . "\n\n"; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub format_stream_open { | ||
| 155 | my ($self, $request) = @_; | ||
| 156 | return $self->_rpc_event( $request, { | ||
| 157 | id => $request->extra->{task_id}, | ||
| 158 | status => { state => 'working' }, | ||
| 159 | final => JSON::MaybeXS::false(), | ||
| 160 | }); | ||
| 161 | } | ||
| 162 | |||
| 163 | sub format_stream_chunk { | ||
| 164 | my ($self, $delta_text, $request) = @_; | ||
| 165 | return $self->_rpc_event( $request, { | ||
| 166 | id => $request->extra->{task_id}, | ||
| 167 | artifact => { | ||
| 168 | parts => [ { type => 'text', text => $delta_text } ], | ||
| 169 | index => 0, | ||
| 170 | append => JSON::MaybeXS::true(), | ||
| 171 | }, | ||
| 172 | final => JSON::MaybeXS::false(), | ||
| 173 | }); | ||
| 174 | } | ||
| 175 | |||
| 176 | sub format_stream_close { | ||
| 177 | my ($self, $request) = @_; | ||
| 178 | return $self->_rpc_event( $request, { | ||
| 179 | id => $request->extra->{task_id}, | ||
| 180 | status => { state => 'completed' }, | ||
| 181 | final => JSON::MaybeXS::true(), | ||
| 182 | }); | ||
| 183 | } | ||
| 184 | |||
| 185 | sub format_stream_done { '' } | ||
| 186 | |||
| 187 | # JSON-RPC's error object (k79): -32603 Internal error for a server-side | ||
| 188 | # status, -32600 Invalid Request for a client-side one; the HTTP status in | ||
| 189 | # data. The id is the request's: from the parsed request, else read off the | ||
| 190 | # raw body (the 401 comes before the parse), else null -- JSON-RPC 2.0: null | ||
| 191 | # when the id could not be detected. A body that is not JSON at all is the | ||
| 192 | # failure of the parse: 400, -32700 Parse error, id null, the decoder's own | ||
| 193 | # text kept out of the answer (k80). | ||
| 194 | sub _rpc_error { | ||
| 195 | my ($self, $status, $message, $request, $body) = @_; | ||
| 196 | my ($id, $code) = ( undef, $status >= 500 ? -32603 : -32600 ); | ||
| 197 | if ( $request ) { | ||
| 198 | $id = $request->extra->{jsonrpc_id}; | ||
| 199 | } | ||
| 200 | elsif ( defined $body && length $body ) { | ||
| 201 | my $env; | ||
| 202 | my $decoded = eval { $env = $self->_json->decode($body); 1 }; | ||
| 203 | if ( !$decoded && $status == 500 ) { | ||
| 204 | ( $status, $code, $message ) = ( 400, -32700, 'Parse error' ); | ||
| 205 | } | ||
| 206 | elsif ( ref $env eq 'HASH' ) { | ||
| 207 | $id = $env->{id} if defined $env->{id} && !ref $env->{id}; | ||
| 208 | } | ||
| 209 | elsif ( $status == 500 ) { | ||
| 210 | ( $status, $code, $message ) = ( 400, -32600, 'Invalid Request' ); | ||
| 211 | } | ||
| 212 | } | ||
| 213 | return { | ||
| 214 | jsonrpc => '2.0', | ||
| 215 | id => $id, | ||
| 216 | error => { | ||
| 217 | code => $code, | ||
| 218 | message => "$message", | ||
| 219 | data => { status => $status + 0 }, | ||
| 220 | }, | ||
| 221 | }; | ||
| 222 | } | ||
| 223 | |||
| 224 | sub error_uses_body { 1 } | ||
| 225 | |||
| 226 | # A body that is not JSON is answered by _rpc_error as -32700 / -32600 (k80). | ||
| 227 | sub request_body_error { return } | ||
| 228 | |||
| 229 | |||
| 230 | sub format_error_response { | ||
| 231 | my ($self, $status, $message, $request, $body) = @_; | ||
| 232 | my $error = $self->_rpc_error( $status, $message, $request, $body ); | ||
| 233 | return ( $error->{error}{data}{status}, { 'Content-Type' => 'application/json' }, | ||
| 234 | $self->_json->encode($error) ); | ||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | sub format_stream_error { | ||
| 239 | my ($self, $status, $message, $request) = @_; | ||
| 240 | return "data: " . $self->_json->encode( $self->_rpc_error( $status, $message, $request ) ) . "\n\n"; | ||
| 241 | } | ||
| 242 | |||
| 243 | __PACKAGE__->meta->make_immutable; | ||
| 244 | 1; | ||
| 245 | |||
| 246 | __END__ | ||