gemini_rust/cache/
model.rs

1use serde::{Deserialize, Serialize};
2use time::OffsetDateTime;
3
4use crate::models::Content;
5use crate::{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    /// Format: models/{model}
17    pub model: String,
18
19    /// Output only. Creation time of the cached content.
20    #[serde(with = "time::serde::rfc3339")]
21    pub create_time: OffsetDateTime,
22
23    /// Output only. Last update time of the cached content.
24    #[serde(with = "time::serde::rfc3339")]
25    pub update_time: OffsetDateTime,
26
27    /// Output only. Usage metadata for the cached content.
28    pub usage_metadata: CacheUsageMetadata,
29
30    /// Expiration information for the cached content.
31    #[serde(flatten)]
32    pub expiration: CacheExpirationResponse,
33
34    /// The user-generated display name (if provided).
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub display_name: Option<String>,
37
38    /// The cached contents (may be omitted in some API responses for size).
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub contents: Option<Vec<Content>>,
41
42    /// The cached tools (may be omitted in some API responses for size).
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub tools: Option<Vec<Tool>>,
45
46    /// The cached system instruction (may be omitted in some API responses for size).
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub system_instruction: Option<Content>,
49
50    /// The cached tool config (may be omitted in some API responses for size).
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub tool_config: Option<ToolConfig>,
53}
54
55/// Usage metadata specifically for cached content (more predictable than general UsageMetadata).
56#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
57#[serde(rename_all = "camelCase")]
58pub struct CacheUsageMetadata {
59    /// Total tokens in the cached content.
60    pub total_token_count: i32,
61}
62
63/// Expiration configuration for cached content in requests (union type).
64#[derive(Debug, Clone, Serialize, PartialEq)]
65#[serde(untagged)]
66pub enum CacheExpirationRequest {
67    /// Timestamp in UTC of when this resource is considered expired.
68    /// Uses RFC 3339 format.
69    ExpireTime {
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 { ttl: String },
77}
78
79impl CacheExpirationRequest {
80    /// Create expiration with TTL from a Duration.
81    pub fn from_ttl(duration: std::time::Duration) -> Self {
82        Self::Ttl {
83            ttl: format!("{}s", duration.as_secs()),
84        }
85    }
86
87    /// Create expiration with specific expire time.
88    pub fn from_expire_time(expire_time: OffsetDateTime) -> Self {
89        Self::ExpireTime { expire_time }
90    }
91}
92
93/// Expiration information in cached content responses.
94#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
95#[serde(rename_all = "camelCase")]
96pub struct CacheExpirationResponse {
97    /// Timestamp in UTC of when this resource is considered expired.
98    /// This is always provided on output, regardless of what was sent on input.
99    #[serde(skip_serializing_if = "Option::is_none")]
100    #[serde(default, with = "time::serde::rfc3339::option")]
101    pub expire_time: Option<OffsetDateTime>,
102    /// TTL that was set for this resource (input only, may not be present in response).
103    #[serde(skip_serializing_if = "Option::is_none")]
104    pub ttl: Option<String>,
105}
106
107/// Request for creating cached content.
108#[derive(Debug, Clone, Serialize, PartialEq)]
109#[serde(rename_all = "camelCase")]
110pub struct CreateCachedContentRequest {
111    /// The user-generated meaningful display name of the cached content.
112    /// Maximum 128 Unicode characters.
113    #[serde(skip_serializing_if = "Option::is_none")]
114    pub display_name: Option<String>,
115
116    /// Required. The name of the model to use for cached content.
117    /// Format: models/{model}
118    pub model: String,
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/// Response from deleting cached content.
142#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
143pub struct DeleteCachedContentResponse {
144    /// HTTP status and metadata
145    #[serde(skip_serializing_if = "Option::is_none")]
146    pub success: Option<bool>,
147}
148
149/// Summary of cached content (used in list operations, may omit large content fields).
150#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
151#[serde(rename_all = "camelCase")]
152pub struct CachedContentSummary {
153    /// The resource name of the cached content.
154    pub name: String,
155
156    /// The name of the model used for cached content.
157    pub model: String,
158
159    /// Creation time of the cached content.
160    #[serde(with = "time::serde::rfc3339")]
161    pub create_time: OffsetDateTime,
162
163    /// Last update time of the cached content.
164    #[serde(with = "time::serde::rfc3339")]
165    pub update_time: OffsetDateTime,
166
167    /// Usage metadata for the cached content.
168    pub usage_metadata: CacheUsageMetadata,
169
170    /// Expiration information.
171    #[serde(flatten)]
172    pub expiration: CacheExpirationResponse,
173
174    /// Display name if provided.
175    #[serde(skip_serializing_if = "Option::is_none")]
176    pub display_name: Option<String>,
177}
178
179/// Response from listing cached contents.
180#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
181#[serde(rename_all = "camelCase")]
182pub struct ListCachedContentsResponse {
183    /// The list of cached content summaries.
184    #[serde(default)]
185    pub cached_contents: Vec<CachedContentSummary>,
186    /// A token to retrieve the next page of results.
187    #[serde(skip_serializing_if = "Option::is_none")]
188    pub next_page_token: Option<String>,
189}