blib/lib/Langertha/CachedContent.pm

filter: Statementstmt 89.1% 99 / 111 Branchbran 69.5% 64 / 92 Conditioncond 59.3% 19 / 32 MC/DCmcdc 38.1% 8 / 21 Subroutinesub 100% 19 / 19 Podpod 83.3% 5 / 6 totaltotal 76.1% 214 / 281 CC 58 SCAR 26.0
CC
58
Cov
77%
_parse_duration_seconds
24.4
from_hash
24.1
_parse_rfc3339_unix
14.6
CRAP 13.4
« blib/lib/Langertha/Role/Capabilities.pm ↑ Summary blib/lib/Langertha/Role/AsyncHTTP.pm »
1 package Langertha::CachedContent;
2 # ABSTRACT: Immutable value object for a Gemini explicit cachedContent resource
3 60 our $VERSION = '0.503';
4 60 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 60 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 60 has name => (
9 is => 'ro',
10 isa => 'Maybe[Str]',
11 predicate => 'has_name',
12 );
13  
14  
15 60 has model => (
16 is => 'ro',
17 isa => 'Str',
18 predicate => 'has_model',
19 );
20  
21  
22 has contents => (
23 is => 'ro',
24 isa => 'ArrayRef',
25 79 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 60 );
27  
28  
29 has system_instruction => (
30 is => 'ro',
31 isa => 'Maybe[Str]',
32 predicate => 'has_system_instruction',
33 78 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 60 );
35  
36  
37 has tools => (
38 is => 'ro',
39 isa => 'Maybe[ArrayRef]',
40 79 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 60 );
42  
43  
44 has ttl => (
45 is => 'ro',
46 isa => 'Maybe[Str]',
47 60 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 60 );
49  
50  
51 has expire_time => (
52 is => 'ro',
53 isa => 'Maybe[Str]',
54 52 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
55 60 );
56  
57  
58 has display_name => (
59 is => 'ro',
60 isa => 'Maybe[Str]',
61 69 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
62 60 );
63  
64  
65 has create_time => (
66 is => 'ro',
67 isa => 'Maybe[Str]',
68 74 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
69 60 );
70  
71  
72 has update_time => (
73 is => 'ro',
74 isa => 'Maybe[Str]',
75 74 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
76 60 );
77  
78  
79 has total_token_count => (
80 is => 'ro',
81 isa => 'Maybe[Int]',
82 56 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
83 60 );
84  
85  
86 # Build-time wire-truth gate: ttl and expire_time are mutually exclusive
87 # (the CachedContent resource's `expiration` field is a oneof — exactly one
88 # of expireTime|ttl). Croak early rather than emit an ambiguous wire form.
89 80 ▶sub BUILD {
Subroutine BUILD: called CC=5 SCAR=16.1
Pod undocumented
90 80 my ( $self ) = @_;
91  
92 80 ▶ if ( defined $self->ttl && defined $self->expire_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->ttl and defined $self->expire_time) 1 79 80
Condition100%3/3
defined $self->ttl and defined $self->expire_time
!ll&&!rl&&r
60191
MC/DC100%2/2
defined $self->ttl and defined $self->expire_time
defined $self->ttl defined $self->expire_time
Truth table100%3/3
A and B
A= defined $self->ttlB= defined $self->expire_time
ABresult
0X0
100
111
93 1 croak "Langertha::CachedContent: 'ttl' and 'expire_time' are mutually "
94 . "exclusive — pick one (expiration is a oneof on the wire)";
95 }
96  
97 # Name format: cachedContents/{id} when present.
98 79 ▶ if ( $self->has_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_name) 63 16 79
99 63 my $n = $self->name;
100 63 ▶ croak "Langertha::CachedContent: name must be 'cachedContents/{id}'; got '$n'"
Branch: 1 branch
BranchTrueFalseTotal
unless $n =~ m[\AcachedContents/[^/\s]+\z] 1 62 63
101 unless $n =~ m{\AcachedContents/[^/\s]+\z};
102 }
103 }
104  
105 # --- Wire serializers ---
106  
107 14 ▶sub to_create_body {
Subroutine to_create_body: called CC=9 SCAR=22.1
Pod documented
108 14 my ( $self ) = @_;
109 14 ▶ croak "Langertha::CachedContent: model is required to create a cache"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_model 0 14 14
110 unless $self->has_model;
111  
112 14 my %body = ( model => $self->model );
113  
114 14 ▶ if ( $self->has_system_instruction ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_system_instruction) 14 0 14
115 $body{systemInstruction} = {
116 14 parts => [ { text => $self->system_instruction } ],
117 };
118 }
119  
120 14 ▶ if ( $self->tools && @{ $self->tools } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tools and @{$self->tools;}) 1 13 14
Condition66%2/3
$self->tools and @{$self->tools;}
!ll&&!rl&&r
1301
MC/DC50%1/2
$self->tools and @{$self->tools;}
$self->tools @{$self->tools;}
Truth table66%2/3
A and B
A= $self->toolsB= @{$self->tools;}
ABresult
0X0
100
111
121 1 require Langertha::Tool;
122 # tools is Tool[] here as on generateContent: each Tool wraps its
123 # functionDeclarations, so the gemini tools payload goes out as is (k327).
124 1 $body{tools} = Langertha::Tool->format_list( 'gemini', $self->tools );
125 }
126  
127 14 ▶ if ( @{ $self->contents } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->contents;}) 1 13 14
128 1 $body{contents} = $self->contents;
129 }
130  
131 14 ▶ if ( defined $self->ttl ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->ttl) { } 13 1 14
elsif (defined $self->expire_time) { } 1 0 1
132 13 $body{ttl} = $self->ttl;
133 }
134 elsif ( defined $self->expire_time ) {
135 1 $body{expireTime} = $self->expire_time;
136 }
137  
138 14 ▶ if ( defined $self->display_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->display_name) 4 10 14
139 4 $body{displayName} = $self->display_name;
140 }
141  
142 14 return \%body;
143 }
144  
145  
146 # Convenience: the field that L<Langertha::Engine::Gemini> drops into a
147 # generateContent body to reference an existing cache.
148 2 ▶sub to_reference {
Subroutine to_reference: called CC=2 SCAR=6.9
Pod documented
149 2 my ( $self ) = @_;
150 2 ▶ croak "Langertha::CachedContent: cannot reference an unnamed cache (set 'name' first)"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_name 1 1 2
151 unless $self->has_name;
152 1 return { cachedContent => $self->name };
153 }
154  
155  
156 # --- Constructors from wire-format hashes ---
157  
158 52 ▶sub from_hash {
Subroutine from_hash: called CC=22 SCAR=31.8
Pod documented
159 52 my ( $class, $hash ) = @_;
160 52 ▶ return $hash if ref($hash) && eval { $hash->isa(__PACKAGE__) };
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash and eval { do { $hash->isa(__PACKAGE__) } } 0 52 52
Condition33%1/3
ref $hash and eval {
!ll&&!rl&&r
0520
MC/DC0%0/2
ref $hash and eval { do { $hash->isa(__PACKAGE__) } }
ref $hash eval { do { $hash->isa(__PACKAGE__) } }
Truth table33%1/3
A and B
A= ref $hashB= eval { do { $hash->isa(__PACKAGE__) } }
ABresult
0X0
100
111
161 52 ▶ return undef unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 52 52
162 52 ▶ return undef unless length( $hash->{name} // '' );
Branch: 1 branch
BranchTrueFalseTotal
unless length($hash->{"name"} // "") 0 52 52
Condition50%1/2
$hash->{"name"} // ""
l!l
520
MC/DC0%0/1
$hash->{"name"} // ""
$hash->{"name"}
Truth table50%1/2
A
A= $hash->{"name"}
Aresult
11
00
163  
164 # expiration is a proto oneof, flat in JSON: the server answers with a
165 # top-level expireTime (ttl is input only). An `expiration` wrapper is read
166 # as an alternate spelling (ADR 0018). A server time wins over a ttl, which
167 # would otherwise trip the BUILD oneof gate (k327).
168 52 ▶ my $exp = ref( $hash->{expiration} ) eq 'HASH' ? $hash->{expiration} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $hash->{'expiration'} eq 'HASH' ? : 5 47 52
169 52 ▶ my $expire_time = $hash->{expireTime} // $exp->{expireTime};
Condition100%3/3
$hash->{"expireTime"} // $exp->{"expireTime"}
l!l&&r!l&&!r
25126
MC/DC100%2/2
$hash->{"expireTime"} // $exp->{"expireTime"}
$hash->{"expireTime"} $exp->{"expireTime"}
Truth table100%3/3
A // B
A= $hash->{"expireTime"}B= $exp->{"expireTime"}
ABresult
1X1
011
000
170 52 ▶ my $ttl = defined $expire_time ? undef : $hash->{ttl} // $exp->{ttl};
Branch: 1 branch
BranchTrueFalseTotal
defined $expire_time ? : 26 26 52
Condition100%3/3
$hash->{"ttl"} // $exp->{"ttl"}
l!l&&r!l&&!r
1421
MC/DC100%2/2
$hash->{"ttl"} // $exp->{"ttl"}
$hash->{"ttl"} $exp->{"ttl"}
Truth table100%3/3
A // B
A= $hash->{"ttl"}B= $exp->{"ttl"}
ABresult
1X1
011
000
171  
172 52 my $um = $hash->{usageMetadata};
173 52 ▶ my $tokens = ref($um) eq 'HASH' ? $um->{totalTokenCount} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $um eq 'HASH' ? : 24 28 52
174  
175 52 my $si_text;
176 52 ▶ if ( ref( $hash->{systemInstruction} ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash->{"systemInstruction"} eq "HASH") 1 51 52
177 1 my $parts = $hash->{systemInstruction}{parts};
178 $si_text = ref($parts) eq 'ARRAY' && ref( $parts->[0] ) eq 'HASH'
179 ? $parts->[0]{text}
180 1 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $parts eq 'ARRAY' && ref $parts->[0] eq 'HASH' ? : 1 0 1
Condition33%1/3
ref $parts eq "ARRAY" and ref $parts->[0] eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
ref $parts eq "ARRAY" and ref $parts->[0] eq "HASH"
ref $parts eq "ARRAY" ref $parts->[0] eq "HASH"
Truth table33%1/3
A and B
A= ref $parts eq "ARRAY"B= ref $parts->[0] eq "HASH"
ABresult
0X0
100
111
181 }
182  
183 return $class->new(
184 name => $hash->{name},
185 model => $hash->{model},
186 $hash->{displayName} ? ( display_name => $hash->{displayName} ) : (),
187 $hash->{createTime} ? ( create_time => $hash->{createTime} ) : (),
188 $hash->{updateTime} ? ( update_time => $hash->{updateTime} ) : (),
189 defined $ttl ? ( ttl => $ttl ) : (),
190 defined $expire_time ? ( expire_time => $expire_time ) : (),
191 defined $tokens ? ( total_token_count => $tokens ) : (),
192 defined $si_text ? ( system_instruction => $si_text ) : (),
193 52 ▶ ref( $hash->{contents} ) eq 'ARRAY' ? ( contents => $hash->{contents} ) : (),
Branch: 8 branches
BranchTrueFalseTotal
$hash->{'displayName'} ? : 7 45 52
$hash->{'createTime'} ? : 6 46 52
$hash->{'updateTime'} ? : 6 46 52
defined $ttl ? : 5 47 52
defined $expire_time ? : 26 26 52
defined $tokens ? : 24 28 52
defined $si_text ? : 1 51 52
ref $hash->{'contents'} eq 'ARRAY' ? : 0 52 52
194 );
195 }
196  
197  
198 # --- Convenience accessors ---
199  
200 # Clock-vs-expiry comparison. Uses updateTime (or createTime as fallback)
201 # plus the configured ttl so a CachedContent built from a create-body
202 # (which carries ttl but no expireTime) can still answer is_expired.
203 3 ▶sub age_seconds {
Subroutine age_seconds: called CC=5 SCAR=17.8
Pod documented
204 3 my ( $self ) = @_;
205 3 ▶ my $t = $self->update_time || $self->create_time or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $self->update_time || $self->create_time or return undef 3 0 3
Condition33%1/3
$self->update_time || $self->create_time
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->update_time || $self->create_time
$self->update_time $self->create_time
Truth table33%1/3
A || B
A= $self->update_timeB= $self->create_time
ABresult
1X1
011
000
206 3 my $epoch = _parse_rfc3339_unix($t);
207 3 ▶ return undef unless defined $epoch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $epoch 0 3 3
208 3 my $delta = time - $epoch;
209 3 ▶ return $delta < 0 ? 0 : $delta; # future timestamps clamp to 0
Branch: 1 branch
BranchTrueFalseTotal
$delta < 0 ? : 0 3 3
210 }
211  
212  
213 7 ▶sub is_expired {
Subroutine is_expired: called CC=8 SCAR=20.9
Pod documented
214 7 my ( $self ) = @_;
215  
216 # Prefer server-reported expireTime when present.
217 7 ▶ if ( $self->expire_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->expire_time) 4 3 7
218 4 my $deadline = _parse_rfc3339_unix( $self->expire_time );
219 4 ▶ return defined $deadline && time >= $deadline ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $deadline && time >= $deadline ? : 2 2 4
Condition66%2/3
defined $deadline and time >= $deadline
!ll&&!rl&&r
022
MC/DC50%1/2
defined $deadline and time >= $deadline
defined $deadline time >= $deadline
Truth table66%2/3
A and B
A= defined $deadlineB= time >= $deadline
ABresult
0X0
100
111
220 }
221  
222 # Otherwise derive from updateTime + ttl (the resource's clock is the
223 # server's clock, but we keep the predicate conservative: returns 0 when
224 # there is not enough information to decide).
225 3 ▶ return 0 unless defined $self->ttl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->ttl 1 2 3
226 2 my $age = $self->age_seconds;
227 2 ▶ return 0 unless defined $age;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $age 0 2 2
228 2 my $ttl = _parse_duration_seconds( $self->ttl );
229 2 ▶ return 0 unless defined $ttl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ttl 0 2 2
230 2 ▶ return $age >= $ttl ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$age >= $ttl ? : 1 1 2
231 }
232  
233  
234 # Parse "300s" / "3.5s" / "3600s" / "60m" / "1h" etc. into integer seconds.
235 # Supports the subset Google documents explicitly: a number followed by
236 # `s`. Falls back to treating the whole string as a number of seconds.
237 2 ▶sub _parse_duration_seconds {
Subroutine _parse_duration_seconds: called CC=7 SCAR=32.0
238 2 my ( $d ) = @_;
239 2 ▶ return undef unless defined $d && length $d;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d and length $d 0 2 2
Condition33%1/3
defined $d and length $d
!ll&&!rl&&r
002
MC/DC0%0/2
defined $d and length $d
defined $d length $d
Truth table33%1/3
A and B
A= defined $dB= length $d
ABresult
0X0
100
111
240 2 ▶ if ( $d =~ /\A(\d+(?:\.\d+)?)s\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d =~ /\A(\d+(?:\.\d+)?)s\z/) 2 0 2
241 2 return int( $1 + 0.5 );
242 }
243 0 ▶ if ( $d =~ /\A(\d+(?:\.\d+)?)m\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d =~ /\A(\d+(?:\.\d+)?)m\z/) 0 0 0
244 0 return int( $1 * 60 + 0.5 );
245 }
246 0 ▶ if ( $d =~ /\A(\d+(?:\.\d+)?)h\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d =~ /\A(\d+(?:\.\d+)?)h\z/) 0 0 0
247 0 return int( $1 * 3600 + 0.5 );
248 }
249 0 ▶ if ( $d =~ /\A(\d+)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d =~ /\A(\d+)\z/) 0 0 0
250 0 return int $1;
251 }
252 0 return undef;
253 }
254  
255 # Parse RFC 3339 ("2026-08-11T10:05:00Z" / "+00:00" / fractional seconds)
256 # into a unix epoch. Best-effort; returns undef for unparseable input.
257 7 ▶sub _parse_rfc3339_unix {
Subroutine _parse_rfc3339_unix: called CC=7 SCAR=26.8
258 7 my ( $s ) = @_;
259 7 ▶ return undef unless defined $s && length $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and length $s 0 7 7
Condition33%1/3
defined $s and length $s
!ll&&!rl&&r
007
MC/DC0%0/2
defined $s and length $s
defined $s length $s
Truth table33%1/3
A and B
A= defined $sB= length $s
ABresult
0X0
100
111
260  
261 # Match the subset Google returns: 2026-08-11T10:05:00Z, with optional
262 # fractional seconds and optional +HH:MM offset. Hand-rolled so we don't
263 # lean on Time::Piece's TZ semantics — those vary by build and would
264 # produce different "now" deltas across machines.
265 7 ▶ unless ( $s =~ /\A(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(Z|[+-]\d{2}:\d{2})\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s =~ /\A(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(Z|[+-]\d{2}:\d{2})\z/) 0 7 7
266 0 return undef;
267 }
268 7 my ( $y, $mo, $d, $h, $mi, $sec, $tz ) = ( $1, $2, $3, $4, $5, $6, $7 );
269 7 require Time::Local;
270 7 my $utc = Time::Local::timegm( $sec, $mi, $h, $d, $mo - 1, $y );
271 7 ▶ if ( $tz eq 'Z' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tz eq "Z") 7 0 7
272 7 return $utc;
273 }
274 0 ▶ $tz =~ /\A([+-])(\d{2}):(\d{2})\z/ or return $utc;
Branch: 1 branch
BranchTrueFalseTotal
$tz =~ /\A([+-])(\d{2}):(\d{2})\z/ or return $utc 0 0 0
275 0 my $off = $2 * 3600 + $3 * 60;
276 0 ▶ $off = -$off if $1 eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "-" 0 0 0
277 0 return $utc - $off;
278 }
279  
280 60 __PACKAGE__->meta->make_immutable;
281  
282  
283 60 1;
284  
285 __END__
286  
287 =pod
288  
289 =encoding UTF-8
290  
291 =head1 NAME
292  
293 Langertha::CachedContent - Immutable value object for a Gemini explicit cachedContent resource
294  
295 =head1 VERSION
296  
297 version 0.503
298  
299 =head1 SYNOPSIS
300  
301 # Create-input shape (no name yet)
302 my $cc = Langertha::CachedContent->new(
303 model => 'models/gemini-2.5-pro',
304 system_instruction => 'You are a careful reviewer.',
305 ttl => '300s',
306 contents => [ { role => 'user', parts => [ { text => 'long prompt...' } ] } ],
307 );
308 my $body = $cc->to_create_body;
309 # { model => 'models/gemini-2.5-pro',
310 # systemInstruction => { parts => [ { text => '...' } ] },
311 # ttl => '300s',
312 # contents => [ ... ] }
313  
314 # Server-returned shape
315 my $cc2 = Langertha::CachedContent->from_hash({
316 name => 'cachedContents/abc123',
317 model => 'models/gemini-2.5-pro',
318 displayName => 'reviewer-context',
319 createTime => '2026-08-11T10:00:00Z',
320 updateTime => '2026-08-11T10:00:00Z',
321 expireTime => '2026-08-11T10:05:00Z',
322 usageMetadata => { totalTokenCount => 4096 },
323 });
324 $cc2->is_expired; # bool (vs. cached expireTime + clock)
325 $cc2->age_seconds; # since updateTime
326  
327 =head1 DESCRIPTION
328  
329 Canonical value object for the Gemini explicit context-cache resource
330 (L<https://ai.google.dev/api/caching>) — a server-side managed cache of
331 input context with its own lifecycle (create / get / list / patch / delete).
332  
333 Distinct from L<Langertha::PromptCache>:
334  
335 =over
336  
337 =item * L<Langertha::PromptCache> models the B<request-side> caching knob
338 (Anthropic C<cache_control> breakpoint, OpenAI C<prompt_cache_key>) that
339 flips behaviour on the next request. ADR 0009.
340  
341 =item * This class models the B<resource> itself — a long-lived cached
342 context blob identified by C<name> (C<cachedContents/{id}>) that the
343 client manages across requests.
344  
345 =back
346  
347 The two seams are deliberately separate: a CachedContent resource lives
348 across many requests and the engine request body references it by name
349 (via the C<cachedContent> field on a C<generateContent> body). See
350 L<Langertha::Role::CachedContent> for the create / get / list / update /
351 delete wrappers around this object.
352  
353 The value object carries one wire shape today: Gemini C<cachedContent>.
354 Like L<Langertha::Tool>/L<Langertha::PromptCache>, future providers that
355 expose the same resource can add a C<to_$fmt> serializer without touching
356 the engines.
357  
358 =head2 name
359  
360 Resource name, C<cachedContents/{id}>. C<undef> for create-input
361 objects that have not yet been persisted; populated from the server
362 response or set manually when wrapping an externally-obtained cache.
363  
364 =head2 model
365  
366 Required on create. Full model name (C<models/gemini-2.5-pro>) per the
367 Gemini REST contract. Optional on read-only shapes (C<Langertha::Engine::Gemini>
368 always populates it on responses, but a future response-less path may not).
369  
370 =head2 contents
371  
372 ArrayRef of content parts (the same shape L<Langertha::Engine::Gemini> puts in
373 the C<contents> request body field: C<[{ role =E<gt> 'user', parts =E<gt> [{text =E<gt> '...'}] }]>).
374 Immutable on the server once created — input only, never echoed back as
375 updatable.
376  
377 =head2 system_instruction
378  
379 Optional C<systemInstruction> text. The Gemini REST contract accepts text
380 only (no structured parts).
381  
382 =head2 tools
383  
384 Optional ArrayRef of tool definitions in the canonical L<Langertha::Tool>
385 form (or any shape L<Langertha::Tool::from_list> accepts). Serialized on
386 create as the Gemini C<tools> list, C<< [ { functionDeclarations => [...] } ] >>,
387 the shape a C<generateContent> request uses.
388  
389 =head2 ttl
390  
391 Optional Duration string (C<"300s">, C<"3600s">). Mutually exclusive with
392 L</expire_time> — exactly one of the two is permitted per
393 L</BUILD>. L<Langertha::Role::CachedContent/create_f> sends whichever was
394 set.
395  
396 =head2 expire_time
397  
398 Optional absolute expiration as an RFC 3339 timestamp string
399 (C<"2026-08-11T10:05:00Z">). Mutually exclusive with L</ttl>.
400  
401 =head2 display_name
402  
403 Optional human-readable label (max 128 Unicode chars on the server). Only
404 the create body carries it; updates can PATCH it via C<updateMask>.
405  
406 =head2 create_time
407  
408 Server-set RFC 3339 timestamp. Populated from the create / get / list
409 response.
410  
411 =head2 update_time
412  
413 Server-set RFC 3339 timestamp of last update. Populated from the response.
414  
415 =head2 total_token_count
416  
417 From C<usageMetadata.totalTokenCount> in the create / get / list response.
418 Useful for cache-hit accounting; the per-request hit count surfaces via
419 the L<Langertha::Response/usage> C<cached_content_token_count> field.
420  
421 =head2 to_create_body
422  
423 my $href = $cc->to_create_body;
424  
425 Returns the POST C<cachedContents> request body HashRef. Croaks if
426 L</model> is unset. L</ttl> / L</expire_time> serialize into the
427 C<expiration> oneof as C<ttl> or C<expireTime>.
428  
429 =head2 to_reference
430  
431 my $href = $cc->to_reference;
432 # { cachedContent => 'cachedContents/abc123' }
433  
434 Returns the HashRef L<Langertha::Engine::Gemini> merges into a
435 C<generateContent> body to reference this cachedContent by name.
436  
437 =head2 from_hash
438  
439 my $cc = Langertha::CachedContent->from_hash($response_href);
440  
441 Builds a value object from a C<cachedContent> response hash (GET / POST /
442 LIST). Returns C<undef> if C<$hash> is not a hashref or carries no
443 C<name>. The expiry is read from the top-level C<expireTime> (or C<ttl>) the
444 server sends; an C<expiration> wrapper holding either is read as well. When
445 both an C<expireTime> and a C<ttl> are present, C<expireTime> is kept. Accepts an existing L<Langertha::CachedContent> as a pass-through.
446  
447 =head2 age_seconds
448  
449 Returns whole seconds since the cache's L</update_time> (or
450 L</create_time> as fallback), or C<undef> if neither is set. Useful for
451 diagnosing TTL drift between the client's clock and the server.
452  
453 =head2 is_expired
454  
455 Returns true when the cache is past its L</expire_time> (server-reported),
456 or when C<update_time + ttl> has elapsed for caches that did not report
457 an absolute expiry. Returns false when no expiry information is available.
458  
459 =head1 SEE ALSO
460  
461 =over
462  
463 =item * L<Langertha::Role::CachedContent> - Lifecycle role (create / get / list / update / delete)
464  
465 =item * L<Langertha::PromptCache> - Sibling value object for the request-side caching knob (ADR 0009)
466  
467 =item * L<Langertha::Engine::Gemini> - The only engine currently consuming this object
468  
469 =item * L<https://ai.google.dev/api/caching> - Gemini CachedContent REST reference
470  
471 =back
472  
473 =head1 SUPPORT
474  
475 =head2 Issues
476  
477 Please report bugs and feature requests on GitHub at
478 L<https://github.com/Getty/langertha/issues>.
479  
480 =head2 IRC
481  
482 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
483  
484 =head1 CONTRIBUTING
485  
486 Contributions are welcome! Please fork the repository and submit a pull request.
487  
488 =head1 AUTHOR
489  
490 Torsten Raudssus <getty@cpan.org>
491  
492 =head1 COPYRIGHT AND LICENSE
493  
494 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
495  
496 This is free software; you can redistribute it and/or modify it under
497 the same terms as the Perl 5 programming language system itself.
498  
499 =cut
« blib/lib/Langertha/Role/Capabilities.pm ↑ Summary blib/lib/Langertha/Role/AsyncHTTP.pm »