blib/lib/Langertha/Knarr/Handler/A2AClient.pm

filter: Statementstmt 88.7% 55 / 62 Branchbran 41.6% 5 / 12 Conditioncond 29.1% 7 / 24 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 2 totaltotal 62.6% 77 / 123 CC 22 SCAR 34.6
CC
22
Cov
68%
_extract_text
59.2
handle_chat_f
8.0
list_models
2.0
CRAP 31.7
« blib/lib/Langertha/Knarr/CLI/Cmd/Start.pm ↑ Summary blib/lib/Langertha/Knarr/CLI/Cmd/Check.pm »
1 package Langertha::Knarr::Handler::A2AClient;
2 # ABSTRACT: Knarr handler that consumes a remote A2A (Agent2Agent) agent
3 3 our $VERSION = '1.102';
4 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Langertha::Knarr::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 with 'Langertha::Knarr::Handler', 'Langertha::Knarr::Role::UpstreamHTTP';
12  
13  
14 3 has url => ( is => 'ro', isa => 'Str', required => 1 );
15  
16 3 has model_id => ( is => 'ro', isa => 'Str', default => 'a2a-remote' );
17  
18 2 ▶has _json => ( is => 'ro', default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
19 2 ▶has _uuid => ( is => 'ro', default => sub { Data::UUID->new } );
Subroutine __ANON__: called CC=1 SCAR=0.0
20  
21 1 ▶sub _extract_text {
Subroutine _extract_text: called CC=16 SCAR=40.8
22 1 my ($self, $task) = @_;
23 1 ▶ return '' unless ref $task eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $task eq "HASH" 0 1 1
24 # Prefer artifacts with text parts; fall back to status.message.
25 1 my @bits;
26 1 ▶ for my $art ( @{ $task->{artifacts} || [] } ) {
Condition50%1/2
$task->{'artifacts'} || []
l!l
10
MC/DC0%0/1
$task->{'artifacts'} || []
$task->{'artifacts'}
Truth table50%1/2
A
A= $task->{'artifacts'}
Aresult
11
00
27 1 ▶ for my $part ( @{ $art->{parts} || [] } ) {
Condition50%1/2
$art->{'parts'} || []
l!l
10
MC/DC0%0/1
$art->{'parts'} || []
$art->{'parts'}
Truth table50%1/2
A
A= $art->{'parts'}
Aresult
11
00
28 1 ▶ push @bits, ($part->{text} // '') if ($part->{type} // '') eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
if ($part->{"type"} // "") eq "text" 1 0 1
Condition50%2/4
$part->{"type"} // ""
l!l
10
$part->{"text"} // ""
l!l
10
MC/DC0%0/1
$part->{"type"} // ""
$part->{"type"}
MC/DC0%0/1
$part->{"text"} // ""
$part->{"text"}
Truth table50%1/2
A
A= $part->{"type"}
Aresult
11
00
Truth table50%1/2
A
A= $part->{"text"}
Aresult
11
00
29 }
30 }
31 1 ▶ unless ( @bits ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@bits) 0 1 1
32 0 ▶ my $msg = $task->{status}{message} // {};
Condition0%0/2
$task->{"status"}{"message"} // {}
l!l
00
MC/DC0%0/1
$task->{"status"}{"message"} // {}
$task->{"status"}{"message"}
Truth table0%0/2
A
A= $task->{"status"}{"message"}
Aresult
11
00
33 0 ▶ for my $part ( @{ $msg->{parts} || [] } ) {
Condition0%0/2
$msg->{'parts'} || []
l!l
00
MC/DC0%0/1
$msg->{'parts'} || []
$msg->{'parts'}
Truth table0%0/2
A
A= $msg->{'parts'}
Aresult
11
00
34 0 ▶ push @bits, ($part->{text} // '') if ($part->{type} // '') eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
if ($part->{"type"} // "") eq "text" 0 0 0
Condition0%0/4
$part->{"type"} // ""
l!l
00
$part->{"text"} // ""
l!l
00
MC/DC0%0/1
$part->{"type"} // ""
$part->{"type"}
MC/DC0%0/1
$part->{"text"} // ""
$part->{"text"}
Truth table0%0/2
A
A= $part->{"type"}
Aresult
11
00
Truth table0%0/2
A
A= $part->{"text"}
Aresult
11
00
35 }
36 }
37 1 return join '', @bits;
38 }
39  
40 2 ▶async sub handle_chat_f {
Subroutine handle_chat_f: called CC=7 SCAR=20.8
Pod undocumented
41 2 my ($self, $session, $request) = @_;
42 2 ▶ my @user = grep { ($_->{role} // '') eq 'user' } @{ $request->messages };
Condition50%1/2
$_->{"role"} // ""
l!l
20
MC/DC0%0/1
$_->{"role"} // ""
$_->{"role"}
Truth table50%1/2
A
A= $_->{"role"}
Aresult
11
00
43 2 ▶ my $last = $user[-1] // { content => '' };
Condition50%1/2
$user[-1] // {"content", ""}
l!l
20
MC/DC0%0/1
$user[-1] // {"content", ""}
$user[-1]
Truth table50%1/2
A
A= $user[-1]
Aresult
11
00
44  
45 my $envelope = {
46 jsonrpc => '2.0',
47 id => $self->_uuid->create_str,
48 method => 'tasks/send',
49 params => {
50 id => $self->_uuid->create_str,
51 sessionId => $session->id,
52 message => {
53 role => 'user',
54 2 ▶ parts => [ { type => 'text', text => $last->{content} // '' } ],
Condition50%1/2
$last->{"content"} // ""
l!l
20
MC/DC0%0/1
$last->{"content"} // ""
$last->{"content"}
Truth table50%1/2
A
A= $last->{"content"}
Aresult
11
00
55 },
56 },
57 };
58  
59 2 my $http_req = HTTP::Request->new( POST => $self->url );
60 2 $http_req->header( 'Content-Type' => 'application/json' );
61 2 $http_req->content( $self->_json->encode($envelope) );
62  
63 2 my $resp = await $self->_upstream_request_f( request => $http_req );
64 1 ▶ die "A2A remote failed: " . $resp->status_line . "\n" unless $resp->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 0 1 1
65  
66 1 my $data = $self->_json->decode( $resp->decoded_content );
67 1 ▶ if ( $data->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"error"}) 0 1 1
68 0 ▶ die "A2A error: " . ( $data->{error}{message} // 'unknown' ) . "\n";
Condition0%0/2
$data->{"error"}{"message"} // "unknown"
l!l
00
MC/DC0%0/1
$data->{"error"}{"message"} // "unknown"
$data->{"error"}{"message"}
Truth table0%0/2
A
A= $data->{"error"}{"message"}
Aresult
11
00
69 }
70 1 my $text = $self->_extract_text( $data->{result} );
71 1 return Langertha::Knarr::Response->new(
72 content => $text,
73 model => $self->model_id,
74 raw => $data,
75 );
76 3 }
77  
78 0 ▶sub list_models {
Subroutine list_models: not called CC=1 SCAR=6.9
Pod undocumented
79 0 my ($self) = @_;
80 0 return [ { id => $self->model_id, object => 'model' } ];
81 }
82  
83 3 __PACKAGE__->meta->make_immutable;
84 3 1;
85  
86 __END__
87  
88 =pod
89  
90 =encoding UTF-8
91  
92 =head1 NAME
93  
94 Langertha::Knarr::Handler::A2AClient - Knarr handler that consumes a remote A2A (Agent2Agent) agent
95  
96 =head1 VERSION
97  
98 version 1.102
99  
100 =head1 SYNOPSIS
101  
102 use Langertha::Knarr::Handler::A2AClient;
103  
104 my $handler = Langertha::Knarr::Handler::A2AClient->new(
105 url => 'https://some-agent.example/',
106 );
107  
108 =head1 DESCRIPTION
109  
110 Consumes a remote Google Agent2Agent (A2A) agent as a Knarr backend.
111 Each chat request is wrapped in a JSON-RPC 2.0 C<tasks/send> envelope
112 and POSTed to the upstream endpoint; the returned task's text artifacts
113 become the response.
114  
115 Combined with a Knarr instance speaking OpenAI on the front side, this
116 turns Knarr into a universal protocol translator: OpenWebUI → Knarr
117 (OpenAI) → A2AClient → remote A2A agent.
118  
119 =head2 url
120  
121 Required. Base URL of the upstream A2A agent.
122  
123 =head2 timeout
124  
125 Seconds the remote agent may take to answer, in total. Default C<300>;
126 C<0> disables it. See L<Langertha::Knarr::Role::UpstreamHTTP>.
127  
128 =head2 model_id
129  
130 Optional. Defaults to C<a2a-remote>.
131  
132 =head1 SUPPORT
133  
134 =head2 Issues
135  
136 Please report bugs and feature requests on GitHub at
137 L<https://github.com/Getty/langertha-knarr/issues>.
138  
139 =head2 IRC
140  
141 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
142  
143 =head1 CONTRIBUTING
144  
145 Contributions are welcome! Please fork the repository and submit a pull request.
146  
147 =head1 AUTHOR
148  
149 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
150  
151 =head1 COPYRIGHT AND LICENSE
152  
153 This software is copyright (c) 2026 by Torsten Raudssus.
154  
155 This is free software; you can redistribute it and/or modify it under
156 the same terms as the Perl 5 programming language system itself.
157  
158 =cut
« blib/lib/Langertha/Knarr/CLI/Cmd/Start.pm ↑ Summary blib/lib/Langertha/Knarr/CLI/Cmd/Check.pm »