| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 100 | my $item = shift @raw; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 100 | ▶ | if (ref $item && blessed($item) && $item->isa('Langertha::Plugin')) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%4/6
ref $item and builtin::blessed($item) and $item->isa("Langertha::Plugin")
ref $item and builtin::blessed($item)
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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"
not ref $item and @raw
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 60 | use_module($name); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 60 | return $name; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 29 | ▶ | if ($name =~ /::/) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 17 | eval { use_module($name) }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 17 | ▶ | return $name if $name->isa('Langertha::Plugin'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | # Check if already loaded (e.g. inline/test classes) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if ($name->can('new') && $name->isa('Langertha::Plugin')) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$name->can("new") and $name->isa("Langertha::Plugin")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||