/** * Config loader for pi-vertex * * Reads ~/.pi/agent/settings/pi-vertex.json (if present). * All fields are optional — env vars remain a supported fallback. * * Config keys are camelCase versions of the corresponding env var names: * * { * "googleCloudProject": "my-gcp-project", * "googleCloudLocation": "us-central1", * "googleApplicationCredentials": "/path/to/service-account.json" * } */ import { existsSync, readFileSync } from "node:fs"; import { join } from "node:path"; import { getAgentDir } from "@mariozechner/pi-coding-agent"; export function getConfigPath(): string { return join(getAgentDir(), "settings", "pi-vertex.json"); } export interface VertexConfig { /** GCP project ID. Equivalent to GOOGLE_CLOUD_PROJECT. */ googleCloudProject?: string; /** Default region/location. Equivalent to GOOGLE_CLOUD_LOCATION. */ googleCloudLocation?: string; /** Path to a service account JSON key. Equivalent to GOOGLE_APPLICATION_CREDENTIALS. */ googleApplicationCredentials?: string; } let _cachedPath: string | null = null; let _globalCached: VertexConfig | null = null; function loadGlobalConfig(): VertexConfig { const configPath = getConfigPath(); // Bust cache if agentDir changed (e.g. different process env) if (_cachedPath !== configPath) { _cachedPath = configPath; _globalCached = null; } if (_globalCached !== null) return _globalCached; if (!existsSync(configPath)) { _globalCached = {}; return _globalCached; } try { _globalCached = JSON.parse(readFileSync(configPath, "utf-8")) as VertexConfig; return _globalCached; } catch (err) { console.warn(`[pi-vertex] Failed to parse ${configPath}: ${err}`); _globalCached = {}; return _globalCached; } } /** Load config from global settings. */ export function loadConfig(): VertexConfig { return loadGlobalConfig(); }