| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
builtin::blessed($val) and $val->isa(__PACKAGE__)
builtin::blessed($val) and $val->isa(__PACKAGE__)
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__)
Truth table0%0/3
A and B
A= builtin::blessed($val)B= $val->isa(__PACKAGE__)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if ( !ref($val) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | return $class->any if $val eq 'required' || $val eq 'any'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%3/6
$val eq "required" or $val eq "any"
$val eq "required" or $val eq "any"
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"
Truth table0%0/3
A or B
A= $val eq "required"B= $val eq "any"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | return $class->auto if $val eq 'auto'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | return $class->none if $val eq 'none'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | return undef unless ref($val) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my $type = $val->{type} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition25%1/4
$val->{"type"} // ""
$val->{"type"} // ""
MC/DC0%0/1
$val->{"type"} // ""
$val->{"type"}
MC/DC0%0/1
$val->{"type"} // ""
$val->{"type"}
Truth table50%1/2
A
A= $val->{"type"}
Truth table0%0/2
A
A= $val->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if ( $type eq 'function' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my $name = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ( ref( $val->{function} ) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | $name = $val->{function}{name} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition25%1/4
$val->{"function"}{"name"} // ""
$val->{"function"}{"name"} // ""
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"}
Truth table0%0/2
A
A= $val->{"function"}{"name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } elsif ( defined $val->{name} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $name = $val->{name} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/4
$val->{"name"} // ""
$val->{"name"} // ""
MC/DC0%0/1
$val->{"name"} // ""
$val->{"name"}
MC/DC0%0/1
$val->{"name"} // ""
$val->{"name"}
Truth table0%0/2
A
A= $val->{"name"}
Truth table0%0/2
A
A= $val->{"name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | return length($name) ? $class->specific($name) : $class->auto; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ( $type eq 'tool' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | my $name = $val->{name} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition25%1/4
$val->{"name"} // ""
$val->{"name"} // ""
MC/DC0%0/1
$val->{"name"} // ""
$val->{"name"}
MC/DC0%0/1
$val->{"name"} // ""
$val->{"name"}
Truth table50%1/2
A
A= $val->{"name"}
Truth table0%0/2
A
A= $val->{"name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | return length($name) ? $class->specific($name) : $class->auto; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | return $class->any if $type eq 'any' || $type eq 'required'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%3/6
$type eq "any" or $type eq "required"
$type eq "any" or $type eq "required"
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"
Truth table0%0/3
A or B
A= $type eq "any"B= $type eq "required"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | return $class->auto if $type eq 'auto'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | return $class->none if $type eq 'none'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 30 | ▶ | return 'auto' if $self->type eq 'auto'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 24 | ▶ | return 'none' if $self->type eq 'none'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 17 | ▶ | if ( $self->type eq 'tool' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 17 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 24 | ▶ | return { type => 'auto' } if $self->type eq 'auto'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 19 | ▶ | return { type => 'none' } if $self->type eq 'none'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 15 | ▶ | if ( $self->type eq 'tool' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 15 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 3 | ▶ | return 'auto' if $self->type eq 'auto'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 15 | ▶ | return { functionCallingConfig => { mode => 'AUTO' } } if $self->type eq 'auto'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 9 | ▶ | return { functionCallingConfig => { mode => 'ANY' } } if $self->type eq 'any'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 6 | ▶ | if ( $self->type eq 'tool' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 6 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 10 | ▶ | return 'none' if $self->type eq 'none'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 7 | ▶ | return 'required' if $self->type eq 'any'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 5 | ▶ | if ( $self->type eq 'tool' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 5 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%2/4
$fmt // ""
$fmt // ""
MC/DC0%0/1
$fmt // ""
$fmt
MC/DC0%0/1
$fmt // ""
$fmt
Truth table50%1/2
A
A= $fmt
Truth table50%1/2
A
A= $fmt
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||