lib/Langertha/Skeid/Protocol.pm

filter: Statementstmt 88.6% 47 / 53 Branchbran 61.5% 16 / 26 Conditioncond 44.4% 8 / 18 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 92.3% 12 / 13 Podpod 100% 8 / 8 totaltotal 72.0% 93 / 129 CC 21 SCAR 20.9
CC
21
Cov
73%
decode_json_safe
30.0
image_media_type
7.1
image_url_part
6.2
CRAP 8.1
« lib/Langertha/Skeid/Proxy/Realtime.pm ↑ Summary lib/Langertha/Skeid/CapacityProbe.pm »
1 package Langertha::Skeid::Protocol;
2 116 our $VERSION = '0.004';
3 # ABSTRACT: Shared helpers for Skeid wire-format translation
4 116 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 116 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 116 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 116 ▶use JSON::MaybeXS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 116 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Nested JSON (a document carried as a string inside another) must be characters: the body
11 # around it is byte-encoded once when it is sent (skeid #33, core k252).
12 116 my $TEXT_JSON = JSON::MaybeXS->new(utf8 => 0, canonical => 1);
13  
14  
15 1386 ▶sub iso8601_now {
Subroutine iso8601_now: called CC=1 SCAR=0.0
Pod documented
16 1386 return strftime('%Y-%m-%dT%H:%M:%SZ', gmtime());
17 }
18  
19  
20 676 ▶sub encode_json_safe {
Subroutine encode_json_safe: called CC=3 SCAR=11.4
Pod documented
21 676 my ($value) = @_;
22 676 ▶ return '{}' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 676 676
23 676 ▶ return eval { encode_json($value) } || '{}';
Condition50%1/2
eval { do { &encode_json($value) } } || '{}'
l!l
6760
MC/DC0%0/1
eval { do { &encode_json($value) } } || '{}'
eval { do { &encode_json($value) } }
Truth table50%1/2
A
A= eval { do { &encode_json($value) } }
Aresult
11
00
24 }
25  
26  
27 36 ▶sub encode_json_text_safe {
Subroutine encode_json_text_safe: called CC=3 SCAR=11.4
Pod documented
28 36 my ($value) = @_;
29 36 ▶ return '{}' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 36 36
30 36 ▶ return eval { $TEXT_JSON->encode($value) } || '{}';
Condition50%1/2
eval { do { $TEXT_JSON->encode($value) } } || '{}'
l!l
360
MC/DC0%0/1
eval { do { $TEXT_JSON->encode($value) } } || '{}'
eval { do { $TEXT_JSON->encode($value) } }
Truth table50%1/2
A
A= eval { do { $TEXT_JSON->encode($value) } }
Aresult
11
00
31 }
32  
33  
34 830 ▶sub utf8_length {
Subroutine utf8_length: called CC=2 SCAR=7.0
Pod documented
35 830 my ($text) = @_;
36 830 ▶ return 0 unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 830 830
37 830 utf8::encode(my $octets = $text);
38 830 return length $octets;
39 }
40  
41  
42 0 ▶sub decode_json_safe {
Subroutine decode_json_safe: not called CC=5 SCAR=34.0
Pod documented
43 0 my ($value) = @_;
44 0 ▶ return $value if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 0 0
45 0 ▶ return undef unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless 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
46 0 my $decoded = eval { decode_json($value) };
47 0 ▶ return $@ ? undef : $decoded;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
48 }
49  
50  
51 13 ▶sub image_media_type {
Subroutine image_media_type: called CC=7 SCAR=19.6
Pod documented
52 13 my ($base64) = @_;
53 13 ▶ return 'image/png' unless defined($base64) && !ref($base64);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base64 and not ref $base64 0 13 13
Condition33%1/3
defined $base64 and not ref $base64
!ll&&!rl&&r
0013
MC/DC0%0/2
defined $base64 and not ref $base64
defined $base64 not ref $base64
Truth table33%1/3
A and not(B)
A= defined $base64B= not ref $base64
ABresult
0X0
100
111
54 13 (my $head = substr($base64, 0, 64)) =~ s/\s+//g;
55 13 my $bytes = decode_base64(substr($head, 0, 16));
56 13 ▶ return 'image/png' if $bytes =~ /\A\x89PNG\r\n\x1a\n/;
Branch: 1 branch
BranchTrueFalseTotal
if $bytes =~ /\A\x89PNG\r\n\x1a\n/ 4 9 13
57 9 ▶ return 'image/jpeg' if $bytes =~ /\A\xFF\xD8\xFF/;
Branch: 1 branch
BranchTrueFalseTotal
if $bytes =~ /\A\xFF\xD8\xFF/ 3 6 9
58 6 ▶ return 'image/gif' if $bytes =~ /\AGIF8[79]a/;
Branch: 1 branch
BranchTrueFalseTotal
if $bytes =~ /\AGIF8[79]a/ 2 4 6
59 4 ▶ return 'image/webp' if $bytes =~ /\ARIFF.{4}WEBP/s;
Branch: 1 branch
BranchTrueFalseTotal
if $bytes =~ /\ARIFF.{4}WEBP/s 2 2 4
60 2 return 'image/png';
61 }
62  
63  
64 20 ▶sub image_url_part {
Subroutine image_url_part: called CC=6 SCAR=18.2
Pod documented
65 20 my ($url, $base64, $media_type) = @_;
66 20 ▶ unless (defined($url) && length($url)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $url and length $url) 18 2 20
Condition66%2/3
defined $url and length $url
!ll&&!rl&&r
1802
MC/DC50%1/2
defined $url and length $url
defined $url length $url
Truth table66%2/3
A and B
A= defined $urlB= length $url
ABresult
0X0
100
111
67 18 ▶ $base64 //= '';
Condition50%1/2
$base64 //= ""
l!l
180
MC/DC0%0/1
$base64 //= ""
$base64
Truth table50%1/2
A
A= $base64
Aresult
11
00
68 18 ▶ $media_type = image_media_type($base64) unless defined($media_type) && length($media_type);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $media_type and length $media_type 8 10 18
Condition66%2/3
defined $media_type and length $media_type
!ll&&!rl&&r
8010
MC/DC50%1/2
defined $media_type and length $media_type
defined $media_type length $media_type
Truth table66%2/3
A and B
A= defined $media_typeB= length $media_type
ABresult
0X0
100
111
69 18 $url = "data:$media_type;base64,$base64";
70 }
71 20 return { type => 'image_url', image_url => { url => "$url" } };
72 }
73  
74  
75 19 ▶sub openai_manifest_endpoint {
Subroutine openai_manifest_endpoint: called CC=1 SCAR=0.0
Pod documented
76 return {
77 19 dialect => 'openai-chat',
78 path => '/v1',
79 capabilities => [qw(
80 chat streaming system_prompt
81 tools_native tools_hermes
82 tool_choice_auto tool_choice_any tool_choice_none tool_choice_named
83 parallel_tool_use
84 response_format_json_object response_format_json_schema
85 reasoning_effort temperature seed response_size prompt_cache_key
86 image_input
87 )],
88 };
89 }
90  
91  
92 116 1;
93  
94 __END__
« lib/Langertha/Skeid/Proxy/Realtime.pm ↑ Summary lib/Langertha/Skeid/CapacityProbe.pm »