| 1 | package Langertha::Knarr::Protocol::ACP; | ||||||||||||||||||||||
| 2 | # ABSTRACT: BeeAI/IBM Agent Communication Protocol (ACP) for Knarr | ||||||||||||||||||||||
| 3 | |||||||||||||||||||||||
| 4 | 42 | our $VERSION = '1.102'; | |||||||||||||||||||||
| 5 | 42 | ▶ | use Moose; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 42 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 42 | ▶ | use Data::UUID; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 42 | ▶ | use Time::HiRes qw( time ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 42 | ▶ | use Langertha::Knarr::Request; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 42 | ▶ | use Langertha::Knarr::Response; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | 42 | 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 | 95 | ▶ | has _json => ( is => 'ro', default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) } ); | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 35 | 95 | ▶ | has _uuid => ( is => 'ro', default => sub { Data::UUID->new } ); | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | 1 | ▶ | sub protocol_name { 'acp' } | ||||||||||||||||||||
|
Subroutine
protocol_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 38 | |||||||||||||||||||||||
| 39 | 91 | ▶ | sub protocol_routes { | ||||||||||||||||||||
|
Subroutine
protocol_routes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 40 | return [ | ||||||||||||||||||||||
| 41 | 91 | { method => 'GET', path => '/agents', action => 'acp_agents' }, | |||||||||||||||||||||
| 42 | { method => 'POST', path => '/runs', action => 'chat' }, | ||||||||||||||||||||||
| 43 | ]; | ||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||
| 45 | |||||||||||||||||||||||
| 46 | 50 | ▶ | sub parse_chat_request { | ||||||||||||||||||||
|
Subroutine
parse_chat_request: called CC=8 SCAR=21.2
Pod
undocumented
| |||||||||||||||||||||||
| 47 | 50 | my ($self, $http_req, $body_ref) = @_; | |||||||||||||||||||||
| 48 | 50 | ▶ | my $data = $self->_json->decode( $$body_ref || '{}' ); | ||||||||||||||||||||
|
Condition50%1/2
$$body_ref || '{}'
MC/DC0%0/1
$$body_ref || '{}'
$$body_ref
Truth table50%1/2
A
A= $$body_ref
| |||||||||||||||||||||||
| 49 | 50 | my @msgs; | |||||||||||||||||||||
| 50 | 50 | ▶ | for my $turn ( @{ $data->{input} || [] } ) { | ||||||||||||||||||||
|
Condition50%1/2
$data->{'input'} || []
MC/DC0%0/1
$data->{'input'} || []
$data->{'input'}
Truth table50%1/2
A
A= $data->{'input'}
| |||||||||||||||||||||||
| 51 | 50 | ▶ | my $text = join '', map { $_->{content} // '' } @{ $turn->{parts} || [] }; | ||||||||||||||||||||
|
Condition50%2/4
$_->{"content"} // ""
$turn->{'parts'} || []
MC/DC0%0/1
$_->{"content"} // ""
$_->{"content"}
MC/DC0%0/1
$turn->{'parts'} || []
$turn->{'parts'}
Truth table50%1/2
A
A= $_->{"content"}
Truth table50%1/2
A
A= $turn->{'parts'}
| |||||||||||||||||||||||
| 52 | 50 | push @msgs, { role => 'user', content => $text }; | |||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||
| 54 | 50 | ▶ | my $mode = $data->{mode} // 'sync'; | ||||||||||||||||||||
|
Condition100%2/2
$data->{"mode"} // "sync"
MC/DC100%1/1
$data->{"mode"} // "sync"
$data->{"mode"}
Truth table100%2/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 | 50 | ▶ | ); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||
| 68 | |||||||||||||||||||||||
| 69 | 24 | ▶ | sub format_chat_response { | ||||||||||||||||||||
|
Subroutine
format_chat_response: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 70 | 24 | my ($self, $response, $request) = @_; | |||||||||||||||||||||
| 71 | 24 | 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 | 24 | status => 'completed', | |||||||||||||||||||||
| 76 | output => [ | ||||||||||||||||||||||
| 77 | { parts => [ { content_type => 'text/plain', content => $content } ] }, | ||||||||||||||||||||||
| 78 | ], | ||||||||||||||||||||||
| 79 | }; | ||||||||||||||||||||||
| 80 | 24 | return ( 200, { 'Content-Type' => 'application/json' }, $self->_json->encode($payload) ); | |||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||
| 82 | |||||||||||||||||||||||
| 83 | 20 | ▶ | sub format_models_response { | ||||||||||||||||||||
|
Subroutine
format_models_response: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||
| 84 | 20 | my ($self, $models) = @_; | |||||||||||||||||||||
| 85 | my @agents = map { | ||||||||||||||||||||||
| 86 | 20 | ▶ | my $id = ref $_ eq 'HASH' ? $_->{id} : "$_"; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 87 | 20 | { name => $id, description => '', metadata => {} } | |||||||||||||||||||||
| 88 | } @$models; | ||||||||||||||||||||||
| 89 | 20 | return ( 200, { 'Content-Type' => 'application/json' }, | |||||||||||||||||||||
| 90 | $self->_json->encode({ agents => \@agents }) ); | ||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||
| 92 | |||||||||||||||||||||||
| 93 | 123 | ▶ | sub _sse_event { | ||||||||||||||||||||
|
Subroutine
_sse_event: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 94 | 123 | my ($self, $event, $data) = @_; | |||||||||||||||||||||
| 95 | 123 | return "event: $event\ndata: " . $self->_json->encode($data) . "\n\n"; | |||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||
| 97 | |||||||||||||||||||||||
| 98 | 24 | ▶ | sub format_stream_open { | ||||||||||||||||||||
|
Subroutine
format_stream_open: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 99 | 24 | my ($self, $request) = @_; | |||||||||||||||||||||
| 100 | return join( '', | ||||||||||||||||||||||
| 101 | $self->_sse_event( 'run.created' => { run_id => $request->extra->{run_id}, status => 'in_progress' } ), | ||||||||||||||||||||||
| 102 | 24 | $self->_sse_event( 'message.created' => { run_id => $request->extra->{run_id} } ), | |||||||||||||||||||||
| 103 | ); | ||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||
| 105 | |||||||||||||||||||||||
| 106 | 27 | ▶ | sub format_stream_chunk { | ||||||||||||||||||||
|
Subroutine
format_stream_chunk: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 107 | 27 | my ($self, $delta_text, $request) = @_; | |||||||||||||||||||||
| 108 | return $self->_sse_event( 'message.part' => { | ||||||||||||||||||||||
| 109 | run_id => $request->extra->{run_id}, | ||||||||||||||||||||||
| 110 | 27 | part => { content_type => 'text/plain', content => $delta_text }, | |||||||||||||||||||||
| 111 | }); | ||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||
| 113 | |||||||||||||||||||||||
| 114 | 24 | ▶ | sub format_stream_close { | ||||||||||||||||||||
|
Subroutine
format_stream_close: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 115 | 24 | my ($self, $request) = @_; | |||||||||||||||||||||
| 116 | return join( '', | ||||||||||||||||||||||
| 117 | $self->_sse_event( 'message.completed' => { run_id => $request->extra->{run_id} } ), | ||||||||||||||||||||||
| 118 | 24 | $self->_sse_event( 'run.completed' => { run_id => $request->extra->{run_id}, status => 'completed' } ), | |||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||
| 121 | |||||||||||||||||||||||
| 122 | 24 | ▶ | sub format_stream_done { '' } | ||||||||||||||||||||
|
Subroutine
format_stream_done: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 123 | |||||||||||||||||||||||
| 124 | 42 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||
| 125 | 42 | 1; | |||||||||||||||||||||
| 126 | |||||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||||
| 128 | |||||||||||||||||||||||
| 129 | =pod | ||||||||||||||||||||||
| 130 | |||||||||||||||||||||||
| 131 | =encoding UTF-8 | ||||||||||||||||||||||
| 132 | |||||||||||||||||||||||
| 133 | =head1 NAME | ||||||||||||||||||||||
| 134 | |||||||||||||||||||||||
| 135 | Langertha::Knarr::Protocol::ACP - BeeAI/IBM Agent Communication Protocol (ACP) for Knarr | ||||||||||||||||||||||
| 136 | |||||||||||||||||||||||
| 137 | =head1 VERSION | ||||||||||||||||||||||
| 138 | |||||||||||||||||||||||
| 139 | version 1.102 | ||||||||||||||||||||||
| 140 | |||||||||||||||||||||||
| 141 | =head1 DESCRIPTION | ||||||||||||||||||||||
| 142 | |||||||||||||||||||||||
| 143 | Implements the IBM/BeeAI Linux Foundation ACP protocol on top of | ||||||||||||||||||||||
| 144 | L<Langertha::Knarr::Protocol>. Loaded by default. | ||||||||||||||||||||||
| 145 | |||||||||||||||||||||||
| 146 | =over | ||||||||||||||||||||||
| 147 | |||||||||||||||||||||||
| 148 | =item * C<GET /agents> — list agents | ||||||||||||||||||||||
| 149 | |||||||||||||||||||||||
| 150 | =item * C<POST /runs> — create a run (C<sync> or C<stream> mode) | ||||||||||||||||||||||
| 151 | |||||||||||||||||||||||
| 152 | =back | ||||||||||||||||||||||
| 153 | |||||||||||||||||||||||
| 154 | Streaming mode emits the standard ACP event sequence: | ||||||||||||||||||||||
| 155 | C<run.created>, C<message.created>, C<message.part>×N, | ||||||||||||||||||||||
| 156 | C<message.completed>, C<run.completed>. | ||||||||||||||||||||||
| 157 | |||||||||||||||||||||||
| 158 | =head1 SUPPORT | ||||||||||||||||||||||
| 159 | |||||||||||||||||||||||
| 160 | =head2 Issues | ||||||||||||||||||||||
| 161 | |||||||||||||||||||||||
| 162 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||
| 163 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||||||||||||||
| 164 | |||||||||||||||||||||||
| 165 | =head2 IRC | ||||||||||||||||||||||
| 166 | |||||||||||||||||||||||
| 167 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||
| 168 | |||||||||||||||||||||||
| 169 | =head1 CONTRIBUTING | ||||||||||||||||||||||
| 170 | |||||||||||||||||||||||
| 171 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||
| 172 | |||||||||||||||||||||||
| 173 | =head1 AUTHOR | ||||||||||||||||||||||
| 174 | |||||||||||||||||||||||
| 175 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||
| 176 | |||||||||||||||||||||||
| 177 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||
| 178 | |||||||||||||||||||||||
| 179 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||
| 180 | |||||||||||||||||||||||
| 181 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||
| 182 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||
| 183 | |||||||||||||||||||||||
| 184 | =cut | ||||||||||||||||||||||