|
| 1 | +/** |
| 2 | + * The on-disk loopover CLI config, resolved the same way by every bin that reads it (#9521). |
| 3 | + * |
| 4 | + * `@loopover/mcp` owns this file (it is what `loopover-mcp login` writes) and `@loopover/miner` reads |
| 5 | + * it, but the two are separately-installable CLIs on purpose -- installing AMS must not drag in the |
| 6 | + * MCP wrapper just to parse a config format. So the miner hand-copied the resolution and said so in a |
| 7 | + * header comment ("kept in sync by hand -- there is no shared module to import"). This is that module: |
| 8 | + * both packages already depend on @loopover/contract, so it is the one home both can reach. |
| 9 | + * |
| 10 | + * Everything here is PURE -- no node: imports, no I/O. That is not incidental: this package's tsconfig |
| 11 | + * sets `"types": []` precisely so a node builtin cannot compile here and then fail later in the |
| 12 | + * Cloudflare bundle, and the root entry is imported by the Worker. So the caller does the reading |
| 13 | + * (`existsSync`/`readFileSync`, `homedir`, `join`) and this module owns the POLICY -- the path |
| 14 | + * template, the name pattern, the API-URL precedence, the constants. Those are what actually drifted |
| 15 | + * between the two copies; the four lines of fs around them never did. |
| 16 | + * |
| 17 | + * Scope is the READ side only. Writing the config (profile create/switch/remove, redaction, |
| 18 | + * persistence) stays in `@loopover/mcp`, which is the only package that writes it. |
| 19 | + * |
| 20 | + * Named `cli-config`, NOT `local-config`: a common global-gitignore pattern (`local-config.*`) matches the |
| 21 | + * latter, and this file's first incarnation was silently dropped from every commit by exactly that -- the |
| 22 | + * repo built locally, and CI failed on a module that had never been committed. Do not rename it back. |
| 23 | + */ |
| 24 | + |
| 25 | +export const DEFAULT_LOOPOVER_API_URL = "https://api.loopover.ai"; |
| 26 | + |
| 27 | +/** |
| 28 | + * API URLs that used to be the shipped default. A config still naming one is a stale artifact of an |
| 29 | + * older install, not a deliberate override, so resolution SKIPS them rather than honoring them. |
| 30 | + */ |
| 31 | +export const LEGACY_LOOPOVER_API_URLS: ReadonlySet<string> = new Set([ |
| 32 | + "https://gittensory-api.zeronode.workers.dev", |
| 33 | + "https://gittensory-api.aethereal.dev", |
| 34 | +]); |
| 35 | + |
| 36 | +export const DEFAULT_PROFILE_NAME = "default"; |
| 37 | + |
| 38 | +/** 1-64 chars, starting alphanumeric. The same pattern both bins validated against by hand. */ |
| 39 | +export const PROFILE_NAME_PATTERN = /^[a-z0-9][a-z0-9._-]{0,63}$/; |
| 40 | + |
| 41 | +export type LoopoverConfigProfile = { |
| 42 | + apiUrl?: unknown; |
| 43 | + session?: { token?: unknown } | null | undefined; |
| 44 | +}; |
| 45 | + |
| 46 | +export type LoopoverConfig = { |
| 47 | + activeProfile?: unknown; |
| 48 | + profiles?: Record<string, LoopoverConfigProfile | undefined>; |
| 49 | + apiUrl?: unknown; |
| 50 | +}; |
| 51 | + |
| 52 | +/** |
| 53 | + * The environment reads that steer config location. A plain object so no caller needs `process` -- |
| 54 | + * and the index signature is what lets a caller pass `process.env` straight in (without it TS's |
| 55 | + * weak-type check rejects ProcessEnv, whose own properties are all index-signature entries). |
| 56 | + */ |
| 57 | +export type LoopoverConfigEnv = { |
| 58 | + readonly LOOPOVER_CONFIG_PATH?: string | undefined; |
| 59 | + readonly LOOPOVER_CONFIG_DIR?: string | undefined; |
| 60 | + readonly XDG_CONFIG_HOME?: string | undefined; |
| 61 | + readonly LOOPOVER_API_URL?: string | undefined; |
| 62 | + readonly [key: string]: string | undefined; |
| 63 | +}; |
| 64 | + |
| 65 | +/** |
| 66 | + * Where the config lives: LOOPOVER_CONFIG_PATH wins outright; else LOOPOVER_CONFIG_DIR/config.json; |
| 67 | + * else the XDG location under the home directory. |
| 68 | + * |
| 69 | + * `join` and `homeDir` are injected rather than imported so this stays free of node:path/node:os -- |
| 70 | + * callers pass node's own, which keeps Windows separators correct. |
| 71 | + */ |
| 72 | +export function loopoverConfigPath( |
| 73 | + env: LoopoverConfigEnv, |
| 74 | + deps: { join: (...segments: string[]) => string; homeDir: () => string }, |
| 75 | +): string { |
| 76 | + if (env.LOOPOVER_CONFIG_PATH) return env.LOOPOVER_CONFIG_PATH; |
| 77 | + if (env.LOOPOVER_CONFIG_DIR) return deps.join(env.LOOPOVER_CONFIG_DIR, "config.json"); |
| 78 | + return deps.join(env.XDG_CONFIG_HOME || deps.join(deps.homeDir(), ".config"), "loopover", "config.json"); |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * The config a raw file body describes, or `{}` for any reason it cannot be understood (absent file, |
| 83 | + * malformed JSON, or a non-object top level). Never throws and never reports WHY: a missing config is |
| 84 | + * the normal state for a fresh install, and the failure paths must not leak the path or its contents. |
| 85 | + */ |
| 86 | +export function parseLoopoverConfig(body: string | null | undefined): LoopoverConfig { |
| 87 | + if (!body) return {}; |
| 88 | + try { |
| 89 | + const parsed: unknown = JSON.parse(body); |
| 90 | + return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? (parsed as LoopoverConfig) : {}; |
| 91 | + } catch { |
| 92 | + return {}; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/** The canonical (trimmed, lowercased) spelling of a profile name, or null when it is not a legal one. */ |
| 97 | +export function canonicalProfileName(value: unknown): string | null { |
| 98 | + const name = String(value ?? "").trim().toLowerCase(); |
| 99 | + return PROFILE_NAME_PATTERN.test(name) ? name : null; |
| 100 | +} |
| 101 | + |
| 102 | +/** The session token recorded for a profile, or null when that profile has never logged in. */ |
| 103 | +export function profileSessionToken(profile: LoopoverConfigProfile | undefined): string | null { |
| 104 | + const token = profile?.session?.token; |
| 105 | + return typeof token === "string" && token ? token : null; |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * The API URL to call: LOOPOVER_API_URL, else the active profile's apiUrl, else the config's top-level |
| 110 | + * apiUrl, else the default -- skipping any LEGACY_LOOPOVER_API_URLS entry at every step. |
| 111 | + * |
| 112 | + * The fall-THROUGH matters and is the behavior the two copies had drifted on (#9521). @loopover/mcp |
| 113 | + * picked the profile apiUrl if present and fell straight to the default when it was legacy, so a stale |
| 114 | + * profile URL masked a perfectly good top-level override; @loopover/miner kept looking (#8854). The |
| 115 | + * miner's is correct -- a legacy value means "ignore this one," not "stop looking" -- so it is what |
| 116 | + * this shared resolver does for both. |
| 117 | + */ |
| 118 | +export function resolveLoopoverApiUrl( |
| 119 | + env: LoopoverConfigEnv, |
| 120 | + config: LoopoverConfig, |
| 121 | + profile: LoopoverConfigProfile | undefined, |
| 122 | +): string { |
| 123 | + if (env.LOOPOVER_API_URL) return env.LOOPOVER_API_URL.replace(/\/+$/, ""); |
| 124 | + for (const candidate of [profile?.apiUrl, config.apiUrl]) { |
| 125 | + if (typeof candidate === "string" && candidate.trim()) { |
| 126 | + const normalized = candidate.replace(/\/+$/, ""); |
| 127 | + if (!LEGACY_LOOPOVER_API_URLS.has(normalized)) return normalized; |
| 128 | + } |
| 129 | + } |
| 130 | + return DEFAULT_LOOPOVER_API_URL; |
| 131 | +} |
0 commit comments