Langertha/Role/PluginHost.pm

filter: Statementstmt 100% 97 / 97 Branchbran 93.3% 28 / 30 Conditioncond 72.2% 13 / 18 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 92.8% 156 / 168 CC 30 SCAR 19.0
CC
30
Cov
95%
_build_plugin_instances
10.0
_parse_plugin_specs
9.2
_resolve_plugin_name
8.0
CRAP 6.7
« Langertha/Engine/AKI.pm ↑ Summary Langertha/RunContext.pm »
1 package Langertha::Role::PluginHost;
2 # ABSTRACT: Role for objects that host plugins (Raider, Engine)
3 41 our $VERSION = '0.502';
4 41 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 41 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 41 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 41 ▶use Module::Runtime qw( use_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 41 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 41 has plugins => (
12 is => 'ro',
13 isa => 'ArrayRef',
14 lazy => 1,
15 builder => '_build_plugins',
16 );
17  
18  
19 sub _build_plugins {
20 25 ▶ my ( $self ) = @_;
Subroutine _build_plugins: called CC=2 SCAR=6.9
21 25 ▶ return $self->can('_sugar_plugins') ? $self->_sugar_plugins : [];
Branch: 1 branch
BranchTrueFalseTotal
$self->can('_sugar_plugins') ? : 4 21 25
22 }
23  
24 41 has _plugin_instances => (
25 is => 'ro',
26 isa => 'ArrayRef',
27 lazy => 1,
28 builder => '_build_plugin_instances',
29 );
30  
31 has _plugin_args => (
32 is => 'ro',
33 isa => 'HashRef',
34 374 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 41 );
36  
37 sub _parse_plugin_specs {
38 73 ▶ my ( $self ) = @_;
Subroutine _parse_plugin_specs: called CC=9 SCAR=22.2
39 73 my @raw = @{$self->plugins};
40 73 my @specs;
41 73 ▶ while (@raw) {
Branch: 1 branch
BranchTrueFalseTotal
if (@raw) 62 73 135
42 62 my $item = shift @raw;
43 62 ▶ 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')) { } 10 52 62
elsif (not ref $item and @raw and ref $raw[0] eq 'HASH') { } 10 42 52
elsif (not ref $item) { } 42 0 42
Condition66%4/6
ref $item and builtin::blessed($item) and $item->isa("Langertha::Plugin")
!ll&&!rl&&r
52010
ref $item and builtin::blessed($item)
!ll&&!rl&&r
52010
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
44 10 push @specs, $item;
45 ▶ } 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
34810
not ref $item and @raw
!ll&&!rl&&r
03418
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
46 10 push @specs, [ $item, shift @raw ];
47 } elsif (!ref $item) {
48 42 push @specs, $item;
49 }
50 }
51 73 return @specs;
52 }
53  
54 sub _build_plugin_instances {
55 73 ▶ my ( $self ) = @_;
Subroutine _build_plugin_instances: called CC=10 SCAR=23.0
56 73 $log->debugf("[%s] Loading %d plugin(s)", ref $self, scalar @{$self->plugins});
57 73 my @instances;
58 73 for my $spec ($self->_parse_plugin_specs) {
59 62 ▶ if (ref $spec eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $spec eq 'ARRAY') { } 10 52 62
elsif (ref $spec and $spec->isa('Langertha::Plugin')) { } 10 42 52
60 10 my ($name, $args) = @$spec;
61 10 my $class = $self->_resolve_plugin_name($name);
62 10 push @instances, $class->new(%{$self->_plugin_args}, %$args, host => $self);
63 ▶ } elsif (ref $spec && $spec->isa('Langertha::Plugin')) {
Condition66%2/3
ref $spec and $spec->isa("Langertha::Plugin")
!ll&&!rl&&r
42010
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
64 10 push @instances, $spec;
65 } else {
66 42 my $class = $self->_resolve_plugin_name($spec);
67 41 push @instances, $class->new(%{$self->_plugin_args}, host => $self);
68 }
69 }
70 # Build event registry and validate dependencies
71 72 my %provided;
72 72 for my $plugin (@instances) {
73 61 for my $event (@{$plugin->provides_events}) {
74 5 $provided{$event} = ref $plugin;
75 }
76 }
77 72 $self->{_event_providers} = \%provided;
78 72 for my $plugin (@instances) {
79 61 for my $event (@{$plugin->requires_events}) {
80 6 ▶ next if $provided{$event};
Branch: 1 branch
BranchTrueFalseTotal
if $provided{$event} 4 2 6
81 2 die "Plugin '" . ref($plugin) . "' requires event '$event' "
82 . "but no loaded plugin provides it (via provides_events)";
83 }
84 }
85 70 return \@instances;
86 }
87  
88 sub _resolve_plugin_name {
89 52 ▶ my ( $self, $name ) = @_;
Subroutine _resolve_plugin_name: called CC=8 SCAR=20.8
90 # +FullClass::Name — load directly, skip prefix search
91 52 ▶ if ($name =~ s/^\+//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ s/^\+//) 20 32 52
92 20 use_module($name);
93 20 return $name;
94 }
95 32 ▶ if ($name =~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /::/) 27 5 32
96 27 eval { use_module($name) };
97 27 ▶ return $name if $name->isa('Langertha::Plugin');
Branch: 1 branch
BranchTrueFalseTotal
if $name->isa("Langertha::Plugin") 27 0 27
98 }
99 5 for my $prefix ('Langertha::Plugin', 'LangerthaX::Plugin') {
100 9 my $full = "${prefix}::${name}";
101 9 my $ok = eval { use_module($full); 1 };
102 9 ▶ return $full if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 1 8 9
103 }
104 # Check if already loaded (e.g. inline/test classes)
105 4 ▶ if ($name->can('new') && $name->isa('Langertha::Plugin')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name->can("new") and $name->isa("Langertha::Plugin")) 3 1 4
Condition66%2/3
$name->can("new") and $name->isa("Langertha::Plugin")
!ll&&!rl&&r
103
MC/DC50%1/2
$name->can("new") and $name->isa("Langertha::Plugin")
$name->can("new") $name->isa("Langertha::Plugin")
Truth table66%2/3
A and B
A= $name->can("new")B= $name->isa("Langertha::Plugin")
ABresult
0X0
100
111
106 3 return $name;
107 }
108 1 die "Plugin '$name' not found (tried Langertha::Plugin::$name, LangerthaX::Plugin::$name)";
109 }
110  
111 # --- Plugin event system ---
112  
113 4 ▶async sub fire_event_f {
Subroutine fire_event_f: called CC=3 SCAR=11.0
Pod documented
114 4 my ( $self, $event_name, @args ) = @_;
115 4 $log->tracef("[%s] Firing event: %s", ref $self, $event_name);
116 4 my $method = "on_${event_name}";
117 4 my @results;
118 4 for my $plugin (@{$self->_plugin_instances}) {
119 7 ▶ if ($plugin->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->can($method)) 3 4 7
120 3 push @results, await $plugin->$method(@args);
121 }
122 }
123 4 return @results;
124 41 }
125  
126  
127  
128 # --- Plugin hook dispatch ---
129  
130 7 ▶async sub _plugin_pipeline_tool_call {
Subroutine _plugin_pipeline_tool_call: called CC=3 SCAR=11.0
131 7 my ( $self, $name, $input ) = @_;
132 7 for my $plugin (@{$self->_plugin_instances}) {
133 6 my @result = await $plugin->plugin_before_tool_call($name, $input);
134 6 ▶ return () unless @result;
Branch: 1 branch
BranchTrueFalseTotal
unless @result 2 4 6
135 4 ( $name, $input ) = @result;
136 }
137 5 return ( $name, $input );
138 41 }
139  
140 41 1;
141  
142 __END__
143  
144 =pod
145  
146 =encoding UTF-8
147  
148 =head1 NAME
149  
150 Langertha::Role::PluginHost - Role for objects that host plugins (Raider, Engine)
151  
152 =head1 VERSION
153  
154 version 0.502
155  
156 =head1 DESCRIPTION
157  
158 Shared role consumed by L<Langertha::Raider>, L<Langertha::Chat>,
159 L<Langertha::Embedder>, L<Langertha::ImageGen>, and engine classes to
160 provide a plugin system with lifecycle management and a custom event bus.
161  
162 Plugins are instantiated lazily on first use. Each plugin receives the host
163 as its C<host> attribute. Short names are resolved to
164 C<Langertha::Plugin::$name> or C<LangerthaX::Plugin::$name>. Prefixing a
165 name with C<+> bypasses resolution and loads the class directly.
166  
167 =head2 plugins
168  
169 plugins => ['Langfuse']
170 plugins => [Langfuse => { trace_name => 'my-trace', auto_flush => 1 }]
171 plugins => [$plugin_instance]
172  
173 ArrayRef of plugin specifications. Each entry may be:
174  
175 =over 4
176  
177 =item * A short name string (resolved to C<Langertha::Plugin::$name>)
178  
179 =item * A short name followed by a HashRef of constructor args
180  
181 =item * A fully qualified class name (with or without C<+> prefix)
182  
183 =item * An already-instantiated L<Langertha::Plugin> object
184  
185 =back
186  
187 =head2 fire_event_f
188  
189 my @results = await $host->fire_event_f('history_saved', $data);
190  
191 Fires a custom event to all loaded plugins that implement
192 C<on_$event_name>. Returns a list of all plugin return values.
193 Plugins declare the events they provide via
194 L<Langertha::Plugin/provides_events> and the events they require via
195 L<Langertha::Plugin/requires_events>.
196  
197 =head1 SEE ALSO
198  
199 =over
200  
201 =item * L<Langertha::Plugin> - Base class for all plugins
202  
203 =item * L<Langertha::Plugin::Langfuse> - Observability plugin
204  
205 =item * L<Langertha::Chat> - Chat wrapper that consumes this role
206  
207 =item * L<Langertha::Embedder> - Embedder wrapper that consumes this role
208  
209 =item * L<Langertha::ImageGen> - Image generation wrapper that consumes this role
210  
211 =item * L<Langertha::Raider> - Autonomous agent that consumes this role
212  
213 =back
214  
215 =head1 SUPPORT
216  
217 =head2 Issues
218  
219 Please report bugs and feature requests on GitHub at
220 L<https://github.com/Getty/langertha/issues>.
221  
222 =head2 IRC
223  
224 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
225  
226 =head1 CONTRIBUTING
227  
228 Contributions are welcome! Please fork the repository and submit a pull request.
229  
230 =head1 AUTHOR
231  
232 Torsten Raudssus <getty@cpan.org>
233  
234 =head1 COPYRIGHT AND LICENSE
235  
236 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
237  
238 This is free software; you can redistribute it and/or modify it under
239 the same terms as the Perl 5 programming language system itself.
240  
241 =cut
« Langertha/Engine/AKI.pm ↑ Summary Langertha/RunContext.pm »