Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

6.0 KB
Raw Blame
export enum BehaveAs {
  CLOUD = "CLOUD",
  LOCAL = "LOCAL",
}

function getBehaveAs(): BehaveAs {
  return process.env.NEXT_PUBLIC_BEHAVE_AS === "CLOUD"
    ? BehaveAs.CLOUD
    : BehaveAs.LOCAL;
}

export enum AppEnv {
  LOCAL = "local",
  DEV = "dev",
  PROD = "prod",
}

function getAppEnv(): AppEnv {
  const env = process.env.NEXT_PUBLIC_APP_ENV;
  if (env === "dev") return AppEnv.DEV;
  if (env === "prod") return AppEnv.PROD;
  // Some places use prod and others production
  if (env === "production") return AppEnv.PROD;
  return AppEnv.LOCAL;
}

function getAGPTServerApiUrl() {
  if (environment.isServerSide() && process.env.AGPT_SERVER_URL) {
    return process.env.AGPT_SERVER_URL;
  }

  const url =
    process.env.NEXT_PUBLIC_AGPT_SERVER_URL || "http://localhost:8006/api";
  return resolveBrowserURL(url);
}

function getAGPTServerBaseUrl() {
  // Only strip a trailing "/api"; a plain replace("/api", "") would hit the
  // first "/api" anywhere, e.g. the host in https://api.example.com/api.
  return getAGPTServerApiUrl().replace(/\/api\/?$/, "");
}

function getAGPTWsServerUrl() {
  if (environment.isServerSide() && process.env.AGPT_WS_SERVER_URL) {
    return process.env.AGPT_WS_SERVER_URL;
  }

  const configuredURL =
    process.env.NEXT_PUBLIC_AGPT_WS_SERVER_URL || "ws://localhost:8001/ws";
  if (environment.isServerSide()) return configuredURL;

  const url = new URL(configuredURL, window.location.origin);
  if (url.protocol === "http:") url.protocol = "ws:";
  if (url.protocol === "https:") url.protocol = "wss:";
  return url.toString();
}

function resolveBrowserURL(url: string) {
  if (environment.isServerSide()) return url;
  return new URL(url, window.location.origin).toString();
}

function getEnvironmentStr() {
  // Vercel injects one NEXT_PUBLIC_APP_ENV into preview and production builds
  // alike, so without this previews report as the production environment.
  const appEnv = isVercelPreview() ? "preview" : getAppEnv().toLowerCase();
  return `app:${appEnv}-behave:${getBehaveAs().toLowerCase()}`;
}

function getSentryEnvironment() {
  const override = process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT?.trim();
  return override || getEnvironmentStr();
}

function getRevision() {
  return process.env.NEXT_PUBLIC_REVISION?.trim() || "";
}

function getPostHogCredentials() {
  return {
    key: process.env.NEXT_PUBLIC_POSTHOG_KEY,
    host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
  };
}

function getLaunchDarklyClientId() {
  return process.env.NEXT_PUBLIC_LAUNCHDARKLY_CLIENT_ID;
}

function getGAMeasurementID() {
  return process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID?.trim() || "";
}

function getGoogleAdsID() {
  return process.env.NEXT_PUBLIC_GOOGLE_ADS_ID || "";
}

function getGoogleAdsConversionLabels() {
  return process.env.NEXT_PUBLIC_GOOGLE_ADS_CONVERSION_LABELS || "";
}

function getCookiebotCBID() {
  return process.env.NEXT_PUBLIC_COOKIEBOT_CBID?.trim() || "";
}

function getCookiebotGeoRegions() {
  return process.env.NEXT_PUBLIC_COOKIEBOT_GEOREGIONS?.trim() || "";
}

function isProductionBuild() {
  return process.env.NODE_ENV === "production";
}

function isDevelopmentBuild() {
  return process.env.NODE_ENV === "development";
}

function isDev() {
  return isCloud() && getAppEnv() === AppEnv.DEV;
}

function isProd() {
  return isCloud() && getAppEnv() === AppEnv.PROD;
}

function isCloud() {
  return getBehaveAs() === BehaveAs.CLOUD;
}

function isLocal() {
  return getBehaveAs() === BehaveAs.LOCAL;
}

function isServerSide() {
  return typeof window === "undefined";
}

function isClientSide() {
  return typeof window !== "undefined";
}

function isVercelPreview() {
  // VERCEL_ENV and VERCEL_TARGET_ENV are server-only; next.config.mjs mirrors
  // them into NEXT_PUBLIC_ copies so the browser bundle can read them too.
  // VERCEL_ENV is "preview" for every custom environment (the dev deployment's
  // "staging") as well as for branch previews, while VERCEL_TARGET_ENV names
  // the environment itself. Builds that don't carry it fall back to VERCEL_ENV.
  const vercelEnv =
    process.env.NEXT_PUBLIC_VERCEL_ENV || process.env.VERCEL_ENV;
  const targetEnv =
    process.env.NEXT_PUBLIC_VERCEL_TARGET_ENV || process.env.VERCEL_TARGET_ENV;
  return vercelEnv === "preview" && (!targetEnv || targetEnv === "preview");
}

function isVercel() {
  return Boolean(process.env.NEXT_PUBLIC_VERCEL_ENV || process.env.VERCEL);
}

// AutoGPT's own Sentry project. Only our hosted deployments (on Vercel) report
// to it; a self-hosted build reports nowhere unless its operator sets their own
// NEXT_PUBLIC_SENTRY_DSN, so no self-hoster sends us errors without opting in.
export const HOSTED_SENTRY_DSN =
  "https://fe4e4aa4a283391808a5da396da20159@o4505260022104064.ingest.us.sentry.io/4507946746380288";

function getSentryDSN() {
  if (process.env.DISABLE_SENTRY === "true") return undefined;
  const ownDSN = process.env.NEXT_PUBLIC_SENTRY_DSN;
  if (ownDSN) return ownDSN;
  return isVercel() && (isProd() || isDev()) ? HOSTED_SENTRY_DSN : undefined;
}

function isSentryEnabled() {
  return Boolean(getSentryDSN());
}

function areFeatureFlagsEnabled() {
  return (
    process.env.NEXT_PUBLIC_LAUNCHDARKLY_ENABLED === "true" &&
    Boolean(process.env.NEXT_PUBLIC_LAUNCHDARKLY_CLIENT_ID)
  );
}

function isPostHogEnabled() {
  const inCloud = isCloud();
  const key = process.env.NEXT_PUBLIC_POSTHOG_KEY;
  const host = process.env.NEXT_PUBLIC_POSTHOG_HOST;
  return inCloud && key && host;
}

export const environment = {
  // Generic
  getEnvironmentStr,
  // Get environment variables config
  getBehaveAs,
  getAppEnv,
  getAGPTServerApiUrl,
  getAGPTServerBaseUrl,
  getAGPTWsServerUrl,
  getRevision,
  getPostHogCredentials,
  getLaunchDarklyClientId,
  getGAMeasurementID,
  getGoogleAdsID,
  getGoogleAdsConversionLabels,
  getCookiebotCBID,
  getCookiebotGeoRegions,
  getSentryDSN,
  getSentryEnvironment,
  // Assertions
  isServerSide,
  isClientSide,
  isProductionBuild,
  isDevelopmentBuild,
  isDev,
  isProd,
  isCloud,
  isLocal,
  isVercel,
  isVercelPreview,
  isSentryEnabled,
  isPostHogEnabled,
  areFeatureFlagsEnabled,
};