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

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