| 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
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 20 | ▶ | if ( !ref($val) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 11 | ▶ | return $class->any if $val eq 'required' || $val eq 'any'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$val eq "required" or $val eq "any"
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"
| |||||||||||||||||||||||||||||
| 41 | 5 | ▶ | return $class->auto if $val eq 'auto'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 2 | ▶ | return $class->none if $val eq 'none'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | return undef; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 9 | ▶ | return undef unless ref($val) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 9 | ▶ | my $type = $val->{type} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$val->{"type"} // ""
MC/DC0%0/1
$val->{"type"} // ""
$val->{"type"}
Truth table50%1/2
A
A= $val->{"type"}
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 9 | ▶ | if ( $type eq 'function' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 3 | my $name = ''; | |||||||||||||||||||||||||||
| 51 | 3 | ▶ | if ( ref( $val->{function} ) eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 52 | 3 | ▶ | $name = $val->{function}{name} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$val->{"function"}{"name"} // ""
MC/DC0%0/1
$val->{"function"}{"name"} // ""
$val->{"function"}{"name"}
Truth table50%1/2
A
A= $val->{"function"}{"name"}
| |||||||||||||||||||||||||||||
| 53 | } elsif ( defined $val->{name} ) { | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $name = $val->{name} // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$val->{"name"} // ""
MC/DC0%0/1
$val->{"name"} // ""
$val->{"name"}
Truth table0%0/2
A
A= $val->{"name"}
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 3 | ▶ | return length($name) ? $class->specific($name) : $class->auto; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 6 | ▶ | if ( $type eq 'tool' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 5 | ▶ | my $name = $val->{name} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$val->{"name"} // ""
MC/DC0%0/1
$val->{"name"} // ""
$val->{"name"}
Truth table50%1/2
A
A= $val->{"name"}
| |||||||||||||||||||||||||||||
| 61 | 5 | ▶ | return length($name) ? $class->specific($name) : $class->auto; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | return $class->any if $type eq 'any' || $type eq 'required'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$type eq "any" or $type eq "required"
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"
| |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | return $class->auto if $type eq 'auto'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | return $class->none if $type eq 'none'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 78 | 8 | ▶ | return 'auto' if $self->type eq 'auto'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 6 | ▶ | return 'none' if $self->type eq 'none'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 4 | ▶ | if ( $self->type eq 'tool' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 4 | ▶ | return defined $self->name && length $self->name | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->name and length $self->name
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 91 | 7 | ▶ | return { type => 'auto' } if $self->type eq 'auto'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 6 | ▶ | return { type => 'none' } if $self->type eq 'none'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 4 | ▶ | if ( $self->type eq 'tool' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 4 | ▶ | return defined $self->name && length $self->name | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->name and length $self->name
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 109 | 3 | ▶ | return 'auto' if $self->type eq 'auto'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 118 | 4 | ▶ | return { functionCallingConfig => { mode => 'AUTO' } } if $self->type eq 'auto'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 3 | ▶ | return { functionCallingConfig => { mode => 'ANY' } } if $self->type eq 'any'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 2 | ▶ | if ( $self->type eq 'tool' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 2 | ▶ | return { functionCallingConfig => { mode => 'AUTO' } } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->name and length $self->name
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 137 | 1 | ▶ | return 'none' if $self->type eq 'none'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 1 | ▶ | return 'required' if $self->type eq 'any'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 1 | ▶ | if ( $self->type eq 'tool' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 1 | ▶ | return defined $self->name && length $self->name | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->name and length $self->name
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||