Knarr/PSGI.pm

filter: Statementstmt 94.3% 67 / 71 Branchbran 70.0% 14 / 20 Conditioncond 57.1% 4 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 85.8% 97 / 113 CC 14 SCAR 17.3
CC
14
Cov
87%
_handle_psgi
9.4
_read_body
6.2
BEGIN
1.0
CRAP 5.7
« Knarr/Handler/Engine.pm ↑ Summary Knarr/Response.pm »
1 package Langertha::Knarr::PSGI;
2 # ABSTRACT: PSGI adapter for Langertha::Knarr (buffered, no streaming)
3 2 our $VERSION = '1.100';
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Langertha::Knarr::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 # Wraps a Langertha::Knarr instance and returns a PSGI app coderef.
10 # Streaming requests are coerced into buffered responses: the full body is
11 # assembled (open + chunks + close + done) before being returned to the
12 # PSGI server. Use the native Net::Async::HTTP::Server entrypoint
13 # (Langertha::Knarr->run) if you need real streaming.
14  
15 2 has knarr => ( is => 'ro', required => 1 );
16  
17 has _json => (
18 is => 'ro',
19 1 ▶ default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 2 );
21  
22 sub to_app {
23 1 ▶ my ($self) = @_;
Subroutine to_app: called CC=1 SCAR=0.0
Pod documented
24 return sub {
25 4 ▶ my ($env) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
26 4 return $self->_handle_psgi($env);
27 1 };
28 }
29  
30 sub _read_body {
31 3 ▶ my ($self, $env) = @_;
Subroutine _read_body: called CC=6 SCAR=18.2
32 3 ▶ my $input = $env->{'psgi.input'} or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $input = $env->{"psgi.input"} or return "" 0 3 3
33 3 ▶ my $len = $env->{CONTENT_LENGTH} // 0;
Condition50%1/2
$env->{"CONTENT_LENGTH"} // 0
l!l
30
MC/DC0%0/1
$env->{"CONTENT_LENGTH"} // 0
$env->{"CONTENT_LENGTH"}
Truth table50%1/2
A
A= $env->{"CONTENT_LENGTH"}
Aresult
11
00
34 3 ▶ return '' unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 3 3
35 3 my $body = '';
36 3 my $read = 0;
37 3 ▶ while ( $read < $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read < $len) 3 3 6
38 3 my $chunk;
39 3 my $n = $input->read( $chunk, $len - $read );
40 3 ▶ last unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 3 3
41 3 $body .= $chunk;
42 3 $read += $n;
43 }
44 3 return $body;
45 }
46  
47 sub _handle_psgi {
48 4 ▶ my ($self, $env) = @_;
Subroutine _handle_psgi: called CC=9 SCAR=22.5
49 4 my $sb = $self->knarr;
50 4 my $method = $env->{REQUEST_METHOD};
51 4 ▶ my $path = $env->{PATH_INFO} // '/';
Condition50%1/2
$env->{"PATH_INFO"} // "/"
l!l
40
MC/DC0%0/1
$env->{"PATH_INFO"} // "/"
$env->{"PATH_INFO"}
Truth table50%1/2
A
A= $env->{"PATH_INFO"}
Aresult
11
00
52  
53 4 my $route = $sb->_match_route( $method, $path );
54 4 ▶ unless ( $route ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($route) 0 4 4
55 0 return [ 404, [ 'Content-Type' => 'application/json' ],
56 [ $self->_json->encode({ error => { message => "no route for $method $path" } }) ] ];
57 }
58  
59 4 my $proto = $route->{protocol};
60 4 my $action = $route->{action};
61  
62 4 ▶ if ( $action eq 'models' || $action eq 'acp_agents' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "models" or $action eq "acp_agents") 1 3 4
Condition66%2/3
$action eq "models" or $action eq "acp_agents"
l!l&&r!l&&!r
103
MC/DC50%1/2
$action eq "models" or $action eq "acp_agents"
$action eq "models" $action eq "acp_agents"
Truth table66%2/3
A or B
A= $action eq "models"B= $action eq "acp_agents"
ABresult
1X1
011
000
63 1 my $models = $sb->handler->list_models;
64 1 my ($status, $headers, $body) = $proto->format_models_response($models);
65 1 return [ $status, [ %$headers ], [ $body ] ];
66 }
67 3 ▶ if ( $action eq 'a2a_card' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "a2a_card") 0 3 3
68 0 my ($status, $headers, $body) = $proto->format_agent_card;
69 0 return [ $status, [ %$headers ], [ $body ] ];
70 }
71 3 ▶ if ( $action ne 'chat' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action ne "chat") 0 3 3
72 0 return [ 500, [ 'Content-Type' => 'application/json' ],
73 [ $self->_json->encode({ error => { message => "unknown action $action" } }) ] ];
74 }
75  
76 3 my $body = $self->_read_body($env);
77 3 my $fake_http = Langertha::Knarr::PSGI::FakeReq->new( $env );
78 3 my $sb_req = $proto->parse_chat_request( $fake_http, \$body );
79 3 my $session = $sb->session( $sb_req->session_id );
80 3 my $handler = $sb->handler;
81  
82 3 ▶ if ( $sb_req->stream ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sb_req->stream) 2 1 3
83 # Buffered streaming: drive the stream to completion, concatenate frames.
84 2 my $stream = $handler->handle_stream_f( $session, $sb_req )->get;
85 2 my $out = $proto->format_stream_open($sb_req);
86 2 ▶ while ( defined( my $delta = $stream->next_chunk_f->get ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $delta = $stream->next_chunk_f->get)) 6 2 8
87 6 $out .= $proto->format_stream_chunk( $delta, $sb_req );
88 }
89 2 $out .= $proto->format_stream_close($sb_req);
90 2 $out .= $proto->format_stream_done($sb_req);
91 2 return [ 200, [ 'Content-Type' => $proto->stream_content_type ], [ $out ] ];
92 }
93  
94 1 my $response = $handler->handle_chat_f( $session, $sb_req )->get;
95 1 my ($status, $headers, $obody) = $proto->format_chat_response( $response, $sb_req );
96 1 return [ $status, [ %$headers ], [ $obody ] ];
97 }
98  
99 package Langertha::Knarr::PSGI::FakeReq;
100 sub new {
101 3 ▶ my ($class, $env) = @_;
Subroutine new: called CC=1 SCAR=0.0
102 3 return bless { env => $env }, $class;
103 }
104 sub header {
105 4 ▶ my ($self, $name) = @_;
Subroutine header: called CC=1 SCAR=0.0
106 4 ( my $key = uc $name ) =~ tr/-/_/;
107 4 return $self->{env}{"HTTP_$key"};
108 }
109  
110 package Langertha::Knarr::PSGI;
111 2 __PACKAGE__->meta->make_immutable;
112 2 1;
113  
114 __END__
115  
116 =pod
117  
118 =encoding UTF-8
119  
120 =head1 NAME
121  
122 Langertha::Knarr::PSGI - PSGI adapter for Langertha::Knarr (buffered, no streaming)
123  
124 =head1 VERSION
125  
126 version 1.100
127  
128 =head1 SYNOPSIS
129  
130 use Langertha::Knarr;
131 use Langertha::Knarr::PSGI;
132  
133 my $knarr = Langertha::Knarr->new( handler => $handler );
134 my $app = Langertha::Knarr::PSGI->new( knarr => $knarr )->to_app;
135 # $app is now a Plack-compatible coderef
136  
137 # Run with any PSGI server:
138 # plackup -s Starman -p 8088 app.psgi
139  
140 =head1 DESCRIPTION
141  
142 Adapter that wraps a L<Langertha::Knarr> instance and exposes it as a
143 PSGI app, so you can deploy Knarr behind any Plack server (Starman,
144 Twiggy, Gazelle, mod_perl, etc.) instead of running its native
145 L<Net::Async::HTTP::Server> loop.
146  
147 B<Streaming responses are buffered.> The PSGI streaming protocol's
148 delayed-response form does work in theory but mixes badly with
149 L<IO::Async> in the same process; for honesty's sake this adapter
150 just drives the inner stream to completion in a blocking loop and
151 returns the full assembled body. Use the native
152 L<Langertha::Knarr/run> entry point if you need real-time streaming.
153  
154 =head2 knarr
155  
156 Required. The L<Langertha::Knarr> instance to expose.
157  
158 =head2 to_app
159  
160 Returns the PSGI coderef.
161  
162 =head1 SUPPORT
163  
164 =head2 Issues
165  
166 Please report bugs and feature requests on GitHub at
167 L<https://github.com/Getty/langertha-knarr/issues>.
168  
169 =head2 IRC
170  
171 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
172  
173 =head1 CONTRIBUTING
174  
175 Contributions are welcome! Please fork the repository and submit a pull request.
176  
177 =head1 AUTHOR
178  
179 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
180  
181 =head1 COPYRIGHT AND LICENSE
182  
183 This software is copyright (c) 2026 by Torsten Raudssus.
184  
185 This is free software; you can redistribute it and/or modify it under
186 the same terms as the Perl 5 programming language system itself.
187  
188 =cut
« Knarr/Handler/Engine.pm ↑ Summary Knarr/Response.pm »