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 livekit_server: Option<String>,
160 pub livekit_key: Option<String>,
161 pub livekit_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 kinesis_region: Option<String>,
174 pub kinesis_stream: Option<String>,
175 pub kinesis_access_key: Option<String>,
176 pub kinesis_secret_key: Option<String>,
177 pub zed_environment: Arc<str>,
178 pub openai_api_key: Option<Arc<str>>,
179 pub google_ai_api_key: Option<Arc<str>>,
180 pub anthropic_api_key: Option<Arc<str>>,
181 pub anthropic_staff_api_key: Option<Arc<str>>,
182 pub llm_closed_beta_model_name: Option<Arc<str>>,
183 pub zed_client_checksum_seed: Option<String>,
184 pub slack_panics_webhook: Option<String>,
185 pub auto_join_channel_id: Option<ChannelId>,
186 pub stripe_api_key: Option<String>,
187 pub supermaven_admin_api_key: Option<Arc<str>>,
188 pub user_backfiller_github_access_token: Option<Arc<str>>,
189}
190
191impl Config {
192 pub fn is_development(&self) -> bool {
193 self.zed_environment == "development".into()
194 }
195
196 /// Returns the base `zed.dev` URL.
197 pub fn zed_dot_dev_url(&self) -> &str {
198 match self.zed_environment.as_ref() {
199 "development" => "http://localhost:3000",
200 "staging" => "https://staging.zed.dev",
201 _ => "https://zed.dev",
202 }
203 }
204
205 #[cfg(test)]
206 pub fn test() -> Self {
207 Self {
208 http_port: 0,
209 database_url: "".into(),
210 database_max_connections: 0,
211 api_token: "".into(),
212 invite_link_prefix: "".into(),
213 livekit_server: None,
214 livekit_key: None,
215 livekit_secret: None,
216 llm_database_url: None,
217 llm_database_max_connections: None,
218 llm_database_migrations_path: None,
219 llm_api_secret: None,
220 rust_log: None,
221 log_json: None,
222 zed_environment: "test".into(),
223 blob_store_url: None,
224 blob_store_region: None,
225 blob_store_access_key: None,
226 blob_store_secret_key: None,
227 blob_store_bucket: None,
228 openai_api_key: None,
229 google_ai_api_key: None,
230 anthropic_api_key: None,
231 anthropic_staff_api_key: None,
232 llm_closed_beta_model_name: None,
233 clickhouse_url: None,
234 clickhouse_user: None,
235 clickhouse_password: None,
236 clickhouse_database: None,
237 zed_client_checksum_seed: None,
238 slack_panics_webhook: None,
239 auto_join_channel_id: None,
240 migrations_path: None,
241 seed_path: None,
242 stripe_api_key: None,
243 supermaven_admin_api_key: None,
244 user_backfiller_github_access_token: None,
245 kinesis_region: None,
246 kinesis_access_key: None,
247 kinesis_secret_key: None,
248 kinesis_stream: None,
249 }
250 }
251}
252
253/// The service mode that collab should run in.
254#[derive(Debug, PartialEq, Eq, Clone, Copy, strum::Display)]
255#[strum(serialize_all = "snake_case")]
256pub enum ServiceMode {
257 Api,
258 Collab,
259 Llm,
260 All,
261}
262
263impl ServiceMode {
264 pub fn is_collab(&self) -> bool {
265 matches!(self, Self::Collab | Self::All)
266 }
267
268 pub fn is_api(&self) -> bool {
269 matches!(self, Self::Api | Self::All)
270 }
271
272 pub fn is_llm(&self) -> bool {
273 matches!(self, Self::Llm | Self::All)
274 }
275}
276
277pub struct AppState {
278 pub db: Arc<Database>,
279 pub llm_db: Option<Arc<LlmDatabase>>,
280 pub livekit_client: Option<Arc<dyn livekit_server::api::Client>>,
281 pub blob_store_client: Option<aws_sdk_s3::Client>,
282 pub stripe_client: Option<Arc<stripe::Client>>,
283 pub stripe_billing: Option<Arc<StripeBilling>>,
284 pub rate_limiter: Arc<RateLimiter>,
285 pub executor: Executor,
286 pub clickhouse_client: Option<::clickhouse::Client>,
287 pub kinesis_client: Option<::aws_sdk_kinesis::Client>,
288 pub config: Config,
289}
290
291impl AppState {
292 pub async fn new(config: Config, executor: Executor) -> Result<Arc<Self>> {
293 let mut db_options = db::ConnectOptions::new(config.database_url.clone());
294 db_options.max_connections(config.database_max_connections);
295 let mut db = Database::new(db_options, Executor::Production).await?;
296 db.initialize_notification_kinds().await?;
297
298 let llm_db = if let Some((llm_database_url, llm_database_max_connections)) = config
299 .llm_database_url
300 .clone()
301 .zip(config.llm_database_max_connections)
302 {
303 let mut llm_db_options = db::ConnectOptions::new(llm_database_url);
304 llm_db_options.max_connections(llm_database_max_connections);
305 let mut llm_db = LlmDatabase::new(llm_db_options, executor.clone()).await?;
306 llm_db.initialize().await?;
307 Some(Arc::new(llm_db))
308 } else {
309 None
310 };
311
312 let livekit_client = if let Some(((server, key), secret)) = config
313 .livekit_server
314 .as_ref()
315 .zip(config.livekit_key.as_ref())
316 .zip(config.livekit_secret.as_ref())
317 {
318 Some(Arc::new(livekit_server::api::LiveKitClient::new(
319 server.clone(),
320 key.clone(),
321 secret.clone(),
322 )) as Arc<dyn livekit_server::api::Client>)
323 } else {
324 None
325 };
326
327 let db = Arc::new(db);
328 let stripe_client = build_stripe_client(&config).map(Arc::new).log_err();
329 let this = Self {
330 db: db.clone(),
331 llm_db,
332 livekit_client,
333 blob_store_client: build_blob_store_client(&config).await.log_err(),
334 stripe_billing: stripe_client
335 .clone()
336 .map(|stripe_client| Arc::new(StripeBilling::new(stripe_client))),
337 stripe_client,
338 rate_limiter: Arc::new(RateLimiter::new(db)),
339 executor,
340 clickhouse_client: config
341 .clickhouse_url
342 .as_ref()
343 .and_then(|_| build_clickhouse_client(&config).log_err()),
344 kinesis_client: if config.kinesis_access_key.is_some() {
345 build_kinesis_client(&config).await.log_err()
346 } else {
347 None
348 },
349 config,
350 };
351 Ok(Arc::new(this))
352 }
353}
354
355fn build_stripe_client(config: &Config) -> anyhow::Result<stripe::Client> {
356 let api_key = config
357 .stripe_api_key
358 .as_ref()
359 .ok_or_else(|| anyhow!("missing stripe_api_key"))?;
360 Ok(stripe::Client::new(api_key))
361}
362
363async fn build_blob_store_client(config: &Config) -> anyhow::Result<aws_sdk_s3::Client> {
364 let keys = aws_sdk_s3::config::Credentials::new(
365 config
366 .blob_store_access_key
367 .clone()
368 .ok_or_else(|| anyhow!("missing blob_store_access_key"))?,
369 config
370 .blob_store_secret_key
371 .clone()
372 .ok_or_else(|| anyhow!("missing blob_store_secret_key"))?,
373 None,
374 None,
375 "env",
376 );
377
378 let s3_config = aws_config::defaults(BehaviorVersion::latest())
379 .endpoint_url(
380 config
381 .blob_store_url
382 .as_ref()
383 .ok_or_else(|| anyhow!("missing blob_store_url"))?,
384 )
385 .region(Region::new(
386 config
387 .blob_store_region
388 .clone()
389 .ok_or_else(|| anyhow!("missing blob_store_region"))?,
390 ))
391 .credentials_provider(keys)
392 .load()
393 .await;
394
395 Ok(aws_sdk_s3::Client::new(&s3_config))
396}
397
398async fn build_kinesis_client(config: &Config) -> anyhow::Result<aws_sdk_kinesis::Client> {
399 let keys = aws_sdk_s3::config::Credentials::new(
400 config
401 .kinesis_access_key
402 .clone()
403 .ok_or_else(|| anyhow!("missing kinesis_access_key"))?,
404 config
405 .kinesis_secret_key
406 .clone()
407 .ok_or_else(|| anyhow!("missing kinesis_secret_key"))?,
408 None,
409 None,
410 "env",
411 );
412
413 let kinesis_config = aws_config::defaults(BehaviorVersion::latest())
414 .region(Region::new(
415 config
416 .kinesis_region
417 .clone()
418 .ok_or_else(|| anyhow!("missing blob_store_region"))?,
419 ))
420 .credentials_provider(keys)
421 .load()
422 .await;
423
424 Ok(aws_sdk_kinesis::Client::new(&kinesis_config))
425}
426
427fn build_clickhouse_client(config: &Config) -> anyhow::Result<::clickhouse::Client> {
428 Ok(::clickhouse::Client::default()
429 .with_url(
430 config
431 .clickhouse_url
432 .as_ref()
433 .ok_or_else(|| anyhow!("missing clickhouse_url"))?,
434 )
435 .with_user(
436 config
437 .clickhouse_user
438 .as_ref()
439 .ok_or_else(|| anyhow!("missing clickhouse_user"))?,
440 )
441 .with_password(
442 config
443 .clickhouse_password
444 .as_ref()
445 .ok_or_else(|| anyhow!("missing clickhouse_password"))?,
446 )
447 .with_database(
448 config
449 .clickhouse_database
450 .as_ref()
451 .ok_or_else(|| anyhow!("missing clickhouse_database"))?,
452 ))
453}