| 1 | package Langertha::Knarr::Handler::Engine; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Knarr handler that proxies directly to a Langertha engine | ||||||||||||||||||||||||||||||||||||||||||
| 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 Langertha::Knarr::Stream; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Langertha::Knarr::Response; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 6 | with 'Langertha::Knarr::Handler'; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 6 | has engine => ( | |||||||||||||||||||||||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 15 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | has model_id => ( | ||||||||||||||||||||||||||||||||||||||||||
| 19 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 20 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 22 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 6 | ); | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | ▶ | sub _model_id { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_model_id: called CC=5 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | ▶ | return $self->model_id if $self->model_id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 2 | my $e = $self->engine; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | return $e->chat_model if $e->can('chat_model') && $e->chat_model; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$e->can("chat_model") and $e->chat_model
MC/DC0%0/2
$e->can("chat_model") and $e->chat_model
$e->can("chat_model")
$e->chat_model
Truth table33%1/3
A and B
A= $e->can("chat_model")B= $e->chat_model
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | return ( ref($e) =~ /::([^:]+)$/ ) ? lc($1) : 'engine'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 21 | ▶ | async sub handle_chat_f { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
handle_chat_f: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 21 | my ($self, $session, $request) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 21 | my $response = await $self->engine->chat_f( $request->chat_f_args($self->engine) ); | |||||||||||||||||||||||||||||||||||||||||
| 35 | 21 | my $r = Langertha::Knarr::Response->coerce($response); | |||||||||||||||||||||||||||||||||||||||||
| 36 | 21 | ▶ | return $r->clone_with( model => $r->model // $self->_model_id ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$r->model // $self->_model_id
MC/DC0%0/2
$r->model // $self->_model_id
$r->model
$self->_model_id
Truth table66%2/3
A // B
A= $r->modelB= $self->_model_id
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 6 | } | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | ▶ | async sub handle_stream_f { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
handle_stream_f: called CC=3 SCAR=14.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | my ($self, $session, $request) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | my $engine = $self->engine; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | ▶ | unless ( _supports_streaming($engine) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | # Engine doesn't support native streaming — fall back to single-chunk. | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | my $r = await $self->handle_chat_f($session, $request); | |||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | my $stream = Langertha::Knarr::Stream->from_list( $r->content ); | |||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | $stream->finish_reason( $r->finish_reason ); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | $stream->tool_calls( $r->tool_calls ); | |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | $stream->usage( $r->usage ) if $r->usage; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | $stream->model( $r->model ); | |||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | $stream->upstream_model( $r->upstream_model ); | |||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | return $stream; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | return Langertha::Knarr::Stream->from_callback( sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.0
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | my ($emit, $done, $fail, $finish, $tool_call, $usage, $model) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 4 | ▶ | my $cb = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=15 SCAR=38.9
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 4 | my ($chunk) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 4 | ▶ | my $text = ref $chunk && $chunk->can('content') ? $chunk->content : "$chunk"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $chunk and $chunk->can("content")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 4 | $emit->($text); | |||||||||||||||||||||||||||||||||||||||||
| 60 | # Langertha::Stream::Chunk carries the backend's finish_reason on the | ||||||||||||||||||||||||||||||||||||||||||
| 61 | # terminal chunk; the protocol maps it when it closes the stream. | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 4 | ▶ | $finish->( $chunk->finish_reason ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
ref $chunk and $chunk->can("has_finish_reason") and $chunk->has_finish_reason
ref $chunk and $chunk->can("has_finish_reason")
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
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | if ref $chunk && $chunk->can('has_finish_reason') && $chunk->has_finish_reason; | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # Core assembles streamed tool-call fragments and attaches the finished | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # Langertha::ToolCall objects to a chunk (Role::Chat::aggregate_tool_calls | ||||||||||||||||||||||||||||||||||||||||||
| 66 | # collects the same); a core whose parser attaches none yields none. The | ||||||||||||||||||||||||||||||||||||||||||
| 67 | # protocol emits them when it closes the stream (k19). | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | $tool_call->( @{ $chunk->tool_calls } ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
ref $chunk and $chunk->can("has_tool_calls") and $chunk->has_tool_calls
ref $chunk and $chunk->can("has_tool_calls")
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
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | if ref $chunk && $chunk->can('has_tool_calls') && $chunk->has_tool_calls; | ||||||||||||||||||||||||||||||||||||||||||
| 70 | # The token usage rides on a chunk too, cumulative (the last report is | ||||||||||||||||||||||||||||||||||||||||||
| 71 | # the stream's totals); the protocol puts it on its terminal frames and | ||||||||||||||||||||||||||||||||||||||||||
| 72 | # the tracing decorator on the generation. | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 4 | ▶ | $usage->( $chunk->usage ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
ref $chunk and $chunk->can("has_usage") and $chunk->has_usage
ref $chunk and $chunk->can("has_usage")
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
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | if ref $chunk && $chunk->can('has_usage') && $chunk->has_usage; | ||||||||||||||||||||||||||||||||||||||||||
| 75 | # The model the backend reports answering with, for the trace. | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 4 | ▶ | $model->( $chunk->model ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
ref $chunk and $chunk->can("has_model") and $chunk->has_model
ref $chunk and $chunk->can("has_model")
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
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | if ref $chunk && $chunk->can('has_model') && $chunk->has_model; | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | my $f = $engine->chat_stream_realtime_f( chunk_callback => $cb, $request->chat_f_args($engine) ); | |||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | $f->on_done( $done ); | |||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | $f->on_fail( $fail ); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | $f->retain; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | }); | |||||||||||||||||||||||||||||||||||||||||
| 84 | 6 | } | |||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | ▶ | sub _supports_streaming { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_supports_streaming: called CC=3 SCAR=16.5
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 1 | my ($engine) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | ▶ | return $engine->supports('streaming') if $engine->can('supports'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 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')
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | ▶ | sub list_models { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_models: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | return [ { id => $self->_model_id, object => 'model' } ]; | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 6 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | __END__ | ||||||||||||||||||||||||||||||||||||||||||