mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
refactor(auto-reply): share standard set/unset slash parsing
This commit is contained in:
23
src/auto-reply/reply/commands-setunset-standard.ts
Normal file
23
src/auto-reply/reply/commands-setunset-standard.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { parseSlashCommandWithSetUnset } from "./commands-setunset.js";
|
||||
|
||||
export function parseStandardSetUnsetSlashCommand<T>(params: {
|
||||
raw: string;
|
||||
slash: string;
|
||||
invalidMessage: string;
|
||||
usageMessage: string;
|
||||
onKnownAction: (action: string, args: string) => T | undefined;
|
||||
onSet?: (path: string, value: unknown) => T;
|
||||
onUnset?: (path: string) => T;
|
||||
onError?: (message: string) => T;
|
||||
}): T | null {
|
||||
return parseSlashCommandWithSetUnset<T>({
|
||||
raw: params.raw,
|
||||
slash: params.slash,
|
||||
invalidMessage: params.invalidMessage,
|
||||
usageMessage: params.usageMessage,
|
||||
onKnownAction: params.onKnownAction,
|
||||
onSet: params.onSet ?? ((path, value) => ({ action: "set", path, value }) as T),
|
||||
onUnset: params.onUnset ?? ((path) => ({ action: "unset", path }) as T),
|
||||
onError: params.onError ?? ((message) => ({ action: "error", message }) as T),
|
||||
});
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { parseStandardSetUnsetSlashCommand } from "./commands-setunset-standard.js";
|
||||
import {
|
||||
parseSetUnsetCommand,
|
||||
parseSetUnsetCommandAction,
|
||||
@@ -114,3 +115,28 @@ describe("parseSlashCommandWithSetUnset", () => {
|
||||
expect(unknownAction).toEqual({ action: "error", message: "Usage: /config show|set|unset" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseStandardSetUnsetSlashCommand", () => {
|
||||
it("uses default set/unset/error mappings", () => {
|
||||
const result = parseStandardSetUnsetSlashCommand<ParsedSetUnsetAction>({
|
||||
raw: '/config set a.b={"ok":true}',
|
||||
slash: "/config",
|
||||
invalidMessage: "Invalid /config syntax.",
|
||||
usageMessage: "Usage: /config show|set|unset",
|
||||
onKnownAction: () => undefined,
|
||||
});
|
||||
expect(result).toEqual({ action: "set", path: "a.b", value: { ok: true } });
|
||||
});
|
||||
|
||||
it("supports caller-provided mappings", () => {
|
||||
const result = parseStandardSetUnsetSlashCommand<ParsedSetUnsetAction>({
|
||||
raw: "/config unset a.b",
|
||||
slash: "/config",
|
||||
invalidMessage: "Invalid /config syntax.",
|
||||
usageMessage: "Usage: /config show|set|unset",
|
||||
onKnownAction: () => undefined,
|
||||
onUnset: (path) => ({ action: "unset", path: `wrapped:${path}` }),
|
||||
});
|
||||
expect(result).toEqual({ action: "unset", path: "wrapped:a.b" });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { parseSlashCommandWithSetUnset } from "./commands-setunset.js";
|
||||
import { parseStandardSetUnsetSlashCommand } from "./commands-setunset-standard.js";
|
||||
|
||||
export type ConfigCommand =
|
||||
| { action: "show"; path?: string }
|
||||
@@ -7,7 +7,7 @@ export type ConfigCommand =
|
||||
| { action: "error"; message: string };
|
||||
|
||||
export function parseConfigCommand(raw: string): ConfigCommand | null {
|
||||
return parseSlashCommandWithSetUnset<ConfigCommand>({
|
||||
return parseStandardSetUnsetSlashCommand<ConfigCommand>({
|
||||
raw,
|
||||
slash: "/config",
|
||||
invalidMessage: "Invalid /config syntax.",
|
||||
@@ -18,8 +18,5 @@ export function parseConfigCommand(raw: string): ConfigCommand | null {
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
onSet: (path, value) => ({ action: "set", path, value }),
|
||||
onUnset: (path) => ({ action: "unset", path }),
|
||||
onError: (message) => ({ action: "error", message }),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { parseSlashCommandWithSetUnset } from "./commands-setunset.js";
|
||||
import { parseStandardSetUnsetSlashCommand } from "./commands-setunset-standard.js";
|
||||
|
||||
export type DebugCommand =
|
||||
| { action: "show" }
|
||||
@@ -8,7 +8,7 @@ export type DebugCommand =
|
||||
| { action: "error"; message: string };
|
||||
|
||||
export function parseDebugCommand(raw: string): DebugCommand | null {
|
||||
return parseSlashCommandWithSetUnset<DebugCommand>({
|
||||
return parseStandardSetUnsetSlashCommand<DebugCommand>({
|
||||
raw,
|
||||
slash: "/debug",
|
||||
invalidMessage: "Invalid /debug syntax.",
|
||||
@@ -22,8 +22,5 @@ export function parseDebugCommand(raw: string): DebugCommand | null {
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
onSet: (path, value) => ({ action: "set", path, value }),
|
||||
onUnset: (path) => ({ action: "unset", path }),
|
||||
onError: (message) => ({ action: "error", message }),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user