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