/** * Environment configuration for Tavus API integration * * This module provides environment-aware configuration for managing * Tavus API integration across development, staging, and production environments. */ export type Environment = 'development' | 'staging' | 'production'; export type TestingMode = | 'full-testing-with-safe-real' | 'fake-only' | 'real-only' | 'safe-only' | 'production'; export interface EndpointFlags { free: boolean; // Does not consume credits safe: boolean; // No side effects, idempotent, non-destructive destructive: boolean; // Permanently deletes or changes data (only when safe=false) } export interface EndpointConfig { path: string; method: string; flags: EndpointFlags; strategy: EndpointStrategy; } export type EndpointStrategy = | 'fake-only' // Use only fake server | 'real-only' // Use only real server | 'both' // Test both servers | 'customer-only' // Only for real customer requests | 'fake-primary' // Fake server with real fallback | 'real-primary' // Real server with fake fallback | 'disabled'; // Endpoint disabled export interface EnvironmentConfig { environment: Environment; testingMode: TestingMode; fakeServerUrl: string; realServerUrl: string; enableOpenTelemetry: boolean; grafanaConfig?: { endpoint: string; apiToken: string; }; endpointRestrictions: { allowedEndpoints: string[]; blockedEndpoints: string[]; }; } /** * Get the current environment */ export function getEnvironment(): Environment { return (process.env.NODE_ENV || 'development') as Environment; } /** * Get the testing mode for the current environment */ export function getTestingMode(): TestingMode { const envMode = process.env.TESTING_MODE; if (envMode && Object.keys(TESTING_MODES).includes(envMode)) { return envMode as TestingMode; } // Default testing modes per environment switch (getEnvironment()) { case 'development': return 'full-testing-with-safe-real'; case 'staging': return 'full-testing-with-safe-real'; case 'production': return 'production'; default: return 'full-testing-with-safe-real'; } } /** * Get the complete environment configuration */ export function getEnvironmentConfig(): EnvironmentConfig { const environment = getEnvironment(); const testingMode = getTestingMode(); const baseConfig: EnvironmentConfig = { environment, testingMode, fakeServerUrl: process.env.FAKE_SERVER_URL || 'http://localhost:3001', realServerUrl: process.env.REAL_SERVER_URL || 'https://tavusapi.com', enableOpenTelemetry: process.env.ENABLE_OPENTELEMETRY === 'true', endpointRestrictions: { allowedEndpoints: [], blockedEndpoints: [] } }; // Environment-specific configurations switch (environment) { case 'development': return { ...baseConfig, grafanaConfig: { endpoint: process.env.GRAFANA_OTLP_ENDPOINT || 'https://otlp-gateway-prod-gb-south-1.grafana.net/otlp', apiToken: process.env.GRAFANA_OTLP_API_TOKEN || '' }, endpointRestrictions: { allowedEndpoints: [ 'GET /v2/health', 'GET /v2/status', 'GET /v2/personas', 'GET /v2/personas/{id}', 'GET /v2/conversations', 'GET /v2/conversations/{id}', 'GET /v2/config', 'GET /v2/analytics', 'GET /v2/usage' ], blockedEndpoints: [] } }; case 'staging': return { ...baseConfig, fakeServerUrl: process.env.FAKE_SERVER_URL || 'https://fake-server.staging.buzztrail.ai', grafanaConfig: { endpoint: process.env.GRAFANA_OTLP_ENDPOINT || 'https://otlp-gateway-prod-gb-south-1.grafana.net/otlp', apiToken: process.env.GRAFANA_OTLP_API_TOKEN || '' }, endpointRestrictions: { allowedEndpoints: [ 'GET /v2/health', 'GET /v2/status', 'GET /v2/personas', 'GET /v2/personas/{id}', 'GET /v2/conversations', 'GET /v2/conversations/{id}', 'GET /v2/config' ], blockedEndpoints: [ 'POST /v2/conversations', 'POST /v2/personas', 'PATCH /v2/personas/{id}', 'PATCH /v2/conversations/{id}', 'DELETE /v2/personas/{id}', 'DELETE /v2/conversations/{id}' ] } }; case 'production': return { ...baseConfig, testingMode: 'production', fakeServerUrl: process.env.FAKE_SERVER_URL || 'https://fake-server.production.buzztrail.ai', grafanaConfig: { endpoint: process.env.GRAFANA_OTLP_ENDPOINT || 'https://otlp-gateway-prod-gb-south-1.grafana.net/otlp', apiToken: process.env.GRAFANA_OTLP_API_TOKEN || '' }, endpointRestrictions: { allowedEndpoints: [ 'GET /v2/health', 'GET /v2/status', 'GET /v2/config' ], blockedEndpoints: [] } }; default: return baseConfig; } } /** * Testing mode configurations */ export const TESTING_MODES = { 'full-testing-with-safe-real': { description: 'Test all endpoints against fake server, safe/free against real server', fakeServer: 'all endpoints', realServer: 'safe and free endpoints only', useCase: 'Development and staging default' }, 'fake-only': { description: 'Use fake server for all endpoints', fakeServer: 'all endpoints', realServer: 'none', useCase: 'Pure development without real API calls' }, 'real-only': { description: 'Use real server for safe/free endpoints only', fakeServer: 'none', realServer: 'safe and free endpoints only', useCase: 'Testing real API behavior' }, 'safe-only': { description: 'Use real server for safe endpoints, block others', fakeServer: 'none', realServer: 'safe endpoints only', useCase: 'Staging environment with restrictions' }, 'production': { description: 'Production mode - all endpoints for authenticated customers', fakeServer: 'emergency fallback only', realServer: 'all endpoints for authenticated customers', useCase: 'Production environment' } } as const; /** * Check if an endpoint is allowed in the current environment */ export function isEndpointAllowed(endpoint: string, method: string): boolean { const config = getEnvironmentConfig(); const endpointKey = `${method} ${endpoint}`; // Check if endpoint is explicitly blocked if (config.endpointRestrictions.blockedEndpoints.includes(endpointKey)) { return false; } // Check if endpoint is explicitly allowed if (config.endpointRestrictions.allowedEndpoints.includes(endpointKey)) { return true; } // In production, all endpoints are allowed for authenticated customers if (config.environment === 'production') { return true; } // In development and staging, only safe/free endpoints are allowed return isSafeOrFreeEndpoint(endpoint, method); } /** * Check if an endpoint is safe or free */ export function isSafeOrFreeEndpoint(endpoint: string, method: string): boolean { const safeEndpoints = [ 'GET /v2/health', 'GET /v2/status', 'GET /v2/personas', 'GET /v2/personas/{id}', 'GET /v2/conversations', 'GET /v2/conversations/{id}', 'GET /v2/config', 'GET /v2/analytics', 'GET /v2/usage' ]; const endpointKey = `${method} ${endpoint}`; return safeEndpoints.includes(endpointKey); } /** * Get endpoint flags for classification */ export function getEndpointFlags(endpoint: string, method: string): EndpointFlags { const endpointKey = `${method} ${endpoint}`; // Free endpoints (no credit consumption) const freeEndpoints = [ 'GET /v2/health', 'GET /v2/status', 'GET /v2/personas', 'GET /v2/personas/{id}', 'GET /v2/conversations', 'GET /v2/conversations/{id}', 'GET /v2/config' ]; // Safe endpoints (no side effects) const safeEndpoints = [ ...freeEndpoints, 'GET /v2/analytics', 'GET /v2/usage' ]; // Destructive endpoints const destructiveEndpoints = [ 'PATCH /v2/personas/{id}', 'PATCH /v2/conversations/{id}', 'DELETE /v2/personas/{id}', 'DELETE /v2/conversations/{id}' ]; return { free: freeEndpoints.includes(endpointKey), safe: safeEndpoints.includes(endpointKey), destructive: destructiveEndpoints.includes(endpointKey) }; }