Skip to content

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

TypeScript
6.9 KB
Raw Blame
/**
 * LLM Provider factory.
 */
import { config } from '../config'
import { logger } from '../logger'
import { ApiKeys, ModelConfig } from '../types'
import { ClaudeProvider } from './claude'
import { CodexLlmProxyClient, resolveCodexProxyRuntimeUrl } from './codexLlmProxyClient'
import { readCodexPlatformJwt, refreshCodexPlatformJwt } from './codexPlatformJwt'
import { readLiveCodexPolicyBinding, resolveCodexAttemptPolicy } from './codexPolicyBinding'
import type { CodexAttemptContext } from './codexSubscription'
import { GrokLlmProxyClient, resolveGrokProxyRuntimeUrl } from './grokLlmProxyClient'
import { readLiveGrokPolicyBinding, resolveGrokAttemptPolicy } from './grokPolicyBinding'
import type { GrokAttemptContext } from './grokSubscription'
import { OpenAIProvider } from './openai'
import { ProviderAttemptAuthorizer, resolveCodexAuthorizeUrl } from './providerAttemptAuthorizer'
import type { ProviderEnvLookup } from './providerEnv'
import { makeProvider } from './registry'
import { ALL_PROVIDERS, descriptorFor, isLlmProvider } from './registryCore'
// Re-export the transport interfaces (moved to ./types to break the registry
// import cycle) so existing `import { SingleTurnProvider, ClassifiedError }
// from '../llm'` sites keep working unchanged.
import type { ClassifiedError, SingleTurnProvider } from './types'

export type { ClassifiedError, SingleTurnProvider } from './types'

const DEFAULT_CODEX_AUTHORIZE_GATEWAY =
  'http://nginx-workflow-approval-gateway.control-plane.svc.cluster.local:8092'

function createGrokRuntimeDeps(captured?: GrokAttemptContext) {
  const gateway = (config.mcpHostGatewayUrl ?? '').trim() || DEFAULT_CODEX_AUTHORIZE_GATEWAY
  return {
    authorizer: new ProviderAttemptAuthorizer({
      authorizeUrl: resolveCodexAuthorizeUrl(gateway),
      readPlatformJwt: readCodexPlatformJwt,
      refreshOnUnauthorized: refreshCodexPlatformJwt,
    }),
    proxy: new GrokLlmProxyClient({
      runtimeUrl: resolveGrokProxyRuntimeUrl(config.grokProxyRuntimeBaseUrl),
      readPlatformJwt: readCodexPlatformJwt,
      refreshOnUnauthorized: refreshCodexPlatformJwt,
    }),
    attemptContext: ({ model }: { model: string }): GrokAttemptContext => {
      if (captured) return captured
      const resolved = resolveGrokAttemptPolicy({
        model,
        envRevision: config.grokPolicyRevision,
        envHash: config.grokPolicyHash,
        binding: readLiveGrokPolicyBinding(),
      })
      if (!resolved) {
        return { policyRevision: 0, policyHash: '' }
      }
      return {
        ...resolved,
        hostRef: config.hostName,
      }
    },
  }
}

function createCodexRuntimeDeps(captured?: CodexAttemptContext) {
  const gateway = (config.mcpHostGatewayUrl ?? '').trim() || DEFAULT_CODEX_AUTHORIZE_GATEWAY
  return {
    authorizer: new ProviderAttemptAuthorizer({
      authorizeUrl: resolveCodexAuthorizeUrl(gateway),
      readPlatformJwt: readCodexPlatformJwt,
      refreshOnUnauthorized: refreshCodexPlatformJwt,
    }),
    proxy: new CodexLlmProxyClient({
      runtimeUrl: resolveCodexProxyRuntimeUrl(config.codexProxyRuntimeBaseUrl),
      readPlatformJwt: readCodexPlatformJwt,
      refreshOnUnauthorized: refreshCodexPlatformJwt,
    }),
    attemptContext: ({ model }: { model: string }): CodexAttemptContext => {
      if (captured) return captured
      const resolved = resolveCodexAttemptPolicy({
        model,
        envRevision: config.codexPolicyRevision,
        envHash: config.codexPolicyHash,
        binding: readLiveCodexPolicyBinding(),
      })
      if (!resolved) {
        return { policyRevision: 0, policyHash: '' }
      }
      return {
        ...resolved,
        hostRef: config.hostName,
      }
    },
  }
}

