Skip to main content

adk_gemini/cache/
model.rs

1use serde::{Deserialize, Serialize};
2use time::OffsetDateTime;
3
4use crate::models::Content;
5use crate::{Model, Tool, ToolConfig};
6
7/// Cached content resource returned by the API (with all server-provided fields).
8#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
9#[serde(rename_all = "camelCase")]
10pub struct CachedContent {
11    /// The resource name of the cached content.
12    /// Format: cachedContents/{id}
13    pub name: String,
14
15    /// The name of the model used for cached content.
16    pub model: Model,
17
18    /// Output only. Creation time of the cached content.
19    #[serde(with = "time::serde::rfc3339")]
20    pub create_time: OffsetDateTime,
21
22    /// Output only. Last update time of the cached content.
23    #[serde(with = "time::serde::rfc3339")]
24    pub update_time: OffsetDateTime,
25
26    /// Output only. Usage metadata for the cached content.
27    pub usage_metadata: CacheUsageMetadata,
28
29    /// Expiration information for the cached content.
30    #[serde(flatten)]
31    pub expiration: CacheExpirationResponse,
32
33    /// The user-generated display name (if provided).
34    #[serde(skip_serializing_if = "Option::is_none")]
35    pub display_name: Option<String>,
36
37    /// The cached contents (may be omitted in some API responses for size).
38    #[serde(skip_serializing_if = "Option::is_none")]
39    pub contents: Option<Vec<Content>>,
40
41    /// The cached tools (may be omitted in some API responses for size).
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub tools: Option<Vec<Tool>>,
44
45    /// The cached system instruction (may be omitted in some API responses for size).
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub system_instruction: Option<Content>,
48
49    /// The cached tool config (may be omitted in some API responses for size).
50    #[serde(skip_serializing_if = "Option::is_none")]
51    pub tool_config: Option<ToolConfig>,
52}
53
54/// Usage metadata specifically for cached content (more predictable than general UsageMetadata).
55#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
56#[serde(rename_all = "camelCase")]
57pub struct CacheUsageMetadata {
58    /// Total tokens in the cached content.
59    pub total_token_count: i32,
60}
61
62/// Expiration configuration for cached content in requests (union type).
63#[derive(Debug, Clone, Serialize, PartialEq)]
64#[serde(untagged)]
65pub enum CacheExpirationRequest {
66    /// Timestamp in UTC of when this resource is considered expired.
67    /// Uses RFC 3339 format.
68    ExpireTime {
69        /// The expiration timestamp.
70        #[serde(with = "time::serde::rfc3339")]
71        expire_time: OffsetDateTime,
72    },
73    /// New TTL for this resource, input only.
74    /// A duration in seconds with up to nine fractional digits, ending with 's'.
75    /// Example: "3.5s" or "86400s".
76    Ttl {
77        /// The TTL string (e.g. `"86400s"`).
78        ttl: String,
79    },
80}
81
82impl CacheExpirationRequest {
83    /// Create expiration with TTL from a Duration.
84    pub fn from_ttl(duration: std::time::Duration) -> Self {
85        Self::Ttl { ttl: format!("{}s", duration.as_secs()) }
86    }
87
88    /// Create expiration with specific expire time.
89    pub fn from_expire_time(expire_time: OffsetDateTime) -> Self {
90        Self::ExpireTime { expire_time }
91    }
92}
93
94/// Expiration information in cached content responses.
95#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
96#[serde(rename_all = "camelCase")]
97pub struct CacheExpirationResponse {
98    /// Timestamp in UTC of when this resource is considered expired.
99    /// This is always provided on output, regardless of what was sent on input.
100    #[serde(skip_serializing_if = "Option::is_none")]
101    #[serde(default, with = "time::serde::rfc3339::option")]
102    pub expire_time: Option<OffsetDateTime>,
103    /// TTL that was set for this resource (input only, may not be present in response).
104    #[serde(skip_serializing_if = "Option::is_none")]
105    pub ttl: Option<String>,
106}
107
108/// Request for creating cached content.
109#[derive(Debug, Clone, Serialize, PartialEq)]
110#[serde(rename_all = "camelCase")]
111pub struct CreateCachedContentRequest {
112    /// The user-generated meaningful display name of the cached content.
113    /// Maximum 128 Unicode characters.
114    #[serde(skip_serializing_if = "Option::is_none")]
115    pub display_name: Option<String>,
116
117    /// Required. The name of the model to use for cached content.
118    pub model: Model,
119
120    /// Optional. Input only. Immutable. The content to cache.
121    #[serde(skip_serializing_if = "Option::is_none")]
122    pub contents: Option<Vec<Content>>,
123
124    /// Optional. Input only. Immutable. A list of tools the model may use to generate the next response.
125    #[serde(skip_serializing_if = "Option::is_none")]
126    pub tools: Option<Vec<Tool>>,
127
128    /// Optional. Input only. Immutable. Developer set system instruction. Currently text only.
129    #[serde(skip_serializing_if = "Option::is_none")]
130    pub system_instruction: Option<Content>,
131
132    /// Optional. Input only. Immutable. Tool config. This config is shared for all tools.
133    #[serde(skip_serializing_if = "Option::is_none")]
134    pub tool_config: Option<ToolConfig>,
135
136    /// Expiration configuration for the cached content.
137    #[serde(flatten)]
138    pub expiration: CacheExpirationRequest,
139}
140
141/// Summary of cached content (used in list operations, may omit large content fields).
142#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
143#[serde(rename_all = "camelCase")]
144pub struct CachedContentSummary {
145    /// The resource name of the cached content.
146    pub name: String,
147
148    /// The name of the model used for cached content.
149    pub model: Model,
150
151    /// Creation time of the cached content.
152    #[serde(with = "time::serde::rfc3339")]
153    pub create_time: OffsetDateTime,
154
155    /// Last update time of the cached content.
156    #[serde(with = "time::serde::rfc3339")]
157    pub update_time: OffsetDateTime,
158
159    /// Usage metadata for the cached content.
160    pub usage_metadata: CacheUsageMetadata,
161
162    /// Expiration information.
163    #[serde(flatten)]
164    pub expiration: CacheExpirationResponse,
165
166    /// Display name if provided.
167    #[serde(skip_serializing_if = "Option::is_none")]
168    pub display_name: Option<String>,
169}
170
171/// Response from listing cached contents.
172#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
173#[serde(rename_all = "camelCase")]
174pub struct ListCachedContentsResponse {
175    /// The list of cached content summaries.
176    #[serde(default)]
177    pub cached_contents: Vec<CachedContentSummary>,
178    /// A token to retrieve the next page of results.
179    #[serde(skip_serializing_if = "Option::is_none")]
180    pub next_page_token: Option<String>,
181}