Knarr/Handler/Engine.pm

filter: Statementstmt 62.5% 35 / 56 Branchbran 16.6% 2 / 12 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 69.2% 9 / 13 Podpod 0.0% 0 / 3 totaltotal 47.1% 49 / 104 CC 11 SCAR 17.9
CC
11
Cov
50%
__ANON__
12.0
_supports_streaming
12.0
_model_id
8.1
CRAP 6.0
« Knarr/Request.pm ↑ Summary Knarr/PSGI.pm »
1 package Langertha::Knarr::Handler::Engine;
2 # ABSTRACT: Knarr handler that proxies directly to a Langertha engine
3 3 our $VERSION = '1.100';
4 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Langertha::Knarr::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Langertha::Knarr::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 with 'Langertha::Knarr::Handler';
11  
12  
13 3 has engine => (
14 is => 'ro',
15 required => 1,
16 );
17  
18 has model_id => (
19 is => 'ro',
20 isa => 'Maybe[Str]',
21 4 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 3 );
23  
24 sub _model_id {
25 2 ▶ my ($self) = @_;
Subroutine _model_id: called CC=5 SCAR=20.9
26 2 ▶ return $self->model_id if $self->model_id;
Branch: 1 branch
BranchTrueFalseTotal
if $self->model_id 0 2 2
27 2 my $e = $self->engine;
28 2 ▶ return $e->chat_model if $e->can('chat_model') && $e->chat_model;
Branch: 1 branch
BranchTrueFalseTotal
if $e->can("chat_model") and $e->chat_model 2 0 2
Condition33%1/3
$e->can("chat_model") and $e->chat_model
!ll&&!rl&&r
002
MC/DC0%0/2
$e->can("chat_model") and $e->chat_model
$e->can("chat_model") $e->chat_model
Truth table33%1/3
A and B
A= $e->can("chat_model")B= $e->chat_model
ABresult
0X0
100
111
29 0 ▶ return ( ref($e) =~ /::([^:]+)$/ ) ? lc($1) : 'engine';
Branch: 1 branch
BranchTrueFalseTotal
ref($e) =~ /::([^:]+)$/ ? : 0 0 0
30 }
31  
32 4 ▶async sub handle_chat_f {
Subroutine handle_chat_f: called CC=2 SCAR=7.0
Pod undocumented
33 4 my ($self, $session, $request) = @_;
34 4 my $response = await $self->engine->chat_f( $request->chat_f_args($self->engine) );
35 4 my $r = Langertha::Knarr::Response->coerce($response);
36 4 ▶ return $r->clone_with( model => $r->model // $self->_model_id );
Condition66%2/3
$r->model // $self->_model_id
l!l&&r!l&&!r
310
MC/DC0%0/2
$r->model // $self->_model_id
$r->model $self->_model_id
Truth table66%2/3
A // B
A= $r->modelB= $self->_model_id
ABresult
1X1
011
000
37 3 }
38  
39 0 ▶async sub handle_stream_f {
Subroutine handle_stream_f: not called CC=2 SCAR=17.9
Pod undocumented
40 0 my ($self, $session, $request) = @_;
41 0 my $engine = $self->engine;
42 0 ▶ unless ( _supports_streaming($engine) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_supports_streaming($engine)) 0 0 0
43 # Engine doesn't support native streaming — fall back to single-chunk.
44 0 my $r = await $self->handle_chat_f($session, $request);
45 0 return Langertha::Knarr::Stream->from_list( $r->content );
46 }
47  
48 return Langertha::Knarr::Stream->from_callback( sub {
49 0 ▶ my ($emit, $done, $fail) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 my $cb = sub {
51 0 ▶ my ($chunk) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
52 0 ▶ my $text = ref $chunk && $chunk->can('content') ? $chunk->content : "$chunk";
Branch: 1 branch
BranchTrueFalseTotal
ref $chunk && $chunk->can('content') ? : 0 0 0
Condition0%0/3
ref $chunk and $chunk->can("content")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $chunk and $chunk->can("content")
ref $chunk $chunk->can("content")
Truth table0%0/3
A and B
A= ref $chunkB= $chunk->can("content")
ABresult
0X0
100
111
53 0 $emit->($text);
54 0 };
55 0 my $f = $engine->simple_chat_stream_realtime_f( $cb, @{ $request->messages } );
56 0 $f->on_done( $done );
57 0 $f->on_fail( $fail );
58 0 $f->retain;
59 0 });
60 3 }
61  
62 sub _supports_streaming {
63 0 ▶ my ($engine) = @_;
Subroutine _supports_streaming: not called CC=3 SCAR=24.8
64 0 ▶ return $engine->supports('streaming') if $engine->can('supports');
Branch: 1 branch
BranchTrueFalseTotal
if $engine->can("supports") 0 0 0
65 0 ▶ return $engine->can('simple_chat_stream_realtime_f') && $engine->can('chat_stream_request');
Condition0%0/3
$engine->can('simple_chat_stream_realtime_f') && $engine->can('chat_stream_request')
!ll&&!rl&&r
000
MC/DC0%0/2
$engine->can('simple_chat_stream_realtime_f') && $engine->can('chat_stream_request')
$engine->can('simple_chat_stream_realtime_f') $engine->can('chat_stream_request')
Truth table0%0/3
A && B
A= $engine->can('simple_chat_stream_realtime_f')B= $engine->can('chat_stream_request')
ABresult
0X0
100
111
66 }
67  
68 sub list_models {
69 1 ▶ my ($self) = @_;
Subroutine list_models: called CC=1 SCAR=0.0
Pod undocumented
70 1 return [ { id => $self->_model_id, object => 'model' } ];
71 }
72  
73 3 __PACKAGE__->meta->make_immutable;
74 3 1;
75  
76 __END__
77  
78 =pod
79  
80 =encoding UTF-8
81  
82 =head1 NAME
83  
84 Langertha::Knarr::Handler::Engine - Knarr handler that proxies directly to a Langertha engine
85  
86 =head1 VERSION
87  
88 version 1.100
89  
90 =head1 SYNOPSIS
91  
92 use Langertha::Engine::Groq;
93 use Langertha::Knarr::Handler::Engine;
94  
95 my $engine = Langertha::Engine::Groq->new(
96 api_key => $ENV{GROQ_API_KEY},
97 chat_model => 'llama-3.3-70b-versatile',
98 );
99  
100 my $handler = Langertha::Knarr::Handler::Engine->new(
101 engine => $engine,
102 model_id => 'groq-llama-3.3-70b',
103 );
104  
105 =head1 DESCRIPTION
106  
107 Wraps a single L<Langertha::Engine::*> instance and exposes it as a
108 Knarr handler. Non-streaming requests are dispatched via
109 C<< $engine->chat_f >> with the full set of generation parameters
110 (C<tools>, C<tool_choice>, C<response_format>, C<temperature>,
111 C<max_tokens>) forwarded from the client request — subject to the
112 engine's reported capabilities. Streaming requests use
113 C<simple_chat_stream_realtime_f> for native token-by-token delivery;
114 engines that don't support streaming fall back to a single-chunk
115 emission.
116  
117 For routing across multiple engines based on model name, use
118 L<Langertha::Knarr::Handler::Router> with a L<Langertha::Knarr::Router>
119 config instead.
120  
121 =head2 engine
122  
123 Required. Any object consuming L<Langertha::Role::Chat>. Streaming
124 support is detected via C<< $engine->supports('streaming') >> (Langertha
125 0.500+) or the presence of C<simple_chat_stream_realtime_f>.
126  
127 =head2 model_id
128  
129 Optional. The id reported by L</list_models> and surfaced in responses.
130 Defaults to the engine's C<chat_model>, falling back to a derived name
131 from the engine class.
132  
133 =head1 SUPPORT
134  
135 =head2 Issues
136  
137 Please report bugs and feature requests on GitHub at
138 L<https://github.com/Getty/langertha-knarr/issues>.
139  
140 =head2 IRC
141  
142 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
143  
144 =head1 CONTRIBUTING
145  
146 Contributions are welcome! Please fork the repository and submit a pull request.
147  
148 =head1 AUTHOR
149  
150 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
151  
152 =head1 COPYRIGHT AND LICENSE
153  
154 This software is copyright (c) 2026 by Torsten Raudssus.
155  
156 This is free software; you can redistribute it and/or modify it under
157 the same terms as the Perl 5 programming language system itself.
158  
159 =cut
« Knarr/Request.pm ↑ Summary Knarr/PSGI.pm »