File Coverage

blib/lib/SignalWire/Agents/Prefabs/Receptionist.pm
Criterion Covered Total %
statement 31 31 100.0
branch 4 6 66.6
condition 1 2 50.0
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::Receptionist;
2             # Copyright (c) 2025 SignalWire
3             # Licensed under the MIT License.
4              
5 2     2   317016 use strict;
  2         5  
  2         87  
6 2     2   11 use warnings;
  2         4  
  2         172  
7 2     2   598 use Moo;
  2         9638  
  2         12  
8 2     2   3090 use JSON qw(encode_json);
  2         13508  
  2         16  
9             extends 'SignalWire::Agents::Agent::AgentBase';
10              
11             has departments => (is => 'ro', default => sub { [] });
12             has greeting => (is => 'ro', default => sub { 'Thank you for calling. How can I help you today?' });
13             has voice => (is => 'ro', default => sub { 'rime.spore' });
14              
15             sub BUILD {
16 11     11 0 232 my ($self, $args) = @_;
17              
18 11 50       86 $self->name('receptionist') if $self->name eq 'agent';
19 11 50       64 $self->route('/receptionist') if $self->route eq '/';
20 11         73 $self->use_pom(1);
21              
22 11         36 my $departments = $self->departments;
23              
24 11         109 $self->set_global_data({
25             departments => $departments,
26             caller_info => {},
27             });
28              
29             # Build department list for prompt
30 11         22 my @dept_bullets;
31 11         34 for my $dept (@$departments) {
32 14         70 push @dept_bullets, "$dept->{name}: $dept->{description}";
33             }
34              
35             $self->prompt_add_section(
36 11         120 'Receptionist Role',
37             $self->greeting,
38             bullets => [
39             'Greet the caller warmly',
40             'Determine which department they need',
41             'Transfer them to the correct department',
42             @dept_bullets,
43             ],
44             );
45              
46             # Register transfer tool
47             $self->define_tool(
48             name => 'transfer_to_department',
49             description => 'Transfer the caller to the specified department',
50             parameters => {
51             type => 'object',
52             properties => {
53             department => { type => 'string', description => 'Department name to transfer to' },
54             },
55             required => ['department'],
56             },
57             handler => sub {
58 3     3   10 my ($a, $raw) = @_;
59 3         802 require SignalWire::Agents::SWAIG::FunctionResult;
60 3   50     20 my $dept_name = $a->{department} // '';
61 3         10 for my $dept (@$departments) {
62 3 100       18 if (lc($dept->{name}) eq lc($dept_name)) {
63 2         72 my $result = SignalWire::Agents::SWAIG::FunctionResult->new(
64             response => "Transferring to $dept_name",
65             );
66 2         26 return $result;
67             }
68             }
69 1         72 return SignalWire::Agents::SWAIG::FunctionResult->new(
70             response => "Department '$dept_name' not found",
71             );
72             },
73 11         170 );
74             }
75              
76             1;