Langertha/Response.pm

filter: Statementstmt 100% 68 / 68 Branchbran 91.1% 31 / 34 Conditioncond 56.2% 9 / 16 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 100% 13 / 13 Podpod 100% 8 / 8 totaltotal 87.2% 130 / 149 CC 26 SCAR 15.1
CC
26
Cov
92%
__ANON__
8.3
tool_call
6.0
total_tokens
5.2
CRAP 4.5
« Langertha/Engine/HuggingFace.pm ↑ Summary Langertha.pm »
1 package Langertha::Response;
2 # ABSTRACT: LLM response with metadata
3 35 our $VERSION = '0.502';
4 35 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use Langertha::ToolCall;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 use overload
8 12 ▶ '""' => sub { $_[0]->content },
Subroutine __ANON__: called CC=1 SCAR=0.0
9 35 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 35 has content => (
13 is => 'ro',
14 isa => 'Str',
15 required => 1,
16 );
17  
18  
19 35 has raw => (
20 is => 'ro',
21 isa => 'HashRef',
22 predicate => 'has_raw',
23 );
24  
25  
26 35 has id => (
27 is => 'ro',
28 isa => 'Str',
29 predicate => 'has_id',
30 );
31  
32  
33 35 has model => (
34 is => 'ro',
35 isa => 'Str',
36 predicate => 'has_model',
37 );
38  
39  
40 35 has finish_reason => (
41 is => 'ro',
42 isa => 'Maybe[Str]',
43 predicate => 'has_finish_reason',
44 );
45  
46  
47 35 has usage => (
48 is => 'ro',
49 isa => 'Maybe[HashRef]',
50 predicate => 'has_usage',
51 );
52  
53  
54 35 has timing => (
55 is => 'ro',
56 isa => 'Maybe[HashRef]',
57 predicate => 'has_timing',
58 );
59  
60  
61 35 has created => (
62 is => 'ro',
63 isa => 'Maybe[Int]',
64 predicate => 'has_created',
65 );
66  
67  
68 35 has thinking => (
69 is => 'ro',
70 isa => 'Maybe[Str]',
71 predicate => 'has_thinking',
72 );
73  
74 35 has rate_limit => (
75 is => 'ro',
76 isa => 'Maybe[Langertha::RateLimit]',
77 predicate => 'has_rate_limit',
78 );
79  
80 35 has tool_calls => (
81 is => 'ro',
82 isa => 'Maybe[ArrayRef[Langertha::ToolCall]]',
83 predicate => 'has_tool_calls',
84 );
85  
86 around BUILDARGS => sub {
87 73 ▶ my ( $orig, $class, @args ) = @_;
Subroutine __ANON__: called CC=8 SCAR=21.2
88 73 my $params = $class->$orig(@args);
89  
90 # Accept legacy ArrayRef[HashRef] input by upgrading to ToolCall objects.
91 73 ▶ if ( ref( $params->{tool_calls} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params->{"tool_calls"} eq "ARRAY") 6 67 73
92 $params->{tool_calls} = [
93 map {
94 6 ▶ ref($_) && eval { $_->isa('Langertha::ToolCall') }
Branch: 2 branches
BranchTrueFalseTotal
ref $_->{'arguments'} eq 'HASH' ? : 2 0 2
$_->{'synthetic'} ? : 1 1 2
Condition50%2/4
$_->{"name"} // ""
l!l
20
$_->{"id"} // ""
l!l
02
MC/DC0%0/1
$_->{"name"} // ""
$_->{"name"}
MC/DC0%0/1
$_->{"id"} // ""
$_->{"id"}
Truth table50%1/2
A
A= $_->{"name"}
Aresult
11
00
Truth table50%1/2
A
A= $_->{"id"}
Aresult
11
00
95 ? $_
96 : Langertha::ToolCall->new(
97 name => ( $_->{name} // '' ),
98 arguments => ( ref( $_->{arguments} ) eq 'HASH' ? $_->{arguments} : {} ),
99 id => ( $_->{id} // '' ),
100 6 ▶ synthetic => ( $_->{synthetic} ? 1 : 0 ),
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && eval { do { $_->isa('Langertha::ToolCall') } } ? : 4 2 6
Condition66%2/3
ref $_ and eval {
!ll&&!rl&&r
024
MC/DC50%1/2
ref $_ and eval { do { $_->isa("Langertha::ToolCall") } }
ref $_ eval { do { $_->isa("Langertha::ToolCall") } }
Truth table66%2/3
A and B
A= ref $_B= eval { do { $_->isa("Langertha::ToolCall") } }
ABresult
0X0
100
111
101 );
102 6 } @{ $params->{tool_calls} }
103 ];
104 }
105 73 return $params;
106 35 };
107  
108  
109  
110 sub tool_call {
111 11 ▶ my ( $self, $name ) = @_;
Subroutine tool_call: called CC=6 SCAR=17.9
Pod documented
112 11 ▶ my $tcs = $self->tool_calls or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $tcs = $self->tool_calls or return undef 2 9 11
113 9 ▶ return undef unless @$tcs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$tcs 0 9 9
114 9 ▶ return $tcs->[0] unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 7 2 9
115 2 for my $tc (@$tcs) {
116 2 ▶ return $tc if $tc->name eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $tc->name eq $name 1 1 2
117 }
118 1 return undef;
119 }
120  
121  
122 sub tool_call_args {
123 5 ▶ my ( $self, $name ) = @_;
Subroutine tool_call_args: called CC=2 SCAR=6.9
Pod documented
124 5 ▶ my $tc = $self->tool_call($name) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $tc = $self->tool_call($name) or return undef 2 3 5
125 3 return $tc->arguments;
126 }
127  
128  
129  
130 sub clone_with {
131 30 ▶ my ( $self, %overrides ) = @_;
Subroutine clone_with: called CC=3 SCAR=11.0
Pod documented
132 30 my %args = (content => $self->content);
133 30 for my $attr (qw( raw id model finish_reason usage timing created thinking rate_limit tool_calls )) {
134 300 my $pred = "has_$attr";
135 300 ▶ $args{$attr} = $self->$attr if $self->$pred;
Branch: 1 branch
BranchTrueFalseTotal
if $self->$pred 134 166 300
136 }
137 30 return (ref $self)->new(%args, %overrides);
138 }
139  
140  
141 sub prompt_tokens {
142 11 ▶ my ( $self ) = @_;
Subroutine prompt_tokens: called CC=3 SCAR=11.0
Pod documented
143 11 ▶ my $u = $self->usage or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $u = $self->usage or return undef 1 10 11
144 10 ▶ return $u->{prompt_tokens} // $u->{input_tokens};
Condition66%2/3
$u->{"prompt_tokens"} // $u->{"input_tokens"}
l!l&&r!l&&!r
640
MC/DC0%0/2
$u->{"prompt_tokens"} // $u->{"input_tokens"}
$u->{"prompt_tokens"} $u->{"input_tokens"}
Truth table66%2/3
A // B
A= $u->{"prompt_tokens"}B= $u->{"input_tokens"}
ABresult
1X1
011
000
145 }
146  
147  
148 sub completion_tokens {
149 9 ▶ my ( $self ) = @_;
Subroutine completion_tokens: called CC=3 SCAR=11.0
Pod documented
150 9 ▶ my $u = $self->usage or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $u = $self->usage or return undef 1 8 9
151 8 ▶ return $u->{completion_tokens} // $u->{output_tokens};
Condition66%2/3
$u->{"completion_tokens"} // $u->{"output_tokens"}
l!l&&r!l&&!r
440
MC/DC0%0/2
$u->{"completion_tokens"} // $u->{"output_tokens"}
$u->{"completion_tokens"} $u->{"output_tokens"}
Truth table66%2/3
A // B
A= $u->{"completion_tokens"}B= $u->{"output_tokens"}
ABresult
1X1
011
000
152 }
153  
154  
155 sub total_tokens {
156 6 ▶ my ( $self ) = @_;
Subroutine total_tokens: called CC=5 SCAR=16.4
Pod documented
157 6 ▶ my $u = $self->usage or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $u = $self->usage or return undef 1 5 6
158 5 ▶ return $u->{total_tokens} if defined $u->{total_tokens};
Branch: 1 branch
BranchTrueFalseTotal
if defined $u->{"total_tokens"} 3 2 5
159 2 my $p = $self->prompt_tokens;
160 2 my $c = $self->completion_tokens;
161 2 ▶ return undef unless defined $p && defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p and defined $c 0 2 2
Condition33%1/3
defined $p and defined $c
!ll&&!rl&&r
002
MC/DC0%0/2
defined $p and defined $c
defined $p defined $c
Truth table33%1/3
A and B
A= defined $pB= defined $c
ABresult
0X0
100
111
162 2 return $p + $c;
163 }
164  
165  
166 sub requests_remaining {
167 2 ▶ my ( $self ) = @_;
Subroutine requests_remaining: called CC=2 SCAR=6.9
Pod documented
168 2 ▶ my $rl = $self->rate_limit or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $rl = $self->rate_limit or return undef 1 1 2
169 1 return $rl->requests_remaining;
170 }
171  
172  
173 sub tokens_remaining {
174 2 ▶ my ( $self ) = @_;
Subroutine tokens_remaining: called CC=2 SCAR=6.9
Pod documented
175 2 ▶ my $rl = $self->rate_limit or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $rl = $self->rate_limit or return undef 1 1 2
176 1 return $rl->tokens_remaining;
177 }
178  
179  
180  
181 35 __PACKAGE__->meta->make_immutable;
182  
183 35 1;
184  
185 __END__
186  
187 =pod
188  
189 =encoding UTF-8
190  
191 =head1 NAME
192  
193 Langertha::Response - LLM response with metadata
194  
195 =head1 VERSION
196  
197 version 0.502
198  
199 =head1 SYNOPSIS
200  
201 my $response = $engine->simple_chat('Hello');
202  
203 # Stringifies to content (backward compatible)
204 print $response;
205 print "Response: $response\n";
206  
207 # Access metadata
208 say $response->model;
209 say $response->id;
210 say $response->finish_reason;
211  
212 # Token usage
213 say "Prompt tokens: ", $response->prompt_tokens;
214 say "Completion tokens: ", $response->completion_tokens;
215 say "Total tokens: ", $response->total_tokens;
216  
217 # Full raw response
218 use Data::Dumper;
219 print Dumper($response->raw);
220  
221 =head1 DESCRIPTION
222  
223 Wraps LLM response text content together with all available metadata
224 from the API response. Uses C<overload> for string context so existing
225 code treating responses as plain strings continues to work.
226  
227 =head2 content
228  
229 The text content of the response. Required.
230  
231 =head2 raw
232  
233 The full parsed API response as a HashRef.
234  
235 =head2 id
236  
237 Provider-specific response ID.
238  
239 =head2 model
240  
241 The actual model used for the response.
242  
243 =head2 finish_reason
244  
245 Why the response ended: C<stop>, C<end_turn>, C<length>, C<tool_calls>, etc.
246 Provider-specific values are preserved as-is.
247  
248 =head2 usage
249  
250 Token usage counts as a HashRef. Keys vary by provider but are normalized
251 by the convenience methods.
252  
253 =head2 timing
254  
255 Timing information as a HashRef. Currently only populated by Ollama.
256  
257 =head2 created
258  
259 Unix timestamp of when the response was created.
260  
261 =head2 tool_calls
262  
263 ArrayRef of L<Langertha::ToolCall> objects extracted from the
264 response, when the engine produced any. Single source of truth for
265 "what tool calls did the model emit" — both native provider tool
266 calling and synthesized fallbacks (forced-name via response_format)
267 land here in the same shape.
268  
269 For backward compatibility C<BUILDARGS> upgrades plain HashRefs
270 (C<{ name =E<gt> ..., arguments =E<gt> ..., id =E<gt> ..., synthetic =E<gt> ... }>)
271 into L<Langertha::ToolCall> objects automatically; new code should
272 construct the objects directly.
273  
274 =head2 tool_call
275  
276 my $tc = $response->tool_call; # first tool call
277 my $tc = $response->tool_call($name); # named lookup
278  
279 Returns the L<Langertha::ToolCall> for the first tool call (or the
280 first matching C<$name>), or C<undef> when no tool calls were
281 produced.
282  
283 =head2 tool_call_args
284  
285 my $args = $response->tool_call_args; # first tool call
286 my $args = $response->tool_call_args($name); # named lookup
287  
288 Returns the C<arguments> HashRef of the first tool call, or of the
289 first tool call matching C<$name>. Returns C<undef> when no tool
290 calls were produced.
291  
292 =head2 rate_limit
293  
294 Optional L<Langertha::RateLimit> object with rate limit information from the
295 API response headers. Only present when the provider returns rate limit headers.
296  
297 =head2 thinking
298  
299 Chain-of-thought reasoning content. Populated automatically from native API
300 fields (DeepSeek C<reasoning_content>, Anthropic C<thinking> blocks, Gemini
301 C<thought> parts) or from C<E<lt>thinkE<gt>> tag filtering when
302 L<Langertha::Role::ThinkTag/think_tag_filter> is enabled.
303  
304 =head2 clone_with
305  
306 my $new = $response->clone_with(content => $filtered, thinking => $thought);
307  
308 Returns a new Response with the same attributes as the original, except for
309 the overrides provided. Used by L<Langertha::Role::ThinkTag> to produce a
310 filtered response while preserving metadata.
311  
312 =head2 prompt_tokens
313  
314 Returns the number of prompt/input tokens. Checks C<prompt_tokens> and
315 C<input_tokens> keys in usage.
316  
317 =head2 completion_tokens
318  
319 Returns the number of completion/output tokens. Checks C<completion_tokens>
320 and C<output_tokens> keys in usage.
321  
322 =head2 total_tokens
323  
324 Returns the total token count. Uses C<total_tokens> from usage if available,
325 otherwise sums prompt and completion tokens.
326  
327 =head2 requests_remaining
328  
329 Returns the number of requests remaining from rate limit headers, or C<undef>.
330  
331 =head2 tokens_remaining
332  
333 Returns the number of tokens remaining from rate limit headers, or C<undef>.
334  
335 =head1 SEE ALSO
336  
337 =over
338  
339 =item * L<Langertha::RateLimit> - Rate limit data from response headers
340  
341 =item * L<Langertha::Stream::Chunk> - Single chunk from a streaming response
342  
343 =item * L<Langertha::Role::Chat> - Chat role that produces response objects
344  
345 =item * L<Langertha::Role::OpenAICompatible> - Parses responses into this class
346  
347 =back
348  
349 =head1 SUPPORT
350  
351 =head2 Issues
352  
353 Please report bugs and feature requests on GitHub at
354 L<https://github.com/Getty/langertha/issues>.
355  
356 =head2 IRC
357  
358 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
359  
360 =head1 CONTRIBUTING
361  
362 Contributions are welcome! Please fork the repository and submit a pull request.
363  
364 =head1 AUTHOR
365  
366 Torsten Raudssus <getty@cpan.org>
367  
368 =head1 COPYRIGHT AND LICENSE
369  
370 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
371  
372 This is free software; you can redistribute it and/or modify it under
373 the same terms as the Perl 5 programming language system itself.
374  
375 =cut
« Langertha/Engine/HuggingFace.pm ↑ Summary Langertha.pm »