blib/lib/Langertha/Role/CachedContent.pm

filter: Statementstmt 98.7% 157 / 159 Branchbran 83.3% 25 / 30 Conditioncond 58.8% 10 / 17 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 36 / 36 Podpod 60.0% 6 / 10 totaltotal 90.8% 238 / 262 CC 23 SCAR 8.8
CC
23
Cov
93%
_update_cached_content_request
5.0
_create_cached_content_request
4.8
_list_cached_contents_done
4.0
CRAP 2.4
« blib/lib/Langertha/Engine/MiniMaxAnthropic.pm ↑ Summary blib/lib/Langertha/Engine/Anthropic.pm »
1 package Langertha::Role::CachedContent;
2 # ABSTRACT: Role for the explicit cachedContent resource lifecycle (create/get/list/update/delete)
3 64 our $VERSION = '0.505';
4 64 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 64 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 64 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 64 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 64 ▶use Langertha::CachedContent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # The consumer's endpoint/auth seam (Langertha::Engine::Gemini's
11 # gemini_endpoint / gemini_url, ADR 0016 decision 3, karr #88, #94). These are
12 # plain subs, installed at compile time, so they are already present when the
13 # engine's runtime `with` executes — `requires` holds and fails at composition
14 # rather than at the first HTTP call.
15 #
16 # The name is deliberately Gemini's own rather than a neutral alias: the
17 # cachedContents lifecycle *is* the Gemini Generative Language REST surface,
18 # so any engine that can compose this role speaks that dialect and brings
19 # those methods. A provider-neutral indirection would be an abstraction with
20 # one implementation and no second caller to justify it.
21 64 requires qw(
22 gemini_endpoint
23 gemini_url
24 );
25  
26 # No `requires` for user_agent / json: those are defined as attributes on the
27 # engine *after* role composition, so requiring them at composition time would
28 # fail. The role methods do `$self->can($m)` guards instead (see
29 # _assert_http_seam below; matches how Langertha::Role::Tools handles optional
30 # cross-role seams).
31  
32  
33 # The role owns the resource *paths* only. Base URL, API version and the
34 # credential all come from the consumer's seam, so a subclass that moves any
35 # of them (header auth, /v1, another base path) moves the whole cachedContents
36 # lifecycle with it instead of being half converted.
37 #
38 # cachedContents is its own top-level collection, not a sub-resource of
39 # models/ — hence gemini_url / gemini_endpoint, never gemini_model_url.
40 37 ▶sub _cached_contents_path { 'cachedContents' }
Subroutine _cached_contents_path: called CC=1 SCAR=0.0
41  
42 37 ▶sub _cached_content_path {
Subroutine _cached_content_path: called CC=3 SCAR=11.0
43 37 my ( $self, $name ) = @_;
44 37 ▶ croak "name must look like cachedContents/{id}; got '$name'"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $name =~ m[\AcachedContents/[^/\s]+\z] 2 35 37
Condition66%2/3
defined $name and $name =~ m[\AcachedContents/[^/\s]+\z]
!ll&&!rl&&r
0235
MC/DC50%1/2
defined $name and $name =~ m[\AcachedContents/[^/\s]+\z]
defined $name $name =~ m[\AcachedContents/[^/\s]+\z]
Truth table66%2/3
A and B
A= defined $nameB= $name =~ m[\AcachedContents/[^/\s]+\z]
ABresult
0X0
100
111
45 unless defined $name && $name =~ m{\AcachedContents/[^/\s]+\z};
46 35 return $name;
47 }
48  
49 # Credential-free resource URLs, for callers that want to name a cache rather
50 # than fetch it; the request sites below go through gemini_url, which is what
51 # places the credential.
52 2 ▶sub _cached_contents_url {
Subroutine _cached_contents_url: called CC=1 SCAR=0.0
53 2 my ( $self ) = @_;
54 2 return $self->gemini_endpoint( $self->_cached_contents_path );
55 }
56  
57 # Guard: the engine must expose the HTTP / JSON seams the role uses to talk
58 # to the cachedContents REST surface. Composed on Engine::Gemini (which
59 # extends Engine::Remote -> composes Role::HTTP -> Role::JSON) so this
60 # passes; it croaks loudly when the role is composed on an engine without the
61 # right surface — better than a "method not found" later.
62 #
63 # url and api_key are deliberately NOT on this list: the role no longer reads
64 # either, it asks the required gemini_endpoint / gemini_url for the finished
65 # URL. A consumer that authenticates without an api_key attribute at all
66 # (ADC bearer token, mTLS) is a legitimate consumer of this role.
67 64 ▶sub _assert_http_seam {
Subroutine _assert_http_seam: called CC=3 SCAR=11.2
68 64 my ( $self ) = @_;
69 64 for my $m (qw( user_agent _async_do_request_f generate_http_request parse_response json )) {
70 320 ▶ croak "Langertha::Role::CachedContent: consumer must provide '$m'"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($m) 0 320 320
71 unless $self->can($m);
72 }
73 }
74  
75 3 ▶sub _cached_content_url {
Subroutine _cached_content_url: called CC=1 SCAR=0.0
76 3 my ( $self, $name ) = @_;
77 3 return $self->gemini_endpoint( $self->_cached_content_path($name) );
78 }
79  
80 # Strip the leading "cachedContents/" so callers can pass either a bare
81 # id or the full resource name uniformly.
82 35 ▶sub _normalize_name {
Subroutine _normalize_name: called CC=2 SCAR=7.0
83 35 my ( $class, $name ) = @_;
84 35 ▶ return undef unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 35 35
85 35 $name =~ s{\AcachedContents/}{};
86 35 return 'cachedContents/' . $name;
87 }
88  
89  
90 6 ▶async sub create_cached_content_f {
Subroutine create_cached_content_f: called CC=1 SCAR=0.0
Pod documented
91 6 my ( $self, @args ) = @_;
92 6 my $req = $self->_create_cached_content_request(@args);
93 6 my $http_res = await $self->_async_do_request_f( request => $req );
94 5 return $req->response_call->($http_res);
95 64 }
96  
97 12 ▶sub _create_cached_content_request {
Subroutine _create_cached_content_request: called CC=4 SCAR=15.7
98 12 my ( $self, @args ) = @_;
99  
100 12 $self->_assert_http_seam;
101  
102 12 my $cc;
103 0 ▶ if ( @args == 1 && ref( $args[0] ) && eval { $args[0]->isa('Langertha::CachedContent') } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and ref $args[0] and eval { do { $args[0]->isa('Langertha::CachedContent') } }) { } 0 12 12
Condition33%2/6
@args == 1 and ref $args[0] and eval { do { $args[0]->isa("Langertha::CachedContent") } }
!ll&&!rl&&r
1200
@args == 1 and ref $args[0]
!ll&&!rl&&r
1200
MC/DC0%0/3
@args == 1 and ref $args[0] and eval { do { $args[0]->isa("Langertha::CachedContent") } }
@args == 1 ref $args[0] eval { do { $args[0]->isa("Langertha::CachedContent") } }
Truth table25%1/4
A and B and C
A= @args == 1B= ref $args[0]C= eval { do { $args[0]->isa("Langertha::CachedContent") } }
ABCresult
0XX0
10X0
1100
1111
104 0 $cc = $args[0];
105 }
106 else {
107 12 $cc = Langertha::CachedContent->new( @args );
108 }
109  
110 12 my $url = $self->gemini_url( $self->_cached_contents_path );
111 12 my $body = $cc->to_create_body;
112  
113 return $self->generate_http_request(
114 POST => $url,
115 10 ▶ sub { $self->_parse_cached_content_response(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
116 12 %$body,
117 );
118 }
119  
120  
121 5 ▶sub create_cached_content {
Subroutine create_cached_content: called CC=1 SCAR=0.0
Pod documented
122 5 my ( $self, @args ) = @_;
123 5 return $self->_cached_content_sync( $self->_create_cached_content_request(@args) );
124 }
125  
126  
127 6 ▶async sub get_cached_content_f {
Subroutine get_cached_content_f: called CC=1 SCAR=0.0
Pod documented
128 6 my ( $self, $name ) = @_;
129 6 my $req = $self->_get_cached_content_request($name);
130 6 my $http_res = await $self->_async_do_request_f( request => $req );
131 5 return $req->response_call->($http_res);
132 64 }
133  
134 10 ▶sub _get_cached_content_request {
Subroutine _get_cached_content_request: called CC=1 SCAR=0.0
135 10 my ( $self, $name ) = @_;
136 10 $self->_assert_http_seam;
137 10 $name = $self->_normalize_name($name);
138  
139 10 my $url = $self->gemini_url( $self->_cached_content_path($name) );
140 return $self->generate_http_request(
141 GET => $url,
142 9 ▶ sub { $self->_parse_cached_content_response(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
143 10 );
144 }
145  
146 4 ▶sub get_cached_content {
Subroutine get_cached_content: called CC=1 SCAR=0.0
Pod undocumented
147 4 my ( $self, $name ) = @_;
148 4 return $self->_cached_content_sync( $self->_get_cached_content_request($name) );
149 }
150  
151  
152 11 ▶async sub list_cached_contents_f {
Subroutine list_cached_contents_f: called CC=2 SCAR=6.9
Pod documented
153 11 my ( $self, %opts ) = @_;
154 11 $self->_assert_http_seam;
155  
156 11 my @all;
157 11 my $token = $opts{page_token};
158  
159 # A real loop block, not a do{}while: the exit below is `last`, and `last`
160 # inside a do-BLOCK-while is fatal at runtime ("Can't last outside a loop
161 # block") — it killed both one-page options after the request had already
162 # gone on the wire (karr #104, t/49). while(1) keeps the do{}while semantics
163 # (always issue at least one request) with a working exit.
164 11 while (1) {
165 16 my $req = $self->_list_cached_contents_request( $token, $opts{page_size} );
166 16 my $http_res = await $self->_async_do_request_f( request => $req );
167 15 my ( $items, $next ) = $req->response_call->($http_res);
168 13 push @all, @$items;
169 13 $token = $next;
170 13 ▶ last if $self->_list_cached_contents_done( \%opts, $token );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_list_cached_contents_done(\%opts, $token) 8 5 13
171 }
172  
173 8 return @all;
174 64 }
175  
176 6 ▶sub list_cached_contents {
Subroutine list_cached_contents: called CC=2 SCAR=6.9
Pod undocumented
177 6 my ( $self, %opts ) = @_;
178 6 $self->_assert_http_seam;
179  
180 6 my @all;
181 6 my $token = $opts{page_token};
182 6 while (1) {
183 my ( $items, $next ) = $self->_cached_content_sync(
184 7 $self->_list_cached_contents_request( $token, $opts{page_size} ) );
185 6 push @all, @$items;
186 6 $token = $next;
187 6 ▶ last if $self->_list_cached_contents_done( \%opts, $token );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_list_cached_contents_done(\%opts, $token) 5 1 6
188 }
189 5 return \@all;
190 }
191  
192 23 ▶sub _list_cached_contents_request {
Subroutine _list_cached_contents_request: called CC=4 SCAR=13.9
193 23 my ( $self, $token, $page_size ) = @_;
194 23 my $url = $self->gemini_url( $self->_cached_contents_path );
195  
196 # Pagination goes on with URI rather than through the seam's query list:
197 # pageToken is opaque server data that has to be percent-encoded, whereas
198 # the seam interpolates verbatim (it carries the credential and fixed
199 # switches like alt=sse). Same handling as
200 # Langertha::Engine::Gemini::list_models_request.
201 23 my %params;
202 23 ▶ $params{pageSize} = $page_size if defined $page_size;
Branch: 1 branch
BranchTrueFalseTotal
if defined $page_size 3 20 23
203 23 ▶ $params{pageToken} = $token if defined $token;
Branch: 1 branch
BranchTrueFalseTotal
if defined $token 8 15 23
204 23 ▶ if (%params) {
Branch: 1 branch
BranchTrueFalseTotal
if (%params) 10 13 23
205 10 my $uri = URI->new($url);
206 10 my %query = $uri->query_form;
207 10 $uri->query_form( %query, %params );
208 10 $url = $uri->as_string;
209 }
210  
211 return $self->generate_http_request(
212 GET => $url,
213 22 ▶ sub { $self->_parse_list_response(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
214 23 );
215 }
216  
217 # If the caller constrained to one page, stop after the first response;
218 # otherwise walk on for as long as the server hands out a next page.
219 19 ▶sub _list_cached_contents_done {
Subroutine _list_cached_contents_done: called CC=4 SCAR=13.9
220 19 my ( $self, $opts, $token ) = @_;
221 19 ▶ return 1 if defined $opts->{page_token} || defined $opts->{page_size};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$opts{"page_token"} or defined $$opts{"page_size"} 4 15 19
Condition100%3/3
defined $$opts{"page_token"} or defined $$opts{"page_size"}
l!l&&r!l&&!r
2215
MC/DC100%2/2
defined $$opts{"page_token"} or defined $$opts{"page_size"}
defined $$opts{"page_token"} defined $$opts{"page_size"}
Truth table100%3/3
A or B
A= defined $$opts{"page_token"}B= defined $$opts{"page_size"}
ABresult
1X1
011
000
222 15 ▶ return !( defined $token && length $token );
Condition66%2/3
defined $token && length $token
!ll&&!rl&&r
906
MC/DC50%1/2
defined $token && length $token
defined $token length $token
Truth table66%2/3
A && B
A= defined $tokenB= length $token
ABresult
0X0
100
111
223 }
224  
225  
226 6 ▶async sub update_cached_content_f {
Subroutine update_cached_content_f: called CC=1 SCAR=0.0
Pod documented
227 6 my ( $self, $name, %opts ) = @_;
228 6 my $req = $self->_update_cached_content_request( $name, %opts );
229 6 my $http_res = await $self->_async_do_request_f( request => $req );
230 5 return $req->response_call->($http_res);
231 64 }
232  
233 14 ▶sub _update_cached_content_request {
Subroutine _update_cached_content_request: called CC=5 SCAR=16.1
234 14 my ( $self, $name, %opts ) = @_;
235 14 $self->_assert_http_seam;
236 14 $name = $self->_normalize_name($name);
237  
238 14 my $body = {};
239 14 my @mask;
240 14 ▶ if ( exists $opts{ttl} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"ttl"}) 13 1 14
241 13 $body->{ttl} = delete $opts{ttl};
242 13 push @mask, 'ttl';
243 }
244 14 ▶ if ( exists $opts{expire_time} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"expire_time"}) 1 13 14
245 1 $body->{expireTime} = delete $opts{expire_time};
246 1 push @mask, 'expireTime';
247 }
248 14 ▶ croak "update_cached_content_f requires exactly one of ttl / expire_time (got "
Branch: 1 branch
BranchTrueFalseTotal
unless @mask == 1 2 12 14
249 . scalar(@mask) . ")"
250 unless @mask == 1;
251 12 ▶ croak "update_cached_content_f: unknown option(s): " . join(', ', sort keys %opts)
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 12 12
252 if %opts;
253  
254 # updateMask is a closed set of literal field names (ttl / expireTime), so
255 # it rides the seam's query list next to the credential — same shape as
256 # alt=sse on the streaming endpoint.
257 12 my $url = $self->gemini_url(
258 $self->_cached_content_path($name), updateMask => join(',', @mask) );
259  
260 return $self->generate_http_request(
261 PATCH => $url,
262 11 ▶ sub { $self->_parse_cached_content_response(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
263 12 %$body,
264 );
265 }
266  
267 8 ▶sub update_cached_content {
Subroutine update_cached_content: called CC=1 SCAR=0.0
Pod undocumented
268 8 my ( $self, $name, %opts ) = @_;
269 8 return $self->_cached_content_sync( $self->_update_cached_content_request( $name, %opts ) );
270 }
271  
272  
273 6 ▶async sub delete_cached_content_f {
Subroutine delete_cached_content_f: called CC=1 SCAR=0.0
Pod documented
274 6 my ( $self, $name ) = @_;
275 6 my $req = $self->_delete_cached_content_request($name);
276 6 my $http_res = await $self->_async_do_request_f( request => $req );
277 5 return $req->response_call->($http_res);
278 64 }
279  
280 11 ▶sub _delete_cached_content_request {
Subroutine _delete_cached_content_request: called CC=1 SCAR=0.0
281 11 my ( $self, $name ) = @_;
282 11 $self->_assert_http_seam;
283 11 $name = $self->_normalize_name($name);
284  
285 11 my $url = $self->gemini_url( $self->_cached_content_path($name) );
286 return $self->generate_http_request(
287 DELETE => $url,
288 10 ▶ sub { $self->_parse_delete_response(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
289 11 );
290 }
291  
292 5 ▶sub delete_cached_content {
Subroutine delete_cached_content: called CC=1 SCAR=0.0
Pod undocumented
293 5 my ( $self, $name ) = @_;
294 5 return $self->_cached_content_sync( $self->_delete_cached_content_request($name) );
295 }
296  
297 # The sync methods send over the engine's user_agent, like every other sync
298 # call; the _f methods above go through _async_do_request_f (k329, ADR 0027).
299 # Both hand the response to the same response_call, so value and error text
300 # match on every backend.
301 27 ▶sub _cached_content_sync {
Subroutine _cached_content_sync: called CC=1 SCAR=0.0
302 27 my ( $self, $req ) = @_;
303 27 return $req->response_call->( $self->user_agent->request($req) );
304 }
305  
306 # --- Internal response parsers ---
307  
308 30 ▶sub _parse_cached_content_response {
Subroutine _parse_cached_content_response: called CC=2 SCAR=7.0
309 30 my ( $self, $http_res ) = @_;
310 30 my $data = $self->parse_response($http_res);
311 21 my $cc = Langertha::CachedContent->from_hash($data);
312 21 ▶ croak "Langertha::Role::CachedContent: response had no name; got: " . $self->json->encode($data)
Branch: 1 branch
BranchTrueFalseTotal
unless $cc 0 21 21
313 unless $cc;
314 21 return $cc;
315 }
316  
317 22 ▶sub _parse_list_response {
Subroutine _parse_list_response: called CC=2 SCAR=7.0
318 22 my ( $self, $http_res ) = @_;
319 22 my $data = $self->parse_response($http_res);
320 19 ▶ my @items = map { Langertha::CachedContent->from_hash($_) } @{ $data->{cachedContents} || [] };
Condition50%1/2
$$data{'cachedContents'} || []
l!l
190
MC/DC0%0/1
$$data{'cachedContents'} || []
$$data{'cachedContents'}
Truth table50%1/2
A
A= $$data{'cachedContents'}
Aresult
11
00
321 19 return ( \@items, $data->{nextPageToken} );
322 }
323  
324 10 ▶sub _parse_delete_response {
Subroutine _parse_delete_response: called CC=1 SCAR=0.0
325 10 my ( $self, $http_res ) = @_;
326 # DELETE returns an empty JSON object on success; we don't need the body.
327 10 $self->parse_response($http_res);
328 7 return 1;
329 }
330  
331  
332 64 1;
333  
334 __END__
« blib/lib/Langertha/Engine/MiniMaxAnthropic.pm ↑ Summary blib/lib/Langertha/Engine/Anthropic.pm »