Files
openclaw/src/auto-reply/reply/reply-inline.ts
scoootscooob 6200e242b2 fix(auto-reply): preserve newlines in stripInlineStatus and extractInlineSimpleCommand
The /\s+/g whitespace normalizer collapsed newlines along with spaces/tabs,
destroying paragraph structure in multi-line messages before they reached
the LLM. Use /[^\S\n]+/g to only collapse horizontal whitespace while
preserving line breaks.

Closes #32216

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-02 22:43:10 +00:00

50 lines
1.3 KiB
TypeScript

const INLINE_SIMPLE_COMMAND_ALIASES = new Map<string, string>([
["/help", "/help"],
["/commands", "/commands"],
["/whoami", "/whoami"],
["/id", "/whoami"],
]);
const INLINE_SIMPLE_COMMAND_RE = /(?:^|\s)\/(help|commands|whoami|id)(?=$|\s|:)/i;
const INLINE_STATUS_RE = /(?:^|\s)\/status(?=$|\s|:)(?:\s*:\s*)?/gi;
export function extractInlineSimpleCommand(body?: string): {
command: string;
cleaned: string;
} | null {
if (!body) {
return null;
}
const match = body.match(INLINE_SIMPLE_COMMAND_RE);
if (!match || match.index === undefined) {
return null;
}
const alias = `/${match[1].toLowerCase()}`;
const command = INLINE_SIMPLE_COMMAND_ALIASES.get(alias);
if (!command) {
return null;
}
const cleaned = body
.replace(match[0], " ")
.replace(/[^\S\n]+/g, " ")
.trim();
return { command, cleaned };
}
export function stripInlineStatus(body: string): {
cleaned: string;
didStrip: boolean;
} {
const trimmed = body.trim();
if (!trimmed) {
return { cleaned: "", didStrip: false };
}
// Use [^\S\n]+ instead of \s+ to only collapse horizontal whitespace,
// preserving newlines so multi-line messages keep their paragraph structure.
const cleaned = trimmed
.replace(INLINE_STATUS_RE, " ")
.replace(/[^\S\n]+/g, " ")
.trim();
return { cleaned, didStrip: cleaned !== trimmed };
}