UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

600 lines (599 loc) • 20.7 kB
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs"; import { n as BASE_THINKING_LEVELS } from "./thinking.shared-jmDGV33r.mjs"; import { o as listThinkingLevels } from "./thinking-Da4gHblr.mjs"; import { t as formatFastModeAutoLabel, u as resolveFastModeModelAutoOnSeconds } from "./fast-mode-Dk3A3IUk.mjs"; import { n as parseActivationCommand } from "./group-activation-sjWfqrud.mjs"; //#region src/auto-reply/commands-args.ts function normalizeArgValue(e) { if (e == null) return; if (typeof e === "string") return normalizeOptionalString(e); if (typeof e === "number" || typeof e === "boolean" || typeof e === "bigint") return normalizeOptionalString(String(e)); if (typeof e === "symbol" || typeof e === "function") return normalizeOptionalString(e.toString()); return JSON.stringify(e) || void 0; } function formatActionArgs(e, t = ["show", "get"]) { const n = normalizeArgValue(e.action)?.toLowerCase(); const r = normalizeArgValue(e.path); const i = normalizeArgValue(e.value); if (!n) return; if (n === "set" && r && i) return `${n} ${r}=${i}`; const a = n === "set" || n === "unset" || t.includes(n); return r && a ? `${n} ${r}` : n; } function formatNamedArgs(e, t, n, r) { const i = []; for (const a of t) { const t = normalizeArgValue(e[a]); if (t) i.push(a === r ? t : `${a}${n}${t}`); } return i.length > 0 ? i.join(" ") : void 0; } const COMMAND_ARG_FORMATTERS = { config: formatActionArgs, mcp: formatActionArgs, plugins: (e) => formatActionArgs(e, [ "show", "get", "enable", "disable" ]), debug: (e) => formatActionArgs(e, []), queue: (e) => formatNamedArgs(e, [ "mode", "debounce", "cap", "drop" ], ":", "mode"), exec: (e) => formatNamedArgs(e, [ "host", "security", "ask", "node" ], "=") }; //#endregion //#region src/auto-reply/reply/commands-slash-parse.ts /** Splits trimmed command text without normalizing the remaining arguments. */ function splitCommandAction(e, t = "") { const n = e.search(/\s/); return { action: (n === -1 ? e : e.slice(0, n)).toLowerCase() || t, args: n === -1 ? "" : e.slice(n).trim() }; } /** Matches a whole, case-insensitive command token and preserves its argument text. */ function matchSlashCommandToken(e, t) { const { action: n, args: r } = splitCommandAction(e.trim()); return n === t ? r : null; } function resolveSlashCommandName(e) { return e.trim().match(/^\/([^\s:]+)(?::|\s|$)/)?.[1]?.toLowerCase() ?? null; } /** Parses a normalized send-policy command without importing command runtime state. */ function parseSendPolicyCommandBody(e) { const t = e.match(/^\/send(?:\s+([a-zA-Z]+))?\s*$/i); if (!t) return { hasCommand: false }; const n = t[1]?.toLowerCase(); if (!n) return { hasCommand: true }; if (n === "inherit" || n === "default" || n === "reset") return { hasCommand: true, mode: "inherit" }; return { hasCommand: true, mode: n === "allow" || n === "on" ? "allow" : n === "deny" || n === "off" ? "deny" : void 0 }; } function parseSlashCommandOrNull(e, t, n = "show") { const r = e.trim(); const i = normalizeLowercaseStringOrEmpty(t); if (!r.toLowerCase().startsWith(i)) return null; const a = r.charAt(t.length); if (a && !/[\s:]/.test(a)) return null; return splitCommandAction(r.slice(t.length).trim(), n); } //#endregion //#region src/auto-reply/commands-registry.shared.ts /** Shared command registry builders used by browser-safe and runtime command lists. */ const BROWSER_SAFE_THINKING_LEVELS = [ ...BASE_THINKING_LEVELS, "xhigh", "adaptive", "max" ]; /** * Keep simple model selections on fast client-side patch paths. Semantic reset * and multi-token forms require the server directive parser to own the full * atomic transaction. */ function shouldForwardModelCommandToServer(e) { const t = e.trim(); const n = t.toLowerCase(); return [ "default", "list", "status" ].includes(n) || /\s/u.test(t); } function defineCommandArgument(e, t, n = {}) { return { name: e, description: t, type: "string", ...n }; } function choiceMenu(e, t, n) { return { args: [defineCommandArgument(e, t, { choices: n })], argsMenu: "auto" }; } function freeformArgs(e, t, n = {}) { return { args: [defineCommandArgument(e, t, { ...n, captureRemaining: true })] }; } /** Defines a built-in command with its aliases and argument parsing defaults. */ function defineBuiltinCommand(e, t, n, r, i = {}) { const { nativeName: a = e } = i; return { ...i, key: e, nativeName: a === false ? void 0 : a, description: t, acceptsArgs: i.acceptsArgs ?? Boolean(i.args?.length), argsParsing: i.argsParsing ?? (i.args?.length ? "positional" : "none"), textAliases: i.textAliases ?? [`/${e}`], scope: a === false ? "text" : "both", category: n, tier: r }; } function definePathCommand(e, t, n, r, i, a) { const o = [defineCommandArgument("action", n.join(" | "), { choices: n }), defineCommandArgument("path", r)]; if (i) o.push(defineCommandArgument("value", i, { captureRemaining: true })); return defineBuiltinCommand(e, t, "management", "power", { modelIndependent: "always", textAliases: a, args: o, argsParsing: "none", formatArgs: COMMAND_ARG_FORMATTERS[e] }); } /** Builds the built-in command list with context-aware thinking choices. */ function buildBuiltinChatCommands(e = {}) { const t = e.listThinkingLevels ?? (() => BROWSER_SAFE_THINKING_LEVELS); const listThinkingLevelChoices = (e, n, r, i) => { return ["default", ...t(e, n, r, i).filter((e) => e !== "default")]; }; return [ defineBuiltinCommand("help", "Show available commands.", "status", "essential", { activeRunSafe: true, modelIndependent: "always" }), defineBuiltinCommand("commands", "List all slash commands.", "status", "power", { activeRunSafe: true, modelIndependent: "no-args" }), defineBuiltinCommand("tools", "List available runtime tools.", "status", "standard", { activeRunSafe: true, modelIndependent: "always", ...choiceMenu("mode", "compact or verbose", ["compact", "verbose"]) }), defineBuiltinCommand("skill", "Run a skill by name.", "tools", "standard", { modelIndependent: "no-args", args: [defineCommandArgument("name", "Skill name", { required: true }), defineCommandArgument("input", "Skill input", { captureRemaining: true })] }), defineBuiltinCommand("dashboard", "Create or update this session's dashboard.", "tools", "standard", freeformArgs("request", "Dashboard requirements")), defineBuiltinCommand("learn", "Learn from recent work or named sources into skills or memory.", "tools", "standard", freeformArgs("request", "Sources and what to learn from them")), defineBuiltinCommand("loop", "Loop a prompt: /loop [interval] <prompt> | /loop status | /loop stop [name]", "tools", "standard", { modelIndependent: (e) => !e || e.toLowerCase() === "help", ...freeformArgs("spec", "[interval] prompt, or status/stop", { required: false }) }), defineBuiltinCommand("status", "Show current status.", "status", "essential", { acceptsArgs: true, activeRunSafe: true, modelIndependent: "always" }), defineBuiltinCommand("goal", "Show or control the current goal.", "status", "standard", { modelIndependent: (e) => { const t = parseSlashCommandOrNull(`/goal ${e}`, "/goal", "status"); return t !== null && ([ "status", "edit", "pause", "complete", "done", "block", "blocked", "clear" ].includes(t.action) || [ "start", "set", "create" ].includes(t.action) && !t.args); }, args: [defineCommandArgument("action", "status, start, edit, pause, resume, complete, block, clear", { choices: [ "status", "start", "edit", "pause", "resume", "complete", "block", "clear" ] }), defineCommandArgument("text", "Goal objective or note", { captureRemaining: true })] }), defineBuiltinCommand("diagnostics", "Explain Gateway diagnostics and Codex feedback upload options.", "status", "standard", { modelIndependent: "always", ...freeformArgs("note", "Optional note for Codex feedback upload") }), defineBuiltinCommand("login", "Connect a model provider.", "management", "standard", { modelIndependent: "always", nativeProviders: [ "discord", "slack", "telegram" ], args: [defineCommandArgument("provider", "Provider or connection method")] }), defineBuiltinCommand("openclaw", "Run the OpenClaw setup and repair helper.", "management", "essential", { modelIndependent: "always", nativeName: false, acceptsArgs: true }), defineBuiltinCommand("allowlist", "List/add/remove allowlist entries.", "management", "power", { modelIndependent: "always", nativeName: false, acceptsArgs: true }), defineBuiltinCommand("approve", "Approve or deny exec requests.", "management", "power", { acceptsArgs: true, activeRunSafe: true, modelIndependent: "always" }), defineBuiltinCommand("context", "Explain how context is built and used.", "status", "standard", { acceptsArgs: true, activeRunSafe: true, modelIndependent: "always" }), defineBuiltinCommand("btw", "Ask a side question without changing future session context.", "tools", "standard", { activeRunSafe: true, modelIndependent: "no-args", nativeAliases: ["side"], textAliases: ["/btw", "/side"], acceptsArgs: true }), defineBuiltinCommand("export-session", "Export current session to an owner-only HTML file in the workspace.", "status", "essential", { modelIndependent: "always", textAliases: ["/export-session", "/export"], args: [defineCommandArgument("path", "Output path inside workspace (default: workspace)", { required: false })] }), defineBuiltinCommand("export-trajectory", "Export a JSONL trajectory bundle for the active session.", "status", "essential", { modelIndependent: "always", textAliases: ["/export-trajectory", "/trajectory"], args: [defineCommandArgument("path", "Output directory (default: workspace)", { required: false })] }), defineBuiltinCommand("tts", "Control text-to-speech (TTS).", "media", "standard", { modelIndependent: "always", args: [defineCommandArgument("action", "TTS action", { choices: [ { value: "on", label: "On" }, { value: "off", label: "Off" }, { value: "status", label: "Status" }, { value: "provider", label: "Provider" }, { value: "limit", label: "Limit" }, { value: "summary", label: "Summary" }, { value: "audio", label: "Audio" }, { value: "help", label: "Help" } ] }), defineCommandArgument("value", "Provider, limit, or text", { captureRemaining: true })], argsMenu: { arg: "action", title: "TTS Actions:\n• On – Enable TTS for responses\n• Off – Disable TTS\n• Status – Show current settings\n• Provider – Show or set the voice provider\n• Limit – Set max characters for TTS\n• Summary – Toggle AI summary for long texts\n• Audio – Generate TTS from custom text\n• Help – Show usage guide" } }), defineBuiltinCommand("whoami", "Show your sender id.", "status", "power", { textAliases: ["/whoami", "/id"], activeRunSafe: true, modelIndependent: "no-args" }), defineBuiltinCommand("session", "Manage conversation bindings and session lifecycle settings.", "session", "power", { modelIndependent: "always", args: [defineCommandArgument("action", "idle | max-age | unbind", { choices: [ "idle", "max-age", "unbind" ] }), defineCommandArgument("value", "Duration (24h, 90m) or off", { captureRemaining: true })], argsMenu: "auto" }), defineBuiltinCommand("subagents", "Inspect subagent runs for this session.", "management", "standard", { activeRunSafe: true, modelIndependent: "always", args: [ defineCommandArgument("action", "list | log | info", { choices: [ "list", "log", "info" ] }), defineCommandArgument("target", "Run id, index, or session key"), defineCommandArgument("value", "Additional input (limit/message)", { captureRemaining: true }) ], argsMenu: "auto" }), defineBuiltinCommand("acp", "Manage ACP sessions and runtime options.", "management", "power", { modelIndependent: (e) => { const t = parseSlashCommandOrNull(`/acp ${e}`, "/acp", "help"); return t !== null && (t.action !== "steer" || !t.args); }, args: [defineCommandArgument("action", "Action to run", { preferAutocomplete: true, choices: [ "spawn", "cancel", "steer", "close", "sessions", "status", "set-mode", "set", "cwd", "permissions", "timeout", "model", "reset-options", "doctor", "install", "help" ] }), defineCommandArgument("value", "Action arguments", { captureRemaining: true })], argsMenu: "auto" }), defineBuiltinCommand("agents", "List thread-bound agents for this session.", "management", "standard", { activeRunSafe: true, modelIndependent: "always" }), defineBuiltinCommand("steer", "Send guidance to the active run in this session.", "management", "standard", { modelIndependent: "no-args", textAliases: ["/steer", "/tell"], ...freeformArgs("message", "Steering message") }), defineBuiltinCommand("followup", "Run this message as a new turn without changing queue mode.", "management", "standard", { modelIndependent: "no-args", ...freeformArgs("message", "Followup message") }), definePathCommand("config", "Show or set config values.", [ "show", "get", "set", "unset" ], "Config path", "Value for set"), definePathCommand("mcp", "Show or set OpenClaw MCP servers.", [ "show", "get", "set", "unset" ], "MCP server name", "JSON config for set"), definePathCommand("plugins", "List, show, enable, or disable plugins.", [ "list", "show", "get", "enable", "disable" ], "Plugin id or name", void 0, ["/plugins", "/plugin"]), definePathCommand("debug", "Set runtime debug overrides.", [ "show", "reset", "set", "unset" ], "Debug path", "Value for set"), defineBuiltinCommand("usage", "Usage footer or cost summary.", "options", "standard", { modelIndependent: "always", ...choiceMenu("mode", "off, tokens, full, or cost", [ "off", "tokens", "full", "cost" ]) }), defineBuiltinCommand("stop", "Stop the current run.", "session", "essential", { activeRunSafe: true, modelIndependent: "no-args" }), defineBuiltinCommand("restart", "Restart OpenClaw.", "tools", "power", { modelIndependent: "no-args" }), defineBuiltinCommand("update", "Update OpenClaw and restart.", "tools", "power", { modelIndependent: "no-args" }), defineBuiltinCommand("activation", "Set group activation mode.", "management", "power", { modelIndependent: (e) => parseActivationCommand(`/activation ${e}`).hasCommand, ...choiceMenu("mode", "mention or always", ["mention", "always"]) }), defineBuiltinCommand("send", "Set send policy.", "management", "power", { modelIndependent: (e) => parseSendPolicyCommandBody(`/send ${e}`).hasCommand, ...choiceMenu("mode", "on, off, or inherit", [ "on", "off", "inherit" ]) }), defineBuiltinCommand("reset", "Reset the current session.", "session", "essential", { activeRunSafe: true, acceptsArgs: true }), defineBuiltinCommand("new", "Start a new session.", "session", "essential", { activeRunSafe: true, modelIndependent: "always", acceptsArgs: true }), defineBuiltinCommand("name", "Name or rename the current session.", "session", "standard", { modelIndependent: "always", ...freeformArgs("title", "New session name (omit to see a suggestion)") }), defineBuiltinCommand("compact", "Compact the session context.", "session", "essential", freeformArgs("instructions", "Extra compaction instructions")), defineBuiltinCommand("think", "Set thinking level.", "options", "essential", { modelIndependent: "always", textAliases: [ "/think", "/thinking", "/t" ], activeRunSafe: true, ...choiceMenu("level", "Thinking level", ({ provider: e, model: t, catalog: n, agentRuntime: r }) => listThinkingLevelChoices(e, t, n, r)) }), defineBuiltinCommand("verbose", "Toggle verbose mode.", "options", "standard", { modelIndependent: "always", textAliases: ["/verbose", "/v"], ...choiceMenu("mode", "on, off, or full", [ "on", "off", "full" ]) }), defineBuiltinCommand("trace", "Toggle plugin trace lines.", "options", "power", { modelIndependent: "directive", ...choiceMenu("mode", "on, off, or raw", [ "on", "off", "raw" ]) }), defineBuiltinCommand("fast", "Toggle fast mode.", "options", "standard", { modelIndependent: "always", ...choiceMenu("mode", "on, off, ultrafast, auto, default, or status", ({ cfg: e, provider: t, model: n }) => [ "on", "off", { value: "auto", label: formatFastModeAutoLabel({ fastAutoOnSeconds: resolveFastModeModelAutoOnSeconds({ cfg: e, provider: t, model: n }) }) }, "default", "status" ]) }), defineBuiltinCommand("reasoning", "Toggle reasoning visibility.", "options", "standard", { modelIndependent: "directive", textAliases: ["/reasoning", "/reason"], ...choiceMenu("mode", "on, off, or stream", [ "on", "off", "stream" ]) }), defineBuiltinCommand("elevated", "Toggle elevated mode.", "options", "power", { modelIndependent: "directive", textAliases: ["/elevated", "/elev"], ...choiceMenu("mode", "on, off, ask, or full", [ "on", "off", "ask", "full" ]) }), defineBuiltinCommand("exec", "Set exec defaults for this session.", "options", "power", { modelIndependent: "directive", args: [ defineCommandArgument("host", "auto, sandbox, gateway, or node", { choices: [ "auto", "sandbox", "gateway", "node" ] }), defineCommandArgument("security", "deny, allowlist, or full", { choices: [ "deny", "allowlist", "full" ] }), defineCommandArgument("ask", "off, on-miss, or always", { choices: [ "off", "on-miss", "always" ] }), defineCommandArgument("node", "Node id or name") ], argsParsing: "none", formatArgs: COMMAND_ARG_FORMATTERS.exec }), defineBuiltinCommand("model", "Show or set the model; use -s, -a, or -g to choose scope.", "options", "essential", { modelIndependent: "directive", args: [defineCommandArgument("model", "Model id; add -s for session, -a for agent, or -g for global scope")] }), defineBuiltinCommand("models", "List model providers/models.", "options", "standard", { acceptsArgs: true, activeRunSafe: true, modelIndependent: "always" }), defineBuiltinCommand("queue", "Adjust queue settings.", "options", "power", { modelIndependent: "directive", args: [ defineCommandArgument("mode", "queue mode", { choices: [ "steer", "followup", "collect", "interrupt" ] }), defineCommandArgument("debounce", "debounce duration (e.g. 500ms, 2s)"), defineCommandArgument("cap", "queue cap", { type: "number" }), defineCommandArgument("drop", "drop policy", { choices: [ "old", "new", "summarize" ] }) ], argsParsing: "none", formatArgs: COMMAND_ARG_FORMATTERS.queue }), defineBuiltinCommand("bash", "Run host shell commands (host-only).", "tools", "power", { modelIndependent: "always", nativeName: false, ...freeformArgs("command", "Shell command") }) ]; } //#endregion //#region src/auto-reply/commands-registry.data.ts let cachedCommands = null; /** Returns the built-in command registry with runtime thinking-level choices. */ function getChatCommands() { return cachedCommands ??= buildBuiltinChatCommands({ listThinkingLevels }); } /** Lists slash command names reserved by built-in chat commands and callers. */ function listReservedChatSlashCommandNames(e = []) { const t = /* @__PURE__ */ new Set(); for (const e of getChatCommands()) { if (e.nativeName) t.add(e.nativeName); for (const n of e.textAliases) t.add(n.slice(1)); } for (const n of e) { const e = normalizeOptionalLowercaseString(n); if (e) t.add(e); } return t; } //#endregion export { parseSendPolicyCommandBody as a, splitCommandAction as c, matchSlashCommandToken as i, listReservedChatSlashCommandNames as n, parseSlashCommandOrNull as o, shouldForwardModelCommandToServer as r, resolveSlashCommandName as s, getChatCommands as t };