bin/langertha_image

filter: Statementstmt 33.1% 79 / 238 Branchbran 18.5% 26 / 140 Conditioncond 19.8% 20 / 101 MC/DCmcdc 9.0% 5 / 55 Subroutinesub 53.3% 16 / 30 Podpod n/a totaltotal 25.8% 146 / 564 CC 110 SCAR 55.1
CC
110
Cov
26%
_run
992.0
_validate_images
110.0
_read_prompt
72.0
CRAP 247.2
« blib/lib/Langertha/Usage.pm ↑ Summary blib/lib/Langertha/Role/OpenAPI.pm »
1 #!/usr/bin/env perl
2 # ABSTRACT: Generate images through OpenAI or an explicit subscription proxy
3 # PODNAME: langertha_image
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Getopt::Long qw( GetOptionsFromArray Configure );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Pod::Usage qw( pod2usage );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use MIME::Base64 qw( decode_base64 encode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Temp qw( tempfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our $VERSION = '0.505';
17  
18 1 Configure(qw( no_auto_abbrev no_ignore_case ));
19  
20 1 ▶use constant MAX_DOWNLOAD_BYTES => 64 * 1024 * 1024;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # Text inputs (prompt, prefix) arrive as raw bytes and are sent to the provider
23 # as characters; paths stay bytes end to end so what is printed is the real name.
24 0 ▶sub _decode_text {
Subroutine _decode_text: not called CC=3 SCAR=24.8
25 0 my ( $text, $what ) = @_;
26 0 ▶ return $text unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
27 0 my $decoded = eval { Encode::decode( 'UTF-8', $text, Encode::FB_CROAK | Encode::LEAVE_SRC ) };
28 0 ▶ die "$what is not valid UTF-8\n" unless defined $decoded;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decoded 0 0 0
29 0 return $decoded;
30 }
31  
32 8 ▶sub _resolve_backend_url {
Subroutine _resolve_backend_url: called CC=17 SCAR=32.1
33 8 my ( $backend_option, $url_option, $environment ) = @_;
34  
35 my $backend = defined $backend_option
36 ? lc $backend_option
37 8 ▶ : lc( $environment->{LANGERTHA_IMAGE_BACKEND} // 'openai' );
Branch: 1 branch
BranchTrueFalseTotal
defined $backend_option ? : 6 2 8
Condition100%2/2
$environment->{"LANGERTHA_IMAGE_BACKEND"} // "openai"
l!l
11
MC/DC100%1/1
$environment->{"LANGERTHA_IMAGE_BACKEND"} // "openai"
$environment->{"LANGERTHA_IMAGE_BACKEND"}
Truth table100%2/2
A
A= $environment->{"LANGERTHA_IMAGE_BACKEND"}
Aresult
11
00
38 8 ▶ die "backend must be proxy or openai\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $backend eq "proxy" or $backend eq "openai" 0 8 8
Condition66%2/3
$backend eq "proxy" or $backend eq "openai"
l!l&&r!l&&!r
440
MC/DC0%0/2
$backend eq "proxy" or $backend eq "openai"
$backend eq "proxy" $backend eq "openai"
Truth table66%2/3
A or B
A= $backend eq "proxy"B= $backend eq "openai"
ABresult
1X1
011
000
39 unless $backend eq 'proxy' || $backend eq 'openai';
40  
41 my $url = defined $url_option
42 ? $url_option
43 : $backend eq 'proxy'
44 ? $environment->{LANGERTHA_IMAGE_PROXY_URL}
45 8 ▶ : $environment->{LANGERTHA_IMAGE_OPENAI_URL};
Branch: 2 branches
BranchTrueFalseTotal
defined $url_option ? : 1 7 8
$backend eq 'proxy' ? : 3 4 7
46  
47 8 ▶ die "proxy backend requires --url or LANGERTHA_IMAGE_PROXY_URL\n"
Branch: 1 branch
BranchTrueFalseTotal
if $backend eq "proxy" and not defined $url 1 7 8
Condition100%3/3
$backend eq "proxy" and not defined $url
!ll&&!rl&&r
431
MC/DC100%2/2
$backend eq "proxy" and not defined $url
$backend eq "proxy" not defined $url
Truth table100%3/3
A and not(B)
A= $backend eq "proxy"B= not defined $url
ABresult
0X0
100
111
48 if $backend eq 'proxy' && !defined $url;
49  
50 7 ▶ if ( defined $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $url) 4 3 7
51 4 my $uri = URI->new($url);
52 4 ▶ die "image URL must be http(s), without credentials, query, or fragment, and end in /v1\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri->scheme and $uri->scheme =~ /\Ahttps?\z/ and $uri->can("userinfo") and not defined $uri->userinfo and not defined $uri->query and not defined $uri->fragment and $uri->path =~ m[/v1/?\z] 0 4 4
Condition33%6/18
defined $uri->scheme and $uri->scheme =~ /\Ahttps?\z/ and $uri->can("userinfo") and not defined $uri->userinfo and not defined $uri->query and not defined $uri->fragment and $uri->path =~ m[/v1/?\z]
!ll&&!rl&&r
004
defined $uri->scheme and $uri->scheme =~ /\Ahttps?\z/ and $uri->can("userinfo") and not defined $uri->userinfo and not defined $uri->query and not defined $uri->fragment
!ll&&!rl&&r
004
defined $uri->scheme and $uri->scheme =~ /\Ahttps?\z/ and $uri->can("userinfo") and not defined $uri->userinfo and not defined $uri->query
!ll&&!rl&&r
004
defined $uri->scheme and $uri->scheme =~ /\Ahttps?\z/ and $uri->can("userinfo") and not defined $uri->userinfo
!ll&&!rl&&r
004
defined $uri->scheme and $uri->scheme =~ /\Ahttps?\z/ and $uri->can("userinfo")
!ll&&!rl&&r
004
defined $uri->scheme and $uri->scheme =~ /\Ahttps?\z/
!ll&&!rl&&r
004
MC/DC0%0/7
defined $uri->scheme and $uri->scheme =~ /\Ahttps?\z/ and $uri->can("userinfo") and not defined $uri->userinfo and not defined $uri->query and not defined $uri->fragment and $uri->path =~ m[/v1/?\z]
defined $uri->scheme $uri->scheme =~ /\Ahttps?\z/ $uri->can("userinfo") not defined $uri->userinfo not defined $uri->query not defined $uri->fragment $uri->path =~ m[/v1/?\z]
Truth table12%1/8
A and B and C and not(D) and not(E) and not(F) and G
A= defined $uri->schemeB= $uri->scheme =~ /\Ahttps?\z/C= $uri->can("userinfo")D= not defined $uri->userinfoE= not defined $uri->queryF= not defined $uri->fragmentG= $uri->path =~ m[/v1/?\z]
ABCDEFGresult
0XXXXXX0
10XXXXX0
110XXXX0
1110XXX0
11110XX0
111110X0
11111100
11111111
53 unless defined $uri->scheme
54 && $uri->scheme =~ /\Ahttps?\z/
55 && $uri->can('userinfo')
56 && !defined $uri->userinfo
57 && !defined $uri->query
58 && !defined $uri->fragment
59 && $uri->path =~ m{/v1/?\z};
60 4 $url =~ s{/\z}{};
61 }
62  
63 7 return ( $backend, $url );
64 }
65  
66 0 ▶sub _read_prompt {
Subroutine _read_prompt: not called CC=8 SCAR=42.8
67 0 my ( $arguments, $prompt_file ) = @_;
68 die "use either PROMPT or --prompt-file, not both\n"
69 0 ▶ if defined $prompt_file && @{$arguments};
Branch: 1 branch
BranchTrueFalseTotal
if defined $prompt_file and @{$arguments;} 0 0 0
Condition0%0/3
defined $prompt_file and @{$arguments;}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $prompt_file and @{$arguments;}
defined $prompt_file @{$arguments;}
Truth table0%0/3
A and B
A= defined $prompt_fileB= @{$arguments;}
ABresult
0X0
100
111
70 die "exactly one PROMPT or --prompt-file is required\n"
71 0 ▶ unless defined $prompt_file || @{$arguments} == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prompt_file or @{$arguments;} == 1 0 0 0
Condition0%0/3
defined $prompt_file or @{$arguments;} == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $prompt_file or @{$arguments;} == 1
defined $prompt_file @{$arguments;} == 1
Truth table0%0/3
A or B
A= defined $prompt_fileB= @{$arguments;} == 1
ABresult
1X1
011
000
72  
73 0 ▶ my $prompt = defined $prompt_file
Branch: 1 branch
BranchTrueFalseTotal
defined $prompt_file ? : 0 0 0
74 ? path($prompt_file)->slurp_utf8
75 : $arguments->[0];
76 0 ▶ die "prompt must not be empty\n" unless defined $prompt && $prompt =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prompt and $prompt =~ /\S/ 0 0 0
Condition0%0/3
defined $prompt and $prompt =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $prompt and $prompt =~ /\S/
defined $prompt $prompt =~ /\S/
Truth table0%0/3
A and B
A= defined $promptB= $prompt =~ /\S/
ABresult
0X0
100
111
77 0 return $prompt;
78 }
79  
80 0 ▶sub _effective_prompt {
Subroutine _effective_prompt: not called CC=4 SCAR=30.0
81 0 my ( $prompt, $prefix_option, $environment ) = @_;
82 my $prefix = defined $prefix_option
83 ? $prefix_option
84 0 ▶ : $environment->{LANGERTHA_IMAGE_PROMPT_PREFIX};
Branch: 1 branch
BranchTrueFalseTotal
defined $prefix_option ? : 0 0 0
85 0 ▶ return defined $prefix && length $prefix ? "$prefix\n\n$prompt" : $prompt;
Branch: 1 branch
BranchTrueFalseTotal
defined $prefix && length $prefix ? : 0 0 0
Condition0%0/3
defined $prefix and length $prefix
!ll&&!rl&&r
000
MC/DC0%0/2
defined $prefix and length $prefix
defined $prefix length $prefix
Truth table0%0/3
A and B
A= defined $prefixB= length $prefix
ABresult
0X0
100
111
86 }
87  
88 0 ▶sub _default_output_path {
Subroutine _default_output_path: not called CC=2 SCAR=17.9
89 0 my ( $home, $prompt ) = @_;
90 0 my $slug = lc $prompt;
91 0 $slug =~ s/[^a-z0-9]+/-/g;
92 0 $slug =~ s/\A-+|-+\z//g;
93 0 $slug = substr $slug, 0, 48;
94 0 $slug =~ s/-+\z//;
95 0 ▶ $slug = 'image' unless length $slug;
Branch: 1 branch
BranchTrueFalseTotal
unless length $slug 0 0 0
96 0 return path($home)->child("langertha-image-$slug.png")->absolute;
97 }
98  
99 0 ▶sub _output_paths {
Subroutine _output_paths: not called CC=3 SCAR=24.8
100 0 my ( $base, $count ) = @_;
101 0 my $absolute = path($base)->absolute;
102 0 ▶ return ($absolute) if $count == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $count == 1 0 0 0
103  
104 0 my $parent = $absolute->parent;
105 0 my ( $stem, $extension ) = $absolute->basename =~ /\A(.+?)(\.[^.]+)?\z/;
106 0 ▶ $extension //= q{};
Condition0%0/2
$extension //= ""
l!l
00
MC/DC0%0/1
$extension //= ""
$extension
Truth table0%0/2
A
A= $extension
Aresult
11
00
107 0 return map { $parent->child("$stem-$_$extension") } 1 .. $count;
108 }
109  
110 0 ▶sub _decode_base64_image {
Subroutine _decode_base64_image: not called CC=6 SCAR=37.4
111 0 my ($encoded) = @_;
112 0 ▶ die "malformed b64_json\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoded and not ref $encoded and length $encoded and $encoded =~ m[\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z] 0 0 0
Condition0%0/9
defined $encoded and not ref $encoded and length $encoded and $encoded =~ m[\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z]
!ll&&!rl&&r
000
defined $encoded and not ref $encoded and length $encoded
!ll&&!rl&&r
000
defined $encoded and not ref $encoded
!ll&&!rl&&r
000
MC/DC0%0/4
defined $encoded and not ref $encoded and length $encoded and $encoded =~ m[\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z]
defined $encoded not ref $encoded length $encoded $encoded =~ m[\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z]
Truth table0%0/5
A and not(B) and C and D
A= defined $encodedB= not ref $encodedC= length $encodedD= $encoded =~ m[\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
113 unless defined $encoded
114 && !ref $encoded
115 && length $encoded
116 && $encoded =~ m{\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z};
117  
118 0 my $bytes = decode_base64($encoded);
119 0 ▶ die "non-canonical b64_json\n" unless encode_base64( $bytes, q{} ) eq $encoded;
Branch: 1 branch
BranchTrueFalseTotal
unless &encode_base64($bytes, "") eq $encoded 0 0 0
120 0 return $bytes;
121 }
122  
123 2 ▶sub _validate_image_bytes {
Subroutine _validate_image_bytes: called CC=10 SCAR=40.9
124 2 my ($bytes) = @_;
125 2 ▶ die "empty image result\n" unless defined $bytes && length $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes and length $bytes 0 2 2
Condition33%1/3
defined $bytes and length $bytes
!ll&&!rl&&r
002
MC/DC0%0/2
defined $bytes and length $bytes
defined $bytes length $bytes
Truth table33%1/3
A and B
A= defined $bytesB= length $bytes
ABresult
0X0
100
111
126 2 ▶ return 1 if substr( $bytes, 0, 8 ) eq "\x89PNG\r\n\x1a\n";
Branch: 1 branch
BranchTrueFalseTotal
if substr($bytes, 0, 8) eq "\211PNG\r\n\32\n" 2 0 2
127 0 ▶ return 1 if substr( $bytes, 0, 3 ) eq "\xff\xd8\xff";
Branch: 1 branch
BranchTrueFalseTotal
if substr($bytes, 0, 3) eq "\377\330\377" 0 0 0
128 0 ▶ return 1 if substr( $bytes, 0, 6 ) eq 'GIF87a';
Branch: 1 branch
BranchTrueFalseTotal
if substr($bytes, 0, 6) eq "GIF87a" 0 0 0
129 0 ▶ return 1 if substr( $bytes, 0, 6 ) eq 'GIF89a';
Branch: 1 branch
BranchTrueFalseTotal
if substr($bytes, 0, 6) eq "GIF89a" 0 0 0
130 0 ▶ return 1 if length($bytes) >= 12
Branch: 1 branch
BranchTrueFalseTotal
if length $bytes >= 12 and substr($bytes, 0, 4) eq "RIFF" and substr($bytes, 8, 4) eq "WEBP" 0 0 0
Condition0%0/6
length $bytes >= 12 and substr($bytes, 0, 4) eq "RIFF" and substr($bytes, 8, 4) eq "WEBP"
!ll&&!rl&&r
000
length $bytes >= 12 and substr($bytes, 0, 4) eq "RIFF"
!ll&&!rl&&r
000
MC/DC0%0/3
length $bytes >= 12 and substr($bytes, 0, 4) eq "RIFF" and substr($bytes, 8, 4) eq "WEBP"
length $bytes >= 12 substr($bytes, 0, 4) eq "RIFF" substr($bytes, 8, 4) eq "WEBP"
Truth table0%0/4
A and B and C
A= length $bytes >= 12B= substr($bytes, 0, 4) eq "RIFF"C= substr($bytes, 8, 4) eq "WEBP"
ABCresult
0XX0
10X0
1100
1111
131 && substr( $bytes, 0, 4 ) eq 'RIFF'
132 && substr( $bytes, 8, 4 ) eq 'WEBP';
133 0 die "provider result is not a supported image\n";
134 }
135  
136 2 ▶sub _redacted_url {
Subroutine _redacted_url: called CC=5 SCAR=16.4
137 2 my ($url) = @_;
138 2 my $uri = URI->new($url);
139 2 ▶ my $scheme = $uri->scheme // q{};
Condition50%1/2
$uri->scheme // ""
l!l
20
MC/DC0%0/1
$uri->scheme // ""
$uri->scheme
Truth table50%1/2
A
A= $uri->scheme
Aresult
11
00
140 2 ▶ return "<$scheme: URL>" unless $scheme =~ /\Ahttps?\z/ && $uri->can('query');
Branch: 1 branch
BranchTrueFalseTotal
unless $scheme =~ /\Ahttps?\z/ and $uri->can("query") 1 1 2
Condition66%2/3
$scheme =~ /\Ahttps?\z/ and $uri->can("query")
!ll&&!rl&&r
101
MC/DC50%1/2
$scheme =~ /\Ahttps?\z/ and $uri->can("query")
$scheme =~ /\Ahttps?\z/ $uri->can("query")
Truth table66%2/3
A and B
A= $scheme =~ /\Ahttps?\z/B= $uri->can("query")
ABresult
0X0
100
111
141 1 ▶ $uri->userinfo(undef) if $uri->can('userinfo');
Branch: 1 branch
BranchTrueFalseTotal
if $uri->can("userinfo") 1 0 1
142 1 $uri->query(undef);
143 1 $uri->fragment(undef);
144 1 return $uri->as_string;
145 }
146  
147 0 ▶sub _image_bytes {
Subroutine _image_bytes: not called CC=4 SCAR=30.0
148 0 my ($item) = @_;
149 0 ▶ if ( defined $item->{b64_json} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item->{"b64_json"}) 0 0 0
150 0 return _decode_base64_image( $item->{b64_json} );
151 }
152 0 my $url = $item->{url};
153 0 require Langertha::Content::Image;
154 0 ▶ my $failure = sub { return 'image download failed from ' . _redacted_url($url) . "\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
155 0 ▶ my $image = eval { Langertha::Content::Image->from_url($url) }
Branch: 1 branch
BranchTrueFalseTotal
my $image = eval { do { "Langertha::Content::Image"->from_url($url) } } or die &$failure() 0 0 0
156 or die $failure->();
157 0 my $encoded = eval { $image->ensure_base64( max_bytes => MAX_DOWNLOAD_BYTES ) };
158 0 ▶ die $failure->() unless defined $encoded;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoded 0 0 0
159 0 return _decode_base64_image($encoded);
160 }
161  
162 # Writes to a temporary file in the target directory and renames it into place
163 # only after the bytes passed the signature check, so a failure never leaves a
164 # finished-looking file (and --force never destroys the old one for bad data).
165 # Without --force the move must not clobber a file that appeared after the
166 # preflight: link() fails if the target exists, rename() would replace it.
167 1 ▶sub _move_into_place {
Subroutine _move_into_place: called CC=6 SCAR=28.6
168 1 my ( $temp, $target, $force ) = @_;
169 1 ▶ if ( !$force ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($force) 0 1 1
170 0 ▶ if ( link $temp, $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (link $temp, $target) 0 0 0
171 0 unlink $temp;
172 0 return 1;
173 }
174 0 ▶ die "output exists: $target\n" if -e $target || -l $target;
Branch: 1 branch
BranchTrueFalseTotal
if -e $target or -l $target 0 0 0
Condition0%0/3
-e $target or -l $target
l!l&&r!l&&!r
000
MC/DC0%0/2
-e $target or -l $target
-e $target -l $target
Truth table0%0/3
A or B
A= -e $targetB= -l $target
ABresult
1X1
011
000
175 # no hard links on this filesystem: fall through to rename
176 }
177 1 ▶ rename $temp, $target or die "cannot move image into place: $target\n";
Branch: 1 branch
BranchTrueFalseTotal
rename $temp, $target or die "cannot move image into place: $target\n" 1 0 1
178 1 return 1;
179 }
180  
181 2 ▶sub _write_image_bytes {
Subroutine _write_image_bytes: called CC=9 SCAR=24.5
182 2 my ( $bytes, $target, $force ) = @_;
183 2 _validate_image_bytes($bytes);
184 2 my $parent = $target->parent;
185 2 ▶ die "output directory does not exist: $parent\n" unless $parent->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->is_dir 0 2 2
186 2 ▶ die "output exists: $target\n" if !$force && ( -e "$target" || -l "$target" );
Branch: 1 branch
BranchTrueFalseTotal
if not $force and -e "$target" || -l "$target" 1 1 2
Condition50%3/6
not $force and -e "$target" || -l "$target"
!ll&&!rl&&r
101
-e "$target" or -l "$target"
l!l&&r!l&&!r
100
MC/DC33%1/3
not $force and -e "$target" || -l "$target"
not $force -e "$target" -l "$target"
Truth table50%2/4
not(A) and B or C
A= not $forceB= -e "$target"C= -l "$target"
ABCresult
0XX0
1000
11X1
1011
187  
188 1 my ( $handle, $temp ) = tempfile( '.langertha_image-XXXXXXXX', DIR => "$parent" );
189 1 my $written = eval {
190 1 binmode $handle;
191 1 ▶ print {$handle} $bytes or die "cannot write image\n";
Branch: 1 branch
BranchTrueFalseTotal
print {$handle;} $bytes or die "cannot write image\n" 1 0 1
192 1 ▶ close $handle or die "cannot write image\n";
Branch: 1 branch
BranchTrueFalseTotal
close $handle or die "cannot write image\n" 1 0 1
193 1 chmod 0666 & ~umask, $temp;
194 1 _move_into_place( $temp, "$target", $force );
195 1 1;
196 };
197 1 my $error = $@;
198 1 ▶ unless ($written) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($written) 0 1 1
199 0 ▶ close $handle if defined fileno $handle;
Branch: 1 branch
BranchTrueFalseTotal
if defined fileno $handle 0 0 0
200 0 unlink $temp;
201 0 die $error;
202 }
203 1 return $target;
204 }
205  
206 0 ▶sub _materialize_image {
Subroutine _materialize_image: not called CC=1 SCAR=6.9
207 0 my ( $engine, $item, $target, $force ) = @_;
208 0 my $bytes = _image_bytes($item);
209 0 return _write_image_bytes( $bytes, path($target)->absolute, $force )->stringify;
210 }
211  
212 0 ▶sub _validate_images {
Subroutine _validate_images: not called CC=10 SCAR=47.0
213 0 my ( $images, $count ) = @_;
214 0 ▶ die "image response must be an array\n" unless ref($images) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $images eq "ARRAY" 0 0 0
215 0 ▶ die "provider returned a different image count\n" unless @{$images} == $count;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$images;} == $count 0 0 0
216 0 for my $item ( @{$images} ) {
217 0 ▶ die "image item must be an object\n" unless ref($item) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item eq "HASH" 0 0 0
218 0 my $has_b64 = defined $item->{b64_json};
219 0 my $has_url = defined $item->{url};
220 0 ▶ die "image item needs exactly one of b64_json or url\n" unless $has_b64 xor $has_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_b64 xor $has_url 0 0 0
Condition0%0/4
$has_b64 xor $has_url
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$has_b64 xor $has_url
$has_b64 $has_url
Truth table0%0/4
A xor B
A= $has_b64B= $has_url
ABresult
110
101
011
000
221 0 ▶ my $source = $has_b64 ? $item->{b64_json} : $item->{url};
Branch: 1 branch
BranchTrueFalseTotal
$has_b64 ? : 0 0 0
222 0 ▶ die "image item source must be a non-empty string\n" if ref $source || !length $source;
Branch: 1 branch
BranchTrueFalseTotal
if ref $source or not length $source 0 0 0
Condition0%0/3
ref $source or not length $source
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $source or not length $source
ref $source not length $source
Truth table0%0/3
A or not(B)
A= ref $sourceB= not length $source
ABresult
1X1
011
000
223 }
224 0 return 1;
225 }
226  
227 0 ▶sub _usage_line {
Subroutine _usage_line: not called CC=2 SCAR=17.9
228 0 my ($call_result) = @_;
229 0 ▶ return unless $call_result->has_usage;
Branch: 1 branch
BranchTrueFalseTotal
unless $call_result->has_usage 0 0 0
230 0 my $usage = $call_result->usage;
231 0 return sprintf 'usage: input_tokens=%d output_tokens=%d total_tokens=%d',
232 $usage->input_tokens, $usage->output_tokens, $usage->total_tokens;
233 }
234  
235 0 ▶sub _run {
Subroutine _run: not called CC=31 SCAR=69.0
236 0 my (@arguments) = @_;
237 0 my %opt = ( force => 0 );
238 0 my @warnings;
239 0 my $parsed = do {
240 0 ▶ local $SIG{__WARN__} = sub { push @warnings, @_ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
241 0 GetOptionsFromArray( \@arguments, \%opt,
242 'backend=s', 'url=s', 'prompt-file=s', 'prompt-prefix=s', 'output=s',
243 'model=s', 'size=s', 'quality=s', 'background=s', 'n=s', 'force!', 'help|h' );
244 };
245 0 ▶ if ( !$parsed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parsed) 0 0 0
246 0 print STDERR @warnings;
247 0 die "see langertha_image --help\n";
248 }
249  
250 0 ▶ if ( $opt{help} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 0 0 0
251 0 pod2usage( -input => __FILE__, -verbose => 99, -exitval => 'NOEXIT', -output => \*STDOUT,
252 -sections => 'SYNOPSIS|DESCRIPTION|OPTIONS|ENVIRONMENT|OUTPUT|BILLING BOUNDARY|ERRORS' );
253 0 return 0;
254 }
255  
256 0 my $environment = \%ENV;
257 0 ▶ my $count = defined $opt{n} ? $opt{n} : ( $environment->{LANGERTHA_IMAGE_N} // 1 );
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'n'} ? : 0 0 0
Condition0%0/2
$environment->{"LANGERTHA_IMAGE_N"} // 1
l!l
00
MC/DC0%0/1
$environment->{"LANGERTHA_IMAGE_N"} // 1
$environment->{"LANGERTHA_IMAGE_N"}
Truth table0%0/2
A
A= $environment->{"LANGERTHA_IMAGE_N"}
Aresult
11
00
258 0 ▶ die "--n must be an integer from 1 to 10\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $count =~ /\A[0-9]+\z/ and $count >= 1 and $count <= 10 0 0 0
Condition0%0/6
$count =~ /\A[0-9]+\z/ and $count >= 1 and $count <= 10
!ll&&!rl&&r
000
$count =~ /\A[0-9]+\z/ and $count >= 1
!ll&&!rl&&r
000
MC/DC0%0/3
$count =~ /\A[0-9]+\z/ and $count >= 1 and $count <= 10
$count =~ /\A[0-9]+\z/ $count >= 1 $count <= 10
Truth table0%0/4
A and B and C
A= $count =~ /\A[0-9]+\z/B= $count >= 1C= $count <= 10
ABCresult
0XX0
10X0
1100
1111
259 unless $count =~ /\A[0-9]+\z/ && $count >= 1 && $count <= 10;
260 0 $count += 0;
261  
262 0 @arguments = map { _decode_text( $_, 'PROMPT' ) } @arguments;
263 0 my $prompt = _read_prompt( \@arguments, $opt{'prompt-file'} );
264 my $prefix = defined $opt{'prompt-prefix'}
265 ? _decode_text( $opt{'prompt-prefix'}, '--prompt-prefix' )
266 0 ▶ : _decode_text( $environment->{LANGERTHA_IMAGE_PROMPT_PREFIX}, 'LANGERTHA_IMAGE_PROMPT_PREFIX' );
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'prompt-prefix'} ? : 0 0 0
267 0 my $effective_prompt = _effective_prompt( $prompt, $prefix, $environment );
268 0 my ( $backend, $url ) = _resolve_backend_url( $opt{backend}, $opt{url}, $environment );
269  
270 0 my %image_option;
271 0 for my $name (qw( model size quality background )) {
272 0 ▶ my $value = defined $opt{$name} ? $opt{$name} : $environment->{ 'LANGERTHA_IMAGE_' . uc $name };
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{$name} ? : 0 0 0
273 0 ▶ $image_option{$name} = $value if defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and length $value 0 0 0
Condition0%0/3
defined $value and length $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table0%0/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
274 }
275  
276 my $base = defined $opt{output}
277 ? path( $opt{output} )->absolute
278 0 ▶ : _default_output_path( $environment->{HOME} // path('~')->stringify, $prompt );
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'output'} ? : 0 0 0
Condition0%0/3
$environment->{"HOME"} // path("~")->stringify
l!l&&r!l&&!r
000
MC/DC0%0/2
$environment->{"HOME"} // path("~")->stringify
$environment->{"HOME"} path("~")->stringify
Truth table0%0/3
A // B
A= $environment->{"HOME"}B= path("~")->stringify
ABresult
1X1
011
000
279 0 my @targets = _output_paths( $base, $count );
280 0 my $parent = $targets[0]->parent;
281 0 ▶ die "output directory does not exist: $parent\n" unless $parent->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->is_dir 0 0 0
282 0 ▶ die "output directory is not writable: $parent\n" unless -w "$parent";
Branch: 1 branch
BranchTrueFalseTotal
unless -w "$parent" 0 0 0
283 0 ▶ unless ( $opt{force} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"force"}) 0 0 0
284 0 for my $target (@targets) {
285 0 ▶ die "output exists (use --force to replace): $target\n" if -e "$target" || -l "$target";
Branch: 1 branch
BranchTrueFalseTotal
if -e "$target" or -l "$target" 0 0 0
Condition0%0/3
-e "$target" or -l "$target"
l!l&&r!l&&!r
000
MC/DC0%0/2
-e "$target" or -l "$target"
-e "$target" -l "$target"
Truth table0%0/3
A or B
A= -e "$target"B= -l "$target"
ABresult
1X1
011
000
286 }
287 }
288  
289 0 print STDERR "backend: $backend\n";
290  
291 0 require Langertha::Engine::OpenAI;
292 0 ▶ my %engine_arguments = defined $url ? ( url => $url ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $url ? : 0 0 0
293 0 ▶ $engine_arguments{api_key} = undef if $backend eq 'proxy';
Branch: 1 branch
BranchTrueFalseTotal
if $backend eq "proxy" 0 0 0
294 0 my $engine = Langertha::Engine::OpenAI->new(%engine_arguments);
295  
296 0 my $call_result = eval {
297 $engine->simple_image_result( $effective_prompt, n => $count,
298 0 map { ( $_ => $image_option{$_} ) } sort keys %image_option );
299 };
300 0 ▶ if ( !$call_result ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($call_result) 0 0 0
301 0 ▶ my $error = $@ || "image request failed\n";
Condition0%0/2
$@ || "image request failed\n"
l!l
00
MC/DC0%0/1
$@ || "image request failed\n"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
302 0 $error =~ s/ at .+? line \d+\.?\s*\z//s;
303 0 chomp $error;
304 0 die "image request failed: $error\n";
305 }
306 0 my $images = $call_result->value;
307  
308 0 ▶ print STDERR 'model: ' . $call_result->model . "\n" if $call_result->has_model;
Branch: 1 branch
BranchTrueFalseTotal
if $call_result->has_model 0 0 0
309 0 my $usage_line = _usage_line($call_result);
310 0 ▶ print STDERR "$usage_line\n" if defined $usage_line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $usage_line 0 0 0
311 0 ▶ printf STDERR "runtime: %.3f seconds\n", $call_result->total_seconds if $call_result->has_total_seconds;
Branch: 1 branch
BranchTrueFalseTotal
if $call_result->has_total_seconds 0 0 0
312  
313 0 _validate_images( $images, $count );
314  
315 0 my @finalized;
316 0 for my $index ( 0 .. $#{$images} ) {
317 0 my $final = eval { _materialize_image( $engine, $images->[$index], $targets[$index], $opt{force} ) };
318 0 ▶ if ( !defined $final ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $final) 0 0 0
319 0 my $reason = $@;
320 0 chomp $reason;
321 0 print STDERR "already finalized: $_\n" for @finalized;
322 0 die 'image ' . ( $index + 1 ) . " failed: $reason\n";
323 }
324 0 push @finalized, $final;
325 0 print STDOUT "$final\n";
326 }
327 0 return 0;
328 }
329  
330 0 ▶sub main {
Subroutine main: not called CC=5 SCAR=34.0
331 0 my (@arguments) = @_;
332 0 my $code = eval { _run(@arguments) };
333 0 ▶ return $code if defined $code;
Branch: 1 branch
BranchTrueFalseTotal
if defined $code 0 0 0
334 0 my $error = $@;
335 0 ▶ $error = "langertha_image failed\n" unless defined $error && length $error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error and length $error 0 0 0
Condition0%0/3
defined $error and length $error
!ll&&!rl&&r
000
MC/DC0%0/2
defined $error and length $error
defined $error length $error
Truth table0%0/3
A and B
A= defined $errorB= length $error
ABresult
0X0
100
111
336 0 ▶ $error .= "\n" unless $error =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $error =~ /\n\z/ 0 0 0
337 0 print STDERR "langertha_image: $error";
338 0 return 2;
339 }
340  
341 1 ▶exit main(@ARGV) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
342 1 1;
343  
344 __END__
« blib/lib/Langertha/Usage.pm ↑ Summary blib/lib/Langertha/Role/OpenAPI.pm »