Knarr/Protocol/AGUI.pm

filter: Statementstmt 100% 48 / 48 Branchbran 50.0% 1 / 2 Conditioncond 41.6% 5 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 8 totaltotal 75.5% 71 / 94 CC 8 SCAR 15.1
CC
8
Cov
87%
parse_chat_request
11.5
BEGIN
1.0
BEGIN
1.0
CRAP 4.5
« Knarr/Protocol/Ollama.pm ↑ Summary Knarr.pm »
1 package Langertha::Knarr::Protocol::AGUI;
2 # ABSTRACT: AG-UI (Agent-UI) event protocol 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 # --- 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 15 ▶has _json => ( is => 'ro', default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
34 15 ▶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 sub protocol_routes {
39 return [
40 14 ▶ { method => 'POST', path => '/awp', action => 'chat' },
Subroutine protocol_routes: called CC=1 SCAR=0.0
Pod undocumented
41 ];
42 }
43  
44 sub parse_chat_request {
45 2 ▶ my ($self, $http_req, $body_ref) = @_;
Subroutine parse_chat_request: called CC=8 SCAR=24.5
Pod undocumented
46 2 ▶ my $data = $self->_json->decode( $$body_ref || '{}' );
Condition50%1/2
$$body_ref || '{}'
l!l
20
MC/DC0%0/1
$$body_ref || '{}'
$$body_ref
Truth table50%1/2
A
A= $$body_ref
Aresult
11
00
47 2 my @msgs;
48 2 ▶ for my $m ( @{ $data->{messages} || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$data->{"messages"} or [] 0 2 2
49 2 ▶ push @msgs, { role => $m->{role} // 'user', content => $m->{content} // '' };
Condition50%2/4
$m->{"role"} // "user"
l!l
20
$m->{"content"} // ""
l!l
20
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"}
Aresult
11
00
Truth table50%1/2
A
A= $m->{"content"}
Aresult
11
00
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 2 ▶ run_id => $data->{runId} // $self->_uuid->create_str,
Condition33%2/6
$data->{"threadId"} // $self->_uuid->create_str
l!l&&r!l&&!r
200
$data->{"runId"} // $self->_uuid->create_str
l!l&&r!l&&!r
200
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
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $data->{"runId"}B= $self->_uuid->create_str
ABresult
1X1
011
000
61 message_id => $self->_uuid->create_str,
62 },
63 );
64 }
65  
66 sub format_chat_response {
67 1 ▶ my ($self, $response, $request) = @_;
Subroutine format_chat_response: called CC=1 SCAR=0.0
Pod undocumented
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 sub _sse {
80 18 ▶ my ($self, $data) = @_;
Subroutine _sse: called CC=1 SCAR=0.0
81 18 return "data: " . $self->_json->encode($data) . "\n\n";
82 }
83  
84 sub format_stream_open {
85 3 ▶ my ($self, $request) = @_;
Subroutine format_stream_open: called CC=1 SCAR=0.0
Pod undocumented
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 3 role => 'assistant',
96 }),
97 );
98 }
99  
100 sub format_stream_chunk {
101 6 ▶ my ($self, $delta_text, $request) = @_;
Subroutine format_stream_chunk: called CC=1 SCAR=0.0
Pod undocumented
102 return $self->_sse({
103 type => 'TEXT_MESSAGE_CONTENT',
104 messageId => $request->extra->{message_id},
105 6 delta => $delta_text,
106 });
107 }
108  
109 sub format_stream_close {
110 3 ▶ my ($self, $request) = @_;
Subroutine format_stream_close: called CC=1 SCAR=0.0
Pod undocumented
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 3 }),
121 );
122 }
123  
124 2 ▶sub format_stream_done { '' }
Subroutine format_stream_done: called CC=1 SCAR=0.0
Pod undocumented
125  
126 10 __PACKAGE__->meta->make_immutable;
127 10 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.100
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
« Knarr/Protocol/Ollama.pm ↑ Summary Knarr.pm »