Knarr/CLI/Cmd/Start.pm

filter: Statementstmt 16.6% 24 / 144 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 50.0% 5 / 10 Podpod 0.0% 0 / 1 totaltotal 11.2% 29 / 257 CC 45 SCAR 73.9
CC
45
Cov
10%
execute
1980.0
_strip_quotes
6.0
__ANON__
2.0
CRAP 1613.8
↑ Summary Knarr/CLI/Cmd/Check.pm »
1 package Langertha::Knarr::CLI::Cmd::Start;
2 1 our $VERSION = '1.100';
3 # ABSTRACT: Start the Knarr proxy server
4 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use MooX::Cmd;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use MooX::Options protect_argv => 0, usage_string => 'USAGE: knarr start [options]';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Log::Any::Adapter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 option port => (
12 is => 'ro',
13 format => 'i@',
14 short => 'p',
15 doc => 'Port(s) to listen on, repeatable (default: 8080 11434)',
16 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
17 1 );
18  
19 1 option host => (
20 is => 'ro',
21 format => 's',
22 short => 'H',
23 doc => 'Host to bind to (default: 0.0.0.0)',
24 default => '0.0.0.0',
25 );
26  
27 1 option workers => (
28 is => 'ro',
29 format => 'i',
30 short => 'w',
31 doc => 'Number of worker processes (default: 1)',
32 default => 1,
33 );
34  
35 1 option from_env => (
36 is => 'ro',
37 doc => 'Build config from environment variables when no config file found',
38 default => 0,
39 );
40  
41 1 option trace_name => (
42 is => 'ro',
43 format => 's',
44 short => 'n',
45 doc => 'Langfuse trace name (default: knarr-proxy, or KNARR_TRACE_NAME env)',
46 predicate => 'has_trace_name',
47 );
48  
49 1 option log_file => (
50 is => 'ro',
51 format => 's',
52 doc => 'JSONL log file path (or KNARR_LOG_FILE env)',
53 predicate => 'has_log_file',
54 );
55  
56 1 option log_dir => (
57 is => 'ro',
58 format => 's',
59 doc => 'Directory for per-request JSON log files (or KNARR_LOG_DIR env)',
60 predicate => 'has_log_dir',
61 );
62  
63 sub execute {
64 0 ▶ my ($self, $args, $chain) = @_;
Subroutine execute: not called CC=44 SCAR=75.9
Pod undocumented
65 0 my $main = $chain->[0];
66  
67 0 my $verbose = $main->verbose;
68 0 ▶ Log::Any::Adapter->set('Stderr', log_level => $verbose ? 'trace' : 'warning');
Branch: 1 branch
BranchTrueFalseTotal
$verbose ? : 0 0 0
69  
70 0 require Langertha::Knarr::Config;
71  
72 0 my $config_file = $main->config;
73 0 my $config;
74  
75 0 ▶ if (-f $config_file) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $config_file) { } 0 0 0
elsif ($self->from_env) { } 0 0 0
76 0 $config = Langertha::Knarr::Config->new(file => $config_file);
77 0 my @errors = $config->validate;
78 0 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 0 0 0
79 0 _err("Configuration errors:");
80 0 _err(" - $_") for @errors;
81 0 exit 1;
82 }
83 0 _log("Config: loaded from $config_file");
84 } elsif ($self->from_env) {
85 0 _log("Config: auto-detecting from environment variables");
86 0 $config = Langertha::Knarr::Config->from_env(include_test => 0);
87 } else {
88 0 print STDERR "Config file not found: $config_file\n";
89 0 print STDERR "\n";
90 0 print STDERR " knarr init > knarr.yaml Generate a config from your environment\n";
91 0 print STDERR " knarr start --from-env Auto-detect config from environment variables\n";
92 0 print STDERR "\n";
93 0 exit 1;
94 }
95  
96 # Inject CLI trace_name into config
97 0 ▶ if ($self->has_trace_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_trace_name) 0 0 0
98 0 ▶ $config->data->{langfuse} //= {};
Condition0%0/2
$config->data->{"langfuse"} //= {}
l!l
00
MC/DC0%0/1
$config->data->{"langfuse"} //= {}
$config->data->{"langfuse"}
Truth table0%0/2
A
A= $config->data->{"langfuse"}
Aresult
11
00
99 0 $config->data->{langfuse}{trace_name} = $self->trace_name;
100 }
101  
102 # Inject CLI logging options into config
103 0 ▶ if ($self->has_log_file || $self->has_log_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_log_file or $self->has_log_dir) 0 0 0
Condition0%0/3
$self->has_log_file or $self->has_log_dir
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->has_log_file or $self->has_log_dir
$self->has_log_file $self->has_log_dir
Truth table0%0/3
A or B
A= $self->has_log_fileB= $self->has_log_dir
ABresult
1X1
011
000
104 0 ▶ $config->data->{logging} //= {};
Condition0%0/2
$config->data->{"logging"} //= {}
l!l
00
MC/DC0%0/1
$config->data->{"logging"} //= {}
$config->data->{"logging"}
Truth table0%0/2
A
A= $config->data->{"logging"}
Aresult
11
00
105 0 ▶ $config->data->{logging}{file} = $self->log_file if $self->has_log_file;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_log_file 0 0 0
106 0 ▶ $config->data->{logging}{dir} = $self->log_dir if $self->has_log_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_log_dir 0 0 0
107 }
108  
109 # Build listen addresses
110 0 my $listen_addrs;
111 0 my $h = $self->host;
112 0 my @ports = @{ $self->port };
113  
114 0 ▶ if (@ports) {
Branch: 2 branches
BranchTrueFalseTotal
if (@ports) { } 0 0 0
elsif (my $cfg_listen = $config->listen) { } 0 0 0
115 0 $listen_addrs = [ map { "$h:$_" } @ports ];
116 } elsif (my $cfg_listen = $config->listen) {
117 0 $listen_addrs = $cfg_listen;
118 } else {
119 0 $listen_addrs = [ "$h:8080", "$h:11434" ];
120 }
121  
122 # Startup banner
123 0 _log("Knarr LLM Proxy starting...");
124 0 _log("");
125  
126 # Log discovered engines and models
127 0 my $models = $config->models;
128 0 my $model_count = scalar keys %$models;
129 0 ▶ if ($model_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model_count) { } 0 0 0
130 0 _log("Engines: $model_count provider(s) configured");
131 0 _log("");
132 0 for my $name (sort keys %$models) {
133 0 my $m = $models->{$name};
134 0 my $line = " $name";
135 0 $line .= " => $m->{engine}";
136 0 ▶ $line .= " / $m->{model}" if $m->{model};
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"model"} 0 0 0
137 0 ▶ if ($m->{api_key_env}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->{"api_key_env"}) 0 0 0
138 0 $line .= " (key from \$$m->{api_key_env})";
139 }
140 0 _log($line);
141 }
142 0 _log("");
143 } else {
144 0 _log("Engines: none (passthrough only mode)");
145 }
146  
147 0 ▶ if ($config->auto_discover) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->auto_discover) 0 0 0
148 0 _log("Auto-discover: enabled (will query provider model lists)");
149 }
150  
151 0 ▶ if ($config->default_engine) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->default_engine) 0 0 0
152 0 _log("Default engine: $config->{data}{default}{engine}");
153 }
154  
155 # Passthrough status
156 0 my $pt = $config->passthrough;
157 0 ▶ if (keys %$pt) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$pt) { } 0 0 0
158 0 my @fmts;
159 0 for my $fmt (sort keys %$pt) {
160 0 push @fmts, "$fmt -> $pt->{$fmt}";
161 }
162 0 _log("Passthrough: " . join(', ', @fmts));
163 } else {
164 0 _log("Passthrough: disabled");
165 }
166  
167 # Langfuse tracing status
168 0 ▶ my $lf_pub = $config->langfuse->{public_key} // _strip_quotes($ENV{LANGFUSE_PUBLIC_KEY});
Condition0%0/3
$config->langfuse->{"public_key"} // _strip_quotes($ENV{"LANGFUSE_PUBLIC_KEY"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->langfuse->{"public_key"} // _strip_quotes($ENV{"LANGFUSE_PUBLIC_KEY"})
$config->langfuse->{"public_key"} _strip_quotes($ENV{"LANGFUSE_PUBLIC_KEY"})
Truth table0%0/3
A // B
A= $config->langfuse->{"public_key"}B= _strip_quotes($ENV{"LANGFUSE_PUBLIC_KEY"})
ABresult
1X1
011
000
169 0 ▶ my $lf_sec = $config->langfuse->{secret_key} // _strip_quotes($ENV{LANGFUSE_SECRET_KEY});
Condition0%0/3
$config->langfuse->{"secret_key"} // _strip_quotes($ENV{"LANGFUSE_SECRET_KEY"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->langfuse->{"secret_key"} // _strip_quotes($ENV{"LANGFUSE_SECRET_KEY"})
$config->langfuse->{"secret_key"} _strip_quotes($ENV{"LANGFUSE_SECRET_KEY"})
Truth table0%0/3
A // B
A= $config->langfuse->{"secret_key"}B= _strip_quotes($ENV{"LANGFUSE_SECRET_KEY"})
ABresult
1X1
011
000
170 0 ▶ my $lf_url = $config->langfuse->{url} // _strip_quotes($ENV{LANGFUSE_URL}) // _strip_quotes($ENV{LANGFUSE_BASE_URL}) // 'https://cloud.langfuse.com';
Condition0%0/8
(($config->langfuse->{"url"} // _strip_quotes($ENV{"LANGFUSE_URL"})) // _strip_quotes($ENV{"LANGFUSE_BASE_URL"})) // "https://cloud.langfuse.com"
l!l
00
($config->langfuse->{"url"} // _strip_quotes($ENV{"LANGFUSE_URL"})) // _strip_quotes($ENV{"LANGFUSE_BASE_URL"})
l!l&&r!l&&!r
000
$config->langfuse->{"url"} // _strip_quotes($ENV{"LANGFUSE_URL"})
l!l&&r!l&&!r
000
MC/DC0%0/3
(($config->langfuse->{"url"} // _strip_quotes($ENV{"LANGFUSE_URL"})) // _strip_quotes($ENV{"LANGFUSE_BASE_URL"})) // "https://cloud.langfuse.com"
$config->langfuse->{"url"} _strip_quotes($ENV{"LANGFUSE_URL"}) _strip_quotes($ENV{"LANGFUSE_BASE_URL"})
Truth table0%0/4
A // B // C
A= $config->langfuse->{"url"}B= _strip_quotes($ENV{"LANGFUSE_URL"})C= _strip_quotes($ENV{"LANGFUSE_BASE_URL"})
ABCresult
1XX1
01X1
0011
0000
171 0 ▶ if ($lf_pub && $lf_sec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lf_pub and $lf_sec) { } 0 0 0
Condition0%0/3
$lf_pub and $lf_sec
!ll&&!rl&&r
000
MC/DC0%0/2
$lf_pub and $lf_sec
$lf_pub $lf_sec
Truth table0%0/3
A and B
A= $lf_pubB= $lf_sec
ABresult
0X0
100
111
172 0 _log("Langfuse: enabled -> $lf_url");
173 } else {
174 0 _log("Langfuse: disabled (set LANGFUSE_PUBLIC_KEY + LANGFUSE_SECRET_KEY to enable)");
175 }
176  
177 # Proxy auth status
178 0 ▶ if ($config->has_proxy_api_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->has_proxy_api_key) { } 0 0 0
179 0 _log("Proxy auth: enabled (KNARR_API_KEY)");
180 } else {
181 0 _log("Proxy auth: open (set KNARR_API_KEY to require authentication)");
182 }
183  
184 # Request logging status
185 0 my $log_file = $config->log_file;
186 0 my $log_dir = $config->log_dir;
187 0 ▶ if ($log_file || $log_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log_file or $log_dir) { } 0 0 0
Condition0%0/3
$log_file or $log_dir
l!l&&r!l&&!r
000
MC/DC0%0/2
$log_file or $log_dir
$log_file $log_dir
Truth table0%0/3
A or B
A= $log_fileB= $log_dir
ABresult
1X1
011
000
188 0 my @parts;
189 0 ▶ push @parts, "file: $log_file" if $log_file;
Branch: 1 branch
BranchTrueFalseTotal
if $log_file 0 0 0
190 0 ▶ push @parts, "dir: $log_dir" if $log_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $log_dir 0 0 0
191 0 _log("Logging: " . join(', ', @parts));
192 } else {
193 0 _log("Logging: disabled (set KNARR_LOG_FILE or KNARR_LOG_DIR to enable)");
194 }
195  
196 0 _log("");
197  
198 # Build server
199 0 require Langertha::Knarr;
200 0 require Langertha::Knarr::Router;
201 0 require Langertha::Knarr::Handler::Router;
202 0 require IO::Async::Loop;
203  
204 0 my $loop = IO::Async::Loop->new;
205 0 my $router = Langertha::Knarr::Router->new( config => $config );
206  
207 0 my $passthrough;
208 0 ▶ if ( my $upstreams = $config->passthrough ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $upstreams = $config->passthrough) 0 0 0
209 0 ▶ if ( %$upstreams ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$upstreams) 0 0 0
210 0 require Langertha::Knarr::Handler::Passthrough;
211 0 $passthrough = Langertha::Knarr::Handler::Passthrough->new(
212 upstreams => $upstreams,
213 loop => $loop,
214 );
215 }
216 }
217  
218 0 ▶ my $handler = Langertha::Knarr::Handler::Router->new(
Branch: 1 branch
BranchTrueFalseTotal
$passthrough ? : 0 0 0
219 router => $router,
220 ( $passthrough ? ( passthrough => $passthrough ) : () ),
221 );
222  
223 0 require Langertha::Knarr::Tracing;
224 0 my $tracing = Langertha::Knarr::Tracing->new( config => $config );
225 0 ▶ if ( $tracing->_enabled ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tracing->_enabled) 0 0 0
226 0 require Langertha::Knarr::Handler::Tracing;
227 0 $handler = Langertha::Knarr::Handler::Tracing->new(
228 wrapped => $handler,
229 tracing => $tracing,
230 );
231 }
232  
233 0 require Langertha::Knarr::RequestLog;
234 0 my $rlog = Langertha::Knarr::RequestLog->new( config => $config );
235 0 ▶ if ( $rlog->_enabled ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rlog->_enabled) 0 0 0
236 0 require Langertha::Knarr::Handler::RequestLog;
237 0 $handler = Langertha::Knarr::Handler::RequestLog->new(
238 wrapped => $handler,
239 request_log => $rlog,
240 );
241 }
242  
243 0 ▶ my $knarr = Langertha::Knarr->new(
Branch: 3 branches
BranchTrueFalseTotal
$passthrough ? : 0 0 0
$tracing->_enabled ? : 0 0 0
$config->has_proxy_api_key ? : 0 0 0
244 handler => $handler,
245 loop => $loop,
246 listen => $listen_addrs,
247 router => $router,
248 ( $passthrough ? ( raw_passthrough => $passthrough ) : () ),
249 ( $tracing->_enabled ? ( tracing => $tracing ) : () ),
250 ( $config->has_proxy_api_key ? ( auth_token => $config->proxy_api_key ) : () ),
251 );
252  
253 0 _log("Starting server:");
254 0 for my $addr (@$listen_addrs) {
255 0 _log(" http://$addr");
256 }
257 0 _log("");
258  
259 0 $knarr->run;
260 }
261  
262 0 ▶sub _log { print STDERR "[knarr] $_[0]\n" }
Subroutine _log: not called CC=1 SCAR=6.9
263 0 ▶sub _err { print STDERR "[knarr] ERROR: $_[0]\n" }
Subroutine _err: not called CC=1 SCAR=6.9
264  
265 sub _strip_quotes {
266 0 ▶ my $v = shift;
Subroutine _strip_quotes: not called CC=2 SCAR=17.9
267 0 ▶ return $v unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
268 0 $v =~ s/^["']|["']$//g;
269 0 return $v;
270 }
271  
272 1 1;
273  
274 __END__
275  
276 =pod
277  
278 =encoding UTF-8
279  
280 =head1 NAME
281  
282 Langertha::Knarr::CLI::Cmd::Start - Start the Knarr proxy server
283  
284 =head1 VERSION
285  
286 version 1.100
287  
288 =head1 DESCRIPTION
289  
290 Implements the C<knarr start> command. Loads the config file, validates it,
291 and starts the server.
292  
293 With C<--from-env>, the config is built automatically from environment
294 variables when no config file is found — this is how the Docker image starts.
295  
296 See L<knarr> for the full option reference and L<Langertha::Knarr> for the
297 configuration file format.
298  
299 =head1 SEE ALSO
300  
301 =over
302  
303 =item * L<knarr> — CLI synopsis and option reference
304  
305 =item * L<Langertha::Knarr> — Full documentation
306  
307 =back
308  
309 =head1 SUPPORT
310  
311 =head2 Issues
312  
313 Please report bugs and feature requests on GitHub at
314 L<https://github.com/Getty/langertha-knarr/issues>.
315  
316 =head2 IRC
317  
318 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
319  
320 =head1 CONTRIBUTING
321  
322 Contributions are welcome! Please fork the repository and submit a pull request.
323  
324 =head1 AUTHOR
325  
326 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
327  
328 =head1 COPYRIGHT AND LICENSE
329  
330 This software is copyright (c) 2026 by Torsten Raudssus.
331  
332 This is free software; you can redistribute it and/or modify it under
333 the same terms as the Perl 5 programming language system itself.
334  
335 =cut
↑ Summary Knarr/CLI/Cmd/Check.pm »