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 authored and GitHub committed 2026-07-19 23:30:49 +08:00
1 parent 7aaac37bca
commit 462a0dfb0f
388 files changed
+17093 -5110

No files matched your search

@@ -2,25 +2,121 @@ import { useMemo, type ReactNode } from "react";
import type { useTranslation } from "react-i18next";
import {
CHANNEL_PRESENTATION,
type ChannelSetupPresentation,
channelFieldMessageKey,
channelTranslator,
} from "@/channel-plugins/i18n";
import { channelLocaleMessages } from "@/channel-plugins/locale-registry";
import {
channelUiOwner,
channelUiPresentation,
} from "@/channel-plugins/registry";
import type {
ChannelConfigField,
ChannelSetupPresentation,
} from "@/components/settings/channels/catalog";
import { useLogoFallback } from "@/hooks/useLogoFallback";
import { normalizeLocale } from "@/i18n/config";
import { logoFallbackUrls } from "@/lib/provider-brand";
import type { NanobotFeatureInfo } from "@/lib/types";
import type { ChannelRuntimeStatus, NanobotFeatureInfo } from "@/lib/types";
export type ChannelFilter = "all" | "on" | "off";
export function channelSetup(feature: NanobotFeatureInfo): ChannelSetupPresentation {
return CHANNEL_PRESENTATION[feature.name]?.setup ?? {
export function channelSetup(
feature: NanobotFeatureInfo,
locale = "en",
): ChannelSetupPresentation {
const definition = channelUiPresentation(feature.name, feature.webui)?.setup;
const owner = channelUiOwner(feature.name);
const messages = channelLocaleMessages(owner, normalizeLocale(locale));
const setupMessages = messages?.setup;
const localizeField = (key: string): ChannelConfigField => {
const copy = setupMessages?.fields?.[channelFieldMessageKey(feature.name, key)];
return {
key,
label: copy?.label ?? fieldLabel(key.split(".").at(-1) ?? key),
placeholder: copy?.placeholder,
help: copy?.help,
};
};
const presentation: ChannelSetupPresentation = {
...definition,
primaryActionLabel: setupMessages?.primaryAction,
docsLabel: setupMessages?.docsLabel,
officialLabel: setupMessages?.officialLabel,
summary:
"Enable turns on this channel in nanobot, but this integration still needs platform-specific setup before it can receive messages.",
steps: [
setupMessages?.summary
?? "Enable turns on this channel in nanobot, but this integration still needs platform-specific setup before it can receive messages.",
tryIt: setupMessages?.tryIt,
steps: setupMessages?.steps ?? [
`Open ~/.nanobot/config.json and find channels.${feature.name}.`,
"Add the credentials required by that platform, using the channel documentation as the source of truth.",
"Restart nanobot, then send a small test message from that platform.",
],
fields: definition?.fields?.map((field) => localizeField(field.key)),
manualFields: definition?.manualFields?.map((field) => localizeField(field.key)),
actions: definition?.actions?.map((action) => ({
...action,
label: setupMessages?.actions?.[action.id] ?? fieldLabel(action.id),
})),
presets: definition?.presets?.map((preset) => ({
...preset,
label: setupMessages?.presets?.[preset.id] ?? fieldLabel(preset.id),
})),
};
const contract = feature.setup;
if (!contract) return presentation;
const primaryFields = new Map(
(presentation.fields ?? []).map((field) => [field.key, field]),
);
const manualFields = new Map(
(presentation.manualFields ?? []).map((field) => [field.key, field]),
);
const authoritativeFields = contract.fields.map((field): ChannelConfigField => {
const local = primaryFields.get(field.key) ?? manualFields.get(field.key);
const copy = local ?? localizeField(field.key);
const choiceLabels = setupMessages?.fields?.[
channelFieldMessageKey(feature.name, field.key)
]?.choices ?? {};
const choices = field.kind === "bool" ? ["true", "false"] : field.choices;
return {
...copy,
key: field.key,
label: copy.label,
secret: field.kind === "secret",
optional: !field.required,
inputType: field.kind === "int" ? "number" : undefined,
defaultValue: field.default_value,
options:
field.kind === "enum" || field.kind === "bool"
? choices.map((choice) => ({
value: choice,
label: choiceLabels[choice] ?? fieldLabel(choice),
}))
: undefined,
};
});
const manualKeys = new Set(manualFields.keys());
const fields = authoritativeFields.filter((field) => !manualKeys.has(field.key));
const manual = authoritativeFields.filter((field) => manualKeys.has(field.key));
return {
...presentation,
officialUrl: contract.official_url,
officialLabel:
presentation.officialLabel
?? (contract.official_url ? "Open official setup" : undefined),
fields: fields.length ? fields : undefined,
manualFields: manual.length ? manual : undefined,
};
}
function fieldLabel(value: string): string {
const spaced = value
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
.replace(/[_-]+/g, " ")
.trim();
return spaced ? spaced[0].toUpperCase() + spaced.slice(1) : value;
}
export function ChannelLogo({
@@ -30,7 +126,7 @@ export function ChannelLogo({
feature: NanobotFeatureInfo;
showBrandLogos: boolean;
}) {
const presentation = CHANNEL_PRESENTATION[feature.name];
const presentation = channelUiPresentation(feature.name, feature.webui);
const initials = presentation?.initials ?? feature.display_name.slice(0, 2).toUpperCase();
const color = presentation?.color ?? "#6B7280";
const Icon = presentation?.icon;
@@ -80,55 +176,108 @@ export function ChannelLogo({
}
export function channelDisplayName(feature: NanobotFeatureInfo): string {
return CHANNEL_PRESENTATION[feature.name]?.displayName ?? feature.display_name;
return channelUiPresentation(feature.name, feature.webui)?.displayName ?? feature.display_name;
}
export function localizedChannelDisplayName(
feature: NanobotFeatureInfo,
t: ReturnType<typeof useTranslation>["t"],
): string {
const fallback = channelDisplayName(feature);
return channelTranslator(t, channelUiOwner(feature.name))("displayName", fallback);
}
export function channelDescription(feature: NanobotFeatureInfo, t: ReturnType<typeof useTranslation>["t"]): string {
const fallback =
CHANNEL_PRESENTATION[feature.name]?.description ??
`Use nanobot from ${channelDisplayName(feature)}.`;
return t(`settings.channels.items.${feature.name}.description`, { defaultValue: fallback });
return channelTranslator(t, channelUiOwner(feature.name))("description", fallback);
}
export function channelRequirements(feature: NanobotFeatureInfo, t: ReturnType<typeof useTranslation>["t"]): string {
const fallback =
CHANNEL_PRESENTATION[feature.name]?.requirements ??
"Channel credentials and gateway settings";
return t(`settings.channels.items.${feature.name}.requirements`, { defaultValue: fallback });
return channelTranslator(t, channelUiOwner(feature.name))("requirements", fallback);
}
export function channelMatchesFilter(feature: NanobotFeatureInfo, filter: ChannelFilter): boolean {
if (filter === "on") return feature.enabled;
if (filter === "off") return !feature.enabled;
if (filter === "on") return channelIsRunning(feature);
if (filter === "off") return !channelIsRunning(feature);
return true;
}
export function channelIsRunning(feature: NanobotFeatureInfo): boolean {
return feature.runtime_status === "running";
}
export function channelToggleChecked(feature: NanobotFeatureInfo): boolean {
return feature.runtime_status === "running" || feature.runtime_status === "starting";
}
export function channelStatusLabel(
feature: NanobotFeatureInfo,
tx: (key: string, fallback: string) => string,
): string {
if (feature.enabled) return tx("settings.values.on", "On");
if (feature.runtime_status === "failed") {
return tx("settings.channels.runtimeFailed", "Failed");
}
if (feature.runtime_status === "starting") {
return tx("settings.channels.runtimeStarting", "Starting");
}
if (channelIsRunning(feature)) return tx("settings.values.on", "On");
if (feature.enabled) return tx("settings.channels.runtimeStopped", "Not running");
return tx("settings.values.off", "Off");
}
export function channelSearchText(feature: NanobotFeatureInfo): string {
export function channelSearchText(
feature: NanobotFeatureInfo,
t?: ReturnType<typeof useTranslation>["t"],
): string {
return [
t ? localizedChannelDisplayName(feature, t) : undefined,
channelDisplayName(feature),
feature.display_name,
feature.name,
feature.status,
CHANNEL_PRESENTATION[feature.name]?.description,
CHANNEL_PRESENTATION[feature.name]?.requirements,
t ? channelDescription(feature, t) : undefined,
t ? channelRequirements(feature, t) : undefined,
]
.join(" ")
.toLowerCase();
}
export function ChannelStatusBadge({ children }: { children: ReactNode }) {
export function ChannelStatusBadge({
children,
status,
}: {
children: ReactNode;
status?: ChannelRuntimeStatus;
}) {
return (
<span className="shrink-0 rounded-full bg-muted/75 px-2 py-0.5 text-[11px] font-medium leading-4 text-muted-foreground">
<span className={[
"shrink-0 rounded-full px-2 py-0.5 text-[11px] font-medium leading-4",
status === "failed"
? "bg-destructive/10 text-destructive"
: status === "running"
? "bg-emerald-500/10 text-emerald-700 dark:text-emerald-200"
: "bg-muted/75 text-muted-foreground",
].join(" ")}>
{children}
</span>
);
}
export function ChannelRuntimeError({
message,
className = "mt-3",
}: {
message?: string;
className?: string;
}) {
if (!message) return null;
return (
<div className={`${className} rounded-[12px] border border-destructive/20 bg-destructive/5 px-3 py-2 text-[12px] leading-5 text-destructive`}>
{message}
</div>
);
}