gemini_genai_rs/session/
traits.rs1use super::errors::SessionError;
7use super::events::SessionEvent;
8use super::state::SessionPhase;
9use crate::protocol::{Content, ContextUpdate, FunctionResponse};
10use crate::transport::CodecError;
11use async_trait::async_trait;
12use bytes::Bytes;
13use tokio::sync::broadcast;
14
15#[async_trait]
17pub trait SessionWriter: Send + Sync + 'static {
18 async fn send_audio(&self, data: Bytes) -> Result<(), SessionError>;
20 async fn send_text(&self, text: String) -> Result<(), SessionError>;
22 async fn send_tool_response(
24 &self,
25 responses: Vec<FunctionResponse>,
26 ) -> Result<(), SessionError>;
27 async fn send_client_content(
29 &self,
30 turns: Vec<Content>,
31 turn_complete: bool,
32 ) -> Result<(), SessionError>;
33 async fn send_video(&self, jpeg_data: Bytes) -> Result<(), SessionError>;
35 async fn update_instruction(&self, instruction: String) -> Result<(), SessionError>;
37 async fn update_context(&self, update: ContextUpdate) -> Result<(), SessionError> {
45 let _ = update;
46 Err(SessionError::Codec(CodecError::Serialize(
47 "this session writer does not send contextUpdate".into(),
48 )))
49 }
50 async fn signal_activity_start(&self) -> Result<(), SessionError>;
52 async fn signal_activity_end(&self) -> Result<(), SessionError>;
54 async fn disconnect(&self) -> Result<(), SessionError>;
56}
57
58pub trait SessionReader: Send + Sync + 'static {
60 fn subscribe(&self) -> broadcast::Receiver<SessionEvent>;
62 fn phase(&self) -> SessionPhase;
64 fn session_id(&self) -> &str;
66}
67
68#[cfg(test)]
69mod tests {
70 use super::super::handle::SessionHandle;
71 use super::*;
72
73 #[test]
74 fn session_handle_implements_session_writer() {
75 fn assert_impl<T: SessionWriter>() {}
76 assert_impl::<SessionHandle>();
77 }
78
79 #[test]
80 fn session_handle_implements_session_reader() {
81 fn assert_impl<T: SessionReader>() {}
82 assert_impl::<SessionHandle>();
83 }
84
85 #[test]
86 fn session_writer_is_object_safe() {
87 fn _assert(_: &dyn SessionWriter) {}
88 }
89
90 #[test]
91 fn session_reader_is_object_safe() {
92 fn _assert(_: &dyn SessionReader) {}
93 }
94}