refactor: register channel bootstrap capabilities

This commit is contained in:
Peter Steinberger
2026-04-05 09:13:30 +01:00
parent a5b6b71468
commit 41e39eb46f
58 changed files with 444 additions and 949 deletions

View File

@@ -1,5 +1,3 @@
import { getBundledChannelContractSurfaceModule } from "../channels/plugins/contract-surfaces.js";
export type TelegramCustomCommandInput = {
command?: string | null;
description?: string | null;
@@ -10,26 +8,9 @@ export type TelegramCustomCommandIssue = {
field: "command" | "description";
message: string;
};
const TELEGRAM_COMMAND_NAME_PATTERN_VALUE = /^[a-z0-9_]{1,32}$/;
type TelegramCommandConfigContract = {
TELEGRAM_COMMAND_NAME_PATTERN: RegExp;
normalizeTelegramCommandName: (value: string) => string;
normalizeTelegramCommandDescription: (value: string) => string;
resolveTelegramCustomCommands: (params: {
commands?: TelegramCustomCommandInput[] | null;
reservedCommands?: Set<string>;
checkReserved?: boolean;
checkDuplicates?: boolean;
}) => {
commands: Array<{ command: string; description: string }>;
issues: TelegramCustomCommandIssue[];
};
};
const FALLBACK_TELEGRAM_COMMAND_NAME_PATTERN = /^[a-z0-9_]{1,32}$/;
let cachedTelegramCommandConfigContract: TelegramCommandConfigContract | null = null;
function fallbackNormalizeTelegramCommandName(value: string): string {
function normalizeTelegramCommandNameImpl(value: string): string {
const trimmed = value.trim();
if (!trimmed) {
return "";
@@ -38,11 +19,11 @@ function fallbackNormalizeTelegramCommandName(value: string): string {
return withoutSlash.trim().toLowerCase().replace(/-/g, "_");
}
function fallbackNormalizeTelegramCommandDescription(value: string): string {
function normalizeTelegramCommandDescriptionImpl(value: string): string {
return value.trim();
}
function fallbackResolveTelegramCustomCommands(params: {
function resolveTelegramCustomCommandsImpl(params: {
commands?: TelegramCustomCommandInput[] | null;
reservedCommands?: Set<string>;
checkReserved?: boolean;
@@ -61,7 +42,7 @@ function fallbackResolveTelegramCustomCommands(params: {
for (let index = 0; index < entries.length; index += 1) {
const entry = entries[index];
const normalized = fallbackNormalizeTelegramCommandName(String(entry?.command ?? ""));
const normalized = normalizeTelegramCommandNameImpl(String(entry?.command ?? ""));
if (!normalized) {
issues.push({
index,
@@ -70,7 +51,7 @@ function fallbackResolveTelegramCustomCommands(params: {
});
continue;
}
if (!FALLBACK_TELEGRAM_COMMAND_NAME_PATTERN.test(normalized)) {
if (!TELEGRAM_COMMAND_NAME_PATTERN_VALUE.test(normalized)) {
issues.push({
index,
field: "command",
@@ -94,9 +75,7 @@ function fallbackResolveTelegramCustomCommands(params: {
});
continue;
}
const description = fallbackNormalizeTelegramCommandDescription(
String(entry?.description ?? ""),
);
const description = normalizeTelegramCommandDescriptionImpl(String(entry?.description ?? ""));
if (!description) {
issues.push({
index,
@@ -114,38 +93,18 @@ function fallbackResolveTelegramCustomCommands(params: {
return { commands: resolved, issues };
}
const FALLBACK_TELEGRAM_COMMAND_CONFIG_CONTRACT: TelegramCommandConfigContract = {
TELEGRAM_COMMAND_NAME_PATTERN: FALLBACK_TELEGRAM_COMMAND_NAME_PATTERN,
normalizeTelegramCommandName: fallbackNormalizeTelegramCommandName,
normalizeTelegramCommandDescription: fallbackNormalizeTelegramCommandDescription,
resolveTelegramCustomCommands: fallbackResolveTelegramCustomCommands,
};
function loadTelegramCommandConfigContract(): TelegramCommandConfigContract {
cachedTelegramCommandConfigContract ??=
getBundledChannelContractSurfaceModule<TelegramCommandConfigContract>({
pluginId: "telegram",
preferredBasename: "contract-surfaces.ts",
}) ?? FALLBACK_TELEGRAM_COMMAND_CONFIG_CONTRACT;
return cachedTelegramCommandConfigContract;
}
export function getTelegramCommandNamePattern(): RegExp {
return loadTelegramCommandConfigContract().TELEGRAM_COMMAND_NAME_PATTERN;
return TELEGRAM_COMMAND_NAME_PATTERN_VALUE;
}
/**
* @deprecated Use `getTelegramCommandNamePattern()` when you need the live
* bundled contract value. This export remains an import-time-safe fallback.
*/
export const TELEGRAM_COMMAND_NAME_PATTERN = FALLBACK_TELEGRAM_COMMAND_NAME_PATTERN;
export const TELEGRAM_COMMAND_NAME_PATTERN = TELEGRAM_COMMAND_NAME_PATTERN_VALUE;
export function normalizeTelegramCommandName(value: string): string {
return loadTelegramCommandConfigContract().normalizeTelegramCommandName(value);
return normalizeTelegramCommandNameImpl(value);
}
export function normalizeTelegramCommandDescription(value: string): string {
return loadTelegramCommandConfigContract().normalizeTelegramCommandDescription(value);
return normalizeTelegramCommandDescriptionImpl(value);
}
export function resolveTelegramCustomCommands(params: {
@@ -157,5 +116,5 @@ export function resolveTelegramCustomCommands(params: {
commands: Array<{ command: string; description: string }>;
issues: TelegramCustomCommandIssue[];
} {
return loadTelegramCommandConfigContract().resolveTelegramCustomCommands(params);
return resolveTelegramCustomCommandsImpl(params);
}