refactor(src): split oversized modules
This commit is contained in:
71
src/commands/onboard-non-interactive/api-keys.ts
Normal file
71
src/commands/onboard-non-interactive/api-keys.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import {
|
||||
ensureAuthProfileStore,
|
||||
resolveApiKeyForProfile,
|
||||
resolveAuthProfileOrder,
|
||||
} from "../../agents/auth-profiles.js";
|
||||
import { resolveEnvApiKey } from "../../agents/model-auth.js";
|
||||
import type { ClawdbotConfig } from "../../config/config.js";
|
||||
import type { RuntimeEnv } from "../../runtime.js";
|
||||
|
||||
export type NonInteractiveApiKeySource = "flag" | "env" | "profile";
|
||||
|
||||
async function resolveApiKeyFromProfiles(params: {
|
||||
provider: string;
|
||||
cfg: ClawdbotConfig;
|
||||
agentDir?: string;
|
||||
}): Promise<string | null> {
|
||||
const store = ensureAuthProfileStore(params.agentDir);
|
||||
const order = resolveAuthProfileOrder({
|
||||
cfg: params.cfg,
|
||||
store,
|
||||
provider: params.provider,
|
||||
});
|
||||
for (const profileId of order) {
|
||||
const cred = store.profiles[profileId];
|
||||
if (cred?.type !== "api_key") continue;
|
||||
const resolved = await resolveApiKeyForProfile({
|
||||
cfg: params.cfg,
|
||||
store,
|
||||
profileId,
|
||||
agentDir: params.agentDir,
|
||||
});
|
||||
if (resolved?.apiKey) return resolved.apiKey;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function resolveNonInteractiveApiKey(params: {
|
||||
provider: string;
|
||||
cfg: ClawdbotConfig;
|
||||
flagValue?: string;
|
||||
flagName: string;
|
||||
envVar: string;
|
||||
runtime: RuntimeEnv;
|
||||
agentDir?: string;
|
||||
allowProfile?: boolean;
|
||||
}): Promise<{ key: string; source: NonInteractiveApiKeySource } | null> {
|
||||
const flagKey = params.flagValue?.trim();
|
||||
if (flagKey) return { key: flagKey, source: "flag" };
|
||||
|
||||
const envResolved = resolveEnvApiKey(params.provider);
|
||||
if (envResolved?.apiKey) return { key: envResolved.apiKey, source: "env" };
|
||||
|
||||
if (params.allowProfile ?? true) {
|
||||
const profileKey = await resolveApiKeyFromProfiles({
|
||||
provider: params.provider,
|
||||
cfg: params.cfg,
|
||||
agentDir: params.agentDir,
|
||||
});
|
||||
if (profileKey) return { key: profileKey, source: "profile" };
|
||||
}
|
||||
|
||||
const profileHint =
|
||||
params.allowProfile === false
|
||||
? ""
|
||||
: `, or existing ${params.provider} API-key profile`;
|
||||
params.runtime.error(
|
||||
`Missing ${params.flagName} (or ${params.envVar} in env${profileHint}).`,
|
||||
);
|
||||
params.runtime.exit(1);
|
||||
return null;
|
||||
}
|
||||
117
src/commands/onboard-non-interactive/local.ts
Normal file
117
src/commands/onboard-non-interactive/local.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import type { ClawdbotConfig } from "../../config/config.js";
|
||||
import {
|
||||
CONFIG_PATH_CLAWDBOT,
|
||||
resolveGatewayPort,
|
||||
writeConfigFile,
|
||||
} from "../../config/config.js";
|
||||
import type { RuntimeEnv } from "../../runtime.js";
|
||||
import { sleep } from "../../utils.js";
|
||||
import { DEFAULT_GATEWAY_DAEMON_RUNTIME } from "../daemon-runtime.js";
|
||||
import { healthCommand } from "../health.js";
|
||||
import {
|
||||
applyWizardMetadata,
|
||||
DEFAULT_WORKSPACE,
|
||||
ensureWorkspaceAndSessions,
|
||||
} from "../onboard-helpers.js";
|
||||
import type { OnboardOptions } from "../onboard-types.js";
|
||||
|
||||
import { applyNonInteractiveAuthChoice } from "./local/auth-choice.js";
|
||||
import { installGatewayDaemonNonInteractive } from "./local/daemon-install.js";
|
||||
import { applyNonInteractiveGatewayConfig } from "./local/gateway-config.js";
|
||||
import { logNonInteractiveOnboardingJson } from "./local/output.js";
|
||||
import { applyNonInteractiveSkillsConfig } from "./local/skills-config.js";
|
||||
import { resolveNonInteractiveWorkspaceDir } from "./local/workspace.js";
|
||||
|
||||
export async function runNonInteractiveOnboardingLocal(params: {
|
||||
opts: OnboardOptions;
|
||||
runtime: RuntimeEnv;
|
||||
baseConfig: ClawdbotConfig;
|
||||
}) {
|
||||
const { opts, runtime, baseConfig } = params;
|
||||
const mode = "local" as const;
|
||||
|
||||
const workspaceDir = resolveNonInteractiveWorkspaceDir({
|
||||
opts,
|
||||
baseConfig,
|
||||
defaultWorkspaceDir: DEFAULT_WORKSPACE,
|
||||
});
|
||||
|
||||
let nextConfig: ClawdbotConfig = {
|
||||
...baseConfig,
|
||||
agents: {
|
||||
...baseConfig.agents,
|
||||
defaults: {
|
||||
...baseConfig.agents?.defaults,
|
||||
workspace: workspaceDir,
|
||||
},
|
||||
},
|
||||
gateway: {
|
||||
...baseConfig.gateway,
|
||||
mode: "local",
|
||||
},
|
||||
};
|
||||
|
||||
const authChoice = opts.authChoice ?? "skip";
|
||||
const nextConfigAfterAuth = await applyNonInteractiveAuthChoice({
|
||||
nextConfig,
|
||||
authChoice,
|
||||
opts,
|
||||
runtime,
|
||||
baseConfig,
|
||||
});
|
||||
if (!nextConfigAfterAuth) return;
|
||||
nextConfig = nextConfigAfterAuth;
|
||||
|
||||
const gatewayBasePort = resolveGatewayPort(baseConfig);
|
||||
const gatewayResult = applyNonInteractiveGatewayConfig({
|
||||
nextConfig,
|
||||
opts,
|
||||
runtime,
|
||||
defaultPort: gatewayBasePort,
|
||||
});
|
||||
if (!gatewayResult) return;
|
||||
nextConfig = gatewayResult.nextConfig;
|
||||
|
||||
nextConfig = applyNonInteractiveSkillsConfig({ nextConfig, opts, runtime });
|
||||
|
||||
nextConfig = applyWizardMetadata(nextConfig, { command: "onboard", mode });
|
||||
await writeConfigFile(nextConfig);
|
||||
runtime.log(`Updated ${CONFIG_PATH_CLAWDBOT}`);
|
||||
|
||||
await ensureWorkspaceAndSessions(workspaceDir, runtime, {
|
||||
skipBootstrap: Boolean(nextConfig.agents?.defaults?.skipBootstrap),
|
||||
});
|
||||
|
||||
await installGatewayDaemonNonInteractive({
|
||||
nextConfig,
|
||||
opts,
|
||||
runtime,
|
||||
port: gatewayResult.port,
|
||||
gatewayToken: gatewayResult.gatewayToken,
|
||||
});
|
||||
|
||||
const daemonRuntimeRaw = opts.daemonRuntime ?? DEFAULT_GATEWAY_DAEMON_RUNTIME;
|
||||
if (!opts.skipHealth) {
|
||||
await sleep(1000);
|
||||
// Health check runs against the gateway; small delay avoids flakiness during install/start.
|
||||
await healthCommand({ json: false, timeoutMs: 10_000 }, runtime);
|
||||
}
|
||||
|
||||
logNonInteractiveOnboardingJson({
|
||||
opts,
|
||||
runtime,
|
||||
mode,
|
||||
workspaceDir,
|
||||
authChoice,
|
||||
gateway: {
|
||||
port: gatewayResult.port,
|
||||
bind: gatewayResult.bind,
|
||||
authMode: gatewayResult.authMode,
|
||||
tailscaleMode: gatewayResult.tailscaleMode,
|
||||
},
|
||||
installDaemon: Boolean(opts.installDaemon),
|
||||
daemonRuntime: opts.installDaemon ? daemonRuntimeRaw : undefined,
|
||||
skipSkills: Boolean(opts.skipSkills),
|
||||
skipHealth: Boolean(opts.skipHealth),
|
||||
});
|
||||
}
|
||||
276
src/commands/onboard-non-interactive/local/auth-choice.ts
Normal file
276
src/commands/onboard-non-interactive/local/auth-choice.ts
Normal file
@@ -0,0 +1,276 @@
|
||||
import {
|
||||
CLAUDE_CLI_PROFILE_ID,
|
||||
CODEX_CLI_PROFILE_ID,
|
||||
ensureAuthProfileStore,
|
||||
} from "../../../agents/auth-profiles.js";
|
||||
import type { ClawdbotConfig } from "../../../config/config.js";
|
||||
import { upsertSharedEnvVar } from "../../../infra/env-file.js";
|
||||
import type { RuntimeEnv } from "../../../runtime.js";
|
||||
import { applyGoogleGeminiModelDefault } from "../../google-gemini-model-default.js";
|
||||
import {
|
||||
applyAuthProfileConfig,
|
||||
applyMinimaxApiConfig,
|
||||
applyMinimaxConfig,
|
||||
applyMoonshotConfig,
|
||||
applyOpencodeZenConfig,
|
||||
applyOpenrouterConfig,
|
||||
applySyntheticConfig,
|
||||
applyZaiConfig,
|
||||
setAnthropicApiKey,
|
||||
setGeminiApiKey,
|
||||
setMinimaxApiKey,
|
||||
setMoonshotApiKey,
|
||||
setOpencodeZenApiKey,
|
||||
setOpenrouterApiKey,
|
||||
setSyntheticApiKey,
|
||||
setZaiApiKey,
|
||||
} from "../../onboard-auth.js";
|
||||
import type { AuthChoice, OnboardOptions } from "../../onboard-types.js";
|
||||
import { applyOpenAICodexModelDefault } from "../../openai-codex-model-default.js";
|
||||
|
||||
import { resolveNonInteractiveApiKey } from "../api-keys.js";
|
||||
|
||||
export async function applyNonInteractiveAuthChoice(params: {
|
||||
nextConfig: ClawdbotConfig;
|
||||
authChoice: AuthChoice;
|
||||
opts: OnboardOptions;
|
||||
runtime: RuntimeEnv;
|
||||
baseConfig: ClawdbotConfig;
|
||||
}): Promise<ClawdbotConfig | null> {
|
||||
const { authChoice, opts, runtime, baseConfig } = params;
|
||||
let nextConfig = params.nextConfig;
|
||||
|
||||
if (authChoice === "apiKey") {
|
||||
const resolved = await resolveNonInteractiveApiKey({
|
||||
provider: "anthropic",
|
||||
cfg: baseConfig,
|
||||
flagValue: opts.anthropicApiKey,
|
||||
flagName: "--anthropic-api-key",
|
||||
envVar: "ANTHROPIC_API_KEY",
|
||||
runtime,
|
||||
});
|
||||
if (!resolved) return null;
|
||||
if (resolved.source !== "profile") await setAnthropicApiKey(resolved.key);
|
||||
return applyAuthProfileConfig(nextConfig, {
|
||||
profileId: "anthropic:default",
|
||||
provider: "anthropic",
|
||||
mode: "api_key",
|
||||
});
|
||||
}
|
||||
|
||||
if (authChoice === "gemini-api-key") {
|
||||
const resolved = await resolveNonInteractiveApiKey({
|
||||
provider: "google",
|
||||
cfg: baseConfig,
|
||||
flagValue: opts.geminiApiKey,
|
||||
flagName: "--gemini-api-key",
|
||||
envVar: "GEMINI_API_KEY",
|
||||
runtime,
|
||||
});
|
||||
if (!resolved) return null;
|
||||
if (resolved.source !== "profile") await setGeminiApiKey(resolved.key);
|
||||
nextConfig = applyAuthProfileConfig(nextConfig, {
|
||||
profileId: "google:default",
|
||||
provider: "google",
|
||||
mode: "api_key",
|
||||
});
|
||||
return applyGoogleGeminiModelDefault(nextConfig).next;
|
||||
}
|
||||
|
||||
if (authChoice === "zai-api-key") {
|
||||
const resolved = await resolveNonInteractiveApiKey({
|
||||
provider: "zai",
|
||||
cfg: baseConfig,
|
||||
flagValue: opts.zaiApiKey,
|
||||
flagName: "--zai-api-key",
|
||||
envVar: "ZAI_API_KEY",
|
||||
runtime,
|
||||
});
|
||||
if (!resolved) return null;
|
||||
if (resolved.source !== "profile") await setZaiApiKey(resolved.key);
|
||||
nextConfig = applyAuthProfileConfig(nextConfig, {
|
||||
profileId: "zai:default",
|
||||
provider: "zai",
|
||||
mode: "api_key",
|
||||
});
|
||||
return applyZaiConfig(nextConfig);
|
||||
}
|
||||
|
||||
if (authChoice === "openai-api-key") {
|
||||
const resolved = await resolveNonInteractiveApiKey({
|
||||
provider: "openai",
|
||||
cfg: baseConfig,
|
||||
flagValue: opts.openaiApiKey,
|
||||
flagName: "--openai-api-key",
|
||||
envVar: "OPENAI_API_KEY",
|
||||
runtime,
|
||||
allowProfile: false,
|
||||
});
|
||||
if (!resolved) return null;
|
||||
const key = resolved.key;
|
||||
const result = upsertSharedEnvVar({ key: "OPENAI_API_KEY", value: key });
|
||||
process.env.OPENAI_API_KEY = key;
|
||||
runtime.log(`Saved OPENAI_API_KEY to ${result.path}`);
|
||||
return nextConfig;
|
||||
}
|
||||
|
||||
if (authChoice === "openrouter-api-key") {
|
||||
const resolved = await resolveNonInteractiveApiKey({
|
||||
provider: "openrouter",
|
||||
cfg: baseConfig,
|
||||
flagValue: opts.openrouterApiKey,
|
||||
flagName: "--openrouter-api-key",
|
||||
envVar: "OPENROUTER_API_KEY",
|
||||
runtime,
|
||||
});
|
||||
if (!resolved) return null;
|
||||
if (resolved.source !== "profile") await setOpenrouterApiKey(resolved.key);
|
||||
nextConfig = applyAuthProfileConfig(nextConfig, {
|
||||
profileId: "openrouter:default",
|
||||
provider: "openrouter",
|
||||
mode: "api_key",
|
||||
});
|
||||
return applyOpenrouterConfig(nextConfig);
|
||||
}
|
||||
|
||||
if (authChoice === "moonshot-api-key") {
|
||||
const resolved = await resolveNonInteractiveApiKey({
|
||||
provider: "moonshot",
|
||||
cfg: baseConfig,
|
||||
flagValue: opts.moonshotApiKey,
|
||||
flagName: "--moonshot-api-key",
|
||||
envVar: "MOONSHOT_API_KEY",
|
||||
runtime,
|
||||
});
|
||||
if (!resolved) return null;
|
||||
if (resolved.source !== "profile") await setMoonshotApiKey(resolved.key);
|
||||
nextConfig = applyAuthProfileConfig(nextConfig, {
|
||||
profileId: "moonshot:default",
|
||||
provider: "moonshot",
|
||||
mode: "api_key",
|
||||
});
|
||||
return applyMoonshotConfig(nextConfig);
|
||||
}
|
||||
|
||||
if (authChoice === "synthetic-api-key") {
|
||||
const resolved = await resolveNonInteractiveApiKey({
|
||||
provider: "synthetic",
|
||||
cfg: baseConfig,
|
||||
flagValue: opts.syntheticApiKey,
|
||||
flagName: "--synthetic-api-key",
|
||||
envVar: "SYNTHETIC_API_KEY",
|
||||
runtime,
|
||||
});
|
||||
if (!resolved) return null;
|
||||
if (resolved.source !== "profile") await setSyntheticApiKey(resolved.key);
|
||||
nextConfig = applyAuthProfileConfig(nextConfig, {
|
||||
profileId: "synthetic:default",
|
||||
provider: "synthetic",
|
||||
mode: "api_key",
|
||||
});
|
||||
return applySyntheticConfig(nextConfig);
|
||||
}
|
||||
|
||||
if (
|
||||
authChoice === "minimax-cloud" ||
|
||||
authChoice === "minimax-api" ||
|
||||
authChoice === "minimax-api-lightning"
|
||||
) {
|
||||
const resolved = await resolveNonInteractiveApiKey({
|
||||
provider: "minimax",
|
||||
cfg: baseConfig,
|
||||
flagValue: opts.minimaxApiKey,
|
||||
flagName: "--minimax-api-key",
|
||||
envVar: "MINIMAX_API_KEY",
|
||||
runtime,
|
||||
});
|
||||
if (!resolved) return null;
|
||||
if (resolved.source !== "profile") await setMinimaxApiKey(resolved.key);
|
||||
nextConfig = applyAuthProfileConfig(nextConfig, {
|
||||
profileId: "minimax:default",
|
||||
provider: "minimax",
|
||||
mode: "api_key",
|
||||
});
|
||||
const modelId =
|
||||
authChoice === "minimax-api-lightning"
|
||||
? "MiniMax-M2.1-lightning"
|
||||
: "MiniMax-M2.1";
|
||||
return applyMinimaxApiConfig(nextConfig, modelId);
|
||||
}
|
||||
|
||||
if (authChoice === "claude-cli") {
|
||||
const store = ensureAuthProfileStore(undefined, {
|
||||
allowKeychainPrompt: false,
|
||||
});
|
||||
if (!store.profiles[CLAUDE_CLI_PROFILE_ID]) {
|
||||
runtime.error(
|
||||
process.platform === "darwin"
|
||||
? 'No Claude CLI credentials found. Run interactive onboarding to approve Keychain access for "Claude Code-credentials".'
|
||||
: "No Claude CLI credentials found at ~/.claude/.credentials.json",
|
||||
);
|
||||
runtime.exit(1);
|
||||
return null;
|
||||
}
|
||||
return applyAuthProfileConfig(nextConfig, {
|
||||
profileId: CLAUDE_CLI_PROFILE_ID,
|
||||
provider: "anthropic",
|
||||
mode: "token",
|
||||
});
|
||||
}
|
||||
|
||||
if (authChoice === "codex-cli") {
|
||||
const store = ensureAuthProfileStore();
|
||||
if (!store.profiles[CODEX_CLI_PROFILE_ID]) {
|
||||
runtime.error("No Codex CLI credentials found at ~/.codex/auth.json");
|
||||
runtime.exit(1);
|
||||
return null;
|
||||
}
|
||||
nextConfig = applyAuthProfileConfig(nextConfig, {
|
||||
profileId: CODEX_CLI_PROFILE_ID,
|
||||
provider: "openai-codex",
|
||||
mode: "oauth",
|
||||
});
|
||||
return applyOpenAICodexModelDefault(nextConfig).next;
|
||||
}
|
||||
|
||||
if (authChoice === "minimax") return applyMinimaxConfig(nextConfig);
|
||||
|
||||
if (authChoice === "opencode-zen") {
|
||||
const resolved = await resolveNonInteractiveApiKey({
|
||||
provider: "opencode",
|
||||
cfg: baseConfig,
|
||||
flagValue: opts.opencodeZenApiKey,
|
||||
flagName: "--opencode-zen-api-key",
|
||||
envVar: "OPENCODE_API_KEY (or OPENCODE_ZEN_API_KEY)",
|
||||
runtime,
|
||||
});
|
||||
if (!resolved) return null;
|
||||
if (resolved.source !== "profile") await setOpencodeZenApiKey(resolved.key);
|
||||
nextConfig = applyAuthProfileConfig(nextConfig, {
|
||||
profileId: "opencode:default",
|
||||
provider: "opencode",
|
||||
mode: "api_key",
|
||||
});
|
||||
return applyOpencodeZenConfig(nextConfig);
|
||||
}
|
||||
|
||||
if (
|
||||
authChoice === "token" ||
|
||||
authChoice === "oauth" ||
|
||||
authChoice === "chutes" ||
|
||||
authChoice === "openai-codex" ||
|
||||
authChoice === "antigravity"
|
||||
) {
|
||||
const label =
|
||||
authChoice === "antigravity"
|
||||
? "Antigravity"
|
||||
: authChoice === "token"
|
||||
? "Token"
|
||||
: "OAuth";
|
||||
runtime.error(`${label} requires interactive mode.`);
|
||||
runtime.exit(1);
|
||||
return null;
|
||||
}
|
||||
|
||||
return nextConfig;
|
||||
}
|
||||
87
src/commands/onboard-non-interactive/local/daemon-install.ts
Normal file
87
src/commands/onboard-non-interactive/local/daemon-install.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import path from "node:path";
|
||||
|
||||
import type { ClawdbotConfig } from "../../../config/config.js";
|
||||
import { resolveGatewayLaunchAgentLabel } from "../../../daemon/constants.js";
|
||||
import { resolveGatewayProgramArguments } from "../../../daemon/program-args.js";
|
||||
import {
|
||||
renderSystemNodeWarning,
|
||||
resolvePreferredNodePath,
|
||||
resolveSystemNodeInfo,
|
||||
} from "../../../daemon/runtime-paths.js";
|
||||
import { resolveGatewayService } from "../../../daemon/service.js";
|
||||
import { buildServiceEnvironment } from "../../../daemon/service-env.js";
|
||||
import { isSystemdUserServiceAvailable } from "../../../daemon/systemd.js";
|
||||
import type { RuntimeEnv } from "../../../runtime.js";
|
||||
import {
|
||||
DEFAULT_GATEWAY_DAEMON_RUNTIME,
|
||||
isGatewayDaemonRuntime,
|
||||
} from "../../daemon-runtime.js";
|
||||
import type { OnboardOptions } from "../../onboard-types.js";
|
||||
import { ensureSystemdUserLingerNonInteractive } from "../../systemd-linger.js";
|
||||
|
||||
export async function installGatewayDaemonNonInteractive(params: {
|
||||
nextConfig: ClawdbotConfig;
|
||||
opts: OnboardOptions;
|
||||
runtime: RuntimeEnv;
|
||||
port: number;
|
||||
gatewayToken?: string;
|
||||
}) {
|
||||
const { opts, runtime, port, gatewayToken } = params;
|
||||
if (!opts.installDaemon) return;
|
||||
|
||||
const daemonRuntimeRaw = opts.daemonRuntime ?? DEFAULT_GATEWAY_DAEMON_RUNTIME;
|
||||
const systemdAvailable =
|
||||
process.platform === "linux" ? await isSystemdUserServiceAvailable() : true;
|
||||
if (process.platform === "linux" && !systemdAvailable) {
|
||||
runtime.log(
|
||||
"Systemd user services are unavailable; skipping daemon install.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isGatewayDaemonRuntime(daemonRuntimeRaw)) {
|
||||
runtime.error("Invalid --daemon-runtime (use node or bun)");
|
||||
runtime.exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
const service = resolveGatewayService();
|
||||
const devMode =
|
||||
process.argv[1]?.includes(`${path.sep}src${path.sep}`) &&
|
||||
process.argv[1]?.endsWith(".ts");
|
||||
const nodePath = await resolvePreferredNodePath({
|
||||
env: process.env,
|
||||
runtime: daemonRuntimeRaw,
|
||||
});
|
||||
const { programArguments, workingDirectory } =
|
||||
await resolveGatewayProgramArguments({
|
||||
port,
|
||||
dev: devMode,
|
||||
runtime: daemonRuntimeRaw,
|
||||
nodePath,
|
||||
});
|
||||
|
||||
if (daemonRuntimeRaw === "node") {
|
||||
const systemNode = await resolveSystemNodeInfo({ env: process.env });
|
||||
const warning = renderSystemNodeWarning(systemNode, programArguments[0]);
|
||||
if (warning) runtime.log(warning);
|
||||
}
|
||||
|
||||
const environment = buildServiceEnvironment({
|
||||
env: process.env,
|
||||
port,
|
||||
token: gatewayToken,
|
||||
launchdLabel:
|
||||
process.platform === "darwin"
|
||||
? resolveGatewayLaunchAgentLabel(process.env.CLAWDBOT_PROFILE)
|
||||
: undefined,
|
||||
});
|
||||
await service.install({
|
||||
env: process.env,
|
||||
stdout: process.stdout,
|
||||
programArguments,
|
||||
workingDirectory,
|
||||
environment,
|
||||
});
|
||||
await ensureSystemdUserLingerNonInteractive({ runtime });
|
||||
}
|
||||
110
src/commands/onboard-non-interactive/local/gateway-config.ts
Normal file
110
src/commands/onboard-non-interactive/local/gateway-config.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import type { ClawdbotConfig } from "../../../config/config.js";
|
||||
import type { RuntimeEnv } from "../../../runtime.js";
|
||||
import { randomToken } from "../../onboard-helpers.js";
|
||||
import type { OnboardOptions } from "../../onboard-types.js";
|
||||
|
||||
export function applyNonInteractiveGatewayConfig(params: {
|
||||
nextConfig: ClawdbotConfig;
|
||||
opts: OnboardOptions;
|
||||
runtime: RuntimeEnv;
|
||||
defaultPort: number;
|
||||
}): {
|
||||
nextConfig: ClawdbotConfig;
|
||||
port: number;
|
||||
bind: string;
|
||||
authMode: string;
|
||||
tailscaleMode: string;
|
||||
tailscaleResetOnExit: boolean;
|
||||
gatewayToken?: string;
|
||||
} | null {
|
||||
const { opts, runtime } = params;
|
||||
|
||||
const hasGatewayPort = opts.gatewayPort !== undefined;
|
||||
if (
|
||||
hasGatewayPort &&
|
||||
(!Number.isFinite(opts.gatewayPort) || (opts.gatewayPort ?? 0) <= 0)
|
||||
) {
|
||||
runtime.error("Invalid --gateway-port");
|
||||
runtime.exit(1);
|
||||
return null;
|
||||
}
|
||||
|
||||
const port = hasGatewayPort
|
||||
? (opts.gatewayPort as number)
|
||||
: params.defaultPort;
|
||||
let bind = opts.gatewayBind ?? "loopback";
|
||||
let authMode = opts.gatewayAuth ?? "token";
|
||||
const tailscaleMode = opts.tailscale ?? "off";
|
||||
const tailscaleResetOnExit = Boolean(opts.tailscaleResetOnExit);
|
||||
|
||||
// Tighten config to safe combos:
|
||||
// - If Tailscale is on, force loopback bind (the tunnel handles external access).
|
||||
// - If binding beyond loopback, disallow auth=off.
|
||||
// - If using Tailscale Funnel, require password auth.
|
||||
if (tailscaleMode !== "off" && bind !== "loopback") bind = "loopback";
|
||||
if (authMode === "off" && bind !== "loopback") authMode = "token";
|
||||
if (tailscaleMode === "funnel" && authMode !== "password")
|
||||
authMode = "password";
|
||||
|
||||
let nextConfig = params.nextConfig;
|
||||
let gatewayToken = opts.gatewayToken?.trim() || undefined;
|
||||
|
||||
if (authMode === "token") {
|
||||
if (!gatewayToken) gatewayToken = randomToken();
|
||||
nextConfig = {
|
||||
...nextConfig,
|
||||
gateway: {
|
||||
...nextConfig.gateway,
|
||||
auth: {
|
||||
...nextConfig.gateway?.auth,
|
||||
mode: "token",
|
||||
token: gatewayToken,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (authMode === "password") {
|
||||
const password = opts.gatewayPassword?.trim();
|
||||
if (!password) {
|
||||
runtime.error("Missing --gateway-password for password auth.");
|
||||
runtime.exit(1);
|
||||
return null;
|
||||
}
|
||||
nextConfig = {
|
||||
...nextConfig,
|
||||
gateway: {
|
||||
...nextConfig.gateway,
|
||||
auth: {
|
||||
...nextConfig.gateway?.auth,
|
||||
mode: "password",
|
||||
password,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
nextConfig = {
|
||||
...nextConfig,
|
||||
gateway: {
|
||||
...nextConfig.gateway,
|
||||
port,
|
||||
bind,
|
||||
tailscale: {
|
||||
...nextConfig.gateway?.tailscale,
|
||||
mode: tailscaleMode,
|
||||
resetOnExit: tailscaleResetOnExit,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
return {
|
||||
nextConfig,
|
||||
port,
|
||||
bind,
|
||||
authMode,
|
||||
tailscaleMode,
|
||||
tailscaleResetOnExit,
|
||||
gatewayToken,
|
||||
};
|
||||
}
|
||||
38
src/commands/onboard-non-interactive/local/output.ts
Normal file
38
src/commands/onboard-non-interactive/local/output.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import type { RuntimeEnv } from "../../../runtime.js";
|
||||
import type { OnboardOptions } from "../../onboard-types.js";
|
||||
|
||||
export function logNonInteractiveOnboardingJson(params: {
|
||||
opts: OnboardOptions;
|
||||
runtime: RuntimeEnv;
|
||||
mode: "local" | "remote";
|
||||
workspaceDir?: string;
|
||||
authChoice?: string;
|
||||
gateway?: {
|
||||
port: number;
|
||||
bind: string;
|
||||
authMode: string;
|
||||
tailscaleMode: string;
|
||||
};
|
||||
installDaemon?: boolean;
|
||||
daemonRuntime?: string;
|
||||
skipSkills?: boolean;
|
||||
skipHealth?: boolean;
|
||||
}) {
|
||||
if (!params.opts.json) return;
|
||||
params.runtime.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
mode: params.mode,
|
||||
workspace: params.workspaceDir,
|
||||
authChoice: params.authChoice,
|
||||
gateway: params.gateway,
|
||||
installDaemon: Boolean(params.installDaemon),
|
||||
daemonRuntime: params.daemonRuntime,
|
||||
skipSkills: Boolean(params.skipSkills),
|
||||
skipHealth: Boolean(params.skipHealth),
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
}
|
||||
29
src/commands/onboard-non-interactive/local/skills-config.ts
Normal file
29
src/commands/onboard-non-interactive/local/skills-config.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import type { ClawdbotConfig } from "../../../config/config.js";
|
||||
import type { RuntimeEnv } from "../../../runtime.js";
|
||||
import type { OnboardOptions } from "../../onboard-types.js";
|
||||
|
||||
export function applyNonInteractiveSkillsConfig(params: {
|
||||
nextConfig: ClawdbotConfig;
|
||||
opts: OnboardOptions;
|
||||
runtime: RuntimeEnv;
|
||||
}) {
|
||||
const { nextConfig, opts, runtime } = params;
|
||||
if (opts.skipSkills) return nextConfig;
|
||||
|
||||
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;
|
||||
}
|
||||
return {
|
||||
...nextConfig,
|
||||
skills: {
|
||||
...nextConfig.skills,
|
||||
install: {
|
||||
...nextConfig.skills?.install,
|
||||
nodeManager,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
16
src/commands/onboard-non-interactive/local/workspace.ts
Normal file
16
src/commands/onboard-non-interactive/local/workspace.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import type { ClawdbotConfig } from "../../../config/config.js";
|
||||
import { resolveUserPath } from "../../../utils.js";
|
||||
import type { OnboardOptions } from "../../onboard-types.js";
|
||||
|
||||
export function resolveNonInteractiveWorkspaceDir(params: {
|
||||
opts: OnboardOptions;
|
||||
baseConfig: ClawdbotConfig;
|
||||
defaultWorkspaceDir: string;
|
||||
}) {
|
||||
const raw = (
|
||||
params.opts.workspace ??
|
||||
params.baseConfig.agents?.defaults?.workspace ??
|
||||
params.defaultWorkspaceDir
|
||||
).trim();
|
||||
return resolveUserPath(raw);
|
||||
}
|
||||
48
src/commands/onboard-non-interactive/remote.ts
Normal file
48
src/commands/onboard-non-interactive/remote.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import type { ClawdbotConfig } from "../../config/config.js";
|
||||
import { CONFIG_PATH_CLAWDBOT, writeConfigFile } from "../../config/config.js";
|
||||
import type { RuntimeEnv } from "../../runtime.js";
|
||||
import { applyWizardMetadata } from "../onboard-helpers.js";
|
||||
import type { OnboardOptions } from "../onboard-types.js";
|
||||
|
||||
export async function runNonInteractiveOnboardingRemote(params: {
|
||||
opts: OnboardOptions;
|
||||
runtime: RuntimeEnv;
|
||||
baseConfig: ClawdbotConfig;
|
||||
}) {
|
||||
const { opts, runtime, baseConfig } = params;
|
||||
const mode = "remote" as const;
|
||||
|
||||
const remoteUrl = opts.remoteUrl?.trim();
|
||||
if (!remoteUrl) {
|
||||
runtime.error("Missing --remote-url for remote mode.");
|
||||
runtime.exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
let nextConfig: ClawdbotConfig = {
|
||||
...baseConfig,
|
||||
gateway: {
|
||||
...baseConfig.gateway,
|
||||
mode: "remote",
|
||||
remote: {
|
||||
url: remoteUrl,
|
||||
token: opts.remoteToken?.trim() || undefined,
|
||||
},
|
||||
},
|
||||
};
|
||||
nextConfig = applyWizardMetadata(nextConfig, { command: "onboard", mode });
|
||||
await writeConfigFile(nextConfig);
|
||||
runtime.log(`Updated ${CONFIG_PATH_CLAWDBOT}`);
|
||||
|
||||
const payload = {
|
||||
mode,
|
||||
remoteUrl,
|
||||
auth: opts.remoteToken ? "token" : "none",
|
||||
};
|
||||
if (opts.json) {
|
||||
runtime.log(JSON.stringify(payload, null, 2));
|
||||
} else {
|
||||
runtime.log(`Remote gateway: ${remoteUrl}`);
|
||||
runtime.log(`Auth: ${payload.auth}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user