test: add onboarding e2e harness
This commit is contained in:
@@ -50,6 +50,20 @@ type OnboardOptions = {
|
||||
mode?: OnboardMode;
|
||||
workspace?: string;
|
||||
nonInteractive?: boolean;
|
||||
authChoice?: AuthChoice;
|
||||
anthropicApiKey?: string;
|
||||
gatewayPort?: number;
|
||||
gatewayBind?: "loopback" | "lan" | "tailnet" | "auto";
|
||||
gatewayAuth?: GatewayAuthChoice;
|
||||
gatewayToken?: string;
|
||||
gatewayPassword?: string;
|
||||
tailscale?: "off" | "serve" | "funnel";
|
||||
tailscaleResetOnExit?: boolean;
|
||||
installDaemon?: boolean;
|
||||
skipSkills?: boolean;
|
||||
skipHealth?: boolean;
|
||||
nodeManager?: "npm" | "pnpm" | "bun";
|
||||
json?: boolean;
|
||||
};
|
||||
|
||||
function guardCancel<T>(value: T, runtime: RuntimeEnv): T {
|
||||
@@ -345,8 +359,193 @@ export async function onboardCommand(
|
||||
assertSupportedRuntime(runtime);
|
||||
|
||||
if (opts.nonInteractive) {
|
||||
runtime.error("Non-interactive mode is not implemented yet.");
|
||||
runtime.exit(1);
|
||||
const snapshot = await readConfigFileSnapshot();
|
||||
const baseConfig: ClawdisConfig = snapshot.valid ? snapshot.config : {};
|
||||
const mode: OnboardMode = opts.mode ?? "local";
|
||||
|
||||
if (mode === "remote") {
|
||||
const payload = {
|
||||
mode,
|
||||
instructions: [
|
||||
"clawdis setup",
|
||||
"clawdis gateway-daemon --port 18789",
|
||||
"OAuth creds: ~/.clawdis/credentials/oauth.json",
|
||||
"Workspace: ~/clawd",
|
||||
],
|
||||
};
|
||||
if (opts.json) {
|
||||
runtime.log(JSON.stringify(payload, null, 2));
|
||||
} else {
|
||||
runtime.log(payload.instructions.join("\n"));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const workspaceDir = resolveUserPath(
|
||||
(opts.workspace ?? baseConfig.agent?.workspace ?? DEFAULT_AGENT_WORKSPACE_DIR)
|
||||
.trim(),
|
||||
);
|
||||
|
||||
let nextConfig: ClawdisConfig = {
|
||||
...baseConfig,
|
||||
agent: {
|
||||
...baseConfig.agent,
|
||||
workspace: workspaceDir,
|
||||
},
|
||||
gateway: {
|
||||
...baseConfig.gateway,
|
||||
mode: "local",
|
||||
},
|
||||
};
|
||||
|
||||
const authChoice: AuthChoice = opts.authChoice ?? "skip";
|
||||
if (authChoice === "apiKey") {
|
||||
const key = opts.anthropicApiKey?.trim();
|
||||
if (!key) {
|
||||
runtime.error("Missing --anthropic-api-key");
|
||||
runtime.exit(1);
|
||||
return;
|
||||
}
|
||||
await setAnthropicApiKey(key);
|
||||
} else if (authChoice === "minimax") {
|
||||
nextConfig = applyMinimaxConfig(nextConfig);
|
||||
} else if (authChoice === "oauth") {
|
||||
runtime.error("OAuth requires interactive mode.");
|
||||
runtime.exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
const port = opts.gatewayPort ?? 18789;
|
||||
if (!Number.isFinite(port) || port <= 0) {
|
||||
runtime.error("Invalid --gateway-port");
|
||||
runtime.exit(1);
|
||||
return;
|
||||
}
|
||||
let bind = opts.gatewayBind ?? "loopback";
|
||||
let authMode = opts.gatewayAuth ?? "off";
|
||||
const tailscaleMode = opts.tailscale ?? "off";
|
||||
const tailscaleResetOnExit = Boolean(opts.tailscaleResetOnExit);
|
||||
|
||||
if (tailscaleMode !== "off" && bind !== "loopback") {
|
||||
bind = "loopback";
|
||||
}
|
||||
if (authMode === "off" && bind !== "loopback") {
|
||||
authMode = "token";
|
||||
}
|
||||
if (tailscaleMode === "funnel" && authMode !== "password") {
|
||||
authMode = "password";
|
||||
}
|
||||
|
||||
let gatewayToken = opts.gatewayToken?.trim() || undefined;
|
||||
if (authMode === "token") {
|
||||
if (!gatewayToken) gatewayToken = randomToken();
|
||||
nextConfig = {
|
||||
...nextConfig,
|
||||
gateway: {
|
||||
...nextConfig.gateway,
|
||||
auth: { ...nextConfig.gateway?.auth, mode: "token" },
|
||||
},
|
||||
};
|
||||
}
|
||||
if (authMode === "password") {
|
||||
const password = opts.gatewayPassword?.trim();
|
||||
if (!password) {
|
||||
runtime.error("Missing --gateway-password for password auth.");
|
||||
runtime.exit(1);
|
||||
return;
|
||||
}
|
||||
nextConfig = {
|
||||
...nextConfig,
|
||||
gateway: {
|
||||
...nextConfig.gateway,
|
||||
auth: {
|
||||
...nextConfig.gateway?.auth,
|
||||
mode: "password",
|
||||
password,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
nextConfig = {
|
||||
...nextConfig,
|
||||
gateway: {
|
||||
...nextConfig.gateway,
|
||||
bind,
|
||||
tailscale: {
|
||||
...nextConfig.gateway?.tailscale,
|
||||
mode: tailscaleMode,
|
||||
resetOnExit: tailscaleResetOnExit,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
if (!opts.skipSkills) {
|
||||
const nodeManager = opts.nodeManager ?? "npm";
|
||||
if (!["npm", "pnpm", "bun"].includes(nodeManager)) {
|
||||
runtime.error("Invalid --node-manager (use npm, pnpm, or bun)");
|
||||
runtime.exit(1);
|
||||
return;
|
||||
}
|
||||
nextConfig = {
|
||||
...nextConfig,
|
||||
skills: {
|
||||
...nextConfig.skills,
|
||||
install: {
|
||||
...nextConfig.skills?.install,
|
||||
nodeManager,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
await writeConfigFile(nextConfig);
|
||||
await ensureWorkspaceAndSessions(workspaceDir, runtime);
|
||||
|
||||
if (opts.installDaemon) {
|
||||
const service = resolveGatewayService();
|
||||
const devMode =
|
||||
process.argv[1]?.includes(`${path.sep}src${path.sep}`) &&
|
||||
process.argv[1]?.endsWith(".ts");
|
||||
const { programArguments, workingDirectory } =
|
||||
await resolveGatewayProgramArguments({ port, dev: devMode });
|
||||
const environment: Record<string, string | undefined> = {
|
||||
PATH: process.env.PATH,
|
||||
CLAWDIS_GATEWAY_TOKEN: gatewayToken,
|
||||
CLAWDIS_LAUNCHD_LABEL:
|
||||
process.platform === "darwin" ? GATEWAY_LAUNCH_AGENT_LABEL : undefined,
|
||||
};
|
||||
await service.install({
|
||||
env: process.env,
|
||||
stdout: process.stdout,
|
||||
programArguments,
|
||||
workingDirectory,
|
||||
environment,
|
||||
});
|
||||
}
|
||||
|
||||
if (!opts.skipHealth) {
|
||||
await sleep(1000);
|
||||
await healthCommand({ json: false, timeoutMs: 10_000 }, runtime);
|
||||
}
|
||||
|
||||
if (opts.json) {
|
||||
runtime.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
mode,
|
||||
workspace: workspaceDir,
|
||||
authChoice,
|
||||
gateway: { port, bind, authMode, tailscaleMode },
|
||||
installDaemon: Boolean(opts.installDaemon),
|
||||
skipSkills: Boolean(opts.skipSkills),
|
||||
skipHealth: Boolean(opts.skipHealth),
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user