UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

98 lines (97 loc) • 4.27 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; //#region src/auto-reply/command-turn-context.ts /** Command-source normalization for native slash commands, text slash commands, and plain messages. */ function resolveCommandBody(e) { if (typeof e.commandText === "string") return e.commandText; return normalizeOptionalString(e.CommandBody) ?? normalizeOptionalString(e.BodyForCommands) ?? normalizeOptionalString(e.RawBody) ?? normalizeOptionalString(e.Body); } function parseCommandName(e) { if (!e?.startsWith("/")) return; const t = e.slice(1).split(/\s+/, 1)[0]?.split("@", 1)[0]; return normalizeOptionalString(t); } /** Maps the internal turn discriminator to the source value used by downstream routing. */ function commandTurnKindToSource(e) { return e === "native" ? "native" : e === "text-slash" ? "text" : "message"; } function normalizeCommandTurnKind(e) { return e === "native" || e === "text-slash" || e === "normal" ? e : void 0; } function normalizeCommandTurnSource(e) { return e === "native" || e === "text" || e === "message" ? e : void 0; } /** Builds a normalized command-turn context and forces normal messages to unauthorized. */ function createCommandTurnContext(e, t) { return { ...e === "native" ? { kind: "native", source: "native", authorized: t.authorized } : e === "text" ? { kind: "text-slash", source: "text", authorized: t.authorized } : { kind: "normal", source: "message", authorized: false }, commandName: t.commandName, body: t.body }; } function normalizeExplicitCommandTurn(e, t) { if (!e || typeof e !== "object") return; const n = e; const r = normalizeCommandTurnKind(n.kind); const i = normalizeCommandTurnSource(n.source) ?? (r ? commandTurnKindToSource(r) : void 0); if (r && i && commandTurnKindToSource(r) !== i) return; if (!i) return; const a = normalizeOptionalString(n.body) ?? resolveCommandBody(t); return createCommandTurnContext(i, { authorized: typeof n.authorized === "boolean" ? n.authorized : t.CommandAuthorized === true, commandName: normalizeOptionalString(n.commandName) ?? parseCommandName(a), body: a }); } /** Normalizes command metadata with a legacy body fallback for older channel contexts. */ function resolveCommandTurnContext(e) { const t = normalizeExplicitCommandTurn(e.CommandTurn, e); if (t) return t; const n = normalizeCommandTurnSource(e.CommandSource) ?? "message"; const r = resolveCommandBody(e); return createCommandTurnContext(n, { authorized: e.CommandAuthorized === true, commandName: parseCommandName(r), body: r }); } /** Returns true for channel-native command turns. */ function isNativeCommandTurn(e) { return e?.kind === "native"; } /** Returns true for text slash-command turns regardless of authorization. */ function isTextSlashCommandTurn(e) { return e?.kind === "text-slash"; } function isAuthorizedTextSlashCommandTurn(e) { return e?.kind === "text-slash" && e.authorized; } /** Returns true when a turn was explicitly invoked by a native or authorized text command. */ function isExplicitCommandTurn(e) { return e?.kind === "native" || e?.kind === "text-slash" && e.authorized; } /** Retains one-message followup intent after its authorized handler strips command text. */ function isExplicitFollowupCommandTurn(e) { const t = resolveCommandTurnContext(e); return t.kind !== "normal" && t.authorized && t.commandName?.toLowerCase() === "followup"; } /** Resolves the target session override for trusted native or continuing command turns. */ function resolveCommandTurnTargetSessionKey(e) { const t = resolveCommandTurnContext(e); const n = isAuthorizedTextSlashCommandTurn(t) && (t.commandName?.toLowerCase() === "steer" || t.commandName?.toLowerCase() === "tell" || t.commandName?.toLowerCase() === "followup"); if (!isNativeCommandTurn(t) && !n) return; return normalizeOptionalString(e.CommandTargetSessionKey); } //#endregion export { isExplicitFollowupCommandTurn as a, resolveCommandBody as c, isExplicitCommandTurn as i, resolveCommandTurnContext as l, createCommandTurnContext as n, isNativeCommandTurn as o, isAuthorizedTextSlashCommandTurn as r, isTextSlashCommandTurn as s, commandTurnKindToSource as t, resolveCommandTurnTargetSessionKey as u };