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

filter: Statementstmt 91.6% 22 / 24 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.7% 30 / 32 CC 1 SCAR 0.4
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Langertha/Knarr/Authorizer/StaticKey.pm ↑ Summary blib/lib/Langertha/Knarr/Protocol/A2A.pm »
1 package Langertha::Knarr::Handler::TokenRoutes;
2 # ABSTRACT: Knarr handler that routes a token-checked request by its token's provider
3 our $VERSION = '1.103';
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Future::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Langertha ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Langertha::Knarr::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Langertha::Knarr::Router;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=2.6
12  
13 extends 'Langertha::Knarr::Handler::Router';
14  
15  
16 has routes => (
17 is => 'ro',
18 isa => 'HashRef[HashRef]',
19 required => 1,
20 );
21  
22 has '+router' => (
23 required => 0,
24 lazy => 1,
25 builder => '_build_router',
26 );
27  
28 # The engine an upstream route speaks through, and the classes named once
29 # (override points).
30 sub upstream_engine { 'OpenAI' }
31 sub router_class { 'Langertha::Knarr::Router' }
32 sub config_class { 'Langertha::Knarr::Config' }
33  
34 sub _build_router {
35 my ($self) = @_;
36 return $self->router_class->new( config => $self->config_class->new( data => {} ) );
37 }
38  
39 # Engine name per route, resolved once at construction.
40 has _engines => (
41 is => 'ro',
42 lazy => 1,
43 builder => '_build_engines',
44 init_arg => undef,
45 );
46  
47 sub BUILD {
48 my ($self) = @_;
49 my $engines = $self->_engines;
50 $self->_load_engine_class( $_, $engines->{$_} ) for sort keys %$engines;
51 return;
52 }
53  
54 sub _build_engines {
55 my ($self) = @_;
56 my %engine;
57 for my $name ( sort keys %{ $self->routes } ) {
58 my $route = $self->routes->{$name};
59 croak __PACKAGE__.": route '$name' needs key_env"
60 unless defined $route->{key_env} && length $route->{key_env};
61 my $has_upstream = defined $route->{upstream} && length $route->{upstream};
62 my $has_provider = defined $route->{provider} && length $route->{provider};
63 croak __PACKAGE__.": route '$name' needs exactly one of upstream and provider"
64 unless $has_upstream xor $has_provider;
65 $engine{$name} = $has_upstream ? $self->upstream_engine : $self->engine_name( $route->{provider} );
66 }
67 return \%engine;
68 }
69  
70 # Loads a route's engine class now -- in the prefork parent, before the
71 # fork, so the workers share it and none pays the load on its first request
72 # -- and fails here, naming the route, when it cannot be loaded. Class only:
73 # no engine, no key, no connection.
74 sub _load_engine_class {
75 my ($self, $name, $engine) = @_;
76 my $class = eval { Langertha->resolve_engine_class($engine) };
77 return $class if $class;
78 my $err = ( split /\n/, ( $@ // '' ) )[0] // '';
79 $err =~ s/\s*\(\@INC[^)]*\).*//;
80 $err =~ s/ at \S+ line \d+.*//;
81 # reported from the code that built the handler, not Moose's constructor
82 local $Carp::CarpLevel = 1;
83 croak __PACKAGE__.": route '$name': engine $engine cannot be loaded"
84 .( length $err ? ": $err" : '' );
85 }
86  
87  
88 sub engine_name {
89 my ($self, $provider) = @_;
90 my ($known) = grep { lc $_ eq lc $provider }
91 map { $_->{engine} } @{ $self->config_class->engine_catalog };
92 return $known if defined $known;
93 return $provider if eval { Langertha->resolve_engine_class($provider) };
94 croak __PACKAGE__.": unknown provider '$provider'";
95 }
96  
97  
98 sub route_def {
99 my ($self, $name, $model) = @_;
100 my $route = $self->routes->{$name} or return;
101 my $url = $route->{upstream} // $route->{url};
102 return {
103 engine => $self->_engines->{$name},
104 ( defined $url && length $url ? ( url => $url ) : () ),
105 api_key_env => $route->{key_env},
106 model => $model,
107 };
108 }
109  
110 # The engine for the request's grant (overrides Handler::Router's model
111 # lookup): its provider's route, with the request's model.
112 sub _resolve {
113 my ($self, $request) = @_;
114 my $grant = $request->grant;
115 my $provider = $grant ? $grant->{provider} : undef;
116 my $route = defined $provider ? $self->routes->{$provider} : undef;
117 unless ( $route ) {
118 $log->debug('token routes: no route for the token\'s provider');
119 die Future::Exception->new( 'forbidden: no route for this token', 'forbidden' );
120 }
121 my $model = $request->model;
122 die Future::Exception->new( 'The request names no model', 'model_not_found' )
123 unless defined $model && length $model;
124 my $key = $ENV{ $route->{key_env} };
125 unless ( defined $key && length $key ) {
126 $log->errorf( 'token routes: environment variable %s of route %s is not set',
127 $route->{key_env}, $provider );
128 die Future::Exception->new( 'backend unavailable', 'unavailable' );
129 }
130 my $engine = $self->router->engine_for( $self->route_def( $provider, $model ), $model );
131 return ( $engine, $model, 0 );
132 }
133  
134  
135 sub list_models { [] }
136  
137 __PACKAGE__->meta->make_immutable;
138 1;
139  
140 __END__
« blib/lib/Langertha/Knarr/Authorizer/StaticKey.pm ↑ Summary blib/lib/Langertha/Knarr/Protocol/A2A.pm »