Langertha/Tool.pm

filter: Statementstmt 69.2% 45 / 65 Branchbran 50.0% 18 / 36 Conditioncond 37.2% 16 / 43 MC/DCmcdc 17.3% 4 / 23 Subroutinesub 52.9% 9 / 17 Podpod 0.0% 0 / 14 totaltotal 46.4% 92 / 198 CC 38 SCAR 28.7
CC
38
Cov
55%
from_mcp
56.0
from_gemini
42.0
from_openai
12.6
CRAP 17.6
« Langertha/Content/Image.pm ↑ Summary Langertha/ToolCall.pm »
1 package Langertha::Tool;
2 # ABSTRACT: Immutable canonical tool definition with cross-provider format conversion
3 35 our $VERSION = '0.502';
4 35 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 35 has name => (
7 is => 'ro',
8 isa => 'Str',
9 required => 1,
10 );
11  
12 35 has description => (
13 is => 'ro',
14 isa => 'Str',
15 default => '',
16 );
17  
18 has input_schema => (
19 is => 'ro',
20 isa => 'HashRef',
21 0 ▶ default => sub { { type => 'object', properties => {} } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 35 );
23  
24 2 ▶sub _empty_schema { { type => 'object', properties => {} } }
Subroutine _empty_schema: called CC=1 SCAR=0.0
25  
26 # --- Constructors from wire-format hashes ---
27  
28 sub from_openai {
29 4 ▶ my ($class, $hash) = @_;
Subroutine from_openai: called CC=10 SCAR=25.3
Pod undocumented
30 4 ▶ return undef unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 4 4
31 4 ▶ return undef unless ($hash->{type} // '') eq 'function';
Branch: 1 branch
BranchTrueFalseTotal
unless ($$hash{"type"} // "") eq "function" 0 4 4
Condition50%1/2
$$hash{"type"} // ""
l!l
40
MC/DC0%0/1
$$hash{"type"} // ""
$$hash{"type"}
Truth table50%1/2
A
A= $$hash{"type"}
Aresult
11
00
32 4 ▶ my $fn = $hash->{function} || {};
Condition50%1/2
$$hash{'function'} || {}
l!l
40
MC/DC0%0/1
$$hash{'function'} || {}
$$hash{'function'}
Truth table50%1/2
A
A= $$hash{'function'}
Aresult
11
00
33 4 ▶ return undef unless ref($fn) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fn eq "HASH" 0 4 4
34 4 ▶ my $name = $fn->{name} // '';
Condition50%1/2
$$fn{"name"} // ""
l!l
40
MC/DC0%0/1
$$fn{"name"} // ""
$$fn{"name"}
Truth table50%1/2
A
A= $$fn{"name"}
Aresult
11
00
35 4 ▶ return undef unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 4 4
36 return $class->new(
37 name => $name,
38 description => ( $fn->{description} // '' ),
39 4 ▶ input_schema => ( $fn->{parameters} || $class->_empty_schema ),
Condition80%4/5
$$fn{"description"} // ""
l!l
31
$$fn{'parameters'} || $class->_empty_schema
l!l&&r!l&&!r
220
MC/DC100%1/1
$$fn{"description"} // ""
$$fn{"description"}
MC/DC0%0/2
$$fn{'parameters'} || $class->_empty_schema
$$fn{'parameters'} $class->_empty_schema
Truth table100%2/2
A
A= $$fn{"description"}
Aresult
11
00
Truth table66%2/3
A || B
A= $$fn{'parameters'}B= $class->_empty_schema
ABresult
1X1
011
000
40 );
41 }
42  
43 sub from_anthropic {
44 6 ▶ my ($class, $hash) = @_;
Subroutine from_anthropic: called CC=7 SCAR=20.7
Pod undocumented
45 6 ▶ return undef unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 6 6
46 6 ▶ my $name = $hash->{name} // '';
Condition100%2/2
$$hash{"name"} // ""
l!l
42
MC/DC100%1/1
$$hash{"name"} // ""
$$hash{"name"}
Truth table100%2/2
A
A= $$hash{"name"}
Aresult
11
00
47 6 ▶ return undef unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 2 4 6
48 return $class->new(
49 name => $name,
50 description => ( $hash->{description} // '' ),
51 4 ▶ input_schema => ( $hash->{input_schema} || $hash->{parameters} || $class->_empty_schema ),
Condition50%4/8
$$hash{"description"} // ""
l!l
22
$$hash{'input_schema'} || $$hash{'parameters'} || $class->_empty_schema
l!l&&r!l&&!r
400
$$hash{"input_schema"} or $$hash{"parameters"}
l!l&&r!l&&!r
400
MC/DC100%1/1
$$hash{"description"} // ""
$$hash{"description"}
MC/DC0%0/3
$$hash{'input_schema'} || $$hash{'parameters'} || $class->_empty_schema
$$hash{"input_schema"} $$hash{"parameters"} $class->_empty_schema
Truth table100%2/2
A
A= $$hash{"description"}
Aresult
11
00
Truth table25%1/4
A or B || C
A= $$hash{"input_schema"}B= $$hash{"parameters"}C= $class->_empty_schema
ABCresult
1XX1
01X1
0011
0000
52 );
53 }
54  
55 # MCP server tool definition: name + description + inputSchema (camelCase).
56 sub from_mcp {
57 0 ▶ my ($class, $hash) = @_;
Subroutine from_mcp: not called CC=7 SCAR=40.3
Pod undocumented
58 0 ▶ return undef unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 0 0
59 0 ▶ my $name = $hash->{name} // '';
Condition0%0/2
$$hash{"name"} // ""
l!l
00
MC/DC0%0/1
$$hash{"name"} // ""
$$hash{"name"}
Truth table0%0/2
A
A= $$hash{"name"}
Aresult
11
00
60 0 ▶ return undef unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 0 0
61 return $class->new(
62 name => $name,
63 description => ( $hash->{description} // '' ),
64 0 ▶ input_schema => ( $hash->{inputSchema} || $hash->{input_schema} || $class->_empty_schema ),
Condition0%0/8
$$hash{"description"} // ""
l!l
00
$$hash{'inputSchema'} || $$hash{'input_schema'} || $class->_empty_schema
l!l&&r!l&&!r
000
$$hash{"inputSchema"} or $$hash{"input_schema"}
l!l&&r!l&&!r
000
MC/DC0%0/1
$$hash{"description"} // ""
$$hash{"description"}
MC/DC0%0/3
$$hash{'inputSchema'} || $$hash{'input_schema'} || $class->_empty_schema
$$hash{"inputSchema"} $$hash{"input_schema"} $class->_empty_schema
Truth table0%0/2
A
A= $$hash{"description"}
Aresult
11
00
Truth table0%0/4
A or B || C
A= $$hash{"inputSchema"}B= $$hash{"input_schema"}C= $class->_empty_schema
ABCresult
1XX1
01X1
0011
0000
65 );
66 }
67  
68 # Gemini functionDeclarations: name + description + parameters (flat).
69 sub from_gemini {
70 0 ▶ my ($class, $hash) = @_;
Subroutine from_gemini: not called CC=6 SCAR=37.4
Pod undocumented
71 0 ▶ return undef unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 0 0
72 0 ▶ my $name = $hash->{name} // '';
Condition0%0/2
$$hash{"name"} // ""
l!l
00
MC/DC0%0/1
$$hash{"name"} // ""
$$hash{"name"}
Truth table0%0/2
A
A= $$hash{"name"}
Aresult
11
00
73 0 ▶ return undef unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 0 0
74 return $class->new(
75 name => $name,
76 description => ( $hash->{description} // '' ),
77 0 ▶ input_schema => ( $hash->{parameters} || $class->_empty_schema ),
Condition0%0/5
$$hash{"description"} // ""
l!l
00
$$hash{'parameters'} || $class->_empty_schema
l!l&&r!l&&!r
000
MC/DC0%0/1
$$hash{"description"} // ""
$$hash{"description"}
MC/DC0%0/2
$$hash{'parameters'} || $class->_empty_schema
$$hash{'parameters'} $class->_empty_schema
Truth table0%0/2
A
A= $$hash{"description"}
Aresult
11
00
Truth table0%0/3
A || B
A= $$hash{'parameters'}B= $class->_empty_schema
ABresult
1X1
011
000
78 );
79 }
80  
81 # Generic: figure out the wire shape and route accordingly. Order matters —
82 # we test the most specific markers first.
83 sub from_hash {
84 8 ▶ my ($class, $hash) = @_;
Subroutine from_hash: called CC=9 SCAR=23.0
Pod undocumented
85 8 ▶ return $hash if ref($hash) && eval { $hash->isa(__PACKAGE__) };
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash and eval { do { $hash->isa(__PACKAGE__) } } 0 8 8
Condition33%1/3
ref $hash and eval {
!ll&&!rl&&r
080
MC/DC0%0/2
ref $hash and eval { do { $hash->isa(__PACKAGE__) } }
ref $hash eval { do { $hash->isa(__PACKAGE__) } }
Truth table33%1/3
A and B
A= ref $hashB= eval { do { $hash->isa(__PACKAGE__) } }
ABresult
0X0
100
111
86 8 ▶ return undef unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 8 8
87 8 ▶ return $class->from_openai($hash) if ( $hash->{type} // '' ) eq 'function';
Branch: 1 branch
BranchTrueFalseTotal
if ($$hash{"type"} // "") eq "function" 3 5 8
Condition100%2/2
$$hash{"type"} // ""
l!l
35
MC/DC100%1/1
$$hash{"type"} // ""
$$hash{"type"}
Truth table100%2/2
A
A= $$hash{"type"}
Aresult
11
00
88 5 ▶ return $class->from_mcp($hash) if ref( $hash->{inputSchema} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $$hash{"inputSchema"} eq "HASH" 0 5 5
89 5 ▶ return $class->from_anthropic($hash) if ref( $hash->{input_schema} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $$hash{"input_schema"} eq "HASH" 3 2 5
90 2 ▶ return $class->from_gemini($hash) if ref( $hash->{parameters} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $$hash{"parameters"} eq "HASH" 0 2 2
91 # Last resort: name-only / schemaless
92 2 return $class->from_anthropic($hash);
93 }
94  
95 # Build from a list of any-shape hashrefs and skip ones that don't parse.
96 sub from_list {
97 3 ▶ my ($class, $list) = @_;
Subroutine from_list: called CC=4 SCAR=13.9
Pod undocumented
98 3 ▶ return [] unless ref($list) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 0 3 3
99 3 my @out;
100 3 for my $item (@$list) {
101 8 my $tool = $class->from_hash($item);
102 8 ▶ push @out, $tool if $tool;
Branch: 1 branch
BranchTrueFalseTotal
if $tool 6 2 8
103 }
104 3 return \@out;
105 }
106  
107 # --- Serializers to wire-format hashes ---
108  
109 sub to_openai {
110 8 ▶ my ($self) = @_;
Subroutine to_openai: called CC=1 SCAR=0.0
Pod undocumented
111 return {
112 8 type => 'function',
113 function => {
114 name => $self->name,
115 description => $self->description,
116 parameters => $self->input_schema,
117 },
118 };
119 }
120  
121 sub to_anthropic {
122 6 ▶ my ($self) = @_;
Subroutine to_anthropic: called CC=1 SCAR=0.0
Pod undocumented
123 return {
124 6 name => $self->name,
125 description => $self->description,
126 input_schema => $self->input_schema,
127 };
128 }
129  
130 0 ▶sub to_ollama { $_[0]->to_openai }
Subroutine to_ollama: not called CC=1 SCAR=6.9
Pod undocumented
131  
132 sub to_gemini {
133 0 ▶ my ($self) = @_;
Subroutine to_gemini: not called CC=1 SCAR=6.9
Pod undocumented
134 return {
135 0 name => $self->name,
136 description => $self->description,
137 parameters => $self->input_schema,
138 };
139 }
140  
141 # OpenAI Responses API: flat tool objects, no {type:'function',function:{...}} wrapper
142 sub to_responses {
143 0 ▶ my ($self) = @_;
Subroutine to_responses: not called CC=1 SCAR=6.9
Pod undocumented
144 return {
145 0 type => 'function',
146 name => $self->name,
147 description => $self->description,
148 parameters => $self->input_schema,
149 };
150 }
151  
152 sub to_mcp {
153 0 ▶ my ($self) = @_;
Subroutine to_mcp: not called CC=1 SCAR=6.9
Pod undocumented
154 return {
155 0 name => $self->name,
156 description => $self->description,
157 inputSchema => $self->input_schema,
158 };
159 }
160  
161 # Shape used inside OpenAI's response_format => { type=>'json_schema',
162 # json_schema => { ... } } — and the basis of the chat_f forced-tool
163 # fallback path.
164 sub to_json_schema {
165 0 ▶ my ($self) = @_;
Subroutine to_json_schema: not called CC=1 SCAR=6.9
Pod undocumented
166 return {
167 0 name => $self->name,
168 description => $self->description,
169 schema => $self->input_schema,
170 };
171 }
172  
173 # Canonical hash (matches the legacy Input::Tools->normalize_tools shape).
174 sub to_hash {
175 4 ▶ my ($self) = @_;
Subroutine to_hash: called CC=1 SCAR=0.0
Pod undocumented
176 return {
177 4 name => $self->name,
178 description => $self->description,
179 input_schema => $self->input_schema,
180 };
181 }
182  
183 35 __PACKAGE__->meta->make_immutable;
184 35 1;
185  
186 __END__
187  
188 =pod
189  
190 =encoding UTF-8
191  
192 =head1 NAME
193  
194 Langertha::Tool - Immutable canonical tool definition with cross-provider format conversion
195  
196 =head1 VERSION
197  
198 version 0.502
199  
200 =head1 SUPPORT
201  
202 =head2 Issues
203  
204 Please report bugs and feature requests on GitHub at
205 L<https://github.com/Getty/langertha/issues>.
206  
207 =head2 IRC
208  
209 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
210  
211 =head1 CONTRIBUTING
212  
213 Contributions are welcome! Please fork the repository and submit a pull request.
214  
215 =head1 AUTHOR
216  
217 Torsten Raudssus <getty@cpan.org>
218  
219 =head1 COPYRIGHT AND LICENSE
220  
221 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
222  
223 This is free software; you can redistribute it and/or modify it under
224 the same terms as the Perl 5 programming language system itself.
225  
226 =cut
« Langertha/Content/Image.pm ↑ Summary Langertha/ToolCall.pm »