File Coverage

blib/lib/SignalWire/Agents/Prefabs/InfoGatherer.pm
Criterion Covered Total %
statement 28 28 100.0
branch 4 4 100.0
condition 1 2 50.0
subroutine 7 7 100.0
pod 0 1 0.0
total 40 42 95.2


line stmt bran cond sub pod time code
1             package SignalWire::Agents::Prefabs::InfoGatherer;
2             # Copyright (c) 2025 SignalWire
3             # Licensed under the MIT License.
4              
5 2     2   673040 use strict;
  2         6  
  2         89  
6 2     2   13 use warnings;
  2         23  
  2         199  
7 2     2   1180 use Moo;
  2         19432  
  2         12  
8 2     2   3820 use JSON qw(encode_json);
  2         5  
  2         20  
9             extends 'SignalWire::Agents::Agent::AgentBase';
10              
11             has questions => (is => 'ro', default => sub { [] });
12              
13             sub BUILD {
14 13     13 0 210 my ($self, $args) = @_;
15              
16             # Set defaults
17 13 100       87 $self->name('info_gatherer') if $self->name eq 'agent';
18 13 100       65 $self->route('/info_gatherer') if $self->route eq '/';
19 13         45 $self->use_pom(1);
20              
21 13         44 my $questions = $self->questions;
22              
23             # Set global data
24 13         108 $self->set_global_data({
25             questions => $questions,
26             question_index => 0,
27             answers => [],
28             });
29              
30             # Build prompt
31 13         81 $self->prompt_add_section(
32             'Information Gathering',
33             'You are an information-gathering assistant. Your job is to ask the user a series of questions and collect their answers.',
34             bullets => [
35             'Ask questions one at a time in order',
36             'Wait for the user to answer before asking the next question',
37             'Confirm answers when the question requires confirmation',
38             'Use start_questions to begin and submit_answer for each response',
39             ],
40             );
41              
42             # Register tools
43             $self->define_tool(
44             name => 'start_questions',
45             description => 'Start the question-gathering process and return the first question',
46             parameters => { type => 'object', properties => {} },
47             handler => sub {
48 2     2   5 my ($a, $raw) = @_;
49 2         1523 require SignalWire::Agents::SWAIG::FunctionResult;
50 2   50     19 my $first = $questions->[0]{question_text} // 'No questions configured';
51 2         12 return SignalWire::Agents::SWAIG::FunctionResult->new(response => $first);
52             },
53 13         138 );
54              
55             $self->define_tool(
56             name => 'submit_answer',
57             description => 'Submit an answer to the current question',
58             parameters => {
59             type => 'object',
60             properties => {
61             answer => { type => 'string', description => 'The answer' },
62             confirmed_by_user => { type => 'boolean', description => 'User confirmed this answer' },
63             },
64             required => ['answer'],
65             },
66             handler => sub {
67 2     2   9 my ($a, $raw) = @_;
68 2         17 require SignalWire::Agents::SWAIG::FunctionResult;
69 2         77 return SignalWire::Agents::SWAIG::FunctionResult->new(
70             response => "Answer recorded: $a->{answer}",
71             );
72             },
73 13         151 );
74             }
75              
76             1;