mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-26 00:21:59 +00:00
refactor: share channel setup status helpers
This commit is contained in:
@@ -14,6 +14,7 @@ import {
|
||||
createLegacyCompatChannelDmPolicy,
|
||||
createNestedChannelParsedAllowFromPrompt,
|
||||
createPromptParsedAllowFromForAccount,
|
||||
createStandardChannelSetupStatus,
|
||||
createNestedChannelAllowFromSetter,
|
||||
createNestedChannelDmPolicy,
|
||||
createNestedChannelDmPolicySetter,
|
||||
@@ -1816,6 +1817,46 @@ describe("normalizeAllowFromEntries", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("createStandardChannelSetupStatus", () => {
|
||||
it("returns the shared status fields without status lines by default", async () => {
|
||||
const status = createStandardChannelSetupStatus({
|
||||
channelLabel: "Demo",
|
||||
configuredLabel: "configured",
|
||||
unconfiguredLabel: "needs token",
|
||||
configuredHint: "ready",
|
||||
unconfiguredHint: "missing token",
|
||||
configuredScore: 2,
|
||||
unconfiguredScore: 0,
|
||||
resolveConfigured: ({ cfg }) => Boolean(cfg.channels?.demo),
|
||||
});
|
||||
|
||||
expect(status.configuredHint).toBe("ready");
|
||||
expect(status.unconfiguredHint).toBe("missing token");
|
||||
expect(status.configuredScore).toBe(2);
|
||||
expect(status.unconfiguredScore).toBe(0);
|
||||
expect(await status.resolveConfigured({ cfg: { channels: { demo: {} } } })).toBe(true);
|
||||
expect(status.resolveStatusLines).toBeUndefined();
|
||||
});
|
||||
|
||||
it("builds the default status line plus extra lines when requested", async () => {
|
||||
const status = createStandardChannelSetupStatus({
|
||||
channelLabel: "Demo",
|
||||
configuredLabel: "configured",
|
||||
unconfiguredLabel: "needs token",
|
||||
includeStatusLine: true,
|
||||
resolveConfigured: ({ cfg }) => Boolean(cfg.channels?.demo),
|
||||
resolveExtraStatusLines: ({ configured }) => [`Configured: ${configured ? "yes" : "no"}`],
|
||||
});
|
||||
|
||||
expect(
|
||||
await status.resolveStatusLines?.({
|
||||
cfg: { channels: { demo: {} } },
|
||||
configured: true,
|
||||
}),
|
||||
).toEqual(["Demo: configured", "Configured: yes"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveSetupAccountId", () => {
|
||||
it("normalizes provided account ids", () => {
|
||||
expect(
|
||||
|
||||
@@ -13,7 +13,11 @@ import type {
|
||||
PromptAccountId,
|
||||
PromptAccountIdParams,
|
||||
} from "./setup-wizard-types.js";
|
||||
import type { ChannelSetupWizard, ChannelSetupWizardAllowFromEntry } from "./setup-wizard.js";
|
||||
import type {
|
||||
ChannelSetupWizard,
|
||||
ChannelSetupWizardAllowFromEntry,
|
||||
ChannelSetupWizardStatus,
|
||||
} from "./setup-wizard.js";
|
||||
|
||||
let providerAuthInputPromise:
|
||||
| Promise<Pick<typeof import("../../plugins/provider-auth-ref.js"), "promptSecretRefForSetup">>
|
||||
@@ -156,6 +160,50 @@ export function normalizeAllowFromEntries(
|
||||
return [...new Set(normalized)];
|
||||
}
|
||||
|
||||
export function createStandardChannelSetupStatus(params: {
|
||||
channelLabel: string;
|
||||
configuredLabel: string;
|
||||
unconfiguredLabel: string;
|
||||
configuredHint?: string;
|
||||
unconfiguredHint?: string;
|
||||
configuredScore?: number;
|
||||
unconfiguredScore?: number;
|
||||
includeStatusLine?: boolean;
|
||||
resolveConfigured: ChannelSetupWizardStatus["resolveConfigured"];
|
||||
resolveExtraStatusLines?: (params: {
|
||||
cfg: OpenClawConfig;
|
||||
configured: boolean;
|
||||
}) => string[] | Promise<string[]>;
|
||||
}): ChannelSetupWizardStatus {
|
||||
const status: ChannelSetupWizardStatus = {
|
||||
configuredLabel: params.configuredLabel,
|
||||
unconfiguredLabel: params.unconfiguredLabel,
|
||||
resolveConfigured: params.resolveConfigured,
|
||||
...(params.configuredHint ? { configuredHint: params.configuredHint } : {}),
|
||||
...(params.unconfiguredHint ? { unconfiguredHint: params.unconfiguredHint } : {}),
|
||||
...(typeof params.configuredScore === "number"
|
||||
? { configuredScore: params.configuredScore }
|
||||
: {}),
|
||||
...(typeof params.unconfiguredScore === "number"
|
||||
? { unconfiguredScore: params.unconfiguredScore }
|
||||
: {}),
|
||||
};
|
||||
|
||||
if (params.includeStatusLine || params.resolveExtraStatusLines) {
|
||||
status.resolveStatusLines = async ({ cfg, configured }) => {
|
||||
const lines = params.includeStatusLine
|
||||
? [
|
||||
`${params.channelLabel}: ${configured ? params.configuredLabel : params.unconfiguredLabel}`,
|
||||
]
|
||||
: [];
|
||||
const extraLines = (await params.resolveExtraStatusLines?.({ cfg, configured })) ?? [];
|
||||
return [...lines, ...extraLines];
|
||||
};
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
export function resolveSetupAccountId(params: {
|
||||
accountId?: string;
|
||||
defaultAccountId: string;
|
||||
|
||||
@@ -15,6 +15,7 @@ export {
|
||||
createAccountScopedAllowFromSection,
|
||||
createAccountScopedGroupAccessSection,
|
||||
createLegacyCompatChannelDmPolicy,
|
||||
createStandardChannelSetupStatus,
|
||||
parseMentionOrPrefixedId,
|
||||
patchChannelConfigForAccount,
|
||||
promptLegacyChannelAllowFromForAccount,
|
||||
|
||||
@@ -42,6 +42,7 @@ export {
|
||||
createLegacyCompatChannelDmPolicy,
|
||||
createNestedChannelParsedAllowFromPrompt,
|
||||
createPromptParsedAllowFromForAccount,
|
||||
createStandardChannelSetupStatus,
|
||||
createNestedChannelAllowFromSetter,
|
||||
createNestedChannelDmPolicy,
|
||||
createNestedChannelDmPolicySetter,
|
||||
|
||||
Reference in New Issue
Block a user