| 1 | package Langertha::Knarr::Protocol::AGUI; | ||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: AG-UI (Agent-UI) event protocol 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 | # --- AG-UI overview --- | ||||||||||||||||||||||||||||||||||||||
| 15 | # CopilotKit-driven event protocol meant for agent <-> UI streaming. | ||||||||||||||||||||||||||||||||||||||
| 16 | # Transport-agnostic; SSE is the canonical wire form. | ||||||||||||||||||||||||||||||||||||||
| 17 | # Endpoint convention: POST /awp (Agent Wire Protocol) — body is the run input, | ||||||||||||||||||||||||||||||||||||||
| 18 | # response is an SSE stream of typed events. | ||||||||||||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||||||||||||
| 20 | # Event types (subset, all framed as SSE "data: { ... }" with "type" field): | ||||||||||||||||||||||||||||||||||||||
| 21 | # RUN_STARTED { type, threadId, runId } | ||||||||||||||||||||||||||||||||||||||
| 22 | # TEXT_MESSAGE_START { type, messageId, role:"assistant" } | ||||||||||||||||||||||||||||||||||||||
| 23 | # TEXT_MESSAGE_CONTENT { type, messageId, delta:"..." } | ||||||||||||||||||||||||||||||||||||||
| 24 | # TEXT_MESSAGE_END { type, messageId } | ||||||||||||||||||||||||||||||||||||||
| 25 | # TOOL_CALL_START { type, toolCallId, toolCallName, parentMessageId } | ||||||||||||||||||||||||||||||||||||||
| 26 | # TOOL_CALL_ARGS { type, toolCallId, delta:"..." } | ||||||||||||||||||||||||||||||||||||||
| 27 | # TOOL_CALL_END { type, toolCallId } | ||||||||||||||||||||||||||||||||||||||
| 28 | # STATE_SNAPSHOT / STATE_DELTA / MESSAGES_SNAPSHOT | ||||||||||||||||||||||||||||||||||||||
| 29 | # RUN_FINISHED { type, threadId, runId } | ||||||||||||||||||||||||||||||||||||||
| 30 | # RUN_ERROR { type, message, code } | ||||||||||||||||||||||||||||||||||||||
| 31 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||
| 33 | 95 | ▶ | has _json => ( is => 'ro', default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) } ); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 34 | 95 | ▶ | has _uuid => ( is => 'ro', default => sub { Data::UUID->new } ); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||
| 36 | 1 | ▶ | sub protocol_name { 'agui' } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
protocol_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||
| 38 | 91 | ▶ | sub protocol_routes { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
protocol_routes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 39 | return [ | ||||||||||||||||||||||||||||||||||||||
| 40 | 91 | { method => 'POST', path => '/awp', action => 'chat' }, | |||||||||||||||||||||||||||||||||||||
| 41 | ]; | ||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||
| 44 | 27 | ▶ | sub parse_chat_request { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_chat_request: called CC=8 SCAR=24.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 45 | 27 | my ($self, $http_req, $body_ref) = @_; | |||||||||||||||||||||||||||||||||||||
| 46 | 27 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||
| 47 | 27 | my @msgs; | |||||||||||||||||||||||||||||||||||||
| 48 | 27 | ▶ | for my $m ( @{ $data->{messages} || [] } ) { | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$data->{'messages'} || []
MC/DC0%0/1
$data->{'messages'} || []
$data->{'messages'}
Truth table50%1/2
A
A= $data->{'messages'}
| |||||||||||||||||||||||||||||||||||||||
| 49 | 27 | ▶ | push @msgs, { role => $m->{role} // 'user', content => $m->{content} // '' }; | ||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$m->{"role"} // "user"
$m->{"content"} // ""
MC/DC0%0/1
$m->{"role"} // "user"
$m->{"role"}
MC/DC0%0/1
$m->{"content"} // ""
$m->{"content"}
Truth table50%1/2
A
A= $m->{"role"}
Truth table50%1/2
A
A= $m->{"content"}
| |||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||
| 51 | return Langertha::Knarr::Request->new( | ||||||||||||||||||||||||||||||||||||||
| 52 | protocol => 'agui', | ||||||||||||||||||||||||||||||||||||||
| 53 | raw => $data, | ||||||||||||||||||||||||||||||||||||||
| 54 | model => $data->{model}, | ||||||||||||||||||||||||||||||||||||||
| 55 | messages => \@msgs, | ||||||||||||||||||||||||||||||||||||||
| 56 | stream => 1, # AG-UI is always streaming | ||||||||||||||||||||||||||||||||||||||
| 57 | session_id => $data->{threadId}, | ||||||||||||||||||||||||||||||||||||||
| 58 | extra => { | ||||||||||||||||||||||||||||||||||||||
| 59 | thread_id => $data->{threadId} // $self->_uuid->create_str, | ||||||||||||||||||||||||||||||||||||||
| 60 | 27 | ▶ | run_id => $data->{runId} // $self->_uuid->create_str, | ||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
$data->{"threadId"} // $self->_uuid->create_str
$data->{"runId"} // $self->_uuid->create_str
MC/DC0%0/2
$data->{"threadId"} // $self->_uuid->create_str
$data->{"threadId"}
$self->_uuid->create_str
MC/DC0%0/2
$data->{"runId"} // $self->_uuid->create_str
$data->{"runId"}
$self->_uuid->create_str
Truth table33%1/3
A // B
A= $data->{"threadId"}B= $self->_uuid->create_str
Truth table33%1/3
A // B
A= $data->{"runId"}B= $self->_uuid->create_str
| |||||||||||||||||||||||||||||||||||||||
| 61 | message_id => $self->_uuid->create_str, | ||||||||||||||||||||||||||||||||||||||
| 62 | }, | ||||||||||||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||
| 66 | 1 | ▶ | sub format_chat_response { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
format_chat_response: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 67 | 1 | my ($self, $response, $request) = @_; | |||||||||||||||||||||||||||||||||||||
| 68 | # AG-UI doesn't really have a "non-stream" mode — emit a synthetic single | ||||||||||||||||||||||||||||||||||||||
| 69 | # SSE-encoded sequence as the body for sync clients. | ||||||||||||||||||||||||||||||||||||||
| 70 | 1 | my $content = Langertha::Knarr::Response->coerce($response)->content; | |||||||||||||||||||||||||||||||||||||
| 71 | 1 | my $body = join( '', | |||||||||||||||||||||||||||||||||||||
| 72 | $self->format_stream_open($request), | ||||||||||||||||||||||||||||||||||||||
| 73 | $self->format_stream_chunk($content, $request), | ||||||||||||||||||||||||||||||||||||||
| 74 | $self->format_stream_close($request), | ||||||||||||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||||||||||||
| 76 | 1 | return ( 200, { 'Content-Type' => 'text/event-stream' }, $body ); | |||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||
| 79 | 128 | ▶ | sub _sse { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_sse: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 80 | 128 | my ($self, $data) = @_; | |||||||||||||||||||||||||||||||||||||
| 81 | 128 | return "data: " . $self->_json->encode($data) . "\n\n"; | |||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||
| 84 | 25 | ▶ | sub format_stream_open { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
format_stream_open: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 85 | 25 | my ($self, $request) = @_; | |||||||||||||||||||||||||||||||||||||
| 86 | return join( '', | ||||||||||||||||||||||||||||||||||||||
| 87 | $self->_sse({ | ||||||||||||||||||||||||||||||||||||||
| 88 | type => 'RUN_STARTED', | ||||||||||||||||||||||||||||||||||||||
| 89 | threadId => $request->extra->{thread_id}, | ||||||||||||||||||||||||||||||||||||||
| 90 | runId => $request->extra->{run_id}, | ||||||||||||||||||||||||||||||||||||||
| 91 | }), | ||||||||||||||||||||||||||||||||||||||
| 92 | $self->_sse({ | ||||||||||||||||||||||||||||||||||||||
| 93 | type => 'TEXT_MESSAGE_START', | ||||||||||||||||||||||||||||||||||||||
| 94 | messageId => $request->extra->{message_id}, | ||||||||||||||||||||||||||||||||||||||
| 95 | 25 | role => 'assistant', | |||||||||||||||||||||||||||||||||||||
| 96 | }), | ||||||||||||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||
| 100 | 28 | ▶ | sub format_stream_chunk { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
format_stream_chunk: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 101 | 28 | my ($self, $delta_text, $request) = @_; | |||||||||||||||||||||||||||||||||||||
| 102 | return $self->_sse({ | ||||||||||||||||||||||||||||||||||||||
| 103 | type => 'TEXT_MESSAGE_CONTENT', | ||||||||||||||||||||||||||||||||||||||
| 104 | messageId => $request->extra->{message_id}, | ||||||||||||||||||||||||||||||||||||||
| 105 | 28 | delta => $delta_text, | |||||||||||||||||||||||||||||||||||||
| 106 | }); | ||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||
| 109 | 25 | ▶ | sub format_stream_close { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
format_stream_close: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 110 | 25 | my ($self, $request) = @_; | |||||||||||||||||||||||||||||||||||||
| 111 | return join( '', | ||||||||||||||||||||||||||||||||||||||
| 112 | $self->_sse({ | ||||||||||||||||||||||||||||||||||||||
| 113 | type => 'TEXT_MESSAGE_END', | ||||||||||||||||||||||||||||||||||||||
| 114 | messageId => $request->extra->{message_id}, | ||||||||||||||||||||||||||||||||||||||
| 115 | }), | ||||||||||||||||||||||||||||||||||||||
| 116 | $self->_sse({ | ||||||||||||||||||||||||||||||||||||||
| 117 | type => 'RUN_FINISHED', | ||||||||||||||||||||||||||||||||||||||
| 118 | threadId => $request->extra->{thread_id}, | ||||||||||||||||||||||||||||||||||||||
| 119 | runId => $request->extra->{run_id}, | ||||||||||||||||||||||||||||||||||||||
| 120 | 25 | }), | |||||||||||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||
| 124 | 24 | ▶ | sub format_stream_done { '' } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
format_stream_done: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||
| 126 | 42 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||
| 127 | 42 | 1; | |||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||
| 129 | __END__ | ||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||
| 131 | =pod | ||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||
| 133 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||
| 135 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||
| 137 | Langertha::Knarr::Protocol::AGUI - AG-UI (Agent-UI) event protocol for Knarr | ||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||
| 139 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||
| 141 | version 1.102 | ||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||
| 143 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||
| 145 | Implements the CopilotKit AG-UI agent-to-UI event protocol on top of | ||||||||||||||||||||||||||||||||||||||
| 146 | L<Langertha::Knarr::Protocol>. Loaded by default. | ||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||
| 148 | =over | ||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||
| 150 | =item * C<POST /awp> — Agent Wire Protocol endpoint, always streaming | ||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||
| 152 | =back | ||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||
| 154 | Streaming emits the typed AG-UI event sequence: | ||||||||||||||||||||||||||||||||||||||
| 155 | C<RUN_STARTED>, C<TEXT_MESSAGE_START>, C<TEXT_MESSAGE_CONTENT>×N, | ||||||||||||||||||||||||||||||||||||||
| 156 | C<TEXT_MESSAGE_END>, C<RUN_FINISHED>. Sync mode synthesizes the same | ||||||||||||||||||||||||||||||||||||||
| 157 | sequence into a single response body for non-streaming clients. | ||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||
| 159 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||
| 161 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||
| 163 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||
| 164 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||
| 166 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||
| 168 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||
| 170 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||
| 172 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||
| 174 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||
| 176 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||
| 178 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||
| 180 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||
| 182 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||
| 183 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||
| 185 | =cut | ||||||||||||||||||||||||||||||||||||||