blib/lib/Langertha/ToolCall.pm

filter: Statementstmt 54.9% 151 / 275 Branchbran 39.4% 71 / 180 Conditioncond 33.8% 46 / 136 MC/DCmcdc 18.5% 13 / 70 Subroutinesub 60.0% 27 / 45 Podpod 25.8% 8 / 31 totaltotal 42.8% 316 / 737 CC 159 SCAR 37.7
CC
159
Cov
45%
locate
140.0
locate
140.0
from_ollama
19.3
CRAP 43.4
« blib/lib/Langertha/Output/Tools.pm ↑ Summary blib/lib/Langertha/Role/ResponseFormat.pm »
1 package Langertha::ToolCall;
2 # ABSTRACT: Immutable canonical tool invocation emitted by an LLM
3 205 our $VERSION = '0.505';
4 205 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 205 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 205 ▶use Encode qw( encode_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 205 ▶use JSON::MaybeXS qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Character-string codec for JSON nested inside a JSON body or model text
10 # (function.arguments, the hermes <tool_call> payload). The transport encodes
11 # the whole body to UTF-8 once; a byte string here would be encoded twice
12 # ("Köln" -> "Köln"). -- karr k252, ADR 0010
13 205 my $TEXT_JSON = JSON::MaybeXS->new( utf8 => 0, canonical => 1 );
14  
15 205 has name => (
16 is => 'ro',
17 isa => 'Str',
18 required => 1,
19 );
20  
21 has arguments => (
22 is => 'ro',
23 isa => 'HashRef',
24 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 205 );
26  
27 # Provider-specific call id (may be empty if the upstream didn't supply one).
28 205 has id => (
29 is => 'ro',
30 isa => 'Str',
31 default => '',
32 );
33  
34 # True when this call was synthesized by Langertha (e.g. forced-tool
35 # rewrite via response_format on engines without native named-tool
36 # forcing) rather than emitted directly by the model. Useful for
37 # callers that want to distinguish "the model decided to call this"
38 # from "we asked it to and parsed the result back into a tool_call".
39 205 has synthetic => (
40 is => 'ro',
41 isa => 'Bool',
42 default => 0,
43 );
44  
45  
46 # True when the wire sent arguments (a string or a non-object) that do not
47 # decode to an object -- a JSON string cut off by max_tokens, for one. The
48 # arguments are {} then, and the tool loops must not run the call on them
49 # (karr k324).
50 205 has arguments_undecodable => (
51 is => 'ro',
52 isa => 'Bool',
53 default => 0,
54 );
55  
56  
57 # Why the arguments did not decode: the JSON parser's message, or "not a JSON
58 # object". Set exactly when arguments_undecodable is (karr k345).
59 205 has arguments_error => (
60 is => 'ro',
61 isa => 'Str',
62 predicate => 'has_arguments_error',
63 );
64  
65  
66 # The constructor arguments for one raw arguments value: the decoded object
67 # ({} when there is none) plus arguments_undecodable when the value does not
68 # decode to an object. Argument strings reach us as Perl-Unicode (pulled out of
69 # an already-decoded response tree), so UTF-8-encode before the utf8 JSON
70 # decoder — same convention as Role::JSON's decode_json_text.
71 487 ▶sub _args_kwargs {
Subroutine _args_kwargs: called CC=7 SCAR=25.7
72 0 ▶ my ($args) = @_;
Subroutine _args_kwargs: not called CC=7 SCAR=25.7
73 0 ▶ return ( arguments => {} ) unless defined $args;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $args 1 486 487
unless defined $args 0 0 0
74 0 ▶ return ( arguments => $args ) if ref($args) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
if ref $args eq "HASH" 235 251 486
if ref $args eq "HASH" 0 0 0
75 0 ▶ return _undecodable('not a JSON object') if ref $args;
Branch: 2 branches
BranchTrueFalseTotal
if ref $args 10 241 251
if ref $args 0 0 0
76 0 ▶ return ( arguments => {} ) unless length $args;
Branch: 2 branches
BranchTrueFalseTotal
unless length $args 1 240 241
unless length $args 0 0 0
77 0 my $decoded = eval { decode_json( encode_utf8($args) ) };
78 0 ▶ return ( arguments => $decoded ) if ref($decoded) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
if ref $decoded eq "HASH" 190 50 240
if ref $decoded eq "HASH" 0 0 0
79 0 my $error = $@;
80 0 ▶ return _undecodable('not a JSON object') unless $error;
Branch: 2 branches
BranchTrueFalseTotal
unless $error 1 49 50
unless $error 0 0 0
81 0 $error =~ s/ at \S+ line \d+\.?\n?\z//;
82 0 return _undecodable($error);
83 }
84  
85 60 ▶sub _undecodable {
Subroutine _undecodable: called CC=1 SCAR=1.2
86 0 ▶ my ($error) = @_;
Subroutine _undecodable: not called CC=1 SCAR=1.2
87 0 return ( arguments => {}, arguments_undecodable => 1, arguments_error => $error );
88 }
89  
90 # --- Constructors from wire-format hashes ---
91  
92 168 ▶sub from_openai {
Subroutine from_openai: called CC=7 SCAR=28.8
Pod undocumented
93 0 ▶ my ($class, $hash) = @_;
Subroutine from_openai: not called CC=7 SCAR=28.8
Pod undocumented
94 0 ▶ return undef unless ref($hash) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 168 168
unless ref $hash eq "HASH" 0 0 0
95 0 ▶ my $fn = $hash->{function} || {};
Condition25%1/4
$hash->{'function'} || {}
l!l
1680
$hash->{'function'} || {}
l!l
00
MC/DC0%0/1
$hash->{'function'} || {}
$hash->{'function'}
MC/DC0%0/1
$hash->{'function'} || {}
$hash->{'function'}
Truth table50%1/2
A
A= $hash->{'function'}
Aresult
11
00
Truth table0%0/2
A
A= $hash->{'function'}
Aresult
11
00
96 0 ▶ return undef unless ref($fn) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $fn eq "HASH" 0 168 168
unless ref $fn eq "HASH" 0 0 0
97 0 ▶ my $name = $fn->{name} // '';
Condition50%2/4
$fn->{"name"} // ""
l!l
1662
$fn->{"name"} // ""
l!l
00
MC/DC100%1/1
$fn->{"name"} // ""
$fn->{"name"}
MC/DC0%0/1
$fn->{"name"} // ""
$fn->{"name"}
Truth table100%2/2
A
A= $fn->{"name"}
Aresult
11
00
Truth table0%0/2
A
A= $fn->{"name"}
Aresult
11
00
98 0 ▶ return undef unless length $name;
Branch: 2 branches
BranchTrueFalseTotal
unless length $name 2 166 168
unless length $name 0 0 0
99 return $class->new(
100 name => $name,
101 _args_kwargs( $fn->{arguments} ),
102 0 ▶ id => ( $hash->{id} // '' ),
Condition25%1/4
$hash->{"id"} // ""
l!l
1660
$hash->{"id"} // ""
l!l
00
MC/DC0%0/1
$hash->{"id"} // ""
$hash->{"id"}
MC/DC0%0/1
$hash->{"id"} // ""
$hash->{"id"}
Truth table50%1/2
A
A= $hash->{"id"}
Aresult
11
00
Truth table0%0/2
A
A= $hash->{"id"}
Aresult
11
00
103 );
104 }
105  
106 51 ▶sub from_anthropic {
Subroutine from_anthropic: called CC=7 SCAR=27.3
Pod undocumented
107 0 ▶ my ($class, $block) = @_;
Subroutine from_anthropic: not called CC=7 SCAR=27.3
Pod undocumented
108 0 ▶ return undef unless ref($block) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $block eq "HASH" 0 51 51
unless ref $block eq "HASH" 0 0 0
109 0 ▶ return undef unless ( $block->{type} // '' ) eq 'tool_use';
Branch: 2 branches
BranchTrueFalseTotal
unless ($block->{"type"} // "") eq "tool_use" 1 50 51
unless ($block->{"type"} // "") eq "tool_use" 0 0 0
Condition50%2/4
$block->{"type"} // ""
l!l
501
$block->{"type"} // ""
l!l
00
MC/DC100%1/1
$block->{"type"} // ""
$block->{"type"}
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table100%2/2
A
A= $block->{"type"}
Aresult
11
00
Truth table0%0/2
A
A= $block->{"type"}
Aresult
11
00
110 0 ▶ my $name = $block->{name} // '';
Condition50%2/4
$block->{"name"} // ""
l!l
482
$block->{"name"} // ""
l!l
00
MC/DC100%1/1
$block->{"name"} // ""
$block->{"name"}
MC/DC0%0/1
$block->{"name"} // ""
$block->{"name"}
Truth table100%2/2
A
A= $block->{"name"}
Aresult
11
00
Truth table0%0/2
A
A= $block->{"name"}
Aresult
11
00
111 0 ▶ return undef unless length $name;
Branch: 2 branches
BranchTrueFalseTotal
unless length $name 2 48 50
unless length $name 0 0 0
112 # Real Anthropic ships input as an object, but the AKI.IO /anthropic shim
113 # ships it as a JSON string (like the OpenAI wire). Route input through the
114 # same decoder from_openai uses so a stringified object is decoded rather
115 # than silently dropped to {}. -- karr k124
116 return $class->new(
117 name => $name,
118 _args_kwargs( $block->{input} ),
119 0 ▶ id => ( $block->{id} // '' ),
Condition25%1/4
$block->{"id"} // ""
l!l
480
$block->{"id"} // ""
l!l
00
MC/DC0%0/1
$block->{"id"} // ""
$block->{"id"}
MC/DC0%0/1
$block->{"id"} // ""
$block->{"id"}
Truth table50%1/2
A
A= $block->{"id"}
Aresult
11
00
Truth table0%0/2
A
A= $block->{"id"}
Aresult
11
00
120 );
121 }
122  
123 12 ▶sub from_ollama {
Subroutine from_ollama: called CC=7 SCAR=29.6
Pod undocumented
124 0 ▶ my ($class, $hash) = @_;
Subroutine from_ollama: not called CC=7 SCAR=29.6
Pod undocumented
125 0 ▶ return undef unless ref($hash) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 12 12
unless ref $hash eq "HASH" 0 0 0
126 0 ▶ my $fn = $hash->{function} || {};
Condition25%1/4
$hash->{'function'} || {}
l!l
120
$hash->{'function'} || {}
l!l
00
MC/DC0%0/1
$hash->{'function'} || {}
$hash->{'function'}
MC/DC0%0/1
$hash->{'function'} || {}
$hash->{'function'}
Truth table50%1/2
A
A= $hash->{'function'}
Aresult
11
00
Truth table0%0/2
A
A= $hash->{'function'}
Aresult
11
00
127 0 ▶ return undef unless ref($fn) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $fn eq "HASH" 0 12 12
unless ref $fn eq "HASH" 0 0 0
128 0 ▶ my $name = $fn->{name} // '';
Condition25%1/4
$fn->{"name"} // ""
l!l
120
$fn->{"name"} // ""
l!l
00
MC/DC0%0/1
$fn->{"name"} // ""
$fn->{"name"}
MC/DC0%0/1
$fn->{"name"} // ""
$fn->{"name"}
Truth table50%1/2
A
A= $fn->{"name"}
Aresult
11
00
Truth table0%0/2
A
A= $fn->{"name"}
Aresult
11
00
129 0 ▶ return undef unless length $name;
Branch: 2 branches
BranchTrueFalseTotal
unless length $name 0 12 12
unless length $name 0 0 0
130 return $class->new(
131 name => $name,
132 _args_kwargs( $fn->{arguments} ),
133 0 ▶ id => ( $hash->{id} // '' ),
Condition50%2/4
$hash->{"id"} // ""
l!l
48
$hash->{"id"} // ""
l!l
00
MC/DC100%1/1
$hash->{"id"} // ""
$hash->{"id"}
MC/DC0%0/1
$hash->{"id"} // ""
$hash->{"id"}
Truth table100%2/2
A
A= $hash->{"id"}
Aresult
11
00
Truth table0%0/2
A
A= $hash->{"id"}
Aresult
11
00
134 );
135 }
136  
137 # Gemini: a single functionCall part inside candidates[0].content.parts[]:
138 # { "functionCall": { "name": "x", "args": { ... } } }
139 26 ▶sub from_gemini {
Subroutine from_gemini: called CC=6 SCAR=27.6
Pod undocumented
140 0 ▶ my ($class, $part) = @_;
Subroutine from_gemini: not called CC=6 SCAR=27.6
Pod undocumented
141 0 ▶ return undef unless ref($part) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $part eq "HASH" 0 26 26
unless ref $part eq "HASH" 0 0 0
142 0 my $fc = $part->{functionCall};
143 0 ▶ return undef unless ref($fc) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $fc eq "HASH" 0 26 26
unless ref $fc eq "HASH" 0 0 0
144 0 ▶ my $name = $fc->{name} // '';
Condition25%1/4
$fc->{"name"} // ""
l!l
260
$fc->{"name"} // ""
l!l
00
MC/DC0%0/1
$fc->{"name"} // ""
$fc->{"name"}
MC/DC0%0/1
$fc->{"name"} // ""
$fc->{"name"}
Truth table50%1/2
A
A= $fc->{"name"}
Aresult
11
00
Truth table0%0/2
A
A= $fc->{"name"}
Aresult
11
00
145 0 ▶ return undef unless length $name;
Branch: 2 branches
BranchTrueFalseTotal
unless length $name 0 26 26
unless length $name 0 0 0
146 # Google-native ships args as an object, but Vertex-style proxies / OpenRouter
147 # / LM Studio can ship it as a JSON string. Route args through the same decoder
148 # the other constructors use so a stringified object is decoded rather than
149 # silently dropped to {}. -- karr k131 (symmetric to k124's from_anthropic fix)
150 return $class->new(
151 name => $name,
152 _args_kwargs( $fc->{args} ),
153 0 ▶ id => ( $fc->{id} // '' ),
Condition25%1/4
$fc->{"id"} // ""
l!l
026
$fc->{"id"} // ""
l!l
00
MC/DC0%0/1
$fc->{"id"} // ""
$fc->{"id"}
MC/DC0%0/1
$fc->{"id"} // ""
$fc->{"id"}
Truth table50%1/2
A
A= $fc->{"id"}
Aresult
11
00
Truth table0%0/2
A
A= $fc->{"id"}
Aresult
11
00
154 );
155 }
156  
157 # OpenAI Responses API: function_call appears either as a top-level output[]
158 # item or nested inside output[type=message].content[]. Both shapes look like:
159 # { "type": "function_call", "call_id": "call_abc", "name": "foo", "arguments": "{...}" }
160 48 ▶sub from_responses {
Subroutine from_responses: called CC=7 SCAR=29.4
Pod undocumented
161 0 ▶ my ($class, $block) = @_;
Subroutine from_responses: not called CC=7 SCAR=29.4
Pod undocumented
162 0 ▶ return undef unless ref($block) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $block eq "HASH" 0 48 48
unless ref $block eq "HASH" 0 0 0
163 # locate('responses') pre-filters output[] items to function_call, and a
164 # located call passed to from_fmt may carry no type at all — only reject a
165 # block whose type is present AND wrong.
166 0 my $type = $block->{type};
167 0 ▶ return undef if defined $type && $type ne 'function_call';
Branch: 2 branches
BranchTrueFalseTotal
if defined $type and $type ne "function_call" 0 48 48
if defined $type and $type ne "function_call" 0 0 0
Condition33%2/6
defined $type and $type ne "function_call"
!ll&&!rl&&r
2460
defined $type and $type ne "function_call"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type and $type ne "function_call"
defined $type $type ne "function_call"
MC/DC0%0/2
defined $type and $type ne "function_call"
defined $type $type ne "function_call"
Truth table66%2/3
A and B
A= defined $typeB= $type ne "function_call"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $typeB= $type ne "function_call"
ABresult
0X0
100
111
168 0 ▶ my $name = $block->{name} // '';
Condition25%1/4
$block->{"name"} // ""
l!l
480
$block->{"name"} // ""
l!l
00
MC/DC0%0/1
$block->{"name"} // ""
$block->{"name"}
MC/DC0%0/1
$block->{"name"} // ""
$block->{"name"}
Truth table50%1/2
A
A= $block->{"name"}
Aresult
11
00
Truth table0%0/2
A
A= $block->{"name"}
Aresult
11
00
169 0 ▶ return undef unless length $name;
Branch: 2 branches
BranchTrueFalseTotal
unless length $name 0 48 48
unless length $name 0 0 0
170 return $class->new(
171 name => $name,
172 _args_kwargs( $block->{arguments} ),
173 0 ▶ id => ( $block->{call_id} // '' ),
Condition50%2/4
$block->{"call_id"} // ""
l!l
462
$block->{"call_id"} // ""
l!l
00
MC/DC100%1/1
$block->{"call_id"} // ""
$block->{"call_id"}
MC/DC0%0/1
$block->{"call_id"} // ""
$block->{"call_id"}
Truth table100%2/2
A
A= $block->{"call_id"}
Aresult
11
00
Truth table0%0/2
A
A= $block->{"call_id"}
Aresult
11
00
174 );
175 }
176  
177 # Maps a tool_wire_format tag to the per-call constructor.
178 205 my %FROM_METHOD = (
179 openai => 'from_openai',
180 anthropic => 'from_anthropic',
181 gemini => 'from_gemini',
182 ollama => 'from_ollama',
183 responses => 'from_responses',
184 );
185  
186 # Construct a single ToolCall from one raw wire-format call hash, pinned to a
187 # format (no shape sniffing). Returns undef if the hash doesn't parse.
188 254 ▶sub from_fmt {
Subroutine from_fmt: called CC=4 SCAR=20.3
Pod undocumented
189 0 ▶ my ($class, $fmt, $hash) = @_;
Subroutine from_fmt: not called CC=4 SCAR=20.3
Pod undocumented
190 0 ▶ my $method = $FROM_METHOD{ $fmt // '' }
Branch: 2 branches
BranchTrueFalseTotal
my $method = $FROM_METHOD{$fmt // ""} or croak(q[Langertha::ToolCall: unknown wire format '] . ($fmt // "") . q[']) 253 1 254
my $method = $FROM_METHOD{$fmt // ""} or croak(q[Langertha::ToolCall: unknown wire format '] . ($fmt // "") . q[']) 0 0 0
Condition25%2/8
$fmt // ""
l!l
2540
$fmt // ""
l!l
10
$fmt // ""
l!l
00
$fmt // ""
l!l
00
MC/DC0%0/1
$fmt // ""
$fmt
MC/DC0%0/1
$fmt // ""
$fmt
MC/DC0%0/1
$fmt // ""
$fmt
MC/DC0%0/1
$fmt // ""
$fmt
Truth table50%1/2
A
A= $fmt
Aresult
11
00
Truth table50%1/2
A
A= $fmt
Aresult
11
00
Truth table0%0/2
A
A= $fmt
Aresult
11
00
Truth table0%0/2
A
A= $fmt
Aresult
11
00
191 or croak "Langertha::ToolCall: unknown wire format '" . ( $fmt // '' ) . "'";
192 0 return $class->$method($hash);
193 }
194  
195 # Locate the raw tool-call structures inside an upstream response for a given
196 # format, WITHOUT parsing them into objects. Returns an arrayref of raw hashes
197 # (possibly empty). This is the per-format locator that engines used to carry
198 # as response_tool_calls.
199 827 ▶sub locate {
Subroutine locate: called CC=25 SCAR=49.4
Pod documented
200 0 ▶ my ($class, $fmt, $data) = @_;
Subroutine locate: not called CC=25 SCAR=49.4
Pod documented
201 0 ▶ $fmt //= '';
Condition25%1/4
$fmt //= ""
l!l
8270
$fmt //= ""
l!l
00
MC/DC0%0/1
$fmt //= ""
$fmt
MC/DC0%0/1
$fmt //= ""
$fmt
Truth table50%1/2
A
A= $fmt
Aresult
11
00
Truth table0%0/2
A
A= $fmt
Aresult
11
00
202 0 ▶ return [] unless ref($data) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $data eq "HASH" 1 826 827
unless ref $data eq "HASH" 0 0 0
203  
204 0 ▶ if ( $fmt eq 'openai' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fmt eq "openai") 524 302 826
if ($fmt eq "openai") 0 0 0
205 0 ▶ my $msg = $data->{choices}[0]{message} or return [];
Branch: 2 branches
BranchTrueFalseTotal
my $msg = $data->{"choices"}[0]{"message"} or return [] 522 2 524
my $msg = $data->{"choices"}[0]{"message"} or return [] 0 0 0
206 0 ▶ return $msg->{tool_calls} // [];
Condition50%2/4
$msg->{"tool_calls"} // []
l!l
117405
$msg->{"tool_calls"} // []
l!l
00
MC/DC100%1/1
$msg->{"tool_calls"} // []
$msg->{"tool_calls"}
MC/DC0%0/1
$msg->{"tool_calls"} // []
$msg->{"tool_calls"}
Truth table100%2/2
A
A= $msg->{"tool_calls"}
Aresult
11
00
Truth table0%0/2
A
A= $msg->{"tool_calls"}
Aresult
11
00
207 }
208 0 ▶ if ( $fmt eq 'ollama' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fmt eq "ollama") 76 226 302
if ($fmt eq "ollama") 0 0 0
209 0 ▶ my $msg = $data->{message} or return [];
Branch: 2 branches
BranchTrueFalseTotal
my $msg = $data->{"message"} or return [] 76 0 76
my $msg = $data->{"message"} or return [] 0 0 0
210 0 ▶ return $msg->{tool_calls} // [];
Condition50%2/4
$msg->{"tool_calls"} // []
l!l
1264
$msg->{"tool_calls"} // []
l!l
00
MC/DC100%1/1
$msg->{"tool_calls"} // []
$msg->{"tool_calls"}
MC/DC0%0/1
$msg->{"tool_calls"} // []
$msg->{"tool_calls"}
Truth table100%2/2
A
A= $msg->{"tool_calls"}
Aresult
11
00
Truth table0%0/2
A
A= $msg->{"tool_calls"}
Aresult
11
00
211 }
212 0 ▶ if ( $fmt eq 'anthropic' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fmt eq "anthropic") 98 128 226
if ($fmt eq "anthropic") 0 0 0
213 0 ▶ return [ grep { ( $_->{type} // '' ) eq 'tool_use' } @{ $data->{content} // [] } ];
Condition37%3/8
$_->{"type"} // ""
l!l
1310
$data->{"content"} // []
l!l
971
$_->{"type"} // ""
l!l
00
$data->{"content"} // []
l!l
00
MC/DC0%0/1
$_->{"type"} // ""
$_->{"type"}
MC/DC100%1/1
$data->{"content"} // []
$data->{"content"}
MC/DC0%0/1
$_->{"type"} // ""
$_->{"type"}
MC/DC0%0/1
$data->{"content"} // []
$data->{"content"}
Truth table50%1/2
A
A= $_->{"type"}
Aresult
11
00
Truth table100%2/2
A
A= $data->{"content"}
Aresult
11
00
Truth table0%0/2
A
A= $_->{"type"}
Aresult
11
00
Truth table0%0/2
A
A= $data->{"content"}
Aresult
11
00
214 }
215 0 ▶ if ( $fmt eq 'gemini' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fmt eq "gemini") 113 15 128
if ($fmt eq "gemini") 0 0 0
216 0 ▶ my $candidates = $data->{candidates} || [];
Condition50%2/4
$data->{'candidates'} || []
l!l
1049
$data->{'candidates'} || []
l!l
00
MC/DC100%1/1
$data->{'candidates'} || []
$data->{'candidates'}
MC/DC0%0/1
$data->{'candidates'} || []
$data->{'candidates'}
Truth table100%2/2
A
A= $data->{'candidates'}
Aresult
11
00
Truth table0%0/2
A
A= $data->{'candidates'}
Aresult
11
00
217 0 ▶ return [] unless @$candidates;
Branch: 2 branches
BranchTrueFalseTotal
unless @$candidates 9 104 113
unless @$candidates 0 0 0
218 0 ▶ my $parts = $candidates->[0]{content}{parts} || [];
Condition25%1/4
$candidates->[0]{'content'}{'parts'} || []
l!l
1040
$candidates->[0]{'content'}{'parts'} || []
l!l
00
MC/DC0%0/1
$candidates->[0]{'content'}{'parts'} || []
$candidates->[0]{'content'}{'parts'}
MC/DC0%0/1
$candidates->[0]{'content'}{'parts'} || []
$candidates->[0]{'content'}{'parts'}
Truth table50%1/2
A
A= $candidates->[0]{'content'}{'parts'}
Aresult
11
00
Truth table0%0/2
A
A= $candidates->[0]{'content'}{'parts'}
Aresult
11
00
219 0 return [ grep { exists $_->{functionCall} } @$parts ];
220 }
221 0 ▶ if ( $fmt eq 'responses' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fmt eq "responses") 15 0 15
if ($fmt eq "responses") 0 0 0
222 # Server-side call items (web_search_call, mcp_call, ...) are never located:
223 # the provider already ran them (ADR 0003 Update k206). A client-actionable
224 # item Langertha cannot map croaks instead of being skipped (ADR 0030).
225 0 require Langertha::Tool;
226 0 my @calls;
227 0 ▶ for my $item ( @{ $data->{output} // [] } ) {
Condition25%1/4
$data->{"output"} // []
l!l
150
$data->{"output"} // []
l!l
00
MC/DC0%0/1
$data->{"output"} // []
$data->{"output"}
MC/DC0%0/1
$data->{"output"} // []
$data->{"output"}
Truth table50%1/2
A
A= $data->{"output"}
Aresult
11
00
Truth table0%0/2
A
A= $data->{"output"}
Aresult
11
00
228 0 ▶ next unless ref($item) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $item eq "HASH" 0 24 24
unless ref $item eq "HASH" 0 0 0
229 0 Langertha::Tool->_croak_on_client_item($item);
230 0 ▶ my $type = $item->{type} // '';
Condition25%1/4
$item->{"type"} // ""
l!l
180
$item->{"type"} // ""
l!l
00
MC/DC0%0/1
$item->{"type"} // ""
$item->{"type"}
MC/DC0%0/1
$item->{"type"} // ""
$item->{"type"}
Truth table50%1/2
A
A= $item->{"type"}
Aresult
11
00
Truth table0%0/2
A
A= $item->{"type"}
Aresult
11
00
231 0 ▶ if ( $type eq 'function_call' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'function_call') { } 7 11 18
elsif ($type eq 'message') { } 8 3 11
if ($type eq 'function_call') { } 0 0 0
elsif ($type eq 'message') { } 0 0 0
232 0 push @calls, $item;
233 }
234 elsif ( $type eq 'message' ) {
235 push @calls,
236 0 ▶ grep { ( $_->{type} // '' ) eq 'function_call' } @{ $item->{content} // [] };
Condition25%2/8
$_->{"type"} // ""
l!l
80
$item->{"content"} // []
l!l
80
$_->{"type"} // ""
l!l
00
$item->{"content"} // []
l!l
00
MC/DC0%0/1
$_->{"type"} // ""
$_->{"type"}
MC/DC0%0/1
$item->{"content"} // []
$item->{"content"}
MC/DC0%0/1
$_->{"type"} // ""
$_->{"type"}
MC/DC0%0/1
$item->{"content"} // []
$item->{"content"}
Truth table50%1/2
A
A= $_->{"type"}
Aresult
11
00
Truth table50%1/2
A
A= $item->{"content"}
Aresult
11
00
Truth table0%0/2
A
A= $_->{"type"}
Aresult
11
00
Truth table0%0/2
A
A= $item->{"content"}
Aresult
11
00
237 }
238 }
239 0 return \@calls;
240 }
241 0 croak "Langertha::ToolCall: unknown wire format '$fmt'";
242 }
243  
244  
245 # THE canonical inbound entry point: pull every tool call out of an upstream
246 # response for a given wire format (locate + from_fmt). Engines pass their
247 # tool_wire_format. Returns a list of ToolCall objects (possibly empty). The
248 # per-format response-walking lives only in locate(). Callers with no format
249 # in scope use extract_sniff() instead.
250 787 ▶sub extract {
Subroutine extract: called CC=2 SCAR=9.2
Pod documented
251 0 ▶ my ( $class, $fmt, $data ) = @_;
Subroutine extract: not called CC=2 SCAR=9.2
Pod documented
252 0 ▶ croak "Langertha::ToolCall->extract requires (\$fmt, \$data)" if ref $fmt;
Branch: 2 branches
BranchTrueFalseTotal
if ref $fmt 1 786 787
if ref $fmt 0 0 0
253 0 return grep { defined }
254 0 map { $class->from_fmt( $fmt, $_ ) } @{ $class->locate( $fmt, $data ) };
255 }
256  
257  
258 # Detect the wire format from the top-level shape of a raw response, WITHOUT
259 # walking the per-format tool structures (that walking lives only in locate).
260 # Probe order matches the legacy self-sniffing extract. Returns a
261 # tool_wire_format tag, or undef if the shape matches nothing known.
262 my @SNIFF_PROBES = (
263 16 ▶ [ openai => sub { ref( $_[0]->{choices} ) eq 'ARRAY' } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
264 11 ▶ [ ollama => sub { ref( $_[0]->{message} ) eq 'HASH' } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
265 9 ▶ [ anthropic => sub { ref( $_[0]->{content} ) eq 'ARRAY' } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
266 6 ▶ [ gemini => sub { ref( $_[0]->{candidates} ) eq 'ARRAY' } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
267 4 ▶ [ responses => sub { ref( $_[0]->{output} ) eq 'ARRAY' } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
268 );
269  
270 16 ▶sub sniff_format {
Subroutine sniff_format: called CC=4 SCAR=19.1
Pod undocumented
271 0 ▶ my ( $class, $data ) = @_;
Subroutine sniff_format: not called CC=4 SCAR=19.1
Pod undocumented
272 0 ▶ return undef unless ref($data) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 16 16
unless ref $data eq "HASH" 0 0 0
273 0 for my $probe (@SNIFF_PROBES) {
274 0 ▶ return $probe->[0] if $probe->[1]->($data);
Branch: 2 branches
BranchTrueFalseTotal
if $probe->[1]->($data) 14 32 46
if $probe->[1]->($data) 0 0 0
275 }
276 0 return undef;
277 }
278  
279 # Format-agnostic inbound for callers that genuinely have no wire format in
280 # scope (the Langertha::Output::Tools back-compat facade): sniff the shape,
281 # then delegate to extract. Deliberately NOT named extract() so there is
282 # exactly one canonical inbound entry point — the format-pinned extract above.
283 10 ▶sub extract_sniff {
Subroutine extract_sniff: called CC=2 SCAR=9.2
Pod documented
284 0 ▶ my ( $class, $data ) = @_;
Subroutine extract_sniff: not called CC=2 SCAR=9.2
Pod documented
285 0 ▶ my $fmt = $class->sniff_format($data) or return ();
Branch: 2 branches
BranchTrueFalseTotal
my $fmt = $class->sniff_format($data) or return () 9 1 10
my $fmt = $class->sniff_format($data) or return () 0 0 0
286 0 return $class->extract( $fmt, $data );
287 }
288  
289  
290 # Hermes-style XML embedded in plain text. Returns ($cleaned_text, \@calls).
291 # The one hermes text lift: Role::Tools delegates here with the engine's
292 # hermes_call_tag (karr k255). Only a well-formed call (a JSON object with a
293 # non-empty name) becomes a ToolCall and leaves the text (k163); a block that
294 # carries no call stays in the text where it was -- what the model wrote is
295 # not dropped (k253).
296 258 ▶sub extract_hermes_from_text {
Subroutine extract_hermes_from_text: called CC=7 SCAR=26.8
Pod documented
297 0 ▶ my ( $class, $text, %opts ) = @_;
Subroutine extract_hermes_from_text: not called CC=7 SCAR=26.8
Pod documented
298 0 ▶ my $tag = defined $opts{tag} && length $opts{tag} ? $opts{tag} : 'tool_call';
Branch: 2 branches
BranchTrueFalseTotal
defined $opts{'tag'} && length $opts{'tag'} ? : 237 21 258
defined $opts{'tag'} && length $opts{'tag'} ? : 0 0 0
Condition33%2/6
defined $opts{"tag"} and length $opts{"tag"}
!ll&&!rl&&r
210237
defined $opts{"tag"} and length $opts{"tag"}
!ll&&!rl&&r
000
MC/DC50%1/2
defined $opts{"tag"} and length $opts{"tag"}
defined $opts{"tag"} length $opts{"tag"}
MC/DC0%0/2
defined $opts{"tag"} and length $opts{"tag"}
defined $opts{"tag"} length $opts{"tag"}
Truth table66%2/3
A and B
A= defined $opts{"tag"}B= length $opts{"tag"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $opts{"tag"}B= length $opts{"tag"}
ABresult
0X0
100
111
299 0 ▶ my $clean = defined($text) ? $text : '';
Branch: 2 branches
BranchTrueFalseTotal
defined $text ? : 258 0 258
defined $text ? : 0 0 0
300 0 my @calls;
301 0 $clean =~ s{(<\Q$tag\E>\s*(.*?)\s*</\Q$tag\E>)}{
302 0 my ( $block, $json ) = ( $1, $2 );
303 0 my $obj = eval { $TEXT_JSON->decode($json) };
304 ( ref($obj) eq 'HASH' && defined $obj->{name} && length $obj->{name} )
305 0 ▶ ? do {
Branch: 2 branches
BranchTrueFalseTotal
ref $obj eq 'HASH' && defined $obj->{'name'} && length $obj->{'name'} ? : 187 38 225
ref $obj eq 'HASH' && defined $obj->{'name'} && length $obj->{'name'} ? : 0 0 0
Condition50%6/12
ref $obj eq "HASH" and defined $obj->{"name"} and length $obj->{"name"}
!ll&&!rl&&r
353187
ref $obj eq "HASH" and defined $obj->{"name"}
!ll&&!rl&&r
269190
ref $obj eq "HASH" and defined $obj->{"name"} and length $obj->{"name"}
!ll&&!rl&&r
000
ref $obj eq "HASH" and defined $obj->{"name"}
!ll&&!rl&&r
000
MC/DC100%3/3
ref $obj eq "HASH" and defined $obj->{"name"} and length $obj->{"name"}
ref $obj eq "HASH" defined $obj->{"name"} length $obj->{"name"}
MC/DC0%0/3
ref $obj eq "HASH" and defined $obj->{"name"} and length $obj->{"name"}
ref $obj eq "HASH" defined $obj->{"name"} length $obj->{"name"}
Truth table100%4/4
A and B and C
A= ref $obj eq "HASH"B= defined $obj->{"name"}C= length $obj->{"name"}
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= ref $obj eq "HASH"B= defined $obj->{"name"}C= length $obj->{"name"}
ABCresult
0XX0
10X0
1100
1111
306 # Route arguments through the same decoder the wire constructors use
307 # (from_openai & co.): a non-object or a value that does not decode to
308 # an object becomes {} with arguments_undecodable / arguments_error
309 # set, so the tool loop answers the model an error result it can retry
310 # rather than running the tool on {} -- karr k350 (the k345 mechanism).
311 push @calls, $class->new(
312 name => $obj->{name},
313 0 _args_kwargs( $obj->{arguments} ),
314 );
315 0 '';
316 }
317 : $block;
318 }seg;
319 0 $clean =~ s/^\s+|\s+$//g;
320 0 return ( $clean, \@calls );
321 }
322  
323  
324 # --- Serializers to wire-format hashes ---
325  
326 5 ▶sub to_openai {
Subroutine to_openai: called CC=3 SCAR=15.4
Pod undocumented
327 0 ▶ my ($self, %opts) = @_;
Subroutine to_openai: not called CC=3 SCAR=15.4
Pod undocumented
328 0 ▶ my $id = length( $self->id ) ? $self->id : ( $opts{fallback_id} // 'call_langertha' );
Branch: 2 branches
BranchTrueFalseTotal
length $self->id ? : 4 1 5
length $self->id ? : 0 0 0
Condition25%1/4
$opts{"fallback_id"} // "call_langertha"
l!l
10
$opts{"fallback_id"} // "call_langertha"
l!l
00
MC/DC0%0/1
$opts{"fallback_id"} // "call_langertha"
$opts{"fallback_id"}
MC/DC0%0/1
$opts{"fallback_id"} // "call_langertha"
$opts{"fallback_id"}
Truth table50%1/2
A
A= $opts{"fallback_id"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"fallback_id"}
Aresult
11
00
329 return {
330 0 id => $id,
331 type => 'function',
332 function => {
333 name => $self->name,
334 arguments => $TEXT_JSON->encode( $self->arguments ),
335 },
336 };
337 }
338  
339 2 ▶sub to_anthropic_block {
Subroutine to_anthropic_block: called CC=3 SCAR=18.4
Pod undocumented
340 0 ▶ my ($self, %opts) = @_;
Subroutine to_anthropic_block: not called CC=3 SCAR=18.4
Pod undocumented
341 0 ▶ my $id = length( $self->id ) ? $self->id : ( $opts{fallback_id} // 'toolu_langertha' );
Branch: 2 branches
BranchTrueFalseTotal
length $self->id ? : 2 0 2
length $self->id ? : 0 0 0
Condition0%0/4
$opts{"fallback_id"} // "toolu_langertha"
l!l
00
$opts{"fallback_id"} // "toolu_langertha"
l!l
00
MC/DC0%0/1
$opts{"fallback_id"} // "toolu_langertha"
$opts{"fallback_id"}
MC/DC0%0/1
$opts{"fallback_id"} // "toolu_langertha"
$opts{"fallback_id"}
Truth table0%0/2
A
A= $opts{"fallback_id"}
Aresult
11
00
Truth table0%0/2
A
A= $opts{"fallback_id"}
Aresult
11
00
342 return {
343 0 type => 'tool_use',
344 id => $id,
345 name => $self->name,
346 input => $self->arguments,
347 };
348 }
349  
350 3 ▶sub to_ollama {
Subroutine to_ollama: called CC=2 SCAR=10.9
Pod undocumented
351 0 ▶ my ($self) = @_;
Subroutine to_ollama: not called CC=2 SCAR=10.9
Pod undocumented
352 return {
353 0 ▶ function => {
Branch: 2 branches
BranchTrueFalseTotal
length $self->id ? : 3 0 3
length $self->id ? : 0 0 0
354 name => $self->name,
355 arguments => $self->arguments,
356 },
357 ( length( $self->id ) ? ( id => $self->id ) : () ),
358 };
359 }
360  
361 42 ▶sub to_hash {
Subroutine to_hash: called CC=2 SCAR=9.2
Pod undocumented
362 0 ▶ my ($self) = @_;
Subroutine to_hash: not called CC=2 SCAR=9.2
Pod undocumented
363 return {
364 0 ▶ id => $self->id,
Branch: 2 branches
BranchTrueFalseTotal
$self->synthetic ? : 2 40 42
$self->synthetic ? : 0 0 0
365 name => $self->name,
366 arguments => $self->arguments,
367 synthetic => $self->synthetic ? 1 : 0,
368 };
369 }
370  
371 # Make the object transparent to any JSON encoder configured with
372 # convert_blessed => 1 (the house default, see Langertha::Plugin::Langfuse).
373 # Response.tool_calls is an ArrayRef of these, so consumers hit them without
374 # ever asking for a ToolCall by name. Plain delegator to to_hash: TO_JSON must
375 # not become a second, divergent shape.
376 4 ▶sub TO_JSON { shift->to_hash }
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod undocumented
377  
378 205 __PACKAGE__->meta->make_immutable;
379 205 1;
380  
381 __END__
« blib/lib/Langertha/Output/Tools.pm ↑ Summary blib/lib/Langertha/Role/ResponseFormat.pm »