File Coverage

blib/lib/SignalWire/Agents/Skills/Builtin/Math.pm
Criterion Covered Total %
statement 32 34 94.1
branch 3 4 75.0
condition 2 5 40.0
subroutine 10 11 90.9
pod 0 3 0.0
total 47 57 82.4


line stmt bran cond sub pod time code
1             package SignalWire::Agents::Skills::Builtin::Math;
2 3     3   24 use strict;
  3         8  
  3         175  
3 3     3   20 use warnings;
  3         7  
  3         258  
4 3     3   22 use Moo;
  3         6  
  3         24  
5             extends 'SignalWire::Agents::Skills::SkillBase';
6              
7 3     3   1497 use SignalWire::Agents::Skills::SkillRegistry;
  3         7  
  3         1422  
8             SignalWire::Agents::Skills::SkillRegistry->register_skill('math', __PACKAGE__);
9              
10             has '+skill_name' => (default => sub { 'math' });
11             has '+skill_description' => (default => sub { 'Perform basic mathematical calculations' });
12             has '+supports_multiple_instances' => (default => sub { 0 });
13              
14 7     7 0 5320 sub setup { 1 }
15              
16             sub register_tools {
17 6     6 0 592 my ($self) = @_;
18              
19             $self->define_tool(
20             name => 'calculate',
21             description => 'Perform a mathematical calculation with basic operations (+, -, *, /, %, **)',
22             parameters => {
23             type => 'object',
24             properties => {
25             expression => { type => 'string', description => 'Mathematical expression to evaluate' },
26             },
27             required => ['expression'],
28             },
29             handler => sub {
30 2     2   8 my ($args, $raw) = @_;
31 2         4950 require SignalWire::Agents::SWAIG::FunctionResult;
32 2   50     17 my $expr = $args->{expression} // '';
33              
34             # Safe evaluation: only allow numbers and basic operators
35 2 100       21 if ($expr =~ /^[\d\s\+\-\*\/\%\.\(\)\^]+$/) {
36 1         3 $expr =~ s/\^/**/g; # Convert ^ to **
37 3     3   27 my $result = eval { no strict; no warnings; eval $expr };
  3     3   61  
  3         137  
  3         19  
  3         7  
  3         1033  
  1         4  
  1         67  
38 1 50 33     11 if (defined $result && !$@) {
39 1         13 return SignalWire::Agents::SWAIG::FunctionResult->new(
40             response => "The result of $args->{expression} is $result"
41             );
42             }
43             }
44 1         39 return SignalWire::Agents::SWAIG::FunctionResult->new(
45             response => "Could not evaluate expression: $args->{expression}"
46             );
47             },
48 6         124 );
49             }
50              
51             sub _get_prompt_sections {
52             return [{
53 3     3   26 title => 'Mathematical Calculations',
54             body => '',
55             bullets => [
56             'Use the calculate tool for math operations',
57             'Supports +, -, *, /, %, and ** (exponentiation)',
58             ],
59             }];
60             }
61              
62             sub get_parameter_schema {
63             return {
64 0     0 0   %{ SignalWire::Agents::Skills::SkillBase->get_parameter_schema },
  0            
65             };
66             }
67              
68             1;