Knarr/Handler/Raider.pm

filter: Statementstmt 93.9% 31 / 33 Branchbran 50.0% 1 / 2 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 85.7% 6 / 7 Podpod 0.0% 0 / 2 totaltotal 78.8% 41 / 52 CC 4 SCAR 6.5
CC
4
Cov
88%
handle_chat_f
3.0
_session_raider
2.0
list_models
2.0
CRAP 1.9
« Knarr/Handler/RequestLog.pm ↑ Summary Knarr/Handler/Code.pm »
1 package Langertha::Knarr::Handler::Raider;
2 # ABSTRACT: Knarr handler that backs each session with a Langertha::Raider
3 2 our $VERSION = '1.100';
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Langertha::Knarr::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 with 'Langertha::Knarr::Handler';
10  
11  
12 # A coderef invoked as $raider_factory->($session, $request) to create a fresh
13 # Raider for a new session. Receives Steerboard session + request for context.
14 2 has raider_factory => (
15 is => 'ro',
16 isa => 'CodeRef',
17 required => 1,
18 );
19  
20 2 has model_id => (
21 is => 'ro',
22 isa => 'Str',
23 default => 'knarr-raider',
24 );
25  
26 sub _session_raider {
27 3 ▶ my ($self, $session, $request) = @_;
Subroutine _session_raider: called CC=2 SCAR=7.1
28 3 ▶ return $session->handler_state->{raider} //= $self->raider_factory->( $session, $request );
Condition66%2/3
$session->handler_state->{"raider"} //= $self->raider_factory->($session, $request)
l!l&&r!l&&!r
120
MC/DC0%0/2
$session->handler_state->{"raider"} //= $self->raider_factory->($session, $request)
$session->handler_state->{"raider"} $self->raider_factory->($session, $request)
Truth table66%2/3
A //= B
A= $session->handler_state->{"raider"}B= $self->raider_factory->($session, $request)
ABresult
1X1
011
000
29 }
30  
31 3 ▶async sub handle_chat_f {
Subroutine handle_chat_f: called CC=3 SCAR=11.1
Pod undocumented
32 3 my ($self, $session, $request) = @_;
33 3 my $raider = $self->_session_raider( $session, $request );
34  
35 # Take only the latest user message — Raider keeps its own history per session.
36 3 ▶ my @user_msgs = grep { ($_->{role} // '') eq 'user' } @{ $request->messages };
Condition50%1/2
$_->{"role"} // ""
l!l
30
MC/DC0%0/1
$_->{"role"} // ""
$_->{"role"}
Truth table50%1/2
A
A= $_->{"role"}
Aresult
11
00
37 3 ▶ my $last = $user_msgs[-1] or die "Raider handler: no user message in request\n";
Branch: 1 branch
BranchTrueFalseTotal
my $last = $user_msgs[-1] or die "Raider handler: no user message in request\n" 0 3 3
38  
39 3 my $result = await $raider->raid_f( $last->{content} );
40 3 my $r = Langertha::Knarr::Response->coerce($result);
41 3 return $r->clone_with( model => $self->model_id );
42 2 }
43  
44 sub list_models {
45 0 ▶ my ($self) = @_;
Subroutine list_models: not called CC=1 SCAR=6.9
Pod undocumented
46 0 return [ { id => $self->model_id, object => 'model' } ];
47 }
48  
49 2 __PACKAGE__->meta->make_immutable;
50 2 1;
51  
52 __END__
53  
54 =pod
55  
56 =encoding UTF-8
57  
58 =head1 NAME
59  
60 Langertha::Knarr::Handler::Raider - Knarr handler that backs each session with a Langertha::Raider
61  
62 =head1 VERSION
63  
64 version 1.100
65  
66 =head1 SYNOPSIS
67  
68 use Langertha::Engine::Anthropic;
69 use Langertha::Raider;
70 use Langertha::Knarr::Handler::Raider;
71  
72 my $handler = Langertha::Knarr::Handler::Raider->new(
73 model_id => 'langertha-raider',
74 raider_factory => sub {
75 my ($session, $request) = @_;
76 return Langertha::Raider->new(
77 engine => Langertha::Engine::Anthropic->new(
78 api_key => $ENV{ANTHROPIC_API_KEY},
79 ),
80 mission => 'You are a helpful assistant.',
81 );
82 },
83 );
84  
85 =head1 DESCRIPTION
86  
87 Spawns a fresh L<Langertha::Raider> per Knarr session via
88 L</raider_factory> and dispatches the latest user message to its
89 C<raid_f> method. The Raider keeps its own conversation history across
90 turns within a session, so subsequent requests in the same session
91 continue the same agent loop.
92  
93 Use this when you're exposing a real autonomous agent (with MCP tools,
94 mission, persistent context) over a standard LLM wire protocol.
95  
96 =head2 raider_factory
97  
98 Required. Coderef called as C<< $factory->($session, $request) >> the
99 first time a session needs a Raider. The returned Raider is cached on
100 the session and reused for subsequent turns.
101  
102 =head2 model_id
103  
104 Optional. The id reported by L</list_models>. Defaults to
105 C<knarr-raider>.
106  
107 =head1 SUPPORT
108  
109 =head2 Issues
110  
111 Please report bugs and feature requests on GitHub at
112 L<https://github.com/Getty/langertha-knarr/issues>.
113  
114 =head2 IRC
115  
116 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
117  
118 =head1 CONTRIBUTING
119  
120 Contributions are welcome! Please fork the repository and submit a pull request.
121  
122 =head1 AUTHOR
123  
124 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
125  
126 =head1 COPYRIGHT AND LICENSE
127  
128 This software is copyright (c) 2026 by Torsten Raudssus.
129  
130 This is free software; you can redistribute it and/or modify it under
131 the same terms as the Perl 5 programming language system itself.
132  
133 =cut
« Knarr/Handler/RequestLog.pm ↑ Summary Knarr/Handler/Code.pm »