mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-18 08:31:49 -06:00
096b2764e0
* refactor(tests): simplify internal test-only seams * chore(plugin-sdk): refresh API baseline
53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
// Resolves inline reply directives that alter a single reply turn.
|
|
import { normalizeLowercaseStringOrEmpty } from "@openclaw/normalization-core/string-coerce";
|
|
|
|
const INLINE_HORIZONTAL_WHITESPACE_RE = /[^\S\n]+/g;
|
|
|
|
function collapseInlineHorizontalWhitespace(value: string): string {
|
|
return value.replace(INLINE_HORIZONTAL_WHITESPACE_RE, " ");
|
|
}
|
|
|
|
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 = `/${normalizeLowercaseStringOrEmpty(match[1])}`;
|
|
const command = INLINE_SIMPLE_COMMAND_ALIASES.get(alias);
|
|
if (!command) {
|
|
return null;
|
|
}
|
|
const cleaned = collapseInlineHorizontalWhitespace(body.replace(match[0], " ")).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 = collapseInlineHorizontalWhitespace(trimmed.replace(INLINE_STATUS_RE, " ")).trim();
|
|
return { cleaned, didStrip: cleaned !== trimmed };
|
|
}
|