blib/lib/Langertha/ToolChoice.pm

filter: Statementstmt 71.7% 84 / 117 Branchbran 58.9% 66 / 112 Conditioncond 38.0% 19 / 50 MC/DCmcdc 23.3% 7 / 30 Subroutinesub 81.8% 18 / 22 Podpod 100% 17 / 17 totaltotal 60.6% 211 / 348 CC 107 SCAR 40.2
CC
107
Cov
61%
from_hash
146.7
from_hash
146.7
to_openai
7.8
CRAP 55.9
« blib/lib/Langertha/CallResult.pm ↑ Summary blib/lib/Langertha/Role/Streaming.pm »
1 package Langertha::ToolChoice;
2 # ABSTRACT: Immutable canonical tool-selection policy with cross-provider conversion
3 198 our $VERSION = '0.505';
4 198 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 198 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 198 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 198 ▶use Moose::Util::TypeConstraints qw( enum );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 # Canonical types: 'auto' (let model decide), 'any' (must call any tool),
11 # 'none' (no tool calling), 'tool' (must call this specific tool).
12 198 enum 'Langertha::ToolChoice::Type' => [qw( auto any none tool )];
13  
14 198 has type => (
15 is => 'ro',
16 isa => 'Langertha::ToolChoice::Type',
17 required => 1,
18 );
19  
20  
21 has name => (
22 is => 'ro',
23 isa => 'Maybe[Str]',
24 155 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 198 );
26  
27  
28 # --- Convenience constructors ---
29  
30 45 ▶sub auto { my $class = shift; $class->new( type => 'auto' ) }
Subroutine auto: called CC=1 SCAR=0.0
Pod documented
31 58 ▶sub any { my $class = shift; $class->new( type => 'any' ) }
Subroutine any: called CC=1 SCAR=0.0
Pod documented
32 51 ▶sub none { my $class = shift; $class->new( type => 'none' ) }
Subroutine none: called CC=1 SCAR=0.0
Pod documented
33 99 ▶sub specific {
Subroutine specific: called CC=1 SCAR=1.2
Pod documented
34 0 ▶ my ( $class, $name ) = @_;
Subroutine specific: not called CC=1 SCAR=1.2
Pod documented
35 0 return $class->new( type => 'tool', name => $name );
36 }
37  
38  
39 # --- Constructors from wire-format hashes/strings ---
40  
41 241 ▶sub from_hash {
Subroutine from_hash: called CC=24 SCAR=49.9
Pod documented
42 0 ▶ my ($class, $val) = @_;
Subroutine from_hash: not called CC=24 SCAR=49.9
Pod documented
43 0 ▶ return undef unless defined $val;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $val 2 239 241
unless defined $val 0 0 0
44 # A ToolChoice is already canonical input (karr k235): hand it back, so every
45 # request builder serializes it with ->to($fmt) instead of leaking TO_JSON.
46 0 ▶ return $val if blessed($val) && $val->isa(__PACKAGE__);
Branch: 2 branches
BranchTrueFalseTotal
if builtin::blessed($val) and $val->isa(__PACKAGE__) 54 185 239
if builtin::blessed($val) and $val->isa(__PACKAGE__) 0 0 0
Condition50%3/6
builtin::blessed($val) and $val->isa(__PACKAGE__)
!ll&&!rl&&r
184154
builtin::blessed($val) and $val->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC100%2/2
builtin::blessed($val) and $val->isa(__PACKAGE__)
builtin::blessed($val) $val->isa(__PACKAGE__)
MC/DC0%0/2
builtin::blessed($val) and $val->isa(__PACKAGE__)
builtin::blessed($val) $val->isa(__PACKAGE__)
Truth table100%3/3
A and B
A= builtin::blessed($val)B= $val->isa(__PACKAGE__)
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($val)B= $val->isa(__PACKAGE__)
ABresult
0X0
100
111
47  
48 0 ▶ if ( !ref($val) ) {
Branch: 2 branches
BranchTrueFalseTotal
unless (ref $val) 100 85 185
unless (ref $val) 0 0 0
49 0 ▶ return $class->any if $val eq 'required' || $val eq 'any';
Branch: 2 branches
BranchTrueFalseTotal
if $val eq "required" or $val eq "any" 41 59 100
if $val eq "required" or $val eq "any" 0 0 0
Condition50%3/6
$val eq "required" or $val eq "any"
l!l&&r!l&&!r
32959
$val eq "required" or $val eq "any"
l!l&&r!l&&!r
000
MC/DC100%2/2
$val eq "required" or $val eq "any"
$val eq "required" $val eq "any"
MC/DC0%0/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
Truth table0%0/3
A or B
A= $val eq "required"B= $val eq "any"
ABresult
1X1
011
000
50 0 ▶ return $class->auto if $val eq 'auto';
Branch: 2 branches
BranchTrueFalseTotal
if $val eq "auto" 25 34 59
if $val eq "auto" 0 0 0
51 0 ▶ return $class->none if $val eq 'none';
Branch: 2 branches
BranchTrueFalseTotal
if $val eq "none" 34 0 34
if $val eq "none" 0 0 0
52 0 return undef;
53 }
54  
55 0 ▶ return undef unless ref($val) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $val eq "HASH" 1 84 85
unless ref $val eq "HASH" 0 0 0
56 0 ▶ my $type = $val->{type} // '';
Condition25%1/4
$val->{"type"} // ""
l!l
840
$val->{"type"} // ""
l!l
00
MC/DC0%0/1
$val->{"type"} // ""
$val->{"type"}
MC/DC0%0/1
$val->{"type"} // ""
$val->{"type"}
Truth table50%1/2
A
A= $val->{"type"}
Aresult
11
00
Truth table0%0/2
A
A= $val->{"type"}
Aresult
11
00
57  
58 0 ▶ if ( $type eq 'function' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq "function") 7 77 84
if ($type eq "function") 0 0 0
59 0 my $name = '';
60 0 ▶ if ( ref( $val->{function} ) eq 'HASH' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $val->{'function'} eq 'HASH') { } 7 0 7
elsif (defined $val->{'name'}) { } 0 0 0
if (ref $val->{'function'} eq 'HASH') { } 0 0 0
elsif (defined $val->{'name'}) { } 0 0 0
61 0 ▶ $name = $val->{function}{name} // '';
Condition25%1/4
$val->{"function"}{"name"} // ""
l!l
70
$val->{"function"}{"name"} // ""
l!l
00
MC/DC0%0/1
$val->{"function"}{"name"} // ""
$val->{"function"}{"name"}
MC/DC0%0/1
$val->{"function"}{"name"} // ""
$val->{"function"}{"name"}
Truth table50%1/2
A
A= $val->{"function"}{"name"}
Aresult
11
00
Truth table0%0/2
A
A= $val->{"function"}{"name"}
Aresult
11
00
62 } elsif ( defined $val->{name} ) {
63 0 ▶ $name = $val->{name} // '';
Condition0%0/4
$val->{"name"} // ""
l!l
00
$val->{"name"} // ""
l!l
00
MC/DC0%0/1
$val->{"name"} // ""
$val->{"name"}
MC/DC0%0/1
$val->{"name"} // ""
$val->{"name"}
Truth table0%0/2
A
A= $val->{"name"}
Aresult
11
00
Truth table0%0/2
A
A= $val->{"name"}
Aresult
11
00
64 }
65 0 ▶ return length($name) ? $class->specific($name) : $class->auto;
Branch: 2 branches
BranchTrueFalseTotal
length $name ? : 7 0 7
length $name ? : 0 0 0
66 }
67  
68 0 ▶ if ( $type eq 'tool' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq "tool") 67 10 77
if ($type eq "tool") 0 0 0
69 0 ▶ my $name = $val->{name} // '';
Condition25%1/4
$val->{"name"} // ""
l!l
670
$val->{"name"} // ""
l!l
00
MC/DC0%0/1
$val->{"name"} // ""
$val->{"name"}
MC/DC0%0/1
$val->{"name"} // ""
$val->{"name"}
Truth table50%1/2
A
A= $val->{"name"}
Aresult
11
00
Truth table0%0/2
A
A= $val->{"name"}
Aresult
11
00
70 0 ▶ return length($name) ? $class->specific($name) : $class->auto;
Branch: 2 branches
BranchTrueFalseTotal
length $name ? : 67 0 67
length $name ? : 0 0 0
71 }
72  
73 0 ▶ return $class->any if $type eq 'any' || $type eq 'required';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "any" or $type eq "required" 3 7 10
if $type eq "any" or $type eq "required" 0 0 0
Condition50%3/6
$type eq "any" or $type eq "required"
l!l&&r!l&&!r
217
$type eq "any" or $type eq "required"
l!l&&r!l&&!r
000
MC/DC100%2/2
$type eq "any" or $type eq "required"
$type eq "any" $type eq "required"
MC/DC0%0/2
$type eq "any" or $type eq "required"
$type eq "any" $type eq "required"
Truth table100%3/3
A or B
A= $type eq "any"B= $type eq "required"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $type eq "any"B= $type eq "required"
ABresult
1X1
011
000
74 0 ▶ return $class->auto if $type eq 'auto';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "auto" 2 5 7
if $type eq "auto" 0 0 0
75 0 ▶ return $class->none if $type eq 'none';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "none" 0 5 5
if $type eq "none" 0 0 0
76 0 return undef;
77 }
78  
79  
80 0 ▶sub from_openai { shift->from_hash(@_) }
Subroutine from_openai: not called CC=1 SCAR=6.9
Pod documented
81 0 ▶sub from_anthropic { shift->from_hash(@_) }
Subroutine from_anthropic: not called CC=1 SCAR=6.9
Pod documented
82  
83 # --- Serializers ---
84  
85 39 ▶sub to_openai {
Subroutine to_openai: called CC=7 SCAR=20.5
Pod documented
86 39 my ($self) = @_;
87 39 ▶ return 'required' if $self->type eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "any" 9 30 39
88 30 ▶ return 'auto' if $self->type eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "auto" 6 24 30
89 24 ▶ return 'none' if $self->type eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "none" 7 17 24
90 17 ▶ if ( $self->type eq 'tool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "tool") 17 0 17
91 17 ▶ return defined $self->name && length $self->name
Branch: 1 branch
BranchTrueFalseTotal
defined $self->name && length $self->name ? : 17 0 17
Condition33%1/3
defined $self->name and length $self->name
!ll&&!rl&&r
0017
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
92 ? { type => 'function', function => { name => $self->name } }
93 : 'auto';
94 }
95 0 return undef;
96 }
97  
98 28 ▶sub to_anthropic {
Subroutine to_anthropic: called CC=7 SCAR=20.5
Pod documented
99 28 my ($self) = @_;
100 28 ▶ return { type => 'any' } if $self->type eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "any" 4 24 28
101 24 ▶ return { type => 'auto' } if $self->type eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "auto" 5 19 24
102 19 ▶ return { type => 'none' } if $self->type eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "none" 4 15 19
103 15 ▶ if ( $self->type eq 'tool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "tool") 15 0 15
104 15 ▶ return defined $self->name && length $self->name
Branch: 1 branch
BranchTrueFalseTotal
defined $self->name && length $self->name ? : 15 0 15
Condition33%1/3
defined $self->name and length $self->name
!ll&&!rl&&r
0015
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
105 ? { type => 'tool', name => $self->name }
106 : { type => 'auto' };
107 }
108 0 return undef;
109 }
110  
111  
112 4 ▶sub to_perplexity {
Subroutine to_perplexity: called CC=3 SCAR=11.0
Pod documented
113 4 my ($self) = @_;
114 # Perplexity only accepts string forms: none / auto / required.
115 # Named-tool forcing is not supported on the wire — engine layer
116 # is expected to switch to a response_format-based path when a
117 # named tool is requested. We coerce here so callers that ignore
118 # capabilities still get *something* sensible.
119 4 ▶ return 'none' if $self->type eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "none" 1 3 4
120 3 ▶ return 'auto' if $self->type eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "auto" 1 2 3
121 2 return 'required';
122 }
123  
124  
125 18 ▶sub to_gemini {
Subroutine to_gemini: called CC=7 SCAR=19.7
Pod documented
126 18 my ($self) = @_;
127 # Gemini uses toolConfig.functionCallingConfig:
128 # { mode => AUTO|ANY|NONE, allowed_function_names => [...] }
129 # Gemini also offers a fifth mode, VALIDATED ("model decides, but validates a
130 # function call with constrained decoding") — deliberately NOT modeled here:
131 # the canonical vocabulary is none|auto|required|named, and VALIDATED ("auto,
132 # but a call it makes is schema-valid") has no canonical equivalent. If the
133 # canonical set ever grows a fifth policy, that is where it maps (karr k140).
134 18 ▶ return { functionCallingConfig => { mode => 'NONE' } } if $self->type eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "none" 3 15 18
135 15 ▶ return { functionCallingConfig => { mode => 'AUTO' } } if $self->type eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "auto" 6 9 15
136 9 ▶ return { functionCallingConfig => { mode => 'ANY' } } if $self->type eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "any" 3 6 9
137 6 ▶ if ( $self->type eq 'tool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "tool") 6 0 6
138 6 ▶ return { functionCallingConfig => { mode => 'AUTO' } }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->name and length $self->name 1 5 6
Condition66%2/3
defined $self->name and length $self->name
!ll&&!rl&&r
105
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
139 unless defined $self->name && length $self->name;
140 return {
141 5 functionCallingConfig => {
142 mode => 'ANY',
143 allowed_function_names => [ $self->name ],
144 },
145 };
146 }
147 0 return undef;
148 }
149  
150  
151 13 ▶sub to_responses {
Subroutine to_responses: called CC=7 SCAR=20.5
Pod documented
152 13 my ($self) = @_;
153 # Responses API uses flat {type => 'function', name => 'foo'} — no nested function wrapper
154 13 ▶ return 'auto' if $self->type eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "auto" 3 10 13
155 10 ▶ return 'none' if $self->type eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "none" 3 7 10
156 7 ▶ return 'required' if $self->type eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "any" 2 5 7
157 5 ▶ if ( $self->type eq 'tool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "tool") 5 0 5
158 5 ▶ return defined $self->name && length $self->name
Branch: 1 branch
BranchTrueFalseTotal
defined $self->name && length $self->name ? : 5 0 5
Condition33%1/3
defined $self->name and length $self->name
!ll&&!rl&&r
005
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
159 ? { type => 'function', name => $self->name }
160 : 'auto';
161 }
162 0 return undef;
163 }
164  
165  
166 5 ▶sub to_hash {
Subroutine to_hash: called CC=2 SCAR=6.9
Pod documented
167 5 my ($self) = @_;
168 5 ▶ return { type => $self->type, ( defined $self->name ? ( name => $self->name ) : () ) };
Branch: 1 branch
BranchTrueFalseTotal
defined $self->name ? : 3 2 5
169 }
170  
171  
172 # Make the object transparent to any JSON encoder configured with
173 # convert_blessed => 1 (the house default, see Langertha::Plugin::Langfuse).
174 # to_hash is the complete canonical representation, so this is a plain
175 # delegator — nothing is dropped.
176 2 ▶sub TO_JSON { shift->to_hash }
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod documented
177  
178 # --- Tag-driven dispatch ---
179  
180 # Maps a tool_wire_format tag to the per-format serializer. Only the wires that
181 # actually carry a tool_choice request parameter are listed: Ollama and Hermes
182 # have no wire-level tool_choice (Hermes forces via prompt injection), so to()
183 # croaks for them. Perplexity is not a tool_wire_format value — its named-tool
184 # request is rewritten to response_format by chat_f — so to_perplexity stays a
185 # standalone helper, off the tag dispatch; it is legacy (Sonar /chat/completions
186 # forms), the Agent API has no tool_choice (karr k233).
187 198 my %TO_METHOD = (
188 openai => 'to_openai',
189 anthropic => 'to_anthropic',
190 gemini => 'to_gemini',
191 responses => 'to_responses',
192 );
193  
194 71 ▶sub to {
Subroutine to: called CC=4 SCAR=14.3
Pod documented
195 71 my ( $self, $fmt ) = @_;
196 71 ▶ my $method = $TO_METHOD{ $fmt // '' }
Branch: 1 branch
BranchTrueFalseTotal
my $method = $TO_METHOD{$fmt // ""} or croak(q[Langertha::ToolChoice: unknown wire format '] . ($fmt // "") . q[']) 68 3 71
Condition50%2/4
$fmt // ""
l!l
710
$fmt // ""
l!l
30
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
197 or croak "Langertha::ToolChoice: unknown wire format '" . ( $fmt // '' ) . "'";
198 68 return $self->$method;
199 }
200  
201  
202 198 __PACKAGE__->meta->make_immutable;
203  
204  
205 198 1;
206  
207 __END__
« blib/lib/Langertha/CallResult.pm ↑ Summary blib/lib/Langertha/Role/Streaming.pm »