Files
openclaw/src/plugins/hook-agent-context.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

83 lines
2.6 KiB
TypeScript

import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
import { parseRawSessionConversationRef } from "../sessions/session-key-utils.js";
import type { PluginHookAgentContext } from "./hook-types.js";
const TARGET_PREFIXES = new Set(["channel", "chat", "direct", "dm", "group", "thread", "user"]);
function normalizeKey(value: string | undefined): string {
return (value ?? "").trim().toLowerCase();
}
function stripConversationPrefix(
value: string | undefined,
...providers: Array<string | undefined>
): string | undefined {
const text = normalizeOptionalString(value);
if (!text) {
return undefined;
}
const separatorIndex = text.indexOf(":");
if (separatorIndex === -1) {
return text;
}
const prefix = normalizeKey(text.slice(0, separatorIndex));
const suffix = normalizeOptionalString(text.slice(separatorIndex + 1));
if (!suffix) {
return text;
}
if (
TARGET_PREFIXES.has(prefix) ||
providers.some((provider) => prefix === normalizeKey(provider))
) {
return suffix;
}
return text;
}
export function resolveAgentHookChannelId(params: {
sessionKey?: string | null;
messageChannel?: string | null;
messageProvider?: string | null;
currentChannelId?: string | null;
messageTo?: string | null;
}): string | undefined {
const provider = normalizeOptionalString(params.messageProvider);
const messageChannel = normalizeOptionalString(params.messageChannel);
const parsed = parseRawSessionConversationRef(params.sessionKey);
if (parsed?.rawId) {
return parsed.rawId;
}
const metadataChannel =
stripConversationPrefix(params.currentChannelId ?? undefined, provider, messageChannel) ??
stripConversationPrefix(params.messageTo ?? undefined, provider, messageChannel);
if (metadataChannel && normalizeKey(metadataChannel) !== normalizeKey(provider)) {
return metadataChannel;
}
const strippedMessageChannel = stripConversationPrefix(
params.messageChannel ?? undefined,
provider,
messageChannel,
);
if (strippedMessageChannel && normalizeKey(strippedMessageChannel) !== normalizeKey(provider)) {
return strippedMessageChannel;
}
return messageChannel ?? provider;
}
export function buildAgentHookContextChannelFields(params: {
sessionKey?: string | null;
messageChannel?: string | null;
messageProvider?: string | null;
currentChannelId?: string | null;
messageTo?: string | null;
}): Pick<PluginHookAgentContext, "channelId" | "messageProvider"> {
return {
messageProvider: normalizeOptionalString(params.messageProvider),
channelId: resolveAgentHookChannelId(params),
};
}