blib/lib/SignalWire/Agents/Agent/AgentBase.pm

filter: Statementstmt 92.6% 601 / 649 Branchbran 77.7% 140 / 180 Conditioncond 51.9% 79 / 152 MC/DCmcdc 30.2% 26 / 86 Subroutinesub 93.5% 115 / 123 Podpod 0.0% 0 / 58 totaltotal 77.0% 961 / 1248 CC 161 SCAR 24.6
CC
161
Cov
84%
extract_sip_username
72.0
_handle_post_prompt
30.0
_build_ai_verb
29.4
CRAP 11.7
« blib/lib/SignalWire/Agents/Skills/SkillManager.pm ↑ Summary blib/lib/SignalWire/Agents/Contexts.pm »
1 package SignalWire::Agents::Agent::AgentBase;
2 # Copyright (c) 2025 SignalWire
3 # Licensed under the MIT License.
4  
5 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 35 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 ▶use MIME::Base64 qw(encode_base64 decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use Digest::SHA qw(hmac_sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 35 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 35 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # ---------- attributes ----------
17  
18 55 ▶has name => (is => 'rw', default => sub { 'agent' });
Subroutine __ANON__: called CC=1 SCAR=0.0
19 338 ▶has route => (is => 'rw', default => sub { '/' });
Subroutine __ANON__: called CC=1 SCAR=0.0
20 357 ▶has host => (is => 'rw', default => sub { '0.0.0.0' });
Subroutine __ANON__: called CC=1 SCAR=0.0
21 357 ▶has port => (is => 'rw', default => sub { $ENV{PORT} || 3000 });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"PORT"} or 3000 0 357 357
22 35 has basic_auth_user => (is => 'rw', lazy => 1, builder => '_build_basic_auth_user');
23 35 has basic_auth_password => (is => 'rw', lazy => 1, builder => '_build_basic_auth_password');
24  
25 # Call settings
26 357 ▶has auto_answer => (is => 'rw', default => sub { 1 });
Subroutine __ANON__: called CC=1 SCAR=0.0
27 356 ▶has record_call => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
28 358 ▶has record_format => (is => 'rw', default => sub { 'mp4' });
Subroutine __ANON__: called CC=1 SCAR=0.0
29 358 ▶has record_stereo => (is => 'rw', default => sub { 1 });
Subroutine __ANON__: called CC=1 SCAR=0.0
30  
31 # Prompt system
32 360 ▶has prompt_text => (is => 'rw', default => sub { '' });
Subroutine __ANON__: called CC=1 SCAR=0.0
33 360 ▶has post_prompt => (is => 'rw', default => sub { '' });
Subroutine __ANON__: called CC=1 SCAR=0.0
34 355 ▶has use_pom => (is => 'rw', default => sub { 1 });
Subroutine __ANON__: called CC=1 SCAR=0.0
35 360 ▶has pom_sections => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
36  
37 # Tool registry
38 360 ▶has tools => (is => 'rw', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
39 360 ▶has tool_order => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
40  
41 # AI config
42 360 ▶has hints => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
43 360 ▶has pattern_hints => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
44 360 ▶has languages => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
45 360 ▶has pronunciations => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
46 360 ▶has params => (is => 'rw', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
47 360 ▶has global_data => (is => 'rw', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
48 360 ▶has native_functions => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
49  
50 # Internal settings
51 360 ▶has internal_fillers => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
52 360 ▶has debug_events_level => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
53  
54 # Includes and LLM params
55 360 ▶has function_includes => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
56 360 ▶has prompt_llm_params => (is => 'rw', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
57 360 ▶has post_prompt_llm_params => (is => 'rw', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
58  
59 # Verb insertion points
60 360 ▶has pre_answer_verbs => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
61 360 ▶has post_answer_verbs => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
62 360 ▶has post_ai_verbs => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
63 360 ▶has answer_config => (is => 'rw', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
64  
65 # Context system (lazy)
66 35 has context_builder => (
67 is => 'rw',
68 lazy => 1,
69 builder => '_build_context_builder',
70 );
71  
72 # Callbacks
73 363 ▶has dynamic_config_callback => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
74 363 ▶has summary_callback => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
75 363 ▶has debug_event_handler => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
76  
77 # URLs
78 360 ▶has webhook_url => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
79 360 ▶has post_prompt_url => (is => 'rw', default => sub { undef });
Subroutine __ANON__: called CC=1 SCAR=0.0
80 35 has proxy_url_base => (is => 'rw', lazy => 1, builder => '_build_proxy_url_base');
81 360 ▶has swaig_query_params => (is => 'rw', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
82  
83 # Session manager (built in BUILD)
84 35 has session_manager => (is => 'rw');
85  
86 # Skill manager
87 35 has skill_manager => (is => 'rw', lazy => 1, builder => '_build_skill_manager');
88  
89 # ---------- builders ----------
90  
91 66 ▶sub _build_basic_auth_user {
Subroutine _build_basic_auth_user: called CC=2 SCAR=7.1
92 66 my ($self) = @_;
93 66 ▶ return $ENV{SWML_BASIC_AUTH_USER} || $self->name;
Condition66%2/3
$ENV{'SWML_BASIC_AUTH_USER'} || $self->name
l!l&&r!l&&!r
1650
MC/DC0%0/2
$ENV{'SWML_BASIC_AUTH_USER'} || $self->name
$ENV{'SWML_BASIC_AUTH_USER'} $self->name
Truth table66%2/3
A || B
A= $ENV{'SWML_BASIC_AUTH_USER'}B= $self->name
ABresult
1X1
011
000
94 }
95  
96 66 ▶sub _build_basic_auth_password {
Subroutine _build_basic_auth_password: called CC=2 SCAR=7.1
97 66 my ($self) = @_;
98 66 ▶ return $ENV{SWML_BASIC_AUTH_PASSWORD} || _generate_random_password();
Condition66%2/3
$ENV{'SWML_BASIC_AUTH_PASSWORD'} || _generate_random_password()
l!l&&r!l&&!r
1650
MC/DC0%0/2
$ENV{'SWML_BASIC_AUTH_PASSWORD'} || _generate_random_password()
$ENV{'SWML_BASIC_AUTH_PASSWORD'} _generate_random_password()
Truth table66%2/3
A || B
A= $ENV{'SWML_BASIC_AUTH_PASSWORD'}B= _generate_random_password()
ABresult
1X1
011
000
99 }
100  
101 78 ▶sub _build_proxy_url_base {
Subroutine _build_proxy_url_base: called CC=2 SCAR=6.9
102 78 ▶ return $ENV{SWML_PROXY_URL_BASE} || undef;
Condition100%2/2
$ENV{'SWML_PROXY_URL_BASE'} || undef
l!l
177
MC/DC100%1/1
$ENV{'SWML_PROXY_URL_BASE'} || undef
$ENV{'SWML_PROXY_URL_BASE'}
Truth table100%2/2
A
A= $ENV{'SWML_PROXY_URL_BASE'}
Aresult
11
00
103 }
104  
105 71 ▶sub _build_context_builder {
Subroutine _build_context_builder: called CC=1 SCAR=0.0
106 71 require SignalWire::Agents::Contexts::ContextBuilder;
107 71 return SignalWire::Agents::Contexts::ContextBuilder->new;
108 }
109  
110 2 ▶sub _build_skill_manager {
Subroutine _build_skill_manager: called CC=1 SCAR=0.0
111 2 my ($self) = @_;
112 2 require SignalWire::Agents::Skills::SkillManager;
113 2 return SignalWire::Agents::Skills::SkillManager->new(agent => $self);
114 }
115  
116 363 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
117 363 my ($self) = @_;
118  
119 # Strip trailing slash from route
120 363 my $r = $self->route;
121 363 ▶ $r =~ s{/+$}{} if $r ne '/';
Branch: 1 branch
BranchTrueFalseTotal
if $r ne "/" 21 342 363
122 363 $self->route($r);
123  
124 # Initialize session manager
125 363 require SignalWire::Agents::Security::SessionManager;
126 363 $self->session_manager(
127 SignalWire::Agents::Security::SessionManager->new(token_expiry_secs => 3600)
128 );
129 }
130  
131 # ---------- Prompt methods ----------
132  
133 11 ▶sub set_prompt_text {
Subroutine set_prompt_text: called CC=1 SCAR=0.0
Pod undocumented
134 11 my ($self, $text) = @_;
135 11 $self->prompt_text($text);
136 11 return $self;
137 }
138  
139 6 ▶sub set_post_prompt {
Subroutine set_post_prompt: called CC=1 SCAR=0.0
Pod undocumented
140 6 my ($self, $text) = @_;
141 6 $self->post_prompt($text);
142 6 return $self;
143 }
144  
145 159 ▶sub prompt_add_section {
Subroutine prompt_add_section: called CC=3 SCAR=11.0
Pod undocumented
146 159 my ($self, $title, $body, %opts) = @_;
147 159 ▶ my $section = {
Condition100%2/2
$body // ""
l!l
1581
MC/DC100%1/1
$body // ""
$body
Truth table100%2/2
A
A= $body
Aresult
11
00
148 title => $title,
149 body => $body // '',
150 };
151 159 ▶ $section->{bullets} = $opts{bullets} if $opts{bullets};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"bullets"} 108 51 159
152 159 push @{ $self->pom_sections }, $section;
153 159 return $self;
154 }
155  
156 7 ▶sub prompt_add_subsection {
Subroutine prompt_add_subsection: called CC=6 SCAR=18.0
Pod undocumented
157 7 my ($self, $parent_title, $title, $body, %opts) = @_;
158 7 for my $sec (@{ $self->pom_sections }) {
159 6 ▶ if ($sec->{title} eq $parent_title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sec->{"title"} eq $parent_title) 6 0 6
160 6 ▶ $sec->{subsections} //= [];
Condition100%2/2
$sec->{"subsections"} //= []
l!l
15
MC/DC100%1/1
$sec->{"subsections"} //= []
$sec->{"subsections"}
Truth table100%2/2
A
A= $sec->{"subsections"}
Aresult
11
00
161 6 ▶ my $sub = { title => $title, body => $body // '' };
Condition50%1/2
$body // ""
l!l
60
MC/DC0%0/1
$body // ""
$body
Truth table50%1/2
A
A= $body
Aresult
11
00
162 6 ▶ $sub->{bullets} = $opts{bullets} if $opts{bullets};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"bullets"} 2 4 6
163 6 push @{ $sec->{subsections} }, $sub;
164 6 last;
165 }
166 }
167 7 return $self;
168 }
169  
170 5 ▶sub prompt_add_to_section {
Subroutine prompt_add_to_section: called CC=6 SCAR=17.9
Pod undocumented
171 5 my ($self, $title, %opts) = @_;
172 5 for my $sec (@{ $self->pom_sections }) {
173 5 ▶ if ($sec->{title} eq $title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sec->{"title"} eq $title) 5 0 5
174 5 ▶ if ($opts{body}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"body"}) 1 4 5
175 1 $sec->{body} .= "\n" . $opts{body};
176 }
177 5 ▶ if ($opts{bullets}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"bullets"}) 4 1 5
178 4 ▶ $sec->{bullets} //= [];
Condition100%2/2
$sec->{"bullets"} //= []
l!l
22
MC/DC100%1/1
$sec->{"bullets"} //= []
$sec->{"bullets"}
Truth table100%2/2
A
A= $sec->{"bullets"}
Aresult
11
00
179 4 push @{ $sec->{bullets} }, @{ $opts{bullets} };
180 }
181 5 last;
182 }
183 }
184 5 return $self;
185 }
186  
187 35 ▶sub prompt_has_section {
Subroutine prompt_has_section: called CC=3 SCAR=11.0
Pod undocumented
188 35 my ($self, $title) = @_;
189 35 for my $sec (@{ $self->pom_sections }) {
190 65 ▶ return 1 if $sec->{title} eq $title;
Branch: 1 branch
BranchTrueFalseTotal
if $sec->{"title"} eq $title 28 37 65
191 }
192 7 return 0;
193 }
194  
195 75 ▶sub get_prompt {
Subroutine get_prompt: called CC=3 SCAR=11.0
Pod undocumented
196 75 my ($self) = @_;
197 75 ▶ if ($self->use_pom && @{ $self->pom_sections }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_pom and @{$self->pom_sections;}) 24 51 75
Condition100%3/3
$self->use_pom and @{$self->pom_sections;}
!ll&&!rl&&r
54624
MC/DC100%2/2
$self->use_pom and @{$self->pom_sections;}
$self->use_pom @{$self->pom_sections;}
Truth table100%3/3
A and B
A= $self->use_pomB= @{$self->pom_sections;}
ABresult
0X0
100
111
198 24 return $self->pom_sections;
199 }
200 51 return $self->prompt_text;
201 }
202  
203 # ---------- Tool methods ----------
204  
205 163 ▶sub define_tool {
Subroutine define_tool: called CC=8 SCAR=20.8
Pod undocumented
206 163 my ($self, %opts) = @_;
207 163 ▶ my $name = $opts{name} // croak("define_tool requires 'name'");
Condition100%2/2
$opts{"name"} // croak("define_tool requires 'name'")
l!l
1621
MC/DC100%1/1
$opts{"name"} // croak("define_tool requires 'name'")
$opts{"name"}
Truth table100%2/2
A
A= $opts{"name"}
Aresult
11
00
208 162 ▶ my $description = $opts{description} // '';
Condition100%2/2
$opts{"description"} // ""
l!l
1602
MC/DC100%1/1
$opts{"description"} // ""
$opts{"description"}
Truth table100%2/2
A
A= $opts{"description"}
Aresult
11
00
209 162 ▶ my $parameters = $opts{parameters} // { type => 'object', properties => {} };
Condition100%2/2
$opts{"parameters"} // {"type", "object", "properties", {}}
l!l
14220
MC/DC100%1/1
$opts{"parameters"} // {"type", "object", "properties", {}}
$opts{"parameters"}
Truth table100%2/2
A
A= $opts{"parameters"}
Aresult
11
00
210 162 my $handler = $opts{handler};
211  
212 162 ▶ my $tool_def = {
Branch: 1 branch
BranchTrueFalseTotal
defined $handler ? : 150 12 162
213 function => $name,
214 description => $description,
215 parameters => $parameters,
216 (defined $handler ? (_handler => $handler) : ()),
217 };
218  
219 # Merge any extra fields (fillers, meta_data_token, etc.)
220 162 for my $k (keys %opts) {
221 616 ▶ next if $k =~ /^(name|description|parameters|handler)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^(name|description|parameters|handler)$/ 614 2 616
222 2 $tool_def->{$k} = $opts{$k};
223 }
224  
225 162 $self->tools->{$name} = $tool_def;
226 # Maintain insertion order
227 161 push @{ $self->tool_order }, $name
228 162 ▶ unless grep { $_ eq $name } @{ $self->tool_order };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $name;} @{$self->tool_order;} 161 1 162
229  
230 162 return $self;
231 }
232  
233 23 ▶sub register_swaig_function {
Subroutine register_swaig_function: called CC=3 SCAR=11.0
Pod undocumented
234 23 my ($self, $func_def) = @_;
235 23 ▶ my $name = $func_def->{function} // croak("register_swaig_function needs 'function' key");
Condition100%2/2
$func_def->{"function"} // croak("register_swaig_function needs 'function' key")
l!l
221
MC/DC100%1/1
$func_def->{"function"} // croak("register_swaig_function needs 'function' key")
$func_def->{"function"}
Truth table100%2/2
A
A= $func_def->{"function"}
Aresult
11
00
236 22 $self->tools->{$name} = $func_def;
237 22 push @{ $self->tool_order }, $name
238 22 ▶ unless grep { $_ eq $name } @{ $self->tool_order };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $name;} @{$self->tool_order;} 22 0 22
239 22 return $self;
240 }
241  
242 2 ▶sub define_tools {
Subroutine define_tools: called CC=4 SCAR=13.9
Pod undocumented
243 2 my ($self, @tool_defs) = @_;
244 2 for my $t (@tool_defs) {
245 4 ▶ if (ref $t eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $t eq "HASH") 4 0 4
246 4 ▶ if (exists $t->{function}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{'function'}) { } 2 2 4
247 2 $self->register_swaig_function($t);
248 } else {
249 2 $self->define_tool(%$t);
250 }
251 }
252 }
253 2 return $self;
254 }
255  
256 24 ▶sub on_function_call {
Subroutine on_function_call: called CC=3 SCAR=11.0
Pod undocumented
257 24 my ($self, $name, $args, $raw_data) = @_;
258 24 my $tool = $self->tools->{$name};
259 24 ▶ return undef unless $tool && $tool->{_handler};
Branch: 1 branch
BranchTrueFalseTotal
unless $tool and $tool->{"_handler"} 3 21 24
Condition100%3/3
$tool and $tool->{"_handler"}
!ll&&!rl&&r
2121
MC/DC100%2/2
$tool and $tool->{"_handler"}
$tool $tool->{"_handler"}
Truth table100%3/3
A and B
A= $toolB= $tool->{"_handler"}
ABresult
0X0
100
111
260 21 return $tool->{_handler}->($args, $raw_data);
261 }
262  
263 # ---------- AI Config methods ----------
264  
265 10 ▶sub add_hint {
Subroutine add_hint: called CC=1 SCAR=0.0
Pod undocumented
266 10 my ($self, $hint) = @_;
267 10 push @{ $self->hints }, $hint;
268 10 return $self;
269 }
270  
271 4 ▶sub add_hints {
Subroutine add_hints: called CC=1 SCAR=0.0
Pod undocumented
272 4 my ($self, @h) = @_;
273 4 push @{ $self->hints }, @h;
274 4 return $self;
275 }
276  
277 1 ▶sub add_pattern_hint {
Subroutine add_pattern_hint: called CC=1 SCAR=0.0
Pod undocumented
278 1 my ($self, $pattern) = @_;
279 1 push @{ $self->pattern_hints }, $pattern;
280 1 return $self;
281 }
282  
283 4 ▶sub add_language {
Subroutine add_language: called CC=1 SCAR=0.0
Pod undocumented
284 4 my ($self, %lang) = @_;
285 4 push @{ $self->languages }, \%lang;
286 4 return $self;
287 }
288  
289 1 ▶sub set_languages {
Subroutine set_languages: called CC=1 SCAR=0.0
Pod undocumented
290 1 my ($self, $langs) = @_;
291 1 $self->languages($langs);
292 1 return $self;
293 }
294  
295 4 ▶sub add_pronunciation {
Subroutine add_pronunciation: called CC=1 SCAR=0.0
Pod undocumented
296 4 my ($self, %pron) = @_;
297 4 push @{ $self->pronunciations }, \%pron;
298 4 return $self;
299 }
300  
301 1 ▶sub set_pronunciations {
Subroutine set_pronunciations: called CC=1 SCAR=0.0
Pod undocumented
302 1 my ($self, $prons) = @_;
303 1 $self->pronunciations($prons);
304 1 return $self;
305 }
306  
307 7 ▶sub set_param {
Subroutine set_param: called CC=1 SCAR=0.0
Pod undocumented
308 7 my ($self, $key, $value) = @_;
309 7 $self->params->{$key} = $value;
310 7 return $self;
311 }
312  
313 2 ▶sub set_params {
Subroutine set_params: called CC=1 SCAR=0.0
Pod undocumented
314 2 my ($self, $p) = @_;
315 2 $self->params({ %{ $self->params }, %$p });
316 2 return $self;
317 }
318  
319 66 ▶sub set_global_data {
Subroutine set_global_data: called CC=1 SCAR=0.0
Pod undocumented
320 66 my ($self, $data) = @_;
321 66 $self->global_data($data);
322 66 return $self;
323 }
324  
325 3 ▶sub update_global_data {
Subroutine update_global_data: called CC=1 SCAR=0.0
Pod undocumented
326 3 my ($self, $data) = @_;
327 3 $self->global_data({ %{ $self->global_data }, %$data });
328 3 return $self;
329 }
330  
331 5 ▶sub set_native_functions {
Subroutine set_native_functions: called CC=1 SCAR=0.0
Pod undocumented
332 5 my ($self, $funcs) = @_;
333 5 $self->native_functions($funcs);
334 5 return $self;
335 }
336  
337 3 ▶sub set_internal_fillers {
Subroutine set_internal_fillers: called CC=1 SCAR=0.0
Pod undocumented
338 3 my ($self, $fillers) = @_;
339 3 $self->internal_fillers($fillers);
340 3 return $self;
341 }
342  
343 2 ▶sub add_internal_filler {
Subroutine add_internal_filler: called CC=2 SCAR=6.9
Pod undocumented
344 2 my ($self, $filler) = @_;
345 2 ▶ if (!defined $self->internal_fillers) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->internal_fillers) 1 1 2
346 1 $self->internal_fillers([]);
347 }
348 2 push @{ $self->internal_fillers }, $filler;
349 2 return $self;
350 }
351  
352 5 ▶sub enable_debug_events {
Subroutine enable_debug_events: called CC=2 SCAR=6.9
Pod undocumented
353 5 my ($self, $level) = @_;
354 5 ▶ $level //= 1;
Condition100%2/2
$level //= 1
l!l
41
MC/DC100%1/1
$level //= 1
$level
Truth table100%2/2
A
A= $level
Aresult
11
00
355 5 $self->debug_events_level($level);
356 5 return $self;
357 }
358  
359 3 ▶sub add_function_include {
Subroutine add_function_include: called CC=1 SCAR=0.0
Pod undocumented
360 3 my ($self, $include) = @_;
361 3 push @{ $self->function_includes }, $include;
362 3 return $self;
363 }
364  
365 0 ▶sub set_function_includes {
Subroutine set_function_includes: not called CC=1 SCAR=6.9
Pod undocumented
366 0 my ($self, $includes) = @_;
367 0 $self->function_includes($includes);
368 0 return $self;
369 }
370  
371 8 ▶sub set_prompt_llm_params {
Subroutine set_prompt_llm_params: called CC=1 SCAR=0.0
Pod undocumented
372 8 my ($self, %p) = @_;
373 8 $self->prompt_llm_params({ %{ $self->prompt_llm_params }, %p });
374 8 return $self;
375 }
376  
377 4 ▶sub set_post_prompt_llm_params {
Subroutine set_post_prompt_llm_params: called CC=1 SCAR=0.0
Pod undocumented
378 4 my ($self, %p) = @_;
379 4 $self->post_prompt_llm_params({ %{ $self->post_prompt_llm_params }, %p });
380 4 return $self;
381 }
382  
383 # ---------- Verb management ----------
384  
385 10 ▶sub add_pre_answer_verb {
Subroutine add_pre_answer_verb: called CC=1 SCAR=0.0
Pod undocumented
386 10 my ($self, $verb_name, $verb_config) = @_;
387 10 push @{ $self->pre_answer_verbs }, { $verb_name => $verb_config };
388 10 return $self;
389 }
390  
391 8 ▶sub add_post_answer_verb {
Subroutine add_post_answer_verb: called CC=1 SCAR=0.0
Pod undocumented
392 8 my ($self, $verb_name, $verb_config) = @_;
393 8 push @{ $self->post_answer_verbs }, { $verb_name => $verb_config };
394 8 return $self;
395 }
396  
397 8 ▶sub add_post_ai_verb {
Subroutine add_post_ai_verb: called CC=1 SCAR=0.0
Pod undocumented
398 8 my ($self, $verb_name, $verb_config) = @_;
399 8 push @{ $self->post_ai_verbs }, { $verb_name => $verb_config };
400 8 return $self;
401 }
402  
403 2 ▶sub clear_pre_answer_verbs {
Subroutine clear_pre_answer_verbs: called CC=1 SCAR=0.0
Pod undocumented
404 2 my ($self) = @_;
405 2 $self->pre_answer_verbs([]);
406 2 return $self;
407 }
408  
409 2 ▶sub clear_post_answer_verbs {
Subroutine clear_post_answer_verbs: called CC=1 SCAR=0.0
Pod undocumented
410 2 my ($self) = @_;
411 2 $self->post_answer_verbs([]);
412 2 return $self;
413 }
414  
415 2 ▶sub clear_post_ai_verbs {
Subroutine clear_post_ai_verbs: called CC=1 SCAR=0.0
Pod undocumented
416 2 my ($self) = @_;
417 2 $self->post_ai_verbs([]);
418 2 return $self;
419 }
420  
421 1 ▶sub set_answer_config {
Subroutine set_answer_config: called CC=1 SCAR=0.0
Pod undocumented
422 1 my ($self, $config) = @_;
423 1 $self->answer_config($config);
424 1 return $self;
425 }
426  
427 # ---------- Contexts ----------
428  
429 2 ▶sub define_contexts {
Subroutine define_contexts: called CC=1 SCAR=0.0
Pod undocumented
430 2 my ($self) = @_;
431 2 return $self->context_builder;
432 }
433  
434 0 ▶sub contexts {
Subroutine contexts: not called CC=1 SCAR=6.9
Pod undocumented
435 0 my ($self) = @_;
436 0 return $self->context_builder;
437 }
438  
439 # ---------- Skills ----------
440  
441 2 ▶sub add_skill {
Subroutine add_skill: called CC=2 SCAR=7.1
Pod undocumented
442 2 my ($self, $skill_name, $params) = @_;
443 2 ▶ $params //= {};
Condition50%1/2
$params //= {}
l!l
02
MC/DC0%0/1
$params //= {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
444 2 return $self->skill_manager->load_skill($skill_name, undef, $params);
445 }
446  
447 0 ▶sub remove_skill {
Subroutine remove_skill: not called CC=1 SCAR=6.9
Pod undocumented
448 0 my ($self, $skill_name) = @_;
449 0 return $self->skill_manager->unload_skill($skill_name);
450 }
451  
452 0 ▶sub list_skills {
Subroutine list_skills: not called CC=1 SCAR=6.9
Pod undocumented
453 0 my ($self) = @_;
454 0 return $self->skill_manager->list_skills;
455 }
456  
457 1 ▶sub has_skill {
Subroutine has_skill: called CC=1 SCAR=0.0
Pod undocumented
458 1 my ($self, $skill_name) = @_;
459 1 return $self->skill_manager->has_skill($skill_name);
460 }
461  
462 # ---------- Web / callback setters ----------
463  
464 3 ▶sub set_dynamic_config_callback {
Subroutine set_dynamic_config_callback: called CC=1 SCAR=0.0
Pod undocumented
465 3 my ($self, $cb) = @_;
466 3 $self->dynamic_config_callback($cb);
467 3 return $self;
468 }
469  
470 3 ▶sub set_web_hook_url {
Subroutine set_web_hook_url: called CC=1 SCAR=0.0
Pod undocumented
471 3 my ($self, $url) = @_;
472 3 $self->webhook_url($url);
473 3 return $self;
474 }
475  
476 2 ▶sub set_post_prompt_url {
Subroutine set_post_prompt_url: called CC=1 SCAR=0.0
Pod undocumented
477 2 my ($self, $url) = @_;
478 2 $self->post_prompt_url($url);
479 2 return $self;
480 }
481  
482 2 ▶sub manual_set_proxy_url {
Subroutine manual_set_proxy_url: called CC=1 SCAR=0.0
Pod undocumented
483 2 my ($self, $url) = @_;
484 2 $self->proxy_url_base($url);
485 2 return $self;
486 }
487  
488 6 ▶sub add_swaig_query_params {
Subroutine add_swaig_query_params: called CC=1 SCAR=0.0
Pod undocumented
489 6 my ($self, %params) = @_;
490 6 $self->swaig_query_params({ %{ $self->swaig_query_params }, %params });
491 6 return $self;
492 }
493  
494 2 ▶sub clear_swaig_query_params {
Subroutine clear_swaig_query_params: called CC=1 SCAR=0.0
Pod undocumented
495 2 my ($self) = @_;
496 2 $self->swaig_query_params({});
497 2 return $self;
498 }
499  
500 2 ▶sub on_summary {
Subroutine on_summary: called CC=1 SCAR=0.0
Pod undocumented
501 2 my ($self, $cb) = @_;
502 2 $self->summary_callback($cb);
503 2 return $self;
504 }
505  
506 1 ▶sub on_debug_event {
Subroutine on_debug_event: called CC=1 SCAR=0.0
Pod undocumented
507 1 my ($self, $cb) = @_;
508 1 $self->debug_event_handler($cb);
509 1 return $self;
510 }
511  
512 # ---------- URL construction ----------
513  
514 70 ▶sub _build_webhook_url {
Subroutine _build_webhook_url: called CC=6 SCAR=17.9
515 70 my ($self, $request_env) = @_;
516 # If explicit override set, use it
517 70 ▶ return $self->webhook_url if defined $self->webhook_url;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->webhook_url 1 69 70
518  
519 69 my $base = $self->_detect_proxy_url($request_env);
520 69 ▶ my $route = $self->route eq '/' ? '' : $self->route;
Branch: 1 branch
BranchTrueFalseTotal
$self->route eq '/' ? : 58 11 69
521 69 my $url = $base . $route . '/swaig';
522  
523 # Append query params
524 69 ▶ if (%{ $self->swaig_query_params }) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$self->swaig_query_params;}) 1 68 69
525 1 my @parts;
526 1 for my $k (sort keys %{ $self->swaig_query_params }) {
527 2 ▶ push @parts, "$k=" . ($self->swaig_query_params->{$k} // '');
Condition50%1/2
$self->swaig_query_params->{$k} // ""
l!l
20
MC/DC0%0/1
$self->swaig_query_params->{$k} // ""
$self->swaig_query_params->{$k}
Truth table50%1/2
A
A= $self->swaig_query_params->{$k}
Aresult
11
00
528 }
529 1 $url .= '?' . join('&', @parts);
530 }
531  
532 69 return $url;
533 }
534  
535 70 ▶sub _build_post_prompt_url {
Subroutine _build_post_prompt_url: called CC=3 SCAR=11.0
536 70 my ($self, $request_env) = @_;
537 70 ▶ return $self->post_prompt_url if defined $self->post_prompt_url;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->post_prompt_url 0 70 70
538 70 my $base = $self->_detect_proxy_url($request_env);
539 70 ▶ my $route = $self->route eq '/' ? '' : $self->route;
Branch: 1 branch
BranchTrueFalseTotal
$self->route eq '/' ? : 59 11 70
540 70 return $base . $route . '/post_prompt';
541 }
542  
543 142 ▶sub _detect_proxy_url {
Subroutine _detect_proxy_url: called CC=10 SCAR=24.4
544 142 my ($self, $env) = @_;
545  
546 142 ▶ return $self->proxy_url_base if defined $self->proxy_url_base;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->proxy_url_base 0 142 142
547  
548 142 ▶ $env //= {};
Condition50%1/2
$env //= {}
l!l
1420
MC/DC0%0/1
$env //= {}
$env
Truth table50%1/2
A
A= $env
Aresult
11
00
549  
550 # Check X-Forwarded headers
551 142 my $proto = $env->{HTTP_X_FORWARDED_PROTO};
552 142 my $fhost = $env->{HTTP_X_FORWARDED_HOST};
553 142 ▶ if ($proto && $fhost) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto and $fhost) 1 141 142
Condition66%2/3
$proto and $fhost
!ll&&!rl&&r
14101
MC/DC50%1/2
$proto and $fhost
$proto $fhost
Truth table66%2/3
A and B
A= $protoB= $fhost
ABresult
0X0
100
111
554 1 return "${proto}://${fhost}";
555 }
556  
557 # Check X-Original-URL
558 141 my $orig = $env->{HTTP_X_ORIGINAL_URL};
559 141 ▶ return $orig if $orig;
Branch: 1 branch
BranchTrueFalseTotal
if $orig 1 140 141
560  
561 # Fallback to server config
562 140 ▶ my $scheme = ($env->{HTTPS} || $env->{'psgi.url_scheme'} || 'http');
Condition40%2/5
$env->{'HTTPS'} || $env->{'psgi.url_scheme'} || 'http'
l!l
0140
$env->{"HTTPS"} or $env->{"psgi.url_scheme"}
l!l&&r!l&&!r
00140
MC/DC0%0/2
$env->{'HTTPS'} || $env->{'psgi.url_scheme'} || 'http'
$env->{"HTTPS"} $env->{"psgi.url_scheme"}
Truth table33%1/3
A or B
A= $env->{"HTTPS"}B= $env->{"psgi.url_scheme"}
ABresult
1X1
011
000
563 140 ▶ $scheme = 'https' if $scheme eq 'on';
Branch: 1 branch
BranchTrueFalseTotal
if $scheme eq "on" 0 140 140
564 140 ▶ my $host = $env->{HTTP_HOST} || $self->host . ':' . $self->port;
Condition50%1/2
$env->{'HTTP_HOST'} || $self->host . ':' . $self->port
l!l
0140
MC/DC0%0/1
$env->{'HTTP_HOST'} || $self->host . ':' . $self->port
$env->{'HTTP_HOST'}
Truth table50%1/2
A
A= $env->{'HTTP_HOST'}
Aresult
11
00
565 140 return "${scheme}://${host}";
566 }
567  
568 4 ▶sub get_full_url {
Subroutine get_full_url: called CC=4 SCAR=14.0
Pod undocumented
569 4 my ($self, %opts) = @_;
570 4 ▶ my $base = $self->proxy_url_base // ('http://' . $self->host . ':' . $self->port);
Condition50%1/2
$self->proxy_url_base // 'http://' . $self->host . ':' . $self->port
l!l
04
MC/DC0%0/1
$self->proxy_url_base // 'http://' . $self->host . ':' . $self->port
$self->proxy_url_base
Truth table50%1/2
A
A= $self->proxy_url_base
Aresult
11
00
571 4 ▶ my $route = $self->route eq '/' ? '' : $self->route;
Branch: 1 branch
BranchTrueFalseTotal
$self->route eq '/' ? : 0 4 4
572 4 my $url = $base . $route;
573 4 ▶ if ($opts{include_auth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"include_auth"}) 2 2 4
574 2 my $user = $self->basic_auth_user;
575 2 my $pass = $self->basic_auth_password;
576 2 $url =~ s{^(https?://)}{$1${user}:${pass}\@};
577 }
578 4 return $url;
579 }
580  
581 # ---------- render_swml (5-phase pipeline) ----------
582  
583 70 ▶sub render_swml {
Subroutine render_swml: called CC=8 SCAR=20.8
Pod undocumented
584 70 my ($self, $request_env) = @_;
585 70 ▶ $request_env //= {};
Condition100%2/2
$request_env //= {}
l!l
763
MC/DC100%1/1
$request_env //= {}
$request_env
Truth table100%2/2
A
A= $request_env
Aresult
11
00
586  
587 70 my $webhook_url = $self->_build_webhook_url($request_env);
588 70 my $post_prompt_url = $self->_build_post_prompt_url($request_env);
589  
590 # Embed auth credentials in webhook URL
591 70 my $auth_user = $self->basic_auth_user;
592 70 my $auth_pass = $self->basic_auth_password;
593 70 ▶ $webhook_url =~ s{^(https?://)}{$1${auth_user}:${auth_pass}\@}
Branch: 1 branch
BranchTrueFalseTotal
unless $webhook_url =~ /\@/ 70 0 70
594 unless $webhook_url =~ /\@/;
595 70 ▶ $post_prompt_url =~ s{^(https?://)}{$1${auth_user}:${auth_pass}\@}
Branch: 1 branch
BranchTrueFalseTotal
unless $post_prompt_url =~ /\@/ 70 0 70
596 unless $post_prompt_url =~ /\@/;
597  
598 70 my @main_section;
599  
600 # Phase 1: Pre-answer verbs
601 70 push @main_section, @{ $self->pre_answer_verbs };
602  
603 # Phase 2: Answer verb
604 70 ▶ if ($self->auto_answer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_answer) 69 1 70
605 69 my %answer_params = (max_duration => 14400);
606 69 ▶ %answer_params = (%answer_params, %{ $self->answer_config }) if %{ $self->answer_config };
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->answer_config;} 1 68 69
607 69 push @main_section, { answer => \%answer_params };
608 }
609  
610 # Record call if enabled
611 70 ▶ if ($self->record_call) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->record_call) 4 66 70
612 4 ▶ push @main_section, { record_call => {
Branch: 1 branch
BranchTrueFalseTotal
$self->record_stereo ? : 3 1 4
613 format => $self->record_format,
614 stereo => $self->record_stereo ? JSON::true : JSON::false,
615 }};
616 }
617  
618 # Phase 3: Post-answer verbs
619 70 push @main_section, @{ $self->post_answer_verbs };
620  
621 # Phase 4: AI verb
622 70 my $ai = $self->_build_ai_verb($webhook_url, $post_prompt_url);
623 70 push @main_section, { ai => $ai };
624  
625 # Phase 5: Post-AI verbs
626 70 push @main_section, @{ $self->post_ai_verbs };
627  
628 70 my $doc = {
629 version => '1.0.0',
630 sections => { main => \@main_section },
631 };
632  
633 70 return $doc;
634 }
635  
636 70 ▶sub _build_ai_verb {
Subroutine _build_ai_verb: called CC=29 SCAR=33.8
637 70 my ($self, $webhook_url, $post_prompt_url) = @_;
638  
639 70 my %ai;
640  
641 # Prompt
642 70 my $prompt = $self->get_prompt;
643 70 ▶ if (ref $prompt eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $prompt eq 'ARRAY') { } 22 48 70
644 # POM mode
645 22 $ai{prompt} = { pom => $prompt };
646 } else {
647 48 ▶ $ai{prompt} = { text => $prompt } if $prompt;
Branch: 1 branch
BranchTrueFalseTotal
if $prompt 6 42 48
648 }
649  
650 # Merge prompt LLM params
651 70 ▶ if (%{ $self->prompt_llm_params }) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$self->prompt_llm_params;}) 3 67 70
652 3 ▶ $ai{prompt} //= {};
Condition50%1/2
$ai{"prompt"} //= {}
l!l
30
MC/DC0%0/1
$ai{"prompt"} //= {}
$ai{"prompt"}
Truth table50%1/2
A
A= $ai{"prompt"}
Aresult
11
00
653 3 for my $k (keys %{ $self->prompt_llm_params }) {
654 5 $ai{prompt}{$k} = $self->prompt_llm_params->{$k};
655 }
656 }
657  
658 # Post prompt
659 70 ▶ if ($self->post_prompt && $self->post_prompt ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->post_prompt and $self->post_prompt ne "") 4 66 70
Condition66%2/3
$self->post_prompt and $self->post_prompt ne ""
!ll&&!rl&&r
6604
MC/DC50%1/2
$self->post_prompt and $self->post_prompt ne ""
$self->post_prompt $self->post_prompt ne ""
Truth table66%2/3
A and B
A= $self->post_promptB= $self->post_prompt ne ""
ABresult
0X0
100
111
660 4 $ai{post_prompt} = { text => $self->post_prompt };
661 4 ▶ if (%{ $self->post_prompt_llm_params }) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$self->post_prompt_llm_params;}) 1 3 4
662 1 for my $k (keys %{ $self->post_prompt_llm_params }) {
663 1 $ai{post_prompt}{$k} = $self->post_prompt_llm_params->{$k};
664 }
665 }
666 }
667  
668 70 ▶ $ai{post_prompt_url} = $post_prompt_url if $post_prompt_url;
Branch: 1 branch
BranchTrueFalseTotal
if $post_prompt_url 70 0 70
669  
670 # Params
671 70 ▶ $ai{params} = { %{ $self->params } } if %{ $self->params };
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->params;} 3 67 70
672  
673 # Hints
674 70 my @all_hints = @{ $self->hints };
675 70 push @all_hints, @{ $self->pattern_hints };
676 70 ▶ $ai{hints} = \@all_hints if @all_hints;
Branch: 1 branch
BranchTrueFalseTotal
if @all_hints 6 64 70
677  
678 # Languages
679 70 ▶ $ai{languages} = $self->languages if @{ $self->languages };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->languages;} 1 69 70
680  
681 # Pronunciations
682 70 ▶ $ai{pronounce} = $self->pronunciations if @{ $self->pronunciations };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->pronunciations;} 1 69 70
683  
684 # SWAIG
685 70 my $swaig = {};
686  
687 # Build function list
688 70 my @functions;
689 70 for my $fname (@{ $self->tool_order }) {
690 20 my $tool = $self->tools->{$fname};
691 20 ▶ next unless $tool;
Branch: 1 branch
BranchTrueFalseTotal
unless $tool 0 20 20
692 20 my %func = %$tool;
693 20 delete $func{_handler}; # Don't include handler in SWML
694 20 ▶ $func{web_hook_url} //= $webhook_url;
Condition33%1/3
$func{"web_hook_url"} //= $webhook_url
l!l&&r!l&&!r
0200
MC/DC0%0/2
$func{"web_hook_url"} //= $webhook_url
$func{"web_hook_url"} $webhook_url
Truth table33%1/3
A //= B
A= $func{"web_hook_url"}B= $webhook_url
ABresult
1X1
011
000
695 20 push @functions, \%func;
696 }
697 70 ▶ $swaig->{functions} = \@functions if @functions;
Branch: 1 branch
BranchTrueFalseTotal
if @functions 17 53 70
698  
699 # Native functions
700 $swaig->{native_functions} = $self->native_functions
701 70 ▶ if @{ $self->native_functions };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->native_functions;} 2 68 70
702  
703 # Includes
704 $swaig->{includes} = $self->function_includes
705 70 ▶ if @{ $self->function_includes };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->function_includes;} 2 68 70
706  
707 70 ▶ $ai{SWAIG} = $swaig if %$swaig;
Branch: 1 branch
BranchTrueFalseTotal
if %$swaig 21 49 70
708  
709 # Global data
710 10 $ai{global_data} = { %{ $self->global_data } }
711 70 ▶ if %{ $self->global_data };
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->global_data;} 10 60 70
712  
713 # Internal fillers
714 70 ▶ if (defined $self->internal_fillers) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->internal_fillers) 2 68 70
715 2 ▶ $ai{params} //= {};
Condition50%1/2
$ai{"params"} //= {}
l!l
02
MC/DC0%0/1
$ai{"params"} //= {}
$ai{"params"}
Truth table50%1/2
A
A= $ai{"params"}
Aresult
11
00
716 2 $ai{params}{internal_fillers} = $self->internal_fillers;
717 }
718  
719 # Debug events
720 70 ▶ if ($self->debug_events_level > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug_events_level > 0) 2 68 70
721 2 ▶ $ai{params} //= {};
Condition50%1/2
$ai{"params"} //= {}
l!l
02
MC/DC0%0/1
$ai{"params"} //= {}
$ai{"params"}
Truth table50%1/2
A
A= $ai{"params"}
Aresult
11
00
722 2 $ai{params}{debug_events} = $self->debug_events_level;
723 }
724  
725 # Contexts
726 70 ▶ if ($self->context_builder && $self->context_builder->has_contexts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->context_builder and $self->context_builder->has_contexts) 1 69 70
Condition66%2/3
$self->context_builder and $self->context_builder->has_contexts
!ll&&!rl&&r
0691
MC/DC50%1/2
$self->context_builder and $self->context_builder->has_contexts
$self->context_builder $self->context_builder->has_contexts
Truth table66%2/3
A and B
A= $self->context_builderB= $self->context_builder->has_contexts
ABresult
0X0
100
111
727 1 $ai{context_switch} = $self->context_builder->to_hashref;
728 }
729  
730 70 return \%ai;
731 }
732  
733 # ---------- PSGI / Plack ----------
734  
735 22 ▶sub psgi_app {
Subroutine psgi_app: called CC=1 SCAR=0.0
Pod undocumented
736 22 my ($self) = @_;
737 22 return $self->_build_psgi_app;
738 }
739  
740 22 ▶sub _build_psgi_app {
Subroutine _build_psgi_app: called CC=2 SCAR=7.1
741 22 my ($self) = @_;
742 22 require Plack::Request;
743  
744 22 my $route = $self->route;
745 22 ▶ $route = '' if $route eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $route eq "/" 13 9 22
746  
747 22 my $agent = $self;
748  
749 # Build the core app as a plain PSGI sub
750 20 ▶ my $core_app = sub {
Subroutine __ANON__: called CC=20 SCAR=33.8
751 20 my $env = shift;
752 20 my $req = Plack::Request->new($env);
753 20 my $path = $req->path_info;
754  
755 # Normalize path
756 20 ▶ $path =~ s{/+$}{} unless $path eq '/';
Branch: 1 branch
BranchTrueFalseTotal
unless $path eq "/" 10 10 20
757  
758 # Health/ready endpoints (no auth)
759 20 ▶ if ($path eq '/health') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "/health") 4 16 20
760 4 return [200, ['Content-Type' => 'application/json'],
761 [encode_json({ status => 'healthy', agent => $agent->name })]];
762 }
763 16 ▶ if ($path eq '/ready') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "/ready") 2 14 16
764 2 return [200, ['Content-Type' => 'application/json'],
765 [encode_json({ status => 'ready' })]];
766 }
767  
768 # Auth check for protected routes
769 14 ▶ my $expected_route = $route eq '' ? '/' : $route;
Branch: 1 branch
BranchTrueFalseTotal
$route eq '' ? : 11 3 14
770 14 my $is_swaig = ($path eq "$route/swaig");
771 14 my $is_post_prompt = ($path eq "$route/post_prompt");
772 14 ▶ my $is_main = ($path eq $expected_route || ($route ne '' && $path eq "$route/"));
Condition50%3/6
$path eq $expected_route || $route ne '' && $path eq "$route/"
l!l&&r!l&&!r
1301
$route ne "" and $path eq "$route/"
!ll&&!rl&&r
100
MC/DC33%1/3
$path eq $expected_route || $route ne '' && $path eq "$route/"
$path eq $expected_route $route ne "" $path eq "$route/"
Truth table50%2/4
A || B and C
A= $path eq $expected_routeB= $route ne ""C= $path eq "$route/"
ABCresult
1XX1
0111
00X0
0100
773  
774 # Root agent: treat '/' as main
775 14 ▶ if ($route eq '' && $path eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($route eq "" and $path eq "/") 10 4 14
Condition100%3/3
$route eq "" and $path eq "/"
!ll&&!rl&&r
3110
MC/DC100%2/2
$route eq "" and $path eq "/"
$route eq "" $path eq "/"
Truth table100%3/3
A and B
A= $route eq ""B= $path eq "/"
ABresult
0X0
100
111
776 10 $is_main = 1;
777 }
778  
779 14 ▶ if ($is_main || $is_swaig || $is_post_prompt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_main or $is_swaig or $is_post_prompt) 14 0 14
Condition50%3/6
$is_main or $is_swaig or $is_post_prompt
l!l&&r!l&&!r
1400
$is_main or $is_swaig
l!l&&r!l&&!r
1310
MC/DC0%0/3
$is_main or $is_swaig or $is_post_prompt
$is_main $is_swaig $is_post_prompt
Truth table50%2/4
A or B or C
A= $is_mainB= $is_swaigC= $is_post_prompt
ABCresult
1XX1
01X1
0011
0000
780 14 my $auth_ok = $agent->_check_auth($env);
781 14 ▶ unless ($auth_ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($auth_ok) 6 8 14
782 6 return [401,
783 ['Content-Type' => 'text/plain', 'WWW-Authenticate' => 'Basic realm="SignalWire Agent"'],
784 ['Unauthorized']];
785 }
786 }
787  
788 # Route dispatch
789 8 ▶ if ($is_main && ($req->method eq 'GET' || $req->method eq 'POST')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($is_main and $req->method eq 'GET' || $req->method eq 'POST') { } 7 1 8
elsif ($is_swaig and $req->method eq 'POST') { } 1 0 1
elsif ($is_post_prompt and $req->method eq 'POST') { } 0 0 0
Condition50%3/6
$is_main and $req->method eq "GET" || $req->method eq "POST"
!ll&&!rl&&r
107
$req->method eq "GET" or $req->method eq "POST"
l!l&&r!l&&!r
700
MC/DC33%1/3
$is_main and $req->method eq "GET" || $req->method eq "POST"
$is_main $req->method eq "GET" $req->method eq "POST"
Truth table50%2/4
A and B or C
A= $is_mainB= $req->method eq "GET"C= $req->method eq "POST"
ABCresult
0XX0
1000
11X1
1011
790 7 return $agent->_handle_swml($env, $req);
791 }
792 ▶ elsif ($is_swaig && $req->method eq 'POST') {
Condition33%1/3
$is_swaig and $req->method eq "POST"
!ll&&!rl&&r
001
MC/DC0%0/2
$is_swaig and $req->method eq "POST"
$is_swaig $req->method eq "POST"
Truth table33%1/3
A and B
A= $is_swaigB= $req->method eq "POST"
ABresult
0X0
100
111
793 1 return $agent->_handle_swaig($env, $req);
794 }
795 ▶ elsif ($is_post_prompt && $req->method eq 'POST') {
Condition0%0/3
$is_post_prompt and $req->method eq "POST"
!ll&&!rl&&r
000
MC/DC0%0/2
$is_post_prompt and $req->method eq "POST"
$is_post_prompt $req->method eq "POST"
Truth table0%0/3
A and B
A= $is_post_promptB= $req->method eq "POST"
ABresult
0X0
100
111
796 0 return $agent->_handle_post_prompt($env, $req);
797 }
798  
799 0 return [404, ['Content-Type' => 'text/plain'], ['Not Found']];
800 22 };
801  
802 # Maximum request body size: 1MB
803 22 my $max_body_size = 1_048_576;
804  
805 # Wrap with body size limit and security headers middleware
806 20 ▶ my $app_with_middleware = sub {
Subroutine __ANON__: called CC=7 SCAR=19.7
807 20 my $env = shift;
808  
809 # Enforce body size limit by actually reading the body
810 20 ▶ if ($env->{REQUEST_METHOD} eq 'POST' || $env->{REQUEST_METHOD} eq 'PUT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"REQUEST_METHOD"} eq "POST" or $env->{"REQUEST_METHOD"} eq "PUT") 1 19 20
Condition66%2/3
$env->{"REQUEST_METHOD"} eq "POST" or $env->{"REQUEST_METHOD"} eq "PUT"
l!l&&r!l&&!r
1019
MC/DC50%1/2
$env->{"REQUEST_METHOD"} eq "POST" or $env->{"REQUEST_METHOD"} eq "PUT"
$env->{"REQUEST_METHOD"} eq "POST" $env->{"REQUEST_METHOD"} eq "PUT"
Truth table66%2/3
A or B
A= $env->{"REQUEST_METHOD"} eq "POST"B= $env->{"REQUEST_METHOD"} eq "PUT"
ABresult
1X1
011
000
811 1 my $input = $env->{'psgi.input'};
812 1 ▶ if ($input) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input) 1 0 1
813 1 my $body = '';
814 1 my $total = 0;
815 1 my $buf;
816 1 ▶ while (my $read = $input->read($buf, 8192)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $read = $input->read($buf, 8192)) 1 1 2
817 1 $total += $read;
818 1 ▶ if ($total > $max_body_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total > $max_body_size) 0 1 1
819 0 return [413, ['Content-Type' => 'application/json',
820 'X-Content-Type-Options' => 'nosniff',
821 'X-Frame-Options' => 'DENY',
822 'Cache-Control' => 'no-store'],
823 [encode_json({ error => 'Request body too large' })]];
824 }
825 1 $body .= $buf;
826 }
827 # Replace psgi.input with the buffered content so handlers can re-read
828 1 open my $new_input, '<', \$body;
829 1 $env->{'psgi.input'} = $new_input;
830 1 $env->{CONTENT_LENGTH} = length($body);
831 }
832 }
833  
834 20 my $res = $core_app->($env);
835 20 ▶ if (ref $res eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res eq "ARRAY") 20 0 20
836 20 push @{ $res->[1] },
837 'X-Content-Type-Options' => 'nosniff',
838 'X-Frame-Options' => 'DENY',
839 'Cache-Control' => 'no-store';
840 }
841 20 return $res;
842 22 };
843  
844 22 return $app_with_middleware;
845 }
846  
847 14 ▶sub _check_auth {
Subroutine _check_auth: called CC=8 SCAR=20.8
848 14 my ($self, $env) = @_;
849 14 ▶ my $auth_header = $env->{HTTP_AUTHORIZATION} // '';
Condition100%2/2
$env->{"HTTP_AUTHORIZATION"} // ""
l!l
122
MC/DC100%1/1
$env->{"HTTP_AUTHORIZATION"} // ""
$env->{"HTTP_AUTHORIZATION"}
Truth table100%2/2
A
A= $env->{"HTTP_AUTHORIZATION"}
Aresult
11
00
850 14 ▶ return 0 unless $auth_header =~ /^Basic\s+(.+)$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $auth_header =~ /^Basic\s+(.+)$/i 3 11 14
851 11 ▶ my $decoded = eval { decode_base64($1) } // '';
Condition50%1/2
eval { do { &decode_base64($1) } } // ""
l!l
110
MC/DC0%0/1
eval { do { &decode_base64($1) } } // ""
eval { do { &decode_base64($1) } }
Truth table50%1/2
A
A= eval { do { &decode_base64($1) } }
Aresult
11
00
852 11 my ($user, $pass) = split(/:/, $decoded, 2);
853 11 ▶ return 0 unless defined $user && defined $pass;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user and defined $pass 1 10 11
Condition66%2/3
defined $user and defined $pass
!ll&&!rl&&r
0110
MC/DC50%1/2
defined $user and defined $pass
defined $user defined $pass
Truth table66%2/3
A and B
A= defined $userB= defined $pass
ABresult
0X0
100
111
854  
855 # Timing-safe comparison using HMAC (constant-time, no length leak)
856 10 my $expected_user = $self->basic_auth_user;
857 10 my $expected_pass = $self->basic_auth_password;
858  
859 10 my $user_ok = _timing_safe_eq($user, $expected_user);
860 10 my $pass_ok = _timing_safe_eq($pass, $expected_pass);
861  
862 10 ▶ return ($user_ok && $pass_ok) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$user_ok && $pass_ok ? : 8 2 10
Condition100%3/3
$user_ok and $pass_ok
!ll&&!rl&&r
118
MC/DC100%2/2
$user_ok and $pass_ok
$user_ok $pass_ok
Truth table100%3/3
A and B
A= $user_okB= $pass_ok
ABresult
0X0
100
111
863 }
864  
865 27 ▶sub _timing_safe_eq {
Subroutine _timing_safe_eq: called CC=1 SCAR=0.0
866 27 my ($a, $b) = @_;
867 # HMAC-based constant-time comparison: no length leak
868 27 my $key = 'signalwire-timing-safe-comparison';
869 27 my $hmac_a = hmac_sha256_hex($a, $key);
870 27 my $hmac_b = hmac_sha256_hex($b, $key);
871 27 return $hmac_a eq $hmac_b;
872 }
873  
874 7 ▶sub _handle_swml {
Subroutine _handle_swml: called CC=6 SCAR=18.3
875 7 my ($self, $env, $req) = @_;
876  
877 7 my $agent = $self;
878  
879 # If dynamic config callback is set, clone and apply
880 7 ▶ if ($self->dynamic_config_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dynamic_config_callback) 1 6 7
881 1 $agent = $self->_clone_for_request;
882 1 my $query_params = $req->query_parameters->as_hashref_mixed;
883 1 my $body_params = {};
884 1 ▶ if ($req->method eq 'POST' && $req->content_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->method eq "POST" and $req->content_length) 0 1 1
Condition33%1/3
$req->method eq "POST" and $req->content_length
!ll&&!rl&&r
100
MC/DC0%0/2
$req->method eq "POST" and $req->content_length
$req->method eq "POST" $req->content_length
Truth table33%1/3
A and B
A= $req->method eq "POST"B= $req->content_length
ABresult
0X0
100
111
885 0 eval { $body_params = decode_json($req->content) };
886 }
887 1 my $headers = {};
888 1 for my $k (keys %$env) {
889 10 ▶ if ($k =~ /^HTTP_(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^HTTP_(.+)/) 1 9 10
890 1 $headers->{lc($1)} = $env->{$k};
891 }
892 }
893 1 $self->dynamic_config_callback->($query_params, $body_params, $headers, $agent);
894 }
895  
896 7 my $swml = $agent->render_swml($env);
897 7 my $json = encode_json($swml);
898  
899 7 return [200, ['Content-Type' => 'application/json'], [$json]];
900 }
901  
902 1 ▶sub _handle_swaig {
Subroutine _handle_swaig: called CC=13 SCAR=33.1
903 1 my ($self, $env, $req) = @_;
904  
905 1 my $body = eval { decode_json($req->content) };
906 1 ▶ unless ($body && ref $body eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($body and ref $body eq "HASH") 0 1 1
Condition33%1/3
$body and ref $body eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
$body and ref $body eq "HASH"
$body ref $body eq "HASH"
Truth table33%1/3
A and B
A= $bodyB= ref $body eq "HASH"
ABresult
0X0
100
111
907 0 return [400, ['Content-Type' => 'application/json'],
908 [encode_json({ error => 'Invalid JSON' })]];
909 }
910  
911 1 my $func_name = $body->{function};
912 1 ▶ unless ($func_name && exists $self->tools->{$func_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($func_name and exists $self->tools->{$func_name}) 0 1 1
Condition33%1/3
$func_name and exists $self->tools->{$func_name}
!ll&&!rl&&r
001
MC/DC0%0/2
$func_name and exists $self->tools->{$func_name}
$func_name exists $self->tools->{$func_name}
Truth table33%1/3
A and B
A= $func_nameB= exists $self->tools->{$func_name}
ABresult
0X0
100
111
913 0 return [404, ['Content-Type' => 'application/json'],
914 [encode_json({ error => 'Function not found' })]];
915 }
916  
917 # Extract args
918 1 my $args = {};
919 1 ▶ if ($body->{argument} && ref $body->{argument}{parsed} eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
if ($body->{"argument"} and ref $body->{"argument"}{"parsed"} eq "ARRAY" and @{$body->{"argument"}{"parsed"};}) 1 0 1
Condition33%2/6
$body->{"argument"} and ref $body->{"argument"}{"parsed"} eq "ARRAY" and @{$body->{"argument"}{"parsed"};}
!ll&&!rl&&r
001
$body->{"argument"} and ref $body->{"argument"}{"parsed"} eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/3
$body->{"argument"} and ref $body->{"argument"}{"parsed"} eq "ARRAY" and @{$body->{"argument"}{"parsed"};}
$body->{"argument"} ref $body->{"argument"}{"parsed"} eq "ARRAY" @{$body->{"argument"}{"parsed"};}
Truth table25%1/4
A and B and C
A= $body->{"argument"}B= ref $body->{"argument"}{"parsed"} eq "ARRAY"C= @{$body->{"argument"}{"parsed"};}
ABCresult
0XX0
10X0
1100
1111
920 1 && @{ $body->{argument}{parsed} }) {
921 1 ▶ $args = $body->{argument}{parsed}[0] // {};
Condition50%1/2
$body->{"argument"}{"parsed"}[0] // {}
l!l
10
MC/DC0%0/1
$body->{"argument"}{"parsed"}[0] // {}
$body->{"argument"}{"parsed"}[0]
Truth table50%1/2
A
A= $body->{"argument"}{"parsed"}[0]
Aresult
11
00
922 }
923  
924 1 my $result = $self->on_function_call($func_name, $args, $body);
925 1 ▶ unless (defined $result) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 0 1 1
926 0 return [500, ['Content-Type' => 'application/json'],
927 [encode_json({ error => 'Handler returned no result' })]];
928 }
929  
930 # Serialize result
931 1 my $response;
932 1 ▶ if (blessed($result) && $result->can('to_hash')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($result) and $result->can('to_hash')) { } 0 1 1
elsif (ref $result eq 'HASH') { } 1 0 1
Condition33%1/3
builtin::blessed($result) and $result->can("to_hash")
!ll&&!rl&&r
100
MC/DC0%0/2
builtin::blessed($result) and $result->can("to_hash")
builtin::blessed($result) $result->can("to_hash")
Truth table33%1/3
A and B
A= builtin::blessed($result)B= $result->can("to_hash")
ABresult
0X0
100
111
933 0 $response = $result->to_hash;
934 } elsif (ref $result eq 'HASH') {
935 1 $response = $result;
936 } else {
937 0 $response = { response => "$result" };
938 }
939  
940 1 return [200, ['Content-Type' => 'application/json'], [encode_json($response)]];
941 }
942  
943 0 ▶sub _handle_post_prompt {
Subroutine _handle_post_prompt: not called CC=5 SCAR=34.0
944 0 my ($self, $env, $req) = @_;
945  
946 0 my $body = eval { decode_json($req->content) };
947 0 ▶ $body //= {};
Condition0%0/2
$body //= {}
l!l
00
MC/DC0%0/1
$body //= {}
$body
Truth table0%0/2
A
A= $body
Aresult
11
00
948  
949 0 ▶ if ($self->summary_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->summary_callback) 0 0 0
950 0 my $summary = undef;
951 0 ▶ if ($body->{post_prompt_data}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body->{"post_prompt_data"}) 0 0 0
952 $summary = $body->{post_prompt_data}{parsed}
953 0 ▶ // $body->{post_prompt_data}{raw};
Condition0%0/3
$body->{"post_prompt_data"}{"parsed"} // $body->{"post_prompt_data"}{"raw"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$body->{"post_prompt_data"}{"parsed"} // $body->{"post_prompt_data"}{"raw"}
$body->{"post_prompt_data"}{"parsed"} $body->{"post_prompt_data"}{"raw"}
Truth table0%0/3
A // B
A= $body->{"post_prompt_data"}{"parsed"}B= $body->{"post_prompt_data"}{"raw"}
ABresult
1X1
011
000
954 }
955 0 $self->summary_callback->($summary, $body);
956 }
957  
958 0 return [200, ['Content-Type' => 'application/json'],
959 [encode_json({ status => 'ok' })]];
960 }
961  
962 # ---------- Clone for dynamic config ----------
963  
964 3 ▶sub _clone_for_request {
Subroutine _clone_for_request: called CC=3 SCAR=11.0
965 3 my ($self) = @_;
966 3 my %init;
967 3 for my $attr (qw(name route host port auto_answer record_call record_format
968 record_stereo prompt_text post_prompt use_pom
969 debug_events_level)) {
970 36 $init{$attr} = $self->$attr;
971 }
972 # Deep copy complex attributes
973 3 $init{pom_sections} = dclone($self->pom_sections);
974 3 $init{tools} = dclone($self->tools);
975 3 $init{tool_order} = [ @{ $self->tool_order } ];
976 3 $init{hints} = [ @{ $self->hints } ];
977 3 $init{pattern_hints} = [ @{ $self->pattern_hints } ];
978 3 $init{languages} = dclone($self->languages);
979 3 $init{pronunciations} = dclone($self->pronunciations);
980 3 $init{params} = { %{ $self->params } };
981 3 $init{global_data} = dclone($self->global_data);
982 3 $init{native_functions} = [ @{ $self->native_functions } ];
983 3 $init{function_includes} = dclone($self->function_includes);
984 3 $init{prompt_llm_params} = { %{ $self->prompt_llm_params } };
985 3 $init{post_prompt_llm_params} = { %{ $self->post_prompt_llm_params } };
986 3 $init{pre_answer_verbs} = dclone($self->pre_answer_verbs);
987 3 $init{post_answer_verbs} = dclone($self->post_answer_verbs);
988 3 $init{post_ai_verbs} = dclone($self->post_ai_verbs);
989 3 $init{answer_config} = { %{ $self->answer_config } };
990 3 $init{swaig_query_params} = { %{ $self->swaig_query_params } };
991 3 $init{basic_auth_user} = $self->basic_auth_user;
992 3 $init{basic_auth_password} = $self->basic_auth_password;
993 3 $init{webhook_url} = $self->webhook_url;
994 3 $init{post_prompt_url} = $self->post_prompt_url;
995 3 $init{proxy_url_base} = $self->proxy_url_base;
996 3 ▶ $init{internal_fillers} = defined $self->internal_fillers
Branch: 1 branch
BranchTrueFalseTotal
defined $self->internal_fillers ? : 0 3 3
997 ? dclone($self->internal_fillers) : undef;
998 3 $init{session_manager} = $self->session_manager;
999  
1000 3 my $clone = (ref $self)->new(%init);
1001 3 return $clone;
1002 }
1003  
1004 # ---------- run / serve ----------
1005  
1006 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
1007 0 my ($self, %opts) = @_;
1008 0 $self->serve(%opts);
1009 }
1010  
1011 0 ▶sub serve {
Subroutine serve: not called CC=3 SCAR=24.8
Pod undocumented
1012 0 my ($self, %opts) = @_;
1013 0 my $app = $self->psgi_app;
1014 0 ▶ my $host = $opts{host} // $self->host;
Condition0%0/3
$opts{"host"} // $self->host
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"host"} // $self->host
$opts{"host"} $self->host
Truth table0%0/3
A // B
A= $opts{"host"}B= $self->host
ABresult
1X1
011
000
1015 0 ▶ my $port = $opts{port} // $self->port;
Condition0%0/3
$opts{"port"} // $self->port
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"port"} // $self->port
$opts{"port"} $self->port
Truth table0%0/3
A // B
A= $opts{"port"}B= $self->port
ABresult
1X1
011
000
1016  
1017 0 require Plack::Runner;
1018 0 my $runner = Plack::Runner->new;
1019 0 $runner->parse_options(
1020 '--host' => $host,
1021 '--port' => $port,
1022 '--server' => 'HTTP::Server::PSGI',
1023 );
1024 0 $runner->run($app);
1025 }
1026  
1027 # ---------- helpers ----------
1028  
1029 65 ▶sub _generate_random_password {
Subroutine _generate_random_password: called CC=4 SCAR=15.5
1030 # Use /dev/urandom for cryptographically secure random bytes.
1031 # Die on failure rather than falling back to a weak password.
1032 65 my $bytes = '';
1033 65 ▶ if (open my $fh, '<:raw', '/dev/urandom') {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<:raw", "/dev/urandom") 65 0 65
1034 65 my $read = read($fh, $bytes, 32);
1035 65 close $fh;
1036 65 ▶ if (defined $read && $read == 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $read and $read == 32) 65 0 65
Condition33%1/3
defined $read and $read == 32
!ll&&!rl&&r
0065
MC/DC0%0/2
defined $read and $read == 32
defined $read $read == 32
Truth table33%1/3
A and B
A= defined $readB= $read == 32
ABresult
0X0
100
111
1037 # Convert to hex string (64 chars)
1038 65 return unpack('H*', $bytes);
1039 }
1040 }
1041 0 die "FATAL: Cannot generate secure random password - /dev/urandom unavailable or read failed. "
1042 . "Set SWML_BASIC_AUTH_PASSWORD environment variable instead.\n";
1043 }
1044  
1045 0 ▶sub extract_sip_username {
Subroutine extract_sip_username: not called CC=8 SCAR=42.8
Pod undocumented
1046 0 my ($class_or_self, $body) = @_;
1047 # Extract SIP username from a request body (hashref).
1048 # Looks in standard SignalWire fields for the SIP caller identity.
1049 0 ▶ return undef unless ref $body eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 0 0 0
1050  
1051 # Check call.from field (e.g., "sip:user@domain")
1052 my $from = $body->{call}{from}
1053 // $body->{sip_from}
1054 // $body->{from}
1055 0 ▶ // '';
Condition0%0/8
(($body->{"call"}{"from"} // $body->{"sip_from"}) // $body->{"from"}) // ""
l!l
00
($body->{"call"}{"from"} // $body->{"sip_from"}) // $body->{"from"}
l!l&&r!l&&!r
000
$body->{"call"}{"from"} // $body->{"sip_from"}
l!l&&r!l&&!r
000
MC/DC0%0/3
(($body->{"call"}{"from"} // $body->{"sip_from"}) // $body->{"from"}) // ""
$body->{"call"}{"from"} $body->{"sip_from"} $body->{"from"}
Truth table0%0/4
A // B // C
A= $body->{"call"}{"from"}B= $body->{"sip_from"}C= $body->{"from"}
ABCresult
1XX1
01X1
0011
0000
1056  
1057 0 ▶ if ($from =~ m{^sip:([^@]+)\@}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from =~ /^sip:([^\@]+)\@/i) 0 0 0
1058 0 return $1;
1059 }
1060  
1061 # Check for a direct caller_id_number
1062 0 ▶ if (my $cid = $body->{call}{caller_id_number} // $body->{caller_id_number}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cid = $body->{"call"}{"caller_id_number"} // $body->{"caller_id_number"}) 0 0 0
Condition0%0/3
$body->{"call"}{"caller_id_number"} // $body->{"caller_id_number"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$body->{"call"}{"caller_id_number"} // $body->{"caller_id_number"}
$body->{"call"}{"caller_id_number"} $body->{"caller_id_number"}
Truth table0%0/3
A // B
A= $body->{"call"}{"caller_id_number"}B= $body->{"caller_id_number"}
ABresult
1X1
011
000
1063 0 return $cid;
1064 }
1065  
1066 0 return undef;
1067 }
1068  
1069 35 1;
« blib/lib/SignalWire/Agents/Skills/SkillManager.pm ↑ Summary blib/lib/SignalWire/Agents/Contexts.pm »