All files / shared/config/src environment.ts

0% Statements 0/41
0% Branches 0/36
0% Functions 0/6
0% Lines 0/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
/**
 * 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;
  
  Iif (envMode && Object.values(TESTING_MODES).includes(envMode as TestingMode)) {
    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
  Iif (config.endpointRestrictions.blockedEndpoints.includes(endpointKey)) {
    return false;
  }
  
  // Check if endpoint is explicitly allowed
  Iif (config.endpointRestrictions.allowedEndpoints.includes(endpointKey)) {
    return true;
  }
  
  // In production, all endpoints are allowed for authenticated customers
  Iif (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)
  };
}