blib/lib/Langertha/Role/PluginHost.pm

filter: Statementstmt 84.3% 86 / 102 Branchbran 68.7% 22 / 32 Conditioncond 61.1% 11 / 18 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 93.3% 14 / 15 Podpod 100% 2 / 2 totaltotal 77.6% 139 / 179 CC 31 SCAR 20.0
CC
31
Cov
78%
fire_event_f
12.0
_build_plugin_instances
10.5
_resolve_plugin_name
10.4
CRAP 7.4
« blib/lib/Langertha/HTTP/UserAgent.pm ↑ Summary blib/lib/Langertha/Manifest/Validation.pm »
1 package Langertha::Role::PluginHost;
2 # ABSTRACT: Role for objects that host plugins (Raider, Engine)
3 184 our $VERSION = '0.503';
4 184 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 184 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 184 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 184 ▶use Module::Runtime qw( use_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 184 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 184 has plugins => (
12 is => 'ro',
13 isa => 'ArrayRef',
14 lazy => 1,
15 builder => '_build_plugins',
16 );
17  
18  
19 110 ▶sub _build_plugins {
Subroutine _build_plugins: called CC=2 SCAR=7.2
20 110 my ( $self ) = @_;
21 110 ▶ return $self->can('_sugar_plugins') ? $self->_sugar_plugins : [];
Branch: 1 branch
BranchTrueFalseTotal
$self->can('_sugar_plugins') ? : 0 110 110
22 }
23  
24 184 has plugin_instances => (
25 is => 'ro',
26 isa => 'ArrayRef',
27 lazy => 1,
28 builder => '_build_plugin_instances',
29 # constructor key kept as the original private name (compat)
30 init_arg => '_plugin_instances',
31 );
32  
33  
34 525 ▶sub _plugin_instances { $_[0]->plugin_instances }
Subroutine _plugin_instances: called CC=1 SCAR=0.0
35  
36 184 has plugin_args => (
37 is => 'ro',
38 isa => 'HashRef',
39 lazy => 1,
40 builder => '_build_plugin_args',
41 );
42  
43  
44 184 has _legacy_plugin_args => (
45 is => 'ro',
46 isa => 'HashRef',
47 init_arg => '_plugin_args',
48 predicate => '_has_legacy_plugin_args',
49 );
50  
51 81 ▶sub _build_plugin_args {
Subroutine _build_plugin_args: called CC=2 SCAR=6.9
52 81 my ( $self ) = @_;
53 81 ▶ return $self->_has_legacy_plugin_args ? $self->_legacy_plugin_args : {};
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_legacy_plugin_args ? : 3 78 81
54 }
55  
56 1 ▶sub _plugin_args { $_[0]->plugin_args }
Subroutine _plugin_args: called CC=1 SCAR=0.0
57  
58 203 ▶sub _parse_plugin_specs {
Subroutine _parse_plugin_specs: called CC=9 SCAR=22.2
59 203 my ( $self ) = @_;
60 203 my @raw = @{$self->plugins};
61 203 my @specs;
62 203 ▶ while (@raw) {
Branch: 1 branch
BranchTrueFalseTotal
if (@raw) 100 203 303
63 100 my $item = shift @raw;
64 100 ▶ if (ref $item && blessed($item) && $item->isa('Langertha::Plugin')) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $item and builtin::blessed($item) and $item->isa('Langertha::Plugin')) { } 11 89 100
elsif (not ref $item and @raw and ref $raw[0] eq 'HASH') { } 23 66 89
elsif (not ref $item) { } 66 0 66
Condition66%4/6
ref $item and builtin::blessed($item) and $item->isa("Langertha::Plugin")
!ll&&!rl&&r
89011
ref $item and builtin::blessed($item)
!ll&&!rl&&r
89011
MC/DC33%1/3
ref $item and builtin::blessed($item) and $item->isa("Langertha::Plugin")
ref $item builtin::blessed($item) $item->isa("Langertha::Plugin")
Truth table50%2/4
A and B and C
A= ref $itemB= builtin::blessed($item)C= $item->isa("Langertha::Plugin")
ABCresult
0XX0
10X0
1100
1111
65 11 push @specs, $item;
66 ▶ } elsif (!ref $item && @raw && ref $raw[0] eq 'HASH') {
Condition83%5/6
not ref $item and @raw and ref $raw[0] eq "HASH"
!ll&&!rl&&r
61523
not ref $item and @raw
!ll&&!rl&&r
06128
MC/DC66%2/3
not ref $item and @raw and ref $raw[0] eq "HASH"
not ref $item @raw ref $raw[0] eq "HASH"
Truth table75%3/4
not(A) and B and C
A= not ref $itemB= @rawC= ref $raw[0] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
67 23 push @specs, [ $item, shift @raw ];
68 } elsif (!ref $item) {
69 66 push @specs, $item;
70 }
71 }
72 203 return @specs;
73 }
74  
75 203 ▶sub _build_plugin_instances {
Subroutine _build_plugin_instances: called CC=10 SCAR=23.5
76 203 my ( $self ) = @_;
77 203 $log->debugf("[%s] Loading %d plugin(s)", ref $self, scalar @{$self->plugins});
78 203 my @instances;
79 203 for my $spec ($self->_parse_plugin_specs) {
80 100 ▶ if (ref $spec eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $spec eq 'ARRAY') { } 23 77 100
elsif (ref $spec and $spec->isa('Langertha::Plugin')) { } 11 66 77
81 23 my ($name, $args) = @$spec;
82 23 my $class = $self->_resolve_plugin_name($name);
83 23 push @instances, $class->new(%{$self->plugin_args}, %$args, host => $self);
84 ▶ } elsif (ref $spec && $spec->isa('Langertha::Plugin')) {
Condition66%2/3
ref $spec and $spec->isa("Langertha::Plugin")
!ll&&!rl&&r
66011
MC/DC50%1/2
ref $spec and $spec->isa("Langertha::Plugin")
ref $spec $spec->isa("Langertha::Plugin")
Truth table66%2/3
A and B
A= ref $specB= $spec->isa("Langertha::Plugin")
ABresult
0X0
100
111
85 11 push @instances, $spec;
86 } else {
87 66 my $class = $self->_resolve_plugin_name($spec);
88 66 push @instances, $class->new(%{$self->plugin_args}, host => $self);
89 }
90 }
91 # Build event registry and validate dependencies
92 203 my %provided;
93 203 for my $plugin (@instances) {
94 100 for my $event (@{$plugin->provides_events}) {
95 0 $provided{$event} = ref $plugin;
96 }
97 }
98 203 $self->{_event_providers} = \%provided;
99 203 for my $plugin (@instances) {
100 100 for my $event (@{$plugin->requires_events}) {
101 0 ▶ next if $provided{$event};
Branch: 1 branch
BranchTrueFalseTotal
if $provided{$event} 0 0 0
102 0 die "Plugin '" . ref($plugin) . "' requires event '$event' "
103 . "but no loaded plugin provides it (via provides_events)";
104 }
105 }
106 203 return \@instances;
107 }
108  
109 89 ▶sub _resolve_plugin_name {
Subroutine _resolve_plugin_name: called CC=8 SCAR=23.4
110 89 my ( $self, $name ) = @_;
111 # +FullClass::Name — load directly, skip prefix search
112 89 ▶ if ($name =~ s/^\+//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ s/^\+//) 60 29 89
113 60 use_module($name);
114 60 return $name;
115 }
116 29 ▶ if ($name =~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /::/) 17 12 29
117 17 eval { use_module($name) };
118 17 ▶ return $name if $name->isa('Langertha::Plugin');
Branch: 1 branch
BranchTrueFalseTotal
if $name->isa("Langertha::Plugin") 17 0 17
119 }
120 12 for my $prefix ('Langertha::Plugin', 'LangerthaX::Plugin') {
121 12 my $full = "${prefix}::${name}";
122 12 my $ok = eval { use_module($full); 1 };
123 12 ▶ return $full if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 12 0 12
124 }
125 # Check if already loaded (e.g. inline/test classes)
126 0 ▶ if ($name->can('new') && $name->isa('Langertha::Plugin')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name->can("new") and $name->isa("Langertha::Plugin")) 0 0 0
Condition0%0/3
$name->can("new") and $name->isa("Langertha::Plugin")
!ll&&!rl&&r
000
MC/DC0%0/2
$name->can("new") and $name->isa("Langertha::Plugin")
$name->can("new") $name->isa("Langertha::Plugin")
Truth table0%0/3
A and B
A= $name->can("new")B= $name->isa("Langertha::Plugin")
ABresult
0X0
100
111
127 0 return $name;
128 }
129 0 die "Plugin '$name' not found (tried Langertha::Plugin::$name, LangerthaX::Plugin::$name)";
130 }
131  
132 # --- Plugin event system ---
133  
134 0 ▶async sub fire_event_f {
Subroutine fire_event_f: not called CC=3 SCAR=24.8
Pod documented
135 0 my ( $self, $event_name, @args ) = @_;
136 0 $log->tracef("[%s] Firing event: %s", ref $self, $event_name);
137 0 my $method = "on_${event_name}";
138 0 my @results;
139 0 for my $plugin (@{$self->plugin_instances}) {
140 0 ▶ if ($plugin->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->can($method)) 0 0 0
141 0 push @results, await $plugin->$method(@args);
142 }
143 }
144 0 return @results;
145 184 }
146  
147  
148  
149 # --- Plugin hook dispatch ---
150  
151 65 ▶async sub plugin_pipeline_tool_call_f {
Subroutine plugin_pipeline_tool_call_f: called CC=3 SCAR=11.0
Pod documented
152 65 my ( $self, $name, $input ) = @_;
153 65 for my $plugin (@{$self->plugin_instances}) {
154 35 my @result = await $plugin->plugin_before_tool_call($name, $input);
155 35 ▶ return () unless @result;
Branch: 1 branch
BranchTrueFalseTotal
unless @result 5 30 35
156 30 ( $name, $input ) = @result;
157 }
158 60 return ( $name, $input );
159 184 }
160  
161  
162 61 ▶sub _plugin_pipeline_tool_call { shift->plugin_pipeline_tool_call_f(@_) }
Subroutine _plugin_pipeline_tool_call: called CC=1 SCAR=0.0
163  
164 184 1;
165  
166 __END__
167  
168 =pod
169  
170 =encoding UTF-8
171  
172 =head1 NAME
173  
174 Langertha::Role::PluginHost - Role for objects that host plugins (Raider, Engine)
175  
176 =head1 VERSION
177  
178 version 0.503
179  
180 =head1 DESCRIPTION
181  
182 Shared role consumed by L<Langertha::Raider>, L<Langertha::Chat>,
183 L<Langertha::Embedder>, L<Langertha::ImageGen>, and engine classes to
184 provide a plugin system with lifecycle management and a custom event bus.
185  
186 Plugins are instantiated lazily on first use. Each plugin receives the host
187 as its C<host> attribute. Short names are resolved to
188 C<Langertha::Plugin::$name> or C<LangerthaX::Plugin::$name>. Prefixing a
189 name with C<+> bypasses resolution and loads the class directly.
190  
191 =head2 plugins
192  
193 plugins => ['Langfuse']
194 plugins => [Langfuse => { trace_name => 'my-trace', auto_flush => 1 }]
195 plugins => [$plugin_instance]
196  
197 ArrayRef of plugin specifications. Each entry may be:
198  
199 =over 4
200  
201 =item * A short name string (resolved to C<Langertha::Plugin::$name>)
202  
203 =item * A short name followed by a HashRef of constructor args
204  
205 =item * A fully qualified class name (with or without C<+> prefix)
206  
207 =item * An already-instantiated L<Langertha::Plugin> object
208  
209 =back
210  
211 =head2 plugin_instances
212  
213 for my $plugin (@{ $host->plugin_instances }) {
214 await $plugin->plugin_before_llm_call($conversation, $iteration);
215 }
216  
217 Read-only ArrayRef of the instantiated L<Langertha::Plugin> objects, in the
218 order given in L</plugins>. Built lazily on first access: short names are
219 resolved, constructed with L</plugin_args> plus any per-plugin args and
220 C<< host => $self >>, and C<requires_events> is checked against the loaded
221 plugins' C<provides_events> (dies when a required event has no provider).
222 A host that runs its own hook chain (for example L<Langertha::Raider>)
223 iterates this list and calls the hook on each plugin itself.
224  
225 C<_plugin_instances> remains an alias of this reader.
226  
227 =head2 plugin_args
228  
229 plugins => ['MyPlugin'],
230 plugin_args => { verbose => 1 },
231  
232 HashRef of constructor arguments passed to every plugin the host builds from
233 a name. Per-plugin args (C<< Name => { ... } >> in L</plugins>) win over these;
234 C<host> always wins. Already-instantiated plugin objects are not touched.
235  
236 The older constructor key C<_plugin_args> is still accepted and used when
237 C<plugin_args> is not given; the C<_plugin_args> reader is an alias of this one.
238  
239 =head2 fire_event_f
240  
241 my @results = await $host->fire_event_f('history_saved', $data);
242  
243 Fires a custom event to all loaded plugins that implement
244 C<on_$event_name>. Returns a list of all plugin return values.
245 Plugins declare the events they provide via
246 L<Langertha::Plugin/provides_events> and the events they require via
247 L<Langertha::Plugin/requires_events>.
248  
249 =head1 SEE ALSO
250  
251 =over
252  
253 =item * L<Langertha::Plugin> - Base class for all plugins
254  
255 =item * L<Langertha::Plugin::Langfuse> - Observability plugin
256  
257 =item * L<Langertha::Chat> - Chat wrapper that consumes this role
258  
259 =item * L<Langertha::Embedder> - Embedder wrapper that consumes this role
260  
261 =item * L<Langertha::ImageGen> - Image generation wrapper that consumes this role
262  
263 =item * L<Langertha::Raider> - Autonomous agent that consumes this role
264  
265 =back
266  
267 =head2 plugin_pipeline_tool_call_f
268  
269 my @call = await $host->plugin_pipeline_tool_call_f($name, $input);
270 return skipped_result() unless @call;
271 my ( $tool_name, $tool_input ) = @call;
272  
273 Runs C<plugin_before_tool_call> through every plugin in L</plugin_instances>,
274 in order, as a pipeline: each plugin receives the C<($name, $input)> the
275 previous one returned. Returns a Future resolving to the final
276 C<($name, $input)> pair, or to the empty list as soon as any plugin returns
277 the empty list, meaning the tool call is skipped (later plugins are not
278 asked). With no plugins it returns the arguments unchanged. A plugin's
279 failure fails the returned Future.
280  
281 C<_plugin_pipeline_tool_call> remains an alias of this method.
282  
283 =head1 SUPPORT
284  
285 =head2 Issues
286  
287 Please report bugs and feature requests on GitHub at
288 L<https://github.com/Getty/langertha/issues>.
289  
290 =head2 IRC
291  
292 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
293  
294 =head1 CONTRIBUTING
295  
296 Contributions are welcome! Please fork the repository and submit a pull request.
297  
298 =head1 AUTHOR
299  
300 Torsten Raudssus <getty@cpan.org>
301  
302 =head1 COPYRIGHT AND LICENSE
303  
304 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
305  
306 This is free software; you can redistribute it and/or modify it under
307 the same terms as the Perl 5 programming language system itself.
308  
309 =cut
« blib/lib/Langertha/HTTP/UserAgent.pm ↑ Summary blib/lib/Langertha/Manifest/Validation.pm »