| 1 | package Langertha::Engine::Remote; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Base class for all remote engines | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 193 | our $VERSION = '0.505'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 193 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 193 | ▶ | use Langertha::RateLimit; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 193 | with map { 'Langertha::Role::'.$_ } qw( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | HTTP | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | PluginHost | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 193 | has '+url' => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 50 | ▶ | sub api_key_env { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
api_key_env: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | my ( $class ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
api_key_env: not called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | (my $name = $class) =~ s/^Langertha::Engine:://; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | return 'LANGERTHA_'.uc($name).'_API_KEY'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 30 | ▶ | sub api_key_required { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
api_key_required: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my ( $class ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
api_key_required: not called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | return defined $class->api_key_env ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 193 | has _last_rate_limit => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | isa => 'Maybe[Langertha::RateLimit]', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | predicate => '_has_last_rate_limit', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | clearer => '_clear_last_rate_limit', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | init_arg => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 83 | ▶ | sub rate_limit { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rate_limit: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rate_limit: not called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | return $self->_last_rate_limit; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 392 | ▶ | sub has_rate_limit { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_rate_limit: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_rate_limit: not called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | return defined $self->_last_rate_limit; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 1522 | ▶ | sub _update_rate_limit { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_update_rate_limit: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my ( $self, $http_response ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_update_rate_limit: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # No headers clears: rate_limit describes the latest response, never an | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # older one (karr k300). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my $rl = $self->_parse_rate_limit_headers($http_response); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $self->_last_rate_limit($rl); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | return $rl; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # The fallback for engines without a dialect parser (Gemini, Ollama native, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # AKI native, LM Studio native): no buckets to normalize, but a Retry-After or | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # retry-after-ms on a 429/503 is still what a caller backs off from, so it | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # gets a RateLimit carrying retry_after and raw. No per-engine parsers (k312). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 305 | ▶ | sub _parse_rate_limit_headers { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_rate_limit_headers: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | my ( $self, $http_response ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_rate_limit_headers: not called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | require Langertha::RateLimit; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | require Langertha::Moment; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my %raw = Langertha::RateLimit::_collect_headers($http_response); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | return undef unless defined $raw{'retry-after'} || defined $raw{'retry-after-ms'}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%3/6
defined $raw{"retry-after"} or defined $raw{"retry-after-ms"}
defined $raw{"retry-after"} or defined $raw{"retry-after-ms"}
MC/DC100%2/2
defined $raw{"retry-after"} or defined $raw{"retry-after-ms"}
defined $raw{"retry-after"}
defined $raw{"retry-after-ms"}
MC/DC0%0/2
defined $raw{"retry-after"} or defined $raw{"retry-after-ms"}
defined $raw{"retry-after"}
defined $raw{"retry-after-ms"}
Truth table100%3/3
A or B
A= defined $raw{"retry-after"}B= defined $raw{"retry-after-ms"}
Truth table0%0/3
A or B
A= defined $raw{"retry-after"}B= defined $raw{"retry-after-ms"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | return Langertha::RateLimit->new( received => Langertha::Moment->now_utc, raw => \%raw ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # Wire-agnostic generation-parameter block shared by every chat dialect | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | # (OpenAI, Anthropic, ...). Extracts the can-guarded sibling calls for | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # reasoning_kwargs_for / prompt_cache_kwargs_for; the dialect-specific | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # fields (response_format on OpenAI, inference_geo on Anthropic, ...) stay | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # inline because their placement in the request body depends on dialect | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # conventions. Temperature also stays inline: OpenAI emits seed between | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # temperature and reasoning, so folding temperature in here would shift | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # seed and break the byte order of the OpenAI body (karr #98, ADR 0015 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # Decision 3). The helper name is deliberately not knobs_kwargs_for - | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # that name belongs to Langertha::Role::RuntimeKnobs for the self-hosted | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | # prefix-cache knobs (ADR 0012). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 2643 | ▶ | sub generation_kwargs_for { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
generation_kwargs_for: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | my ( $self, %controls ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
generation_kwargs_for: not called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | ( $self->can('reasoning_kwargs_for') ? $self->reasoning_kwargs_for(%controls) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | ( $self->can('prompt_cache_kwargs_for') ? $self->prompt_cache_kwargs_for(%controls) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 193 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 193 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||