mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-29 10:02:04 +00:00
refactor: move remaining channel seams into plugins
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import type { ChannelDock } from "../channels/dock.js";
|
||||
import { getChannelDock, listChannelDocks } from "../channels/dock.js";
|
||||
import type { ChannelId } from "../channels/plugins/types.js";
|
||||
import { getChannelPlugin, listChannelPlugins } from "../channels/plugins/index.js";
|
||||
import type { ChannelId, ChannelPlugin } from "../channels/plugins/types.js";
|
||||
import { normalizeAnyChannelId } from "../channels/registry.js";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import { normalizeStringEntries } from "../shared/string-normalization.js";
|
||||
@@ -52,19 +51,19 @@ function resolveProviderFromContext(ctx: MsgContext, cfg: OpenClawConfig): Chann
|
||||
return normalized;
|
||||
}
|
||||
}
|
||||
const configured = listChannelDocks()
|
||||
.map((dock) => {
|
||||
if (!dock.config?.resolveAllowFrom) {
|
||||
const configured = listChannelPlugins()
|
||||
.map((plugin) => {
|
||||
if (!plugin.config?.resolveAllowFrom) {
|
||||
return null;
|
||||
}
|
||||
const allowFrom = dock.config.resolveAllowFrom({
|
||||
const allowFrom = plugin.config.resolveAllowFrom({
|
||||
cfg,
|
||||
accountId: ctx.AccountId,
|
||||
});
|
||||
if (!Array.isArray(allowFrom) || allowFrom.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return dock.id;
|
||||
return plugin.id;
|
||||
})
|
||||
.filter((value): value is ChannelId => Boolean(value));
|
||||
if (configured.length === 1) {
|
||||
@@ -74,29 +73,29 @@ function resolveProviderFromContext(ctx: MsgContext, cfg: OpenClawConfig): Chann
|
||||
}
|
||||
|
||||
function formatAllowFromList(params: {
|
||||
dock?: ChannelDock;
|
||||
plugin?: ChannelPlugin;
|
||||
cfg: OpenClawConfig;
|
||||
accountId?: string | null;
|
||||
allowFrom: Array<string | number>;
|
||||
}): string[] {
|
||||
const { dock, cfg, accountId, allowFrom } = params;
|
||||
const { plugin, cfg, accountId, allowFrom } = params;
|
||||
if (!allowFrom || allowFrom.length === 0) {
|
||||
return [];
|
||||
}
|
||||
if (dock?.config?.formatAllowFrom) {
|
||||
return dock.config.formatAllowFrom({ cfg, accountId, allowFrom });
|
||||
if (plugin?.config?.formatAllowFrom) {
|
||||
return plugin.config.formatAllowFrom({ cfg, accountId, allowFrom });
|
||||
}
|
||||
return normalizeStringEntries(allowFrom);
|
||||
}
|
||||
|
||||
function normalizeAllowFromEntry(params: {
|
||||
dock?: ChannelDock;
|
||||
plugin?: ChannelPlugin;
|
||||
cfg: OpenClawConfig;
|
||||
accountId?: string | null;
|
||||
value: string;
|
||||
}): string[] {
|
||||
const normalized = formatAllowFromList({
|
||||
dock: params.dock,
|
||||
plugin: params.plugin,
|
||||
cfg: params.cfg,
|
||||
accountId: params.accountId,
|
||||
allowFrom: [params.value],
|
||||
@@ -105,7 +104,7 @@ function normalizeAllowFromEntry(params: {
|
||||
}
|
||||
|
||||
function resolveOwnerAllowFromList(params: {
|
||||
dock?: ChannelDock;
|
||||
plugin?: ChannelPlugin;
|
||||
cfg: OpenClawConfig;
|
||||
accountId?: string | null;
|
||||
providerId?: ChannelId;
|
||||
@@ -139,7 +138,7 @@ function resolveOwnerAllowFromList(params: {
|
||||
filtered.push(trimmed);
|
||||
}
|
||||
return formatAllowFromList({
|
||||
dock: params.dock,
|
||||
plugin: params.plugin,
|
||||
cfg: params.cfg,
|
||||
accountId: params.accountId,
|
||||
allowFrom: filtered,
|
||||
@@ -152,12 +151,12 @@ function resolveOwnerAllowFromList(params: {
|
||||
* Returns null if commands.allowFrom is not configured at all (fall back to channel allowFrom).
|
||||
*/
|
||||
function resolveCommandsAllowFromList(params: {
|
||||
dock?: ChannelDock;
|
||||
plugin?: ChannelPlugin;
|
||||
cfg: OpenClawConfig;
|
||||
accountId?: string | null;
|
||||
providerId?: ChannelId;
|
||||
}): string[] | null {
|
||||
const { dock, cfg, accountId, providerId } = params;
|
||||
const { plugin, cfg, accountId, providerId } = params;
|
||||
const commandsAllowFrom = cfg.commands?.allowFrom;
|
||||
if (!commandsAllowFrom || typeof commandsAllowFrom !== "object") {
|
||||
return null; // Not configured, fall back to channel allowFrom
|
||||
@@ -174,7 +173,7 @@ function resolveCommandsAllowFromList(params: {
|
||||
}
|
||||
|
||||
return formatAllowFromList({
|
||||
dock,
|
||||
plugin,
|
||||
cfg,
|
||||
accountId,
|
||||
allowFrom: rawList,
|
||||
@@ -211,7 +210,7 @@ function shouldUseFromAsSenderFallback(params: {
|
||||
}
|
||||
|
||||
function resolveSenderCandidates(params: {
|
||||
dock?: ChannelDock;
|
||||
plugin?: ChannelPlugin;
|
||||
providerId?: ChannelId;
|
||||
cfg: OpenClawConfig;
|
||||
accountId?: string | null;
|
||||
@@ -220,7 +219,7 @@ function resolveSenderCandidates(params: {
|
||||
from?: string | null;
|
||||
chatType?: string | null;
|
||||
}): string[] {
|
||||
const { dock, cfg, accountId } = params;
|
||||
const { plugin, cfg, accountId } = params;
|
||||
const candidates: string[] = [];
|
||||
const pushCandidate = (value?: string | null) => {
|
||||
const trimmed = (value ?? "").trim();
|
||||
@@ -245,7 +244,7 @@ function resolveSenderCandidates(params: {
|
||||
|
||||
const normalized: string[] = [];
|
||||
for (const sender of candidates) {
|
||||
const entries = normalizeAllowFromEntry({ dock, cfg, accountId, value: sender });
|
||||
const entries = normalizeAllowFromEntry({ plugin, cfg, accountId, value: sender });
|
||||
for (const entry of entries) {
|
||||
if (!normalized.includes(entry)) {
|
||||
normalized.push(entry);
|
||||
@@ -262,36 +261,36 @@ export function resolveCommandAuthorization(params: {
|
||||
}): CommandAuthorization {
|
||||
const { ctx, cfg, commandAuthorized } = params;
|
||||
const providerId = resolveProviderFromContext(ctx, cfg);
|
||||
const dock = providerId ? getChannelDock(providerId) : undefined;
|
||||
const plugin = providerId ? getChannelPlugin(providerId) : undefined;
|
||||
const from = (ctx.From ?? "").trim();
|
||||
const to = (ctx.To ?? "").trim();
|
||||
|
||||
// Check if commands.allowFrom is configured (separate command authorization)
|
||||
const commandsAllowFromList = resolveCommandsAllowFromList({
|
||||
dock,
|
||||
plugin,
|
||||
cfg,
|
||||
accountId: ctx.AccountId,
|
||||
providerId,
|
||||
});
|
||||
|
||||
const allowFromRaw = dock?.config?.resolveAllowFrom
|
||||
? dock.config.resolveAllowFrom({ cfg, accountId: ctx.AccountId })
|
||||
const allowFromRaw = plugin?.config?.resolveAllowFrom
|
||||
? plugin.config.resolveAllowFrom({ cfg, accountId: ctx.AccountId })
|
||||
: [];
|
||||
const allowFromList = formatAllowFromList({
|
||||
dock,
|
||||
plugin,
|
||||
cfg,
|
||||
accountId: ctx.AccountId,
|
||||
allowFrom: Array.isArray(allowFromRaw) ? allowFromRaw : [],
|
||||
});
|
||||
const configOwnerAllowFromList = resolveOwnerAllowFromList({
|
||||
dock,
|
||||
plugin,
|
||||
cfg,
|
||||
accountId: ctx.AccountId,
|
||||
providerId,
|
||||
allowFrom: cfg.commands?.ownerAllowFrom,
|
||||
});
|
||||
const contextOwnerAllowFromList = resolveOwnerAllowFromList({
|
||||
dock,
|
||||
plugin,
|
||||
cfg,
|
||||
accountId: ctx.AccountId,
|
||||
providerId,
|
||||
@@ -303,7 +302,7 @@ export function resolveCommandAuthorization(params: {
|
||||
const ownerCandidatesForCommands = allowAll ? [] : allowFromList.filter((entry) => entry !== "*");
|
||||
if (!allowAll && ownerCandidatesForCommands.length === 0 && to) {
|
||||
const normalizedTo = normalizeAllowFromEntry({
|
||||
dock,
|
||||
plugin,
|
||||
cfg,
|
||||
accountId: ctx.AccountId,
|
||||
value: to,
|
||||
@@ -328,7 +327,7 @@ export function resolveCommandAuthorization(params: {
|
||||
);
|
||||
|
||||
const senderCandidates = resolveSenderCandidates({
|
||||
dock,
|
||||
plugin,
|
||||
providerId,
|
||||
cfg,
|
||||
accountId: ctx.AccountId,
|
||||
@@ -345,7 +344,7 @@ export function resolveCommandAuthorization(params: {
|
||||
: undefined;
|
||||
const senderId = matchedSender ?? senderCandidates[0];
|
||||
|
||||
const enforceOwner = Boolean(dock?.commands?.enforceOwnerForCommands);
|
||||
const enforceOwner = Boolean(plugin?.commands?.enforceOwnerForCommands);
|
||||
const senderIsOwnerByIdentity = Boolean(matchedSender);
|
||||
const senderIsOwnerByScope =
|
||||
isInternalMessageChannel(ctx.Provider) &&
|
||||
|
||||
Reference in New Issue
Block a user