| 1 | package Langertha::Knarr::Handler::Passthrough; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Knarr handler that forwards requests verbatim to an upstream HTTP API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 65 | our $VERSION = '1.102'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 65 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 65 | ▶ | use Future; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 65 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 65 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 65 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 65 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 65 | ▶ | use Langertha::Knarr::Stream; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 65 | ▶ | use Langertha::Knarr::Response; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 65 | ▶ | use Langertha::ToolCall; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 65 | with 'Langertha::Knarr::Handler', 'Langertha::Knarr::Role::UpstreamHTTP'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # Forwards the original wire-format request to a real upstream API. The | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # protocol's parser already turned the body into a Knarr::Request, so we | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # rebuild a body from $request->raw and re-POST it. Headers (especially | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # Authorization) are passed through if the caller registers them with the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # session via $request->extra->{forward_headers}. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # Per-protocol upstream URLs. Keys are protocol names ("openai", "anthropic", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # "ollama"). Each value is the base URL of the upstream provider — e.g. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # https://api.openai.com or https://api.anthropic.com. Knarr appends the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # original request path to this base URL. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 65 | has upstreams => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | isa => 'HashRef[Str]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # Optional: a default Authorization header value to inject if the client | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # didn't send one. If undef, the client must supply its own. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | has default_auth => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 40 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 65 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 65 | has model_id => ( is => 'ro', isa => 'Str', default => 'passthrough' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 40 | ▶ | has _json => ( is => 'ro', default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # Per-protocol path the request should hit upstream. We use the protocol | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # defaults; this lookup table can be extended. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 65 | my %DEFAULT_PATH = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | openai => '/v1/chat/completions', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | anthropic => '/v1/messages', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | ollama => '/api/chat', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # Other chat paths a protocol has upstream, reached as the client asked: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # Ollama's /api/generate takes a prompt, not messages, and answers in its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # own shape, so its bytes must reach the upstream's /api/generate (k46). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 65 | my %OTHER_PATHS = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | ollama => { '/api/generate' => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # $client_path: the path the client asked for; used when it is one of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # protocol's chat paths, else the protocol's default chat path. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 320 | ▶ | sub _upstream_url { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_upstream_url: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 320 | my ($self, $protocol_name, $client_path) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 320 | ▶ | my $base = $self->upstreams->{$protocol_name} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | or die "Passthrough: no upstream configured for protocol '$protocol_name'\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 320 | $base =~ s{/+$}{}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 320 | ▶ | my $path = $DEFAULT_PATH{$protocol_name} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | or die "Passthrough: no default path for protocol '$protocol_name'\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | $path = $client_path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 320 | ▶ | if defined $client_path && $OTHER_PATHS{$protocol_name}{$client_path}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $client_path and $OTHER_PATHS{$protocol_name}{$client_path}
MC/DC100%2/2
defined $client_path and $OTHER_PATHS{$protocol_name}{$client_path}
defined $client_path
$OTHER_PATHS{$protocol_name}{$client_path}
Truth table100%3/3
A and B
A= defined $client_pathB= $OTHER_PATHS{$protocol_name}{$client_path}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 320 | return "$base$path"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # Whether a request in this protocol has anywhere to go: an upstream URL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | # and a known chat path -- exactly what _upstream_url needs (k41). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 393 | ▶ | sub serves_protocol { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
serves_protocol: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 393 | my ($self, $protocol_name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 393 | ▶ | return 0 unless defined $protocol_name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 393 | ▶ | return $self->upstreams->{$protocol_name} && $DEFAULT_PATH{$protocol_name} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->upstreams->{$protocol_name} and $DEFAULT_PATH{$protocol_name}
MC/DC50%1/2
$self->upstreams->{$protocol_name} and $DEFAULT_PATH{$protocol_name}
$self->upstreams->{$protocol_name}
$DEFAULT_PATH{$protocol_name}
Truth table66%2/3
A and B
A= $self->upstreams->{$protocol_name}B= $DEFAULT_PATH{$protocol_name}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # Whether $url lives on this protocol's upstream (k47). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 102 | ▶ | sub is_upstream_for { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_upstream_for: called CC=4 SCAR=15.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 102 | my ($self, $protocol_name, $url) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 102 | ▶ | return 0 unless defined $protocol_name && defined $url; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $protocol_name and defined $url
MC/DC0%0/2
defined $protocol_name and defined $url
defined $protocol_name
defined $url
Truth table33%1/3
A and B
A= defined $protocol_nameB= defined $url
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 102 | ▶ | my $base = $self->upstreams->{$protocol_name} or return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 102 | return $self->same_upstream( $base, $url ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # Same scheme, host, port and path (k54): a gateway serves several | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # providers under paths of one host. An engine's base URL carries the API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # version the passthrough base leaves out (https://api.openai.com/v1 vs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | # https://api.openai.com), so a trailing /v1 is not compared. URI's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # canonical form takes care of host case and default ports. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 171 | ▶ | sub same_upstream { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
same_upstream: called CC=9 SCAR=23.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 171 | my ($self, $base, $url) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 171 | ▶ | return 0 unless defined $base && defined $url; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $base and defined $url
MC/DC0%0/2
defined $base and defined $url
defined $base
defined $url
Truth table33%1/3
A and B
A= defined $baseB= defined $url
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 171 | my ($upstream, $other) = map { URI->new($_)->canonical } $base, $url; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 171 | ▶ | return 0 unless $upstream->can('host') && $other->can('host'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$upstream->can("host") and $other->can("host")
MC/DC0%0/2
$upstream->can("host") and $other->can("host")
$upstream->can("host")
$other->can("host")
Truth table33%1/3
A and B
A= $upstream->can("host")B= $other->can("host")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 171 | ▶ | return $upstream->scheme eq $other->scheme && $upstream->host eq $other->host | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
$upstream->scheme eq $other->scheme and $upstream->host eq $other->host and $upstream->port == $other->port and $self->_upstream_path($upstream) eq $self->_upstream_path($other)
$upstream->scheme eq $other->scheme and $upstream->host eq $other->host and $upstream->port == $other->port
$upstream->scheme eq $other->scheme and $upstream->host eq $other->host
MC/DC75%3/4
$upstream->scheme eq $other->scheme and $upstream->host eq $other->host and $upstream->port == $other->port and $self->_upstream_path($upstream) eq $self->_upstream_path($other)
$upstream->scheme eq $other->scheme
$upstream->host eq $other->host
$upstream->port == $other->port
$self->_upstream_path($upstream) eq $self->_upstream_path($other)
Truth table80%4/5
A and B and C and D
A= $upstream->scheme eq $other->schemeB= $upstream->host eq $other->hostC= $upstream->port == $other->portD= $self->_upstream_path($upstream) eq $self->_upstream_path($other)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | && $upstream->port == $other->port | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | && $self->_upstream_path($upstream) eq $self->_upstream_path($other) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 240 | ▶ | sub _upstream_path { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_upstream_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 240 | my ($self, $uri) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 240 | ( my $path = $uri->path ) =~ s{/+\z}{}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 240 | $path =~ s{/v1\z}{}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 240 | return $path; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # An Ollama /api/generate request: its upstream answer carries the text on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | # response, not message.content, and no tool calls (k48). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 111 | ▶ | sub _is_ollama_generate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_ollama_generate: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 111 | my ($self, $request) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | return $request->protocol eq 'ollama' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 111 | ▶ | && ( $request->extra->{path} // '' ) eq '/api/generate' ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
$request->protocol eq "ollama" and ($request->extra->{"path"} // "") eq "/api/generate"
$request->extra->{"path"} // ""
MC/DC100%2/2
$request->protocol eq "ollama" and ($request->extra->{"path"} // "") eq "/api/generate"
$request->protocol eq "ollama"
$request->extra->{"path"}
Truth table100%3/3
A and B
A= $request->protocol eq "ollama"B= $request->extra->{"path"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 118 | ▶ | sub _build_upstream_request { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_upstream_request: called CC=7 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 118 | my ($self, $request, $force_stream) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 118 | ▶ | my $body = { %{ $request->raw || {} } }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$request->raw || {}
MC/DC0%0/1
$request->raw || {}
$request->raw
Truth table50%1/2
A
A= $request->raw
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 118 | ▶ | $body->{stream} = $force_stream ? JSON::MaybeXS::true() : JSON::MaybeXS::false() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | if defined $force_stream; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | # The client's path, where the protocol's parser recorded it: an Ollama | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # /api/generate goes to the upstream's /api/generate (k48). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 118 | my $url = $self->_upstream_url( $request->protocol, $request->extra->{path} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 118 | my $http_req = HTTP::Request->new( POST => $url ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 118 | $http_req->header( 'Content-Type' => 'application/json' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | # Forward client auth headers (captured by protocol parsers), each line | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | # added, not set: a header sent twice reaches the upstream twice, in its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | # order (k60). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 118 | $http_req->push_header(@$_) for $request->forward_header_pairs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 118 | ▶ | if ( my $auth = $self->default_auth ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | $http_req->header( Authorization => $auth ) unless $http_req->header('Authorization'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 118 | $http_req->content( $self->_json->encode($body) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 118 | return $http_req; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | # Read an upstream response body for the protocol it came from: the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | # assistant text, the tool calls as Langertha::ToolCall objects (through | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | # core's canonical inbound door, the same one the streaming path uses) and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | # the terminal reason verbatim -- the client-side protocol maps it (k21). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | # $generate: the body is an Ollama /api/generate answer, the text on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | # response instead of message.content (k48). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 60 | ▶ | sub _parse_response { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_response: called CC=13 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 60 | my ($self, $protocol_name, $resp_body, $generate) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 60 | my %parsed = ( content => '', tool_calls => [], finish_reason => undef ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 60 | my $data = eval { $self->_json->decode($resp_body) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 60 | ▶ | return %parsed unless ref $data eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 60 | ▶ | if ( $protocol_name eq 'openai' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 18 | ▶ | $parsed{content} = $data->{choices}[0]{message}{content} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$data->{"choices"}[0]{"message"}{"content"} // ""
MC/DC100%1/1
$data->{"choices"}[0]{"message"}{"content"} // ""
$data->{"choices"}[0]{"message"}{"content"}
Truth table100%2/2
A
A= $data->{"choices"}[0]{"message"}{"content"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 18 | $parsed{finish_reason} = $data->{choices}[0]{finish_reason}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | elsif ( $protocol_name eq 'anthropic' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 17 | ▶ | for my $b ( @{ $data->{content} || [] } ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$data->{'content'} || []
MC/DC0%0/1
$data->{'content'} || []
$data->{'content'}
Truth table50%1/2
A
A= $data->{'content'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 21 | ▶ | $parsed{content} .= $b->{text} // '' if ($b->{type} // '') eq 'text'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$b->{"type"} // ""
$b->{"text"} // ""
MC/DC0%0/1
$b->{"type"} // ""
$b->{"type"}
MC/DC0%0/1
$b->{"text"} // ""
$b->{"text"}
Truth table50%1/2
A
A= $b->{"type"}
Truth table50%1/2
A
A= $b->{"text"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 17 | $parsed{finish_reason} = $data->{stop_reason}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | elsif ( $protocol_name eq 'ollama' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 25 | ▶ | $parsed{content} = ( $generate ? $data->{response} : $data->{message}{content} ) // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
($generate ? $data->{"response"} : $data->{"message"}{"content"}) // ""
MC/DC0%0/1
($generate ? $data->{"response"} : $data->{"message"}{"content"}) // ""
($generate ? $data->{"response"} : $data->{"message"}{"content"})
Truth table50%1/2
A
A= ($generate ? $data->{"response"} : $data->{"message"}{"content"})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 25 | $parsed{finish_reason} = $data->{done_reason}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | return %parsed; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 60 | $parsed{tool_calls} = [ Langertha::ToolCall->extract( $protocol_name, $data ) ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 60 | return %parsed; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 67 | ▶ | async sub handle_chat_f { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
handle_chat_f: called CC=3 SCAR=11.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 67 | my ($self, $session, $request) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 67 | my $http_req = $self->_build_upstream_request( $request, 0 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 67 | my $resp = await $self->_upstream_request_f( request => $http_req ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 60 | ▶ | die "Passthrough upstream failed: " . $resp->status_line . "\n" unless $resp->is_success; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 60 | ▶ | return Langertha::Knarr::Response->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$request->model // $self->model_id
MC/DC0%0/2
$request->model // $self->model_id
$request->model
$self->model_id
Truth table33%1/3
A // B
A= $request->modelB= $self->model_id
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | $self->_parse_response( $request->protocol, $resp->decoded_content, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | $self->_is_ollama_generate($request) ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | model => $request->model // $self->model_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 65 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 51 | ▶ | async sub handle_stream_f { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
handle_stream_f: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 51 | my ($self, $session, $request) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 51 | my $http_req = $self->_build_upstream_request( $request, 1 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 51 | my @queue; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | my $pending; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 51 | my $finished = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 51 | my @error; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 51 | my $buffer = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 93 | ▶ | my $deliver = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 93 | my ($v) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 93 | ▶ | if ( $pending ) { my $p = $pending; $pending = undef; $p->done($v) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 18 | else { push @queue, $v } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 51 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | # Streaming request: hand the body chunks straight back as deltas. The | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | # upstream already speaks the same wire format the client requested, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | # we forward bytes 1:1 by extracting just the text content from each | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | # protocol-native chunk. The Knarr core then re-frames them via the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | # client-side protocol's format_stream_chunk — keeping symmetry even | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | # when client and upstream use the same protocol. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | my $stream = Langertha::Knarr::Stream->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 106 | ▶ | source => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=15.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 106 | ▶ | if ( @queue ) { return Future->done( shift @queue ) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 88 | ▶ | if ( $finished ) { return $error[0] ? Future->fail(@error) : Future->done(undef) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 88 | $pending = Future->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 88 | return $pending; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 51 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 51 | my $proto_name = $request->protocol; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 51 | my $generate = $self->_is_ollama_generate($request); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | # The upstream's terminal reason, verbatim; the client-side protocol maps | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # it when it closes the stream (k18). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 54 | ▶ | my $note_finish = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 54 | my ($reason) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 54 | ▶ | $stream->finish_reason($reason) if defined $reason && length $reason; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $reason and length $reason
MC/DC50%1/2
defined $reason and length $reason
defined $reason
length $reason
Truth table66%2/3
A and B
A= defined $reasonB= length $reason
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 51 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | # The upstream's tool calls, assembled into complete Langertha::ToolCall | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | # objects: OpenAI delta.tool_calls fragments per index until the stream | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | # ends, Anthropic tool_use blocks with their input_json_delta until | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | # content_block_stop, Ollama message.tool_calls whole. The client-side | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | # protocol frames them when it closes the stream (k19). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 51 | my %openai_calls; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | my %anthropic_blocks; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 40 | ▶ | my $note_calls = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 40 | my @calls = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 40 | ▶ | $stream->tool_calls( [ @{ $stream->tool_calls }, @calls ] ) if @calls; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 51 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 38 | ▶ | my $finish_openai_calls = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 38 | ▶ | return unless %openai_calls; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 2 | my @raw = map { $openai_calls{$_} } sort { $a <=> $b } keys %openai_calls; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 2 | %openai_calls = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 2 | $note_calls->( Langertha::ToolCall->extract( 'openai', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | { choices => [ { message => { tool_calls => \@raw } } ] } ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 51 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 178 | ▶ | my $extract_chunk = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=36 SCAR=36.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 178 | my ($line) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 178 | ▶ | if ( $proto_name eq 'openai' || $proto_name eq 'anthropic' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$proto_name eq "openai" or $proto_name eq "anthropic"
MC/DC100%2/2
$proto_name eq "openai" or $proto_name eq "anthropic"
$proto_name eq "openai"
$proto_name eq "anthropic"
Truth table100%3/3
A or B
A= $proto_name eq "openai"B= $proto_name eq "anthropic"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 132 | ▶ | return undef unless $line =~ /^data:\s*(.+)$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 80 | my $payload = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 80 | ▶ | return undef if $payload eq '[DONE]'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 74 | my $d = eval { $self->_json->decode($payload) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 74 | ▶ | return undef unless ref $d eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 74 | ▶ | if ( $proto_name eq 'openai' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 22 | $note_finish->( $d->{choices}[0]{finish_reason} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 22 | my $frags = $d->{choices}[0]{delta}{tool_calls}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 22 | ▶ | for my $frag ( ref $frags eq 'ARRAY' ? @$frags : () ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 6 | ▶ | next unless ref $frag eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 6 | ▶ | my $call = $openai_calls{ $frag->{index} // 0 } //= | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition75%3/4
$openai_calls{$frag->{"index"} // 0} //= {"id", "", "type", "function", "function", {"name", "", "arguments", ""}}
$frag->{"index"} // 0
MC/DC100%1/1
$openai_calls{$frag->{"index"} // 0} //= {"id", "", "type", "function", "function", {"name", "", "arguments", ""}}
$openai_calls{$frag->{"index"} // 0}
MC/DC0%0/1
$frag->{"index"} // 0
$frag->{"index"}
Truth table100%2/2
A
A= $openai_calls{$frag->{"index"} // 0}
Truth table50%1/2
A
A= $frag->{"index"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | { id => '', type => 'function', function => { name => '', arguments => '' } }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 6 | ▶ | $call->{id} = $frag->{id} if defined $frag->{id} && length $frag->{id}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $frag->{"id"} and length $frag->{"id"}
MC/DC50%1/2
defined $frag->{"id"} and length $frag->{"id"}
defined $frag->{"id"}
length $frag->{"id"}
Truth table66%2/3
A and B
A= defined $frag->{"id"}B= length $frag->{"id"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 6 | ▶ | my $fn = ref $frag->{function} eq 'HASH' ? $frag->{function} : {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 6 | ▶ | $call->{function}{name} = $fn->{name} if defined $fn->{name} && length $fn->{name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $fn->{"name"} and length $fn->{"name"}
MC/DC50%1/2
defined $fn->{"name"} and length $fn->{"name"}
defined $fn->{"name"}
length $fn->{"name"}
Truth table66%2/3
A and B
A= defined $fn->{"name"}B= length $fn->{"name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 6 | ▶ | $call->{function}{arguments} .= $fn->{arguments} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$fn->{"arguments"} // ""
MC/DC0%0/1
$fn->{"arguments"} // ""
$fn->{"arguments"}
Truth table50%1/2
A
A= $fn->{"arguments"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 22 | return $d->{choices}[0]{delta}{content}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 52 | ▶ | my $type = $d->{type} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$d->{"type"} // ""
MC/DC0%0/1
$d->{"type"} // ""
$d->{"type"}
Truth table50%1/2
A
A= $d->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 52 | ▶ | $note_finish->( $d->{delta}{stop_reason} ) if $type eq 'message_delta'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 52 | ▶ | my $index = $d->{index} // 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$d->{"index"} // 0
MC/DC100%1/1
$d->{"index"} // 0
$d->{"index"}
Truth table100%2/2
A
A= $d->{"index"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 52 | ▶ | if ( $type eq 'content_block_start' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%6/8
$type eq "content_block_start" and ref $d->{"content_block"} eq "HASH" and ($d->{"content_block"}{"type"} // "") eq "tool_use"
$type eq "content_block_start" and ref $d->{"content_block"} eq "HASH"
$d->{"content_block"}{"type"} // ""
MC/DC33%1/3
$type eq "content_block_start" and ref $d->{"content_block"} eq "HASH" and ($d->{"content_block"}{"type"} // "") eq "tool_use"
$type eq "content_block_start"
ref $d->{"content_block"} eq "HASH"
$d->{"content_block"}{"type"}
Truth table50%2/4
A and B and C
A= $type eq "content_block_start"B= ref $d->{"content_block"} eq "HASH"C= $d->{"content_block"}{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | && ref $d->{content_block} eq 'HASH' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | && ( $d->{content_block}{type} // '' ) eq 'tool_use' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 6 | $anthropic_blocks{$index} = { %{ $d->{content_block} }, json => '' }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 6 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 46 | ▶ | if ( $type eq 'content_block_delta' && $anthropic_blocks{$index} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type eq "content_block_delta" and $anthropic_blocks{$index}
MC/DC100%2/2
$type eq "content_block_delta" and $anthropic_blocks{$index}
$type eq "content_block_delta"
$anthropic_blocks{$index}
Truth table100%3/3
A and B
A= $type eq "content_block_delta"B= $anthropic_blocks{$index}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 7 | ▶ | $anthropic_blocks{$index}{json} .= $d->{delta}{partial_json} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$d->{"delta"}{"partial_json"} // ""
MC/DC0%0/1
$d->{"delta"}{"partial_json"} // ""
$d->{"delta"}{"partial_json"}
Truth table50%1/2
A
A= $d->{"delta"}{"partial_json"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 7 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 39 | ▶ | if ( $type eq 'content_block_stop' && ( my $block = delete $anthropic_blocks{$index} ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type eq "content_block_stop" and my $block = delete $anthropic_blocks{$index}
MC/DC100%2/2
$type eq "content_block_stop" and my $block = delete $anthropic_blocks{$index}
$type eq "content_block_stop"
my $block = delete $anthropic_blocks{$index}
Truth table100%3/3
A and B
A= $type eq "content_block_stop"B= my $block = delete $anthropic_blocks{$index}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 6 | my $json = delete $block->{json}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 6 | ▶ | $block->{input} = $json if length $json; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 6 | $note_calls->( grep { defined } Langertha::ToolCall->from_anthropic($block) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 6 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 33 | ▶ | return $d->{delta}{text} if $type eq 'content_block_delta'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 24 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 46 | ▶ | if ( $proto_name eq 'ollama' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 46 | my $d = eval { $self->_json->decode($line) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 46 | ▶ | return undef unless ref $d eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 46 | ▶ | $note_finish->( $d->{done_reason} ) if $d->{done}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 46 | ▶ | return $d->{response} if $generate; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 32 | $note_calls->( Langertha::ToolCall->extract( 'ollama', $d ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 32 | return $d->{message}{content}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 51 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | my $f = $self->_upstream_request_f( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | request => $http_req, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 44 | ▶ | on_header => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 44 | my ($r) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 122 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 122 | my ($data) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 122 | ▶ | if ( !defined $data ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 38 | $finish_openai_calls->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 38 | $finished = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 38 | $deliver->(undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 38 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 84 | $buffer .= $data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | # Frame separator: blank line for SSE, single \n for NDJSON. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 84 | ▶ | my $sep = $proto_name eq 'ollama' ? qr/\n/ : qr/\n\n/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 84 | ▶ | while ( $buffer =~ s/^(.*?)$sep//s ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 126 | my $frame = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 126 | for my $line ( split /\n/, $frame ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 178 | ▶ | next unless length $line; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 178 | my $delta = $extract_chunk->($line); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 178 | ▶ | $deliver->($delta) if defined $delta && length $delta; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $delta and length $delta
MC/DC100%2/2
defined $delta and length $delta
defined $delta
length $delta
Truth table100%3/3
A and B
A= defined $deltaB= length $delta
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 44 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 51 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | # A read already waiting gets the failure itself (a timeout included, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | # k35), not an undef that would end the stream as if it were complete. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | # The whole failure, so a timeout keeps its category (k36). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 13 | ▶ | $f->on_fail( sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 13 | @error = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 13 | $finished = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 13 | ▶ | if ( $pending ) { my $p = $pending; $pending = undef; $p->fail(@error) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 51 | } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 51 | $f->retain; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 51 | return $stream; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 65 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 1 | ▶ | sub list_models { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_models: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 1 | return [ { id => $self->model_id, object => 'model' } ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 65 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 65 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | Langertha::Knarr::Handler::Passthrough - Knarr handler that forwards requests verbatim to an upstream HTTP API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | version 1.102 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | use Langertha::Knarr::Handler::Passthrough; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | my $handler = Langertha::Knarr::Handler::Passthrough->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | upstreams => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | openai => 'https://api.openai.com', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | anthropic => 'https://api.anthropic.com', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | ollama => 'http://localhost:11434', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | Forwards the original wire-format request verbatim to a real upstream | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | API. The protocol's parser already turned the body into a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | L<Langertha::Knarr::Request>; Passthrough rebuilds the upstream JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | from C<$request-E<gt>raw> and re-POSTs it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | Both sync and streaming requests are supported. A sync answer carries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | the upstream's text, its tool calls as L<Langertha::ToolCall> objects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | and its finish reason (verbatim; the front-side protocol maps it). For | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | streaming, the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | upstream's protocol-native chunks are extracted into plain text deltas, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | the upstream's tool calls are assembled into complete | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | L<Langertha::ToolCall> objects on the stream's C<tool_calls> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | which the front-side protocol then re-frames — keeping symmetry even | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | when client and upstream use the same protocol. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | The client's auth headers go along as the protocol's parser captured them | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | in C<< $request->extra->{forward_headers} >>: C<Authorization> for OpenAI | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | and Ollama, C<x-api-key>, C<anthropic-version> and C<Authorization> for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | Anthropic. L<Langertha::Knarr> takes its own proxy key out of them first | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | (see L<Langertha::Knarr/auth_token>). Each header line the client sent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | reaches the upstream as a line of its own, in its order (see | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | L<Langertha::Knarr::Request/forward_header_pairs>). Under | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | L<Langertha::Knarr::PSGI> the PSGI server has already joined a header sent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | twice into one value with C<, >, and that value goes on as one line. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | This is the building block behind Knarr's classic "configure your API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | keys once, point everything at me" use case. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | =head2 upstreams | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | Required. HashRef mapping protocol name (C<openai>, C<anthropic>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | C<ollama>) to upstream base URL. The protocol's default chat path is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | appended (C</v1/chat/completions>, C</v1/messages>, C</api/chat>); an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | Ollama C</api/generate> request goes to the upstream's C</api/generate>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | here and on the raw passthrough of L<Langertha::Knarr>, and its answer is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | read from C<response>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | =head2 default_auth | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | Optional. An C<Authorization> header value to inject when the client | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | didn't send one. Usually you let the client supply its own key. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | =head2 model_id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | Optional. Defaults to C<passthrough>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | =head2 timeout | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | =head2 stall_timeout | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | Upstream timeouts in seconds from L<Langertha::Knarr::Role::UpstreamHTTP>: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | C<timeout> (default C<300>) is the total time of a non-streaming request, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | C<stall_timeout> (default C<120>) the time a streaming one may go without | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | data. C<0> disables either. They also apply to the raw passthrough of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | L<Langertha::Knarr> and L<Langertha::Knarr::PSGI>, which answer an expired | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | one with C<504> in the client protocol's error shape. When this handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | itself times out, its failure carries the category C<timeout> or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | C<stall_timeout> through the handler chain, and the client gets the same | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | C<504> or stream error frame. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | =head2 is_upstream_for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | my $same = $passthrough->is_upstream_for( 'anthropic', $engine->url ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | True when C<$url> is the upstream configured for the protocol, as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | L</same_upstream> compares them. L<Langertha::Knarr> uses it to send a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | model discovered from that very upstream to the raw passthrough. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | =head2 same_upstream | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | my $same = Langertha::Knarr::Handler::Passthrough->same_upstream( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 'https://api.openai.com', 'https://api.openai.com/v1' ); # 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | True when both URLs name the same upstream: same scheme, host, port and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | path, host case and default ports normalised, and a trailing C</v1> (the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | API version an engine's base URL carries) ignored. A gateway serving | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | several providers under paths of one host | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | (C<https://gateway.example/gw/openai>, C<.../gw/groq>) is several upstreams. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | Needs no instance; L<Langertha::Knarr::Router> calls it on the class. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | =head2 serves_protocol | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | next unless $passthrough->serves_protocol( $request->protocol ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | True when this handler can forward a request of the given protocol: an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | upstream is configured for it and the protocol has a known chat path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | (C<openai>, C<anthropic>, C<ollama>). L<Langertha::Knarr> and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | L<Langertha::Knarr::Handler::Router> only send a request to the passthrough | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | when this is true; any other request goes to the default engine instead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||