bamboo_llm/protocol/
mod.rs1mod anthropic;
15mod errors;
16pub mod gemini;
17pub mod llama_cpp;
18mod openai;
19
20pub use anthropic::AnthropicProtocol;
21pub use errors::{ProtocolError, ProtocolResult};
22pub use gemini::GeminiProtocol;
23pub use openai::OpenAIProtocol;
24
25use bamboo_domain::Message;
26
27pub trait FromProvider<T>: Sized {
31 fn from_provider(value: T) -> ProtocolResult<Self>;
33}
34
35pub trait ToProvider<T>: Sized {
39 fn to_provider(&self) -> ProtocolResult<T>;
41}
42
43pub trait FromProviderBatch<T>: Sized {
45 fn from_provider_batch(values: Vec<T>) -> ProtocolResult<Vec<Self>>;
46}
47
48pub trait ToProviderBatch<T>: Sized {
50 fn to_provider_batch(&self) -> ProtocolResult<Vec<T>>;
51}
52
53impl FromProviderBatch<crate::api::models::ChatMessage> for Message {
56 fn from_provider_batch(
57 values: Vec<crate::api::models::ChatMessage>,
58 ) -> ProtocolResult<Vec<Self>> {
59 values.into_iter().map(Self::from_provider).collect()
60 }
61}
62
63impl ToProviderBatch<crate::api::models::ChatMessage> for Vec<Message> {
64 fn to_provider_batch(&self) -> ProtocolResult<Vec<crate::api::models::ChatMessage>> {
65 self.iter().map(|msg| msg.to_provider()).collect()
66 }
67}
68
69#[cfg(test)]
70mod tests {
71 use super::*;
72
73 #[test]
74 fn test_trait_bounds() {
75 fn assert_from_provider<T, U>()
77 where
78 T: FromProvider<U>,
79 {
80 }
81
82 fn assert_to_provider<T, U>()
83 where
84 T: ToProvider<U>,
85 {
86 }
87
88 assert_from_provider::<Message, crate::api::models::ChatMessage>();
90 assert_to_provider::<Message, crate::api::models::ChatMessage>();
91 }
92}