blib/lib/Langertha/Engine/OpenRouter.pm

filter: Statementstmt 100% 28 / 28 Branchbran 50.0% 2 / 4 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 11 / 11 Podpod 25.0% 1 / 4 totaltotal 76.2% 45 / 59 CC 6 SCAR 10.6
CC
6
Cov
82%
_wire_usage
6.6
_build_api_key
2.1
BEGIN
1.0
CRAP 2.9
« blib/lib/Langertha/Role/ImageGeneration.pm ↑ Summary blib/lib/Langertha/Engine/SGLang.pm »
1 package Langertha::Engine::OpenRouter;
2 # ABSTRACT: OpenRouter API
3 19 our $VERSION = '0.505';
4 19 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 19 extends 'Langertha::Engine::OpenAIBase';
9  
10 19 with 'Langertha::Role::Tools';
11  
12  
13 has '+url' => (
14 lazy => 1,
15 5 ▶ default => sub { 'https://openrouter.ai/api/v1' },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 19 );
17  
18 1 ▶sub _build_api_key {
Subroutine _build_api_key: called CC=2 SCAR=7.2
19 1 my ( $self ) = @_;
20 return $ENV{LANGERTHA_OPENROUTER_API_KEY}
21 1 ▶ || croak "".(ref $self)." requires LANGERTHA_OPENROUTER_API_KEY or api_key set";
Condition50%1/2
$ENV{'LANGERTHA_OPENROUTER_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_OPENROUTER_API_KEY or api_key set')
l!l
10
MC/DC0%0/1
$ENV{'LANGERTHA_OPENROUTER_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_OPENROUTER_API_KEY or api_key set')
$ENV{'LANGERTHA_OPENROUTER_API_KEY'}
Truth table50%1/2
A
A= $ENV{'LANGERTHA_OPENROUTER_API_KEY'}
Aresult
11
00
22 }
23  
24 4 ▶sub default_model { croak "".(ref $_[0])." requires model to be set" }
Subroutine default_model: called CC=1 SCAR=0.0
Pod documented
25  
26 18 ▶sub _build_supported_operations {[qw(
Subroutine _build_supported_operations: called CC=1 SCAR=0.0
27 createChatCompletion
28 )]}
29  
30 # image_input (k266, ADR 0019): a gateway: the model behind it is unknown to
31 # the client, so no static claim. The catch-all is a layer-3 row, not a
32 # layer-2 delete, so a fact probed from /models (architecture.input_modalities)
33 # can answer per model (ADR 0032).
34 144 ▶sub model_capability_corrections {
Subroutine model_capability_corrections: called CC=1 SCAR=0.0
Pod undocumented
35 144 return ( qr/\A/ => { image_input => 0 } );
36 }
37  
38 # usage.cost is what OpenRouter charged, in its credits, and "OpenRouter uses a
39 # credit system where the base currency is US dollars" (openrouter.ai/docs/faq,
40 # read 2026-09-30). The wire number names no unit and "cost" is too generic to
41 # read as USD from every OpenAI-compatible server, so this engine states it:
42 # the copy of the usage block gets the canonical cost_usd key that
43 # Langertha::Usage reads (ADR 0018 tier 3, ADR 0031, k363).
44 # cost_details.upstream_inference_cost (BYOK: what the key's own provider
45 # charged) is not OpenRouter's bill and is not folded in.
46 2 ▶sub _wire_usage {
Subroutine _wire_usage: called CC=5 SCAR=18.9
47 2 my ( $self, $usage ) = @_;
48 2 ▶ return $usage unless ref $usage eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $usage eq "HASH" 0 2 2
49 2 my $cost = $usage->{cost};
50 2 ▶ return $usage unless defined $cost && !ref $cost && looks_like_number($cost);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cost and not ref $cost and &looks_like_number($cost) 0 2 2
Condition33%2/6
defined $cost and not ref $cost and &looks_like_number($cost)
!ll&&!rl&&r
002
defined $cost and not ref $cost
!ll&&!rl&&r
002
MC/DC0%0/3
defined $cost and not ref $cost and &looks_like_number($cost)
defined $cost not ref $cost &looks_like_number($cost)
Truth table25%1/4
A and not(B) and C
A= defined $costB= not ref $costC= &looks_like_number($cost)
ABCresult
0XX0
10X0
1100
1111
51 2 return { %$usage, cost_usd => 0 + $cost };
52 }
53  
54 35 ▶sub model_metadata_format { 'openrouter' }
Subroutine model_metadata_format: called CC=1 SCAR=0.0
Pod undocumented
55 16 ▶sub model_metadata_url { $_[0]->url . $_[0]->list_models_path }
Subroutine model_metadata_url: called CC=1 SCAR=0.0
Pod undocumented
56  
57 19 __PACKAGE__->meta->make_immutable;
58  
59  
60 19 1;
61  
62 __END__
« blib/lib/Langertha/Role/ImageGeneration.pm ↑ Summary blib/lib/Langertha/Engine/SGLang.pm »