| 1 | package Langertha::Knarr::CLI::Cmd::Models; | ||||||||||||
| 2 | 2 | our $VERSION = '1.100'; | |||||||||||
| 3 | # ABSTRACT: List configured models and their backends | ||||||||||||
| 4 | 2 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use MooX::Cmd; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use MooX::Options protect_argv => 0, usage_string => 'USAGE: knarr models [options]'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | |||||||||||||
| 9 | 2 | option format => ( | |||||||||||
| 10 | is => 'ro', | ||||||||||||
| 11 | format => 's', | ||||||||||||
| 12 | short => 'f', | ||||||||||||
| 13 | doc => 'Output format: table, json (default: table)', | ||||||||||||
| 14 | default => 'table', | ||||||||||||
| 15 | ); | ||||||||||||
| 16 | |||||||||||||
| 17 | sub execute { | ||||||||||||
| 18 | 2 | ▶ | my ($self, $args, $chain) = @_; | ||||||||||
|
Subroutine
execute: called CC=9 SCAR=23.3
Pod
undocumented
| |||||||||||||
| 19 | 2 | my $main = $chain->[0]; | |||||||||||
| 20 | 2 | my $config_file = $main->config; | |||||||||||
| 21 | |||||||||||||
| 22 | 2 | ▶ | unless (-f $config_file) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | 0 | print STDERR "Config file not found: $config_file\n"; | |||||||||||
| 24 | 0 | exit 1; | |||||||||||
| 25 | } | ||||||||||||
| 26 | |||||||||||||
| 27 | 2 | require Langertha::Knarr::Config; | |||||||||||
| 28 | 2 | require Langertha::Knarr::Router; | |||||||||||
| 29 | |||||||||||||
| 30 | 2 | my $config = Langertha::Knarr::Config->new(file => $config_file); | |||||||||||
| 31 | 2 | my $router = Langertha::Knarr::Router->new(config => $config); | |||||||||||
| 32 | 2 | my $models = $router->list_models; | |||||||||||
| 33 | |||||||||||||
| 34 | 2 | ▶ | if ($self->format eq 'json') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | 1 | require JSON::MaybeXS; | |||||||||||
| 36 | 1 | print JSON::MaybeXS->new(pretty => 1, utf8 => 1, convert_blessed => 1)->encode($models); | |||||||||||
| 37 | 1 | return; | |||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | # Table format | ||||||||||||
| 41 | 1 | ▶ | unless (@$models) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | 0 | print "No models configured.\n"; | |||||||||||
| 43 | 0 | print "Run 'knarr init' to scan your environment for API keys.\n"; | |||||||||||
| 44 | 0 | return; | |||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | 1 | my $max_id = _max_len(map { $_->{id} } @$models); | |||||||||||
| 48 | 1 | my $max_engine = _max_len(map { $_->{engine} } @$models); | |||||||||||
| 49 | 1 | my $max_model = _max_len(map { $_->{model} } @$models); | |||||||||||
| 50 | |||||||||||||
| 51 | 1 | ▶ | $max_id = 5 if $max_id < 5; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | 1 | ▶ | $max_engine = 6 if $max_engine < 6; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | 1 | ▶ | $max_model = 5 if $max_model < 5; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | |||||||||||||
| 55 | 1 | printf "%-${max_id}s %-${max_engine}s %-${max_model}s %s\n", | |||||||||||
| 56 | 'ID', 'ENGINE', 'MODEL', 'SOURCE'; | ||||||||||||
| 57 | 1 | printf "%-${max_id}s %-${max_engine}s %-${max_model}s %s\n", | |||||||||||
| 58 | '-' x $max_id, '-' x $max_engine, '-' x $max_model, '------'; | ||||||||||||
| 59 | |||||||||||||
| 60 | 1 | for my $m (@$models) { | |||||||||||
| 61 | printf "%-${max_id}s %-${max_engine}s %-${max_model}s %s\n", | ||||||||||||
| 62 | 1 | $m->{id}, $m->{engine}, $m->{model}, $m->{source}; | |||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | 1 | ▶ | if (my $default = $config->default_engine) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | 0 | print "\nDefault engine: $default->{engine}\n"; | |||||||||||
| 67 | } | ||||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | sub _max_len { | ||||||||||||
| 71 | 3 | ▶ | my $max = 0; | ||||||||||
|
Subroutine
_max_len: called CC=4 SCAR=14.3
| |||||||||||||
| 72 | 3 | for (@_) { | |||||||||||
| 73 | 3 | ▶ | my $l = length($_ // ''); | ||||||||||
|
Condition50%1/2
$_ // ""
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
| |||||||||||||
| 74 | 3 | ▶ | $max = $l if $l > $max; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | } | ||||||||||||
| 76 | 3 | return $max; | |||||||||||
| 77 | } | ||||||||||||
| 78 | |||||||||||||
| 79 | 2 | 1; | |||||||||||
| 80 | |||||||||||||
| 81 | __END__ | ||||||||||||
| 82 | |||||||||||||
| 83 | =pod | ||||||||||||
| 84 | |||||||||||||
| 85 | =encoding UTF-8 | ||||||||||||
| 86 | |||||||||||||
| 87 | =head1 NAME | ||||||||||||
| 88 | |||||||||||||
| 89 | Langertha::Knarr::CLI::Cmd::Models - List configured models and their backends | ||||||||||||
| 90 | |||||||||||||
| 91 | =head1 VERSION | ||||||||||||
| 92 | |||||||||||||
| 93 | version 1.100 | ||||||||||||
| 94 | |||||||||||||
| 95 | =head1 DESCRIPTION | ||||||||||||
| 96 | |||||||||||||
| 97 | Implements the C<knarr models> command. Loads the config file, triggers | ||||||||||||
| 98 | auto-discovery (if enabled), and prints the full model list as a table or | ||||||||||||
| 99 | JSON. Each row shows the model ID, engine class, backend model name, and | ||||||||||||
| 100 | whether it was explicitly configured or auto-discovered. | ||||||||||||
| 101 | |||||||||||||
| 102 | See L<knarr> for option details and L<Langertha::Knarr> for full documentation. | ||||||||||||
| 103 | |||||||||||||
| 104 | =head1 SEE ALSO | ||||||||||||
| 105 | |||||||||||||
| 106 | =over | ||||||||||||
| 107 | |||||||||||||
| 108 | =item * L<knarr> — CLI synopsis and option reference | ||||||||||||
| 109 | |||||||||||||
| 110 | =item * L<Langertha::Knarr::Router/list_models> — Underlying data source | ||||||||||||
| 111 | |||||||||||||
| 112 | =back | ||||||||||||
| 113 | |||||||||||||
| 114 | =head1 SUPPORT | ||||||||||||
| 115 | |||||||||||||
| 116 | =head2 Issues | ||||||||||||
| 117 | |||||||||||||
| 118 | Please report bugs and feature requests on GitHub at | ||||||||||||
| 119 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||||
| 120 | |||||||||||||
| 121 | =head2 IRC | ||||||||||||
| 122 | |||||||||||||
| 123 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||
| 124 | |||||||||||||
| 125 | =head1 CONTRIBUTING | ||||||||||||
| 126 | |||||||||||||
| 127 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||
| 128 | |||||||||||||
| 129 | =head1 AUTHOR | ||||||||||||
| 130 | |||||||||||||
| 131 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||
| 132 | |||||||||||||
| 133 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 134 | |||||||||||||
| 135 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||
| 136 | |||||||||||||
| 137 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 138 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 139 | |||||||||||||
| 140 | =cut | ||||||||||||