| 1 | package Langertha::Knarr::Config; | ||||||||||||||||||||||||||||
| 2 | 6 | our $VERSION = '1.100'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: YAML configuration loader and validator | ||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use YAML::PP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Log::Any qw( $log ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 6 | has file => ( | |||||||||||||||||||||||||||
| 11 | is => 'ro', | ||||||||||||||||||||||||||||
| 12 | predicate => 'has_file', | ||||||||||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 6 | has data => ( | |||||||||||||||||||||||||||
| 17 | is => 'lazy', | ||||||||||||||||||||||||||||
| 18 | builder => '_build_data', | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub _build_data { | ||||||||||||||||||||||||||||
| 23 | 21 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_data: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 24 | 21 | ▶ | return {} unless $self->has_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 19 | my $file = $self->file; | |||||||||||||||||||||||||||
| 26 | 19 | ▶ | croak "Config file not found: $file" unless -f $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 19 | my $ypp = YAML::PP->new; | |||||||||||||||||||||||||||
| 28 | 19 | my $data = $ypp->load_file($file); | |||||||||||||||||||||||||||
| 29 | 19 | _interpolate_env($data); | |||||||||||||||||||||||||||
| 30 | 19 | $log->debugf("Loaded config from %s", $file); | |||||||||||||||||||||||||||
| 31 | 19 | return $data; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Recursively interpolate ${ENV_VAR} in string values | ||||||||||||||||||||||||||||
| 35 | sub _interpolate_env { | ||||||||||||||||||||||||||||
| 36 | 67 | ▶ | my ($ref) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_interpolate_env: called CC=11 SCAR=26.3
| |||||||||||||||||||||||||||||
| 37 | 67 | ▶ | if (ref $ref eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 38 | 63 | for my $key (keys %$ref) { | |||||||||||||||||||||||||||
| 39 | 103 | ▶ | if (ref $ref->{$key}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 40 | 48 | _interpolate_env($ref->{$key}); | |||||||||||||||||||||||||||
| 41 | } elsif (defined $ref->{$key}) { | ||||||||||||||||||||||||||||
| 42 | 55 | ▶ | $ref->{$key} =~ s/\$\{(\w+)\}/$ENV{$1} \/\/ ''/ge; | ||||||||||||||||||||||||||
|
Condition100%2/2
$ENV{$1} // ""
MC/DC100%1/1
$ENV{$1} // ""
$ENV{$1}
Truth table100%2/2
A
A= $ENV{$1}
| |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } elsif (ref $ref eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 46 | 4 | for my $i (0..$#$ref) { | |||||||||||||||||||||||||||
| 47 | 6 | ▶ | if (ref $ref->[$i]) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 48 | 0 | _interpolate_env($ref->[$i]); | |||||||||||||||||||||||||||
| 49 | } elsif (defined $ref->[$i]) { | ||||||||||||||||||||||||||||
| 50 | 6 | ▶ | $ref->[$i] =~ s/\$\{(\w+)\}/$ENV{$1} \/\/ ''/ge; | ||||||||||||||||||||||||||
|
Condition0%0/2
$ENV{$1} // ""
MC/DC0%0/1
$ENV{$1} // ""
$ENV{$1}
Truth table0%0/2
A
A= $ENV{$1}
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Build config purely from environment variables (zero-config Docker mode) | ||||||||||||||||||||||||||||
| 58 | sub from_env { | ||||||||||||||||||||||||||||
| 59 | 2 | ▶ | my ($class, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_env: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 2 | my $found = $class->scan_env(%opts); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | my %default_models = ( | |||||||||||||||||||||||||||
| 63 | OpenAI => 'gpt-4o-mini', | ||||||||||||||||||||||||||||
| 64 | Anthropic => 'claude-sonnet-4-6', | ||||||||||||||||||||||||||||
| 65 | Groq => 'llama-3.3-70b-versatile', | ||||||||||||||||||||||||||||
| 66 | Mistral => 'mistral-large-latest', | ||||||||||||||||||||||||||||
| 67 | DeepSeek => 'deepseek-chat', | ||||||||||||||||||||||||||||
| 68 | MiniMax => 'MiniMax-M2.1', | ||||||||||||||||||||||||||||
| 69 | Cerebras => 'llama-3.3-70b', | ||||||||||||||||||||||||||||
| 70 | OpenRouter => 'openai/gpt-4o-mini', | ||||||||||||||||||||||||||||
| 71 | Perplexity => 'sonar', | ||||||||||||||||||||||||||||
| 72 | Gemini => 'gemini-2.0-flash', | ||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 2 | my %models; | |||||||||||||||||||||||||||
| 76 | 2 | for my $engine (keys %$found) { | |||||||||||||||||||||||||||
| 77 | 3 | my $name = lc($engine); | |||||||||||||||||||||||||||
| 78 | $models{$name} = { | ||||||||||||||||||||||||||||
| 79 | engine => $engine, | ||||||||||||||||||||||||||||
| 80 | model => $default_models{$engine}, | ||||||||||||||||||||||||||||
| 81 | api_key_env => $found->{$engine}{api_key_env}, | ||||||||||||||||||||||||||||
| 82 | 3 | }; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 2 | my %data = ( | |||||||||||||||||||||||||||
| 86 | models => \%models, | ||||||||||||||||||||||||||||
| 87 | auto_discover => 1, | ||||||||||||||||||||||||||||
| 88 | passthrough => 1, | ||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Set default engine if OpenAI found | ||||||||||||||||||||||||||||
| 92 | 2 | ▶ | if ($found->{OpenAI}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 2 | $data{default} = { engine => 'OpenAI' }; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | return $class->new(data => \%data); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 6 | has listen => ( | |||||||||||||||||||||||||||
| 100 | is => 'lazy', | ||||||||||||||||||||||||||||
| 101 | builder => '_build_listen', | ||||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | sub _build_listen { | ||||||||||||||||||||||||||||
| 106 | 4 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_listen: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 107 | 4 | my $raw = $self->data->{listen}; | |||||||||||||||||||||||||||
| 108 | 4 | ▶ | return ['127.0.0.1:8080', '127.0.0.1:11434'] unless defined $raw; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 3 | ▶ | return ref $raw eq 'ARRAY' ? $raw : [$raw]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 6 | has models => ( | |||||||||||||||||||||||||||
| 113 | is => 'lazy', | ||||||||||||||||||||||||||||
| 114 | builder => '_build_models', | ||||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | sub _build_models { | ||||||||||||||||||||||||||||
| 119 | 18 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_models: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 120 | 18 | ▶ | return $self->data->{models} // {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->data->{"models"} // {}
MC/DC100%1/1
$self->data->{"models"} // {}
$self->data->{"models"}
Truth table100%2/2
A
A= $self->data->{"models"}
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 6 | has default_engine => ( | |||||||||||||||||||||||||||
| 124 | is => 'lazy', | ||||||||||||||||||||||||||||
| 125 | builder => '_build_default_engine', | ||||||||||||||||||||||||||||
| 126 | ); | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | sub _build_default_engine { | ||||||||||||||||||||||||||||
| 130 | 9 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_default_engine: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 131 | 9 | ▶ | return $self->data->{default} // undef; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->data->{"default"} // undef
MC/DC100%1/1
$self->data->{"default"} // undef
$self->data->{"default"}
Truth table100%2/2
A
A= $self->data->{"default"}
| |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 6 | has log_file => ( | |||||||||||||||||||||||||||
| 135 | is => 'lazy', | ||||||||||||||||||||||||||||
| 136 | builder => '_build_log_file', | ||||||||||||||||||||||||||||
| 137 | ); | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | sub _build_log_file { | ||||||||||||||||||||||||||||
| 141 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_log_file: called CC=3 SCAR=13.1
| |||||||||||||||||||||||||||||
| 142 | 1 | ▶ | return $self->data->{logging}{file} // _strip_quotes($ENV{KNARR_LOG_FILE}) // undef; | ||||||||||||||||||||||||||
|
Condition40%2/5
($self->data->{"logging"}{"file"} // _strip_quotes($ENV{"KNARR_LOG_FILE"})) // undef
$self->data->{"logging"}{"file"} // _strip_quotes($ENV{"KNARR_LOG_FILE"})
MC/DC0%0/2
($self->data->{"logging"}{"file"} // _strip_quotes($ENV{"KNARR_LOG_FILE"})) // undef
$self->data->{"logging"}{"file"}
_strip_quotes($ENV{"KNARR_LOG_FILE"})
Truth table33%1/3
A // B
A= $self->data->{"logging"}{"file"}B= _strip_quotes($ENV{"KNARR_LOG_FILE"})
| |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 6 | has log_dir => ( | |||||||||||||||||||||||||||
| 146 | is => 'lazy', | ||||||||||||||||||||||||||||
| 147 | builder => '_build_log_dir', | ||||||||||||||||||||||||||||
| 148 | ); | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | sub _build_log_dir { | ||||||||||||||||||||||||||||
| 152 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_log_dir: called CC=3 SCAR=13.1
| |||||||||||||||||||||||||||||
| 153 | 1 | ▶ | return $self->data->{logging}{dir} // _strip_quotes($ENV{KNARR_LOG_DIR}) // undef; | ||||||||||||||||||||||||||
|
Condition40%2/5
($self->data->{"logging"}{"dir"} // _strip_quotes($ENV{"KNARR_LOG_DIR"})) // undef
$self->data->{"logging"}{"dir"} // _strip_quotes($ENV{"KNARR_LOG_DIR"})
MC/DC0%0/2
($self->data->{"logging"}{"dir"} // _strip_quotes($ENV{"KNARR_LOG_DIR"})) // undef
$self->data->{"logging"}{"dir"}
_strip_quotes($ENV{"KNARR_LOG_DIR"})
Truth table33%1/3
A // B
A= $self->data->{"logging"}{"dir"}B= _strip_quotes($ENV{"KNARR_LOG_DIR"})
| |||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 6 | has langfuse => ( | |||||||||||||||||||||||||||
| 157 | is => 'lazy', | ||||||||||||||||||||||||||||
| 158 | builder => '_build_langfuse', | ||||||||||||||||||||||||||||
| 159 | ); | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | sub _build_langfuse { | ||||||||||||||||||||||||||||
| 163 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_langfuse: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 164 | 6 | ▶ | return $self->data->{langfuse} // {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->data->{"langfuse"} // {}
MC/DC100%1/1
$self->data->{"langfuse"} // {}
$self->data->{"langfuse"}
Truth table100%2/2
A
A= $self->data->{"langfuse"}
| |||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 6 | has proxy_api_key => ( | |||||||||||||||||||||||||||
| 168 | is => 'lazy', | ||||||||||||||||||||||||||||
| 169 | builder => '_build_proxy_api_key', | ||||||||||||||||||||||||||||
| 170 | ); | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | sub _build_proxy_api_key { | ||||||||||||||||||||||||||||
| 174 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_proxy_api_key: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 175 | 2 | ▶ | return $self->data->{proxy_api_key} // $ENV{KNARR_API_KEY} // undef; | ||||||||||||||||||||||||||
|
Condition80%4/5
($self->data->{"proxy_api_key"} // $ENV{"KNARR_API_KEY"}) // undef
$self->data->{"proxy_api_key"} // $ENV{"KNARR_API_KEY"}
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"}
| |||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | sub has_proxy_api_key { | ||||||||||||||||||||||||||||
| 179 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
has_proxy_api_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 180 | 2 | return defined $self->proxy_api_key; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 6 | has auto_discover => ( | |||||||||||||||||||||||||||
| 185 | is => 'lazy', | ||||||||||||||||||||||||||||
| 186 | builder => '_build_auto_discover', | ||||||||||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | sub _build_auto_discover { | ||||||||||||||||||||||||||||
| 191 | 10 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_auto_discover: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 192 | 10 | ▶ | return $self->data->{auto_discover} // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->data->{"auto_discover"} // 0
MC/DC100%1/1
$self->data->{"auto_discover"} // 0
$self->data->{"auto_discover"}
Truth table100%2/2
A
A= $self->data->{"auto_discover"}
| |||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 6 | my %PASSTHROUGH_DEFAULTS = ( | |||||||||||||||||||||||||||
| 196 | anthropic => 'https://api.anthropic.com', | ||||||||||||||||||||||||||||
| 197 | openai => 'https://api.openai.com', | ||||||||||||||||||||||||||||
| 198 | ); | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 6 | has passthrough => ( | |||||||||||||||||||||||||||
| 201 | is => 'lazy', | ||||||||||||||||||||||||||||
| 202 | builder => '_build_passthrough', | ||||||||||||||||||||||||||||
| 203 | ); | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | sub _build_passthrough { | ||||||||||||||||||||||||||||
| 207 | 5 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_passthrough: called CC=10 SCAR=23.4
| |||||||||||||||||||||||||||||
| 208 | 5 | my $raw = $self->data->{passthrough}; | |||||||||||||||||||||||||||
| 209 | 5 | ▶ | return {} unless defined $raw; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # passthrough: true → enable all with default URLs | ||||||||||||||||||||||||||||
| 212 | 4 | ▶ | if (!ref $raw) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 2 | ▶ | return $raw ? { %PASSTHROUGH_DEFAULTS } : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 2 | ▶ | return {} unless ref $raw eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 2 | my %result; | |||||||||||||||||||||||||||
| 219 | 2 | for my $format (keys %$raw) { | |||||||||||||||||||||||||||
| 220 | 4 | my $val = $raw->{$format}; | |||||||||||||||||||||||||||
| 221 | 4 | ▶ | next unless $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 4 | ▶ | if ($val eq '1' || $val eq 'true') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$val eq 1 or $val eq "true"
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"
| |||||||||||||||||||||||||||||
| 223 | 1 | ▶ | $result{$format} = $PASSTHROUGH_DEFAULTS{$format} // next; | ||||||||||||||||||||||||||
|
Condition50%1/2
$PASSTHROUGH_DEFAULTS{$format} // (next)
MC/DC0%0/1
$PASSTHROUGH_DEFAULTS{$format} // (next)
$PASSTHROUGH_DEFAULTS{$format}
Truth table50%1/2
A
A= $PASSTHROUGH_DEFAULTS{$format}
| |||||||||||||||||||||||||||||
| 224 | } else { | ||||||||||||||||||||||||||||
| 225 | # Custom URL | ||||||||||||||||||||||||||||
| 226 | 3 | $result{$format} = $val; | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | 2 | return \%result; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | sub passthrough_url_for { | ||||||||||||||||||||||||||||
| 233 | 8 | ▶ | my ($self, $format) = @_; | ||||||||||||||||||||||||||
|
Subroutine
passthrough_url_for: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 234 | 8 | return $self->passthrough->{$format}; | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | sub validate { | ||||||||||||||||||||||||||||
| 240 | 3 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=7 SCAR=22.6
Pod
documented
| |||||||||||||||||||||||||||||
| 241 | 3 | my @errors; | |||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 3 | my $models = $self->models; | |||||||||||||||||||||||||||
| 244 | 3 | for my $name (keys %$models) { | |||||||||||||||||||||||||||
| 245 | 3 | my $def = $models->{$name}; | |||||||||||||||||||||||||||
| 246 | 3 | ▶ | unless ($def->{engine}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | 1 | push @errors, "Model '$name': missing 'engine' key"; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 3 | ▶ | if (my $default = $self->default_engine) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 0 | ▶ | unless ($default->{engine}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | 0 | push @errors, "Default: missing 'engine' key"; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 3 | ▶ | unless (keys %$models || $self->default_engine) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
keys %$models or $self->default_engine
MC/DC0%0/2
keys %$models or $self->default_engine
keys %$models
$self->default_engine
Truth table33%1/3
A or B
A= keys %$modelsB= $self->default_engine
| |||||||||||||||||||||||||||||
| 258 | 0 | push @errors, "No models configured and no default engine set"; | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 3 | return @errors; | |||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | sub engine_definitions { | ||||||||||||||||||||||||||||
| 265 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
engine_definitions: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 266 | 1 | my %defs; | |||||||||||||||||||||||||||
| 267 | 1 | my $models = $self->models; | |||||||||||||||||||||||||||
| 268 | 1 | for my $name (keys %$models) { | |||||||||||||||||||||||||||
| 269 | 2 | $defs{$name} = { %{$models->{$name}}, name => $name }; | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | 1 | return \%defs; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | # Scan environment and .env files for API keys, return model config suggestions | ||||||||||||||||||||||||||||
| 276 | sub scan_env { | ||||||||||||||||||||||||||||
| 277 | 5 | ▶ | my ($class, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
scan_env: called CC=15 SCAR=27.8
Pod
documented
| |||||||||||||||||||||||||||||
| 278 | 5 | ▶ | my @env_files = @{$opts{env_files} // []}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"env_files"} // []
MC/DC100%1/1
$opts{"env_files"} // []
$opts{"env_files"}
Truth table100%2/2
A
A= $opts{"env_files"}
| |||||||||||||||||||||||||||||
| 279 | 5 | my %env = %ENV; | |||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | # Load .env files | ||||||||||||||||||||||||||||
| 282 | 5 | for my $file (@env_files) { | |||||||||||||||||||||||||||
| 283 | 1 | ▶ | next unless -f $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 1 | ▶ | open my $fh, '<', $file or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 1 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 3 | chomp; | |||||||||||||||||||||||||||
| 287 | 3 | ▶ | next if /^\s*#/ || /^\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
/^\s*#/ or /^\s*$/
MC/DC50%1/2
/^\s*#/ or /^\s*$/
/^\s*#/
/^\s*$/
Truth table66%2/3
A or B
A= /^\s*#/B= /^\s*$/
| |||||||||||||||||||||||||||||
| 288 | 2 | ▶ | if (/^\s*(?:export\s+)?(\w+)\s*=\s*['"]?(.*?)['"]?\s*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | 2 | $env{$1} = $2; | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | 1 | close $fh; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | # Engine definitions with env var names in priority order | ||||||||||||||||||||||||||||
| 296 | # First match wins per engine: LANGERTHA_ > bare name > TEST_ | ||||||||||||||||||||||||||||
| 297 | 5 | my @engine_defs = ( | |||||||||||||||||||||||||||
| 298 | { engine => 'OpenAI', vars => [qw( LANGERTHA_OPENAI_API_KEY OPENAI_API_KEY TEST_LANGERTHA_OPENAI_API_KEY )] }, | ||||||||||||||||||||||||||||
| 299 | { engine => 'Anthropic', vars => [qw( LANGERTHA_ANTHROPIC_API_KEY ANTHROPIC_API_KEY TEST_LANGERTHA_ANTHROPIC_API_KEY )] }, | ||||||||||||||||||||||||||||
| 300 | { engine => 'Groq', vars => [qw( LANGERTHA_GROQ_API_KEY GROQ_API_KEY TEST_LANGERTHA_GROQ_API_KEY )] }, | ||||||||||||||||||||||||||||
| 301 | { engine => 'Mistral', vars => [qw( LANGERTHA_MISTRAL_API_KEY MISTRAL_API_KEY TEST_LANGERTHA_MISTRAL_API_KEY )] }, | ||||||||||||||||||||||||||||
| 302 | { engine => 'DeepSeek', vars => [qw( LANGERTHA_DEEPSEEK_API_KEY DEEPSEEK_API_KEY TEST_LANGERTHA_DEEPSEEK_API_KEY )] }, | ||||||||||||||||||||||||||||
| 303 | { engine => 'MiniMax', vars => [qw( LANGERTHA_MINIMAX_API_KEY MINIMAX_API_KEY TEST_LANGERTHA_MINIMAX_API_KEY )] }, | ||||||||||||||||||||||||||||
| 304 | { engine => 'Cerebras', vars => [qw( LANGERTHA_CEREBRAS_API_KEY CEREBRAS_API_KEY TEST_LANGERTHA_CEREBRAS_API_KEY )] }, | ||||||||||||||||||||||||||||
| 305 | { engine => 'OpenRouter', vars => [qw( LANGERTHA_OPENROUTER_API_KEY OPENROUTER_API_KEY TEST_LANGERTHA_OPENROUTER_API_KEY )] }, | ||||||||||||||||||||||||||||
| 306 | { engine => 'Perplexity', vars => [qw( LANGERTHA_PERPLEXITY_API_KEY PERPLEXITY_API_KEY TEST_LANGERTHA_PERPLEXITY_API_KEY )] }, | ||||||||||||||||||||||||||||
| 307 | { engine => 'Replicate', vars => [qw( LANGERTHA_REPLICATE_API_KEY REPLICATE_API_TOKEN TEST_LANGERTHA_REPLICATE_API_KEY )] }, | ||||||||||||||||||||||||||||
| 308 | { engine => 'HuggingFace',vars => [qw( LANGERTHA_HUGGINGFACE_API_KEY HUGGINGFACE_API_KEY TEST_LANGERTHA_HUGGINGFACE_API_KEY )] }, | ||||||||||||||||||||||||||||
| 309 | { engine => 'Gemini', vars => [qw( LANGERTHA_GEMINI_API_KEY GEMINI_API_KEY TEST_LANGERTHA_GEMINI_API_KEY )] }, | ||||||||||||||||||||||||||||
| 310 | ); | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 5 | ▶ | my $include_test = $opts{include_test} // 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{"include_test"} // 1
MC/DC0%0/1
$opts{"include_test"} // 1
$opts{"include_test"}
Truth table50%1/2
A
A= $opts{"include_test"}
| |||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 5 | my %found; | |||||||||||||||||||||||||||
| 315 | 5 | for my $def (@engine_defs) { | |||||||||||||||||||||||||||
| 316 | 60 | for my $var (@{$def->{vars}}) { | |||||||||||||||||||||||||||
| 317 | 165 | ▶ | next unless $env{$var}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | 8 | ▶ | next if !$include_test && $var =~ /^TEST_/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $include_test and $var =~ /^TEST_/
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_/
| |||||||||||||||||||||||||||||
| 319 | $found{$def->{engine}} = { | ||||||||||||||||||||||||||||
| 320 | engine => $def->{engine}, | ||||||||||||||||||||||||||||
| 321 | 8 | api_key_env => $var, | |||||||||||||||||||||||||||
| 322 | }; | ||||||||||||||||||||||||||||
| 323 | 8 | last; # first match wins (priority order) | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 5 | return \%found; | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | # Generate a YAML config string from scan results | ||||||||||||||||||||||||||||
| 332 | sub generate_config { | ||||||||||||||||||||||||||||
| 333 | 2 | ▶ | my ($class, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
generate_config: called CC=12 SCAR=26.2
Pod
documented
| |||||||||||||||||||||||||||||
| 334 | 2 | ▶ | my $found = $opts{engines} // {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{"engines"} // {}
MC/DC0%0/1
$opts{"engines"} // {}
$opts{"engines"}
Truth table50%1/2
A
A= $opts{"engines"}
| |||||||||||||||||||||||||||||
| 335 | 2 | ▶ | 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"]
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"}
| |||||||||||||||||||||||||||||
| 336 | 2 | ▶ | $listen = [$listen] unless ref $listen eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 2 | my %default_models = ( | |||||||||||||||||||||||||||
| 339 | OpenAI => 'gpt-4o-mini', | ||||||||||||||||||||||||||||
| 340 | Anthropic => 'claude-sonnet-4-6', | ||||||||||||||||||||||||||||
| 341 | Groq => 'llama-3.3-70b-versatile', | ||||||||||||||||||||||||||||
| 342 | Mistral => 'mistral-large-latest', | ||||||||||||||||||||||||||||
| 343 | DeepSeek => 'deepseek-chat', | ||||||||||||||||||||||||||||
| 344 | MiniMax => 'MiniMax-M2.1', | ||||||||||||||||||||||||||||
| 345 | Cerebras => 'llama-3.3-70b', | ||||||||||||||||||||||||||||
| 346 | OpenRouter => 'openai/gpt-4o-mini', | ||||||||||||||||||||||||||||
| 347 | Perplexity => 'sonar', | ||||||||||||||||||||||||||||
| 348 | Gemini => 'gemini-2.0-flash', | ||||||||||||||||||||||||||||
| 349 | ); | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 2 | my @lines; | |||||||||||||||||||||||||||
| 352 | 2 | push @lines, "# Knarr configuration - auto-generated"; | |||||||||||||||||||||||||||
| 353 | 2 | push @lines, "listen:"; | |||||||||||||||||||||||||||
| 354 | 2 | for my $addr (@$listen) { | |||||||||||||||||||||||||||
| 355 | 4 | push @lines, " - \"$addr\""; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | 2 | push @lines, ""; | |||||||||||||||||||||||||||
| 358 | 2 | push @lines, "models:"; | |||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 2 | for my $engine (sort keys %$found) { | |||||||||||||||||||||||||||
| 361 | 3 | my $info = $found->{$engine}; | |||||||||||||||||||||||||||
| 362 | 3 | my $model = $default_models{$engine}; | |||||||||||||||||||||||||||
| 363 | 3 | my $name = lc($engine); | |||||||||||||||||||||||||||
| 364 | 3 | ▶ | $name .= "-default" if $name eq 'openai' || $name eq 'anthropic'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$name eq "openai" or $name eq "anthropic"
MC/DC0%0/2
$name eq "openai" or $name eq "anthropic"
$name eq "openai"
$name eq "anthropic"
Truth table66%2/3
A or B
A= $name eq "openai"B= $name eq "anthropic"
| |||||||||||||||||||||||||||||
| 365 | 3 | push @lines, " $name:"; | |||||||||||||||||||||||||||
| 366 | 3 | push @lines, " engine: $engine"; | |||||||||||||||||||||||||||
| 367 | 3 | ▶ | push @lines, " model: $model" if $model; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | 3 | ▶ | push @lines, " api_key_env: $info->{api_key_env}" if $info->{api_key_env}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | 3 | push @lines, ""; | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | 2 | ▶ | unless (keys %$found) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | 0 | push @lines, " # No API keys found. Add your models here:"; | |||||||||||||||||||||||||||
| 374 | 0 | push @lines, " # my-model:"; | |||||||||||||||||||||||||||
| 375 | 0 | push @lines, " # engine: OpenAI"; | |||||||||||||||||||||||||||
| 376 | 0 | push @lines, " # model: gpt-4o-mini"; | |||||||||||||||||||||||||||
| 377 | 0 | push @lines, ""; | |||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 2 | push @lines, "# Default engine for models without explicit config (optional)"; | |||||||||||||||||||||||||||
| 381 | 2 | ▶ | if ($found->{OpenAI}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 382 | 2 | push @lines, "default:"; | |||||||||||||||||||||||||||
| 383 | 2 | push @lines, " engine: OpenAI"; | |||||||||||||||||||||||||||
| 384 | } else { | ||||||||||||||||||||||||||||
| 385 | 0 | push @lines, "# default:"; | |||||||||||||||||||||||||||
| 386 | 0 | push @lines, "# engine: OpenAI"; | |||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | 2 | push @lines, ""; | |||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 2 | push @lines, "# Auto-discover models from configured engines"; | |||||||||||||||||||||||||||
| 391 | 2 | push @lines, "auto_discover: true"; | |||||||||||||||||||||||||||
| 392 | 2 | push @lines, ""; | |||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | 2 | push @lines, "# Optional: proxy authentication"; | |||||||||||||||||||||||||||
| 395 | 2 | push @lines, "# proxy_api_key: your-secret-key"; | |||||||||||||||||||||||||||
| 396 | 2 | push @lines, ""; | |||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 2 | push @lines, "# Optional: Langfuse tracing (or set env vars)"; | |||||||||||||||||||||||||||
| 399 | 2 | push @lines, "# langfuse:"; | |||||||||||||||||||||||||||
| 400 | 2 | push @lines, "# url: http://localhost:3000"; | |||||||||||||||||||||||||||
| 401 | 2 | push @lines, "# public_key: pk-lf-..."; | |||||||||||||||||||||||||||
| 402 | 2 | push @lines, "# secret_key: sk-lf-..."; | |||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 2 | return join("\n", @lines) . "\n"; | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | # Strip surrounding quotes from env values (Docker --env-file includes them literally) | ||||||||||||||||||||||||||||
| 408 | sub _strip_quotes { | ||||||||||||||||||||||||||||
| 409 | 1 | ▶ | my $v = shift; | ||||||||||||||||||||||||||
|
Subroutine
_strip_quotes: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 410 | 1 | ▶ | return $v unless defined $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 411 | 0 | $v =~ s/^["']|["']$//g; | |||||||||||||||||||||||||||
| 412 | 0 | return $v; | |||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 6 | 1; | |||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | __END__ | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | =pod | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | =head1 NAME | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | Langertha::Knarr::Config - YAML configuration loader and validator | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | =head1 VERSION | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | version 1.100 | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | use Langertha::Knarr::Config; | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | # Load from file | ||||||||||||||||||||||||||||
| 437 | my $config = Langertha::Knarr::Config->new(file => 'knarr.yaml'); | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | # Build from environment (Docker zero-config mode) | ||||||||||||||||||||||||||||
| 440 | my $config = Langertha::Knarr::Config->from_env; | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | # Validate | ||||||||||||||||||||||||||||
| 443 | my @errors = $config->validate; | ||||||||||||||||||||||||||||
| 444 | die join("\n", @errors) if @errors; | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | Loads and validates Knarr configuration from a YAML file or from environment | ||||||||||||||||||||||||||||
| 449 | variables. All string values in the YAML file support C<${ENV_VAR}> | ||||||||||||||||||||||||||||
| 450 | interpolation. | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | See L<Langertha::Knarr> for the full configuration file format reference. | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | =head2 file | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | Path to the YAML configuration file. Optional — when omitted the config | ||||||||||||||||||||||||||||
| 457 | object starts with an empty data hash (useful for testing or for | ||||||||||||||||||||||||||||
| 458 | configs built purely from environment variables via L</from_env>). | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | =head2 data | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | The raw configuration hashref, loaded from L</file> and with all | ||||||||||||||||||||||||||||
| 463 | C<${ENV_VAR}> references expanded. Can be supplied directly to bypass | ||||||||||||||||||||||||||||
| 464 | file loading. | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | =head2 from_env | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | my $config = Langertha::Knarr::Config->from_env(%opts); | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | Class method. Builds a config object purely from environment variables | ||||||||||||||||||||||||||||
| 471 | (zero-config Docker mode). Calls L</scan_env> to detect which API keys are | ||||||||||||||||||||||||||||
| 472 | set, assigns a sensible default model for each detected engine, enables | ||||||||||||||||||||||||||||
| 473 | C<auto_discover> and C<passthrough>, and sets OpenAI as the default engine | ||||||||||||||||||||||||||||
| 474 | when C<OPENAI_API_KEY> is present. | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | Options are passed through to L</scan_env> (e.g. C<include_test>). | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | =head2 listen | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | ArrayRef of C<host:port> strings to listen on. Defaults to | ||||||||||||||||||||||||||||
| 481 | C<['127.0.0.1:8080', '127.0.0.1:11434']>. | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | =head2 models | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | HashRef of model name → model definition hashref from the C<models:> config | ||||||||||||||||||||||||||||
| 486 | section. Each definition may include C<engine>, C<model>, C<api_key_env>, | ||||||||||||||||||||||||||||
| 487 | C<api_key>, C<url>, C<system_prompt>, C<temperature>, and C<response_size>. | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | =head2 default_engine | ||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | HashRef from the C<default:> config section, or C<undef> if not set. At | ||||||||||||||||||||||||||||
| 492 | minimum contains C<engine>. Used as the fallback when a model name is not | ||||||||||||||||||||||||||||
| 493 | explicitly configured and no passthrough URL matches. | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | =head2 log_file | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | Path to a JSONL log file for request logging. Resolved from | ||||||||||||||||||||||||||||
| 498 | C<logging.file> in config or C<KNARR_LOG_FILE> environment variable. | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | =head2 log_dir | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | Path to a directory for per-request JSON log files. Resolved from | ||||||||||||||||||||||||||||
| 503 | C<logging.dir> in config or C<KNARR_LOG_DIR> environment variable. | ||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | =head2 langfuse | ||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | HashRef from the C<langfuse:> config section. May contain C<url>, | ||||||||||||||||||||||||||||
| 508 | C<public_key>, C<secret_key>, and C<trace_name>. Returns an empty hashref | ||||||||||||||||||||||||||||
| 509 | when the section is absent. | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | =head2 proxy_api_key | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | Optional shared secret that clients must present in the C<Authorization: Bearer> | ||||||||||||||||||||||||||||
| 514 | or C<x-api-key> header. Falls back to the C<KNARR_API_KEY> environment variable. | ||||||||||||||||||||||||||||
| 515 | When not set, the proxy is open (no auth required). | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | =head2 has_proxy_api_key | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | if ($config->has_proxy_api_key) { ... } | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | Returns true when a L</proxy_api_key> is configured. | ||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | =head2 auto_discover | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | Boolean. When true, L<Langertha::Knarr::Router> queries each configured engine | ||||||||||||||||||||||||||||
| 526 | for its model list at startup, making all discovered models available without | ||||||||||||||||||||||||||||
| 527 | explicit config entries. Defaults to C<0>. | ||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | =head2 passthrough | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | HashRef of format name → upstream base URL. C<passthrough: true> in YAML | ||||||||||||||||||||||||||||
| 532 | enables all known formats with their default upstream URLs | ||||||||||||||||||||||||||||
| 533 | (C<https://api.openai.com> and C<https://api.anthropic.com>). Per-format | ||||||||||||||||||||||||||||
| 534 | URLs can be customised or set to C<false> to disable selectively. | ||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||
| 536 | =head2 passthrough_url_for | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | my $url = $config->passthrough_url_for('openai'); | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | Returns the upstream base URL for the given format name (e.g. C<openai> or | ||||||||||||||||||||||||||||
| 541 | C<anthropic>), or C<undef> if passthrough is not configured for that format. | ||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | =head2 validate | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | my @errors = $config->validate; | ||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | Validates the configuration and returns a list of error strings. Returns an | ||||||||||||||||||||||||||||
| 548 | empty list when the config is valid. Checks that every model entry has an | ||||||||||||||||||||||||||||
| 549 | C<engine> key, that the default engine (if set) has an C<engine> key, and | ||||||||||||||||||||||||||||
| 550 | that at least one model or default engine is configured. | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | =head2 scan_env | ||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | my $found = Langertha::Knarr::Config->scan_env( | ||||||||||||||||||||||||||||
| 555 | env_files => ['.env', '.env.local'], | ||||||||||||||||||||||||||||
| 556 | include_test => 1, | ||||||||||||||||||||||||||||
| 557 | ); | ||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | Class method. Scans C<%ENV> and optional C<.env> files for known API key | ||||||||||||||||||||||||||||
| 560 | environment variables. Returns a HashRef of engine name → C<{engine, | ||||||||||||||||||||||||||||
| 561 | api_key_env}> for every engine whose key was found. | ||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||
| 563 | Priority order per engine: C<LANGERTHA_*_API_KEY> beats the bare vendor key | ||||||||||||||||||||||||||||
| 564 | (e.g. C<OPENAI_API_KEY>), which beats the C<TEST_LANGERTHA_*_API_KEY> variant. | ||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||
| 566 | Options: | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | =over | ||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | =item * C<env_files> — ArrayRef of .env file paths to parse (optional) | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | =item * C<include_test> — Include C<TEST_*> variables (default: C<1>) | ||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | =back | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | =head2 generate_config | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | my $yaml = Langertha::Knarr::Config->generate_config( | ||||||||||||||||||||||||||||
| 579 | engines => $found, # from scan_env | ||||||||||||||||||||||||||||
| 580 | listen => ['127.0.0.1:8080', '127.0.0.1:11434'], | ||||||||||||||||||||||||||||
| 581 | ); | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | Class method. Generates a YAML configuration string from C<scan_env> results. | ||||||||||||||||||||||||||||
| 584 | The generated YAML includes sensible defaults for each detected engine and | ||||||||||||||||||||||||||||
| 585 | commented-out stanzas for optional features. Used by C<knarr init>. | ||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | Returns a string. | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | =over | ||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | =item * L<Langertha::Knarr> — Main documentation and config format reference | ||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | =item * L<Langertha::Knarr::Router> — Uses config to resolve models to engines | ||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | =item * L<Langertha::Knarr::Tracing> — Uses config for Langfuse credentials | ||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||
| 599 | =item * L<Langertha::Knarr::RequestLog> — Uses config for request logging paths | ||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | =back | ||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | =head2 Issues | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 608 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | =head2 IRC | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 627 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | =cut | ||||||||||||||||||||||||||||