Files
openclaw/src/auto-reply/command-turn-detection.ts
Peter Steinberger 00d8d7ead0 refactor: extract normalization core package
Extract shared normalization/coercion helpers into private @openclaw/normalization-core workspace package while preserving existing plugin SDK helper subpaths.\n\nAlso keeps direct normalization-core imports internal, wires UI/build/loader resolution, and replaces the slow PR network CodeQL lane with a fast added-line boundary scan while retaining full CodeQL for scheduled/manual runs.\n\nVerification: local moved tests, plugin SDK boundary tests, extension loader tests, agents-support shard, UI build/test, build artifacts, lint, workflow guards, autoreview, and GitHub CI passed on PR head 963d893715.
2026-05-31 01:33:00 +01:00

60 lines
1.9 KiB
TypeScript

import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { isControlCommandMessage } from "./command-detection.js";
import {
isExplicitCommandTurn,
resolveCommandTurnContext,
type CommandTurnContextInput,
} from "./command-turn-context.js";
function resolveCommandBody(input: CommandTurnContextInput): string | undefined {
return (
normalizeOptionalString(input.CommandBody) ??
normalizeOptionalString(input.BodyForCommands) ??
normalizeOptionalString(input.RawBody) ??
normalizeOptionalString(input.Body)
);
}
function resolveVisibleMessageBody(input: CommandTurnContextInput): string | undefined {
return normalizeOptionalString(input.RawBody) ?? normalizeOptionalString(input.Body);
}
function resolveStructuredNormalFallbackBody(input: CommandTurnContextInput): string | undefined {
const visibleBody = resolveVisibleMessageBody(input);
if (!/^[!/]/.test(visibleBody ?? "")) {
return undefined;
}
return resolveCommandBody(input) ?? visibleBody;
}
function hasCommandSourceMetadata(input: CommandTurnContextInput): boolean {
return (
input.CommandSource === "native" ||
input.CommandSource === "text" ||
input.CommandSource === "message"
);
}
export function isExplicitCommandTurnContext(
input: CommandTurnContextInput,
cfg: OpenClawConfig,
): boolean {
if (isExplicitCommandTurn(resolveCommandTurnContext(input))) {
return true;
}
if (input.CommandSource === "native" || input.CommandSource === "text") {
return false;
}
const fallbackBody =
input.CommandTurn !== undefined || hasCommandSourceMetadata(input)
? resolveStructuredNormalFallbackBody(input)
: resolveCommandBody(input);
return (
input.CommandAuthorized === true &&
isControlCommandMessage(fallbackBody, cfg, {
botUsername: normalizeOptionalString(input.BotUsername),
})
);
}