export type CreateLlmProviderOptions = {
  capturedCodexAttemptContext?: CodexAttemptContext
  capturedGrokAttemptContext?: GrokAttemptContext
  /**
   * Non-secret provider env source (see ./providerEnv). Omitted → process.env.
   * mcp-host's Host runtime passes the per-Host env from ConfigStore so values
   * set under Control UI Host → Environment reach the drivers.
   */
  env?: ProviderEnvLookup
}

/**
 * Create an LLM provider based on configuration.
 */
export function createLLMProvider(
  keys: ApiKeys,
  modelConfig?: ModelConfig,
  options?: CreateLlmProviderOptions
): SingleTurnProvider | null {
  const provider = modelConfig?.provider || 'openai'
  const modelName = modelConfig?.name

  if (!isLlmProvider(provider)) {
    logger.error({}, 'Unknown LLM provider')
    return null
  }

  // Fail-safe (§5.7): a missing/empty REQUIRED slot → console.error + return
  // null BEFORE calling make(). Deferring to make() would surface as an opaque
  // 401 later. Multi-slot (R4): every required slot must be present, so Bedrock
  // never constructs half-credentialed. The provider id IS the ApiKeys key.
  const credentials = keys[provider] ?? {}
  for (const slot of descriptorFor(provider).credentialSlots) {
    if (slot.required && !credentials[slot.dataKey]) {
      logger.error({}, 'Required LLM credential is missing')
      return null
    }
  }

  // The own-SDK arms (vertex/bedrock) can still throw at construction if their
  // non-secret pod env (Vertex project id, AWS region) is absent or the Vertex
  // service-account JSON is malformed. Treat that as the same fail-safe: log +
  // return null (→ degraded), never crash the process. The four original arms
  // never throw here (their empty-key case is already handled above), so their
  // behaviour is byte-identical.
  try {
    return makeProvider(provider, credentials, modelName, {
      env: options?.env,
      ...(provider === 'codex-subscription'
        ? { codex: createCodexRuntimeDeps(options?.capturedCodexAttemptContext) }
        : provider === 'grok-subscription'
          ? { grok: createGrokRuntimeDeps(options?.capturedGrokAttemptContext) }
          : {}),
    })
  } catch (err) {
    logger.error({}, 'Failed to construct LLM provider')
    return null
  }
}

/**
 * Build the `ApiKeys` bag from an env map (dev mode + Plugin Workload SDK env
 * mode). Registry-driven & multi-slot (R4): for each provider, read every
 * credential slot by its env var name; include the provider only when ALL its
 * required slots are present (e.g. Bedrock needs both AWS keys). `ALL_PROVIDERS`
 * order = dev auto-detection priority.
 */
export function apiKeysFromEnv(env: NodeJS.ProcessEnv = process.env): ApiKeys {
  const keys: ApiKeys = {}
  for (const p of ALL_PROVIDERS) {
    if (descriptorFor(p).authMode !== 'static-credentials') continue
    const slots = descriptorFor(p).credentialSlots
    const bag: Record<string, string> = {}
    for (const slot of slots) {
      const value = env[slot.envName]
      if (value) bag[slot.dataKey] = value
    }
    const hasAllRequired = slots.every(slot => !slot.required || bag[slot.dataKey])
    if (hasAllRequired && Object.keys(bag).length > 0) keys[p] = bag
  }
  return keys
}

export { OpenAIProvider, ClaudeProvider }