refactor: share channel account inspection

This commit is contained in:
Peter Steinberger
2026-04-20 13:50:12 +01:00
parent 26a0172568
commit ef9b1a0001
3 changed files with 93 additions and 106 deletions

View File

@@ -0,0 +1,77 @@
import type { OpenClawConfig } from "../config/types.openclaw.js";
import {
hasConfiguredUnavailableCredentialStatus,
hasResolvedCredentialValue,
} from "./account-snapshot-fields.js";
import {
resolveChannelAccountConfigured,
resolveChannelAccountEnabled,
} from "./account-summary.js";
import type { ChannelPlugin } from "./plugins/types.plugin.js";
import { inspectReadOnlyChannelAccount } from "./read-only-account-inspect.js";
type AccountInspectionFields = {
enabled?: boolean;
configured?: boolean;
} | null;
export async function inspectChannelAccount(params: {
plugin: ChannelPlugin;
cfg: OpenClawConfig;
accountId: string;
}): Promise<unknown> {
return (
params.plugin.config.inspectAccount?.(params.cfg, params.accountId) ??
(await inspectReadOnlyChannelAccount({
channelId: params.plugin.id,
cfg: params.cfg,
accountId: params.accountId,
}))
);
}
export async function resolveInspectedChannelAccount(params: {
plugin: ChannelPlugin;
cfg: OpenClawConfig;
sourceConfig: OpenClawConfig;
accountId: string;
}): Promise<{
account: unknown;
enabled: boolean;
configured: boolean;
}> {
const sourceInspectedAccount = await inspectChannelAccount({
plugin: params.plugin,
cfg: params.sourceConfig,
accountId: params.accountId,
});
const resolvedInspectedAccount = await inspectChannelAccount({
plugin: params.plugin,
cfg: params.cfg,
accountId: params.accountId,
});
const resolvedInspection = resolvedInspectedAccount as AccountInspectionFields;
const sourceInspection = sourceInspectedAccount as AccountInspectionFields;
const resolvedAccount =
resolvedInspectedAccount ?? params.plugin.config.resolveAccount(params.cfg, params.accountId);
const useSourceUnavailableAccount = Boolean(
sourceInspectedAccount &&
hasConfiguredUnavailableCredentialStatus(sourceInspectedAccount) &&
(!hasResolvedCredentialValue(resolvedAccount) ||
(sourceInspection?.configured === true && resolvedInspection?.configured === false)),
);
const account = useSourceUnavailableAccount ? sourceInspectedAccount : resolvedAccount;
const selectedInspection = useSourceUnavailableAccount ? sourceInspection : resolvedInspection;
const enabled =
selectedInspection?.enabled ??
resolveChannelAccountEnabled({ plugin: params.plugin, account, cfg: params.cfg });
const configured =
selectedInspection?.configured ??
(await resolveChannelAccountConfigured({
plugin: params.plugin,
account,
cfg: params.cfg,
readAccountConfiguredField: true,
}));
return { account, enabled, configured };
}

View File

