blib/lib/Langertha/Plugin.pm

filter: Statementstmt 70.0% 35 / 50 Branchbran 25.0% 1 / 4 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 64.2% 9 / 14 Podpod 100% 11 / 11 totaltotal 67.8% 57 / 84 CC 4 SCAR 8.4
CC
4
Cov
65%
raider
6.0
__ANON__
3.8
plugin_before_raid
2.0
CRAP 2.3
« blib/lib/Langertha/Stream.pm ↑ Summary blib/lib/Langertha/Engine/Anthropic.pm »
1 package Langertha::Plugin;
2 # ABSTRACT: Base class for plugins
3 18 our $VERSION = '0.503';
4 18 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 18 has host => (
9 is => 'ro',
10 weak_ref => 1,
11 required => 1,
12 );
13  
14  
15 116 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=13.3
16 116 my ( $orig, $class, %args ) = @_;
17 116 ▶ if ($args{raider} && !$args{host}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"raider"} and not $args{"host"}) 0 116 116
Condition33%1/3
$args{"raider"} and not $args{"host"}
!ll&&!rl&&r
11600
MC/DC0%0/2
$args{"raider"} and not $args{"host"}
$args{"raider"} not $args{"host"}
Truth table33%1/3
A and not(B)
A= $args{"raider"}B= not $args{"host"}
ABresult
0X0
100
111
18 0 $args{host} = delete $args{raider};
19 }
20 116 return $class->$orig(%args);
21 18 };
22  
23 0 ▶sub raider {
Subroutine raider: not called CC=2 SCAR=17.9
Pod documented
24 0 my ( $self ) = @_;
25 0 my $host = $self->host;
26 0 ▶ return $host if $host->isa('Langertha::Raider');
Branch: 1 branch
BranchTrueFalseTotal
if $host->isa("Langertha::Raider") 0 0 0
27 0 return undef;
28 }
29  
30  
31 # --- Hook methods (identity/passthrough defaults) ---
32  
33 0 ▶async sub plugin_before_raid {
Subroutine plugin_before_raid: not called CC=1 SCAR=6.9
Pod documented
34 0 my ( $self, $messages ) = @_;
35 0 return $messages;
36 18 }
37  
38 0 ▶async sub plugin_build_conversation {
Subroutine plugin_build_conversation: not called CC=1 SCAR=6.9
Pod documented
39 0 my ( $self, $conversation ) = @_;
40 0 return $conversation;
41 18 }
42  
43 22 ▶async sub plugin_before_llm_call {
Subroutine plugin_before_llm_call: called CC=1 SCAR=0.0
Pod documented
44 22 my ( $self, $conversation, $iteration ) = @_;
45 22 return $conversation;
46 18 }
47  
48 15 ▶async sub plugin_after_llm_response {
Subroutine plugin_after_llm_response: called CC=1 SCAR=0.0
Pod documented
49 15 my ( $self, $data, $iteration ) = @_;
50 15 return $data;
51 18 }
52  
53 19 ▶async sub plugin_before_tool_call {
Subroutine plugin_before_tool_call: called CC=1 SCAR=0.0
Pod documented
54 19 my ( $self, $name, $input ) = @_;
55 19 return ($name, $input);
56 18 }
57  
58 21 ▶async sub plugin_after_tool_call {
Subroutine plugin_after_tool_call: called CC=1 SCAR=0.0
Pod documented
59 21 my ( $self, $name, $input, $result ) = @_;
60 21 return $result;
61 18 }
62  
63 0 ▶async sub plugin_after_raid {
Subroutine plugin_after_raid: not called CC=1 SCAR=6.9
Pod documented
64 0 my ( $self, $result ) = @_;
65 0 return $result;
66 18 }
67  
68  
69 0 ▶sub self_tools { [] }
Subroutine self_tools: not called CC=1 SCAR=6.9
Pod documented
70  
71  
72 100 ▶sub provides_events { [] }
Subroutine provides_events: called CC=1 SCAR=0.0
Pod documented
73  
74  
75 100 ▶sub requires_events { [] }
Subroutine requires_events: called CC=1 SCAR=0.0
Pod documented
76  
77 18 __PACKAGE__->meta->make_immutable;
78  
79 18 1;
80  
81 __END__
82  
83 =pod
84  
85 =encoding UTF-8
86  
87 =head1 NAME
88  
89 Langertha::Plugin - Base class for plugins
90  
91 =head1 VERSION
92  
93 version 0.503
94  
95 =head1 SYNOPSIS
96  
97 package Langertha::Plugin::MyPlugin;
98 use Moose;
99 use Future::AsyncAwait;
100 extends 'Langertha::Plugin';
101  
102 has my_option => (is => 'ro', default => 42);
103  
104 async sub plugin_before_llm_call {
105 my ($self, $conversation, $iteration) = @_;
106 # ... modify conversation ...
107 return $conversation;
108 }
109  
110 __PACKAGE__->meta->make_immutable;
111  
112 # Or with sugar:
113 package Langertha::Plugin::MyPlugin;
114 use Langertha qw( Plugin );
115  
116 has my_option => (is => 'ro', default => 42);
117  
118 async sub plugin_before_llm_call { ... }
119  
120 __PACKAGE__->meta->make_immutable;
121  
122 =head1 DESCRIPTION
123  
124 Base class for Langertha plugins. Plugins are Moose classes that extend
125 C<Langertha::Plugin> and override hook methods. Plugins can be attached to
126 any plugin host — a wrapper class such as L<Langertha::Chat>,
127 L<Langertha::Embedder> or L<Langertha::ImageGen>, an engine that consumes
128 L<Langertha::Role::PluginHost>, or L<Langertha::Raider> from the
129 C<langertha-raider> distribution.
130  
131 Plugins are registered via the C<plugins> attribute on the host:
132  
133 my $chat = Langertha::Chat->new(
134 engine => $engine,
135 plugins => ['Langfuse', 'MyPlugin'],
136 );
137  
138 Short names are resolved first to C<Langertha::Plugin::$name>, then to
139 C<LangerthaX::Plugin::$name>. Fully qualified names (containing C<::>)
140 are used as-is.
141  
142 =head1 HOOK METHODS
143  
144 Override these in your subclass. All hooks are C<async sub> and form a
145 pipeline: the host calls each plugin's hook in order, passing the
146 return value of one as input to the next.
147  
148 =over 4
149  
150 =item B<plugin_before_raid>($messages) -> $messages
151  
152 Transform input messages before the raid starts.
153  
154 =item B<plugin_build_conversation>($conversation) -> $conversation
155  
156 Transform the assembled conversation (mission + history + messages).
157  
158 =item B<plugin_before_llm_call>($conversation, $iteration) -> $conversation
159  
160 Transform the conversation before each LLM request.
161  
162 =item B<plugin_after_llm_response>($data, $iteration) -> $data
163  
164 Inspect or transform the parsed LLM response.
165  
166 =item B<plugin_before_tool_call>($name, $input) -> ($name, $input) or ()
167  
168 Inspect or transform before each tool execution. Return an empty list
169 to skip the tool call.
170  
171 =item B<plugin_after_tool_call>($name, $input, $result) -> $result
172  
173 Transform the tool result after execution.
174  
175 =item B<plugin_after_raid>($result) -> $result
176  
177 Transform the final L<Langertha::Raider::Result> before return.
178  
179 =item B<plugin_before_embedding>($text) -> $text
180  
181 Transform the input before an embedding request (L<Langertha::Embedder>).
182  
183 =item B<plugin_after_embedding>($text, $vector, $call_result?) -> $vector
184  
185 Inspect or transform the embedding. C<$call_result> is passed only when the
186 call went through L<Langertha::Embedder/simple_embedding_result> (or its
187 C<_f>): the L<Langertha::CallResult> of the request, with its C<usage>,
188 C<rate_limit>, C<model> and C<total_seconds>. Every hook gets the same
189 object, whose C<value> is the engine's vector before any hook ran; the
190 return value of one hook is the C<$vector> of the next. A hook written for
191 two arguments keeps working.
192  
193 =item B<plugin_before_image_gen>($prompt) -> $prompt
194  
195 Transform the prompt before an image generation request (L<Langertha::ImageGen>).
196  
197 =item B<plugin_after_image_gen>($prompt, $result, $call_result?) -> $result
198  
199 Inspect or transform the generated images. C<$call_result> is passed only
200 when the call went through L<Langertha::ImageGen/simple_image_result> (or its
201 C<_f>), with the same meaning as for C<plugin_after_embedding>.
202  
203 =back
204  
205 =head2 host
206  
207 Back-reference to the plugin host (L<Langertha::Raider> or engine)
208 this plugin belongs to. Weakened to avoid circular references.
209  
210 =head2 raider
211  
212 Convenience accessor. Returns the host if it is a L<Langertha::Raider>,
213 C<undef> otherwise.
214  
215 =head2 self_tools
216  
217 sub self_tools {
218 return [{
219 name => 'my_tool',
220 description => 'Does something useful',
221 inputSchema => { type => 'object', properties => { ... } },
222 code => sub { $_[0]->text_result('done') },
223 }];
224 }
225  
226 Override to register additional self-tools on the host. Returns an
227 arrayref of tool definitions in MCP format. Tool codes receive
228 C<($mcp_tool, $args)>.
229  
230 =head2 provides_events
231  
232 sub provides_events { ['history_saved', 'history_compressed'] }
233  
234 Override to declare custom events this plugin provides. Other plugins
235 can hook into these events by implementing C<on_$event_name> methods.
236 The host validates at instantiation that all required events are
237 provided.
238  
239 =head2 requires_events
240  
241 sub requires_events { ['history_saved'] }
242  
243 Override to declare events this plugin depends on. If no loaded plugin
244 provides a required event, the host dies with a useful error at
245 instantiation.
246  
247 =head1 SUPPORT
248  
249 =head2 Issues
250  
251 Please report bugs and feature requests on GitHub at
252 L<https://github.com/Getty/langertha/issues>.
253  
254 =head2 IRC
255  
256 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
257  
258 =head1 CONTRIBUTING
259  
260 Contributions are welcome! Please fork the repository and submit a pull request.
261  
262 =head1 AUTHOR
263  
264 Torsten Raudssus <getty@cpan.org>
265  
266 =head1 COPYRIGHT AND LICENSE
267  
268 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
269  
270 This is free software; you can redistribute it and/or modify it under
271 the same terms as the Perl 5 programming language system itself.
272  
273 =cut
« blib/lib/Langertha/Stream.pm ↑ Summary blib/lib/Langertha/Engine/Anthropic.pm »