blib/lib/Langertha/Role/ResponseFormat.pm

filter: Statementstmt 50.0% 29 / 58 Branchbran 27.2% 12 / 44 Conditioncond 50.0% 3 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 71.4% 5 / 7 Podpod 100% 2 / 2 totaltotal 43.8% 53 / 121 CC 25 SCAR 37.6
CC
25
Cov
41%
decode_loose_json
54.7
decode_loose_json
54.7
__ANON__
2.5
CRAP 42.7
« blib/lib/Langertha/ToolCall.pm ↑ Summary blib/lib/Langertha/Role/Tools.pm »
1 package Langertha::Role::ResponseFormat;
2 # ABSTRACT: Role for an engine where you can specify structured output
3 190 our $VERSION = '0.505';
4 190 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 190 ▶use JSON::MaybeXS qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 190 ▶use Encode qw( encode_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 190 has response_format => (
9 isa => 'HashRef',
10 is => 'ro',
11 predicate => 'has_response_format',
12 );
13  
14  
15 29 ▶sub decode_loose_json {
Subroutine decode_loose_json: called CC=12 SCAR=40.0
Pod documented
16 0 ▶ my ( $self, $text ) = @_;
Subroutine decode_loose_json: not called CC=12 SCAR=40.0
Pod documented
17 0 ▶ return undef unless defined $text && length $text;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $text and length $text 2 27 29
unless defined $text and length $text 0 0 0
Condition50%3/6
defined $text and length $text
!ll&&!rl&&r
1127
defined $text and length $text
!ll&&!rl&&r
000
MC/DC100%2/2
defined $text and length $text
defined $text length $text
MC/DC0%0/2
defined $text and length $text
defined $text length $text
Truth table100%3/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
18  
19 30 ▶ my $try = sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
20 0 ▶ my ($s) = @_;
Subroutine __ANON__: not called CC=2 SCAR=9.2
21 # decode_json (the JSON::MaybeXS utf8 variant) expects bytes, but $s reaches
22 # us as a Perl-Unicode string (already-decoded response content). Decoding it
23 # directly dies with "Wide character in subroutine entry" on any non-ASCII
24 # byte, so UTF-8-encode first -- same convention as ToolCall::_args_kwargs
25 # and Role::JSON's decode_json_text. All three strategies route through this
26 # closure, so the fence/substring candidates are encoded here too.
27 0 my $r = eval { decode_json( encode_utf8($s) ) };
28 0 ▶ return $@ ? undef : $r;
Branch: 2 branches
BranchTrueFalseTotal
$@ ? : 6 24 30
$@ ? : 0 0 0
29 0 };
30  
31 0 ▶ if ( my $r = $try->($text) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $r = &$try($text)) 21 6 27
if (my $r = &$try($text)) 0 0 0
32 0 return $r;
33 }
34 0 ▶ if ( $text =~ /```(?:json)?\s*(.*?)\s*```/s ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ /```(?:json)?\s*(.*?)\s*```/s) 2 4 6
if ($text =~ /```(?:json)?\s*(.*?)\s*```/s) 0 0 0
35 0 ▶ if ( my $r = $try->($1) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $r = &$try($1)) 2 0 2
if (my $r = &$try($1)) 0 0 0
36 0 return $r;
37 }
38 }
39 0 ▶ if ( $text =~ /(\{.*\})/s ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ /(\{.*\})/s) 1 3 4
if ($text =~ /(\{.*\})/s) 0 0 0
40 0 my $candidate = $1;
41 0 ▶ if ( my $r = $try->($candidate) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $r = &$try($candidate)) 1 0 1
if (my $r = &$try($candidate)) 0 0 0
42 0 return $r;
43 }
44 0 ▶ while ( length $candidate > 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $candidate > 2) 0 0 0
if (length $candidate > 2) 0 0 0
45 0 my $before = length $candidate;
46 0 ▶ $candidate =~ s/\}[^}]*$/\}/ or last;
Branch: 2 branches
BranchTrueFalseTotal
$candidate =~ s/\}[^}]*$/}/ or last 0 0 0
$candidate =~ s/\}[^}]*$/}/ or last 0 0 0
47 # The substitution collapses trailing junk after the final '}' into that
48 # '}'. When the candidate already ends at '}' (which the greedy capture
49 # above guarantees), it is a no-op that still reports a successful
50 # substitution, so `or last` never fires -- an unbalanced candidate such
51 # as '{{"a":1}' would spin here forever and wedge the async event loop.
52 # Bail the moment the length stops shrinking: the method must terminate
53 # (returning undef), never hang. -- karr k161
54 0 ▶ last unless length $candidate < $before;
Branch: 2 branches
BranchTrueFalseTotal
unless length $candidate < $before 0 0 0
unless length $candidate < $before 0 0 0
55 0 ▶ if ( my $r = $try->($candidate) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $r = &$try($candidate)) 0 0 0
if (my $r = &$try($candidate)) 0 0 0
56 0 return $r;
57 }
58 }
59 }
60 0 return undef;
61 }
62  
63  
64  
65 190 1;
66  
67 __END__
« blib/lib/Langertha/ToolCall.pm ↑ Summary blib/lib/Langertha/Role/Tools.pm »