blib/lib/Langertha/Role/HTTP.pm

filter: Statementstmt 68.7% 191 / 278 Branchbran 55.8% 86 / 154 Conditioncond 37.4% 58 / 155 MC/DCmcdc 28.0% 23 / 82 Subroutinesub 72.7% 32 / 44 Podpod 100% 8 / 8 totaltotal 55.2% 398 / 721 CC 163 SCAR 28.6
CC
163
Cov
57%
generate_http_request
50.4
generate_http_request
50.4
generate_multipart_body
26.6
CRAP 17.4
« blib/lib/Langertha/Role/ResponsesCompatible.pm ↑ Summary blib/lib/Langertha/Engine/Ollama.pm »
1 package Langertha::Role::HTTP;
2 # ABSTRACT: Role for HTTP APIs
3 194 our $VERSION = '0.505';
4 194 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 194 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 194 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 194 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 194 ▶use Time::HiRes qw( gettimeofday tv_interval );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 194 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 194 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 194 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 194 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 194 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 194 ▶use Langertha::Request::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 194 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 194 requires qw(
20 json
21 );
22  
23 194 has url => (
24 is => 'ro',
25 isa => 'Str',
26 predicate => 'has_url',
27 );
28  
29  
30 has connect_address => (
31 is => 'ro',
32 isa => 'Maybe[Str]',
33 62 ▶ trigger => sub { $_[0]->_check_connect_address },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 194 );
35  
36  
37 # The host connect_address pins (lower-cased), or undef without a pin.
38 1413 ▶sub _connect_host {
Subroutine _connect_host: called CC=6 SCAR=27.9
39 0 ▶ my ($self) = @_;
Subroutine _connect_host: not called CC=6 SCAR=27.9
40 0 ▶ return undef unless defined $self->connect_address;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->connect_address 1229 184 1413
unless defined $self->connect_address 0 0 0
41 # The accessor, not has_url: many engines build a default url lazily.
42 0 my $url = $self->url;
43 0 ▶ return undef unless defined $url;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $url 0 184 184
unless defined $url 0 0 0
44 0 my $uri = URI->new($url);
45 0 ▶ return undef unless $uri->can('host') && defined $uri->host && length $uri->host;
Branch: 2 branches
BranchTrueFalseTotal
unless $uri->can("host") and defined $uri->host and length $uri->host 0 184 184
unless $uri->can("host") and defined $uri->host and length $uri->host 0 0 0
Condition16%2/12
$uri->can("host") and defined $uri->host and length $uri->host
!ll&&!rl&&r
00184
$uri->can("host") and defined $uri->host
!ll&&!rl&&r
00184
$uri->can("host") and defined $uri->host and length $uri->host
!ll&&!rl&&r
000
$uri->can("host") and defined $uri->host
!ll&&!rl&&r
000
MC/DC0%0/3
$uri->can("host") and defined $uri->host and length $uri->host
$uri->can("host") defined $uri->host length $uri->host
MC/DC0%0/3
$uri->can("host") and defined $uri->host and length $uri->host
$uri->can("host") defined $uri->host length $uri->host
Truth table25%1/4
A and B and C
A= $uri->can("host")B= defined $uri->hostC= length $uri->host
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= $uri->can("host")B= defined $uri->hostC= length $uri->host
ABCresult
0XX0
10X0
1100
1111
46 0 return lc $uri->host;
47 }
48  
49 # ( connect_address => ... ) for an engine derived from this one that will use
50 # $url, when $url is on the pinned host; else nothing (karr k375).
51 15 ▶sub _connect_address_for {
Subroutine _connect_address_for: called CC=6 SCAR=25.8
52 0 ▶ my ( $self, $url ) = @_;
Subroutine _connect_address_for: not called CC=6 SCAR=25.8
53 0 my $host = $self->_connect_host;
54 0 ▶ return () unless defined $host && defined $url;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $host and defined $url 10 5 15
unless defined $host and defined $url 0 0 0
Condition33%2/6
defined $host and defined $url
!ll&&!rl&&r
1005
defined $host and defined $url
!ll&&!rl&&r
000
MC/DC50%1/2
defined $host and defined $url
defined $host defined $url
MC/DC0%0/2
defined $host and defined $url
defined $host defined $url
Truth table66%2/3
A and B
A= defined $hostB= defined $url
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $hostB= defined $url
ABresult
0X0
100
111
55 0 my $uri = URI->new("$url");
56 0 ▶ return () unless $uri->can('host') && lc( $uri->host // '' ) eq $host;
Branch: 2 branches
BranchTrueFalseTotal
unless $uri->can("host") and lc($uri->host // "") eq $host 1 4 5
unless $uri->can("host") and lc($uri->host // "") eq $host 0 0 0
Condition30%3/10
$uri->can("host") and lc($uri->host // "") eq $host
!ll&&!rl&&r
014
$uri->host // ""
l!l
50
$uri->can("host") and lc($uri->host // "") eq $host
!ll&&!rl&&r
000
$uri->host // ""
l!l
00
MC/DC0%0/2
$uri->can("host") and lc($uri->host // "") eq $host
$uri->can("host") $uri->host
MC/DC0%0/2
$uri->can("host") and lc($uri->host // "") eq $host
$uri->can("host") $uri->host
Truth table33%1/3
A and B
A= $uri->can("host")B= $uri->host
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $uri->can("host")B= $uri->host
ABresult
0X0
100
111
57 0 return ( connect_address => $self->connect_address );
58 }
59  
60 # Runs once, after construction set every attribute (Moose triggers do).
61 62 ▶sub _check_connect_address {
Subroutine _check_connect_address: called CC=8 SCAR=30.1
62 0 ▶ my ($self) = @_;
Subroutine _check_connect_address: not called CC=8 SCAR=30.1
63 0 my $address = $self->connect_address;
64 0 ▶ return unless defined $address;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $address 1 61 62
unless defined $address 0 0 0
65 0 require Langertha::HTTP::UserAgent;
66 0 my $error = Langertha::HTTP::UserAgent::connect_address_error($address);
67 0 ▶ croak "".(ref $self).": $error" if $error;
Branch: 2 branches
BranchTrueFalseTotal
if $error 1 60 61
if $error 0 0 0
68 0 ▶ croak "".(ref $self).": connect_address needs a url with an http or https host to pin"
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->_connect_host and ("URI"->new($self->url)->scheme // "") =~ /\Ahttps?\z/i 0 60 60
unless defined $self->_connect_host and ("URI"->new($self->url)->scheme // "") =~ /\Ahttps?\z/i 0 0 0
Condition20%2/10
defined $self->_connect_host and ("URI"->new($self->url)->scheme // "") =~ /\Ahttps?\z/i
!ll&&!rl&&r
0060
"URI"->new($self->url)->scheme // ""
l!l
600
defined $self->_connect_host and ("URI"->new($self->url)->scheme // "") =~ /\Ahttps?\z/i
!ll&&!rl&&r
000
"URI"->new($self->url)->scheme // ""
l!l
00
MC/DC0%0/2
defined $self->_connect_host and ("URI"->new($self->url)->scheme // "") =~ /\Ahttps?\z/i
defined $self->_connect_host "URI"->new($self->url)->scheme
MC/DC0%0/2
defined $self->_connect_host and ("URI"->new($self->url)->scheme // "") =~ /\Ahttps?\z/i
defined $self->_connect_host "URI"->new($self->url)->scheme
Truth table33%1/3
A and B
A= defined $self->_connect_hostB= "URI"->new($self->url)->scheme
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $self->_connect_hostB= "URI"->new($self->url)->scheme
ABresult
0X0
100
111
69 unless defined $self->_connect_host && ( URI->new( $self->url )->scheme // '' ) =~ /\Ahttps?\z/i;
70 0 ▶ if ( $self->has_user_agent and my $why = $self->_connect_pin_error( $self->user_agent ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_user_agent and my $why = $self->_connect_pin_error($self->user_agent)) 3 57 60
if ($self->has_user_agent and my $why = $self->_connect_pin_error($self->user_agent)) 0 0 0
Condition50%3/6
$self->has_user_agent and my $why = $self->_connect_pin_error($self->user_agent)
!ll&&!rl&&r
45123
$self->has_user_agent and my $why = $self->_connect_pin_error($self->user_agent)
!ll&&!rl&&r
000
MC/DC100%2/2
$self->has_user_agent and my $why = $self->_connect_pin_error($self->user_agent)
$self->has_user_agent my $why = $self->_connect_pin_error($self->user_agent)
MC/DC0%0/2
$self->has_user_agent and my $why = $self->_connect_pin_error($self->user_agent)
$self->has_user_agent my $why = $self->_connect_pin_error($self->user_agent)
Truth table100%3/3
A and B
A= $self->has_user_agentB= my $why = $self->_connect_pin_error($self->user_agent)
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $self->has_user_agentB= my $why = $self->_connect_pin_error($self->user_agent)
ABresult
0X0
100
111
71 0 croak "".(ref $self).": $why";
72 }
73 0 return;
74 }
75  
76 # Why $ua would not pin this engine's requests, or undef when it does.
77 26 ▶sub _connect_pin_error {
Subroutine _connect_pin_error: called CC=9 SCAR=32.5
78 0 ▶ my ( $self, $ua ) = @_;
Subroutine _connect_pin_error: not called CC=9 SCAR=32.5
79 0 my $host = $self->_connect_host;
80 0 my $address = $self->connect_address;
81 0 ▶ my $pinned = Scalar::Util::blessed($ua) && $ua->isa('Langertha::HTTP::UserAgent')
Condition40%12/30
builtin::blessed($ua) && $ua->isa('Langertha::HTTP::UserAgent') && defined $ua->connect_address && defined $ua->connect_host && lc $ua->connect_host eq $host && Langertha::HTTP::UserAgent::same_address($ua->connect_address, $address)
!ll&&!rl&&r
4022
builtin::blessed($ua) and $ua->isa("Langertha::HTTP::UserAgent") and defined $ua->connect_address and defined $ua->connect_host and lc $ua->connect_host eq $host
!ll&&!rl&&r
3122
builtin::blessed($ua) and $ua->isa("Langertha::HTTP::UserAgent") and defined $ua->connect_address and defined $ua->connect_host
!ll&&!rl&&r
3023
builtin::blessed($ua) and $ua->isa("Langertha::HTTP::UserAgent") and defined $ua->connect_address
!ll&&!rl&&r
2123
builtin::blessed($ua) and $ua->isa("Langertha::HTTP::UserAgent")
!ll&&!rl&&r
0224
builtin::blessed($ua) && $ua->isa('Langertha::HTTP::UserAgent') && defined $ua->connect_address && defined $ua->connect_host && lc $ua->connect_host eq $host && Langertha::HTTP::UserAgent::same_address($ua->connect_address, $address)
!ll&&!rl&&r
000
builtin::blessed($ua) and $ua->isa("Langertha::HTTP::UserAgent") and defined $ua->connect_address and defined $ua->connect_host and lc $ua->connect_host eq $host
!ll&&!rl&&r
000
builtin::blessed($ua) and $ua->isa("Langertha::HTTP::UserAgent") and defined $ua->connect_address and defined $ua->connect_host
!ll&&!rl&&r
000
builtin::blessed($ua) and $ua->isa("Langertha::HTTP::UserAgent") and defined $ua->connect_address
!ll&&!rl&&r
000
builtin::blessed($ua) and $ua->isa("Langertha::HTTP::UserAgent")
!ll&&!rl&&r
000
MC/DC50%3/6
builtin::blessed($ua) && $ua->isa('Langertha::HTTP::UserAgent') && defined $ua->connect_address && defined $ua->connect_host && lc $ua->connect_host eq $host && Langertha::HTTP::UserAgent::same_address($ua->connect_address, $address)
builtin::blessed($ua) $ua->isa("Langertha::HTTP::UserAgent") defined $ua->connect_address defined $ua->connect_host lc $ua->connect_host eq $host Langertha::HTTP::UserAgent::same_address($ua->connect_address, $address)
MC/DC0%0/6
builtin::blessed($ua) && $ua->isa('Langertha::HTTP::UserAgent') && defined $ua->connect_address && defined $ua->connect_host && lc $ua->connect_host eq $host && Langertha::HTTP::UserAgent::same_address($ua->connect_address, $address)
builtin::blessed($ua) $ua->isa("Langertha::HTTP::UserAgent") defined $ua->connect_address defined $ua->connect_host lc $ua->connect_host eq $host Langertha::HTTP::UserAgent::same_address($ua->connect_address, $address)
Truth table57%4/7
A and B and C and D and E && F
A= builtin::blessed($ua)B= $ua->isa("Langertha::HTTP::UserAgent")C= defined $ua->connect_addressD= defined $ua->connect_hostE= lc $ua->connect_host eq $hostF= Langertha::HTTP::UserAgent::same_address($ua->connect_address, $address)
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
Truth table0%0/7
A and B and C and D and E && F
A= builtin::blessed($ua)B= $ua->isa("Langertha::HTTP::UserAgent")C= defined $ua->connect_addressD= defined $ua->connect_hostE= lc $ua->connect_host eq $hostF= Langertha::HTTP::UserAgent::same_address($ua->connect_address, $address)
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
82 && defined $ua->connect_address && defined $ua->connect_host
83 && lc $ua->connect_host eq $host
84 && Langertha::HTTP::UserAgent::same_address( $ua->connect_address, $address );
85 0 ▶ return undef if $pinned;
Branch: 2 branches
BranchTrueFalseTotal
if $pinned 22 4 26
if $pinned 0 0 0
86 0 ▶ return "connect_address $address cannot be applied through the user_agent passed in ("
Condition25%2/8
(builtin::blessed($ua) // ref $ua) || 'not an object'
l!l
40
builtin::blessed($ua) // ref $ua
l!l
40
(builtin::blessed($ua) // ref $ua) || 'not an object'
l!l
00
builtin::blessed($ua) // ref $ua
l!l
00
MC/DC0%0/1
(builtin::blessed($ua) // ref $ua) || 'not an object'
builtin::blessed($ua)
MC/DC0%0/1
(builtin::blessed($ua) // ref $ua) || 'not an object'
builtin::blessed($ua)
Truth table50%1/2
A
A= builtin::blessed($ua)
Aresult
11
00
Truth table0%0/2
A
A= builtin::blessed($ua)
Aresult
11
00
87 . ( Scalar::Util::blessed($ua) // ref($ua) || 'not an object' ) . '); pass a Langertha::HTTP::UserAgent built with'
88 . " connect_host => '$host', connect_address => '$address', or let the engine build its user_agent";
89 }
90  
91 194 has response_max_bytes => (
92 is => 'ro',
93 isa => 'Int',
94 default => 268_435_456, # 256 MiB
95 );
96  
97  
98 3428 ▶sub generate_json_body {
Subroutine generate_json_body: called CC=1 SCAR=1.2
Pod documented
99 0 ▶ my ( $self, %args ) = @_;
Subroutine generate_json_body: not called CC=1 SCAR=1.2
Pod documented
100 0 return $self->json->encode({ %args });
101 }
102  
103  
104 194 our $boundary = 'XyXLaXyXngXyXerXyXthXyXaXyX';
105  
106 # Character strings go on the wire as UTF-8, like the JSON body (json->utf8).
107 115 ▶sub _multipart_text {
Subroutine _multipart_text: called CC=2 SCAR=10.5
108 0 ▶ my ( $value ) = @_;
Subroutine _multipart_text: not called CC=2 SCAR=10.5
109 0 ▶ return $value unless defined $value;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $value 0 115 115
unless defined $value 0 0 0
110 0 return Encode::encode( 'UTF-8', "$value" );
111 }
112  
113 # A filename is path-like: a decoded (UTF-8 flagged) name is encoded, an
114 # undecoded one is already the filesystem's bytes and passes unchanged.
115 # RFC 7578 section 4.2: the name goes into filename="..." as raw UTF-8 (what
116 # browsers and the OpenAI SDKs send), never as filename*.
117 64 ▶sub _multipart_filename {
Subroutine _multipart_filename: called CC=3 SCAR=15.3
118 0 ▶ my ( $name ) = @_;
Subroutine _multipart_filename: not called CC=3 SCAR=15.3
119 0 ▶ return $name unless defined $name && utf8::is_utf8($name);
Branch: 2 branches
BranchTrueFalseTotal
unless defined $name and utf8::is_utf8($name) 63 1 64
unless defined $name and utf8::is_utf8($name) 0 0 0
Condition33%2/6
defined $name and utf8::is_utf8($name)
!ll&&!rl&&r
0631
defined $name and utf8::is_utf8($name)
!ll&&!rl&&r
000
MC/DC50%1/2
defined $name and utf8::is_utf8($name)
defined $name utf8::is_utf8($name)
MC/DC0%0/2
defined $name and utf8::is_utf8($name)
defined $name utf8::is_utf8($name)
Truth table66%2/3
A and B
A= defined $nameB= utf8::is_utf8($name)
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $nameB= utf8::is_utf8($name)
ABresult
0X0
100
111
120 0 return Encode::encode( 'UTF-8', $name );
121 }
122  
123 65 ▶sub generate_multipart_body {
Subroutine generate_multipart_body: called CC=10 SCAR=32.8
Pod documented
124 0 ▶ my ( $self, $req, %args ) = @_;
Subroutine generate_multipart_body: not called CC=10 SCAR=32.8
Pod documented
125 0 my @formdata;
126 0 for my $key ( sort { $a cmp $b } keys %args ) {
127 0 my $value = $args{$key};
128 0 ▶ if ( ref $value eq 'ARRAY' && $key =~ /\[\]\z/ ) {
Branch: 8 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY' and $key =~ /\[\]\z/) { } 21 148 169
elsif (ref $value eq 'HASH' and ref $value->{'repeated'} eq 'ARRAY') { } 5 143 148
elsif (ref $value eq 'ARRAY') { } 64 79 143
elsif (ref $value) { } 0 79 79
if (ref $value eq 'ARRAY' and $key =~ /\[\]\z/) { } 0 0 0
elsif (ref $value eq 'HASH' and ref $value->{'repeated'} eq 'ARRAY') { } 0 0 0
elsif (ref $value eq 'ARRAY') { } 0 0 0
elsif (ref $value) { } 0 0 0
Condition50%3/6
ref $value eq "ARRAY" and $key =~ /\[\]\z/
!ll&&!rl&&r
846421
ref $value eq "ARRAY" and $key =~ /\[\]\z/
!ll&&!rl&&r
000
MC/DC100%2/2
ref $value eq "ARRAY" and $key =~ /\[\]\z/
ref $value eq "ARRAY" $key =~ /\[\]\z/
MC/DC0%0/2
ref $value eq "ARRAY" and $key =~ /\[\]\z/
ref $value eq "ARRAY" $key =~ /\[\]\z/
Truth table100%3/3
A and B
A= ref $value eq "ARRAY"B= $key =~ /\[\]\z/
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= ref $value eq "ARRAY"B= $key =~ /\[\]\z/
ABresult
0X0
100
111
129 # Multi-valued field (OpenAI: timestamp_granularities[], include[]):
130 # one part per element, in order. -- karr k286
131 0 push @formdata, map { ( $key, _multipart_text($_) ) } @$value;
132 }
133 ▶ elsif ( ref $value eq 'HASH' && ref $value->{repeated} eq 'ARRAY' ) {
Condition33%2/6
ref $value eq "HASH" and ref $value->{"repeated"} eq "ARRAY"
!ll&&!rl&&r
14305
ref $value eq "HASH" and ref $value->{"repeated"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC50%1/2
ref $value eq "HASH" and ref $value->{"repeated"} eq "ARRAY"
ref $value eq "HASH" ref $value->{"repeated"} eq "ARRAY"
MC/DC0%0/2
ref $value eq "HASH" and ref $value->{"repeated"} eq "ARRAY"
ref $value eq "HASH" ref $value->{"repeated"} eq "ARRAY"
Truth table66%2/3
A and B
A= ref $value eq "HASH"B= ref $value->{"repeated"} eq "ARRAY"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= ref $value eq "HASH"B= ref $value->{"repeated"} eq "ARRAY"
ABresult
0X0
100
111
134 # Multi-valued field under the key as given, no [] (Mistral:
135 # timestamp_granularities, context_bias). Explicit marker, because a
136 # plain-key ArrayRef is a file spec. -- karr k315
137 0 push @formdata, map { ( $key, _multipart_text($_) ) } @{ $value->{repeated} };
138 }
139 elsif ( ref $value eq 'ARRAY' ) {
140 # File spec, HTTP::Request::Common form_data convention:
141 # [ $path, $filename, @headers ] or [ undef, $filename, Content => $bytes ].
142 0 my ( $file, $filename, @headers ) = @$value;
143 0 ▶ $filename = ( File::Spec->splitpath("$file") )[-1]
Branch: 2 branches
BranchTrueFalseTotal
if not defined $filename and defined $file 33 31 64
if not defined $filename and defined $file 0 0 0
Condition33%2/6
not defined $filename and defined $file
!ll&&!rl&&r
31033
not defined $filename and defined $file
!ll&&!rl&&r
000
MC/DC50%1/2
not defined $filename and defined $file
not defined $filename defined $file
MC/DC0%0/2
not defined $filename and defined $file
not defined $filename defined $file
Truth table66%2/3
not(A) and B
A= not defined $filenameB= defined $file
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not defined $filenameB= defined $file
ABresult
0X0
100
111
144 if !defined $filename && defined $file;
145 0 push @formdata, $key, [ $file, _multipart_filename($filename), @headers ];
146 }
147 elsif ( ref $value ) {
148 0 push @formdata, $key, $value;
149 }
150 else {
151 0 push @formdata, $key, _multipart_text($value);
152 }
153 }
154 0 return HTTP::Request::Common::form_data(\@formdata, $boundary, $req);
155 }
156  
157  
158 3678 ▶sub generate_http_request {
Subroutine generate_http_request: called CC=15 SCAR=39.2
Pod documented
159 0 ▶ my ( $self, $method, $url, $response_call, %args ) = @_;
Subroutine generate_http_request: not called CC=15 SCAR=39.2
Pod documented
160 0 my $uri = URI->new($url);
161 0 ▶ my $content_type = (delete $args{content_type}||"");
Condition50%2/4
delete $args{'content_type'} || ''
l!l
24471231
delete $args{'content_type'} || ''
l!l
00
MC/DC100%1/1
delete $args{'content_type'} || ''
delete $args{'content_type'}
MC/DC0%0/1
delete $args{'content_type'} || ''
delete $args{'content_type'}
Truth table100%2/2
A
A= delete $args{'content_type'}
Aresult
11
00
Truth table0%0/2
A
A= delete $args{'content_type'}
Aresult
11
00
162 0 my $userinfo = $uri->userinfo;
163 0 ▶ $uri->userinfo(undef) if defined $userinfo;
Branch: 2 branches
BranchTrueFalseTotal
if defined $userinfo 15 3663 3678
if defined $userinfo 0 0 0
164 0 ▶ my $headers = [
Branch: 2 branches
BranchTrueFalseTotal
$content_type eq 'multipart/form-data' ? : 63 3615 3678
$content_type eq 'multipart/form-data' ? : 0 0 0
165 # multipart gets its Content-Type below, from the boundary the body used
166 $content_type eq 'multipart/form-data' ? ()
167 : ( 'Content-Type', 'application/json; charset=utf-8' )
168 ];
169 0 ▶ my $request = Langertha::Request::HTTP->new(
Branch: 4 branches
BranchTrueFalseTotal
scalar %args > 0 ? : 3491 187 3678
!$content_type || $content_type eq 'application/json' ? : 3428 63 3491
scalar %args > 0 ? : 0 0 0
!$content_type || $content_type eq 'application/json' ? : 0 0 0
Condition50%3/6
not $content_type or $content_type eq "application/json"
l!l&&r!l&&!r
1044238463
not $content_type or $content_type eq "application/json"
l!l&&r!l&&!r
000
MC/DC100%2/2
not $content_type or $content_type eq "application/json"
not $content_type $content_type eq "application/json"
MC/DC0%0/2
not $content_type or $content_type eq "application/json"
not $content_type $content_type eq "application/json"
Truth table100%3/3
not(A) or B
A= not $content_typeB= $content_type eq "application/json"
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not $content_typeB= $content_type eq "application/json"
ABresult
1X1
011
000
170 http => [ uc($method), $uri, $headers, ( scalar %args > 0 ?
171 ( !$content_type or $content_type eq 'application/json' )
172 ? $self->generate_json_body(%args)
173 : ()
174 : ()
175 ) ],
176 request_source => $self,
177 response_call => $response_call,
178 );
179 0 ▶ if ($content_type and $content_type eq 'multipart/form-data') {
Branch: 2 branches
BranchTrueFalseTotal
if ($content_type and $content_type eq "multipart/form-data") 63 3615 3678
if ($content_type and $content_type eq "multipart/form-data") 0 0 0
Condition50%3/6
$content_type and $content_type eq "multipart/form-data"
!ll&&!rl&&r
1231238463
$content_type and $content_type eq "multipart/form-data"
!ll&&!rl&&r
000
MC/DC100%2/2
$content_type and $content_type eq "multipart/form-data"
$content_type $content_type eq "multipart/form-data"
MC/DC0%0/2
$content_type and $content_type eq "multipart/form-data"
$content_type $content_type eq "multipart/form-data"
Truth table100%3/3
A and B
A= $content_typeB= $content_type eq "multipart/form-data"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $content_typeB= $content_type eq "multipart/form-data"
ABresult
0X0
100
111
180 0 my ( $body, $used_boundary ) = $self->generate_multipart_body($request, %args);
181 0 $request->header( 'Content-Type' => 'multipart/form-data; boundary="'.$used_boundary.'"' );
182 0 $request->content($body);
183 }
184 # Userinfo is percent-encoded in the URL and the password may hold a colon:
185 # split once, decode both parts, test for presence not truth ("0" is a valid
186 # user or password). -- karr k377
187 0 ▶ if ( defined $userinfo && length $userinfo ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $userinfo and length $userinfo) 15 3663 3678
if (defined $userinfo and length $userinfo) 0 0 0
Condition33%2/6
defined $userinfo and length $userinfo
!ll&&!rl&&r
3663015
defined $userinfo and length $userinfo
!ll&&!rl&&r
000
MC/DC50%1/2
defined $userinfo and length $userinfo
defined $userinfo length $userinfo
MC/DC0%0/2
defined $userinfo and length $userinfo
defined $userinfo length $userinfo
Truth table66%2/3
A and B
A= defined $userinfoB= length $userinfo
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $userinfoB= length $userinfo
ABresult
0X0
100
111
188 0 my ( $user, $pass ) = split( /:/, $userinfo, 2 );
189 0 ▶ if ( defined $user && length $user && defined $pass ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $user and length $user and defined $pass) 14 1 15
if (defined $user and length $user and defined $pass) 0 0 0
Condition25%3/12
defined $user and length $user and defined $pass
!ll&&!rl&&r
0114
defined $user and length $user
!ll&&!rl&&r
0015
defined $user and length $user and defined $pass
!ll&&!rl&&r
000
defined $user and length $user
!ll&&!rl&&r
000
MC/DC33%1/3
defined $user and length $user and defined $pass
defined $user length $user defined $pass
MC/DC0%0/3
defined $user and length $user and defined $pass
defined $user length $user defined $pass
Truth table50%2/4
A and B and C
A= defined $userB= length $userC= defined $pass
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= defined $userB= length $userC= defined $pass
ABCresult
0XX0
10X0
1100
1111
190 0 $request->authorization_basic( URI::Escape::uri_unescape($user), URI::Escape::uri_unescape($pass) );
191 }
192 }
193 0 ▶ $self->update_request($request) if $self->can('update_request');
Branch: 2 branches
BranchTrueFalseTotal
if $self->can("update_request") 3660 18 3678
if $self->can("update_request") 0 0 0
194 0 return $request;
195 }
196  
197  
198 194 our $error_body_max_length = 500;
199  
200 # A response body with its Content-Encoding undone, but bounded (karr k346,
201 # reusing the k342 inflate path): decoded_content inflates a Content-Encoding
202 # with no size limit, so a hostile or broken endpoint could inflate a tiny
203 # compressed body to gigabytes in memory. Inflate in bounded blocks through the
204 # shared Langertha::HTTP::BoundedDecode and refuse a body whose decoded size
205 # passes response_max_bytes; then run the charset step the same as
206 # decoded_content, on the already-bounded bytes (a throwaway response carries
207 # them without a Content-Encoding, so no second inflate). Any %opt
208 # (default_charset => ...) reaches that charset step. response_max_bytes => 0
209 # removes the cap and decodes as decoded_content did. An uncompressed body just
210 # passes through the inflate and gets the charset step.
211 158 ▶sub _bounded_decoded_content {
Subroutine _bounded_decoded_content: called CC=3 SCAR=14.4
212 0 ▶ my ( $self, $response, %opt ) = @_;
Subroutine _bounded_decoded_content: not called CC=3 SCAR=14.4
213 0 my $max = $self->response_max_bytes;
214 0 ▶ return $response->decoded_content(%opt) unless $max;
Branch: 2 branches
BranchTrueFalseTotal
unless $max 1 157 158
unless $max 0 0 0
215 0 require Langertha::HTTP::BoundedDecode;
216 0 require HTTP::Response;
217 my $bytes = Langertha::HTTP::BoundedDecode::decode_within(
218 $response->content, scalar $response->header('Content-Encoding'), $max, {
219 4 ▶ too_big => sub { croak "".(ref $self)." response body exceeds response_max_bytes ($_[0])" },
Subroutine __ANON__: called CC=1 SCAR=0.0
220 0 ▶ undecodable => sub { croak "".(ref $self)." response body Content-Encoding '$_[0]' cannot be decoded" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
221 # An encoding this bounded decoder cannot inflate in blocks (br, zstd, ...)
222 # is refused, not passed to decoded_content: falling through to the
223 # unbounded decode would reopen the bomb for that encoding. This is a
224 # deliberate safety choice -- such a body IS decodable by HTTP::Message,
225 # it just cannot be bounded here (karr k346).
226 0 ▶ unbounded_encoding => sub { croak "".(ref $self)." response body Content-Encoding '$_[0]'"
Subroutine __ANON__: not called CC=1 SCAR=6.9
227 . " cannot be decoded within response_max_bytes ($max)" },
228 0 } );
229 0 my $decoded = HTTP::Response->new(200);
230 0 my $type = $response->header('Content-Type');
231 0 ▶ $decoded->header( 'Content-Type' => $type ) if defined $type;
Branch: 2 branches
BranchTrueFalseTotal
if defined $type 138 15 153
if defined $type 0 0 0
232 0 $decoded->content_ref( \$bytes );
233 0 return $decoded->decoded_content(%opt);
234 }
235  
236  
237 126 ▶sub _error_response_body {
Subroutine _error_response_body: called CC=7 SCAR=19.6
238 126 my ( $self, $response ) = @_;
239 126 my $body = eval { $self->_bounded_decoded_content($response) };
240 126 ▶ $body = $response->content unless defined $body && length $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body and length $body 16 110 126
Condition66%2/3
defined $body and length $body
!ll&&!rl&&r
016110
MC/DC50%1/2
defined $body and length $body
defined $body length $body
Truth table66%2/3
A and B
A= defined $bodyB= length $body
ABresult
0X0
100
111
241 126 ▶ return '' unless defined $body && length $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body and length $body 16 110 126
Condition66%2/3
defined $body and length $body
!ll&&!rl&&r
016110
MC/DC50%1/2
defined $body and length $body
defined $body length $body
Truth table66%2/3
A and B
A= defined $bodyB= length $body
ABresult
0X0
100
111
242 110 $body =~ s/\s+/ /g;
243 110 $body =~ s/\A\s+//;
244 110 $body =~ s/\s+\z//;
245 110 ▶ return '' unless length $body;
Branch: 1 branch
BranchTrueFalseTotal
unless length $body 0 110 110
246 110 ▶ if ( length($body) > $error_body_max_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body > $Langertha::Role::HTTP::error_body_max_length) 1 109 110
247 1 $body = substr($body, 0, $error_body_max_length) . '...';
248 }
249 110 return $body;
250 }
251  
252 # The status line of a failed response, with the wait the provider asked for
253 # as seconds: "429 Too Many Requests (retry after 8s)" (karr k300). Resolved
254 # as RateLimit->retry_after resolves it, retry-after-ms first (karr k312).
255 121 ▶sub _failed_status_line {
Subroutine _failed_status_line: called CC=3 SCAR=11.0
256 121 my ( $self, $response ) = @_;
257 121 require Langertha::RateLimit;
258 121 my %raw = Langertha::RateLimit::_collect_headers($response);
259 121 my $wait = Langertha::RateLimit::_resolve_retry_after( \%raw );
260 121 ▶ return $response->status_line unless defined $wait;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wait 81 40 121
261 40 ▶ if ( $wait != int $wait ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wait != int $wait) 21 19 40
262 21 $wait = sprintf( '%.3f', $wait );
263 21 $wait =~ s/\.?0+\z//;
264 }
265 40 return $response->status_line . " (retry after ${wait}s)";
266 }
267  
268 # The one error text of a failed request, whichever backend ran it: the sync
269 # croak and every async die use it, so a caller reads the same message
270 # everywhere (ADR 0027 parity, karr k312). $what is "request", "streaming
271 # request" or "tool chat request".
272 121 ▶sub _request_failed_message {
Subroutine _request_failed_message: called CC=2 SCAR=6.9
273 121 my ( $self, $response, $what ) = @_;
274 121 my $body = $self->_error_response_body($response);
275 121 ▶ return "".(ref $self)." $what failed: ".$self->_failed_status_line($response)
Branch: 1 branch
BranchTrueFalseTotal
length $body ? : 105 16 121
276 .( length $body ? " - ".$body : "" );
277 }
278  
279 # "message (code)" of an `error` a provider put into a 200 body: an object
280 # with message/code, the same nested once more under `error`, or a plain
281 # string; undef when there is none. Shared by the chat parsers that croak on
282 # such a body (karr k301, k311).
283 63 ▶sub _body_error_text {
Subroutine _body_error_text: called CC=9 SCAR=22.3
284 63 my ( $self, $err ) = @_;
285 63 ▶ return undef unless defined $err;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err 12 51 63
286 51 ▶ $err = $err->{error} if ref $err eq 'HASH' && ref $err->{error} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $err eq "HASH" and ref $err->{"error"} eq "HASH" 2 49 51
Condition100%3/3
ref $err eq "HASH" and ref $err->{"error"} eq "HASH"
!ll&&!rl&&r
3462
MC/DC100%2/2
ref $err eq "HASH" and ref $err->{"error"} eq "HASH"
ref $err eq "HASH" ref $err->{"error"} eq "HASH"
Truth table100%3/3
A and B
A= ref $err eq "HASH"B= ref $err->{"error"} eq "HASH"
ABresult
0X0
100
111
287 51 ▶ return "$err" unless ref $err eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $err eq "HASH" 3 48 51
288 48 ▶ my $message = defined $err->{message} && !ref $err->{message} ? $err->{message} : 'no error message';
Branch: 1 branch
BranchTrueFalseTotal
defined $err->{'message'} && !ref($err->{'message'}) ? : 48 0 48
Condition33%1/3
defined $err->{"message"} and not ref $err->{"message"}
!ll&&!rl&&r
0048
MC/DC0%0/2
defined $err->{"message"} and not ref $err->{"message"}
defined $err->{"message"} not ref $err->{"message"}
Truth table33%1/3
A and not(B)
A= defined $err->{"message"}B= not ref $err->{"message"}
ABresult
0X0
100
111
289 48 ▶ my $code = defined $err->{code} && !ref $err->{code} ? " ($err->{code})" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $err->{'code'} && !ref($err->{'code'}) ? : 27 21 48
Condition66%2/3
defined $err->{"code"} and not ref $err->{"code"}
!ll&&!rl&&r
21027
MC/DC50%1/2
defined $err->{"code"} and not ref $err->{"code"}
defined $err->{"code"} not ref $err->{"code"}
Truth table66%2/3
A and not(B)
A= defined $err->{"code"}B= not ref $err->{"code"}
ABresult
0X0
100
111
290 48 return "$message$code";
291 }
292  
293 1258 ▶sub parse_response {
Subroutine parse_response: called CC=7 SCAR=20.7
Pod documented
294 1258 my ( $self, $response ) = @_;
295 # Every response, error or not, replaces the engine's rate limit first: a
296 # 429's remaining/reset/retry-after is what a caller backs off from (k300).
297 1258 ▶ $self->_update_rate_limit($response) if $self->can('_update_rate_limit');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("_update_rate_limit") 1258 0 1258
298 1258 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 74 1184 1258
299 74 $log->errorf("[%s] HTTP %s", ref $self, $response->status_line);
300 74 croak $self->_request_failed_message( $response, 'request' );
301 }
302 # Bounded (karr k346) and computed only when trace is on: decoded_content
303 # inflates a Content-Encoding unbounded, and this ran on every response. A
304 # body over response_max_bytes (a bomb, or a legitimately huge response) must
305 # not turn a trace line into a fatal, so the bound croak is caught here.
306 1184 ▶ if ( $log->is_trace ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Langertha::Role::HTTP::log->is_trace) 0 1184 1184
307 0 my $body = eval { $self->_bounded_decoded_content($response) };
308 0 ▶ $log->tracef("[%s] Response: %s", ref $self,
Branch: 1 branch
BranchTrueFalseTotal
defined $body ? : 0 0 0
309 defined $body ? $body : $response->status_line);
310 }
311 # A 200 that is not JSON (a proxy's HTML page, a truncated body) names the
312 # engine and shows the body, like the non-2xx path above (k290).
313 1184 my $data;
314 {
315 1184 local $@;
316 1184 ▶ unless ( eval { $data = $self->json->decode($response->content); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $data = $self->json->decode($response->content); 1 } }) 1 1183 1184
317 1 my $body = $self->_error_response_body($response);
318 1 ▶ croak "".(ref $self)." response is not valid JSON"
Branch: 1 branch
BranchTrueFalseTotal
length $body ? : 1 0 1
319 .( length $body ? ": ".$body : " (empty body)" );
320 }
321 }
322 1183 return $data;
323 }
324  
325  
326 194 has user_agent_timeout => (
327 isa => 'Int',
328 is => 'ro',
329 predicate => 'has_user_agent_timeout',
330 );
331  
332  
333 194 has user_agent_agent => (
334 isa => 'Str',
335 is => 'ro',
336 lazy_build => 1,
337 );
338 128 ▶sub _build_user_agent_agent {
Subroutine _build_user_agent_agent: called CC=1 SCAR=0.0
339 128 my ( $self ) = @_;
340 128 return "".(ref $self)."";
341 }
342  
343  
344 194 has user_agent => (
345 isa => 'LWP::UserAgent',
346 is => 'ro',
347 lazy_build => 1,
348 );
349 82 ▶sub _build_user_agent {
Subroutine _build_user_agent: called CC=3 SCAR=11.0
350 82 my ( $self ) = @_;
351 82 require Langertha::HTTP::UserAgent;
352 82 ▶ return Langertha::HTTP::UserAgent->new(
Branch: 2 branches
BranchTrueFalseTotal
$self->has_user_agent_timeout ? : 1 81 82
defined $self->connect_address ? : 17 65 82
353 agent => $self->user_agent_agent,
354 $self->has_user_agent_timeout ? ( timeout => $self->user_agent_timeout ) : (),
355 defined $self->connect_address
356 ? ( connect_host => $self->_connect_host, connect_address => $self->connect_address ) : (),
357 );
358 }
359  
360  
361 9 ▶sub execute_streaming_request {
Subroutine execute_streaming_request: called CC=4 SCAR=13.9
Pod documented
362 9 my ($self, $request, $chunk_callback) = @_;
363  
364 9 ▶ croak "execute_streaming_request requires Langertha::Role::Streaming"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->does("Langertha::Role::Streaming") 0 9 9
365 unless $self->does('Langertha::Role::Streaming');
366  
367 9 my $t0 = [gettimeofday];
368 9 my $response = $self->user_agent->request($request);
369  
370 9 ▶ $self->_update_rate_limit($response) if $self->can('_update_rate_limit');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("_update_rate_limit") 9 0 9
371 9 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 5 4 9
372 5 croak $self->_request_failed_message( $response, 'streaming request' );
373 }
374  
375 4 my $chunks = $self->process_stream_data($response->content, $chunk_callback);
376 4 my $total_seconds = tv_interval($t0);
377  
378 # This path reads the whole stream with one blocking LWP request before
379 # process_stream_data runs, so a true TTFT (time to first token) is
380 # not observable here; only end-to-end wall-clock. Consumers that need
381 # TTFT should use L<Langertha::Role::Chat/simple_chat_stream_realtime_f>,
382 # which delivers chunks as they arrive on either backend of
383 # L<Langertha::Role::AsyncHTTP> (Net::Async::HTTP, or LWP's content
384 # callback on the synchronous fallback).
385 4 return ($chunks, { total_seconds => $total_seconds });
386 }
387  
388  
389  
390 194 1;
391  
392 __END__
« blib/lib/Langertha/Role/ResponsesCompatible.pm ↑ Summary blib/lib/Langertha/Engine/Ollama.pm »