Skip to main content

rig_core/client/
cached_content.rs

1//! The explicit context-cache verbs of a Gemini [`CachedContents`] model.
2//! Each is one call through the driver; a request that targets an existing
3//! handle maps HTTP 403 and 404 to [`ProviderError::CacheExpired`].
4
5use crate::driver::{Model, Transport};
6use crate::error::ProviderError;
7use crate::providers::gemini::cached_content::{
8    CacheExpiry, CachedContent, CachedContentRequest, CachedContents, NewCachedContent, on_handle,
9};
10
11/// Explicit context-cache operations. Requests targeting an existing handle
12/// map HTTP 403 and 404 to [`ProviderError::CacheExpired`].
13impl<T> Model<CachedContents, T>
14where
15    T: Transport<CachedContents>,
16{
17    /// Creates cached content and returns its handle and storage usage metadata.
18    pub async fn create(&self, request: NewCachedContent) -> Result<CachedContent, ProviderError> {
19        self.call(CachedContentRequest::Create(request))
20            .await?
21            .resource()
22    }
23
24    /// Fetch one cached content by handle.
25    pub async fn get(&self, name: &str) -> Result<CachedContent, ProviderError> {
26        self.call(CachedContentRequest::Get(name.to_owned()))
27            .await
28            .map_err(|error| on_handle(error, name))?
29            .resource()
30    }
31
32    /// Every cached content this API key can see, following pagination at
33    /// the wire's page size.
34    pub async fn list(&self) -> Result<Vec<CachedContent>, ProviderError> {
35        let pages =
36            crate::driver::follow_cursors(self.name(), "cached_content", |page_token| async move {
37                let reply = self.call(CachedContentRequest::List { page_token }).await?;
38                let next = reply.next_page_token();
39                Ok((reply.entries()?, next))
40            })
41            .await?;
42        Ok(pages.into_iter().flatten().collect())
43    }
44
45    /// Changes cache expiry without modifying its immutable content.
46    /// Returns the updated resource or an error, including `Expired` for HTTP 403/404.
47    pub async fn update_expiry(
48        &self,
49        name: &str,
50        expiry: CacheExpiry,
51    ) -> Result<CachedContent, ProviderError> {
52        let request = CachedContentRequest::UpdateExpiry {
53            name: name.to_owned(),
54            expiry,
55        };
56        self.call(request)
57            .await
58            .map_err(|error| on_handle(error, name))?
59            .resource()
60    }
61
62    /// Deletes cached content before its expiry. Callers should also clean up
63    /// task-scoped caches on failure to avoid continued storage charges.
64    /// Handles other than `cachedContents/<id>` or bare `<id>` return
65    /// [`ProviderError::Request`] before dispatch.
66    pub async fn delete(&self, name: &str) -> Result<(), ProviderError> {
67        self.call(CachedContentRequest::Delete(name.to_owned()))
68            .await
69            .map_err(|error| on_handle(error, name))?;
70        Ok(())
71    }
72}