UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

86 lines (85 loc) • 3.62 kB
import { r as filterStringEntries } from "./string-normalization-DLmfYZXI.mjs"; import { l as sanitizeHostExecEnv } from "./host-env-security-BrTYS4pH.mjs"; import { r as listKnownProviderAuthEnvVarNamesCore } from "./provider-env-vars-DPvuIf4v.mjs"; //#region src/agents/cli-runner/execute-logging.ts const CLAUDE_SELECTED_AUTH_ENV_KEYS = /* @__PURE__ */ new Set(["ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN"]); const NODE_CLAUDE_FORWARD_ENV_KEYS = /* @__PURE__ */ new Set(["CLAUDE_CODE_AUTO_COMPACT_WINDOW", "CLAUDE_CODE_DISABLE_1M_CONTEXT"]); function resolveNodeClaudeAuthEnv(e) { const t = e.preparedBackend.secretInput; if (!t) return {}; const n = e.preparedBackend.env ?? {}; const r = Object.hasOwn(n, "CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR") ? "CLAUDE_CODE_OAUTH_TOKEN" : Object.hasOwn(n, "CLAUDE_CODE_API_KEY_FILE_DESCRIPTOR") ? "ANTHROPIC_API_KEY" : void 0; if (!r) return {}; const i = t.createData(); try { return { [r]: i.toString("utf8") }; } finally { i.fill(0); } } const CLI_BACKEND_PRESERVE_ENV = "OPENCLAW_LIVE_CLI_BACKEND_PRESERVE_ENV"; function parseCliBackendPreserveEnv(e) { const t = e?.trim(); if (!t) return /* @__PURE__ */ new Set(); if (t.startsWith("[")) try { return new Set(filterStringEntries(JSON.parse(t))); } catch { return /* @__PURE__ */ new Set(); } return new Set(t.split(/[,\s]+/).filter(Boolean)); } //#endregion //#region src/agents/cli-runner/execution-env.ts const CLI_RUNTIME_ENV_KEYS = /* @__PURE__ */ new Set([ ...NODE_CLAUDE_FORWARD_ENV_KEYS, "CLAUDE_CODE_DISABLE_ADAPTIVE_THINKING", "MAX_THINKING_TOKENS" ]); /** Apply native-login policy after all overlays, using this execution host's inventory. */ function stripCliNativeAuthEnv(e, t, n = []) { const r = new Set([ ...Array.from(n).filter((e) => !CLI_RUNTIME_ENV_KEYS.has(e.toUpperCase())), ...listKnownProviderAuthEnvVarNamesCore(t), ...CLAUDE_SELECTED_AUTH_ENV_KEYS, "CLAUDE_CODE_API_KEY_FILE_DESCRIPTOR", "CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR", "CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST" ].map((e) => e.toUpperCase())); for (const t of Object.keys(e)) if (r.has(t.toUpperCase())) delete e[t]; } /** * The environment a local CLI child starts from: the Gateway process, minus the backend's * clearEnv, plus backend and prepared overrides. Preparation and execution both call this, * so what a history owner is derived from is the environment the child actually receives. * Per-attempt MCP capture and installation variables are overlaid before native auth is stripped. */ function resolveCliChildEnv(e, t = {}, n) { const { backend: r } = e; const i = [...r.clearEnv ?? [], ...e.resetEnv ?? []]; const a = e.env ?? {}; const o = !t.nativeAuthOnly && (Boolean(e.secretInput) || [...CLAUDE_SELECTED_AUTH_ENV_KEYS].some((e) => Object.hasOwn(a, e))) ? new Set(i) : void 0; const s = { ...Object.fromEntries(Object.entries(r.env ?? {}).filter(([e]) => !o?.has(e))), ...a }; const c = sanitizeHostExecEnv({ baseEnv: process.env, blockPathOverrides: true }); const l = parseCliBackendPreserveEnv(process.env[CLI_BACKEND_PRESERVE_ENV]); for (const e of i) if (!l.has(e) || o?.has(e)) delete c[e]; if (Object.keys(s).length > 0) Object.assign(c, sanitizeHostExecEnv({ baseEnv: {}, overrides: s, blockPathOverrides: true })); Object.assign(c, n); if (t.nativeAuthOnly) stripCliNativeAuthEnv(c, t, r.clearEnv); delete c.CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST; return { env: c, selectedClaudeClearEnv: o }; } //#endregion export { resolveNodeClaudeAuthEnv as i, stripCliNativeAuthEnv as n, NODE_CLAUDE_FORWARD_ENV_KEYS as r, resolveCliChildEnv as t };