import { parse } from "@dotenvx/dotenvx"; import { getGlobalEnvPath, readGlobalEnvFile } from "./config"; export interface LoadResult { path: string; exists: boolean; loadedKeys: string[]; skippedKeys: string[]; } function mergeEnv( parsed: Record, target: NodeJS.ProcessEnv, ): { loadedKeys: string[]; skippedKeys: string[] } { const loadedKeys: string[] = []; const skippedKeys: string[] = []; for (const [key, value] of Object.entries(parsed)) { if (typeof value !== "string") continue; if (target[key] !== undefined) { skippedKeys.push(key); continue; } target[key] = value; loadedKeys.push(key); } return { loadedKeys, skippedKeys }; } export function loadGlobalEnv( target: NodeJS.ProcessEnv = process.env, filePath = getGlobalEnvPath(), ): LoadResult { try { const envFile = readGlobalEnvFile(filePath); if (!envFile.exists || envFile.content === undefined) { return { path: envFile.path, exists: false, loadedKeys: [], skippedKeys: [], }; } const parsed = parse(envFile.content, { processEnv: {} }); const result = mergeEnv(parsed, target); return { path: envFile.path, exists: true, loadedKeys: result.loadedKeys, skippedKeys: result.skippedKeys, }; } catch (error) { const message = error instanceof Error ? error.message : String(error); throw new Error(`Failed to load ~/.pi/agent/.env: ${message}`, { cause: error, }); } }