Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
6.0 KB
Raw Blame
import { LIMITS } from '@clerum/llm-provider-attempt-contract'
import { DEFAULT_MAX_BODY_BYTES, STREAM_LIMITS } from './requestLimits.js'
import { DEFAULT_HEARTBEAT_INTERVAL_MS, MAX_HEARTBEAT_INTERVAL_MS } from './sseHeartbeat.js'

export type CodexLlmProxyConfig = {
  runtimePort: number
  adminPort: number
  probePort: number
  maxBodyBytes: number
  maxVisualBodyBytes: number
  maxStreamDurationMs: number
  maxDeadlineMs: number
  /** Lowers STREAM_LIMITS.upstreamIdleTimeoutMs; the transport never raises it. */
  upstreamIdleTimeoutMs: number
  /** Interval between SSE keepalive comments once the redeem succeeded. */
  heartbeatIntervalMs: number
  jwtIssuer: string
  jwtPublicKey: string
  executionEnabled: boolean
  controlApiBaseUrl: string
  controlApiServiceName: string
  controlApiServiceToken: string
}

function requiredPositiveInt(name: string, raw: string | undefined, fallback?: number): number {
  const value = raw === undefined || raw === '' ? fallback : Number(raw)
  if (value === undefined || !Number.isFinite(value) || !Number.isInteger(value) || value <= 0) {
    throw new Error(`${name} must be a finite integer greater than zero`)
  }
  if (value === Number.MAX_SAFE_INTEGER) {
    throw new Error(`${name} must be a bounded positive integer`)
  }
  return value
}

function atMost(name: string, value: number, max: number): number {
  if (value > max) throw new Error(`${name} must be at most ${max}`)
  return value
}

function requiredPem(name: string, raw: string | undefined): string {
  const value = raw?.trim() ?? ''
  if (!value.includes('BEGIN') || !value.includes('KEY')) {
    throw new Error(`${name} must be a PEM-encoded public key`)
  }
  return value
}

// Fail at startup instead of at the first redeem. Messages name the variable
// only; the raw value (URL or service token) is never echoed.
function requiredHttpUrl(name: string, raw: string | undefined): string {
  const value = raw?.trim() ?? ''
  if (!value) throw new Error(`${name} must be set`)
  let parsed: URL
  try {
    parsed = new URL(value)
  } catch {
    throw new Error(`${name} must be an absolute http(s) URL`)
  }
  if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
    throw new Error(`${name} must be an absolute http(s) URL`)
  }
  return value
}

function requiredNonEmpty(name: string, raw: string | undefined): string {
  const value = raw?.trim() ?? ''
  if (!value) throw new Error(`${name} must be set`)
  return value
}

