Skip to main content

gemini_adk_rs/artifacts/
mod.rs

1//! Artifact service — versioned binary/JSON artifact storage.
2//!
3//! Mirrors ADK-JS's `BaseArtifactService`. Provides a trait for storing
4//! and retrieving versioned artifacts with an in-memory default.
5
6mod file_service;
7mod forwarding;
8#[cfg(feature = "gcs-artifacts")]
9mod gcs_service;
10mod in_memory;
11
12pub use file_service::FileArtifactService;
13pub use forwarding::ForwardingArtifactService;
14#[cfg(feature = "gcs-artifacts")]
15pub use gcs_service::GcsArtifactService;
16pub use in_memory::InMemoryArtifactService;
17
18use async_trait::async_trait;
19use serde::{Deserialize, Serialize};
20
21/// Metadata for a stored artifact.
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct ArtifactMetadata {
24    /// Artifact name/key.
25    pub name: String,
26    /// MIME type (e.g., "application/json", "image/png").
27    pub mime_type: String,
28    /// Current version number (1-based).
29    pub version: u32,
30    /// Size in bytes.
31    pub size: usize,
32    /// When created (Unix timestamp seconds).
33    pub created_at: u64,
34    /// When last updated (Unix timestamp seconds).
35    pub updated_at: u64,
36}
37
38/// A versioned artifact with data and metadata.
39#[derive(Debug, Clone)]
40pub struct Artifact {
41    /// Artifact metadata.
42    pub metadata: ArtifactMetadata,
43    /// The artifact data.
44    pub data: Vec<u8>,
45}
46
47impl Artifact {
48    /// Create a new artifact.
49    pub fn new(name: impl Into<String>, mime_type: impl Into<String>, data: Vec<u8>) -> Self {
50        let now = now_secs();
51        let size = data.len();
52        Self {
53            metadata: ArtifactMetadata {
54                name: name.into(),
55                mime_type: mime_type.into(),
56                version: 1,
57                size,
58                created_at: now,
59                updated_at: now,
60            },
61            data,
62        }
63    }
64
65    /// Create a JSON artifact.
66    pub fn json(name: impl Into<String>, value: &serde_json::Value) -> Self {
67        let data = serde_json::to_vec(value).unwrap_or_default();
68        Self::new(name, "application/json", data)
69    }
70
71    /// Create a text artifact.
72    pub fn text(name: impl Into<String>, text: impl Into<String>) -> Self {
73        Self::new(name, "text/plain", text.into().into_bytes())
74    }
75}
76
77/// Errors from artifact service operations.
78#[derive(Debug, thiserror::Error)]
79pub enum ArtifactError {
80    /// The requested artifact was not found.
81    #[error("Artifact not found: {0}")]
82    NotFound(String),
83    /// The requested version of the artifact was not found.
84    #[error("Version not found: {name} v{version}")]
85    VersionNotFound {
86        /// Artifact name.
87        name: String,
88        /// Requested version number.
89        version: u32,
90    },
91    /// A storage backend error.
92    #[error("Storage error: {0}")]
93    Storage(String),
94}
95
96/// Trait for artifact persistence — CRUD with versioning.
97///
98/// Artifacts are scoped by session ID and identified by name.
99/// Each update creates a new version.
100#[async_trait]
101pub trait ArtifactService: Send + Sync {
102    /// Save an artifact, creating a new version if it already exists.
103    async fn save(
104        &self,
105        session_id: &str,
106        artifact: Artifact,
107    ) -> Result<ArtifactMetadata, ArtifactError>;
108
109    /// Load the latest version of an artifact.
110    async fn load(&self, session_id: &str, name: &str) -> Result<Option<Artifact>, ArtifactError>;
111
112    /// Load a specific version of an artifact.
113    async fn load_version(
114        &self,
115        session_id: &str,
116        name: &str,
117        version: u32,
118    ) -> Result<Option<Artifact>, ArtifactError>;
119
120    /// List all artifact metadata for a session.
121    async fn list(&self, session_id: &str) -> Result<Vec<ArtifactMetadata>, ArtifactError>;
122
123    /// Delete all versions of an artifact.
124    async fn delete(&self, session_id: &str, name: &str) -> Result<(), ArtifactError>;
125}
126
127pub(crate) use crate::utils::time::now_secs;
128
129#[cfg(test)]
130mod tests {
131    use super::*;
132
133    #[test]
134    fn artifact_new() {
135        let a = Artifact::new("file.bin", "application/octet-stream", vec![1, 2, 3]);
136        assert_eq!(a.metadata.name, "file.bin");
137        assert_eq!(a.metadata.mime_type, "application/octet-stream");
138        assert_eq!(a.metadata.version, 1);
139        assert_eq!(a.metadata.size, 3);
140        assert_eq!(a.data, vec![1, 2, 3]);
141    }
142
143    #[test]
144    fn artifact_json() {
145        let val = serde_json::json!({"key": "value"});
146        let a = Artifact::json("config", &val);
147        assert_eq!(a.metadata.mime_type, "application/json");
148        let parsed: serde_json::Value = serde_json::from_slice(&a.data).unwrap();
149        assert_eq!(parsed["key"], "value");
150    }
151
152    #[test]
153    fn artifact_text() {
154        let a = Artifact::text("readme", "Hello, world!");
155        assert_eq!(a.metadata.mime_type, "text/plain");
156        assert_eq!(std::str::from_utf8(&a.data).unwrap(), "Hello, world!");
157    }
158
159    #[test]
160    fn artifact_service_is_object_safe() {
161        fn _assert(_: &dyn ArtifactService) {}
162    }
163
164    #[tokio::test]
165    async fn save_and_load() {
166        let svc = InMemoryArtifactService::new();
167        let artifact = Artifact::text("notes", "First version");
168        svc.save("s1", artifact).await.unwrap();
169
170        let loaded = svc.load("s1", "notes").await.unwrap();
171        assert!(loaded.is_some());
172        let loaded = loaded.unwrap();
173        assert_eq!(std::str::from_utf8(&loaded.data).unwrap(), "First version");
174        assert_eq!(loaded.metadata.version, 1);
175    }
176
177    #[tokio::test]
178    async fn versioning() {
179        let svc = InMemoryArtifactService::new();
180        svc.save("s1", Artifact::text("notes", "v1")).await.unwrap();
181        svc.save("s1", Artifact::text("notes", "v2")).await.unwrap();
182        svc.save("s1", Artifact::text("notes", "v3")).await.unwrap();
183
184        // Latest should be v3
185        let latest = svc.load("s1", "notes").await.unwrap().unwrap();
186        assert_eq!(latest.metadata.version, 3);
187        assert_eq!(std::str::from_utf8(&latest.data).unwrap(), "v3");
188
189        // Load specific version
190        let v1 = svc.load_version("s1", "notes", 1).await.unwrap().unwrap();
191        assert_eq!(std::str::from_utf8(&v1.data).unwrap(), "v1");
192
193        let v2 = svc.load_version("s1", "notes", 2).await.unwrap().unwrap();
194        assert_eq!(std::str::from_utf8(&v2.data).unwrap(), "v2");
195    }
196
197    #[tokio::test]
198    async fn load_nonexistent_returns_none() {
199        let svc = InMemoryArtifactService::new();
200        let result = svc.load("s1", "missing").await.unwrap();
201        assert!(result.is_none());
202    }
203
204    #[tokio::test]
205    async fn list_artifacts() {
206        let svc = InMemoryArtifactService::new();
207        svc.save("s1", Artifact::text("a", "data")).await.unwrap();
208        svc.save("s1", Artifact::text("b", "data")).await.unwrap();
209        svc.save("s2", Artifact::text("c", "data")).await.unwrap();
210
211        let list = svc.list("s1").await.unwrap();
212        assert_eq!(list.len(), 2);
213    }
214
215    #[tokio::test]
216    async fn delete_artifact() {
217        let svc = InMemoryArtifactService::new();
218        svc.save("s1", Artifact::text("notes", "data"))
219            .await
220            .unwrap();
221        svc.delete("s1", "notes").await.unwrap();
222        let result = svc.load("s1", "notes").await.unwrap();
223        assert!(result.is_none());
224    }
225}