From 0d0ebd0e2019cc343e04245175458f8fe26d6d65 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 15 Feb 2026 00:27:55 +0000 Subject: [PATCH] refactor(onboarding): share promptAccountId helper --- .../mattermost/src/onboarding-helpers.ts | 45 +------------------ src/channels/plugins/onboarding/helpers.ts | 33 +------------- src/plugin-sdk/onboarding.ts | 45 +++++++++++++++++++ 3 files changed, 48 insertions(+), 75 deletions(-) create mode 100644 src/plugin-sdk/onboarding.ts diff --git a/extensions/mattermost/src/onboarding-helpers.ts b/extensions/mattermost/src/onboarding-helpers.ts index cd3b84b386..796de0f1cb 100644 --- a/extensions/mattermost/src/onboarding-helpers.ts +++ b/extensions/mattermost/src/onboarding-helpers.ts @@ -1,44 +1 @@ -import type { OpenClawConfig, WizardPrompter } from "openclaw/plugin-sdk"; -import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "openclaw/plugin-sdk/account-id"; - -type PromptAccountIdParams = { - cfg: OpenClawConfig; - prompter: WizardPrompter; - label: string; - currentId?: string; - listAccountIds: (cfg: OpenClawConfig) => string[]; - defaultAccountId: string; -}; - -export async function promptAccountId(params: PromptAccountIdParams): Promise { - const existingIds = params.listAccountIds(params.cfg); - const initial = params.currentId?.trim() || params.defaultAccountId || DEFAULT_ACCOUNT_ID; - const choice = await params.prompter.select({ - message: `${params.label} account`, - options: [ - ...existingIds.map((id) => ({ - value: id, - label: id === DEFAULT_ACCOUNT_ID ? "default (primary)" : id, - })), - { value: "__new__", label: "Add a new account" }, - ], - initialValue: initial, - }); - - if (choice !== "__new__") { - return normalizeAccountId(choice); - } - - const entered = await params.prompter.text({ - message: `New ${params.label} account id`, - validate: (value) => (value?.trim() ? undefined : "Required"), - }); - const normalized = normalizeAccountId(String(entered)); - if (String(entered).trim() !== normalized) { - await params.prompter.note( - `Normalized account id to "${normalized}".`, - `${params.label} account`, - ); - } - return normalized; -} +export { promptAccountId } from "openclaw/plugin-sdk"; diff --git a/src/channels/plugins/onboarding/helpers.ts b/src/channels/plugins/onboarding/helpers.ts index 951f4522a8..e713904b8d 100644 --- a/src/channels/plugins/onboarding/helpers.ts +++ b/src/channels/plugins/onboarding/helpers.ts @@ -1,37 +1,8 @@ import type { PromptAccountId, PromptAccountIdParams } from "../onboarding-types.js"; -import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../../../routing/session-key.js"; +import { promptAccountId as promptAccountIdSdk } from "../../../plugin-sdk/onboarding.js"; export const promptAccountId: PromptAccountId = async (params: PromptAccountIdParams) => { - const existingIds = params.listAccountIds(params.cfg); - const initial = params.currentId?.trim() || params.defaultAccountId || DEFAULT_ACCOUNT_ID; - const choice = await params.prompter.select({ - message: `${params.label} account`, - options: [ - ...existingIds.map((id) => ({ - value: id, - label: id === DEFAULT_ACCOUNT_ID ? "default (primary)" : id, - })), - { value: "__new__", label: "Add a new account" }, - ], - initialValue: initial, - }); - - if (choice !== "__new__") { - return normalizeAccountId(choice); - } - - const entered = await params.prompter.text({ - message: `New ${params.label} account id`, - validate: (value) => (value?.trim() ? undefined : "Required"), - }); - const normalized = normalizeAccountId(String(entered)); - if (String(entered).trim() !== normalized) { - await params.prompter.note( - `Normalized account id to "${normalized}".`, - `${params.label} account`, - ); - } - return normalized; + return await promptAccountIdSdk(params); }; export function addWildcardAllowFrom( diff --git a/src/plugin-sdk/onboarding.ts b/src/plugin-sdk/onboarding.ts new file mode 100644 index 0000000000..16782faa3a --- /dev/null +++ b/src/plugin-sdk/onboarding.ts @@ -0,0 +1,45 @@ +import type { OpenClawConfig } from "../config/config.js"; +import type { WizardPrompter } from "../wizard/prompts.js"; +import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../routing/session-key.js"; + +export type PromptAccountIdParams = { + cfg: OpenClawConfig; + prompter: WizardPrompter; + label: string; + currentId?: string; + listAccountIds: (cfg: OpenClawConfig) => string[]; + defaultAccountId: string; +}; + +export async function promptAccountId(params: PromptAccountIdParams): Promise { + const existingIds = params.listAccountIds(params.cfg); + const initial = params.currentId?.trim() || params.defaultAccountId || DEFAULT_ACCOUNT_ID; + const choice = await params.prompter.select({ + message: `${params.label} account`, + options: [ + ...existingIds.map((id) => ({ + value: id, + label: id === DEFAULT_ACCOUNT_ID ? "default (primary)" : id, + })), + { value: "__new__", label: "Add a new account" }, + ], + initialValue: initial, + }); + + if (choice !== "__new__") { + return normalizeAccountId(choice); + } + + const entered = await params.prompter.text({ + message: `New ${params.label} account id`, + validate: (value) => (value?.trim() ? undefined : "Required"), + }); + const normalized = normalizeAccountId(String(entered)); + if (String(entered).trim() !== normalized) { + await params.prompter.note( + `Normalized account id to "${normalized}".`, + `${params.label} account`, + ); + } + return normalized; +}