gemini_rust/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        #[serde(with = "time::serde::rfc3339")]
70        expire_time: OffsetDateTime,
71    },
72    /// New TTL for this resource, input only.
73    /// A duration in seconds with up to nine fractional digits, ending with 's'.
74    /// Example: "3.5s" or "86400s".
75    Ttl { ttl: String },
76}
77
78impl CacheExpirationRequest {
79    /// Create expiration with TTL from a Duration.
80    pub fn from_ttl(duration: std::time::Duration) -> Self {
81        Self::Ttl {
82            ttl: format!("{}s", duration.as_secs()),
83        }
84    }
85
86    /// Create expiration with specific expire time.
87    pub fn from_expire_time(expire_time: OffsetDateTime) -> Self {
88        Self::ExpireTime { expire_time }
89    }
90}
91
92/// Expiration information in cached content responses.
93#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
94#[serde(rename_all = "camelCase")]
95pub struct CacheExpirationResponse {
96    /// Timestamp in UTC of when this resource is considered expired.
97    /// This is always provided on output, regardless of what was sent on input.
98    #[serde(skip_serializing_if = "Option::is_none")]
99    #[serde(default, with = "time::serde::rfc3339::option")]
100    pub expire_time: Option<OffsetDateTime>,
101    /// TTL that was set for this resource (input only, may not be present in response).
102    #[serde(skip_serializing_if = "Option::is_none")]
103    pub ttl: Option<String>,
104}
105
106/// Request for creating cached content.
107#[derive(Debug, Clone, Serialize, PartialEq)]
108#[serde(rename_all = "camelCase")]
109pub struct CreateCachedContentRequest {
110    /// The user-generated meaningful display name of the cached content.
111    /// Maximum 128 Unicode characters.
112    #[serde(skip_serializing_if = "Option::is_none")]
113    pub display_name: Option<String>,
114
115    /// Required. The name of the model to use for cached content.
116    pub model: Model,
117
118    /// Optional. Input only. Immutable. The content to cache.
119    #[serde(skip_serializing_if = "Option::is_none")]
120    pub contents: Option<Vec<Content>>,
121
122    /// Optional. Input only. Immutable. A list of tools the model may use to generate the next response.
123    #[serde(skip_serializing_if = "Option::is_none")]
124    pub tools: Option<Vec<Tool>>,
125
126    /// Optional. Input only. Immutable. Developer set system instruction. Currently text only.
127    #[serde(skip_serializing_if = "Option::is_none")]
128    pub system_instruction: Option<Content>,
129
130    /// Optional. Input only. Immutable. Tool config. This config is shared for all tools.
131    #[serde(skip_serializing_if = "Option::is_none")]
132    pub tool_config: Option<ToolConfig>,
133
134    /// Expiration configuration for the cached content.
135    #[serde(flatten)]
136    pub expiration: CacheExpirationRequest,
137}
138
139/// Summary of cached content (used in list operations, may omit large content fields).
140#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
141#[serde(rename_all = "camelCase")]
142pub struct CachedContentSummary {
143    /// The resource name of the cached content.
144    pub name: String,
145
146    /// The name of the model used for cached content.
147    pub model: Model,
148
149    /// Creation time of the cached content.
150    #[serde(with = "time::serde::rfc3339")]
151    pub create_time: OffsetDateTime,
152
153    /// Last update time of the cached content.
154    #[serde(with = "time::serde::rfc3339")]
155    pub update_time: OffsetDateTime,
156
157    /// Usage metadata for the cached content.
158    pub usage_metadata: CacheUsageMetadata,
159
160    /// Expiration information.
161    #[serde(flatten)]
162    pub expiration: CacheExpirationResponse,
163
164    /// Display name if provided.
165    #[serde(skip_serializing_if = "Option::is_none")]
166    pub display_name: Option<String>,
167}
168
169/// Response from listing cached contents.
170#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
171#[serde(rename_all = "camelCase")]
172pub struct ListCachedContentsResponse {
173    /// The list of cached content summaries.
174    #[serde(default)]
175    pub cached_contents: Vec<CachedContentSummary>,
176    /// A token to retrieve the next page of results.
177    #[serde(skip_serializing_if = "Option::is_none")]
178    pub next_page_token: Option<String>,
179}