blib/lib/SignalWire/Agents/Contexts.pm

filter: Statementstmt 88.9% 417 / 469 Branchbran 70.1% 136 / 194 Conditioncond 53.3% 8 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 86.5% 90 / 104 Podpod 0.0% 0 / 49 totaltotal 77.8% 654 / 840 CC 119 SCAR 22.7
CC
119
Cov
83%
validate
36.9
add_enter_filler
20.0
to_hash
14.0
CRAP 9.7
« blib/lib/SignalWire/Agents/Agent/AgentBase.pm ↑ Summary blib/lib/SignalWire/Agents/Security/SessionManager.pm »
1 package SignalWire::Agents::Contexts;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $MAX_CONTEXTS = 50;
7 1 our $MAX_STEPS_PER_CONTEXT = 100;
8  
9 # ==========================================================================
10 # GatherQuestion
11 # ==========================================================================
12 package SignalWire::Agents::Contexts::GatherQuestion;
13 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 has 'key' => (is => 'ro', required => 1);
17 1 has 'question' => (is => 'ro', required => 1);
18 0 ▶has 'type' => (is => 'ro', default => sub { 'string' });
Subroutine __ANON__: not called CC=1 SCAR=1.2
19 0 ▶has 'confirm' => (is => 'ro', default => sub { 0 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
20 0 ▶has 'prompt' => (is => 'ro', default => sub { undef });
Subroutine __ANON__: not called CC=1 SCAR=1.2
21 0 ▶has 'functions' => (is => 'ro', default => sub { undef });
Subroutine __ANON__: not called CC=1 SCAR=1.2
22  
23 2 ▶sub to_hash {
Subroutine to_hash: called CC=5 SCAR=16.5
Pod undocumented
24 2 my ($self) = @_;
25 2 my %d = (key => $self->key, question => $self->question);
26 2 ▶ $d{type} = $self->type if $self->type ne 'string';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type ne "string" 0 2 2
27 2 ▶ $d{confirm} = JSON::true if $self->confirm;
Branch: 1 branch
BranchTrueFalseTotal
if $self->confirm 1 1 2
28 2 ▶ $d{prompt} = $self->prompt if defined $self->prompt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->prompt 0 2 2
29 2 ▶ $d{functions} = $self->functions if defined $self->functions;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->functions 0 2 2
30 2 return \%d;
31 }
32  
33 # ==========================================================================
34 # GatherInfo
35 # ==========================================================================
36 package SignalWire::Agents::Contexts::GatherInfo;
37 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 4 ▶has '_questions' => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
40 0 ▶has '_output_key' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: not called CC=1 SCAR=1.2
41 0 ▶has '_completion_action' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: not called CC=1 SCAR=1.2
42 0 ▶has '_prompt' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: not called CC=1 SCAR=1.2
43  
44 5 ▶sub add_question {
Subroutine add_question: called CC=3 SCAR=11.0
Pod undocumented
45 5 my ($self, %opts) = @_;
46 my $q = SignalWire::Agents::Contexts::GatherQuestion->new(
47 key => $opts{key},
48 question => $opts{question},
49 type => $opts{type} // 'string',
50 confirm => $opts{confirm} // 0,
51 prompt => $opts{prompt},
52 functions => $opts{functions},
53 5 ▶ );
Condition100%4/4
$opts{"type"} // "string"
l!l
14
$opts{"confirm"} // 0
l!l
14
MC/DC100%1/1
$opts{"type"} // "string"
$opts{"type"}
MC/DC100%1/1
$opts{"confirm"} // 0
$opts{"confirm"}
Truth table100%2/2
A
A= $opts{"type"}
Aresult
11
00
Truth table100%2/2
A
A= $opts{"confirm"}
Aresult
11
00
54 5 push @{ $self->_questions }, $q;
55 5 return $self;
56 }
57  
58 1 ▶sub to_hash {
Subroutine to_hash: called CC=5 SCAR=16.6
Pod undocumented
59 1 my ($self) = @_;
60 1 ▶ die "gather_info must have at least one question" unless @{ $self->_questions };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->_questions;} 0 1 1
61 1 my %d = (questions => [ map { $_->to_hash } @{ $self->_questions } ]);
62 1 ▶ $d{prompt} = $self->_prompt if defined $self->_prompt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_prompt 1 0 1
63 1 ▶ $d{output_key} = $self->_output_key if defined $self->_output_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_output_key 1 0 1
64 1 ▶ $d{completion_action} = $self->_completion_action if defined $self->_completion_action;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_completion_action 1 0 1
65 1 return \%d;
66 }
67  
68 # ==========================================================================
69 # Step
70 # ==========================================================================
71 package SignalWire::Agents::Contexts::Step;
72 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 1 has 'name' => (is => 'ro', required => 1);
76  
77 38 ▶has '_text' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
78 38 ▶has '_step_criteria' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
79 38 ▶has '_functions' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
80 38 ▶has '_valid_steps' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
81 38 ▶has '_valid_contexts' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
82 38 ▶has '_sections' => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
83 38 ▶has '_gather_info' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
84 38 ▶has '_end' => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
85 38 ▶has '_skip_user_turn' => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
86 38 ▶has '_skip_to_next_step' => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
87 38 ▶has '_reset_system_prompt' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
88 38 ▶has '_reset_user_prompt' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
89 38 ▶has '_reset_consolidate' => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
90 38 ▶has '_reset_full_reset' => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
91  
92 35 ▶sub set_text {
Subroutine set_text: called CC=2 SCAR=6.9
Pod undocumented
93 35 my ($self, $text) = @_;
94 die "Cannot use set_text() when POM sections have been added"
95 35 ▶ if @{ $self->_sections };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->_sections;} 1 34 35
96 34 $self->_text($text);
97 34 return $self;
98 }
99  
100 5 ▶sub add_section {
Subroutine add_section: called CC=2 SCAR=6.9
Pod undocumented
101 5 my ($self, $title, $body) = @_;
102 5 ▶ die "Cannot add POM sections when set_text() has been used"
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_text 1 4 5
103 if defined $self->_text;
104 4 push @{ $self->_sections }, { title => $title, body => $body };
105 4 return $self;
106 }
107  
108 2 ▶sub add_bullets {
Subroutine add_bullets: called CC=2 SCAR=7.0
Pod undocumented
109 2 my ($self, $title, $bullets) = @_;
110 2 ▶ die "Cannot add POM sections when set_text() has been used"
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_text 0 2 2
111 if defined $self->_text;
112 2 push @{ $self->_sections }, { title => $title, bullets => $bullets };
113 2 return $self;
114 }
115  
116 2 ▶sub set_step_criteria {
Subroutine set_step_criteria: called CC=1 SCAR=0.0
Pod undocumented
117 2 my ($self, $criteria) = @_;
118 2 $self->_step_criteria($criteria);
119 2 return $self;
120 }
121  
122 3 ▶sub set_functions {
Subroutine set_functions: called CC=1 SCAR=0.0
Pod undocumented
123 3 my ($self, $functions) = @_;
124 3 $self->_functions($functions);
125 3 return $self;
126 }
127  
128 4 ▶sub set_valid_steps {
Subroutine set_valid_steps: called CC=1 SCAR=0.0
Pod undocumented
129 4 my ($self, $steps) = @_;
130 4 $self->_valid_steps($steps);
131 4 return $self;
132 }
133  
134 1 ▶sub set_valid_contexts {
Subroutine set_valid_contexts: called CC=1 SCAR=0.0
Pod undocumented
135 1 my ($self, $contexts) = @_;
136 1 $self->_valid_contexts($contexts);
137 1 return $self;
138 }
139  
140 1 ▶sub set_end {
Subroutine set_end: called CC=2 SCAR=7.1
Pod undocumented
141 1 my ($self, $end) = @_;
142 1 ▶ $self->_end($end ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$end ? : 1 0 1
143 1 return $self;
144 }
145  
146 1 ▶sub set_skip_user_turn {
Subroutine set_skip_user_turn: called CC=2 SCAR=7.1
Pod undocumented
147 1 my ($self, $skip) = @_;
148 1 ▶ $self->_skip_user_turn($skip ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$skip ? : 1 0 1
149 1 return $self;
150 }
151  
152 1 ▶sub set_skip_to_next_step {
Subroutine set_skip_to_next_step: called CC=2 SCAR=7.1
Pod undocumented
153 1 my ($self, $skip) = @_;
154 1 ▶ $self->_skip_to_next_step($skip ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$skip ? : 1 0 1
155 1 return $self;
156 }
157  
158 4 ▶sub set_gather_info {
Subroutine set_gather_info: called CC=1 SCAR=0.0
Pod undocumented
159 4 my ($self, %opts) = @_;
160 $self->_gather_info(SignalWire::Agents::Contexts::GatherInfo->new(
161 _output_key => $opts{output_key},
162 _completion_action => $opts{completion_action},
163 _prompt => $opts{prompt},
164 4 ));
165 4 return $self;
166 }
167  
168 6 ▶sub add_gather_question {
Subroutine add_gather_question: called CC=2 SCAR=6.9
Pod undocumented
169 6 my ($self, %opts) = @_;
170 6 ▶ die "Must call set_gather_info() before add_gather_question()"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_gather_info 1 5 6
171 unless defined $self->_gather_info;
172 5 $self->_gather_info->add_question(%opts);
173 5 return $self;
174 }
175  
176 1 ▶sub clear_sections {
Subroutine clear_sections: called CC=1 SCAR=0.0
Pod undocumented
177 1 my ($self) = @_;
178 1 $self->_sections([]);
179 1 $self->_text(undef);
180 1 return $self;
181 }
182  
183 1 ▶sub set_reset_system_prompt {
Subroutine set_reset_system_prompt: called CC=1 SCAR=0.0
Pod undocumented
184 1 my ($self, $sp) = @_;
185 1 $self->_reset_system_prompt($sp);
186 1 return $self;
187 }
188  
189 1 ▶sub set_reset_user_prompt {
Subroutine set_reset_user_prompt: called CC=1 SCAR=0.0
Pod undocumented
190 1 my ($self, $up) = @_;
191 1 $self->_reset_user_prompt($up);
192 1 return $self;
193 }
194  
195 1 ▶sub set_reset_consolidate {
Subroutine set_reset_consolidate: called CC=2 SCAR=7.1
Pod undocumented
196 1 my ($self, $c) = @_;
197 1 ▶ $self->_reset_consolidate($c ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$c ? : 1 0 1
198 1 return $self;
199 }
200  
201 1 ▶sub set_reset_full_reset {
Subroutine set_reset_full_reset: called CC=2 SCAR=7.1
Pod undocumented
202 1 my ($self, $fr) = @_;
203 1 ▶ $self->_reset_full_reset($fr ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$fr ? : 1 0 1
204 1 return $self;
205 }
206  
207 26 ▶sub _render_text {
Subroutine _render_text: called CC=5 SCAR=16.1
208 26 my ($self) = @_;
209 26 ▶ return $self->_text if defined $self->_text;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_text 24 2 26
210  
211 die "Step '" . $self->name . "' has no text or POM sections defined"
212 2 ▶ unless @{ $self->_sections };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->_sections;} 0 2 2
213  
214 2 my @parts;
215 2 for my $sec (@{ $self->_sections }) {
216 4 ▶ if (exists $sec->{bullets}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sec->{'bullets'}) { } 2 2 4
217 2 push @parts, "## $sec->{title}";
218 2 push @parts, map { "- $_" } @{ $sec->{bullets} };
219 } else {
220 2 push @parts, "## $sec->{title}";
221 2 push @parts, $sec->{body};
222 }
223 4 push @parts, '';
224 }
225 2 my $text = join("\n", @parts);
226 2 $text =~ s/\s+$//;
227 2 return $text;
228 }
229  
230 26 ▶sub to_hash {
Subroutine to_hash: called CC=14 SCAR=26.4
Pod undocumented
231 26 my ($self) = @_;
232 26 my %d = (
233 name => $self->name,
234 text => $self->_render_text,
235 );
236  
237 26 ▶ $d{step_criteria} = $self->_step_criteria if defined $self->_step_criteria;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_step_criteria 2 24 26
238 26 ▶ $d{functions} = $self->_functions if defined $self->_functions;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_functions 3 23 26
239 26 ▶ $d{valid_steps} = $self->_valid_steps if defined $self->_valid_steps;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_valid_steps 3 23 26
240 26 ▶ $d{valid_contexts} = $self->_valid_contexts if defined $self->_valid_contexts;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_valid_contexts 1 25 26
241 26 ▶ $d{end} = JSON::true if $self->_end;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_end 1 25 26
242 26 ▶ $d{skip_user_turn} = JSON::true if $self->_skip_user_turn;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_skip_user_turn 1 25 26
243 26 ▶ $d{skip_to_next_step} = JSON::true if $self->_skip_to_next_step;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_skip_to_next_step 1 25 26
244  
245 26 my %reset;
246 26 ▶ $reset{system_prompt} = $self->_reset_system_prompt if defined $self->_reset_system_prompt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_reset_system_prompt 1 25 26
247 26 ▶ $reset{user_prompt} = $self->_reset_user_prompt if defined $self->_reset_user_prompt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_reset_user_prompt 1 25 26
248 26 ▶ $reset{consolidate} = JSON::true if $self->_reset_consolidate;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_reset_consolidate 1 25 26
249 26 ▶ $reset{full_reset} = JSON::true if $self->_reset_full_reset;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_reset_full_reset 1 25 26
250 26 ▶ $d{reset} = \%reset if keys %reset;
Branch: 1 branch
BranchTrueFalseTotal
if keys %reset 1 25 26
251  
252 26 ▶ $d{gather_info} = $self->_gather_info->to_hash if defined $self->_gather_info;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_gather_info 1 25 26
253  
254 26 return \%d;
255 }
256  
257 # ==========================================================================
258 # Context
259 # ==========================================================================
260 package SignalWire::Agents::Contexts::Context;
261 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
262 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
263  
264 1 has 'name' => (is => 'ro', required => 1);
265  
266 25 ▶has '_steps' => (is => 'rw', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
267 25 ▶has '_step_order' => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
268 25 ▶has '_valid_contexts' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
269 25 ▶has '_valid_steps' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
270 25 ▶has '_post_prompt' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
271 25 ▶has '_system_prompt' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
272 25 ▶has '_system_prompt_sections' => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
273 25 ▶has '_consolidate' => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
274 25 ▶has '_full_reset' => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
275 25 ▶has '_user_prompt' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
276 25 ▶has '_isolated' => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
277 25 ▶has '_prompt_text' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
278 25 ▶has '_prompt_sections' => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
279 25 ▶has '_enter_fillers' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
280 25 ▶has '_exit_fillers' => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
281  
282 28 ▶sub add_step {
Subroutine add_step: called CC=8 SCAR=20.8
Pod undocumented
283 28 my ($self, $name, %opts) = @_;
284 die "Step '$name' already exists in context '" . $self->name . "'"
285 28 ▶ if exists $self->_steps->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_steps->{$name} 1 27 28
286 die "Maximum steps per context ($SignalWire::Agents::Contexts::MAX_STEPS_PER_CONTEXT) exceeded"
287 27 ▶ if keys %{ $self->_steps } >= $SignalWire::Agents::Contexts::MAX_STEPS_PER_CONTEXT;
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$self->_steps;} >= $SignalWire::Agents::Contexts::MAX_STEPS_PER_CONTEXT 0 27 27
288  
289 27 my $step = SignalWire::Agents::Contexts::Step->new(name => $name);
290 27 $self->_steps->{$name} = $step;
291 27 push @{ $self->_step_order }, $name;
292  
293 27 ▶ $step->add_section('Task', $opts{task}) if defined $opts{task};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"task"} 1 26 27
294 27 ▶ $step->add_bullets('Process', $opts{bullets}) if defined $opts{bullets};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"bullets"} 1 26 27
295 27 ▶ $step->set_step_criteria($opts{criteria}) if defined $opts{criteria};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"criteria"} 1 26 27
296 27 ▶ $step->set_functions($opts{functions}) if defined $opts{functions};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"functions"} 1 26 27
297 27 ▶ $step->set_valid_steps($opts{valid_steps}) if defined $opts{valid_steps};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"valid_steps"} 1 26 27
298  
299 27 return $step;
300 }
301  
302 3 ▶sub get_step {
Subroutine get_step: called CC=1 SCAR=0.0
Pod undocumented
303 3 my ($self, $name) = @_;
304 3 return $self->_steps->{$name};
305 }
306  
307 1 ▶sub remove_step {
Subroutine remove_step: called CC=2 SCAR=7.0
Pod undocumented
308 1 my ($self, $name) = @_;
309 1 ▶ if (exists $self->_steps->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->_steps->{$name}) 1 0 1
310 1 delete $self->_steps->{$name};
311 1 $self->_step_order([ grep { $_ ne $name } @{ $self->_step_order } ]);
312 }
313 1 return $self;
314 }
315  
316 1 ▶sub move_step {
Subroutine move_step: called CC=2 SCAR=7.0
Pod undocumented
317 1 my ($self, $name, $position) = @_;
318 die "Step '$name' not found in context '" . $self->name . "'"
319 1 ▶ unless exists $self->_steps->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->_steps->{$name} 0 1 1
320 1 my @order = grep { $_ ne $name } @{ $self->_step_order };
321 1 splice @order, $position, 0, $name;
322 1 $self->_step_order(\@order);
323 1 return $self;
324 }
325  
326 4 ▶sub set_valid_contexts {
Subroutine set_valid_contexts: called CC=1 SCAR=0.0
Pod undocumented
327 4 my ($self, $contexts) = @_;
328 4 $self->_valid_contexts($contexts);
329 4 return $self;
330 }
331  
332 1 ▶sub set_valid_steps {
Subroutine set_valid_steps: called CC=1 SCAR=0.0
Pod undocumented
333 1 my ($self, $steps) = @_;
334 1 $self->_valid_steps($steps);
335 1 return $self;
336 }
337  
338 1 ▶sub set_post_prompt {
Subroutine set_post_prompt: called CC=1 SCAR=0.0
Pod undocumented
339 1 my ($self, $pp) = @_;
340 1 $self->_post_prompt($pp);
341 1 return $self;
342 }
343  
344 1 ▶sub set_system_prompt {
Subroutine set_system_prompt: called CC=2 SCAR=7.0
Pod undocumented
345 1 my ($self, $sp) = @_;
346 die "Cannot use set_system_prompt() when POM sections have been added for system prompt"
347 1 ▶ if @{ $self->_system_prompt_sections };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->_system_prompt_sections;} 0 1 1
348 1 $self->_system_prompt($sp);
349 1 return $self;
350 }
351  
352 1 ▶sub set_consolidate {
Subroutine set_consolidate: called CC=2 SCAR=7.1
Pod undocumented
353 1 my ($self, $c) = @_;
354 1 ▶ $self->_consolidate($c ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$c ? : 1 0 1
355 1 return $self;
356 }
357  
358 1 ▶sub set_full_reset {
Subroutine set_full_reset: called CC=2 SCAR=7.1
Pod undocumented
359 1 my ($self, $fr) = @_;
360 1 ▶ $self->_full_reset($fr ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$fr ? : 1 0 1
361 1 return $self;
362 }
363  
364 1 ▶sub set_user_prompt {
Subroutine set_user_prompt: called CC=1 SCAR=0.0
Pod undocumented
365 1 my ($self, $up) = @_;
366 1 $self->_user_prompt($up);
367 1 return $self;
368 }
369  
370 1 ▶sub set_isolated {
Subroutine set_isolated: called CC=2 SCAR=7.1
Pod undocumented
371 1 my ($self, $iso) = @_;
372 1 ▶ $self->_isolated($iso ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$iso ? : 1 0 1
373 1 return $self;
374 }
375  
376 0 ▶sub add_system_section {
Subroutine add_system_section: not called CC=2 SCAR=17.9
Pod undocumented
377 0 my ($self, $title, $body) = @_;
378 0 ▶ die "Cannot add POM sections for system prompt when set_system_prompt() has been used"
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_system_prompt 0 0 0
379 if defined $self->_system_prompt;
380 0 push @{ $self->_system_prompt_sections }, { title => $title, body => $body };
381 0 return $self;
382 }
383  
384 0 ▶sub add_system_bullets {
Subroutine add_system_bullets: not called CC=2 SCAR=17.9
Pod undocumented
385 0 my ($self, $title, $bullets) = @_;
386 0 ▶ die "Cannot add POM sections for system prompt when set_system_prompt() has been used"
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_system_prompt 0 0 0
387 if defined $self->_system_prompt;
388 0 push @{ $self->_system_prompt_sections }, { title => $title, bullets => $bullets };
389 0 return $self;
390 }
391  
392 1 ▶sub set_prompt {
Subroutine set_prompt: called CC=2 SCAR=7.0
Pod undocumented
393 1 my ($self, $prompt) = @_;
394 die "Cannot use set_prompt() when POM sections have been added"
395 1 ▶ if @{ $self->_prompt_sections };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->_prompt_sections;} 0 1 1
396 1 $self->_prompt_text($prompt);
397 1 return $self;
398 }
399  
400 1 ▶sub add_section {
Subroutine add_section: called CC=2 SCAR=7.0
Pod undocumented
401 1 my ($self, $title, $body) = @_;
402 1 ▶ die "Cannot add POM sections when set_prompt() has been used"
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_prompt_text 0 1 1
403 if defined $self->_prompt_text;
404 1 push @{ $self->_prompt_sections }, { title => $title, body => $body };
405 1 return $self;
406 }
407  
408 1 ▶sub add_bullets {
Subroutine add_bullets: called CC=2 SCAR=7.0
Pod undocumented
409 1 my ($self, $title, $bullets) = @_;
410 1 ▶ die "Cannot add POM sections when set_prompt() has been used"
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_prompt_text 0 1 1
411 if defined $self->_prompt_text;
412 1 push @{ $self->_prompt_sections }, { title => $title, bullets => $bullets };
413 1 return $self;
414 }
415  
416 1 ▶sub set_enter_fillers {
Subroutine set_enter_fillers: called CC=2 SCAR=7.1
Pod undocumented
417 1 my ($self, $fillers) = @_;
418 1 ▶ $self->_enter_fillers($fillers) if ref $fillers eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $fillers eq "HASH" 1 0 1
419 1 return $self;
420 }
421  
422 0 ▶sub set_exit_fillers {
Subroutine set_exit_fillers: not called CC=2 SCAR=17.9
Pod undocumented
423 0 my ($self, $fillers) = @_;
424 0 ▶ $self->_exit_fillers($fillers) if ref $fillers eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $fillers eq "HASH" 0 0 0
425 0 return $self;
426 }
427  
428 0 ▶sub add_enter_filler {
Subroutine add_enter_filler: not called CC=4 SCAR=30.0
Pod undocumented
429 0 my ($self, $lang, $fillers) = @_;
430 0 ▶ if ($lang && ref $fillers eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang and ref $fillers eq "ARRAY") 0 0 0
Condition0%0/3
$lang and ref $fillers eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$lang and ref $fillers eq "ARRAY"
$lang ref $fillers eq "ARRAY"
Truth table0%0/3
A and B
A= $langB= ref $fillers eq "ARRAY"
ABresult
0X0
100
111
431 0 ▶ $self->_enter_fillers({}) unless defined $self->_enter_fillers;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_enter_fillers 0 0 0
432 0 $self->_enter_fillers->{$lang} = $fillers;
433 }
434 0 return $self;
435 }
436  
437 1 ▶sub add_exit_filler {
Subroutine add_exit_filler: called CC=4 SCAR=15.2
Pod undocumented
438 1 my ($self, $lang, $fillers) = @_;
439 1 ▶ if ($lang && ref $fillers eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang and ref $fillers eq "ARRAY") 1 0 1
Condition33%1/3
$lang and ref $fillers eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
$lang and ref $fillers eq "ARRAY"
$lang ref $fillers eq "ARRAY"
Truth table33%1/3
A and B
A= $langB= ref $fillers eq "ARRAY"
ABresult
0X0
100
111
440 1 ▶ $self->_exit_fillers({}) unless defined $self->_exit_fillers;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_exit_fillers 1 0 1
441 1 $self->_exit_fillers->{$lang} = $fillers;
442 }
443 1 return $self;
444 }
445  
446 0 ▶sub _render_prompt {
Subroutine _render_prompt: not called CC=3 SCAR=24.8
447 0 my ($self) = @_;
448 0 ▶ return $self->_prompt_text if defined $self->_prompt_text;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_prompt_text 0 0 0
449 0 ▶ return undef unless @{ $self->_prompt_sections };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->_prompt_sections;} 0 0 0
450 0 return _render_sections($self->_prompt_sections);
451 }
452  
453 12 ▶sub _render_system_prompt {
Subroutine _render_system_prompt: called CC=3 SCAR=11.3
454 12 my ($self) = @_;
455 12 ▶ return $self->_system_prompt if defined $self->_system_prompt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_system_prompt 1 11 12
456 11 ▶ return undef unless @{ $self->_system_prompt_sections };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->_system_prompt_sections;} 11 0 11
457 0 return _render_sections($self->_system_prompt_sections);
458 }
459  
460 0 ▶sub _render_sections {
Subroutine _render_sections: not called CC=3 SCAR=24.8
461 0 my ($sections) = @_;
462 0 my @parts;
463 0 for my $sec (@$sections) {
464 0 ▶ if (exists $sec->{bullets}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sec->{'bullets'}) { } 0 0 0
465 0 push @parts, "## $sec->{title}";
466 0 push @parts, map { "- $_" } @{ $sec->{bullets} };
467 } else {
468 0 push @parts, "## $sec->{title}";
469 0 push @parts, $sec->{body};
470 }
471 0 push @parts, '';
472 }
473 0 my $text = join("\n", @parts);
474 0 $text =~ s/\s+$//;
475 0 return $text;
476 }
477  
478 12 ▶sub to_hash {
Subroutine to_hash: called CC=14 SCAR=26.4
Pod undocumented
479 12 my ($self) = @_;
480 die "Context '" . $self->name . "' has no steps defined"
481 12 ▶ unless keys %{ $self->_steps };
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$self->_steps;} 0 12 12
482  
483 my %d = (
484 12 steps => [ map { $self->_steps->{$_}->to_hash } @{ $self->_step_order } ],
485 );
486  
487 12 ▶ $d{valid_contexts} = $self->_valid_contexts if defined $self->_valid_contexts;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_valid_contexts 3 9 12
488 12 ▶ $d{valid_steps} = $self->_valid_steps if defined $self->_valid_steps;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_valid_steps 1 11 12
489 12 ▶ $d{post_prompt} = $self->_post_prompt if defined $self->_post_prompt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_post_prompt 1 11 12
490  
491 12 my $sp = $self->_render_system_prompt;
492 12 ▶ $d{system_prompt} = $sp if defined $sp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sp 1 11 12
493  
494 12 ▶ $d{consolidate} = JSON::true if $self->_consolidate;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_consolidate 1 11 12
495 12 ▶ $d{full_reset} = JSON::true if $self->_full_reset;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_full_reset 1 11 12
496 12 ▶ $d{user_prompt} = $self->_user_prompt if defined $self->_user_prompt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_user_prompt 1 11 12
497 12 ▶ $d{isolated} = JSON::true if $self->_isolated;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isolated 1 11 12
498  
499 12 ▶ if (@{ $self->_prompt_sections }) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->_prompt_sections;}) { } 1 11 12
elsif (defined $self->_prompt_text) { } 1 10 11
500 1 $d{pom} = $self->_prompt_sections;
501 } elsif (defined $self->_prompt_text) {
502 1 $d{prompt} = $self->_prompt_text;
503 }
504  
505 12 ▶ $d{enter_fillers} = $self->_enter_fillers if defined $self->_enter_fillers;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_enter_fillers 1 11 12
506 12 ▶ $d{exit_fillers} = $self->_exit_fillers if defined $self->_exit_fillers;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_exit_fillers 1 11 12
507  
508 12 return \%d;
509 }
510  
511 # ==========================================================================
512 # ContextBuilder
513 # ==========================================================================
514 package SignalWire::Agents::Contexts::ContextBuilder;
515 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
516 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
517  
518 13 ▶has '_contexts' => (is => 'rw', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
519 13 ▶has '_context_order' => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
520  
521 14 ▶sub add_context {
Subroutine add_context: called CC=3 SCAR=11.0
Pod undocumented
522 14 my ($self, $name) = @_;
523 14 ▶ die "Context '$name' already exists" if exists $self->_contexts->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_contexts->{$name} 1 13 14
524 die "Maximum number of contexts ($SignalWire::Agents::Contexts::MAX_CONTEXTS) exceeded"
525 13 ▶ if keys %{ $self->_contexts } >= $SignalWire::Agents::Contexts::MAX_CONTEXTS;
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$self->_contexts;} >= $SignalWire::Agents::Contexts::MAX_CONTEXTS 0 13 13
526  
527 13 my $ctx = SignalWire::Agents::Contexts::Context->new(name => $name);
528 13 $self->_contexts->{$name} = $ctx;
529 13 push @{ $self->_context_order }, $name;
530 13 return $ctx;
531 }
532  
533 2 ▶sub get_context {
Subroutine get_context: called CC=1 SCAR=0.0
Pod undocumented
534 2 my ($self, $name) = @_;
535 2 return $self->_contexts->{$name};
536 }
537  
538 0 ▶sub has_contexts {
Subroutine has_contexts: not called CC=2 SCAR=17.9
Pod undocumented
539 0 my ($self) = @_;
540 0 ▶ return scalar(keys %{ $self->_contexts }) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %{$self->_contexts;} ? : 0 0 0
541 }
542  
543 11 ▶sub validate {
Subroutine validate: called CC=33 SCAR=36.1
Pod undocumented
544 11 my ($self) = @_;
545 11 ▶ die "At least one context must be defined" unless keys %{ $self->_contexts };
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$self->_contexts;} 1 10 11
546  
547 # Single context must be "default"
548 10 ▶ if (keys %{ $self->_contexts } == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$self->_contexts;} == 1) 9 1 10
549 9 my ($name) = keys %{ $self->_contexts };
550 9 ▶ die 'When using a single context, it must be named "default"'
Branch: 1 branch
BranchTrueFalseTotal
unless $name eq "default" 1 8 9
551 unless $name eq 'default';
552 }
553  
554 # Each context must have steps
555 9 for my $cname (keys %{ $self->_contexts }) {
556 10 my $ctx = $self->_contexts->{$cname};
557 die "Context '$cname' must have at least one step"
558 10 ▶ unless keys %{ $ctx->_steps };
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$ctx->_steps;} 1 9 10
559 }
560  
561 # Validate step references in valid_steps
562 8 for my $cname (keys %{ $self->_contexts }) {
563 9 my $ctx = $self->_contexts->{$cname};
564 9 for my $sname (keys %{ $ctx->_steps }) {
565 11 my $step = $ctx->_steps->{$sname};
566 11 ▶ if (defined $step->_valid_steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $step->_valid_steps) 2 9 11
567 2 for my $vs (@{ $step->_valid_steps }) {
568 2 ▶ next if $vs eq 'next';
Branch: 1 branch
BranchTrueFalseTotal
if $vs eq "next" 1 1 2
569 die "Step '$sname' in context '$cname' references unknown step '$vs'"
570 1 ▶ unless exists $ctx->_steps->{$vs};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ctx->_steps->{$vs} 1 0 1
571 }
572 }
573 }
574 }
575  
576 # Validate context references (context-level and step-level)
577 7 for my $cname (keys %{ $self->_contexts }) {
578 8 my $ctx = $self->_contexts->{$cname};
579 8 ▶ if (defined $ctx->_valid_contexts) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctx->_valid_contexts) 3 5 8
580 3 for my $vc (@{ $ctx->_valid_contexts }) {
581 die "Context '$cname' references unknown context '$vc'"
582 3 ▶ unless exists $self->_contexts->{$vc};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->_contexts->{$vc} 1 2 3
583 }
584 }
585 7 for my $sname (keys %{ $ctx->_steps }) {
586 9 my $step = $ctx->_steps->{$sname};
587 9 ▶ if (defined $step->_valid_contexts) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $step->_valid_contexts) 0 9 9
588 0 for my $vc (@{ $step->_valid_contexts }) {
589 die "Step '$sname' in context '$cname' references unknown context '$vc'"
590 0 ▶ unless exists $self->_contexts->{$vc};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->_contexts->{$vc} 0 0 0
591 }
592 }
593 }
594 }
595  
596 # Validate gather_info
597 6 for my $cname (keys %{ $self->_contexts }) {
598 7 my $ctx = $self->_contexts->{$cname};
599 7 for my $sname (keys %{ $ctx->_steps }) {
600 9 my $step = $ctx->_steps->{$sname};
601 9 ▶ if (defined $step->_gather_info) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $step->_gather_info) 3 6 9
602 die "Step '$sname' in context '$cname' has gather_info with no questions"
603 3 ▶ unless @{ $step->_gather_info->_questions };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$step->_gather_info->_questions;} 1 2 3
604  
605 2 my %seen;
606 2 for my $q (@{ $step->_gather_info->_questions }) {
607 1 die "Step '$sname' in context '$cname' has duplicate gather_info question key '${\$q->key}'"
608 3 ▶ if $seen{ $q->key }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$q->key}++ 1 2 3
609 }
610  
611 1 my $action = $step->_gather_info->_completion_action;
612 1 ▶ if (defined $action) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $action) 1 0 1
613 1 ▶ if ($action eq 'next_step') {
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq 'next_step') { } 1 0 1
elsif (not exists $ctx->_steps->{$action}) { } 0 0 0
614 1 my $idx;
615 1 my @order = @{ $ctx->_step_order };
616 1 for my $i (0 .. $#order) {
617 1 ▶ if ($order[$i] eq $sname) { $idx = $i; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($order[$i] eq $sname) 1 0 1
618 }
619 1 ▶ die "Step '$sname' in context '$cname' has gather_info completion_action='next_step' but it is the last step"
Branch: 1 branch
BranchTrueFalseTotal
if defined $idx and $idx >= $#order 1 0 1
Condition33%1/3
defined $idx and $idx >= $#order
!ll&&!rl&&r
001
MC/DC0%0/2
defined $idx and $idx >= $#order
defined $idx $idx >= $#order
Truth table33%1/3
A and B
A= defined $idxB= $idx >= $#order
ABresult
0X0
100
111
620 if defined $idx && $idx >= $#order;
621 } elsif (!exists $ctx->_steps->{$action}) {
622 0 die "Step '$sname' in context '$cname' has gather_info completion_action='$action' but step '$action' does not exist";
623 }
624 }
625 }
626 }
627 }
628 }
629  
630 11 ▶sub to_hash {
Subroutine to_hash: called CC=2 SCAR=6.9
Pod undocumented
631 11 my ($self) = @_;
632 11 $self->validate;
633  
634 3 my %result;
635 3 for my $cname (@{ $self->_context_order }) {
636 4 $result{$cname} = $self->_contexts->{$cname}->to_hash;
637 }
638 3 return \%result;
639 }
640  
641 # Back to main package
642 package SignalWire::Agents::Contexts;
643  
644 2 ▶sub create_simple_context {
Subroutine create_simple_context: called CC=2 SCAR=6.9
Pod undocumented
645 2 my ($class, $name) = @_;
646 2 ▶ $name //= 'default';
Condition100%2/2
$name //= "default"
l!l
11
MC/DC100%1/1
$name //= "default"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
647 2 return SignalWire::Agents::Contexts::Context->new(name => $name);
648 }
649  
650 1 1;
« blib/lib/SignalWire/Agents/Agent/AgentBase.pm ↑ Summary blib/lib/SignalWire/Agents/Security/SessionManager.pm »