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