feat: unify onboarding + config schema

This commit is contained in:
Peter Steinberger
2026-01-03 16:04:19 +01:00
parent 0f85080d81
commit 53baba71fa
43 changed files with 3478 additions and 1011 deletions

View File

@@ -1,15 +1,12 @@
import fs from "node:fs/promises";
import path from "node:path";
import { confirm, multiselect, note, select, text } from "@clack/prompts";
import chalk from "chalk";
import type { ClawdisConfig } from "../config/config.js";
import { loginWeb } from "../provider-web.js";
import type { RuntimeEnv } from "../runtime.js";
import { normalizeE164 } from "../utils.js";
import { resolveWebAuthDir } from "../web/session.js";
import { detectBinary, guardCancel } from "./onboard-helpers.js";
import type { WizardPrompter } from "../wizard/prompts.js";
import { detectBinary } from "./onboard-helpers.js";
import type { ProviderChoice } from "./onboard-types.js";
import { installSignalCli } from "./signal-install.js";
@@ -27,8 +24,8 @@ async function detectWhatsAppLinked(): Promise<boolean> {
return await pathExists(credsPath);
}
function noteProviderPrimer(): void {
note(
async function noteProviderPrimer(prompter: WizardPrompter): Promise<void> {
await prompter.note(
[
"WhatsApp: links via WhatsApp Web (scan QR), stores creds for future sends.",
"Telegram: Bot API (token from @BotFather), replies via your bot.",
@@ -40,8 +37,8 @@ function noteProviderPrimer(): void {
);
}
function noteTelegramTokenHelp(): void {
note(
async function noteTelegramTokenHelp(prompter: WizardPrompter): Promise<void> {
await prompter.note(
[
"1) Open Telegram and chat with @BotFather",
"2) Run /newbot (or /mybots)",
@@ -52,8 +49,8 @@ function noteTelegramTokenHelp(): void {
);
}
function noteDiscordTokenHelp(): void {
note(
async function noteDiscordTokenHelp(prompter: WizardPrompter): Promise<void> {
await prompter.note(
[
"1) Discord Developer Portal → Applications → New Application",
"2) Bot → Add Bot → Reset Token → copy token",
@@ -76,13 +73,14 @@ function setWhatsAppAllowFrom(cfg: ClawdisConfig, allowFrom?: string[]) {
async function promptWhatsAppAllowFrom(
cfg: ClawdisConfig,
runtime: RuntimeEnv,
_runtime: RuntimeEnv,
prompter: WizardPrompter,
): Promise<ClawdisConfig> {
const existingAllowFrom = cfg.whatsapp?.allowFrom ?? [];
const existingLabel =
existingAllowFrom.length > 0 ? existingAllowFrom.join(", ") : "unset";
note(
await prompter.note(
[
"WhatsApp direct chats are gated by `whatsapp.allowFrom`.",
'Default (unset) = self-chat only; use "*" to allow anyone.',
@@ -105,40 +103,34 @@ async function promptWhatsAppAllowFrom(
{ value: "any", label: "Anyone (*)" },
] as const);
const mode = guardCancel(
await select({
message: "Who can trigger the bot via WhatsApp?",
options: options.map((opt) => ({ value: opt.value, label: opt.label })),
}),
runtime,
) as (typeof options)[number]["value"];
const mode = (await prompter.select({
message: "Who can trigger the bot via WhatsApp?",
options: options.map((opt) => ({ value: opt.value, label: opt.label })),
})) as (typeof options)[number]["value"];
if (mode === "keep") return cfg;
if (mode === "self") return setWhatsAppAllowFrom(cfg, undefined);
if (mode === "any") return setWhatsAppAllowFrom(cfg, ["*"]);
const allowRaw = guardCancel(
await text({
message: "Allowed sender numbers (comma-separated, E.164)",
placeholder: "+15555550123, +447700900123",
validate: (value) => {
const raw = String(value ?? "").trim();
if (!raw) return "Required";
const parts = raw
.split(/[\n,;]+/g)
.map((p) => p.trim())
.filter(Boolean);
if (parts.length === 0) return "Required";
for (const part of parts) {
if (part === "*") continue;
const normalized = normalizeE164(part);
if (!normalized) return `Invalid number: ${part}`;
}
return undefined;
},
}),
runtime,
);
const allowRaw = await prompter.text({
message: "Allowed sender numbers (comma-separated, E.164)",
placeholder: "+15555550123, +447700900123",
validate: (value) => {
const raw = String(value ?? "").trim();
if (!raw) return "Required";
const parts = raw
.split(/[\n,;]+/g)
.map((p) => p.trim())
.filter(Boolean);
if (parts.length === 0) return "Required";
for (const part of parts) {
if (part === "*") continue;
const normalized = normalizeE164(part);
if (!normalized) return `Invalid number: ${part}`;
}
return undefined;
},
});
const parts = String(allowRaw)
.split(/[\n,;]+/g)
@@ -154,6 +146,7 @@ async function promptWhatsAppAllowFrom(
export async function setupProviders(
cfg: ClawdisConfig,
runtime: RuntimeEnv,
prompter: WizardPrompter,
options?: { allowDisable?: boolean; allowSignalInstall?: boolean },
): Promise<ClawdisConfig> {
const whatsappLinked = await detectWhatsAppLinked();
@@ -174,91 +167,63 @@ export async function setupProviders(
const imessageCliPath = cfg.imessage?.cliPath ?? "imsg";
const imessageCliDetected = await detectBinary(imessageCliPath);
note(
await prompter.note(
[
`WhatsApp: ${
whatsappLinked ? chalk.green("linked") : chalk.red("not linked")
}`,
`Telegram: ${
telegramConfigured
? chalk.green("configured")
: chalk.yellow("needs token")
}`,
`Discord: ${
discordConfigured
? chalk.green("configured")
: chalk.yellow("needs token")
}`,
`Signal: ${
signalConfigured
? chalk.green("configured")
: chalk.yellow("needs setup")
}`,
`iMessage: ${
imessageConfigured
? chalk.green("configured")
: chalk.yellow("needs setup")
}`,
`signal-cli: ${
signalCliDetected ? chalk.green("found") : chalk.red("missing")
} (${signalCliPath})`,
`imsg: ${
imessageCliDetected ? chalk.green("found") : chalk.red("missing")
} (${imessageCliPath})`,
`WhatsApp: ${whatsappLinked ? "linked" : "not linked"}`,
`Telegram: ${telegramConfigured ? "configured" : "needs token"}`,
`Discord: ${discordConfigured ? "configured" : "needs token"}`,
`Signal: ${signalConfigured ? "configured" : "needs setup"}`,
`iMessage: ${imessageConfigured ? "configured" : "needs setup"}`,
`signal-cli: ${signalCliDetected ? "found" : "missing"} (${signalCliPath})`,
`imsg: ${imessageCliDetected ? "found" : "missing"} (${imessageCliPath})`,
].join("\n"),
"Provider status",
);
const shouldConfigure = guardCancel(
await confirm({
message: "Configure chat providers now?",
initialValue: true,
}),
runtime,
);
const shouldConfigure = await prompter.confirm({
message: "Configure chat providers now?",
initialValue: true,
});
if (!shouldConfigure) return cfg;
noteProviderPrimer();
await noteProviderPrimer(prompter);
const selection = guardCancel(
await multiselect({
message: "Select providers",
options: [
{
value: "whatsapp",
label: "WhatsApp (QR link)",
hint: whatsappLinked ? "linked" : "not linked",
},
{
value: "telegram",
label: "Telegram (Bot API)",
hint: telegramConfigured ? "configured" : "needs token",
},
{
value: "discord",
label: "Discord (Bot API)",
hint: discordConfigured ? "configured" : "needs token",
},
{
value: "signal",
label: "Signal (signal-cli)",
hint: signalCliDetected ? "signal-cli found" : "signal-cli missing",
},
{
value: "imessage",
label: "iMessage (imsg)",
hint: imessageCliDetected ? "imsg found" : "imsg missing",
},
],
}),
runtime,
) as ProviderChoice[];
const selection = (await prompter.multiselect({
message: "Select providers",
options: [
{
value: "whatsapp",
label: "WhatsApp (QR link)",
hint: whatsappLinked ? "linked" : "not linked",
},
{
value: "telegram",
label: "Telegram (Bot API)",
hint: telegramConfigured ? "configured" : "needs token",
},
{
value: "discord",
label: "Discord (Bot API)",
hint: discordConfigured ? "configured" : "needs token",
},
{
value: "signal",
label: "Signal (signal-cli)",
hint: signalCliDetected ? "signal-cli found" : "signal-cli missing",
},
{
value: "imessage",
label: "iMessage (imsg)",
hint: imessageCliDetected ? "imsg found" : "imsg missing",
},
],
})) as ProviderChoice[];
let next = cfg;
if (selection.includes("whatsapp")) {
if (!whatsappLinked) {
note(
await prompter.note(
[
"Scan the QR with WhatsApp on your phone.",
"Credentials are stored under ~/.clawdis/credentials/ for future runs.",
@@ -266,15 +231,12 @@ export async function setupProviders(
"WhatsApp linking",
);
}
const wantsLink = guardCancel(
await confirm({
message: whatsappLinked
? "WhatsApp already linked. Re-link now?"
: "Link WhatsApp now (QR)?",
initialValue: !whatsappLinked,
}),
runtime,
);
const wantsLink = await prompter.confirm({
message: whatsappLinked
? "WhatsApp already linked. Re-link now?"
: "Link WhatsApp now (QR)?",
initialValue: !whatsappLinked,
});
if (wantsLink) {
try {
await loginWeb(false, "web");
@@ -282,25 +244,25 @@ export async function setupProviders(
runtime.error(`WhatsApp login failed: ${String(err)}`);
}
} else if (!whatsappLinked) {
note("Run `clawdis login` later to link WhatsApp.", "WhatsApp");
await prompter.note(
"Run `clawdis login` later to link WhatsApp.",
"WhatsApp",
);
}
next = await promptWhatsAppAllowFrom(next, runtime);
next = await promptWhatsAppAllowFrom(next, runtime, prompter);
}
if (selection.includes("telegram")) {
let token: string | null = null;
if (!telegramConfigured) {
noteTelegramTokenHelp();
await noteTelegramTokenHelp(prompter);
}
if (telegramEnv && !cfg.telegram?.botToken) {
const keepEnv = guardCancel(
await confirm({
message: "TELEGRAM_BOT_TOKEN detected. Use env var?",
initialValue: true,
}),
runtime,
);
const keepEnv = await prompter.confirm({
message: "TELEGRAM_BOT_TOKEN detected. Use env var?",
initialValue: true,
});
if (keepEnv) {
next = {
...next,
@@ -311,43 +273,31 @@ export async function setupProviders(
};
} else {
token = String(
guardCancel(
await text({
message: "Enter Telegram bot token",
validate: (value) => (value?.trim() ? undefined : "Required"),
}),
runtime,
),
await prompter.text({
message: "Enter Telegram bot token",
validate: (value) => (value?.trim() ? undefined : "Required"),
}),
).trim();
}
} else if (cfg.telegram?.botToken) {
const keep = guardCancel(
await confirm({
message: "Telegram token already configured. Keep it?",
initialValue: true,
}),
runtime,
);
const keep = await prompter.confirm({
message: "Telegram token already configured. Keep it?",
initialValue: true,
});
if (!keep) {
token = String(
guardCancel(
await text({
message: "Enter Telegram bot token",
validate: (value) => (value?.trim() ? undefined : "Required"),
}),
runtime,
),
await prompter.text({
message: "Enter Telegram bot token",
validate: (value) => (value?.trim() ? undefined : "Required"),
}),
).trim();
}
} else {
token = String(
guardCancel(
await text({
message: "Enter Telegram bot token",
validate: (value) => (value?.trim() ? undefined : "Required"),
}),
runtime,
),
await prompter.text({
message: "Enter Telegram bot token",
validate: (value) => (value?.trim() ? undefined : "Required"),
}),
).trim();
}
@@ -366,16 +316,13 @@ export async function setupProviders(
if (selection.includes("discord")) {
let token: string | null = null;
if (!discordConfigured) {
noteDiscordTokenHelp();
await noteDiscordTokenHelp(prompter);
}
if (discordEnv && !cfg.discord?.token) {
const keepEnv = guardCancel(
await confirm({
message: "DISCORD_BOT_TOKEN detected. Use env var?",
initialValue: true,
}),
runtime,
);
const keepEnv = await prompter.confirm({
message: "DISCORD_BOT_TOKEN detected. Use env var?",
initialValue: true,
});
if (keepEnv) {
next = {
...next,
@@ -386,43 +333,31 @@ export async function setupProviders(
};
} else {
token = String(
guardCancel(
await text({
message: "Enter Discord bot token",
validate: (value) => (value?.trim() ? undefined : "Required"),
}),
runtime,
),
await prompter.text({
message: "Enter Discord bot token",
validate: (value) => (value?.trim() ? undefined : "Required"),
}),
).trim();
}
} else if (cfg.discord?.token) {
const keep = guardCancel(
await confirm({
message: "Discord token already configured. Keep it?",
initialValue: true,
}),
runtime,
);
const keep = await prompter.confirm({
message: "Discord token already configured. Keep it?",
initialValue: true,
});
if (!keep) {
token = String(
guardCancel(
await text({
message: "Enter Discord bot token",
validate: (value) => (value?.trim() ? undefined : "Required"),
}),
runtime,
),
await prompter.text({
message: "Enter Discord bot token",
validate: (value) => (value?.trim() ? undefined : "Required"),
}),
).trim();
}
} else {
token = String(
guardCancel(
await text({
message: "Enter Discord bot token",
validate: (value) => (value?.trim() ? undefined : "Required"),
}),
runtime,
),
await prompter.text({
message: "Enter Discord bot token",
validate: (value) => (value?.trim() ? undefined : "Required"),
}),
).trim();
}
@@ -442,33 +377,39 @@ export async function setupProviders(
let resolvedCliPath = signalCliPath;
let cliDetected = signalCliDetected;
if (options?.allowSignalInstall) {
const wantsInstall = guardCancel(
await confirm({
message: cliDetected
? "signal-cli detected. Reinstall/update now?"
: "signal-cli not found. Install now?",
initialValue: !cliDetected,
}),
runtime,
);
const wantsInstall = await prompter.confirm({
message: cliDetected
? "signal-cli detected. Reinstall/update now?"
: "signal-cli not found. Install now?",
initialValue: !cliDetected,
});
if (wantsInstall) {
try {
const result = await installSignalCli(runtime);
if (result.ok && result.cliPath) {
cliDetected = true;
resolvedCliPath = result.cliPath;
note(`Installed signal-cli at ${result.cliPath}`, "Signal");
await prompter.note(
`Installed signal-cli at ${result.cliPath}`,
"Signal",
);
} else if (!result.ok) {
note(result.error ?? "signal-cli install failed.", "Signal");
await prompter.note(
result.error ?? "signal-cli install failed.",
"Signal",
);
}
} catch (err) {
note(`signal-cli install failed: ${String(err)}`, "Signal");
await prompter.note(
`signal-cli install failed: ${String(err)}`,
"Signal",
);
}
}
}
if (!cliDetected) {
note(
await prompter.note(
"signal-cli not found. Install it, then rerun this step or set signal.cliPath.",
"Signal",
);
@@ -476,25 +417,19 @@ export async function setupProviders(
let account = cfg.signal?.account ?? "";
if (account) {
const keep = guardCancel(
await confirm({
message: `Signal account set (${account}). Keep it?`,
initialValue: true,
}),
runtime,
);
const keep = await prompter.confirm({
message: `Signal account set (${account}). Keep it?`,
initialValue: true,
});
if (!keep) account = "";
}
if (!account) {
account = String(
guardCancel(
await text({
message: "Signal bot number (E.164)",
validate: (value) => (value?.trim() ? undefined : "Required"),
}),
runtime,
),
await prompter.text({
message: "Signal bot number (E.164)",
validate: (value) => (value?.trim() ? undefined : "Required"),
}),
).trim();
}
@@ -510,7 +445,7 @@ export async function setupProviders(
};
}
note(
await prompter.note(
[
'Link device with: signal-cli link -n "Clawdis"',
"Scan QR in Signal → Linked Devices",
@@ -523,17 +458,17 @@ export async function setupProviders(
if (selection.includes("imessage")) {
let resolvedCliPath = imessageCliPath;
if (!imessageCliDetected) {
const entered = guardCancel(
await text({
message: "imsg CLI path",
initialValue: resolvedCliPath,
validate: (value) => (value?.trim() ? undefined : "Required"),
}),
runtime,
);
const entered = await prompter.text({
message: "imsg CLI path",
initialValue: resolvedCliPath,
validate: (value) => (value?.trim() ? undefined : "Required"),
});
resolvedCliPath = String(entered).trim();
if (!resolvedCliPath) {
note("imsg CLI path required to enable iMessage.", "iMessage");
await prompter.note(
"imsg CLI path required to enable iMessage.",
"iMessage",
);
}
}
@@ -548,7 +483,7 @@ export async function setupProviders(
};
}
note(
await prompter.note(
[
"Ensure Clawdis has Full Disk Access to Messages DB.",
"Grant Automation permission for Messages when prompted.",
@@ -560,13 +495,10 @@ export async function setupProviders(
if (options?.allowDisable) {
if (!selection.includes("telegram") && telegramConfigured) {
const disable = guardCancel(
await confirm({
message: "Disable Telegram provider?",
initialValue: false,
}),
runtime,
);
const disable = await prompter.confirm({
message: "Disable Telegram provider?",
initialValue: false,
});
if (disable) {
next = {
...next,
@@ -576,13 +508,10 @@ export async function setupProviders(
}
if (!selection.includes("discord") && discordConfigured) {
const disable = guardCancel(
await confirm({
message: "Disable Discord provider?",
initialValue: false,
}),
runtime,
);
const disable = await prompter.confirm({
message: "Disable Discord provider?",
initialValue: false,
});
if (disable) {
next = {
...next,
@@ -592,13 +521,10 @@ export async function setupProviders(
}
if (!selection.includes("signal") && signalConfigured) {
const disable = guardCancel(
await confirm({
message: "Disable Signal provider?",
initialValue: false,
}),
runtime,
);
const disable = await prompter.confirm({
message: "Disable Signal provider?",
initialValue: false,
});
if (disable) {
next = {
...next,
@@ -608,13 +534,10 @@ export async function setupProviders(
}
if (!selection.includes("imessage") && imessageConfigured) {
const disable = guardCancel(
await confirm({
message: "Disable iMessage provider?",
initialValue: false,
}),
runtime,
);
const disable = await prompter.confirm({
message: "Disable iMessage provider?",
initialValue: false,
});
if (disable) {
next = {
...next,