blib/lib/Langertha/Virtual/Rules.pm

filter: Statementstmt 100% 59 / 59 Branchbran 88.2% 30 / 34 Conditioncond 68.2% 28 / 41 MC/DCmcdc 47.8% 11 / 23 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 83.1% 143 / 172 CC 34 SCAR 19.5
CC
34
Cov
87%
after
10.1
tools
9.2
tool
6.9
CRAP 7.0
« blib/lib/Langertha/Engine/DeepSeek.pm ↑ Summary blib/lib/Langertha/RunContext.pm »
1 package Langertha::Virtual::Rules;
2 # ABSTRACT: Helpers that build the rules of Langertha::Engine::Virtual
3 10 our $VERSION = '0.505';
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 our @EXPORT_OK = qw( answer tool tools after error );
10 10 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
11  
12  
13 # The optional arguments of a reaction helper: only usage, a HashRef.
14 168 ▶sub _reaction_options {
Subroutine _reaction_options: called CC=5 SCAR=16.1
15 168 my ( $helper, @options ) = @_;
16 168 ▶ croak __PACKAGE__."::$helper takes usage => \\%usage after its arguments" if @options % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @options % 2 0 168 168
17 168 my %options = @options;
18 168 my @unknown = grep { $_ ne 'usage' } sort keys %options;
19 168 ▶ croak __PACKAGE__."::$helper has unknown options: @unknown (known: usage)" if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 167 168
20 167 ▶ return () unless exists $options{usage};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"usage"} 148 19 167
21 19 ▶ croak __PACKAGE__."::$helper usage must be a HashRef" unless ref $options{usage} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options{"usage"} eq "HASH" 1 18 19
22 18 return ( usage => $options{usage} );
23 }
24  
25 83 ▶sub answer {
Subroutine answer: called CC=3 SCAR=11.0
Pod documented
26 83 my ( $text, @options ) = @_;
27 83 ▶ croak __PACKAGE__.'::answer needs a text' unless defined $text && !ref $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 1 82 83
Condition66%2/3
defined $text and not ref $text
!ll&&!rl&&r
1082
MC/DC50%1/2
defined $text and not ref $text
defined $text not ref $text
Truth table66%2/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
28 82 return { answer => $text, _reaction_options( answer => @options ) };
29 }
30  
31  
32 69 ▶sub tool {
Subroutine tool: called CC=6 SCAR=19.3
Pod documented
33 69 my ( $name, $arguments, @options ) = @_;
34 69 ▶ croak __PACKAGE__.'::tool needs a tool name' unless defined $name && !ref $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not ref $name and length $name 0 69 69
Condition33%2/6
defined $name and not ref $name and length $name
!ll&&!rl&&r
0069
defined $name and not ref $name
!ll&&!rl&&r
0069
MC/DC0%0/3
defined $name and not ref $name and length $name
defined $name not ref $name length $name
Truth table25%1/4
A and not(B) and C
A= defined $nameB= not ref $nameC= length $name
ABCresult
0XX0
10X0
1100
1111
35 69 ▶ $arguments //= {};
Condition100%2/2
$arguments //= {}
l!l
681
MC/DC100%1/1
$arguments //= {}
$arguments
Truth table100%2/2
A
A= $arguments
Aresult
11
00
36 69 ▶ croak __PACKAGE__.'::tool arguments must be a HashRef' unless ref $arguments eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arguments eq "HASH" 1 68 69
37 68 return { tool_calls => [ [ $name, $arguments ] ], _reaction_options( tool => @options ) };
38 }
39  
40  
41 21 ▶sub tools {
Subroutine tools: called CC=9 SCAR=22.2
Pod documented
42 21 my ( $calls, @options ) = @_;
43 21 ▶ croak __PACKAGE__.'::tools needs an ArrayRef of calls' unless ref $calls eq 'ARRAY' && @$calls;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $calls eq "ARRAY" and @$calls 0 21 21
Condition33%1/3
ref $calls eq "ARRAY" and @$calls
!ll&&!rl&&r
0021
MC/DC0%0/2
ref $calls eq "ARRAY" and @$calls
ref $calls eq "ARRAY" @$calls
Truth table33%1/3
A and B
A= ref $calls eq "ARRAY"B= @$calls
ABresult
0X0
100
111
44 21 my @tool_calls;
45 21 for my $call (@$calls) {
46 38 ▶ if ( ref $call eq 'ARRAY' && @$call <= 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $call eq 'ARRAY' and @$call <= 2) { } 34 4 38
elsif (ref $call eq 'HASH' and ref $$call{'tool_calls'} eq 'ARRAY') { } 2 2 4
Condition100%3/3
ref $call eq "ARRAY" and @$call <= 2
!ll&&!rl&&r
3134
MC/DC100%2/2
ref $call eq "ARRAY" and @$call <= 2
ref $call eq "ARRAY" @$call <= 2
Truth table100%3/3
A and B
A= ref $call eq "ARRAY"B= @$call <= 2
ABresult
0X0
100
111
47 34 push @tool_calls, @{ tool(@$call)->{tool_calls} };
48 }
49 ▶ elsif ( ref $call eq 'HASH' && ref $call->{tool_calls} eq 'ARRAY' ) {
Condition66%2/3
ref $call eq "HASH" and ref $$call{"tool_calls"} eq "ARRAY"
!ll&&!rl&&r
202
MC/DC50%1/2
ref $call eq "HASH" and ref $$call{"tool_calls"} eq "ARRAY"
ref $call eq "HASH" ref $$call{"tool_calls"} eq "ARRAY"
Truth table66%2/3
A and B
A= ref $call eq "HASH"B= ref $$call{"tool_calls"} eq "ARRAY"
ABresult
0X0
100
111
50 croak __PACKAGE__.'::tools takes the usage of the whole turn: give it to tools(...), not to a tool(...) in it'
51 2 ▶ if exists $call->{usage};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$call{"usage"} 1 1 2
52 1 push @tool_calls, @{ $call->{tool_calls} };
53 }
54 else {
55 2 croak __PACKAGE__.'::tools takes [ $name, \%arguments ] pairs or tool(...) reactions';
56 }
57 }
58 18 return { tool_calls => \@tool_calls, _reaction_options( tools => @options ) };
59 }
60  
61  
62 27 ▶sub error {
Subroutine error: called CC=6 SCAR=18.2
Pod documented
63 27 my ( $status, $message ) = @_;
64 27 ▶ croak __PACKAGE__.'::error needs an HTTP status from 400 to 599'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $status and not ref $status and $status =~ /\A[45]\d\d\z/ 2 25 27
Condition50%3/6
defined $status and not ref $status and $status =~ /\A[45]\d\d\z/
!ll&&!rl&&r
0225
defined $status and not ref $status
!ll&&!rl&&r
0027
MC/DC33%1/3
defined $status and not ref $status and $status =~ /\A[45]\d\d\z/
defined $status not ref $status $status =~ /\A[45]\d\d\z/
Truth table50%2/4
A and not(B) and C
A= defined $statusB= not ref $statusC= $status =~ /\A[45]\d\d\z/
ABCresult
0XX0
10X0
1100
1111
65 unless defined $status && !ref $status && $status =~ /\A[45]\d\d\z/;
66 25 ▶ croak __PACKAGE__.'::error needs a message' unless defined $message && !ref $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message and not ref $message 2 23 25
Condition100%3/3
defined $message and not ref $message
!ll&&!rl&&r
1123
MC/DC100%2/2
defined $message and not ref $message
defined $message not ref $message
Truth table100%3/3
A and not(B)
A= defined $messageB= not ref $message
ABresult
0X0
100
111
67 23 return { error => { status => 0 + $status, message => $message } };
68 }
69  
70  
71 10 my %AFTER_KEYS = map { $_ => 1 } qw( tool result );
72  
73 21 ▶sub after {
Subroutine after: called CC=10 SCAR=23.1
Pod documented
74 21 my ( @args ) = @_;
75 21 ▶ my %match = @args == 1 ? ( tool => $args[0] ) : @args % 2 ? () : @args;
Branch: 2 branches
BranchTrueFalseTotal
@args == 1 ? : 12 9 21
@args % 2 ? : 1 8 9
76 croak __PACKAGE__.'::after takes a tool name, or tool => ... and result => ...; '
77 .'it is a trigger: write after(...) => $reaction'
78 21 ▶ if ( @args > 1 && @args % 2 ) || grep { !$AFTER_KEYS{$_} } keys %match;
Branch: 1 branch
BranchTrueFalseTotal
if @args > 1 and @args % 2 or grep {not $AFTER_KEYS{$_};} keys %match 2 19 21
Condition100%6/6
@args > 1 and @args % 2 or grep {not $AFTER_KEYS{$_};} keys %match
l!l&&r!l&&!r
1119
@args > 1 and @args % 2
!ll&&!rl&&r
1641
MC/DC100%3/3
@args > 1 and @args % 2 or grep {not $AFTER_KEYS{$_};} keys %match
@args > 1 @args % 2 grep {not $AFTER_KEYS{$_};} keys %match
Truth table80%4/5
A and B or C
A= @args > 1B= @args % 2C= grep {not $AFTER_KEYS{$_};} keys %match
ABCresult
11X1
0X11
1011
0X00
1000
79 19 for my $key ( keys %match ) {
80 17 my $value = $match{$key};
81 17 ▶ croak __PACKAGE__."::after $key must be a string or a Regexp"
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and ref $value and ref $value ne "Regexp" 0 17 17
Condition66%4/6
defined $value and ref $value and ref $value ne "Regexp"
!ll&&!rl&&r
1520
defined $value and ref $value
!ll&&!rl&&r
0152
MC/DC0%0/3
defined $value and ref $value and ref $value ne "Regexp"
defined $value ref $value ref $value ne "Regexp"
Truth table50%2/4
A and B and C
A= defined $valueB= ref $valueC= ref $value ne "Regexp"
ABCresult
0XX0
10X0
1100
1111
82 if defined $value && ref $value && ref $value ne 'Regexp';
83 }
84 19 return { after => \%match };
85 }
86  
87  
88  
89 10 1;
90  
91 __END__
« blib/lib/Langertha/Engine/DeepSeek.pm ↑ Summary blib/lib/Langertha/RunContext.pm »