blib/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
« blib/lib/Langertha/Skeid/Protocol/Ollama/Stream.pm ↑ Summary blib/lib/Langertha/Skeid/CapacityProbe.pm »
1 package Langertha::Skeid::Protocol;
2 70 our $VERSION = '0.003';
3 # ABSTRACT: Shared helpers for Skeid wire-format translation
4 70 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 70 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 70 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 70 ▶use JSON::MaybeXS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 70 ▶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 70 my $TEXT_JSON = JSON::MaybeXS->new(utf8 => 0, canonical => 1);
13  
14  
15 767 ▶sub iso8601_now {
Subroutine iso8601_now: called CC=1 SCAR=0.0
Pod documented
16 767 return strftime('%Y-%m-%dT%H:%M:%SZ', gmtime());
17 }
18  
19  
20 636 ▶sub encode_json_safe {
Subroutine encode_json_safe: called CC=3 SCAR=11.4
Pod documented
21 636 my ($value) = @_;
22 636 ▶ return '{}' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 636 636
23 636 ▶ return eval { encode_json($value) } || '{}';
Condition50%1/2
eval { || '{}'
l!l
6360
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 24 ▶sub encode_json_text_safe {
Subroutine encode_json_text_safe: called CC=3 SCAR=11.4
Pod documented
28 24 my ($value) = @_;
29 24 ▶ return '{}' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 24 24
30 24 ▶ return eval { $TEXT_JSON->encode($value) } || '{}';
Condition50%1/2
eval { || '{}'
l!l
240
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 742 ▶sub utf8_length {
Subroutine utf8_length: called CC=2 SCAR=7.0
Pod documented
35 742 my ($text) = @_;
36 742 ▶ return 0 unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 742 742
37 742 utf8::encode(my $octets = $text);
38 742 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 13 ▶sub image_url_part {
Subroutine image_url_part: called CC=6 SCAR=18.2
Pod documented
65 13 my ($url, $base64, $media_type) = @_;
66 13 ▶ unless (defined($url) && length($url)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $url and length $url) 11 2 13
Condition66%2/3
defined $url and length $url
!ll&&!rl&&r
1102
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 11 ▶ $base64 //= '';
Condition50%1/2
$base64 //= ""
l!l
110
MC/DC0%0/1
$base64 //= ""
$base64
Truth table50%1/2
A
A= $base64
Aresult
11
00
68 11 ▶ $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 3 11
Condition66%2/3
defined $media_type and length $media_type
!ll&&!rl&&r
803
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 11 $url = "data:$media_type;base64,$base64";
70 }
71 13 return { type => 'image_url', image_url => { url => "$url" } };
72 }
73  
74  
75 18 ▶sub openai_manifest_endpoint {
Subroutine openai_manifest_endpoint: called CC=1 SCAR=0.0
Pod documented
76 return {
77 18 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 70 1;
93  
94 __END__
95  
96 =pod
97  
98 =encoding UTF-8
99  
100 =head1 NAME
101  
102 Langertha::Skeid::Protocol - Shared helpers for Skeid wire-format translation
103  
104 =head1 VERSION
105  
106 version 0.003
107  
108 =head1 DESCRIPTION
109  
110 Skeid speaks several client dialects but makes exactly one kind of upstream call: an
111 OpenAI-shaped C<POST> to the selected node. Every other API format is translated in on the way
112 up and out on the way back, by a module under this namespace — one per format.
113  
114 That is the whole rule, and it is load-bearing: a format-specific field name belongs inside its
115 own translator and nowhere else. Routing, admission, usage accounting and the upstream request
116 builder never learn that Anthropic calls it C<system> or that Ollama calls it
117 C<prompt_eval_count>. See F<docs/adr/0001-one-upstream-call-shape-all-client-formats-translated.md>.
118  
119 This module itself holds only the handful of helpers the translators share. They are plain
120 functions, called fully qualified (C<Langertha::Skeid::Protocol::utf8_length($text)>) and not
121 exported; only L</openai_manifest_endpoint> is a class method.
122  
123 =head2 iso8601_now
124  
125 my $now = Langertha::Skeid::Protocol::iso8601_now(); # '2026-09-29T16:54:00Z'
126  
127 Current UTC time as C<YYYY-MM-DDTHH:MM:SSZ>.
128  
129 =head2 encode_json_safe
130  
131 my $line = Langertha::Skeid::Protocol::encode_json_safe($payload) . "\n";
132  
133 JSON-encodes a value to UTF-8 B<bytes>, returning C<'{}'> rather than dying on anything
134 unencodable. For a whole wire unit that goes out as-is -- one SSE event or NDJSON line. Never
135 for a string nested inside another JSON document: use L</encode_json_text_safe>.
136  
137 =head2 encode_json_text_safe
138  
139 my $arguments = Langertha::Skeid::Protocol::encode_json_text_safe($block->{input});
140  
141 JSON-encodes a value to a B<character> string, returning C<'{}'> rather than dying. For JSON
142 nested as a string inside a body that is encoded as a whole later -- C<tool_use.input> becoming
143 C<function.arguments>, a structured C<tool_result> becoming a tool message's content. Byte
144 output there would be encoded a second time and every non-ASCII character would reach the
145 model as mojibake.
146 Used where a malformed tool argument must not take the whole request down.
147  
148 =head2 utf8_length
149  
150 my $bytes = Langertha::Skeid::Protocol::utf8_length($text);
151  
152 Length of a character string in UTF-8 B<bytes> -- what a C<content_bytes> count means.
153 C<length> on decoded text counts characters and undercounts every non-ASCII answer.
154  
155 =head2 decode_json_safe
156  
157 my $data = Langertha::Skeid::Protocol::decode_json_safe($bytes); # or undef
158  
159 Decodes a JSON string of UTF-8 B<bytes> (a raw body or SSE payload), returning C<undef> instead
160 of dying. Not for text that is already characters, such as C<function.arguments> read from a
161 decoded body. A reference is passed through unchanged, so it is safe to call on a value that
162 may already be decoded.
163  
164 =head2 image_media_type
165  
166 my $mt = Langertha::Skeid::Protocol::image_media_type($base64); # 'image/jpeg'
167  
168 The media type of a base64-encoded image, read from its magic bytes: PNG, JPEG, GIF and WebP
169 are recognised, anything else is reported as C<image/png>. For a client format that sends
170 raw base64 without saying what it is (Ollama's C<images>), so the data URL the OpenAI
171 upstream needs can name a type.
172  
173 =head2 image_url_part
174  
175 my $part = Langertha::Skeid::Protocol::image_url_part($url);
176 my $part = Langertha::Skeid::Protocol::image_url_part(undef, $base64, $media_type);
177  
178 An OpenAI C<image_url> content part: C<< { type => 'image_url', image_url => { url => ... } } >>.
179 Given a URL, it is used as is. Given base64 data, it becomes a C<data:> URL with
180 C<$media_type>, or with L</image_media_type> when none is given.
181  
182 =head2 openai_manifest_endpoint
183  
184 my $spec = Langertha::Skeid::Protocol->openai_manifest_endpoint;
185 # { dialect => 'openai-chat', path => '/v1', capabilities => [ ... ] }
186  
187 How the OpenAI face (C</v1/chat/completions>) appears in the provider manifest (skeid #29).
188 That face is the upstream call shape itself: its body goes to the node untranslated, apart from
189 the model name, so it carries every OpenAI-chat request field a model entry may claim.
190 C<capabilities> is the list of those flags; a model is published on this face with the
191 capabilities declared for it, cut down to this list. The translated faces carry their own
192 spec (L<Langertha::Skeid::Protocol::Anthropic/manifest_endpoint>,
193 L<Langertha::Skeid::Protocol::Ollama/manifest_endpoint>).
194  
195 =head1 SEE ALSO
196  
197 =over 4
198  
199 =item * L<Langertha::Skeid::Protocol::Anthropic>, L<Langertha::Skeid::Protocol::Anthropic::Stream>
200  
201 =item * L<Langertha::Skeid::Protocol::Ollama>, L<Langertha::Skeid::Protocol::Ollama::Stream>
202  
203 =item * L<Langertha::Skeid::Proxy> -- the routes that use them
204  
205 =back
206  
207 =head1 SUPPORT
208  
209 =head2 Issues
210  
211 Please report bugs and feature requests on GitHub at
212 L<https://github.com/Getty/langertha-skeid/issues>.
213  
214 =head2 IRC
215  
216 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
217  
218 =head1 CONTRIBUTING
219  
220 Contributions are welcome! Please fork the repository and submit a pull request.
221  
222 =head1 AUTHOR
223  
224 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
225  
226 =head1 COPYRIGHT AND LICENSE
227  
228 This software is copyright (c) 2026 by Torsten Raudssus.
229  
230 This is free software; you can redistribute it and/or modify it under
231 the same terms as the Perl 5 programming language system itself.
232  
233 =cut
« blib/lib/Langertha/Skeid/Protocol/Ollama/Stream.pm ↑ Summary blib/lib/Langertha/Skeid/CapacityProbe.pm »