| 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
| |||||||||||||
| 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"
MC/DC100%1/1
$self->params->{"tool_name"} // "get_trivia"
$self->params->{"tool_name"}
Truth table100%2/2
A
A= $self->params->{"tool_name"}
| |||||||||||||
| 51 | 5 | ▶ | my $api_key = $self->params->{api_key} // ''; | ||||||||||
|
Condition50%1/2
$self->params->{"api_key"} // ""
MC/DC0%0/1
$self->params->{"api_key"} // ""
$self->params->{"api_key"}
Truth table50%1/2
A
A= $self->params->{"api_key"}
| |||||||||||||
| 52 | 5 | ▶ | my $categories = $self->params->{categories} // [@ALL_CATEGORIES]; | ||||||||||
|
Condition100%2/2
$self->params->{"categories"} // [@ALL_CATEGORIES]
MC/DC100%1/1
$self->params->{"categories"} // [@ALL_CATEGORIES]
$self->params->{"categories"}
Truth table100%2/2
A
A= $self->params->{"categories"}
| |||||||||||||
| 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; | |||||||||||