mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05: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,
|
||||
} from "../../channels/plugins/types.js";
|
||||
import type { OpenClawConfig } from "../../config/config.js";
|
||||
import {
|
||||
buildChannelAccountSnapshot,
|
||||
formatChannelAllowFrom,
|
||||
} from "../../channels/account-summary.js";
|
||||
import { resolveChannelDefaultAccountId } from "../../channels/plugins/helpers.js";
|
||||
import { listChannelPlugins } from "../../channels/plugins/index.js";
|
||||
import { sha256HexPrefix } from "../../logging/redact-identifier.js";
|
||||
@@ -105,39 +109,6 @@ const resolveAccountConfigured = async (
|
||||
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: {
|
||||
plugin: ChannelPlugin;
|
||||
cfg: OpenClawConfig;
|
||||
@@ -177,7 +148,7 @@ const buildAccountNotes = (params: {
|
||||
const allowFrom =
|
||||
plugin.config.resolveAllowFrom?.({ cfg, accountId: snapshot.accountId }) ?? snapshot.allowFrom;
|
||||
if (allowFrom?.length) {
|
||||
const formatted = formatAllowFrom({
|
||||
const formatted = formatChannelAllowFrom({
|
||||
plugin,
|
||||
cfg,
|
||||
accountId: snapshot.accountId,
|
||||
@@ -349,7 +320,7 @@ export async function buildChannelsTable(
|
||||
const account = plugin.config.resolveAccount(cfg, accountId);
|
||||
const enabled = resolveAccountEnabled(plugin, account, cfg);
|
||||
const configured = await resolveAccountConfigured(plugin, account, cfg);
|
||||
const snapshot = buildAccountSnapshot({
|
||||
const snapshot = buildChannelAccountSnapshot({
|
||||
plugin,
|
||||
cfg,
|
||||
accountId,
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
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 { type OpenClawConfig, loadConfig } from "../config/config.js";
|
||||
import { DEFAULT_ACCOUNT_ID } from "../routing/session-key.js";
|
||||
@@ -60,41 +64,6 @@ const resolveAccountConfigured = async (
|
||||
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: {
|
||||
entry: ChannelAccountEntry;
|
||||
plugin: ChannelPlugin;
|
||||
@@ -132,7 +101,7 @@ const buildAccountDetails = (params: {
|
||||
}
|
||||
|
||||
if (params.includeAllowFrom && snapshot.allowFrom?.length) {
|
||||
const formatted = formatAllowFrom({
|
||||
const formatted = formatChannelAllowFrom({
|
||||
plugin: params.plugin,
|
||||
cfg: params.cfg,
|
||||
accountId: snapshot.accountId,
|
||||
@@ -166,7 +135,7 @@ export async function buildChannelSummary(
|
||||
const account = plugin.config.resolveAccount(effective, accountId);
|
||||
const enabled = resolveAccountEnabled(plugin, account, effective);
|
||||
const configured = await resolveAccountConfigured(plugin, account, effective);
|
||||
const snapshot = buildAccountSnapshot({
|
||||
const snapshot = buildChannelAccountSnapshot({
|
||||
plugin,
|
||||
account,
|
||||
cfg: effective,
|
||||
|
||||
Reference in New Issue
Block a user