| 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 // ""
MC/DC0%0/1
$route // ""
$route
Truth table50%1/2
A
A= $route
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 208 | ▶ | my $disposition = $part->headers->content_disposition // next; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$part->headers->content_disposition // (next)
MC/DC0%0/1
$part->headers->content_disposition // (next)
$part->headers->content_disposition
Truth table50%1/2
A
A= $part->headers->content_disposition
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | if $extended; | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 207 | ▶ | next if $is_file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 132 | ▶ | my ($name) = grep { $_ eq 'model' || $_ eq 'stream' } @$names; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$_ eq 'model' || $_ eq 'stream'
MC/DC100%2/2
$_ eq 'model' || $_ eq 'stream'
$_ eq 'model'
$_ eq 'stream'
Truth table100%3/3
A || B
A= $_ eq 'model'B= $_ eq 'stream'
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 132 | ▶ | next unless defined $name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 110 | my $asset = $part->asset; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 110 | ▶ | push @{$field{$name}}, { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 547 | ▶ | my ($key, $value) = (lc($1), $2 // $3); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$2 // $3
MC/DC0%0/2
$2 // $3
$2
$3
Truth table66%2/3
A // B
A= $2B= $3
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 547 | ▶ | if ($key eq 'name') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 353 | $value =~ s/\\(.)/$1/gs; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 353 | push @names, $value; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 547 | ▶ | $extended = 1 if $key =~ /\Aname\*/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 547 | ▶ | $is_file = 1 if $key =~ /\Afilename(?:\*|\z)/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$value // ""
MC/DC100%1/1
$value // ""
$value
Truth table100%2/2
A
A= $value
| |||||||||||||||||||||||||||||||||||||||||||
| 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 ||= {}
MC/DC0%0/1
$replace ||= {}
$replace
Truth table50%1/2
A
A= $replace
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%5/5
ref $usage eq "HASH" and ($usage->{"type"} // "") eq "duration"
$usage->{"type"} // ""
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 60 | ▶ | $seconds = $payload->{duration} unless $class->_is_measure($seconds); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 60 | ▶ | return () unless $class->_is_measure($seconds); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
defined $value and not ref $value and &looks_like_number($value)
defined $value and not ref $value
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 65 | ▶ | return ($value >= 0 && $value == $value && $value != 9**9**9) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$value >= 0 and $value == $value and $value != "Inf"
$value >= 0 and $value == $value
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition80%4/5
ref $frame eq "HASH" and ($frame->{"type"} // "") eq "transcript.text.delta"
$frame->{"type"} // ""
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 11 | my $text = $frame->{delta}; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 11 | ▶ | return unless defined($text) && !ref($text); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $text and not ref $text
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
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 9 | return $text; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 113 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | __END__ | ||||||||||||||||||||||||||||||||||||||||||