« blib/lib/Langertha/Knarr/CLI/Role/GlobalOptions.pm
↑ Summary
blib/lib/Langertha/Knarr/Handler.pm »
| 1 | package Langertha::Knarr::Handler::Code; | ||||||||||||||||||||||||
| 2 | # ABSTRACT: Coderef-backed Knarr handler for fakes, tests, and custom logic | ||||||||||||||||||||||||
| 3 | 36 | our $VERSION = '1.102'; | |||||||||||||||||||||||
| 4 | 36 | ▶ | use Moose; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 5 | 36 | ▶ | use Future; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 6 | 36 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 7 | 36 | ▶ | use Langertha::Knarr::Stream; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 8 | 36 | ▶ | use Langertha::Knarr::Response; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||
| 10 | 36 | with 'Langertha::Knarr::Handler'; | |||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||
| 13 | 36 | has code => ( | |||||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||||||
| 15 | isa => 'CodeRef', | ||||||||||||||||||||||||
| 16 | required => 1, | ||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||
| 19 | # Optional: separate generator for streaming. Returns a coderef that itself | ||||||||||||||||||||||||
| 20 | # returns next-chunk strings (undef = done) when called. | ||||||||||||||||||||||||
| 21 | has stream_code => ( | ||||||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||||||
| 23 | isa => 'Maybe[CodeRef]', | ||||||||||||||||||||||||
| 24 | 48 | ▶ | default => sub { undef }, | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 25 | 36 | ); | |||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||
| 27 | has models => ( | ||||||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||||||
| 29 | isa => 'ArrayRef', | ||||||||||||||||||||||||
| 30 | 58 | ▶ | default => sub { [ { id => 'knarr-code', object => 'model' } ] }, | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 31 | 36 | ); | |||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||
| 33 | 168 | ▶ | async sub handle_chat_f { | ||||||||||||||||||||||
|
Subroutine
handle_chat_f: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||
| 34 | 168 | my ($self, $session, $request) = @_; | |||||||||||||||||||||||
| 35 | 168 | my $out = $self->code->( $session, $request ); | |||||||||||||||||||||||
| 36 | 167 | my $r = Langertha::Knarr::Response->coerce($out); | |||||||||||||||||||||||
| 37 | 167 | ▶ | return $r->clone_with( model => $r->model // $request->model // 'knarr-code' ); | ||||||||||||||||||||||
|
Condition100%5/5
($r->model // $request->model) // "knarr-code"
$r->model // $request->model
MC/DC100%2/2
($r->model // $request->model) // "knarr-code"
$r->model
$request->model
Truth table100%3/3
A // B
A= $r->modelB= $request->model
| |||||||||||||||||||||||||
| 38 | 36 | } | |||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||
| 40 | 139 | ▶ | async sub handle_stream_f { | ||||||||||||||||||||||
|
Subroutine
handle_stream_f: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||
| 41 | 139 | my ($self, $session, $request) = @_; | |||||||||||||||||||||||
| 42 | 139 | ▶ | if ( my $sc = $self->stream_code ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 43 | 139 | my $gen = $sc->( $session, $request ); | |||||||||||||||||||||||
| 44 | 139 | return Langertha::Knarr::Stream->new( generator => $gen ); | |||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||
| 46 | 0 | my $text = $self->code->( $session, $request ); | |||||||||||||||||||||||
| 47 | 0 | return Langertha::Knarr::Stream->from_list("$text"); | |||||||||||||||||||||||
| 48 | 36 | } | |||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||
| 50 | 107 | ▶ | sub list_models { $_[0]->models } | ||||||||||||||||||||||
|
Subroutine
list_models: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||
| 52 | 36 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||
| 53 | 36 | 1; | |||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||
| 55 | __END__ | ||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||
| 57 | =pod | ||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||
| 59 | =encoding UTF-8 | ||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||
| 61 | =head1 NAME | ||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||
| 63 | Langertha::Knarr::Handler::Code - Coderef-backed Knarr handler for fakes, tests, and custom logic | ||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||
| 65 | =head1 VERSION | ||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||
| 67 | version 1.102 | ||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||
| 69 | =head1 SYNOPSIS | ||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||
| 71 | use Langertha::Knarr::Handler::Code; | ||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||
| 73 | my $handler = Langertha::Knarr::Handler::Code->new( | ||||||||||||||||||||||||
| 74 | code => sub { | ||||||||||||||||||||||||
| 75 | my ($session, $request) = @_; | ||||||||||||||||||||||||
| 76 | return 'echo: ' . $request->messages->[-1]{content}; | ||||||||||||||||||||||||
| 77 | }, | ||||||||||||||||||||||||
| 78 | stream_code => sub { | ||||||||||||||||||||||||
| 79 | my @parts = ('hel', 'lo'); | ||||||||||||||||||||||||
| 80 | return sub { @parts ? shift @parts : undef }; | ||||||||||||||||||||||||
| 81 | }, | ||||||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||
| 84 | =head1 DESCRIPTION | ||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||
| 86 | The simplest possible handler: pass coderefs that return strings (or | ||||||||||||||||||||||||
| 87 | chunk generators for streaming) and you get a working Knarr handler. | ||||||||||||||||||||||||
| 88 | Useful for tests, fakes, smoketests, and "fake LLM" demos. | ||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||
| 90 | =head2 code | ||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||
| 92 | Required. Coderef called as C<< $code->($session, $request) >> for | ||||||||||||||||||||||||
| 93 | non-streaming requests; must return a scalar string. | ||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||
| 95 | =head2 stream_code | ||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||
| 97 | Optional. Coderef returning another coderef that yields the next chunk | ||||||||||||||||||||||||
| 98 | per call, C<undef> to signal end. | ||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||
| 100 | =head2 models | ||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||
| 102 | Optional. Arrayref of model descriptors. Defaults to a single | ||||||||||||||||||||||||
| 103 | C<knarr-code> entry. | ||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||
| 105 | =head1 SUPPORT | ||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||
| 107 | =head2 Issues | ||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||
| 109 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||
| 110 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||
| 112 | =head2 IRC | ||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||
| 114 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||
| 116 | =head1 CONTRIBUTING | ||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||
| 118 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||
| 120 | =head1 AUTHOR | ||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||
| 122 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||
| 124 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||
| 126 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||
| 128 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||
| 129 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||