blib/lib/Langertha/CallResult.pm

filter: Statementstmt 100% 33 / 33 Branchbran 90.9% 20 / 22 Conditioncond 72.0% 18 / 25 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 100% 4 / 4 Podpod 100% 2 / 2 totaltotal 84.6% 83 / 98 CC 21 SCAR 29.0
CC
21
Cov
89%
from_http_response
21.4
with_value
2.0
BEGIN
1.0
CRAP 18.1
« blib/lib/Langertha/Engine/Gemini.pm ↑ Summary blib/lib/Langertha/Role/Chat.pm »
1 package Langertha::CallResult;
2 # ABSTRACT: Result of an embedding, transcription or image call, with usage, rate limit and timing
3 157 our $VERSION = '0.503';
4 157 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 157 ▶use Langertha::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 157 has value => (
9 is => 'ro',
10 required => 1,
11 );
12  
13  
14 157 has usage => (
15 is => 'ro',
16 isa => 'Langertha::Usage',
17 predicate => 'has_usage',
18 );
19  
20  
21 157 has rate_limit => (
22 is => 'ro',
23 isa => 'Langertha::RateLimit',
24 predicate => 'has_rate_limit',
25 );
26  
27  
28 157 has model => (
29 is => 'ro',
30 isa => 'Str',
31 predicate => 'has_model',
32 );
33  
34  
35 157 has total_seconds => (
36 is => 'ro',
37 isa => 'Num',
38 predicate => 'has_total_seconds',
39 );
40  
41  
42 157 has raw => (
43 is => 'ro',
44 isa => 'Ref',
45 predicate => 'has_raw',
46 );
47  
48  
49 27 ▶sub from_http_response {
Subroutine from_http_response: called CC=20 SCAR=30.6
Pod documented
50 27 my ( $class, $engine, $http_response, %args ) = @_;
51 27 my $raw;
52 27 ▶ my $type = lc( $http_response->content_type // '' );
Condition50%1/2
$http_response->content_type // ""
l!l
270
MC/DC0%0/1
$http_response->content_type // ""
$http_response->content_type
Truth table50%1/2
A
A= $http_response->content_type
Aresult
11
00
53 27 ▶ if ( $type =~ /json/ || ( $type !~ m{\Atext/} && $http_response->content =~ /\A\s*[\{\[]/ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /json/ or not $type =~ m[\Atext/] and $http_response->content =~ /\A\s*[\{\[]/) 26 1 27
Condition50%3/6
$type =~ /json/ or not $type =~ m[\Atext/] and $http_response->content =~ /\A\s*[\{\[]/
l!l&&r!l&&!r
2601
not $type =~ m[\Atext/] and $http_response->content =~ /\A\s*[\{\[]/
!ll&&!rl&&r
100
MC/DC33%1/3
$type =~ /json/ or not $type =~ m[\Atext/] and $http_response->content =~ /\A\s*[\{\[]/
$type =~ /json/ not $type =~ m[\Atext/] $http_response->content =~ /\A\s*[\{\[]/
Truth table50%2/4
A or not(B) and C
A= $type =~ /json/B= not $type =~ m[\Atext/]C= $http_response->content =~ /\A\s*[\{\[]/
ABCresult
1XX1
0111
00X0
0100
54 26 local $@;
55 26 $raw = eval { $engine->json->decode( $http_response->content ) };
56 }
57 27 my $usage;
58 27 ▶ if ( ref $raw eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $raw eq "HASH") 26 1 27
59 26 my $block = $raw->{usage};
60 # A duration-billed transcription has no token counts; zeros would lie.
61 $usage = Langertha::Usage->from_raw($raw)
62 26 ▶ unless ref $block eq 'HASH' && ( $block->{type} // '' ) eq 'duration';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $block eq "HASH" and ($block->{"type"} // "") eq "duration" 25 1 26
Condition100%5/5
ref $block eq "HASH" and ($block->{"type"} // "") eq "duration"
!ll&&!rl&&r
5201
$block->{"type"} // ""
l!l
516
MC/DC100%2/2
ref $block eq "HASH" and ($block->{"type"} // "") eq "duration"
ref $block eq "HASH" $block->{"type"}
Truth table100%3/3
A and B
A= ref $block eq "HASH"B= $block->{"type"}
ABresult
0X0
100
111
63 }
64 my $model = ref $raw eq 'HASH' && defined $raw->{model} && !ref $raw->{model}
65 27 ▶ && length $raw->{model} ? $raw->{model} : $args{model};
Branch: 1 branch
BranchTrueFalseTotal
ref $raw eq 'HASH' && defined $raw->{'model'} && !ref($raw->{'model'}) && length $raw->{'model'} ? : 9 18 27
Condition77%7/9
ref $raw eq "HASH" and defined $raw->{"model"} and not ref $raw->{"model"} and length $raw->{"model"}
!ll&&!rl&&r
1809
ref $raw eq "HASH" and defined $raw->{"model"} and not ref $raw->{"model"}
!ll&&!rl&&r
1809
ref $raw eq "HASH" and defined $raw->{"model"}
!ll&&!rl&&r
1179
MC/DC50%2/4
ref $raw eq "HASH" and defined $raw->{"model"} and not ref $raw->{"model"} and length $raw->{"model"}
ref $raw eq "HASH" defined $raw->{"model"} not ref $raw->{"model"} length $raw->{"model"}
Truth table60%3/5
A and B and not(C) and D
A= ref $raw eq "HASH"B= defined $raw->{"model"}C= not ref $raw->{"model"}D= length $raw->{"model"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
66 27 ▶ my $rate_limit = $engine->can('has_rate_limit') && $engine->has_rate_limit
Branch: 1 branch
BranchTrueFalseTotal
$engine->can('has_rate_limit') && $engine->has_rate_limit ? : 12 15 27
Condition66%2/3
$engine->can("has_rate_limit") and $engine->has_rate_limit
!ll&&!rl&&r
01512
MC/DC50%1/2
$engine->can("has_rate_limit") and $engine->has_rate_limit
$engine->can("has_rate_limit") $engine->has_rate_limit
Truth table66%2/3
A and B
A= $engine->can("has_rate_limit")B= $engine->has_rate_limit
ABresult
0X0
100
111
67 ? $engine->rate_limit : undef;
68 return $class->new(
69 value => $args{value},
70 defined $usage ? ( usage => $usage ) : (),
71 defined $rate_limit ? ( rate_limit => $rate_limit ) : (),
72 defined $model ? ( model => $model ) : (),
73 27 ▶ defined $args{total_seconds} ? ( total_seconds => $args{total_seconds} ) : (),
Branch: 5 branches
BranchTrueFalseTotal
defined $usage ? : 21 6 27
defined $rate_limit ? : 12 15 27
defined $model ? : 27 0 27
defined $args{'total_seconds'} ? : 27 0 27
defined $raw ? : 26 1 27
74 defined $raw ? ( raw => $raw ) : (),
75 );
76 }
77  
78  
79 2 ▶sub with_value {
Subroutine with_value: called CC=2 SCAR=6.9
Pod documented
80 2 my ( $self, $value ) = @_;
81 return $self->new(
82 value => $value,
83 2 ▶ map { my $has = "has_$_"; $self->$has ? ( $_ => $self->$_ ) : () }
Branch: 1 branch
BranchTrueFalseTotal
$self->$has ? : 8 2 10
84 qw( usage rate_limit model total_seconds raw ),
85 );
86 }
87  
88  
89 157 __PACKAGE__->meta->make_immutable;
90  
91  
92 157 1;
93  
94 __END__
95  
96 =pod
97  
98 =encoding UTF-8
99  
100 =head1 NAME
101  
102 Langertha::CallResult - Result of an embedding, transcription or image call, with usage, rate limit and timing
103  
104 =head1 VERSION
105  
106 version 0.503
107  
108 =head1 SYNOPSIS
109  
110 my $result = $engine->simple_embedding_result('Hello world');
111 my $vector = $result->value; # what simple_embedding returns
112 say $result->usage->input_tokens if $result->has_usage;
113 say $result->rate_limit->requests_remaining if $result->has_rate_limit;
114 say $result->model, ' in ', $result->total_seconds, 's';
115  
116 my $images = ( await $engine->simple_image_result_f('A cat') )->value;
117 my $text = $engine->simple_transcription_call('speech.mp3')->value;
118  
119 =head1 DESCRIPTION
120  
121 The answer of one non-chat call (embedding, transcription, image generation)
122 together with what the HTTP response said about it. L</value> is exactly what
123 the bare method (C<simple_embedding>, C<simple_transcription>,
124 C<simple_image>) returns; the other attributes are the provider's usage block,
125 the rate limit of this response, the model that answered and the measured time.
126  
127 It is deliberately not a L<Langertha::Response>: that class is chat-shaped (it
128 stringifies to C<content>, carries C<tool_calls>, C<thinking>,
129 C<finish_reason>), and a vector or a list of images has none of that. See ADR
130 0034 in the distribution's C<docs/adr/>.
131  
132 =head2 value
133  
134 The call's result, the same value the bare method returns: an embedding vector
135 (ArrayRef of floats) or an ArrayRef of vectors for a batch, the transcript
136 text, or the ArrayRef of image objects.
137  
138 =head2 usage
139  
140 A L<Langertha::Usage> read from the response body by
141 L<Langertha::Usage/from_raw>: OpenAI embeddings' C<usage>
142 (C<prompt_tokens>, C<total_tokens>), a GPT image model's C<usage>, a token-billed
143 transcription's C<usage>, Ollama's C<prompt_eval_count>. Not set when the body
144 reports none (Gemini embeddings, Whisper servers), and not set for a
145 duration-billed transcription (C<< usage => { type => 'duration', seconds => N } >>):
146 it has no tokens to count, and its seconds stay in L</raw>. Check
147 L</has_usage> first.
148  
149 =head2 rate_limit
150  
151 The L<Langertha::RateLimit> parsed from this response's headers, the same
152 object L<Langertha::Engine::Remote/rate_limit> returns right after the call.
153 Not set when the response carried no rate limit headers.
154  
155 =head2 model
156  
157 The model that answered: the C<model> field of the response body when the
158 provider sends one (OpenAI embeddings do), otherwise the model that was
159 requested. Not set when neither is known (a self-hosted embedding server
160 called without a model).
161  
162 =head2 total_seconds
163  
164 Wall-clock seconds from sending the request to receiving the full response,
165 measured by the client like L<Langertha::Response/total_seconds>.
166  
167 =head2 raw
168  
169 The decoded response body when the provider answered JSON; not set for a
170 plain-text body (a transcription with C<response_format> C<text>, C<srt> or
171 C<vtt>). Fields Langertha does not lift (C<segments>, C<duration>,
172 C<revised_prompt>, ...) stay reachable here.
173  
174 =head2 from_http_response
175  
176 my $result = Langertha::CallResult->from_http_response( $engine, $http_response,
177 value => $value, model => $requested_model, total_seconds => $seconds );
178  
179 Builds the result of a call whose C<$http_response> the engine has already
180 parsed into C<$value> (so an HTTP error has croaked before this point). Reads
181 L</raw>, L</usage> and the answering L</model> from the body, and takes
182 L</rate_limit> from the engine, which recorded it from this response while
183 parsing. C<model> is the requested model, used when the body names none.
184  
185 =head2 with_value
186  
187 my $replaced = $result->with_value($new_value);
188  
189 Returns a new C<Langertha::CallResult> with C<$new_value> as L</value> and
190 every other attribute copied; C<$result> itself is unchanged. Used by
191 L<Langertha::Embedder> and L<Langertha::ImageGen> when a plugin after-hook
192 replaces the value.
193  
194 =head1 SEE ALSO
195  
196 =over
197  
198 =item * L<Langertha::Role::Embedding/simple_embedding_result>
199  
200 =item * L<Langertha::Role::Transcription/simple_transcription_call>
201  
202 =item * L<Langertha::Role::ImageGeneration/simple_image_result>
203  
204 =item * L<Langertha::Response> - The chat counterpart
205  
206 =item * L<Langertha::Usage>, L<Langertha::RateLimit>
207  
208 =back
209  
210 =head1 SUPPORT
211  
212 =head2 Issues
213  
214 Please report bugs and feature requests on GitHub at
215 L<https://github.com/Getty/langertha/issues>.
216  
217 =head2 IRC
218  
219 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
220  
221 =head1 CONTRIBUTING
222  
223 Contributions are welcome! Please fork the repository and submit a pull request.
224  
225 =head1 AUTHOR
226  
227 Torsten Raudssus <getty@cpan.org>
228  
229 =head1 COPYRIGHT AND LICENSE
230  
231 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
232  
233 This is free software; you can redistribute it and/or modify it under
234 the same terms as the Perl 5 programming language system itself.
235  
236 =cut
« blib/lib/Langertha/Engine/Gemini.pm ↑ Summary blib/lib/Langertha/Role/Chat.pm »