| 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
| |||||||||||||||||||||||||||||||
| 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'} || []
MC/DC0%0/1
$task->{'artifacts'} || []
$task->{'artifacts'}
Truth table50%1/2
A
A= $task->{'artifacts'}
| |||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | for my $part ( @{ $art->{parts} || [] } ) { | ||||||||||||||||||||||||||||
|
Condition50%1/2
$art->{'parts'} || []
MC/DC0%0/1
$art->{'parts'} || []
$art->{'parts'}
Truth table50%1/2
A
A= $art->{'parts'}
| |||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | push @bits, ($part->{text} // '') if ($part->{type} // '') eq 'text'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$part->{"type"} // ""
$part->{"text"} // ""
MC/DC0%0/1
$part->{"type"} // ""
$part->{"type"}
MC/DC0%0/1
$part->{"text"} // ""
$part->{"text"}
Truth table50%1/2
A
A= $part->{"type"}
Truth table50%1/2
A
A= $part->{"text"}
| |||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | 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 | 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"} // ""
MC/DC0%0/1
$_->{"role"} // ""
$_->{"role"}
Truth table50%1/2
A
A= $_->{"role"}
| |||||||||||||||||||||||||||||||
| 43 | 2 | ▶ | my $last = $user[-1] // { content => '' }; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$user[-1] // {"content", ""}
MC/DC0%0/1
$user[-1] // {"content", ""}
$user[-1]
Truth table50%1/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 | 2 | ▶ | parts => [ { type => 'text', text => $last->{content} // '' } ], | ||||||||||||||||||||||||||||
|
Condition50%1/2
$last->{"content"} // ""
MC/DC0%0/1
$last->{"content"} // ""
$last->{"content"}
Truth table50%1/2
A
A= $last->{"content"}
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||
| 66 | 1 | my $data = $self->_json->decode( $resp->decoded_content ); | |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | 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 | 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 | ||||||||||||||||||||||||||||||