Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tabby): Implement demo mode #1865

Merged
merged 5 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ee/tabby-webserver/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ enum LicenseType {
type OAuthCredential {
provider: OAuthProvider!
clientId: String!
clientSecret: String
createdAt: DateTimeUtc!
updatedAt: DateTimeUtc!
}
Expand Down
3 changes: 3 additions & 0 deletions ee/tabby-webserver/src/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn demo_mode() -> bool {
std::env::var("TABBY_WEBSERVER_DEMO_MODE").is_ok()
}
1 change: 1 addition & 0 deletions ee/tabby-webserver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! Using the web interface (e.g chat playground) requires using this module with the `--webserver` flag on the command line.
mod axum;
mod cron;
mod env;
mod handler;
mod hub;
mod integrations;
Expand Down
5 changes: 5 additions & 0 deletions ee/tabby-webserver/src/service/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use tracing::warn;

use super::{dao::DbEnum, graphql_pagination_to_filter, AsID, AsRowid};
use crate::{
env::demo_mode,
oauth,
schema::{
auth::{
Expand Down Expand Up @@ -157,6 +158,10 @@ impl AuthenticationService for AuthenticationServiceImpl {
old_password: Option<&str>,
new_password: &str,
) -> Result<()> {
if demo_mode() {
return Err(anyhow!("Demo mode is enabled, cannot change passwords").into());
}

let user = self
.db
.get_user(id.as_rowid()?)
Expand Down
29 changes: 26 additions & 3 deletions ee/tabby-webserver/src/service/license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ use lazy_static::lazy_static;
use serde::Deserialize;
use tabby_db::DbConn;

use crate::schema::{
license::{LicenseInfo, LicenseService, LicenseStatus, LicenseType},
Result,
use crate::{
env::demo_mode,
schema::{
license::{LicenseInfo, LicenseService, LicenseStatus, LicenseType},
Result,
},
};

lazy_static! {
Expand Down Expand Up @@ -82,6 +85,18 @@ impl LicenseServiceImpl {
}
.guard_seat_limit())
}

async fn make_demo_license(&self) -> Result<LicenseInfo> {
let seats_used = self.db.count_active_users().await? as i32;
Ok(LicenseInfo {
r#type: LicenseType::Enterprise,
status: LicenseStatus::Ok,
seats: 100,
seats_used,
issued_at: None,
expires_at: None,
})
}
}

pub async fn new_license_service(db: DbConn) -> Result<impl LicenseService> {
Expand Down Expand Up @@ -115,6 +130,10 @@ fn license_info_from_raw(raw: LicenseJWTPayload, seats_used: usize) -> Result<Li
#[async_trait]
impl LicenseService for LicenseServiceImpl {
async fn read_license(&self) -> Result<LicenseInfo> {
if demo_mode() {
return self.make_demo_license().await;
}

let Some(license) = self.db.read_enterprise_license().await? else {
return self.make_community_license().await;
};
Expand All @@ -127,6 +146,10 @@ impl LicenseService for LicenseServiceImpl {
}

async fn update_license(&self, license: String) -> Result<()> {
if demo_mode() {
return Err(anyhow!("Demo mode is enabled, cannot set license").into());
}

let raw = validate_license(&license).map_err(|_e| anyhow!("License is not valid"))?;
let seats = self.db.count_active_users().await?;
match license_info_from_raw(raw, seats)?.status {
Expand Down
31 changes: 20 additions & 11 deletions ee/tabby-webserver/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,20 @@ use self::{
github_repository_provider::new_github_repository_provider_service,
license::new_license_service,
};
use crate::schema::{
analytic::AnalyticService,
auth::AuthenticationService,
email::EmailService,
github_repository_provider::GithubRepositoryProviderService,
job::JobService,
license::{IsLicenseValid, LicenseService},
repository::RepositoryService,
setting::SettingService,
worker::{RegisterWorkerError, Worker, WorkerKind, WorkerService},
CoreError, Result, ServiceLocator,
use crate::{
env::demo_mode,
schema::{
analytic::AnalyticService,
auth::AuthenticationService,
email::EmailService,
github_repository_provider::GithubRepositoryProviderService,
job::JobService,
license::{IsLicenseValid, LicenseService},
repository::RepositoryService,
setting::SettingService,
worker::{RegisterWorkerError, Worker, WorkerKind, WorkerService},
CoreError, Result, ServiceLocator,
},
};

struct ServerContext {
Expand Down Expand Up @@ -104,8 +107,14 @@ impl ServerContext {
}
}

/// Returns whether a request is authorized to access the content, and the user ID if authentication was used.
async fn authorize_request(&self, request: &Request<Body>) -> (bool, Option<ID>) {
let path = request.uri().path();
if demo_mode()
&& (path.starts_with("/v1/completions") || path.starts_with("/v1beta/chat/completions"))
wsxiaoys marked this conversation as resolved.
Show resolved Hide resolved
{
return (false, None);
}
if !(path.starts_with("/v1/") || path.starts_with("/v1beta/")) {
return (true, None);
}
Expand Down
Loading