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

filter: Statementstmt 80.6% 75 / 93 Branchbran 54.1% 26 / 48 Conditioncond 25.7% 9 / 35 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 3 totaltotal 58.2% 124 / 213 CC 37 SCAR 29.7
CC
37
Cov
62%
__ANON__
49.1
_resolve
13.6
list_models
12.0
CRAP 19.4
« blib/lib/Langertha/Knarr/Protocol/ACP.pm ↑ Summary blib/lib/Langertha/Knarr/Authorizer/Token.pm »
1 package Langertha::Knarr::Handler::Router;
2 # ABSTRACT: Knarr handler that resolves model names via Langertha::Knarr::Router and dispatches to engines
3 6 our $VERSION = '1.103';
4 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Future::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Langertha::Knarr::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Langertha::Knarr::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 with 'Langertha::Knarr::Handler';
12  
13  
14 # Wraps a Langertha::Knarr::Router (which is Moo) and uses it to resolve
15 # incoming model names to Langertha engine instances. Also keeps the
16 # upstream Knarr::Config visible for the rest of the request lifecycle.
17  
18 6 has router => ( is => 'ro', required => 1 );
19  
20 # Optional Passthrough handler used as fallback when the router can't
21 # resolve a model. Allows mixed mode: configured models go via Langertha
22 # engines (with tracing/middleware support), unknown models tunnel straight
23 # to the upstream API the client thinks they're talking to.
24 has passthrough => (
25 is => 'ro',
26 isa => 'Maybe[Object]',
27 3 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 6 );
29  
30 # The engine for a request, or () when it goes to the passthrough. Unknown
31 # models go to the passthrough only when it can forward the client's
32 # protocol; otherwise to the default engine (k41). With neither, the
33 # failure carries the category 'model_not_found', which Knarr answers as a
34 # 404 in the client protocol's error shape.
35 #
36 # A request without a model (A2A never names one) is resolved as such, not
37 # as a placeholder name: the default engine then answers with its own
38 # configured model instead of being sent a model called 'default' (k42).
39 9 ▶sub _resolve {
Subroutine _resolve: called CC=8 SCAR=26.1
40 9 my ($self, $request) = @_;
41 9 my $model = $request->model;
42 9 my $router = $self->router;
43 9 ▶ if ( $self->_passthrough_serves( $request->protocol ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_passthrough_serves($request->protocol)) 2 7 9
44 # With passthrough: try without default engine first so unknown models
45 # go to passthrough instead of being routed to the default engine.
46 2 my @r = eval { $router->resolve($model, skip_default => 1) };
47 2 ▶ return @r if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 1 1 2
48 1 return (); # not found or error → passthrough
49 }
50 7 my @r = $router->resolve($model, skip_default => 1);
51 6 ▶ return @r if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 6 0 6
52 0 ▶ return $router->resolve($model) if $router->config->default_engine;
Branch: 1 branch
BranchTrueFalseTotal
if $router->config->default_engine 0 0 0
53 0 ▶ die Future::Exception->new(
Branch: 2 branches
BranchTrueFalseTotal
defined $model && length $model ? : 0 0 0
$self->passthrough ? : 0 0 0
Condition0%0/3
defined $model and length $model
!ll&&!rl&&r
000
MC/DC0%0/2
defined $model and length $model
defined $model length $model
Truth table0%0/3
A and B
A= defined $modelB= length $model
ABresult
0X0
100
111
54 ( defined $model && length $model ? "Model '$model' is not configured" : 'The request names no model' )
55 . ' and there is no default engine'
56 . ( $self->passthrough ? ' (no passthrough upstream for '.$request->protocol.')' : '' ),
57 'model_not_found' );
58 }
59  
60 9 ▶sub _passthrough_serves {
Subroutine _passthrough_serves: called CC=3 SCAR=11.1
61 9 my ($self, $protocol) = @_;
62 9 ▶ my $pt = $self->passthrough or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $pt = $self->passthrough or return 0 2 7 9
63 2 ▶ return $pt->can('serves_protocol') ? $pt->serves_protocol($protocol) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$pt->can('serves_protocol') ? : 0 2 2
64 }
65  
66 8 ▶async sub handle_chat_f {
Subroutine handle_chat_f: called CC=6 SCAR=17.9
Pod undocumented
67 8 my ($self, $session, $request) = @_;
68 8 my ($engine, $canonical_model, $alias_only) = $self->_resolve($request);
69 7 ▶ unless ( $engine ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($engine) 1 6 7
70 1 return Langertha::Knarr::Response->coerce(
71 await $self->passthrough->handle_chat_f( $session, $request )
72 );
73 }
74 6 my $response = await $engine->chat_f( $request->chat_f_args($engine) );
75 6 my $r = Langertha::Knarr::Response->coerce($response);
76 # An alias without a configured model: the provider default answered, so
77 # report the model that answered, never the alias (k22).
78 6 ▶ if ( $alias_only ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias_only) 3 3 6
79 3 ▶ return $r if defined $r->model;
Branch: 1 branch
BranchTrueFalseTotal
if defined $r->model 1 2 3
80 2 ▶ my $chat_model = $engine->can('chat_model') ? $engine->chat_model : undef;
Branch: 1 branch
BranchTrueFalseTotal
$engine->can('chat_model') ? : 2 0 2
81 2 ▶ return defined $chat_model ? $r->clone_with( model => $chat_model ) : $r;
Branch: 1 branch
BranchTrueFalseTotal
defined $chat_model ? : 1 1 2
82 }
83 # The client sees the configured name, as /v1/models lists it; the model
84 # the upstream reported goes to the trace (k70).
85 3 return $r->clone_with( model => $canonical_model, upstream_model => $r->model );
86 6 }
87  
88 1 ▶async sub handle_stream_f {
Subroutine handle_stream_f: called CC=5 SCAR=20.9
Pod undocumented
89 1 my ($self, $session, $request) = @_;
90 1 my ($engine, $canonical_model, $alias_only) = $self->_resolve($request);
91  
92 1 ▶ unless ( $engine ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($engine) 0 1 1
93 0 return await $self->passthrough->handle_stream_f( $session, $request );
94 }
95  
96 1 ▶ unless ( _supports_streaming($engine) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_supports_streaming($engine)) 0 1 1
97 0 my $r = await $self->handle_chat_f($session, $request);
98 0 my $stream = Langertha::Knarr::Stream->from_list( $r->content );
99 0 $stream->finish_reason( $r->finish_reason );
100 0 $stream->tool_calls( $r->tool_calls );
101 0 ▶ $stream->usage( $r->usage ) if $r->usage;
Branch: 1 branch
BranchTrueFalseTotal
if $r->usage 0 0 0
102 0 $stream->model( $r->model );
103 0 $stream->upstream_model( $r->upstream_model );
104 0 return $stream;
105 }
106  
107 1 ▶ my $stream = Langertha::Knarr::Stream->from_callback( sub {
Subroutine __ANON__: called CC=1 SCAR=1.0
108 1 my ($emit, $done, $fail, $finish, $tool_call, $usage, $model) = @_;
109 4 ▶ my $cb = sub {
Subroutine __ANON__: called CC=15 SCAR=38.9
110 4 my ($chunk) = @_;
111 4 ▶ my $text = ref $chunk && $chunk->can('content') ? $chunk->content : "$chunk";
Branch: 1 branch
BranchTrueFalseTotal
ref $chunk && $chunk->can('content') ? : 0 4 4
Condition33%1/3
ref $chunk and $chunk->can("content")
!ll&&!rl&&r
400
MC/DC0%0/2
ref $chunk and $chunk->can("content")
ref $chunk $chunk->can("content")
Truth table33%1/3
A and B
A= ref $chunkB= $chunk->can("content")
ABresult
0X0
100
111
112 4 $emit->($text);
113 # Langertha::Stream::Chunk carries the backend's finish_reason on the
114 # terminal chunk; the protocol maps it when it closes the stream.
115 4 ▶ $finish->( $chunk->finish_reason )
Branch: 1 branch
BranchTrueFalseTotal
if ref $chunk and $chunk->can("has_finish_reason") and $chunk->has_finish_reason 0 4 4
Condition33%2/6
ref $chunk and $chunk->can("has_finish_reason") and $chunk->has_finish_reason
!ll&&!rl&&r
400
ref $chunk and $chunk->can("has_finish_reason")
!ll&&!rl&&r
400
MC/DC0%0/3
ref $chunk and $chunk->can("has_finish_reason") and $chunk->has_finish_reason
ref $chunk $chunk->can("has_finish_reason") $chunk->has_finish_reason
Truth table25%1/4
A and B and C
A= ref $chunkB= $chunk->can("has_finish_reason")C= $chunk->has_finish_reason
ABCresult
0XX0
10X0
1100
1111
116 if ref $chunk && $chunk->can('has_finish_reason') && $chunk->has_finish_reason;
117 # Core assembles streamed tool-call fragments and attaches the finished
118 # Langertha::ToolCall objects to a chunk (Role::Chat::aggregate_tool_calls
119 # collects the same); a core whose parser attaches none yields none. The
120 # protocol emits them when it closes the stream (k19).
121 0 ▶ $tool_call->( @{ $chunk->tool_calls } )
Branch: 1 branch
BranchTrueFalseTotal
if ref $chunk and $chunk->can("has_tool_calls") and $chunk->has_tool_calls 0 4 4
Condition33%2/6
ref $chunk and $chunk->can("has_tool_calls") and $chunk->has_tool_calls
!ll&&!rl&&r
400
ref $chunk and $chunk->can("has_tool_calls")
!ll&&!rl&&r
400
MC/DC0%0/3
ref $chunk and $chunk->can("has_tool_calls") and $chunk->has_tool_calls
ref $chunk $chunk->can("has_tool_calls") $chunk->has_tool_calls
Truth table25%1/4
A and B and C
A= ref $chunkB= $chunk->can("has_tool_calls")C= $chunk->has_tool_calls
ABCresult
0XX0
10X0
1100
1111
122 if ref $chunk && $chunk->can('has_tool_calls') && $chunk->has_tool_calls;
123 # The token usage rides on a chunk too, cumulative (the last report is
124 # the stream's totals); the protocol puts it on its terminal frames and
125 # the tracing decorator on the generation.
126 4 ▶ $usage->( $chunk->usage )
Branch: 1 branch
BranchTrueFalseTotal
if ref $chunk and $chunk->can("has_usage") and $chunk->has_usage 0 4 4
Condition33%2/6
ref $chunk and $chunk->can("has_usage") and $chunk->has_usage
!ll&&!rl&&r
400
ref $chunk and $chunk->can("has_usage")
!ll&&!rl&&r
400
MC/DC0%0/3
ref $chunk and $chunk->can("has_usage") and $chunk->has_usage
ref $chunk $chunk->can("has_usage") $chunk->has_usage
Truth table25%1/4
A and B and C
A= ref $chunkB= $chunk->can("has_usage")C= $chunk->has_usage
ABCresult
0XX0
10X0
1100
1111
127 if ref $chunk && $chunk->can('has_usage') && $chunk->has_usage;
128 # The model the backend reports answering with, for the trace.
129 4 ▶ $model->( $chunk->model )
Branch: 1 branch
BranchTrueFalseTotal
if ref $chunk and $chunk->can("has_model") and $chunk->has_model 0 4 4
Condition33%2/6
ref $chunk and $chunk->can("has_model") and $chunk->has_model
!ll&&!rl&&r
400
ref $chunk and $chunk->can("has_model")
!ll&&!rl&&r
400
MC/DC0%0/3
ref $chunk and $chunk->can("has_model") and $chunk->has_model
ref $chunk $chunk->can("has_model") $chunk->has_model
Truth table25%1/4
A and B and C
A= ref $chunkB= $chunk->can("has_model")C= $chunk->has_model
ABCresult
0XX0
10X0
1100
1111
130 if ref $chunk && $chunk->can('has_model') && $chunk->has_model;
131 1 };
132 1 my $f = $engine->chat_stream_realtime_f( chunk_callback => $cb, $request->chat_f_args($engine) );
133 1 $f->on_done( $done );
134 1 $f->on_fail( $fail );
135 1 $f->retain;
136 1 });
137 # Labeled like the non-streaming answer: the configured model, unless the
138 # config names none and the provider default answers (k22).
139 1 ▶ $stream->model($canonical_model) unless $alias_only;
Branch: 1 branch
BranchTrueFalseTotal
unless $alias_only 1 0 1
140 1 return $stream;
141 6 }
142  
143 1 ▶sub _supports_streaming {
Subroutine _supports_streaming: called CC=3 SCAR=16.5
144 1 my ($engine) = @_;
145 1 ▶ return $engine->supports('streaming') if $engine->can('supports');
Branch: 1 branch
BranchTrueFalseTotal
if $engine->can("supports") 1 0 1
146 0 ▶ return $engine->can('simple_chat_stream_realtime_f') && $engine->can('chat_stream_request');
Condition0%0/3
$engine->can('simple_chat_stream_realtime_f') && $engine->can('chat_stream_request')
!ll&&!rl&&r
000
MC/DC0%0/2
$engine->can('simple_chat_stream_realtime_f') && $engine->can('chat_stream_request')
$engine->can('simple_chat_stream_realtime_f') $engine->can('chat_stream_request')
Truth table0%0/3
A && B
A= $engine->can('simple_chat_stream_realtime_f')B= $engine->can('chat_stream_request')
ABresult
0X0
100
111
147 }
148  
149 0 ▶sub list_models {
Subroutine list_models: not called CC=3 SCAR=24.8
Pod undocumented
150 0 my ($self) = @_;
151 0 my $models = $self->router->list_models;
152 0 ▶ return [ map { ref $_ eq 'HASH' ? $_ : { id => "$_", object => 'model' } } @{ $models || [] } ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 0 0 0
Condition0%0/2
$models || []
l!l
00
MC/DC0%0/1
$models || []
$models
Truth table0%0/2
A
A= $models
Aresult
11
00
153 }
154  
155 6 __PACKAGE__->meta->make_immutable;
156 6 1;
157  
158 __END__
« blib/lib/Langertha/Knarr/Protocol/ACP.pm ↑ Summary blib/lib/Langertha/Knarr/Authorizer/Token.pm »