Skip to content

Commit 64df787

Browse files
committed
refactor(channels): share account summary helpers
1 parent cc233da commit 64df787

3 files changed

Lines changed: 48 additions & 72 deletions

File tree

src/channels/account-summary.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { OpenClawConfig } from "../config/config.js";
2+
import type { ChannelAccountSnapshot } from "./plugins/types.core.js";
3+
import type { ChannelPlugin } from "./plugins/types.plugin.js";
4+
5+
export function buildChannelAccountSnapshot(params: {
6+
plugin: ChannelPlugin;
7+
account: unknown;
8+
cfg: OpenClawConfig;
9+
accountId: string;
10+
enabled: boolean;
11+
configured: boolean;
12+
}): ChannelAccountSnapshot {
13+
const described = params.plugin.config.describeAccount?.(params.account, params.cfg);
14+
return {
15+
enabled: params.enabled,
16+
configured: params.configured,
17+
...described,
18+
accountId: params.accountId,
19+
};
20+
}
21+
22+
export function formatChannelAllowFrom(params: {
23+
plugin: ChannelPlugin;
24+
cfg: OpenClawConfig;
25+
accountId?: string | null;
26+
allowFrom: Array<string | number>;
27+
}): string[] {
28+
if (params.plugin.config.formatAllowFrom) {
29+
return params.plugin.config.formatAllowFrom({
30+
cfg: params.cfg,
31+
accountId: params.accountId,
32+
allowFrom: params.allowFrom,
33+
});
34+
}
35+
return params.allowFrom.map((entry) => String(entry).trim()).filter(Boolean);
36+
}

src/commands/status-all/channels.ts

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import type {
55
ChannelPlugin,
66
} from "../../channels/plugins/types.js";
77
import type { OpenClawConfig } from "../../config/config.js";
8+
import {
9+
buildChannelAccountSnapshot,
10+
formatChannelAllowFrom,
11+
} from "../../channels/account-summary.js";
812
import { resolveChannelDefaultAccountId } from "../../channels/plugins/helpers.js";
913
import { listChannelPlugins } from "../../channels/plugins/index.js";
1014
import { sha256HexPrefix } from "../../logging/redact-identifier.js";
@@ -105,39 +109,6 @@ const resolveAccountConfigured = async (
105109
return configured !== false;
106110
};
107111

108-
const buildAccountSnapshot = (params: {
109-
plugin: ChannelPlugin;
110-
account: unknown;
111-
cfg: OpenClawConfig;
112-
accountId: string;
113-
enabled: boolean;
114-
configured: boolean;
115-
}): ChannelAccountSnapshot => {
116-
const described = params.plugin.config.describeAccount?.(params.account, params.cfg);
117-
return {
118-
enabled: params.enabled,
119-
configured: params.configured,
120-
...described,
121-
accountId: params.accountId,
122-
};
123-
};
124-
125-
const formatAllowFrom = (params: {
126-
plugin: ChannelPlugin;
127-
cfg: OpenClawConfig;
128-
accountId?: string | null;
129-
allowFrom: Array<string | number>;
130-
}) => {
131-
if (params.plugin.config.formatAllowFrom) {
132-
return params.plugin.config.formatAllowFrom({
133-
cfg: params.cfg,
134-
accountId: params.accountId,
135-
allowFrom: params.allowFrom,
136-
});
137-
}
138-
return params.allowFrom.map((entry) => String(entry).trim()).filter(Boolean);
139-
};
140-
141112
const buildAccountNotes = (params: {
142113
plugin: ChannelPlugin;
143114
cfg: OpenClawConfig;
@@ -177,7 +148,7 @@ const buildAccountNotes = (params: {
177148
const allowFrom =
178149
plugin.config.resolveAllowFrom?.({ cfg, accountId: snapshot.accountId }) ?? snapshot.allowFrom;
179150
if (allowFrom?.length) {
180-
const formatted = formatAllowFrom({
151+
const formatted = formatChannelAllowFrom({
181152
plugin,
182153
cfg,
183154
accountId: snapshot.accountId,
@@ -349,7 +320,7 @@ export async function buildChannelsTable(
349320
const account = plugin.config.resolveAccount(cfg, accountId);
350321
const enabled = resolveAccountEnabled(plugin, account, cfg);
351322
const configured = await resolveAccountConfigured(plugin, account, cfg);
352-
const snapshot = buildAccountSnapshot({
323+
const snapshot = buildChannelAccountSnapshot({
353324
plugin,
354325
cfg,
355326
accountId,

src/infra/channel-summary.ts

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import type { ChannelAccountSnapshot, ChannelPlugin } from "../channels/plugins/types.js";
2+
import {
3+
buildChannelAccountSnapshot,
4+
formatChannelAllowFrom,
5+
} from "../channels/account-summary.js";
26
import { listChannelPlugins } from "../channels/plugins/index.js";
37
import { type OpenClawConfig, loadConfig } from "../config/config.js";
48
import { DEFAULT_ACCOUNT_ID } from "../routing/session-key.js";
@@ -60,41 +64,6 @@ const resolveAccountConfigured = async (
6064
return true;
6165
};
6266

63-
const buildAccountSnapshot = (params: {
64-
plugin: ChannelPlugin;
65-
account: unknown;
66-
cfg: OpenClawConfig;
67-
accountId: string;
68-
enabled: boolean;
69-
configured: boolean;
70-
}): ChannelAccountSnapshot => {
71-
const described = params.plugin.config.describeAccount
72-
? params.plugin.config.describeAccount(params.account, params.cfg)
73-
: undefined;
74-
return {
75-
enabled: params.enabled,
76-
configured: params.configured,
77-
...described,
78-
accountId: params.accountId,
79-
};
80-
};
81-
82-
const formatAllowFrom = (params: {
83-
plugin: ChannelPlugin;
84-
cfg: OpenClawConfig;
85-
accountId?: string | null;
86-
allowFrom: Array<string | number>;
87-
}) => {
88-
if (params.plugin.config.formatAllowFrom) {
89-
return params.plugin.config.formatAllowFrom({
90-
cfg: params.cfg,
91-
accountId: params.accountId,
92-
allowFrom: params.allowFrom,
93-
});
94-
}
95-
return params.allowFrom.map((entry) => String(entry).trim()).filter(Boolean);
96-
};
97-
9867
const buildAccountDetails = (params: {
9968
entry: ChannelAccountEntry;
10069
plugin: ChannelPlugin;
@@ -132,7 +101,7 @@ const buildAccountDetails = (params: {
132101
}
133102

134103
if (params.includeAllowFrom && snapshot.allowFrom?.length) {
135-
const formatted = formatAllowFrom({
104+
const formatted = formatChannelAllowFrom({
136105
plugin: params.plugin,
137106
cfg: params.cfg,
138107
accountId: snapshot.accountId,
@@ -166,7 +135,7 @@ export async function buildChannelSummary(
166135
const account = plugin.config.resolveAccount(effective, accountId);
167136
const enabled = resolveAccountEnabled(plugin, account, effective);
168137
const configured = await resolveAccountConfigured(plugin, account, effective);
169-
const snapshot = buildAccountSnapshot({
138+
const snapshot = buildChannelAccountSnapshot({
170139
plugin,
171140
account,
172141
cfg: effective,

0 commit comments

Comments
 (0)