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
This commit is contained in:
chengyongru
2026-07-19 23:30:49 +08:00
committed by GitHub
parent 7aaac37bca
commit 462a0dfb0f
388 changed files with 17093 additions and 5110 deletions
+51
View File
@@ -0,0 +1,51 @@
import type { TFunction } from "i18next";
export type ChannelFieldMessages = {
label: string;
placeholder?: string;
help?: string;
choices?: Record<string, string>;
};
export type ChannelMessages = {
displayName?: string;
description: string;
requirements: string;
setup: {
primaryAction?: string;
docsLabel?: string;
officialLabel?: string;
summary?: string;
tryIt?: string;
steps: string[];
fields?: Record<string, ChannelFieldMessages>;
actions?: Record<string, string>;
presets?: Record<string, string>;
};
custom?: Record<string, string>;
};
export type ChannelTranslator = (
key: string,
fallback: string,
values?: Record<string, unknown>,
) => string;
export function channelNamespace(channel: string): string {
return `channel-${channel}`;
}
export function channelTranslator(t: TFunction, channel: string): ChannelTranslator {
const namespace = channelNamespace(channel);
return (key, fallback, values = {}) => t(key, {
ns: namespace,
defaultValue: fallback,
...values,
});
}
export function channelFieldMessageKey(channel: string, configKey: string): string {
const prefix = `channels.${channel}.`;
const field = configKey.startsWith(prefix) ? configKey.slice(prefix.length) : configKey;
return field.replace(/[^A-Za-z0-9_-]+/g, "_");
}
@@ -0,0 +1,65 @@
import type { ChannelMessages } from "@/channel-plugins/i18n";
import { channelNamespace } from "@/channel-plugins/i18n";
import {
supportedLocales,
type SupportedLocale,
} from "@/i18n/config";
type ChannelMessagesModule = {
default?: ChannelMessages;
};
const modules = import.meta.glob<ChannelMessagesModule>(
"../../../nanobot/channels/*/webui/locales/*.json",
{ eager: true },
);
const translationsByChannel = new Map<string, Map<SupportedLocale, ChannelMessages>>();
const supportedLocaleCodes = new Set<string>(supportedLocales.map(({ code }) => code));
for (const [modulePath, module] of Object.entries(modules)) {
const messages = module.default;
if (!messages) continue;
const match = modulePath.match(/nanobot\/channels\/([^/]+)\/webui\/locales\/([^/]+)\.json$/);
if (!match) {
throw new Error(`Cannot derive channel locale identity from '${modulePath}'`);
}
const [, channel, locale] = match;
if (!supportedLocaleCodes.has(locale)) {
throw new Error(`Channel '${channel}' has unsupported locale '${locale}'`);
}
const translations = translationsByChannel.get(channel) ?? new Map();
if (translations.has(locale as SupportedLocale)) {
throw new Error(`Channel '${channel}' registers locale '${locale}' more than once`);
}
translations.set(locale as SupportedLocale, messages);
translationsByChannel.set(channel, translations);
}
export function channelLocaleNamespaces(): string[] {
return [...translationsByChannel.keys()].map(channelNamespace);
}
export function channelLocaleResources(locale: SupportedLocale): Record<string, unknown> {
return Object.fromEntries(
[...translationsByChannel.keys()].map((channel) => [
channelNamespace(channel),
channelLocaleMessages(channel, locale) ?? {},
]),
);
}
export function channelLocaleMessages(
channel: string,
locale: SupportedLocale,
): ChannelMessages | undefined {
const translations = translationsByChannel.get(channel);
return translations?.get(locale) ?? translations?.get("en");
}
export function registeredChannelLocales(): ReadonlyMap<
string,
ReadonlyMap<SupportedLocale, ChannelMessages>
> {
return translationsByChannel;
}
+83
View File
@@ -0,0 +1,83 @@
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("\\", "/")}`;
}
+39
View File
@@ -0,0 +1,39 @@
import type { ComponentType } from "react";
import type { ChannelPresentation } from "@/components/settings/channels/catalog";
import type {
NanobotFeatureInfo,
NanobotFeaturesPayload,
} from "@/lib/types";
export type ChannelPluginPanelProps = {
token: string;
feature: NanobotFeatureInfo;
actionKey: string | null;
chatAppsDocsUrl?: string;
showBrandLogos: boolean;
onAction: (action: "enable" | "disable", name: string) => void;
onFeaturesUpdate: (payload: NanobotFeaturesPayload) => void;
};
export type ChannelPluginConnectFlowProps = {
token: string;
feature: NanobotFeatureInfo;
idleLabel?: string;
connectRequestId?: number;
onFeaturesUpdate: (payload: NanobotFeaturesPayload) => void;
};
export type ChannelUiContribution = {
presentation: ChannelPresentation;
aliases?: Record<string, Partial<ChannelPresentation>>;
Panel?: ComponentType<ChannelPluginPanelProps>;
ConnectFlow?: ComponentType<ChannelPluginConnectFlowProps>;
canConnectBeforeConfigured?: boolean;
};
export type RegisteredChannelUiContribution = {
channel: string;
webui: string;
contribution: ChannelUiContribution;
};