| 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
| |||||||||||||||||||||||||||||||
| 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'} || []
MC/DC0%0/1
$task->{'artifacts'} || []
$task->{'artifacts'}
Truth table0%0/2
A
A= $task->{'artifacts'}
| |||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | for my $part ( @{ $art->{parts} || [] } ) { | ||||||||||||||||||||||||||||
|
Condition0%0/2
$art->{'parts'} || []
MC/DC0%0/1
$art->{'parts'} || []
$art->{'parts'}
Truth table0%0/2
A
A= $art->{'parts'}
| |||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | push @bits, ($part->{text} // '') if ($part->{type} // '') eq 'text'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/4
$part->{"type"} // ""
$part->{"text"} // ""
MC/DC0%0/1
$part->{"type"} // ""
$part->{"type"}
MC/DC0%0/1
$part->{"text"} // ""
$part->{"text"}
Truth table0%0/2
A
A= $part->{"type"}
Truth table0%0/2
A
A= $part->{"text"}
| |||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | unless ( @bits ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | my $msg = $task->{status}{message} // {}; | ||||||||||||||||||||||||||||
|
Condition0%0/2
$task->{"status"}{"message"} // {}
MC/DC0%0/1
$task->{"status"}{"message"} // {}
$task->{"status"}{"message"}
Truth table0%0/2
A
A= $task->{"status"}{"message"}
| |||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | for my $part ( @{ $msg->{parts} || [] } ) { | ||||||||||||||||||||||||||||
|
Condition0%0/2
$msg->{'parts'} || []
MC/DC0%0/1
$msg->{'parts'} || []
$msg->{'parts'}
Truth table0%0/2
A
A= $msg->{'parts'}
| |||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | push @bits, ($part->{text} // '') if ($part->{type} // '') eq 'text'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/4
$part->{"type"} // ""
$part->{"text"} // ""
MC/DC0%0/1
$part->{"type"} // ""
$part->{"type"}
MC/DC0%0/1
$part->{"text"} // ""
$part->{"text"}
Truth table0%0/2
A
A= $part->{"type"}
Truth table0%0/2
A
A= $part->{"text"}
| |||||||||||||||||||||||||||||||
| 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"} // ""
MC/DC0%0/1
$_->{"role"} // ""
$_->{"role"}
Truth table0%0/2
A
A= $_->{"role"}
| |||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | my $last = $user[-1] // { content => '' }; | ||||||||||||||||||||||||||||
|
Condition0%0/2
$user[-1] // {"content", ""}
MC/DC0%0/1
$user[-1] // {"content", ""}
$user[-1]
Truth table0%0/2
A
A= $user[-1]
| |||||||||||||||||||||||||||||||
| 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"} // ""
MC/DC0%0/1
$last->{"content"} // ""
$last->{"content"}
Truth table0%0/2
A
A= $last->{"content"}
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||
| 66 | 0 | my $data = $self->_json->decode( $resp->decoded_content ); | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ( $data->{error} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | die "A2A error: " . ( $data->{error}{message} // 'unknown' ) . "\n"; | ||||||||||||||||||||||||||||
|
Condition0%0/2
$data->{"error"}{"message"} // "unknown"
MC/DC0%0/1
$data->{"error"}{"message"} // "unknown"
$data->{"error"}{"message"}
Truth table0%0/2
A
A= $data->{"error"}{"message"}
| |||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||