blib/lib/SignalWire/Skills/Builtin/ApiNinjasTrivia.pm

filter: Statementstmt 95.2% 40 / 42 Branchbran 50.0% 1 / 2 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 3 totaltotal 88.2% 60 / 68 CC 5 SCAR 6.5
CC
5
Cov
92%
register_tools
4.0
_trivia_url
2.3
BEGIN
1.0
CRAP 1.9
« blib/lib/SignalWire.pm ↑ Summary blib/lib/SignalWire/Skills/Builtin/GoogleMaps.pm »
1 package SignalWire::Skills::Builtin::ApiNinjasTrivia;
2 # Copyright (c) 2025 SignalWire
3 # Licensed under the MIT License.
4 #
5 # DataMap-based API Ninjas trivia skill. Mirrors signalwire-python's
6 # skills/api_ninjas_trivia/skill.py:get_tools — the SDK does NOT issue
7 # the HTTP request itself; the SignalWire SWML platform fetches the
8 # webhook URL described in the data_map, runs the response template,
9 # and returns the formatted result to the LLM. The SDK's job here is
10 # to register the right DataMap shape with the right URL/headers.
11  
12 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 extends 'SignalWire::Skills::SkillBase';
17  
18 4 ▶use SignalWire::Skills::SkillRegistry;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 SignalWire::Skills::SkillRegistry->register_skill('api_ninjas_trivia', __PACKAGE__);
20  
21 10 ▶has '+skill_name' => (default => sub { 'api_ninjas_trivia' });
Subroutine __ANON__: called CC=1 SCAR=0.0
22 10 ▶has '+skill_description' => (default => sub { 'Get trivia questions from API Ninjas' });
Subroutine __ANON__: called CC=1 SCAR=0.0
23 10 ▶has '+supports_multiple_instances' => (default => sub { 1 });
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25 4 my @ALL_CATEGORIES = qw(
26 artliterature language sciencenature general fooddrink
27 peopleplaces geography historyholidays entertainment
28 toysgames music mathematics religionmythology
29 sportsleisure
30 );
31  
32 # Honor API_NINJAS_BASE_URL env var so the audit fixture
33 # (audit_skills_dispatch.py) can redirect us at a local HTTP server.
34 # When unset we use the canonical https://api.api-ninjas.com/v1/trivia
35 # URL Python writes verbatim. The audit's expected_path_substring is
36 # `trivia`, which is preserved either way.
37 5 ▶sub _trivia_url {
Subroutine _trivia_url: called CC=2 SCAR=8.4
38 5 my $override = $ENV{API_NINJAS_BASE_URL};
39 5 ▶ if ($override) {
Branch: 1 branch
BranchTrueFalseTotal
if ($override) 0 5 5
40 0 $override =~ s{/+$}{};
41 0 return "$override/v1/trivia";
42 }
43 5 return 'https://api.api-ninjas.com/v1/trivia';
44 }
45  
46 6 ▶sub setup { 1 }
Subroutine setup: called CC=1 SCAR=0.0
Pod undocumented
47  
48 5 ▶sub register_tools {
Subroutine register_tools: called CC=4 SCAR=13.9
Pod undocumented
49 5 my ($self) = @_;
50 5 ▶ my $tool_name = $self->params->{tool_name} // 'get_trivia';
Condition100%2/2
$self->params->{"tool_name"} // "get_trivia"
l!l
32
MC/DC100%1/1
$self->params->{"tool_name"} // "get_trivia"
$self->params->{"tool_name"}
Truth table100%2/2
A
A= $self->params->{"tool_name"}
Aresult
11
00
51 5 ▶ my $api_key = $self->params->{api_key} // '';
Condition50%1/2
$self->params->{"api_key"} // ""
l!l
05
MC/DC0%0/1
$self->params->{"api_key"} // ""
$self->params->{"api_key"}
Truth table50%1/2
A
A= $self->params->{"api_key"}
Aresult
11
00
52 5 ▶ my $categories = $self->params->{categories} // [@ALL_CATEGORIES];
Condition100%2/2
$self->params->{"categories"} // [@ALL_CATEGORIES]
l!l
14
MC/DC100%1/1
$self->params->{"categories"} // [@ALL_CATEGORIES]
$self->params->{"categories"}
Truth table100%2/2
A
A= $self->params->{"categories"}
Aresult
11
00
53  
54 5 require SignalWire::SWAIG::FunctionResult;
55  
56 5 my $no_results = SignalWire::SWAIG::FunctionResult->new(
57 response => 'Sorry, I cannot get trivia questions right now. Please try again later.',
58 )->to_hash;
59 5 my $on_success = SignalWire::SWAIG::FunctionResult->new(
60 response => 'Category %{array[0].category} question: %{array[0].question} '
61 . 'Answer: %{array[0].answer}, be sure to give the user time to answer '
62 . 'before saying the answer.',
63 )->to_hash;
64  
65 5 my $url = _trivia_url();
66  
67 5 $self->agent->register_swaig_function({
68 function => $tool_name,
69 description => "Get trivia questions for " . ($tool_name =~ s/_/ /gr),
70 parameters => {
71 type => 'object',
72 properties => {
73 category => {
74 type => 'string',
75 description => 'Category for trivia question. Options: '
76 . join('; ', @$categories),
77 enum => $categories,
78 },
79 },
80 required => ['category'],
81 },
82 data_map => {
83 webhooks => [{
84 url => "$url?category=%{args.category}",
85 method => 'GET',
86 headers => { 'X-Api-Key' => $api_key },
87 output => $on_success,
88 }],
89 error_keys => ['error'],
90 output => $no_results,
91 },
92 });
93 }
94  
95 1 ▶sub get_parameter_schema {
Subroutine get_parameter_schema: called CC=1 SCAR=0.0
Pod undocumented
96 return {
97 1 %{ SignalWire::Skills::SkillBase->get_parameter_schema },
98 api_key => { type => 'string', required => 1, hidden => 1 },
99 categories => { type => 'array', default => [@ALL_CATEGORIES] },
100 tool_name => { type => 'string', default => 'get_trivia' },
101 };
102 }
103  
104 4 1;
« blib/lib/SignalWire.pm ↑ Summary blib/lib/SignalWire/Skills/Builtin/GoogleMaps.pm »