| 1 | package Langertha::Knarr::Handler::Router; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Knarr handler that resolves model names via Langertha::Knarr::Router and dispatches to engines | ||||||||||||||||||||||||||||
| 3 | 3 | our $VERSION = '1.100'; | |||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Future; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Langertha::Knarr::Stream; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Langertha::Knarr::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | with 'Langertha::Knarr::Handler'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # Wraps a Langertha::Knarr::Router (which is Moo) and uses it to resolve | ||||||||||||||||||||||||||||
| 14 | # incoming model names to Langertha engine instances. Also keeps the | ||||||||||||||||||||||||||||
| 15 | # upstream Knarr::Config visible for the rest of the request lifecycle. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | has router => ( is => 'ro', required => 1 ); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Optional Passthrough handler used as fallback when the router can't | ||||||||||||||||||||||||||||
| 20 | # resolve a model. Allows mixed mode: configured models go via Langertha | ||||||||||||||||||||||||||||
| 21 | # engines (with tracing/middleware support), unknown models tunnel straight | ||||||||||||||||||||||||||||
| 22 | # to the upstream API the client thinks they're talking to. | ||||||||||||||||||||||||||||
| 23 | has passthrough => ( | ||||||||||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||||||||||
| 25 | isa => 'Maybe[Object]', | ||||||||||||||||||||||||||||
| 26 | 1 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 3 | ); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub _resolve { | ||||||||||||||||||||||||||||
| 30 | 4 | ▶ | my ($self, $model) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_resolve: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 31 | 4 | ▶ | $model //= 'default'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$model //= "default"
MC/DC0%0/1
$model //= "default"
$model
Truth table50%1/2
A
A= $model
| |||||||||||||||||||||||||||||
| 32 | 4 | ▶ | if ($self->passthrough) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | # With passthrough: try without default engine first so unknown models | ||||||||||||||||||||||||||||
| 34 | # go to passthrough instead of being routed to the default engine. | ||||||||||||||||||||||||||||
| 35 | 2 | my @r = eval { $self->router->resolve($model, skip_default => 1) }; | |||||||||||||||||||||||||||
| 36 | 2 | ▶ | return @r if @r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 1 | return (); # not found or error → passthrough | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 2 | my @r = eval { $self->router->resolve($model) }; | |||||||||||||||||||||||||||
| 40 | 2 | ▶ | return @r unless $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 1 | die $@; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 4 | ▶ | async sub handle_chat_f { | ||||||||||||||||||||||||||
|
Subroutine
handle_chat_f: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 4 | my ($self, $session, $request) = @_; | |||||||||||||||||||||||||||
| 46 | 4 | my ($engine, $canonical_model) = $self->_resolve( $request->model ); | |||||||||||||||||||||||||||
| 47 | 3 | ▶ | unless ( $engine ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 1 | return Langertha::Knarr::Response->coerce( | |||||||||||||||||||||||||||
| 49 | await $self->passthrough->handle_chat_f( $session, $request ) | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 2 | my $response = await $engine->chat_f( $request->chat_f_args($engine) ); | |||||||||||||||||||||||||||
| 53 | 2 | my $r = Langertha::Knarr::Response->coerce($response); | |||||||||||||||||||||||||||
| 54 | 2 | return $r->clone_with( model => $canonical_model ); | |||||||||||||||||||||||||||
| 55 | 3 | } | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | async sub handle_stream_f { | ||||||||||||||||||||||||||
|
Subroutine
handle_stream_f: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 0 | my ($self, $session, $request) = @_; | |||||||||||||||||||||||||||
| 59 | 0 | my ($engine) = $self->_resolve( $request->model ); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | unless ( $engine ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | return await $self->passthrough->handle_stream_f( $session, $request ); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | unless ( _supports_streaming($engine) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | my $r = await $self->handle_chat_f($session, $request); | |||||||||||||||||||||||||||
| 67 | 0 | return Langertha::Knarr::Stream->from_list( $r->content ); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | return Langertha::Knarr::Stream->from_callback( sub { | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | my ($emit, $done, $fail) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 72 | my $cb = sub { | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | my ($chunk) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | my $text = ref $chunk && $chunk->can('content') ? $chunk->content : "$chunk"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $chunk and $chunk->can("content")
MC/DC0%0/2
ref $chunk and $chunk->can("content")
ref $chunk
$chunk->can("content")
Truth table0%0/3
A and B
A= ref $chunkB= $chunk->can("content")
| |||||||||||||||||||||||||||||
| 75 | 0 | $emit->($text); | |||||||||||||||||||||||||||
| 76 | 0 | }; | |||||||||||||||||||||||||||
| 77 | 0 | my $f = $engine->simple_chat_stream_realtime_f( $cb, @{ $request->messages } ); | |||||||||||||||||||||||||||
| 78 | 0 | $f->on_done( $done ); | |||||||||||||||||||||||||||
| 79 | 0 | $f->on_fail( $fail ); | |||||||||||||||||||||||||||
| 80 | 0 | $f->retain; | |||||||||||||||||||||||||||
| 81 | 0 | }); | |||||||||||||||||||||||||||
| 82 | 3 | } | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub _supports_streaming { | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | my ($engine) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_supports_streaming: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | return $engine->supports('streaming') if $engine->can('supports'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 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')
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')
| |||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub list_models { | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
list_models: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 0 | my $models = $self->router->list_models; | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | return [ map { ref $_ eq 'HASH' ? $_ : { id => "$_", object => 'model' } } @{ $models || [] } ]; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 97 | 3 | 1; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =pod | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head1 NAME | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | Langertha::Knarr::Handler::Router - Knarr handler that resolves model names via Langertha::Knarr::Router and dispatches to engines | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head1 VERSION | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | version 1.100 | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | use Langertha::Knarr::Config; | ||||||||||||||||||||||||||||
| 116 | use Langertha::Knarr::Router; | ||||||||||||||||||||||||||||
| 117 | use Langertha::Knarr::Handler::Router; | ||||||||||||||||||||||||||||
| 118 | use Langertha::Knarr::Handler::Passthrough; | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | my $config = Langertha::Knarr::Config->new(file => 'knarr.yaml'); | ||||||||||||||||||||||||||||
| 121 | my $router = Langertha::Knarr::Router->new(config => $config); | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | my $handler = Langertha::Knarr::Handler::Router->new( | ||||||||||||||||||||||||||||
| 124 | router => $router, | ||||||||||||||||||||||||||||
| 125 | passthrough => Langertha::Knarr::Handler::Passthrough->new( | ||||||||||||||||||||||||||||
| 126 | upstreams => $config->passthrough, | ||||||||||||||||||||||||||||
| 127 | ), | ||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | Resolves incoming model names against a L<Langertha::Knarr::Router> | ||||||||||||||||||||||||||||
| 133 | (which knows your C<knarr.yaml>) and dispatches to the matched | ||||||||||||||||||||||||||||
| 134 | L<Langertha::Engine>. When a passthrough fallback handler is supplied, | ||||||||||||||||||||||||||||
| 135 | unknown model names tunnel through to it instead of failing — this | ||||||||||||||||||||||||||||
| 136 | preserves the classic Knarr behaviour where configured models go via | ||||||||||||||||||||||||||||
| 137 | Langertha and everything else passes straight to the upstream API. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Streaming responses are pumped via the engine's | ||||||||||||||||||||||||||||
| 140 | C<simple_chat_stream_realtime_f> for native token-by-token delivery. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head2 router | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Required. A L<Langertha::Knarr::Router> instance. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head2 passthrough | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | Optional. Any L<Langertha::Knarr::Handler> consumer used as a fallback | ||||||||||||||||||||||||||||
| 149 | when the router can't resolve a model. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head2 Issues | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 156 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head2 IRC | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 175 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =cut | ||||||||||||||||||||||||||||