@@ -1,13 +1,9 @@
import fs from "node:fs";
import {
hasConfiguredUnavailableCredentialStatus,
hasResolvedCredentialValue,
} from "../../channels/account-snapshot-fields.js";
import { resolveInspectedChannelAccount } from "../../channels/account-inspection.js";
import { hasConfiguredUnavailableCredentialStatus } from "../../channels/account-snapshot-fields.js";
import {
buildChannelAccountSnapshot,
formatChannelAllowFrom,
resolveChannelAccountConfigured,
resolveChannelAccountEnabled,
} from "../../channels/account-summary.js";
import { resolveChannelDefaultAccountId } from "../../channels/plugins/helpers.js";
import { listChannelPlugins } from "../../channels/plugins/index.js";
@@ -17,7 +13,6 @@ import type {
ChannelId,
ChannelPlugin,
} from "../../channels/plugins/types.public.js";
import { inspectReadOnlyChannelAccount } from "../../channels/read-only-account-inspect.js";
import type { OpenClawConfig } from "../../config/types.openclaw.js";
import { asRecord } from "../../shared/record-coerce.js";
import { normalizeOptionalString } from "../../shared/string-coerce.js";
@@ -59,54 +54,16 @@ function existsSyncMaybe(p: string | undefined): boolean | null {
}
}
async function inspectChannelAccount(
plugin: ChannelPlugin,
cfg: OpenClawConfig,
accountId: string,
) {
return (
plugin.config.inspectAccount?.(cfg, accountId) ??
(await inspectReadOnlyChannelAccount({
channelId: plugin.id,
cfg,
accountId,
}))
);
}
async function resolveChannelAccountRow(
params: ResolvedChannelAccountRowParams,
): Promise<ChannelAccountRow> {
const { plugin, cfg, sourceConfig, accountId } = params;
const sourceInspectedAccount = await inspectChannelAccount(plugin, sourceConfig, accountId);
const resolvedInspectedAccount = await inspectChannelAccount(plugin, cfg, accountId);
const resolvedInspection = resolvedInspectedAccount as {
enabled?: boolean;
configured?: boolean;
} | null;
const sourceInspection = sourceInspectedAccount as {
enabled?: boolean;
configured?: boolean;
} | null;
const resolvedAccount = resolvedInspectedAccount ?? plugin.config.resolveAccount(cfg, accountId);
const useSourceUnavailableAccount = Boolean(
sourceInspectedAccount &&
hasConfiguredUnavailableCredentialStatus(sourceInspectedAccount) &&
(!hasResolvedCredentialValue(resolvedAccount) ||
(sourceInspection?.configured === true && resolvedInspection?.configured === false)),
);
const account = useSourceUnavailableAccount ? sourceInspectedAccount : resolvedAccount;
const selectedInspection = useSourceUnavailableAccount ? sourceInspection : resolvedInspection;
const enabled =
selectedInspection?.enabled ?? resolveChannelAccountEnabled({ plugin, account, cfg });
const configured =
selectedInspection?.configured ??
(await resolveChannelAccountConfigured({
plugin,
account,
cfg,
readAccountConfiguredField: true,
}));
const { account, enabled, configured } = await resolveInspectedChannelAccount({
plugin,
cfg,
sourceConfig,
accountId,
});
const snapshot = buildChannelAccountSnapshot({
plugin,
cfg,

View File

@@ -1,18 +1,13 @@
import {
hasConfiguredUnavailableCredentialStatus,
hasResolvedCredentialValue,
} from "../channels/account-snapshot-fields.js";
import { resolveInspectedChannelAccount } from "../channels/account-inspection.js";
import { hasConfiguredUnavailableCredentialStatus } from "../channels/account-snapshot-fields.js";
import {
buildChannelAccountSnapshot,
formatChannelAllowFrom,
resolveChannelAccountConfigured,
resolveChannelAccountEnabled,
} from "../channels/account-summary.js";
import { listChannelPlugins } from "../channels/plugins/index.js";
import { formatChannelStatusState } from "../channels/plugins/status-state.js";
import type { ChannelPlugin } from "../channels/plugins/types.plugin.js";
import type { ChannelAccountSnapshot } from "../channels/plugins/types.public.js";
import { inspectReadOnlyChannelAccount } from "../channels/read-only-account-inspect.js";
import { type OpenClawConfig, loadConfig } from "../config/config.js";
import { DEFAULT_ACCOUNT_ID } from "../routing/session-key.js";
import { theme } from "../terminal/theme.js";
@@ -107,21 +102,6 @@ const buildAccountDetails = (params: {
return details;
};
async function inspectChannelAccount(
plugin: ChannelPlugin,
cfg: OpenClawConfig,
accountId: string,
) {
return (
plugin.config.inspectAccount?.(cfg, accountId) ??
(await inspectReadOnlyChannelAccount({
channelId: plugin.id,
cfg,
accountId,
}))
);
}
export async function buildChannelSummary(
cfg?: OpenClawConfig,
options?: ChannelSummaryOptions,
@@ -141,39 +121,12 @@ export async function buildChannelSummary(
const entries: ChannelAccountEntry[] = [];
for (const accountId of resolvedAccountIds) {
const sourceInspectedAccount = await inspectChannelAccount(plugin, sourceConfig, accountId);
const resolvedInspectedAccount = await inspectChannelAccount(plugin, effective, accountId);
const resolvedInspection = resolvedInspectedAccount as {
enabled?: boolean;
configured?: boolean;
} | null;
const sourceInspection = sourceInspectedAccount as {
enabled?: boolean;
configured?: boolean;
} | null;
const resolvedAccount =
resolvedInspectedAccount ?? plugin.config.resolveAccount(effective, accountId);
const useSourceUnavailableAccount = Boolean(
sourceInspectedAccount &&
hasConfiguredUnavailableCredentialStatus(sourceInspectedAccount) &&
(!hasResolvedCredentialValue(resolvedAccount) ||
(sourceInspection?.configured === true && resolvedInspection?.configured === false)),
);
const account = useSourceUnavailableAccount ? sourceInspectedAccount : resolvedAccount;
const selectedInspection = useSourceUnavailableAccount
? sourceInspection
: resolvedInspection;
const enabled =
selectedInspection?.enabled ??
resolveChannelAccountEnabled({ plugin, account, cfg: effective });
const configured =
selectedInspection?.configured ??
(await resolveChannelAccountConfigured({
plugin,
account,
cfg: effective,
readAccountConfiguredField: true,
}));
const { account, enabled, configured } = await resolveInspectedChannelAccount({
plugin,
cfg: effective,
sourceConfig,
accountId,
});
const snapshot = buildChannelAccountSnapshot({
plugin,
account,