lib/Langertha/Skeid/Protocol/Realtime.pm

filter: Statementstmt 100% 111 / 111 Branchbran 91.1% 62 / 68 Conditioncond 66.6% 56 / 84 MC/DCmcdc 37.5% 15 / 40 Subroutinesub 100% 17 / 17 Podpod 100% 11 / 11 totaltotal 82.1% 272 / 331 CC 68 SCAR 23.7
CC
68
Cov
87%
add_usage
18.8
__ANON__
13.5
server_frame
12.6
CRAP 10.7
« lib/Langertha/Skeid/Protocol/Documents.pm ↑ Summary lib/Langertha/Skeid/CustomerSource.pm »
1 package Langertha::Skeid::Protocol::Realtime;
2 87 our $VERSION = '0.004';
3 # ABSTRACT: The OpenAI realtime route -- what a session's handshake and frames say, relayed untranslated
4 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 87 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 87 my $ROUTE = '/v1/realtime';
12 87 my $UPSTREAM_PATH = '/realtime';
13 87 my $KEY_PREFIX = 'openai-insecure-api-key.';
14 87 my $SUBPROTOCOL = 'realtime';
15 87 my $MODEL_MAX = 1024;
16  
17 87 my $COMPLETED = 'conversation.item.input_audio_transcription.completed';
18 87 my $DELTA = 'conversation.item.input_audio_transcription.delta';
19  
20 # What Skeid decodes with, and what it encodes the frames it forwards with. Strict on duplicate
21 # keys where the decoder is: a document with two readings is not forwarded, because Skeid and
22 # the node could take different ones.
23 87 my $JSON = JSON::MaybeXS->new(utf8 => 1, canonical => 1, convert_blessed => 1);
24  
25  
26 156 ▶sub route { return $ROUTE }
Subroutine route: called CC=1 SCAR=0.0
Pod documented
27 91 ▶sub upstream_path { return $UPSTREAM_PATH }
Subroutine upstream_path: called CC=1 SCAR=0.0
Pod documented
28  
29  
30 104 ▶sub query_model {
Subroutine query_model: called CC=6 SCAR=17.9
Pod documented
31 104 my ($class, $query) = @_;
32 104 my @models = @{ $query->every_param('model') };
33 104 ▶ return (undef, "The 'model' query parameter is required") unless @models;
Branch: 1 branch
BranchTrueFalseTotal
unless @models 3 101 104
34 101 ▶ return (undef, "The 'model' query parameter may be given only once") if @models > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @models > 1 2 99 101
35 99 my $model = $models[0];
36 99 ▶ return (undef, "The 'model' query parameter is empty or too long")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model and length $model and length $model <= $MODEL_MAX 3 96 99
Condition83%5/6
defined $model and length $model and length $model <= $MODEL_MAX
!ll&&!rl&&r
2196
defined $model and length $model
!ll&&!rl&&r
0297
MC/DC66%2/3
defined $model and length $model and length $model <= $MODEL_MAX
defined $model length $model length $model <= $MODEL_MAX
Truth table75%3/4
A and B and C
A= defined $modelB= length $modelC= length $model <= $MODEL_MAX
ABCresult
0XX0
10X0
1100
1111
37 unless defined($model) && length($model) && length($model) <= $MODEL_MAX;
38 96 return ($model, undef);
39 }
40  
41  
42 93 ▶sub upstream_url {
Subroutine upstream_url: called CC=7 SCAR=19.6
Pod documented
43 93 my ($class, $endpoint, $served_model, $query) = @_;
44 93 my $url = Mojo::URL->new($endpoint);
45 93 ▶ my $scheme = lc($url->scheme // 'http');
Condition50%1/2
$url->scheme // "http"
l!l
930
MC/DC0%0/1
$url->scheme // "http"
$url->scheme
Truth table50%1/2
A
A= $url->scheme
Aresult
11
00
46 93 ▶ $url->scheme($scheme eq 'https' || $scheme eq 'wss' ? 'wss' : 'ws');
Branch: 1 branch
BranchTrueFalseTotal
$scheme eq 'https' || $scheme eq 'wss' ? : 1 92 93
Condition66%2/3
$scheme eq "https" or $scheme eq "wss"
l!l&&r!l&&!r
1092
MC/DC50%1/2
$scheme eq "https" or $scheme eq "wss"
$scheme eq "https" $scheme eq "wss"
Truth table66%2/3
A or B
A= $scheme eq "https"B= $scheme eq "wss"
ABresult
1X1
011
000
47 93 my @pairs = ('model', $served_model);
48 93 ▶ if ($query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query) 93 0 93
49 93 my $all = $query->pairs;
50 93 ▶ for (my $i = 0; $i < @$all; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$all) 177 93 270
51 177 ▶ next if $all->[$i] eq 'model';
Branch: 1 branch
BranchTrueFalseTotal
if $all->[$i] eq "model" 93 84 177
52 84 push @pairs, $all->[$i], $all->[$i + 1];
53 }
54 }
55 93 $url->query(\@pairs);
56 93 return $url->to_string;
57 }
58  
59  
60 93 ▶sub subprotocol_key {
Subroutine subprotocol_key: called CC=6 SCAR=17.9
Pod documented
61 93 my ($class, $header) = @_;
62 93 ▶ return unless defined($header) && length($header);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header and length $header 83 10 93
Condition66%2/3
defined $header and length $header
!ll&&!rl&&r
83010
MC/DC50%1/2
defined $header and length $header
defined $header length $header
Truth table66%2/3
A and B
A= defined $headerB= length $header
ABresult
0X0
100
111
63 10 for my $entry (split /,/, $header) {
64 15 $entry =~ s/\A\s+//;
65 15 $entry =~ s/\s+\z//;
66 15 ▶ next unless index($entry, $KEY_PREFIX) == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($entry, $KEY_PREFIX) == 0 6 9 15
67 9 my $key = substr($entry, length $KEY_PREFIX);
68 9 ▶ return $key if length $key;
Branch: 1 branch
BranchTrueFalseTotal
if length $key 8 1 9
69 }
70 2 return;
71 }
72  
73  
74 89 ▶sub subprotocol { return $SUBPROTOCOL }
Subroutine subprotocol: called CC=1 SCAR=0.0
Pod documented
75  
76  
77 92 ▶sub client_frame {
Subroutine client_frame: called CC=5 SCAR=16.1
Pod documented
78 92 my ($class, $bytes, $requested, $served) = @_;
79 92 my $data = eval { $JSON->decode($bytes) };
80 92 ▶ return { action => 'close', code => 1003, reason => 'frames must be JSON objects' }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 11 81 92
81 unless ref($data) eq 'HASH';
82  
83 81 my ($rewritten, $foreign) = (0, 0);
84 81 my $walk;
85 117 ▶ $walk = sub {
Subroutine __ANON__: called CC=13 SCAR=26.0
86 117 my ($node, $depth) = @_;
87 117 ▶ return if $depth > 64;
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 64 0 117 117
88 117 ▶ if (ref($node) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $node eq 'HASH') { } 116 1 117
elsif (ref $node eq 'ARRAY') { } 1 0 1
89 116 for my $key (keys %$node) {
90 211 my $value = $node->{$key};
91 211 ▶ if ($key eq 'model' && defined($value) && !ref($value)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'model' and defined $value and not ref $value) { } 15 196 211
elsif (ref $value) { } 35 161 196
Condition66%4/6
$key eq "model" and defined $value and not ref $value
!ll&&!rl&&r
196015
$key eq "model" and defined $value
!ll&&!rl&&r
196015
MC/DC33%1/3
$key eq "model" and defined $value and not ref $value
$key eq "model" defined $value not ref $value
Truth table50%2/4
A and B and not(C)
A= $key eq "model"B= defined $valueC= not ref $value
ABCresult
0XX0
10X0
1100
1111
92 15 ▶ if ("$value" eq $requested) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$value" eq $requested) { } 6 9 15
93 6 ▶ if ($served ne $requested) {
Branch: 1 branch
BranchTrueFalseTotal
if ($served ne $requested) 5 1 6
94 5 $node->{$key} = $served;
95 5 $rewritten = 1;
96 }
97 }
98 9 else { $foreign = 1 }
99 }
100 35 elsif (ref $value) { $walk->($value, $depth + 1) }
101 }
102 }
103 elsif (ref($node) eq 'ARRAY') {
104 1 ▶ ref($_) && $walk->($_, $depth + 1) for @$node;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ and &$walk($_, $depth + 1) 1 0 1
105 }
106 117 return;
107 81 };
108 81 $walk->($data, 0);
109 81 undef $walk;
110  
111 81 ▶ if ($foreign) {
Branch: 1 branch
BranchTrueFalseTotal
if ($foreign) 9 72 81
112 9 my $client_event = $data->{event_id};
113 return {
114 9 ▶ action => 'refuse',
Branch: 1 branch
BranchTrueFalseTotal
defined $client_event && !ref($client_event) ? : 7 2 9
Condition66%2/3
defined $client_event and not ref $client_event
!ll&&!rl&&r
207
MC/DC50%1/2
defined $client_event and not ref $client_event
defined $client_event not ref $client_event
Truth table66%2/3
A and not(B)
A= defined $client_eventB= not ref $client_event
ABresult
0X0
100
111
115 text => $class->error_event(
116 'invalid_request_error', 'model_not_allowed',
117 "The model of a session cannot be changed; this session runs '$requested'",
118 (defined($client_event) && !ref($client_event) ? $client_event : undef),
119 ),
120 };
121 }
122 72 return { action => 'forward', text => $JSON->encode($data), rewritten => $rewritten };
123 }
124  
125  
126 9 ▶sub error_event {
Subroutine error_event: called CC=1 SCAR=0.0
Pod documented
127 9 my ($class, $type, $code, $message, $client_event_id) = @_;
128 return $JSON->encode({
129 type => 'error',
130 9 event_id => 'event_skeid_' . join('', map { sprintf '%02x', int rand 256 } 1 .. 8),
131 error => {
132 type => $type,
133 code => $code,
134 message => $message,
135 param => undef,
136 event_id => $client_event_id,
137 },
138 });
139 }
140  
141  
142 25 ▶sub server_frame {
Subroutine server_frame: called CC=12 SCAR=25.3
Pod documented
143 25 my ($class, $bytes) = @_;
144 25 my $data = eval { $JSON->decode($bytes) };
145 25 ▶ return { type => undef, usage => undef, delta_text => undef, error => 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 1 24 25
146 unless ref($data) eq 'HASH';
147 24 my $type = $data->{type};
148 24 ▶ $type = undef if ref $type;
Branch: 1 branch
BranchTrueFalseTotal
if ref $type 0 24 24
149 my $usage = (defined($type) && $type eq $COMPLETED && ref($data->{usage}) eq 'HASH')
150 24 ▶ ? $data->{usage} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $type && $type eq $COMPLETED && ref $data->{'usage'} eq 'HASH' ? : 9 15 24
Condition83%5/6
defined $type and $type eq $COMPLETED and ref $data->{"usage"} eq "HASH"
!ll&&!rl&&r
1419
defined $type and $type eq $COMPLETED
!ll&&!rl&&r
01410
MC/DC66%2/3
defined $type and $type eq $COMPLETED and ref $data->{"usage"} eq "HASH"
defined $type $type eq $COMPLETED ref $data->{"usage"} eq "HASH"
Truth table75%3/4
A and B and C
A= defined $typeB= $type eq $COMPLETEDC= ref $data->{"usage"} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
151 my $text = (defined($type) && $type eq $DELTA && defined($data->{delta}) && !ref($data->{delta}))
152 24 ▶ ? $data->{delta} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $type && $type eq $DELTA && defined $data->{'delta'} && !ref($data->{'delta'}) ? : 9 15 24
Condition66%6/9
defined $type and $type eq $DELTA and defined $data->{"delta"} and not ref $data->{"delta"}
!ll&&!rl&&r
1509
defined $type and $type eq $DELTA and defined $data->{"delta"}
!ll&&!rl&&r
1509
defined $type and $type eq $DELTA
!ll&&!rl&&r
0159
MC/DC25%1/4
defined $type and $type eq $DELTA and defined $data->{"delta"} and not ref $data->{"delta"}
defined $type $type eq $DELTA defined $data->{"delta"} not ref $data->{"delta"}
Truth table40%2/5
A and B and C and not(D)
A= defined $typeB= $type eq $DELTAC= defined $data->{"delta"}D= not ref $data->{"delta"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
153 return {
154 24 ▶ type => $type,
Branch: 1 branch
BranchTrueFalseTotal
defined $type && $type eq 'error' ? : 2 22 24
Condition66%2/3
defined $type and $type eq "error"
!ll&&!rl&&r
0222
MC/DC50%1/2
defined $type and $type eq "error"
defined $type $type eq "error"
Truth table66%2/3
A and B
A= defined $typeB= $type eq "error"
ABresult
0X0
100
111
155 usage => $usage,
156 delta_text => $text,
157 error => (defined($type) && $type eq 'error') ? 1 : 0,
158 };
159 }
160  
161  
162 12 ▶sub add_usage {
Subroutine add_usage: called CC=16 SCAR=29.3
Pod documented
163 12 my ($class, $sum, $block) = @_;
164 12 ▶ $sum = {} unless ref($sum) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sum eq "HASH" 7 5 12
165 12 ▶ return $sum unless ref($block) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $block eq "HASH" 0 12 12
166 12 for my $key (keys %$block) {
167 38 my $value = $block->{$key};
168 38 ▶ if (ref($value) eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $value eq 'HASH') { } 2 36 38
elsif (not ref $value and defined $value and &looks_like_number($value) and $value == $value and $value != 9**9**9 and $value != -9**9**9) { } 26 10 36
elsif (not ref $value and defined $value and not exists $sum->{$key}) { } 7 3 10
169 2 ▶ $sum->{$key} = $class->add_usage(ref($sum->{$key}) eq 'HASH' ? $sum->{$key} : {}, $value);
Branch: 1 branch
BranchTrueFalseTotal
ref $sum->{$key} eq 'HASH' ? : 1 1 2
170 }
171 ▶ elsif (!ref($value) && defined($value) && looks_like_number($value)
Condition60%9/15
not ref $value and defined $value and &looks_like_number($value) and $value == $value and $value != "Inf" and $value != "-Inf"
!ll&&!rl&&r
10026
not ref $value and defined $value and &looks_like_number($value) and $value == $value and $value != "Inf"
!ll&&!rl&&r
10026
not ref $value and defined $value and &looks_like_number($value) and $value == $value
!ll&&!rl&&r
10026
not ref $value and defined $value and &looks_like_number($value)
!ll&&!rl&&r
01026
not ref $value and defined $value
!ll&&!rl&&r
0036
MC/DC16%1/6
not ref $value and defined $value and &looks_like_number($value) and $value == $value and $value != "Inf" and $value != "-Inf"
not ref $value defined $value &looks_like_number($value) $value == $value $value != "Inf" $value != "-Inf"
Truth table28%2/7
not(A) and B and C and D and E and F
A= not ref $valueB= defined $valueC= &looks_like_number($value)D= $value == $valueE= $value != "Inf"F= $value != "-Inf"
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
172 && $value == $value && $value != 9**9**9 && $value != -9**9**9) {
173 26 ▶ $sum->{$key} = ($sum->{$key} // 0) + $value;
Condition100%2/2
$sum->{$key} // 0
l!l
917
MC/DC100%1/1
$sum->{$key} // 0
$sum->{$key}
Truth table100%2/2
A
A= $sum->{$key}
Aresult
11
00
174 }
175 ▶ elsif (!ref($value) && defined($value) && !exists $sum->{$key}) {
Condition50%3/6
not ref $value and defined $value and not exists $sum->{$key}
!ll&&!rl&&r
037
not ref $value and defined $value
!ll&&!rl&&r
0010
MC/DC33%1/3
not ref $value and defined $value and not exists $sum->{$key}
not ref $value defined $value not exists $sum->{$key}
Truth table50%2/4
not(A) and B and not(C)
A= not ref $valueB= defined $valueC= not exists $sum->{$key}
ABCresult
0XX0
10X0
1100
1111
176 7 $sum->{$key} = $value;
177 }
178 }
179 12 return $sum;
180 }
181  
182  
183 12 ▶sub seconds {
Subroutine seconds: called CC=10 SCAR=24.3
Pod documented
184 12 my ($class, $block) = @_;
185 12 ▶ return unless ref($block) eq 'HASH' && ($block->{type} // '') eq 'duration';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $block eq "HASH" and ($block->{"type"} // "") eq "duration" 5 7 12
Condition60%3/5
ref $block eq "HASH" and ($block->{"type"} // "") eq "duration"
!ll&&!rl&&r
057
$block->{"type"} // ""
l!l
120
MC/DC0%0/2
ref $block eq "HASH" and ($block->{"type"} // "") eq "duration"
ref $block eq "HASH" $block->{"type"}
Truth table33%1/3
A and B
A= ref $block eq "HASH"B= $block->{"type"}
ABresult
0X0
100
111
186 7 my $s = $block->{seconds};
187 7 ▶ return unless defined($s) && !ref($s) && looks_like_number($s) && $s >= 0 && $s != 9**9**9
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s and &looks_like_number($s) and $s >= 0 and $s != "Inf" and $s == $s 2 5 7
Condition66%10/15
defined $s and not ref $s and &looks_like_number($s) and $s >= 0 and $s != "Inf" and $s == $s
!ll&&!rl&&r
205
defined $s and not ref $s and &looks_like_number($s) and $s >= 0 and $s != "Inf"
!ll&&!rl&&r
205
defined $s and not ref $s and &looks_like_number($s) and $s >= 0
!ll&&!rl&&r
115
defined $s and not ref $s and &looks_like_number($s)
!ll&&!rl&&r
016
defined $s and not ref $s
!ll&&!rl&&r
007
MC/DC33%2/6
defined $s and not ref $s and &looks_like_number($s) and $s >= 0 and $s != "Inf" and $s == $s
defined $s not ref $s &looks_like_number($s) $s >= 0 $s != "Inf" $s == $s
Truth table42%3/7
A and not(B) and C and D and E and F
A= defined $sB= not ref $sC= &looks_like_number($s)D= $s >= 0E= $s != "Inf"F= $s == $s
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
188 && $s == $s;
189 5 return 0 + $s;
190 }
191  
192  
193 87 1;
194  
195 __END__
« lib/Langertha/Skeid/Protocol/Documents.pm ↑ Summary lib/Langertha/Skeid/CustomerSource.pm »