chengyongru 462a0dfb0f
refactor(channels): make built-in channels self-contained (#4908)
* refactor(channels): own setup and instance contracts

* refactor(channels): isolate management contracts

* refactor(channels): normalize activation contracts

* fix(channels): enforce management contracts

* refactor(channels): finish setup ownership migration

* fix(channels): harden management contracts

* fix(channels): enforce lazy loading and runtime ownership

* fix(feishu): make multi-instance startup idempotent

* fix(webui): render channel setup contracts cleanly

* fix(feishu): stop websocket clients cleanly

* fix(channels): enforce persistence and activation gates

* fix(channels): preserve global feature action scope

* fix(channels): apply defaults for single plugins

* fix(channels): enforce management contract boundaries

* refactor(feishu): remove identity helper indirection

* fix(channels): preserve management setup contracts

* refactor(channels): generalize instance settings UI

* refactor(channels): package channel plugins with web UI metadata

* refactor(channels): make built-ins self-contained packages

* test(channels): colocate tests with channel packages

* fix(dingtalk): use official brand icon

* feat(channels): colocate webui translations

* docs(channels): clarify plugin ownership

* test(exec): remove output wait race

* refactor(channels): unify plugin descriptors

* fix(channels): enforce descriptor-owned contracts

* refactor(channels): finish package-owned plugin setup

* refactor(channels): use repository-owned packages only

* fix(channels): self-describe dependencies and runtime state

* fix(channels): warn about legacy entry points
2026-07-19 23:30:49 +08:00

84 lines
2.8 KiB
TypeScript

import type {
ChannelUiContribution,
RegisteredChannelUiContribution,
} from "@/channel-plugins/types";
type ChannelUiContributionModule = {
default?: ChannelUiContribution;
};
const modules = import.meta.glob<ChannelUiContributionModule>(
"../../../nanobot/channels/*/webui/**/*.{ts,tsx}",
{
eager: true,
},
);
const registrations = new Map<string, RegisteredChannelUiContribution>();
const registrationsByChannel = new Map<string, RegisteredChannelUiContribution>();
const presentationsByChannel = new Map<string, ChannelUiContribution["presentation"]>();
const translationOwners = new Map<string, string>();
for (const [modulePath, module] of Object.entries(modules)) {
const contribution = module.default;
if (!contribution) continue;
const match = modulePath.match(/nanobot\/channels\/([^/]+)\/(.+)$/);
if (!match) {
throw new Error(`Cannot derive channel UI identity from '${modulePath}'`);
}
const [, channel, webui] = match;
const registration = { channel, webui, contribution };
if (registrationsByChannel.has(channel)) {
throw new Error(`Channel '${channel}' has more than one UI contribution`);
}
registrations.set(registrationKey(channel, webui), registration);
registrationsByChannel.set(channel, registration);
presentationsByChannel.set(channel, contribution.presentation);
translationOwners.set(channel, channel);
for (const [alias, aliasPresentation] of Object.entries(contribution.aliases ?? {})) {
if (presentationsByChannel.has(alias)) {
throw new Error(`Channel UI alias '${alias}' is registered more than once`);
}
presentationsByChannel.set(alias, {
...contribution.presentation,
...aliasPresentation,
});
translationOwners.set(alias, channel);
}
}
export function channelUiContribution(
channel: string,
webui: string | undefined,
): ChannelUiContribution | undefined {
if (!webui) return undefined;
return registrations.get(registrationKey(channel, webui))?.contribution;
}
export function registeredChannelUiContributions(): readonly RegisteredChannelUiContribution[] {
return [...registrations.values()];
}
export function channelUiOwner(channel: string): string {
return translationOwners.get(channel) ?? channel;
}
export function channelUiPresentation(
channel: string,
): ChannelUiContribution["presentation"] | undefined;
export function channelUiPresentation(
channel: string,
webui: string | undefined,
): ChannelUiContribution["presentation"] | undefined;
export function channelUiPresentation(
channel: string,
webui?: string,
): ChannelUiContribution["presentation"] | undefined {
if (arguments.length > 1) return channelUiContribution(channel, webui)?.presentation;
return presentationsByChannel.get(channel);
}
function registrationKey(channel: string, webui: string): string {
return `${channel}:${webui.replaceAll("\\", "/")}`;
}