openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
214 lines (213 loc) • 8.57 kB
JavaScript
import { r as asNullableRecord } from "./record-coerce-CVPuDqcc.mjs";
import { n as safeParseJsonRecord } from "./json-coercion-CaJLaav-.mjs";
import { i as decodeNodeInvokeParams } from "./invoke-payload-Csjq7TlY.mjs";
import fs from "node:fs/promises";
//#region src/node-host/invoke-agent-cli-claude-params.ts
const MAX_ARG_COUNT = 128;
const MAX_ARG_BYTES = 1048576;
const MAX_REQUEST_BYTES = 2097152;
const MAX_TIMEOUT_MS = 864e5;
const MIN_IDLE_TIMEOUT_MS = 1e3;
const MAX_IDLE_TIMEOUT_MS = 18e5;
const BARE_ARGS = /* @__PURE__ */ new Set([
"-p",
"--print",
"--include-partial-messages",
"--verbose",
"--fork-session",
"--safe-mode",
"--bare",
"--no-chrome",
"--disable-slash-commands",
"--no-session-persistence",
"--exclude-dynamic-system-prompt-sections",
"--include-hook-events",
"--replay-user-messages"
]);
const VALUE_ARGS = /* @__PURE__ */ new Set([
"--output-format",
"--input-format",
"--setting-sources",
"--permission-mode",
"--resume",
"-r",
"--session-id",
"--model",
"--effort",
"--max-turns",
"--fallback-model",
"--prompt-suggestions",
"--max-budget-usd",
"--tools",
"--disallowedTools"
]);
const ENV_ALLOWLIST = /* @__PURE__ */ new Set([
"ANTHROPIC_API_KEY",
"CLAUDE_CODE_AUTO_COMPACT_WINDOW",
"CLAUDE_CODE_DISABLE_1M_CONTEXT",
"CLAUDE_CODE_OAUTH_TOKEN",
"FORCE_COLOR",
"LANG",
"LC_ALL",
"LC_CTYPE",
"NO_COLOR",
"TERM"
]);
const CLAUDE_NODE_CLEAR_ENV = /* @__PURE__ */ new Set([
"ANTHROPIC_API_KEY",
"ANTHROPIC_API_KEY_OLD",
"ANTHROPIC_API_TOKEN",
"ANTHROPIC_AUTH_TOKEN",
"ANTHROPIC_BASE_URL",
"ANTHROPIC_CUSTOM_HEADERS",
"ANTHROPIC_OAUTH_TOKEN",
"ANTHROPIC_UNIX_SOCKET",
"CLAUDE_CONFIG_DIR",
"CLAUDE_CODE_AUTO_COMPACT_WINDOW",
"CLAUDE_CODE_DISABLE_1M_CONTEXT",
"CLAUDE_CODE_API_KEY_FILE_DESCRIPTOR",
"CLAUDE_CODE_ENTRYPOINT",
"CLAUDE_CODE_OAUTH_REFRESH_TOKEN",
"CLAUDE_CODE_OAUTH_SCOPES",
"CLAUDE_CODE_OAUTH_TOKEN",
"CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR",
"CLAUDE_CODE_PLUGIN_CACHE_DIR",
"CLAUDE_CODE_PLUGIN_SEED_DIR",
"CLAUDE_CODE_REMOTE",
"CLAUDE_CODE_USE_COWORK_PLUGINS",
"CLAUDE_CODE_USE_BEDROCK",
"CLAUDE_CODE_USE_FOUNDRY",
"CLAUDE_CODE_USE_VERTEX",
"OTEL_EXPORTER_OTLP_ENDPOINT",
"OTEL_EXPORTER_OTLP_HEADERS",
"OTEL_EXPORTER_OTLP_LOGS_ENDPOINT",
"OTEL_EXPORTER_OTLP_LOGS_HEADERS",
"OTEL_EXPORTER_OTLP_LOGS_PROTOCOL",
"OTEL_EXPORTER_OTLP_METRICS_ENDPOINT",
"OTEL_EXPORTER_OTLP_METRICS_HEADERS",
"OTEL_EXPORTER_OTLP_METRICS_PROTOCOL",
"OTEL_EXPORTER_OTLP_PROTOCOL",
"OTEL_EXPORTER_OTLP_TRACES_ENDPOINT",
"OTEL_EXPORTER_OTLP_TRACES_HEADERS",
"OTEL_EXPORTER_OTLP_TRACES_PROTOCOL",
"OTEL_LOGS_EXPORTER",
"OTEL_METRICS_EXPORTER",
"OTEL_SDK_DISABLED",
"OTEL_TRACES_EXPORTER"
]);
const ALLOWED_PARAMS = /* @__PURE__ */ new Set([
"argv",
"stdin",
"cwd",
"env",
"clearEnv",
"nativeAuthOnly",
"systemPrompt",
"agentId",
"sessionKey",
"approvalDecision",
"systemRunPlan",
"idleTimeoutMs",
"timeoutMs",
"skillRuntime"
]);
function requireBoundedString(e, t, n) {
if (typeof e !== "string" || Buffer.byteLength(e, "utf8") > n) throw new Error(`INVALID_REQUEST: ${t} must be a bounded string`);
return e;
}
function optionalBoundedString(e, t, n) {
return e === void 0 ? void 0 : requireBoundedString(e, t, n);
}
/** Claude CLI session ids are bounded, non-option argv values. */
function validateClaudeSessionId(e) {
const t = requireBoundedString(e, "threadId", MAX_ARG_BYTES).trim();
if (!t || t.startsWith("-")) throw new Error("INVALID_REQUEST: threadId must be a Claude session id");
return t;
}
function validateArgs(e) {
if (!Array.isArray(e) || e.length === 0 || e.length > MAX_ARG_COUNT) throw new Error("INVALID_REQUEST: argv must be a bounded non-empty array");
const t = e.map((e, t) => requireBoundedString(e, `argv[${t}]`, MAX_ARG_BYTES));
for (let e = 0; e < t.length; e += 1) {
const n = t[e] ?? "";
const r = n.indexOf("=");
const i = r > 0 ? n.slice(0, r) : n;
if (BARE_ARGS.has(n)) continue;
if (!VALUE_ARGS.has(i)) throw new Error(`INVALID_REQUEST: unsupported Claude CLI argument: ${n || "<empty>"}`);
if (r < 0 && e + 1 >= t.length) throw new Error(`INVALID_REQUEST: Claude CLI argument requires a value: ${i}`);
const a = r > 0 ? n.slice(r + 1) : t[++e];
if (r > 0 && !a || a.startsWith("-")) throw new Error(`INVALID_REQUEST: Claude CLI argument requires a non-option value: ${i}`);
if (i === "--permission-mode" && a === "bypassPermissions") throw new Error("INVALID_REQUEST: bypassPermissions is not allowed for node agent runs");
}
return t;
}
function validateTimeout(e, t, n, r) {
if (typeof e !== "number" || !Number.isInteger(e) || e < n || e > r) throw new Error(`INVALID_REQUEST: ${t} must be an integer from ${n} to ${r}`);
return e;
}
/** Select framing before the command handler validates the complete narrow request. */
function requestsClaudeNodeSkillRuntime(e) {
if (!e || Buffer.byteLength(e, "utf8") > MAX_REQUEST_BYTES) return false;
return safeParseJsonRecord(e)?.skillRuntime === true;
}
/** Resource bytes use the negotiated duplex, never argv or node filesystem paths. */
async function decodeClaudeCliNodeRunParams(e) {
if (Buffer.byteLength(e ?? "", "utf8") > MAX_REQUEST_BYTES) throw new Error("INVALID_REQUEST: Claude CLI request is too large");
const t = asNullableRecord(decodeNodeInvokeParams(e));
if (!t) throw new Error("INVALID_REQUEST: Claude CLI params must be an object");
const n = Object.keys(t).find((e) => !ALLOWED_PARAMS.has(e));
if (n) throw new Error(`INVALID_REQUEST: unknown Claude CLI parameter: ${n}`);
const r = validateArgs(t.argv);
if (t.skillRuntime !== void 0 && t.skillRuntime !== true) throw new Error("INVALID_REQUEST: skillRuntime must be true when supplied");
if (t.nativeAuthOnly !== void 0 && t.nativeAuthOnly !== true) throw new Error("INVALID_REQUEST: nativeAuthOnly must be true when supplied");
const i = optionalBoundedString(t.stdin, "stdin", MAX_REQUEST_BYTES);
const a = optionalBoundedString(t.systemPrompt, "systemPrompt", MAX_REQUEST_BYTES);
const o = optionalBoundedString(t.agentId, "agentId", MAX_ARG_BYTES);
const s = optionalBoundedString(t.sessionKey, "sessionKey", MAX_ARG_BYTES);
const c = t.approvalDecision === "allow-once" || t.approvalDecision === "allow-always" ? t.approvalDecision : void 0;
if (t.approvalDecision !== void 0 && !c) throw new Error("INVALID_REQUEST: approvalDecision is invalid");
const l = t.systemRunPlan === void 0 ? void 0 : asNullableRecord(t.systemRunPlan);
if (t.systemRunPlan !== void 0 && !l) throw new Error("INVALID_REQUEST: systemRunPlan must be an object");
const u = optionalBoundedString(t.cwd, "cwd", MAX_ARG_BYTES);
if (u) {
if (!(await fs.stat(u).catch(() => void 0))?.isDirectory()) throw new Error("INVALID_REQUEST: cwd must be an existing directory on the node");
}
let d;
if (t.env !== void 0) {
const e = asNullableRecord(t.env);
if (!e) throw new Error("INVALID_REQUEST: env must be an object");
d = {};
for (const [t, n] of Object.entries(e)) {
if (!ENV_ALLOWLIST.has(t)) throw new Error(`INVALID_REQUEST: environment key is not allowed: ${t}`);
d[t] = requireBoundedString(n, `env.${t}`, MAX_ARG_BYTES);
}
if (Object.hasOwn(d, "ANTHROPIC_API_KEY") && Object.hasOwn(d, "CLAUDE_CODE_OAUTH_TOKEN")) throw new Error("INVALID_REQUEST: exactly one Claude credential may be provided");
}
let f;
if (t.clearEnv !== void 0) {
if (!Array.isArray(t.clearEnv) || t.clearEnv.length > CLAUDE_NODE_CLEAR_ENV.size) throw new Error("INVALID_REQUEST: clearEnv must be a bounded array");
f = [];
for (const e of t.clearEnv) {
const t = requireBoundedString(e, "clearEnv entry", MAX_ARG_BYTES);
if (!CLAUDE_NODE_CLEAR_ENV.has(t)) throw new Error(`INVALID_REQUEST: clearEnv key is not allowed: ${t}`);
if (!f.includes(t)) f.push(t);
}
}
return {
argv: r,
...t.skillRuntime === true ? { skillRuntime: true } : {},
...t.nativeAuthOnly === true ? { nativeAuthOnly: true } : {},
...i !== void 0 ? { stdin: i } : {},
...a !== void 0 ? { systemPrompt: a } : {},
...o ? { agentId: o } : {},
...s ? { sessionKey: s } : {},
...c ? { approvalDecision: c } : {},
...l ? { systemRunPlan: l } : {},
...u ? { cwd: u } : {},
...d ? { env: d } : {},
...f ? { clearEnv: f } : {},
idleTimeoutMs: validateTimeout(t.idleTimeoutMs, "idleTimeoutMs", MIN_IDLE_TIMEOUT_MS, MAX_IDLE_TIMEOUT_MS),
timeoutMs: validateTimeout(t.timeoutMs, "timeoutMs", 1, MAX_TIMEOUT_MS)
};
}
//#endregion
export { validateClaudeSessionId as i, decodeClaudeCliNodeRunParams as n, requestsClaudeNodeSkillRuntime as r, CLAUDE_NODE_CLEAR_ENV as t };