Source

config/env.js

import { config as dotenvConfig } from "dotenv";
import { z } from "zod";
// Load environment variables from .env file, if available
dotenvConfig({ quiet: true });
/**
 * Environment variables schema and validation.
 * @category Config
 */
const envSchema = z.object({
    /**
     * HTTP server port
     * @category Config
     */
    PORT: z.coerce.number().default(3000),
    /**
     * Socket IO server port
     * @category Config
     */
    SOCKET_PORT: z.coerce.number().default(3001),
    /**
     * Node.js environment (development, production, test)
     * @category Config
     */
    NODE_ENV: z.enum(["development", "production", "test"]).default("development"),
    /**
     * MongoDB connection URI - must be provided
     * @category Config
     */
    MONGO_URI: z.string().url(),
    /**
     * Redis connection URI - must be provided
     * @category Config
     */
    REDIS_URI: z.string().url(),
    /**
     * Redis database index
     * @category Config
     */
    REDIS_DB_INDEX: z.coerce.number().int().min(0).default(0),
    /**
     * Authentication gRPC service hostname
     * @category Config
     */
    AUTH_GRPC_HOST: z.string().default("localhost"),
    /**
     * Authentication gRPC service port
     * @category Config
     */
    AUTH_GRPC_PORT: z.coerce.number().default(50051),
    /**
     * OpenAI Key
     * @category Config
     */
    OPENAI_API_KEY: z.coerce.string(),
    /**
     * TogetherAI APR key (this is for future)
     * @category Config
     */
    TOGETHER_API_KEY: z.coerce.string().default(""),
    /**
     * Replicate Key
     * @category Config
     */
    REPLICATE_API_TOKEN: z.coerce.string(),
    /**
     * OpenAI LLM model
     * @category Config
     */
    OPENAI_MODEL: z.coerce.string().default("gpt-4o"),
    /**
     * OpenAI LLM model temperature
     * @category Config
     */
    OPENAI_MODEL_TEMPERATURE: z.coerce.number().default(0.7),
    /**
     * Assistant greeting message to the user
     * @category Config
     */
    ASSISTANT_GREETING_MESSAGE: z
        .string()
        .default("Hi! My name is Galileo. I am here to guide you in shaping fascinating investigations that align with your curriculum. Please tell me what subject or area of study you would like to begin exploring."),
    /**
     * Cache Configuration
     * @category Config
     */
    CACHE_DURATION_DEFAULT: z
        .string()
        .transform((val) => parseInt(val, 10))
        .pipe(z.number().min(1000))
        .default("60000"), // 1 minute
    /**
     * Cache duration short
     * @category Config
     */
    CACHE_DURATION_SHORT: z
        .string()
        .transform((val) => parseInt(val, 10))
        .pipe(z.number().min(1000))
        .default("30000"), // 30 seconds
    /**
     * Cache duration long
     * @category Config
     */
    CACHE_DURATION_LONG: z
        .string()
        .transform((val) => parseInt(val, 10))
        .pipe(z.number().min(1000))
        .default("300000"), // 5 minutes
    /**
     * gRPC Configuration
     * @category Config
     */
    GRPC_KEEPALIVE_TIME_MS: z
        .string()
        .transform((val) => parseInt(val, 10))
        .pipe(z.number().min(1000))
        .default("30000"), // 30 seconds
    /**
     * gRPC HTTP2 ping interval
     * @category Config
     */
    GRPC_HTTP2_PING_INTERVAL_MS: z
        .string()
        .transform((val) => parseInt(val, 10))
        .pipe(z.number().min(1000))
        .default("300000"), // 5 minutes
    JWT_SECRET: z.string().default("secret"),
    /**
     * HashiCorp Vault Configuration
     * @category Config
     */
    VAULT_URL: z.string().url().default("http://127.0.0.1:8200"),
    /**
     * HashiCorp Vault authentication token
     * @category Config
     */
    VAULT_TOKEN: z.string().optional(),
    /**
     * HashiCorp Vault role ID for AppRole auth
     * @category Config
     */
    VAULT_ROLE_ID: z.string(),
    /**
     * HashiCorp Vault secret ID for AppRole auth
     * @category Config
     */
    VAULT_SECRET_ID: z.string(),
    /**
     * HashiCorp Vault secrets engine mount path
     * @category Config
     */
    VAULT_MOUNT_PATH: z.string().default("secret"),
    /**
     * HashiCorp Vault KV key
     * @category Config
     */
    VAULT_KV_KEY: z.string().default("K12"),
    /**
     * HashiCorp Vault API version
     * @category Config
     */
    VAULT_API_VERSION: z.enum(["v1", "v2"]).default("v1"),
});
/**
 * Parsed and validated environment variables.
 * @category Config
 */
export const env = envSchema.parse(process.env);