Files
openclaw/src/cli/message-secret-scope.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

79 lines
2.2 KiB
TypeScript

import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
import { normalizeAccountId } from "../routing/session-key.js";
import { isDeliverableMessageChannel, normalizeMessageChannel } from "../utils/message-channel.js";
function resolveScopedChannelCandidate(value: unknown): string | undefined {
if (typeof value !== "string") {
return undefined;
}
const normalized = normalizeMessageChannel(value);
if (!normalized || !isDeliverableMessageChannel(normalized)) {
return undefined;
}
return normalized;
}
function resolveChannelFromTargetValue(target: unknown): string | undefined {
const trimmed = normalizeOptionalString(target);
if (!trimmed) {
return undefined;
}
const separator = trimmed.indexOf(":");
if (separator <= 0) {
return undefined;
}
return resolveScopedChannelCandidate(trimmed.slice(0, separator));
}
function resolveChannelFromTargets(targets: unknown): string | undefined {
if (!Array.isArray(targets)) {
return undefined;
}
const seen = new Set<string>();
for (const target of targets) {
const channel = resolveChannelFromTargetValue(target);
if (channel) {
seen.add(channel);
}
}
if (seen.size !== 1) {
return undefined;
}
return [...seen][0];
}
function resolveScopedAccountId(value: unknown): string | undefined {
const trimmed = normalizeOptionalString(value);
if (!trimmed) {
return undefined;
}
return normalizeAccountId(trimmed);
}
export function resolveMessageSecretScope(params: {
channel?: unknown;
target?: unknown;
targets?: unknown;
fallbackChannel?: string | null;
accountId?: unknown;
fallbackAccountId?: string | null;
}): {
channel?: string;
accountId?: string;
} {
const channel =
resolveScopedChannelCandidate(params.channel) ??
resolveChannelFromTargetValue(params.target) ??
resolveChannelFromTargets(params.targets) ??
resolveScopedChannelCandidate(params.fallbackChannel);
const accountId =
resolveScopedAccountId(params.accountId) ??
resolveScopedAccountId(params.fallbackAccountId ?? undefined);
return {
...(channel ? { channel } : {}),
...(accountId ? { accountId } : {}),
};
}