| 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
| |||||||||||||||||||||||||||||
| 23 | 676 | ▶ | return eval { encode_json($value) } || '{}'; | ||||||||||||||||||||||||||
|
Condition50%1/2
eval {
do {
&encode_json($value)
}
} || '{}'
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)
}
}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 30 | 36 | ▶ | return eval { $TEXT_JSON->encode($value) } || '{}'; | ||||||||||||||||||||||||||
|
Condition50%1/2
eval {
do {
$TEXT_JSON->encode($value)
}
} || '{}'
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)
}
}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | return undef unless defined $value && length $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $value and length $value
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
| |||||||||||||||||||||||||||||
| 46 | 0 | my $decoded = eval { decode_json($value) }; | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | return $@ ? undef : $decoded; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $base64 and not ref $base64
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 57 | 9 | ▶ | return 'image/jpeg' if $bytes =~ /\A\xFF\xD8\xFF/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 6 | ▶ | return 'image/gif' if $bytes =~ /\AGIF8[79]a/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 4 | ▶ | return 'image/webp' if $bytes =~ /\ARIFF.{4}WEBP/s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $url and length $url
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
| |||||||||||||||||||||||||||||
| 67 | 18 | ▶ | $base64 //= ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$base64 //= ""
MC/DC0%0/1
$base64 //= ""
$base64
Truth table50%1/2
A
A= $base64
| |||||||||||||||||||||||||||||
| 68 | 18 | ▶ | $media_type = image_media_type($base64) unless defined($media_type) && length($media_type); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $media_type and length $media_type
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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||