Langertha/ToolCall.pm

filter: Statementstmt 87.3% 104 / 119 Branchbran 56.6% 51 / 90 Conditioncond 36.3% 16 / 44 MC/DCmcdc 4.3% 1 / 23 Subroutinesub 86.6% 13 / 15 Podpod 0.0% 0 / 11 totaltotal 61.2% 185 / 302 CC 69 SCAR 28.5
CC
69
Cov
68%
from_ollama
56.0
extract
25.3
from_gemini
22.7
CRAP 17.3
« Langertha/Tool.pm ↑ Summary Langertha/Chat.pm »
1 package Langertha::ToolCall;
2 # ABSTRACT: Immutable canonical tool invocation emitted by an LLM
3 39 our $VERSION = '0.502';
4 39 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 39 ▶use JSON::MaybeXS qw( encode_json decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 39 has name => (
8 is => 'ro',
9 isa => 'Str',
10 required => 1,
11 );
12  
13 has arguments => (
14 is => 'ro',
15 isa => 'HashRef',
16 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
17 39 );
18  
19 # Provider-specific call id (may be empty if the upstream didn't supply one).
20 39 has id => (
21 is => 'ro',
22 isa => 'Str',
23 default => '',
24 );
25  
26 # True when this call was synthesized by Langertha (e.g. forced-tool
27 # rewrite via response_format on engines without native named-tool
28 # forcing) rather than emitted directly by the model. Useful for
29 # callers that want to distinguish "the model decided to call this"
30 # from "we asked it to and parsed the result back into a tool_call".
31 39 has synthetic => (
32 is => 'ro',
33 isa => 'Bool',
34 default => 0,
35 );
36  
37  
38 sub _decode_args {
39 7 ▶ my ($args) = @_;
Subroutine _decode_args: called CC=5 SCAR=17.0
40 7 ▶ return {} unless defined $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 0 7 7
41 7 ▶ return $args if ref($args) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args eq "HASH" 0 7 7
42 7 ▶ return {} unless length $args;
Branch: 1 branch
BranchTrueFalseTotal
unless length $args 0 7 7
43 7 my $decoded = eval { decode_json($args) };
44 7 ▶ return ( ref($decoded) eq 'HASH' ) ? $decoded : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $decoded eq 'HASH' ? : 7 0 7
45 }
46  
47 # --- Constructors from wire-format hashes ---
48  
49 sub from_openai {
50 5 ▶ my ($class, $hash) = @_;
Subroutine from_openai: called CC=7 SCAR=21.5
Pod undocumented
51 5 ▶ return undef unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 5 5
52 5 ▶ my $fn = $hash->{function} || {};
Condition50%1/2
$hash->{'function'} || {}
l!l
50
MC/DC0%0/1
$hash->{'function'} || {}
$hash->{'function'}
Truth table50%1/2
A
A= $hash->{'function'}
Aresult
11
00
53 5 ▶ return undef unless ref($fn) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fn eq "HASH" 0 5 5
54 5 ▶ my $name = $fn->{name} // '';
Condition50%1/2
$fn->{"name"} // ""
l!l
50
MC/DC0%0/1
$fn->{"name"} // ""
$fn->{"name"}
Truth table50%1/2
A
A= $fn->{"name"}
Aresult
11
00
55 5 ▶ return undef unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 5 5
56 return $class->new(
57 name => $name,
58 arguments => _decode_args( $fn->{arguments} ),
59 5 ▶ id => ( $hash->{id} // '' ),
Condition50%1/2
$hash->{"id"} // ""
l!l
50
MC/DC0%0/1
$hash->{"id"} // ""
$hash->{"id"}
Truth table50%1/2
A
A= $hash->{"id"}
Aresult
11
00
60 );
61 }
62  
63 sub from_anthropic {
64 12 ▶ my ($class, $block) = @_;
Subroutine from_anthropic: called CC=8 SCAR=22.8
Pod undocumented
65 12 ▶ return undef unless ref($block) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $block eq "HASH" 0 12 12
66 12 ▶ return undef unless ( $block->{type} // '' ) eq 'tool_use';
Branch: 1 branch
BranchTrueFalseTotal
unless ($block->{"type"} // "") eq "tool_use" 9 3 12
Condition50%1/2
$block->{"type"} // ""
l!l
120
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table50%1/2
A
A= $block->{"type"}
Aresult
11
00
67 3 ▶ my $name = $block->{name} // '';
Condition50%1/2
$block->{"name"} // ""
l!l
30
MC/DC0%0/1
$block->{"name"} // ""
$block->{"name"}
Truth table50%1/2
A
A= $block->{"name"}
Aresult
11
00
68 3 ▶ return undef unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 3 3
69 return $class->new(
70 name => $name,
71 arguments => ( ref( $block->{input} ) eq 'HASH' ? $block->{input} : {} ),
72 3 ▶ id => ( $block->{id} // '' ),
Branch: 1 branch
BranchTrueFalseTotal
ref $block->{'input'} eq 'HASH' ? : 3 0 3
Condition50%1/2
$block->{"id"} // ""
l!l
30
MC/DC0%0/1
$block->{"id"} // ""
$block->{"id"}
Truth table50%1/2
A
A= $block->{"id"}
Aresult
11
00
73 );
74 }
75  
76 sub from_ollama {
77 0 ▶ my ($class, $hash) = @_;
Subroutine from_ollama: not called CC=7 SCAR=40.3
Pod undocumented
78 0 ▶ return undef unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 0 0
79 0 ▶ my $fn = $hash->{function} || {};
Condition0%0/2
$hash->{'function'} || {}
l!l
00
MC/DC0%0/1
$hash->{'function'} || {}
$hash->{'function'}
Truth table0%0/2
A
A= $hash->{'function'}
Aresult
11
00
80 0 ▶ return undef unless ref($fn) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fn eq "HASH" 0 0 0
81 0 ▶ my $name = $fn->{name} // '';
Condition0%0/2
$fn->{"name"} // ""
l!l
00
MC/DC0%0/1
$fn->{"name"} // ""
$fn->{"name"}
Truth table0%0/2
A
A= $fn->{"name"}
Aresult
11
00
82 0 ▶ return undef unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 0 0
83 return $class->new(
84 name => $name,
85 arguments => _decode_args( $fn->{arguments} ),
86 0 ▶ id => ( $hash->{id} // '' ),
Condition0%0/2
$hash->{"id"} // ""
l!l
00
MC/DC0%0/1
$hash->{"id"} // ""
$hash->{"id"}
Truth table0%0/2
A
A= $hash->{"id"}
Aresult
11
00
87 );
88 }
89  
90 # Gemini: a single functionCall part inside candidates[0].content.parts[]:
91 # { "functionCall": { "name": "x", "args": { ... } } }
92 sub from_gemini {
93 4 ▶ my ($class, $part) = @_;
Subroutine from_gemini: called CC=7 SCAR=31.2
Pod undocumented
94 4 ▶ return undef unless ref($part) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $part eq "HASH" 0 4 4
95 4 my $fc = $part->{functionCall};
96 4 ▶ return undef unless ref($fc) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fc eq "HASH" 4 0 4
97 0 ▶ my $name = $fc->{name} // '';
Condition0%0/2
$fc->{"name"} // ""
l!l
00
MC/DC0%0/1
$fc->{"name"} // ""
$fc->{"name"}
Truth table0%0/2
A
A= $fc->{"name"}
Aresult
11
00
98 0 ▶ return undef unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 0 0
99 return $class->new(
100 name => $name,
101 arguments => ( ref( $fc->{args} ) eq 'HASH' ? $fc->{args} : {} ),
102 0 ▶ id => ( $fc->{id} // '' ),
Branch: 1 branch
BranchTrueFalseTotal
ref $fc->{'args'} eq 'HASH' ? : 0 0 0
Condition0%0/2
$fc->{"id"} // ""
l!l
00
MC/DC0%0/1
$fc->{"id"} // ""
$fc->{"id"}
Truth table0%0/2
A
A= $fc->{"id"}
Aresult
11
00
103 );
104 }
105  
106 # OpenAI Responses API: function_call appears either as a top-level output[]
107 # item or nested inside output[type=message].content[]. Both shapes look like:
108 # { "type": "function_call", "call_id": "call_abc", "name": "foo", "arguments": "{...}" }
109 sub from_responses {
110 2 ▶ my ($class, $block) = @_;
Subroutine from_responses: called CC=8 SCAR=23.1
Pod undocumented
111 2 ▶ return undef unless ref($block) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $block eq "HASH" 0 2 2
112 2 ▶ return undef unless ( $block->{type} // '' ) eq 'function_call';
Branch: 1 branch
BranchTrueFalseTotal
unless ($block->{"type"} // "") eq "function_call" 0 2 2
Condition50%1/2
$block->{"type"} // ""
l!l
20
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table50%1/2
A
A= $block->{"type"}
Aresult
11
00
113 2 ▶ my $name = $block->{name} // '';
Condition50%1/2
$block->{"name"} // ""
l!l
20
MC/DC0%0/1
$block->{"name"} // ""
$block->{"name"}
Truth table50%1/2
A
A= $block->{"name"}
Aresult
11
00
114 2 ▶ return undef unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 2 2
115 2 my $args = $block->{arguments};
116 2 $args = _decode_args($args);
117 return $class->new(
118 name => $name,
119 arguments => ( ref($args) eq 'HASH' ? $args : {} ),
120 2 ▶ id => ( $block->{call_id} // '' ),
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'HASH' ? : 2 0 2
Condition50%1/2
$block->{"call_id"} // ""
l!l
20
MC/DC0%0/1
$block->{"call_id"} // ""
$block->{"call_id"}
Truth table50%1/2
A
A= $block->{"call_id"}
Aresult
11
00
121 );
122 }
123  
124 # Pull every tool call out of an upstream response, in any of the formats
125 # we know about. Returns a list of ToolCall objects (possibly empty).
126 sub extract {
127 31 ▶ my ($class, $raw) = @_;
Subroutine extract: called CC=22 SCAR=32.3
Pod undocumented
128 31 ▶ return () unless ref($raw) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $raw eq "HASH" 0 31 31
129  
130 # OpenAI shape: choices[0].message.tool_calls
131 31 ▶ if ( my $oai_msg = $raw->{choices}[0]{message} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $oai_msg = $raw->{"choices"}[0]{"message"}) 18 13 31
132 18 ▶ if ( ref( $oai_msg->{tool_calls} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $oai_msg->{"tool_calls"} eq "ARRAY") 3 15 18
133 3 return grep { defined } map { $class->from_openai($_) } @{ $oai_msg->{tool_calls} };
134 }
135 }
136  
137 # Ollama shape: message.tool_calls
138 28 ▶ if ( my $msg = $raw->{message} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $raw->{"message"}) 2 26 28
139 2 ▶ if ( ref( $msg->{tool_calls} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msg->{"tool_calls"} eq "ARRAY") 0 2 2
140 0 return grep { defined } map { $class->from_ollama($_) } @{ $msg->{tool_calls} };
141 }
142 }
143  
144 # Anthropic shape: content[*] where type=tool_use
145 28 ▶ if ( ref( $raw->{content} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $raw->{"content"} eq "ARRAY") 6 22 28
146 6 return grep { defined } map { $class->from_anthropic($_) } @{ $raw->{content} };
147 }
148  
149 # Gemini shape: candidates[0].content.parts[*].functionCall
150 22 ▶ if ( ref( $raw->{candidates} ) eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $raw->{"candidates"} eq "ARRAY" and ref $raw->{"candidates"}[0]{"content"}{"parts"} eq "ARRAY") 3 19 22
Condition66%2/3
ref $raw->{"candidates"} eq "ARRAY" and ref $raw->{"candidates"}[0]{"content"}{"parts"} eq "ARRAY"
!ll&&!rl&&r
1903
MC/DC50%1/2
ref $raw->{"candidates"} eq "ARRAY" and ref $raw->{"candidates"}[0]{"content"}{"parts"} eq "ARRAY"
ref $raw->{"candidates"} eq "ARRAY" ref $raw->{"candidates"}[0]{"content"}{"parts"} eq "ARRAY"
Truth table66%2/3
A and B
A= ref $raw->{"candidates"} eq "ARRAY"B= ref $raw->{"candidates"}[0]{"content"}{"parts"} eq "ARRAY"
ABresult
0X0
100
111
151 && ref( $raw->{candidates}[0]{content}{parts} ) eq 'ARRAY' ) {
152 4 return grep { defined }
153 4 map { $class->from_gemini($_) }
154 3 @{ $raw->{candidates}[0]{content}{parts} };
155 }
156  
157 # OpenAI Responses shape: function_call appears either as a top-level
158 # output[] item (real API) or nested under output[type=message].content[]
159 # (older / streaming shape). Walk both.
160 19 ▶ if ( ref( $raw->{output} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $raw->{"output"} eq "ARRAY") 2 17 19
161 2 my @calls;
162 2 for my $item (@{$raw->{output}}) {
163 3 ▶ next unless ref($item) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item eq "HASH" 0 3 3
164 3 ▶ my $type = $item->{type} // '';
Condition50%1/2
$item->{"type"} // ""
l!l
30
MC/DC0%0/1
$item->{"type"} // ""
$item->{"type"}
Truth table50%1/2
A
A= $item->{"type"}
Aresult
11
00
165 3 ▶ if ( $type eq 'function_call' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'function_call') { } 1 2 3
elsif ($type eq 'message') { } 1 1 2
166 1 ▶ if ( my $tc = $class->from_responses($item) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tc = $class->from_responses($item)) 1 0 1
167 1 push @calls, $tc;
168 }
169 }
170 elsif ( $type eq 'message' ) {
171 1 ▶ for my $block (@{$item->{content} // []}) {
Condition50%1/2
$item->{"content"} // []
l!l
10
MC/DC0%0/1
$item->{"content"} // []
$item->{"content"}
Truth table50%1/2
A
A= $item->{"content"}
Aresult
11
00
172 1 ▶ if (($block->{type} // '') eq 'function_call') {
Branch: 1 branch
BranchTrueFalseTotal
if (($block->{"type"} // "") eq "function_call") 1 0 1
Condition50%1/2
$block->{"type"} // ""
l!l
10
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table50%1/2
A
A= $block->{"type"}
Aresult
11
00
173 1 ▶ if (my $tc = $class->from_responses($block)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tc = $class->from_responses($block)) 1 0 1
174 1 push @calls, $tc;
175 }
176 }
177 }
178 }
179 }
180 2 ▶ return @calls if @calls;
Branch: 1 branch
BranchTrueFalseTotal
if @calls 2 0 2
181 }
182  
183 17 return ();
184 }
185  
186 # Hermes-style XML embedded in plain text. Returns ($cleaned_text, \@calls).
187 sub extract_hermes_from_text {
188 2 ▶ my ($class, $text) = @_;
Subroutine extract_hermes_from_text: called CC=7 SCAR=20.3
Pod undocumented
189 2 ▶ my $clean = defined($text) ? $text : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 2 0 2
190 2 my @calls;
191 2 ▶ while ( $clean =~ m{<tool_call>\s*(.*?)\s*</tool_call>}sg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean =~ m[<tool_call>\s*(.*?)\s*</tool_call>]gs) 2 2 4
192 2 my $json = $1;
193 2 my $obj = eval { decode_json($json) };
194 2 ▶ next unless ref($obj) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj eq "HASH" 0 2 2
195 2 ▶ next unless defined $obj->{name} && length $obj->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj->{"name"} and length $obj->{"name"} 0 2 2
Condition33%1/3
defined $obj->{"name"} and length $obj->{"name"}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $obj->{"name"} and length $obj->{"name"}
defined $obj->{"name"} length $obj->{"name"}
Truth table33%1/3
A and B
A= defined $obj->{"name"}B= length $obj->{"name"}
ABresult
0X0
100
111
196 push @calls, $class->new(
197 name => $obj->{name},
198 2 ▶ arguments => ( ref( $obj->{arguments} ) eq 'HASH' ? $obj->{arguments} : {} ),
Branch: 1 branch
BranchTrueFalseTotal
ref $obj->{'arguments'} eq 'HASH' ? : 2 0 2
199 );
200 }
201 2 $clean =~ s{<tool_call>.*?</tool_call>}{}sg;
202 2 $clean =~ s/^\s+|\s+$//g;
203 2 return ( $clean, \@calls );
204 }
205  
206 # --- Serializers to wire-format hashes ---
207  
208 sub to_openai {
209 3 ▶ my ($self, %opts) = @_;
Subroutine to_openai: called CC=3 SCAR=11.1
Pod undocumented
210 3 ▶ my $id = length( $self->id ) ? $self->id : ( $opts{fallback_id} // 'call_langertha' );
Branch: 1 branch
BranchTrueFalseTotal
length $self->id ? : 2 1 3
Condition50%1/2
$opts{"fallback_id"} // "call_langertha"
l!l
10
MC/DC0%0/1
$opts{"fallback_id"} // "call_langertha"
$opts{"fallback_id"}
Truth table50%1/2
A
A= $opts{"fallback_id"}
Aresult
11
00
211 return {
212 3 id => $id,
213 type => 'function',
214 function => {
215 name => $self->name,
216 arguments => encode_json( $self->arguments ),
217 },
218 };
219 }
220  
221 sub to_anthropic_block {
222 2 ▶ my ($self, %opts) = @_;
Subroutine to_anthropic_block: called CC=3 SCAR=13.1
Pod undocumented
223 2 ▶ my $id = length( $self->id ) ? $self->id : ( $opts{fallback_id} // 'toolu_langertha' );
Branch: 1 branch
BranchTrueFalseTotal
length $self->id ? : 2 0 2
Condition0%0/2
$opts{"fallback_id"} // "toolu_langertha"
l!l
00
MC/DC0%0/1
$opts{"fallback_id"} // "toolu_langertha"
$opts{"fallback_id"}
Truth table0%0/2
A
A= $opts{"fallback_id"}
Aresult
11
00
224 return {
225 2 type => 'tool_use',
226 id => $id,
227 name => $self->name,
228 input => $self->arguments,
229 };
230 }
231  
232 sub to_ollama {
233 2 ▶ my ($self) = @_;
Subroutine to_ollama: called CC=2 SCAR=7.2
Pod undocumented
234 return {
235 2 ▶ function => {
Branch: 1 branch
BranchTrueFalseTotal
length $self->id ? : 2 0 2
236 name => $self->name,
237 arguments => $self->arguments,
238 },
239 ( length( $self->id ) ? ( id => $self->id ) : () ),
240 };
241 }
242  
243 sub to_hash {
244 3 ▶ my ($self) = @_;
Subroutine to_hash: called CC=1 SCAR=0.0
Pod undocumented
245 return {
246 3 id => $self->id,
247 name => $self->name,
248 arguments => $self->arguments,
249 };
250 }
251  
252 39 __PACKAGE__->meta->make_immutable;
253 39 1;
254  
255 __END__
256  
257 =pod
258  
259 =encoding UTF-8
260  
261 =head1 NAME
262  
263 Langertha::ToolCall - Immutable canonical tool invocation emitted by an LLM
264  
265 =head1 VERSION
266  
267 version 0.502
268  
269 =head2 synthetic
270  
271 Boolean. True when the tool call was synthesized by Langertha — for
272 example when L<Langertha::Role::Chat/chat_f> rewrote a forced named
273 tool into a C<response_format> JSON Schema request and parsed the
274 output back into a C<ToolCall>. False (the default) for native model
275 output.
276  
277 =head1 SUPPORT
278  
279 =head2 Issues
280  
281 Please report bugs and feature requests on GitHub at
282 L<https://github.com/Getty/langertha/issues>.
283  
284 =head2 IRC
285  
286 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
287  
288 =head1 CONTRIBUTING
289  
290 Contributions are welcome! Please fork the repository and submit a pull request.
291  
292 =head1 AUTHOR
293  
294 Torsten Raudssus <getty@cpan.org>
295  
296 =head1 COPYRIGHT AND LICENSE
297  
298 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
299  
300 This is free software; you can redistribute it and/or modify it under
301 the same terms as the Perl 5 programming language system itself.
302  
303 =cut
« Langertha/Tool.pm ↑ Summary Langertha/Chat.pm »