blib/lib/Langertha/Knarr/Protocol/A2A.pm

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