Knarr/Handler/Code.pm

filter: Statementstmt 94.8% 37 / 39 Branchbran 50.0% 1 / 2 Conditioncond 80.0% 4 / 5 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 3 totaltotal 86.8% 53 / 61 CC 4 SCAR 4.9
CC
4
Cov
91%
handle_chat_f
3.0
handle_stream_f
2.1
BEGIN
1.0
CRAP 1.6
« Knarr/Handler/Raider.pm ↑ Summary Knarr/Protocol.pm »
1 package Langertha::Knarr::Handler::Code;
2 # ABSTRACT: Coderef-backed Knarr handler for fakes, tests, and custom logic
3 13 our $VERSION = '1.100';
4 13 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Langertha::Knarr::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Langertha::Knarr::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 with 'Langertha::Knarr::Handler';
11  
12  
13 13 has code => (
14 is => 'ro',
15 isa => 'CodeRef',
16 required => 1,
17 );
18  
19 # Optional: separate generator for streaming. Returns a coderef that itself
20 # returns next-chunk strings (undef = done) when called.
21 has stream_code => (
22 is => 'ro',
23 isa => 'Maybe[CodeRef]',
24 8 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 13 );
26  
27 has models => (
28 is => 'ro',
29 isa => 'ArrayRef',
30 15 ▶ default => sub { [ { id => 'knarr-code', object => 'model' } ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 13 );
32  
33 24 ▶async sub handle_chat_f {
Subroutine handle_chat_f: called CC=3 SCAR=11.0
Pod undocumented
34 24 my ($self, $session, $request) = @_;
35 24 my $out = $self->code->( $session, $request );
36 24 my $r = Langertha::Knarr::Response->coerce($out);
37 24 ▶ return $r->clone_with( model => $r->model // $request->model // 'knarr-code' );
Condition80%4/5
($r->model // $request->model) // "knarr-code"
l!l
213
$r->model // $request->model
l!l&&r!l&&!r
0213
MC/DC50%1/2
($r->model // $request->model) // "knarr-code"
$r->model $request->model
Truth table66%2/3
A // B
A= $r->modelB= $request->model
ABresult
1X1
011
000
38 13 }
39  
40 13 ▶async sub handle_stream_f {
Subroutine handle_stream_f: called CC=2 SCAR=7.6
Pod undocumented
41 13 my ($self, $session, $request) = @_;
42 13 ▶ if ( my $sc = $self->stream_code ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sc = $self->stream_code) 13 0 13
43 13 my $gen = $sc->( $session, $request );
44 13 return Langertha::Knarr::Stream->new( generator => $gen );
45 }
46 0 my $text = $self->code->( $session, $request );
47 0 return Langertha::Knarr::Stream->from_list("$text");
48 13 }
49  
50 3 ▶sub list_models { $_[0]->models }
Subroutine list_models: called CC=1 SCAR=0.0
Pod undocumented
51  
52 13 __PACKAGE__->meta->make_immutable;
53 13 1;
54  
55 __END__
56  
57 =pod
58  
59 =encoding UTF-8
60  
61 =head1 NAME
62  
63 Langertha::Knarr::Handler::Code - Coderef-backed Knarr handler for fakes, tests, and custom logic
64  
65 =head1 VERSION
66  
67 version 1.100
68  
69 =head1 SYNOPSIS
70  
71 use Langertha::Knarr::Handler::Code;
72  
73 my $handler = Langertha::Knarr::Handler::Code->new(
74 code => sub {
75 my ($session, $request) = @_;
76 return 'echo: ' . $request->messages->[-1]{content};
77 },
78 stream_code => sub {
79 my @parts = ('hel', 'lo');
80 return sub { @parts ? shift @parts : undef };
81 },
82 );
83  
84 =head1 DESCRIPTION
85  
86 The simplest possible handler: pass coderefs that return strings (or
87 chunk generators for streaming) and you get a working Knarr handler.
88 Useful for tests, fakes, smoketests, and "fake LLM" demos.
89  
90 =head2 code
91  
92 Required. Coderef called as C<< $code->($session, $request) >> for
93 non-streaming requests; must return a scalar string.
94  
95 =head2 stream_code
96  
97 Optional. Coderef returning another coderef that yields the next chunk
98 per call, C<undef> to signal end.
99  
100 =head2 models
101  
102 Optional. Arrayref of model descriptors. Defaults to a single
103 C<knarr-code> entry.
104  
105 =head1 SUPPORT
106  
107 =head2 Issues
108  
109 Please report bugs and feature requests on GitHub at
110 L<https://github.com/Getty/langertha-knarr/issues>.
111  
112 =head2 IRC
113  
114 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
115  
116 =head1 CONTRIBUTING
117  
118 Contributions are welcome! Please fork the repository and submit a pull request.
119  
120 =head1 AUTHOR
121  
122 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
123  
124 =head1 COPYRIGHT AND LICENSE
125  
126 This software is copyright (c) 2026 by Torsten Raudssus.
127  
128 This is free software; you can redistribute it and/or modify it under
129 the same terms as the Perl 5 programming language system itself.
130  
131 =cut
« Knarr/Handler/Raider.pm ↑ Summary Knarr/Protocol.pm »