blib/lib/SignalWire/Skills/SkillRegistry.pm

filter: Statementstmt 95.0% 58 / 61 Branchbran 79.1% 19 / 24 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 6 totaltotal 82.5% 90 / 109 CC 17 SCAR 18.4
CC
17
Cov
88%
get_all_skills_schema
12.6
get_factory
4.0
add_skill_directory
4.0
CRAP 6.3
« examples/swmlservice_ai_sidecar.pl ↑ Summary blib/lib/SignalWire/SWML/Schema.pm »
1 package SignalWire::Skills::SkillRegistry;
2 # Copyright (c) 2025 SignalWire
3 # Licensed under the MIT License.
4  
5 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Global registry mapping skill name -> class name
9 20 my %REGISTRY;
10 # External skill directories registered via add_skill_directory.
11 # Mirrors Python's SkillRegistry._external_paths.
12 my @EXTERNAL_PATHS;
13  
14 73 ▶sub register_skill {
Subroutine register_skill: called CC=1 SCAR=0.0
Pod undocumented
15 73 my ($class, $skill_name, $skill_class) = @_;
16 73 $REGISTRY{$skill_name} = $skill_class;
17 }
18  
19 221 ▶sub get_factory {
Subroutine get_factory: called CC=4 SCAR=13.9
Pod undocumented
20 221 my ($class, $skill_name) = @_;
21  
22 # Return if already registered
23 221 ▶ return $REGISTRY{$skill_name} if exists $REGISTRY{$skill_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $REGISTRY{$skill_name} 129 92 221
24  
25 # Attempt to auto-load from Builtin namespace
26 92 my $module = 'SignalWire::Skills::Builtin::' . _camelize($skill_name);
27 92 eval "require $module"; ## no critic
28 92 ▶ if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 89 3 92
29 # If the module registered itself, return it
30 89 ▶ return $REGISTRY{$skill_name} if exists $REGISTRY{$skill_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $REGISTRY{$skill_name} 71 18 89
31 # Otherwise register it
32 18 $REGISTRY{$skill_name} = $module;
33 18 return $module;
34 }
35  
36 3 return undef;
37 }
38  
39 6 ▶sub list_skills {
Subroutine list_skills: called CC=1 SCAR=0.0
Pod undocumented
40 6 my ($class) = @_;
41 # Make sure all builtins are loaded
42 6 $class->_load_all_builtins;
43 6 return [ sort keys %REGISTRY ];
44 }
45  
46 # Get complete schema for all registered skills.
47 #
48 # Mirrors Python's ``SkillRegistry.get_all_skills_schema()`` — returns a
49 # hashref keyed by skill name where each value carries metadata + the
50 # parameter schema for that skill. Perl skills don't carry rich
51 # Python-style parameter introspection in v1, so the value defaults to
52 # the minimal shape with just the skill name; built-in skills that
53 # expose ``parameter_schema`` get richer detail.
54 1 ▶sub get_all_skills_schema {
Subroutine get_all_skills_schema: called CC=10 SCAR=25.4
Pod undocumented
55 1 my ($self) = @_;
56 # Accept both class-method (SkillRegistry->get_all_skills_schema) and
57 # instance-method ($registry->get_all_skills_schema) calls.
58 1 ▶ my $class = ref($self) || $self || __PACKAGE__;
Condition40%2/5
ref $self || $self || __PACKAGE__
l!l
10
ref $self or $self
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $self || $self || __PACKAGE__
ref $self $self
Truth table33%1/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
59 1 $class->_load_all_builtins;
60 1 my %schema;
61 1 for my $name (sort keys %REGISTRY) {
62 19 my $skill_class = $REGISTRY{$name};
63 19 my %entry = (name => $name, parameters => {});
64 19 ▶ if (ref($skill_class) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $skill_class eq 'CODE') { } 0 19 19
elsif (defined $skill_class) { } 19 0 19
65 # Factory closure; can't introspect statically
66 ;
67 } elsif (defined $skill_class) {
68 19 ▶ if ($skill_class->can('parameter_schema')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skill_class->can("parameter_schema")) 0 19 19
69 0 eval {
70 0 my $params = $skill_class->parameter_schema;
71 0 ▶ $entry{parameters} = $params if ref($params) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $params eq "HASH" 0 0 0
72 };
73 }
74 19 ▶ if ($skill_class->can('skill_description')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skill_class->can("skill_description")) 18 1 19
75 18 eval { $entry{description} = $skill_class->skill_description };
76 }
77 19 ▶ if ($skill_class->can('skill_version')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skill_class->can("skill_version")) 18 1 19
78 18 eval { $entry{version} = $skill_class->skill_version };
79 }
80 }
81 19 $schema{$name} = \%entry;
82 }
83 1 return \%schema;
84 }
85  
86 7 ▶sub _load_all_builtins {
Subroutine _load_all_builtins: called CC=2 SCAR=6.9
87 7 my ($class) = @_;
88 7 my @names = qw(
89 api_ninjas_trivia claude_skills datasphere datasphere_serverless
90 datetime google_maps info_gatherer joke math mcp_gateway
91 native_vector_search play_background_file spider swml_transfer
92 weather_api web_search wikipedia_search custom_skills
93 );
94 7 for my $name (@names) {
95 126 $class->get_factory($name); # triggers auto-load
96 }
97 }
98  
99 92 ▶sub _camelize {
Subroutine _camelize: called CC=1 SCAR=0.0
100 92 my ($name) = @_;
101 # Convert snake_case to CamelCase: api_ninjas_trivia -> ApiNinjasTrivia
102 92 $name =~ s/_(.)/uc($1)/ge;
103 92 return ucfirst($name);
104 }
105  
106 7 ▶sub clear_registry {
Subroutine clear_registry: called CC=1 SCAR=0.0
Pod undocumented
107 7 %REGISTRY = ();
108 7 @EXTERNAL_PATHS = ();
109 }
110  
111 # Add a directory to search for skills.
112 #
113 # Mirrors Python's
114 # `signalwire.skills.registry.SkillRegistry.add_skill_directory`:
115 # validate the path, die with an "X: <path>" message (Perl's analog of
116 # raising ValueError) when the path doesn't exist or isn't a directory,
117 # and de-duplicate entries in the external paths list.
118 7 ▶sub add_skill_directory {
Subroutine add_skill_directory: called CC=4 SCAR=13.9
Pod undocumented
119 7 my ($class, $path) = @_;
120 7 ▶ die "Skill directory does not exist: $path\n" unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 2 5 7
121 5 ▶ die "Path is not a directory: $path\n" unless -d $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 1 4 5
122 4 ▶ return if grep { $_ eq $path } @EXTERNAL_PATHS;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $path;} @EXTERNAL_PATHS 1 3 4
123 3 push @EXTERNAL_PATHS, $path;
124 3 return;
125 }
126  
127 # Returns the registered external skill directories.
128 # Parity surface for Python's private `_external_paths` attribute —
129 # exposed under the underscored name so the signature enumerator skips
130 # it (matches Python convention).
131 3 ▶sub _external_paths {
Subroutine _external_paths: called CC=1 SCAR=0.0
132 3 my ($class) = @_;
133 3 return [@EXTERNAL_PATHS];
134 }
135  
136 20 1;
« examples/swmlservice_ai_sidecar.pl ↑ Summary blib/lib/SignalWire/SWML/Schema.pm »