firebase_rs_sdk/ai/
backend.rs

1use std::fmt;
2
3use crate::ai::constants::DEFAULT_LOCATION;
4
5/// Supported backend types for the Firebase AI SDK.
6///
7/// Ported from `packages/ai/src/public-types.ts` (`BackendType`).
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub enum BackendType {
10    VertexAi,
11    GoogleAi,
12}
13
14impl fmt::Display for BackendType {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        match self {
17            BackendType::VertexAi => write!(f, "VERTEX_AI"),
18            BackendType::GoogleAi => write!(f, "GOOGLE_AI"),
19        }
20    }
21}
22
23/// Configuration for the Gemini Developer API backend.
24///
25/// Mirrors `GoogleAIBackend` in `packages/ai/src/backend.ts`.
26#[derive(Clone, Debug, PartialEq, Eq, Default)]
27pub struct GoogleAiBackend;
28
29impl GoogleAiBackend {
30    /// Creates a configuration for the Gemini Developer API backend.
31    pub fn new() -> Self {
32        Self
33    }
34
35    /// Returns the backend type tag.
36    pub fn backend_type(&self) -> BackendType {
37        BackendType::GoogleAi
38    }
39}
40
41/// Configuration for the Vertex AI Gemini backend.
42///
43/// Mirrors `VertexAIBackend` in `packages/ai/src/backend.ts`.
44#[derive(Clone, Debug, PartialEq, Eq)]
45pub struct VertexAiBackend {
46    location: String,
47}
48
49impl VertexAiBackend {
50    /// Creates a configuration for the Vertex AI backend.
51    ///
52    /// When `location` is empty the default `us-central1` region is used, matching
53    /// the TypeScript implementation.
54    pub fn new<S: Into<String>>(location: S) -> Self {
55        let location = location.into();
56        let location = if location.trim().is_empty() {
57            DEFAULT_LOCATION.to_string()
58        } else {
59            location
60        };
61        Self { location }
62    }
63
64    /// Returns the configured region (e.g. `us-central1`).
65    pub fn location(&self) -> &str {
66        &self.location
67    }
68
69    /// Returns the backend type tag.
70    pub fn backend_type(&self) -> BackendType {
71        BackendType::VertexAi
72    }
73}
74
75impl Default for VertexAiBackend {
76    fn default() -> Self {
77        Self::new(DEFAULT_LOCATION)
78    }
79}
80
81/// High-level backend configuration enum used by the Rust port.
82///
83/// This combines the behaviour of the TypeScript `Backend` base class and its concrete
84/// subclasses so we can work with a single, cloneable configuration value.
85#[derive(Clone, Debug, PartialEq, Eq)]
86pub enum Backend {
87    GoogleAi(GoogleAiBackend),
88    VertexAi(VertexAiBackend),
89}
90
91impl Backend {
92    /// Creates a Google AI backend configuration.
93    pub fn google_ai() -> Self {
94        Self::GoogleAi(GoogleAiBackend::new())
95    }
96
97    /// Creates a Vertex AI backend configuration with the provided region.
98    pub fn vertex_ai<S: Into<String>>(location: S) -> Self {
99        Self::VertexAi(VertexAiBackend::new(location))
100    }
101
102    /// Returns the backend type tag.
103    pub fn backend_type(&self) -> BackendType {
104        match self {
105            Backend::GoogleAi(inner) => inner.backend_type(),
106            Backend::VertexAi(inner) => inner.backend_type(),
107        }
108    }
109
110    /// Returns the backend as a Vertex AI configuration if applicable.
111    pub fn as_vertex_ai(&self) -> Option<&VertexAiBackend> {
112        match self {
113            Backend::VertexAi(inner) => Some(inner),
114            _ => None,
115        }
116    }
117}
118
119impl Default for Backend {
120    fn default() -> Self {
121        Backend::google_ai()
122    }
123}