| 1 | package SignalWire::Skills::Builtin::WikipediaSearch; | ||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2025 SignalWire | ||||||||||||||||||||||||||||
| 3 | # Licensed under the MIT License. | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # Real Wikipedia API client. Mirrors signalwire-python's | ||||||||||||||||||||||||||||
| 6 | # skills/wikipedia_search/skill.py:_search_wiki_handler — issue an | ||||||||||||||||||||||||||||
| 7 | # outbound GET to /w/api.php with a `srsearch` query, parse the JSON | ||||||||||||||||||||||||||||
| 8 | # `query.search` list, fetch each article extract, return a formatted | ||||||||||||||||||||||||||||
| 9 | # string. | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use JSON (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use URI::Escape qw(uri_escape); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | extends 'SignalWire::Skills::SkillBase'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use SignalWire::Skills::SkillRegistry; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 4 | SignalWire::Skills::SkillRegistry->register_skill('wikipedia_search', __PACKAGE__); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 6 | ▶ | has '+skill_name' => (default => sub { 'wikipedia_search' }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 6 | ▶ | has '+skill_description' => (default => sub { 'Search Wikipedia for information about a topic and get article summaries' }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 6 | ▶ | has '+supports_multiple_instances' => (default => sub { 0 }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # Default Wikipedia API base. Honor WIKIPEDIA_BASE_URL env var so the | ||||||||||||||||||||||||||||
| 27 | # audit fixture (audit_skills_dispatch.py) can redirect us at a local | ||||||||||||||||||||||||||||
| 28 | # HTTP server. The override replaces the host+scheme; the canonical | ||||||||||||||||||||||||||||
| 29 | # `/w/api.php` path is always appended so the documented Wikipedia | ||||||||||||||||||||||||||||
| 30 | # API wire shape is preserved (the audit's `expected_path_substring` | ||||||||||||||||||||||||||||
| 31 | # is `api.php`). | ||||||||||||||||||||||||||||
| 32 | has 'base_url' => ( | ||||||||||||||||||||||||||||
| 33 | is => 'ro', | ||||||||||||||||||||||||||||
| 34 | lazy => 1, | ||||||||||||||||||||||||||||
| 35 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 36 | 1 | my $override = $ENV{WIKIPEDIA_BASE_URL}; | |||||||||||||||||||||||||||
| 37 | 1 | ▶ | return 'https://en.wikipedia.org/w/api.php' unless $override; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | $override =~ s{/+$}{}; | |||||||||||||||||||||||||||
| 39 | 0 | return "$override/w/api.php"; | |||||||||||||||||||||||||||
| 40 | }, | ||||||||||||||||||||||||||||
| 41 | 4 | ); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | ▶ | has 'num_results' => (is => 'ro', lazy => 1, default => sub { 1 }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | has 'no_results_message' => ( | ||||||||||||||||||||||||||||
| 45 | is => 'ro', | ||||||||||||||||||||||||||||
| 46 | lazy => 1, | ||||||||||||||||||||||||||||
| 47 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 48 | 0 | "I couldn't find any Wikipedia articles for '{query}'. " | |||||||||||||||||||||||||||
| 49 | . "Try rephrasing your search or using different keywords."; | ||||||||||||||||||||||||||||
| 50 | }, | ||||||||||||||||||||||||||||
| 51 | 4 | ); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | has '_http' => ( | ||||||||||||||||||||||||||||
| 54 | is => 'ro', | ||||||||||||||||||||||||||||
| 55 | lazy => 1, | ||||||||||||||||||||||||||||
| 56 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 1 | HTTP::Tiny->new( | |||||||||||||||||||||||||||
| 58 | agent => 'SignalWire-Perl-WikipediaSearch/1.0', | ||||||||||||||||||||||||||||
| 59 | timeout => 10, | ||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||||
| 62 | 4 | ); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 4 | ▶ | sub setup { | ||||||||||||||||||||||||||
|
Subroutine
setup: called CC=4 SCAR=19.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 65 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 66 | 4 | ▶ | if (defined $self->params->{num_results}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | my $n = $self->params->{num_results}; | |||||||||||||||||||||||||||
| 68 | 0 | ▶ | $n = 1 if $n < 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | $self->{num_results} = $n; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 4 | ▶ | if (defined $self->params->{no_results_message}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | $self->{no_results_message} = $self->params->{no_results_message}; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 4 | return 1; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 3 | ▶ | sub register_tools { | ||||||||||||||||||||||||||
|
Subroutine
register_tools: called CC=1 SCAR=0.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 79 | 3 | my $weak_self = $self; | |||||||||||||||||||||||||||
| 80 | 3 | require Scalar::Util; | |||||||||||||||||||||||||||
| 81 | 3 | Scalar::Util::weaken($weak_self); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | $self->define_tool( | ||||||||||||||||||||||||||||
| 84 | name => 'search_wiki', | ||||||||||||||||||||||||||||
| 85 | description => 'Search Wikipedia for information about a topic and get article summaries', | ||||||||||||||||||||||||||||
| 86 | parameters => { | ||||||||||||||||||||||||||||
| 87 | type => 'object', | ||||||||||||||||||||||||||||
| 88 | properties => { | ||||||||||||||||||||||||||||
| 89 | query => { | ||||||||||||||||||||||||||||
| 90 | type => 'string', | ||||||||||||||||||||||||||||
| 91 | description => 'The search term or topic to look up on Wikipedia', | ||||||||||||||||||||||||||||
| 92 | }, | ||||||||||||||||||||||||||||
| 93 | }, | ||||||||||||||||||||||||||||
| 94 | required => ['query'], | ||||||||||||||||||||||||||||
| 95 | }, | ||||||||||||||||||||||||||||
| 96 | 1 | ▶ | handler => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||
| 97 | 1 | my ($args, $raw) = @_; | |||||||||||||||||||||||||||
| 98 | 1 | require SignalWire::SWAIG::FunctionResult; | |||||||||||||||||||||||||||
| 99 | 1 | ▶ | my $query = $args->{query} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"query"} // ""
MC/DC0%0/1
$args->{"query"} // ""
$args->{"query"}
Truth table50%1/2
A
A= $args->{"query"}
| |||||||||||||||||||||||||||||
| 100 | 1 | $query =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 101 | 1 | ▶ | unless (length $query) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | return SignalWire::SWAIG::FunctionResult->new( | |||||||||||||||||||||||||||
| 103 | response => 'Please provide a search query for Wikipedia.', | ||||||||||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 1 | my $text = $weak_self->search_wiki($query); | |||||||||||||||||||||||||||
| 107 | 1 | return SignalWire::SWAIG::FunctionResult->new(response => $text); | |||||||||||||||||||||||||||
| 108 | }, | ||||||||||||||||||||||||||||
| 109 | 3 | ); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | ▶ | sub search_wiki { | ||||||||||||||||||||||||||
|
Subroutine
search_wiki: called CC=17 SCAR=40.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | 1 | my ($self, $query) = @_; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 1 | my $base = $self->base_url; | |||||||||||||||||||||||||||
| 116 | # Step 1: search. | ||||||||||||||||||||||||||||
| 117 | 1 | my $search_url = $base | |||||||||||||||||||||||||||
| 118 | . '?action=query&list=search&format=json' | ||||||||||||||||||||||||||||
| 119 | . '&srsearch=' . uri_escape($query) | ||||||||||||||||||||||||||||
| 120 | . '&srlimit=' . $self->num_results; | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | my $resp = $self->_http->get($search_url); | |||||||||||||||||||||||||||
| 123 | 1 | ▶ | unless ($resp->{success}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | return "Error accessing Wikipedia: $resp->{status} $resp->{reason}"; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 1 | my $data = eval { JSON::decode_json($resp->{content}) }; | |||||||||||||||||||||||||||
| 127 | 1 | ▶ | return "Error parsing Wikipedia response: $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 1 | ▶ | my $hits = $data->{query}{search} // []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$data->{"query"}{"search"} // []
MC/DC0%0/1
$data->{"query"}{"search"} // []
$data->{"query"}{"search"}
Truth table50%1/2
A
A= $data->{"query"}{"search"}
| |||||||||||||||||||||||||||||
| 130 | 1 | ▶ | unless (@$hits) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | my $msg = $self->no_results_message; | |||||||||||||||||||||||||||
| 132 | 0 | $msg =~ s/\{query\}/$query/g; | |||||||||||||||||||||||||||
| 133 | 0 | return $msg; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # Step 2: extract for each article (or, more pragmatically, parse | ||||||||||||||||||||||||||||
| 137 | # the search snippet directly when present — the canned audit | ||||||||||||||||||||||||||||
| 138 | # fixture returns `query.search[].snippet`, not a separate extract | ||||||||||||||||||||||||||||
| 139 | # endpoint, so always preferring the snippet keeps the parser | ||||||||||||||||||||||||||||
| 140 | # uniform across live + fixture). | ||||||||||||||||||||||||||||
| 141 | 1 | my @articles; | |||||||||||||||||||||||||||
| 142 | 1 | my $i = 0; | |||||||||||||||||||||||||||
| 143 | 1 | for my $hit (@$hits) { | |||||||||||||||||||||||||||
| 144 | 1 | ▶ | last if $i++ >= $self->num_results; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 1 | ▶ | my $title = $hit->{title} // 'Unknown'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$hit->{"title"} // "Unknown"
MC/DC0%0/1
$hit->{"title"} // "Unknown"
$hit->{"title"}
Truth table50%1/2
A
A= $hit->{"title"}
| |||||||||||||||||||||||||||||
| 146 | 1 | my $snippet = $hit->{snippet}; | |||||||||||||||||||||||||||
| 147 | 1 | my $extract; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 1 | ▶ | if (defined $snippet && length $snippet) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $snippet and length $snippet
MC/DC0%0/2
defined $snippet and length $snippet
defined $snippet
length $snippet
Truth table33%1/3
A and B
A= defined $snippetB= length $snippet
| |||||||||||||||||||||||||||||
| 150 | # Strip HTML tags Wikipedia includes in the snippet. | ||||||||||||||||||||||||||||
| 151 | 1 | $extract = $snippet; | |||||||||||||||||||||||||||
| 152 | 1 | $extract =~ s/<[^>]+>//g; | |||||||||||||||||||||||||||
| 153 | } else { | ||||||||||||||||||||||||||||
| 154 | 0 | my $extract_url = $base | |||||||||||||||||||||||||||
| 155 | . '?action=query&prop=extracts&exintro&explaintext&format=json' | ||||||||||||||||||||||||||||
| 156 | . '&titles=' . uri_escape($title); | ||||||||||||||||||||||||||||
| 157 | 0 | my $er = $self->_http->get($extract_url); | |||||||||||||||||||||||||||
| 158 | 0 | ▶ | if ($er->{success}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | my $ed = eval { JSON::decode_json($er->{content}) }; | |||||||||||||||||||||||||||
| 160 | 0 | ▶ | if (!$@ && $ed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $@ and $ed
MC/DC0%0/2
not $@ and $ed
not $@
$ed
Truth table0%0/3
not(A) and B
A= not $@B= $ed
| |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | my $pages = $ed->{query}{pages} // {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$ed->{"query"}{"pages"} // {}
MC/DC0%0/1
$ed->{"query"}{"pages"} // {}
$ed->{"query"}{"pages"}
Truth table0%0/2
A
A= $ed->{"query"}{"pages"}
| |||||||||||||||||||||||||||||
| 162 | 0 | my ($first) = values %$pages; | |||||||||||||||||||||||||||
| 163 | 0 | ▶ | $extract = $first->{extract} if $first; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 1 | ▶ | if (defined $extract && length $extract) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $extract and length $extract
MC/DC0%0/2
defined $extract and length $extract
defined $extract
length $extract
Truth table33%1/3
A and B
A= defined $extractB= length $extract
| |||||||||||||||||||||||||||||
| 169 | 1 | push @articles, "**$title**\n\n$extract"; | |||||||||||||||||||||||||||
| 170 | } else { | ||||||||||||||||||||||||||||
| 171 | 0 | push @articles, "**$title**\n\nNo summary available for this article."; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 1 | return join("\n\n" . ('=' x 50) . "\n\n", @articles); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 1 | ▶ | sub _get_prompt_sections { | ||||||||||||||||||||||||||
|
Subroutine
_get_prompt_sections: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 179 | return [{ | ||||||||||||||||||||||||||||
| 180 | 1 | title => 'Wikipedia Search', | |||||||||||||||||||||||||||
| 181 | body => 'You can search Wikipedia for factual information.', | ||||||||||||||||||||||||||||
| 182 | bullets => [ | ||||||||||||||||||||||||||||
| 183 | 'Use search_wiki to find information about any topic', | ||||||||||||||||||||||||||||
| 184 | 'Results include article summaries from Wikipedia', | ||||||||||||||||||||||||||||
| 185 | ], | ||||||||||||||||||||||||||||
| 186 | }]; | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 1 | ▶ | sub get_parameter_schema { | ||||||||||||||||||||||||||
|
Subroutine
get_parameter_schema: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 190 | return { | ||||||||||||||||||||||||||||
| 191 | 1 | %{ SignalWire::Skills::SkillBase->get_parameter_schema }, | |||||||||||||||||||||||||||
| 192 | num_results => { type => 'integer', default => 1, min => 1, max => 5 }, | ||||||||||||||||||||||||||||
| 193 | no_results_message => { type => 'string' }, | ||||||||||||||||||||||||||||
| 194 | }; | ||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 4 | 1; | |||||||||||||||||||||||||||