refactor(auto-reply): share slash set/unset command parsing

This commit is contained in:
Peter Steinberger
2026-02-18 17:29:24 +00:00
parent fedebc245e
commit f46bcbe16d
4 changed files with 126 additions and 61 deletions

View File

@@ -1,5 +1,4 @@
import { parseSetUnsetCommandAction } from "./commands-setunset.js";
import { parseSlashCommandOrNull } from "./commands-slash-parse.js";
import { parseSlashCommandWithSetUnset } from "./commands-setunset.js";
export type DebugCommand =
| { action: "show" }
@@ -9,37 +8,22 @@ export type DebugCommand =
| { action: "error"; message: string };
export function parseDebugCommand(raw: string): DebugCommand | null {
const parsed = parseSlashCommandOrNull(raw, "/debug", {
invalidMessage: "Invalid /debug syntax.",
});
if (!parsed) {
return null;
}
if (!parsed.ok) {
return { action: "error", message: parsed.message };
}
const { action, args } = parsed;
const setUnset = parseSetUnsetCommandAction<DebugCommand>({
return parseSlashCommandWithSetUnset<DebugCommand>({
raw,
slash: "/debug",
action,
args,
invalidMessage: "Invalid /debug syntax.",
usageMessage: "Usage: /debug show|set|unset|reset",
onKnownAction: (action) => {
if (action === "show") {
return { action: "show" };
}
if (action === "reset") {
return { action: "reset" };
}
return undefined;
},
onSet: (path, value) => ({ action: "set", path, value }),
onUnset: (path) => ({ action: "unset", path }),
onError: (message) => ({ action: "error", message }),
});
if (setUnset) {
return setUnset;
}
switch (action) {
case "show":
return { action: "show" };
case "reset":
return { action: "reset" };
default:
return {
action: "error",
message: "Usage: /debug show|set|unset|reset",
};
}
}