Knarr/Handler/ACPClient.pm

filter: Statementstmt 95.3% 62 / 65 Branchbran 50.0% 5 / 10 Conditioncond 50.0% 5 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 85.7% 12 / 14 Podpod 0.0% 0 / 2 totaltotal 79.2% 84 / 106 CC 13 SCAR 16.6
CC
13
Cov
85%
_extract_text
10.9
handle_chat_f
5.1
list_models
2.0
CRAP 5.3
« Knarr/Response.pm ↑ Summary Knarr/Protocol/OpenAI.pm »
1 package Langertha::Knarr::Handler::ACPClient;
2 # ABSTRACT: Steerboard handler that consumes a remote ACP (BeeAI) 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 Net::Async::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Langertha::Knarr::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 with 'Langertha::Knarr::Handler';
13  
14  
15 2 has url => ( is => 'ro', isa => 'Str', required => 1 ); # base URL of ACP server
16 2 has agent_name => ( is => 'ro', isa => 'Str', required => 1 );
17 1 ▶has model_id => ( is => 'ro', isa => 'Str', lazy => 1, default => sub { $_[0]->agent_name } );
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 0 ▶has loop => ( is => 'ro', lazy => 1, default => sub { IO::Async::Loop->new } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
20  
21 2 has _http => ( is => 'ro', lazy => 1, builder => '_build_http' );
22 sub _build_http {
23 1 ▶ my ($self) = @_;
Subroutine _build_http: called CC=1 SCAR=0.0
24 1 my $h = Net::Async::HTTP->new;
25 1 $self->loop->add($h);
26 1 return $h;
27 }
28  
29 1 ▶has _json => ( is => 'ro', default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
30  
31 sub _extract_text {
32 1 ▶ my ($self, $run) = @_;
Subroutine _extract_text: called CC=9 SCAR=23.9
33 1 ▶ return '' unless ref $run eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $run eq "HASH" 0 1 1
34 1 my @bits;
35 1 ▶ for my $msg ( @{ $run->{output} || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$run->{"output"} or [] 0 1 1
36 1 ▶ for my $part ( @{ $msg->{parts} || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$msg->{"parts"} or [] 0 1 1
37 push @bits, ( $part->{content} // '' )
38 1 ▶ if ( $part->{content_type} // '' ) =~ m{^text/};
Branch: 1 branch
BranchTrueFalseTotal
if ($part->{"content_type"} // "") =~ m[^text/] 1 0 1
Condition50%2/4
$part->{"content_type"} // ""
l!l
10
$part->{"content"} // ""
l!l
10
MC/DC0%0/1
$part->{"content_type"} // ""
$part->{"content_type"}
MC/DC0%0/1
$part->{"content"} // ""
$part->{"content"}
Truth table50%1/2
A
A= $part->{"content_type"}
Aresult
11
00
Truth table50%1/2
A
A= $part->{"content"}
Aresult
11
00
39 }
40 }
41 1 return join '', @bits;
42 }
43  
44 1 ▶async sub handle_chat_f {
Subroutine handle_chat_f: called CC=5 SCAR=16.4
Pod undocumented
45 1 my ($self, $session, $request) = @_;
46 1 ▶ my @user = grep { ($_->{role} // '') eq 'user' } @{ $request->messages };
Condition50%1/2
$_->{"role"} // ""
l!l
10
MC/DC0%0/1
$_->{"role"} // ""
$_->{"role"}
Truth table50%1/2
A
A= $_->{"role"}
Aresult
11
00
47 1 ▶ my $last = $user[-1] // { content => '' };
Condition50%1/2
$user[-1] // {"content", ""}
l!l
10
MC/DC0%0/1
$user[-1] // {"content", ""}
$user[-1]
Truth table50%1/2
A
A= $user[-1]
Aresult
11
00
48  
49 my $body = {
50 agent_name => $self->agent_name,
51 mode => 'sync',
52 input => [ {
53 1 ▶ parts => [ { content_type => 'text/plain', content => $last->{content} // '' } ],
Condition50%1/2
$last->{"content"} // ""
l!l
10
MC/DC0%0/1
$last->{"content"} // ""
$last->{"content"}
Truth table50%1/2
A
A= $last->{"content"}
Aresult
11
00
54 } ],
55 };
56  
57 1 ( my $base = $self->url ) =~ s{/$}{};
58 1 my $http_req = HTTP::Request->new( POST => "$base/runs" );
59 1 $http_req->header( 'Content-Type' => 'application/json' );
60 1 $http_req->content( $self->_json->encode($body) );
61  
62 1 my $resp = await $self->_http->do_request( request => $http_req );
63 1 ▶ die "ACP remote failed: " . $resp->status_line . "\n" unless $resp->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 0 1 1
64  
65 1 my $data = $self->_json->decode( $resp->decoded_content );
66 1 return Langertha::Knarr::Response->new(
67 content => $self->_extract_text($data),
68 model => $self->model_id,
69 raw => $data,
70 );
71 2 }
72  
73 sub list_models {
74 0 ▶ my ($self) = @_;
Subroutine list_models: not called CC=1 SCAR=6.9
Pod undocumented
75 0 return [ { id => $self->model_id, object => 'model' } ];
76 }
77  
78 2 __PACKAGE__->meta->make_immutable;
79 2 1;
80  
81 __END__
82  
83 =pod
84  
85 =encoding UTF-8
86  
87 =head1 NAME
88  
89 Langertha::Knarr::Handler::ACPClient - Steerboard handler that consumes a remote ACP (BeeAI) agent
90  
91 =head1 VERSION
92  
93 version 1.100
94  
95 =head1 SYNOPSIS
96  
97 use Langertha::Knarr::Handler::ACPClient;
98  
99 my $handler = Langertha::Knarr::Handler::ACPClient->new(
100 url => 'https://some-acp-server.example',
101 agent_name => 'my-agent',
102 );
103  
104 =head1 DESCRIPTION
105  
106 Consumes a remote IBM/BeeAI Agent Communication Protocol (ACP) agent
107 as a Knarr backend. Each chat request is sent as a synchronous
108 C<POST /runs> with a single C<text/plain> input part; the returned
109 run's output text becomes the response.
110  
111 Pair with a front-side Knarr speaking OpenAI to expose any ACP agent
112 to OpenAI-format clients.
113  
114 =head2 url
115  
116 Required. Base URL of the upstream ACP server (path C</runs> is
117 appended).
118  
119 =head2 agent_name
120  
121 Required. The C<agent_name> to send in each ACP run request.
122  
123 =head2 model_id
124  
125 Optional. Defaults to L</agent_name>.
126  
127 =head1 SUPPORT
128  
129 =head2 Issues
130  
131 Please report bugs and feature requests on GitHub at
132 L<https://github.com/Getty/langertha-knarr/issues>.
133  
134 =head2 IRC
135  
136 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
137  
138 =head1 CONTRIBUTING
139  
140 Contributions are welcome! Please fork the repository and submit a pull request.
141  
142 =head1 AUTHOR
143  
144 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
145  
146 =head1 COPYRIGHT AND LICENSE
147  
148 This software is copyright (c) 2026 by Torsten Raudssus.
149  
150 This is free software; you can redistribute it and/or modify it under
151 the same terms as the Perl 5 programming language system itself.
152  
153 =cut
« Knarr/Response.pm ↑ Summary Knarr/Protocol/OpenAI.pm »