blib/lib/Langertha/Tool.pm

filter: Statementstmt 99.5% 219 / 220 Branchbran 84.7% 144 / 170 Conditioncond 75.6% 127 / 168 MC/DCmcdc 51.1% 44 / 86 Subroutinesub 100% 35 / 35 Podpod 21.0% 4 / 19 totaltotal 82.0% 573 / 698 CC 155 SCAR 24.7
CC
155
Cov
88%
classify
26.3
_builtin_kind
21.0
_is_wire_function
13.2
CRAP 11.9
« blib/lib/Langertha/Role/Streaming.pm ↑ Summary blib/lib/Langertha/Plugin/Langfuse.pm »
1 package Langertha::Tool;
2 # ABSTRACT: Immutable canonical tool definition with cross-provider format conversion
3 189 our $VERSION = '0.503';
4 189 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 189 ▶use Carp qw( croak carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 189 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 189 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 189 has name => (
10 is => 'ro',
11 isa => 'Str',
12 required => 1,
13 );
14  
15 189 has description => (
16 is => 'ro',
17 isa => 'Str',
18 default => '',
19 );
20  
21 has input_schema => (
22 is => 'ro',
23 isa => 'HashRef',
24 3 ▶ default => sub { { type => 'object', properties => {} } },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 189 );
26  
27 14 ▶sub _empty_schema { { type => 'object', properties => {} } }
Subroutine _empty_schema: called CC=1 SCAR=0.0
28  
29 # --- Constructors from wire-format hashes ---
30  
31 40 ▶sub from_openai {
Subroutine from_openai: called CC=9 SCAR=23.3
Pod undocumented
32 40 my ($class, $hash) = @_;
33 40 ▶ return undef unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 40 40
34 40 ▶ return undef unless ($hash->{type} // '') eq 'function';
Branch: 1 branch
BranchTrueFalseTotal
unless ($hash->{"type"} // "") eq "function" 0 40 40
Condition50%1/2
$hash->{"type"} // ""
l!l
400
MC/DC0%0/1
$hash->{"type"} // ""
$hash->{"type"}
Truth table50%1/2
A
A= $hash->{"type"}
Aresult
11
00
35 # Chat Completions nests the function fields under `function`; the
36 # Responses API's flat form puts name/description/parameters directly on
37 # the tool hash (k217). Normalize both here (ADR 0018 tier 1: the
38 # value-object inbound door), so the nested shape still wins whenever it
39 # is actually present.
40 40 ▶ my $fn = ref( $hash->{function} ) eq 'HASH' ? $hash->{function} : $hash;
Branch: 1 branch
BranchTrueFalseTotal
ref $hash->{'function'} eq 'HASH' ? : 26 14 40
41 40 ▶ my $name = $fn->{name} // '';
Condition50%1/2
$fn->{"name"} // ""
l!l
400
MC/DC0%0/1
$fn->{"name"} // ""
$fn->{"name"}
Truth table50%1/2
A
A= $fn->{"name"}
Aresult
11
00
42 40 ▶ return undef unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 40 40
43 return $class->new(
44 name => $name,
45 description => ( $fn->{description} // '' ),
46 40 ▶ input_schema => ( $fn->{parameters} || $class->_empty_schema ),
Condition80%4/5
$fn->{"description"} // ""
l!l
373
$fn->{'parameters'} || $class->_empty_schema
l!l&&r!l&&!r
3730
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
47 );
48 }
49  
50 66 ▶sub from_anthropic {
Subroutine from_anthropic: called CC=7 SCAR=20.7
Pod undocumented
51 66 my ($class, $hash) = @_;
52 66 ▶ return undef unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 66 66
53 66 ▶ my $name = $hash->{name} // '';
Condition50%1/2
$hash->{"name"} // ""
l!l
660
MC/DC0%0/1
$hash->{"name"} // ""
$hash->{"name"}
Truth table50%1/2
A
A= $hash->{"name"}
Aresult
11
00
54 66 ▶ return undef unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 66 66
55 return $class->new(
56 name => $name,
57 description => ( $hash->{description} // '' ),
58 66 ▶ input_schema => ( $hash->{input_schema} || $hash->{parameters} || $class->_empty_schema ),
Condition75%6/8
$hash->{"description"} // ""
l!l
624
$hash->{'input_schema'} || $hash->{'parameters'} || $class->_empty_schema
l!l&&r!l&&!r
55110
$hash->{"input_schema"} or $hash->{"parameters"}
l!l&&r!l&&!r
55011
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 table50%2/4
A or B || C
A= $hash->{"input_schema"}B= $hash->{"parameters"}C= $class->_empty_schema
ABCresult
1XX1
01X1
0011
0000
59 );
60 }
61  
62 # MCP server tool definition: name + description + inputSchema (camelCase).
63 745 ▶sub from_mcp {
Subroutine from_mcp: called CC=7 SCAR=23.7
Pod undocumented
64 745 my ($class, $hash) = @_;
65 745 ▶ return undef unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 745 745
66 745 ▶ my $name = $hash->{name} // '';
Condition50%1/2
$hash->{"name"} // ""
l!l
7450
MC/DC0%0/1
$hash->{"name"} // ""
$hash->{"name"}
Truth table50%1/2
A
A= $hash->{"name"}
Aresult
11
00
67 745 ▶ return undef unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 745 745
68 return $class->new(
69 name => $name,
70 description => ( $hash->{description} // '' ),
71 745 ▶ input_schema => ( $hash->{inputSchema} || $hash->{input_schema} || $class->_empty_schema ),
Condition37%3/8
$hash->{"description"} // ""
l!l
7450
$hash->{'inputSchema'} || $hash->{'input_schema'} || $class->_empty_schema
l!l&&r!l&&!r
74500
$hash->{"inputSchema"} or $hash->{"input_schema"}
l!l&&r!l&&!r
74500
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 table50%1/2
A
A= $hash->{"description"}
Aresult
11
00
Truth table25%1/4
A or B || C
A= $hash->{"inputSchema"}B= $hash->{"input_schema"}C= $class->_empty_schema
ABCresult
1XX1
01X1
0011
0000
72 );
73 }
74  
75 # Gemini functionDeclarations: name + description + parameters (flat), or
76 # the JSON-Schema parametersJsonSchema (both spellings, ADR 0018).
77 17 ▶sub from_gemini {
Subroutine from_gemini: called CC=8 SCAR=22.3
Pod undocumented
78 17 my ($class, $hash) = @_;
79 17 ▶ return undef unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 17 17
80 17 ▶ my $name = $hash->{name} // '';
Condition50%1/2
$hash->{"name"} // ""
l!l
170
MC/DC0%0/1
$hash->{"name"} // ""
$hash->{"name"}
Truth table50%1/2
A
A= $hash->{"name"}
Aresult
11
00
81 17 ▶ return undef unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 17 17
82 return $class->new(
83 name => $name,
84 description => ( $hash->{description} // '' ),
85 input_schema => ( $hash->{parameters} || $hash->{parametersJsonSchema}
86 17 ▶ || $hash->{parameters_json_schema} || $class->_empty_schema ),
Condition72%8/11
$hash->{"description"} // ""
l!l
143
$hash->{'parameters'} || $hash->{'parametersJsonSchema'} || $hash->{'parameters_json_schema'} || $class->_empty_schema
l!l&&r!l&&!r
1700
$hash->{"parameters"} or $hash->{"parametersJsonSchema"} or $hash->{"parameters_json_schema"}
l!l&&r!l&&!r
1340
$hash->{"parameters"} or $hash->{"parametersJsonSchema"}
l!l&&r!l&&!r
944
MC/DC100%1/1
$hash->{"description"} // ""
$hash->{"description"}
MC/DC0%0/4
$hash->{'parameters'} || $hash->{'parametersJsonSchema'} || $hash->{'parameters_json_schema'} || $class->_empty_schema
$hash->{"parameters"} $hash->{"parametersJsonSchema"} $hash->{"parameters_json_schema"} $class->_empty_schema
Truth table100%2/2
A
A= $hash->{"description"}
Aresult
11
00
Truth table60%3/5
A or B or C || D
A= $hash->{"parameters"}B= $hash->{"parametersJsonSchema"}C= $hash->{"parameters_json_schema"}D= $class->_empty_schema
ABCDresult
1XXX1
01XX1
001X1
00011
00000
87 );
88 }
89  
90 # Generic: figure out the wire shape and route accordingly. Order matters —
91 # we test the most specific markers first.
92 1458 ▶sub from_hash {
Subroutine from_hash: called CC=11 SCAR=24.3
Pod documented
93 1458 my ($class, $hash) = @_;
94 1458 ▶ return $hash if ref($hash) && eval { $hash->isa(__PACKAGE__) };
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash and eval { do { $hash->isa(__PACKAGE__) } } 35 1423 1458
Condition66%2/3
ref $hash and eval {
!ll&&!rl&&r
0142335
MC/DC50%1/2
ref $hash and eval { do { $hash->isa(__PACKAGE__) } }
ref $hash eval { do { $hash->isa(__PACKAGE__) } }
Truth table66%2/3
A and B
A= ref $hashB= eval { do { $hash->isa(__PACKAGE__) } }
ABresult
0X0
100
111
95 croak "Langertha::Tool: a Langertha::ServerTool is not a function tool; "
96 . "format_list and the engine's tools list take it, Langertha::Tool does not"
97 1423 ▶ if ref($hash) && eval { $hash->isa('Langertha::ServerTool') };
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash and eval { do { $hash->isa("Langertha::ServerTool") } } 0 1423 1423
Condition33%1/3
ref $hash and eval {
!ll&&!rl&&r
014230
MC/DC0%0/2
ref $hash and eval { do { $hash->isa("Langertha::ServerTool") } }
ref $hash eval { do { $hash->isa("Langertha::ServerTool") } }
Truth table33%1/3
A and B
A= ref $hashB= eval { do { $hash->isa("Langertha::ServerTool") } }
ABresult
0X0
100
111
98 1423 ▶ return undef unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 1423 1423
99 1423 $class->_croak_unless_function_tool($hash);
100 865 ▶ return $class->from_openai($hash) if ( $hash->{type} // '' ) eq 'function';
Branch: 1 branch
BranchTrueFalseTotal
if ($hash->{"type"} // "") eq "function" 39 826 865
Condition100%2/2
$hash->{"type"} // ""
l!l
48817
MC/DC100%1/1
$hash->{"type"} // ""
$hash->{"type"}
Truth table100%2/2
A
A= $hash->{"type"}
Aresult
11
00
101 826 ▶ return $class->from_mcp($hash) if ref( $hash->{inputSchema} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash->{"inputSchema"} eq "HASH" 744 82 826
102 82 ▶ return $class->from_anthropic($hash) if ref( $hash->{input_schema} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash->{"input_schema"} eq "HASH" 54 28 82
103 return $class->from_gemini($hash)
104 28 ▶ if grep { ref( $hash->{$_} ) eq 'HASH' } qw( parameters parametersJsonSchema parameters_json_schema );
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $hash->{$_} eq "HASH";} "parameters", "parametersJsonSchema", "parameters_json_schema" 17 11 28
105 # Last resort: name-only / schemaless
106 11 return $class->from_anthropic($hash);
107 }
108  
109 # Only function tools pass this door; since k210 (ADR 0001) everything else
110 # croaks instead of being dropped or turned into a function tool. The decision
111 # is classify()'s -- the one source of truth, also used by
112 # Role::ResponsesCompatible and by sibling callers that must not croak.
113 1423 ▶sub _croak_unless_function_tool {
Subroutine _croak_unless_function_tool: called CC=7 SCAR=19.5
114 1423 my ($class, $hash) = @_;
115 1423 my ( $category, $wire, $label ) = $class->classify($hash);
116 1423 ▶ return if $category eq 'function';
Branch: 1 branch
BranchTrueFalseTotal
if $category eq "function" 865 558 1423
117 558 my $tail = '(refusing to drop it or send it as a function tool)';
118 558 ▶ croak "Langertha::Tool: '$label' is a server-side tool ($wire), not a function "
Branch: 2 branches
BranchTrueFalseTotal
if $category eq "server" 304 254 558
$wire eq 'responses' ? : 121 183 304
119 . "tool $tail; "
120 . ( $wire eq 'responses'
121 ? "pass it in the tools list of an engine that supports('server_tools'), "
122 . "or wrap it in Langertha::ServerTool"
123 : "Langertha::ServerTool does not support the $wire wire yet" )
124 if $category eq 'server';
125 254 ▶ croak "Langertha::Tool: '$label' is a client-executed built-in tool ($wire), "
Branch: 1 branch
BranchTrueFalseTotal
if $category eq "client_builtin" 169 85 254
126 . "not a server tool, and Langertha cannot run it $tail"
127 if $category eq 'client_builtin';
128 croak "Langertha::Tool: unsupported tool type '$label': not a function tool $tail"
129 85 ▶ if length( $hash->{type} // '' );
Branch: 1 branch
BranchTrueFalseTotal
if length($hash->{"type"} // "") 44 41 85
Condition100%2/2
$hash->{"type"} // ""
l!l
4441
MC/DC100%1/1
$hash->{"type"} // ""
$hash->{"type"}
Truth table100%2/2
A
A= $hash->{"type"}
Aresult
11
00
130 41 croak "Langertha::Tool: tool hash has no type and no name (keys: $label): "
131 . "not a function tool $tail";
132 }
133  
134 # Built-in tools, recognised explicitly per wire (spec k206 section 3.4,
135 # llm-advisor against the provider references, 2026-09-25). Typed wires match
136 # on `type` (plus `execution` / `environment` where that decides who runs it);
137 # Gemini's built-ins are keyed, not typed ({ google_search => {} }), in both
138 # snake and camel case (ADR 0018). Documentation-derived, not capture-verified.
139 189 my %RESPONSES_SERVER_TYPE = map { $_ => 1 } qw(
140 web_search file_search code_interpreter image_generation mcp
141 x_search collections_search
142 );
143 189 my %RESPONSES_CLIENT_TYPE = map { $_ => 1 } qw(
144 local_shell computer computer_use_preview apply_patch
145 );
146 189 my @GEMINI_SERVER_KEY = qw(
147 google_search googleSearch google_search_retrieval googleSearchRetrieval
148 code_execution codeExecution url_context urlContext google_maps googleMaps
149 enterprise_web_search enterpriseWebSearch file_search fileSearch retrieval
150 );
151 189 my @GEMINI_CLIENT_KEY = qw( computer_use computerUse );
152  
153 # The inbound mirror of %RESPONSES_CLIENT_TYPE: Responses output[] items the
154 # CLIENT must answer and Langertha does not map into Response.tool_calls
155 # (llm-advisor must-change 1 on k206, OpenAI create-response reference). A
156 # known one croaks rather than being skipped -- skipped, a tool loop ends as if
157 # the model were done and a chat_f caller never learns it is waiting. A
158 # tool_search_call is client-actionable only with execution => 'client'.
159 # Unknown item types stay skipped (values open); ADR 0030.
160 189 my %RESPONSES_CLIENT_ITEM = (
161 custom_tool_call => 'a custom tool call',
162 computer_call => 'a computer-use action',
163 local_shell_call => 'a local shell command',
164 apply_patch_call => 'a patch to apply',
165 mcp_approval_request => "an MCP approval request; send the mcp tool with require_approval => 'never'",
166 tool_search_call => 'a client-side tool search',
167 );
168  
169 173 ▶sub _croak_on_client_item {
Subroutine _croak_on_client_item: called CC=7 SCAR=20.7
170 173 my ( $class, $item ) = @_;
171 173 ▶ return unless ref $item eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item eq "HASH" 0 173 173
172 173 ▶ my $type = $item->{type} // '';
Condition50%1/2
$item->{"type"} // ""
l!l
1730
MC/DC0%0/1
$item->{"type"} // ""
$item->{"type"}
Truth table50%1/2
A
A= $item->{"type"}
Aresult
11
00
173 173 ▶ my $what = $RESPONSES_CLIENT_ITEM{$type} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $what = $RESPONSES_CLIENT_ITEM{$type} or return 13 160 173
174 13 ▶ return if $type eq 'tool_search_call' && ( $item->{execution} // '' ) ne 'client';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "tool_search_call" and ($item->{"execution"} // "") ne "client" 0 13 13
Condition60%3/5
$type eq "tool_search_call" and ($item->{"execution"} // "") ne "client"
!ll&&!rl&&r
1120
$item->{"execution"} // ""
l!l
20
MC/DC50%1/2
$type eq "tool_search_call" and ($item->{"execution"} // "") ne "client"
$type eq "tool_search_call" $item->{"execution"}
Truth table66%2/3
A and B
A= $type eq "tool_search_call"B= $item->{"execution"}
ABresult
0X0
100
111
175 13 croak "Langertha: the reply contains a '$type' output item ($what) that the "
176 . "client must answer, and Langertha cannot; refusing to end the turn as if the "
177 . "model were done";
178 }
179  
180 # ($category, $wire) for a recognised built-in, else ().
181 2797 ▶sub _builtin_kind {
Subroutine _builtin_kind: called CC=21 SCAR=30.4
182 2797 my ($hash) = @_;
183 2797 ▶ my $type = $hash->{type} // '';
Condition100%2/2
$hash->{"type"} // ""
l!l
11701627
MC/DC100%1/1
$hash->{"type"} // ""
$hash->{"type"}
Truth table100%2/2
A
A= $hash->{"type"}
Aresult
11
00
184 2797 ▶ if ( length $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $type) 1170 1627 2797
185 1170 ▶ if ( $type eq 'tool_search' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tool_search") 77 1093 1170
186 77 ▶ return ( ( $hash->{execution} // '' ) eq 'client' ? 'client_builtin' : 'server', 'responses' );
Branch: 1 branch
BranchTrueFalseTotal
($hash->{'execution'} // '') eq 'client' ? : 27 50 77
Condition100%2/2
$hash->{"execution"} // ""
l!l
5126
MC/DC100%1/1
$hash->{"execution"} // ""
$hash->{"execution"}
Truth table100%2/2
A
A= $hash->{"execution"}
Aresult
11
00
187 }
188 1093 ▶ if ( $type eq 'shell' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "shell") 128 965 1093
189 # Hosted only in one of the two container environments; a bare, local
190 # or unrecognised environment runs on the client (spec k206 3.4, Q3).
191 128 ▶ my $env = ref $hash->{environment} eq 'HASH' ? ( $hash->{environment}{type} // '' ) : '';
Branch: 1 branch
BranchTrueFalseTotal
ref $hash->{'environment'} eq 'HASH' ? : 102 26 128
Condition50%1/2
$hash->{"environment"}{"type"} // ""
l!l
1020
MC/DC0%0/1
$hash->{"environment"}{"type"} // ""
$hash->{"environment"}{"type"}
Truth table50%1/2
A
A= $hash->{"environment"}{"type"}
Aresult
11
00
192 128 ▶ return ( ( $env eq 'container_auto' || $env eq 'container_reference' )
Branch: 1 branch
BranchTrueFalseTotal
$env eq 'container_auto' || $env eq 'container_reference' ? : 50 78 128
Condition100%3/3
$env eq "container_auto" or $env eq "container_reference"
l!l&&r!l&&!r
262478
MC/DC100%2/2
$env eq "container_auto" or $env eq "container_reference"
$env eq "container_auto" $env eq "container_reference"
Truth table100%3/3
A or B
A= $env eq "container_auto"B= $env eq "container_reference"
ABresult
1X1
011
000
193 ? 'server' : 'client_builtin', 'responses' );
194 }
195 return ( server => 'responses' )
196 965 ▶ if $RESPONSES_SERVER_TYPE{$type}
Branch: 1 branch
BranchTrueFalseTotal
if $RESPONSES_SERVER_TYPE{$type} or $type =~ /\Aweb_search_(?:preview|\d{4}_\d{2}_\d{2}\z)/ 337 628 965
Condition100%3/3
$RESPONSES_SERVER_TYPE{$type} or $type =~ /\Aweb_search_(?:preview|\d{4}_\d{2}_\d{2}\z)/
l!l&&r!l&&!r
28750628
MC/DC100%2/2
$RESPONSES_SERVER_TYPE{$type} or $type =~ /\Aweb_search_(?:preview|\d{4}_\d{2}_\d{2}\z)/
$RESPONSES_SERVER_TYPE{$type} $type =~ /\Aweb_search_(?:preview|\d{4}_\d{2}_\d{2}\z)/
Truth table100%3/3
A or B
A= $RESPONSES_SERVER_TYPE{$type}B= $type =~ /\Aweb_search_(?:preview|\d{4}_\d{2}_\d{2}\z)/
ABresult
1X1
011
000
197 || $type =~ /\Aweb_search_(?:preview|\d{4}_\d{2}_\d{2}\z)/;
198 628 ▶ return ( client_builtin => 'responses' ) if $RESPONSES_CLIENT_TYPE{$type};
Branch: 1 branch
BranchTrueFalseTotal
if $RESPONSES_CLIENT_TYPE{$type} 106 522 628
199 522 ▶ return ( server => 'anthropic' )
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "mcp_toolset" or $type =~ /\A(?:web_search|web_fetch|code_execution|tool_search_tool_\w+?)_\d{8}\z/ 138 384 522
Condition100%3/3
$type eq "mcp_toolset" or $type =~ /\A(?:web_search|web_fetch|code_execution|tool_search_tool_\w+?)_\d{8}\z/
l!l&&r!l&&!r
26112384
MC/DC100%2/2
$type eq "mcp_toolset" or $type =~ /\A(?:web_search|web_fetch|code_execution|tool_search_tool_\w+?)_\d{8}\z/
$type eq "mcp_toolset" $type =~ /\A(?:web_search|web_fetch|code_execution|tool_search_tool_\w+?)_\d{8}\z/
Truth table100%3/3
A or B
A= $type eq "mcp_toolset"B= $type =~ /\A(?:web_search|web_fetch|code_execution|tool_search_tool_\w+?)_\d{8}\z/
ABresult
1X1
011
000
200 if $type eq 'mcp_toolset'
201 || $type =~ /\A(?:web_search|web_fetch|code_execution|tool_search_tool_\w+?)_\d{8}\z/;
202 384 ▶ return ( client_builtin => 'anthropic' )
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\A(?:bash|text_editor|computer|memory)_\d{8}\z/ 104 280 384
203 if $type =~ /\A(?:bash|text_editor|computer|memory)_\d{8}\z/;
204 280 return ();
205 }
206 1627 ▶ for my $key (@GEMINI_SERVER_KEY) { return ( server => 'gemini' ) if exists $hash->{$key} }
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{$key} 230 22333 22563
207 1397 ▶ for my $key (@GEMINI_CLIENT_KEY) { return ( client_builtin => 'gemini' ) if exists $hash->{$key} }
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{$key} 24 2746 2770
208 1373 return ();
209 }
210  
211  
212 189 my %KNOWN_FMT = map { $_ => 1 } qw( openai anthropic gemini ollama responses mcp hermes );
213 189 my %CARPED_FMT;
214  
215 2802 ▶sub classify {
Subroutine classify: called CC=26 SCAR=32.7
Pod documented
216 2802 my ( $class, $hash, $fmt ) = @_;
217 2802 ▶ if ( defined $fmt && !$KNOWN_FMT{$fmt} && !$CARPED_FMT{$fmt}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fmt and not $KNOWN_FMT{$fmt} and not $CARPED_FMT{$fmt}++) 1 2801 2802
Condition100%6/6
defined $fmt and not $KNOWN_FMT{$fmt} and not $CARPED_FMT{$fmt}++
!ll&&!rl&&r
280011
defined $fmt and not $KNOWN_FMT{$fmt}
!ll&&!rl&&r
179910012
MC/DC100%3/3
defined $fmt and not $KNOWN_FMT{$fmt} and not $CARPED_FMT{$fmt}++
defined $fmt not $KNOWN_FMT{$fmt} not $CARPED_FMT{$fmt}++
Truth table100%4/4
A and not(B) and not(C)
A= defined $fmtB= not $KNOWN_FMT{$fmt}C= not $CARPED_FMT{$fmt}++
ABCresult
0XX0
10X0
1100
1111
218 1 carp "Langertha::Tool->classify: unknown tool_wire_format '$fmt'; "
219 . "every built-in counts as foreign for it";
220 }
221 2802 my @none = ( undef, undef );
222 2802 ▶ if ( ref($hash) && eval { $hash->isa(__PACKAGE__) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash and eval { do { $hash->isa(__PACKAGE__) } }) 2 2800 2802
Condition100%3/3
ref $hash and eval {
!ll&&!rl&&r
227982
MC/DC100%2/2
ref $hash and eval { do { $hash->isa(__PACKAGE__) } }
ref $hash eval { do { $hash->isa(__PACKAGE__) } }
Truth table100%3/3
A and B
A= ref $hashB= eval { do { $hash->isa(__PACKAGE__) } }
ABresult
0X0
100
111
223 2 ▶ return wantarray ? ( 'function', @none ) : 'function';
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
224 }
225 2800 ▶ unless ( ref($hash) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $hash eq "HASH") 3 2797 2800
226 3 ▶ return wantarray ? ( 'unknown', undef, ref($hash) || 'non-reference' ) : 'unknown';
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 3 3
ref $hash or "non-reference" 0 0 0
227 }
228 2797 ▶ my $type = $hash->{type} // '';
Condition100%2/2
$hash->{"type"} // ""
l!l
11701627
MC/DC100%1/1
$hash->{"type"} // ""
$hash->{"type"}
Truth table100%2/2
A
A= $hash->{"type"}
Aresult
11
00
229 2797 my ( $category, $wire ) = _builtin_kind($hash);
230 my $label = length $type ? $type
231 2797 ▶ : $category ? ( grep { exists $hash->{$_} } @GEMINI_SERVER_KEY, @GEMINI_CLIENT_KEY )[0]
Branch: 2 branches
BranchTrueFalseTotal
length $type ? : 1170 1627 2797
$category ? : 254 1373 1627
232 : undef;
233 2797 ▶ if ($category) {
Branch: 2 branches
BranchTrueFalseTotal
if ($category) { } 1144 1653 2797
elsif ($type eq 'function' or $type eq 'custom' and ref $hash->{'input_schema'} eq 'HASH' or $type eq '' and not ref $hash->{'name'} and length($hash->{'name'} // '')) { } 1468 185 1653
234 1144 ▶ $category = 'foreign' if defined $fmt && $wire ne $fmt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fmt and $wire ne $fmt 197 947 1144
Condition100%3/3
defined $fmt and $wire ne $fmt
!ll&&!rl&&r
712235197
MC/DC100%2/2
defined $fmt and $wire ne $fmt
defined $fmt $wire ne $fmt
Truth table100%3/3
A and B
A= defined $fmtB= $wire ne $fmt
ABresult
0X0
100
111
235 }
236 ▶ elsif ( $type eq 'function'
Condition94%16/17
$type eq "function" or $type eq "custom" and ref $hash->{"input_schema"} eq "HASH" or $type eq "" and not ref $hash->{"name"} and length($hash->{"name"} // "")
l!l&&r!l&&!r
1791289185
$type eq "function" or $type eq "custom" and ref $hash->{"input_schema"} eq "HASH"
l!l&&r!l&&!r
164151474
$type eq "custom" and ref $hash->{"input_schema"} eq "HASH"
!ll&&!rl&&r
14492515
$type eq "" and not ref $hash->{"name"} and length($hash->{"name"} // "")
!ll&&!rl&&r
101841289
$type eq "" and not ref $hash->{"name"}
!ll&&!rl&&r
10101373
$hash->{"name"} // ""
l!l
128984
MC/DC83%5/6
$type eq "function" or $type eq "custom" and ref $hash->{"input_schema"} eq "HASH" or $type eq "" and not ref $hash->{"name"} and length($hash->{"name"} // "")
$type eq "function" $type eq "custom" ref $hash->{"input_schema"} eq "HASH" $type eq "" not ref $hash->{"name"} $hash->{"name"}
Truth table60%6/10
A or B and C or D and not(E) and F
A= $type eq "function"B= $type eq "custom"C= ref $hash->{"input_schema"} eq "HASH"D= $type eq ""E= not ref $hash->{"name"}F= $hash->{"name"}
ABCDEFresult
1XXXXX1
011XXX1
00X1111
0101111
00X0XX0
00X10X0
00X1100
0100XX0
01010X0
0101100
237 || ( $type eq 'custom' && ref( $hash->{input_schema} ) eq 'HASH' )
238 || ( $type eq '' && !ref( $hash->{name} ) && length( $hash->{name} // '' ) ) ) {
239 1468 $category = 'function';
240 1468 $label = undef;
241 }
242 else {
243 185 $category = 'unknown';
244 185 ▶ $label //= join( ',', sort keys %$hash ) || '(empty)';
Condition80%4/5
$label //= join(",", sort(keys %$hash)) || "(empty)"
l!l&&r!l&&!r
101840
join(',', sort(keys %$hash)) || '(empty)'
l!l
5925
MC/DC0%0/2
$label //= join(",", sort(keys %$hash)) || "(empty)"
$label join(',', sort(keys %$hash))
Truth table66%2/3
A //= B
A= $labelB= join(',', sort(keys %$hash))
ABresult
1X1
011
000
245 }
246 2797 ▶ return wantarray ? ( $category, $wire, $label ) : $category;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1617 1180 2797
247 }
248  
249 # Build from a list of any-shape tool definitions. A plain hash that is not a
250 # function tool croaks in from_hash (k210). Anything that is not a plain HASH
251 # and not a Langertha::Tool -- a string, an array ref, a blessed non-Tool
252 # object even if it is hash-based -- is still skipped silently.
253 1272 ▶sub from_list {
Subroutine from_list: called CC=4 SCAR=14.1
Pod undocumented
254 1272 my ($class, $list) = @_;
255 1272 ▶ return [] unless ref($list) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 0 1272 1272
256 1272 my @out;
257 1272 for my $item (@$list) {
258 1324 my $tool = $class->from_hash($item);
259 814 ▶ push @out, $tool if $tool;
Branch: 1 branch
BranchTrueFalseTotal
if $tool 814 0 814
260 }
261 762 return \@out;
262 }
263  
264 # --- Serializers to wire-format hashes ---
265  
266 162 ▶sub to_openai {
Subroutine to_openai: called CC=1 SCAR=0.0
Pod undocumented
267 162 my ($self) = @_;
268 return {
269 162 type => 'function',
270 function => {
271 name => $self->name,
272 description => $self->description,
273 parameters => $self->input_schema,
274 },
275 };
276 }
277  
278 83 ▶sub to_anthropic {
Subroutine to_anthropic: called CC=2 SCAR=6.9
Pod undocumented
279 83 my ($self) = @_;
280 83 my $schema = $self->input_schema;
281 return {
282 83 ▶ name => $self->name,
Branch: 1 branch
BranchTrueFalseTotal
_schema_is_strict($schema) ? : 1 82 83
283 description => $self->description,
284 input_schema => $schema,
285 # Strict tool use (GA, no beta header): guarantees tool_use.input validates
286 # exactly against input_schema. Anthropic requires a closed schema for it —
287 # additionalProperties:false plus a non-empty required list — so we only
288 # emit strict:true where the schema author opted into that shape, and stay
289 # silent (and lenient) otherwise. Emitting strict on an open schema 400s.
290 ( _schema_is_strict($schema) ? ( strict => JSON->true ) : () ),
291 };
292 }
293  
294 # True when input_schema is closed enough for Anthropic strict tool use:
295 # additionalProperties explicitly false and a non-empty required array.
296 83 ▶sub _schema_is_strict {
Subroutine _schema_is_strict: called CC=6 SCAR=18.0
297 83 my ($schema) = @_;
298 83 ▶ return 0 unless ref($schema) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 83 83
299 83 ▶ return 0 unless exists $schema->{additionalProperties};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{"additionalProperties"} 76 7 83
300 7 ▶ return 0 if $schema->{additionalProperties}; # true / truthy -> open schema
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"additionalProperties"} 5 2 7
301 2 ▶ return 0 unless ref($schema->{required}) eq 'ARRAY' && @{$schema->{required}};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema->{"required"} eq "ARRAY" and @{$schema->{"required"};} 1 1 2
Condition66%2/3
ref $schema->{"required"} eq "ARRAY" and @{$schema->{"required"};}
!ll&&!rl&&r
101
MC/DC50%1/2
ref $schema->{"required"} eq "ARRAY" and @{$schema->{"required"};}
ref $schema->{"required"} eq "ARRAY" @{$schema->{"required"};}
Truth table66%2/3
A and B
A= ref $schema->{"required"} eq "ARRAY"B= @{$schema->{"required"};}
ABresult
0X0
100
111
302 1 return 1;
303 }
304  
305 32 ▶sub to_ollama { $_[0]->to_openai }
Subroutine to_ollama: called CC=1 SCAR=0.0
Pod undocumented
306  
307 # Gemini: the schema goes out as parametersJsonSchema (v1beta), which takes JSON
308 # Schema as-is -- `parameters` is an OpenAPI-subset proto that 400s on MCP's
309 # additionalProperties, $ref/$defs, const. The two are mutually exclusive, so
310 # `parameters` is never sent; no sanitizer. Only a top-level $schema is dropped,
311 # and a tool without arguments declares no schema at all (karr k330, ADR 0001).
312 189 my %NO_ARG_KEY = map { $_ => 1 } qw( type properties required additionalProperties );
313  
314 86 ▶sub _gemini_schema {
Subroutine _gemini_schema: called CC=5 SCAR=16.3
315 86 my ($self) = @_;
316 86 my $schema = $self->input_schema;
317 86 ▶ return undef unless ref $schema eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 86 86
318 86 my %schema = %$schema;
319 86 delete $schema{'$schema'};
320 86 my $props = $schema{properties};
321 my $no_args = !( ref $props eq 'HASH' && %$props )
322 86 ▶ && !grep { !$NO_ARG_KEY{$_} } keys %schema;
Condition66%4/6
ref $props eq 'HASH' && %$props || grep({not $NO_ARG_KEY{$_};} keys %schema)
l!l&&r!l&&!r
60026
ref $props eq "HASH" and %$props
!ll&&!rl&&r
02660
MC/DC33%1/3
ref $props eq 'HASH' && %$props || grep({not $NO_ARG_KEY{$_};} keys %schema)
ref $props eq "HASH" %$props grep({not $NO_ARG_KEY{$_};} keys %schema)
Truth table40%2/5
A and B || C
A= ref $props eq "HASH"B= %$propsC= grep({not $NO_ARG_KEY{$_};} keys %schema)
ABCresult
11X1
0X11
1011
0X00
1000
323 86 ▶ return $no_args ? undef : \%schema;
Branch: 1 branch
BranchTrueFalseTotal
$no_args ? : 26 60 86
324 }
325  
326 86 ▶sub to_gemini {
Subroutine to_gemini: called CC=2 SCAR=6.9
Pod undocumented
327 86 my ($self) = @_;
328 86 my $schema = $self->_gemini_schema;
329 return {
330 86 ▶ name => $self->name,
Branch: 1 branch
BranchTrueFalseTotal
$schema ? : 60 26 86
331 description => $self->description,
332 ( $schema ? ( parametersJsonSchema => $schema ) : () ),
333 };
334 }
335  
336 # OpenAI Responses API: flat tool objects, no {type:'function',function:{...}} wrapper
337 182 ▶sub to_responses {
Subroutine to_responses: called CC=1 SCAR=0.0
Pod undocumented
338 182 my ($self) = @_;
339 return {
340 182 type => 'function',
341 name => $self->name,
342 description => $self->description,
343 parameters => $self->input_schema,
344 };
345 }
346  
347 198 ▶sub to_mcp {
Subroutine to_mcp: called CC=1 SCAR=0.0
Pod undocumented
348 198 my ($self) = @_;
349 return {
350 198 name => $self->name,
351 description => $self->description,
352 inputSchema => $self->input_schema,
353 };
354 }
355  
356 # Shape used inside OpenAI's response_format => { type=>'json_schema',
357 # json_schema => { ... } } — and the basis of the chat_f forced-tool
358 # fallback path.
359 22 ▶sub to_json_schema {
Subroutine to_json_schema: called CC=1 SCAR=0.0
Pod undocumented
360 22 my ($self) = @_;
361 return {
362 22 name => $self->name,
363 description => $self->description,
364 schema => $self->input_schema,
365 };
366 }
367  
368 # Canonical hash (matches the legacy Input::Tools->normalize_tools shape).
369 5 ▶sub to_hash {
Subroutine to_hash: called CC=1 SCAR=0.0
Pod undocumented
370 5 my ($self) = @_;
371 return {
372 5 name => $self->name,
373 description => $self->description,
374 input_schema => $self->input_schema,
375 };
376 }
377  
378 # Make the object transparent to any JSON encoder configured with
379 # convert_blessed => 1 (the house default, see Langertha::Plugin::Langfuse).
380 # to_hash is the complete canonical representation, so this is a plain
381 # delegator — nothing is dropped.
382 1 ▶sub TO_JSON { shift->to_hash }
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod undocumented
383  
384 # --- Tag-driven dispatch ---
385  
386 # Maps a tool_wire_format tag to the per-tool serializer method.
387 189 my %TO_METHOD = (
388 openai => 'to_openai',
389 anthropic => 'to_anthropic',
390 gemini => 'to_gemini',
391 ollama => 'to_ollama',
392 responses => 'to_responses',
393 mcp => 'to_mcp',
394 hermes => 'to_mcp', # Hermes injects raw MCP defs into the prompt as JSON
395 );
396  
397 # Serialize this single tool to the given wire format.
398 674 ▶sub to {
Subroutine to: called CC=4 SCAR=14.3
Pod documented
399 674 my ($self, $fmt) = @_;
400 674 ▶ my $method = $TO_METHOD{ $fmt // '' }
Branch: 1 branch
BranchTrueFalseTotal
my $method = $TO_METHOD{$fmt // ""} or croak(q[Langertha::Tool: unknown wire format '] . ($fmt // "") . q[']) 673 1 674
Condition50%2/4
$fmt // ""
l!l
6740
$fmt // ""
l!l
10
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
401 or croak "Langertha::Tool: unknown wire format '" . ( $fmt // '' ) . "'";
402 673 return $self->$method;
403 }
404  
405 # Class method: turn a list of any-shape (usually MCP) tool hashrefs into the
406 # full wire `tools` payload for the given format. Handles collection-level
407 # shaping (Gemini wraps its declarations) that a per-tool serializer cannot.
408 #
409 # A server-side tool (a Langertha::ServerTool, or a hash ServerTool->from_hash
410 # recognises for $fmt) keeps its place in the list and goes out as its native
411 # hash; ServerTool->to croaks when its wire is not $fmt (k206, ADR 0030).
412 # Everything else goes through from_list, so a server tool of another wire, a
413 # client-executed built-in or an unknown type still croaks at the Tool door.
414 976 ▶sub format_list {
Subroutine format_list: called CC=5 SCAR=16.1
Pod undocumented
415 976 my ($class, $fmt, $tools) = @_;
416 976 ▶ $fmt //= '';
Condition50%1/2
$fmt //= ""
l!l
9760
MC/DC0%0/1
$fmt //= ""
$fmt
Truth table50%1/2
A
A= $fmt
Aresult
11
00
417 976 require Langertha::ServerTool;
418 my @items = map {
419 976 my $st = Langertha::ServerTool->from_hash( $fmt, $_ );
420 1200 ▶ $st ? $st : @{ $class->from_list([$_]) };
Branch: 2 branches
BranchTrueFalseTotal
$st ? : 35 1165 1200
ref $tools eq 'ARRAY' ? : 976 0 976
421 } ( ref($tools) eq 'ARRAY' ? @$tools : () );
422 562 ▶ if ( $fmt eq 'gemini' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fmt eq "gemini") 53 509 562
423 53 return [ { functionDeclarations => [ map { $_->to('gemini') } @items ] } ];
424 }
425 509 return [ map { $_->to($fmt) } @items ];
426 }
427  
428 # --- A caller's request tools list (k227, ADR 0001) ---
429 #
430 # chat_f and chat_stream_realtime_f take a tools list the caller built by
431 # hand: Tool objects, ServerTool objects and hashes of any shape, often mixed.
432 # Unlike format_list (whose callers only ever emit function tools), this keeps
433 # everything the caller meant and decides per item, in place:
434 # - a Tool or ServerTool object goes through its own to($fmt);
435 # - a function-tool hash already in $fmt's own shape goes out verbatim, so
436 # the extras the value object does not model survive (function.strict,
437 # cache_control, Gemini's declaration fields);
438 # - a function-tool hash in another shape (MCP inputSchema, canonical
439 # input_schema, the other dialect's shape) is converted, carrying over
440 # the extras $fmt takes (%CARRY);
441 # - anything else -- a built-in, a typed item Langertha does not know, a
442 # Gemini { functionDeclarations => [...] } -- goes out verbatim: the
443 # provider judges it, as the Responses envelope does for typed items
444 # (k210). Nothing is dropped and nothing becomes a function tool.
445 # Gemini keeps all function declarations in ONE functionDeclarations entry
446 # (k221 review M4), placed where the first declaration came from; a later
447 # raw functionDeclarations (or function_declarations) entry gives up its
448 # declarations to it and keeps its other fields.
449  
450 # True when a function-tool hash is already in $fmt's own tools-list shape.
451 78 ▶sub _is_wire_function {
Subroutine _is_wire_function: called CC=12 SCAR=25.8
452 78 my ( $hash, $fmt ) = @_;
453 78 ▶ my $type = $hash->{type} // '';
Condition100%2/2
$hash->{"type"} // ""
l!l
3444
MC/DC100%1/1
$hash->{"type"} // ""
$hash->{"type"}
Truth table100%2/2
A
A= $hash->{"type"}
Aresult
11
00
454 78 ▶ return $type eq 'function' && ref $hash->{function} eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if $fmt eq "openai" or $fmt eq "ollama" 53 25 78
Condition83%5/6
$fmt eq "openai" or $fmt eq "ollama"
l!l&&r!l&&!r
421125
$type eq 'function' && ref $hash->{'function'} eq 'HASH'
!ll&&!rl&&r
21032
MC/DC100%2/2
$fmt eq "openai" or $fmt eq "ollama"
$fmt eq "openai" $fmt eq "ollama"
MC/DC50%1/2
$type eq 'function' && ref $hash->{'function'} eq 'HASH'
$type eq 'function' ref $hash->{'function'} eq 'HASH'
Truth table100%3/3
A or B
A= $fmt eq "openai"B= $fmt eq "ollama"
ABresult
1X1
011
000
Truth table66%2/3
A && B
A= $type eq 'function'B= ref $hash->{'function'} eq 'HASH'
ABresult
0X0
100
111
455 if $fmt eq 'openai' || $fmt eq 'ollama';
456 return ( $type eq '' || $type eq 'custom' )
457 && ref $hash->{input_schema} eq 'HASH' && !exists $hash->{inputSchema}
458 25 ▶ if $fmt eq 'anthropic';
Branch: 1 branch
BranchTrueFalseTotal
if $fmt eq "anthropic" 12 13 25
Condition77%7/9
($type eq '' || $type eq 'custom') && ref $hash->{'input_schema'} eq 'HASH' && !exists $hash->{'inputSchema'}
!ll&&!rl&&r
705
$type eq "" || $type eq "custom" and ref $hash->{"input_schema"} eq "HASH"
!ll&&!rl&&r
255
$type eq "" or $type eq "custom"
l!l&&r!l&&!r
1002
MC/DC50%2/4
($type eq '' || $type eq 'custom') && ref $hash->{'input_schema'} eq 'HASH' && !exists $hash->{'inputSchema'}
$type eq "" $type eq "custom" ref $hash->{"input_schema"} eq "HASH" !exists $hash->{'inputSchema'}
Truth table42%3/7
A or B and C && not(D)
A= $type eq ""B= $type eq "custom"C= ref $hash->{"input_schema"} eq "HASH"D= !exists $hash->{'inputSchema'}
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
459 # A Gemini declaration (name + parameters, and fields such as behavior).
460 return $type eq '' && !exists $hash->{inputSchema} && !exists $hash->{input_schema}
461 13 ▶ if $fmt eq 'gemini';
Branch: 1 branch
BranchTrueFalseTotal
if $fmt eq "gemini" 13 0 13
Condition66%4/6
$type eq '' && !exists $hash->{'inputSchema'} && !exists $hash->{'input_schema'}
!ll&&!rl&&r
1003
$type eq "" and not exists $hash->{"inputSchema"}
!ll&&!rl&&r
0103
MC/DC33%1/3
$type eq '' && !exists $hash->{'inputSchema'} && !exists $hash->{'input_schema'}
$type eq "" not exists $hash->{"inputSchema"} !exists $hash->{'input_schema'}
Truth table50%2/4
A and not(B) && not(C)
A= $type eq ""B= not exists $hash->{"inputSchema"}C= !exists $hash->{'input_schema'}
ABCresult
0XX0
10X0
1100
1111
462 0 return 0;
463 }
464  
465 # The extras a converted hash keeps, per target wire. strict may sit on the
466 # hash or in OpenAI's nested function; an explicit value beats the schema
467 # guess of to_anthropic.
468 38 ▶sub _carry_extras {
Subroutine _carry_extras: called CC=8 SCAR=20.8
469 38 my ( $hash, $out, $fmt ) = @_;
470 38 ▶ my $fn = ref $hash->{function} eq 'HASH' ? $hash->{function} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $hash->{'function'} eq 'HASH' ? : 2 36 38
471 38 ▶ my $strict = exists $hash->{strict} ? $hash->{strict} : $fn->{strict};
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'strict'} ? : 1 37 38
472 38 ▶ if ( $fmt eq 'openai' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fmt eq 'openai') { } 13 25 38
elsif ($fmt eq 'anthropic') { } 7 18 25
473 13 ▶ $out->{function}{strict} = $strict if defined $strict;
Branch: 1 branch
BranchTrueFalseTotal
if defined $strict 1 12 13
474 }
475 elsif ( $fmt eq 'anthropic' ) {
476 7 ▶ $out->{strict} = $strict if defined $strict;
Branch: 1 branch
BranchTrueFalseTotal
if defined $strict 1 6 7
477 7 ▶ $out->{cache_control} = $hash->{cache_control} if exists $hash->{cache_control};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{"cache_control"} 1 6 7
478 }
479 38 return $out;
480 }
481  
482 # One item on $fmt; for gemini, a function tool comes back as a declaration.
483 112 ▶sub _request_item {
Subroutine _request_item: called CC=6 SCAR=18.2
484 112 my ( $class, $item, $fmt ) = @_;
485 112 ▶ return $item->to($fmt)
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($item) and $item->isa(__PACKAGE__) || $item->isa("Langertha::ServerTool") 20 92 112
Condition50%3/6
builtin::blessed($item) and $item->isa(__PACKAGE__) || $item->isa("Langertha::ServerTool")
!ll&&!rl&&r
92020
$item->isa(__PACKAGE__) or $item->isa("Langertha::ServerTool")
l!l&&r!l&&!r
2000
MC/DC33%1/3
builtin::blessed($item) and $item->isa(__PACKAGE__) || $item->isa("Langertha::ServerTool")
builtin::blessed($item) $item->isa(__PACKAGE__) $item->isa("Langertha::ServerTool")
Truth table50%2/4
A and B or C
A= builtin::blessed($item)B= $item->isa(__PACKAGE__)C= $item->isa("Langertha::ServerTool")
ABCresult
0XX0
1000
11X1
1011
486 if blessed($item) && ( $item->isa(__PACKAGE__) || $item->isa('Langertha::ServerTool') );
487 92 ▶ return $item unless _is_function_hash($item);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_function_hash($item) 14 78 92
488 78 ▶ return $item if _is_wire_function( $item, $fmt );
Branch: 1 branch
BranchTrueFalseTotal
if _is_wire_function($item, $fmt) 40 38 78
489 38 return _carry_extras( $item, $class->from_hash($item)->to($fmt), $fmt );
490 }
491  
492 123 ▶sub _is_function_hash {
Subroutine _is_function_hash: called CC=2 SCAR=7.1
493 123 my ($item) = @_;
494 123 ▶ return ref $item eq 'HASH' && scalar __PACKAGE__->classify($item) eq 'function';
Condition66%2/3
ref $item eq 'HASH' && scalar __PACKAGE__->classify($item) eq 'function'
!ll&&!rl&&r
03291
MC/DC50%1/2
ref $item eq 'HASH' && scalar __PACKAGE__->classify($item) eq 'function'
ref $item eq 'HASH' scalar __PACKAGE__->classify($item) eq 'function'
Truth table66%2/3
A && B
A= ref $item eq 'HASH'B= scalar __PACKAGE__->classify($item) eq 'function'
ABresult
0X0
100
111
495 }
496  
497 # The key a raw Gemini tool entry keeps its declarations under; the REST API
498 # reads both spellings (ADR 0018), the merged entry is written in camelCase.
499 18 ▶sub _declarations_key {
Subroutine _declarations_key: called CC=4 SCAR=13.9
500 18 my ($item) = @_;
501 18 ▶ return undef unless ref $item eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item eq "HASH" 0 18 18
502 18 for my $key (qw( functionDeclarations function_declarations )) {
503 26 ▶ return $key if ref $item->{$key} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $item->{$key} eq "ARRAY" 12 14 26
504 }
505 6 return undef;
506 }
507  
508 91 ▶sub request_list {
Subroutine request_list: called CC=13 SCAR=25.7
Pod documented
509 91 my ( $class, $fmt, $tools ) = @_;
510 91 ▶ return $tools unless ref $tools eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tools eq "ARRAY" 0 91 91
511 91 ▶ return [ map { $class->_request_item( $_, $fmt ) } @$tools ] unless $fmt eq 'gemini';
Branch: 1 branch
BranchTrueFalseTotal
unless $fmt eq "gemini" 68 23 91
512 23 my ( @out, @decls, $group );
513 23 for my $item (@$tools) {
514 39 ▶ if ( ( blessed($item) && $item->isa(__PACKAGE__) ) || _is_function_hash($item) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($item) and $item->isa(__PACKAGE__) or _is_function_hash($item)) { } 21 18 39
elsif (my $key = _declarations_key($item)) { } 12 6 18
Condition83%5/6
builtin::blessed($item) and $item->isa(__PACKAGE__) or _is_function_hash($item)
l!l&&r!l&&!r
81318
builtin::blessed($item) and $item->isa(__PACKAGE__)
!ll&&!rl&&r
3108
MC/DC66%2/3
builtin::blessed($item) and $item->isa(__PACKAGE__) or _is_function_hash($item)
builtin::blessed($item) $item->isa(__PACKAGE__) _is_function_hash($item)
Truth table60%3/5
A and B or C
A= builtin::blessed($item)B= $item->isa(__PACKAGE__)C= _is_function_hash($item)
ABCresult
11X1
0X11
1011
0X00
1000
515 21 push @decls, $class->_request_item( $item, $fmt );
516 21 ▶ push @out, $group = {} unless $group;
Branch: 1 branch
BranchTrueFalseTotal
unless $group 16 5 21
517 }
518 elsif ( my $key = _declarations_key($item) ) {
519 12 push @decls, @{ $item->{$key} };
520 12 my %rest = %$item;
521 12 delete $rest{$key};
522 12 ▶ push @out, \%rest if !$group || %rest;
Branch: 1 branch
BranchTrueFalseTotal
if not $group or %rest 9 3 12
Condition100%3/3
not $group or %rest
l!l&&r!l&&!r
723
MC/DC100%2/2
not $group or %rest
not $group %rest
Truth table100%3/3
not(A) or B
A= not $groupB= %rest
ABresult
1X1
011
000
523 12 ▶ $group //= \%rest;
Condition100%2/2
$group //= \%rest
l!l
57
MC/DC100%1/1
$group //= \%rest
$group
Truth table100%2/2
A
A= $group
Aresult
11
00
524 }
525 else {
526 6 push @out, $class->_request_item( $item, $fmt );
527 }
528 }
529 23 ▶ $group->{functionDeclarations} = \@decls if $group;
Branch: 1 branch
BranchTrueFalseTotal
if $group 23 0 23
530 23 return \@out;
531 }
532  
533  
534 189 __PACKAGE__->meta->make_immutable;
535 189 1;
536  
537 __END__
538  
539 =pod
540  
541 =encoding UTF-8
542  
543 =head1 NAME
544  
545 Langertha::Tool - Immutable canonical tool definition with cross-provider format conversion
546  
547 =head1 VERSION
548  
549 version 0.503
550  
551 =head2 classify
552  
553 my $category = Langertha::Tool->classify( $tool_hash );
554 my $category = Langertha::Tool->classify( $tool_hash, 'responses' );
555 my ( $category, $wire, $label ) = Langertha::Tool->classify( $tool_hash );
556  
557 Says what kind of tool definition C<$tool_hash> is, without croaking. Use it
558 where a tool list comes from someone else, such as a gateway that must answer
559 a bad client request with a 400 instead of dying: C<from_hash>,
560 C<from_list> and C<format_list> croak on every category except
561 C<function>, and they take that decision from this method.
562  
563 The category is one of:
564  
565 =over 4
566  
567 =item C<function>
568  
569 A function tool C<from_hash> translates: a C<Langertha::Tool>, a hash
570 without C<type> that has a C<name> (canonical, MCP, Gemini declaration,
571 Anthropic client tool), C<< type => 'function' >> (OpenAI, nested or flat),
572 or C<< type => 'custom' >> with an C<input_schema> (Anthropic's explicit
573 client tool).
574  
575 =item C<server>
576  
577 A known provider built-in that the provider runs, for example
578 C<web_search> (Responses), C<web_search_20250305> (Anthropic) or
579 C<< { google_search => {} } >> (Gemini). Server-side tools of the
580 C<responses> wire are carried by L<Langertha::ServerTool>; those of the
581 Anthropic and Gemini wires are not supported yet.
582  
583 =item C<client_builtin>
584  
585 A known provider built-in that the I<client> has to run and Langertha
586 cannot, for example C<local_shell>, C<computer_use_preview>, C<apply_patch>,
587 a C<shell> with a local environment, a C<tool_search> with
588 C<< execution => 'client' >> (Responses), C<bash_20250124> (Anthropic) or
589 C<computer_use> (Gemini).
590  
591 =item C<foreign>
592  
593 Only with C<$fmt>: a C<server> or C<client_builtin> tool whose own wire is
594 not C<$fmt> -- that is, for any C<$fmt> other than the built-in's own wire
595 (C<responses>, C<anthropic> or C<gemini>), including C<openai>, C<ollama> or
596 C<hermes>, which have no built-ins of their own. A C<$fmt> outside the known
597 C<tool_wire_format> values carps once per value. C<foreign> hides whether
598 the tool is server-side or client-executed; to learn that, call
599 C<classify> again without C<$fmt>.
600  
601 =item C<unknown>
602  
603 Anything else: a C<type> Langertha does not recognise (including OpenAI's
604 C<custom> without C<input_schema> and C<namespace>), a hash with neither
605 C<type> nor C<name> (such as C<< { functionDeclarations => [...] } >>), or
606 not a hash at all. A wire that takes native items verbatim (the Responses
607 envelope) passes a typed C<unknown> through to the provider.
608  
609 =back
610  
611 C<$fmt> is a C<tool_wire_format> (C<responses>, C<anthropic>, C<gemini>, ...).
612 In list context the method also returns the wire the built-in belongs to
613 (C<undef> for C<function> and C<unknown>) and a label: the C<type>, the
614 Gemini key, or, for an untyped C<unknown>, its keys. The per-wire lists are
615 taken from the provider documentation and are not verified against live
616 responses.
617  
618 =head2 request_list
619  
620 my $wire_tools = Langertha::Tool->request_list( $engine->tool_wire_format, \@tools );
621  
622 Shapes a caller's C<tools> list for one request on the wire C<$fmt>
623 (C<openai>, C<anthropic>, C<gemini> or C<ollama>); the Responses envelope
624 and the C<hermes> wire shape their own. L<Langertha::Role::Chat/chat_f> and
625 L<Langertha::Role::Chat/chat_stream_realtime_f> call it. Each item is decided
626 on its own and keeps its place:
627  
628 =over 4
629  
630 =item * a C<Langertha::Tool> or L<Langertha::ServerTool> goes through its
631 C<to($fmt)> (a server tool croaks off its own wire);
632  
633 =item * a function-tool hash already in the wire's shape goes out verbatim,
634 extras such as C<function.strict> and C<cache_control> included;
635  
636 =item * a function-tool hash in another shape (MCP C<inputSchema>, canonical
637 C<input_schema>, another dialect's shape) is converted through L</from_hash>,
638 keeping C<strict> (OpenAI and Anthropic) and C<cache_control> (Anthropic);
639  
640 =item * anything else -- a provider built-in, a typed item Langertha does not
641 know, a Gemini C<< { functionDeclarations => [...] } >> entry -- goes out
642 verbatim, for the provider to judge.
643  
644 =back
645  
646 On C<gemini> every function declaration ends up in one
647 C<functionDeclarations> entry, where the first declaration came from; a later
648 raw C<functionDeclarations> or C<function_declarations> entry is merged into it
649 and keeps its other fields. The merged entry is spelled C<functionDeclarations>.
650  
651 =head1 SUPPORT
652  
653 =head2 Issues
654  
655 Please report bugs and feature requests on GitHub at
656 L<https://github.com/Getty/langertha/issues>.
657  
658 =head2 IRC
659  
660 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
661  
662 =head1 CONTRIBUTING
663  
664 Contributions are welcome! Please fork the repository and submit a pull request.
665  
666 =head1 AUTHOR
667  
668 Torsten Raudssus <getty@cpan.org>
669  
670 =head1 COPYRIGHT AND LICENSE
671  
672 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
673  
674 This is free software; you can redistribute it and/or modify it under
675 the same terms as the Perl 5 programming language system itself.
676  
677 =cut
« blib/lib/Langertha/Role/Streaming.pm ↑ Summary blib/lib/Langertha/Plugin/Langfuse.pm »