lib/Langertha/Skeid/Protocol/Ollama/Stream.pm

filter: Statementstmt 99.0% 99 / 100 Branchbran 89.6% 52 / 58 Conditioncond 74.2% 49 / 66 MC/DCmcdc 40.0% 14 / 35 Subroutinesub 100% 17 / 17 Podpod 100% 8 / 8 totaltotal 84.1% 239 / 284 CC 55 SCAR 26.0
CC
55
Cov
89%
delta
21.3
_tool_calls_from_chunk
18.6
finish
12.0
CRAP 13.4
« lib/Langertha/Skeid/Proxy.pm ↑ Summary lib/Langertha/Skeid/Protocol/Documents.pm »
1 package Langertha::Skeid::Protocol::Ollama::Stream;
2 87 our $VERSION = '0.004';
3 # ABSTRACT: Rewrites an OpenAI SSE stream as Ollama newline-delimited JSON
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 Langertha::Skeid::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use Langertha::Skeid::Protocol::Ollama;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10  
11 116 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
12 116 my ($class, %args) = @_;
13 return bless {
14 model => ($args{model} // ''),
15 116 ▶ shape => (($args{shape} // 'chat') eq 'generate' ? 'generate' : 'chat'),
Branch: 1 branch
BranchTrueFalseTotal
($args{'shape'} // 'chat') eq 'generate' ? : 45 71 116
Condition75%3/4
$args{"model"} // ""
l!l
1160
$args{"shape"} // "chat"
l!l
10610
MC/DC0%0/1
$args{"model"} // ""
$args{"model"}
MC/DC100%1/1
$args{"shape"} // "chat"
$args{"shape"}
Truth table50%1/2
A
A= $args{"model"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"shape"}
Aresult
11
00
16 started => 0,
17 finished => 0,
18 input_tokens => 0,
19 output_tokens => 0,
20 done_reason => undef,
21 text_bytes => 0,
22 errored => 0,
23 tool_stream_state => {},
24 tool_parser_active => 0,
25 tool_call_pending => 0,
26 }, $class;
27 }
28  
29  
30 83 ▶sub content_type { 'application/x-ndjson' }
Subroutine content_type: called CC=1 SCAR=0.0
Pod documented
31  
32 248 ▶sub _line {
Subroutine _line: called CC=1 SCAR=0.0
33 248 my ($payload) = @_;
34 248 return Langertha::Skeid::Protocol::encode_json_safe($payload) . "\n";
35 }
36  
37 # Parsing OpenAI tool-call deltas is Langertha's wire-format job. The parser
38 # object is stateless when every caller supplies its own state, so construct it
39 # only when the first tool fragment reaches this process and share it; each
40 # translator below still owns a separate tool_stream_state HashRef.
41 87 my $OPENAI_STREAM_PARSER;
42 30 ▶sub _openai_stream_parser {
Subroutine _openai_stream_parser: called CC=2 SCAR=7.1
43 30 require Langertha::Engine::OpenAIBase;
44 30 ▶ return $OPENAI_STREAM_PARSER ||= Langertha::Engine::OpenAIBase->new(
Condition66%2/3
$OPENAI_STREAM_PARSER ||= "Langertha::Engine::OpenAIBase"->new("url", "http://skeid.invalid")
l!l&&r!l&&!r
2820
MC/DC0%0/2
$OPENAI_STREAM_PARSER ||= "Langertha::Engine::OpenAIBase"->new("url", "http://skeid.invalid")
$OPENAI_STREAM_PARSER "Langertha::Engine::OpenAIBase"->new("url", "http://skeid.invalid")
Truth table66%2/3
A ||= B
A= $OPENAI_STREAM_PARSERB= "Langertha::Engine::OpenAIBase"->new("url", "http://skeid.invalid")
ABresult
1X1
011
000
45 url => 'http://skeid.invalid',
46 );
47 }
48  
49 # The state belongs to one translated request. Replacing our reference ends
50 # its lifetime without reaching into Langertha's private parser state.
51 96 ▶sub _discard_tool_stream_state {
Subroutine _discard_tool_stream_state: called CC=1 SCAR=0.0
52 96 my ($self) = @_;
53 96 $self->{tool_stream_state} = {};
54 96 $self->{tool_parser_active} = 0;
55 96 $self->{tool_call_pending} = 0;
56 96 return;
57 }
58  
59 # Feed a tool-bearing stream through Langertha until it returns finished
60 # Langertha::ToolCall objects. Text-only streams never instantiate the engine,
61 # and /api/generate never grows a chat-only tool_calls field.
62 252 ▶sub _tool_calls_from_chunk {
Subroutine _tool_calls_from_chunk: called CC=18 SCAR=29.2
63 252 my ($self, $chunk) = @_;
64 252 ▶ return [] if $self->{shape} eq 'generate';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shape"} eq "generate" 92 160 252
65  
66 160 ▶ my $choice = (ref($chunk->{choices}) eq 'ARRAY' ? $chunk->{choices}[0] : undef) || {};
Branch: 1 branch
BranchTrueFalseTotal
ref $chunk->{'choices'} eq 'ARRAY' ? : 160 0 160
Condition100%2/2
(ref $chunk->{'choices'} eq 'ARRAY' ? $chunk->{'choices'}[0] : undef) || {}
l!l
1582
MC/DC100%1/1
(ref $chunk->{'choices'} eq 'ARRAY' ? $chunk->{'choices'}[0] : undef) || {}
(ref $chunk->{'choices'} eq 'ARRAY' ? $chunk->{'choices'}[0] : undef)
Truth table100%2/2
A
A= (ref $chunk->{'choices'} eq 'ARRAY' ? $chunk->{'choices'}[0] : undef)
Aresult
11
00
67 160 ▶ my $delta = ref($choice->{delta}) eq 'HASH' ? $choice->{delta} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $choice->{'delta'} eq 'HASH' ? : 158 2 160
68 160 my $fragments = $delta->{tool_calls};
69 160 ▶ my $has_fragments = ref($fragments) eq 'ARRAY' && @$fragments;
Condition66%2/3
ref $fragments eq 'ARRAY' && @$fragments
!ll&&!rl&&r
141019
MC/DC50%1/2
ref $fragments eq 'ARRAY' && @$fragments
ref $fragments eq 'ARRAY' @$fragments
Truth table66%2/3
A && B
A= ref $fragments eq 'ARRAY'B= @$fragments
ABresult
0X0
100
111
70 160 ▶ return [] unless $self->{tool_parser_active} || $has_fragments;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"tool_parser_active"} or $has_fragments 130 30 160
Condition100%3/3
$self->{"tool_parser_active"} or $has_fragments
l!l&&r!l&&!r
1713130
MC/DC100%2/2
$self->{"tool_parser_active"} or $has_fragments
$self->{"tool_parser_active"} $has_fragments
Truth table100%3/3
A or B
A= $self->{"tool_parser_active"}B= $has_fragments
ABresult
1X1
011
000
71  
72 30 ▶ if ($has_fragments) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_fragments) 19 11 30
73 19 $self->{tool_parser_active} = 1;
74 19 $self->{tool_call_pending} = 1;
75 }
76 my $parsed = _openai_stream_parser()->parse_stream_chunk(
77 $chunk, undef, $self->{tool_stream_state},
78 30 );
79 26 ▶ return [] unless $parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed 0 26 26
80  
81 26 ▶ my $tool_calls = $parsed->has_tool_calls ? ($parsed->tool_calls // []) : [];
Branch: 1 branch
BranchTrueFalseTotal
$parsed->has_tool_calls ? : 6 20 26
Condition50%1/2
$parsed->tool_calls // []
l!l
60
MC/DC0%0/1
$parsed->tool_calls // []
$parsed->tool_calls
Truth table50%1/2
A
A= $parsed->tool_calls
Aresult
11
00
82 die "OpenAI stream produced undecodable tool arguments\n"
83 26 ▶ if grep { $_->arguments_undecodable } @$tool_calls;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->arguments_undecodable;} @$tool_calls 1 25 26
84 die "OpenAI stream ended without a complete tool call\n"
85 25 ▶ if $parsed->is_final && $self->{tool_call_pending} && !@$tool_calls;
Branch: 1 branch
BranchTrueFalseTotal
if $parsed->is_final and $self->{"tool_call_pending"} and not @$tool_calls 0 25 25
Condition66%4/6
$parsed->is_final and $self->{"tool_call_pending"} and not @$tool_calls
!ll&&!rl&&r
2050
$parsed->is_final and $self->{"tool_call_pending"}
!ll&&!rl&&r
2005
MC/DC0%0/3
$parsed->is_final and $self->{"tool_call_pending"} and not @$tool_calls
$parsed->is_final $self->{"tool_call_pending"} not @$tool_calls
Truth table50%2/4
A and B and not(C)
A= $parsed->is_finalB= $self->{"tool_call_pending"}C= not @$tool_calls
ABCresult
0XX0
10X0
1100
1111
86  
87 25 ▶ $self->{tool_call_pending} = 0 if @$tool_calls;
Branch: 1 branch
BranchTrueFalseTotal
if @$tool_calls 5 20 25
88 25 ▶ $self->_discard_tool_stream_state if $parsed->is_final;
Branch: 1 branch
BranchTrueFalseTotal
if $parsed->is_final 5 20 25
89 25 return $tool_calls;
90 }
91  
92 # The content of one line in this stream's shape: a chat message, or generate's bare response.
93 226 ▶sub _text_field {
Subroutine _text_field: called CC=4 SCAR=13.9
94 226 my ($self, $text, $tool_calls) = @_;
95 226 ▶ return (response => $text) if $self->{shape} eq 'generate';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shape"} eq "generate" 92 134 226
96  
97 134 my $message = { role => 'assistant', content => $text };
98 134 ▶ $message->{tool_calls} = [ map { $_->to_ollama } @$tool_calls ]
Branch: 1 branch
BranchTrueFalseTotal
if ref $tool_calls eq "ARRAY" and @$tool_calls 5 129 134
Condition100%3/3
ref $tool_calls eq "ARRAY" and @$tool_calls
!ll&&!rl&&r
40895
MC/DC100%2/2
ref $tool_calls eq "ARRAY" and @$tool_calls
ref $tool_calls eq "ARRAY" @$tool_calls
Truth table100%3/3
A and B
A= ref $tool_calls eq "ARRAY"B= @$tool_calls
ABresult
0X0
100
111
99 if ref($tool_calls) eq 'ARRAY' && @$tool_calls;
100 134 return (message => $message);
101 }
102  
103  
104 1 ▶sub start {
Subroutine start: called CC=1 SCAR=0.0
Pod documented
105 1 my ($self) = @_;
106 1 $self->{started} = 1;
107 1 return '';
108 }
109  
110  
111 255 ▶sub delta {
Subroutine delta: called CC=21 SCAR=30.6
Pod documented
112 255 my ($self, $chunk) = @_;
113 255 ▶ return '' unless ref($chunk) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $chunk eq "HASH" 0 255 255
114 255 ▶ return '' if $self->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} 1 254 255
115  
116 # Some servers report a failure inside an open stream as a chunk carrying an OpenAI error
117 # object. The HTTP status is already 200, so an Ollama client can only learn of it in-band,
118 # as Ollama's own error line -- and nothing after it (skeid #47).
119 254 ▶ if (ref($chunk->{error}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $chunk->{"error"} eq "HASH") 2 252 254
120 2 ▶ my $message = $chunk->{error}{message} // 'upstream error';
Condition50%1/2
$chunk->{"error"}{"message"} // "upstream error"
l!l
20
MC/DC0%0/1
$chunk->{"error"}{"message"} // "upstream error"
$chunk->{"error"}{"message"}
Truth table50%1/2
A
A= $chunk->{"error"}{"message"}
Aresult
11
00
121 2 return $self->error_event(500, "Upstream error: $message");
122 }
123  
124 252 ▶ if (my $usage = $chunk->{usage}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $usage = $chunk->{"usage"}) 85 167 252
125 85 ▶ $self->{input_tokens} = 0 + ($usage->{prompt_tokens} // $usage->{input_tokens} // $self->{input_tokens});
Condition83%5/6
($usage->{"prompt_tokens"} // $usage->{"input_tokens"}) // $self->{"input_tokens"}
l!l&&r!l&&!r
7960
$usage->{"prompt_tokens"} // $usage->{"input_tokens"}
l!l&&r!l&&!r
7366
MC/DC0%0/3
($usage->{"prompt_tokens"} // $usage->{"input_tokens"}) // $self->{"input_tokens"}
$usage->{"prompt_tokens"} $usage->{"input_tokens"} $self->{"input_tokens"}
Truth table75%3/4
A // B // C
A= $usage->{"prompt_tokens"}B= $usage->{"input_tokens"}C= $self->{"input_tokens"}
ABCresult
1XX1
01X1
0011
0000
126 85 ▶ $self->{output_tokens} = 0 + ($usage->{completion_tokens} // $usage->{output_tokens} // $self->{output_tokens});
Condition83%5/6
($usage->{"completion_tokens"} // $usage->{"output_tokens"}) // $self->{"output_tokens"}
l!l&&r!l&&!r
7960
$usage->{"completion_tokens"} // $usage->{"output_tokens"}
l!l&&r!l&&!r
7366
MC/DC0%0/3
($usage->{"completion_tokens"} // $usage->{"output_tokens"}) // $self->{"output_tokens"}
$usage->{"completion_tokens"} $usage->{"output_tokens"} $self->{"output_tokens"}
Truth table75%3/4
A // B // C
A= $usage->{"completion_tokens"}B= $usage->{"output_tokens"}C= $self->{"output_tokens"}
ABCresult
1XX1
01X1
0011
0000
127 }
128  
129 252 ▶ my $choice = (ref($chunk->{choices}) eq 'ARRAY' ? $chunk->{choices}[0] : undef) || {};
Branch: 1 branch
BranchTrueFalseTotal
ref $chunk->{'choices'} eq 'ARRAY' ? : 252 0 252
Condition100%2/2
(ref $chunk->{'choices'} eq 'ARRAY' ? $chunk->{'choices'}[0] : undef) || {}
l!l
2493
MC/DC100%1/1
(ref $chunk->{'choices'} eq 'ARRAY' ? $chunk->{'choices'}[0] : undef) || {}
(ref $chunk->{'choices'} eq 'ARRAY' ? $chunk->{'choices'}[0] : undef)
Truth table100%2/2
A
A= (ref $chunk->{'choices'} eq 'ARRAY' ? $chunk->{'choices'}[0] : undef)
Aresult
11
00
130 252 ▶ $self->{done_reason} = $choice->{finish_reason} if defined $choice->{finish_reason};
Branch: 1 branch
BranchTrueFalseTotal
if defined $choice->{"finish_reason"} 46 206 252
131 252 ▶ $self->{model} = $chunk->{model} if defined($chunk->{model}) && length($chunk->{model});
Branch: 1 branch
BranchTrueFalseTotal
if defined $chunk->{"model"} and length $chunk->{"model"} 155 97 252
Condition66%2/3
defined $chunk->{"model"} and length $chunk->{"model"}
!ll&&!rl&&r
970155
MC/DC50%1/2
defined $chunk->{"model"} and length $chunk->{"model"}
defined $chunk->{"model"} length $chunk->{"model"}
Truth table66%2/3
A and B
A= defined $chunk->{"model"}B= length $chunk->{"model"}
ABresult
0X0
100
111
132  
133 252 my $text = $choice->{delta}{content};
134 252 my $tool_calls;
135 252 ▶ unless (eval { $tool_calls = $self->_tool_calls_from_chunk($chunk); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $tool_calls = $self->_tool_calls_from_chunk($chunk); 1 } }) 5 247 252
136 5 return $self->error_event(500, 'Upstream stream could not be translated');
137 }
138 247 ▶ my $has_text = defined($text) && length($text);
Condition100%3/3
defined $text && length $text
!ll&&!rl&&r
932152
MC/DC100%2/2
defined $text && length $text
defined $text length $text
Truth table100%3/3
A && B
A= defined $textB= length $text
ABresult
0X0
100
111
139 247 ▶ return '' unless $has_text || @$tool_calls;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_text or @$tool_calls 90 157 247
Condition100%3/3
$has_text or @$tool_calls
l!l&&r!l&&!r
152590
MC/DC100%2/2
$has_text or @$tool_calls
$has_text @$tool_calls
Truth table100%3/3
A or B
A= $has_textB= @$tool_calls
ABresult
1X1
011
000
140  
141 157 ▶ $text = '' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 5 152 157
142 157 $self->{started} = 1;
143 157 ▶ $self->{text_bytes} += Langertha::Skeid::Protocol::utf8_length($text) if $has_text;
Branch: 1 branch
BranchTrueFalseTotal
if $has_text 152 5 157
144 return _line({
145 model => $self->{model},
146 157 created_at => Langertha::Skeid::Protocol::iso8601_now(),
147 $self->_text_field($text, $tool_calls),
148 done => \0,
149 });
150 }
151  
152  
153 88 ▶sub finish {
Subroutine finish: called CC=10 SCAR=24.9
Pod documented
154 88 my ($self, %args) = @_;
155 88 ▶ return '' if $self->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} 16 72 88
156  
157 # A tool fragment is not a call until Langertha returns it on a final chunk.
158 # Ending the request first is a failed stream, never a successful call with
159 # guessed arguments. Dropping our request-owned state needs no Core hook.
160 72 ▶ if ($self->{tool_call_pending}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tool_call_pending"}) 3 69 72
161 3 return $self->error_event(500, 'Upstream tool stream ended without finish_reason');
162 }
163  
164 69 my $line;
165 69 ▶ unless (eval {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $line = _line({"model", $self->{"model"}, "created_at", Langertha::Skeid::Protocol::iso8601_now(), $self->_text_field(""), "done", \1, "done_reason", ($args{"done_reason"} // $self->{"done_reason"}) // "stop", "prompt_eval_count", 0 + (($args{"input_tokens"} // $self->{"input_tokens"}) // 0), "eval_count", 0 + (($args{"output_tokens"} // $self->{"output_tokens"}) // 0)}); 1 } }) 0 69 69
166 $line = _line({
167 model => $self->{model},
168 created_at => Langertha::Skeid::Protocol::iso8601_now(),
169 $self->_text_field(''),
170 done => \1,
171 done_reason => ($args{done_reason} // $self->{done_reason} // 'stop'),
172 prompt_eval_count => 0 + ($args{input_tokens} // $self->{input_tokens} // 0),
173 69 ▶ eval_count => 0 + ($args{output_tokens} // $self->{output_tokens} // 0),
Condition53%8/15
($args{"done_reason"} // $self->{"done_reason"}) // "stop"
l!l
4227
$args{"done_reason"} // $self->{"done_reason"}
l!l&&r!l&&!r
04227
($args{"input_tokens"} // $self->{"input_tokens"}) // 0
l!l
690
$args{"input_tokens"} // $self->{"input_tokens"}
l!l&&r!l&&!r
0690
($args{"output_tokens"} // $self->{"output_tokens"}) // 0
l!l
690
$args{"output_tokens"} // $self->{"output_tokens"}
l!l&&r!l&&!r
0690
MC/DC50%1/2
($args{"done_reason"} // $self->{"done_reason"}) // "stop"
$args{"done_reason"} $self->{"done_reason"}
MC/DC0%0/2
($args{"input_tokens"} // $self->{"input_tokens"}) // 0
$args{"input_tokens"} $self->{"input_tokens"}
MC/DC0%0/2
($args{"output_tokens"} // $self->{"output_tokens"}) // 0
$args{"output_tokens"} $self->{"output_tokens"}
Truth table66%2/3
A // B
A= $args{"done_reason"}B= $self->{"done_reason"}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $args{"input_tokens"}B= $self->{"input_tokens"}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $args{"output_tokens"}B= $self->{"output_tokens"}
ABresult
1X1
011
000
174 });
175 69 1;
176 }) {
177 0 return $self->error_event(500, 'Stream translation failed');
178 }
179  
180 69 $self->_discard_tool_stream_state;
181 69 $self->{finished} = 1;
182 69 return $line;
183 }
184  
185  
186 23 ▶sub error_event {
Subroutine error_event: called CC=2 SCAR=6.9
Pod documented
187 23 my ($self, $status, $message) = @_;
188 23 ▶ return '' if $self->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} 1 22 23
189 22 $self->_discard_tool_stream_state;
190 22 $self->{finished} = 1;
191 22 $self->{errored} = 1;
192 22 return _line(Langertha::Skeid::Protocol::Ollama->error_body($message));
193 }
194  
195  
196 86 ▶sub errored { $_[0]->{errored} }
Subroutine errored: called CC=1 SCAR=0.0
Pod documented
197  
198  
199 93 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
Pod documented
200 93 my ($self) = @_;
201 93 return ($self->{input_tokens}, $self->{output_tokens}, $self->{text_bytes});
202 }
203  
204  
205 87 1;
206  
207 __END__
« lib/Langertha/Skeid/Proxy.pm ↑ Summary lib/Langertha/Skeid/Protocol/Documents.pm »