« blib/lib/SignalWire/Skills/Builtin/SwmlTransfer.pm
↑ Summary
blib/lib/SignalWire/Skills/Builtin/WeatherApi.pm »
| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # swmlservice_swaig_standalone.pl | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # Proves that SignalWire::SWML::Service - by itself, with NO AgentBase - | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # can host SWAIG functions and serve them on its own /swaig endpoint. | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # This is the path you take when you want a SWAIG-callable HTTP service | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # that isn't an <ai> agent: the SWAIG verb is a generic LLM-tool surface | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # and SWML::Service is the host. AgentBase is just an SWML::Service | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # subclass that *also* layers in prompts, AI config, dynamic config, and | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # token validation. | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # Run: | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # perl -Ilib examples/swmlservice_swaig_standalone.pl | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # Then exercise the endpoints: | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # curl -u user:pass http://localhost:3000/standalone | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # curl -u user:pass http://localhost:3000/standalone/swaig \ | ||||||||||||||||||||||||||||||||||||||||||
| 19 | # -H 'Content-Type: application/json' \ | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # -d '{"function":"lookup_competitor","argument":{"parsed":[{"competitor":"ACME"}]}}' | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # Or drive it through the bundled CLI without standing up the server: | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # bin/swaig-test --url http://user:pass@localhost:3000/standalone --list-tools | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # bin/swaig-test --url http://user:pass@localhost:3000/standalone \ | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # --exec lookup_competitor --param competitor=ACME | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 2 | ▶ | use lib 'lib'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 2 | ▶ | use SignalWire::SWML::Service; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 2 | ▶ | sub build_service { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build_service: called CC=7 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | my %opts = @_; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 2 | my %args = ( | |||||||||||||||||||||||||||||||||||||||||
| 35 | name => 'standalone-swaig', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | route => '/standalone', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | # Auth: honor SWML_BASIC_AUTH_USER / SWML_BASIC_AUTH_PASSWORD env | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # vars (Service's defaults), or accept caller-supplied creds, or | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # fall back to a stable demo pair so the curl invocations in the | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # docstring keep working out of the box. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | if (defined $opts{user}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | $args{basic_auth_user} = $opts{user}; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } elsif (!defined $ENV{SWML_BASIC_AUTH_USER}) { | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 2 | $args{basic_auth_user} = 'user'; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | ▶ | if (defined $opts{pass}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | $args{basic_auth_password} = $opts{pass}; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } elsif (!defined $ENV{SWML_BASIC_AUTH_PASSWORD}) { | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 2 | $args{basic_auth_password} = 'pass'; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | # Only override host/port when caller provides one - otherwise the | ||||||||||||||||||||||||||||||||||||||||||
| 53 | # Service constructor honors SWML_HOST / SWML_PORT environment vars. | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 2 | ▶ | $args{host} = $opts{host} if defined $opts{host}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | ▶ | $args{port} = $opts{port} if defined $opts{port}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 2 | my $svc = SignalWire::SWML::Service->new(%args); | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | # 1. Build a minimal SWML document. Any verbs are fine - the SWAIG | ||||||||||||||||||||||||||||||||||||||||||
| 59 | # HTTP surface is independent of what the document contains. | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | $svc->document->add_verb('main', 'answer', {}); | |||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | $svc->document->add_verb('main', 'hangup', {}); | |||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | # 2. Register a SWAIG function. define_tool() lives on SWML::Service, | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # not just AgentBase. The handler receives parsed arguments plus | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # the raw POST body. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | $svc->define_tool( | ||||||||||||||||||||||||||||||||||||||||||
| 67 | name => 'lookup_competitor', | ||||||||||||||||||||||||||||||||||||||||||
| 68 | description => 'Look up competitor pricing by company name. Use this when ' | ||||||||||||||||||||||||||||||||||||||||||
| 69 | . "the user asks how a competitor's price compares to ours.", | ||||||||||||||||||||||||||||||||||||||||||
| 70 | parameters => { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | type => 'object', | ||||||||||||||||||||||||||||||||||||||||||
| 72 | properties => { | ||||||||||||||||||||||||||||||||||||||||||
| 73 | competitor => { | ||||||||||||||||||||||||||||||||||||||||||
| 74 | type => 'string', | ||||||||||||||||||||||||||||||||||||||||||
| 75 | description => "The competitor's company name, e.g. 'ACME'.", | ||||||||||||||||||||||||||||||||||||||||||
| 76 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 77 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 78 | required => ['competitor'], | ||||||||||||||||||||||||||||||||||||||||||
| 79 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | ▶ | handler => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | my ($args, $raw_data) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | ▶ | my $competitor = $args->{competitor} // '<unknown>'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"competitor"} // "<unknown>"
MC/DC0%0/1
$args->{"competitor"} // "<unknown>"
$args->{"competitor"}
Truth table50%1/2
A
A= $args->{"competitor"}
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | response => "$competitor pricing is \$99/seat; we're \$79/seat.", | |||||||||||||||||||||||||||||||||||||||||
| 85 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 2 | return $svc; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | # Top-level: only run the server when executed directly. `caller` is | ||||||||||||||||||||||||||||||||||||||||||
| 93 | # false in script context, true when this file is loaded via `require` | ||||||||||||||||||||||||||||||||||||||||||
| 94 | # (e.g. by tests). | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 2 | ▶ | unless (caller) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | # Honor `PORT` env var (preferred — matches audit_http_swml fixture | ||||||||||||||||||||||||||||||||||||||||||
| 97 | # contract and 12-factor convention) or first positional arg, falling | ||||||||||||||||||||||||||||||||||||||||||
| 98 | # back to SWML_PORT (which the Service constructor reads) and finally | ||||||||||||||||||||||||||||||||||||||||||
| 99 | # to 3000 via Service's own default. | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | my $cli_port = $ARGV[0]; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my $env_port = $ENV{PORT}; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my %svc_args; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $svc_args{port} = $cli_port if defined $cli_port && $cli_port =~ /^\d+$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $cli_port and $cli_port =~ /^\d+$/
MC/DC0%0/2
defined $cli_port and $cli_port =~ /^\d+$/
defined $cli_port
$cli_port =~ /^\d+$/
Truth table0%0/3
A and B
A= defined $cli_portB= $cli_port =~ /^\d+$/
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | $svc_args{port} = $env_port if !defined $svc_args{port} | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | && defined $env_port | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not defined $svc_args{"port"} and defined $env_port and $env_port =~ /^\d+$/
not defined $svc_args{"port"} and defined $env_port
MC/DC0%0/3
not defined $svc_args{"port"} and defined $env_port and $env_port =~ /^\d+$/
not defined $svc_args{"port"}
defined $env_port
$env_port =~ /^\d+$/
Truth table0%0/4
not(A) and B and C
A= not defined $svc_args{"port"}B= defined $env_portC= $env_port =~ /^\d+$/
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | && $env_port =~ /^\d+$/; | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | my $svc = build_service(%svc_args); | |||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | print "SWML::Service standalone SWAIG host\n"; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | print "Route: " . $svc->route . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | print "Basic auth: " . $svc->basic_auth_user . ":" . $svc->basic_auth_password . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | print "SWAIG URL: http://" | |||||||||||||||||||||||||||||||||||||||||
| 113 | . $svc->basic_auth_user . ":" . $svc->basic_auth_password | ||||||||||||||||||||||||||||||||||||||||||
| 114 | . "\@" . $svc->host . ":" . $svc->port . $svc->route . "\n\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | require Plack::Runner; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | my $runner = Plack::Runner->new; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | $runner->parse_options( | |||||||||||||||||||||||||||||||||||||||||
| 119 | '--host' => $svc->host, | ||||||||||||||||||||||||||||||||||||||||||
| 120 | '--port' => $svc->port, | ||||||||||||||||||||||||||||||||||||||||||
| 121 | '--server' => 'HTTP::Server::PSGI', | ||||||||||||||||||||||||||||||||||||||||||
| 122 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $runner->run($svc->to_psgi_app); | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||