Knarr/Handler.pm

filter: Statementstmt 80.9% 17 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 81.4% 22 / 27 CC 1 SCAR 1.8
CC
1
Cov
81%
handle_stream_f
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« Knarr/CLI/Cmd/Container.pm ↑ Summary Knarr/Session.pm »
1 package Langertha::Knarr::Handler;
2 # ABSTRACT: Role for Knarr backend handlers (Raider, Engine, Code, ...)
3 16 our $VERSION = '1.100';
4 16 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Langertha::Knarr::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Langertha::Knarr::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 16 requires 'handle_chat_f';
11 16 requires 'list_models';
12  
13 # Default streaming = run handle_chat_f and emit one chunk.
14 # Handlers that natively stream should override.
15 0 ▶async sub handle_stream_f {
Subroutine handle_stream_f: not called CC=1 SCAR=6.9
Pod documented
16 0 my ($self, $session, $request) = @_;
17 0 my $r = Langertha::Knarr::Response->coerce( await $self->handle_chat_f($session, $request) );
18 0 return Langertha::Knarr::Stream->from_list( $r->content );
19 16 }
20  
21 16 1;
22  
23 __END__
24  
25 =pod
26  
27 =encoding UTF-8
28  
29 =head1 NAME
30  
31 Langertha::Knarr::Handler - Role for Knarr backend handlers (Raider, Engine, Code, ...)
32  
33 =head1 VERSION
34  
35 version 1.100
36  
37 =head1 SYNOPSIS
38  
39 package My::Handler;
40 use Moose;
41 use Future;
42 with 'Langertha::Knarr::Handler';
43  
44 sub handle_chat_f {
45 my ($self, $session, $request) = @_;
46 return Future->done(
47 Langertha::Knarr::Response->new( content => 'hello', model => 'my-model' )
48 );
49 }
50  
51 sub list_models { [ { id => 'my-model', object => 'model' } ] }
52  
53 1;
54  
55 =head1 DESCRIPTION
56  
57 The role every Knarr backend handler must consume. A handler is the
58 "what answers the request" half of Knarr — it receives a normalized
59 L<Langertha::Knarr::Request> and returns either a sync result hash via
60 L</handle_chat_f> or an async chunk iterator via L</handle_stream_f>.
61  
62 Knarr ships with concrete handlers for the common cases:
63  
64 =over
65  
66 =item * L<Langertha::Knarr::Handler::Code> — coderef-backed, for tests/fakes
67  
68 =item * L<Langertha::Knarr::Handler::Engine> — passthrough to a Langertha engine
69  
70 =item * L<Langertha::Knarr::Handler::Raider> — per-session L<Langertha::Raider>
71  
72 =item * L<Langertha::Knarr::Handler::Router> — model-name routing via L<Langertha::Knarr::Router>
73  
74 =item * L<Langertha::Knarr::Handler::Passthrough> — raw HTTP forward to upstream
75  
76 =item * L<Langertha::Knarr::Handler::A2AClient> — consume a remote A2A agent
77  
78 =item * L<Langertha::Knarr::Handler::ACPClient> — consume a remote ACP agent
79  
80 =item * L<Langertha::Knarr::Handler::Tracing> — Langfuse tracing decorator
81  
82 =item * L<Langertha::Knarr::Handler::RequestLog> — JSONL request logging decorator
83  
84 =back
85  
86 Decorators (Tracing, RequestLog) wrap an inner handler and themselves
87 consume this role, so they compose freely.
88  
89 =head2 handle_chat_f
90  
91 my $future = $handler->handle_chat_f($session, $request);
92  
93 Required. Returns a L<Future> that resolves to a
94 L<Langertha::Knarr::Response>. Plain strings, C<{ content =E<gt> ..., model
95 =E<gt> ... }> hashrefs, and L<Langertha::Response> objects are all accepted
96 and coerced automatically by L<Langertha::Knarr::Response/coerce> at the
97 protocol boundary.
98  
99 =head2 handle_stream_f
100  
101 my $stream = await $handler->handle_stream_f($session, $request);
102  
103 Default implementation calls L</handle_chat_f> and emits the result as
104 a single-chunk stream. Native streamers should override and return a
105 L<Langertha::Knarr::Stream> whose C<next_chunk_f> yields chunk strings.
106  
107 =head2 list_models
108  
109 my $models = $handler->list_models;
110  
111 Required. Returns an arrayref of model descriptors as hashes with at
112 least an C<id> key.
113  
114 =head1 SUPPORT
115  
116 =head2 Issues
117  
118 Please report bugs and feature requests on GitHub at
119 L<https://github.com/Getty/langertha-knarr/issues>.
120  
121 =head2 IRC
122  
123 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
124  
125 =head1 CONTRIBUTING
126  
127 Contributions are welcome! Please fork the repository and submit a pull request.
128  
129 =head1 AUTHOR
130  
131 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
132  
133 =head1 COPYRIGHT AND LICENSE
134  
135 This software is copyright (c) 2026 by Torsten Raudssus.
136  
137 This is free software; you can redistribute it and/or modify it under
138 the same terms as the Perl 5 programming language system itself.
139  
140 =cut
« Knarr/CLI/Cmd/Container.pm ↑ Summary Knarr/Session.pm »