lib/Langertha/Skeid/Protocol/Audio.pm

filter: Statementstmt 100% 77 / 77 Branchbran 97.3% 37 / 38 Conditioncond 84.6% 33 / 39 MC/DCmcdc 65.0% 13 / 20 Subroutinesub 100% 12 / 12 Podpod 100% 8 / 8 totaltotal 92.7% 180 / 194 CC 37 SCAR 17.8
CC
37
Cov
95%
form_fields
9.0
_is_measure
7.0
usage_units
7.0
CRAP 5.9
« lib/Langertha/Skeid/Protocol/Rerank.pm ↑ Summary lib/Langertha/Skeid/SwaggerUI.pm »
1 package Langertha::Skeid::Protocol::Audio;
2 113 our $VERSION = '0.004';
3 # ABSTRACT: The OpenAI audio routes -- the upload form relayed, the answer's usage read
4 113 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 113 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 113 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 # The client's path and the path below the node URL it is relayed to.
10 113 my %UPSTREAM_PATH = (
11 '/v1/audio/transcriptions' => '/audio/transcriptions',
12 '/v1/audio/translations' => '/audio/translations',
13 );
14  
15 # A form field Skeid reads itself -- model, stream -- is a short token. A longer one is not read
16 # into memory to find that out.
17 113 my $FIELD_MAX = 1024;
18  
19 # What a boolean form field has to say to be on. A form has no booleans, only strings, and the
20 # nodes behind these routes read theirs through pydantic, which takes exactly these for true, in
21 # any case.
22 113 my %FORM_TRUE = map { $_ => 1 } qw(1 true on yes t y);
23  
24  
25 152 ▶sub routes { return sort keys %UPSTREAM_PATH }
Subroutine routes: called CC=1 SCAR=0.0
Pod documented
26  
27  
28 62 ▶sub upstream_path {
Subroutine upstream_path: called CC=2 SCAR=7.2
Pod documented
29 62 my ($class, $route) = @_;
30 62 ▶ return $UPSTREAM_PATH{$route // ''};
Condition50%1/2
$route // ""
l!l
620
MC/DC0%0/1
$route // ""
$route
Truth table50%1/2
A
A= $route
Aresult
11
00
31 }
32  
33  
34 74 ▶sub form_fields {
Subroutine form_fields: called CC=9 SCAR=22.0
Pod documented
35 74 my ($class, $content) = @_;
36 74 my %field;
37 74 my $parts = $content->parts;
38 74 for my $index (0 .. $#$parts) {
39 208 my $part = $parts->[$index];
40 208 ▶ return ({}, 'Nested multipart parts are not supported') if $part->is_multipart;
Branch: 1 branch
BranchTrueFalseTotal
if $part->is_multipart 0 208 208
41 208 ▶ my $disposition = $part->headers->content_disposition // next;
Condition50%1/2
$part->headers->content_disposition // (next)
l!l
2080
MC/DC0%0/1
$part->headers->content_disposition // (next)
$part->headers->content_disposition
Truth table50%1/2
A
A= $part->headers->content_disposition
Aresult
11
00
42 208 my ($names, $is_file, $extended) = $class->disposition_names($disposition);
43 208 ▶ return ({}, 'A form field name in the extended notation (name*) is not supported')
Branch: 1 branch
BranchTrueFalseTotal
if $extended 1 207 208
44 if $extended;
45 207 ▶ next if $is_file;
Branch: 1 branch
BranchTrueFalseTotal
if $is_file 75 132 207
46 132 ▶ my ($name) = grep { $_ eq 'model' || $_ eq 'stream' } @$names;
Condition100%3/3
$_ eq 'model' || $_ eq 'stream'
l!l&&r!l&&!r
812923
MC/DC100%2/2
$_ eq 'model' || $_ eq 'stream'
$_ eq 'model' $_ eq 'stream'
Truth table100%3/3
A || B
A= $_ eq 'model'B= $_ eq 'stream'
ABresult
1X1
011
000
47 132 ▶ next unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 22 110 132
48 110 my $asset = $part->asset;
49 110 ▶ push @{$field{$name}}, {
Branch: 1 branch
BranchTrueFalseTotal
$asset->size > $FIELD_MAX ? : 1 109 110
50 index => $index,
51 value => ($asset->size > $FIELD_MAX ? undef : $asset->slurp),
52 };
53 }
54 73 return (\%field, undef);
55 }
56  
57  
58 354 ▶sub disposition_names {
Subroutine disposition_names: called CC=6 SCAR=17.9
Pod documented
59 354 my ($class, $disposition) = @_;
60 354 my (@names, $is_file, $extended);
61 354 ▶ while ($disposition =~ /(?:\A|[;,])\s*([^\s=;,"]+)\s*=\s*(?:"((?:\\.|[^"\\])*)"|([^;,\s]*))/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($disposition =~ /(?:\A|[;,])\s*([^\s=;,"]+)\s*=\s*(?:"((?:\\.|[^"\\])*)"|([^;,\s]*))/g) 547 354 901
62 547 ▶ my ($key, $value) = (lc($1), $2 // $3);
Condition66%2/3
$2 // $3
l!l&&r!l&&!r
54070
MC/DC0%0/2
$2 // $3
$2 $3
Truth table66%2/3
A // B
A= $2B= $3
ABresult
1X1
011
000
63 547 ▶ if ($key eq 'name') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "name") 353 194 547
64 353 $value =~ s/\\(.)/$1/gs;
65 353 push @names, $value;
66 }
67 547 ▶ $extended = 1 if $key =~ /\Aname\*/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /\Aname\*/ 2 545 547
68 547 ▶ $is_file = 1 if $key =~ /\Afilename(?:\*|\z)/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /\Afilename(?:\*|\z)/ 192 355 547
69 }
70 354 return (\@names, $is_file, $extended);
71 }
72  
73  
74 46 ▶sub is_true {
Subroutine is_true: called CC=3 SCAR=11.0
Pod documented
75 46 my ($class, $value) = @_;
76 46 ▶ return $FORM_TRUE{lc($value // '')} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$FORM_TRUE{lc($value // '')} ? : 29 17 46
Condition100%2/2
$value // ""
l!l
451
MC/DC100%1/1
$value // ""
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
77 }
78  
79  
80 100 ▶sub upstream_parts {
Subroutine upstream_parts: called CC=4 SCAR=13.9
Pod documented
81 100 my ($class, $content, $replace) = @_;
82 100 ▶ $replace ||= {};
Condition50%1/2
$replace ||= {}
l!l
1000
MC/DC0%0/1
$replace ||= {}
$replace
Truth table50%1/2
A
A= $replace
Aresult
11
00
83 100 my $source = $content->parts;
84 100 my @parts;
85 100 for my $index (0 .. $#$source) {
86 214 my $part = $source->[$index];
87 214 my %headers = %{$part->headers->to_hash};
88 214 ▶ if (exists $replace->{$index}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $replace->{$index}) 2 212 214
89 2 delete @headers{ grep { lc($_) eq 'content-length' } keys %headers };
90 2 push @parts, { %headers, content => $replace->{$index} };
91 2 next;
92 }
93 212 push @parts, { %headers, file => $part->asset };
94 }
95 100 return \@parts;
96 }
97  
98  
99 66 ▶sub usage_units {
Subroutine usage_units: called CC=7 SCAR=19.5
Pod documented
100 66 my ($class, $payload) = @_;
101 66 ▶ return () unless ref($payload) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $payload eq "HASH" 6 60 66
102 60 my $usage = $payload->{usage};
103 my $seconds = (ref($usage) eq 'HASH' && ($usage->{type} // '') eq 'duration')
104 ? $usage->{seconds}
105 60 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $usage eq 'HASH' && ($usage->{'type'} // '') eq 'duration' ? : 31 29 60
Condition100%5/5
ref $usage eq "HASH" and ($usage->{"type"} // "") eq "duration"
!ll&&!rl&&r
171231
$usage->{"type"} // ""
l!l
358
MC/DC100%2/2
ref $usage eq "HASH" and ($usage->{"type"} // "") eq "duration"
ref $usage eq "HASH" $usage->{"type"}
Truth table100%3/3
A and B
A= ref $usage eq "HASH"B= $usage->{"type"}
ABresult
0X0
100
111
106 60 ▶ $seconds = $payload->{duration} unless $class->_is_measure($seconds);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_is_measure($seconds) 31 29 60
107 60 ▶ return () unless $class->_is_measure($seconds);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_is_measure($seconds) 27 33 60
108 33 return (audio_seconds => 0 + $seconds);
109 }
110  
111 # A finite number that is not negative: what a count or a duration off the wire has to be.
112 120 ▶sub _is_measure {
Subroutine _is_measure: called CC=7 SCAR=19.5
113 120 my ($class, $value) = @_;
114 120 ▶ return 0 unless defined($value) && !ref($value) && looks_like_number($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and &looks_like_number($value) 55 65 120
Condition100%6/6
defined $value and not ref $value and &looks_like_number($value)
!ll&&!rl&&r
53265
defined $value and not ref $value
!ll&&!rl&&r
52167
MC/DC100%3/3
defined $value and not ref $value and &looks_like_number($value)
defined $value not ref $value &looks_like_number($value)
Truth table100%4/4
A and not(B) and C
A= defined $valueB= not ref $valueC= &looks_like_number($value)
ABCresult
0XX0
10X0
1100
1111
115 65 ▶ return ($value >= 0 && $value == $value && $value != 9**9**9) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value >= 0 && $value == $value && $value != 9**9**9 ? : 62 3 65
Condition83%5/6
$value >= 0 and $value == $value and $value != "Inf"
!ll&&!rl&&r
2162
$value >= 0 and $value == $value
!ll&&!rl&&r
2063
MC/DC66%2/3
$value >= 0 and $value == $value and $value != "Inf"
$value >= 0 $value == $value $value != "Inf"
Truth table75%3/4
A and B and C
A= $value >= 0B= $value == $valueC= $value != "Inf"
ABCresult
0XX0
10X0
1100
1111
116 }
117  
118  
119 59 ▶sub delta_text {
Subroutine delta_text: called CC=6 SCAR=17.9
Pod documented
120 59 my ($class, $frame) = @_;
121 59 ▶ return unless ref($frame) eq 'HASH' && ($frame->{type} // '') eq 'transcript.text.delta';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $frame eq "HASH" and ($frame->{"type"} // "") eq "transcript.text.delta" 48 11 59
Condition80%4/5
ref $frame eq "HASH" and ($frame->{"type"} // "") eq "transcript.text.delta"
!ll&&!rl&&r
04811
$frame->{"type"} // ""
l!l
1445
MC/DC50%1/2
ref $frame eq "HASH" and ($frame->{"type"} // "") eq "transcript.text.delta"
ref $frame eq "HASH" $frame->{"type"}
Truth table66%2/3
A and B
A= ref $frame eq "HASH"B= $frame->{"type"}
ABresult
0X0
100
111
122 11 my $text = $frame->{delta};
123 11 ▶ return unless defined($text) && !ref($text);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 2 9 11
Condition100%3/3
defined $text and not ref $text
!ll&&!rl&&r
119
MC/DC100%2/2
defined $text and not ref $text
defined $text not ref $text
Truth table100%3/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
124 9 return $text;
125 }
126  
127  
128 113 1;
129  
130 __END__
« lib/Langertha/Skeid/Protocol/Rerank.pm ↑ Summary lib/Langertha/Skeid/SwaggerUI.pm »