blib/lib/Langertha/Knarr/Protocol/AGUI.pm

filter: Statementstmt 44.4% 24 / 54 Branchbran n/a Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 30.0% 6 / 20 Podpod 20.0% 2 / 10 totaltotal 30.1% 32 / 106 CC 8 SCAR 31.1
CC
8
Cov
35%
parse_chat_request
72.0
protocol_name
2.0
protocol_routes
2.0
CRAP 22.5
« blib/lib/Langertha/Knarr/Handler/Engine.pm ↑ Summary blib/lib/Langertha/Knarr/Protocol/ACP.pm »
1 package Langertha::Knarr::Protocol::AGUI;
2 # ABSTRACT: AG-UI (Agent-UI) event protocol 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 # --- 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 0 ▶has _json => ( is => 'ro', default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
34 0 ▶has _uuid => ( is => 'ro', default => sub { Data::UUID->new } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
35  
36 0 ▶sub protocol_name { 'agui' }
Subroutine protocol_name: not called CC=1 SCAR=6.9
Pod undocumented
37  
38 0 ▶sub protocol_routes {
Subroutine protocol_routes: not called CC=1 SCAR=6.9
Pod undocumented
39 return [
40 0 { method => 'POST', path => '/awp', action => 'chat' },
41 ];
42 }
43  
44 0 ▶sub parse_chat_request {
Subroutine parse_chat_request: not called CC=8 SCAR=42.8
Pod undocumented
45 0 my ($self, $http_req, $body_ref) = @_;
46 0 ▶ my $data = $self->_json->decode( $$body_ref || '{}' );
Condition0%0/2
$$body_ref || '{}'
l!l
00
MC/DC0%0/1
$$body_ref || '{}'
$$body_ref
Truth table0%0/2
A
A= $$body_ref
Aresult
11
00
47 0 my @msgs;
48 0 ▶ for my $m ( @{ $data->{messages} || [] } ) {
Condition0%0/2
$data->{'messages'} || []
l!l
00
MC/DC0%0/1
$data->{'messages'} || []
$data->{'messages'}
Truth table0%0/2
A
A= $data->{'messages'}
Aresult
11
00
49 0 ▶ push @msgs, { role => $m->{role} // 'user', content => $m->{content} // '' };
Condition0%0/4
$m->{"role"} // "user"
l!l
00
$m->{"content"} // ""
l!l
00
MC/DC0%0/1
$m->{"role"} // "user"
$m->{"role"}
MC/DC0%0/1
$m->{"content"} // ""
$m->{"content"}
Truth table0%0/2
A
A= $m->{"role"}
Aresult
11
00
Truth table0%0/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 0 ▶ run_id => $data->{runId} // $self->_uuid->create_str,
Condition0%0/6
$data->{"threadId"} // $self->_uuid->create_str
l!l&&r!l&&!r
000
$data->{"runId"} // $self->_uuid->create_str
l!l&&r!l&&!r
000
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 table0%0/3
A // B
A= $data->{"threadId"}B= $self->_uuid->create_str
ABresult
1X1
011
000
Truth table0%0/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 0 ▶sub format_chat_response {
Subroutine format_chat_response: not called CC=1 SCAR=6.9
Pod undocumented
67 0 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 0 my $content = Langertha::Knarr::Response->coerce($response)->content;
71 0 my $body = join( '',
72 $self->format_stream_open($request),
73 $self->format_stream_chunk($content, $request),
74 $self->format_stream_close($request),
75 );
76 0 return ( 200, { 'Content-Type' => 'text/event-stream' }, $body );
77 }
78  
79 0 ▶sub _sse {
Subroutine _sse: not called CC=1 SCAR=6.9
80 0 my ($self, $data) = @_;
81 0 return "data: " . $self->_json->encode($data) . "\n\n";
82 }
83  
84 0 ▶sub format_stream_open {
Subroutine format_stream_open: not called CC=1 SCAR=6.9
Pod undocumented
85 0 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 0 role => 'assistant',
96 }),
97 );
98 }
99  
100 0 ▶sub format_stream_chunk {
Subroutine format_stream_chunk: not called CC=1 SCAR=6.9
Pod undocumented
101 0 my ($self, $delta_text, $request) = @_;
102 return $self->_sse({
103 type => 'TEXT_MESSAGE_CONTENT',
104 messageId => $request->extra->{message_id},
105 0 delta => $delta_text,
106 });
107 }
108  
109 0 ▶sub format_stream_close {
Subroutine format_stream_close: not called CC=1 SCAR=6.9
Pod undocumented
110 0 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 0 }),
121 );
122 }
123  
124 0 ▶sub format_stream_done { '' }
Subroutine format_stream_done: not called CC=1 SCAR=6.9
Pod undocumented
125  
126  
127 0 ▶sub _run_error {
Subroutine _run_error: not called CC=1 SCAR=6.9
128 0 my ($self, $status, $message) = @_;
129 return {
130 0 type => 'RUN_ERROR',
131 message => "$message",
132 code => $self->_status_error_code($status),
133 };
134 }
135  
136 0 ▶sub format_stream_error {
Subroutine format_stream_error: not called CC=1 SCAR=6.9
Pod documented
137 0 my ($self, $status, $message) = @_;
138 0 return $self->_sse( $self->_run_error( $status, $message ) );
139 }
140  
141  
142 0 ▶sub format_error_response {
Subroutine format_error_response: not called CC=1 SCAR=6.9
Pod documented
143 0 my ($self, $status, $message) = @_;
144 0 return ( $status, { 'Content-Type' => 'application/json' },
145 $self->_json->encode( $self->_run_error( $status, $message ) ) );
146 }
147  
148 1 __PACKAGE__->meta->make_immutable;
149 1 1;
150  
151 __END__
« blib/lib/Langertha/Knarr/Handler/Engine.pm ↑ Summary blib/lib/Langertha/Knarr/Protocol/ACP.pm »