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

filter: Statementstmt 46.1% 24 / 52 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 50.0% 5 / 10 Podpod 0.0% 0 / 2 totaltotal 31.8% 29 / 91 CC 13 SCAR 37.9
CC
13
Cov
33%
_extract_text
90.0
handle_chat_f
30.0
list_models
2.0
CRAP 44.1
« blib/lib/Langertha/Knarr/TokenStore/Pg.pm ↑ Summary blib/lib/Langertha/Knarr/Config.pm »
1 package Langertha::Knarr::Handler::ACPClient;
2 # ABSTRACT: Knarr handler that consumes a remote ACP (BeeAI) 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 Langertha::Knarr::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 with 'Langertha::Knarr::Handler', 'Langertha::Knarr::Role::UpstreamHTTP';
11  
12  
13 1 has url => ( is => 'ro', isa => 'Str', required => 1 ); # base URL of ACP server
14 1 has agent_name => ( is => 'ro', isa => 'Str', required => 1 );
15 0 ▶has model_id => ( is => 'ro', isa => 'Str', lazy => 1, default => sub { $_[0]->agent_name } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
16  
17 0 ▶has _json => ( is => 'ro', default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
18  
19 0 ▶sub _extract_text {
Subroutine _extract_text: not called CC=9 SCAR=45.0
20 0 my ($self, $run) = @_;
21 0 ▶ return '' unless ref $run eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $run eq "HASH" 0 0 0
22 0 my @bits;
23 0 ▶ for my $msg ( @{ $run->{output} || [] } ) {
Condition0%0/2
$run->{'output'} || []
l!l
00
MC/DC0%0/1
$run->{'output'} || []
$run->{'output'}
Truth table0%0/2
A
A= $run->{'output'}
Aresult
11
00
24 0 ▶ for my $part ( @{ $msg->{parts} || [] } ) {
Condition0%0/2
$msg->{'parts'} || []
l!l
00
MC/DC0%0/1
$msg->{'parts'} || []
$msg->{'parts'}
Truth table0%0/2
A
A= $msg->{'parts'}
Aresult
11
00
25 push @bits, ( $part->{content} // '' )
26 0 ▶ if ( $part->{content_type} // '' ) =~ m{^text/};
Branch: 1 branch
BranchTrueFalseTotal
if ($part->{"content_type"} // "") =~ m[^text/] 0 0 0
Condition0%0/4
$part->{"content_type"} // ""
l!l
00
$part->{"content"} // ""
l!l
00
MC/DC0%0/1
$part->{"content_type"} // ""
$part->{"content_type"}
MC/DC0%0/1
$part->{"content"} // ""
$part->{"content"}
Truth table0%0/2
A
A= $part->{"content_type"}
Aresult
11
00
Truth table0%0/2
A
A= $part->{"content"}
Aresult
11
00
27 }
28 }
29 0 return join '', @bits;
30 }
31  
32 0 ▶async sub handle_chat_f {
Subroutine handle_chat_f: not called CC=5 SCAR=34.0
Pod undocumented
33 0 my ($self, $session, $request) = @_;
34 0 ▶ my @user = grep { ($_->{role} // '') eq 'user' } @{ $request->messages };
Condition0%0/2
$_->{"role"} // ""
l!l
00
MC/DC0%0/1
$_->{"role"} // ""
$_->{"role"}
Truth table0%0/2
A
A= $_->{"role"}
Aresult
11
00
35 0 ▶ my $last = $user[-1] // { content => '' };
Condition0%0/2
$user[-1] // {"content", ""}
l!l
00
MC/DC0%0/1
$user[-1] // {"content", ""}
$user[-1]
Truth table0%0/2
A
A= $user[-1]
Aresult
11
00
36  
37 my $body = {
38 agent_name => $self->agent_name,
39 mode => 'sync',
40 input => [ {
41 0 ▶ parts => [ { content_type => 'text/plain', content => $last->{content} // '' } ],
Condition0%0/2
$last->{"content"} // ""
l!l
00
MC/DC0%0/1
$last->{"content"} // ""
$last->{"content"}
Truth table0%0/2
A
A= $last->{"content"}
Aresult
11
00
42 } ],
43 };
44  
45 0 ( my $base = $self->url ) =~ s{/$}{};
46 0 my $http_req = HTTP::Request->new( POST => "$base/runs" );
47 0 $http_req->header( 'Content-Type' => 'application/json' );
48 0 $http_req->content( $self->_json->encode($body) );
49  
50 0 my $resp = await $self->_upstream_request_f( request => $http_req );
51 0 ▶ die "ACP remote failed: " . $resp->status_line . "\n" unless $resp->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 0 0 0
52  
53 0 my $data = $self->_json->decode( $resp->decoded_content );
54 0 return Langertha::Knarr::Response->new(
55 content => $self->_extract_text($data),
56 model => $self->model_id,
57 raw => $data,
58 );
59 1 }
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 1 __PACKAGE__->meta->make_immutable;
67 1 1;
68  
69 __END__
« blib/lib/Langertha/Knarr/TokenStore/Pg.pm ↑ Summary blib/lib/Langertha/Knarr/Config.pm »