| 1 | package Langertha::Metrics; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | our $VERSION = '0.505'; | |||||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | our $DEPRECATED = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: DEPRECATED back-compat facade over Langertha::Usage / Pricing / Cost / UsageRecord - use the value objects directly | ||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Langertha::Usage; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Langertha::Pricing; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Langertha::Cost; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Langertha::UsageRecord; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | Carp::carp( | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | "Langertha::Metrics is a backwards-compatibility facade. New code should use " | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | . "Langertha::Usage / Langertha::Pricing / Langertha::Cost / Langertha::UsageRecord directly." | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # All methods here are kept for backwards compatibility with existing | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # Skeid/Knarr code. New code should construct Langertha::Usage, | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # Langertha::Pricing, and Langertha::UsageRecord directly. | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | ▶ | sub normalize_usage { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
normalize_usage: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 3 | my ($class, $usage) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | return Langertha::Usage->from_hash($usage)->to_hash; | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub usage_from_response { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usage_from_response: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my ($class, $response) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | return Langertha::Usage->from_response($response)->to_hash; | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 3 | ▶ | sub normalize_tool_metrics { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
normalize_tool_metrics: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 3 | my ($class, $tool_calls) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | my @names; | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | ▶ | for my $tc ( @{ $tool_calls || [] } ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$tool_calls || []
MC/DC0%0/1
$tool_calls || []
$tool_calls
Truth table50%1/2
A
A= $tool_calls
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 4 | ▶ | next unless ref($tc) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 4 | my $name = $tc->{name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | 4 | ▶ | if ( !defined $name && ref( $tc->{function} ) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $name and ref $$tc{"function"} eq "HASH"
MC/DC100%2/2
not defined $name and ref $$tc{"function"} eq "HASH"
not defined $name
ref $$tc{"function"} eq "HASH"
Truth table100%3/3
not(A) and B
A= not defined $nameB= ref $$tc{"function"} eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | $name = $tc->{function}{name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 4 | ▶ | next unless defined $name && length $name; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $name and length $name
MC/DC50%1/2
defined $name and length $name
defined $name
length $name
Truth table66%2/3
A and B
A= defined $nameB= length $name
| |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 3 | push @names, $name; | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | return { | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 3 | tool_calls => scalar(@names), | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | tool_names => \@names, | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | ▶ | sub estimate_cost_usd { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
estimate_cost_usd: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | my $usage = Langertha::Usage->from_hash( $args{usage} || {} ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'usage'} || {}
MC/DC0%0/1
$args{'usage'} || {}
$args{'usage'}
Truth table50%1/2
A
A= $args{'usage'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | my $rule = $args{pricing} || {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'pricing'} || {}
MC/DC0%0/1
$args{'pricing'} || {}
$args{'pricing'}
Truth table50%1/2
A
A= $args{'pricing'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | my $pricing = Langertha::Pricing->new( default_rule => $rule ); | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | my $cost = $pricing->cost_for( $usage, undef ); | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | return $cost->to_hash; | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | ▶ | sub build_record { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build_record: called CC=7 SCAR=23.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | my $usage = Langertha::Usage->from_hash( | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | $args{usage} | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | || ( ( $args{response} && ref( $args{response} ) eq 'HASH' ) ? $args{response}{usage} : {} ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | ▶ | || {} | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition25%2/8
$args{'usage'} || ($args{'response'} && ref $args{'response'} eq 'HASH' ? $args{'response'}{'usage'} : {}) || {}
$args{"usage"} or $args{"response"} && ref $args{"response"} eq "HASH" ? $args{"response"}{"usage"} : {}
$args{"response"} and ref $args{"response"} eq "HASH"
MC/DC0%0/3
$args{'usage'} || ($args{'response'} && ref $args{'response'} eq 'HASH' ? $args{'response'}{'usage'} : {}) || {}
$args{"usage"}
$args{"response"}
ref $args{"response"} eq "HASH"
Truth table25%1/4
A or B and C
A= $args{"usage"}B= $args{"response"}C= ref $args{"response"} eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | my $tool = $class->normalize_tool_metrics( $args{tool_calls} || [] ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'tool_calls'} || []
MC/DC100%1/1
$args{'tool_calls'} || []
$args{'tool_calls'}
Truth table100%2/2
A
A= $args{'tool_calls'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | ▶ | my $pricing_rule = $args{pricing} || {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'pricing'} || {}
MC/DC0%0/1
$args{'pricing'} || {}
$args{'pricing'}
Truth table50%1/2
A
A= $args{'pricing'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 2 | my $pricing = Langertha::Pricing->new( default_rule => $pricing_rule ); | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | 2 | my $cost = $pricing->cost_for( $usage, $args{model} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | my $usage_record = Langertha::UsageRecord->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | usage => $usage, | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | cost => $cost, | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | provider => $args{provider}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | engine => $args{engine}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | model => $args{model}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | route => $args{route}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | api_key_id => $args{api_key_id}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | duration_ms => $args{duration_ms}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | started_at => $args{started_at}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | finished_at => $args{finished_at}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | tool_calls => $tool->{tool_calls}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | tool_names => $tool->{tool_names}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | pricing_version => $args{pricing_version}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | 2 | return $usage_record->to_hash; | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||