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 127 our $VERSION = '0.005';
3 # ABSTRACT: The OpenAI audio routes -- the upload form relayed, the answer's usage read
4 127 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 127 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 127 ▶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 127 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 127 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 127 my %FORM_TRUE = map { $_ => 1 } qw(1 true on yes t y);
23  
24  
25 173 ▶sub routes { return sort keys %UPSTREAM_PATH }
Subroutine routes: called CC=1 SCAR=0.0
Pod documented
26  
27  
28 64 ▶sub upstream_path {
Subroutine upstream_path: called CC=2 SCAR=7.2
Pod documented
29 64 my ($class, $route) = @_;
30 64 ▶ return $UPSTREAM_PATH{$route // ''};
Condition50%1/2
$route // ""
l!l
640
MC/DC0%0/1
$route // ""
$route
Truth table50%1/2
A
A= $route
Aresult
11
00
31 }
32  
33  
34 76 ▶sub form_fields {
Subroutine form_fields: called CC=9 SCAR=22.0
Pod documented
35 76 my ($class, $content) = @_;
36 76 my %field;
37 76 my $parts = $content->parts;
38 76 for my $index (0 .. $#$parts) {
39 212 my $part = $parts->[$index];
40 212 ▶ return ({}, 'Nested multipart parts are not supported') if $part->is_multipart;
Branch: 1 branch
BranchTrueFalseTotal
if $part->is_multipart 0 212 212
41 212 ▶ my $disposition = $part->headers->content_disposition // next;
Condition50%1/2
$part->headers->content_disposition // (next)
l!l
2120
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 212 my ($names, $is_file, $extended) = $class->disposition_names($disposition);
43 212 ▶ return ({}, 'A form field name in the extended notation (name*) is not supported')
Branch: 1 branch
BranchTrueFalseTotal
if $extended 1 211 212
44 if $extended;
45 211 ▶ next if $is_file;
Branch: 1 branch
BranchTrueFalseTotal
if $is_file 77 134 211
46 134 ▶ my ($name) = grep { $_ eq 'model' || $_ eq 'stream' } @$names;
Condition100%3/3
$_ eq 'model' || $_ eq 'stream'
l!l&&r!l&&!r
832923
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 134 ▶ next unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 22 112 134
48 112 my $asset = $part->asset;
49 112 ▶ push @{$field{$name}}, {
Branch: 1 branch
BranchTrueFalseTotal
$asset->size > $FIELD_MAX ? : 1 111 112
50 index => $index,
51 value => ($asset->size > $FIELD_MAX ? undef : $asset->slurp),
52 };
53 }
54 75 return (\%field, undef);
55 }
56  
57  
58 358 ▶sub disposition_names {
Subroutine disposition_names: called CC=6 SCAR=17.9
Pod documented
59 358 my ($class, $disposition) = @_;
60 358 my (@names, $is_file, $extended);
61 358 ▶ while ($disposition =~ /(?:\A|[;,])\s*([^\s=;,"]+)\s*=\s*(?:"((?:\\.|[^"\\])*)"|([^;,\s]*))/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($disposition =~ /(?:\A|[;,])\s*([^\s=;,"]+)\s*=\s*(?:"((?:\\.|[^"\\])*)"|([^;,\s]*))/g) 553 358 911
62 553 ▶ my ($key, $value) = (lc($1), $2 // $3);
Condition66%2/3
$2 // $3
l!l&&r!l&&!r
54670
MC/DC0%0/2
$2 // $3
$2 $3
Truth table66%2/3
A // B
A= $2B= $3
ABresult
1X1
011
000
63 553 ▶ if ($key eq 'name') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "name") 357 196 553
64 357 $value =~ s/\\(.)/$1/gs;
65 357 push @names, $value;
66 }
67 553 ▶ $extended = 1 if $key =~ /\Aname\*/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /\Aname\*/ 2 551 553
68 553 ▶ $is_file = 1 if $key =~ /\Afilename(?:\*|\z)/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /\Afilename(?:\*|\z)/ 194 359 553
69 }
70 358 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 102 ▶sub upstream_parts {
Subroutine upstream_parts: called CC=4 SCAR=13.9
Pod documented
81 102 my ($class, $content, $replace) = @_;
82 102 ▶ $replace ||= {};
Condition50%1/2
$replace ||= {}
l!l
1020
MC/DC0%0/1
$replace ||= {}
$replace
Truth table50%1/2
A
A= $replace
Aresult
11
00
83 102 my $source = $content->parts;
84 102 my @parts;
85 102 for my $index (0 .. $#$source) {
86 218 my $part = $source->[$index];
87 218 my %headers = %{$part->headers->to_hash};
88 218 ▶ if (exists $replace->{$index}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $replace->{$index}) 2 216 218
89 2 delete @headers{ grep { lc($_) eq 'content-length' } keys %headers };
90 2 push @parts, { %headers, content => $replace->{$index} };
91 2 next;
92 }
93 216 push @parts, { %headers, file => $part->asset };
94 }
95 102 return \@parts;
96 }
97  
98  
99 68 ▶sub usage_units {
Subroutine usage_units: called CC=7 SCAR=19.5
Pod documented
100 68 my ($class, $payload) = @_;
101 68 ▶ return () unless ref($payload) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $payload eq "HASH" 6 62 68
102 62 my $usage = $payload->{usage};
103 my $seconds = (ref($usage) eq 'HASH' && ($usage->{type} // '') eq 'duration')
104 ? $usage->{seconds}
105 62 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $usage eq 'HASH' && ($usage->{'type'} // '') eq 'duration' ? : 33 29 62
Condition100%5/5
ref $usage eq "HASH" and ($usage->{"type"} // "") eq "duration"
!ll&&!rl&&r
171233
$usage->{"type"} // ""
l!l
378
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 62 ▶ $seconds = $payload->{duration} unless $class->_is_measure($seconds);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_is_measure($seconds) 31 31 62
107 62 ▶ return () unless $class->_is_measure($seconds);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_is_measure($seconds) 27 35 62
108 35 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 124 ▶sub _is_measure {
Subroutine _is_measure: called CC=7 SCAR=19.5
113 124 my ($class, $value) = @_;
114 124 ▶ 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 69 124
Condition100%6/6
defined $value and not ref $value and &looks_like_number($value)
!ll&&!rl&&r
53269
defined $value and not ref $value
!ll&&!rl&&r
52171
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 69 ▶ return ($value >= 0 && $value == $value && $value != 9**9**9) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value >= 0 && $value == $value && $value != 9**9**9 ? : 66 3 69
Condition83%5/6
$value >= 0 and $value == $value and $value != "Inf"
!ll&&!rl&&r
2166
$value >= 0 and $value == $value
!ll&&!rl&&r
2067
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 127 1;
129  
130 __END__
« lib/Langertha/Skeid/Protocol/Rerank.pm ↑ Summary lib/Langertha/Skeid/SwaggerUI.pm »