refactor(auto-reply): share slash parsing for config/debug

This commit is contained in:
Peter Steinberger
2026-02-16 02:10:57 +00:00
parent 1b6704ef53
commit d9ca051a1d
4 changed files with 116 additions and 88 deletions

View File

@@ -1,4 +1,5 @@
import { parseConfigValue } from "./config-value.js";
import { parseSetUnsetCommand } from "./commands-setunset.js";
import { parseSlashCommandOrNull } from "./commands-slash-parse.js";
export type ConfigCommand =
| { action: "show"; path?: string }
@@ -7,60 +8,31 @@ export type ConfigCommand =
| { action: "error"; message: string };
export function parseConfigCommand(raw: string): ConfigCommand | null {
const trimmed = raw.trim();
if (!trimmed.toLowerCase().startsWith("/config")) {
const parsed = parseSlashCommandOrNull(raw, "/config", {
invalidMessage: "Invalid /config syntax.",
});
if (!parsed) {
return null;
}
const rest = trimmed.slice("/config".length).trim();
if (!rest) {
return { action: "show" };
if (!parsed.ok) {
return { action: "error", message: parsed.message };
}
const match = rest.match(/^(\S+)(?:\s+([\s\S]+))?$/);
if (!match) {
return { action: "error", message: "Invalid /config syntax." };
}
const action = match[1].toLowerCase();
const args = (match[2] ?? "").trim();
const { action, args } = parsed;
switch (action) {
case "show":
return { action: "show", path: args || undefined };
case "get":
return { action: "show", path: args || undefined };
case "unset": {
if (!args) {
return { action: "error", message: "Usage: /config unset path" };
}
return { action: "unset", path: args };
}
case "unset":
case "set": {
if (!args) {
return {
action: "error",
message: "Usage: /config set path=value",
};
const parsed = parseSetUnsetCommand({ slash: "/config", action, args });
if (parsed.kind === "error") {
return { action: "error", message: parsed.message };
}
const eqIndex = args.indexOf("=");
if (eqIndex <= 0) {
return {
action: "error",
message: "Usage: /config set path=value",
};
}
const path = args.slice(0, eqIndex).trim();
const rawValue = args.slice(eqIndex + 1);
if (!path) {
return {
action: "error",
message: "Usage: /config set path=value",
};
}
const parsed = parseConfigValue(rawValue);
if (parsed.error) {
return { action: "error", message: parsed.error };
}
return { action: "set", path, value: parsed.value };
return parsed.kind === "set"
? { action: "set", path: parsed.path, value: parsed.value }
: { action: "unset", path: parsed.path };
}
default:
return {