|
| 1 | +import os from 'node:os' |
| 2 | +import path from 'node:path' |
| 3 | + |
| 4 | +/** |
| 5 | + * Shared resolver for autonomous-agent home/config locations (Hermes, OpenClaw). |
| 6 | + * |
| 7 | + * Lives in `connectors/shared` so both the skill and MCP connectors can resolve |
| 8 | + * the same root without the MCP connector importing skill internals. When no |
| 9 | + * options are supplied the resolver falls back to `process.env` / `os.homedir()` |
| 10 | + * so call sites without an injection seam (e.g. an MCP `configPathResolver`) |
| 11 | + * still honor `HERMES_HOME` / `OPENCLAW_STATE_DIR` / `OPENCLAW_CONFIG_PATH`. |
| 12 | + */ |
| 13 | +export type AgentPathResolverOptions = { |
| 14 | + env?: NodeJS.ProcessEnv |
| 15 | + homeDir?: string |
| 16 | +} |
| 17 | + |
| 18 | +const resolveEnv = (options?: AgentPathResolverOptions): NodeJS.ProcessEnv => options?.env ?? process.env |
| 19 | + |
| 20 | +const resolveHomeDir = (options?: AgentPathResolverOptions): string => options?.homeDir ?? os.homedir() |
| 21 | + |
| 22 | +export function resolveUserPath(input: string, options?: AgentPathResolverOptions): string { |
| 23 | + const value = input.trim() |
| 24 | + const homeDir = resolveHomeDir(options) |
| 25 | + if (value === '~') { |
| 26 | + return homeDir |
| 27 | + } |
| 28 | + |
| 29 | + if (value.startsWith('~/')) { |
| 30 | + return path.join(homeDir, value.slice(2)) |
| 31 | + } |
| 32 | + |
| 33 | + if (path.isAbsolute(value)) { |
| 34 | + return value |
| 35 | + } |
| 36 | + |
| 37 | + return path.join(homeDir, value) |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * OpenClaw home dir, mirroring OpenClaw's `resolveRequiredHomeDir`: `OPENCLAW_HOME` |
| 42 | + * wins (with `~` expanded against the base home), otherwise the injected/OS home. |
| 43 | + * `options.homeDir` stands in for OpenClaw's OS-home chain (HOME/USERPROFILE/os.homedir). |
| 44 | + */ |
| 45 | +export function resolveOpenClawHomeDir(options?: AgentPathResolverOptions): string { |
| 46 | + const base = resolveHomeDir(options) |
| 47 | + const override = resolveEnv(options).OPENCLAW_HOME?.trim() |
| 48 | + if (!override) { |
| 49 | + return base |
| 50 | + } |
| 51 | + |
| 52 | + if (override === '~' || override.startsWith('~/') || override.startsWith('~\\')) { |
| 53 | + return path.resolve(override.replace(/^~(?=$|[\\/])/u, base)) |
| 54 | + } |
| 55 | + |
| 56 | + return path.resolve(override) |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * OpenClaw path resolution, mirroring OpenClaw's `resolveHomeRelativePath`: |
| 61 | + * `~`-prefixed expands against the OpenClaw home; every other value is |
| 62 | + * `path.resolve`d (i.e. relative paths are CWD-relative, not home-relative). |
| 63 | + */ |
| 64 | +export function resolveOpenClawUserPath(input: string, options?: AgentPathResolverOptions): string { |
| 65 | + const trimmed = input.trim() |
| 66 | + if (!trimmed) { |
| 67 | + return trimmed |
| 68 | + } |
| 69 | + |
| 70 | + if (trimmed.startsWith('~')) { |
| 71 | + const home = resolveOpenClawHomeDir(options) |
| 72 | + const expanded = trimmed === '~' ? home : path.join(home, trimmed.replace(/^~[\\/]/u, '')) |
| 73 | + return path.resolve(expanded) |
| 74 | + } |
| 75 | + |
| 76 | + return path.resolve(trimmed) |
| 77 | +} |
| 78 | + |
| 79 | +export function resolveOpenClawStateDir(options?: AgentPathResolverOptions): string { |
| 80 | + const override = resolveEnv(options).OPENCLAW_STATE_DIR?.trim() |
| 81 | + if (override) { |
| 82 | + return resolveOpenClawUserPath(override, options) |
| 83 | + } |
| 84 | + |
| 85 | + return path.join(resolveOpenClawHomeDir(options), '.openclaw') |
| 86 | +} |
| 87 | + |
| 88 | +export function resolveOpenClawConfigPath(options?: AgentPathResolverOptions): string { |
| 89 | + const override = resolveEnv(options).OPENCLAW_CONFIG_PATH?.trim() |
| 90 | + if (override) { |
| 91 | + return resolveOpenClawUserPath(override, options) |
| 92 | + } |
| 93 | + |
| 94 | + return path.join(resolveOpenClawStateDir(options), 'openclaw.json') |
| 95 | +} |
| 96 | + |
| 97 | +export function resolveHermesHome(options?: AgentPathResolverOptions): string { |
| 98 | + const override = resolveEnv(options).HERMES_HOME?.trim() |
| 99 | + if (override) { |
| 100 | + return resolveUserPath(override, options) |
| 101 | + } |
| 102 | + |
| 103 | + return path.join(resolveHomeDir(options), '.hermes') |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Default workspace dir for the OpenClaw default agent, mirroring OpenClaw's |
| 108 | + * `resolveDefaultAgentWorkspaceDir`. Note: this is HOME-based |
| 109 | + * (`<home>/.openclaw/workspace`) and intentionally does NOT honor |
| 110 | + * OPENCLAW_STATE_DIR — only the OPENCLAW_PROFILE suffix. |
| 111 | + */ |
| 112 | +export function resolveOpenClawDefaultWorkspaceDir(options?: AgentPathResolverOptions): string { |
| 113 | + const profile = resolveEnv(options).OPENCLAW_PROFILE?.trim() |
| 114 | + const home = resolveOpenClawHomeDir(options) |
| 115 | + if (profile && profile.toLowerCase() !== 'default') { |
| 116 | + return path.join(home, '.openclaw', `workspace-${profile}`) |
| 117 | + } |
| 118 | + |
| 119 | + return path.join(home, '.openclaw', 'workspace') |
| 120 | +} |
0 commit comments