| 1 | package Langertha::Metrics; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | Langertha::Metrics - DEPRECATED back-compat facade over Langertha::Usage / Pricing / Cost / UsageRecord — use the value objects directly | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||
| 105 | version 0.503 | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | =head1 DEPRECATION | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | Langertha::Metrics is a thin back-compat facade kept solely so existing | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | Skeid/Knarr consumers keep working. It is B<scheduled for removal in | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | Langertha 0.504>. New code must construct the value objects directly | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | — no replacement module, just stop calling this one. | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | Migration map (one-to-one): | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | =item * C<normalize_usage($hash)> | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | = Langertha::Usage->from_hash($hash)->to_hash | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | =item * C<usage_from_response($response)> | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | = Langertha::Usage->from_response($response)->to_hash | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | =item * C<estimate_cost_usd(usage => ..., pricing => ...)> | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | = Langertha::Pricing->new( default_rule => $pricing )->cost_for( | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | Langertha::Usage->from_hash($usage), $model | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | )->to_hash | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | =item * C<build_record(...)> | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | Construct L<Langertha::UsageRecord> directly: build a | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | L<Langertha::Usage> from the response or hash, build a | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | L<Langertha::Pricing> with the C<pricing> rule, call | ||||||||||||||||||||||||||||||||||||||||||||||
| 137 | C<< ->cost_for($usage, $model) >>, then C<< Langertha::UsageRecord->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | usage => $usage, cost => $cost, provider => ..., engine => ..., model => ..., | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | route => ..., api_key_id => ..., duration_ms => ..., started_at => ..., | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | finished_at => ..., tool_calls => ..., tool_names => ..., | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | pricing_version => ... ) >> and C<< ->to_hash >>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||
| 143 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||
| 145 | Note: C<Langertha::Response>'s C<usage> attribute is the raw provider | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | payload as returned on the wire — it is B<unrelated> to this facade's | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | normalization helpers and is not affected by the deprecation. | ||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||
| 151 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||
| 153 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||
| 160 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||
| 166 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||
| 173 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||