« blib/lib/Langertha/Knarr/Handler/Code.pm
↑ Summary
blib/lib/Langertha/Knarr/CLI/Cmd/Container.pm »
| 1 | package Langertha::Knarr::Handler; | ||
| 2 | # ABSTRACT: Role for Knarr backend handlers (Raider, Engine, Code, ...) | ||
| 3 | 92 | our $VERSION = '1.102'; | |
| 4 | 92 | ▶ | use Moose::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 92 | ▶ | use Future::AsyncAwait; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 92 | ▶ | use Langertha::Knarr::Stream; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 92 | ▶ | use Langertha::Knarr::Response; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | |||
| 10 | 92 | requires 'handle_chat_f'; | |
| 11 | 92 | requires 'list_models'; | |
| 12 | |||
| 13 | # Default streaming = run handle_chat_f and emit one chunk. | ||
| 14 | # Handlers that natively stream should override. | ||
| 15 | 0 | ▶ | async sub handle_stream_f { |
|
Subroutine
handle_stream_f: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 16 | 0 | my ($self, $session, $request) = @_; | |
| 17 | 0 | my $r = Langertha::Knarr::Response->coerce( await $self->handle_chat_f($session, $request) ); | |
| 18 | 0 | my $stream = Langertha::Knarr::Stream->from_list( $r->content ); | |
| 19 | 0 | $stream->finish_reason( $r->finish_reason ); | |
| 20 | 0 | $stream->tool_calls( $r->tool_calls ); | |
| 21 | 0 | return $stream; | |
| 22 | 92 | } | |
| 23 | |||
| 24 | 92 | 1; | |
| 25 | |||
| 26 | __END__ | ||
| 27 | |||
| 28 | =pod | ||
| 29 | |||
| 30 | =encoding UTF-8 | ||
| 31 | |||
| 32 | =head1 NAME | ||
| 33 | |||
| 34 | Langertha::Knarr::Handler - Role for Knarr backend handlers (Raider, Engine, Code, ...) | ||
| 35 | |||
| 36 | =head1 VERSION | ||
| 37 | |||
| 38 | version 1.102 | ||
| 39 | |||
| 40 | =head1 SYNOPSIS | ||
| 41 | |||
| 42 | package My::Handler; | ||
| 43 | use Moose; | ||
| 44 | use Future; | ||
| 45 | with 'Langertha::Knarr::Handler'; | ||
| 46 | |||
| 47 | sub handle_chat_f { | ||
| 48 | my ($self, $session, $request) = @_; | ||
| 49 | return Future->done( | ||
| 50 | Langertha::Knarr::Response->new( content => 'hello', model => 'my-model' ) | ||
| 51 | ); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub list_models { [ { id => 'my-model', object => 'model' } ] } | ||
| 55 | |||
| 56 | 1; | ||
| 57 | |||
| 58 | =head1 DESCRIPTION | ||
| 59 | |||
| 60 | The role every Knarr backend handler must consume. A handler is the | ||
| 61 | "what answers the request" half of Knarr — it receives a normalized | ||
| 62 | L<Langertha::Knarr::Request> and returns either a sync result hash via | ||
| 63 | L</handle_chat_f> or an async chunk iterator via L</handle_stream_f>. | ||
| 64 | |||
| 65 | Knarr ships with concrete handlers for the common cases: | ||
| 66 | |||
| 67 | =over | ||
| 68 | |||
| 69 | =item * L<Langertha::Knarr::Handler::Code> — coderef-backed, for tests/fakes | ||
| 70 | |||
| 71 | =item * L<Langertha::Knarr::Handler::Engine> — passthrough to a Langertha engine | ||
| 72 | |||
| 73 | =item * L<Langertha::Knarr::Handler::Raider> — per-session L<Langertha::Raider> | ||
| 74 | |||
| 75 | =item * L<Langertha::Knarr::Handler::Router> — model-name routing via L<Langertha::Knarr::Router> | ||
| 76 | |||
| 77 | =item * L<Langertha::Knarr::Handler::Passthrough> — raw HTTP forward to upstream | ||
| 78 | |||
| 79 | =item * L<Langertha::Knarr::Handler::A2AClient> — consume a remote A2A agent | ||
| 80 | |||
| 81 | =item * L<Langertha::Knarr::Handler::ACPClient> — consume a remote ACP agent | ||
| 82 | |||
| 83 | =item * L<Langertha::Knarr::Handler::Tracing> — Langfuse tracing decorator | ||
| 84 | |||
| 85 | =item * L<Langertha::Knarr::Handler::RequestLog> — JSONL request logging decorator | ||
| 86 | |||
| 87 | =back | ||
| 88 | |||
| 89 | Decorators (Tracing, RequestLog) wrap an inner handler and themselves | ||
| 90 | consume this role, so they compose freely. | ||
| 91 | |||
| 92 | =head2 handle_chat_f | ||
| 93 | |||
| 94 | my $future = $handler->handle_chat_f($session, $request); | ||
| 95 | |||
| 96 | Required. Returns a L<Future> that resolves to a | ||
| 97 | L<Langertha::Knarr::Response>. Plain strings, C<{ content =E<gt> ..., model | ||
| 98 | =E<gt> ... }> hashrefs, and L<Langertha::Response> objects are all accepted | ||
| 99 | and coerced automatically by L<Langertha::Knarr::Response/coerce> at the | ||
| 100 | protocol boundary. | ||
| 101 | |||
| 102 | =head2 handle_stream_f | ||
| 103 | |||
| 104 | my $stream = await $handler->handle_stream_f($session, $request); | ||
| 105 | |||
| 106 | Default implementation calls L</handle_chat_f> and emits the result as | ||
| 107 | a single-chunk stream. Native streamers should override and return a | ||
| 108 | L<Langertha::Knarr::Stream> whose C<next_chunk_f> yields chunk strings. | ||
| 109 | |||
| 110 | =head2 list_models | ||
| 111 | |||
| 112 | my $models = $handler->list_models; | ||
| 113 | |||
| 114 | Required. Returns an arrayref of model descriptors as hashes with at | ||
| 115 | least an C<id> key. | ||
| 116 | |||
| 117 | =head1 SUPPORT | ||
| 118 | |||
| 119 | =head2 Issues | ||
| 120 | |||
| 121 | Please report bugs and feature requests on GitHub at | ||
| 122 | L<https://github.com/Getty/langertha-knarr/issues>. | ||
| 123 | |||
| 124 | =head2 IRC | ||
| 125 | |||
| 126 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||
| 127 | |||
| 128 | =head1 CONTRIBUTING | ||
| 129 | |||
| 130 | Contributions are welcome! Please fork the repository and submit a pull request. | ||
| 131 | |||
| 132 | =head1 AUTHOR | ||
| 133 | |||
| 134 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||
| 135 | |||
| 136 | =head1 COPYRIGHT AND LICENSE | ||
| 137 | |||
| 138 | This software is copyright (c) 2026 by Torsten Raudssus. | ||
| 139 | |||
| 140 | This is free software; you can redistribute it and/or modify it under | ||
| 141 | the same terms as the Perl 5 programming language system itself. | ||
| 142 | |||
| 143 | =cut | ||