blib/lib/Langertha/Knarr/Handler/Engine.pm

filter: Statementstmt 96.9% 64 / 66 Branchbran 77.2% 17 / 22 Conditioncond 69.4% 25 / 36 MC/DCmcdc 45.0% 9 / 20 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 3 totaltotal 80.0% 128 / 160 CC 24 SCAR 21.4
CC
24
Cov
85%
__ANON__
15.3
_model_id
8.1
_supports_streaming
5.2
CRAP 8.5
« blib/lib/Langertha/Knarr/CLI/Cmd/Models.pm ↑ Summary blib/lib/Langertha/Knarr/RequestLog.pm »
1 package Langertha::Knarr::Handler::Engine;
2 # ABSTRACT: Knarr handler that proxies directly to a Langertha engine
3 10 our $VERSION = '1.102';
4 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Langertha::Knarr::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Langertha::Knarr::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 with 'Langertha::Knarr::Handler';
11  
12  
13 10 has engine => (
14 is => 'ro',
15 required => 1,
16 );
17  
18 has model_id => (
19 is => 'ro',
20 isa => 'Maybe[Str]',
21 44 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 10 );
23  
24 2 ▶sub _model_id {
Subroutine _model_id: called CC=5 SCAR=20.9
25 2 my ($self) = @_;
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 21 ▶async sub handle_chat_f {
Subroutine handle_chat_f: called CC=2 SCAR=7.0
Pod undocumented
33 21 my ($self, $session, $request) = @_;
34 21 my $response = await $self->engine->chat_f( $request->chat_f_args($self->engine) );
35 21 my $r = Langertha::Knarr::Response->coerce($response);
36 21 ▶ return $r->clone_with( model => $r->model // $self->_model_id );
Condition66%2/3
$r->model // $self->_model_id
l!l&&r!l&&!r
2010
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 10 }
38  
39 42 ▶async sub handle_stream_f {
Subroutine handle_stream_f: called CC=3 SCAR=11.0
Pod undocumented
40 42 my ($self, $session, $request) = @_;
41 42 my $engine = $self->engine;
42 42 ▶ unless ( _supports_streaming($engine) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_supports_streaming($engine)) 3 39 42
43 # Engine doesn't support native streaming — fall back to single-chunk.
44 3 my $r = await $self->handle_chat_f($session, $request);
45 3 my $stream = Langertha::Knarr::Stream->from_list( $r->content );
46 3 $stream->finish_reason( $r->finish_reason );
47 3 $stream->tool_calls( $r->tool_calls );
48 3 ▶ $stream->usage( $r->usage ) if $r->usage;
Branch: 1 branch
BranchTrueFalseTotal
if $r->usage 1 2 3
49 3 $stream->model( $r->model );
50 3 $stream->upstream_model( $r->upstream_model );
51 3 return $stream;
52 }
53  
54 39 ▶ return Langertha::Knarr::Stream->from_callback( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
55 39 my ($emit, $done, $fail, $finish, $tool_call, $usage, $model) = @_;
56 132 ▶ my $cb = sub {
Subroutine __ANON__: called CC=15 SCAR=27.3
57 132 my ($chunk) = @_;
58 132 ▶ my $text = ref $chunk && $chunk->can('content') ? $chunk->content : "$chunk";
Branch: 1 branch
BranchTrueFalseTotal
ref $chunk && $chunk->can('content') ? : 126 6 132
Condition66%2/3
ref $chunk and $chunk->can("content")
!ll&&!rl&&r
60126
MC/DC50%1/2
ref $chunk and $chunk->can("content")
ref $chunk $chunk->can("content")
Truth table66%2/3
A and B
A= ref $chunkB= $chunk->can("content")
ABresult
0X0
100
111
59 132 $emit->($text);
60 # Langertha::Stream::Chunk carries the backend's finish_reason on the
61 # terminal chunk; the protocol maps it when it closes the stream.
62 132 ▶ $finish->( $chunk->finish_reason )
Branch: 1 branch
BranchTrueFalseTotal
if ref $chunk and $chunk->can("has_finish_reason") and $chunk->has_finish_reason 37 95 132
Condition83%5/6
ref $chunk and $chunk->can("has_finish_reason") and $chunk->has_finish_reason
!ll&&!rl&&r
68937
ref $chunk and $chunk->can("has_finish_reason")
!ll&&!rl&&r
60126
MC/DC66%2/3
ref $chunk and $chunk->can("has_finish_reason") and $chunk->has_finish_reason
ref $chunk $chunk->can("has_finish_reason") $chunk->has_finish_reason
Truth table75%3/4
A and B and C
A= ref $chunkB= $chunk->can("has_finish_reason")C= $chunk->has_finish_reason
ABCresult
0XX0
10X0
1100
1111
63 if ref $chunk && $chunk->can('has_finish_reason') && $chunk->has_finish_reason;
64 # Core assembles streamed tool-call fragments and attaches the finished
65 # Langertha::ToolCall objects to a chunk (Role::Chat::aggregate_tool_calls
66 # collects the same); a core whose parser attaches none yields none. The
67 # protocol emits them when it closes the stream (k19).
68 132 ▶ $tool_call->( @{ $chunk->tool_calls } )
Branch: 1 branch
BranchTrueFalseTotal
if ref $chunk and $chunk->can("has_tool_calls") and $chunk->has_tool_calls 31 101 132
Condition83%5/6
ref $chunk and $chunk->can("has_tool_calls") and $chunk->has_tool_calls
!ll&&!rl&&r
69531
ref $chunk and $chunk->can("has_tool_calls")
!ll&&!rl&&r
60126
MC/DC66%2/3
ref $chunk and $chunk->can("has_tool_calls") and $chunk->has_tool_calls
ref $chunk $chunk->can("has_tool_calls") $chunk->has_tool_calls
Truth table75%3/4
A and B and C
A= ref $chunkB= $chunk->can("has_tool_calls")C= $chunk->has_tool_calls
ABCresult
0XX0
10X0
1100
1111
69 if ref $chunk && $chunk->can('has_tool_calls') && $chunk->has_tool_calls;
70 # The token usage rides on a chunk too, cumulative (the last report is
71 # the stream's totals); the protocol puts it on its terminal frames and
72 # the tracing decorator on the generation.
73 132 ▶ $usage->( $chunk->usage )
Branch: 1 branch
BranchTrueFalseTotal
if ref $chunk and $chunk->can("has_usage") and $chunk->has_usage 6 126 132
Condition83%5/6
ref $chunk and $chunk->can("has_usage") and $chunk->has_usage
!ll&&!rl&&r
61206
ref $chunk and $chunk->can("has_usage")
!ll&&!rl&&r
60126
MC/DC66%2/3
ref $chunk and $chunk->can("has_usage") and $chunk->has_usage
ref $chunk $chunk->can("has_usage") $chunk->has_usage
Truth table75%3/4
A and B and C
A= ref $chunkB= $chunk->can("has_usage")C= $chunk->has_usage
ABCresult
0XX0
10X0
1100
1111
74 if ref $chunk && $chunk->can('has_usage') && $chunk->has_usage;
75 # The model the backend reports answering with, for the trace.
76 132 ▶ $model->( $chunk->model )
Branch: 1 branch
BranchTrueFalseTotal
if ref $chunk and $chunk->can("has_model") and $chunk->has_model 21 111 132
Condition83%5/6
ref $chunk and $chunk->can("has_model") and $chunk->has_model
!ll&&!rl&&r
610521
ref $chunk and $chunk->can("has_model")
!ll&&!rl&&r
60126
MC/DC66%2/3
ref $chunk and $chunk->can("has_model") and $chunk->has_model
ref $chunk $chunk->can("has_model") $chunk->has_model
Truth table75%3/4
A and B and C
A= ref $chunkB= $chunk->can("has_model")C= $chunk->has_model
ABCresult
0XX0
10X0
1100
1111
77 if ref $chunk && $chunk->can('has_model') && $chunk->has_model;
78 39 };
79 39 my $f = $engine->chat_stream_realtime_f( chunk_callback => $cb, $request->chat_f_args($engine) );
80 39 $f->on_done( $done );
81 39 $f->on_fail( $fail );
82 39 $f->retain;
83 39 });
84 10 }
85  
86 39 ▶sub _supports_streaming {
Subroutine _supports_streaming: called CC=3 SCAR=16.5
87 39 my ($engine) = @_;
88 39 ▶ return $engine->supports('streaming') if $engine->can('supports');
Branch: 1 branch
BranchTrueFalseTotal
if $engine->can("supports") 39 0 39
89 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
90 }
91  
92 1 ▶sub list_models {
Subroutine list_models: called CC=1 SCAR=0.0
Pod undocumented
93 1 my ($self) = @_;
94 1 return [ { id => $self->_model_id, object => 'model' } ];
95 }
96  
97 10 __PACKAGE__->meta->make_immutable;
98 10 1;
99  
100 __END__
101  
102 =pod
103  
104 =encoding UTF-8
105  
106 =head1 NAME
107  
108 Langertha::Knarr::Handler::Engine - Knarr handler that proxies directly to a Langertha engine
109  
110 =head1 VERSION
111  
112 version 1.102
113  
114 =head1 SYNOPSIS
115  
116 use Langertha::Engine::Groq;
117 use Langertha::Knarr::Handler::Engine;
118  
119 my $engine = Langertha::Engine::Groq->new(
120 api_key => $ENV{GROQ_API_KEY},
121 chat_model => 'llama-3.3-70b-versatile',
122 );
123  
124 my $handler = Langertha::Knarr::Handler::Engine->new(
125 engine => $engine,
126 model_id => 'groq-llama-3.3-70b',
127 );
128  
129 =head1 DESCRIPTION
130  
131 Wraps a single L<Langertha::Engine::*> instance and exposes it as a
132 Knarr handler. Non-streaming requests are dispatched via
133 C<< $engine->chat_f >> with the full set of generation parameters
134 (C<tools>, C<tool_choice>, C<response_format>, C<temperature>,
135 C<max_tokens>) forwarded from the client request — subject to the
136 engine's reported capabilities. Streaming requests use
137 C<chat_stream_realtime_f> with the same capability-filtered generation
138 parameters for native token-by-token delivery; engines that don't
139 support streaming fall back to a single-chunk emission.
140  
141 For routing across multiple engines based on model name, use
142 L<Langertha::Knarr::Handler::Router> with a L<Langertha::Knarr::Router>
143 config instead.
144  
145 =head2 engine
146  
147 Required. Any object consuming L<Langertha::Role::Chat>. Streaming
148 support is detected via C<< $engine->supports('streaming') >> (Langertha
149 0.500+) or the presence of C<simple_chat_stream_realtime_f>.
150  
151 =head2 model_id
152  
153 Optional. The id reported by L</list_models> and surfaced in responses.
154 Defaults to the engine's C<chat_model>, falling back to a derived name
155 from the engine class.
156  
157 =head1 SUPPORT
158  
159 =head2 Issues
160  
161 Please report bugs and feature requests on GitHub at
162 L<https://github.com/Getty/langertha-knarr/issues>.
163  
164 =head2 IRC
165  
166 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
167  
168 =head1 CONTRIBUTING
169  
170 Contributions are welcome! Please fork the repository and submit a pull request.
171  
172 =head1 AUTHOR
173  
174 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
175  
176 =head1 COPYRIGHT AND LICENSE
177  
178 This software is copyright (c) 2026 by Torsten Raudssus.
179  
180 This is free software; you can redistribute it and/or modify it under
181 the same terms as the Perl 5 programming language system itself.
182  
183 =cut
« blib/lib/Langertha/Knarr/CLI/Cmd/Models.pm ↑ Summary blib/lib/Langertha/Knarr/RequestLog.pm »