SignalWire-Agents

 view release on metacpan or  search on metacpan

examples/session_state.pl  view on Meta::CPAN

    "customer_name": "NAME_OR_UNKNOWN",
    "call_reason": "REASON",
    "resolved": true/false,
    "actions_taken": ["action1", "action2"]
}
POST

# Summary callback
$agent->on_summary(sub {
    my ($summary, $raw) = @_;
    if ($summary) {
        print "CONVERSATION SUMMARY:\n";
        if (ref $summary) {
            print encode_json($summary) . "\n";
        } else {
            print "$summary\n";
        }
    }
});

# --- Tool: check_account ---
$agent->define_tool(
    name        => 'check_account',
    description => 'Look up a customer account by name or ID',
    parameters  => {
        type       => 'object',
        properties => {
            identifier => { type => 'string', description => 'Customer name or account ID' },
        },
        required => ['identifier'],
    },
    handler => sub {
        my ($args, $raw) = @_;
        my $id = $args->{identifier} // 'unknown';
        my $result = SignalWire::Agents::SWAIG::FunctionResult->new(
            "Found account for $id: Premium tier, active since 2020."
        );
        # Update global data so the AI knows the customer
        $result->update_global_data({
            customer_name => $id,
            account_tier  => 'premium',
            call_reason   => 'account_inquiry',
        });
        return $result;
    },
);

# --- Tool: update_preferences ---
$agent->define_tool(
    name        => 'update_preferences',
    description => 'Update customer communication preferences',
    parameters  => {
        type       => 'object',
        properties => {
            email_notifications => { type => 'boolean', description => 'Enable email notifications' },
            sms_notifications   => { type => 'boolean', description => 'Enable SMS notifications' },
        },
    },
    handler => sub {
        my ($args, $raw) = @_;
        my @prefs;
        push @prefs, 'email' if $args->{email_notifications};
        push @prefs, 'SMS'   if $args->{sms_notifications};
        my $pref_str = @prefs ? join(' and ', @prefs) : 'none';
        return SignalWire::Agents::SWAIG::FunctionResult->new(
            "Preferences updated: $pref_str notifications enabled."
        );
    },
);

# --- Tool: end_call ---
$agent->define_tool(
    name        => 'end_call',
    description => 'End the call after saying goodbye',
    parameters  => { type => 'object', properties => {} },
    handler     => sub {
        my ($args, $raw) = @_;
        my $result = SignalWire::Agents::SWAIG::FunctionResult->new(
            'Thank you for calling. Goodbye!'
        );
        $result->hangup;
        return $result;
    },
);

$agent->add_language(name => 'English', code => 'en-US', voice => 'inworld.Mark');
$agent->set_params({ ai_model => 'gpt-4.1-nano' });

print "Starting Session State Demo\n";
print "Available at: http://localhost:3000/session-state\n";

$agent->run;



( run in 0.731 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )