| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||
| 2 | # swmlservice_ai_sidecar.pl | ||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||
| 4 | # Proves that SignalWire::SWML::Service can emit the `ai_sidecar` verb, | ||||||||||||||||||||||
| 5 | # register SWAIG tools the sidecar's LLM can call, and dispatch them | ||||||||||||||||||||||
| 6 | # end-to-end - without any AgentBase code path. | ||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||
| 8 | # The `ai_sidecar` verb runs an AI listener alongside an in-progress | ||||||||||||||||||||||
| 9 | # call (real-time copilot, transcription analyzer, compliance monitor, | ||||||||||||||||||||||
| 10 | # etc.). It is NOT an agent - it does not own the call. So the right | ||||||||||||||||||||||
| 11 | # host is SWML::Service, not AgentBase. | ||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||
| 13 | # Run: | ||||||||||||||||||||||
| 14 | # perl -Ilib examples/swmlservice_ai_sidecar.pl | ||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||
| 16 | # What this serves: | ||||||||||||||||||||||
| 17 | # GET /sales-sidecar -> SWML doc with the ai_sidecar verb | ||||||||||||||||||||||
| 18 | # POST /sales-sidecar/swaig -> SWAIG tool dispatch (used by the | ||||||||||||||||||||||
| 19 | # sidecar's LLM) | ||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||
| 21 | # Drive the SWAIG path through the bundled CLI: | ||||||||||||||||||||||
| 22 | # bin/swaig-test --url http://user:pass@localhost:3000/sales-sidecar --list-tools | ||||||||||||||||||||||
| 23 | # bin/swaig-test --url http://user:pass@localhost:3000/sales-sidecar \ | ||||||||||||||||||||||
| 24 | # --exec lookup_competitor --param competitor=ACME | ||||||||||||||||||||||
| 25 | |||||||||||||||||||||||
| 26 | 1 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 27 | 1 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 28 | 1 | ▶ | use lib 'lib'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 29 | 1 | ▶ | use SignalWire::SWML::Service; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 30 | |||||||||||||||||||||||
| 31 | 1 | ▶ | sub build_service { | ||||||||||||||||||||
|
Subroutine
build_service: called CC=6 SCAR=23.1
| |||||||||||||||||||||||
| 32 | 1 | my %opts = @_; | |||||||||||||||||||||
| 33 | my $public_url = $opts{public_url} | ||||||||||||||||||||||
| 34 | 1 | ▶ | // 'https://your-host.example.com/sales-sidecar'; | ||||||||||||||||||||
|
Condition50%1/2
$opts{"public_url"} // "https://your-host.example.com/sales-sidecar"
MC/DC0%0/1
$opts{"public_url"} // "https://your-host.example.com/sales-sidecar"
$opts{"public_url"}
Truth table50%1/2
A
A= $opts{"public_url"}
| |||||||||||||||||||||||
| 35 | |||||||||||||||||||||||
| 36 | my %args = ( | ||||||||||||||||||||||
| 37 | name => 'sales-sidecar', | ||||||||||||||||||||||
| 38 | route => '/sales-sidecar', | ||||||||||||||||||||||
| 39 | basic_auth_user => $opts{user} // 'user', | ||||||||||||||||||||||
| 40 | 1 | ▶ | basic_auth_password => $opts{pass} // 'pass', | ||||||||||||||||||||
|
Condition50%2/4
$opts{"user"} // "user"
$opts{"pass"} // "pass"
MC/DC0%0/1
$opts{"user"} // "user"
$opts{"user"}
MC/DC0%0/1
$opts{"pass"} // "pass"
$opts{"pass"}
Truth table50%1/2
A
A= $opts{"user"}
Truth table50%1/2
A
A= $opts{"pass"}
| |||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||
| 42 | # Only override host/port when caller provides one - otherwise the | ||||||||||||||||||||||
| 43 | # Service constructor honors SWML_HOST / SWML_PORT environment vars. | ||||||||||||||||||||||
| 44 | 1 | ▶ | $args{host} = $opts{host} if defined $opts{host}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 45 | 1 | ▶ | $args{port} = $opts{port} if defined $opts{port}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 46 | 1 | my $svc = SignalWire::SWML::Service->new(%args); | |||||||||||||||||||||
| 47 | |||||||||||||||||||||||
| 48 | # 1. Emit any SWML - including ai_sidecar. The Document API's | ||||||||||||||||||||||
| 49 | # add_verb() accepts arbitrary verb dicts, so new platform verbs | ||||||||||||||||||||||
| 50 | # work without an SDK release. NOTE the SWAIG hash key MUST be | ||||||||||||||||||||||
| 51 | # UPPERCASE - that is how mod_openai recognizes it. | ||||||||||||||||||||||
| 52 | 1 | $svc->document->add_verb('main', 'answer', {}); | |||||||||||||||||||||
| 53 | 1 | $svc->document->add_verb('main', 'ai_sidecar', { | |||||||||||||||||||||
| 54 | prompt => 'You are a real-time sales copilot. Listen to the call ' | ||||||||||||||||||||||
| 55 | . 'and surface competitor pricing comparisons when relevant.', | ||||||||||||||||||||||
| 56 | lang => 'en-US', | ||||||||||||||||||||||
| 57 | direction => ['remote-caller', 'local-caller'], | ||||||||||||||||||||||
| 58 | # Where the sidecar POSTs lifecycle / transcription events. | ||||||||||||||||||||||
| 59 | # Optional - skip if you don't need an event sink. | ||||||||||||||||||||||
| 60 | url => "$public_url/events", | ||||||||||||||||||||||
| 61 | # Where the sidecar's LLM POSTs SWAIG tool calls. This SDK's | ||||||||||||||||||||||
| 62 | # /swaig route is what answers them. | ||||||||||||||||||||||
| 63 | SWAIG => { | ||||||||||||||||||||||
| 64 | defaults => { web_hook_url => "$public_url/swaig" }, | ||||||||||||||||||||||
| 65 | }, | ||||||||||||||||||||||
| 66 | }); | ||||||||||||||||||||||
| 67 | 1 | $svc->document->add_verb('main', 'hangup', {}); | |||||||||||||||||||||
| 68 | |||||||||||||||||||||||
| 69 | # 2. Register tools the sidecar's LLM can call. Same define_tool() | ||||||||||||||||||||||
| 70 | # you'd use on AgentBase - it lives on SWML::Service. | ||||||||||||||||||||||
| 71 | $svc->define_tool( | ||||||||||||||||||||||
| 72 | name => 'lookup_competitor', | ||||||||||||||||||||||
| 73 | description => 'Look up competitor pricing by company name. The sidecar ' | ||||||||||||||||||||||
| 74 | . 'should call this whenever the caller mentions a competitor.', | ||||||||||||||||||||||
| 75 | parameters => { | ||||||||||||||||||||||
| 76 | type => 'object', | ||||||||||||||||||||||
| 77 | properties => { | ||||||||||||||||||||||
| 78 | competitor => { | ||||||||||||||||||||||
| 79 | type => 'string', | ||||||||||||||||||||||
| 80 | description => "The competitor's company name, e.g. 'ACME'.", | ||||||||||||||||||||||
| 81 | }, | ||||||||||||||||||||||
| 82 | }, | ||||||||||||||||||||||
| 83 | required => ['competitor'], | ||||||||||||||||||||||
| 84 | }, | ||||||||||||||||||||||
| 85 | 0 | ▶ | handler => sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 86 | 0 | my ($args, $raw_data) = @_; | |||||||||||||||||||||
| 87 | 0 | ▶ | my $competitor = $args->{competitor} // '<unknown>'; | ||||||||||||||||||||
|
Condition0%0/2
$args->{"competitor"} // "<unknown>"
MC/DC0%0/1
$args->{"competitor"} // "<unknown>"
$args->{"competitor"}
Truth table0%0/2
A
A= $args->{"competitor"}
| |||||||||||||||||||||||
| 88 | return { | ||||||||||||||||||||||
| 89 | 0 | response => "Pricing for $competitor: \$99/seat. Our equivalent " | |||||||||||||||||||||
| 90 | . 'plan is $79/seat with the same SLA.', | ||||||||||||||||||||||
| 91 | }; | ||||||||||||||||||||||
| 92 | }, | ||||||||||||||||||||||
| 93 | 1 | ); | |||||||||||||||||||||
| 94 | |||||||||||||||||||||||
| 95 | # 3. (Optional) Register a routing callback for ai_sidecar lifecycle | ||||||||||||||||||||||
| 96 | # events. The Service stores the callback under its routing | ||||||||||||||||||||||
| 97 | # table; it is invoked by handlers that override | ||||||||||||||||||||||
| 98 | # handle_additional_route() in subclasses. Comment this out if | ||||||||||||||||||||||
| 99 | # you don't need it. | ||||||||||||||||||||||
| 100 | 0 | ▶ | $svc->register_routing_callback('/events', sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 101 | 0 | my ($request_data, $env) = @_; | |||||||||||||||||||||
| 102 | 0 | ▶ | my $event_type = $request_data->{type} // '<unknown>'; | ||||||||||||||||||||
|
Condition0%0/2
$request_data->{"type"} // "<unknown>"
MC/DC0%0/1
$request_data->{"type"} // "<unknown>"
$request_data->{"type"}
Truth table0%0/2
A
A= $request_data->{"type"}
| |||||||||||||||||||||||
| 103 | 0 | warn "[sidecar event] type=$event_type\n"; | |||||||||||||||||||||
| 104 | 0 | return { ok => 1 }; | |||||||||||||||||||||
| 105 | 1 | }); | |||||||||||||||||||||
| 106 | |||||||||||||||||||||||
| 107 | 1 | return $svc; | |||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||
| 109 | |||||||||||||||||||||||
| 110 | # Top-level: only run the server when executed directly. | ||||||||||||||||||||||
| 111 | 1 | ▶ | unless (caller) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 112 | 0 | my $svc = build_service(); | |||||||||||||||||||||
| 113 | 0 | print "SWML::Service ai_sidecar host\n"; | |||||||||||||||||||||
| 114 | 0 | print "Route: " . $svc->route . "\n"; | |||||||||||||||||||||
| 115 | 0 | print "Basic auth: " . $svc->basic_auth_user . ":" . $svc->basic_auth_password . "\n"; | |||||||||||||||||||||
| 116 | 0 | print "SWML URL: http://" | |||||||||||||||||||||
| 117 | . $svc->basic_auth_user . ":" . $svc->basic_auth_password | ||||||||||||||||||||||
| 118 | . "\@" . $svc->host . ":" . $svc->port . $svc->route . "\n\n"; | ||||||||||||||||||||||
| 119 | |||||||||||||||||||||||
| 120 | 0 | require Plack::Runner; | |||||||||||||||||||||
| 121 | 0 | my $runner = Plack::Runner->new; | |||||||||||||||||||||
| 122 | 0 | $runner->parse_options( | |||||||||||||||||||||
| 123 | '--host' => $svc->host, | ||||||||||||||||||||||
| 124 | '--port' => $svc->port, | ||||||||||||||||||||||
| 125 | '--server' => 'HTTP::Server::PSGI', | ||||||||||||||||||||||
| 126 | ); | ||||||||||||||||||||||
| 127 | 0 | $runner->run($svc->to_psgi_app); | |||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||
| 129 | |||||||||||||||||||||||
| 130 | 1 | 1; | |||||||||||||||||||||