blib/lib/Langertha/ToolResult.pm

filter: Statementstmt 100% 203 / 203 Branchbran 90.9% 120 / 132 Conditioncond 71.6% 81 / 113 MC/DCmcdc 53.2% 33 / 62 Subroutinesub 100% 33 / 33 Podpod 14.2% 1 / 7 totaltotal 85.6% 471 / 550 CC 110 SCAR 23.0
CC
110
Cov
90%
_native_string
20.5
_anthropic_block
19.0
_mcp_item
16.0
CRAP 10.0
« blib/lib/Langertha/Engine/AKI.pm ↑ Summary blib/lib/Langertha/HTTP/ConnectCheck.pm »
1 package Langertha::ToolResult;
2 # ABSTRACT: Immutable canonical result of executing one tool, with cross-provider conversion
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 183 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 183 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 183 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 183 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 183 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 183 has name => (
14 is => 'ro',
15 isa => 'Str',
16 default => '',
17 );
18  
19  
20 183 has id => (
21 is => 'ro',
22 isa => 'Str',
23 default => '',
24 );
25  
26  
27 has content => (
28 is => 'ro',
29 isa => 'ArrayRef',
30 7 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 183 );
32  
33  
34 183 has is_error => (
35 is => 'ro',
36 isa => 'Bool',
37 default => 0,
38 );
39  
40  
41 183 has structured_content => (
42 is => 'ro',
43 predicate => 'has_structured_content',
44 );
45  
46  
47 # Shared encoder for result payloads that ride as a JSON *string* inside the
48 # request body (or inside hermes text): characters, not bytes. The transport
49 # (Role::JSON) encodes the whole body to UTF-8 once; a byte string here would be
50 # encoded twice ("Köln" -> "Köln"). Same key order as Role::JSON. -- karr k252
51 183 my $JSON = JSON::MaybeXS->new( utf8 => 0, canonical => 1 );
52  
53 # --- MCP content normalizer, shared by every format (karr k326, k336) ---
54 #
55 # One pass over the MCP content array yields neutral items; each format renders
56 # them. Kinds:
57 # text a text block (keeps cache_control, for Anthropic)
58 # document text from an embedded resource (whatever its MIME type), or a
59 # text/* blob decoded as UTF-8
60 # blob a base64 payload (image, audio, binary resource) with its MIME
61 # native an Anthropic-native block a caller built for that wire
62 # note a ready text placeholder (resource_link, non-hash blocks)
63 # A blob the wire cannot carry becomes a placeholder naming type, MIME, URI and
64 # decoded size -- never the base64 payload.
65  
66 83 ▶sub _b64_size {
Subroutine _b64_size: called CC=3 SCAR=11.0
67 83 my ($data) = @_;
68 83 ▶ return undef unless defined $data && !ref $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data and not ref $data 5 78 83
Condition66%2/3
defined $data and not ref $data
!ll&&!rl&&r
5078
MC/DC50%1/2
defined $data and not ref $data
defined $data not ref $data
Truth table66%2/3
A and not(B)
A= defined $dataB= not ref $data
ABresult
0X0
100
111
69 78 ( my $b64 = $data ) =~ s/\s+//g;
70 78 my $pad = () = $b64 =~ /=/g;
71 78 return int( length($b64) * 3 / 4 ) - $pad;
72 }
73  
74 83 ▶sub _placeholder {
Subroutine _placeholder: called CC=4 SCAR=13.9
75 83 my ( $type, $mime, $uri, $data ) = @_;
76 83 my $size = _b64_size($data);
77 83 ▶ return join( ' ', "[$type]", grep { defined && length } $mime,
Branch: 2 branches
BranchTrueFalseTotal
defined $uri ? : 25 58 83
defined $size ? : 78 5 83
Condition66%2/3
defined $_ && length $_
!ll&&!rl&&r
20184
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
78 ( defined $uri ? "<$uri>" : () ), ( defined $size ? "($size bytes)" : () ) );
79 }
80  
81 78 ▶sub _mcp_resource {
Subroutine _mcp_resource: called CC=6 SCAR=18.4
82 78 my ($res) = @_;
83 78 ▶ $res = {} unless ref $res eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res eq "HASH" 0 78 78
84 78 my $mime = $res->{mimeType};
85 78 ▶ return { kind => 'document', text => $res->{text} } if defined $res->{text};
Branch: 1 branch
BranchTrueFalseTotal
if defined $res->{"text"} 19 59 78
86 59 ▶ if ( defined $res->{blob} && defined $mime && $mime =~ m{\Atext/} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res->{"blob"} and defined $mime and $mime =~ m[\Atext/]) 3 56 59
Condition50%3/6
defined $res->{"blob"} and defined $mime and $mime =~ m[\Atext/]
!ll&&!rl&&r
0563
defined $res->{"blob"} and defined $mime
!ll&&!rl&&r
0059
MC/DC33%1/3
defined $res->{"blob"} and defined $mime and $mime =~ m[\Atext/]
defined $res->{"blob"} defined $mime $mime =~ m[\Atext/]
Truth table50%2/4
A and B and C
A= defined $res->{"blob"}B= defined $mimeC= $mime =~ m[\Atext/]
ABCresult
0XX0
10X0
1100
1111
87 3 my $bytes = MIME::Base64::decode_base64( $res->{blob} );
88 3 return { kind => 'document',
89 text => Encode::decode( 'UTF-8', $bytes, Encode::FB_DEFAULT() ) };
90 }
91 return { kind => 'blob', type => 'resource', mime => $mime, uri => $res->{uri},
92 56 data => $res->{blob} };
93 }
94  
95 421 ▶sub _mcp_item {
Subroutine _mcp_item: called CC=15 SCAR=27.7
96 421 my ($block) = @_;
97 421 ▶ return { kind => 'note', text => _placeholder('unsupported') } unless ref $block eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $block eq "HASH" 0 421 421
98 421 ▶ my $type = $block->{type} // '';
Condition50%1/2
$block->{"type"} // ""
l!l
4210
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table50%1/2
A
A= $block->{"type"}
Aresult
11
00
99 421 ▶ if ( $type eq 'text' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "text") 190 231 421
100 return { kind => 'text', text => ( $block->{text} // '' ),
101 190 ▶ ( exists $block->{cache_control} ? ( cache_control => $block->{cache_control} ) : () ) };
Branch: 1 branch
BranchTrueFalseTotal
exists $block->{'cache_control'} ? : 1 189 190
Condition50%1/2
$block->{"text"} // ""
l!l
1900
MC/DC0%0/1
$block->{"text"} // ""
$block->{"text"}
Truth table50%1/2
A
A= $block->{"text"}
Aresult
11
00
102 }
103 return { kind => 'native', block => $block }
104 231 ▶ if ( ( $type eq 'image' || $type eq 'document' ) && ref $block->{source} eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "image" || $type eq "document" and ref $block->{"source"} eq "HASH" or $type eq "search_result" 41 190 231
Condition100%9/9
$type eq "image" || $type eq "document" and ref $block->{"source"} eq "HASH" or $type eq "search_result"
l!l&&r!l&&!r
1922190
$type eq "image" || $type eq "document" and ref $block->{"source"} eq "HASH"
!ll&&!rl&&r
1417119
$type eq "image" or $type eq "document"
l!l&&r!l&&!r
7416141
MC/DC100%4/4
$type eq "image" || $type eq "document" and ref $block->{"source"} eq "HASH" or $type eq "search_result"
$type eq "image" $type eq "document" ref $block->{"source"} eq "HASH" $type eq "search_result"
Truth table62%5/8
A or B and C or D
A= $type eq "image"B= $type eq "document"C= ref $block->{"source"} eq "HASH"D= $type eq "search_result"
ABCDresult
1X1X1
011X1
00X11
1X011
01011
00X00
1X000
01000
105 || $type eq 'search_result';
106 190 ▶ return _mcp_resource( $block->{resource} ) if $type eq 'resource';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "resource" 78 112 190
107 112 ▶ if ( $type eq 'resource_link' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "resource_link") 21 91 112
108 return { kind => 'note', text => join( ' ', '[resource_link]',
109 42 ▶ grep { defined && length } $block->{name},
Branch: 1 branch
BranchTrueFalseTotal
defined $block->{'uri'} ? : 21 0 21
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
0042
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
110 21 ( defined $block->{uri} ? "<$block->{uri}>" : () ) ) };
111 }
112 return { kind => 'blob', type => ( length $type ? $type : 'unsupported' ),
113 91 ▶ mime => $block->{mimeType}, uri => $block->{uri}, data => $block->{data} };
Branch: 1 branch
BranchTrueFalseTotal
length $type ? : 91 0 91
114 }
115  
116 328 ▶sub _mcp_items {
Subroutine _mcp_items: called CC=1 SCAR=0.0
117 328 my ($self) = @_;
118 328 return map { _mcp_item($_) } @{ $self->content };
119 }
120  
121 # One item as text for the string wires.
122 266 ▶sub _string_item {
Subroutine _string_item: called CC=3 SCAR=11.0
123 266 my ($item) = @_;
124 266 my $kind = $item->{kind};
125 266 ▶ return _placeholder( @{$item}{qw( type mime uri data )} ) if $kind eq 'blob';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "blob" 62 204 266
126 204 ▶ return $item->{text} unless $kind eq 'native';
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "native" 183 21 204
127 21 return _native_string( $item->{block} );
128 }
129  
130 # An Anthropic-native block as text, for a wire that cannot carry it: a text
131 # document gives its text, a content document its inner parts (karr k367), a
132 # search_result a "[search_result] title <source>" line and its text parts
133 # (karr k366), anything else a placeholder. Inner parts render the same way.
134 43 ▶sub _native_string {
Subroutine _native_string: called CC=16 SCAR=30.2
135 43 my ($native) = @_;
136 43 ▶ return _placeholder('unsupported') unless ref $native eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $native eq "HASH" 0 43 43
137 43 ▶ my $type = $native->{type} // '';
Condition50%1/2
$native->{"type"} // ""
l!l
430
MC/DC0%0/1
$native->{"type"} // ""
$native->{"type"}
Truth table50%1/2
A
A= $native->{"type"}
Aresult
11
00
138 43 ▶ return $native->{text} // '' if $type eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "text" 20 23 43
Condition50%1/2
$native->{"text"} // ""
l!l
200
MC/DC0%0/1
$native->{"text"} // ""
$native->{"text"}
Truth table50%1/2
A
A= $native->{"text"}
Aresult
11
00
139 23 ▶ if ( $type eq 'search_result' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "search_result") 11 12 23
140 11 my $source = $native->{source};
141 22 ▶ my $head = join ' ', '[search_result]', grep { defined && length } $native->{title},
Branch: 1 branch
BranchTrueFalseTotal
defined $source && !ref($source) && length $source ? : 11 0 11
Condition33%3/9
defined $_ && length $_
!ll&&!rl&&r
0022
defined $source and not ref $source and length $source
!ll&&!rl&&r
0011
defined $source and not ref $source
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
MC/DC0%0/3
defined $source and not ref $source and length $source
defined $source not ref $source length $source
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
Truth table25%1/4
A and not(B) and C
A= defined $sourceB= not ref $sourceC= length $source
ABCresult
0XX0
10X0
1100
1111
142 11 ( defined $source && !ref $source && length $source ? "<$source>" : () );
143 11 return join "\n", $head, _native_parts( $native->{content} );
144 }
145 12 ▶ my $src = ref $native->{source} eq 'HASH' ? $native->{source} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $native->{'source'} eq 'HASH' ? : 12 0 12
146 12 ▶ my $src_type = $src->{type} // '';
Condition50%1/2
$src->{"type"} // ""
l!l
120
MC/DC0%0/1
$src->{"type"} // ""
$src->{"type"}
Truth table50%1/2
A
A= $src->{"type"}
Aresult
11
00
147 12 ▶ return $src->{data} if $src_type eq 'text' && defined $src->{data};
Branch: 1 branch
BranchTrueFalseTotal
if $src_type eq "text" and defined $src->{"data"} 5 7 12
Condition66%2/3
$src_type eq "text" and defined $src->{"data"}
!ll&&!rl&&r
705
MC/DC50%1/2
$src_type eq "text" and defined $src->{"data"}
$src_type eq "text" defined $src->{"data"}
Truth table66%2/3
A and B
A= $src_type eq "text"B= defined $src->{"data"}
ABresult
0X0
100
111
148 7 ▶ if ( $src_type eq 'content' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_type eq "content") 4 3 7
149 4 my @parts = _native_parts( $src->{content} );
150 4 ▶ return join "\n", @parts if @parts;
Branch: 1 branch
BranchTrueFalseTotal
if @parts 3 1 4
151 }
152 4 return _placeholder( $type, $src->{media_type}, $src->{url} );
153 }
154  
155 15 ▶sub _native_parts {
Subroutine _native_parts: called CC=4 SCAR=14.5
156 15 my ($parts) = @_;
157 15 ▶ return () unless defined $parts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parts 0 15 15
158 15 ▶ return ($parts) unless ref $parts;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $parts 0 15 15
159 15 ▶ return () unless ref $parts eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $parts eq "ARRAY" 0 15 15
160 15 return map { _native_string($_) } @$parts;
161 }
162  
163 # The whole result as one string: parts joined with "\n"; empty content falls
164 # back to the JSON-encoded structured_content, else ''. _string_of renders a
165 # subset, for a wire that carries the other items natively (karr k344).
166 118 ▶sub _string_content {
Subroutine _string_content: called CC=1 SCAR=0.0
167 118 my ($self) = @_;
168 118 return $self->_string_of( $self->_mcp_items );
169 }
170  
171 190 ▶sub _string_of {
Subroutine _string_of: called CC=3 SCAR=11.0
172 190 my ( $self, @items ) = @_;
173 190 my @parts = map { _string_item($_) } @items;
174 190 ▶ return join( "\n", @parts ) if @parts;
Branch: 1 branch
BranchTrueFalseTotal
if @parts 169 21 190
175 21 ▶ return $JSON->encode( $self->structured_content ) if $self->has_structured_content;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_structured_content 6 15 21
176 15 return '';
177 }
178  
179 # --- Native tool-result images (karr k344) ---
180 #
181 # Two wires take an image inside a tool result: the Open Responses
182 # function_call_output (output = string | array of input_text / input_image
183 # parts; OpenAI /v1/responses and Perplexity /v1/agent) and Gemini 3's
184 # functionResponse.parts[].inlineData (v1beta). Docs-derived, not
185 # live-verified (2026-09-29). The caller (Role::Tools) passes image_input => 1
186 # only when the model sees images; without it, and on every other wire but
187 # Anthropic (same gate, karr k359, below), the image stays the k336
188 # placeholder.
189  
190 183 my %RESPONSES_IMAGE_MIME = map { $_ => 1 } qw( image/jpeg image/png image/gif image/webp );
191 183 my %GEMINI_IMAGE_MIME = map { $_ => 1 } qw( image/jpeg image/png image/webp );
192  
193 # The base64 payload of an item the wire takes as an image, else undef.
194 53 ▶sub _native_image_data {
Subroutine _native_image_data: called CC=8 SCAR=21.0
195 53 my ( $item, $mimes ) = @_;
196 53 ▶ return undef unless $item->{kind} eq 'blob' && defined $item->{data} && !ref $item->{data};
Branch: 1 branch
BranchTrueFalseTotal
unless $item->{"kind"} eq "blob" and defined $item->{"data"} and not ref $item->{"data"} 20 33 53
Condition66%4/6
$item->{"kind"} eq "blob" and defined $item->{"data"} and not ref $item->{"data"}
!ll&&!rl&&r
20033
$item->{"kind"} eq "blob" and defined $item->{"data"}
!ll&&!rl&&r
20033
MC/DC33%1/3
$item->{"kind"} eq "blob" and defined $item->{"data"} and not ref $item->{"data"}
$item->{"kind"} eq "blob" defined $item->{"data"} not ref $item->{"data"}
Truth table50%2/4
A and B and not(C)
A= $item->{"kind"} eq "blob"B= defined $item->{"data"}C= not ref $item->{"data"}
ABCresult
0XX0
10X0
1100
1111
197 33 ▶ return undef unless $item->{type} eq 'image' || $item->{type} eq 'resource';
Branch: 1 branch
BranchTrueFalseTotal
unless $item->{"type"} eq "image" or $item->{"type"} eq "resource" 2 31 33
Condition100%3/3
$item->{"type"} eq "image" or $item->{"type"} eq "resource"
l!l&&r!l&&!r
18132
MC/DC100%2/2
$item->{"type"} eq "image" or $item->{"type"} eq "resource"
$item->{"type"} eq "image" $item->{"type"} eq "resource"
Truth table100%3/3
A or B
A= $item->{"type"} eq "image"B= $item->{"type"} eq "resource"
ABresult
1X1
011
000
198 31 ▶ return undef unless $mimes->{ $item->{mime} // '' };
Branch: 1 branch
BranchTrueFalseTotal
unless $$mimes{$item->{"mime"} // ""} 15 16 31
Condition50%1/2
$item->{"mime"} // ""
l!l
310
MC/DC0%0/1
$item->{"mime"} // ""
$item->{"mime"}
Truth table50%1/2
A
A= $item->{"mime"}
Aresult
11
00
199 16 ( my $b64 = $item->{data} ) =~ s/\s+//g;
200 16 return $b64;
201 }
202  
203 # --- Native tool-result PDFs (karr k361) ---
204 #
205 # The same two wires take a PDF inside a tool result: OpenAI /v1/responses as
206 # an input_file part of function_call_output.output (API reference,
207 # FunctionCallOutput: string | [input_text | input_image | input_file]; the
208 # PDF-files guide sends file_data as a data:application/pdf;base64,... URL
209 # with a filename), Gemini 3 as functionResponse.parts[].inlineData
210 # (generate-content function-calling guide: "Documents: application/pdf,
211 # text/plain"). Perplexity's /v1/agent lists only input_text / input_image.
212 # Docs-derived, not live-verified (developers.openai.com, ai.google.dev,
213 # docs.perplexity.ai, 2026-09-30). The caller (Role::Tools) passes
214 # native_pdf => 1 only where the wire takes it and the model claims
215 # image_input; otherwise the PDF stays the k336 placeholder. A PDF is an MCP
216 # embedded resource blob, the one MCP shape that carries a document (as on the
217 # anthropic wire, k326).
218  
219 # The base64 payload of an item that is a PDF, else undef.
220 37 ▶sub _native_pdf_data {
Subroutine _native_pdf_data: called CC=7 SCAR=20.0
221 37 my ($item) = @_;
222 37 ▶ return undef unless $item->{kind} eq 'blob' && $item->{type} eq 'resource';
Branch: 1 branch
BranchTrueFalseTotal
unless $item->{"kind"} eq "blob" and $item->{"type"} eq "resource" 15 22 37
Condition100%3/3
$item->{"kind"} eq "blob" and $item->{"type"} eq "resource"
!ll&&!rl&&r
14122
MC/DC100%2/2
$item->{"kind"} eq "blob" and $item->{"type"} eq "resource"
$item->{"kind"} eq "blob" $item->{"type"} eq "resource"
Truth table100%3/3
A and B
A= $item->{"kind"} eq "blob"B= $item->{"type"} eq "resource"
ABresult
0X0
100
111
223 22 ▶ return undef unless ( $item->{mime} // '' ) eq 'application/pdf';
Branch: 1 branch
BranchTrueFalseTotal
unless ($item->{"mime"} // "") eq "application/pdf" 1 21 22
Condition50%1/2
$item->{"mime"} // ""
l!l
220
MC/DC0%0/1
$item->{"mime"} // ""
$item->{"mime"}
Truth table50%1/2
A
A= $item->{"mime"}
Aresult
11
00
224 21 ▶ return undef unless defined $item->{data} && !ref $item->{data};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item->{"data"} and not ref $item->{"data"} 0 21 21
Condition33%1/3
defined $item->{"data"} and not ref $item->{"data"}
!ll&&!rl&&r
0021
MC/DC0%0/2
defined $item->{"data"} and not ref $item->{"data"}
defined $item->{"data"} not ref $item->{"data"}
Truth table33%1/3
A and not(B)
A= defined $item->{"data"}B= not ref $item->{"data"}
ABresult
0X0
100
111
225 21 ( my $b64 = $item->{data} ) =~ s/\s+//g;
226 21 return $b64;
227 }
228  
229 # input_file's filename: the reference marks it optional, but the server
230 # rejects file_data without one (community reports, 2025-2026). The last
231 # segment of the resource URI's hierarchical path, percent-decoded (as UTF-8
232 # characters, like every string here -- k252), with .pdf appended when it has
233 # no such extension (the name is what the model sees the attachment as). An
234 # opaque URI (urn:, data:, a rootless scheme:path) or an empty path gives a
235 # generic name.
236 15 ▶sub _pdf_filename {
Subroutine _pdf_filename: called CC=9 SCAR=22.0
237 15 my ($uri) = @_;
238 15 my $name = '';
239 15 ▶ if ( defined $uri && !ref $uri && length $uri ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uri and not ref $uri and length $uri) 14 1 15
Condition66%4/6
defined $uri and not ref $uri and length $uri
!ll&&!rl&&r
1014
defined $uri and not ref $uri
!ll&&!rl&&r
1014
MC/DC33%1/3
defined $uri and not ref $uri and length $uri
defined $uri not ref $uri length $uri
Truth table50%2/4
A and not(B) and C
A= defined $uriB= not ref $uriC= length $uri
ABCresult
0XX0
10X0
1100
1111
240 14 my $parsed = URI->new($uri);
241 14 ▶ if ( $parsed->isa('URI::_generic')
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed->isa("URI::_generic") and !defined($parsed->scheme) || $parsed->path =~ m[\A/]) 11 3 14
Condition100%6/6
$parsed->isa("URI::_generic") and !defined($parsed->scheme) || $parsed->path =~ m[\A/]
!ll&&!rl&&r
1211
not defined $parsed->scheme or $parsed->path =~ m[\A/]
l!l&&r!l&&!r
1102
MC/DC100%3/3
$parsed->isa("URI::_generic") and !defined($parsed->scheme) || $parsed->path =~ m[\A/]
$parsed->isa("URI::_generic") not defined $parsed->scheme $parsed->path =~ m[\A/]
Truth table100%4/4
A and not(B) or C
A= $parsed->isa("URI::_generic")B= not defined $parsed->schemeC= $parsed->path =~ m[\A/]
ABCresult
0XX0
1000
11X1
1011
242 && ( !defined $parsed->scheme || $parsed->path =~ m{\A/} ) ) {
243 11 my ($segment) = $parsed->path =~ m{([^/]*)\z};
244 11 $name = Encode::decode( 'UTF-8', URI::Escape::uri_unescape($segment),
245 Encode::FB_DEFAULT() );
246 }
247 }
248 15 ▶ return 'document.pdf' unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 5 10 15
249 10 ▶ return $name =~ /\.pdf\z/i ? $name : "$name.pdf";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\.pdf\z/i ? : 7 3 10
250 }
251  
252 # --- Serializers to per-provider result blocks ---
253  
254 71 ▶sub to_openai {
Subroutine to_openai: called CC=1 SCAR=0.0
Pod undocumented
255 71 my ($self) = @_;
256 # The chat tool message takes a string (or text parts only) -- karr k336.
257 return {
258 71 role => 'tool',
259 tool_call_id => $self->id,
260 content => $self->_string_content,
261 };
262 }
263  
264 26 ▶sub to_ollama {
Subroutine to_ollama: called CC=2 SCAR=6.9
Pod undocumented
265 26 my ($self) = @_;
266 # Ollama's Message carries tool_name and tool_call_id (both omitempty); with
267 # same-name parallel calls they are what correlates a result (karr k328).
268 return {
269 26 ▶ role => 'tool',
Branch: 1 branch
BranchTrueFalseTotal
length $self->id ? : 16 10 26
270 tool_name => $self->name,
271 ( length( $self->id ) ? ( tool_call_id => $self->id ) : () ),
272 content => $self->_string_content,
273 };
274 }
275  
276 51 ▶sub to_responses {
Subroutine to_responses: called CC=1 SCAR=0.0
Pod undocumented
277 51 my ( $self, %opts ) = @_;
278 # A Responses API input item, not a chat message: the wire discriminates on
279 # `type`, carries the payload in `output`, and has no `role` at all.
280 return {
281 51 type => 'function_call_output',
282 call_id => $self->id,
283 output => $self->_responses_output(%opts),
284 };
285 }
286  
287 # One item as a native Responses part (input_image with image_input, karr
288 # k344; input_file with native_pdf, karr k361), else undef.
289 68 ▶sub _responses_native_part {
Subroutine _responses_native_part: called CC=5 SCAR=16.1
290 68 my ( $item, %opts ) = @_;
291 68 ▶ if ( $opts{image_input} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"image_input"}) 27 41 68
292 27 my $b64 = _native_image_data( $item, \%RESPONSES_IMAGE_MIME );
293 27 ▶ return { type => 'input_image', image_url => "data:$item->{mime};base64,$b64" }
Branch: 1 branch
BranchTrueFalseTotal
if defined $b64 8 19 27
294 if defined $b64;
295 }
296 60 ▶ if ( $opts{native_pdf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"native_pdf"}) 25 35 60
297 25 my $b64 = _native_pdf_data($item);
298 25 ▶ return { type => 'input_file', filename => _pdf_filename( $item->{uri} ),
Branch: 1 branch
BranchTrueFalseTotal
if defined $b64 15 10 25
299 file_data => "data:application/pdf;base64,$b64" } if defined $b64;
300 }
301 45 return undef;
302 }
303  
304 # With an item the wire takes natively, `output` becomes a part array in
305 # content order (one input_text per other item, as the string form renders
306 # it); without one it stays the string (karr k344, k361).
307 51 ▶sub _responses_output {
Subroutine _responses_output: called CC=3 SCAR=11.0
308 51 my ( $self, %opts ) = @_;
309 51 my @items = $self->_mcp_items;
310 51 my @native = map { _responses_native_part( $_, %opts ) } @items;
311 51 ▶ return $self->_string_of(@items) unless grep { defined } @native;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_;} @native 29 22 51
312 return [ map {
313 22 ▶ $native[$_] // { type => 'input_text', text => _string_item( $items[$_] ) }
Condition100%2/2
$native[$_] // {"type", "input_text", "text", _string_item($items[$_])}
l!l
237
MC/DC100%1/1
$native[$_] // {"type", "input_text", "text", _string_item($items[$_])}
$native[$_]
Truth table100%2/2
A
A= $native[$_]
Aresult
11
00
314 } 0 .. $#items ];
315 }
316  
317 # --- Anthropic tool_result content (karr k326) ---
318 #
319 # Anthropic's tool_result takes text | image | document | search_result blocks
320 # and rejects unknown fields, so MCP blocks are mapped, not embedded. The mapping
321 # follows anthropic-sdk-python lib/tools/mcp.py, except that nothing dies inside
322 # the tool loop: what Anthropic cannot carry (audio, resource_link, unsupported
323 # MIME types) becomes a text placeholder.
324 #
325 # Two options, both decided by the caller (Role::Tools) per engine and model:
326 # image_input an MCP image becomes an image block only with it (karr k359),
327 # as on the responses / gemini wires: the /anthropic shims
328 # answer 200 whether or not the model sees it. An
329 # Anthropic-native image block the caller built passes through.
330 # source_blocks 0 on a wire that rejects Anthropic's source blocks
331 # (document, search_result) in a tool_result (karr k364, k366:
332 # AKI's shim answers 529 "Unsupported content type: document" /
333 # "... search_result"): a text document goes inline as a text
334 # block, as on the string wires, a PDF as the placeholder. That
335 # is wire truth, not a model question, so a native document or
336 # search_result is degraded the same way (Role::Tools carps
337 # when it does). Default 1.
338  
339 183 my %ANTHROPIC_IMAGE_MIME = map { $_ => 1 } qw( image/jpeg image/png image/gif image/webp );
340  
341 # The Anthropic-native block types source_blocks => 0 degrades.
342 183 my %ANTHROPIC_SOURCE_BLOCK = map { $_ => 1 } qw( document search_result );
343  
344 108 ▶sub _anthropic_block {
Subroutine _anthropic_block: called CC=19 SCAR=29.4
345 108 my ( $item, %opts ) = @_;
346 108 my $kind = $item->{kind};
347 108 ▶ my $source_blocks = $opts{source_blocks} // 1;
Condition100%2/2
$opts{"source_blocks"} // 1
l!l
3474
MC/DC100%1/1
$opts{"source_blocks"} // 1
$opts{"source_blocks"}
Truth table100%2/2
A
A= $opts{"source_blocks"}
Aresult
11
00
348 108 ▶ if ( $kind eq 'native' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "native") 24 84 108
349 24 ▶ return $item->{block} if $source_blocks || !$ANTHROPIC_SOURCE_BLOCK{ $item->{block}{type} };
Branch: 1 branch
BranchTrueFalseTotal
if $source_blocks or not $ANTHROPIC_SOURCE_BLOCK{$item->{"block"}{"type"}} 9 15 24
Condition100%3/3
$source_blocks or not $ANTHROPIC_SOURCE_BLOCK{$item->{"block"}{"type"}}
l!l&&r!l&&!r
8115
MC/DC100%2/2
$source_blocks or not $ANTHROPIC_SOURCE_BLOCK{$item->{"block"}{"type"}}
$source_blocks not $ANTHROPIC_SOURCE_BLOCK{$item->{"block"}{"type"}}
Truth table100%3/3
A or not(B)
A= $source_blocksB= not $ANTHROPIC_SOURCE_BLOCK{$item->{"block"}{"type"}}
ABresult
1X1
011
000
350 15 return { type => 'text', text => _string_item($item) };
351 }
352 84 ▶ if ( $kind eq 'text' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "text") 30 54 84
353 return { type => 'text', text => $item->{text},
354 30 ▶ ( exists $item->{cache_control} ? ( cache_control => $item->{cache_control} ) : () ) };
Branch: 1 branch
BranchTrueFalseTotal
exists $item->{'cache_control'} ? : 1 29 30
355 }
356 54 ▶ return { type => 'text', text => $item->{text} } if $kind eq 'note';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "note" 4 50 54
357 50 ▶ if ( $kind eq 'document' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "document") 12 38 50
358 12 ▶ return { type => 'text', text => $item->{text} } unless $source_blocks;
Branch: 1 branch
BranchTrueFalseTotal
unless $source_blocks 5 7 12
359 return { type => 'document',
360 7 source => { type => 'text', media_type => 'text/plain', data => $item->{text} } };
361 }
362 38 ▶ my ( $type, $mime, $data ) = ( $item->{type}, $item->{mime} // '', $item->{data} );
Condition100%2/2
$item->{"mime"} // ""
l!l
371
MC/DC100%1/1
$item->{"mime"} // ""
$item->{"mime"}
Truth table100%2/2
A
A= $item->{"mime"}
Aresult
11
00
363 38 ▶ if ( defined $data && ( $type eq 'image' || $type eq 'resource' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data and $type eq "image" || $type eq "resource") 34 4 38
Condition100%6/6
defined $data and $type eq "image" || $type eq "resource"
!ll&&!rl&&r
1334
$type eq "image" or $type eq "resource"
l!l&&r!l&&!r
22123
MC/DC100%3/3
defined $data and $type eq "image" || $type eq "resource"
defined $data $type eq "image" $type eq "resource"
Truth table100%4/4
A and B or C
A= defined $dataB= $type eq "image"C= $type eq "resource"
ABCresult
0XX0
1000
11X1
1011
364 return { type => 'image', source => { type => 'base64', media_type => $mime, data => $data } }
365 34 ▶ if $opts{image_input} && $ANTHROPIC_IMAGE_MIME{$mime};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"image_input"} and $ANTHROPIC_IMAGE_MIME{$mime} 15 19 34
Condition100%3/3
$opts{"image_input"} and $ANTHROPIC_IMAGE_MIME{$mime}
!ll&&!rl&&r
13615
MC/DC100%2/2
$opts{"image_input"} and $ANTHROPIC_IMAGE_MIME{$mime}
$opts{"image_input"} $ANTHROPIC_IMAGE_MIME{$mime}
Truth table100%3/3
A and B
A= $opts{"image_input"}B= $ANTHROPIC_IMAGE_MIME{$mime}
ABresult
0X0
100
111
366 19 ▶ return { type => 'document',
Branch: 1 branch
BranchTrueFalseTotal
if $source_blocks and $type eq "resource" and $mime eq "application/pdf" 6 13 19
Condition100%6/6
$source_blocks and $type eq "resource" and $mime eq "application/pdf"
!ll&&!rl&&r
1126
$source_blocks and $type eq "resource"
!ll&&!rl&&r
838
MC/DC100%3/3
$source_blocks and $type eq "resource" and $mime eq "application/pdf"
$source_blocks $type eq "resource" $mime eq "application/pdf"
Truth table100%4/4
A and B and C
A= $source_blocksB= $type eq "resource"C= $mime eq "application/pdf"
ABCresult
0XX0
10X0
1100
1111
367 source => { type => 'base64', media_type => 'application/pdf', data => $data } }
368 if $source_blocks && $type eq 'resource' && $mime eq 'application/pdf';
369 }
370 17 return { type => 'text', text => _placeholder( @{$item}{qw( type mime uri data )} ) };
371 }
372  
373 94 ▶sub _anthropic_content {
Subroutine _anthropic_content: called CC=3 SCAR=11.0
374 94 my ( $self, %opts ) = @_;
375 94 my @blocks = map { _anthropic_block( $_, %opts ) } $self->_mcp_items;
376 94 ▶ return \@blocks if @blocks;
Branch: 1 branch
BranchTrueFalseTotal
if @blocks 91 3 94
377 3 ▶ return $JSON->encode( $self->structured_content ) if $self->has_structured_content;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_structured_content 2 1 3
378 1 return '';
379 }
380  
381 # The types of the Anthropic-native source blocks a caller put into the
382 # content, each once: what source_blocks => 0 degrades. Role::Tools warns about
383 # them (karr k366); an MCP resource is the normal path and not among them.
384 20 ▶sub _native_source_block_types {
Subroutine _native_source_block_types: called CC=2 SCAR=7.1
385 20 my ($self) = @_;
386 20 my %seen;
387 11 ▶ return grep { $ANTHROPIC_SOURCE_BLOCK{$_} && !$seen{$_}++ }
Condition33%1/3
$ANTHROPIC_SOURCE_BLOCK{$_} && !$seen{$_}++
!ll&&!rl&&r
0011
MC/DC0%0/2
$ANTHROPIC_SOURCE_BLOCK{$_} && !$seen{$_}++
$ANTHROPIC_SOURCE_BLOCK{$_} !$seen{$_}++
Truth table33%1/3
A && not(B)
A= $ANTHROPIC_SOURCE_BLOCK{$_}B= !$seen{$_}++
ABresult
0X0
100
111
388 20 map { $_->{block}{type} } grep { $_->{kind} eq 'native' } $self->_mcp_items;
389 }
390  
391 94 ▶sub to_anthropic {
Subroutine to_anthropic: called CC=2 SCAR=6.9
Pod undocumented
392 94 my ( $self, %opts ) = @_;
393 return {
394 94 ▶ type => 'tool_result',
Branch: 1 branch
BranchTrueFalseTotal
$self->is_error ? : 8 86 94
395 tool_use_id => $self->id,
396 content => $self->_anthropic_content(%opts),
397 ( $self->is_error ? ( is_error => JSON::MaybeXS::true() ) : () ),
398 };
399 }
400  
401 45 ▶sub to_gemini {
Subroutine to_gemini: called CC=10 SCAR=23.0
Pod undocumented
402 45 my ( $self, %opts ) = @_;
403 # With image_input, an image Gemini 3 takes moves out of the result string
404 # into functionResponse.parts[].inlineData (karr k344), with native_pdf a
405 # PDF likewise (karr k361). No displayName: it is only needed to $ref a
406 # part from `response`, and the v1beta discovery schema of
407 # FunctionResponseBlob does not list it.
408 45 my ( @rest, @parts );
409 45 for my $item ( $self->_mcp_items ) {
410 60 my ( $b64, $mime );
411 60 ▶ $b64 = _native_image_data( $item, \%GEMINI_IMAGE_MIME ) if $opts{image_input};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"image_input"} 26 34 60
412 60 ▶ $mime = $item->{mime} if defined $b64;
Branch: 1 branch
BranchTrueFalseTotal
if defined $b64 8 52 60
413 60 ▶ if ( !defined $b64 && $opts{native_pdf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $b64 and $opts{"native_pdf"}) 12 48 60
Condition100%3/3
not defined $b64 and $opts{"native_pdf"}
!ll&&!rl&&r
84012
MC/DC100%2/2
not defined $b64 and $opts{"native_pdf"}
not defined $b64 $opts{"native_pdf"}
Truth table100%3/3
not(A) and B
A= not defined $b64B= $opts{"native_pdf"}
ABresult
0X0
100
111
414 12 $b64 = _native_pdf_data($item);
415 12 $mime = 'application/pdf';
416 }
417 60 ▶ if ( defined $b64 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $b64) { } 14 46 60
418 14 push @parts, { inlineData => { mimeType => $mime, data => $b64 } };
419 }
420 else {
421 46 push @rest, $item;
422 }
423 }
424 # functionResponse.response is a JSON object: the MCP structuredContent when
425 # the tool gave one, else the result string under `result` (karr k336).
426 # It is required, so an image-only result still sends a `result` string
427 # ('' unless a non-hash structuredContent fills it).
428 45 my $structured = $self->structured_content;
429 return {
430 45 ▶ functionResponse => {
Branch: 3 branches
BranchTrueFalseTotal
length $self->id ? : 34 11 45
ref $structured eq 'HASH' ? : 2 43 45
@parts ? : 13 32 45
431 name => $self->name,
432 # Gemini 3 requires the functionCall's id back; 2.5 may send none, and
433 # an invented one would match nothing (karr k328).
434 ( length( $self->id ) ? ( id => $self->id ) : () ),
435 response => ( ref $structured eq 'HASH'
436 ? $structured : { result => $self->_string_of(@rest) } ),
437 ( @parts ? ( parts => \@parts ) : () ),
438 },
439 };
440 }
441  
442 21 ▶sub to_hermes {
Subroutine to_hermes: called CC=2 SCAR=6.9
Pod undocumented
443 21 my ( $self, %opts ) = @_;
444 21 ▶ my $tag = $opts{response_tag} // 'tool_response';
Condition100%2/2
$opts{"response_tag"} // "tool_response"
l!l
156
MC/DC100%1/1
$opts{"response_tag"} // "tool_response"
$opts{"response_tag"}
Truth table100%2/2
A
A= $opts{"response_tag"}
Aresult
11
00
445 21 return "<${tag}>\n"
446 . $JSON->encode( { name => $self->name, content => $self->_string_content } )
447 . "\n</${tag}>";
448 }
449  
450 # --- Tag-driven dispatch ---
451  
452 183 my %TO_METHOD = (
453 openai => 'to_openai',
454 anthropic => 'to_anthropic',
455 gemini => 'to_gemini',
456 ollama => 'to_ollama',
457 responses => 'to_responses',
458 hermes => 'to_hermes',
459 );
460  
461  
462 308 ▶sub to {
Subroutine to: called CC=4 SCAR=16.9
Pod documented
463 308 my ( $self, $fmt, %opts ) = @_;
464 308 ▶ my $method = $TO_METHOD{ $fmt // '' }
Branch: 1 branch
BranchTrueFalseTotal
my $method = $TO_METHOD{$fmt // ""} or croak(q[Langertha::ToolResult: unknown wire format '] . ($fmt // "") . q[']) 308 0 308
Condition25%1/4
$fmt // ""
l!l
3080
$fmt // ""
l!l
00
MC/DC0%0/1
$fmt // ""
$fmt
MC/DC0%0/1
$fmt // ""
$fmt
Truth table50%1/2
A
A= $fmt
Aresult
11
00
Truth table0%0/2
A
A= $fmt
Aresult
11
00
465 or croak "Langertha::ToolResult: unknown wire format '" . ( $fmt // '' ) . "'";
466 308 return $self->$method(%opts);
467 }
468  
469 183 __PACKAGE__->meta->make_immutable;
470  
471  
472 183 1;
473  
474 __END__
475  
476 =pod
477  
478 =encoding UTF-8
479  
480 =head1 NAME
481  
482 Langertha::ToolResult - Immutable canonical result of executing one tool, with cross-provider conversion
483  
484 =head1 VERSION
485  
486 version 0.503
487  
488 =head1 SYNOPSIS
489  
490 use Langertha::ToolResult;
491  
492 my $result = Langertha::ToolResult->new(
493 name => 'get_weather',
494 id => 'call_abc',
495 content => [ { type => 'text', text => 'Sunny, 22C' } ],
496 is_error => 0,
497 );
498  
499 my $block = $result->to('anthropic');
500 # { type => 'tool_result', tool_use_id => 'call_abc', content => [...] }
501  
502 =head1 DESCRIPTION
503  
504 Canonical, provider-neutral result of a single tool execution. Serializes to
505 the per-provider result I<block> via C<to($fmt)> — one block per result. The
506 surrounding message envelope (arity, the assistant echo of the prior turn) is
507 assembled by L<Langertha::Role::Tools>, not here: a ToolResult knows only its
508 own block shape.
509  
510 The C<content> is the MCP-style content array (C<[ { type => 'text', text =>
511 ... } ]>). OpenAI, OpenAI Responses, Ollama and Hermes send it as one string:
512 text parts joined with C<"\n">, an embedded text resource as its text, a
513 C<text/*> blob decoded as UTF-8, a C<resource_link> as
514 C<[resource_link] name E<lt>uriE<gt>>, and an image, audio or binary blob as a
515 placeholder such as C<[image] image/png (12345 bytes)> -- never the base64
516 payload. Gemini sends that string as C<< { result => ... } >>, or the
517 L</structured_content> object itself when there is one.
518  
519 Anthropic takes structured blocks, so each MCP block is mapped onto one: text
520 keeps only C<text> (and C<cache_control>); an image, or an embedded resource
521 whose blob is an image, becomes a base64 C<image> (JPEG, PNG, GIF, WebP) when
522 C<to> is called with C<< image_input => 1 >> (see below), else the text
523 placeholder; a PDF blob becomes a base64 C<document>; a text resource (whatever
524 its MIME type) or a C<text/*> blob becomes a text C<document>. Anthropic-native
525 C<image> / C<document> blocks (with a C<source>) and C<search_result> pass
526 through. Everything else -- C<resource_link>, audio, other MIME types -- becomes
527 the same text placeholder, naming type, MIME type, URI and size.
528  
529 With C<< source_blocks => 0 >> the Anthropic form sends no C<document> or
530 C<search_result> block, for a server that rejects them inside a
531 C<tool_result>: a text resource or C<text/*> blob becomes a C<text> block
532 holding the text (as on the string wires), a PDF blob the placeholder, and an
533 Anthropic-native C<document> or C<search_result> a C<text> block with the text
534 it has in the string form (below). L<Langertha::Role::Tools/format_tool_results>
535 passes it on the C</anthropic> shims of AKI.IO, Moonshot and
536 (conservatively, not live-verified) LM Studio.
537  
538 An Anthropic-native block on a string wire (or with C<< source_blocks => 0 >>)
539 keeps its text: a C<document> with a C<text> source gives that text, one with a
540 C<content> source its inner parts joined with C<"\n"> (an image among them as a
541 placeholder), a C<search_result> a line C<[search_result] title E<lt>sourceE<gt>>
542 followed by its text parts. Anything else becomes a placeholder.
543  
544 On every string wire and on Anthropic, empty content goes out as the
545 JSON-encoded L</structured_content>, or as C<''>.
546  
547 Three wires carry an image natively, and do so only when C<to> is called with
548 C<< image_input => 1 >> (L<Langertha::Role::Tools/format_tool_results> passes
549 it when the model claims C<image_input>):
550  
551 =over
552  
553 =item * C<anthropic> -- the base64 C<image> block above. Documented for
554 first-party Anthropic; on the C</anthropic> shims it is not live-verified:
555 Kimi's schema lists C<image> inside a C<tool_result> (docs only), MiniMax
556 documents nothing about it, and the only live evidence is negative (AKI.IO's
557 shim accepts it but its models do not see it, so that engine never sends it).
558  
559 =item * C<responses> -- C<output> becomes an array of parts in content order:
560 an image (C<image> block or image resource; JPEG, PNG, GIF, WebP) is an
561 C<input_image> with a C<data:> URL in C<image_url>, every other item an
562 C<input_text> with the text it has in the string form. Without such an image
563 C<output> stays the string. OpenAI C</v1/responses> and Perplexity
564 C</v1/agent> document the same shape.
565  
566 =item * C<gemini> -- an image (JPEG, PNG, WebP) moves out of the C<result>
567 string into C<< functionResponse.parts[].inlineData >> (C<mimeType>,
568 C<data>). Documented for the Gemini 3 series (v1beta) only.
569  
570 =back
571  
572 The C<responses> and C<gemini> forms are B<documentation-derived, not
573 live-verified>. C<openai>, C<ollama> and C<hermes> have no image form in a
574 tool result and ignore the option: the image stays a placeholder there.
575  
576 A PDF (an embedded resource whose blob is C<application/pdf>) rides natively
577 on two more of those wires when C<to> is called with C<< native_pdf => 1 >>
578 (L<Langertha::Role::Tools/format_tool_results> passes it on OpenAI Responses
579 and Gemini 3, for a model that claims C<image_input>):
580  
581 =over
582  
583 =item * C<responses> -- C<output> becomes the part array as above, the PDF an
584 C<input_file> with C<file_data> (a C<data:application/pdf;base64,...> URL) and
585 a C<filename>: the percent-decoded last segment of the resource URI's
586 hierarchical path, with C<.pdf> appended when missing; an opaque URI
587 (C<urn:>, C<data:>) or an empty path gives C<document.pdf>. Documented for OpenAI
588 C</v1/responses>; Perplexity's C</v1/agent> takes no C<input_file>, so the
589 PDF stays the placeholder there.
590  
591 =item * C<gemini> -- the PDF moves out of the C<result> string into
592 C<< functionResponse.parts[].inlineData >> with C<mimeType>
593 C<application/pdf>. Documented for the Gemini 3 series.
594  
595 =back
596  
597 Both are B<documentation-derived, not live-verified>. The other wires ignore
598 C<native_pdf>: C<openai>, C<ollama> and C<hermes> keep the placeholder, and
599 C<anthropic> sends its C<document> block whenever C<source_blocks> allows it.
600  
601 Not every block is a chat message: the OpenAI Responses block is an C<input>
602 I<item> discriminated by C<type> (C<function_call_output>), carrying its payload
603 in C<output> and no C<role> at all.
604  
605 =head2 name
606  
607 The tool's name. Used by formats that key results by name (Gemini, Hermes,
608 Ollama's C<tool_name>).
609  
610 =head2 id
611  
612 The provider call id this result answers (C<tool_call_id> / C<tool_use_id> /
613 C<call_id>; Gemini's C<functionResponse.id>, Ollama's C<tool_call_id>). May be
614 empty; Gemini and Ollama then send no id at all.
615  
616 =head2 content
617  
618 The MCP-style content array of the tool's output, e.g.
619 C<[ { type => 'text', text => '...' } ]>.
620  
621 =head2 is_error
622  
623 Boolean. True when the tool execution failed; surfaced on formats that carry an
624 error flag (Anthropic C<is_error>).
625  
626 =head2 structured_content
627  
628 The MCP C<structuredContent> of the tool's output, if any. Sent JSON-encoded as
629 the result string when C<content> is empty; Gemini sends the object as its
630 C<functionResponse.response> whenever it is present.
631  
632 =head2 to
633  
634 my $block = $result->to($fmt);
635 my $block = $result->to('hermes', response_tag => 'fn_response');
636 my $block = $result->to('responses', image_input => 1);
637 my $block = $result->to('gemini', image_input => 1, native_pdf => 1);
638  
639 Serializes to the result block for the given C<tool_wire_format>. Extra options
640 are passed through to the per-format serializer (Hermes accepts
641 C<response_tag>; C<responses>, C<gemini> and C<anthropic> accept
642 C<image_input>, C<responses> and C<gemini> also C<native_pdf>, C<anthropic>
643 also C<source_blocks>, see L</DESCRIPTION>; the other formats ignore them).
644  
645 =head1 SEE ALSO
646  
647 =over
648  
649 =item * L<Langertha::ToolCall> - The invocation a ToolResult answers
650  
651 =item * L<Langertha::Tool> - The tool definition
652  
653 =item * L<Langertha::Role::Tools> - Assembles result blocks into the message envelope
654  
655 =back
656  
657 =head1 SUPPORT
658  
659 =head2 Issues
660  
661 Please report bugs and feature requests on GitHub at
662 L<https://github.com/Getty/langertha/issues>.
663  
664 =head2 IRC
665  
666 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
667  
668 =head1 CONTRIBUTING
669  
670 Contributions are welcome! Please fork the repository and submit a pull request.
671  
672 =head1 AUTHOR
673  
674 Torsten Raudssus <getty@cpan.org>
675  
676 =head1 COPYRIGHT AND LICENSE
677  
678 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
679  
680 This is free software; you can redistribute it and/or modify it under
681 the same terms as the Perl 5 programming language system itself.
682  
683 =cut
« blib/lib/Langertha/Engine/AKI.pm ↑ Summary blib/lib/Langertha/HTTP/ConnectCheck.pm »