blib/lib/Langertha/Knarr/Config.pm

filter: Statementstmt 96.8% 310 / 320 Branchbran 86.9% 127 / 146 Conditioncond 79.7% 110 / 138 MC/DCmcdc 64.8% 48 / 74 Subroutinesub 100% 42 / 42 Podpod 90.0% 9 / 10 totaltotal 88.4% 646 / 730 CC 142 SCAR 22.3
CC
142
Cov
91%
validate
23.3
scan_env
15.7
generate_config
15.1
CRAP 9.3
« blib/lib/Langertha/Knarr/PassthroughUsage.pm ↑ Summary blib/lib/Langertha/Knarr/CLI/Cmd/Models.pm »
1 package Langertha::Knarr::Config;
2 68 our $VERSION = '1.102';
3 # ABSTRACT: YAML configuration loader and validator
4 68 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 68 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 68 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 68 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 68 ▶use Langertha ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 68 has file => (
12 is => 'ro',
13 predicate => 'has_file',
14 );
15  
16  
17 68 has data => (
18 is => 'lazy',
19 builder => '_build_data',
20 );
21  
22  
23 41 ▶sub _build_data {
Subroutine _build_data: called CC=5 SCAR=16.1
24 41 my ($self) = @_;
25 41 ▶ return {} unless $self->has_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_file 4 37 41
26 37 my $file = $self->file;
27 37 ▶ croak "Config file not found: $file" unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 37 37
28 37 my $ypp = YAML::PP->new;
29 37 my $data = eval { $ypp->load_file($file) };
30 37 ▶ croak "Config file $file is not valid YAML: ".$self->_error_text($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 36 37
31 36 ▶ croak "Config file $file must be a mapping of key: value pairs"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 2 34 36
32 unless ref $data eq 'HASH';
33 34 _interpolate_env($data);
34 34 $log->debugf("Loaded config from %s", $file);
35 34 return $data;
36 }
37  
38 # Recursively interpolate ${ENV_VAR} in string values
39 115 ▶sub _interpolate_env {
Subroutine _interpolate_env: called CC=11 SCAR=26.3
40 115 my ($ref) = @_;
41 115 ▶ if (ref $ref eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ref eq 'HASH') { } 111 4 115
elsif (ref $ref eq 'ARRAY') { } 4 0 4
42 111 for my $key (keys %$ref) {
43 190 ▶ if (ref $ref->{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ref->{$key}) { } 81 109 190
elsif (defined $ref->{$key}) { } 109 0 109
44 81 _interpolate_env($ref->{$key});
45 } elsif (defined $ref->{$key}) {
46 109 ▶ $ref->{$key} =~ s/\$\{(\w+)\}/$ENV{$1} \/\/ ''/ge;
Condition100%2/2
$ENV{$1} // ""
l!l
31
MC/DC100%1/1
$ENV{$1} // ""
$ENV{$1}
Truth table100%2/2
A
A= $ENV{$1}
Aresult
11
00
47 }
48 }
49 } elsif (ref $ref eq 'ARRAY') {
50 4 for my $i (0..$#$ref) {
51 6 ▶ if (ref $ref->[$i]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ref->[$i]) { } 0 6 6
elsif (defined $ref->[$i]) { } 6 0 6
52 0 _interpolate_env($ref->[$i]);
53 } elsif (defined $ref->[$i]) {
54 6 ▶ $ref->[$i] =~ s/\$\{(\w+)\}/$ENV{$1} \/\/ ''/ge;
Condition0%0/2
$ENV{$1} // ""
l!l
00
MC/DC0%0/1
$ENV{$1} // ""
$ENV{$1}
Truth table0%0/2
A
A= $ENV{$1}
Aresult
11
00
55 }
56 }
57 }
58 }
59  
60 # Engine catalog: which Langertha engines are worth auto-detecting from the
61 # environment, and under which variable names, in priority order.
62 # First match wins per engine: LANGERTHA_ > bare vendor name > TEST_.
63 #
64 # Only hosted, API-key authenticated engines belong here. Local engines
65 # (Ollama, LMStudio, vLLM, SGLang, LlamaCpp, Whisper) are reached by URL and
66 # have no meaningful key to detect, and protocol variants of an engine
67 # (MiniMaxAnthropic, MoonshotAnthropic, AKIOpenAI, OpenAIResponses) share
68 # their vendor's key — listing them would emit two model entries per key.
69 68 my @ENGINE_DEFS = (
70 { engine => 'OpenAI', vars => [qw( LANGERTHA_OPENAI_API_KEY OPENAI_API_KEY TEST_LANGERTHA_OPENAI_API_KEY )] },
71 { engine => 'Anthropic', vars => [qw( LANGERTHA_ANTHROPIC_API_KEY ANTHROPIC_API_KEY TEST_LANGERTHA_ANTHROPIC_API_KEY )] },
72 { engine => 'Groq', vars => [qw( LANGERTHA_GROQ_API_KEY GROQ_API_KEY TEST_LANGERTHA_GROQ_API_KEY )] },
73 { engine => 'Mistral', vars => [qw( LANGERTHA_MISTRAL_API_KEY MISTRAL_API_KEY TEST_LANGERTHA_MISTRAL_API_KEY )] },
74 { engine => 'DeepSeek', vars => [qw( LANGERTHA_DEEPSEEK_API_KEY DEEPSEEK_API_KEY TEST_LANGERTHA_DEEPSEEK_API_KEY )] },
75 { engine => 'MiniMax', vars => [qw( LANGERTHA_MINIMAX_API_KEY MINIMAX_API_KEY TEST_LANGERTHA_MINIMAX_API_KEY )] },
76 { engine => 'Cerebras', vars => [qw( LANGERTHA_CEREBRAS_API_KEY CEREBRAS_API_KEY TEST_LANGERTHA_CEREBRAS_API_KEY )] },
77 { engine => 'OpenRouter', vars => [qw( LANGERTHA_OPENROUTER_API_KEY OPENROUTER_API_KEY TEST_LANGERTHA_OPENROUTER_API_KEY )] },
78 { engine => 'Perplexity', vars => [qw( LANGERTHA_PERPLEXITY_API_KEY PERPLEXITY_API_KEY TEST_LANGERTHA_PERPLEXITY_API_KEY )] },
79 { engine => 'Replicate', vars => [qw( LANGERTHA_REPLICATE_API_KEY REPLICATE_API_TOKEN TEST_LANGERTHA_REPLICATE_API_KEY )] },
80 { engine => 'HuggingFace', vars => [qw( LANGERTHA_HUGGINGFACE_API_KEY HUGGINGFACE_API_KEY TEST_LANGERTHA_HUGGINGFACE_API_KEY )] },
81 { engine => 'Gemini', vars => [qw( LANGERTHA_GEMINI_API_KEY GEMINI_API_KEY TEST_LANGERTHA_GEMINI_API_KEY )] },
82 { engine => 'XAI', vars => [qw( LANGERTHA_XAI_API_KEY XAI_API_KEY TEST_LANGERTHA_XAI_API_KEY )] },
83 { engine => 'Moonshot', vars => [qw( LANGERTHA_MOONSHOT_API_KEY MOONSHOT_API_KEY TEST_LANGERTHA_MOONSHOT_API_KEY )] },
84 { engine => 'NousResearch', vars => [qw( LANGERTHA_NOUSRESEARCH_API_KEY NOUSRESEARCH_API_KEY TEST_LANGERTHA_NOUSRESEARCH_API_KEY )] },
85 { engine => 'AKI', vars => [qw( LANGERTHA_AKI_API_KEY AKI_API_KEY TEST_LANGERTHA_AKI_API_KEY )] },
86 { engine => 'Scaleway', vars => [qw( LANGERTHA_SCALEWAY_API_KEY SCALEWAY_API_KEY TEST_LANGERTHA_SCALEWAY_API_KEY )] },
87 { engine => 'TSystems', vars => [qw( LANGERTHA_TSYSTEMS_API_KEY TSYSTEMS_API_KEY TEST_LANGERTHA_TSYSTEMS_API_KEY )] },
88 # No bare HETZNER_API_KEY: that name is in wide use for the Hetzner Cloud
89 # infrastructure API and would false-positive into an unusable model entry.
90 { engine => 'Hetzner', vars => [qw( LANGERTHA_HETZNER_API_KEY TEST_LANGERTHA_HETZNER_API_KEY )] },
91 );
92  
93 # Last resort for engines whose Langertha class deliberately has no usable
94 # default_model (it croaks, demanding an explicit model). Everything else is
95 # read off the engine class — see default_model_for. An engine only belongs
96 # here while its class refuses to name a default; t/10-config.t asserts that.
97 68 my %DEFAULT_MODEL_FALLBACK = (
98 Groq => 'llama-3.3-70b-versatile',
99 OpenRouter => 'openai/gpt-4o-mini',
100 );
101  
102 68 my %DEFAULT_MODEL_CACHE;
103  
104  
105 2 ▶sub engine_catalog {
Subroutine engine_catalog: called CC=1 SCAR=0.0
Pod documented
106 2 return [ map { { engine => $_->{engine}, vars => [@{$_->{vars}}] } } @ENGINE_DEFS ];
107 }
108  
109  
110 74 ▶sub default_model_for {
Subroutine default_model_for: called CC=10 SCAR=23.0
Pod documented
111 74 my ($class, $engine) = @_;
112 74 ▶ return undef unless defined $engine && length $engine;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $engine and length $engine 2 72 74
Condition100%3/3
defined $engine and length $engine
!ll&&!rl&&r
1172
MC/DC100%2/2
defined $engine and length $engine
defined $engine length $engine
Truth table100%3/3
A and B
A= defined $engineB= length $engine
ABresult
0X0
100
111
113 72 ▶ return $DEFAULT_MODEL_CACHE{$engine} if exists $DEFAULT_MODEL_CACHE{$engine};
Branch: 1 branch
BranchTrueFalseTotal
if exists $DEFAULT_MODEL_CACHE{$engine} 49 23 72
114  
115 23 my $model;
116 23 my $engine_class = eval { Langertha->resolve_engine_class($engine) };
117 23 ▶ if (!defined $engine_class) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $engine_class) { } 1 22 23
elsif ($engine_class->can('default_model')) { } 22 0 22
118 1 $log->debugf("No Langertha engine class for %s: %s", $engine, $@);
119 } elsif ($engine_class->can('default_model')) {
120 # Engines without a sensible default croak here instead of returning one.
121 22 $model = eval { $engine_class->default_model };
122 22 ▶ $log->debugf("Engine %s names no default model: %s", $engine, $@)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model 4 18 22
123 unless defined $model;
124 }
125  
126 23 ▶ undef $model unless defined $model && length $model;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model and length $model 5 18 23
Condition66%2/3
defined $model and length $model
!ll&&!rl&&r
5018
MC/DC50%1/2
defined $model and length $model
defined $model length $model
Truth table66%2/3
A and B
A= defined $modelB= length $model
ABresult
0X0
100
111
127 23 ▶ $model = $DEFAULT_MODEL_FALLBACK{$engine} unless defined $model;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model 5 18 23
128  
129 23 return $DEFAULT_MODEL_CACHE{$engine} = $model;
130 }
131  
132  
133 # Build config purely from environment variables (zero-config Docker mode)
134 9 ▶sub from_env {
Subroutine from_env: called CC=3 SCAR=11.0
Pod documented
135 9 my ($class, %opts) = @_;
136 9 my $found = $class->scan_env(%opts);
137  
138 9 my %models;
139 9 for my $engine (keys %$found) {
140 26 my $name = lc($engine);
141 $models{$name} = {
142 engine => $engine,
143 model => $class->default_model_for($engine),
144 api_key_env => $found->{$engine}{api_key_env},
145 26 };
146 }
147  
148 9 my %data = (
149 models => \%models,
150 auto_discover => 1,
151 passthrough => 1,
152 );
153  
154 # Set default engine if OpenAI found, reading its key from the variable
155 # that was found: the engine by itself reads only LANGERTHA_OPENAI_API_KEY
156 # (k45)
157 9 ▶ if ($found->{OpenAI}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found->{"OpenAI"}) 6 3 9
158 6 $data{default} = { engine => 'OpenAI', api_key_env => $found->{OpenAI}{api_key_env} };
159 }
160  
161 9 return $class->new(data => \%data);
162 }
163  
164 68 has listen => (
165 is => 'lazy',
166 builder => '_build_listen',
167 );
168  
169  
170 54 ▶sub _build_listen {
Subroutine _build_listen: called CC=4 SCAR=13.9
171 54 my ($self) = @_;
172 54 my $raw = $self->data->{listen};
173 54 ▶ return ['127.0.0.1:8080', '127.0.0.1:11434'] unless defined $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw 41 13 54
174 13 ▶ my $list = ref $raw eq 'ARRAY' ? $raw : [$raw];
Branch: 1 branch
BranchTrueFalseTotal
ref $raw eq 'ARRAY' ? : 9 4 13
175 # A mapping would be bound as "HASH(0x...)" (k67); validate reports it
176 croak "listen must be a host:port string or a list of them"
177 13 ▶ if grep { ref } @$list;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_;} @$list 6 7 13
178 7 return $list;
179 }
180  
181 68 has models => (
182 is => 'lazy',
183 builder => '_build_models',
184 );
185  
186  
187 110 ▶sub _build_models {
Subroutine _build_models: called CC=3 SCAR=11.0
188 110 my ($self) = @_;
189 110 ▶ my $models = $self->_section('models') // {};
Condition100%2/2
$self->_section("models") // {}
l!l
1071
MC/DC100%1/1
$self->_section("models") // {}
$self->_section("models")
Truth table100%2/2
A
A= $self->_section("models")
Aresult
11
00
190 $self->_warn_unused_context_size( "Model '$_'", $models->{$_} )
191 108 for sort keys %$models;
192 108 return $models;
193 }
194  
195 # A context_size only reaches engines that compose Role::ContextSize (k30);
196 # say so once, at load, instead of dropping the operator's intent silently.
197 # An engine class that does not load is left for the router to report.
198 310 ▶sub _warn_unused_context_size {
Subroutine _warn_unused_context_size: called CC=6 SCAR=18.0
199 310 my ($self, $label, $def) = @_;
200 return unless ref $def eq 'HASH'
201 310 ▶ && defined $def->{context_size} && $def->{engine};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $def eq "HASH" and defined $def->{"context_size"} and $def->{"engine"} 303 7 310
Condition83%5/6
ref $def eq "HASH" and defined $def->{"context_size"} and $def->{"engine"}
!ll&&!rl&&r
30307
ref $def eq "HASH" and defined $def->{"context_size"}
!ll&&!rl&&r
412627
MC/DC66%2/3
ref $def eq "HASH" and defined $def->{"context_size"} and $def->{"engine"}
ref $def eq "HASH" defined $def->{"context_size"} $def->{"engine"}
Truth table75%3/4
A and B and C
A= ref $def eq "HASH"B= defined $def->{"context_size"}C= $def->{"engine"}
ABCresult
0XX0
10X0
1100
1111
202 7 my $class = eval { Langertha->resolve_engine_class( $def->{engine} ) };
203 7 ▶ return if !defined $class || $class->can('context_size');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $class or $class->can("context_size") 5 2 7
Condition66%2/3
not defined $class or $class->can("context_size")
l!l&&r!l&&!r
052
MC/DC50%1/2
not defined $class or $class->can("context_size")
not defined $class $class->can("context_size")
Truth table66%2/3
not(A) or B
A= not defined $classB= $class->can("context_size")
ABresult
1X1
011
000
204 $log->warnf( "%s: engine %s does not take context_size, ignoring it",
205 2 $label, $def->{engine} );
206 }
207  
208 68 has default_engine => (
209 is => 'lazy',
210 builder => '_build_default_engine',
211 );
212  
213  
214 59 ▶sub _build_default_engine {
Subroutine _build_default_engine: called CC=1 SCAR=0.0
215 59 my ($self) = @_;
216 59 my $default = $self->_section('default');
217 58 $self->_warn_unused_context_size( 'Default', $default );
218 58 return $default;
219 }
220  
221 68 has log_file => (
222 is => 'lazy',
223 builder => '_build_log_file',
224 );
225  
226  
227 14 ▶sub _build_log_file {
Subroutine _build_log_file: called CC=4 SCAR=13.9
228 14 my ($self) = @_;
229 14 ▶ return ( $self->_section('logging') // {} )->{file} // _strip_quotes($ENV{KNARR_LOG_FILE}) // undef;
Condition85%6/7
(+($self->_section("logging") // {})->{"file"} // _strip_quotes($ENV{"KNARR_LOG_FILE"})) // undef
l!l
76
+($self->_section("logging") // {})->{"file"} // _strip_quotes($ENV{"KNARR_LOG_FILE"})
l!l&&r!l&&!r
706
$self->_section("logging") // {}
l!l
85
MC/DC50%1/2
(+($self->_section("logging") // {})->{"file"} // _strip_quotes($ENV{"KNARR_LOG_FILE"})) // undef
$self->_section("logging") _strip_quotes($ENV{"KNARR_LOG_FILE"})
Truth table66%2/3
A // B
A= $self->_section("logging")B= _strip_quotes($ENV{"KNARR_LOG_FILE"})
ABresult
1X1
011
000
230 }
231  
232 68 has log_dir => (
233 is => 'lazy',
234 builder => '_build_log_dir',
235 );
236  
237  
238 14 ▶sub _build_log_dir {
Subroutine _build_log_dir: called CC=4 SCAR=13.9
239 14 my ($self) = @_;
240 14 ▶ return ( $self->_section('logging') // {} )->{dir} // _strip_quotes($ENV{KNARR_LOG_DIR}) // undef;
Condition85%6/7
(+($self->_section("logging") // {})->{"dir"} // _strip_quotes($ENV{"KNARR_LOG_DIR"})) // undef
l!l
211
+($self->_section("logging") // {})->{"dir"} // _strip_quotes($ENV{"KNARR_LOG_DIR"})
l!l&&r!l&&!r
2011
$self->_section("logging") // {}
l!l
85
MC/DC50%1/2
(+($self->_section("logging") // {})->{"dir"} // _strip_quotes($ENV{"KNARR_LOG_DIR"})) // undef
$self->_section("logging") _strip_quotes($ENV{"KNARR_LOG_DIR"})
Truth table66%2/3
A // B
A= $self->_section("logging")B= _strip_quotes($ENV{"KNARR_LOG_DIR"})
ABresult
1X1
011
000
241 }
242  
243 68 has langfuse => (
244 is => 'lazy',
245 builder => '_build_langfuse',
246 );
247  
248  
249 171 ▶sub _build_langfuse {
Subroutine _build_langfuse: called CC=2 SCAR=6.9
250 171 my ($self) = @_;
251 171 ▶ return $self->_section('langfuse') // {};
Condition100%2/2
$self->_section("langfuse") // {}
l!l
7199
MC/DC100%1/1
$self->_section("langfuse") // {}
$self->_section("langfuse")
Truth table100%2/2
A
A= $self->_section("langfuse")
Aresult
11
00
252 }
253  
254 68 has langfuse_transport => (
255 is => 'lazy',
256 builder => '_build_langfuse_transport',
257 );
258  
259  
260 95 ▶sub _build_langfuse_transport {
Subroutine _build_langfuse_transport: called CC=5 SCAR=16.1
261 95 my ($self) = @_;
262 95 ▶ my $value = $self->langfuse->{transport} // _strip_quotes($ENV{KNARR_LANGFUSE_TRANSPORT});
Condition100%3/3
$self->langfuse->{"transport"} // _strip_quotes($ENV{"KNARR_LANGFUSE_TRANSPORT"})
l!l&&r!l&&!r
19175
MC/DC100%2/2
$self->langfuse->{"transport"} // _strip_quotes($ENV{"KNARR_LANGFUSE_TRANSPORT"})
$self->langfuse->{"transport"} _strip_quotes($ENV{"KNARR_LANGFUSE_TRANSPORT"})
Truth table100%3/3
A // B
A= $self->langfuse->{"transport"}B= _strip_quotes($ENV{"KNARR_LANGFUSE_TRANSPORT"})
ABresult
1X1
011
000
263 95 ▶ return 'ingestion' unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 75 20 95
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
75020
MC/DC50%1/2
defined $value and length $value
defined $value length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
264 20 ▶ croak "langfuse.transport '$value' must be ingestion or otel"
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A(?:ingestion|otel)\z/i 3 17 20
265 unless $value =~ /\A(?:ingestion|otel)\z/i;
266 17 return lc $value;
267 }
268  
269 68 has langfuse_timeout => (
270 is => 'lazy',
271 builder => '_build_langfuse_timeout',
272 );
273  
274  
275 95 ▶sub _build_langfuse_timeout {
Subroutine _build_langfuse_timeout: called CC=2 SCAR=6.9
276 95 my ($self) = @_;
277 return _seconds( 'langfuse.timeout' =>
278 95 ▶ $self->langfuse->{timeout} // _strip_quotes($ENV{KNARR_LANGFUSE_TIMEOUT}), 15 );
Condition100%3/3
$self->langfuse->{"timeout"} // _strip_quotes($ENV{"KNARR_LANGFUSE_TIMEOUT"})
l!l&&r!l&&!r
10184
MC/DC100%2/2
$self->langfuse->{"timeout"} // _strip_quotes($ENV{"KNARR_LANGFUSE_TIMEOUT"})
$self->langfuse->{"timeout"} _strip_quotes($ENV{"KNARR_LANGFUSE_TIMEOUT"})
Truth table100%3/3
A // B
A= $self->langfuse->{"timeout"}B= _strip_quotes($ENV{"KNARR_LANGFUSE_TIMEOUT"})
ABresult
1X1
011
000
279 }
280  
281 68 has proxy_api_key => (
282 is => 'lazy',
283 builder => '_build_proxy_api_key',
284 );
285  
286  
287 7 ▶sub _build_proxy_api_key {
Subroutine _build_proxy_api_key: called CC=3 SCAR=11.1
288 7 my ($self) = @_;
289 7 ▶ return $self->data->{proxy_api_key} // $ENV{KNARR_API_KEY} // undef;
Condition80%4/5
($self->data->{"proxy_api_key"} // $ENV{"KNARR_API_KEY"}) // undef
l!l
16
$self->data->{"proxy_api_key"} // $ENV{"KNARR_API_KEY"}
l!l&&r!l&&!r
106
MC/DC50%1/2
($self->data->{"proxy_api_key"} // $ENV{"KNARR_API_KEY"}) // undef
$self->data->{"proxy_api_key"} $ENV{"KNARR_API_KEY"}
Truth table66%2/3
A // B
A= $self->data->{"proxy_api_key"}B= $ENV{"KNARR_API_KEY"}
ABresult
1X1
011
000
290 }
291  
292 12 ▶sub has_proxy_api_key {
Subroutine has_proxy_api_key: called CC=1 SCAR=0.0
Pod documented
293 12 my ($self) = @_;
294 12 return defined $self->proxy_api_key;
295 }
296  
297 68 has public_url => (
298 is => 'lazy',
299 builder => '_build_public_url',
300 );
301  
302  
303 6 ▶sub _build_public_url {
Subroutine _build_public_url: called CC=3 SCAR=11.1
304 6 my ($self) = @_;
305 6 ▶ return $self->data->{public_url} // $ENV{KNARR_PUBLIC_URL} // undef;
Condition80%4/5
($self->data->{"public_url"} // $ENV{"KNARR_PUBLIC_URL"}) // undef
l!l
15
$self->data->{"public_url"} // $ENV{"KNARR_PUBLIC_URL"}
l!l&&r!l&&!r
105
MC/DC50%1/2
($self->data->{"public_url"} // $ENV{"KNARR_PUBLIC_URL"}) // undef
$self->data->{"public_url"} $ENV{"KNARR_PUBLIC_URL"}
Truth table66%2/3
A // B
A= $self->data->{"public_url"}B= $ENV{"KNARR_PUBLIC_URL"}
ABresult
1X1
011
000
306 }
307  
308  
309 68 has ollama_compat_version => (
310 is => 'lazy',
311 builder => '_build_ollama_compat_version',
312 );
313  
314  
315 45 ▶sub _build_ollama_compat_version {
Subroutine _build_ollama_compat_version: called CC=4 SCAR=13.9
316 45 my ($self) = @_;
317 45 ▶ my $version = $self->data->{ollama_compat_version} // $ENV{KNARR_OLLAMA_COMPAT_VERSION};
Condition100%3/3
$self->data->{"ollama_compat_version"} // $ENV{"KNARR_OLLAMA_COMPAT_VERSION"}
l!l&&r!l&&!r
14229
MC/DC100%2/2
$self->data->{"ollama_compat_version"} // $ENV{"KNARR_OLLAMA_COMPAT_VERSION"}
$self->data->{"ollama_compat_version"} $ENV{"KNARR_OLLAMA_COMPAT_VERSION"}
Truth table100%3/3
A // B
A= $self->data->{"ollama_compat_version"}B= $ENV{"KNARR_OLLAMA_COMPAT_VERSION"}
ABresult
1X1
011
000
318 45 ▶ return undef unless defined $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 29 16 45
319 # Open WebUI int()s every dotted part (k28): digits and dots only.
320 16 ▶ croak "ollama_compat_version '$version' must be three dot-separated numbers"
Branch: 1 branch
BranchTrueFalseTotal
unless $version =~ /\A\d+\.\d+\.\d+\z/ 13 3 16
321 . " like 0.34.4 (Ollama clients parse each part as an integer)"
322 unless $version =~ /\A\d+\.\d+\.\d+\z/;
323 3 return $version;
324 }
325  
326 68 has a2a_name => (
327 is => 'lazy',
328 builder => '_build_a2a_name',
329 );
330  
331  
332 12 ▶sub _build_a2a_name {
Subroutine _build_a2a_name: called CC=4 SCAR=13.9
333 12 my ($self) = @_;
334 12 ▶ return ( $self->_section('a2a') // {} )->{name} // _strip_quotes($ENV{KNARR_A2A_NAME}) // undef;
Condition100%7/7
(+($self->_section("a2a") // {})->{"name"} // _strip_quotes($ENV{"KNARR_A2A_NAME"})) // undef
l!l
46
+($self->_section("a2a") // {})->{"name"} // _strip_quotes($ENV{"KNARR_A2A_NAME"})
l!l&&r!l&&!r
316
$self->_section("a2a") // {}
l!l
37
MC/DC100%2/2
(+($self->_section("a2a") // {})->{"name"} // _strip_quotes($ENV{"KNARR_A2A_NAME"})) // undef
$self->_section("a2a") _strip_quotes($ENV{"KNARR_A2A_NAME"})
Truth table100%3/3
A // B
A= $self->_section("a2a")B= _strip_quotes($ENV{"KNARR_A2A_NAME"})
ABresult
1X1
011
000
335 }
336  
337 68 has a2a_description => (
338 is => 'lazy',
339 builder => '_build_a2a_description',
340 );
341  
342  
343 9 ▶sub _build_a2a_description {
Subroutine _build_a2a_description: called CC=4 SCAR=13.9
344 9 my ($self) = @_;
345 9 ▶ return ( $self->_section('a2a') // {} )->{description} // _strip_quotes($ENV{KNARR_A2A_DESCRIPTION}) // undef;
Condition100%7/7
(+($self->_section("a2a") // {})->{"description"} // _strip_quotes($ENV{"KNARR_A2A_DESCRIPTION"})) // undef
l!l
26
+($self->_section("a2a") // {})->{"description"} // _strip_quotes($ENV{"KNARR_A2A_DESCRIPTION"})
l!l&&r!l&&!r
116
$self->_section("a2a") // {}
l!l
17
MC/DC100%2/2
(+($self->_section("a2a") // {})->{"description"} // _strip_quotes($ENV{"KNARR_A2A_DESCRIPTION"})) // undef
$self->_section("a2a") _strip_quotes($ENV{"KNARR_A2A_DESCRIPTION"})
Truth table100%3/3
A // B
A= $self->_section("a2a")B= _strip_quotes($ENV{"KNARR_A2A_DESCRIPTION"})
ABresult
1X1
011
000
346 }
347  
348 9 ▶sub protocol_args {
Subroutine protocol_args: called CC=3 SCAR=11.0
Pod documented
349 9 my ($self) = @_;
350 9 ▶ return { A2A => {
Branch: 2 branches
BranchTrueFalseTotal
defined $self->a2a_name ? : 2 6 8
defined $self->a2a_description ? : 2 6 8
351 ( defined $self->a2a_name ? ( agent_name => $self->a2a_name ) : () ),
352 ( defined $self->a2a_description ? ( agent_description => $self->a2a_description ) : () ),
353 } };
354 }
355  
356  
357 68 has upstream_timeout => (
358 is => 'lazy',
359 builder => '_build_upstream_timeout',
360 );
361  
362  
363 105 ▶sub _build_upstream_timeout {
Subroutine _build_upstream_timeout: called CC=2 SCAR=6.9
364 105 my ($self) = @_;
365 return _seconds( upstream_timeout =>
366 105 ▶ $self->data->{upstream_timeout} // _strip_quotes($ENV{KNARR_UPSTREAM_TIMEOUT}), 300 );
Condition100%3/3
$self->data->{"upstream_timeout"} // _strip_quotes($ENV{"KNARR_UPSTREAM_TIMEOUT"})
l!l&&r!l&&!r
9195
MC/DC100%2/2
$self->data->{"upstream_timeout"} // _strip_quotes($ENV{"KNARR_UPSTREAM_TIMEOUT"})
$self->data->{"upstream_timeout"} _strip_quotes($ENV{"KNARR_UPSTREAM_TIMEOUT"})
Truth table100%3/3
A // B
A= $self->data->{"upstream_timeout"}B= _strip_quotes($ENV{"KNARR_UPSTREAM_TIMEOUT"})
ABresult
1X1
011
000
367 }
368  
369 68 has upstream_stall_timeout => (
370 is => 'lazy',
371 builder => '_build_upstream_stall_timeout',
372 );
373  
374  
375 38 ▶sub _build_upstream_stall_timeout {
Subroutine _build_upstream_stall_timeout: called CC=2 SCAR=6.9
376 38 my ($self) = @_;
377 return _seconds( upstream_stall_timeout =>
378 38 ▶ $self->data->{upstream_stall_timeout} // _strip_quotes($ENV{KNARR_UPSTREAM_STALL_TIMEOUT}), 120 );
Condition100%3/3
$self->data->{"upstream_stall_timeout"} // _strip_quotes($ENV{"KNARR_UPSTREAM_STALL_TIMEOUT"})
l!l&&r!l&&!r
1235
MC/DC100%2/2
$self->data->{"upstream_stall_timeout"} // _strip_quotes($ENV{"KNARR_UPSTREAM_STALL_TIMEOUT"})
$self->data->{"upstream_stall_timeout"} _strip_quotes($ENV{"KNARR_UPSTREAM_STALL_TIMEOUT"})
Truth table100%3/3
A // B
A= $self->data->{"upstream_stall_timeout"}B= _strip_quotes($ENV{"KNARR_UPSTREAM_STALL_TIMEOUT"})
ABresult
1X1
011
000
379 }
380  
381 68 has probe_capabilities => (
382 is => 'lazy',
383 builder => '_build_probe_capabilities',
384 );
385  
386  
387 27 ▶sub _build_probe_capabilities {
Subroutine _build_probe_capabilities: called CC=6 SCAR=19.9
388 27 my ($self) = @_;
389 27 my $value = $self->data->{probe_capabilities};
390 27 ▶ unless ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 21 6 27
391 21 $value = _strip_quotes($ENV{KNARR_PROBE_CAPABILITIES});
392 21 ▶ return 1 unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 21 0 21
Condition33%1/3
defined $value and length $value
!ll&&!rl&&r
2100
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table33%1/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
393 }
394 6 ▶ return !$value || $value =~ /\A(?:false|no|off)\z/i ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
!$value || $value =~ /\A(?:false|no|off)\z/i ? : 6 0 6
Condition33%1/3
not $value or $value =~ /\A(?:false|no|off)\z/i
l!l&&r!l&&!r
600
MC/DC0%0/2
not $value or $value =~ /\A(?:false|no|off)\z/i
not $value $value =~ /\A(?:false|no|off)\z/i
Truth table33%1/3
not(A) or B
A= not $valueB= $value =~ /\A(?:false|no|off)\z/i
ABresult
1X1
011
000
395 }
396  
397 68 has probe_timeout => (
398 is => 'lazy',
399 builder => '_build_probe_timeout',
400 );
401  
402  
403 56 ▶sub _build_probe_timeout {
Subroutine _build_probe_timeout: called CC=2 SCAR=7.1
404 56 my ($self) = @_;
405 return _seconds( probe_timeout =>
406 56 ▶ $self->data->{probe_timeout} // _strip_quotes($ENV{KNARR_PROBE_TIMEOUT}), 10 );
Condition66%2/3
$self->data->{"probe_timeout"} // _strip_quotes($ENV{"KNARR_PROBE_TIMEOUT"})
l!l&&r!l&&!r
2054
MC/DC50%1/2
$self->data->{"probe_timeout"} // _strip_quotes($ENV{"KNARR_PROBE_TIMEOUT"})
$self->data->{"probe_timeout"} _strip_quotes($ENV{"KNARR_PROBE_TIMEOUT"})
Truth table66%2/3
A // B
A= $self->data->{"probe_timeout"}B= _strip_quotes($ENV{"KNARR_PROBE_TIMEOUT"})
ABresult
1X1
011
000
407 }
408  
409 68 has workers => (
410 is => 'lazy',
411 builder => '_build_workers',
412 );
413  
414  
415 45 ▶sub _build_workers {
Subroutine _build_workers: called CC=5 SCAR=16.1
416 45 my ($self) = @_;
417 45 ▶ my $value = $self->data->{workers} // _strip_quotes($ENV{KNARR_WORKERS});
Condition100%3/3
$self->data->{"workers"} // _strip_quotes($ENV{"KNARR_WORKERS"})
l!l&&r!l&&!r
13527
MC/DC100%2/2
$self->data->{"workers"} // _strip_quotes($ENV{"KNARR_WORKERS"})
$self->data->{"workers"} _strip_quotes($ENV{"KNARR_WORKERS"})
Truth table100%3/3
A // B
A= $self->data->{"workers"}B= _strip_quotes($ENV{"KNARR_WORKERS"})
ABresult
1X1
011
000
418 45 ▶ return 1 unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 27 18 45
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
27018
MC/DC50%1/2
defined $value and length $value
defined $value length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
419 18 ▶ croak "workers '$value' must be a whole number of 1 or more"
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A[1-9][0-9]*\z/ 10 8 18
420 unless $value =~ /\A[1-9][0-9]*\z/;
421 8 return $value + 0;
422 }
423  
424 # A config section (models:, default:, logging:, ...) that is not a mapping
425 # croaks when read (k67), instead of dying later as a HASH dereference;
426 # validate reports it.
427 634 ▶sub _section {
Subroutine _section: called CC=4 SCAR=13.9
428 634 my ($self, $key) = @_;
429 634 my $value = $self->data->{$key};
430 634 ▶ return $value if !defined $value || ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value eq "HASH" 613 21 634
Condition100%3/3
not defined $value or ref $value eq "HASH"
l!l&&r!l&&!r
34227121
MC/DC100%2/2
not defined $value or ref $value eq "HASH"
not defined $value ref $value eq "HASH"
Truth table100%3/3
not(A) or B
A= not defined $valueB= ref $value eq "HASH"
ABresult
1X1
011
000
431 21 ▶ croak "$key must be a mapping of key: value pairs, not "
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 5 16 21
432 . ( ref $value eq 'ARRAY' ? 'a list' : "'".$value."'" );
433 }
434  
435 338 ▶sub _seconds {
Subroutine _seconds: called CC=4 SCAR=13.9
436 338 my ($name, $value, $default) = @_;
437 338 ▶ return $default unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 309 29 338
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
309029
MC/DC50%1/2
defined $value and length $value
defined $value length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
438 29 ▶ croak "$name '$value' must be a number of seconds (0 disables it)"
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/ 5 24 29
439 unless $value =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/;
440 24 return $value + 0;
441 }
442  
443 68 has auto_discover => (
444 is => 'lazy',
445 builder => '_build_auto_discover',
446 );
447  
448  
449 72 ▶sub _build_auto_discover {
Subroutine _build_auto_discover: called CC=2 SCAR=6.9
450 72 my ($self) = @_;
451 72 ▶ return $self->data->{auto_discover} // 0;
Condition100%2/2
$self->data->{"auto_discover"} // 0
l!l
4725
MC/DC100%1/1
$self->data->{"auto_discover"} // 0
$self->data->{"auto_discover"}
Truth table100%2/2
A
A= $self->data->{"auto_discover"}
Aresult
11
00
452 }
453  
454 68 my %PASSTHROUGH_DEFAULTS = (
455 anthropic => 'https://api.anthropic.com',
456 openai => 'https://api.openai.com',
457 );
458  
459 68 has passthrough => (
460 is => 'lazy',
461 builder => '_build_passthrough',
462 );
463  
464  
465 94 ▶sub _build_passthrough {
Subroutine _build_passthrough: called CC=10 SCAR=23.1
466 94 my ($self) = @_;
467 94 my $raw = $self->data->{passthrough};
468 94 ▶ return {} unless defined $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw 64 30 94
469  
470 # passthrough: true → enable all with default URLs
471 30 ▶ if (!ref $raw) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $raw) 4 26 30
472 4 ▶ return $raw ? { %PASSTHROUGH_DEFAULTS } : {};
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 3 1 4
473 }
474  
475 26 ▶ croak "passthrough must be true, false or a mapping of format: URL, not a list"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $raw eq "HASH" 2 24 26
476 unless ref $raw eq 'HASH';
477  
478 24 my %result;
479 24 for my $format (keys %$raw) {
480 29 my $val = $raw->{$format};
481 29 ▶ next unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 29 29
482 29 ▶ if ($val eq '1' || $val eq 'true') {
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq '1' or $val eq 'true') { } 1 28 29
Condition66%2/3
$val eq 1 or $val eq "true"
l!l&&r!l&&!r
1028
MC/DC50%1/2
$val eq 1 or $val eq "true"
$val eq 1 $val eq "true"
Truth table66%2/3
A or B
A= $val eq 1B= $val eq "true"
ABresult
1X1
011
000
483 1 ▶ $result{$format} = $PASSTHROUGH_DEFAULTS{$format} // next;
Condition50%1/2
$PASSTHROUGH_DEFAULTS{$format} // (next)
l!l
10
MC/DC0%0/1
$PASSTHROUGH_DEFAULTS{$format} // (next)
$PASSTHROUGH_DEFAULTS{$format}
Truth table50%1/2
A
A= $PASSTHROUGH_DEFAULTS{$format}
Aresult
11
00
484 } else {
485 # Custom URL
486 28 $result{$format} = $val;
487 }
488 }
489 24 return \%result;
490 }
491  
492 8 ▶sub passthrough_url_for {
Subroutine passthrough_url_for: called CC=1 SCAR=0.0
Pod documented
493 8 my ($self, $format) = @_;
494 8 return $self->passthrough->{$format};
495 }
496  
497  
498  
499 52 ▶sub validate {
Subroutine validate: called CC=23 SCAR=31.5
Pod documented
500 52 my ($self) = @_;
501 52 my @errors;
502  
503 # A file that does not load leaves nothing else to check (k67)
504 52 ▶ return $self->_error_text($@) unless eval { $self->data; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->data; 1 } } 3 49 52
505  
506 # A section that is not a mapping croaks when read (k67)
507 49 for my $key (qw( models default langfuse logging a2a )) {
508 245 ▶ push @errors, $self->_error_text($@) unless eval { $self->_section($key); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->_section($key); 1 } } 12 233 245
509 }
510 49 for my $attr (qw( passthrough listen )) {
511 98 ▶ push @errors, $self->_error_text($@) unless eval { $self->$attr; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->$attr; 1 } } 4 94 98
512 }
513 49 ▶ return @errors if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 13 36 49
514  
515 36 my $models = $self->models;
516 36 for my $name (keys %$models) {
517 42 my $def = $models->{$name};
518 42 ▶ if ( defined $def && ref $def ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $def and ref $def ne "HASH") 1 41 42
Condition66%2/3
defined $def and ref $def ne "HASH"
!ll&&!rl&&r
0411
MC/DC50%1/2
defined $def and ref $def ne "HASH"
defined $def ref $def ne "HASH"
Truth table66%2/3
A and B
A= defined $defB= ref $def ne "HASH"
ABresult
0X0
100
111
519 1 push @errors, "Model '$name' must be a mapping of key: value pairs";
520 1 next;
521 }
522 41 ▶ unless ($def->{engine}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($def->{"engine"}) 1 40 41
523 1 push @errors, "Model '$name': missing 'engine' key";
524 }
525 41 ▶ if ( defined $def->{context_size} && $def->{context_size} !~ /\A[1-9][0-9]*\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $def->{"context_size"} and not $def->{"context_size"} =~ /\A[1-9][0-9]*\z/) 1 40 41
Condition100%3/3
defined $def->{"context_size"} and not $def->{"context_size"} =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
3641
MC/DC100%2/2
defined $def->{"context_size"} and not $def->{"context_size"} =~ /\A[1-9][0-9]*\z/
defined $def->{"context_size"} not $def->{"context_size"} =~ /\A[1-9][0-9]*\z/
Truth table100%3/3
A and not(B)
A= defined $def->{"context_size"}B= not $def->{"context_size"} =~ /\A[1-9][0-9]*\z/
ABresult
0X0
100
111
526 1 push @errors, "Model '$name': context_size must be a positive integer";
527 }
528 41 ▶ unless ( eval { _seconds( user_agent_timeout => $def->{user_agent_timeout}, 0 ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { _seconds("user_agent_timeout", $def->{"user_agent_timeout"}, 0); 1 } }) 1 40 41
529 1 push @errors, "Model '$name': user_agent_timeout must be a number of seconds";
530 }
531 }
532  
533 36 ▶ if (my $default = $self->default_engine) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $default = $self->default_engine) 3 33 36
534 3 ▶ unless ($default->{engine}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($default->{"engine"}) 0 3 3
535 0 push @errors, "Default: missing 'engine' key";
536 }
537 3 ▶ if ( defined $default->{context_size} && $default->{context_size} !~ /\A[1-9][0-9]*\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default->{"context_size"} and not $default->{"context_size"} =~ /\A[1-9][0-9]*\z/) 1 2 3
Condition66%2/3
defined $default->{"context_size"} and not $default->{"context_size"} =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
021
MC/DC50%1/2
defined $default->{"context_size"} and not $default->{"context_size"} =~ /\A[1-9][0-9]*\z/
defined $default->{"context_size"} not $default->{"context_size"} =~ /\A[1-9][0-9]*\z/
Truth table66%2/3
A and not(B)
A= defined $default->{"context_size"}B= not $default->{"context_size"} =~ /\A[1-9][0-9]*\z/
ABresult
0X0
100
111
538 1 push @errors, "Default: context_size must be a positive integer";
539 }
540 3 ▶ unless ( eval { _seconds( user_agent_timeout => $default->{user_agent_timeout}, 0 ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { _seconds("user_agent_timeout", $default->{"user_agent_timeout"}, 0); 1 } }) 0 3 3
541 0 push @errors, "Default: user_agent_timeout must be a number of seconds";
542 }
543 }
544  
545 36 ▶ unless (keys %$models || $self->default_engine) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$models or $self->default_engine) 2 34 36
Condition66%2/3
keys %$models or $self->default_engine
l!l&&r!l&&!r
3402
MC/DC50%1/2
keys %$models or $self->default_engine
keys %$models $self->default_engine
Truth table66%2/3
A or B
A= keys %$modelsB= $self->default_engine
ABresult
1X1
011
000
546 2 push @errors, "No models configured and no default engine set";
547 }
548  
549 36 for my $attr (qw( ollama_compat_version upstream_timeout upstream_stall_timeout probe_timeout workers langfuse_transport langfuse_timeout )) {
550 252 ▶ next if eval { $self->$attr; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $self->$attr; 1 } } 238 14 252
551 14 push @errors, $self->_error_text($@);
552 }
553  
554 36 return @errors;
555 }
556  
557 # A croak as a validation error: without the trailing "at FILE line N."
558 34 ▶sub _error_text {
Subroutine _error_text: called CC=1 SCAR=0.0
559 34 my ($self, $err) = @_;
560 34 $err =~ s/\s+at \S+ line \d+\.?\s*\z//;
561 34 return $err;
562 }
563  
564 2 ▶sub engine_definitions {
Subroutine engine_definitions: called CC=2 SCAR=6.9
Pod undocumented
565 2 my ($self) = @_;
566 2 my %defs;
567 2 my $models = $self->models;
568 1 for my $name (keys %$models) {
569 2 $defs{$name} = { %{$models->{$name}}, name => $name };
570 }
571 1 return \%defs;
572 }
573  
574  
575 # Scan environment and .env files for API keys, return model config suggestions
576 17 ▶sub scan_env {
Subroutine scan_env: called CC=15 SCAR=27.6
Pod documented
577 17 my ($class, %opts) = @_;
578 17 ▶ my @env_files = @{$opts{env_files} // []};
Condition100%2/2
$opts{"env_files"} // []
l!l
215
MC/DC100%1/1
$opts{"env_files"} // []
$opts{"env_files"}
Truth table100%2/2
A
A= $opts{"env_files"}
Aresult
11
00
579 17 my %env = %ENV;
580  
581 # Load .env files
582 17 for my $file (@env_files) {
583 1 ▶ next unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 1 1
584 1 ▶ open my $fh, '<', $file or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or next 1 0 1
585 1 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 3 1 4
586 3 chomp;
587 3 ▶ next if /^\s*#/ || /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*#/ or /^\s*$/ 1 2 3
Condition66%2/3
/^\s*#/ or /^\s*$/
l!l&&r!l&&!r
102
MC/DC50%1/2
/^\s*#/ or /^\s*$/
/^\s*#/ /^\s*$/
Truth table66%2/3
A or B
A= /^\s*#/B= /^\s*$/
ABresult
1X1
011
000
588 2 ▶ if (/^\s*(?:export\s+)?(\w+)\s*=\s*['"]?(.*?)['"]?\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*(?:export\s+)?(\w+)\s*=\s*['"]?(.*?)['"]?\s*$/) 2 0 2
589 2 $env{$1} = $2;
590 }
591 }
592 1 close $fh;
593 }
594  
595 17 ▶ my $include_test = $opts{include_test} // 1;
Condition100%2/2
$opts{"include_test"} // 1
l!l
215
MC/DC100%1/1
$opts{"include_test"} // 1
$opts{"include_test"}
Truth table100%2/2
A
A= $opts{"include_test"}
Aresult
11
00
596  
597 17 my %found;
598 17 for my $def (@ENGINE_DEFS) {
599 323 for my $var (@{$def->{vars}}) {
600 853 ▶ next unless $env{$var};
Branch: 1 branch
BranchTrueFalseTotal
unless $env{$var} 799 54 853
601 54 ▶ next if !$include_test && $var =~ /^TEST_/;
Branch: 1 branch
BranchTrueFalseTotal
if not $include_test and $var =~ /^TEST_/ 0 54 54
Condition33%1/3
not $include_test and $var =~ /^TEST_/
!ll&&!rl&&r
5400
MC/DC0%0/2
not $include_test and $var =~ /^TEST_/
not $include_test $var =~ /^TEST_/
Truth table33%1/3
not(A) and B
A= not $include_testB= $var =~ /^TEST_/
ABresult
0X0
100
111
602 $found{$def->{engine}} = {
603 engine => $def->{engine},
604 54 api_key_env => $var,
605 };
606 54 last; # first match wins (priority order)
607 }
608 }
609  
610 17 return \%found;
611 }
612  
613  
614 # Generate a YAML config string from scan results
615 7 ▶sub generate_config {
Subroutine generate_config: called CC=14 SCAR=27.2
Pod documented
616 7 my ($class, %opts) = @_;
617 7 ▶ my $found = $opts{engines} // {};
Condition50%1/2
$opts{"engines"} // {}
l!l
70
MC/DC0%0/1
$opts{"engines"} // {}
$opts{"engines"}
Truth table50%1/2
A
A= $opts{"engines"}
Aresult
11
00
618 7 ▶ my $listen = $opts{listen} // ['127.0.0.1:8080', '127.0.0.1:11434'];
Condition100%2/2
$opts{"listen"} // ["127.0.0.1:8080", "127.0.0.1:11434"]
l!l
16
MC/DC100%1/1
$opts{"listen"} // ["127.0.0.1:8080", "127.0.0.1:11434"]
$opts{"listen"}
Truth table100%2/2
A
A= $opts{"listen"}
Aresult
11
00
619 7 ▶ $listen = [$listen] unless ref $listen eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $listen eq "ARRAY" 0 7 7
620  
621 7 my @lines;
622 7 push @lines, "# Knarr configuration - auto-generated";
623 7 push @lines, "listen:";
624 7 for my $addr (@$listen) {
625 14 push @lines, " - \"$addr\"";
626 }
627 7 push @lines, "";
628 7 push @lines, "models:";
629  
630 7 for my $engine (sort keys %$found) {
631 26 my $info = $found->{$engine};
632 26 my $model = $class->default_model_for($engine);
633 26 my $name = lc($engine);
634 26 ▶ $name .= "-default" if $name eq 'openai' || $name eq 'anthropic';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "openai" or $name eq "anthropic" 9 17 26
Condition100%3/3
$name eq "openai" or $name eq "anthropic"
l!l&&r!l&&!r
6317
MC/DC100%2/2
$name eq "openai" or $name eq "anthropic"
$name eq "openai" $name eq "anthropic"
Truth table100%3/3
A or B
A= $name eq "openai"B= $name eq "anthropic"
ABresult
1X1
011
000
635 26 push @lines, " $name:";
636 26 push @lines, " engine: $engine";
637 26 ▶ push @lines, " model: $model" if $model;
Branch: 1 branch
BranchTrueFalseTotal
if $model 24 2 26
638 26 ▶ push @lines, " api_key_env: $info->{api_key_env}" if $info->{api_key_env};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"api_key_env"} 26 0 26
639 26 push @lines, "";
640 }
641  
642 7 ▶ unless (keys %$found) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$found) 0 7 7
643 0 ▶ my $example = $class->default_model_for('OpenAI') // 'gpt-4o-mini';
Condition0%0/2
$class->default_model_for("OpenAI") // "gpt-4o-mini"
l!l
00
MC/DC0%0/1
$class->default_model_for("OpenAI") // "gpt-4o-mini"
$class->default_model_for("OpenAI")
Truth table0%0/2
A
A= $class->default_model_for("OpenAI")
Aresult
11
00
644 0 push @lines, " # No API keys found. Add your models here:";
645 0 push @lines, " # my-model:";
646 0 push @lines, " # engine: OpenAI";
647 0 push @lines, " # model: $example";
648 0 push @lines, "";
649 }
650  
651 7 push @lines, "# Default engine for models without explicit config (optional)";
652 7 ▶ if ($found->{OpenAI}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found->{'OpenAI'}) { } 6 1 7
653 6 push @lines, "default:";
654 6 push @lines, " engine: OpenAI";
655 6 ▶ push @lines, " api_key_env: $found->{OpenAI}{api_key_env}" if $found->{OpenAI}{api_key_env};
Branch: 1 branch
BranchTrueFalseTotal
if $found->{"OpenAI"}{"api_key_env"} 6 0 6
656 } else {
657 1 push @lines, "# default:";
658 1 push @lines, "# engine: OpenAI";
659 }
660 7 push @lines, "";
661  
662 7 push @lines, "# Auto-discover models from configured engines";
663 7 push @lines, "auto_discover: true";
664 7 push @lines, "";
665  
666 7 push @lines, "# Optional: proxy authentication";
667 7 push @lines, "# proxy_api_key: your-secret-key";
668 7 push @lines, "";
669  
670 7 push @lines, "# Optional: Langfuse tracing (or set env vars)";
671 7 push @lines, "# langfuse:";
672 7 push @lines, "# url: http://localhost:3000";
673 7 push @lines, "# public_key: pk-lf-...";
674 7 push @lines, "# secret_key: sk-lf-...";
675 7 push @lines, "# transport: otel # default ingestion; otel needs Langfuse Cloud or v3.22+";
676 7 push @lines, "# timeout: 15 # seconds per trace POST, 0 disables";
677  
678 7 return join("\n", @lines) . "\n";
679 }
680  
681 # Strip surrounding quotes from env values (Docker --env-file includes them literally)
682 432 ▶sub _strip_quotes {
Subroutine _strip_quotes: called CC=2 SCAR=6.9
683 432 my $v = shift;
684 432 ▶ return $v unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 420 12 432
685 12 $v =~ s/^["']|["']$//g;
686 12 return $v;
687 }
688  
689  
690 68 1;
691  
692 __END__
693  
694 =pod
695  
696 =encoding UTF-8
697  
698 =head1 NAME
699  
700 Langertha::Knarr::Config - YAML configuration loader and validator
701  
702 =head1 VERSION
703  
704 version 1.102
705  
706 =head1 SYNOPSIS
707  
708 use Langertha::Knarr::Config;
709  
710 # Load from file
711 my $config = Langertha::Knarr::Config->new(file => 'knarr.yaml');
712  
713 # Build from environment (Docker zero-config mode)
714 my $config = Langertha::Knarr::Config->from_env;
715  
716 # Validate
717 my @errors = $config->validate;
718 die join("\n", @errors) if @errors;
719  
720 =head1 DESCRIPTION
721  
722 Loads and validates Knarr configuration from a YAML file or from environment
723 variables. All string values in the YAML file support C<${ENV_VAR}>
724 interpolation.
725  
726 The attributes below are the configuration file reference: each one
727 names its YAML key and, where there is one, its environment variable. The
728 README has an annotated example, and C<share/example-config.yaml> in the
729 distribution is a commented starting point.
730  
731 =head2 file
732  
733 Path to the YAML configuration file. Optional — when omitted the config
734 object starts with an empty data hash (useful for testing or for
735 configs built purely from environment variables via L</from_env>).
736  
737 =head2 data
738  
739 The raw configuration hashref, loaded from L</file> and with all
740 C<${ENV_VAR}> references expanded. Can be supplied directly to bypass
741 file loading.
742  
743 =head2 engine_catalog
744  
745 my $defs = Langertha::Knarr::Config->engine_catalog;
746 for my $def (@$defs) { say $def->{engine}, ": @{$def->{vars}}" }
747  
748 Class method. Returns an ArrayRef of C<{engine, vars}> hashrefs describing
749 every engine L</scan_env> can auto-detect, with its API key environment
750 variable names in priority order. The list is a copy — mutating it does not
751 affect scanning.
752  
753 =head2 default_model_for
754  
755 my $model = Langertha::Knarr::Config->default_model_for('Anthropic');
756  
757 Class method. Returns the default model name for an engine, read from the
758 Langertha engine class itself (C<< Langertha::Engine::Anthropic->default_model >>)
759 so the catalog cannot drift away from the framework. Returns C<undef> when the
760 engine class is not installed, or when it has no default model and no fallback
761 is known. The result is cached per engine name.
762  
763 =head2 from_env
764  
765 my $config = Langertha::Knarr::Config->from_env(%opts);
766  
767 Class method. Builds a config object purely from environment variables
768 (zero-config Docker mode). Calls L</scan_env> to detect which API keys are
769 set, assigns each detected engine its L</default_model_for> model, enables
770 C<auto_discover> and C<passthrough>, and sets OpenAI as the default engine
771 when an OpenAI key is present. Every generated entry, the default engine
772 included, names the variable its key was found in as C<api_key_env>, so
773 C<OPENAI_API_KEY> works as well as C<LANGERTHA_OPENAI_API_KEY>.
774  
775 Options are passed through to L</scan_env> (e.g. C<include_test>).
776  
777 =head2 listen
778  
779 ArrayRef of C<host:port> strings to listen on (C<listen:>, a list or a
780 single string). Defaults to C<['127.0.0.1:8080', '127.0.0.1:11434']>, so
781 a config without C<listen:> only answers on loopback. C<knarr start -p>
782 replaces it with the given ports on C<-H> (default C<0.0.0.0>); the Docker
783 image starts that way.
784  
785 =head2 models
786  
787 HashRef of model name → model definition hashref from the C<models:> config
788 section. Each definition may include C<engine>, C<model>, C<api_key_env>,
789 C<api_key>, C<url>, C<system_prompt>, C<temperature>, C<response_size>,
790 C<context_size>, and C<user_agent_timeout> (seconds the engine waits for
791 its upstream; defaults to L</upstream_timeout>, C<0> disables it).
792  
793 C<context_size> is passed to engines that compose
794 L<Langertha::Role::ContextSize> (Ollama, LMStudio native), which send it on
795 the wire (Ollama's C<num_ctx>, LM Studio's C<context_length>) and report it
796 from C</api/show>. For any other engine it is ignored, with one warning
797 when the models are loaded.
798  
799 Models found by L</auto_discover> inherit the endpoint-level keys of the
800 entry they were discovered through, but not the model-specific C<model> and
801 C<context_size>; see L<Langertha::Knarr::Router/DESCRIPTION> for the split.
802  
803 =head2 default_engine
804  
805 HashRef from the C<default:> config section, or C<undef> if not set. At
806 minimum contains C<engine>. Used as the fallback when a model name is not
807 explicitly configured and no passthrough URL matches -- that is, no
808 L</passthrough> upstream exists for the protocol the client speaks (Ollama
809 without an C<ollama> entry, and A2A, ACP and AG-UI always). Without a
810 default engine such a request is answered with C<404> in the client
811 protocol's error shape.
812  
813 A C<model> in this section is what the default engine uses for a request
814 that names no model (A2A always, ACP without C<agent_name>); a model the
815 client names replaces it. The section takes the same keys as a
816 L</models> entry, C<api_key_env> or C<api_key> included; without either
817 the engine reads only its own C<LANGERTHA_*_API_KEY> variable.
818  
819 =head2 log_file
820  
821 Path to a JSONL log file for request logging. Resolved from
822 C<logging.file> in config or C<KNARR_LOG_FILE> environment variable.
823  
824 =head2 log_dir
825  
826 Path to a directory for per-request JSON log files. Resolved from
827 C<logging.dir> in config or C<KNARR_LOG_DIR> environment variable.
828  
829 =head2 langfuse
830  
831 HashRef from the C<langfuse:> config section. May contain C<url>,
832 C<public_key>, C<secret_key>, C<trace_name>, C<transport> (see
833 L</langfuse_transport>) and C<timeout> (see L</langfuse_timeout>). Returns an empty hashref when the section is
834 absent.
835  
836 =head2 langfuse_transport
837  
838 How L<Langertha::Knarr::Tracing> sends traces to Langfuse: C<ingestion>
839 (the default) posts trace and generation events to Langfuse's
840 C</api/public/ingestion> API; C<otel> exports them as OpenTelemetry spans
841 (OTLP/HTTP, JSON encoded) to C</api/public/otel/v1/traces>, the path
842 Langfuse now recommends (Langfuse Cloud, or self-hosted v3.22 and later;
843 Langfuse v2 has only the ingestion API). See
844 L<Langertha::Knarr::Tracing/OpenTelemetry transport>. Read from C<langfuse.transport>, falling back to
845 the C<KNARR_LANGFUSE_TRANSPORT> environment variable, so it also applies
846 under C<--from-env>. Case does not matter. Any other value croaks when
847 read, and L</validate> reports it.
848  
849 =head2 langfuse_timeout
850  
851 Seconds a trace POST to Langfuse may take before it is given up and logged
852 as a C<Langfuse flush error> warning, for either
853 L</langfuse_transport>. Default C<15>; C<0> disables it. The POST never
854 holds a request, so this only decides when a slow Langfuse is reported as
855 failed. Read from C<langfuse.timeout>, falling back to the
856 C<KNARR_LANGFUSE_TIMEOUT> environment variable. A value that is not a
857 non-negative number croaks when read, and L</validate> reports it.
858  
859 =head2 proxy_api_key
860  
861 Optional shared secret that clients must present in the C<Authorization: Bearer>
862 or C<x-api-key> header. Falls back to the C<KNARR_API_KEY> environment variable.
863 When not set, the proxy is open (no auth required). It is never forwarded to a
864 passthrough upstream; see L<Langertha::Knarr/auth_token>.
865  
866 =head2 public_url
867  
868 Optional public base URL of this Knarr (e.g. C<https://knarr.example>),
869 published in the provider manifest at C</.well-known/langertha.json>. Falls
870 back to the C<KNARR_PUBLIC_URL> environment variable. When not set, the
871 manifest takes the base URL from each request.
872  
873 =head2 has_proxy_api_key
874  
875 if ($config->has_proxy_api_key) { ... }
876  
877 Returns true when a L</proxy_api_key> is configured.
878  
879 =head2 ollama_compat_version
880  
881 Optional Ollama version to report at C<GET /api/version> (see
882 L<Langertha::Knarr/ollama_compat_version>). Falls back to the
883 C<KNARR_OLLAMA_COMPAT_VERSION> environment variable. When not set, Knarr's
884 default applies. A value that is not three dot-separated numbers
885 (C<0.34.4>) croaks when read, and L</validate> reports it: Ollama clients
886 parse each part as an integer.
887  
888 =head2 a2a_name
889  
890 Optional name of the A2A agent card at C</.well-known/agent.json> (see
891 L<Langertha::Knarr::Protocol::A2A/agent_name>). Resolved from C<a2a.name> in
892 config or the C<KNARR_A2A_NAME> environment variable. When not set, the
893 card says C<Langertha Knarr Agent>.
894  
895 =head2 a2a_description
896  
897 Optional description of the A2A agent card (see
898 L<Langertha::Knarr::Protocol::A2A/agent_description>). Resolved from
899 C<a2a.description> in config or the C<KNARR_A2A_DESCRIPTION> environment
900 variable. When not set, Knarr's default description applies.
901  
902 =head2 protocol_args
903  
904 my $knarr = Langertha::Knarr->new( ..., protocol_args => $config->protocol_args );
905  
906 Returns the L<Langertha::Knarr/protocol_args> HashRef for the configured
907 protocol settings: C<< { A2A => { agent_name => ..., agent_description => ... } } >>,
908 with only the keys that are set (L</a2a_name>, L</a2a_description>).
909  
910 =head2 upstream_timeout
911  
912 Seconds an upstream may take to answer a non-streaming request. Default
913 C<300>; C<0> disables it. Falls back to the C<KNARR_UPSTREAM_TIMEOUT>
914 environment variable. It is the total time of a raw passthrough request
915 (answered with C<504> in the client protocol's error shape when it runs
916 out), and it becomes the C<user_agent_timeout> of every routed engine
917 whose model config sets none -- Langertha applies that as the total time
918 of a plain request and as the time without data of a streaming one. A
919 value that is not a non-negative number croaks when read, and
920 L</validate> reports it.
921  
922 =head2 upstream_stall_timeout
923  
924 Seconds a streaming raw passthrough request may go without data from the
925 upstream, the wait for its response headers included. Default C<120>;
926 C<0> disables it. Falls back to the C<KNARR_UPSTREAM_STALL_TIMEOUT>
927 environment variable. A stream that stalls before its headers is answered
928 with C<504>; one that stalls later ends with the protocol's error frame.
929 Croaks when read and is reported by L</validate> like
930 L</upstream_timeout>.
931  
932 =head2 probe_capabilities
933  
934 Boolean, default C<1>. When true, L<Langertha::Knarr/start> asks every routed
935 engine that can read its provider's model metadata (OpenRouter, Mistral,
936 LM Studio, T-Systems, Ollama, llama.cpp; Langertha core's C<probe_model_capabilities_f>)
937 which capabilities its model has, once at startup, after auto-discovery. What
938 it learns (today: whether the model sees images) then shows as C<vision> in
939 C<POST /api/show> and as C<image_input> in the provider manifest. Engines
940 without such metadata and a Langertha too old to probe (0.503) send nothing.
941 A failed probe is logged and changes nothing. Set C<0> (or
942 C<KNARR_PROBE_CAPABILITIES=0>) to never send these requests. See
943 L<Langertha::Knarr::Router/probe_capabilities_f>.
944  
945 =head2 probe_timeout
946  
947 Seconds one capability probe (see L</probe_capabilities>) may take before it
948 is given up and logged. Default C<10>; C<0> leaves only the engine's own
949 C<user_agent_timeout>. Falls back to C<KNARR_PROBE_TIMEOUT>. A value that is
950 not a non-negative number croaks when read, and L</validate> reports it.
951  
952 =head2 workers
953  
954 Number of processes C<knarr start> serves from. Default C<1>: one process,
955 nothing forked. With more, it forks that many workers on the same listen
956 sockets and supervises them (see L<Langertha::Knarr/workers>). Falls back
957 to C<KNARR_WORKERS>, so it also applies under C<--from-env>; C<knarr start
958 -w N> wins over both. A value that is not a whole number of C<1> or more
959 croaks when read, and L</validate> reports it.
960  
961 =head2 auto_discover
962  
963 Boolean. When true, L<Langertha::Knarr::Router> asks each configured
964 endpoint (engine, URL and API key variable) for its model list the first time
965 a model is resolved, making all discovered models available without
966 explicit config entries and listing them (C</v1/models>, C</api/tags>, the
967 manifest). A discovered model is routed through its engine, except when the
968 L</passthrough> upstream of the client's protocol is the endpoint that
969 listed it (same scheme, host, port and path, an engine's trailing C</v1>
970 aside, so a gateway's C</openai> and C</groq> paths are two upstreams) and
971 the request carries the client's own provider key (C<Authorization> for
972 OpenAI, C<x-api-key> or C<Authorization> for Anthropic, not counting the
973 L</proxy_api_key>; Ollama needs none): then it passes through byte for byte
974 like an unknown model, with that key. Without one it is routed through its
975 engine, with the engine's key, and so it is when the upstream refuses that
976 key with C<401> (see L<Langertha::Knarr/raw_passthrough>). A model under L</models> is always routed.
977 Defaults to C<0>; L</from_env> and the C<knarr init> output turn it on.
978  
979 =head2 passthrough
980  
981 HashRef of format name (C<openai>, C<anthropic>, C<ollama>) → upstream
982 base URL. Empty, and passthrough off, unless the config has a
983 C<passthrough:> key; L</from_env> turns it on. C<passthrough: true> in YAML
984 enables C<openai> and C<anthropic> with their default upstream URLs
985 (C<https://api.openai.com> and C<https://api.anthropic.com>); C<ollama>
986 has no default and needs its URL. Per-format URLs can be customised or set
987 to C<false> to disable selectively.
988  
989 A request for a model that is neither configured nor auto-discovered (or
990 auto-discovered from that very upstream, see L</auto_discover>) goes
991 to the upstream of its protocol byte for byte, with the client's own
992 headers and key. A protocol without an upstream sends such a request to
993 the L</default_engine> instead.
994  
995 =head2 passthrough_url_for
996  
997 my $url = $config->passthrough_url_for('openai');
998  
999 Returns the upstream base URL for the given format name (e.g. C<openai> or
1000 C<anthropic>), or C<undef> if passthrough is not configured for that format.
1001  
1002 =head2 validate
1003  
1004 my @errors = $config->validate;
1005  
1006 Validates the configuration and returns a list of error strings. Returns an
1007 empty list when the config is valid. A config file that does not load (not
1008 YAML, or not a mapping) is reported alone, and so are the sections
1009 C<models>, C<default>, C<langfuse>, C<logging>, C<a2a> and C<passthrough>
1010 when one is not a mapping (C<passthrough> may also be C<true> or C<false>),
1011 and C<listen> when it is not a string or a list of strings: reading such a
1012 section croaks. Otherwise it checks that every model entry is a
1013 mapping with an
1014 C<engine> key, that the default engine (if set) has an C<engine> key,
1015 that at least one model or default engine is configured, that a model's
1016 C<context_size> (and the default engine's), when set, is a positive integer, and its
1017 C<user_agent_timeout> a non-negative number, and that
1018 L</ollama_compat_version>, when set, is three dot-separated numbers, and that
1019 L</upstream_timeout>, L</upstream_stall_timeout> and L</probe_timeout> are
1020 non-negative numbers, and that L</workers> is a whole number of C<1> or
1021 more.
1022  
1023 =head2 scan_env
1024  
1025 my $found = Langertha::Knarr::Config->scan_env(
1026 env_files => ['.env', '.env.local'],
1027 include_test => 1,
1028 );
1029  
1030 Class method. Scans C<%ENV> and optional C<.env> files for known API key
1031 environment variables. Returns a HashRef of engine name → C<{engine,
1032 api_key_env}> for every engine whose key was found. See L</engine_catalog>
1033 for the engines and variable names it knows about.
1034  
1035 Priority order per engine: C<LANGERTHA_*_API_KEY> beats the bare vendor key
1036 (e.g. C<OPENAI_API_KEY>), which beats the C<TEST_LANGERTHA_*_API_KEY> variant.
1037  
1038 Options:
1039  
1040 =over
1041  
1042 =item * C<env_files> — ArrayRef of .env file paths to parse (optional)
1043  
1044 =item * C<include_test> — Include C<TEST_*> variables (default: C<1>)
1045  
1046 =back
1047  
1048 =head2 generate_config
1049  
1050 my $yaml = Langertha::Knarr::Config->generate_config(
1051 engines => $found, # from scan_env
1052 listen => ['127.0.0.1:8080', '127.0.0.1:11434'],
1053 );
1054  
1055 Class method. Generates a YAML configuration string from C<scan_env> results.
1056 The generated YAML includes sensible defaults for each detected engine and
1057 commented-out stanzas for optional features. Used by C<knarr init>.
1058  
1059 Returns a string.
1060  
1061 =head1 SEE ALSO
1062  
1063 =over
1064  
1065 =item * L<Langertha::Knarr> — Main documentation
1066  
1067 =item * L<Langertha::Knarr::Router> — Uses config to resolve models to engines
1068  
1069 =item * L<Langertha::Knarr::Tracing> — Uses config for Langfuse credentials
1070  
1071 =item * L<Langertha::Knarr::RequestLog> — Uses config for request logging paths
1072  
1073 =back
1074  
1075 =head1 SUPPORT
1076  
1077 =head2 Issues
1078  
1079 Please report bugs and feature requests on GitHub at
1080 L<https://github.com/Getty/langertha-knarr/issues>.
1081  
1082 =head2 IRC
1083  
1084 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
1085  
1086 =head1 CONTRIBUTING
1087  
1088 Contributions are welcome! Please fork the repository and submit a pull request.
1089  
1090 =head1 AUTHOR
1091  
1092 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
1093  
1094 =head1 COPYRIGHT AND LICENSE
1095  
1096 This software is copyright (c) 2026 by Torsten Raudssus.
1097  
1098 This is free software; you can redistribute it and/or modify it under
1099 the same terms as the Perl 5 programming language system itself.
1100  
1101 =cut
« blib/lib/Langertha/Knarr/PassthroughUsage.pm ↑ Summary blib/lib/Langertha/Knarr/CLI/Cmd/Models.pm »