« blib/lib/SignalWire/Agents/Relay/Call.pm
↑ Summary
blib/lib/SignalWire/Agents/Agent/AgentBase.pm »
| 1 | package SignalWire::Agents::Skills::SkillManager; | ||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2025 SignalWire | ||||||||||||||||||||||||||||
| 3 | # Licensed under the MIT License. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | has agent => (is => 'ro', required => 1, weak_ref => 1); | |||||||||||||||||||||||||||
| 11 | 10 | ▶ | has loaded_skills => (is => 'rw', default => sub { {} }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 12 | ▶ | sub load_skill { | ||||||||||||||||||||||||||
|
Subroutine
load_skill: called CC=20 SCAR=33.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 12 | my ($self, $skill_name, $skill_class, $params) = @_; | |||||||||||||||||||||||||||
| 15 | 12 | ▶ | $params //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$params //= {}
MC/DC100%1/1
$params //= {}
$params
Truth table100%2/2
A
A= $params
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # Get class from registry if not provided | ||||||||||||||||||||||||||||
| 18 | 12 | ▶ | if (!$skill_class) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 12 | require SignalWire::Agents::Skills::SkillRegistry; | |||||||||||||||||||||||||||
| 20 | 12 | my $factory = SignalWire::Agents::Skills::SkillRegistry->get_factory($skill_name); | |||||||||||||||||||||||||||
| 21 | 12 | ▶ | unless ($factory) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 1 | return (0, "Skill '$skill_name' not found in registry"); | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | 11 | $skill_class = $factory; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Create instance | ||||||||||||||||||||||||||||
| 28 | 11 | my $instance = eval { | |||||||||||||||||||||||||||
| 29 | 11 | $skill_class->new( | |||||||||||||||||||||||||||
| 30 | agent => $self->agent, | ||||||||||||||||||||||||||||
| 31 | params => { %$params }, | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | }; | ||||||||||||||||||||||||||||
| 34 | 11 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | return (0, "Failed to create skill '$skill_name': $@"); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 11 | my $instance_key = $instance->get_instance_key; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Check for duplicates | ||||||||||||||||||||||||||||
| 41 | 11 | ▶ | if (exists $self->loaded_skills->{$instance_key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 1 | ▶ | if (!$instance->supports_multiple_instances) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 1 | return (0, "Skill '$skill_name' already loaded and does not support multiple instances"); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Validate env vars | ||||||||||||||||||||||||||||
| 48 | 10 | ▶ | unless ($instance->validate_env_vars) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | return (0, "Skill '$skill_name' missing required environment variables"); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # Setup | ||||||||||||||||||||||||||||
| 53 | 10 | my $ok = eval { $instance->setup }; | |||||||||||||||||||||||||||
| 54 | 10 | ▶ | if ($@ || !$ok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ or not $ok
MC/DC0%0/2
$@ or not $ok
$@
not $ok
Truth table33%1/3
A or not(B)
A= $@B= not $ok
| |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | my $err = $@ || 'setup() returned false'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$@ || 'setup() returned false'
MC/DC0%0/1
$@ || 'setup() returned false'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||
| 56 | 0 | return (0, "Skill '$skill_name' setup failed: $err"); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Register tools | ||||||||||||||||||||||||||||
| 60 | 10 | eval { $instance->register_tools }; | |||||||||||||||||||||||||||
| 61 | 10 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | return (0, "Skill '$skill_name' register_tools failed: $@"); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Merge hints | ||||||||||||||||||||||||||||
| 66 | 10 | my $hints = $instance->get_hints; | |||||||||||||||||||||||||||
| 67 | 10 | ▶ | if ($hints && @$hints) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$hints and @$hints
MC/DC50%1/2
$hints and @$hints
$hints
@$hints
Truth table66%2/3
A and B
A= $hintsB= @$hints
| |||||||||||||||||||||||||||||
| 68 | 1 | $self->agent->add_hints(@$hints); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Merge global data | ||||||||||||||||||||||||||||
| 72 | 10 | my $gdata = $instance->get_global_data; | |||||||||||||||||||||||||||
| 73 | 10 | ▶ | if ($gdata && %$gdata) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$gdata and %$gdata
MC/DC0%0/2
$gdata and %$gdata
$gdata
%$gdata
Truth table33%1/3
A and B
A= $gdataB= %$gdata
| |||||||||||||||||||||||||||||
| 74 | 0 | $self->agent->update_global_data($gdata); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # Add prompt sections | ||||||||||||||||||||||||||||
| 78 | 10 | my $sections = $instance->get_prompt_sections; | |||||||||||||||||||||||||||
| 79 | 10 | ▶ | if ($sections && @$sections) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$sections and @$sections
MC/DC50%1/2
$sections and @$sections
$sections
@$sections
Truth table66%2/3
A and B
A= $sectionsB= @$sections
| |||||||||||||||||||||||||||||
| 80 | 8 | for my $sec (@$sections) { | |||||||||||||||||||||||||||
| 81 | $self->agent->prompt_add_section( | ||||||||||||||||||||||||||||
| 82 | $sec->{title}, | ||||||||||||||||||||||||||||
| 83 | $sec->{body}, | ||||||||||||||||||||||||||||
| 84 | 8 | ▶ | ($sec->{bullets} ? (bullets => $sec->{bullets}) : ()), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 10 | $self->loaded_skills->{$instance_key} = $instance; | |||||||||||||||||||||||||||
| 90 | 10 | return (1, ''); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | ▶ | sub unload_skill { | ||||||||||||||||||||||||||
|
Subroutine
unload_skill: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | 1 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 95 | 1 | my $instance = delete $self->loaded_skills->{$key}; | |||||||||||||||||||||||||||
| 96 | 1 | ▶ | if ($instance) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 1 | eval { $instance->cleanup }; | |||||||||||||||||||||||||||
| 98 | 1 | return 1; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 0 | return 0; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1 | ▶ | sub list_skills { | ||||||||||||||||||||||||||
|
Subroutine
list_skills: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 104 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 105 | 1 | return [ keys %{ $self->loaded_skills } ]; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 5 | ▶ | sub has_skill { | ||||||||||||||||||||||||||
|
Subroutine
has_skill: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 5 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 110 | 5 | ▶ | return exists $self->loaded_skills->{$key} ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 2 | 1; | |||||||||||||||||||||||||||