blib/lib/DWIM/Block.pm

filter: Statementstmt 71.4% 20 / 28 Branchbran 16.6% 1 / 6 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 85.7% 6 / 7 Podpod n/a totaltotal 49.0% 27 / 55 CC 9 SCAR 29.4
CC
9
Cov
48%
_DWIM
42.0
__ANON__
6.6
BEGIN
1.0
CRAP 18.9
↑ Summary
1 package DWIM::Block;
2  
3 2 ▶use 5.022;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Filter::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use PPR::X;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use AI::Chat;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Look for DWIM {...} blocks in any source that loads this module...
10 1 ▶FILTER {
Subroutine __ANON__: called CC=4 SCAR=18.9
11 1 my %dwim;
12  
13 # This is what code with embedded DWIM blocks looks like...
14 my $PERL_WITH_DWIM_BLOCKS = qr{
15 (?&PerlEntireDocument)
16  
17 (?(DEFINE)
18 (?<PerlControlBlock>
19 DWIM \b (?{ pos() - 4 }) (?>(?&PerlOWS))
20 (?<REQUEST> \{ (?>(?&PPR_X_balanced_curlies_interpolated)) \} )
21 (?{ $dwim{ $^R } = { block => 1, len => pos() - $^R, request => $+{REQUEST} } })
22 |
23 (?>(?&PerlStdControlBlock))
24 )
25  
26 (?<PerlCall>
27 DWIM \b (?{ pos() - 4 }) (?>(?&PerlOWS))
28 (?<REQUEST> \{ (?>(?&PPR_X_balanced_curlies_interpolated)) \} )
29 (?{ $dwim{ $^R } = { len => pos() - $^R, request => $+{REQUEST} } })
30 |
31 (?>(?&PerlStdCall))
32 )
33 )
34  
35 $PPR::X::GRAMMAR
36 1 }xms;
37  
38 # If we find DWIM blocks...
39 1 ▶ if (/$PERL_WITH_DWIM_BLOCKS/) {
Branch: 1 branch
BranchTrueFalseTotal
if () 1 0 1
40 # Work backwards from the end of the source (so the replacements don't mess up pos info)...
41 0 for my $pos (sort {$b <=> $a} keys %dwim) {
42  
43 # Generate the replacement code...
44 0 my $code = "do { DWIM::Block::_DWIM( qq$dwim{$pos}{request}) }";
45  
46 # Substitute the replacement code into the source...
47 0 ▶ substr($_, $pos, $dwim{$pos}{len}) = $dwim{$pos}{block} ? "{$code}" : $code;
Branch: 1 branch
BranchTrueFalseTotal
$dwim{$pos}{'block'} ? : 0 0 0
48 }
49 }
50 }
51  
52 2 our $VERSION = '0.000002';
53  
54 # This sub relays each request to the API and returns the result...
55 0 ▶sub _DWIM {
Subroutine _DWIM: not called CC=6 SCAR=37.4
56 0 my ($request) = @_;
57  
58 # Discover and remember the API key...
59 state $API_KEY = $ENV{'DWIM_API_KEY'}
60 0 ▶ // $ENV{'OPENAI_API_KEY'}
Condition0%0/5
($ENV{"DWIM_API_KEY"} // $ENV{"OPENAI_API_KEY"}) // die(sprintf("Can't find API key at %s line %d\n", (caller 0)[1, 2]))
l!l
00
$ENV{"DWIM_API_KEY"} // $ENV{"OPENAI_API_KEY"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($ENV{"DWIM_API_KEY"} // $ENV{"OPENAI_API_KEY"}) // die(sprintf("Can't find API key at %s line %d\n", (caller 0)[1, 2]))
$ENV{"DWIM_API_KEY"} $ENV{"OPENAI_API_KEY"}
Truth table0%0/3
A // B
A= $ENV{"DWIM_API_KEY"}B= $ENV{"OPENAI_API_KEY"}
ABresult
1X1
011
000
61 // die sprintf qq{Can't find API key at %s line %d\n}, (caller 0)[1,2];
62  
63 # Discover and remember the requested model...
64 state $MODEL = $ENV{'DWIM_MODEL'}
65 0 ▶ // $ENV{'OPENAI_MODEL'}
Condition0%0/5
($ENV{"DWIM_MODEL"} // $ENV{"OPENAI_MODEL"}) // ""
l!l
00
$ENV{"DWIM_MODEL"} // $ENV{"OPENAI_MODEL"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($ENV{"DWIM_MODEL"} // $ENV{"OPENAI_MODEL"}) // ""
$ENV{"DWIM_MODEL"} $ENV{"OPENAI_MODEL"}
Truth table0%0/3
A // B
A= $ENV{"DWIM_MODEL"}B= $ENV{"OPENAI_MODEL"}
ABresult
1X1
011
000
66 // q{};
67  
68 # Build and retain an API object...
69 0 ▶ state $GPT = AI::Chat->new( key => $API_KEY, $MODEL ? ( model => $MODEL ) : () );
Branch: 1 branch
BranchTrueFalseTotal
$MODEL ? : 0 0 0
70  
71 # Send the request and return the response...
72 0 return $GPT->prompt($request);
73 }
74  
75  
76 2 1; # Magic true value required at end of module
77 __END__
↑ Summary