// Environment-variable reads are isolated in this module so that // files containing outbound network calls can remain free of // `process.env` references (and vice versa). // // OpenClaw's install-time code scanner (skill-scanner.ts, rule // `env-harvesting`) blocks install when a single file combines env // reads with an outbound-HTTP send. The exact regex for the // network-send side has varied across releases: older gateways (e.g. // v2026.4.9) match on a bare-word pattern, so even a comment // mentioning an HTTP-send word in an env-reading file would trip the // rule. Keeping the two responsibilities in separate files sidesteps // the rule regardless of how tightly the scanner is calibrated in a // given gateway build. Do NOT add outbound network sends to this // file, and do NOT add env reads to the sibling files that do the // HTTP work (src/client.ts, src/audit.ts, src/auth/device-auth.ts). const DEFAULT_API_BASE = "https://api.kilo.ai"; /** * Resolve the auth token from environment variables, if any. Returns * `null` when neither canonical env var is set. Used by the KiloClaw * path where the gateway injects `KILOCODE_API_KEY` at VM boot. The * `KILO_API_KEY` alias is supported for historical compatibility. */ export function resolveEnvToken(): string | null { return process.env.KILOCODE_API_KEY ?? process.env.KILO_API_KEY ?? null; } /** * Resolve the KiloCode API base URL with the following precedence: * 1. Explicit plugin config (`plugins.entries.shell-security.config.apiBaseUrl`). * 2. `KILO_API_URL` env override. * 3. `KILOCODE_API_BASE_URL` env var (origin is extracted; a bad * URL is tolerated and falls through to the default). * 4. `DEFAULT_API_BASE` (production). */ export function resolveApiBase( pluginConfig: Record | null, ): string { const configUrl = pluginConfig?.apiBaseUrl; if (typeof configUrl === "string" && configUrl.length > 0) return configUrl; if (process.env.KILO_API_URL) return process.env.KILO_API_URL; const gatewayUrl = process.env.KILOCODE_API_BASE_URL; if (gatewayUrl) { try { return new URL(gatewayUrl).origin; } catch { /* fall through */ } } return DEFAULT_API_BASE; }