blib/lib/SignalWire/Agents/Skills/SkillManager.pm

filter: Statementstmt 89.5% 60 / 67 Branchbran 67.8% 19 / 28 Conditioncond 50.0% 8 / 16 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 4 totaltotal 73.8% 99 / 134 CC 22 SCAR 29.7
CC
22
Cov
78%
load_skill
28.6
unload_skill
2.0
has_skill
2.0
CRAP 19.6
« 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 //= {}
l!l
48
MC/DC100%1/1
$params //= {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
16  
17 # Get class from registry if not provided
18 12 ▶ if (!$skill_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($skill_class) 12 0 12
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
BranchTrueFalseTotal
unless ($factory) 1 11 12
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
BranchTrueFalseTotal
if ($@) 0 11 11
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
BranchTrueFalseTotal
if (exists $self->loaded_skills->{$instance_key}) 1 10 11
42 1 ▶ if (!$instance->supports_multiple_instances) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($instance->supports_multiple_instances) 1 0 1
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
BranchTrueFalseTotal
unless ($instance->validate_env_vars) 0 10 10
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
BranchTrueFalseTotal
if ($@ or not $ok) 0 10 10
Condition33%1/3
$@ or not $ok
l!l&&r!l&&!r
0010
MC/DC0%0/2
$@ or not $ok
$@ not $ok
Truth table33%1/3
A or not(B)
A= $@B= not $ok
ABresult
1X1
011
000
55 0 ▶ my $err = $@ || 'setup() returned false';
Condition0%0/2
$@ || 'setup() returned false'
l!l
00
MC/DC0%0/1
$@ || 'setup() returned false'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
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
BranchTrueFalseTotal
if ($@) 0 10 10
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
BranchTrueFalseTotal
if ($hints and @$hints) 1 9 10
Condition66%2/3
$hints and @$hints
!ll&&!rl&&r
091
MC/DC50%1/2
$hints and @$hints
$hints @$hints
Truth table66%2/3
A and B
A= $hintsB= @$hints
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($gdata and %$gdata) 0 10 10
Condition33%1/3
$gdata and %$gdata
!ll&&!rl&&r
0100
MC/DC0%0/2
$gdata and %$gdata
$gdata %$gdata
Truth table33%1/3
A and B
A= $gdataB= %$gdata
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($sections and @$sections) 8 2 10
Condition66%2/3
$sections and @$sections
!ll&&!rl&&r
028
MC/DC50%1/2
$sections and @$sections
$sections @$sections
Truth table66%2/3
A and B
A= $sectionsB= @$sections
ABresult
0X0
100
111
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
BranchTrueFalseTotal
$sec->{'bullets'} ? : 8 0 8
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
BranchTrueFalseTotal
if ($instance) 1 0 1
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
BranchTrueFalseTotal
exists $self->loaded_skills->{$key} ? : 3 2 5
111 }
112  
113 2 1;
« blib/lib/SignalWire/Agents/Relay/Call.pm ↑ Summary blib/lib/SignalWire/Agents/Agent/AgentBase.pm »