| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2025 SignalWire | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # Licensed under the MIT License. | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # swaig-test - CLI tool for testing SWAIG agent endpoints | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # Usage: | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # swaig-test --url http://user:pass@host:port/route --dump-swml | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # swaig-test --url http://user:pass@host:port/route --list-tools | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # swaig-test --url http://user:pass@host:port/route --exec tool_name --param key=value | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # File mode (no HTTP — load a Perl example/script in-process and read | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # the runtime SWAIG tool registry): | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # swaig-test --file examples/swmlservice_swaig_standalone.pl --list-tools | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 10 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 10 | ▶ | use JSON (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 10 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 10 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 10 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 10 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 10 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 10 | my $VERSION = '1.0.0'; | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | # --- Parse CLI options --- | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 10 | my ($url, $file, $dump_swml, $list_tools, $exec_name, @params, $raw, $verbose, $help); | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 10 | ▶ | GetOptions( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 'url=s' => \$url, | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 'file=s' => \$file, | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 'dump-swml' => \$dump_swml, | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 'list-tools' => \$list_tools, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 'exec=s' => \$exec_name, | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 'param=s@' => \@params, | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 'raw' => \$raw, | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 'verbose' => \$verbose, | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 'help|h' => \$help, | ||||||||||||||||||||||||||||||||||||||||||
| 42 | ) or usage_exit(); | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 10 | ▶ | if ($help) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 2 | usage_exit(0); | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 8 | ▶ | unless ($url || $file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$url or $file
MC/DC100%2/2
$url or $file
$url
$file
Truth table100%3/3
A or B
A= $urlB= $file
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | print STDERR "Error: --url or --file is required\n\n"; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | usage_exit(1); | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 7 | ▶ | if ($url && $file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$url and $file
MC/DC100%2/2
$url and $file
$url
$file
Truth table100%3/3
A and B
A= $urlB= $file
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | print STDERR "Error: --url and --file are mutually exclusive\n\n"; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | usage_exit(1); | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 6 | ▶ | unless ($dump_swml || $list_tools || $exec_name) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$dump_swml or $list_tools or $exec_name
$dump_swml or $list_tools
MC/DC66%2/3
$dump_swml or $list_tools or $exec_name
$dump_swml
$list_tools
$exec_name
Truth table75%3/4
A or B or C
A= $dump_swmlB= $list_toolsC= $exec_name
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | print STDERR "Error: one of --dump-swml, --list-tools, or --exec NAME is required\n\n"; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | usage_exit(1); | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | # --- File mode: load Perl script in-process and walk runtime tool registry --- | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 4 | ▶ | if ($file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 4 | do_file_mode(); | |||||||||||||||||||||||||||||||||||||||||
| 66 | 3 | exit 0; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | # --- URL mode: HTTP path --- | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my $uri = URI->new($url); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | my $userinfo = $uri->userinfo // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$uri->userinfo // ""
MC/DC0%0/1
$uri->userinfo // ""
$uri->userinfo
Truth table0%0/2
A
A= $uri->userinfo
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my ($auth_user, $auth_pass) = split(/:/, $userinfo, 2); | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | # Build base URL without credentials | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my $clean_uri = $uri->clone; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $clean_uri->userinfo(undef); | |||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my $base_url = $clean_uri->as_string; | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | # Remove trailing slash | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | $base_url =~ s{/$}{}; | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my $http = HTTP::Tiny->new(timeout => 30); | |||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | # --- Execute the requested operation --- | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if ($dump_swml) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | do_dump_swml(); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | elsif ($list_tools) { | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | do_list_tools(); | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | elsif ($exec_name) { | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | do_exec($exec_name); | |||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | exit 0; | |||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | # ============================================================ | ||||||||||||||||||||||||||||||||||||||||||
| 99 | # Operations | ||||||||||||||||||||||||||||||||||||||||||
| 100 | # ============================================================ | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub do_dump_swml { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_dump_swml: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | my $response = http_get($base_url); | |||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ($raw) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | print $response->{content}; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | print "\n" unless $response->{content} =~ /\n$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | my $data = eval { JSON::decode_json($response->{content}) }; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | die "Error: Failed to parse JSON response: $@\n"; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | print JSON->new->utf8->pretty->canonical->encode($data); | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub do_list_tools { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_list_tools: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | my $response = http_get($base_url); | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | my $data = eval { JSON::decode_json($response->{content}) }; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | die "Error: Failed to parse JSON response: $@\n"; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | # Extract SWAIG functions from the SWML document | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | my @functions; | |||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | # Navigate the SWML structure to find AI verb functions | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | if (my $sections = $data->{sections}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | for my $section_name (keys %$sections) { | |||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | my $verbs = $sections->{$section_name}; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | next unless ref $verbs eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | for my $verb (@$verbs) { | |||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if (ref $verb eq 'HASH' && exists $verb->{ai}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $verb eq "HASH" and exists $verb->{"ai"}
MC/DC0%0/2
ref $verb eq "HASH" and exists $verb->{"ai"}
ref $verb eq "HASH"
exists $verb->{"ai"}
Truth table0%0/3
A and B
A= ref $verb eq "HASH"B= exists $verb->{"ai"}
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | my $ai = $verb->{ai}; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if (my $swaig = $ai->{SWAIG}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | if (my $funcs = $swaig->{functions}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | push @functions, @$funcs; | |||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if (!@functions) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | print "No SWAIG functions found.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | print_tool_list(\@functions); | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | sub do_exec { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_exec: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | my ($func_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | # Parse --param key=value pairs | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | my %args; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | for my $p (@params) { | |||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | if ($p =~ /^([^=]+)=(.*)$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | $args{$1} = $2; | |||||||||||||||||||||||||||||||||||||||||
| 160 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | die "Error: Invalid --param format '$p'. Use key=value\n"; | |||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | my $swaig_url = $base_url . '/swaig'; | |||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | my $payload = { | |||||||||||||||||||||||||||||||||||||||||
| 168 | function => $func_name, | ||||||||||||||||||||||||||||||||||||||||||
| 169 | argument => { | ||||||||||||||||||||||||||||||||||||||||||
| 170 | parsed => [ \%args ], | ||||||||||||||||||||||||||||||||||||||||||
| 171 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 172 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | my $json_body = JSON::encode_json($payload); | |||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | if ($verbose) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | print STDERR ">>> POST $swaig_url\n"; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | print STDERR ">>> Body: $json_body\n"; | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | my $response = http_post($swaig_url, $json_body); | |||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | if ($verbose) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | print STDERR "<<< Status: $response->{status}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | print STDERR "<<< Body: $response->{content}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | if ($raw) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | print $response->{content}; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | print "\n" unless $response->{content} =~ /\n$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | my $data = eval { JSON::decode_json($response->{content}) }; | |||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | print $response->{content}; | |||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | print "\n" unless $response->{content} =~ /\n$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | print JSON->new->utf8->pretty->canonical->encode($data); | |||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | # ============================================================ | ||||||||||||||||||||||||||||||||||||||||||
| 203 | # File mode: in-process loader | ||||||||||||||||||||||||||||||||||||||||||
| 204 | # ============================================================ | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 4 | ▶ | sub do_file_mode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_file_mode: called CC=10 SCAR=26.9
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 4 | ▶ | unless (-f $file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 1 | die "Error: --file '$file' does not exist or is not a regular file\n"; | |||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | # Resolve to absolute path so `do` doesn't depend on @INC. | ||||||||||||||||||||||||||||||||||||||||||
| 212 | 3 | my $abs = File::Spec->rel2abs($file); | |||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | 3 | ▶ | if ($verbose) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | print STDERR ">>> do '$abs'\n"; | |||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | # Run the file. The example scripts guard their server-start with | ||||||||||||||||||||||||||||||||||||||||||
| 219 | # `unless caller`, so loading via `do` does NOT spin a server. | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 3 | my $rv = do $abs; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 3 | ▶ | if (my $e = $@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | die "Error: failed to compile '$file': $e\n"; | |||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | 3 | ▶ | if (!defined $rv && $!) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $rv and $!
MC/DC0%0/2
not defined $rv and $!
not defined $rv
$!
Truth table33%1/3
not(A) and B
A= not defined $rvB= $!
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | die "Error: failed to read '$file': $!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 3 | my $svc = _resolve_service_instance($rv); | |||||||||||||||||||||||||||||||||||||||||
| 229 | 3 | ▶ | unless ($svc) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | die "Error: could not locate a SignalWire::SWML::Service instance in '$file'.\n" | |||||||||||||||||||||||||||||||||||||||||
| 231 | . "Hint: define a build_service() sub that returns an instance, return one as\n" | ||||||||||||||||||||||||||||||||||||||||||
| 232 | . "the file's last value, or declare a package that ISA SignalWire::SWML::Service.\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 3 | ▶ | if ($dump_swml) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | do_file_dump_swml($svc); | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | elsif ($list_tools) { | ||||||||||||||||||||||||||||||||||||||||||
| 239 | 2 | do_file_list_tools($svc); | |||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||
| 241 | elsif ($exec_name) { | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 1 | do_file_exec($svc, $exec_name); | |||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | # Resolve a Service instance from the loaded script. Strategy, in order: | ||||||||||||||||||||||||||||||||||||||||||
| 247 | # 1. If the file's last value (the result of `do`) is itself a | ||||||||||||||||||||||||||||||||||||||||||
| 248 | # SignalWire::SWML::Service object, use that. | ||||||||||||||||||||||||||||||||||||||||||
| 249 | # 2. If main::build_service exists, call it. | ||||||||||||||||||||||||||||||||||||||||||
| 250 | # 3. Scan %:: for packages that ISA SignalWire::SWML::Service (skip | ||||||||||||||||||||||||||||||||||||||||||
| 251 | # Service and AgentBase themselves) and instantiate one. | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 3 | ▶ | sub _resolve_service_instance { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_resolve_service_instance: called CC=13 SCAR=43.2
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 3 | my ($do_rv) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | 3 | ▶ | if (blessed($do_rv) && $do_rv->isa('SignalWire::SWML::Service')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($do_rv) and $do_rv->isa("SignalWire::SWML::Service")
MC/DC0%0/2
builtin::blessed($do_rv) and $do_rv->isa("SignalWire::SWML::Service")
builtin::blessed($do_rv)
$do_rv->isa("SignalWire::SWML::Service")
Truth table33%1/3
A and B
A= builtin::blessed($do_rv)B= $do_rv->isa("SignalWire::SWML::Service")
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | ▶ | if ($verbose) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | print STDERR ">>> using service instance returned by file (" . ref($do_rv) . ")\n"; | |||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | return $do_rv; | |||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 3 | ▶ | if (my $builder = main->can('build_service')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 3 | ▶ | if ($verbose) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | print STDERR ">>> calling main::build_service\n"; | |||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 3 | my $svc = $builder->(); | |||||||||||||||||||||||||||||||||||||||||
| 267 | 3 | ▶ | if (blessed($svc) && $svc->isa('SignalWire::SWML::Service')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($svc) and $svc->isa("SignalWire::SWML::Service")
MC/DC0%0/2
builtin::blessed($svc) and $svc->isa("SignalWire::SWML::Service")
builtin::blessed($svc)
$svc->isa("SignalWire::SWML::Service")
Truth table33%1/3
A and B
A= builtin::blessed($svc)B= $svc->isa("SignalWire::SWML::Service")
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | 3 | return $svc; | |||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | # Walk the symbol table for user subclasses of Service. | ||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | my @candidates = _find_service_subclasses(); | |||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | for my $pkg (@candidates) { | |||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | ▶ | if ($verbose) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | print STDERR ">>> attempting parameterless construction of $pkg\n"; | |||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | my $svc = eval { $pkg->new }; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | if (!$@ && blessed($svc) && $svc->isa('SignalWire::SWML::Service')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not $@ and builtin::blessed($svc) and $svc->isa("SignalWire::SWML::Service")
not $@ and builtin::blessed($svc)
MC/DC0%0/3
not $@ and builtin::blessed($svc) and $svc->isa("SignalWire::SWML::Service")
not $@
builtin::blessed($svc)
$svc->isa("SignalWire::SWML::Service")
Truth table0%0/4
not(A) and B and C
A= not $@B= builtin::blessed($svc)C= $svc->isa("SignalWire::SWML::Service")
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | return $svc; | |||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | ▶ | sub _find_service_subclasses { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_service_subclasses: not called CC=1 SCAR=5.0
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | my @found; | |||||||||||||||||||||||||||||||||||||||||
| 289 | my %seen; | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | # Recursive walk of %:: → all loaded packages. | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | my $walk; | |||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | ▶ | $walk = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=10 SCAR=42.6
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | my ($stash, $prefix) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | for my $entry (keys %$stash) { | |||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | ▶ | next unless $entry =~ /::$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | my $name = $entry; | |||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | $name =~ s/::$//; | |||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | ▶ | next if $name eq '' || $name eq 'main'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$name eq "" or $name eq "main"
MC/DC0%0/2
$name eq "" or $name eq "main"
$name eq ""
$name eq "main"
Truth table0%0/3
A or B
A= $name eq ""B= $name eq "main"
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | ▶ | my $pkg = $prefix ? "${prefix}::${name}" : $name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | ▶ | next if $seen{$pkg}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | # Only treat as a class if it has any symbols at all. | ||||||||||||||||||||||||||||||||||||||||||
| 304 | 10 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | my $sub_stash = \%{ "${pkg}::" }; | |||||||||||||||||||||||||||||||||||||||||
| 306 | 10 | ▶ | use strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | ▶ | if (eval { $pkg->isa('SignalWire::SWML::Service') } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
eval {
do {
$pkg->isa("SignalWire::SWML::Service")
}
} and $pkg ne "SignalWire::SWML::Service" and $pkg ne "SignalWire::Agent::AgentBase"
eval {
do {
$pkg->isa("SignalWire::SWML::Service")
}
} and $pkg ne "SignalWire::SWML::Service"
MC/DC0%0/3
eval {
do {
$pkg->isa("SignalWire::SWML::Service")
}
} and $pkg ne "SignalWire::SWML::Service" and $pkg ne "SignalWire::Agent::AgentBase"
eval {
do {
$pkg->isa("SignalWire::SWML::Service")
}
}
$pkg ne "SignalWire::SWML::Service"
$pkg ne "SignalWire::Agent::AgentBase"
Truth table0%0/4
A and B and C
A= eval {
do {
$pkg->isa("SignalWire::SWML::Service")
}
}B= $pkg ne "SignalWire::SWML::Service"C= $pkg ne "SignalWire::Agent::AgentBase"
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | && $pkg ne 'SignalWire::SWML::Service' | ||||||||||||||||||||||||||||||||||||||||||
| 310 | && $pkg ne 'SignalWire::Agent::AgentBase') | ||||||||||||||||||||||||||||||||||||||||||
| 311 | { | ||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | push @found, $pkg; | |||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | $walk->($sub_stash, $pkg); | |||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | $walk->(\%::, ''); | |||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | return @found; | |||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | ▶ | sub do_file_dump_swml { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_file_dump_swml: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | my ($svc) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 323 | # Render the SWML document the same way the PSGI handler does. We | ||||||||||||||||||||||||||||||||||||||||||
| 324 | # pass a minimal env hash so dynamic renderers don't blow up. | ||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | my $doc = $svc->render_main_swml({}); | |||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | ▶ | if ($raw) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | print JSON::encode_json($doc), "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 328 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | print JSON->new->utf8->pretty->canonical->encode($doc); | |||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | 2 | ▶ | sub do_file_list_tools { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_file_list_tools: called CC=3 SCAR=11.8
| |||||||||||||||||||||||||||||||||||||||||||
| 334 | 2 | my ($svc) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 335 | 2 | my @names = $svc->list_tool_names; | |||||||||||||||||||||||||||||||||||||||||
| 336 | 2 | ▶ | my $registry = $svc->tools // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$svc->tools // {}
MC/DC0%0/1
$svc->tools // {}
$svc->tools
Truth table50%1/2
A
A= $svc->tools
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | 2 | ▶ | if (!@names) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | print "No SWAIG functions found.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | 2 | my @functions = map { $registry->{$_} } @names; | |||||||||||||||||||||||||||||||||||||||||
| 344 | 2 | print_tool_list(\@functions); | |||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | 1 | ▶ | sub do_file_exec { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_file_exec: called CC=8 SCAR=26.3
| |||||||||||||||||||||||||||||||||||||||||||
| 348 | 1 | my ($svc, $func_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | 1 | my %args; | |||||||||||||||||||||||||||||||||||||||||
| 351 | 1 | for my $p (@params) { | |||||||||||||||||||||||||||||||||||||||||
| 352 | 1 | ▶ | if ($p =~ /^([^=]+)=(.*)$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 353 | 1 | $args{$1} = $2; | |||||||||||||||||||||||||||||||||||||||||
| 354 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | die "Error: Invalid --param format '$p'. Use key=value\n"; | |||||||||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | 1 | my $payload = { | |||||||||||||||||||||||||||||||||||||||||
| 360 | function => $func_name, | ||||||||||||||||||||||||||||||||||||||||||
| 361 | argument => { parsed => [ \%args ] }, | ||||||||||||||||||||||||||||||||||||||||||
| 362 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | 1 | my $result = $svc->on_function_call($func_name, \%args, $payload); | |||||||||||||||||||||||||||||||||||||||||
| 365 | 1 | ▶ | unless (defined $result) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | die "Error: function '$func_name' is not registered or returned undef.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | 1 | my $hash; | |||||||||||||||||||||||||||||||||||||||||
| 370 | 1 | ▶ | if (ref $result eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | 1 | $hash = $result; | |||||||||||||||||||||||||||||||||||||||||
| 372 | ▶ | } elsif (blessed($result) && $result->can('to_hash')) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
builtin::blessed($result) and $result->can("to_hash")
MC/DC0%0/2
builtin::blessed($result) and $result->can("to_hash")
builtin::blessed($result)
$result->can("to_hash")
Truth table0%0/3
A and B
A= builtin::blessed($result)B= $result->can("to_hash")
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | $hash = $result->to_hash; | |||||||||||||||||||||||||||||||||||||||||
| 374 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | $hash = { response => "$result" }; | |||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | 1 | ▶ | if ($raw) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | 0 | print JSON::encode_json($hash), "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 380 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 381 | 1 | print JSON->new->utf8->pretty->canonical->encode($hash); | |||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | # ============================================================ | ||||||||||||||||||||||||||||||||||||||||||
| 386 | # Shared rendering | ||||||||||||||||||||||||||||||||||||||||||
| 387 | # ============================================================ | ||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | 2 | ▶ | sub print_tool_list { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
print_tool_list: called CC=15 SCAR=32.3
| |||||||||||||||||||||||||||||||||||||||||||
| 390 | 2 | my ($functions) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 391 | 2 | ▶ | if ($raw) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 392 | 0 | for my $f (@$functions) { | |||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | ▶ | printf "%s\t%s\n", $f->{function} // 'unnamed', $f->{description} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/4
$f->{"function"} // "unnamed"
$f->{"description"} // ""
MC/DC0%0/1
$f->{"function"} // "unnamed"
$f->{"function"}
MC/DC0%0/1
$f->{"description"} // ""
$f->{"description"}
Truth table0%0/2
A
A= $f->{"function"}
Truth table0%0/2
A
A= $f->{"description"}
| |||||||||||||||||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||||||||||||||
| 395 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 396 | 2 | printf "Found %d SWAIG function(s):\n\n", scalar @$functions; | |||||||||||||||||||||||||||||||||||||||||
| 397 | 2 | for my $f (@$functions) { | |||||||||||||||||||||||||||||||||||||||||
| 398 | 2 | ▶ | my $name = $f->{function} // 'unnamed'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$f->{"function"} // "unnamed"
MC/DC0%0/1
$f->{"function"} // "unnamed"
$f->{"function"}
Truth table50%1/2
A
A= $f->{"function"}
| |||||||||||||||||||||||||||||||||||||||||||
| 399 | 2 | ▶ | my $desc = $f->{description} // '(no description)'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$f->{"description"} // "(no description)"
MC/DC0%0/1
$f->{"description"} // "(no description)"
$f->{"description"}
Truth table50%1/2
A
A= $f->{"description"}
| |||||||||||||||||||||||||||||||||||||||||||
| 400 | 2 | printf " %-30s %s\n", $name, $desc; | |||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | # Show parameters if any | ||||||||||||||||||||||||||||||||||||||||||
| 403 | 2 | ▶ | if (my $params_schema = $f->{parameters}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 404 | 2 | ▶ | if (my $props = $params_schema->{properties}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 405 | 2 | ▶ | my $required = $params_schema->{required} // []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params_schema->{"required"} // []
MC/DC0%0/1
$params_schema->{"required"} // []
$params_schema->{"required"}
Truth table50%1/2
A
A= $params_schema->{"required"}
| |||||||||||||||||||||||||||||||||||||||||||
| 406 | 2 | my %required_map = map { $_ => 1 } @$required; | |||||||||||||||||||||||||||||||||||||||||
| 407 | 2 | for my $pname (sort keys %$props) { | |||||||||||||||||||||||||||||||||||||||||
| 408 | 2 | ▶ | my $ptype = $props->{$pname}{type} // 'any'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$props->{$pname}{"type"} // "any"
MC/DC0%0/1
$props->{$pname}{"type"} // "any"
$props->{$pname}{"type"}
Truth table50%1/2
A
A= $props->{$pname}{"type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 409 | 2 | ▶ | my $pdesc = $props->{$pname}{description} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$props->{$pname}{"description"} // ""
MC/DC0%0/1
$props->{$pname}{"description"} // ""
$props->{$pname}{"description"}
Truth table50%1/2
A
A= $props->{$pname}{"description"}
| |||||||||||||||||||||||||||||||||||||||||||
| 410 | 2 | ▶ | my $req_marker = $required_map{$pname} ? '*' : ' '; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 411 | 2 | printf " %s %-20s %-10s %s\n", $req_marker, $pname, "($ptype)", $pdesc; | |||||||||||||||||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||||||||||||
| 415 | 2 | print "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | # ============================================================ | ||||||||||||||||||||||||||||||||||||||||||
| 421 | # HTTP helpers | ||||||||||||||||||||||||||||||||||||||||||
| 422 | # ============================================================ | ||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | 0 | ▶ | sub _auth_headers { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_auth_headers: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | my %headers; | |||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | ▶ | if (defined $auth_user && defined $auth_pass) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $auth_user and defined $auth_pass
MC/DC0%0/2
defined $auth_user and defined $auth_pass
defined $auth_user
defined $auth_pass
Truth table0%0/3
A and B
A= defined $auth_userB= defined $auth_pass
| |||||||||||||||||||||||||||||||||||||||||||
| 427 | 0 | my $encoded = encode_base64("$auth_user:$auth_pass", ''); | |||||||||||||||||||||||||||||||||||||||||
| 428 | 0 | $headers{Authorization} = "Basic $encoded"; | |||||||||||||||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||||||||||||||||
| 430 | 0 | return %headers; | |||||||||||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | 0 | ▶ | sub http_get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
http_get: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 434 | 0 | my ($target_url) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | my %headers = _auth_headers(); | |||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | ▶ | if ($verbose) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | print STDERR ">>> GET $target_url\n"; | |||||||||||||||||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||
| 441 | 0 | my $response = $http->get($target_url, { headers => \%headers }); | |||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | ▶ | if ($verbose) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | print STDERR "<<< Status: $response->{status}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | 0 | ▶ | unless ($response->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 448 | die sprintf("Error: HTTP %s %s\n%s\n", | ||||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | ▶ | $response->{status}, $response->{reason}, $response->{content} // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$response->{"content"} // ""
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table0%0/2
A
A= $response->{"content"}
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||
| 452 | 0 | return $response; | |||||||||||||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | 0 | ▶ | sub http_post { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
http_post: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | my ($target_url, $body) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 457 | 0 | my %headers = _auth_headers(); | |||||||||||||||||||||||||||||||||||||||||
| 458 | 0 | $headers{'Content-Type'} = 'application/json'; | |||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||
| 460 | 0 | my $response = $http->post($target_url, { | |||||||||||||||||||||||||||||||||||||||||
| 461 | headers => \%headers, | ||||||||||||||||||||||||||||||||||||||||||
| 462 | content => $body, | ||||||||||||||||||||||||||||||||||||||||||
| 463 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||
| 465 | 0 | ▶ | unless ($response->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 466 | die sprintf("Error: HTTP %s %s\n%s\n", | ||||||||||||||||||||||||||||||||||||||||||
| 467 | 0 | ▶ | $response->{status}, $response->{reason}, $response->{content} // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$response->{"content"} // ""
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table0%0/2
A
A= $response->{"content"}
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | 0 | return $response; | |||||||||||||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | 6 | ▶ | sub usage_exit { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usage_exit: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 474 | 6 | my ($code) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 475 | 6 | ▶ | $code //= 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$code //= 1
MC/DC0%0/1
$code //= 1
$code
Truth table50%1/2
A
A= $code
| |||||||||||||||||||||||||||||||||||||||||||
| 476 | 6 | print STDERR <<'USAGE'; | |||||||||||||||||||||||||||||||||||||||||
| 477 | swaig-test - CLI tool for testing SWAIG agent endpoints | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | Usage: | ||||||||||||||||||||||||||||||||||||||||||
| 480 | swaig-test --url URL [OPTIONS] | ||||||||||||||||||||||||||||||||||||||||||
| 481 | swaig-test --file PATH [OPTIONS] | ||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | Options: | ||||||||||||||||||||||||||||||||||||||||||
| 484 | --url URL Agent URL with embedded auth (http://user:pass@host:port/route) | ||||||||||||||||||||||||||||||||||||||||||
| 485 | --file PATH Path to a Perl script that builds an SWML::Service / | ||||||||||||||||||||||||||||||||||||||||||
| 486 | AgentBase instance. Loads the file in-process (no HTTP) | ||||||||||||||||||||||||||||||||||||||||||
| 487 | and reads the runtime tool registry. | ||||||||||||||||||||||||||||||||||||||||||
| 488 | --dump-swml Fetch and display the SWML document | ||||||||||||||||||||||||||||||||||||||||||
| 489 | --list-tools List available SWAIG functions | ||||||||||||||||||||||||||||||||||||||||||
| 490 | --exec NAME Execute a SWAIG function by name | ||||||||||||||||||||||||||||||||||||||||||
| 491 | --param key=value Parameter for --exec (repeatable) | ||||||||||||||||||||||||||||||||||||||||||
| 492 | --raw Output compact JSON (no pretty-printing) | ||||||||||||||||||||||||||||||||||||||||||
| 493 | --verbose Show request/response details on stderr | ||||||||||||||||||||||||||||||||||||||||||
| 494 | --help, -h Show this help message | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | Examples: | ||||||||||||||||||||||||||||||||||||||||||
| 497 | swaig-test --url http://user:pass@localhost:3000/ --dump-swml | ||||||||||||||||||||||||||||||||||||||||||
| 498 | swaig-test --url http://user:pass@localhost:3000/ --list-tools | ||||||||||||||||||||||||||||||||||||||||||
| 499 | swaig-test --url http://user:pass@localhost:3000/ --exec get_weather --param location=London | ||||||||||||||||||||||||||||||||||||||||||
| 500 | swaig-test --url http://user:pass@localhost:3000/ --exec get_weather --param location=London --raw | ||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | swaig-test --file examples/swmlservice_swaig_standalone.pl --list-tools | ||||||||||||||||||||||||||||||||||||||||||
| 503 | swaig-test --file examples/swmlservice_swaig_standalone.pl --exec lookup_competitor --param competitor=ACME | ||||||||||||||||||||||||||||||||||||||||||
| 504 | USAGE | ||||||||||||||||||||||||||||||||||||||||||
| 505 | 6 | exit $code; | |||||||||||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||||||||||