| 1 | package Langertha::Plugin; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Base class for plugins | ||||||||||||||||||||||||||||
| 3 | 7 | our $VERSION = '0.502'; | |||||||||||||||||||||||||||
| 4 | 7 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 7 | has host => ( | |||||||||||||||||||||||||||
| 9 | is => 'ro', | ||||||||||||||||||||||||||||
| 10 | weak_ref => 1, | ||||||||||||||||||||||||||||
| 11 | required => 1, | ||||||||||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | around BUILDARGS => sub { | ||||||||||||||||||||||||||||
| 16 | 73 | ▶ | my ( $orig, $class, %args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 17 | 73 | ▶ | if ($args{raider} && !$args{host}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args{"raider"} and not $args{"host"}
MC/DC50%1/2
$args{"raider"} and not $args{"host"}
$args{"raider"}
not $args{"host"}
Truth table66%2/3
A and not(B)
A= $args{"raider"}B= not $args{"host"}
| |||||||||||||||||||||||||||||
| 18 | 3 | $args{host} = delete $args{raider}; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | 73 | return $class->$orig(%args); | |||||||||||||||||||||||||||
| 21 | 7 | }; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | sub raider { | ||||||||||||||||||||||||||||
| 24 | 3 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
raider: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 3 | my $host = $self->host; | |||||||||||||||||||||||||||
| 26 | 3 | ▶ | return $host if $host->isa('Langertha::Raider'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 1 | return undef; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # --- Hook methods (identity/passthrough defaults) --- | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | async sub plugin_before_raid { | ||||||||||||||||||||||||||
|
Subroutine
plugin_before_raid: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 1 | my ( $self, $messages ) = @_; | |||||||||||||||||||||||||||
| 35 | 1 | return $messages; | |||||||||||||||||||||||||||
| 36 | 7 | } | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | async sub plugin_build_conversation { | ||||||||||||||||||||||||||
|
Subroutine
plugin_build_conversation: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 1 | my ( $self, $conversation ) = @_; | |||||||||||||||||||||||||||
| 40 | 1 | return $conversation; | |||||||||||||||||||||||||||
| 41 | 7 | } | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 3 | ▶ | async sub plugin_before_llm_call { | ||||||||||||||||||||||||||
|
Subroutine
plugin_before_llm_call: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 3 | my ( $self, $conversation, $iteration ) = @_; | |||||||||||||||||||||||||||
| 45 | 3 | return $conversation; | |||||||||||||||||||||||||||
| 46 | 7 | } | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 5 | ▶ | async sub plugin_after_llm_response { | ||||||||||||||||||||||||||
|
Subroutine
plugin_after_llm_response: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 5 | my ( $self, $data, $iteration ) = @_; | |||||||||||||||||||||||||||
| 50 | 5 | return $data; | |||||||||||||||||||||||||||
| 51 | 7 | } | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 2 | ▶ | async sub plugin_before_tool_call { | ||||||||||||||||||||||||||
|
Subroutine
plugin_before_tool_call: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 2 | my ( $self, $name, $input ) = @_; | |||||||||||||||||||||||||||
| 55 | 2 | return ($name, $input); | |||||||||||||||||||||||||||
| 56 | 7 | } | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 2 | ▶ | async sub plugin_after_tool_call { | ||||||||||||||||||||||||||
|
Subroutine
plugin_after_tool_call: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 2 | my ( $self, $name, $input, $result ) = @_; | |||||||||||||||||||||||||||
| 60 | 2 | return $result; | |||||||||||||||||||||||||||
| 61 | 7 | } | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 1 | ▶ | async sub plugin_after_raid { | ||||||||||||||||||||||||||
|
Subroutine
plugin_after_raid: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 1 | my ( $self, $result ) = @_; | |||||||||||||||||||||||||||
| 65 | 1 | return $result; | |||||||||||||||||||||||||||
| 66 | 7 | } | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 1 | ▶ | sub self_tools { [] } | ||||||||||||||||||||||||||
|
Subroutine
self_tools: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 56 | ▶ | sub provides_events { [] } | ||||||||||||||||||||||||||
|
Subroutine
provides_events: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 55 | ▶ | sub requires_events { [] } | ||||||||||||||||||||||||||
|
Subroutine
requires_events: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 7 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 7 | 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.502 | ||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||
| 126 | to any plugin host — L<Langertha::Raider> or an engine class that | ||||||||||||||||||||||||||||
| 127 | consumes L<Langertha::Role::PluginHost>. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Plugins are registered via the C<plugins> attribute on the host: | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | my $raider = Langertha::Raider->new( | ||||||||||||||||||||||||||||
| 132 | engine => $engine, | ||||||||||||||||||||||||||||
| 133 | plugins => ['Langfuse', 'MyPlugin'], | ||||||||||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Short names are resolved first to C<Langertha::Plugin::$name>, then to | ||||||||||||||||||||||||||||
| 137 | C<LangerthaX::Plugin::$name>. Fully qualified names (containing C<::>) | ||||||||||||||||||||||||||||
| 138 | are used as-is. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head1 HOOK METHODS | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Override these in your subclass. All hooks are C<async sub> and form a | ||||||||||||||||||||||||||||
| 143 | pipeline: the host calls each plugin's hook in order, passing the | ||||||||||||||||||||||||||||
| 144 | return value of one as input to the next. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =over 4 | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =item B<plugin_before_raid>($messages) -> $messages | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Transform input messages before the raid starts. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =item B<plugin_build_conversation>($conversation) -> $conversation | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Transform the assembled conversation (mission + history + messages). | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =item B<plugin_before_llm_call>($conversation, $iteration) -> $conversation | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Transform the conversation before each LLM request. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =item B<plugin_after_llm_response>($data, $iteration) -> $data | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Inspect or transform the parsed LLM response. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =item B<plugin_before_tool_call>($name, $input) -> ($name, $input) or () | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Inspect or transform before each tool execution. Return an empty list | ||||||||||||||||||||||||||||
| 167 | to skip the tool call. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =item B<plugin_after_tool_call>($name, $input, $result) -> $result | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Transform the tool result after execution. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =item B<plugin_after_raid>($result) -> $result | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Transform the final L<Langertha::Raider::Result> before return. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =back | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head2 host | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | Back-reference to the plugin host (L<Langertha::Raider> or engine) | ||||||||||||||||||||||||||||
| 182 | this plugin belongs to. Weakened to avoid circular references. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head2 raider | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Convenience accessor. Returns the host if it is a L<Langertha::Raider>, | ||||||||||||||||||||||||||||
| 187 | C<undef> otherwise. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head2 self_tools | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | sub self_tools { | ||||||||||||||||||||||||||||
| 192 | return [{ | ||||||||||||||||||||||||||||
| 193 | name => 'my_tool', | ||||||||||||||||||||||||||||
| 194 | description => 'Does something useful', | ||||||||||||||||||||||||||||
| 195 | inputSchema => { type => 'object', properties => { ... } }, | ||||||||||||||||||||||||||||
| 196 | code => sub { $_[0]->text_result('done') }, | ||||||||||||||||||||||||||||
| 197 | }]; | ||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Override to register additional self-tools on the host. Returns an | ||||||||||||||||||||||||||||
| 201 | arrayref of tool definitions in MCP format. Tool codes receive | ||||||||||||||||||||||||||||
| 202 | C<($mcp_tool, $args)>. | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head2 provides_events | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | sub provides_events { ['history_saved', 'history_compressed'] } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Override to declare custom events this plugin provides. Other plugins | ||||||||||||||||||||||||||||
| 209 | can hook into these events by implementing C<on_$event_name> methods. | ||||||||||||||||||||||||||||
| 210 | The host validates at instantiation that all required events are | ||||||||||||||||||||||||||||
| 211 | provided. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head2 requires_events | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | sub requires_events { ['history_saved'] } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | Override to declare events this plugin depends on. If no loaded plugin | ||||||||||||||||||||||||||||
| 218 | provides a required event, the host dies with a useful error at | ||||||||||||||||||||||||||||
| 219 | instantiation. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =head2 Issues | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 226 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =head2 IRC | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 245 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =cut | ||||||||||||||||||||||||||||