blib/lib/SignalWire/Prefabs/FAQBot.pm

filter: Statementstmt 97.5% 40 / 41 Branchbran 100% 10 / 10 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 1 totaltotal 92.1% 59 / 64 CC 9 SCAR 12.5
CC
9
Cov
96%
BUILD
6.0
__ANON__
4.0
__ANON__
1.1
CRAP 3.5
« blib/lib/SignalWire/Skills/Builtin/McpGateway.pm ↑ Summary blib/lib/SignalWire/REST/Namespaces/Logs.pm »
1 package SignalWire::Prefabs::FAQBot;
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 JSON qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 extends 'SignalWire::Agent::AgentBase';
10  
11 0 ▶has faqs => (is => 'ro', default => sub { [] });
Subroutine __ANON__: not called CC=1 SCAR=1.2
12 10 ▶has suggest_related => (is => 'ro', default => sub { 1 });
Subroutine __ANON__: called CC=1 SCAR=0.0
13 13 ▶has persona => (is => 'ro', default => sub { 'You are a helpful FAQ bot that provides accurate answers to common questions.' });
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 13 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=17.9
Pod undocumented
16 13 my ($self, $args) = @_;
17  
18 13 ▶ $self->name('faq_bot') if $self->name eq 'agent';
Branch: 1 branch
BranchTrueFalseTotal
if $self->name eq "agent" 11 2 13
19 13 ▶ $self->route('/faq') if $self->route eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $self->route eq "/" 11 2 13
20 13 $self->use_pom(1);
21  
22 13 my $faqs = $self->faqs;
23  
24 13 ▶ $self->set_global_data({
Branch: 1 branch
BranchTrueFalseTotal
$self->suggest_related ? : 12 1 13
25 faqs => $faqs,
26 suggest_related => $self->suggest_related ? JSON::true : JSON::false,
27 });
28  
29 13 $self->prompt_add_section(
30 'Personality',
31 $self->persona,
32 );
33  
34 # Build FAQ knowledge
35 13 my @faq_bullets;
36 13 for my $faq (@$faqs) {
37 15 push @faq_bullets, "Q: $faq->{question} A: $faq->{answer}";
38 }
39  
40 $self->prompt_add_section(
41 13 'FAQ Knowledge Base',
42 'You have knowledge of the following frequently asked questions.',
43 bullets => \@faq_bullets,
44 );
45  
46 13 ▶ if ($self->suggest_related) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->suggest_related) 12 1 13
47 12 $self->prompt_add_section(
48 'Related Questions',
49 'When appropriate, suggest related questions the user might also be interested in.',
50 );
51 }
52  
53 # Register lookup tool
54 $self->define_tool(
55 name => 'lookup_faq',
56 description => 'Look up an FAQ answer by keyword matching',
57 parameters => {
58 type => 'object',
59 properties => {
60 query => { type => 'string', description => 'The question or keywords to search' },
61 },
62 required => ['query'],
63 },
64 3 ▶ handler => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
65 3 my ($a, $raw) = @_;
66 3 require SignalWire::SWAIG::FunctionResult;
67 3 ▶ my $query = lc($a->{query} // '');
Condition50%1/2
$a->{"query"} // ""
l!l
30
MC/DC0%0/1
$a->{"query"} // ""
$a->{"query"}
Truth table50%1/2
A
A= $a->{"query"}
Aresult
11
00
68 3 for my $faq (@$faqs) {
69 3 ▶ if (index(lc($faq->{question}), $query) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index(lc $faq->{"question"}, $query) != -1) 2 1 3
70 return SignalWire::SWAIG::FunctionResult->new(
71 response => $faq->{answer},
72 2 );
73 }
74 }
75 1 return SignalWire::SWAIG::FunctionResult->new(
76 response => "No FAQ found matching: $a->{query}",
77 );
78 },
79 13 );
80 }
81  
82 2 1;
« blib/lib/SignalWire/Skills/Builtin/McpGateway.pm ↑ Summary blib/lib/SignalWire/REST/Namespaces/Logs.pm »