| 1 | package Langertha::Knarr::Protocol::ACP; | ||||||||||||||||||||||
| 2 | # ABSTRACT: BeeAI/IBM Agent Communication Protocol (ACP) for Knarr | ||||||||||||||||||||||
| 3 | |||||||||||||||||||||||
| 4 | 1 | our $VERSION = '1.103'; | |||||||||||||||||||||
| 5 | 1 | ▶ | use Moose; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 1 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 1 | ▶ | use Data::UUID; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 1 | ▶ | use Time::HiRes qw( time ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 1 | ▶ | use Langertha::Knarr::Request; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 1 | ▶ | use Langertha::Knarr::Response; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | 1 | with 'Langertha::Knarr::Protocol'; | |||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | # --- ACP overview --- | ||||||||||||||||||||||
| 15 | # REST-style, oriented around "agents" and "runs": | ||||||||||||||||||||||
| 16 | # GET /agents — list agents | ||||||||||||||||||||||
| 17 | # POST /runs — create a run (sync or stream) | ||||||||||||||||||||||
| 18 | # GET /runs/{run_id} — fetch run | ||||||||||||||||||||||
| 19 | # POST /runs/{run_id}/cancel | ||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||
| 21 | # POST /runs body: | ||||||||||||||||||||||
| 22 | # { "agent_name": "...", | ||||||||||||||||||||||
| 23 | # "input": [ { "parts": [ { "content_type":"text/plain", "content":"..." } ] } ], | ||||||||||||||||||||||
| 24 | # "mode": "sync" | "stream" | "async" } | ||||||||||||||||||||||
| 25 | # | ||||||||||||||||||||||
| 26 | # Streaming model: when mode=stream, response is SSE with named events: | ||||||||||||||||||||||
| 27 | # event: run.created | ||||||||||||||||||||||
| 28 | # event: message.created | ||||||||||||||||||||||
| 29 | # event: message.part data: { "part": { "content_type":"text/plain","content":"Hi" } } | ||||||||||||||||||||||
| 30 | # event: message.completed | ||||||||||||||||||||||
| 31 | # event: run.completed | ||||||||||||||||||||||
| 32 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||
| 33 | |||||||||||||||||||||||
| 34 | 0 | ▶ | has _json => ( is => 'ro', default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) } ); | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||
| 35 | 0 | ▶ | has _uuid => ( is => 'ro', default => sub { Data::UUID->new } ); | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | 0 | ▶ | sub protocol_name { 'acp' } | ||||||||||||||||||||
|
Subroutine
protocol_name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 38 | |||||||||||||||||||||||
| 39 | 0 | ▶ | sub protocol_routes { | ||||||||||||||||||||
|
Subroutine
protocol_routes: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 40 | return [ | ||||||||||||||||||||||
| 41 | 0 | { method => 'GET', path => '/agents', action => 'acp_agents' }, | |||||||||||||||||||||
| 42 | { method => 'POST', path => '/runs', action => 'chat' }, | ||||||||||||||||||||||
| 43 | ]; | ||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||
| 45 | |||||||||||||||||||||||
| 46 | 0 | ▶ | sub parse_chat_request { | ||||||||||||||||||||
|
Subroutine
parse_chat_request: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||
| 47 | 0 | my ($self, $http_req, $body_ref) = @_; | |||||||||||||||||||||
| 48 | 0 | ▶ | my $data = $self->_json->decode( $$body_ref || '{}' ); | ||||||||||||||||||||
|
Condition0%0/2
$$body_ref || '{}'
MC/DC0%0/1
$$body_ref || '{}'
$$body_ref
Truth table0%0/2
A
A= $$body_ref
| |||||||||||||||||||||||
| 49 | 0 | my @msgs; | |||||||||||||||||||||
| 50 | 0 | ▶ | for my $turn ( @{ $data->{input} || [] } ) { | ||||||||||||||||||||
|
Condition0%0/2
$data->{'input'} || []
MC/DC0%0/1
$data->{'input'} || []
$data->{'input'}
Truth table0%0/2
A
A= $data->{'input'}
| |||||||||||||||||||||||
| 51 | 0 | ▶ | my $text = join '', map { $_->{content} // '' } @{ $turn->{parts} || [] }; | ||||||||||||||||||||
|
Condition0%0/4
$_->{"content"} // ""
$turn->{'parts'} || []
MC/DC0%0/1
$_->{"content"} // ""
$_->{"content"}
MC/DC0%0/1
$turn->{'parts'} || []
$turn->{'parts'}
Truth table0%0/2
A
A= $_->{"content"}
Truth table0%0/2
A
A= $turn->{'parts'}
| |||||||||||||||||||||||
| 52 | 0 | push @msgs, { role => 'user', content => $text }; | |||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||
| 54 | 0 | ▶ | my $mode = $data->{mode} // 'sync'; | ||||||||||||||||||||
|
Condition0%0/2
$data->{"mode"} // "sync"
MC/DC0%0/1
$data->{"mode"} // "sync"
$data->{"mode"}
Truth table0%0/2
A
A= $data->{"mode"}
| |||||||||||||||||||||||
| 55 | return Langertha::Knarr::Request->new( | ||||||||||||||||||||||
| 56 | protocol => 'acp', | ||||||||||||||||||||||
| 57 | raw => $data, | ||||||||||||||||||||||
| 58 | model => $data->{agent_name}, | ||||||||||||||||||||||
| 59 | messages => \@msgs, | ||||||||||||||||||||||
| 60 | stream => $mode eq 'stream' ? 1 : 0, | ||||||||||||||||||||||
| 61 | extra => { | ||||||||||||||||||||||
| 62 | run_id => $self->_uuid->create_str, | ||||||||||||||||||||||
| 63 | mode => $mode, | ||||||||||||||||||||||
| 64 | agent_name => $data->{agent_name}, | ||||||||||||||||||||||
| 65 | }, | ||||||||||||||||||||||
| 66 | 0 | ▶ | ); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||
| 68 | |||||||||||||||||||||||
| 69 | 0 | ▶ | sub format_chat_response { | ||||||||||||||||||||
|
Subroutine
format_chat_response: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 70 | 0 | my ($self, $response, $request) = @_; | |||||||||||||||||||||
| 71 | 0 | my $content = Langertha::Knarr::Response->coerce($response)->content; | |||||||||||||||||||||
| 72 | my $payload = { | ||||||||||||||||||||||
| 73 | run_id => $request->extra->{run_id}, | ||||||||||||||||||||||
| 74 | agent_name => $request->extra->{agent_name}, | ||||||||||||||||||||||
| 75 | 0 | status => 'completed', | |||||||||||||||||||||
| 76 | output => [ | ||||||||||||||||||||||
| 77 | { parts => [ { content_type => 'text/plain', content => $content } ] }, | ||||||||||||||||||||||
| 78 | ], | ||||||||||||||||||||||
| 79 | }; | ||||||||||||||||||||||
| 80 | 0 | return ( 200, { 'Content-Type' => 'application/json' }, $self->_json->encode($payload) ); | |||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||
| 82 | |||||||||||||||||||||||
| 83 | 0 | ▶ | sub format_models_response { | ||||||||||||||||||||
|
Subroutine
format_models_response: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||
| 84 | 0 | my ($self, $models) = @_; | |||||||||||||||||||||
| 85 | my @agents = map { | ||||||||||||||||||||||
| 86 | 0 | ▶ | my $id = ref $_ eq 'HASH' ? $_->{id} : "$_"; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 87 | 0 | { name => $id, description => '', metadata => {} } | |||||||||||||||||||||
| 88 | } @$models; | ||||||||||||||||||||||
| 89 | 0 | return ( 200, { 'Content-Type' => 'application/json' }, | |||||||||||||||||||||
| 90 | $self->_json->encode({ agents => \@agents }) ); | ||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||
| 92 | |||||||||||||||||||||||
| 93 | 0 | ▶ | sub _sse_event { | ||||||||||||||||||||
|
Subroutine
_sse_event: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 94 | 0 | my ($self, $event, $data) = @_; | |||||||||||||||||||||
| 95 | 0 | return "event: $event\ndata: " . $self->_json->encode($data) . "\n\n"; | |||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||
| 97 | |||||||||||||||||||||||
| 98 | 0 | ▶ | sub format_stream_open { | ||||||||||||||||||||
|
Subroutine
format_stream_open: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 99 | 0 | my ($self, $request) = @_; | |||||||||||||||||||||
| 100 | return join( '', | ||||||||||||||||||||||
| 101 | $self->_sse_event( 'run.created' => { run_id => $request->extra->{run_id}, status => 'in_progress' } ), | ||||||||||||||||||||||
| 102 | 0 | $self->_sse_event( 'message.created' => { run_id => $request->extra->{run_id} } ), | |||||||||||||||||||||
| 103 | ); | ||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||
| 105 | |||||||||||||||||||||||
| 106 | 0 | ▶ | sub format_stream_chunk { | ||||||||||||||||||||
|
Subroutine
format_stream_chunk: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 107 | 0 | my ($self, $delta_text, $request) = @_; | |||||||||||||||||||||
| 108 | return $self->_sse_event( 'message.part' => { | ||||||||||||||||||||||
| 109 | run_id => $request->extra->{run_id}, | ||||||||||||||||||||||
| 110 | 0 | part => { content_type => 'text/plain', content => $delta_text }, | |||||||||||||||||||||
| 111 | }); | ||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||
| 113 | |||||||||||||||||||||||
| 114 | 0 | ▶ | sub format_stream_close { | ||||||||||||||||||||
|
Subroutine
format_stream_close: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 115 | 0 | my ($self, $request) = @_; | |||||||||||||||||||||
| 116 | return join( '', | ||||||||||||||||||||||
| 117 | $self->_sse_event( 'message.completed' => { run_id => $request->extra->{run_id} } ), | ||||||||||||||||||||||
| 118 | 0 | $self->_sse_event( 'run.completed' => { run_id => $request->extra->{run_id}, status => 'completed' } ), | |||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||
| 121 | |||||||||||||||||||||||
| 122 | 0 | ▶ | sub format_stream_done { '' } | ||||||||||||||||||||
|
Subroutine
format_stream_done: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 123 | |||||||||||||||||||||||
| 124 | |||||||||||||||||||||||
| 125 | |||||||||||||||||||||||
| 126 | 0 | ▶ | sub format_error_response { | ||||||||||||||||||||
|
Subroutine
format_error_response: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 127 | 0 | my ($self, $status, $message) = @_; | |||||||||||||||||||||
| 128 | 0 | return ( $status, { 'Content-Type' => 'application/json' }, | |||||||||||||||||||||
| 129 | $self->_json->encode({ code => $self->_status_error_code($status), message => "$message" }) ); | ||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||
| 131 | |||||||||||||||||||||||
| 132 | 0 | ▶ | sub format_stream_error { | ||||||||||||||||||||
|
Subroutine
format_stream_error: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||
| 133 | 0 | my ($self, $status, $message, $request) = @_; | |||||||||||||||||||||
| 134 | return $self->_sse_event( 'run.failed' => { | ||||||||||||||||||||||
| 135 | run_id => $request ? $request->extra->{run_id} : undef, | ||||||||||||||||||||||
| 136 | 0 | ▶ | status => 'failed', | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 137 | error => { | ||||||||||||||||||||||
| 138 | code => $self->_status_error_code($status), | ||||||||||||||||||||||
| 139 | message => "$message", | ||||||||||||||||||||||
| 140 | }, | ||||||||||||||||||||||
| 141 | }); | ||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||
| 143 | |||||||||||||||||||||||
| 144 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||
| 145 | 1 | 1; | |||||||||||||||||||||
| 146 | |||||||||||||||||||||||
| 147 | __END__ | ||||||||||||||||||||||