rs_adk/session/
vertex_ai.rs1use async_trait::async_trait;
8
9use super::{Session, SessionError, SessionId, SessionService};
10use crate::events::Event;
11
12#[derive(Debug, Clone)]
14pub struct VertexAiSessionConfig {
15 pub project: String,
17 pub location: String,
19 pub ttl_seconds: Option<u64>,
22}
23
24impl VertexAiSessionConfig {
25 pub fn new(project: impl Into<String>, location: impl Into<String>) -> Self {
27 Self {
28 project: project.into(),
29 location: location.into(),
30 ttl_seconds: None,
31 }
32 }
33
34 pub fn ttl_seconds(mut self, ttl: u64) -> Self {
36 self.ttl_seconds = Some(ttl);
37 self
38 }
39
40 fn base_url(&self) -> String {
44 format!(
45 "https://{location}-aiplatform.googleapis.com/v1beta1/projects/{project}/locations/{location}",
46 project = self.project,
47 location = self.location,
48 )
49 }
50
51 fn sessions_url(&self, engine_id: &str) -> String {
53 format!(
54 "{}/reasoningEngines/{}/sessions",
55 self.base_url(),
56 engine_id,
57 )
58 }
59
60 fn session_url(&self, engine_id: &str, session_id: &str) -> String {
62 format!("{}/{}", self.sessions_url(engine_id), session_id)
63 }
64
65 fn events_url(&self, engine_id: &str, session_id: &str) -> String {
67 format!("{}/events", self.session_url(engine_id, session_id))
68 }
69}
70
71pub struct VertexAiSessionService {
79 config: VertexAiSessionConfig,
80 }
85
86impl VertexAiSessionService {
87 pub fn new(config: VertexAiSessionConfig) -> Self {
89 Self { config }
90 }
91
92 pub fn project(&self) -> &str {
94 &self.config.project
95 }
96
97 pub fn location(&self) -> &str {
99 &self.config.location
100 }
101
102 pub fn ttl_seconds(&self) -> Option<u64> {
104 self.config.ttl_seconds
105 }
106}
107
108#[async_trait]
109impl SessionService for VertexAiSessionService {
110 async fn create_session(&self, app_name: &str, user_id: &str) -> Result<Session, SessionError> {
111 let _url = self.config.sessions_url(app_name);
112 let _user = user_id;
113
114 let _ttl_body = self
126 .config
127 .ttl_seconds
128 .map(|t| format!("\"ttl\": \"{t}s\""));
129
130 todo!("POST to {_url} to create Vertex AI session for user={_user}")
131 }
132
133 async fn get_session(&self, id: &SessionId) -> Result<Option<Session>, SessionError> {
134 let _url = self.config.session_url("default", id.as_str());
135
136 todo!("GET {_url} to fetch Vertex AI session")
143 }
144
145 async fn list_sessions(
146 &self,
147 app_name: &str,
148 user_id: &str,
149 ) -> Result<Vec<Session>, SessionError> {
150 let _url = self.config.sessions_url(app_name);
151 let _user = user_id;
152
153 todo!("GET {_url} to list Vertex AI sessions for user={_user}")
159 }
160
161 async fn delete_session(&self, id: &SessionId) -> Result<(), SessionError> {
162 let _url = self.config.session_url("default", id.as_str());
163
164 todo!("DELETE {_url} to remove Vertex AI session")
170 }
171
172 async fn append_event(&self, id: &SessionId, event: Event) -> Result<(), SessionError> {
173 let _url = self.config.events_url("default", id.as_str());
174 let _event_json =
175 serde_json::to_value(&event).map_err(|e| SessionError::Storage(e.to_string()))?;
176
177 todo!("POST to {_url} to append event to Vertex AI session")
189 }
190
191 async fn get_events(&self, id: &SessionId) -> Result<Vec<Event>, SessionError> {
192 let _url = self.config.events_url("default", id.as_str());
193
194 todo!("GET {_url} to fetch events for Vertex AI session")
201 }
202}
203
204#[cfg(test)]
205mod tests {
206 use super::*;
207
208 #[test]
209 fn config_new() {
210 let config = VertexAiSessionConfig::new("my-project", "us-central1");
211 assert_eq!(config.project, "my-project");
212 assert_eq!(config.location, "us-central1");
213 assert!(config.ttl_seconds.is_none());
214 }
215
216 #[test]
217 fn config_with_ttl() {
218 let config = VertexAiSessionConfig::new("proj", "us-east1").ttl_seconds(3600);
219 assert_eq!(config.ttl_seconds, Some(3600));
220 }
221
222 #[test]
223 fn url_construction() {
224 let config = VertexAiSessionConfig::new("my-project", "us-central1");
225 assert_eq!(
226 config.base_url(),
227 "https://us-central1-aiplatform.googleapis.com/v1beta1/projects/my-project/locations/us-central1"
228 );
229 assert!(config
230 .sessions_url("engine-1")
231 .contains("reasoningEngines/engine-1/sessions"));
232 assert!(config
233 .session_url("engine-1", "sess-1")
234 .contains("sessions/sess-1"));
235 assert!(config
236 .events_url("engine-1", "sess-1")
237 .contains("sessions/sess-1/events"));
238 }
239
240 #[test]
241 fn service_accessors() {
242 let svc = VertexAiSessionService::new(
243 VertexAiSessionConfig::new("proj", "us-west1").ttl_seconds(7200),
244 );
245 assert_eq!(svc.project(), "proj");
246 assert_eq!(svc.location(), "us-west1");
247 assert_eq!(svc.ttl_seconds(), Some(7200));
248 }
249}