| 1 | package Langertha::Role::CachedContent; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Role for the explicit cachedContent resource lifecycle (create/get/list/update/delete) | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 60 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 60 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 60 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 60 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 60 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 60 | ▶ | 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 | 60 | 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 | 36 | ▶ | 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
Condition66%2/3
defined $name and $name =~ m[\AcachedContents/[^/\s]+\z]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 63 | ▶ | sub _assert_http_seam { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_assert_http_seam: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 63 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 63 | for my $m (qw( user_agent _async_do_request_f generate_http_request parse_response json )) { | |||||||||||||||||||||||||||||||||||||||||
| 70 | 315 | ▶ | croak "Langertha::Role::CachedContent: consumer must provide '$m'" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 60 | } | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 11 | ▶ | sub _create_cached_content_request { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_create_cached_content_request: called CC=4 SCAR=15.7
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 11 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 11 | $self->_assert_http_seam; | |||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 11 | my $cc; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 11 | ▶ | if ( @args == 1 && ref( $args[0] ) && eval { $args[0]->isa('Langertha::CachedContent') } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
@args == 1 and ref $args[0] and eval {
@args == 1 and ref $args[0]
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")
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | $cc = $args[0]; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 11 | $cc = Langertha::CachedContent->new( @args ); | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 11 | my $url = $self->gemini_url( $self->_cached_contents_path ); | |||||||||||||||||||||||||||||||||||||||||
| 111 | 11 | 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 | 11 | %$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 | 60 | } | |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 8 | return @all; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 60 | } | |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | 23 | ▶ | $params{pageToken} = $token if defined $token; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 23 | ▶ | if (%params) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $$opts{"page_token"} or defined $$opts{"page_size"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 15 | ▶ | return !( defined $token && length $token ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $token && length $token
MC/DC50%1/2
defined $token && length $token
defined $token
length $token
Truth table66%2/3
A && B
A= defined $tokenB= length $token
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 60 | } | |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 13 | $body->{ttl} = delete $opts{ttl}; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 13 | push @mask, 'ttl'; | |||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||
| 244 | 14 | ▶ | if ( exists $opts{expire_time} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | . scalar(@mask) . ")" | ||||||||||||||||||||||||||||||||||||||||||
| 250 | unless @mask == 1; | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 12 | ▶ | croak "update_cached_content_f: unknown option(s): " . join(', ', sort keys %opts) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 60 | } | |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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'} || []
MC/DC0%0/1
$$data{'cachedContents'} || []
$$data{'cachedContents'}
Truth table50%1/2
A
A= $$data{'cachedContents'}
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 60 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | Langertha::Role::CachedContent - Role for the explicit cachedContent resource lifecycle (create/get/list/update/delete) | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | version 0.503 | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | with 'Langertha::Role::CachedContent'; | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | my $cc = $engine->create_cached_content_f( | ||||||||||||||||||||||||||||||||||||||||||
| 353 | model => 'models/gemini-2.5-pro', | ||||||||||||||||||||||||||||||||||||||||||
| 354 | system_instruction => 'You are a careful reviewer.', | ||||||||||||||||||||||||||||||||||||||||||
| 355 | ttl => '300s', | ||||||||||||||||||||||||||||||||||||||||||
| 356 | contents => [ { role => 'user', parts => [ { text => 'long prompt...' } ] } ], | ||||||||||||||||||||||||||||||||||||||||||
| 357 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 358 | # Returns a Langertha::CachedContent with name set | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | my $same = await $engine->get_cached_content_f( $cc->name ); | ||||||||||||||||||||||||||||||||||||||||||
| 361 | my @all = await $engine->list_cached_contents_f; | ||||||||||||||||||||||||||||||||||||||||||
| 362 | await $engine->update_cached_content_f( $cc->name, ttl => '600s' ); | ||||||||||||||||||||||||||||||||||||||||||
| 363 | await $engine->delete_cached_content_f( $cc->name ); | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | Lifecycle wrapper around the Gemini explicit C<cachedContent> resource | ||||||||||||||||||||||||||||||||||||||||||
| 368 | (L<https://ai.google.dev/api/caching>). Composed by engines that want to | ||||||||||||||||||||||||||||||||||||||||||
| 369 | expose resource management (currently L<Langertha::Engine::Gemini>) — | ||||||||||||||||||||||||||||||||||||||||||
| 370 | the methods are B<role methods>, not engine methods, so any other engine | ||||||||||||||||||||||||||||||||||||||||||
| 371 | with the same REST surface can compose the role rather than re-implement | ||||||||||||||||||||||||||||||||||||||||||
| 372 | it. | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | Endpoints, C<{base}> being the consumer's | ||||||||||||||||||||||||||||||||||||||||||
| 375 | L<Langertha::Engine::Gemini/gemini_endpoint> (C<< {url}/v1beta >> on the | ||||||||||||||||||||||||||||||||||||||||||
| 376 | Developer API): | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | =item * C<POST {base}/cachedContents> — create | ||||||||||||||||||||||||||||||||||||||||||
| 381 | =item * C<GET {base}/cachedContents> — list (paginated) | ||||||||||||||||||||||||||||||||||||||||||
| 382 | =item * C<GET {base}/cachedContents/{name}> — get | ||||||||||||||||||||||||||||||||||||||||||
| 383 | =item * C<PATCH {base}/cachedContents/{name}> — update (expiration only) | ||||||||||||||||||||||||||||||||||||||||||
| 384 | =item * C<DELETE {base}/cachedContents/{name}> — delete | ||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||
| 388 | The C<_f> methods are async via L<Future::AsyncAwait> and send through the | ||||||||||||||||||||||||||||||||||||||||||
| 389 | engine's async transport (L<Langertha::Role::AsyncHTTP>): L<Net::Async::HTTP> | ||||||||||||||||||||||||||||||||||||||||||
| 390 | when installed, an injected C<_async_http> client, or the synchronous LWP | ||||||||||||||||||||||||||||||||||||||||||
| 391 | fallback. On L<Net::Async::HTTP> the engine's C<user_agent_timeout> bounds each | ||||||||||||||||||||||||||||||||||||||||||
| 392 | request. The sync methods (L</create_cached_content>, etc.) send over the | ||||||||||||||||||||||||||||||||||||||||||
| 393 | engine's C<user_agent>. Both give the same result, and an HTTP error croaks / | ||||||||||||||||||||||||||||||||||||||||||
| 394 | fails with the same text (the engine's C<request failed> message, see | ||||||||||||||||||||||||||||||||||||||||||
| 395 | L<Langertha::Role::HTTP/parse_response>). | ||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | The role owns the resource paths and the HTTP plumbing only. Base URL, API | ||||||||||||||||||||||||||||||||||||||||||
| 398 | version and the credential are the consumer's — every request URL is built by | ||||||||||||||||||||||||||||||||||||||||||
| 399 | the required C<gemini_endpoint> / C<gemini_url> seam | ||||||||||||||||||||||||||||||||||||||||||
| 400 | (L<Langertha::Engine::Gemini>, ADR 0016 decision 3), so a subclass that moves | ||||||||||||||||||||||||||||||||||||||||||
| 401 | the endpoint or the auth scheme moves the whole cachedContents lifecycle with | ||||||||||||||||||||||||||||||||||||||||||
| 402 | it. The role does B<not> | ||||||||||||||||||||||||||||||||||||||||||
| 403 | introspect model names or filter by generation. Engines narrow | ||||||||||||||||||||||||||||||||||||||||||
| 404 | C<Langertha::Engine::Gemini>'s advertising of the capability per model | ||||||||||||||||||||||||||||||||||||||||||
| 405 | family (see L<Langertha::Engine::Gemini>'s C<around engine_capabilities>). | ||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||
| 407 | =head2 create_cached_content_f | ||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||
| 409 | my $cc = await $engine->create_cached_content_f( | ||||||||||||||||||||||||||||||||||||||||||
| 410 | model => 'models/gemini-2.5-pro', | ||||||||||||||||||||||||||||||||||||||||||
| 411 | ttl => '300s', | ||||||||||||||||||||||||||||||||||||||||||
| 412 | system_instruction => 'be brief', | ||||||||||||||||||||||||||||||||||||||||||
| 413 | contents => [ ... ], | ||||||||||||||||||||||||||||||||||||||||||
| 414 | tools => [ ... ], # optional | ||||||||||||||||||||||||||||||||||||||||||
| 415 | display_name => 'reviewer', # optional | ||||||||||||||||||||||||||||||||||||||||||
| 416 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 417 | # $cc is a Langertha::CachedContent with name set | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | Posts to the C<cachedContents> collection (C<POST /v1beta/cachedContents> on | ||||||||||||||||||||||||||||||||||||||||||
| 420 | the Developer API), parses the response into a | ||||||||||||||||||||||||||||||||||||||||||
| 421 | L<Langertha::CachedContent>. Accepts either a pre-built | ||||||||||||||||||||||||||||||||||||||||||
| 422 | L<Langertha::CachedContent> (the role will call C<to_create_body> for | ||||||||||||||||||||||||||||||||||||||||||
| 423 | you) or the individual named attributes. | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | Returns the persisted CachedContent. | ||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||
| 427 | =head2 create_cached_content | ||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | my $cc = $engine->create_cached_content(%same_args); | ||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||
| 431 | Sync wrapper around L</create_cached_content_f>. | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | =head2 get_cached_content_f | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | my $cc = await $engine->get_cached_content_f('cachedContents/abc123'); | ||||||||||||||||||||||||||||||||||||||||||
| 436 | my $cc = await $engine->get_cached_content_f('abc123'); # bare id accepted | ||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||
| 438 | Fetch a single cache by C<name> (or bare id). | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | =head2 list_cached_contents_f | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | my @caches = await $engine->list_cached_contents_f; | ||||||||||||||||||||||||||||||||||||||||||
| 443 | my @page = await $engine->list_cached_contents_f(page_size => 50); | ||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | Returns a list of L<Langertha::CachedContent>. Walks pagination | ||||||||||||||||||||||||||||||||||||||||||
| 446 | automatically (L<https://ai.google.dev/api/caching#method:-cachedcontents.list>). | ||||||||||||||||||||||||||||||||||||||||||
| 447 | Accepts C<page_size> and C<page_token> for one-page control. | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | =head2 update_cached_content_f | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | await $engine->update_cached_content_f( 'cachedContents/abc123', ttl => '600s' ); | ||||||||||||||||||||||||||||||||||||||||||
| 452 | await $engine->update_cached_content_f( 'cachedContents/abc123', expire_time => '2030-01-01T00:00:00Z' ); | ||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | Updates a cache. Per the REST contract, only C<expiration> is updatable | ||||||||||||||||||||||||||||||||||||||||||
| 455 | (L<https://ai.google.dev/api/caching#method:-cachedcontents.patch>). Pass | ||||||||||||||||||||||||||||||||||||||||||
| 456 | exactly one of C<ttl> or C<expire_time>. The server requires the | ||||||||||||||||||||||||||||||||||||||||||
| 457 | C<updateMask> query parameter so we set it explicitly to whichever field | ||||||||||||||||||||||||||||||||||||||||||
| 458 | the caller is updating. | ||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||
| 460 | =head2 delete_cached_content_f | ||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||
| 462 | await $engine->delete_cached_content_f('cachedContents/abc123'); | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | Delete a cache. Returns C<1> on success. | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | =item * L<Langertha::CachedContent> - The value object this role round-trips | ||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | =item * L<Langertha::Engine::Gemini> - Composes this role on its Gemini engine | ||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||
| 474 | =item * L<https://ai.google.dev/api/caching> - Gemini CachedContent REST reference | ||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||
| 476 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||
| 483 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||
| 493 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||
| 495 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||
| 497 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||
| 499 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 502 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | =cut | ||||||||||||||||||||||||||||||||||||||||||