blib/lib/Langertha/ServerTool.pm

filter: Statementstmt 100% 66 / 66 Branchbran 94.7% 36 / 38 Conditioncond 81.5% 31 / 38 MC/DCmcdc 69.5% 16 / 23 Subroutinesub 100% 12 / 12 Podpod 85.7% 6 / 7 totaltotal 90.7% 167 / 184 CC 35 SCAR 21.1
CC
35
Cov
94%
BUILD
14.1
to
9.1
from_hash
7.0
CRAP 8.2
« blib/lib/Langertha/Content/Image.pm ↑ Summary blib/lib/Langertha/VLLMHook/Config.pm »
1 package Langertha::ServerTool;
2 # ABSTRACT: Provider-native server-side tool definition, pinned to one wire format
3 182 our $VERSION = '0.503';
4 182 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 182 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 182 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 182 ▶use Langertha::Tool;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 # Wires whose server tools this value object can carry (Phase 1a, k206).
11 182 my %SUPPORTED_WIRE = ( responses => 1 );
12  
13 # Typed client tools that are never server tools, even with unlisted => 1.
14 182 my %CLIENT_TYPE = map { $_ => 1 } qw( function custom namespace );
15  
16 182 has wire => (
17 is => 'ro',
18 isa => 'Str',
19 required => 1,
20 );
21  
22  
23 182 has spec => (
24 is => 'ro',
25 isa => 'HashRef',
26 required => 1,
27 );
28  
29  
30 182 has unlisted => (
31 is => 'ro',
32 isa => 'Bool',
33 default => 0,
34 );
35  
36  
37 149 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
38 149 my ( $orig, $class, @args ) = @_;
39 149 my $params = $class->$orig(@args);
40 149 ▶ $params->{spec} = { %{ $params->{spec} } } if ref $params->{spec} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $params->{"spec"} eq "HASH" 149 0 149
41 149 return $params;
42 182 };
43  
44 149 ▶sub BUILD {
Subroutine BUILD: called CC=14 SCAR=26.4
Pod undocumented
45 149 my ($self) = @_;
46 149 my $wire = $self->wire;
47 149 my $spec = $self->spec;
48 149 ▶ my $label = $spec->{type} // '';
Condition100%2/2
$spec->{"type"} // ""
l!l
1481
MC/DC100%1/1
$spec->{"type"} // ""
$spec->{"type"}
Truth table100%2/2
A
A= $spec->{"type"}
Aresult
11
00
49 croak "Langertha::ServerTool: server tools on the '$wire' wire are not supported yet"
50 149 ▶ unless $SUPPORTED_WIRE{$wire};
Branch: 1 branch
BranchTrueFalseTotal
unless $SUPPORTED_WIRE{$wire} 1 148 149
51 148 ▶ croak "Langertha::ServerTool: spec has no type (keys: "
Branch: 1 branch
BranchTrueFalseTotal
unless length $label 1 147 148
Condition50%1/2
join(',', sort(keys %$spec)) || '(empty)'
l!l
10
MC/DC0%0/1
join(',', sort(keys %$spec)) || '(empty)'
join(',', sort(keys %$spec))
Truth table50%1/2
A
A= join(',', sort(keys %$spec))
Aresult
11
00
52 . ( join( ',', sort keys %$spec ) || '(empty)' ) . ")"
53 unless length $label;
54 147 my ( $category, $own_wire ) = Langertha::Tool->classify($spec);
55 147 ▶ croak "Langertha::ServerTool: '$label' is a function tool; use Langertha::Tool"
Branch: 1 branch
BranchTrueFalseTotal
if $category eq "function" and $label eq "function" 1 146 147
Condition66%2/3
$category eq "function" and $label eq "function"
!ll&&!rl&&r
14601
MC/DC50%1/2
$category eq "function" and $label eq "function"
$category eq "function" $label eq "function"
Truth table66%2/3
A and B
A= $category eq "function"B= $label eq "function"
ABresult
0X0
100
111
56 if $category eq 'function' && $label eq 'function';
57 croak "Langertha::ServerTool: '$label' is a client tool, not a server tool"
58 146 ▶ if $CLIENT_TYPE{$label} || $category eq 'function';
Branch: 1 branch
BranchTrueFalseTotal
if $CLIENT_TYPE{$label} or $category eq "function" 3 143 146
Condition66%2/3
$CLIENT_TYPE{$label} or $category eq "function"
l!l&&r!l&&!r
30143
MC/DC50%1/2
$CLIENT_TYPE{$label} or $category eq "function"
$CLIENT_TYPE{$label} $category eq "function"
Truth table66%2/3
A or B
A= $CLIENT_TYPE{$label}B= $category eq "function"
ABresult
1X1
011
000
59 143 ▶ croak "Langertha::ServerTool: '$label' is a client-executed built-in ($own_wire), not a server tool"
Branch: 1 branch
BranchTrueFalseTotal
if $category eq "client_builtin" 1 142 143
60 if $category eq 'client_builtin';
61 142 ▶ croak "Langertha::ServerTool: '$label' belongs to the $own_wire wire, not '$wire'"
Branch: 1 branch
BranchTrueFalseTotal
if $category eq "server" and $own_wire ne $wire 1 141 142
Condition100%3/3
$category eq "server" and $own_wire ne $wire
!ll&&!rl&&r
31381
MC/DC100%2/2
$category eq "server" and $own_wire ne $wire
$category eq "server" $own_wire ne $wire
Truth table100%3/3
A and B
A= $category eq "server"B= $own_wire ne $wire
ABresult
0X0
100
111
62 if $category eq 'server' && $own_wire ne $wire;
63 141 ▶ return if $category eq 'server' || $self->unlisted;
Branch: 1 branch
BranchTrueFalseTotal
if $category eq "server" or $self->unlisted 140 1 141
Condition100%3/3
$category eq "server" or $self->unlisted
l!l&&r!l&&!r
13821
MC/DC100%2/2
$category eq "server" or $self->unlisted
$category eq "server" $self->unlisted
Truth table100%3/3
A or B
A= $category eq "server"B= $self->unlisted
ABresult
1X1
011
000
64 1 croak "Langertha::ServerTool: '$label' is not a known server tool on '$wire'; "
65 . "pass unlisted => 1 if the provider runs it";
66 }
67  
68 269 ▶sub type { $_[0]->spec->{type} }
Subroutine type: called CC=1 SCAR=0.0
Pod documented
69  
70  
71 1728 ▶sub from_hash {
Subroutine from_hash: called CC=7 SCAR=19.5
Pod documented
72 1728 my ( $class, $fmt, $hash ) = @_;
73 1728 ▶ return $hash if blessed($hash) && $hash->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($hash) and $hash->isa(__PACKAGE__) 26 1702 1728
Condition100%3/3
builtin::blessed($hash) and $hash->isa(__PACKAGE__)
!ll&&!rl&&r
16663626
MC/DC100%2/2
builtin::blessed($hash) and $hash->isa(__PACKAGE__)
builtin::blessed($hash) $hash->isa(__PACKAGE__)
Truth table100%3/3
A and B
A= builtin::blessed($hash)B= $hash->isa(__PACKAGE__)
ABresult
0X0
100
111
74 1702 ▶ return undef unless ref $hash eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 37 1665 1702
75 1665 ▶ return undef unless defined $fmt && $SUPPORTED_WIRE{$fmt};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fmt and $SUPPORTED_WIRE{$fmt} 804 861 1665
Condition66%2/3
defined $fmt and $SUPPORTED_WIRE{$fmt}
!ll&&!rl&&r
0804861
MC/DC50%1/2
defined $fmt and $SUPPORTED_WIRE{$fmt}
defined $fmt $SUPPORTED_WIRE{$fmt}
Truth table66%2/3
A and B
A= defined $fmtB= $SUPPORTED_WIRE{$fmt}
ABresult
0X0
100
111
76 861 ▶ return undef unless Langertha::Tool->classify( $hash, $fmt ) eq 'server';
Branch: 1 branch
BranchTrueFalseTotal
unless "Langertha::Tool"->classify($hash, $fmt) eq "server" 732 129 861
77 129 return $class->new( wire => $fmt, spec => $hash );
78 }
79  
80  
81 157 ▶sub to {
Subroutine to: called CC=9 SCAR=22.1
Pod documented
82 157 my ( $self, $fmt ) = @_;
83 157 ▶ $fmt //= '';
Condition50%1/2
$fmt //= ""
l!l
1570
MC/DC0%0/1
$fmt //= ""
$fmt
Truth table50%1/2
A
A= $fmt
Aresult
11
00
84 157 ▶ croak "Langertha::ServerTool: '" . $self->type . "' belongs to the "
Branch: 1 branch
BranchTrueFalseTotal
unless $fmt eq $self->wire 9 148 157
85 . $self->wire . " wire, not '$fmt'; server tools are provider-native and cannot be translated"
86 unless $fmt eq $self->wire;
87 148 my $spec = $self->spec;
88 # Remote MCP on the Responses wire: OpenAI defaults require_approval to
89 # "always", which answers with an mcp_approval_request the client must
90 # confirm, and Langertha has no approval flow (orchestrator ruling Q2 on
91 # k206). Checked here, on the one door every emission path goes through --
92 # the Responses envelope and Tool->format_list, which sees no engine -- so it
93 # is enforced once (k206 review M3). An engine whose provider does not take
94 # the field (xAI) strips it in its _server_tool_wire_check hook afterwards.
95 148 ▶ if ( $self->type eq 'mcp' && $fmt eq 'responses' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "mcp" and $fmt eq "responses") 26 122 148
Condition66%2/3
$self->type eq "mcp" and $fmt eq "responses"
!ll&&!rl&&r
122026
MC/DC50%1/2
$self->type eq "mcp" and $fmt eq "responses"
$self->type eq "mcp" $fmt eq "responses"
Truth table66%2/3
A and B
A= $self->type eq "mcp"B= $fmt eq "responses"
ABresult
0X0
100
111
96 26 my $approval = $spec->{require_approval};
97 26 ▶ croak "Langertha::ServerTool: remote MCP tool '" . ( $spec->{server_label} // '?' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $approval and not ref $approval and $approval eq "never" 12 14 26
Condition87%7/8
defined $approval and not ref $approval and $approval eq "never"
!ll&&!rl&&r
8414
defined $approval and not ref $approval
!ll&&!rl&&r
4418
$spec->{"server_label"} // "?"
l!l
120
MC/DC100%3/3
defined $approval and not ref $approval and $approval eq "never"
defined $approval not ref $approval $approval eq "never"
MC/DC0%0/1
$spec->{"server_label"} // "?"
$spec->{"server_label"}
Truth table100%4/4
A and not(B) and C
A= defined $approvalB= not ref $approvalC= $approval eq "never"
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $spec->{"server_label"}
Aresult
11
00
98 . "' needs require_approval => 'never'; the approval flow is not supported"
99 unless defined $approval && !ref $approval && $approval eq 'never';
100 }
101 136 return { %$spec };
102 }
103  
104  
105 508 ▶sub check_engine {
Subroutine check_engine: called CC=6 SCAR=17.9
Pod documented
106 508 my ( $class, $engine, $tools ) = @_;
107 508 ▶ return unless ref $tools eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tools eq "ARRAY" 191 317 508
108 317 ▶ return if $engine->supports('server_tools');
Branch: 1 branch
BranchTrueFalseTotal
if $engine->supports("server_tools") 6 311 317
109 311 for my $item (@$tools) {
110 342 ▶ next unless blessed($item) && $item->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($item) and $item->isa(__PACKAGE__) 336 6 342
Condition100%3/3
builtin::blessed($item) and $item->isa(__PACKAGE__)
!ll&&!rl&&r
303336
MC/DC100%2/2
builtin::blessed($item) and $item->isa(__PACKAGE__)
builtin::blessed($item) $item->isa(__PACKAGE__)
Truth table100%3/3
A and B
A= builtin::blessed($item)B= $item->isa(__PACKAGE__)
ABresult
0X0
100
111
111 6 croak "" . ( ref $engine ) . ": '" . $item->type . "' is a Langertha::ServerTool, "
112 . "and this engine does not supports('server_tools')";
113 }
114 305 return;
115 }
116  
117  
118 3 ▶sub to_hash {
Subroutine to_hash: called CC=2 SCAR=7.1
Pod documented
119 3 my ($self) = @_;
120 return {
121 wire => $self->wire,
122 3 ▶ spec => { %{ $self->spec } },
Branch: 1 branch
BranchTrueFalseTotal
$self->unlisted ? : 0 3 3
123 ( $self->unlisted ? ( unlisted => 1 ) : () ),
124 };
125 }
126  
127  
128 1 ▶sub TO_JSON { shift->to_hash }
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod documented
129  
130  
131  
132 182 __PACKAGE__->meta->make_immutable;
133 182 1;
134  
135 __END__
136  
137 =pod
138  
139 =encoding UTF-8
140  
141 =head1 NAME
142  
143 Langertha::ServerTool - Provider-native server-side tool definition, pinned to one wire format
144  
145 =head1 VERSION
146  
147 version 0.503
148  
149 =head1 SYNOPSIS
150  
151 use Langertha::ServerTool;
152  
153 # Wrap a provider-native server tool for the Responses wire
154 my $search = Langertha::ServerTool->new(
155 wire => 'responses',
156 spec => { type => 'web_search' },
157 );
158  
159 # Per request, mixed with function tools
160 my $response = await $engine->chat_f(
161 messages => [ { role => 'user', content => 'Current Perl release?' } ],
162 tools => [ $search, $mcp_tool_hash ],
163 );
164  
165 # Or once on the engine, for every request (Langertha::Role::ServerTools)
166 my $engine = Langertha::Engine::OpenAIResponses->new(
167 model => 'gpt-5.6-luna',
168 server_tools => [ { type => 'web_search' } ],
169 );
170  
171 # Recognise a hash without croaking; undef when it is no server tool
172 my $st = Langertha::ServerTool->from_hash( responses => $hash );
173  
174 =head1 DESCRIPTION
175  
176 A server-side tool is one the provider runs itself during a single request:
177 web search, file search, a code interpreter, remote MCP. Its definition is a
178 provider contract, not a portable function tool -- C<web_search> on the
179 Responses wire, C<web_search_20250305> on Anthropic and C<google_search> on
180 Gemini are three different things. So this value object does not translate:
181 it carries the provider-native hash (L</spec>) verbatim, is keyed by the
182 C<tool_wire_format> it belongs to (L</wire>), and L</to> refuses every other
183 wire. It is the server-side sibling of L<Langertha::Tool>, which takes function
184 tools only.
185  
186 Which hashes count as server tools is decided by
187 L<Langertha::Tool/classify>, the one classifier. A type Langertha does not list
188 yet can still be sent by vouching for it with C<< unlisted => 1 >>.
189  
190 Supported wires: C<responses> (OpenAI's C</v1/responses>). Anthropic and Gemini
191 server tools are not supported yet; the constructor croaks for them.
192  
193 Server-side calls the provider reports back land on
194 L<Langertha::Response/server_tool_calls> as L<Langertha::ServerToolCall>, never
195 on L<Langertha::Response/tool_calls>: the client must not execute them. See
196 ADR 0030.
197  
198 =head2 wire
199  
200 The C<tool_wire_format> the tool belongs to (C<responses>). Required.
201  
202 =head2 spec
203  
204 The provider-native tool hash, for example
205 C<< { type => 'mcp', server_label => 'docs', server_url => '...', require_approval => 'never' } >>.
206 Required. A shallow copy is taken at construction; use L</to> to read it.
207  
208 =head2 unlisted
209  
210 Set to C<1> to send a C<type> that Langertha's table does not list yet, when
211 you know the provider runs it. Function, C<custom> and C<namespace> tools and
212 known client-executed built-ins are refused even then.
213  
214 =head2 type
215  
216 The C<type> of the native hash, for example C<web_search>.
217  
218 =head2 from_hash
219  
220 my $st = Langertha::ServerTool->from_hash( $fmt, $hash );
221  
222 Returns a C<Langertha::ServerTool> when C<$hash> is a known server tool of the
223 wire C<$fmt>, else C<undef>. Never croaks and never sniffs: the wire is given,
224 as for L<Langertha::ToolCall/extract>. A C<Langertha::ServerTool> is returned
225 unchanged.
226  
227 =head2 to
228  
229 my $hash = $st->to('responses');
230  
231 Returns a copy of the native hash for the wire the tool belongs to, and croaks
232 for any other wire. A remote C<mcp> tool on the C<responses> wire also croaks
233 unless it says C<< require_approval => 'never' >>: the provider default asks
234 the client to approve each call, and Langertha has no approval flow. The check
235 lives here so that every path -- an engine request and
236 L<Langertha::Tool/format_list> alike -- enforces it.
237  
238 =head2 check_engine
239  
240 Langertha::ServerTool->check_engine( $engine, \@tools );
241  
242 Croaks when C<@tools> holds a C<Langertha::ServerTool> and C<$engine> does not
243 C<supports('server_tools')>, so the tool never reaches a wire that cannot take
244 it. L<Langertha::Role::Chat/chat_f> calls it before building a request. Plain
245 hashes are left alone: a provider-shaped hash still goes out as the engine
246 sends it today.
247  
248 =head2 to_hash
249  
250 Returns C<< { wire, spec } >> (plus C<unlisted> when set).
251  
252 =head2 TO_JSON
253  
254 Delegates to L</to_hash>, for JSON encoders with C<convert_blessed>.
255  
256 =head1 SEE ALSO
257  
258 =over
259  
260 =item * L<Langertha::Tool> - function tools, and C<classify>
261  
262 =item * L<Langertha::ServerToolCall> - a server-side call reported back
263  
264 =item * L<Langertha::Role::ServerTools> - the engine capability and C<server_tools>
265  
266 =back
267  
268 =head1 SUPPORT
269  
270 =head2 Issues
271  
272 Please report bugs and feature requests on GitHub at
273 L<https://github.com/Getty/langertha/issues>.
274  
275 =head2 IRC
276  
277 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
278  
279 =head1 CONTRIBUTING
280  
281 Contributions are welcome! Please fork the repository and submit a pull request.
282  
283 =head1 AUTHOR
284  
285 Torsten Raudssus <getty@cpan.org>
286  
287 =head1 COPYRIGHT AND LICENSE
288  
289 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
290  
291 This is free software; you can redistribute it and/or modify it under
292 the same terms as the Perl 5 programming language system itself.
293  
294 =cut
« blib/lib/Langertha/Content/Image.pm ↑ Summary blib/lib/Langertha/VLLMHook/Config.pm »