buildCodexProfileEnv function

ProfileEnv? buildCodexProfileEnv({
  1. String? model,
  2. String? baseUrl,
  3. String? apiKey,
  4. Map<String, String>? processEnv,
  5. CodexCredentialResolver? resolveCodexApiCredentials,
})

Build a Codex profile environment. Returns null if credentials unavailable.

Implementation

ProfileEnv? buildCodexProfileEnv({
  String? model,
  String? baseUrl,
  String? apiKey,
  Map<String, String>? processEnv,
  CodexCredentialResolver? resolveCodexApiCredentials,
}) {
  final env = processEnv ?? const {};
  final key = sanitizeApiKey(apiKey ?? env['CODEX_API_KEY']);

  final credentials = resolveCodexApiCredentials != null
      ? resolveCodexApiCredentials(codexApiKey: key, processEnv: env)
      : null;

  if (credentials == null ||
      credentials.apiKey == null ||
      credentials.accountId == null) {
    return null;
  }

  return ProfileEnv(
    openaiBaseUrl: _firstNonEmpty([baseUrl]) ?? _defaultCodexBaseUrl,
    openaiModel: _firstNonEmpty([model]) ?? 'codexplan',
    codexApiKey: key,
    chatgptAccountId: credentials.accountId,
  );
}