Something went wrong. Try again.
I'm going to build my own OpenClaw, with blackjack... and bun!
Something went wrong. Try again.
1.8 kB · 42 lines
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243/** * agent-control/agent-control-parser.ts – Parse raw text into control commands. * * Takes a raw message string, strips any trigger prefix (@BotName), and * attempts to match it against the registered command parsers. Returns * an AgentControlCommand if the text is a valid control command, or null * if it's a regular message. * * Consumers: * - runtime/message-loop.ts calls parseControlCommand() on each inbound * message to determine if it should be handled as a control command. * - channels/web/request-router.ts uses it for web-channel messages. */
import type { AgentControlCommand } from "./agent-control-types.js";import { normalizeControlCommandName } from "./command-registry.js";import { stripTransportAttachmentFooter, stripTrigger } from "./parser-utils.js";import { COMMAND_PARSERS } from "./command-parsers.js";
/** Parse a raw text message into an AgentControlCommand, or return null if not a command. */export function parseControlCommand(text: string, triggerPattern?: RegExp): AgentControlCommand | null { if (!text) return null; const cleaned = stripTrigger(text, triggerPattern); if (!cleaned.startsWith("/")) return null;
const commandMatch = cleaned.match(/^\S+/); const command = commandMatch?.[0]; if (!command) return null;
const rawArgs = cleaned.slice(command.length).replace(/^\s+/, ""); const normalizedArgs = rawArgs.split(/\s+/).filter(Boolean).join(" ").trim(); const commandLower = normalizeControlCommandName(command.toLowerCase());
const parser = COMMAND_PARSERS[commandLower]; if (!parser) return null;
const parserArgs = commandLower === "/shell" || commandLower === "/bash" ? stripTransportAttachmentFooter(rawArgs) : normalizedArgs;
return parser(parserArgs, cleaned);}