| 1 | package Langertha::Skeid::Protocol::Ollama::Stream; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 48 | our $VERSION = '0.003'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Rewrites an OpenAI SSE stream as Ollama newline-delimited JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 48 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 48 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 48 | ▶ | use Langertha::Skeid::Protocol; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 48 | ▶ | use Langertha::Skeid::Protocol::Ollama; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 115 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 115 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | return bless { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | model => ($args{model} // ''), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 115 | ▶ | 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 | 82 | ▶ | sub content_type { 'application/x-ndjson' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
content_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 242 | ▶ | sub _line { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_line: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 242 | my ($payload) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 242 | 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 | 48 | 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 | 95 | ▶ | sub _discard_tool_stream_state { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_discard_tool_stream_state: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 95 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 95 | $self->{tool_stream_state} = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 95 | $self->{tool_parser_active} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 95 | $self->{tool_call_pending} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 95 | 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 | 245 | ▶ | sub _tool_calls_from_chunk { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tool_calls_from_chunk: called CC=18 SCAR=29.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 245 | my ($self, $chunk) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 245 | ▶ | return [] if $self->{shape} eq 'generate'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 153 | ▶ | 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 | 153 | ▶ | my $delta = ref($choice->{delta}) eq 'HASH' ? $choice->{delta} : {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 153 | my $fragments = $delta->{tool_calls}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 153 | ▶ | 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 | 153 | ▶ | 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 | 220 | ▶ | sub _text_field { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_text_field: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 220 | my ($self, $text, $tool_calls) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 220 | ▶ | return (response => $text) if $self->{shape} eq 'generate'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 128 | my $message = { role => 'assistant', content => $text }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 128 | ▶ | $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 | 128 | 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 | 248 | ▶ | sub delta { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delta: called CC=21 SCAR=30.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 248 | my ($self, $chunk) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 248 | ▶ | return '' unless ref($chunk) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 248 | ▶ | 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 | 247 | ▶ | 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 | 245 | ▶ | if (my $usage = $chunk->{usage}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 84 | ▶ | $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 | 84 | ▶ | $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/DC66%2/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 | 245 | ▶ | 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 | 245 | ▶ | $self->{done_reason} = $choice->{finish_reason} if defined $choice->{finish_reason}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 245 | ▶ | $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 | 245 | my $text = $choice->{delta}{content}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 245 | my $tool_calls; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 245 | ▶ | 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 | 240 | ▶ | 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 | 240 | ▶ | 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 | 152 | ▶ | $text = '' unless defined $text; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 152 | $self->{started} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 152 | ▶ | $self->{text_bytes} += Langertha::Skeid::Protocol::utf8_length($text) if $has_text; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | return _line({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | model => $self->{model}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 152 | created_at => Langertha::Skeid::Protocol::iso8601_now(), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | $self->_text_field($text, $tool_calls), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | done => \0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 87 | ▶ | sub finish { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
finish: called CC=10 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 87 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 87 | ▶ | 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 | 71 | ▶ | 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 | 68 | my $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 68 | ▶ | 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 | 68 | ▶ | 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 | 68 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | return $self->error_event(500, 'Stream translation failed'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 68 | $self->_discard_tool_stream_state; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 68 | $self->{finished} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 68 | 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 | 85 | ▶ | sub errored { $_[0]->{errored} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
errored: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 92 | ▶ | sub usage { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usage: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 92 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 92 | return ($self->{input_tokens}, $self->{output_tokens}, $self->{text_bytes}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 48 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | Langertha::Skeid::Protocol::Ollama::Stream - Rewrites an OpenAI SSE stream as Ollama newline-delimited JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | version 0.003 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | The counterpart to L<Langertha::Skeid::Protocol::Anthropic::Stream> for Ollama's C</api/chat>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | and a simpler job: Ollama streams newline-delimited JSON objects rather than SSE events, each | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | one a whole message with a C<done> flag. There is no prologue and no event framing — just one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | line per delta and a final line that says it is over. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | my $stream = Langertha::Skeid::Protocol::Ollama::Stream->new(model => 'qwen3'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | $write->($stream->start); # empty, by design | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | $write->($stream->delta($chunk)) for @chunks; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | $write->($stream->finish); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | The trailing line matters more than it looks: an Ollama client reads token counts from it and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | treats the stream as unfinished without it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | C</api/generate> streams the same lines with the text under C<response> instead of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | C<message>; C<< shape => 'generate' >> selects that, the default C<chat> is C</api/chat>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | my $stream = Langertha::Skeid::Protocol::Ollama::Stream->new(model => 'qwen3', shape => 'generate'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | my $stream = Langertha::Skeid::Protocol::Ollama::Stream->new(model => $requested_model, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | shape => 'chat'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | C<model> is named on every line until an upstream chunk names its own; C<shape> is C<chat> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | (default) or C<generate>. Tool calls are only rendered for C<chat>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | =head2 content_type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | C<application/x-ndjson>. Not SSE: relaying the upstream's C<text/event-stream> here would tell | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | an Ollama client to parse something it does not speak. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | =head2 start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | Nothing. Ollama has no prologue — the first line a client sees is the first delta. Present so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | both stream translators answer the same three calls. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | =head2 delta | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | One decoded OpenAI chunk becomes one Ollama line when it carries text or completed tool calls, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | or nothing for a role-only chunk or the final usage-only one. OpenAI tool-call fragments are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | fed to L<Langertha::Engine::OpenAIBase/parse_stream_chunk> with state private to this stream; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | only its completed L<Langertha::ToolCall> objects are rendered, through C<to_ollama>. Usage and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | finish reason are recorded for the closing line. A top-level OpenAI error object ends the stream | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | with L</error_event>; a parser rejection does the same with a generic message rather than | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | reflecting parser/provider details. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | =head2 finish | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | The closing line: C<done> true, the reason, and the token counts an Ollama client reads its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | statistics from. C<< done_reason => ... >>, C<< input_tokens => ... >> and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | C<< output_tokens => ... >> override what the stream recorded. Idempotent. A pending tool call whose stream supplied no final chunk is an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | L</error_event>, not a successful close; failures while building the closing line are contained | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | at the same boundary. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | =head2 error_event | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | my $bytes = $stream->error_event(500, 'Upstream error: ...'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | Ends the stream with Ollama's error line, C<{"error":"<message>"}> -- how Ollama itself reports a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | failure after the stream has opened, and what its clients check every line for. The status is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | accepted for the same call shape as L<Langertha::Skeid::Protocol::Anthropic::Stream/error_event> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | and is not on the wire: the HTTP status went out with the first line. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | The stream is finished afterwards: C<delta> and C<finish> return nothing, so no C<done: true> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | line follows and a client cannot mistake a failed stream for a complete one. Returns nothing if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | the stream has already finished. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | =head2 errored | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | True once L</error_event> ended the stream, so the proxy records the request as failed even | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | though the HTTP status was 200. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | =head2 usage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | my ($input, $output, $content_bytes) = $stream->usage; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | What the stream carried, for the usage event. C<content_bytes> counts UTF-8 bytes of the text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | this translator wrote and becomes the event's C<content_bytes>, recorded beside the token counts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | on every stream -- an observation, never an estimate of tokens. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | L<Langertha::Skeid::Protocol::Ollama>, L<Langertha::Skeid::Protocol::Anthropic::Stream> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | L<https://github.com/Getty/langertha-skeid/issues>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||