Langertha/ToolChoice.pm

filter: Statementstmt 86.2% 69 / 80 Branchbran 71.0% 54 / 76 Conditioncond 46.1% 12 / 26 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 87.5% 14 / 16 Podpod 0.0% 0 / 13 totaltotal 66.9% 152 / 227 CC 49 SCAR 29.7
CC
49
Cov
74%
from_hash
45.3
to_responses
10.1
to_openai
7.8
CRAP 19.5
« Langertha/Engine/AnthropicBase.pm ↑ Summary Langertha/Content/Image.pm »
1 package Langertha::ToolChoice;
2 # ABSTRACT: Immutable canonical tool-selection policy with cross-provider conversion
3 37 our $VERSION = '0.502';
4 37 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 37 ▶use Moose::Util::TypeConstraints qw( enum );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # Canonical types: 'auto' (let model decide), 'any' (must call any tool),
8 # 'none' (no tool calling), 'tool' (must call this specific tool).
9 37 enum 'Langertha::ToolChoice::Type' => [qw( auto any none tool )];
10  
11 37 has type => (
12 is => 'ro',
13 isa => 'Langertha::ToolChoice::Type',
14 required => 1,
15 );
16  
17 has name => (
18 is => 'ro',
19 isa => 'Maybe[Str]',
20 29 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 37 );
22  
23 # --- Convenience constructors ---
24  
25 8 ▶sub auto { my $class = shift; $class->new( type => 'auto' ) }
Subroutine auto: called CC=1 SCAR=0.0
Pod undocumented
26 12 ▶sub any { my $class = shift; $class->new( type => 'any' ) }
Subroutine any: called CC=1 SCAR=0.0
Pod undocumented
27 8 ▶sub none { my $class = shift; $class->new( type => 'none' ) }
Subroutine none: called CC=1 SCAR=0.0
Pod undocumented
28 sub specific {
29 13 ▶ my ( $class, $name ) = @_;
Subroutine specific: called CC=1 SCAR=0.0
Pod undocumented
30 13 return $class->new( type => 'tool', name => $name );
31 }
32  
33 # --- Constructors from wire-format hashes/strings ---
34  
35 sub from_hash {
36 21 ▶ my ($class, $val) = @_;
Subroutine from_hash: called CC=22 SCAR=38.1
Pod undocumented
37 21 ▶ return undef unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 20 21
38  
39 20 ▶ if ( !ref($val) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $val) 11 9 20
40 11 ▶ return $class->any if $val eq 'required' || $val eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "required" or $val eq "any" 6 5 11
Condition100%3/3
$val eq "required" or $val eq "any"
l!l&&r!l&&!r
425
MC/DC100%2/2
$val eq "required" or $val eq "any"
$val eq "required" $val eq "any"
Truth table100%3/3
A or B
A= $val eq "required"B= $val eq "any"
ABresult
1X1
011
000
41 5 ▶ return $class->auto if $val eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "auto" 3 2 5
42 2 ▶ return $class->none if $val eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "none" 2 0 2
43 0 return undef;
44 }
45  
46 9 ▶ return undef unless ref($val) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val eq "HASH" 0 9 9
47 9 ▶ my $type = $val->{type} // '';
Condition50%1/2
$val->{"type"} // ""
l!l
90
MC/DC0%0/1
$val->{"type"} // ""
$val->{"type"}
Truth table50%1/2
A
A= $val->{"type"}
Aresult
11
00
48  
49 9 ▶ if ( $type eq 'function' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "function") 3 6 9
50 3 my $name = '';
51 3 ▶ if ( ref( $val->{function} ) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val->{'function'} eq 'HASH') { } 3 0 3
elsif (defined $val->{'name'}) { } 0 0 0
52 3 ▶ $name = $val->{function}{name} // '';
Condition50%1/2
$val->{"function"}{"name"} // ""
l!l
30
MC/DC0%0/1
$val->{"function"}{"name"} // ""
$val->{"function"}{"name"}
Truth table50%1/2
A
A= $val->{"function"}{"name"}
Aresult
11
00
53 } elsif ( defined $val->{name} ) {
54 0 ▶ $name = $val->{name} // '';
Condition0%0/2
$val->{"name"} // ""
l!l
00
MC/DC0%0/1
$val->{"name"} // ""
$val->{"name"}
Truth table0%0/2
A
A= $val->{"name"}
Aresult
11
00
55 }
56 3 ▶ return length($name) ? $class->specific($name) : $class->auto;
Branch: 1 branch
BranchTrueFalseTotal
length $name ? : 3 0 3
57 }
58  
59 6 ▶ if ( $type eq 'tool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tool") 5 1 6
60 5 ▶ my $name = $val->{name} // '';
Condition50%1/2
$val->{"name"} // ""
l!l
50
MC/DC0%0/1
$val->{"name"} // ""
$val->{"name"}
Truth table50%1/2
A
A= $val->{"name"}
Aresult
11
00
61 5 ▶ return length($name) ? $class->specific($name) : $class->auto;
Branch: 1 branch
BranchTrueFalseTotal
length $name ? : 5 0 5
62 }
63  
64 1 ▶ return $class->any if $type eq 'any' || $type eq 'required';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "any" or $type eq "required" 1 0 1
Condition33%1/3
$type eq "any" or $type eq "required"
l!l&&r!l&&!r
010
MC/DC0%0/2
$type eq "any" or $type eq "required"
$type eq "any" $type eq "required"
Truth table33%1/3
A or B
A= $type eq "any"B= $type eq "required"
ABresult
1X1
011
000
65 0 ▶ return $class->auto if $type eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "auto" 0 0 0
66 0 ▶ return $class->none if $type eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "none" 0 0 0
67 0 return undef;
68 }
69  
70 0 ▶sub from_openai { shift->from_hash(@_) }
Subroutine from_openai: not called CC=1 SCAR=6.9
Pod undocumented
71 0 ▶sub from_anthropic { shift->from_hash(@_) }
Subroutine from_anthropic: not called CC=1 SCAR=6.9
Pod undocumented
72  
73 # --- Serializers ---
74  
75 sub to_openai {
76 10 ▶ my ($self) = @_;
Subroutine to_openai: called CC=7 SCAR=20.5
Pod undocumented
77 10 ▶ return 'required' if $self->type eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "any" 2 8 10
78 8 ▶ return 'auto' if $self->type eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "auto" 2 6 8
79 6 ▶ return 'none' if $self->type eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "none" 2 4 6
80 4 ▶ if ( $self->type eq 'tool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "tool") 4 0 4
81 4 ▶ return defined $self->name && length $self->name
Branch: 1 branch
BranchTrueFalseTotal
defined $self->name && length $self->name ? : 4 0 4
Condition33%1/3
defined $self->name and length $self->name
!ll&&!rl&&r
004
MC/DC0%0/2
defined $self->name and length $self->name
defined $self->name length $self->name
Truth table33%1/3
A and B
A= defined $self->nameB= length $self->name
ABresult
0X0
100
111
82 ? { type => 'function', function => { name => $self->name } }
83 : 'auto';
84 }
85 0 return undef;
86 }
87  
88 sub to_anthropic {
89 9 ▶ my ($self) = @_;
Subroutine to_anthropic: called CC=7 SCAR=20.5
Pod undocumented
90 9 ▶ return { type => 'any' } if $self->type eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "any" 2 7 9
91 7 ▶ return { type => 'auto' } if $self->type eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "auto" 1 6 7
92 6 ▶ return { type => 'none' } if $self->type eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "none" 2 4 6
93 4 ▶ if ( $self->type eq 'tool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "tool") 4 0 4
94 4 ▶ return defined $self->name && length $self->name
Branch: 1 branch
BranchTrueFalseTotal
defined $self->name && length $self->name ? : 4 0 4
Condition33%1/3
defined $self->name and length $self->name
!ll&&!rl&&r
004
MC/DC0%0/2
defined $self->name and length $self->name
defined $self->name length $self->name
Truth table33%1/3
A and B
A= defined $self->nameB= length $self->name
ABresult
0X0
100
111
95 ? { type => 'tool', name => $self->name }
96 : { type => 'auto' };
97 }
98 0 return undef;
99 }
100  
101 sub to_perplexity {
102 4 ▶ my ($self) = @_;
Subroutine to_perplexity: called CC=3 SCAR=11.0
Pod undocumented
103 # Perplexity only accepts string forms: none / auto / required.
104 # Named-tool forcing is not supported on the wire — engine layer
105 # is expected to switch to a response_format-based path when a
106 # named tool is requested. We coerce here so callers that ignore
107 # capabilities still get *something* sensible.
108 4 ▶ return 'none' if $self->type eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "none" 1 3 4
109 3 ▶ return 'auto' if $self->type eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "auto" 1 2 3
110 2 return 'required';
111 }
112  
113 sub to_gemini {
114 5 ▶ my ($self) = @_;
Subroutine to_gemini: called CC=7 SCAR=19.7
Pod undocumented
115 # Gemini uses toolConfig.functionCallingConfig:
116 # { mode => AUTO|ANY|NONE, allowed_function_names => [...] }
117 5 ▶ return { functionCallingConfig => { mode => 'NONE' } } if $self->type eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "none" 1 4 5
118 4 ▶ return { functionCallingConfig => { mode => 'AUTO' } } if $self->type eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "auto" 1 3 4
119 3 ▶ return { functionCallingConfig => { mode => 'ANY' } } if $self->type eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "any" 1 2 3
120 2 ▶ if ( $self->type eq 'tool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "tool") 2 0 2
121 2 ▶ return { functionCallingConfig => { mode => 'AUTO' } }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->name and length $self->name 1 1 2
Condition66%2/3
defined $self->name and length $self->name
!ll&&!rl&&r
101
MC/DC50%1/2
defined $self->name and length $self->name
defined $self->name length $self->name
Truth table66%2/3
A and B
A= defined $self->nameB= length $self->name
ABresult
0X0
100
111
122 unless defined $self->name && length $self->name;
123 return {
124 1 functionCallingConfig => {
125 mode => 'ANY',
126 allowed_function_names => [ $self->name ],
127 },
128 };
129 }
130 0 return undef;
131 }
132  
133 sub to_responses {
134 1 ▶ my ($self) = @_;
Subroutine to_responses: called CC=7 SCAR=23.2
Pod undocumented
135 # Responses API uses flat {type => 'function', name => 'foo'} — no nested function wrapper
136 1 ▶ return 'auto' if $self->type eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "auto" 0 1 1
137 1 ▶ return 'none' if $self->type eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "none" 0 1 1
138 1 ▶ return 'required' if $self->type eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "any" 0 1 1
139 1 ▶ if ( $self->type eq 'tool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "tool") 1 0 1
140 1 ▶ return defined $self->name && length $self->name
Branch: 1 branch
BranchTrueFalseTotal
defined $self->name && length $self->name ? : 1 0 1
Condition33%1/3
defined $self->name and length $self->name
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self->name and length $self->name
defined $self->name length $self->name
Truth table33%1/3
A and B
A= defined $self->nameB= length $self->name
ABresult
0X0
100
111
141 ? { type => 'function', name => $self->name }
142 : 'auto';
143 }
144 0 return undef;
145 }
146  
147 sub to_hash {
148 2 ▶ my ($self) = @_;
Subroutine to_hash: called CC=2 SCAR=6.9
Pod undocumented
149 2 ▶ return { type => $self->type, ( defined $self->name ? ( name => $self->name ) : () ) };
Branch: 1 branch
BranchTrueFalseTotal
defined $self->name ? : 1 1 2
150 }
151  
152 37 __PACKAGE__->meta->make_immutable;
153 37 1;
154  
155 __END__
156  
157 =pod
158  
159 =encoding UTF-8
160  
161 =head1 NAME
162  
163 Langertha::ToolChoice - Immutable canonical tool-selection policy with cross-provider conversion
164  
165 =head1 VERSION
166  
167 version 0.502
168  
169 =head1 SUPPORT
170  
171 =head2 Issues
172  
173 Please report bugs and feature requests on GitHub at
174 L<https://github.com/Getty/langertha/issues>.
175  
176 =head2 IRC
177  
178 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
179  
180 =head1 CONTRIBUTING
181  
182 Contributions are welcome! Please fork the repository and submit a pull request.
183  
184 =head1 AUTHOR
185  
186 Torsten Raudssus <getty@cpan.org>
187  
188 =head1 COPYRIGHT AND LICENSE
189  
190 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
191  
192 This is free software; you can redistribute it and/or modify it under
193 the same terms as the Perl 5 programming language system itself.
194  
195 =cut
« Langertha/Engine/AnthropicBase.pm ↑ Summary Langertha/Content/Image.pm »