Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

1.1 KB
Raw Blame
import { type IFeatureFlags } from '../schema';
import { FeatureFlagsSchema } from '../schema';

/**
 * Parses the feature flag string from environment variables.
 * @param flagString The feature flag string read from environment variables.
 * @returns The parsed feature flags object.
 */
export function parseFeatureFlag(flagString?: string): Partial<IFeatureFlags> {
  const flags: Partial<IFeatureFlags> = {};

  if (!flagString) return flags;

  // Replace Chinese commas with English commas and split string by comma
  const flagArray = flagString.trim().replaceAll(',', ',').split(',');

  for (let flag of flagArray) {
    flag = flag.trim();
    if (flag.startsWith('+') || flag.startsWith('-')) {
      const operation = flag[0];
      const key = flag.slice(1);

      const featureKey = key as keyof IFeatureFlags;
      const value = operation === '+';

      // Array-only flags (e.g. dev_dock_workspaces) reject the boolean and stay untouched.
      if (FeatureFlagsSchema.shape[featureKey]?.safeParse(value).success) {
        (flags as Record<string, boolean>)[featureKey] = value;
      }
    }
  }

  return flags;
}