openclaw/src/config/paths.ts

106 lines
3.1 KiB
TypeScript
Raw Normal View History

2026-01-04 07:05:04 +01:00
import os from "node:os";
import path from "node:path";
2026-01-04 14:32:47 +00:00
import type { ClawdbotConfig } from "./types.js";
2026-01-04 07:05:04 +01:00
/**
2026-01-04 14:32:47 +00:00
* Nix mode detection: When CLAWDBOT_NIX_MODE=1, the gateway is running under Nix.
2026-01-04 07:05:04 +01:00
* In this mode:
* - No auto-install flows should be attempted
* - Missing dependencies should produce actionable Nix-specific error messages
* - Config is managed externally (read-only from Nix perspective)
*/
export function resolveIsNixMode(
env: NodeJS.ProcessEnv = process.env,
): boolean {
2026-01-04 14:32:47 +00:00
return env.CLAWDBOT_NIX_MODE === "1";
2026-01-04 07:05:04 +01:00
}
export const isNixMode = resolveIsNixMode();
/**
* State directory for mutable data (sessions, logs, caches).
2026-01-04 14:32:47 +00:00
* Can be overridden via CLAWDBOT_STATE_DIR environment variable.
* Default: ~/.clawdbot
2026-01-04 07:05:04 +01:00
*/
export function resolveStateDir(
env: NodeJS.ProcessEnv = process.env,
homedir: () => string = os.homedir,
): string {
const override =
env.CLAWDBOT_STATE_DIR?.trim() || env.CLAWDIS_STATE_DIR?.trim();
if (override) return resolveUserPath(override);
2026-01-04 14:32:47 +00:00
return path.join(homedir(), ".clawdbot");
2026-01-04 07:05:04 +01:00
}
function resolveUserPath(input: string): string {
const trimmed = input.trim();
if (!trimmed) return trimmed;
if (trimmed.startsWith("~")) {
2026-01-08 03:02:46 +00:00
const expanded = trimmed.replace(/^~(?=$|[\\/])/, os.homedir());
return path.resolve(expanded);
}
return path.resolve(trimmed);
}
2026-01-04 14:32:47 +00:00
export const STATE_DIR_CLAWDBOT = resolveStateDir();
2026-01-04 07:05:04 +01:00
/**
* Config file path (JSON5).
2026-01-04 14:32:47 +00:00
* Can be overridden via CLAWDBOT_CONFIG_PATH environment variable.
* Default: ~/.clawdbot/clawdbot.json (or $CLAWDBOT_STATE_DIR/clawdbot.json)
2026-01-04 07:05:04 +01:00
*/
export function resolveConfigPath(
env: NodeJS.ProcessEnv = process.env,
stateDir: string = resolveStateDir(env, os.homedir),
): string {
2026-01-04 14:32:47 +00:00
const override = env.CLAWDBOT_CONFIG_PATH?.trim();
2026-01-04 07:05:04 +01:00
if (override) return override;
2026-01-04 14:32:47 +00:00
return path.join(stateDir, "clawdbot.json");
2026-01-04 07:05:04 +01:00
}
2026-01-04 14:32:47 +00:00
export const CONFIG_PATH_CLAWDBOT = resolveConfigPath();
2026-01-04 07:05:04 +01:00
export const DEFAULT_GATEWAY_PORT = 18789;
const OAUTH_FILENAME = "oauth.json";
/**
* OAuth credentials storage directory.
*
* Precedence:
* - `CLAWDBOT_OAUTH_DIR` (explicit override)
* - `CLAWDBOT_STATE_DIR/credentials` (canonical server/default)
* - `~/.clawdbot/credentials` (legacy default)
*/
export function resolveOAuthDir(
env: NodeJS.ProcessEnv = process.env,
stateDir: string = resolveStateDir(env, os.homedir),
): string {
const override = env.CLAWDBOT_OAUTH_DIR?.trim();
if (override) return resolveUserPath(override);
return path.join(stateDir, "credentials");
}
export function resolveOAuthPath(
env: NodeJS.ProcessEnv = process.env,
stateDir: string = resolveStateDir(env, os.homedir),
): string {
return path.join(resolveOAuthDir(env, stateDir), OAUTH_FILENAME);
}
2026-01-04 07:05:04 +01:00
export function resolveGatewayPort(
2026-01-04 14:32:47 +00:00
cfg?: ClawdbotConfig,
2026-01-04 07:05:04 +01:00
env: NodeJS.ProcessEnv = process.env,
): number {
2026-01-04 14:32:47 +00:00
const envRaw = env.CLAWDBOT_GATEWAY_PORT?.trim();
2026-01-04 07:05:04 +01:00
if (envRaw) {
const parsed = Number.parseInt(envRaw, 10);
if (Number.isFinite(parsed) && parsed > 0) return parsed;
}
const configPort = cfg?.gateway?.port;
if (typeof configPort === "number" && Number.isFinite(configPort)) {
if (configPort > 0) return configPort;
}
return DEFAULT_GATEWAY_PORT;
}