| 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
Condition75%3/4
$args{"model"} // ""
$args{"shape"} // "chat"
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"}
Truth table100%2/2
A
A= $args{"shape"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 160 | ▶ | my $choice = (ref($chunk->{choices}) eq 'ARRAY' ? $chunk->{choices}[0] : undef) || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
(ref $chunk->{'choices'} eq 'ARRAY' ? $chunk->{'choices'}[0] : undef) || {}
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 160 | ▶ | my $delta = ref($choice->{delta}) eq 'HASH' ? $choice->{delta} : {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 160 | ▶ | return [] unless $self->{tool_parser_active} || $has_fragments; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"tool_parser_active"} or $has_fragments
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 30 | ▶ | if ($has_fragments) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 26 | ▶ | my $tool_calls = $parsed->has_tool_calls ? ($parsed->tool_calls // []) : []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$parsed->tool_calls // []
MC/DC0%0/1
$parsed->tool_calls // []
$parsed->tool_calls
Truth table50%1/2
A
A= $parsed->tool_calls
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | die "OpenAI stream produced undecodable tool arguments\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 26 | ▶ | if grep { $_->arguments_undecodable } @$tool_calls; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
$parsed->is_final and $self->{"tool_call_pending"} and not @$tool_calls
$parsed->is_final and $self->{"tool_call_pending"}
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 25 | ▶ | $self->{tool_call_pending} = 0 if @$tool_calls; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 25 | ▶ | $self->_discard_tool_stream_state if $parsed->is_final; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 134 | my $message = { role => 'assistant', content => $text }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 134 | ▶ | $message->{tool_calls} = [ map { $_->to_ollama } @$tool_calls ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $tool_calls eq "ARRAY" and @$tool_calls
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 255 | ▶ | return '' if $self->{finished}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 2 | ▶ | my $message = $chunk->{error}{message} // 'upstream error'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$chunk->{"error"}{"message"} // "upstream error"
MC/DC0%0/1
$chunk->{"error"}{"message"} // "upstream error"
$chunk->{"error"}{"message"}
Truth table50%1/2
A
A= $chunk->{"error"}{"message"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 2 | return $self->error_event(500, "Upstream error: $message"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 252 | ▶ | if (my $usage = $chunk->{usage}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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"}
$usage->{"prompt_tokens"} // $usage->{"input_tokens"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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"}
$usage->{"completion_tokens"} // $usage->{"output_tokens"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 252 | ▶ | my $choice = (ref($chunk->{choices}) eq 'ARRAY' ? $chunk->{choices}[0] : undef) || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
(ref $chunk->{'choices'} eq 'ARRAY' ? $chunk->{'choices'}[0] : undef) || {}
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 252 | ▶ | $self->{done_reason} = $choice->{finish_reason} if defined $choice->{finish_reason}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 252 | ▶ | $self->{model} = $chunk->{model} if defined($chunk->{model}) && length($chunk->{model}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $chunk->{"model"} and length $chunk->{"model"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%2/2
defined $text && length $text
defined $text
length $text
Truth table100%3/3
A && B
A= defined $textB= length $text
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 247 | ▶ | return '' unless $has_text || @$tool_calls; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$has_text or @$tool_calls
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 157 | ▶ | $text = '' unless defined $text; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 157 | $self->{started} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 157 | ▶ | $self->{text_bytes} += Langertha::Skeid::Protocol::utf8_length($text) if $has_text; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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"
$args{"done_reason"} // $self->{"done_reason"}
($args{"input_tokens"} // $self->{"input_tokens"}) // 0
$args{"input_tokens"} // $self->{"input_tokens"}
($args{"output_tokens"} // $self->{"output_tokens"}) // 0
$args{"output_tokens"} // $self->{"output_tokens"}
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"}
Truth table33%1/3
A // B
A= $args{"input_tokens"}B= $self->{"input_tokens"}
Truth table33%1/3
A // B
A= $args{"output_tokens"}B= $self->{"output_tokens"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||