lib/Langertha/Skeid/Protocol/Documents.pm

filter: Statementstmt 100% 115 / 115 Branchbran 84.6% 66 / 78 Conditioncond 64.7% 33 / 51 MC/DCmcdc 34.6% 9 / 26 Subroutinesub 100% 18 / 18 Podpod 100% 12 / 12 totaltotal 84.3% 253 / 300 CC 65 SCAR 25.8
CC
65
Cov
88%
json_remote_fields
27.0
pages
15.8
document_count
8.0
CRAP 13.2
« lib/Langertha/Skeid/Protocol/Ollama/Stream.pm ↑ Summary lib/Langertha/Skeid/Protocol/Realtime.pm »
1 package Langertha::Skeid::Protocol::Documents;
2 113 our $VERSION = '0.004';
3 # ABSTRACT: The document conversion routes of docling-serve -- relayed, the pages read off the answer
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 Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 113 ▶use JSON::MaybeXS qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 113 ▶use Langertha::Skeid::Protocol::Audio;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 # The client's path and the path below the node URL it is relayed to. The node URL is a base:
12 # `_endpoint_url_for_node` puts the /v1 in, so these are the paths docling serves below it.
13 113 my %UPSTREAM_PATH = (
14 '/v1/convert/file' => '/convert/file',
15 '/v1/convert/source' => '/convert/source',
16 );
17  
18 # The node kinds, beside the default (chat). Per kind: nothing yet -- a table, because the next
19 # kind is a row here, not a branch in the router.
20 113 my %KIND = (
21 documents => 1,
22 );
23  
24 # The routing key of the documents route. It stands where the model name stands for every other
25 # route: key policies grant it (`models: [qwen, documents]`), an alias of that name tiers the
26 # nodes. It is not the name of a model and no node carries it.
27 113 my $ROUTING_KEY = 'documents';
28  
29 # Largest answer body Skeid decodes to read the page count off it. A converted document is
30 # markdown, JSON, HTML, text and doctags side by side; beyond this the count is not worth the
31 # CPU of decoding it on the request path, and the event simply has no `pages`.
32 113 my $MAX_PARSE_BYTES = 8 * 1024 * 1024;
33  
34 # Request fields by which the node fetches from, or writes to, an address the client names --
35 # a server-side request made from inside the cluster network. Refused unless the operator allows
36 # remote sources (`documents.allow_remote_sources`). The names are read as a form parser reads
37 # them: any case, any white space around, an index or brackets behind (`callbacks[0]`).
38 113 my %REMOTE_FIELD = map { $_ => 1 } qw(callbacks http_sources);
39  
40 # A header name is a token (RFC 7230 3.2.6).
41 113 my $TOKEN = qr/\A[!#\$%&'*+.^_`|~0-9A-Za-z-]+\z/;
42  
43  
44 151 ▶sub routes { return sort keys %UPSTREAM_PATH }
Subroutine routes: called CC=1 SCAR=0.0
Pod documented
45  
46  
47 53 ▶sub upstream_path {
Subroutine upstream_path: called CC=2 SCAR=7.2
Pod documented
48 53 my ($class, $route) = @_;
49 53 ▶ return $UPSTREAM_PATH{$route // ''};
Condition50%1/2
$route // ""
l!l
530
MC/DC0%0/1
$route // ""
$route
Truth table50%1/2
A
A= $route
Aresult
11
00
50 }
51  
52  
53 53 ▶sub api_format { 'docling' }
Subroutine api_format: called CC=1 SCAR=0.0
Pod documented
54  
55  
56 104 ▶sub routing_key { $ROUTING_KEY }
Subroutine routing_key: called CC=1 SCAR=0.0
Pod documented
57  
58  
59 3 ▶sub kinds { return sort keys %KIND }
Subroutine kinds: called CC=1 SCAR=0.0
Pod documented
60  
61  
62 1344 ▶sub normalize_kind {
Subroutine normalize_kind: called CC=6 SCAR=17.9
Pod documented
63 1344 my ($class, $value) = @_;
64 1344 ▶ return '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1310 34 1344
65 34 ▶ croak 'kind must be a string' if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 1 33 34
66 33 my $kind = lc "$value";
67 33 $kind =~ s/\A\s+//;
68 33 $kind =~ s/\s+\z//;
69 33 ▶ return '' if !length($kind) || $kind eq 'chat';
Branch: 1 branch
BranchTrueFalseTotal
if not length $kind or $kind eq "chat" 1 32 33
Condition66%2/3
not length $kind or $kind eq "chat"
l!l&&r!l&&!r
0132
MC/DC50%1/2
not length $kind or $kind eq "chat"
not length $kind $kind eq "chat"
Truth table66%2/3
not(A) or B
A= not length $kindB= $kind eq "chat"
ABresult
1X1
011
000
70 32 ▶ return $kind if $KIND{$kind};
Branch: 1 branch
BranchTrueFalseTotal
if $KIND{$kind} 29 3 32
71 3 croak "unknown kind '$value' (expected one of: chat, " . join(', ', $class->kinds) . ')';
72 }
73  
74  
75 1340 ▶sub normalize_key_header {
Subroutine normalize_key_header: called CC=6 SCAR=17.9
Pod documented
76 1340 my ($class, $value) = @_;
77 1340 ▶ return '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1332 8 1340
78 8 ▶ croak 'api_key_header must be a string' if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 1 7 8
79 7 my $name = "$value";
80 7 ▶ return '' unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 7 7
81 7 ▶ croak "api_key_header '$name' is not a valid HTTP header name" unless $name =~ $TOKEN;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /$TOKEN/ 3 4 7
82 4 ▶ return '' if lc($name) eq 'authorization';
Branch: 1 branch
BranchTrueFalseTotal
if lc $name eq "authorization" 1 3 4
83 3 return $name;
84 }
85  
86  
87 54 ▶sub max_parse_bytes { $MAX_PARSE_BYTES }
Subroutine max_parse_bytes: called CC=1 SCAR=0.0
Pod documented
88  
89  
90 57 ▶sub form_remote_fields {
Subroutine form_remote_fields: called CC=7 SCAR=19.5
Pod documented
91 57 my ($class, $content) = @_;
92 57 my %found;
93 57 my $parts = $content->parts;
94 57 for my $part (@$parts) {
95 74 ▶ return ([], 'Nested multipart parts are not supported') if $part->is_multipart;
Branch: 1 branch
BranchTrueFalseTotal
if $part->is_multipart 0 74 74
96 74 ▶ my $disposition = $part->headers->content_disposition // next;
Condition50%1/2
$part->headers->content_disposition // (next)
l!l
740
MC/DC0%0/1
$part->headers->content_disposition // (next)
$part->headers->content_disposition
Truth table50%1/2
A
A= $part->headers->content_disposition
Aresult
11
00
97 74 my ($names, undef, $extended) = Langertha::Skeid::Protocol::Audio->disposition_names($disposition);
98 74 ▶ return ([], 'A form field name in the extended notation (name*) is not supported') if $extended;
Branch: 1 branch
BranchTrueFalseTotal
if $extended 1 73 74
99 73 for my $name (@$names) {
100 73 ▶ my $field = $class->_remote_field($name) // next;
Condition100%2/2
$class->_remote_field($name) // (next)
l!l
964
MC/DC100%1/1
$class->_remote_field($name) // (next)
$class->_remote_field($name)
Truth table100%2/2
A
A= $class->_remote_field($name)
Aresult
11
00
101 9 $found{$field} = 1;
102 }
103 }
104 56 return ([ sort keys %found ], undef);
105 }
106  
107 # The canonical field name a request field spells, or undef for a field that is not one of the
108 # remote ones: case-folded, white space trimmed, `[]`, `[0]` and `.0` dropped.
109 73 ▶sub _remote_field {
Subroutine _remote_field: called CC=5 SCAR=16.4
110 73 my ($class, $name) = @_;
111 73 ▶ return unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 73 73
112 73 (my $field = lc $name) =~ s/\A\s+//;
113 73 $field =~ s/\s+\z//;
114 73 $field =~ s/(?:\[[^\]]*\]|\.[0-9a-z_]+)+\z//;
115 73 ▶ return $REMOTE_FIELD{$field} || $field eq 'sources' || $field eq 'target' ? $field : undef;
Branch: 1 branch
BranchTrueFalseTotal
$REMOTE_FIELD{$field} || $field eq 'sources' || $field eq 'target' ? : 9 64 73
Condition66%4/6
$REMOTE_FIELD{$field} or $field eq "sources" or $field eq "target"
l!l&&r!l&&!r
9064
$REMOTE_FIELD{$field} or $field eq "sources"
l!l&&r!l&&!r
9064
MC/DC33%1/3
$REMOTE_FIELD{$field} or $field eq "sources" or $field eq "target"
$REMOTE_FIELD{$field} $field eq "sources" $field eq "target"
Truth table50%2/4
A or B or C
A= $REMOTE_FIELD{$field}B= $field eq "sources"C= $field eq "target"
ABCresult
1XX1
01X1
0011
0000
116 }
117  
118  
119 19 ▶sub json_remote_fields {
Subroutine json_remote_fields: called CC=23 SCAR=33.0
Pod documented
120 19 my ($class, $body, $bytes) = @_;
121 19 my %found;
122 19 ▶ return [] unless ref($body) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 0 19 19
123  
124 19 for my $field (qw(http_sources callbacks)) {
125 38 ▶ next unless exists $body->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $body->{$field} 29 9 38
126 9 my $value = $body->{$field};
127 9 ▶ $found{$field} = 1 if defined($value) && !(ref($value) eq 'ARRAY' && !@$value)
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and not ref $value eq "ARRAY" && !@$value and not ref $value eq "HASH" && !%$value 6 3 9
Condition58%7/12
defined $value and not ref $value eq "ARRAY" && !@$value and not ref $value eq "HASH" && !%$value
!ll&&!rl&&r
306
defined $value and not ref $value eq "ARRAY" && !@$value
!ll&&!rl&&r
036
ref $value eq 'ARRAY' && !@$value
!ll&&!rl&&r
063
ref $value eq 'HASH' && !%$value
!ll&&!rl&&r
600
MC/DC20%1/5
defined $value and not ref $value eq "ARRAY" && !@$value and not ref $value eq "HASH" && !%$value
defined $value ref $value eq 'ARRAY' !@$value ref $value eq 'HASH' !%$value
Truth table25%2/8
A and not(B && not(C)) and not(D && not(E))
A= defined $valueB= ref $value eq 'ARRAY'C= !@$valueD= ref $value eq 'HASH'E= !%$value
ABCDEresult
0XXXX0
111XX0
10X110
110110
10X0X1
10X101
1100X1
110101
128 && !(ref($value) eq 'HASH' && !%$value);
129 # Named twice: refused even when the one that wins is empty.
130 9 ▶ if (defined $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bytes) 9 0 9
131 9 my $count = () = $bytes =~ /"\Q$field\E"/g;
132 9 ▶ $found{$field} = 1 if $count > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $count > 1 2 7 9
133 }
134 }
135  
136 19 ▶ if (defined(my $sources = $body->{sources})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $sources = $body->{"sources"})) 3 16 19
137 3 ▶ my @entries = ref($sources) eq 'ARRAY' ? @$sources : ($sources);
Branch: 1 branch
BranchTrueFalseTotal
ref $sources eq 'ARRAY' ? : 3 0 3
138 3 for my $entry (@entries) {
139 my $inline = ref($entry) eq 'HASH'
140 && (defined($entry->{kind}) ? lc($entry->{kind}) eq 'file' : defined($entry->{base64_string}))
141 3 ▶ && !defined($entry->{url});
Branch: 1 branch
BranchTrueFalseTotal
defined $entry->{'kind'} ? : 2 0 2
Condition83%5/6
ref $entry eq 'HASH' && (defined $entry->{'kind'} ? lc $entry->{'kind'} eq 'file' : defined $entry->{'base64_string'}) && !defined($entry->{'url'})
!ll&&!rl&&r
201
ref $entry eq "HASH" and defined $entry->{"kind"} ? lc $entry->{"kind"} eq "file" : defined $entry->{"base64_string"}
!ll&&!rl&&r
111
MC/DC66%2/3
ref $entry eq 'HASH' && (defined $entry->{'kind'} ? lc $entry->{'kind'} eq 'file' : defined $entry->{'base64_string'}) && !defined($entry->{'url'})
ref $entry eq "HASH" defined $entry->{"kind"} ? lc $entry->{"kind"} eq "file" : defined $entry->{"base64_string"} !defined($entry->{'url'})
Truth table75%3/4
A and B && not(C)
A= ref $entry eq "HASH"B= defined $entry->{"kind"} ? lc $entry->{"kind"} eq "file" : defined $entry->{"base64_string"}C= !defined($entry->{'url'})
ABCresult
0XX0
10X0
1100
1111
142 3 ▶ $found{sources} = 1, last unless $inline;
Branch: 1 branch
BranchTrueFalseTotal
unless $inline 2 1 3
143 }
144 }
145  
146 19 ▶ if (defined(my $target = $body->{target})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $target = $body->{"target"})) 2 17 19
147 2 ▶ my $kind = ref($target) eq 'HASH' ? lc($target->{kind} // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
ref $target eq 'HASH' ? : 2 0 2
Condition50%1/2
$target->{"kind"} // ""
l!l
20
MC/DC0%0/1
$target->{"kind"} // ""
$target->{"kind"}
Truth table50%1/2
A
A= $target->{"kind"}
Aresult
11
00
148 2 ▶ $found{target} = 1 unless $kind eq 'inbody' || $kind eq 'zip';
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "inbody" or $kind eq "zip" 1 1 2
Condition66%2/3
$kind eq "inbody" or $kind eq "zip"
l!l&&r!l&&!r
101
MC/DC50%1/2
$kind eq "inbody" or $kind eq "zip"
$kind eq "inbody" $kind eq "zip"
Truth table66%2/3
A or B
A= $kind eq "inbody"B= $kind eq "zip"
ABresult
1X1
011
000
149 }
150 19 return [ sort keys %found ];
151 }
152  
153  
154 75 ▶sub document_count {
Subroutine document_count: called CC=8 SCAR=20.8
Pod documented
155 75 my ($class, $request) = @_;
156 75 ▶ if (ref($request) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $request eq "HASH") 19 56 75
157 19 my $n = 0;
158 19 for my $field (qw(file_sources http_sources sources)) {
159 57 my $list = $request->{$field};
160 57 ▶ $n += ref($list) eq 'ARRAY' ? scalar(@$list) : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $list eq 'ARRAY' ? : 22 35 57
161 }
162 19 return $n;
163 }
164 56 my $n = 0;
165 56 for my $part (@{ $request->parts }) {
166 72 ▶ next if $part->is_multipart;
Branch: 1 branch
BranchTrueFalseTotal
if $part->is_multipart 0 72 72
167 72 ▶ my $disposition = $part->headers->content_disposition // next;
Condition50%1/2
$part->headers->content_disposition // (next)
l!l
720
MC/DC0%0/1
$part->headers->content_disposition // (next)
$part->headers->content_disposition
Truth table50%1/2
A
A= $part->headers->content_disposition
Aresult
11
00
168 72 my (undef, $is_file) = Langertha::Skeid::Protocol::Audio->disposition_names($disposition);
169 72 ▶ $n++ if $is_file;
Branch: 1 branch
BranchTrueFalseTotal
if $is_file 58 14 72
170 }
171 56 return $n;
172 }
173  
174  
175 48 ▶sub pages {
Subroutine pages: called CC=15 SCAR=27.6
Pod documented
176 48 my ($class, $payload, $res) = @_;
177 48 ▶ return unless ref($payload) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $payload eq "HASH" 5 43 48
178 43 ▶ return unless $res && $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res and $res->is_success 0 43 43
Condition33%1/3
$res and $res->is_success
!ll&&!rl&&r
0043
MC/DC0%0/2
$res and $res->is_success
$res $res->is_success
Truth table33%1/3
A and B
A= $resB= $res->is_success
ABresult
0X0
100
111
179 43 ▶ return unless ($res->headers->content_type // '') =~ m{json}i;
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->headers->content_type // "") =~ /json/i 1 42 43
Condition50%1/2
$res->headers->content_type // ""
l!l
430
MC/DC0%0/1
$res->headers->content_type // ""
$res->headers->content_type
Truth table50%1/2
A
A= $res->headers->content_type
Aresult
11
00
180 42 my $document = $payload->{document};
181 42 ▶ return unless ref($document) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $document eq "HASH" 0 42 42
182  
183 42 my $content = $document->{json_content};
184 42 ▶ if (defined($content) && !ref($content) && length($content)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content and not ref $content and length $content) 1 41 42
Condition83%5/6
defined $content and not ref $content and length $content
!ll&&!rl&&r
4101
defined $content and not ref $content
!ll&&!rl&&r
2391
MC/DC66%2/3
defined $content and not ref $content and length $content
defined $content not ref $content length $content
Truth table75%3/4
A and not(B) and C
A= defined $contentB= not ref $contentC= length $content
ABCresult
0XX0
10X0
1100
1111
185 1 ▶ return if length($content) > $MAX_PARSE_BYTES;
Branch: 1 branch
BranchTrueFalseTotal
if length $content > $MAX_PARSE_BYTES 0 1 1
186 1 $content = eval { decode_json($content) };
187 }
188 42 ▶ return unless ref($content) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $content eq "HASH" 2 40 42
189  
190 40 my $pages = $content->{pages};
191 40 ▶ my $count = ref($pages) eq 'HASH' ? scalar(keys %$pages)
Branch: 2 branches
BranchTrueFalseTotal
ref $pages eq 'HASH' ? : 37 3 40
ref $pages eq 'ARRAY' ? : 1 2 3
192 : ref($pages) eq 'ARRAY' ? scalar(@$pages)
193 : return;
194 38 ▶ return $count > 0 ? $count : undef;
Branch: 1 branch
BranchTrueFalseTotal
$count > 0 ? : 37 1 38
195 }
196  
197  
198 113 1;
199  
200 __END__
« lib/Langertha/Skeid/Protocol/Ollama/Stream.pm ↑ Summary lib/Langertha/Skeid/Protocol/Realtime.pm »