| 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
Condition50%3/6
defined $text and length $text
defined $text and length $text
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
Truth table0%0/3
A and B
A= defined $textB= length $text
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ( my $r = $try->($text) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | return $r; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if ( $text =~ /```(?:json)?\s*(.*?)\s*```/s ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if ( my $r = $try->($1) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | return $r; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | if ( $text =~ /(\{.*\})/s ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my $candidate = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | if ( my $r = $try->($candidate) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | return $r; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | while ( length $candidate > 2 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | my $before = length $candidate; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | $candidate =~ s/\}[^}]*$/\}/ or last; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ( my $r = $try->($candidate) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | return $r; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 190 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||