| 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
| |||||||||||||||||||||||||||||||
| 22 | 0 | my @bits; | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | for my $msg ( @{ $run->{output} || [] } ) { | ||||||||||||||||||||||||||||
|
Condition0%0/2
$run->{'output'} || []
MC/DC0%0/1
$run->{'output'} || []
$run->{'output'}
Truth table0%0/2
A
A= $run->{'output'}
| |||||||||||||||||||||||||||||||
| 24 | 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'}
| |||||||||||||||||||||||||||||||
| 25 | push @bits, ( $part->{content} // '' ) | ||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | if ( $part->{content_type} // '' ) =~ m{^text/}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/4
$part->{"content_type"} // ""
$part->{"content"} // ""
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"}
Truth table0%0/2
A
A= $part->{"content"}
| |||||||||||||||||||||||||||||||
| 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"} // ""
MC/DC0%0/1
$_->{"role"} // ""
$_->{"role"}
Truth table0%0/2
A
A= $_->{"role"}
| |||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | my $last = $user[-1] // { content => '' }; | ||||||||||||||||||||||||||||
|
Condition0%0/2
$user[-1] // {"content", ""}
MC/DC0%0/1
$user[-1] // {"content", ""}
$user[-1]
Truth table0%0/2
A
A= $user[-1]
| |||||||||||||||||||||||||||||||
| 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"} // ""
MC/DC0%0/1
$last->{"content"} // ""
$last->{"content"}
Truth table0%0/2
A
A= $last->{"content"}
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||