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,10 +1,8 @@
import { confirm, note, select, spinner, text } from "@clack/prompts";
import type { ClawdisConfig } from "../config/config.js";
import type { GatewayBonjourBeacon } from "../infra/bonjour-discovery.js";
import { discoverGatewayBeacons } from "../infra/bonjour-discovery.js";
import type { RuntimeEnv } from "../runtime.js";
import { detectBinary, guardCancel } from "./onboard-helpers.js";
import type { WizardPrompter } from "../wizard/prompts.js";
import { detectBinary } from "./onboard-helpers.js";
const DEFAULT_GATEWAY_URL = "ws://127.0.0.1:18789";
@@ -28,7 +26,7 @@ function ensureWsUrl(value: string): string {
export async function promptRemoteGatewayConfig(
cfg: ClawdisConfig,
runtime: RuntimeEnv,
prompter: WizardPrompter,
): Promise<ClawdisConfig> {
let selectedBeacon: GatewayBonjourBeacon | null = null;
let suggestedUrl = cfg.gateway?.remote?.url ?? DEFAULT_GATEWAY_URL;
@@ -36,25 +34,21 @@ export async function promptRemoteGatewayConfig(
const hasBonjourTool =
(await detectBinary("dns-sd")) || (await detectBinary("avahi-browse"));
const wantsDiscover = hasBonjourTool
? guardCancel(
await confirm({
message: "Discover gateway on LAN (Bonjour)?",
initialValue: true,
}),
runtime,
)
? await prompter.confirm({
message: "Discover gateway on LAN (Bonjour)?",
initialValue: true,
})
: false;
if (!hasBonjourTool) {
note(
await prompter.note(
"Bonjour discovery requires dns-sd (macOS) or avahi-browse (Linux).",
"Discovery",
);
}
if (wantsDiscover) {
const spin = spinner();
spin.start("Searching for gateways…");
const spin = prompter.progress("Searching for gateways…");
const beacons = await discoverGatewayBeacons({ timeoutMs: 2000 });
spin.stop(
beacons.length > 0
@@ -63,19 +57,16 @@ export async function promptRemoteGatewayConfig(
);
if (beacons.length > 0) {
const selection = guardCancel(
await select({
message: "Select gateway",
options: [
...beacons.map((beacon, index) => ({
value: String(index),
label: buildLabel(beacon),
})),
{ value: "manual", label: "Enter URL manually" },
],
}),
runtime,
);
const selection = await prompter.select({
message: "Select gateway",
options: [
...beacons.map((beacon, index) => ({
value: String(index),
label: buildLabel(beacon),
})),
{ value: "manual", label: "Enter URL manually" },
],
});
if (selection !== "manual") {
const idx = Number.parseInt(String(selection), 10);
selectedBeacon = Number.isFinite(idx) ? (beacons[idx] ?? null) : null;
@@ -87,24 +78,21 @@ export async function promptRemoteGatewayConfig(
const host = pickHost(selectedBeacon);
const port = selectedBeacon.gatewayPort ?? 18789;
if (host) {
const mode = guardCancel(
await select({
message: "Connection method",
options: [
{
value: "direct",
label: `Direct gateway WS (${host}:${port})`,
},
{ value: "ssh", label: "SSH tunnel (loopback)" },
],
}),
runtime,
);
const mode = await prompter.select({
message: "Connection method",
options: [
{
value: "direct",
label: `Direct gateway WS (${host}:${port})`,
},
{ value: "ssh", label: "SSH tunnel (loopback)" },
],
});
if (mode === "direct") {
suggestedUrl = `ws://${host}:${port}`;
} else {
suggestedUrl = DEFAULT_GATEWAY_URL;
note(
await prompter.note(
[
"Start a tunnel before using the CLI:",
`ssh -N -L 18789:127.0.0.1:18789 <user>@${host}${
@@ -117,42 +105,33 @@ export async function promptRemoteGatewayConfig(
}
}
const urlInput = guardCancel(
await text({
message: "Gateway WebSocket URL",
initialValue: suggestedUrl,
validate: (value) =>
String(value).trim().startsWith("ws://") ||
String(value).trim().startsWith("wss://")
? undefined
: "URL must start with ws:// or wss://",
}),
runtime,
);
const urlInput = await prompter.text({
message: "Gateway WebSocket URL",
initialValue: suggestedUrl,
validate: (value) =>
String(value).trim().startsWith("ws://") ||
String(value).trim().startsWith("wss://")
? undefined
: "URL must start with ws:// or wss://",
});
const url = ensureWsUrl(String(urlInput));
const authChoice = guardCancel(
await select({
message: "Gateway auth",
options: [
{ value: "token", label: "Token (recommended)" },
{ value: "off", label: "No auth" },
],
}),
runtime,
) as "token" | "off";
const authChoice = (await prompter.select({
message: "Gateway auth",
options: [
{ value: "token", label: "Token (recommended)" },
{ value: "off", label: "No auth" },
],
})) as "token" | "off";
let token = cfg.gateway?.remote?.token ?? "";
if (authChoice === "token") {
token = String(
guardCancel(
await text({
message: "Gateway token",
initialValue: token,
validate: (value) => (value?.trim() ? undefined : "Required"),
}),
runtime,
),
await prompter.text({
message: "Gateway token",
initialValue: token,
validate: (value) => (value?.trim() ? undefined : "Required"),
}),
).trim();
} else {
token = "";