lib.rs

  1pub mod api;
  2pub mod auth;
  3mod cents;
  4pub mod clickhouse;
  5pub mod db;
  6pub mod env;
  7pub mod executor;
  8pub mod llm;
  9pub mod migrations;
 10mod rate_limiter;
 11pub mod rpc;
 12pub mod seed;
 13pub mod stripe_billing;
 14pub mod user_backfiller;
 15
 16#[cfg(test)]
 17mod tests;
 18
 19use anyhow::anyhow;
 20use aws_config::{BehaviorVersion, Region};
 21use axum::{
 22    http::{HeaderMap, StatusCode},
 23    response::IntoResponse,
 24};
 25pub use cents::*;
 26use db::{ChannelId, Database};
 27use executor::Executor;
 28use llm::db::LlmDatabase;
 29pub use rate_limiter::*;
 30use serde::Deserialize;
 31use std::{path::PathBuf, sync::Arc};
 32use util::ResultExt;
 33
 34use crate::stripe_billing::StripeBilling;
 35
 36pub type Result<T, E = Error> = std::result::Result<T, E>;
 37
 38pub enum Error {
 39    Http(StatusCode, String, HeaderMap),
 40    Database(sea_orm::error::DbErr),
 41    Internal(anyhow::Error),
 42    Stripe(stripe::StripeError),
 43}
 44
 45impl From<anyhow::Error> for Error {
 46    fn from(error: anyhow::Error) -> Self {
 47        Self::Internal(error)
 48    }
 49}
 50
 51impl From<sea_orm::error::DbErr> for Error {
 52    fn from(error: sea_orm::error::DbErr) -> Self {
 53        Self::Database(error)
 54    }
 55}
 56
 57impl From<stripe::StripeError> for Error {
 58    fn from(error: stripe::StripeError) -> Self {
 59        Self::Stripe(error)
 60    }
 61}
 62
 63impl From<axum::Error> for Error {
 64    fn from(error: axum::Error) -> Self {
 65        Self::Internal(error.into())
 66    }
 67}
 68
 69impl From<axum::http::Error> for Error {
 70    fn from(error: axum::http::Error) -> Self {
 71        Self::Internal(error.into())
 72    }
 73}
 74
 75impl From<serde_json::Error> for Error {
 76    fn from(error: serde_json::Error) -> Self {
 77        Self::Internal(error.into())
 78    }
 79}
 80
 81impl Error {
 82    fn http(code: StatusCode, message: String) -> Self {
 83        Self::Http(code, message, HeaderMap::default())
 84    }
 85}
 86
 87impl IntoResponse for Error {
 88    fn into_response(self) -> axum::response::Response {
 89        match self {
 90            Error::Http(code, message, headers) => {
 91                log::error!("HTTP error {}: {}", code, &message);
 92                (code, headers, message).into_response()
 93            }
 94            Error::Database(error) => {
 95                log::error!(
 96                    "HTTP error {}: {:?}",
 97                    StatusCode::INTERNAL_SERVER_ERROR,
 98                    &error
 99                );
100                (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", &error)).into_response()
101            }
102            Error::Internal(error) => {
103                log::error!(
104                    "HTTP error {}: {:?}",
105                    StatusCode::INTERNAL_SERVER_ERROR,
106                    &error
107                );
108                (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", &error)).into_response()
109            }
110            Error::Stripe(error) => {
111                log::error!(
112                    "HTTP error {}: {:?}",
113                    StatusCode::INTERNAL_SERVER_ERROR,
114                    &error
115                );
116                (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", &error)).into_response()
117            }
118        }
119    }
120}
121
122impl std::fmt::Debug for Error {
123    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124        match self {
125            Error::Http(code, message, _headers) => (code, message).fmt(f),
126            Error::Database(error) => error.fmt(f),
127            Error::Internal(error) => error.fmt(f),
128            Error::Stripe(error) => error.fmt(f),
129        }
130    }
131}
132
133impl std::fmt::Display for Error {
134    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
135        match self {
136            Error::Http(code, message, _) => write!(f, "{code}: {message}"),
137            Error::Database(error) => error.fmt(f),
138            Error::Internal(error) => error.fmt(f),
139            Error::Stripe(error) => error.fmt(f),
140        }
141    }
142}
143
144impl std::error::Error for Error {}
145
146#[derive(Clone, Deserialize)]
147pub struct Config {
148    pub http_port: u16,
149    pub database_url: String,
150    pub migrations_path: Option<PathBuf>,
151    pub seed_path: Option<PathBuf>,
152    pub database_max_connections: u32,
153    pub api_token: String,
154    pub clickhouse_url: Option<String>,
155    pub clickhouse_user: Option<String>,
156    pub clickhouse_password: Option<String>,
157    pub clickhouse_database: Option<String>,
158    pub invite_link_prefix: String,
159    pub live_kit_server: Option<String>,
160    pub live_kit_key: Option<String>,
161    pub live_kit_secret: Option<String>,
162    pub llm_database_url: Option<String>,
163    pub llm_database_max_connections: Option<u32>,
164    pub llm_database_migrations_path: Option<PathBuf>,
165    pub llm_api_secret: Option<String>,
166    pub rust_log: Option<String>,
167    pub log_json: Option<bool>,
168    pub blob_store_url: Option<String>,
169    pub blob_store_region: Option<String>,
170    pub blob_store_access_key: Option<String>,
171    pub blob_store_secret_key: Option<String>,
172    pub blob_store_bucket: Option<String>,
173    pub zed_environment: Arc<str>,
174    pub openai_api_key: Option<Arc<str>>,
175    pub google_ai_api_key: Option<Arc<str>>,
176    pub anthropic_api_key: Option<Arc<str>>,
177    pub anthropic_staff_api_key: Option<Arc<str>>,
178    pub llm_closed_beta_model_name: Option<Arc<str>>,
179    pub zed_client_checksum_seed: Option<String>,
180    pub slack_panics_webhook: Option<String>,
181    pub auto_join_channel_id: Option<ChannelId>,
182    pub stripe_api_key: Option<String>,
183    pub supermaven_admin_api_key: Option<Arc<str>>,
184    pub user_backfiller_github_access_token: Option<Arc<str>>,
185}
186
187impl Config {
188    pub fn is_development(&self) -> bool {
189        self.zed_environment == "development".into()
190    }
191
192    /// Returns the base `zed.dev` URL.
193    pub fn zed_dot_dev_url(&self) -> &str {
194        match self.zed_environment.as_ref() {
195            "development" => "http://localhost:3000",
196            "staging" => "https://staging.zed.dev",
197            _ => "https://zed.dev",
198        }
199    }
200
201    #[cfg(test)]
202    pub fn test() -> Self {
203        Self {
204            http_port: 0,
205            database_url: "".into(),
206            database_max_connections: 0,
207            api_token: "".into(),
208            invite_link_prefix: "".into(),
209            live_kit_server: None,
210            live_kit_key: None,
211            live_kit_secret: None,
212            llm_database_url: None,
213            llm_database_max_connections: None,
214            llm_database_migrations_path: None,
215            llm_api_secret: None,
216            rust_log: None,
217            log_json: None,
218            zed_environment: "test".into(),
219            blob_store_url: None,
220            blob_store_region: None,
221            blob_store_access_key: None,
222            blob_store_secret_key: None,
223            blob_store_bucket: None,
224            openai_api_key: None,
225            google_ai_api_key: None,
226            anthropic_api_key: None,
227            anthropic_staff_api_key: None,
228            llm_closed_beta_model_name: None,
229            clickhouse_url: None,
230            clickhouse_user: None,
231            clickhouse_password: None,
232            clickhouse_database: None,
233            zed_client_checksum_seed: None,
234            slack_panics_webhook: None,
235            auto_join_channel_id: None,
236            migrations_path: None,
237            seed_path: None,
238            stripe_api_key: None,
239            supermaven_admin_api_key: None,
240            user_backfiller_github_access_token: None,
241        }
242    }
243}
244
245/// The service mode that collab should run in.
246#[derive(Debug, PartialEq, Eq, Clone, Copy, strum::Display)]
247#[strum(serialize_all = "snake_case")]
248pub enum ServiceMode {
249    Api,
250    Collab,
251    Llm,
252    All,
253}
254
255impl ServiceMode {
256    pub fn is_collab(&self) -> bool {
257        matches!(self, Self::Collab | Self::All)
258    }
259
260    pub fn is_api(&self) -> bool {
261        matches!(self, Self::Api | Self::All)
262    }
263
264    pub fn is_llm(&self) -> bool {
265        matches!(self, Self::Llm | Self::All)
266    }
267}
268
269pub struct AppState {
270    pub db: Arc<Database>,
271    pub llm_db: Option<Arc<LlmDatabase>>,
272    pub live_kit_client: Option<Arc<dyn live_kit_server::api::Client>>,
273    pub blob_store_client: Option<aws_sdk_s3::Client>,
274    pub stripe_client: Option<Arc<stripe::Client>>,
275    pub stripe_billing: Option<Arc<StripeBilling>>,
276    pub rate_limiter: Arc<RateLimiter>,
277    pub executor: Executor,
278    pub clickhouse_client: Option<::clickhouse::Client>,
279    pub config: Config,
280}
281
282impl AppState {
283    pub async fn new(config: Config, executor: Executor) -> Result<Arc<Self>> {
284        let mut db_options = db::ConnectOptions::new(config.database_url.clone());
285        db_options.max_connections(config.database_max_connections);
286        let mut db = Database::new(db_options, Executor::Production).await?;
287        db.initialize_notification_kinds().await?;
288
289        let llm_db = if let Some((llm_database_url, llm_database_max_connections)) = config
290            .llm_database_url
291            .clone()
292            .zip(config.llm_database_max_connections)
293        {
294            let mut llm_db_options = db::ConnectOptions::new(llm_database_url);
295            llm_db_options.max_connections(llm_database_max_connections);
296            let mut llm_db = LlmDatabase::new(llm_db_options, executor.clone()).await?;
297            llm_db.initialize().await?;
298            Some(Arc::new(llm_db))
299        } else {
300            None
301        };
302
303        let live_kit_client = if let Some(((server, key), secret)) = config
304            .live_kit_server
305            .as_ref()
306            .zip(config.live_kit_key.as_ref())
307            .zip(config.live_kit_secret.as_ref())
308        {
309            Some(Arc::new(live_kit_server::api::LiveKitClient::new(
310                server.clone(),
311                key.clone(),
312                secret.clone(),
313            )) as Arc<dyn live_kit_server::api::Client>)
314        } else {
315            None
316        };
317
318        let db = Arc::new(db);
319        let stripe_client = build_stripe_client(&config).map(Arc::new).log_err();
320        let this = Self {
321            db: db.clone(),
322            llm_db,
323            live_kit_client,
324            blob_store_client: build_blob_store_client(&config).await.log_err(),
325            stripe_billing: stripe_client
326                .clone()
327                .map(|stripe_client| Arc::new(StripeBilling::new(stripe_client))),
328            stripe_client,
329            rate_limiter: Arc::new(RateLimiter::new(db)),
330            executor,
331            clickhouse_client: config
332                .clickhouse_url
333                .as_ref()
334                .and_then(|_| build_clickhouse_client(&config).log_err()),
335            config,
336        };
337        Ok(Arc::new(this))
338    }
339}
340
341fn build_stripe_client(config: &Config) -> anyhow::Result<stripe::Client> {
342    let api_key = config
343        .stripe_api_key
344        .as_ref()
345        .ok_or_else(|| anyhow!("missing stripe_api_key"))?;
346    Ok(stripe::Client::new(api_key))
347}
348
349async fn build_blob_store_client(config: &Config) -> anyhow::Result<aws_sdk_s3::Client> {
350    let keys = aws_sdk_s3::config::Credentials::new(
351        config
352            .blob_store_access_key
353            .clone()
354            .ok_or_else(|| anyhow!("missing blob_store_access_key"))?,
355        config
356            .blob_store_secret_key
357            .clone()
358            .ok_or_else(|| anyhow!("missing blob_store_secret_key"))?,
359        None,
360        None,
361        "env",
362    );
363
364    let s3_config = aws_config::defaults(BehaviorVersion::latest())
365        .endpoint_url(
366            config
367                .blob_store_url
368                .as_ref()
369                .ok_or_else(|| anyhow!("missing blob_store_url"))?,
370        )
371        .region(Region::new(
372            config
373                .blob_store_region
374                .clone()
375                .ok_or_else(|| anyhow!("missing blob_store_region"))?,
376        ))
377        .credentials_provider(keys)
378        .load()
379        .await;
380
381    Ok(aws_sdk_s3::Client::new(&s3_config))
382}
383
384fn build_clickhouse_client(config: &Config) -> anyhow::Result<::clickhouse::Client> {
385    Ok(::clickhouse::Client::default()
386        .with_url(
387            config
388                .clickhouse_url
389                .as_ref()
390                .ok_or_else(|| anyhow!("missing clickhouse_url"))?,
391        )
392        .with_user(
393            config
394                .clickhouse_user
395                .as_ref()
396                .ok_or_else(|| anyhow!("missing clickhouse_user"))?,
397        )
398        .with_password(
399            config
400                .clickhouse_password
401                .as_ref()
402                .ok_or_else(|| anyhow!("missing clickhouse_password"))?,
403        )
404        .with_database(
405            config
406                .clickhouse_database
407                .as_ref()
408                .ok_or_else(|| anyhow!("missing clickhouse_database"))?,
409        ))
410}