blib/lib/Langertha/Metrics.pm

filter: Statementstmt 96.3% 52 / 54 Branchbran 62.5% 5 / 8 Conditioncond 54.1% 13 / 24 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 91.6% 11 / 12 Podpod 80.0% 4 / 5 totaltotal 77.3% 89 / 115 CC 16 SCAR 17.6
CC
16
Cov
81%
build_record
10.4
normalize_tool_metrics
8.1
estimate_cost_usd
3.1
CRAP 5.8
« blib/lib/Langertha/PromptCache.pm ↑ Summary blib/lib/Langertha/Input/Tools.pm »
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 || []
l!l
30
MC/DC0%0/1
$tool_calls || []
$tool_calls
Truth table50%1/2
A
A= $tool_calls
Aresult
11
00
37 4 ▶ next unless ref($tc) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tc eq "HASH" 0 4 4
38 4 my $name = $tc->{name};
39 4 ▶ if ( !defined $name && ref( $tc->{function} ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $name and ref $$tc{"function"} eq "HASH") 1 3 4
Condition100%3/3
not defined $name and ref $$tc{"function"} eq "HASH"
!ll&&!rl&&r
211
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"
ABresult
0X0
100
111
40 1 $name = $tc->{function}{name};
41 }
42 4 ▶ next unless defined $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 1 3 4
Condition66%2/3
defined $name and length $name
!ll&&!rl&&r
103
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
ABresult
0X0
100
111
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'} || {}
l!l
10
MC/DC0%0/1
$args{'usage'} || {}
$args{'usage'}
Truth table50%1/2
A
A= $args{'usage'}
Aresult
11
00
54 1 ▶ my $rule = $args{pricing} || {};
Condition50%1/2
$args{'pricing'} || {}
l!l
10
MC/DC0%0/1
$args{'pricing'} || {}
$args{'pricing'}
Truth table50%1/2
A
A= $args{'pricing'}
Aresult
11
00
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
BranchTrueFalseTotal
$args{'response'} && ref $args{'response'} eq 'HASH' ? : 0 0 0
Condition25%2/8
$args{'usage'} || ($args{'response'} && ref $args{'response'} eq 'HASH' ? $args{'response'}{'usage'} : {}) || {}
l!l
20
$args{"usage"} or $args{"response"} && ref $args{"response"} eq "HASH" ? $args{"response"}{"usage"} : {}
l!l&&r!l&&!r
200
$args{"response"} and ref $args{"response"} eq "HASH"
!ll&&!rl&&r
000
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"
ABCresult
1XX1
0111
00X0
0100
66 );
67 2 ▶ my $tool = $class->normalize_tool_metrics( $args{tool_calls} || [] );
Condition100%2/2
$args{'tool_calls'} || []
l!l
11
MC/DC100%1/1
$args{'tool_calls'} || []
$args{'tool_calls'}
Truth table100%2/2
A
A= $args{'tool_calls'}
Aresult
11
00
68  
69 2 ▶ my $pricing_rule = $args{pricing} || {};
Condition50%1/2
$args{'pricing'} || {}
l!l
20
MC/DC0%0/1
$args{'pricing'} || {}
$args{'pricing'}
Truth table50%1/2
A
A= $args{'pricing'}
Aresult
11
00
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__
« blib/lib/Langertha/PromptCache.pm ↑ Summary blib/lib/Langertha/Input/Tools.pm »