| 1 | package Langertha::Knarr::Protocol::ACP; | ||||||||||||||||||||
| 2 | # ABSTRACT: BeeAI/IBM Agent Communication Protocol (ACP) for Knarr | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 10 | our $VERSION = '1.100'; | |||||||||||||||||||
| 5 | 10 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 10 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 10 | ▶ | use Data::UUID; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 10 | ▶ | use Time::HiRes qw( time ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 10 | ▶ | use Langertha::Knarr::Request; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 10 | ▶ | use Langertha::Knarr::Response; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 10 | 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 | 15 | ▶ | has _json => ( is => 'ro', default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) } ); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | 15 | ▶ | 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 | sub protocol_routes { | ||||||||||||||||||||
| 40 | return [ | ||||||||||||||||||||
| 41 | 14 | ▶ | { method => 'GET', path => '/agents', action => 'acp_agents' }, | ||||||||||||||||||
|
Subroutine
protocol_routes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 42 | { method => 'POST', path => '/runs', action => 'chat' }, | ||||||||||||||||||||
| 43 | ]; | ||||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | sub parse_chat_request { | ||||||||||||||||||||
| 47 | 3 | ▶ | my ($self, $http_req, $body_ref) = @_; | ||||||||||||||||||
|
Subroutine
parse_chat_request: called CC=8 SCAR=21.6
Pod
undocumented
| |||||||||||||||||||||
| 48 | 3 | ▶ | 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 | 3 | my @msgs; | |||||||||||||||||||
| 50 | 3 | ▶ | for my $turn ( @{ $data->{input} || [] } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 3 | ▶ | my $text = join '', map { $_->{content} // '' } @{ $turn->{parts} || [] }; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$_->{"content"} // ""
MC/DC0%0/1
$_->{"content"} // ""
$_->{"content"}
Truth table50%1/2
A
A= $_->{"content"}
| |||||||||||||||||||||
| 52 | 3 | push @msgs, { role => 'user', content => $text }; | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | 3 | ▶ | my $mode = $data->{mode} // 'sync'; | ||||||||||||||||||
|
Condition50%1/2
$data->{"mode"} // "sync"
MC/DC0%0/1
$data->{"mode"} // "sync"
$data->{"mode"}
Truth table50%1/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 | 3 | ▶ | ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | sub format_chat_response { | ||||||||||||||||||||
| 70 | 2 | ▶ | my ($self, $response, $request) = @_; | ||||||||||||||||||
|
Subroutine
format_chat_response: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 71 | 2 | 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 | 2 | status => 'completed', | |||||||||||||||||||
| 76 | output => [ | ||||||||||||||||||||
| 77 | { parts => [ { content_type => 'text/plain', content => $content } ] }, | ||||||||||||||||||||
| 78 | ], | ||||||||||||||||||||
| 79 | }; | ||||||||||||||||||||
| 80 | 2 | return ( 200, { 'Content-Type' => 'application/json' }, $self->_json->encode($payload) ); | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | sub format_models_response { | ||||||||||||||||||||
| 84 | 0 | ▶ | my ($self, $models) = @_; | ||||||||||||||||||
|
Subroutine
format_models_response: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 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 | sub _sse_event { | ||||||||||||||||||||
| 94 | 13 | ▶ | my ($self, $event, $data) = @_; | ||||||||||||||||||
|
Subroutine
_sse_event: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 95 | 13 | return "event: $event\ndata: " . $self->_json->encode($data) . "\n\n"; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | sub format_stream_open { | ||||||||||||||||||||
| 99 | 2 | ▶ | my ($self, $request) = @_; | ||||||||||||||||||
|
Subroutine
format_stream_open: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 100 | return join( '', | ||||||||||||||||||||
| 101 | $self->_sse_event( 'run.created' => { run_id => $request->extra->{run_id}, status => 'in_progress' } ), | ||||||||||||||||||||
| 102 | 2 | $self->_sse_event( 'message.created' => { run_id => $request->extra->{run_id} } ), | |||||||||||||||||||
| 103 | ); | ||||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | sub format_stream_chunk { | ||||||||||||||||||||
| 107 | 5 | ▶ | my ($self, $delta_text, $request) = @_; | ||||||||||||||||||
|
Subroutine
format_stream_chunk: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 108 | return $self->_sse_event( 'message.part' => { | ||||||||||||||||||||
| 109 | run_id => $request->extra->{run_id}, | ||||||||||||||||||||
| 110 | 5 | part => { content_type => 'text/plain', content => $delta_text }, | |||||||||||||||||||
| 111 | }); | ||||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | sub format_stream_close { | ||||||||||||||||||||
| 115 | 2 | ▶ | my ($self, $request) = @_; | ||||||||||||||||||
|
Subroutine
format_stream_close: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 116 | return join( '', | ||||||||||||||||||||
| 117 | $self->_sse_event( 'message.completed' => { run_id => $request->extra->{run_id} } ), | ||||||||||||||||||||
| 118 | 2 | $self->_sse_event( 'run.completed' => { run_id => $request->extra->{run_id}, status => 'completed' } ), | |||||||||||||||||||
| 119 | ); | ||||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 2 | ▶ | sub format_stream_done { '' } | ||||||||||||||||||
|
Subroutine
format_stream_done: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 10 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 125 | 10 | 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.100 | ||||||||||||||||||||
| 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 | ||||||||||||||||||||