openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
56 lines (55 loc) • 2.37 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { a as isRuntimeToolAllowed } from "./tool-policy-match-DzwVnbV4.mjs";
//#region src/cron/agent-turn-command-prompt.ts
const COMMAND_MARKER_RE = /\bCommand to run\s*:/iu;
const COMMAND_FIELD_RE = /^\s*-\s*(command|workdir|timeout)\s*:\s*(.*?)\s*$/iu;
const SHELL_COMMAND_MESSAGE_RE = /\b(?:bash|command|execute|exec|process|run|shell)\b[\s\S]{0,240}\b(?:python3?|node|bun|pnpm|npm|npx|yarn|sh|bash|sudo|cd|\.\/|\/[A-Za-z0-9._/-]+)\b/iu;
function parsePositiveInteger(e) {
const t = e.trim();
if (!/^\d+$/u.test(t)) return;
const n = Number.parseInt(t, 10);
return Number.isFinite(n) && n > 0 ? n : void 0;
}
/** Parses the legacy structured command block used by isolated agent prompts. */
function parseCronAgentTurnCommandPrompt(e) {
const t = normalizeOptionalString(e);
if (!t || !COMMAND_MARKER_RE.test(t)) return null;
let n = "";
let r;
let i;
for (const e of t.split(/\r?\n/u)) {
const t = COMMAND_FIELD_RE.exec(e);
if (!t) continue;
const a = t[1]?.toLowerCase();
const o = t[2]?.trim() ?? "";
if (a === "command" && o && !n) n = o;
else if (a === "workdir" && o && !r) r = o;
else if (a === "timeout" && o && i === void 0) i = parsePositiveInteger(o);
}
return n ? {
command: n,
...r ? { cwd: r } : {},
...i ? { timeoutSeconds: i } : {}
} : null;
}
/** Returns whether an agent-turn cap explicitly permits a shell/process tool. */
function hasCronShellToolAccess(e) {
if (e === void 0) return true;
if (!Array.isArray(e)) return false;
if (!e.every((e) => typeof e === "string")) return false;
if (e.some((e) => e.trim().length === 0)) return false;
return isRuntimeToolAllowed("exec", e) || isRuntimeToolAllowed("process", e);
}
/** Classifies only command-like agent turns that need cron operator attention. */
function classifyCronAgentTurnShellPrompt(e) {
if (e.kind !== "agentTurn") return null;
const t = normalizeOptionalString(e.message);
if (!t) return null;
const n = parseCronAgentTurnCommandPrompt(t);
const r = hasCronShellToolAccess(e.toolsAllow);
if (n && !r) return "commandPromptWithoutShellAccess";
if (r && SHELL_COMMAND_MESSAGE_RE.test(t)) return "shellToolPrompt";
return null;
}
//#endregion
export { hasCronShellToolAccess as n, parseCronAgentTurnCommandPrompt as r, classifyCronAgentTurnShellPrompt as t };