blib/lib/Langertha/Role/PluginHost.pm

filter: Statementstmt 82.6% 86 / 104 Branchbran 64.7% 22 / 34 Conditioncond 61.1% 11 / 18 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 87.5% 14 / 16 Podpod 100% 2 / 2 totaltotal 75.5% 139 / 184 CC 57 SCAR 20.9
CC
57
Cov
76%
fire_event_f
12.0
_build_plugin_instances
10.5
_build_plugin_instances
10.5
CRAP 8.1
« blib/lib/Langertha/Role/Embedding.pm ↑ Summary blib/lib/Langertha/VLLMHook/Config.pm »
1 package Langertha::Role::PluginHost;
2 # ABSTRACT: Role for objects that host plugins (Raider, Engine)
3 199 our $VERSION = '0.505';
4 199 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 199 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 199 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 199 ▶use Module::Runtime qw( use_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 199 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 199 has plugins => (
12 is => 'ro',
13 isa => 'ArrayRef',
14 lazy => 1,
15 builder => '_build_plugins',
16 );
17  
18  
19 111 ▶sub _build_plugins {
Subroutine _build_plugins: called CC=2 SCAR=10.9
20 0 ▶ my ( $self ) = @_;
Subroutine _build_plugins: not called CC=2 SCAR=10.9
21 0 ▶ return $self->can('_sugar_plugins') ? $self->_sugar_plugins : [];
Branch: 2 branches
BranchTrueFalseTotal
$self->can('_sugar_plugins') ? : 0 111 111
$self->can('_sugar_plugins') ? : 0 0 0
22 }
23  
24 199 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 535 ▶sub _plugin_instances { $_[0]->plugin_instances }
Subroutine _plugin_instances: called CC=1 SCAR=0.0
35  
36 199 has plugin_args => (
37 is => 'ro',
38 isa => 'HashRef',
39 lazy => 1,
40 builder => '_build_plugin_args',
41 );
42  
43  
44 199 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 204 ▶sub _parse_plugin_specs {
Subroutine _parse_plugin_specs: called CC=9 SCAR=22.2
59 204 my ( $self ) = @_;
60 204 my @raw = @{$self->plugins};
61 204 my @specs;
62 204 ▶ while (@raw) {
Branch: 1 branch
BranchTrueFalseTotal
if (@raw) 100 204 304
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 204 return @specs;
73 }
74  
75 204 ▶sub _build_plugin_instances {
Subroutine _build_plugin_instances: called CC=10 SCAR=23.5
76 204 my ( $self ) = @_;
77 204 $log->debugf("[%s] Loading %d plugin(s)", ref $self, scalar @{$self->plugins});
78 204 my @instances;
79 204 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 204 my %provided;
93 204 for my $plugin (@instances) {
94 100 for my $event (@{$plugin->provides_events}) {
95 0 $provided{$event} = ref $plugin;
96 }
97 }
98 204 $self->{_event_providers} = \%provided;
99 204 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 204 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 199 }
146  
147  
148  
149 # --- Plugin hook dispatch ---
150  
151 67 ▶async sub plugin_pipeline_tool_call_f {
Subroutine plugin_pipeline_tool_call_f: called CC=3 SCAR=11.0
Pod documented
152 67 my ( $self, $name, $input ) = @_;
153 67 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 62 return ( $name, $input );
159 199 }
160  
161  
162 63 ▶sub _plugin_pipeline_tool_call { shift->plugin_pipeline_tool_call_f(@_) }
Subroutine _plugin_pipeline_tool_call: called CC=1 SCAR=0.0
163  
164 199 1;
165  
166 __END__
« blib/lib/Langertha/Role/Embedding.pm ↑ Summary blib/lib/Langertha/VLLMHook/Config.pm »