blib/lib/Langertha/ServerToolCall.pm

filter: Statementstmt 100% 31 / 31 Branchbran 87.5% 14 / 16 Conditioncond 55.0% 11 / 20 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 100% 6 / 6 Podpod 100% 4 / 4 totaltotal 77.2% 68 / 88 CC 17 SCAR 21.7
CC
17
Cov
84%
from_responses
13.5
extract
6.2
to_hash
2.0
CRAP 8.8
« blib/lib/Langertha/Request/SyncHTTP.pm ↑ Summary blib/lib/Langertha/Response.pm »
1 package Langertha::ServerToolCall;
2 # ABSTRACT: Record of one tool call the provider executed itself
3 183 our $VERSION = '0.503';
4 183 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 183 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 183 has type => (
9 is => 'ro',
10 isa => 'Str',
11 required => 1,
12 );
13  
14  
15 183 has id => (
16 is => 'ro',
17 isa => 'Str',
18 default => '',
19 );
20  
21  
22 183 has status => (
23 is => 'ro',
24 isa => 'Str',
25 predicate => 'has_status',
26 );
27  
28  
29 183 has data => (
30 is => 'ro',
31 isa => 'HashRef',
32 required => 1,
33 );
34  
35  
36 # Output items of the Responses wire that record a call the provider ran
37 # (OpenAI create-response reference, spec k206 section 2.1). A client
38 # tool_search_call (execution => 'client') is client-actionable instead, and
39 # the walkers croak on it (Langertha::Tool->_croak_on_client_item). Anything
40 # else unknown is skipped -- values open; it stays on Response.raw.
41 # x_search_call is xAI's X Search item (docs.x.ai tool-usage-details, k355;
42 # documentation-derived, not capture-verified).
43 183 my %RESPONSES_SERVER_ITEM = map { $_ => 1 } qw(
44 web_search_call file_search_call code_interpreter_call image_generation_call
45 mcp_call mcp_list_tools shell_call tool_search_call x_search_call
46 );
47  
48 153 ▶sub from_responses {
Subroutine from_responses: called CC=11 SCAR=26.1
Pod documented
49 153 my ( $class, $item ) = @_;
50 153 ▶ return undef unless ref $item eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item eq "HASH" 1 152 153
51 152 ▶ my $type = $item->{type} // '';
Condition50%1/2
$item->{"type"} // ""
l!l
1520
MC/DC0%0/1
$item->{"type"} // ""
$item->{"type"}
Truth table50%1/2
A
A= $item->{"type"}
Aresult
11
00
52 152 ▶ return undef unless $RESPONSES_SERVER_ITEM{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $RESPONSES_SERVER_ITEM{$type} 131 21 152
53 21 ▶ return undef if $type eq 'tool_search_call' && ( $item->{execution} // '' ) eq 'client';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "tool_search_call" and ($item->{"execution"} // "") eq "client" 0 21 21
Condition60%3/5
$type eq "tool_search_call" and ($item->{"execution"} // "") eq "client"
!ll&&!rl&&r
2010
$item->{"execution"} // ""
l!l
01
MC/DC0%0/2
$type eq "tool_search_call" and ($item->{"execution"} // "") eq "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
54 return $class->new(
55 type => $type,
56 data => $item,
57 ( defined $item->{id} && !ref $item->{id} ? ( id => $item->{id} ) : () ),
58 21 ▶ ( defined $item->{status} && !ref $item->{status} ? ( status => $item->{status} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
defined $item->{'id'} && !ref($item->{'id'}) ? : 21 0 21
defined $item->{'status'} && !ref($item->{'status'}) ? : 15 6 21
Condition50%3/6
defined $item->{"id"} and not ref $item->{"id"}
!ll&&!rl&&r
0021
defined $item->{"status"} and not ref $item->{"status"}
!ll&&!rl&&r
6015
MC/DC0%0/2
defined $item->{"id"} and not ref $item->{"id"}
defined $item->{"id"} not ref $item->{"id"}
MC/DC50%1/2
defined $item->{"status"} and not ref $item->{"status"}
defined $item->{"status"} not ref $item->{"status"}
Truth table33%1/3
A and not(B)
A= defined $item->{"id"}B= not ref $item->{"id"}
ABresult
0X0
100
111
Truth table66%2/3
A and not(B)
A= defined $item->{"status"}B= not ref $item->{"status"}
ABresult
0X0
100
111
59 );
60 }
61  
62  
63 102 ▶sub extract {
Subroutine extract: called CC=6 SCAR=18.2
Pod documented
64 102 my ( $class, $fmt, $data ) = @_;
65 102 ▶ croak "Langertha::ServerToolCall: server tool calls on the '" . ( $fmt // '' )
Branch: 1 branch
BranchTrueFalseTotal
unless ($fmt // "") eq "responses" 1 101 102
Condition50%2/4
$fmt // ""
l!l
1020
$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
66 . "' wire are not supported yet"
67 unless ( $fmt // '' ) eq 'responses';
68 101 ▶ return () unless ref $data eq 'HASH' && ref $data->{output} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" and ref $data->{"output"} eq "ARRAY" 4 97 101
Condition66%2/3
ref $data eq "HASH" and ref $data->{"output"} eq "ARRAY"
!ll&&!rl&&r
0497
MC/DC50%1/2
ref $data eq "HASH" and ref $data->{"output"} eq "ARRAY"
ref $data eq "HASH" ref $data->{"output"} eq "ARRAY"
Truth table66%2/3
A and B
A= ref $data eq "HASH"B= ref $data->{"output"} eq "ARRAY"
ABresult
0X0
100
111
69 97 return grep { defined } map { $class->from_responses($_) } @{ $data->{output} };
70 }
71  
72  
73 4 ▶sub to_hash {
Subroutine to_hash: called CC=2 SCAR=6.9
Pod documented
74 4 my ($self) = @_;
75 return {
76 4 ▶ type => $self->type,
Branch: 1 branch
BranchTrueFalseTotal
$self->has_status ? : 3 1 4
77 id => $self->id,
78 ( $self->has_status ? ( status => $self->status ) : () ),
79 data => $self->data,
80 };
81 }
82  
83  
84 1 ▶sub TO_JSON { shift->to_hash }
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod documented
85  
86  
87  
88 183 __PACKAGE__->meta->make_immutable;
89 183 1;
90  
91 __END__
92  
93 =pod
94  
95 =encoding UTF-8
96  
97 =head1 NAME
98  
99 Langertha::ServerToolCall - Record of one tool call the provider executed itself
100  
101 =head1 VERSION
102  
103 version 0.503
104  
105 =head1 SYNOPSIS
106  
107 my $response = await $engine->chat_f(
108 messages => [ { role => 'user', content => 'Current Perl release?' } ],
109 tools => [ { type => 'web_search' } ],
110 );
111  
112 for my $call ( @{ $response->server_tool_calls // [] } ) {
113 say $call->type, ' ', $call->id, ' ', $call->status // '';
114 my $item = $call->data; # the provider's item, verbatim
115 }
116  
117 =head1 DESCRIPTION
118  
119 A server-side tool call is one the provider ran itself during the request --
120 a web search, a file search, a remote MCP call. The client has nothing to do
121 for it, so it is kept apart from L<Langertha::ToolCall>, which means "a call
122 the client must act on": these records land on
123 L<Langertha::Response/server_tool_calls>, never on
124 L<Langertha::Response/tool_calls>, and C<chat_with_tools_f> never executes
125 them (ADR 0003 Update k206, ADR 0030).
126  
127 The record is deliberately thin: the wire item type, its id and status, and
128 the item itself verbatim under L</data>. Inputs and results are not
129 normalized across providers.
130  
131 =head2 type
132  
133 The wire item type, for example C<web_search_call>, C<file_search_call> or
134 C<mcp_call>. Required.
135  
136 =head2 id
137  
138 The provider's item id. Empty when the item carried none.
139  
140 =head2 status
141  
142 The item status (for example C<completed>), when the provider sent one. Test
143 with C<has_status>.
144  
145 =head2 data
146  
147 The provider's item, verbatim.
148  
149 =head2 from_responses
150  
151 my $call = Langertha::ServerToolCall->from_responses($output_item);
152  
153 Builds a record from one Responses C<output[]> item, or returns C<undef> when
154 the item is not a server-side call.
155  
156 =head2 extract
157  
158 my @calls = Langertha::ServerToolCall->extract( responses => $data );
159  
160 Returns every server-side call item of a decoded response, in wire order.
161 Pinned to the wire like L<Langertha::ToolCall/extract>. Only C<responses> is
162 supported; other wires croak.
163  
164 =head2 to_hash
165  
166 Returns C<< { type, id, status?, data } >>.
167  
168 =head2 TO_JSON
169  
170 Delegates to L</to_hash>, for JSON encoders with C<convert_blessed>.
171  
172 =head1 SEE ALSO
173  
174 =over
175  
176 =item * L<Langertha::ServerTool> - the server-side tool definition
177  
178 =item * L<Langertha::Response/server_tool_calls>
179  
180 =item * L<Langertha::ToolCall> - calls the client must execute
181  
182 =back
183  
184 =head1 SUPPORT
185  
186 =head2 Issues
187  
188 Please report bugs and feature requests on GitHub at
189 L<https://github.com/Getty/langertha/issues>.
190  
191 =head2 IRC
192  
193 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
194  
195 =head1 CONTRIBUTING
196  
197 Contributions are welcome! Please fork the repository and submit a pull request.
198  
199 =head1 AUTHOR
200  
201 Torsten Raudssus <getty@cpan.org>
202  
203 =head1 COPYRIGHT AND LICENSE
204  
205 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
206  
207 This is free software; you can redistribute it and/or modify it under
208 the same terms as the Perl 5 programming language system itself.
209  
210 =cut
« blib/lib/Langertha/Request/SyncHTTP.pm ↑ Summary blib/lib/Langertha/Response.pm »