fix(session): preserve command argument casing

This commit is contained in:
Doug von Kohorn
2026-01-20 12:53:45 +01:00
parent 7e08de4a5f
commit 528524e4c7
2 changed files with 45 additions and 4 deletions

View File

@@ -151,6 +151,34 @@ describe("initSessionState RawBody", () => {
expect(result.bodyStripped).toBe("");
});
it("preserves argument casing while still matching reset triggers case-insensitively", async () => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-rawbody-reset-case-"));
const storePath = path.join(root, "sessions.json");
const cfg = {
session: {
store: storePath,
resetTriggers: ["/new"],
},
} as ClawdbotConfig;
const ctx = {
RawBody: "/NEW KeepThisCase",
ChatType: "direct",
SessionKey: "agent:main:whatsapp:dm:S1",
};
const result = await initSessionState({
ctx,
cfg,
commandAuthorized: true,
});
expect(result.isNewSession).toBe(true);
expect(result.bodyStripped).toBe("KeepThisCase");
expect(result.triggerBodyNormalized).toBe("/NEW KeepThisCase");
});
it("falls back to Body when RawBody is undefined", async () => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-rawbody-fallback-"));
const storePath = path.join(root, "sessions.json");

View File

@@ -135,7 +135,10 @@ export async function initSessionState(params: {
// Prefer CommandBody/RawBody (clean message) for command detection; fall back
// to Body which may contain structural context (history, sender labels).
const commandSource = ctx.BodyForCommands ?? ctx.CommandBody ?? ctx.RawBody ?? ctx.Body ?? "";
const triggerBodyNormalized = stripStructuralPrefixes(commandSource).trim().toLowerCase();
// IMPORTANT: do NOT lowercase the entire command body.
// Users often pass case-sensitive arguments (e.g. filesystem paths on Linux).
// Command parsing downstream lowercases only the command token for matching.
const triggerBodyNormalized = stripStructuralPrefixes(commandSource).trim();
// Use CommandBody/RawBody for reset trigger matching (clean message without structural context).
const rawBody = commandSource;
@@ -151,17 +154,27 @@ export async function initSessionState(params: {
const strippedForReset = isGroup
? stripMentions(triggerBodyNormalized, ctx, cfg, agentId)
: triggerBodyNormalized;
// Reset triggers are configured as lowercased commands (e.g. "/new"), but users may type
// "/NEW" etc. Match case-insensitively while keeping the original casing for any stripped body.
const trimmedBodyLower = trimmedBody.toLowerCase();
const strippedForResetLower = strippedForReset.toLowerCase();
for (const trigger of resetTriggers) {
if (!trigger) continue;
if (!resetAuthorized) break;
if (trimmedBody === trigger || strippedForReset === trigger) {
const triggerLower = trigger.toLowerCase();
if (trimmedBodyLower === triggerLower || strippedForResetLower === triggerLower) {
isNewSession = true;
bodyStripped = "";
resetTriggered = true;
break;
}
const triggerPrefix = `${trigger} `;
if (trimmedBody.startsWith(triggerPrefix) || strippedForReset.startsWith(triggerPrefix)) {
const triggerPrefixLower = `${triggerLower} `;
if (
trimmedBodyLower.startsWith(triggerPrefixLower) ||
strippedForResetLower.startsWith(triggerPrefixLower)
) {
isNewSession = true;
bodyStripped = strippedForReset.slice(trigger.length).trimStart();
resetTriggered = true;