mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 11:08:37 +00:00
refactor(channels): share account summary helpers
This commit is contained in:
36
src/channels/account-summary.ts
Normal file
36
src/channels/account-summary.ts
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
|
import type { ChannelAccountSnapshot } from "./plugins/types.core.js";
|
||||||
|
import type { ChannelPlugin } from "./plugins/types.plugin.js";
|
||||||
|
|
||||||
|
export function buildChannelAccountSnapshot(params: {
|
||||||
|
plugin: ChannelPlugin;
|
||||||
|
account: unknown;
|
||||||
|
cfg: OpenClawConfig;
|
||||||
|
accountId: string;
|
||||||
|
enabled: boolean;
|
||||||
|
configured: boolean;
|
||||||
|
}): ChannelAccountSnapshot {
|
||||||
|
const described = params.plugin.config.describeAccount?.(params.account, params.cfg);
|
||||||
|
return {
|
||||||
|
enabled: params.enabled,
|
||||||
|
configured: params.configured,
|
||||||
|
...described,
|
||||||
|
accountId: params.accountId,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatChannelAllowFrom(params: {
|
||||||
|
plugin: ChannelPlugin;
|
||||||
|
cfg: OpenClawConfig;
|
||||||
|
accountId?: string | null;
|
||||||
|
allowFrom: Array<string | number>;
|
||||||
|
}): string[] {
|
||||||
|
if (params.plugin.config.formatAllowFrom) {
|
||||||
|
return params.plugin.config.formatAllowFrom({
|
||||||
|
cfg: params.cfg,
|
||||||
|
accountId: params.accountId,
|
||||||
|
allowFrom: params.allowFrom,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return params.allowFrom.map((entry) => String(entry).trim()).filter(Boolean);
|
||||||
|
}
|
||||||
@@ -5,6 +5,10 @@ import type {
|
|||||||
ChannelPlugin,
|
ChannelPlugin,
|
||||||
} from "../../channels/plugins/types.js";
|
} from "../../channels/plugins/types.js";
|
||||||
import type { OpenClawConfig } from "../../config/config.js";
|
import type { OpenClawConfig } from "../../config/config.js";
|
||||||
|
import {
|
||||||
|
buildChannelAccountSnapshot,
|
||||||
|
formatChannelAllowFrom,
|
||||||
|
} from "../../channels/account-summary.js";
|
||||||
import { resolveChannelDefaultAccountId } from "../../channels/plugins/helpers.js";
|
import { resolveChannelDefaultAccountId } from "../../channels/plugins/helpers.js";
|
||||||
import { listChannelPlugins } from "../../channels/plugins/index.js";
|
import { listChannelPlugins } from "../../channels/plugins/index.js";
|
||||||
import { sha256HexPrefix } from "../../logging/redact-identifier.js";
|
import { sha256HexPrefix } from "../../logging/redact-identifier.js";
|
||||||
@@ -105,39 +109,6 @@ const resolveAccountConfigured = async (
|
|||||||
return configured !== false;
|
return configured !== false;
|
||||||
};
|
};
|
||||||
|
|
||||||
const buildAccountSnapshot = (params: {
|
|
||||||
plugin: ChannelPlugin;
|
|
||||||
account: unknown;
|
|
||||||
cfg: OpenClawConfig;
|
|
||||||
accountId: string;
|
|
||||||
enabled: boolean;
|
|
||||||
configured: boolean;
|
|
||||||
}): ChannelAccountSnapshot => {
|
|
||||||
const described = params.plugin.config.describeAccount?.(params.account, params.cfg);
|
|
||||||
return {
|
|
||||||
enabled: params.enabled,
|
|
||||||
configured: params.configured,
|
|
||||||
...described,
|
|
||||||
accountId: params.accountId,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
const formatAllowFrom = (params: {
|
|
||||||
plugin: ChannelPlugin;
|
|
||||||
cfg: OpenClawConfig;
|
|
||||||
accountId?: string | null;
|
|
||||||
allowFrom: Array<string | number>;
|
|
||||||
}) => {
|
|
||||||
if (params.plugin.config.formatAllowFrom) {
|
|
||||||
return params.plugin.config.formatAllowFrom({
|
|
||||||
cfg: params.cfg,
|
|
||||||
accountId: params.accountId,
|
|
||||||
allowFrom: params.allowFrom,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return params.allowFrom.map((entry) => String(entry).trim()).filter(Boolean);
|
|
||||||
};
|
|
||||||
|
|
||||||
const buildAccountNotes = (params: {
|
const buildAccountNotes = (params: {
|
||||||
plugin: ChannelPlugin;
|
plugin: ChannelPlugin;
|
||||||
cfg: OpenClawConfig;
|
cfg: OpenClawConfig;
|
||||||
@@ -177,7 +148,7 @@ const buildAccountNotes = (params: {
|
|||||||
const allowFrom =
|
const allowFrom =
|
||||||
plugin.config.resolveAllowFrom?.({ cfg, accountId: snapshot.accountId }) ?? snapshot.allowFrom;
|
plugin.config.resolveAllowFrom?.({ cfg, accountId: snapshot.accountId }) ?? snapshot.allowFrom;
|
||||||
if (allowFrom?.length) {
|
if (allowFrom?.length) {
|
||||||
const formatted = formatAllowFrom({
|
const formatted = formatChannelAllowFrom({
|
||||||
plugin,
|
plugin,
|
||||||
cfg,
|
cfg,
|
||||||
accountId: snapshot.accountId,
|
accountId: snapshot.accountId,
|
||||||
@@ -349,7 +320,7 @@ export async function buildChannelsTable(
|
|||||||
const account = plugin.config.resolveAccount(cfg, accountId);
|
const account = plugin.config.resolveAccount(cfg, accountId);
|
||||||
const enabled = resolveAccountEnabled(plugin, account, cfg);
|
const enabled = resolveAccountEnabled(plugin, account, cfg);
|
||||||
const configured = await resolveAccountConfigured(plugin, account, cfg);
|
const configured = await resolveAccountConfigured(plugin, account, cfg);
|
||||||
const snapshot = buildAccountSnapshot({
|
const snapshot = buildChannelAccountSnapshot({
|
||||||
plugin,
|
plugin,
|
||||||
cfg,
|
cfg,
|
||||||
accountId,
|
accountId,
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
import type { ChannelAccountSnapshot, ChannelPlugin } from "../channels/plugins/types.js";
|
import type { ChannelAccountSnapshot, ChannelPlugin } from "../channels/plugins/types.js";
|
||||||
|
import {
|
||||||
|
buildChannelAccountSnapshot,
|
||||||
|
formatChannelAllowFrom,
|
||||||
|
} from "../channels/account-summary.js";
|
||||||
import { listChannelPlugins } from "../channels/plugins/index.js";
|
import { listChannelPlugins } from "../channels/plugins/index.js";
|
||||||
import { type OpenClawConfig, loadConfig } from "../config/config.js";
|
import { type OpenClawConfig, loadConfig } from "../config/config.js";
|
||||||
import { DEFAULT_ACCOUNT_ID } from "../routing/session-key.js";
|
import { DEFAULT_ACCOUNT_ID } from "../routing/session-key.js";
|
||||||
@@ -60,41 +64,6 @@ const resolveAccountConfigured = async (
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
const buildAccountSnapshot = (params: {
|
|
||||||
plugin: ChannelPlugin;
|
|
||||||
account: unknown;
|
|
||||||
cfg: OpenClawConfig;
|
|
||||||
accountId: string;
|
|
||||||
enabled: boolean;
|
|
||||||
configured: boolean;
|
|
||||||
}): ChannelAccountSnapshot => {
|
|
||||||
const described = params.plugin.config.describeAccount
|
|
||||||
? params.plugin.config.describeAccount(params.account, params.cfg)
|
|
||||||
: undefined;
|
|
||||||
return {
|
|
||||||
enabled: params.enabled,
|
|
||||||
configured: params.configured,
|
|
||||||
...described,
|
|
||||||
accountId: params.accountId,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
const formatAllowFrom = (params: {
|
|
||||||
plugin: ChannelPlugin;
|
|
||||||
cfg: OpenClawConfig;
|
|
||||||
accountId?: string | null;
|
|
||||||
allowFrom: Array<string | number>;
|
|
||||||
}) => {
|
|
||||||
if (params.plugin.config.formatAllowFrom) {
|
|
||||||
return params.plugin.config.formatAllowFrom({
|
|
||||||
cfg: params.cfg,
|
|
||||||
accountId: params.accountId,
|
|
||||||
allowFrom: params.allowFrom,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return params.allowFrom.map((entry) => String(entry).trim()).filter(Boolean);
|
|
||||||
};
|
|
||||||
|
|
||||||
const buildAccountDetails = (params: {
|
const buildAccountDetails = (params: {
|
||||||
entry: ChannelAccountEntry;
|
entry: ChannelAccountEntry;
|
||||||
plugin: ChannelPlugin;
|
plugin: ChannelPlugin;
|
||||||
@@ -132,7 +101,7 @@ const buildAccountDetails = (params: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (params.includeAllowFrom && snapshot.allowFrom?.length) {
|
if (params.includeAllowFrom && snapshot.allowFrom?.length) {
|
||||||
const formatted = formatAllowFrom({
|
const formatted = formatChannelAllowFrom({
|
||||||
plugin: params.plugin,
|
plugin: params.plugin,
|
||||||
cfg: params.cfg,
|
cfg: params.cfg,
|
||||||
accountId: snapshot.accountId,
|
accountId: snapshot.accountId,
|
||||||
@@ -166,7 +135,7 @@ export async function buildChannelSummary(
|
|||||||
const account = plugin.config.resolveAccount(effective, accountId);
|
const account = plugin.config.resolveAccount(effective, accountId);
|
||||||
const enabled = resolveAccountEnabled(plugin, account, effective);
|
const enabled = resolveAccountEnabled(plugin, account, effective);
|
||||||
const configured = await resolveAccountConfigured(plugin, account, effective);
|
const configured = await resolveAccountConfigured(plugin, account, effective);
|
||||||
const snapshot = buildAccountSnapshot({
|
const snapshot = buildChannelAccountSnapshot({
|
||||||
plugin,
|
plugin,
|
||||||
account,
|
account,
|
||||||
cfg: effective,
|
cfg: effective,
|
||||||
|
|||||||
Reference in New Issue
Block a user