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

filter: Statementstmt 43.5% 27 / 62 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 54.5% 6 / 11 Podpod 0.0% 0 / 2 totaltotal 26.8% 33 / 123 CC 22 SCAR 50.0
CC
22
Cov
28%
_extract_text
272.0
handle_chat_f
56.0
list_models
2.0
CRAP 148.6
« blib/lib/Langertha/Knarr/Handler/Passthrough.pm ↑ Summary blib/lib/Langertha/Knarr/Manifest.pm »
1 package Langertha::Knarr::Handler::A2AClient;
2 # ABSTRACT: Knarr handler that consumes a remote A2A (Agent2Agent) agent
3 1 our $VERSION = '1.103';
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Langertha::Knarr::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 with 'Langertha::Knarr::Handler', 'Langertha::Knarr::Role::UpstreamHTTP';
12  
13  
14 1 has url => ( is => 'ro', isa => 'Str', required => 1 );
15  
16 1 has model_id => ( is => 'ro', isa => 'Str', default => 'a2a-remote' );
17  
18 0 ▶has _json => ( is => 'ro', default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
19 0 ▶has _uuid => ( is => 'ro', default => sub { Data::UUID->new } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
20  
21 0 ▶sub _extract_text {
Subroutine _extract_text: not called CC=16 SCAR=56.1
22 0 my ($self, $task) = @_;
23 0 ▶ return '' unless ref $task eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $task eq "HASH" 0 0 0
24 # Prefer artifacts with text parts; fall back to status.message.
25 0 my @bits;
26 0 ▶ for my $art ( @{ $task->{artifacts} || [] } ) {
Condition0%0/2
$task->{'artifacts'} || []
l!l
00
MC/DC0%0/1
$task->{'artifacts'} || []
$task->{'artifacts'}
Truth table0%0/2
A
A= $task->{'artifacts'}
Aresult
11
00
27 0 ▶ for my $part ( @{ $art->{parts} || [] } ) {
Condition0%0/2
$art->{'parts'} || []
l!l
00
MC/DC0%0/1
$art->{'parts'} || []
$art->{'parts'}
Truth table0%0/2
A
A= $art->{'parts'}
Aresult
11
00
28 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
29 }
30 }
31 0 ▶ unless ( @bits ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@bits) 0 0 0
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 0 return join '', @bits;
38 }
39  
40 0 ▶async sub handle_chat_f {
Subroutine handle_chat_f: not called CC=7 SCAR=40.3
Pod undocumented
41 0 my ($self, $session, $request) = @_;
42 0 ▶ my @user = grep { ($_->{role} // '') eq 'user' } @{ $request->messages };
Condition0%0/2
$_->{"role"} // ""
l!l
00
MC/DC0%0/1
$_->{"role"} // ""
$_->{"role"}
Truth table0%0/2
A
A= $_->{"role"}
Aresult
11
00
43 0 ▶ my $last = $user[-1] // { content => '' };
Condition0%0/2
$user[-1] // {"content", ""}
l!l
00
MC/DC0%0/1
$user[-1] // {"content", ""}
$user[-1]
Truth table0%0/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 0 ▶ parts => [ { type => 'text', text => $last->{content} // '' } ],
Condition0%0/2
$last->{"content"} // ""
l!l
00
MC/DC0%0/1
$last->{"content"} // ""
$last->{"content"}
Truth table0%0/2
A
A= $last->{"content"}
Aresult
11
00
55 },
56 },
57 };
58  
59 0 my $http_req = HTTP::Request->new( POST => $self->url );
60 0 $http_req->header( 'Content-Type' => 'application/json' );
61 0 $http_req->content( $self->_json->encode($envelope) );
62  
63 0 my $resp = await $self->_upstream_request_f( request => $http_req );
64 0 ▶ die "A2A remote failed: " . $resp->status_line . "\n" unless $resp->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 0 0 0
65  
66 0 my $data = $self->_json->decode( $resp->decoded_content );
67 0 ▶ if ( $data->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"error"}) 0 0 0
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 0 my $text = $self->_extract_text( $data->{result} );
71 0 return Langertha::Knarr::Response->new(
72 content => $text,
73 model => $self->model_id,
74 raw => $data,
75 );
76 1 }
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 1 __PACKAGE__->meta->make_immutable;
84 1 1;
85  
86 __END__
« blib/lib/Langertha/Knarr/Handler/Passthrough.pm ↑ Summary blib/lib/Langertha/Knarr/Manifest.pm »