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/Response.pm ↑ Summary blib/lib/Langertha/Engine/Virtual.pm »
1 package Langertha::CachedContent;
2 # ABSTRACT: Immutable value object for a Gemini explicit cachedContent resource
3 64 our $VERSION = '0.505';
4 64 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 64 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 64 has name => (
9 is => 'ro',
10 isa => 'Maybe[Str]',
11 predicate => 'has_name',
12 );
13  
14  
15 64 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 82 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 64 );
27  
28  
29 has system_instruction => (
30 is => 'ro',
31 isa => 'Maybe[Str]',
32 predicate => 'has_system_instruction',
33 81 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 64 );
35  
36  
37 has tools => (
38 is => 'ro',
39 isa => 'Maybe[ArrayRef]',
40 82 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 64 );
42  
43  
44 has ttl => (
45 is => 'ro',
46 isa => 'Maybe[Str]',
47 61 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 64 );
49  
50  
51 has expire_time => (
52 is => 'ro',
53 isa => 'Maybe[Str]',
54 55 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
55 64 );
56  
57  
58 has display_name => (
59 is => 'ro',
60 isa => 'Maybe[Str]',
61 72 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
62 64 );
63  
64  
65 has create_time => (
66 is => 'ro',
67 isa => 'Maybe[Str]',
68 77 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
69 64 );
70  
71  
72 has update_time => (
73 is => 'ro',
74 isa => 'Maybe[Str]',
75 77 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
76 64 );
77  
78  
79 has total_token_count => (
80 is => 'ro',
81 isa => 'Maybe[Int]',
82 59 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
83 64 );
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 83 ▶sub BUILD {
Subroutine BUILD: called CC=5 SCAR=16.1
Pod undocumented
90 83 my ( $self ) = @_;
91  
92 83 ▶ if ( defined $self->ttl && defined $self->expire_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->ttl and defined $self->expire_time) 1 82 83
Condition100%3/3
defined $self->ttl and defined $self->expire_time
!ll&&!rl&&r
61211
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 82 ▶ if ( $self->has_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_name) 64 18 82
99 64 my $n = $self->name;
100 64 ▶ croak "Langertha::CachedContent: name must be 'cachedContents/{id}'; got '$n'"
Branch: 1 branch
BranchTrueFalseTotal
unless $n =~ m[\AcachedContents/[^/\s]+\z] 1 63 64
101 unless $n =~ m{\AcachedContents/[^/\s]+\z};
102 }
103 }
104  
105 # --- Wire serializers ---
106  
107 15 ▶sub to_create_body {
Subroutine to_create_body: called CC=9 SCAR=22.1
Pod documented
108 15 my ( $self ) = @_;
109 15 ▶ croak "Langertha::CachedContent: model is required to create a cache"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_model 0 15 15
110 unless $self->has_model;
111  
112 15 my %body = ( model => $self->model );
113  
114 15 ▶ if ( $self->has_system_instruction ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_system_instruction) 15 0 15
115 $body{systemInstruction} = {
116 15 parts => [ { text => $self->system_instruction } ],
117 };
118 }
119  
120 15 ▶ if ( $self->tools && @{ $self->tools } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tools and @{$self->tools;}) 1 14 15
Condition66%2/3
$self->tools and @{$self->tools;}
!ll&&!rl&&r
1401
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 15 ▶ if ( @{ $self->contents } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->contents;}) 1 14 15
128 1 $body{contents} = $self->contents;
129 }
130  
131 15 ▶ if ( defined $self->ttl ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->ttl) { } 14 1 15
elsif (defined $self->expire_time) { } 1 0 1
132 14 $body{ttl} = $self->ttl;
133 }
134 elsif ( defined $self->expire_time ) {
135 1 $body{expireTime} = $self->expire_time;
136 }
137  
138 15 ▶ if ( defined $self->display_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->display_name) 4 11 15
139 4 $body{displayName} = $self->display_name;
140 }
141  
142 15 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 { do { $hash->isa(__PACKAGE__) } }
!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 64 __PACKAGE__->meta->make_immutable;
281  
282  
283 64 1;
284  
285 __END__
« blib/lib/Langertha/Response.pm ↑ Summary blib/lib/Langertha/Engine/Virtual.pm »