File Coverage

blib/lib/SignalWire/Agents/Prefabs/Survey.pm
Criterion Covered Total %
statement 30 30 100.0
branch 4 6 66.6
condition 2 3 66.6
subroutine 6 6 100.0
pod 0 1 0.0
total 42 46 91.3


line stmt bran cond sub pod time code
1             package SignalWire::Agents::Prefabs::Survey;
2             # Copyright (c) 2025 SignalWire
3             # Licensed under the MIT License.
4              
5 2     2   384461 use strict;
  2         5  
  2         90  
6 2     2   12 use warnings;
  2         4  
  2         134  
7 2     2   644 use Moo;
  2         9480  
  2         13  
8 2     2   2469 use JSON qw(encode_json);
  2         5  
  2         22  
9             extends 'SignalWire::Agents::Agent::AgentBase';
10              
11             has survey_name => (is => 'ro', default => sub { 'Survey' });
12             has survey_questions => (is => 'ro', default => sub { [] });
13             has introduction => (is => 'ro', default => sub { '' });
14             has conclusion => (is => 'ro', default => sub { '' });
15             has brand_name => (is => 'ro', default => sub { '' });
16             has max_retries => (is => 'ro', default => sub { 2 });
17              
18             sub BUILD {
19 10     10 0 180 my ($self, $args) = @_;
20              
21 10 50       80 $self->name('survey') if $self->name eq 'agent';
22 10 50       54 $self->route('/survey') if $self->route eq '/';
23 10         37 $self->use_pom(1);
24              
25 10         34 my $questions = $self->survey_questions;
26              
27 10         99 $self->set_global_data({
28             survey_name => $self->survey_name,
29             questions => $questions,
30             question_index => 0,
31             answers => {},
32             completed => JSON::false,
33             });
34              
35 10   66     53 my $intro = $self->introduction || "Welcome to the ${\$self->survey_name}.";
36 10         64 $self->prompt_add_section(
37             'Survey Introduction',
38             $intro,
39             bullets => [
40             'Introduce the survey to the user',
41             'Ask each question in sequence',
42             'Validate responses based on question type',
43             'Thank the user when complete',
44             ],
45             );
46              
47             # Build question descriptions
48 10         18 my @q_bullets;
49 10         28 for my $q (@$questions) {
50 12         42 my $desc = "Q: $q->{text} (type: $q->{type})";
51 12 100       47 $desc .= " [required]" if $q->{required};
52 12         34 push @q_bullets, $desc;
53             }
54 10         45 $self->prompt_add_section('Survey Questions', '', bullets => \@q_bullets);
55              
56             # Register survey tools
57             $self->define_tool(
58             name => 'submit_survey_answer',
59             description => 'Submit an answer for the current survey question',
60             parameters => {
61             type => 'object',
62             properties => {
63             question_id => { type => 'string', description => 'ID of the question' },
64             answer => { type => 'string', description => 'The answer' },
65             },
66             required => ['question_id', 'answer'],
67             },
68             handler => sub {
69 1     1   2 my ($a, $raw) = @_;
70 1         801 require SignalWire::Agents::SWAIG::FunctionResult;
71 1         11 return SignalWire::Agents::SWAIG::FunctionResult->new(
72             response => "Survey answer for $a->{question_id}: $a->{answer}",
73             );
74             },
75 10         169 );
76             }
77              
78             1;