export function loadConfig(env: NodeJS.ProcessEnv = process.env): CodexLlmProxyConfig {
  const maxBodyBytes = requiredPositiveInt(
    'CODEX_LLM_PROXY_MAX_BODY_BYTES',
    env.CODEX_LLM_PROXY_MAX_BODY_BYTES,
    DEFAULT_MAX_BODY_BYTES
  )
  // R9-3: a lower limit would answer 413 to requests the contract accepts.
  if (maxBodyBytes < DEFAULT_MAX_BODY_BYTES) {
    throw new Error(
      'CODEX_LLM_PROXY_MAX_BODY_BYTES must be at least the contract request cap plus the envelope allowance'
    )
  }
  const maxVisualBodyBytes = requiredPositiveInt(
    'CODEX_LLM_PROXY_MAX_VISUAL_BODY_BYTES',
    env.CODEX_LLM_PROXY_MAX_VISUAL_BODY_BYTES,
    LIMITS.maxVisualRequestBodyBytes
  )
  if (maxVisualBodyBytes < LIMITS.maxVisualRequestBodyBytes) {
    throw new Error(
      `CODEX_LLM_PROXY_MAX_VISUAL_BODY_BYTES must be at least ${LIMITS.maxVisualRequestBodyBytes}, the contract maxVisualRequestBodyBytes`
    )
  }
  // The 2048Mi memory limit was measured with two visual streams at the contract
  // ceiling only (see IN_FLIGHT_BODY_BUDGET_BODIES). A larger value would admit
  // bodies that measurement never covered.
  if (maxVisualBodyBytes > LIMITS.maxVisualRequestBodyBytes) {
    throw new Error(
      `CODEX_LLM_PROXY_MAX_VISUAL_BODY_BYTES must be at most ${LIMITS.maxVisualRequestBodyBytes}, the contract maxVisualRequestBodyBytes`
    )
  }
  // An equal or lower budget would collapse visual admission into the
  // ordinary parser's cap, so the visual gate's separate accounting would
  // never engage.
  if (maxVisualBodyBytes <= maxBodyBytes) {
    throw new Error(
      'CODEX_LLM_PROXY_MAX_VISUAL_BODY_BYTES must be greater than CODEX_LLM_PROXY_MAX_BODY_BYTES'
    )
  }
  return {
    runtimePort: requiredPositiveInt(
      'CODEX_LLM_PROXY_RUNTIME_PORT',
      env.CODEX_LLM_PROXY_RUNTIME_PORT,
      8080
    ),
    adminPort: requiredPositiveInt(
      'CODEX_LLM_PROXY_ADMIN_PORT',
      env.CODEX_LLM_PROXY_ADMIN_PORT,
      8081
    ),
    probePort: requiredPositiveInt(
      'CODEX_LLM_PROXY_PROBE_PORT',
      env.CODEX_LLM_PROXY_PROBE_PORT,
      9090
    ),
    maxBodyBytes,
    maxVisualBodyBytes,
    maxStreamDurationMs: requiredPositiveInt(
      'CODEX_LLM_PROXY_MAX_STREAM_DURATION_MS',
      env.CODEX_LLM_PROXY_MAX_STREAM_DURATION_MS,
      1_800_000
    ),
    maxDeadlineMs: requiredPositiveInt(
      'CODEX_LLM_PROXY_MAX_DEADLINE_MS',
      env.CODEX_LLM_PROXY_MAX_DEADLINE_MS,
      1_800_000
    ),
    upstreamIdleTimeoutMs: requiredPositiveInt(
      'CODEX_LLM_PROXY_UPSTREAM_IDLE_TIMEOUT_MS',
      env.CODEX_LLM_PROXY_UPSTREAM_IDLE_TIMEOUT_MS,
      STREAM_LIMITS.upstreamIdleTimeoutMs
    ),
    // Refused rather than lowered: the operator's value is either applied or
    // the proxy does not start.
    heartbeatIntervalMs: atMost(
      'CODEX_LLM_PROXY_HEARTBEAT_INTERVAL_MS',
      requiredPositiveInt(
        'CODEX_LLM_PROXY_HEARTBEAT_INTERVAL_MS',
        env.CODEX_LLM_PROXY_HEARTBEAT_INTERVAL_MS,
        DEFAULT_HEARTBEAT_INTERVAL_MS
      ),
      MAX_HEARTBEAT_INTERVAL_MS
    ),
    jwtIssuer: env.CODEX_LLM_PROXY_JWT_ISSUER?.trim() || 'control-api',
    jwtPublicKey: requiredPem('CODEX_LLM_PROXY_JWT_PUBLIC_KEY', env.CODEX_LLM_PROXY_JWT_PUBLIC_KEY),
    executionEnabled: env.CODEX_LLM_PROXY_EXECUTION_ENABLED === 'true',
    controlApiBaseUrl: requiredHttpUrl(
      'CODEX_LLM_PROXY_CONTROL_API_URL',
      env.CODEX_LLM_PROXY_CONTROL_API_URL
    ),
    controlApiServiceName: env.CODEX_LLM_PROXY_CONTROL_API_SERVICE?.trim() || 'codex-llm-proxy',
    controlApiServiceToken: requiredNonEmpty('CODEX_LLM_PROXY_CONTROL_API_TOKEN', env.CODEX_LLM_PROXY_CONTROL_API_TOKEN),
  }
}