Knarr/Protocol/A2A.pm

filter: Statementstmt 100% 59 / 59 Branchbran 66.6% 4 / 6 Conditioncond 50.0% 9 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 9 totaltotal 75.2% 91 / 121 CC 12 SCAR 19.7
CC
12
Cov
87%
parse_chat_request
16.5
BEGIN
1.0
BEGIN
1.0
CRAP 7.2
« Knarr/Protocol/Anthropic.pm ↑ Summary Knarr/CLI/Cmd/Init.pm »
1 package Langertha::Knarr::Protocol::A2A;
2 # ABSTRACT: Google Agent2Agent (A2A) wire protocol for Knarr
3  
4 10 our $VERSION = '1.100';
5 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Time::HiRes qw( time );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Langertha::Knarr::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Langertha::Knarr::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 with 'Langertha::Knarr::Protocol';
13  
14 # --- A2A overview ---
15 # Discovery: GET /.well-known/agent.json -> AgentCard JSON
16 # Method bus: POST / body: JSON-RPC 2.0 envelope
17 # methods: "tasks/send", "tasks/sendSubscribe", "tasks/get",
18 # "tasks/cancel", "tasks/pushNotification/set", ...
19 #
20 # tasks/send (sync): returns Task JSON in JSON-RPC result
21 # tasks/sendSubscribe (streaming): returns SSE stream of JSON-RPC responses
22 # Each event is a full JSON-RPC response wrapping a TaskStatusUpdateEvent or
23 # TaskArtifactUpdateEvent. Final event has status.state = "completed".
24 #
25 # Streaming model on the wire:
26 # data: {"jsonrpc":"2.0","id":<reqid>,"result":{"id":"<task>","status":{"state":"working","message":{...}}, "final":false}}\n\n
27 # data: {"jsonrpc":"2.0","id":<reqid>,"result":{"id":"<task>","artifact":{"parts":[{"type":"text","text":"Hi"}], "index":0,"append":true}, "final":false}}\n\n
28 # data: {"jsonrpc":"2.0","id":<reqid>,"result":{"id":"<task>","status":{"state":"completed"},"final":true}}\n\n
29 # ---------------------------------------------------------------------------
30  
31 15 ▶has _json => ( is => 'ro', default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
32 15 ▶has _uuid => ( is => 'ro', default => sub { Data::UUID->new } );
Subroutine __ANON__: called CC=1 SCAR=0.0
33  
34 10 has agent_card => (
35 is => 'ro',
36 isa => 'HashRef',
37 lazy => 1,
38 builder => '_build_agent_card',
39 );
40  
41 sub _build_agent_card {
42 1 ▶ my ($self) = @_;
Subroutine _build_agent_card: called CC=1 SCAR=0.0
43 return {
44 1 name => 'Langertha Steerboard Agent',
45 description => 'Steerboard-exposed agent',
46 url => '/',
47 version => '0.0.1',
48 capabilities => {
49 streaming => JSON::MaybeXS::true(),
50 pushNotifications => JSON::MaybeXS::false(),
51 stateTransitionHistory => JSON::MaybeXS::false(),
52 },
53 defaultInputModes => ['text'],
54 defaultOutputModes => ['text'],
55 skills => [],
56 };
57 }
58  
59 1 ▶sub protocol_name { 'a2a' }
Subroutine protocol_name: called CC=1 SCAR=0.0
Pod undocumented
60  
61 sub protocol_routes {
62 return [
63 14 ▶ { method => 'GET', path => '/.well-known/agent.json', action => 'a2a_card' },
Subroutine protocol_routes: called CC=1 SCAR=0.0
Pod undocumented
64 { method => 'POST', path => '/', action => 'chat' },
65 ];
66 }
67  
68 # A2A puts everything through JSON-RPC; we surface a normalized Steerboard
69 # request from the "tasks/send" or "tasks/sendSubscribe" method params.
70 sub parse_chat_request {
71 3 ▶ my ($self, $http_req, $body_ref) = @_;
Subroutine parse_chat_request: called CC=12 SCAR=28.0
Pod undocumented
72 3 ▶ my $env = $self->_json->decode( $$body_ref || '{}' );
Condition50%1/2
$$body_ref || '{}'
l!l
30
MC/DC0%0/1
$$body_ref || '{}'
$$body_ref
Truth table50%1/2
A
A= $$body_ref
Aresult
11
00
73 3 ▶ my $method = $env->{method} // '';
Condition50%1/2
$env->{"method"} // ""
l!l
30
MC/DC0%0/1
$env->{"method"} // ""
$env->{"method"}
Truth table50%1/2
A
A= $env->{"method"}
Aresult
11
00
74 3 ▶ my $params = $env->{params} // {};
Condition50%1/2
$env->{"params"} // {}
l!l
30
MC/DC0%0/1
$env->{"params"} // {}
$env->{"params"}
Truth table50%1/2
A
A= $env->{"params"}
Aresult
11
00
75 3 ▶ my $stream = $method eq 'tasks/sendSubscribe' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$method eq 'tasks/sendSubscribe' ? : 2 1 3
76  
77 3 ▶ my $msg = $params->{message} // {};
Condition50%1/2
$params->{"message"} // {}
l!l
30
MC/DC0%0/1
$params->{"message"} // {}
$params->{"message"}
Truth table50%1/2
A
A= $params->{"message"}
Aresult
11
00
78 3 ▶ my @parts = @{ $msg->{parts} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$msg->{"parts"} or [] 0 3 3
79 3 ▶ my $text = join '', map { ($_->{type} // '') eq 'text' ? ($_->{text} // '') : '' } @parts;
Branch: 1 branch
BranchTrueFalseTotal
($_->{'type'} // '') eq 'text' ? : 3 0 3
Condition50%2/4
$_->{"type"} // ""
l!l
30
$_->{"text"} // ""
l!l
30
MC/DC0%0/1
$_->{"type"} // ""
$_->{"type"}
MC/DC0%0/1
$_->{"text"} // ""
$_->{"text"}
Truth table50%1/2
A
A= $_->{"type"}
Aresult
11
00
Truth table50%1/2
A
A= $_->{"text"}
Aresult
11
00
80  
81 return Langertha::Knarr::Request->new(
82 protocol => 'a2a',
83 raw => $env,
84 messages => [ { role => 'user', content => $text } ],
85 stream => $stream,
86 session_id => $params->{sessionId} // $params->{id},
87 extra => {
88 jsonrpc_id => $env->{id},
89 3 ▶ task_id => $params->{id} // $self->_uuid->create_str,
Condition50%3/6
$params->{"sessionId"} // $params->{"id"}
l!l&&r!l&&!r
120
$params->{"id"} // $self->_uuid->create_str
l!l&&r!l&&!r
300
MC/DC0%0/2
$params->{"sessionId"} // $params->{"id"}
$params->{"sessionId"} $params->{"id"}
MC/DC0%0/2
$params->{"id"} // $self->_uuid->create_str
$params->{"id"} $self->_uuid->create_str
Truth table66%2/3
A // B
A= $params->{"sessionId"}B= $params->{"id"}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $params->{"id"}B= $self->_uuid->create_str
ABresult
1X1
011
000
90 a2a_method => $method,
91 },
92 );
93 }
94  
95 sub format_chat_response {
96 2 ▶ my ($self, $response, $request) = @_;
Subroutine format_chat_response: called CC=1 SCAR=0.0
Pod undocumented
97 2 my $content = Langertha::Knarr::Response->coerce($response)->content;
98 my $task = {
99 id => $request->extra->{task_id},
100 2 sessionId => $request->session_id,
101 status => {
102 state => 'completed',
103 message => {
104 role => 'agent',
105 parts => [ { type => 'text', text => $content } ],
106 },
107 },
108 artifacts => [
109 { parts => [ { type => 'text', text => $content } ], index => 0, append => JSON::MaybeXS::false() },
110 ],
111 };
112 my $envelope = {
113 jsonrpc => '2.0',
114 id => $request->extra->{jsonrpc_id},
115 2 result => $task,
116 };
117 2 return ( 200, { 'Content-Type' => 'application/json' }, $self->_json->encode($envelope) );
118 }
119  
120 # Card endpoint helper — called via a custom action.
121 sub format_agent_card {
122 1 ▶ my ($self) = @_;
Subroutine format_agent_card: called CC=1 SCAR=0.0
Pod undocumented
123 1 return ( 200, { 'Content-Type' => 'application/json' }, $self->_json->encode($self->agent_card) );
124 }
125  
126 sub _rpc_event {
127 9 ▶ my ($self, $request, $result) = @_;
Subroutine _rpc_event: called CC=1 SCAR=0.0
128 my $env = {
129 jsonrpc => '2.0',
130 id => $request->extra->{jsonrpc_id},
131 9 result => $result,
132 };
133 9 return "data: " . $self->_json->encode($env) . "\n\n";
134 }
135  
136 sub format_stream_open {
137 2 ▶ my ($self, $request) = @_;
Subroutine format_stream_open: called CC=1 SCAR=0.0
Pod undocumented
138 return $self->_rpc_event( $request, {
139 id => $request->extra->{task_id},
140 2 status => { state => 'working' },
141 final => JSON::MaybeXS::false(),
142 });
143 }
144  
145 sub format_stream_chunk {
146 5 ▶ my ($self, $delta_text, $request) = @_;
Subroutine format_stream_chunk: called CC=1 SCAR=0.0
Pod undocumented
147 return $self->_rpc_event( $request, {
148 id => $request->extra->{task_id},
149 5 artifact => {
150 parts => [ { type => 'text', text => $delta_text } ],
151 index => 0,
152 append => JSON::MaybeXS::true(),
153 },
154 final => JSON::MaybeXS::false(),
155 });
156 }
157  
158 sub format_stream_close {
159 2 ▶ my ($self, $request) = @_;
Subroutine format_stream_close: called CC=1 SCAR=0.0
Pod undocumented
160 return $self->_rpc_event( $request, {
161 id => $request->extra->{task_id},
162 2 status => { state => 'completed' },
163 final => JSON::MaybeXS::true(),
164 });
165 }
166  
167 2 ▶sub format_stream_done { '' }
Subroutine format_stream_done: called CC=1 SCAR=0.0
Pod undocumented
168  
169 10 __PACKAGE__->meta->make_immutable;
170 10 1;
171  
172 __END__
173  
174 =pod
175  
176 =encoding UTF-8
177  
178 =head1 NAME
179  
180 Langertha::Knarr::Protocol::A2A - Google Agent2Agent (A2A) wire protocol for Knarr
181  
182 =head1 VERSION
183  
184 version 1.100
185  
186 =head1 DESCRIPTION
187  
188 Implements the Google A2A protocol on top of
189 L<Langertha::Knarr::Protocol>. Loaded by default.
190  
191 =over
192  
193 =item * C<GET /.well-known/agent.json> — agent card discovery (anonymous)
194  
195 =item * C<POST /> — JSON-RPC 2.0 method bus
196  
197 =back
198  
199 Supported methods: C<tasks/send> (sync) and C<tasks/sendSubscribe>
200 (streaming). Streaming wraps every chunk in a JSON-RPC envelope with
201 the original C<id> preserved, transitions C<status.state> from
202 C<working> to C<completed>, and emits artifact append events for the
203 text deltas.
204  
205 =head2 agent_card
206  
207 The HashRef returned by the discovery endpoint. Defaults to a generic
208 "Knarr Agent" card with C<streaming: true>; override to advertise
209 specific skills, version, etc.
210  
211 =head1 SUPPORT
212  
213 =head2 Issues
214  
215 Please report bugs and feature requests on GitHub at
216 L<https://github.com/Getty/langertha-knarr/issues>.
217  
218 =head2 IRC
219  
220 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
221  
222 =head1 CONTRIBUTING
223  
224 Contributions are welcome! Please fork the repository and submit a pull request.
225  
226 =head1 AUTHOR
227  
228 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
229  
230 =head1 COPYRIGHT AND LICENSE
231  
232 This software is copyright (c) 2026 by Torsten Raudssus.
233  
234 This is free software; you can redistribute it and/or modify it under
235 the same terms as the Perl 5 programming language system itself.
236  
237 =cut
« Knarr/Protocol/Anthropic.pm ↑ Summary Knarr/CLI/Cmd/Init.pm »