import { homedir } from "os" import { join } from "path" import { die } from "./fmt.ts" /** Read the ANTHROPIC_API_KEY from ~/.env. Returns undefined if not found. */ export async function getApiKey(): Promise { const envFile = Bun.file(join(homedir(), ".env")) if (!(await envFile.exists())) return undefined const envContent = await envFile.text() return envContent.match(/^(?:export\s+)?ANTHROPIC_API_KEY=["']?([^"'\s]+)["']?/m)?.[1] } /** Read the ANTHROPIC_API_KEY from ~/.env, dying if not found. */ export async function requireApiKey(): Promise { const key = await getApiKey() if (!key) die("ANTHROPIC_API_KEY not found in ~/.env") return key }