burnwatch 0.4.2 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,13 +5,21 @@ All notable changes to burnwatch will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.4.3] - 2026-03-24
9
+
10
+ ### Fixed
11
+
12
+ - **Non-interactive init auto-applies default plans**: `burnwatch init` (non-interactive or re-run) now picks each service's default plan from the registry. Flat-rate services with a cost (e.g., Scrapfly Scale at $80/mo) get budget auto-set to plan cost. Services with existing API keys in global config are auto-detected as LIVE.
13
+ - **Init output shows tier and budget per service**: Non-interactive init now prints each service with its confidence tier, plan name, and budget (if set), plus a clear list of which services still need budgets with the exact `burnwatch add` commands.
14
+ - **Untracked message is actionable**: Changed circular "run burnwatch status" message to "run burnwatch init to configure".
15
+
8
16
  ## [0.4.2] - 2026-03-24
9
17
 
10
18
  ### Fixed
11
19
 
12
- - **Init is re-runnable**: `burnwatch init` no longer early-returns on already-initialized projects. Re-running init re-detects services and walks through interactive setup again, so users who initialized before v0.4.0 can configure budgets without manually running `burnwatch add` 14 times.
13
- - **Budget prompt fires for all services**: Budget prompt was gated inside the `requiresKey` block - services without API key requirements never got asked. Now every non-excluded service gets a budget prompt during interactive init.
14
- - **Untracked message is actionable**: Changed circular "run burnwatch status" message to "run burnwatch init to configure" so users know what to do next.
20
+ - **Init is re-runnable**: `burnwatch init` no longer early-returns on already-initialized projects. Re-running init re-detects services and walks through interactive setup again.
21
+ - **Budget prompt fires for all services**: Budget prompt was gated inside the `requiresKey` block - now every non-excluded service gets a budget prompt during interactive init.
22
+ - **Untracked message fix**: Same as 0.4.3 (first shipped here).
15
23
 
16
24
  ## [0.4.0] - 2026-03-24
17
25
 
@@ -51,6 +59,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
51
59
  - Snapshot system for delta computation across sessions
52
60
  - Claude Code skills: `/spend` (on-demand brief), `/setup-burnwatch` (guided onboarding)
53
61
 
62
+ [0.4.3]: https://github.com/RaleighSF/burnwatch/compare/v0.4.2...v0.4.3
54
63
  [0.4.2]: https://github.com/RaleighSF/burnwatch/compare/v0.4.0...v0.4.2
55
64
  [0.4.0]: https://github.com/RaleighSF/burnwatch/compare/v0.1.0...v0.4.0
56
65
  [0.1.0]: https://github.com/RaleighSF/burnwatch/releases/tag/v0.1.0
package/dist/cli.js CHANGED
@@ -1029,16 +1029,33 @@ async function cmdInit() {
1029
1029
  const result = await runInteractiveInit(detected);
1030
1030
  config.services = result.services;
1031
1031
  } else {
1032
+ const globalConfig = readGlobalConfig();
1032
1033
  for (const det of detected) {
1033
- if (!config.services[det.service.id]) {
1034
- const tracked2 = {
1035
- serviceId: det.service.id,
1036
- detectedVia: det.sources,
1037
- hasApiKey: false,
1038
- firstDetected: (/* @__PURE__ */ new Date()).toISOString()
1039
- };
1040
- config.services[det.service.id] = tracked2;
1034
+ const existing = config.services[det.service.id];
1035
+ const service = det.service;
1036
+ const plans = service.plans ?? [];
1037
+ const defaultPlan = plans.find((p) => p.default) ?? plans[0];
1038
+ if (existing?.budget !== void 0 && existing.budget > 0) continue;
1039
+ const tracked2 = {
1040
+ serviceId: service.id,
1041
+ detectedVia: existing?.detectedVia ?? det.sources,
1042
+ hasApiKey: existing?.hasApiKey ?? false,
1043
+ firstDetected: existing?.firstDetected ?? (/* @__PURE__ */ new Date()).toISOString()
1044
+ };
1045
+ if (defaultPlan && defaultPlan.type !== "exclude") {
1046
+ tracked2.planName = defaultPlan.name;
1047
+ if (defaultPlan.type === "flat" && defaultPlan.monthlyBase !== void 0) {
1048
+ tracked2.planCost = defaultPlan.monthlyBase;
1049
+ if (defaultPlan.monthlyBase > 0) {
1050
+ tracked2.budget = defaultPlan.monthlyBase;
1051
+ }
1052
+ }
1053
+ }
1054
+ const existingKey = globalConfig.services[service.id]?.apiKey;
1055
+ if (existingKey) {
1056
+ tracked2.hasApiKey = true;
1041
1057
  }
1058
+ config.services[service.id] = tracked2;
1042
1059
  }
1043
1060
  if (detected.length === 0) {
1044
1061
  console.log(" No paid services detected yet.");
@@ -1046,12 +1063,25 @@ async function cmdInit() {
1046
1063
  } else {
1047
1064
  console.log(` Found ${detected.length} paid service${detected.length > 1 ? "s" : ""}:
1048
1065
  `);
1066
+ const needBudget = [];
1049
1067
  for (const det of detected) {
1050
- const tierBadge = det.service.apiTier === "live" ? "\u2705 LIVE API available" : det.service.apiTier === "calc" ? "\u{1F7E1} Flat-rate tracking" : det.service.apiTier === "est" ? "\u{1F7E0} Estimate tracking" : "\u{1F534} Detection only";
1051
- console.log(` \u2022 ${det.service.name} (${tierBadge})`);
1052
- console.log(` Detected via: ${det.details.join(", ")}`);
1068
+ const svc = config.services[det.service.id];
1069
+ const tierBadge = det.service.apiTier === "live" ? svc?.hasApiKey ? "\u2705 LIVE" : "\u{1F534} BLIND" : det.service.apiTier === "calc" ? "\u{1F7E1} CALC" : det.service.apiTier === "est" ? "\u{1F7E0} EST" : "\u{1F534} BLIND";
1070
+ const planStr = svc?.planName ? ` (${svc.planName})` : "";
1071
+ const budgetStr = svc?.budget ? ` - $${svc.budget}/mo budget` : "";
1072
+ console.log(` \u2022 ${det.service.name}${planStr} ${tierBadge}${budgetStr}`);
1073
+ if (!svc?.budget) {
1074
+ needBudget.push(det.service.id);
1075
+ }
1053
1076
  }
1054
1077
  console.log("");
1078
+ if (needBudget.length > 0) {
1079
+ console.log(` ${needBudget.length} service${needBudget.length > 1 ? "s" : ""} need budgets. Set them with:`);
1080
+ for (const id of needBudget) {
1081
+ console.log(` burnwatch add ${id} --budget <AMOUNT>`);
1082
+ }
1083
+ console.log("");
1084
+ }
1055
1085
  }
1056
1086
  }
1057
1087
  writeProjectConfig(config, projectRoot);
package/dist/cli.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/cli.ts","../src/core/config.ts","../src/detection/detector.ts","../src/core/registry.ts","../src/services/base.ts","../src/services/anthropic.ts","../src/services/openai.ts","../src/services/vercel.ts","../src/services/scrapfly.ts","../src/services/index.ts","../src/core/types.ts","../src/core/brief.ts","../src/core/ledger.ts","../src/interactive-init.ts"],"sourcesContent":["#!/usr/bin/env node\n\n/**\n * burnwatch CLI\n *\n * Usage:\n * burnwatch init — Initialize in current project\n * burnwatch add <service> [options] — Register a service\n * burnwatch status — Show current spend brief\n * burnwatch reconcile — Scan for untracked sessions\n */\n\nimport * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport {\n ensureProjectDirs,\n readProjectConfig,\n writeProjectConfig,\n readGlobalConfig,\n writeGlobalConfig,\n projectConfigDir,\n isInitialized,\n} from \"./core/config.js\";\nimport type { ProjectConfig } from \"./core/config.js\";\nimport type { TrackedService } from \"./core/types.js\";\nimport { detectServices } from \"./detection/detector.js\";\nimport { pollAllServices } from \"./services/index.js\";\nimport { buildSnapshot, buildBrief, formatBrief } from \"./core/brief.js\";\nimport { writeLedger, saveSnapshot } from \"./core/ledger.js\";\nimport { getService, getAllServices } from \"./core/registry.js\";\nimport { runInteractiveInit } from \"./interactive-init.js\";\n\nconst args = process.argv.slice(2);\nconst command = args[0];\nconst flags = new Set(args.slice(1));\n\nasync function main(): Promise<void> {\n switch (command) {\n case \"init\":\n case \"setup\":\n await cmdInit();\n break;\n case \"add\":\n await cmdAdd();\n break;\n case \"status\":\n await cmdStatus();\n break;\n case \"services\":\n cmdServices();\n break;\n case \"reconcile\":\n await cmdReconcile();\n break;\n case \"help\":\n case \"--help\":\n case \"-h\":\n cmdHelp();\n break;\n case \"version\":\n case \"--version\":\n case \"-v\":\n cmdVersion();\n break;\n default:\n if (command) {\n console.error(`Unknown command: ${command}`);\n console.error('Run \"burnwatch help\" for usage.');\n process.exit(1);\n }\n cmdHelp();\n }\n}\n\n// --- Commands ---\n\nasync function cmdInit(): Promise<void> {\n const projectRoot = process.cwd();\n const nonInteractive = flags.has(\"--non-interactive\") || flags.has(\"--ni\");\n const alreadyInitialized = isInitialized(projectRoot);\n\n // Detect project name from package.json\n let projectName = path.basename(projectRoot);\n try {\n const pkgPath = path.join(projectRoot, \"package.json\");\n const pkg = JSON.parse(fs.readFileSync(pkgPath, \"utf-8\")) as {\n name?: string;\n };\n if (pkg.name) projectName = pkg.name;\n } catch {\n // Use directory name\n }\n\n // Create directories (idempotent)\n ensureProjectDirs(projectRoot);\n\n // Run detection\n console.log(\"🔍 Scanning project for paid services...\\n\");\n const detected = detectServices(projectRoot);\n\n // Load existing config or create new one\n const existingConfig = alreadyInitialized ? readProjectConfig(projectRoot) : null;\n const config: ProjectConfig = {\n projectName: existingConfig?.projectName ?? projectName,\n services: existingConfig?.services ?? {},\n createdAt: existingConfig?.createdAt ?? new Date().toISOString(),\n updatedAt: new Date().toISOString(),\n };\n\n if (!nonInteractive && detected.length > 0 && process.stdin.isTTY) {\n // Interactive mode — walk through each service with plan tiers\n const result = await runInteractiveInit(detected);\n config.services = result.services;\n } else {\n // Non-interactive mode — auto-register all detected services\n for (const det of detected) {\n if (!config.services[det.service.id]) {\n const tracked: TrackedService = {\n serviceId: det.service.id,\n detectedVia: det.sources,\n hasApiKey: false,\n firstDetected: new Date().toISOString(),\n };\n config.services[det.service.id] = tracked;\n }\n }\n\n // Report findings\n if (detected.length === 0) {\n console.log(\" No paid services detected yet.\");\n console.log(' Services will be detected as they enter your project.\\n');\n } else {\n console.log(` Found ${detected.length} paid service${detected.length > 1 ? \"s\" : \"\"}:\\n`);\n for (const det of detected) {\n const tierBadge =\n det.service.apiTier === \"live\"\n ? \"✅ LIVE API available\"\n : det.service.apiTier === \"calc\"\n ? \"🟡 Flat-rate tracking\"\n : det.service.apiTier === \"est\"\n ? \"🟠 Estimate tracking\"\n : \"🔴 Detection only\";\n\n console.log(` • ${det.service.name} (${tierBadge})`);\n console.log(` Detected via: ${det.details.join(\", \")}`);\n }\n console.log(\"\");\n }\n }\n\n writeProjectConfig(config, projectRoot);\n\n // Write initial .gitignore for the .burnwatch directory\n const gitignorePath = path.join(projectConfigDir(projectRoot), \".gitignore\");\n fs.writeFileSync(\n gitignorePath,\n [\n \"# Burnwatch — ignore cache and snapshots, keep ledger and config\",\n \"data/cache/\",\n \"data/snapshots/\",\n \"data/events.jsonl\",\n \"\",\n ].join(\"\\n\"),\n \"utf-8\",\n );\n\n // Register Claude Code hooks\n console.log(\"\\n🔗 Registering Claude Code hooks...\\n\");\n registerHooks(projectRoot);\n\n // Summary of excluded services\n const excluded = Object.values(config.services).filter((s) => s.excluded);\n const tracked = Object.values(config.services).filter((s) => !s.excluded);\n\n console.log(\"✅ burnwatch initialized!\\n\");\n\n if (tracked.length > 0) {\n console.log(` Tracking ${tracked.length} service${tracked.length > 1 ? \"s\" : \"\"}`);\n for (const svc of tracked) {\n const planStr = svc.planName ? ` (${svc.planName})` : \"\";\n const budgetStr = svc.budget !== undefined ? ` — $${svc.budget}/mo budget` : \"\";\n console.log(` • ${svc.serviceId}${planStr}${budgetStr}`);\n }\n }\n\n if (excluded.length > 0) {\n console.log(`\\n Excluded ${excluded.length} service${excluded.length > 1 ? \"s\" : \"\"}:`);\n for (const svc of excluded) {\n console.log(` • ${svc.serviceId}`);\n }\n }\n\n console.log(\"\\nNext steps:\");\n console.log(\" burnwatch status — Check your spend\");\n console.log(\" burnwatch add <svc> — Configure additional services\\n\");\n}\n\nasync function cmdAdd(): Promise<void> {\n const projectRoot = process.cwd();\n\n if (!isInitialized(projectRoot)) {\n console.error('❌ burnwatch not initialized. Run \"burnwatch init\" first.');\n process.exit(1);\n }\n\n const serviceId = args[1];\n if (!serviceId) {\n console.error(\"Usage: burnwatch add <service> [--key KEY] [--budget N]\");\n process.exit(1);\n }\n\n // Parse options\n const options: Record<string, string> = {};\n for (let i = 2; i < args.length; i++) {\n const arg = args[i]!;\n if (arg.startsWith(\"--\") && i + 1 < args.length) {\n options[arg.slice(2)] = args[i + 1]!;\n i++;\n }\n }\n\n const apiKey = options[\"key\"] ?? options[\"token\"];\n const budget = options[\"budget\"] ? parseFloat(options[\"budget\"]) : undefined;\n const planCost = options[\"plan-cost\"]\n ? parseFloat(options[\"plan-cost\"])\n : undefined;\n\n // Check if service is in registry\n const definition = getService(serviceId, projectRoot);\n if (!definition) {\n console.error(\n `⚠️ \"${serviceId}\" not found in registry. Adding as custom service.`,\n );\n }\n\n // Update project config\n const config = readProjectConfig(projectRoot)!;\n const existing = config.services[serviceId];\n\n const tracked: TrackedService = {\n serviceId,\n detectedVia: existing?.detectedVia ?? [\"manual\"],\n budget: budget ?? existing?.budget,\n hasApiKey: !!apiKey || (existing?.hasApiKey ?? false),\n planCost: planCost ?? existing?.planCost,\n firstDetected: existing?.firstDetected ?? new Date().toISOString(),\n };\n\n config.services[serviceId] = tracked;\n writeProjectConfig(config, projectRoot);\n\n // Save API key to global config (never in project dir)\n if (apiKey) {\n const globalConfig = readGlobalConfig();\n if (!globalConfig.services[serviceId]) {\n globalConfig.services[serviceId] = {};\n }\n globalConfig.services[serviceId]!.apiKey = apiKey;\n writeGlobalConfig(globalConfig);\n console.log(`🔐 API key saved to global config (never stored in project)`);\n }\n\n let tierLabel: string;\n if (!definition) {\n tierLabel = \"🔴 BLIND\";\n } else if (apiKey) {\n tierLabel = \"✅ LIVE\";\n } else if (planCost !== undefined) {\n tierLabel = \"🟡 CALC\";\n } else if (definition.apiTier === \"est\") {\n tierLabel = \"🟠 EST\";\n } else if (definition.apiTier === \"calc\") {\n tierLabel = \"🟡 CALC\";\n } else if (definition.apiTier === \"live\" && !apiKey) {\n tierLabel = `🔴 BLIND (add --key for ✅ LIVE)`;\n } else {\n tierLabel = \"🔴 BLIND\";\n }\n\n console.log(`\\n✅ ${serviceId} configured:`);\n console.log(` Tier: ${tierLabel}`);\n if (budget) console.log(` Budget: $${budget}/mo`);\n if (planCost) console.log(` Plan cost: $${planCost}/mo`);\n console.log(\"\");\n}\n\nasync function cmdStatus(): Promise<void> {\n const projectRoot = process.cwd();\n\n if (!isInitialized(projectRoot)) {\n console.error('❌ burnwatch not initialized. Run \"burnwatch init\" first.');\n process.exit(1);\n }\n\n const config = readProjectConfig(projectRoot)!;\n const trackedServices = Object.values(config.services);\n\n if (trackedServices.length === 0) {\n console.log(\"No services tracked yet.\");\n console.log('Run \"burnwatch add <service>\" to start tracking.');\n return;\n }\n\n console.log(\"📊 Polling services...\\n\");\n\n const results = await pollAllServices(trackedServices);\n const snapshots = results.map((r) =>\n buildSnapshot(\n r.serviceId,\n r.tier,\n r.spend,\n config.services[r.serviceId]?.budget,\n ),\n );\n\n const blindCount = snapshots.filter((s) => s.tier === \"blind\").length;\n const brief = buildBrief(config.projectName, snapshots, blindCount);\n\n // Save snapshot and update ledger\n saveSnapshot(brief, projectRoot);\n writeLedger(brief, projectRoot);\n\n // Display the brief\n console.log(formatBrief(brief));\n console.log(\"\");\n\n if (blindCount > 0) {\n console.log(`⚠️ ${blindCount} service${blindCount > 1 ? \"s\" : \"\"} untracked:`);\n for (const snap of snapshots.filter((s) => s.tier === \"blind\")) {\n console.log(` • ${snap.serviceId}`);\n }\n console.log(`\\n Run 'burnwatch init' to configure budgets and API keys.\\n`);\n }\n}\n\nasync function cmdSetup(): Promise<void> {\n const projectRoot = process.cwd();\n\n // Step 1: Init if needed\n if (!isInitialized(projectRoot)) {\n await cmdInit();\n }\n\n const config = readProjectConfig(projectRoot)!;\n const detected = Object.values(config.services);\n\n if (detected.length === 0) {\n console.log(\"No paid services detected. You're all set!\");\n return;\n }\n\n console.log(\"📋 Auto-configuring detected services...\\n\");\n\n // Step 2: Check global config for existing API keys\n const globalConfig = readGlobalConfig();\n\n // Step 3: Auto-configure each service based on registry tier + available keys\n const liveServices: string[] = [];\n const calcServices: string[] = [];\n const estServices: string[] = [];\n const blindServices: string[] = [];\n\n for (const tracked of detected) {\n const definition = getService(tracked.serviceId, projectRoot);\n if (!definition) continue;\n\n const hasKey = !!globalConfig.services[tracked.serviceId]?.apiKey;\n\n if (hasKey && definition.apiTier === \"live\") {\n tracked.hasApiKey = true;\n liveServices.push(`${definition.name}`);\n } else if (definition.apiTier === \"calc\") {\n calcServices.push(`${definition.name}`);\n } else if (definition.apiTier === \"est\") {\n estServices.push(`${definition.name}`);\n } else {\n blindServices.push(`${definition.name}`);\n }\n }\n\n writeProjectConfig(config, projectRoot);\n\n // Report\n if (liveServices.length > 0) {\n console.log(` ✅ LIVE (real billing data): ${liveServices.join(\", \")}`);\n }\n if (calcServices.length > 0) {\n console.log(` 🟡 CALC (flat-rate tracking): ${calcServices.join(\", \")}`);\n }\n if (estServices.length > 0) {\n console.log(` 🟠 EST (estimated from usage): ${estServices.join(\", \")}`);\n }\n if (blindServices.length > 0) {\n console.log(` 🔴 BLIND (detected, need API key): ${blindServices.join(\", \")}`);\n }\n\n console.log(\"\");\n\n if (blindServices.length > 0) {\n console.log(\"To upgrade BLIND services to LIVE, add API keys:\");\n for (const tracked of detected) {\n const definition = getService(tracked.serviceId, projectRoot);\n if (definition?.apiTier === \"live\" && !tracked.hasApiKey) {\n const envHint = definition.envPatterns[0] ?? \"YOUR_KEY\";\n console.log(` burnwatch add ${tracked.serviceId} --key $${envHint} --budget <N>`);\n }\n }\n console.log(\"\");\n }\n\n console.log(\"To set budgets for any service:\");\n console.log(\" burnwatch add <service> --budget <monthly_amount>\");\n console.log(\"\");\n console.log(\"Or use /setup-burnwatch in Claude Code for guided setup with budget suggestions.\\n\");\n\n // Show brief\n await cmdStatus();\n}\n\nfunction cmdServices(): void {\n const services = getAllServices();\n console.log(`\\n📋 Registry: ${services.length} services available\\n`);\n\n for (const svc of services) {\n const tierBadge =\n svc.apiTier === \"live\"\n ? \"✅ LIVE\"\n : svc.apiTier === \"calc\"\n ? \"🟡 CALC\"\n : svc.apiTier === \"est\"\n ? \"🟠 EST\"\n : \"🔴 BLIND\";\n\n console.log(` ${svc.name.padEnd(24)} ${tierBadge.padEnd(10)} ${svc.billingModel}`);\n }\n\n console.log(\"\");\n}\n\nasync function cmdReconcile(): Promise<void> {\n const projectRoot = process.cwd();\n\n if (!isInitialized(projectRoot)) {\n console.error('❌ burnwatch not initialized. Run \"burnwatch init\" first.');\n process.exit(1);\n }\n\n console.log(\"🔍 Scanning for untracked services and missed sessions...\\n\");\n\n // Re-run detection against current project state\n const detected = detectServices(projectRoot);\n const config = readProjectConfig(projectRoot)!;\n let newCount = 0;\n\n for (const det of detected) {\n if (!config.services[det.service.id]) {\n config.services[det.service.id] = {\n serviceId: det.service.id,\n detectedVia: det.sources,\n hasApiKey: false,\n firstDetected: new Date().toISOString(),\n };\n newCount++;\n console.log(` 🆕 ${det.service.name} — detected via ${det.details.join(\", \")}`);\n }\n }\n\n if (newCount > 0) {\n writeProjectConfig(config, projectRoot);\n console.log(\n `\\n✅ Found ${newCount} new service${newCount > 1 ? \"s\" : \"\"}. Run 'burnwatch status' to see updated brief.`,\n );\n } else {\n console.log(\" ✅ No new services found. All services already tracked.\");\n }\n\n console.log(\"\");\n}\n\nfunction cmdHelp(): void {\n console.log(`\nburnwatch — Passive cost memory for vibe coding\n\nUsage:\n burnwatch init Interactive setup — pick plans per service\n burnwatch init --non-interactive Auto-detect services, no prompts\n burnwatch setup Init + auto-configure all detected services\n burnwatch add <service> [options] Register a service for tracking\n burnwatch status Show current spend brief\n burnwatch services List all services in registry\n burnwatch reconcile Scan for untracked services\n\nOptions for 'add':\n --key <API_KEY> API key for LIVE tracking (saved to ~/.config/burnwatch/)\n --token <TOKEN> Same as --key (alias)\n --budget <AMOUNT> Monthly budget in USD\n --plan-cost <AMOUNT> Monthly plan cost for CALC tracking\n\nExamples:\n burnwatch init\n burnwatch init --non-interactive\n burnwatch add anthropic --key sk-ant-admin-xxx --budget 100\n burnwatch add scrapfly --key scp-xxx --budget 50\n burnwatch add posthog --plan-cost 0 --budget 0\n burnwatch status\n`);\n}\n\nfunction cmdVersion(): void {\n try {\n const pkgPath = path.resolve(\n path.dirname(new URL(import.meta.url).pathname),\n \"../package.json\",\n );\n const pkg = JSON.parse(fs.readFileSync(pkgPath, \"utf-8\")) as {\n version: string;\n };\n console.log(`burnwatch v${pkg.version}`);\n } catch {\n console.log(\"burnwatch v0.1.0\");\n }\n}\n\n// --- Hook Registration ---\n\nfunction registerHooks(projectRoot: string): void {\n // Step 1: Copy hook scripts into .burnwatch/hooks/ for durability.\n // This avoids relying on ephemeral npx cache paths.\n const sourceHooksDir = path.resolve(\n path.dirname(new URL(import.meta.url).pathname),\n \"hooks\",\n );\n const localHooksDir = path.join(projectRoot, \".burnwatch\", \"hooks\");\n fs.mkdirSync(localHooksDir, { recursive: true });\n\n const hookFiles = [\n \"on-session-start.js\",\n \"on-prompt.js\",\n \"on-file-change.js\",\n \"on-stop.js\",\n ];\n\n for (const file of hookFiles) {\n const src = path.join(sourceHooksDir, file);\n const dest = path.join(localHooksDir, file);\n try {\n fs.copyFileSync(src, dest);\n // Also copy sourcemaps if they exist\n const mapSrc = src + \".map\";\n if (fs.existsSync(mapSrc)) {\n fs.copyFileSync(mapSrc, dest + \".map\");\n }\n } catch (err) {\n console.error(` Warning: Could not copy hook ${file}: ${err instanceof Error ? err.message : err}`);\n }\n }\n\n console.log(` Hook scripts copied to ${localHooksDir}`);\n\n // Step 2: Find or create .claude/settings.json — MERGE, never overwrite\n const claudeDir = path.join(projectRoot, \".claude\");\n const settingsPath = path.join(claudeDir, \"settings.json\");\n\n fs.mkdirSync(claudeDir, { recursive: true });\n\n // Read existing settings (preserve everything)\n let settings: Record<string, unknown> = {};\n try {\n const existing = fs.readFileSync(settingsPath, \"utf-8\");\n settings = JSON.parse(existing) as Record<string, unknown>;\n console.log(` Merging into existing ${settingsPath}`);\n } catch {\n // No existing settings — start fresh\n }\n\n // Ensure hooks object exists, preserve all existing hooks\n if (!settings[\"hooks\"] || typeof settings[\"hooks\"] !== \"object\") {\n settings[\"hooks\"] = {};\n }\n const hooks = settings[\"hooks\"] as Record<string, unknown[]>;\n\n // Use the local .burnwatch/hooks/ paths (durable, not ephemeral)\n const hooksDir = localHooksDir;\n\n // SessionStart hook\n if (!hooks[\"SessionStart\"]) hooks[\"SessionStart\"] = [];\n addHookIfMissing(hooks[\"SessionStart\"] as unknown[], \"SessionStart\", {\n matcher: \"startup|resume\",\n hooks: [\n {\n type: \"command\",\n command: `node \"${path.join(hooksDir, \"on-session-start.js\")}\"`,\n timeout: 15,\n },\n ],\n });\n\n // UserPromptSubmit hook\n if (!hooks[\"UserPromptSubmit\"]) hooks[\"UserPromptSubmit\"] = [];\n addHookIfMissing(\n hooks[\"UserPromptSubmit\"] as unknown[],\n \"UserPromptSubmit\",\n {\n hooks: [\n {\n type: \"command\",\n command: `node \"${path.join(hooksDir, \"on-prompt.js\")}\"`,\n timeout: 5,\n },\n ],\n },\n );\n\n // PostToolUse hook (Edit|Write only)\n if (!hooks[\"PostToolUse\"]) hooks[\"PostToolUse\"] = [];\n addHookIfMissing(hooks[\"PostToolUse\"] as unknown[], \"PostToolUse\", {\n matcher: \"Edit|Write\",\n hooks: [\n {\n type: \"command\",\n command: `node \"${path.join(hooksDir, \"on-file-change.js\")}\"`,\n timeout: 5,\n },\n ],\n });\n\n // Stop hook (async — don't block session end)\n if (!hooks[\"Stop\"]) hooks[\"Stop\"] = [];\n addHookIfMissing(hooks[\"Stop\"] as unknown[], \"Stop\", {\n hooks: [\n {\n type: \"command\",\n command: `node \"${path.join(hooksDir, \"on-stop.js\")}\"`,\n timeout: 15,\n async: true,\n },\n ],\n });\n\n settings[\"hooks\"] = hooks;\n fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + \"\\n\", \"utf-8\");\n console.log(` Hooks registered in ${settingsPath}`);\n}\n\nfunction addHookIfMissing(\n hookArray: unknown[],\n _eventName: string,\n hookConfig: unknown,\n): void {\n // Check if burnwatch hook is already registered\n const existing = hookArray.some((h) => {\n const hook = h as { hooks?: Array<{ command?: string }> };\n return hook.hooks?.some((inner) => inner.command?.includes(\"burnwatch\"));\n });\n\n if (!existing) {\n hookArray.push(hookConfig);\n }\n}\n\n// --- Entry ---\n\nmain().catch((err) => {\n console.error(\"Error:\", err instanceof Error ? err.message : err);\n process.exit(1);\n});\n","import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport * as os from \"node:os\";\nimport type { TrackedService } from \"./types.js\";\n\n/**\n * Paths for burnwatch configuration and data.\n *\n * Hybrid model:\n * - Global config (API keys, service credentials): ~/.config/burnwatch/\n * - Project config (budgets, tracked services): .burnwatch/\n * - Project data (ledger, events, cache): .burnwatch/data/\n */\n\n/** Global config directory — stores API keys, never in project dirs. */\nexport function globalConfigDir(): string {\n const xdgConfig = process.env[\"XDG_CONFIG_HOME\"];\n if (xdgConfig) return path.join(xdgConfig, \"burnwatch\");\n return path.join(os.homedir(), \".config\", \"burnwatch\");\n}\n\n/** Project config directory — stores budgets, tracked services. */\nexport function projectConfigDir(projectRoot?: string): string {\n const root = projectRoot ?? process.cwd();\n return path.join(root, \".burnwatch\");\n}\n\n/** Project data directory — stores ledger, events, cache. */\nexport function projectDataDir(projectRoot?: string): string {\n return path.join(projectConfigDir(projectRoot), \"data\");\n}\n\n// --- Global config (API keys) ---\n\nexport interface GlobalConfig {\n services: Record<\n string,\n {\n apiKey?: string;\n token?: string;\n orgId?: string;\n }\n >;\n}\n\nexport function readGlobalConfig(): GlobalConfig {\n const configPath = path.join(globalConfigDir(), \"config.json\");\n try {\n const raw = fs.readFileSync(configPath, \"utf-8\");\n return JSON.parse(raw) as GlobalConfig;\n } catch {\n return { services: {} };\n }\n}\n\nexport function writeGlobalConfig(config: GlobalConfig): void {\n const dir = globalConfigDir();\n fs.mkdirSync(dir, { recursive: true });\n const configPath = path.join(dir, \"config.json\");\n fs.writeFileSync(configPath, JSON.stringify(config, null, 2) + \"\\n\", \"utf-8\");\n // Restrict permissions — this file contains API keys\n fs.chmodSync(configPath, 0o600);\n}\n\n// --- Project config (budgets, tracked services) ---\n\nexport interface ProjectConfig {\n projectName: string;\n services: Record<string, TrackedService>;\n createdAt: string;\n updatedAt: string;\n}\n\nexport function readProjectConfig(projectRoot?: string): ProjectConfig | null {\n const configPath = path.join(projectConfigDir(projectRoot), \"config.json\");\n try {\n const raw = fs.readFileSync(configPath, \"utf-8\");\n return JSON.parse(raw) as ProjectConfig;\n } catch {\n return null;\n }\n}\n\nexport function writeProjectConfig(\n config: ProjectConfig,\n projectRoot?: string,\n): void {\n const dir = projectConfigDir(projectRoot);\n fs.mkdirSync(dir, { recursive: true });\n config.updatedAt = new Date().toISOString();\n const configPath = path.join(dir, \"config.json\");\n fs.writeFileSync(configPath, JSON.stringify(config, null, 2) + \"\\n\", \"utf-8\");\n}\n\n/** Ensure all project directories exist. */\nexport function ensureProjectDirs(projectRoot?: string): void {\n const dirs = [\n projectConfigDir(projectRoot),\n projectDataDir(projectRoot),\n path.join(projectDataDir(projectRoot), \"cache\"),\n path.join(projectDataDir(projectRoot), \"snapshots\"),\n ];\n for (const dir of dirs) {\n fs.mkdirSync(dir, { recursive: true });\n }\n}\n\n/** Check if burnwatch is initialized in the given project. */\nexport function isInitialized(projectRoot?: string): boolean {\n return readProjectConfig(projectRoot) !== null;\n}\n","import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport { loadRegistry } from \"../core/registry.js\";\nimport type { ServiceDefinition, DetectionSource } from \"../core/types.js\";\n\nexport interface DetectionResult {\n service: ServiceDefinition;\n sources: DetectionSource[];\n details: string[];\n}\n\n/**\n * Run all detection surfaces against the current project.\n * Returns services detected via any combination of:\n * - package.json dependencies (recursive — finds monorepo subdirectories)\n * - environment variable patterns (process.env + .env* files recursive)\n * - import statement scanning (recursive from project root)\n * - (prompt mention scanning is handled separately in hooks)\n */\nexport function detectServices(projectRoot: string): DetectionResult[] {\n const registry = loadRegistry(projectRoot);\n const results = new Map<string, DetectionResult>();\n\n // Surface 1: Package manifest scanning (recursive — finds all package.json files)\n const pkgDeps = scanAllPackageJsons(projectRoot);\n for (const [serviceId, service] of registry) {\n const matchedPkgs = service.packageNames.filter((pkg) =>\n pkgDeps.has(pkg),\n );\n if (matchedPkgs.length > 0) {\n getOrCreate(results, serviceId, service).sources.push(\"package_json\");\n getOrCreate(results, serviceId, service).details.push(\n `package.json: ${matchedPkgs.join(\", \")}`,\n );\n }\n }\n\n // Surface 2: Environment variable pattern matching\n // Check both process.env AND .env* files in the project tree\n const envVars = collectEnvVars(projectRoot);\n for (const [serviceId, service] of registry) {\n const matchedEnvs = service.envPatterns.filter((pattern) =>\n envVars.has(pattern),\n );\n if (matchedEnvs.length > 0) {\n getOrCreate(results, serviceId, service).sources.push(\"env_var\");\n getOrCreate(results, serviceId, service).details.push(\n `env vars: ${matchedEnvs.join(\", \")}`,\n );\n }\n }\n\n // Surface 3: Import statement analysis (recursive from project root)\n const importHits = scanImports(projectRoot);\n for (const [serviceId, service] of registry) {\n const matchedImports = service.importPatterns.filter((pattern) =>\n importHits.has(pattern),\n );\n if (matchedImports.length > 0) {\n if (\n !getOrCreate(results, serviceId, service).sources.includes(\n \"import_scan\",\n )\n ) {\n getOrCreate(results, serviceId, service).sources.push(\"import_scan\");\n getOrCreate(results, serviceId, service).details.push(\n `imports: ${matchedImports.join(\", \")}`,\n );\n }\n }\n }\n\n return Array.from(results.values());\n}\n\n/**\n * Detect services mentioned in a prompt string.\n * Used by the UserPromptSubmit hook.\n */\nexport function detectMentions(\n prompt: string,\n projectRoot?: string,\n): DetectionResult[] {\n const registry = loadRegistry(projectRoot);\n const results: DetectionResult[] = [];\n const promptLower = prompt.toLowerCase();\n\n for (const [, service] of registry) {\n const matched = service.mentionKeywords.some((keyword) =>\n promptLower.includes(keyword.toLowerCase()),\n );\n if (matched) {\n results.push({\n service,\n sources: [\"prompt_mention\"],\n details: [`mentioned in prompt`],\n });\n }\n }\n\n return results;\n}\n\n/**\n * Detect new services introduced in a file change.\n * Used by the PostToolUse hook for Write/Edit events.\n */\nexport function detectInFileChange(\n filePath: string,\n content: string,\n projectRoot?: string,\n): DetectionResult[] {\n const registry = loadRegistry(projectRoot);\n const results: DetectionResult[] = [];\n const fileName = path.basename(filePath);\n\n // Check if it's a package.json change\n if (fileName === \"package.json\") {\n try {\n const pkg = JSON.parse(content) as {\n dependencies?: Record<string, string>;\n devDependencies?: Record<string, string>;\n };\n const allDeps = new Set([\n ...Object.keys(pkg.dependencies ?? {}),\n ...Object.keys(pkg.devDependencies ?? {}),\n ]);\n\n for (const [, service] of registry) {\n const matched = service.packageNames.filter((p) => allDeps.has(p));\n if (matched.length > 0) {\n results.push({\n service,\n sources: [\"package_json\"],\n details: [`new dependency: ${matched.join(\", \")}`],\n });\n }\n }\n } catch {\n // Not valid JSON, skip\n }\n return results;\n }\n\n // Check if it's an env file change\n if (fileName.startsWith(\".env\")) {\n const envKeys = content\n .split(\"\\n\")\n .filter((line) => line.includes(\"=\") && !line.startsWith(\"#\"))\n .map((line) => line.split(\"=\")[0]!.trim());\n\n for (const [, service] of registry) {\n const matched = service.envPatterns.filter((p) => envKeys.includes(p));\n if (matched.length > 0) {\n results.push({\n service,\n sources: [\"env_var\"],\n details: [`new env var: ${matched.join(\", \")}`],\n });\n }\n }\n return results;\n }\n\n // Check for import statements in source files\n if (/\\.(ts|tsx|js|jsx|mjs|cjs)$/.test(filePath)) {\n for (const [, service] of registry) {\n const matched = service.importPatterns.filter(\n (pattern) =>\n content.includes(`from \"${pattern}`) ||\n content.includes(`from '${pattern}`) ||\n content.includes(`require(\"${pattern}`) ||\n content.includes(`require('${pattern}`),\n );\n if (matched.length > 0) {\n results.push({\n service,\n sources: [\"import_scan\"],\n details: [`import added: ${matched.join(\", \")}`],\n });\n }\n }\n }\n\n return results;\n}\n\n// --- Helpers ---\n\nfunction getOrCreate(\n map: Map<string, DetectionResult>,\n serviceId: string,\n service: ServiceDefinition,\n): DetectionResult {\n let result = map.get(serviceId);\n if (!result) {\n result = { service, sources: [], details: [] };\n map.set(serviceId, result);\n }\n return result;\n}\n\n/**\n * Recursively find and scan ALL package.json files in the project tree.\n * Handles monorepos where dependencies live in subdirectories.\n */\nfunction scanAllPackageJsons(projectRoot: string): Set<string> {\n const deps = new Set<string>();\n const pkgFiles = findFiles(projectRoot, \"package.json\", 4);\n\n for (const pkgPath of pkgFiles) {\n try {\n const raw = fs.readFileSync(pkgPath, \"utf-8\");\n const pkg = JSON.parse(raw) as {\n dependencies?: Record<string, string>;\n devDependencies?: Record<string, string>;\n };\n for (const name of Object.keys(pkg.dependencies ?? {})) deps.add(name);\n for (const name of Object.keys(pkg.devDependencies ?? {})) deps.add(name);\n } catch {\n // Skip malformed package.json\n }\n }\n\n return deps;\n}\n\n/**\n * Collect environment variable names from both process.env\n * and all .env* files found recursively in the project tree.\n */\nfunction collectEnvVars(projectRoot: string): Set<string> {\n const envVars = new Set(Object.keys(process.env));\n\n // Find all .env* files in the project tree\n const envFiles = findEnvFiles(projectRoot, 3);\n\n for (const envFile of envFiles) {\n try {\n const content = fs.readFileSync(envFile, \"utf-8\");\n const keys = content\n .split(\"\\n\")\n .filter((line) => line.includes(\"=\") && !line.startsWith(\"#\"))\n .map((line) => line.split(\"=\")[0]!.trim())\n .filter(Boolean);\n\n for (const key of keys) {\n envVars.add(key);\n }\n } catch {\n // Skip unreadable files\n }\n }\n\n return envVars;\n}\n\n/**\n * Find all .env* files recursively (but not in node_modules, .git, dist, etc.)\n */\nfunction findEnvFiles(dir: string, maxDepth: number): string[] {\n const results: string[] = [];\n if (maxDepth <= 0) return results;\n\n try {\n const entries = fs.readdirSync(dir, { withFileTypes: true });\n for (const entry of entries) {\n if (entry.name === \"node_modules\" || entry.name === \".git\" || entry.name === \"dist\") continue;\n const fullPath = path.join(dir, entry.name);\n if (entry.isDirectory()) {\n results.push(...findEnvFiles(fullPath, maxDepth - 1));\n } else if (entry.name.startsWith(\".env\")) {\n results.push(fullPath);\n }\n }\n } catch {\n // Skip unreadable directories\n }\n\n return results;\n}\n\n/**\n * Find files with a specific name recursively.\n * Used to find package.json files across monorepo subdirectories.\n */\nfunction findFiles(dir: string, fileName: string, maxDepth: number): string[] {\n const results: string[] = [];\n if (maxDepth <= 0) return results;\n\n try {\n const entries = fs.readdirSync(dir, { withFileTypes: true });\n for (const entry of entries) {\n if (entry.name === \"node_modules\" || entry.name === \".git\" || entry.name === \"dist\") continue;\n const fullPath = path.join(dir, entry.name);\n if (entry.isDirectory()) {\n results.push(...findFiles(fullPath, fileName, maxDepth - 1));\n } else if (entry.name === fileName) {\n results.push(fullPath);\n }\n }\n } catch {\n // Skip unreadable directories\n }\n\n return results;\n}\n\n/**\n * Lightweight import scanning.\n * Recursively scans the project for import/require statements.\n * Looks in src/, app/, lib/, pages/, and any other code directories.\n * Does NOT do a full AST parse — just string matching.\n */\nfunction scanImports(projectRoot: string): Set<string> {\n const imports = new Set<string>();\n\n // Scan common code directories + the root itself for source files\n const codeDirs = [\"src\", \"app\", \"lib\", \"pages\", \"components\", \"utils\", \"services\", \"hooks\"];\n const dirsToScan: string[] = [];\n\n for (const dir of codeDirs) {\n const fullPath = path.join(projectRoot, dir);\n if (fs.existsSync(fullPath)) {\n dirsToScan.push(fullPath);\n }\n }\n\n // Also check subdirectories (monorepo support)\n try {\n const entries = fs.readdirSync(projectRoot, { withFileTypes: true });\n for (const entry of entries) {\n if (!entry.isDirectory()) continue;\n if (entry.name === \"node_modules\" || entry.name === \".git\" || entry.name === \"dist\" || entry.name.startsWith(\".\")) continue;\n\n // Check if this subdirectory has its own package.json (monorepo package)\n const subPkgPath = path.join(projectRoot, entry.name, \"package.json\");\n if (fs.existsSync(subPkgPath)) {\n // Scan this subpackage's code directories\n for (const dir of codeDirs) {\n const fullPath = path.join(projectRoot, entry.name, dir);\n if (fs.existsSync(fullPath)) {\n dirsToScan.push(fullPath);\n }\n }\n }\n }\n } catch {\n // Skip if root is unreadable\n }\n\n for (const dir of dirsToScan) {\n const files = walkDir(dir, /\\.(ts|tsx|js|jsx|mjs|cjs)$/);\n for (const file of files) {\n try {\n const content = fs.readFileSync(file, \"utf-8\");\n // Match: import ... from \"package\" or require(\"package\")\n const importRegex =\n /(?:from\\s+[\"']|require\\s*\\(\\s*[\"'])([^./][^\"']*?)(?:[\"'])/g;\n let match: RegExpExecArray | null;\n while ((match = importRegex.exec(content)) !== null) {\n const pkg = match[1];\n if (pkg) {\n // Normalize scoped packages: @scope/pkg/subpath -> @scope/pkg\n const parts = pkg.split(\"/\");\n if (parts[0]?.startsWith(\"@\") && parts.length >= 2) {\n imports.add(`${parts[0]}/${parts[1]}`);\n } else if (parts[0]) {\n imports.add(parts[0]);\n }\n }\n }\n } catch {\n // Skip unreadable files\n }\n }\n }\n\n return imports;\n}\n\n/** Recursively walk a directory, returning files matching the pattern. */\nfunction walkDir(dir: string, pattern: RegExp, maxDepth = 5): string[] {\n const results: string[] = [];\n if (maxDepth <= 0) return results;\n\n try {\n const entries = fs.readdirSync(dir, { withFileTypes: true });\n for (const entry of entries) {\n if (entry.name.startsWith(\".\") || entry.name === \"node_modules\") continue;\n const fullPath = path.join(dir, entry.name);\n if (entry.isDirectory()) {\n results.push(...walkDir(fullPath, pattern, maxDepth - 1));\n } else if (pattern.test(entry.name)) {\n results.push(fullPath);\n }\n }\n } catch {\n // Skip unreadable directories\n }\n\n return results;\n}\n","import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport * as url from \"node:url\";\nimport type { ServiceDefinition } from \"./types.js\";\n\nconst __dirname = path.dirname(url.fileURLToPath(import.meta.url));\n\ninterface RegistryFile {\n version: string;\n lastUpdated: string;\n services: Record<string, ServiceDefinition>;\n}\n\nlet cachedRegistry: Map<string, ServiceDefinition> | null = null;\n\n/**\n * Load the service registry.\n * Checks project-local override first, then falls back to bundled registry.\n */\nexport function loadRegistry(projectRoot?: string): Map<string, ServiceDefinition> {\n if (cachedRegistry) return cachedRegistry;\n\n const registry = new Map<string, ServiceDefinition>();\n\n // Load bundled registry (shipped with package)\n // Try multiple possible locations — depends on whether running from src/ or dist/\n const candidates = [\n path.resolve(__dirname, \"../../registry.json\"), // from src/core/\n path.resolve(__dirname, \"../registry.json\"), // from dist/\n ];\n for (const candidate of candidates) {\n if (fs.existsSync(candidate)) {\n loadRegistryFile(candidate, registry);\n break;\n }\n }\n\n // Load project-local override (if exists)\n if (projectRoot) {\n const localPath = path.join(projectRoot, \".burnwatch\", \"registry.json\");\n if (fs.existsSync(localPath)) {\n loadRegistryFile(localPath, registry);\n }\n }\n\n cachedRegistry = registry;\n return registry;\n}\n\nfunction loadRegistryFile(\n filePath: string,\n registry: Map<string, ServiceDefinition>,\n): void {\n try {\n const raw = fs.readFileSync(filePath, \"utf-8\");\n const data = JSON.parse(raw) as RegistryFile;\n for (const [id, service] of Object.entries(data.services)) {\n registry.set(id, { ...service, id });\n }\n } catch {\n // Silently skip missing or malformed registry files\n }\n}\n\n/** Clear the cached registry (for testing). */\nexport function clearRegistryCache(): void {\n cachedRegistry = null;\n}\n\n/** Get a single service definition by ID. */\nexport function getService(\n id: string,\n projectRoot?: string,\n): ServiceDefinition | undefined {\n return loadRegistry(projectRoot).get(id);\n}\n\n/** Get all service definitions. */\nexport function getAllServices(\n projectRoot?: string,\n): ServiceDefinition[] {\n return Array.from(loadRegistry(projectRoot).values());\n}\n","import type { ConfidenceTier } from \"../core/types.js\";\n\n/** Result from polling a billing API. */\nexport interface BillingResult {\n serviceId: string;\n spend: number;\n isEstimate: boolean;\n tier: ConfidenceTier;\n raw?: Record<string, unknown>;\n error?: string;\n}\n\n/**\n * Base interface for service billing connectors.\n * Each LIVE service implements this to fetch real spend data.\n */\nexport interface BillingConnector {\n serviceId: string;\n /** Fetch current period spend. */\n fetchSpend(apiKey: string, options?: Record<string, string>): Promise<BillingResult>;\n}\n\n/**\n * Make an HTTP request and return JSON.\n * Uses native fetch (Node 18+). No external dependencies.\n */\nexport async function fetchJson<T>(\n url: string,\n options: {\n headers?: Record<string, string>;\n method?: string;\n body?: string;\n timeout?: number;\n } = {},\n): Promise<{ ok: boolean; status: number; data?: T; error?: string }> {\n try {\n const controller = new AbortController();\n const timeoutId = setTimeout(\n () => controller.abort(),\n options.timeout ?? 10_000,\n );\n\n const response = await fetch(url, {\n method: options.method ?? \"GET\",\n headers: options.headers,\n body: options.body,\n signal: controller.signal,\n });\n\n clearTimeout(timeoutId);\n\n if (!response.ok) {\n return {\n ok: false,\n status: response.status,\n error: `HTTP ${response.status}: ${response.statusText}`,\n };\n }\n\n const data = (await response.json()) as T;\n return { ok: true, status: response.status, data };\n } catch (err) {\n return {\n ok: false,\n status: 0,\n error: err instanceof Error ? err.message : \"Unknown error\",\n };\n }\n}\n","import type { BillingConnector, BillingResult } from \"./base.js\";\nimport { fetchJson } from \"./base.js\";\n\n/**\n * Anthropic billing connector.\n * Uses the /v1/organizations/usage endpoint.\n * Requires an admin API key.\n */\nexport const anthropicConnector: BillingConnector = {\n serviceId: \"anthropic\",\n\n async fetchSpend(apiKey: string): Promise<BillingResult> {\n // Get current month date range\n const now = new Date();\n const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);\n const startDate = startOfMonth.toISOString().split(\"T\")[0]!;\n const endDate = now.toISOString().split(\"T\")[0]!;\n\n const url = `https://api.anthropic.com/v1/organizations/usage?start_date=${startDate}&end_date=${endDate}`;\n\n const result = await fetchJson<{\n data?: Array<{ total_cost_usd?: number; spend?: number }>;\n total_cost_usd?: number;\n }>(url, {\n headers: {\n \"x-api-key\": apiKey,\n \"anthropic-version\": \"2023-06-01\",\n },\n });\n\n if (!result.ok || !result.data) {\n return {\n serviceId: \"anthropic\",\n spend: 0,\n isEstimate: true,\n tier: \"est\",\n error: result.error ?? \"Failed to fetch Anthropic usage\",\n };\n }\n\n // Sum up usage across the period\n let totalSpend = 0;\n if (result.data.total_cost_usd !== undefined) {\n totalSpend = result.data.total_cost_usd;\n } else if (result.data.data) {\n totalSpend = result.data.data.reduce(\n (sum, entry) => sum + (entry.total_cost_usd ?? entry.spend ?? 0),\n 0,\n );\n }\n\n return {\n serviceId: \"anthropic\",\n spend: totalSpend,\n isEstimate: false,\n tier: \"live\",\n raw: result.data as unknown as Record<string, unknown>,\n };\n },\n};\n","import type { BillingConnector, BillingResult } from \"./base.js\";\nimport { fetchJson } from \"./base.js\";\n\n/**\n * OpenAI billing connector.\n * Uses the /v1/organization/costs endpoint.\n * Requires an organization-level API key.\n */\nexport const openaiConnector: BillingConnector = {\n serviceId: \"openai\",\n\n async fetchSpend(apiKey: string): Promise<BillingResult> {\n const now = new Date();\n const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);\n // OpenAI uses Unix timestamps\n const startTime = Math.floor(startOfMonth.getTime() / 1000);\n\n const url = `https://api.openai.com/v1/organization/costs?start_time=${startTime}`;\n\n const result = await fetchJson<{\n data?: Array<{\n results?: Array<{\n amount?: { value?: number };\n }>;\n }>;\n object?: string;\n }>(url, {\n headers: {\n Authorization: `Bearer ${apiKey}`,\n },\n });\n\n if (!result.ok || !result.data) {\n return {\n serviceId: \"openai\",\n spend: 0,\n isEstimate: true,\n tier: \"est\",\n error: result.error ?? \"Failed to fetch OpenAI usage\",\n };\n }\n\n // Sum all cost buckets\n let totalSpend = 0;\n if (result.data.data) {\n for (const bucket of result.data.data) {\n if (bucket.results) {\n for (const r of bucket.results) {\n totalSpend += r.amount?.value ?? 0;\n }\n }\n }\n }\n\n // OpenAI returns costs in cents, convert to dollars\n totalSpend = totalSpend / 100;\n\n return {\n serviceId: \"openai\",\n spend: totalSpend,\n isEstimate: false,\n tier: \"live\",\n raw: result.data as unknown as Record<string, unknown>,\n };\n },\n};\n","import type { BillingConnector, BillingResult } from \"./base.js\";\nimport { fetchJson } from \"./base.js\";\n\n/**\n * Vercel billing connector.\n * Uses the Vercel billing API.\n * Requires a Vercel token (personal or team-scoped).\n */\nexport const vercelConnector: BillingConnector = {\n serviceId: \"vercel\",\n\n async fetchSpend(\n token: string,\n options?: Record<string, string>,\n ): Promise<BillingResult> {\n const teamId = options?.[\"teamId\"] ?? \"\";\n const teamParam = teamId ? `?teamId=${teamId}` : \"\";\n\n // Fetch current billing period usage\n const url = `https://api.vercel.com/v2/usage${teamParam}`;\n\n const result = await fetchJson<{\n usage?: {\n total?: number;\n bandwidth?: { total?: number };\n serverlessFunctionExecution?: { total?: number };\n edgeFunctionExecution?: { total?: number };\n imageOptimization?: { total?: number };\n };\n billing?: {\n plan?: string;\n period?: { start?: string; end?: string };\n invoiceItems?: Array<{ amount?: number }>;\n };\n }>(url, {\n headers: {\n Authorization: `Bearer ${token}`,\n },\n });\n\n if (!result.ok || !result.data) {\n return {\n serviceId: \"vercel\",\n spend: 0,\n isEstimate: true,\n tier: \"est\",\n error: result.error ?? \"Failed to fetch Vercel usage\",\n };\n }\n\n // Sum up usage costs\n let totalSpend = 0;\n if (result.data.usage?.total !== undefined) {\n totalSpend = result.data.usage.total;\n } else if (result.data.billing?.invoiceItems) {\n totalSpend = result.data.billing.invoiceItems.reduce(\n (sum, item) => sum + (item.amount ?? 0),\n 0,\n );\n }\n\n return {\n serviceId: \"vercel\",\n spend: totalSpend,\n isEstimate: false,\n tier: \"live\",\n raw: result.data as unknown as Record<string, unknown>,\n };\n },\n};\n","import type { BillingConnector, BillingResult } from \"./base.js\";\nimport { fetchJson } from \"./base.js\";\n\n/**\n * Scrapfly billing connector.\n * Uses the /account endpoint which returns credits used/remaining.\n * Works with the standard API key — no special billing key needed.\n */\nexport const scrapflyConnector: BillingConnector = {\n serviceId: \"scrapfly\",\n\n async fetchSpend(apiKey: string): Promise<BillingResult> {\n const url = `https://api.scrapfly.io/account?key=${apiKey}`;\n\n const result = await fetchJson<{\n subscription?: {\n usage?: {\n scrape?: { used?: number; allowed?: number };\n };\n };\n account?: {\n credits_used?: number;\n credits_total?: number;\n };\n }>(url);\n\n if (!result.ok || !result.data) {\n return {\n serviceId: \"scrapfly\",\n spend: 0,\n isEstimate: true,\n tier: \"est\",\n error: result.error ?? \"Failed to fetch Scrapfly account\",\n };\n }\n\n // Extract credits used from the response\n let creditsUsed = 0;\n let creditsTotal = 0;\n\n if (result.data.subscription?.usage?.scrape) {\n creditsUsed = result.data.subscription.usage.scrape.used ?? 0;\n creditsTotal = result.data.subscription.usage.scrape.allowed ?? 0;\n } else if (result.data.account) {\n creditsUsed = result.data.account.credits_used ?? 0;\n creditsTotal = result.data.account.credits_total ?? 0;\n }\n\n // Convert credits to USD at registry rate\n const creditRate = 0.00015; // $0.00015 per credit\n const spend = creditsUsed * creditRate;\n\n return {\n serviceId: \"scrapfly\",\n spend,\n isEstimate: false,\n tier: \"live\",\n raw: {\n credits_used: creditsUsed,\n credits_total: creditsTotal,\n credit_rate: creditRate,\n ...(result.data as unknown as Record<string, unknown>),\n },\n };\n },\n};\n","import type { BillingConnector, BillingResult } from \"./base.js\";\nimport { anthropicConnector } from \"./anthropic.js\";\nimport { openaiConnector } from \"./openai.js\";\nimport { vercelConnector } from \"./vercel.js\";\nimport { scrapflyConnector } from \"./scrapfly.js\";\nimport type { TrackedService, ConfidenceTier } from \"../core/types.js\";\nimport { readGlobalConfig } from \"../core/config.js\";\nimport { getService } from \"../core/registry.js\";\n\n/** All available billing connectors, keyed by service ID. */\nconst connectors: Map<string, BillingConnector> = new Map([\n [\"anthropic\", anthropicConnector],\n [\"openai\", openaiConnector],\n [\"vercel\", vercelConnector],\n [\"scrapfly\", scrapflyConnector],\n]);\n\n/**\n * Poll spend for a single tracked service.\n * Returns the best available data based on connector availability and API keys.\n */\nexport async function pollService(\n tracked: TrackedService,\n): Promise<BillingResult> {\n const globalConfig = readGlobalConfig();\n const serviceConfig = globalConfig.services[tracked.serviceId];\n const connector = connectors.get(tracked.serviceId);\n const definition = getService(tracked.serviceId);\n\n // If we have a connector and an API key, try LIVE\n if (connector && serviceConfig?.apiKey) {\n try {\n const result = await connector.fetchSpend(\n serviceConfig.apiKey,\n serviceConfig as unknown as Record<string, string>,\n );\n if (!result.error) return result;\n // Fall through to lower tiers on error\n } catch {\n // Fall through\n }\n }\n\n // If user provided a plan cost, use CALC\n if (tracked.planCost !== undefined) {\n const now = new Date();\n const daysInMonth = new Date(\n now.getFullYear(),\n now.getMonth() + 1,\n 0,\n ).getDate();\n const dayOfMonth = now.getDate();\n const projectedSpend = (tracked.planCost / daysInMonth) * dayOfMonth;\n\n return {\n serviceId: tracked.serviceId,\n spend: projectedSpend,\n isEstimate: true,\n tier: \"calc\",\n };\n }\n\n // If service is in registry but we have no key and no plan cost\n if (definition) {\n let tier: ConfidenceTier;\n if (tracked.tierOverride) {\n tier = tracked.tierOverride;\n } else if (definition.apiTier === \"live\") {\n // Has a LIVE API but we don't have the key — mark as BLIND\n tier = \"blind\";\n } else {\n // EST, CALC, or BLIND — use the registry's declared tier\n tier = definition.apiTier;\n }\n\n return {\n serviceId: tracked.serviceId,\n spend: 0,\n isEstimate: tier !== \"live\",\n tier,\n error: tier === \"blind\" ? \"No API key configured\" : undefined,\n };\n }\n\n // Completely unknown service\n return {\n serviceId: tracked.serviceId,\n spend: 0,\n isEstimate: true,\n tier: \"blind\",\n error: \"Unknown service — not in registry\",\n };\n}\n\n/**\n * Poll all tracked services concurrently.\n * Returns results in the same order as input.\n */\nexport async function pollAllServices(\n services: TrackedService[],\n): Promise<BillingResult[]> {\n return Promise.all(services.map(pollService));\n}\n\nexport { type BillingConnector, type BillingResult } from \"./base.js\";\n","/**\n * Confidence tiers for spend tracking.\n *\n * LIVE — Real billing API data\n * CALC — Fixed monthly cost, user-entered\n * EST — Estimated from usage signals + pricing formula\n * BLIND — Detected in project, no tracking configured\n */\nexport type ConfidenceTier = \"live\" | \"calc\" | \"est\" | \"blind\" | \"excluded\";\n\nexport const CONFIDENCE_BADGES: Record<ConfidenceTier, string> = {\n live: \"✅ LIVE\",\n calc: \"🟡 CALC\",\n est: \"🟠 EST\",\n blind: \"🔴 BLIND\",\n excluded: \"⬚ SKIP\",\n};\n\n/** How a service charges — determines tracking strategy. */\nexport type BillingModel =\n | \"token_usage\" // Per-token (Anthropic, OpenAI, Gemini)\n | \"credit_pool\" // Fixed credit bucket (Scrapfly)\n | \"per_unit\" // Per-email, per-session, per-command (Resend, Browserbase, Upstash)\n | \"percentage\" // Percentage of transaction (Stripe)\n | \"flat_monthly\" // Fixed monthly subscription (PostHog, Inngest free tier)\n | \"tiered\" // Free up to X, then jumps (PostHog, Supabase)\n | \"compute\" // Compute-time based (Vercel, AWS)\n | \"unknown\";\n\n/** How cost scales — helps the agent reason about future spend. */\nexport type ScalingShape =\n | \"linear\" // Each unit costs the same\n | \"linear_burndown\" // Fixed pool, each use depletes it\n | \"tiered_jump\" // Free until threshold, then expensive\n | \"percentage\" // Proportional to revenue/volume\n | \"fixed\" // Flat monthly, no scaling\n | \"unknown\";\n\n/** A plan tier option for a service in the registry. */\nexport interface PlanTier {\n /** Human-readable plan name */\n name: string;\n /** Plan type: usage (pay-as-you-go), flat (fixed monthly), exclude (don't track) */\n type: \"usage\" | \"flat\" | \"exclude\";\n /** Monthly base cost for flat plans */\n monthlyBase?: number;\n /** Whether this plan requires an API key for tracking */\n requiresKey?: boolean;\n /** Whether this is the default/most common plan */\n default?: boolean;\n}\n\n/** Risk category for service grouping in interactive init. */\nexport type ServiceRiskCategory = \"llm\" | \"usage\" | \"infra\" | \"flat\";\n\n/** A service definition from the registry. */\nexport interface ServiceDefinition {\n /** Unique service identifier */\n id: string;\n /** Human-readable name */\n name: string;\n /** Package names in npm/pip that indicate this service */\n packageNames: string[];\n /** Env var patterns that indicate this service */\n envPatterns: string[];\n /** Import patterns to scan for (regex strings) */\n importPatterns: string[];\n /** Keywords that indicate mentions in prompts */\n mentionKeywords: string[];\n /** Billing model */\n billingModel: BillingModel;\n /** How cost scales */\n scalingShape: ScalingShape;\n /** What tier of tracking is available */\n apiTier: ConfidenceTier;\n /** Billing API endpoint, if available */\n apiEndpoint?: string;\n /** Pricing details */\n pricing?: {\n /** Human-readable formula */\n formula?: string;\n /** Rate per unit, if applicable */\n unitRate?: number;\n /** Unit name (token, credit, email, session, etc.) */\n unitName?: string;\n /** Monthly base cost, if flat */\n monthlyBase?: number;\n };\n /** Known gotchas that affect cost */\n gotchas?: string[];\n /** Alternative services (free or cheaper) */\n alternatives?: string[];\n /** Documentation URL */\n docsUrl?: string;\n /** Last time pricing was verified */\n lastVerified?: string;\n /** Notes about recent pricing changes */\n pricingNotes?: string;\n /** Available plan tiers for interactive init */\n plans?: PlanTier[];\n /** Whether the plan can be auto-detected from an API key */\n autoDetectPlan?: boolean;\n}\n\n/** A tracked service instance — a service definition + user config. */\nexport interface TrackedService {\n /** Service definition ID */\n serviceId: string;\n /** How this service was detected */\n detectedVia: DetectionSource[];\n /** User-configured monthly budget */\n budget?: number;\n /** Whether the user has provided an API/billing key */\n hasApiKey: boolean;\n /** Override confidence tier (e.g., user provided billing key upgrades to LIVE) */\n tierOverride?: ConfidenceTier;\n /** User-entered monthly plan cost (for CALC tier) */\n planCost?: number;\n /** When this service was first detected */\n firstDetected: string;\n /** Explicitly excluded from tracking by user */\n excluded?: boolean;\n /** Plan name selected during interactive init */\n planName?: string;\n}\n\nexport type DetectionSource =\n | \"package_json\"\n | \"env_var\"\n | \"import_scan\"\n | \"prompt_mention\"\n | \"git_diff\"\n | \"manual\";\n\n/** A spend snapshot for a single service at a point in time. */\nexport interface SpendSnapshot {\n serviceId: string;\n /** Current period spend (or estimate) */\n spend: number;\n /** Is the spend figure exact or estimated? */\n isEstimate: boolean;\n /** Confidence tier for this reading */\n tier: ConfidenceTier;\n /** Budget allocated */\n budget?: number;\n /** Percentage of budget consumed */\n budgetPercent?: number;\n /** Budget status */\n status: \"healthy\" | \"caution\" | \"over\" | \"unknown\";\n /** Human-readable status label */\n statusLabel: string;\n /** Raw data from billing API, if available */\n raw?: Record<string, unknown>;\n /** Timestamp of this snapshot */\n timestamp: string;\n}\n\n/** The full spend brief, injected at session start. */\nexport interface SpendBrief {\n projectName: string;\n generatedAt: string;\n period: string;\n services: SpendSnapshot[];\n totalSpend: number;\n totalIsEstimate: boolean;\n estimateMargin: number;\n untrackedCount: number;\n alerts: SpendAlert[];\n}\n\nexport interface SpendAlert {\n serviceId: string;\n type: \"over_budget\" | \"near_budget\" | \"new_service\" | \"stale_data\" | \"blind_service\";\n message: string;\n severity: \"warning\" | \"critical\" | \"info\";\n}\n\n/** Ledger entry — one row in spend-ledger.md */\nexport interface LedgerEntry {\n serviceId: string;\n serviceName: string;\n spend: number;\n isEstimate: boolean;\n tier: ConfidenceTier;\n budget?: number;\n statusLabel: string;\n}\n\n/** Event logged to events.jsonl */\nexport interface SpendEvent {\n timestamp: string;\n sessionId: string;\n type:\n | \"session_start\"\n | \"session_end\"\n | \"service_detected\"\n | \"service_mentioned\"\n | \"spend_polled\"\n | \"budget_alert\"\n | \"ledger_written\"\n | \"cost_impact\";\n data: Record<string, unknown>;\n}\n\n/** A cost impact estimate for a file change. */\nexport interface CostImpact {\n serviceId: string;\n serviceName: string;\n filePath: string;\n /** Number of SDK call sites found */\n callCount: number;\n /** Detected multipliers (loops, .map(), etc.) */\n multipliers: string[];\n /** Effective multiplier applied to call count */\n multiplierFactor: number;\n /** Estimated monthly invocations */\n monthlyInvocations: number;\n /** Low estimate monthly cost */\n costLow: number;\n /** High estimate monthly cost */\n costHigh: number;\n /** Gotcha-based cost range explanation */\n rangeExplanation?: string;\n}\n\n/**\n * Hook input — the JSON received via stdin from Claude Code.\n * Subset of fields we care about.\n */\nexport interface HookInput {\n session_id: string;\n transcript_path?: string;\n cwd: string;\n hook_event_name: string;\n // SessionStart\n source?: string;\n // UserPromptSubmit\n prompt?: string;\n // PostToolUse\n tool_name?: string;\n tool_input?: {\n file_path?: string;\n command?: string;\n content?: string;\n old_string?: string;\n new_string?: string;\n };\n}\n\n/**\n * Hook output — the JSON we write to stdout for Claude Code.\n */\nexport interface HookOutput {\n hookSpecificOutput?: {\n hookEventName: string;\n additionalContext?: string;\n };\n}\n","import type {\n SpendBrief,\n SpendSnapshot,\n SpendAlert,\n ConfidenceTier,\n} from \"./types.js\";\nimport { CONFIDENCE_BADGES } from \"./types.js\";\n\n/**\n * Format a spend brief as a text block for injection into Claude's context.\n */\nexport function formatBrief(brief: SpendBrief): string {\n const lines: string[] = [];\n const width = 62;\n const hrDouble = \"═\".repeat(width);\n const hrSingle = \"─\".repeat(width - 4);\n\n lines.push(`╔${hrDouble}╗`);\n lines.push(\n `║ BURNWATCH — ${brief.projectName} — ${brief.period}`.padEnd(\n width + 1,\n ) + \"║\",\n );\n lines.push(`╠${hrDouble}╣`);\n\n // Header\n lines.push(\n formatRow(\"Service\", \"Spend\", \"Conf\", \"Budget\", \"Left\", width),\n );\n lines.push(`║ ${hrSingle} ║`);\n\n // Service rows\n for (const svc of brief.services) {\n const spendStr = svc.isEstimate\n ? `~$${svc.spend.toFixed(2)}`\n : `$${svc.spend.toFixed(2)}`;\n const badge = CONFIDENCE_BADGES[svc.tier];\n const budgetStr = svc.budget ? `$${svc.budget}` : \"—\";\n const leftStr = formatLeft(svc);\n\n lines.push(formatRow(svc.serviceId, spendStr, badge, budgetStr, leftStr, width));\n }\n\n // Footer\n lines.push(`╠${hrDouble}╣`);\n const totalStr = brief.totalIsEstimate\n ? `~$${brief.totalSpend.toFixed(2)}`\n : `$${brief.totalSpend.toFixed(2)}`;\n const marginStr = brief.estimateMargin > 0\n ? ` Est margin: ±$${brief.estimateMargin.toFixed(0)}`\n : \"\";\n const untrackedStr =\n brief.untrackedCount > 0\n ? `Untracked: ${brief.untrackedCount} ⚠️`\n : `Untracked: 0 ✅`;\n\n lines.push(\n `║ TOTAL: ${totalStr} ${untrackedStr}${marginStr}`.padEnd(\n width + 1,\n ) + \"║\",\n );\n\n // Alerts\n for (const alert of brief.alerts) {\n const icon = alert.severity === \"critical\" ? \"🚨\" : \"⚠️\";\n lines.push(\n `║ ${icon} ${alert.message}`.padEnd(width + 1) + \"║\",\n );\n }\n\n lines.push(`╚${hrDouble}╝`);\n\n return lines.join(\"\\n\");\n}\n\n/**\n * Format a single-service spend card for injection on mention.\n */\nexport function formatSpendCard(snapshot: SpendSnapshot): string {\n const badge = CONFIDENCE_BADGES[snapshot.tier];\n const spendStr = snapshot.isEstimate\n ? `~$${snapshot.spend.toFixed(2)}`\n : `$${snapshot.spend.toFixed(2)}`;\n const budgetStr = snapshot.budget\n ? `Budget: $${snapshot.budget}`\n : \"No budget set\";\n const statusStr = snapshot.statusLabel;\n\n const lines = [\n `[BURNWATCH] ${snapshot.serviceId} — current period`,\n ` Spend: ${spendStr} | ${budgetStr} | ${statusStr}`,\n ` Confidence: ${badge}`,\n ];\n\n if (snapshot.status === \"over\" && snapshot.budgetPercent) {\n lines.push(\n ` ⚠️ ${snapshot.budgetPercent.toFixed(0)}% of budget consumed`,\n );\n }\n\n return lines.join(\"\\n\");\n}\n\n/**\n * Build a SpendBrief from snapshots and project config.\n */\nexport function buildBrief(\n projectName: string,\n snapshots: SpendSnapshot[],\n blindCount: number,\n): SpendBrief {\n const now = new Date();\n const period = now.toLocaleDateString(\"en-US\", {\n month: \"long\",\n year: \"numeric\",\n });\n\n let totalSpend = 0;\n let hasEstimates = false;\n let estimateMargin = 0;\n const alerts: SpendAlert[] = [];\n\n for (const snap of snapshots) {\n totalSpend += snap.spend;\n if (snap.isEstimate) {\n hasEstimates = true;\n estimateMargin += snap.spend * 0.15; // ±15% margin on estimates\n }\n\n if (snap.status === \"over\") {\n alerts.push({\n serviceId: snap.serviceId,\n type: \"over_budget\",\n message: `${snap.serviceId.toUpperCase()} ${snap.budgetPercent?.toFixed(0) ?? \"?\"}% OVER BUDGET — review before use`,\n severity: \"critical\",\n });\n } else if (snap.status === \"caution\" && snap.budgetPercent && snap.budgetPercent >= 80) {\n alerts.push({\n serviceId: snap.serviceId,\n type: \"near_budget\",\n message: `${snap.serviceId} at ${snap.budgetPercent.toFixed(0)}% of budget`,\n severity: \"warning\",\n });\n }\n }\n\n if (blindCount > 0) {\n alerts.push({\n serviceId: \"_blind\",\n type: \"blind_service\",\n message: `${blindCount} service${blindCount > 1 ? \"s\" : \"\"} detected but untracked - run 'burnwatch init' to configure`,\n severity: \"warning\",\n });\n }\n\n return {\n projectName,\n generatedAt: now.toISOString(),\n period,\n services: snapshots,\n totalSpend,\n totalIsEstimate: hasEstimates,\n estimateMargin,\n untrackedCount: blindCount,\n alerts,\n };\n}\n\n// --- Helpers ---\n\nfunction formatRow(\n service: string,\n spend: string,\n conf: string,\n budget: string,\n left: string,\n width: number,\n): string {\n const row = ` ${service.padEnd(14)} ${spend.padEnd(11)} ${conf.padEnd(7)} ${budget.padEnd(7)} ${left}`;\n return `║${row}`.padEnd(width + 1) + \"║\";\n}\n\nfunction formatLeft(snap: SpendSnapshot): string {\n if (!snap.budget) return \"—\";\n if (snap.status === \"over\") return \"⚠️ OVR\";\n if (snap.budgetPercent !== undefined) {\n const remaining = 100 - snap.budgetPercent;\n return `${remaining.toFixed(0)}%`;\n }\n return \"—\";\n}\n\n/**\n * Build a SpendSnapshot from tracked service data.\n */\nexport function buildSnapshot(\n serviceId: string,\n tier: ConfidenceTier,\n spend: number,\n budget?: number,\n): SpendSnapshot {\n const isEstimate = tier === \"est\" || tier === \"calc\";\n const budgetPercent = budget ? (spend / budget) * 100 : undefined;\n\n let status: SpendSnapshot[\"status\"] = \"unknown\";\n let statusLabel = \"no budget\";\n\n if (budget) {\n if (budgetPercent! > 100) {\n status = \"over\";\n statusLabel = `⚠️ ${budgetPercent!.toFixed(0)}% over`;\n } else if (budgetPercent! >= 75) {\n status = \"caution\";\n statusLabel = `${(100 - budgetPercent!).toFixed(0)}% — caution`;\n } else {\n status = \"healthy\";\n statusLabel = `${(100 - budgetPercent!).toFixed(0)}% — healthy`;\n }\n }\n\n if (tier === \"calc\" && budget) {\n statusLabel = `flat — on plan`;\n status = \"healthy\";\n }\n\n return {\n serviceId,\n spend,\n isEstimate,\n tier,\n budget,\n budgetPercent,\n status,\n statusLabel,\n timestamp: new Date().toISOString(),\n };\n}\n","import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport type { SpendBrief, SpendEvent } from \"./types.js\";\nimport { CONFIDENCE_BADGES } from \"./types.js\";\nimport { projectConfigDir, projectDataDir } from \"./config.js\";\n\n/**\n * Write the spend ledger as a human-readable markdown file.\n * Designed to be git-committable and readable in 10 seconds.\n */\nexport function writeLedger(brief: SpendBrief, projectRoot?: string): void {\n const now = new Date();\n const lines: string[] = [];\n\n lines.push(`# Burnwatch Ledger — ${brief.projectName}`);\n lines.push(`Last updated: ${now.toISOString()}`);\n lines.push(\"\");\n lines.push(`## This Month (${brief.period})`);\n lines.push(\"\");\n lines.push(\"| Service | Spend | Conf | Budget | Status |\");\n lines.push(\"|---------|-------|------|--------|--------|\");\n\n for (const svc of brief.services) {\n const spendStr = svc.isEstimate\n ? `~$${svc.spend.toFixed(2)}`\n : `$${svc.spend.toFixed(2)}`;\n const badge = CONFIDENCE_BADGES[svc.tier];\n const budgetStr = svc.budget ? `$${svc.budget}` : \"—\";\n\n lines.push(\n `| ${svc.serviceId} | ${spendStr} | ${badge} | ${budgetStr} | ${svc.statusLabel} |`,\n );\n }\n\n // Add projected impact row if session impacts exist in alerts\n const impactAlert = brief.alerts.find(\n (a) => a.serviceId === \"_session_impact\",\n );\n if (impactAlert) {\n lines.push(\n `| _projected impact_ | — | 📈 EST | — | ${impactAlert.message} |`,\n );\n }\n\n lines.push(\"\");\n const totalStr = brief.totalIsEstimate\n ? `~$${brief.totalSpend.toFixed(2)}`\n : `$${brief.totalSpend.toFixed(2)}`;\n const marginStr =\n brief.estimateMargin > 0\n ? ` (±$${brief.estimateMargin.toFixed(0)} estimated margin)`\n : \"\";\n lines.push(`## TOTAL: ${totalStr}${marginStr}`);\n lines.push(`## Untracked services: ${brief.untrackedCount}`);\n lines.push(\"\");\n\n if (brief.alerts.length > 0) {\n lines.push(\"## Alerts\");\n for (const alert of brief.alerts) {\n const icon = alert.severity === \"critical\" ? \"🚨\" : \"⚠️\";\n lines.push(`- ${icon} ${alert.message}`);\n }\n lines.push(\"\");\n }\n\n const ledgerPath = path.join(\n projectConfigDir(projectRoot),\n \"spend-ledger.md\",\n );\n fs.mkdirSync(path.dirname(ledgerPath), { recursive: true });\n fs.writeFileSync(ledgerPath, lines.join(\"\\n\") + \"\\n\", \"utf-8\");\n}\n\n/**\n * Append an event to the append-only event log.\n */\nexport function logEvent(event: SpendEvent, projectRoot?: string): void {\n const logPath = path.join(projectDataDir(projectRoot), \"events.jsonl\");\n fs.mkdirSync(path.dirname(logPath), { recursive: true });\n fs.appendFileSync(logPath, JSON.stringify(event) + \"\\n\", \"utf-8\");\n}\n\n/**\n * Read recent events from the event log.\n */\nexport function readRecentEvents(\n count: number,\n projectRoot?: string,\n): SpendEvent[] {\n const logPath = path.join(projectDataDir(projectRoot), \"events.jsonl\");\n try {\n const raw = fs.readFileSync(logPath, \"utf-8\");\n const lines = raw.trim().split(\"\\n\").filter(Boolean);\n return lines\n .slice(-count)\n .map((line) => JSON.parse(line) as SpendEvent);\n } catch {\n return [];\n }\n}\n\n/**\n * Save a spend snapshot to the snapshots directory.\n * Used for delta computation across sessions.\n */\nexport function saveSnapshot(brief: SpendBrief, projectRoot?: string): void {\n const snapshotDir = path.join(projectDataDir(projectRoot), \"snapshots\");\n fs.mkdirSync(snapshotDir, { recursive: true });\n const filename = `snapshot-${new Date().toISOString().replace(/[:.]/g, \"-\")}.json`;\n fs.writeFileSync(\n path.join(snapshotDir, filename),\n JSON.stringify(brief, null, 2) + \"\\n\",\n \"utf-8\",\n );\n}\n\n/**\n * Read the most recent snapshot, if any.\n */\nexport function readLatestSnapshot(\n projectRoot?: string,\n): SpendBrief | null {\n const snapshotDir = path.join(projectDataDir(projectRoot), \"snapshots\");\n try {\n const files = fs\n .readdirSync(snapshotDir)\n .filter((f) => f.startsWith(\"snapshot-\") && f.endsWith(\".json\"))\n .sort()\n .reverse();\n\n if (files.length === 0) return null;\n\n const raw = fs.readFileSync(\n path.join(snapshotDir, files[0]!),\n \"utf-8\",\n );\n return JSON.parse(raw) as SpendBrief;\n } catch {\n return null;\n }\n}\n","/**\n * Interactive init flow for burnwatch.\n *\n * Groups detected services by risk category, presents plan tiers,\n * and collects user choices via Node readline.\n */\n\nimport * as readline from \"node:readline\";\nimport type {\n ServiceDefinition,\n PlanTier,\n TrackedService,\n ServiceRiskCategory,\n} from \"./core/types.js\";\nimport type { DetectionResult } from \"./detection/detector.js\";\nimport { readGlobalConfig, writeGlobalConfig } from \"./core/config.js\";\nimport { fetchJson } from \"./services/base.js\";\n\n/** Risk categories in display order: LLMs first, then usage-based, infra, flat-rate */\nconst RISK_ORDER: ServiceRiskCategory[] = [\"llm\", \"usage\", \"infra\", \"flat\"];\n\nconst RISK_LABELS: Record<ServiceRiskCategory, string> = {\n llm: \"🤖 LLM / AI Services (highest variable cost)\",\n usage: \"📊 Usage-Based Services\",\n infra: \"🏗️ Infrastructure & Compute\",\n flat: \"📦 Flat-Rate / Free Tier Services\",\n};\n\n/** Map service IDs to risk categories */\nfunction classifyRisk(service: ServiceDefinition): ServiceRiskCategory {\n if (service.billingModel === \"token_usage\") return \"llm\";\n if (\n service.billingModel === \"credit_pool\" ||\n service.billingModel === \"percentage\" ||\n service.billingModel === \"per_unit\"\n )\n return \"usage\";\n if (service.billingModel === \"compute\") return \"infra\";\n return \"flat\";\n}\n\n/** Group detection results by risk category */\nfunction groupByRisk(\n detected: DetectionResult[],\n): Map<ServiceRiskCategory, DetectionResult[]> {\n const groups = new Map<ServiceRiskCategory, DetectionResult[]>();\n for (const cat of RISK_ORDER) {\n groups.set(cat, []);\n }\n\n for (const det of detected) {\n const cat = classifyRisk(det.service);\n groups.get(cat)!.push(det);\n }\n\n return groups;\n}\n\n/** Prompt the user with a question and return their answer */\nfunction ask(rl: readline.Interface, question: string): Promise<string> {\n return new Promise((resolve) => {\n rl.question(question, (answer) => {\n resolve(answer.trim());\n });\n });\n}\n\n/** Try to auto-detect plan from Scrapfly API */\nasync function autoDetectScrapflyPlan(\n apiKey: string,\n): Promise<string | null> {\n try {\n const result = await fetchJson<{\n subscription?: { plan?: { name?: string } };\n }>(`https://api.scrapfly.io/account?key=${apiKey}`);\n\n if (result.ok && result.data?.subscription?.plan?.name) {\n return result.data.subscription.plan.name;\n }\n } catch {\n // Ignore errors\n }\n return null;\n}\n\nexport interface InteractiveInitResult {\n services: Record<string, TrackedService>;\n}\n\n/**\n * Run the interactive init flow.\n * Shows detected services grouped by risk, lets user pick plans.\n */\nexport async function runInteractiveInit(\n detected: DetectionResult[],\n): Promise<InteractiveInitResult> {\n const rl = readline.createInterface({\n input: process.stdin,\n output: process.stdout,\n });\n\n const services: Record<string, TrackedService> = {};\n const groups = groupByRisk(detected);\n const globalConfig = readGlobalConfig();\n\n console.log(\n \"\\n📋 Let's configure each detected service. Services are grouped by cost risk.\\n\",\n );\n\n for (const category of RISK_ORDER) {\n const group = groups.get(category)!;\n if (group.length === 0) continue;\n\n console.log(`\\n${RISK_LABELS[category]}`);\n console.log(\"─\".repeat(50));\n\n for (const det of group) {\n const service = det.service;\n const plans = service.plans;\n\n console.log(`\\n ${service.name}`);\n console.log(` Detected via: ${det.details.join(\", \")}`);\n\n if (!plans || plans.length === 0) {\n // No plans defined — fall back to basic tracking\n services[service.id] = {\n serviceId: service.id,\n detectedVia: det.sources,\n hasApiKey: false,\n firstDetected: new Date().toISOString(),\n };\n console.log(\" → Auto-configured (no plan tiers available)\");\n continue;\n }\n\n // Show plan options\n const defaultIndex = plans.findIndex((p) => p.default);\n console.log(\"\");\n for (let i = 0; i < plans.length; i++) {\n const plan = plans[i]!;\n const marker = i === defaultIndex ? \" (recommended)\" : \"\";\n const costStr =\n plan.type === \"exclude\"\n ? \"\"\n : plan.monthlyBase !== undefined\n ? ` — $${plan.monthlyBase}/mo`\n : \" — variable\";\n console.log(` ${i + 1}) ${plan.name}${costStr}${marker}`);\n }\n\n const defaultChoice =\n defaultIndex >= 0 ? String(defaultIndex + 1) : \"1\";\n const answer = await ask(\n rl,\n ` Choose [${defaultChoice}]: `,\n );\n\n const choiceIndex = (answer === \"\" ? parseInt(defaultChoice) : parseInt(answer)) - 1;\n const chosen =\n plans[choiceIndex] ?? plans[defaultIndex >= 0 ? defaultIndex : 0]!;\n\n if (chosen.type === \"exclude\") {\n // Explicitly excluded\n services[service.id] = {\n serviceId: service.id,\n detectedVia: det.sources,\n hasApiKey: false,\n firstDetected: new Date().toISOString(),\n excluded: true,\n planName: chosen.name,\n };\n console.log(` → ${service.name}: excluded from tracking`);\n continue;\n }\n\n const tracked: TrackedService = {\n serviceId: service.id,\n detectedVia: det.sources,\n hasApiKey: false,\n firstDetected: new Date().toISOString(),\n planName: chosen.name,\n };\n\n if (chosen.type === \"flat\" && chosen.monthlyBase !== undefined) {\n tracked.planCost = chosen.monthlyBase;\n // Auto-set budget to plan cost for paid flat plans\n if (chosen.monthlyBase > 0) {\n tracked.budget = chosen.monthlyBase;\n }\n }\n\n // If the service has a billing API, offer to provide a key\n if (service.apiTier === \"live\" || chosen.requiresKey) {\n // Check if we already have a key in global config\n const existingKey = globalConfig.services[service.id]?.apiKey;\n if (existingKey) {\n console.log(` 🔐 Using existing API key from global config`);\n tracked.hasApiKey = true;\n\n // Auto-detect plan for Scrapfly\n if (service.autoDetectPlan && service.id === \"scrapfly\") {\n console.log(\" 🔍 Auto-detecting plan from API...\");\n const planName = await autoDetectScrapflyPlan(existingKey);\n if (planName) {\n console.log(` → Detected plan: ${planName}`);\n tracked.planName = planName;\n }\n }\n } else if (chosen.requiresKey) {\n const keyAnswer = await ask(\n rl,\n ` Enter API key (or press Enter to skip): `,\n );\n if (keyAnswer) {\n tracked.hasApiKey = true;\n if (!globalConfig.services[service.id]) {\n globalConfig.services[service.id] = {};\n }\n globalConfig.services[service.id]!.apiKey = keyAnswer;\n\n // Auto-detect plan for Scrapfly\n if (service.autoDetectPlan && service.id === \"scrapfly\") {\n console.log(\" 🔍 Auto-detecting plan from API...\");\n const planName = await autoDetectScrapflyPlan(keyAnswer);\n if (planName) {\n console.log(` → Detected plan: ${planName}`);\n tracked.planName = planName;\n }\n }\n }\n }\n }\n\n // Always ask for budget if not already set to a meaningful value\n if (tracked.budget === undefined || tracked.budget === 0) {\n const suggestion = chosen.monthlyBase && chosen.monthlyBase > 0\n ? ` [${chosen.monthlyBase}]`\n : \"\";\n const budgetAnswer = await ask(\n rl,\n ` Monthly budget in USD${suggestion} (or press Enter to skip): $`,\n );\n if (budgetAnswer) {\n const budget = parseFloat(budgetAnswer);\n if (!isNaN(budget)) {\n tracked.budget = budget;\n }\n }\n }\n\n services[service.id] = tracked;\n\n const tierLabel = tracked.hasApiKey\n ? \"✅ LIVE\"\n : tracked.planCost !== undefined\n ? \"🟡 CALC\"\n : \"🔴 BLIND\";\n const budgetStr = tracked.budget !== undefined ? ` | Budget: $${tracked.budget}/mo` : \"\";\n console.log(\n ` → ${service.name}: ${chosen.name} (${tierLabel}${budgetStr})`,\n );\n }\n }\n\n // Save any collected API keys\n writeGlobalConfig(globalConfig);\n\n rl.close();\n\n return { services };\n}\n"],"mappings":";;;AAYA,YAAYA,SAAQ;AACpB,YAAYC,WAAU;;;ACbtB,YAAY,QAAQ;AACpB,YAAY,UAAU;AACtB,YAAY,QAAQ;AAab,SAAS,kBAA0B;AACxC,QAAM,YAAY,QAAQ,IAAI,iBAAiB;AAC/C,MAAI,UAAW,QAAY,UAAK,WAAW,WAAW;AACtD,SAAY,UAAQ,WAAQ,GAAG,WAAW,WAAW;AACvD;AAGO,SAAS,iBAAiB,aAA8B;AAC7D,QAAM,OAAO,eAAe,QAAQ,IAAI;AACxC,SAAY,UAAK,MAAM,YAAY;AACrC;AAGO,SAAS,eAAe,aAA8B;AAC3D,SAAY,UAAK,iBAAiB,WAAW,GAAG,MAAM;AACxD;AAeO,SAAS,mBAAiC;AAC/C,QAAM,aAAkB,UAAK,gBAAgB,GAAG,aAAa;AAC7D,MAAI;AACF,UAAM,MAAS,gBAAa,YAAY,OAAO;AAC/C,WAAO,KAAK,MAAM,GAAG;AAAA,EACvB,QAAQ;AACN,WAAO,EAAE,UAAU,CAAC,EAAE;AAAA,EACxB;AACF;AAEO,SAAS,kBAAkB,QAA4B;AAC5D,QAAM,MAAM,gBAAgB;AAC5B,EAAG,aAAU,KAAK,EAAE,WAAW,KAAK,CAAC;AACrC,QAAM,aAAkB,UAAK,KAAK,aAAa;AAC/C,EAAG,iBAAc,YAAY,KAAK,UAAU,QAAQ,MAAM,CAAC,IAAI,MAAM,OAA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AAM,MAAM,IAAI,CAAC;AACrC,mBAAiB,MAAM,MAAM,GAAgB,QAAQ;AAAA,IACnD,OAAO;AAAA,MACL;AAAA,QACE,MAAM;AAAA,QACN,SAAS,SAAc,WAAK,UAAU,YAAY,CAAC;AAAA,QACnD,SAAS;AAAA,QACT,OAAO;AAAA,MACT;AAAA,IACF;AAAA,EACF,CAAC;AAED,WAAS,OAAO,IAAI;AACpB,EAAG,kBAAc,cAAc,KAAK,UAAU,UAAU,MAAM,CAAC,IAAI,MAAM,OAAO;AAChF,UAAQ,IAAI,0BAA0B,YAAY,EAAE;AACtD;AAEA,SAAS,iBACP,WACA,YACA,YACM;AAEN,QAAM,WAAW,UAAU,KAAK,CAAC,MAAM;AACrC,UAAM,OAAO;AACb,WAAO,KAAK,OAAO,KAAK,CAAC,UAAU,MAAM,SAAS,SAAS,WAAW,CAAC;AAAA,EACzE,CAAC;AAED,MAAI,CAAC,UAAU;AACb,cAAU,KAAK,UAAU;AAAA,EAC3B;AACF;AAIA,KAAK,EAAE,MAAM,CAAC,QAAQ;AACpB,UAAQ,MAAM,UAAU,eAAe,QAAQ,IAAI,UAAU,GAAG;AAChE,UAAQ,KAAK,CAAC;AAChB,CAAC;","names":["fs","path","fs","path","fs","path","url","url","url","url","url","fs","path","resolve","tracked"]}
1
+ {"version":3,"sources":["../src/cli.ts","../src/core/config.ts","../src/detection/detector.ts","../src/core/registry.ts","../src/services/base.ts","../src/services/anthropic.ts","../src/services/openai.ts","../src/services/vercel.ts","../src/services/scrapfly.ts","../src/services/index.ts","../src/core/types.ts","../src/core/brief.ts","../src/core/ledger.ts","../src/interactive-init.ts"],"sourcesContent":["#!/usr/bin/env node\n\n/**\n * burnwatch CLI\n *\n * Usage:\n * burnwatch init — Initialize in current project\n * burnwatch add <service> [options] — Register a service\n * burnwatch status — Show current spend brief\n * burnwatch reconcile — Scan for untracked sessions\n */\n\nimport * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport {\n ensureProjectDirs,\n readProjectConfig,\n writeProjectConfig,\n readGlobalConfig,\n writeGlobalConfig,\n projectConfigDir,\n isInitialized,\n} from \"./core/config.js\";\nimport type { ProjectConfig } from \"./core/config.js\";\nimport type { TrackedService } from \"./core/types.js\";\nimport { detectServices } from \"./detection/detector.js\";\nimport { pollAllServices } from \"./services/index.js\";\nimport { buildSnapshot, buildBrief, formatBrief } from \"./core/brief.js\";\nimport { writeLedger, saveSnapshot } from \"./core/ledger.js\";\nimport { getService, getAllServices } from \"./core/registry.js\";\nimport { runInteractiveInit } from \"./interactive-init.js\";\n\nconst args = process.argv.slice(2);\nconst command = args[0];\nconst flags = new Set(args.slice(1));\n\nasync function main(): Promise<void> {\n switch (command) {\n case \"init\":\n case \"setup\":\n await cmdInit();\n break;\n case \"add\":\n await cmdAdd();\n break;\n case \"status\":\n await cmdStatus();\n break;\n case \"services\":\n cmdServices();\n break;\n case \"reconcile\":\n await cmdReconcile();\n break;\n case \"help\":\n case \"--help\":\n case \"-h\":\n cmdHelp();\n break;\n case \"version\":\n case \"--version\":\n case \"-v\":\n cmdVersion();\n break;\n default:\n if (command) {\n console.error(`Unknown command: ${command}`);\n console.error('Run \"burnwatch help\" for usage.');\n process.exit(1);\n }\n cmdHelp();\n }\n}\n\n// --- Commands ---\n\nasync function cmdInit(): Promise<void> {\n const projectRoot = process.cwd();\n const nonInteractive = flags.has(\"--non-interactive\") || flags.has(\"--ni\");\n const alreadyInitialized = isInitialized(projectRoot);\n\n // Detect project name from package.json\n let projectName = path.basename(projectRoot);\n try {\n const pkgPath = path.join(projectRoot, \"package.json\");\n const pkg = JSON.parse(fs.readFileSync(pkgPath, \"utf-8\")) as {\n name?: string;\n };\n if (pkg.name) projectName = pkg.name;\n } catch {\n // Use directory name\n }\n\n // Create directories (idempotent)\n ensureProjectDirs(projectRoot);\n\n // Run detection\n console.log(\"🔍 Scanning project for paid services...\\n\");\n const detected = detectServices(projectRoot);\n\n // Load existing config or create new one\n const existingConfig = alreadyInitialized ? readProjectConfig(projectRoot) : null;\n const config: ProjectConfig = {\n projectName: existingConfig?.projectName ?? projectName,\n services: existingConfig?.services ?? {},\n createdAt: existingConfig?.createdAt ?? new Date().toISOString(),\n updatedAt: new Date().toISOString(),\n };\n\n if (!nonInteractive && detected.length > 0 && process.stdin.isTTY) {\n // Interactive mode — walk through each service with plan tiers\n const result = await runInteractiveInit(detected);\n config.services = result.services;\n } else {\n // Non-interactive mode — auto-register with default plans and budgets\n const globalConfig = readGlobalConfig();\n\n for (const det of detected) {\n const existing = config.services[det.service.id];\n const service = det.service;\n const plans = service.plans ?? [];\n const defaultPlan = plans.find((p) => p.default) ?? plans[0];\n\n // Skip services that already have budgets configured\n if (existing?.budget !== undefined && existing.budget > 0) continue;\n\n const tracked: TrackedService = {\n serviceId: service.id,\n detectedVia: existing?.detectedVia ?? det.sources,\n hasApiKey: existing?.hasApiKey ?? false,\n firstDetected: existing?.firstDetected ?? new Date().toISOString(),\n };\n\n if (defaultPlan && defaultPlan.type !== \"exclude\") {\n tracked.planName = defaultPlan.name;\n\n if (defaultPlan.type === \"flat\" && defaultPlan.monthlyBase !== undefined) {\n tracked.planCost = defaultPlan.monthlyBase;\n if (defaultPlan.monthlyBase > 0) {\n tracked.budget = defaultPlan.monthlyBase;\n }\n }\n }\n\n // Check for existing API key in global config\n const existingKey = globalConfig.services[service.id]?.apiKey;\n if (existingKey) {\n tracked.hasApiKey = true;\n }\n\n config.services[service.id] = tracked;\n }\n\n // Report findings\n if (detected.length === 0) {\n console.log(\" No paid services detected yet.\");\n console.log(' Services will be detected as they enter your project.\\n');\n } else {\n console.log(` Found ${detected.length} paid service${detected.length > 1 ? \"s\" : \"\"}:\\n`);\n\n const needBudget: string[] = [];\n for (const det of detected) {\n const svc = config.services[det.service.id];\n const tierBadge =\n det.service.apiTier === \"live\"\n ? svc?.hasApiKey ? \"✅ LIVE\" : \"🔴 BLIND\"\n : det.service.apiTier === \"calc\"\n ? \"🟡 CALC\"\n : det.service.apiTier === \"est\"\n ? \"🟠 EST\"\n : \"🔴 BLIND\";\n\n const planStr = svc?.planName ? ` (${svc.planName})` : \"\";\n const budgetStr = svc?.budget ? ` - $${svc.budget}/mo budget` : \"\";\n console.log(` • ${det.service.name}${planStr} ${tierBadge}${budgetStr}`);\n\n if (!svc?.budget) {\n needBudget.push(det.service.id);\n }\n }\n console.log(\"\");\n\n if (needBudget.length > 0) {\n console.log(` ${needBudget.length} service${needBudget.length > 1 ? \"s\" : \"\"} need budgets. Set them with:`);\n for (const id of needBudget) {\n console.log(` burnwatch add ${id} --budget <AMOUNT>`);\n }\n console.log(\"\");\n }\n }\n }\n\n writeProjectConfig(config, projectRoot);\n\n // Write initial .gitignore for the .burnwatch directory\n const gitignorePath = path.join(projectConfigDir(projectRoot), \".gitignore\");\n fs.writeFileSync(\n gitignorePath,\n [\n \"# Burnwatch — ignore cache and snapshots, keep ledger and config\",\n \"data/cache/\",\n \"data/snapshots/\",\n \"data/events.jsonl\",\n \"\",\n ].join(\"\\n\"),\n \"utf-8\",\n );\n\n // Register Claude Code hooks\n console.log(\"\\n🔗 Registering Claude Code hooks...\\n\");\n registerHooks(projectRoot);\n\n // Summary of excluded services\n const excluded = Object.values(config.services).filter((s) => s.excluded);\n const tracked = Object.values(config.services).filter((s) => !s.excluded);\n\n console.log(\"✅ burnwatch initialized!\\n\");\n\n if (tracked.length > 0) {\n console.log(` Tracking ${tracked.length} service${tracked.length > 1 ? \"s\" : \"\"}`);\n for (const svc of tracked) {\n const planStr = svc.planName ? ` (${svc.planName})` : \"\";\n const budgetStr = svc.budget !== undefined ? ` — $${svc.budget}/mo budget` : \"\";\n console.log(` • ${svc.serviceId}${planStr}${budgetStr}`);\n }\n }\n\n if (excluded.length > 0) {\n console.log(`\\n Excluded ${excluded.length} service${excluded.length > 1 ? \"s\" : \"\"}:`);\n for (const svc of excluded) {\n console.log(` • ${svc.serviceId}`);\n }\n }\n\n console.log(\"\\nNext steps:\");\n console.log(\" burnwatch status — Check your spend\");\n console.log(\" burnwatch add <svc> — Configure additional services\\n\");\n}\n\nasync function cmdAdd(): Promise<void> {\n const projectRoot = process.cwd();\n\n if (!isInitialized(projectRoot)) {\n console.error('❌ burnwatch not initialized. Run \"burnwatch init\" first.');\n process.exit(1);\n }\n\n const serviceId = args[1];\n if (!serviceId) {\n console.error(\"Usage: burnwatch add <service> [--key KEY] [--budget N]\");\n process.exit(1);\n }\n\n // Parse options\n const options: Record<string, string> = {};\n for (let i = 2; i < args.length; i++) {\n const arg = args[i]!;\n if (arg.startsWith(\"--\") && i + 1 < args.length) {\n options[arg.slice(2)] = args[i + 1]!;\n i++;\n }\n }\n\n const apiKey = options[\"key\"] ?? options[\"token\"];\n const budget = options[\"budget\"] ? parseFloat(options[\"budget\"]) : undefined;\n const planCost = options[\"plan-cost\"]\n ? parseFloat(options[\"plan-cost\"])\n : undefined;\n\n // Check if service is in registry\n const definition = getService(serviceId, projectRoot);\n if (!definition) {\n console.error(\n `⚠️ \"${serviceId}\" not found in registry. Adding as custom service.`,\n );\n }\n\n // Update project config\n const config = readProjectConfig(projectRoot)!;\n const existing = config.services[serviceId];\n\n const tracked: TrackedService = {\n serviceId,\n detectedVia: existing?.detectedVia ?? [\"manual\"],\n budget: budget ?? existing?.budget,\n hasApiKey: !!apiKey || (existing?.hasApiKey ?? false),\n planCost: planCost ?? existing?.planCost,\n firstDetected: existing?.firstDetected ?? new Date().toISOString(),\n };\n\n config.services[serviceId] = tracked;\n writeProjectConfig(config, projectRoot);\n\n // Save API key to global config (never in project dir)\n if (apiKey) {\n const globalConfig = readGlobalConfig();\n if (!globalConfig.services[serviceId]) {\n globalConfig.services[serviceId] = {};\n }\n globalConfig.services[serviceId]!.apiKey = apiKey;\n writeGlobalConfig(globalConfig);\n console.log(`🔐 API key saved to global config (never stored in project)`);\n }\n\n let tierLabel: string;\n if (!definition) {\n tierLabel = \"🔴 BLIND\";\n } else if (apiKey) {\n tierLabel = \"✅ LIVE\";\n } else if (planCost !== undefined) {\n tierLabel = \"🟡 CALC\";\n } else if (definition.apiTier === \"est\") {\n tierLabel = \"🟠 EST\";\n } else if (definition.apiTier === \"calc\") {\n tierLabel = \"🟡 CALC\";\n } else if (definition.apiTier === \"live\" && !apiKey) {\n tierLabel = `🔴 BLIND (add --key for ✅ LIVE)`;\n } else {\n tierLabel = \"🔴 BLIND\";\n }\n\n console.log(`\\n✅ ${serviceId} configured:`);\n console.log(` Tier: ${tierLabel}`);\n if (budget) console.log(` Budget: $${budget}/mo`);\n if (planCost) console.log(` Plan cost: $${planCost}/mo`);\n console.log(\"\");\n}\n\nasync function cmdStatus(): Promise<void> {\n const projectRoot = process.cwd();\n\n if (!isInitialized(projectRoot)) {\n console.error('❌ burnwatch not initialized. Run \"burnwatch init\" first.');\n process.exit(1);\n }\n\n const config = readProjectConfig(projectRoot)!;\n const trackedServices = Object.values(config.services);\n\n if (trackedServices.length === 0) {\n console.log(\"No services tracked yet.\");\n console.log('Run \"burnwatch add <service>\" to start tracking.');\n return;\n }\n\n console.log(\"📊 Polling services...\\n\");\n\n const results = await pollAllServices(trackedServices);\n const snapshots = results.map((r) =>\n buildSnapshot(\n r.serviceId,\n r.tier,\n r.spend,\n config.services[r.serviceId]?.budget,\n ),\n );\n\n const blindCount = snapshots.filter((s) => s.tier === \"blind\").length;\n const brief = buildBrief(config.projectName, snapshots, blindCount);\n\n // Save snapshot and update ledger\n saveSnapshot(brief, projectRoot);\n writeLedger(brief, projectRoot);\n\n // Display the brief\n console.log(formatBrief(brief));\n console.log(\"\");\n\n if (blindCount > 0) {\n console.log(`⚠️ ${blindCount} service${blindCount > 1 ? \"s\" : \"\"} untracked:`);\n for (const snap of snapshots.filter((s) => s.tier === \"blind\")) {\n console.log(` • ${snap.serviceId}`);\n }\n console.log(`\\n Run 'burnwatch init' to configure budgets and API keys.\\n`);\n }\n}\n\nasync function cmdSetup(): Promise<void> {\n const projectRoot = process.cwd();\n\n // Step 1: Init if needed\n if (!isInitialized(projectRoot)) {\n await cmdInit();\n }\n\n const config = readProjectConfig(projectRoot)!;\n const detected = Object.values(config.services);\n\n if (detected.length === 0) {\n console.log(\"No paid services detected. You're all set!\");\n return;\n }\n\n console.log(\"📋 Auto-configuring detected services...\\n\");\n\n // Step 2: Check global config for existing API keys\n const globalConfig = readGlobalConfig();\n\n // Step 3: Auto-configure each service based on registry tier + available keys\n const liveServices: string[] = [];\n const calcServices: string[] = [];\n const estServices: string[] = [];\n const blindServices: string[] = [];\n\n for (const tracked of detected) {\n const definition = getService(tracked.serviceId, projectRoot);\n if (!definition) continue;\n\n const hasKey = !!globalConfig.services[tracked.serviceId]?.apiKey;\n\n if (hasKey && definition.apiTier === \"live\") {\n tracked.hasApiKey = true;\n liveServices.push(`${definition.name}`);\n } else if (definition.apiTier === \"calc\") {\n calcServices.push(`${definition.name}`);\n } else if (definition.apiTier === \"est\") {\n estServices.push(`${definition.name}`);\n } else {\n blindServices.push(`${definition.name}`);\n }\n }\n\n writeProjectConfig(config, projectRoot);\n\n // Report\n if (liveServices.length > 0) {\n console.log(` ✅ LIVE (real billing data): ${liveServices.join(\", \")}`);\n }\n if (calcServices.length > 0) {\n console.log(` 🟡 CALC (flat-rate tracking): ${calcServices.join(\", \")}`);\n }\n if (estServices.length > 0) {\n console.log(` 🟠 EST (estimated from usage): ${estServices.join(\", \")}`);\n }\n if (blindServices.length > 0) {\n console.log(` 🔴 BLIND (detected, need API key): ${blindServices.join(\", \")}`);\n }\n\n console.log(\"\");\n\n if (blindServices.length > 0) {\n console.log(\"To upgrade BLIND services to LIVE, add API keys:\");\n for (const tracked of detected) {\n const definition = getService(tracked.serviceId, projectRoot);\n if (definition?.apiTier === \"live\" && !tracked.hasApiKey) {\n const envHint = definition.envPatterns[0] ?? \"YOUR_KEY\";\n console.log(` burnwatch add ${tracked.serviceId} --key $${envHint} --budget <N>`);\n }\n }\n console.log(\"\");\n }\n\n console.log(\"To set budgets for any service:\");\n console.log(\" burnwatch add <service> --budget <monthly_amount>\");\n console.log(\"\");\n console.log(\"Or use /setup-burnwatch in Claude Code for guided setup with budget suggestions.\\n\");\n\n // Show brief\n await cmdStatus();\n}\n\nfunction cmdServices(): void {\n const services = getAllServices();\n console.log(`\\n📋 Registry: ${services.length} services available\\n`);\n\n for (const svc of services) {\n const tierBadge =\n svc.apiTier === \"live\"\n ? \"✅ LIVE\"\n : svc.apiTier === \"calc\"\n ? \"🟡 CALC\"\n : svc.apiTier === \"est\"\n ? \"🟠 EST\"\n : \"🔴 BLIND\";\n\n console.log(` ${svc.name.padEnd(24)} ${tierBadge.padEnd(10)} ${svc.billingModel}`);\n }\n\n console.log(\"\");\n}\n\nasync function cmdReconcile(): Promise<void> {\n const projectRoot = process.cwd();\n\n if (!isInitialized(projectRoot)) {\n console.error('❌ burnwatch not initialized. Run \"burnwatch init\" first.');\n process.exit(1);\n }\n\n console.log(\"🔍 Scanning for untracked services and missed sessions...\\n\");\n\n // Re-run detection against current project state\n const detected = detectServices(projectRoot);\n const config = readProjectConfig(projectRoot)!;\n let newCount = 0;\n\n for (const det of detected) {\n if (!config.services[det.service.id]) {\n config.services[det.service.id] = {\n serviceId: det.service.id,\n detectedVia: det.sources,\n hasApiKey: false,\n firstDetected: new Date().toISOString(),\n };\n newCount++;\n console.log(` 🆕 ${det.service.name} — detected via ${det.details.join(\", \")}`);\n }\n }\n\n if (newCount > 0) {\n writeProjectConfig(config, projectRoot);\n console.log(\n `\\n✅ Found ${newCount} new service${newCount > 1 ? \"s\" : \"\"}. Run 'burnwatch status' to see updated brief.`,\n );\n } else {\n console.log(\" ✅ No new services found. All services already tracked.\");\n }\n\n console.log(\"\");\n}\n\nfunction cmdHelp(): void {\n console.log(`\nburnwatch — Passive cost memory for vibe coding\n\nUsage:\n burnwatch init Interactive setup — pick plans per service\n burnwatch init --non-interactive Auto-detect services, no prompts\n burnwatch setup Init + auto-configure all detected services\n burnwatch add <service> [options] Register a service for tracking\n burnwatch status Show current spend brief\n burnwatch services List all services in registry\n burnwatch reconcile Scan for untracked services\n\nOptions for 'add':\n --key <API_KEY> API key for LIVE tracking (saved to ~/.config/burnwatch/)\n --token <TOKEN> Same as --key (alias)\n --budget <AMOUNT> Monthly budget in USD\n --plan-cost <AMOUNT> Monthly plan cost for CALC tracking\n\nExamples:\n burnwatch init\n burnwatch init --non-interactive\n burnwatch add anthropic --key sk-ant-admin-xxx --budget 100\n burnwatch add scrapfly --key scp-xxx --budget 50\n burnwatch add posthog --plan-cost 0 --budget 0\n burnwatch status\n`);\n}\n\nfunction cmdVersion(): void {\n try {\n const pkgPath = path.resolve(\n path.dirname(new URL(import.meta.url).pathname),\n \"../package.json\",\n );\n const pkg = JSON.parse(fs.readFileSync(pkgPath, \"utf-8\")) as {\n version: string;\n };\n console.log(`burnwatch v${pkg.version}`);\n } catch {\n console.log(\"burnwatch v0.1.0\");\n }\n}\n\n// --- Hook Registration ---\n\nfunction registerHooks(projectRoot: string): void {\n // Step 1: Copy hook scripts into .burnwatch/hooks/ for durability.\n // This avoids relying on ephemeral npx cache paths.\n const sourceHooksDir = path.resolve(\n path.dirname(new URL(import.meta.url).pathname),\n \"hooks\",\n );\n const localHooksDir = path.join(projectRoot, \".burnwatch\", \"hooks\");\n fs.mkdirSync(localHooksDir, { recursive: true });\n\n const hookFiles = [\n \"on-session-start.js\",\n \"on-prompt.js\",\n \"on-file-change.js\",\n \"on-stop.js\",\n ];\n\n for (const file of hookFiles) {\n const src = path.join(sourceHooksDir, file);\n const dest = path.join(localHooksDir, file);\n try {\n fs.copyFileSync(src, dest);\n // Also copy sourcemaps if they exist\n const mapSrc = src + \".map\";\n if (fs.existsSync(mapSrc)) {\n fs.copyFileSync(mapSrc, dest + \".map\");\n }\n } catch (err) {\n console.error(` Warning: Could not copy hook ${file}: ${err instanceof Error ? err.message : err}`);\n }\n }\n\n console.log(` Hook scripts copied to ${localHooksDir}`);\n\n // Step 2: Find or create .claude/settings.json — MERGE, never overwrite\n const claudeDir = path.join(projectRoot, \".claude\");\n const settingsPath = path.join(claudeDir, \"settings.json\");\n\n fs.mkdirSync(claudeDir, { recursive: true });\n\n // Read existing settings (preserve everything)\n let settings: Record<string, unknown> = {};\n try {\n const existing = fs.readFileSync(settingsPath, \"utf-8\");\n settings = JSON.parse(existing) as Record<string, unknown>;\n console.log(` Merging into existing ${settingsPath}`);\n } catch {\n // No existing settings — start fresh\n }\n\n // Ensure hooks object exists, preserve all existing hooks\n if (!settings[\"hooks\"] || typeof settings[\"hooks\"] !== \"object\") {\n settings[\"hooks\"] = {};\n }\n const hooks = settings[\"hooks\"] as Record<string, unknown[]>;\n\n // Use the local .burnwatch/hooks/ paths (durable, not ephemeral)\n const hooksDir = localHooksDir;\n\n // SessionStart hook\n if (!hooks[\"SessionStart\"]) hooks[\"SessionStart\"] = [];\n addHookIfMissing(hooks[\"SessionStart\"] as unknown[], \"SessionStart\", {\n matcher: \"startup|resume\",\n hooks: [\n {\n type: \"command\",\n command: `node \"${path.join(hooksDir, \"on-session-start.js\")}\"`,\n timeout: 15,\n },\n ],\n });\n\n // UserPromptSubmit hook\n if (!hooks[\"UserPromptSubmit\"]) hooks[\"UserPromptSubmit\"] = [];\n addHookIfMissing(\n hooks[\"UserPromptSubmit\"] as unknown[],\n \"UserPromptSubmit\",\n {\n hooks: [\n {\n type: \"command\",\n command: `node \"${path.join(hooksDir, \"on-prompt.js\")}\"`,\n timeout: 5,\n },\n ],\n },\n );\n\n // PostToolUse hook (Edit|Write only)\n if (!hooks[\"PostToolUse\"]) hooks[\"PostToolUse\"] = [];\n addHookIfMissing(hooks[\"PostToolUse\"] as unknown[], \"PostToolUse\", {\n matcher: \"Edit|Write\",\n hooks: [\n {\n type: \"command\",\n command: `node \"${path.join(hooksDir, \"on-file-change.js\")}\"`,\n timeout: 5,\n },\n ],\n });\n\n // Stop hook (async — don't block session end)\n if (!hooks[\"Stop\"]) hooks[\"Stop\"] = [];\n addHookIfMissing(hooks[\"Stop\"] as unknown[], \"Stop\", {\n hooks: [\n {\n type: \"command\",\n command: `node \"${path.join(hooksDir, \"on-stop.js\")}\"`,\n timeout: 15,\n async: true,\n },\n ],\n });\n\n settings[\"hooks\"] = hooks;\n fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + \"\\n\", \"utf-8\");\n console.log(` Hooks registered in ${settingsPath}`);\n}\n\nfunction addHookIfMissing(\n hookArray: unknown[],\n _eventName: string,\n hookConfig: unknown,\n): void {\n // Check if burnwatch hook is already registered\n const existing = hookArray.some((h) => {\n const hook = h as { hooks?: Array<{ command?: string }> };\n return hook.hooks?.some((inner) => inner.command?.includes(\"burnwatch\"));\n });\n\n if (!existing) {\n hookArray.push(hookConfig);\n }\n}\n\n// --- Entry ---\n\nmain().catch((err) => {\n console.error(\"Error:\", err instanceof Error ? err.message : err);\n process.exit(1);\n});\n","import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport * as os from \"node:os\";\nimport type { TrackedService } from \"./types.js\";\n\n/**\n * Paths for burnwatch configuration and data.\n *\n * Hybrid model:\n * - Global config (API keys, service credentials): ~/.config/burnwatch/\n * - Project config (budgets, tracked services): .burnwatch/\n * - Project data (ledger, events, cache): .burnwatch/data/\n */\n\n/** Global config directory — stores API keys, never in project dirs. */\nexport function globalConfigDir(): string {\n const xdgConfig = process.env[\"XDG_CONFIG_HOME\"];\n if (xdgConfig) return path.join(xdgConfig, \"burnwatch\");\n return path.join(os.homedir(), \".config\", \"burnwatch\");\n}\n\n/** Project config directory — stores budgets, tracked services. */\nexport function projectConfigDir(projectRoot?: string): string {\n const root = projectRoot ?? process.cwd();\n return path.join(root, \".burnwatch\");\n}\n\n/** Project data directory — stores ledger, events, cache. */\nexport function projectDataDir(projectRoot?: string): string {\n return path.join(projectConfigDir(projectRoot), \"data\");\n}\n\n// --- Global config (API keys) ---\n\nexport interface GlobalConfig {\n services: Record<\n string,\n {\n apiKey?: string;\n token?: string;\n orgId?: string;\n }\n >;\n}\n\nexport function readGlobalConfig(): GlobalConfig {\n const configPath = path.join(globalConfigDir(), \"config.json\");\n try {\n const raw = fs.readFileSync(configPath, \"utf-8\");\n return JSON.parse(raw) as GlobalConfig;\n } catch {\n return { services: {} };\n }\n}\n\nexport function writeGlobalConfig(config: GlobalConfig): void {\n const dir = globalConfigDir();\n fs.mkdirSync(dir, { recursive: true });\n const configPath = path.join(dir, \"config.json\");\n fs.writeFileSync(configPath, JSON.stringify(config, null, 2) + \"\\n\", \"utf-8\");\n // Restrict permissions — this file contains API keys\n fs.chmodSync(configPath, 0o600);\n}\n\n// --- Project config (budgets, tracked services) ---\n\nexport interface ProjectConfig {\n projectName: string;\n services: Record<string, TrackedService>;\n createdAt: string;\n updatedAt: string;\n}\n\nexport function readProjectConfig(projectRoot?: string): ProjectConfig | null {\n const configPath = path.join(projectConfigDir(projectRoot), \"config.json\");\n try {\n const raw = fs.readFileSync(configPath, \"utf-8\");\n return JSON.parse(raw) as ProjectConfig;\n } catch {\n return null;\n }\n}\n\nexport function writeProjectConfig(\n config: ProjectConfig,\n projectRoot?: string,\n): void {\n const dir = projectConfigDir(projectRoot);\n fs.mkdirSync(dir, { recursive: true });\n config.updatedAt = new Date().toISOString();\n const configPath = path.join(dir, \"config.json\");\n fs.writeFileSync(configPath, JSON.stringify(config, null, 2) + \"\\n\", \"utf-8\");\n}\n\n/** Ensure all project directories exist. */\nexport function ensureProjectDirs(projectRoot?: string): void {\n const dirs = [\n projectConfigDir(projectRoot),\n projectDataDir(projectRoot),\n path.join(projectDataDir(projectRoot), \"cache\"),\n path.join(projectDataDir(projectRoot), \"snapshots\"),\n ];\n for (const dir of dirs) {\n fs.mkdirSync(dir, { recursive: true });\n }\n}\n\n/** Check if burnwatch is initialized in the given project. */\nexport function isInitialized(projectRoot?: string): boolean {\n return readProjectConfig(projectRoot) !== null;\n}\n","import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport { loadRegistry } from \"../core/registry.js\";\nimport type { ServiceDefinition, DetectionSource } from \"../core/types.js\";\n\nexport interface DetectionResult {\n service: ServiceDefinition;\n sources: DetectionSource[];\n details: string[];\n}\n\n/**\n * Run all detection surfaces against the current project.\n * Returns services detected via any combination of:\n * - package.json dependencies (recursive — finds monorepo subdirectories)\n * - environment variable patterns (process.env + .env* files recursive)\n * - import statement scanning (recursive from project root)\n * - (prompt mention scanning is handled separately in hooks)\n */\nexport function detectServices(projectRoot: string): DetectionResult[] {\n const registry = loadRegistry(projectRoot);\n const results = new Map<string, DetectionResult>();\n\n // Surface 1: Package manifest scanning (recursive — finds all package.json files)\n const pkgDeps = scanAllPackageJsons(projectRoot);\n for (const [serviceId, service] of registry) {\n const matchedPkgs = service.packageNames.filter((pkg) =>\n pkgDeps.has(pkg),\n );\n if (matchedPkgs.length > 0) {\n getOrCreate(results, serviceId, service).sources.push(\"package_json\");\n getOrCreate(results, serviceId, service).details.push(\n `package.json: ${matchedPkgs.join(\", \")}`,\n );\n }\n }\n\n // Surface 2: Environment variable pattern matching\n // Check both process.env AND .env* files in the project tree\n const envVars = collectEnvVars(projectRoot);\n for (const [serviceId, service] of registry) {\n const matchedEnvs = service.envPatterns.filter((pattern) =>\n envVars.has(pattern),\n );\n if (matchedEnvs.length > 0) {\n getOrCreate(results, serviceId, service).sources.push(\"env_var\");\n getOrCreate(results, serviceId, service).details.push(\n `env vars: ${matchedEnvs.join(\", \")}`,\n );\n }\n }\n\n // Surface 3: Import statement analysis (recursive from project root)\n const importHits = scanImports(projectRoot);\n for (const [serviceId, service] of registry) {\n const matchedImports = service.importPatterns.filter((pattern) =>\n importHits.has(pattern),\n );\n if (matchedImports.length > 0) {\n if (\n !getOrCreate(results, serviceId, service).sources.includes(\n \"import_scan\",\n )\n ) {\n getOrCreate(results, serviceId, service).sources.push(\"import_scan\");\n getOrCreate(results, serviceId, service).details.push(\n `imports: ${matchedImports.join(\", \")}`,\n );\n }\n }\n }\n\n return Array.from(results.values());\n}\n\n/**\n * Detect services mentioned in a prompt string.\n * Used by the UserPromptSubmit hook.\n */\nexport function detectMentions(\n prompt: string,\n projectRoot?: string,\n): DetectionResult[] {\n const registry = loadRegistry(projectRoot);\n const results: DetectionResult[] = [];\n const promptLower = prompt.toLowerCase();\n\n for (const [, service] of registry) {\n const matched = service.mentionKeywords.some((keyword) =>\n promptLower.includes(keyword.toLowerCase()),\n );\n if (matched) {\n results.push({\n service,\n sources: [\"prompt_mention\"],\n details: [`mentioned in prompt`],\n });\n }\n }\n\n return results;\n}\n\n/**\n * Detect new services introduced in a file change.\n * Used by the PostToolUse hook for Write/Edit events.\n */\nexport function detectInFileChange(\n filePath: string,\n content: string,\n projectRoot?: string,\n): DetectionResult[] {\n const registry = loadRegistry(projectRoot);\n const results: DetectionResult[] = [];\n const fileName = path.basename(filePath);\n\n // Check if it's a package.json change\n if (fileName === \"package.json\") {\n try {\n const pkg = JSON.parse(content) as {\n dependencies?: Record<string, string>;\n devDependencies?: Record<string, string>;\n };\n const allDeps = new Set([\n ...Object.keys(pkg.dependencies ?? {}),\n ...Object.keys(pkg.devDependencies ?? {}),\n ]);\n\n for (const [, service] of registry) {\n const matched = service.packageNames.filter((p) => allDeps.has(p));\n if (matched.length > 0) {\n results.push({\n service,\n sources: [\"package_json\"],\n details: [`new dependency: ${matched.join(\", \")}`],\n });\n }\n }\n } catch {\n // Not valid JSON, skip\n }\n return results;\n }\n\n // Check if it's an env file change\n if (fileName.startsWith(\".env\")) {\n const envKeys = content\n .split(\"\\n\")\n .filter((line) => line.includes(\"=\") && !line.startsWith(\"#\"))\n .map((line) => line.split(\"=\")[0]!.trim());\n\n for (const [, service] of registry) {\n const matched = service.envPatterns.filter((p) => envKeys.includes(p));\n if (matched.length > 0) {\n results.push({\n service,\n sources: [\"env_var\"],\n details: [`new env var: ${matched.join(\", \")}`],\n });\n }\n }\n return results;\n }\n\n // Check for import statements in source files\n if (/\\.(ts|tsx|js|jsx|mjs|cjs)$/.test(filePath)) {\n for (const [, service] of registry) {\n const matched = service.importPatterns.filter(\n (pattern) =>\n content.includes(`from \"${pattern}`) ||\n content.includes(`from '${pattern}`) ||\n content.includes(`require(\"${pattern}`) ||\n content.includes(`require('${pattern}`),\n );\n if (matched.length > 0) {\n results.push({\n service,\n sources: [\"import_scan\"],\n details: [`import added: ${matched.join(\", \")}`],\n });\n }\n }\n }\n\n return results;\n}\n\n// --- Helpers ---\n\nfunction getOrCreate(\n map: Map<string, DetectionResult>,\n serviceId: string,\n service: ServiceDefinition,\n): DetectionResult {\n let result = map.get(serviceId);\n if (!result) {\n result = { service, sources: [], details: [] };\n map.set(serviceId, result);\n }\n return result;\n}\n\n/**\n * Recursively find and scan ALL package.json files in the project tree.\n * Handles monorepos where dependencies live in subdirectories.\n */\nfunction scanAllPackageJsons(projectRoot: string): Set<string> {\n const deps = new Set<string>();\n const pkgFiles = findFiles(projectRoot, \"package.json\", 4);\n\n for (const pkgPath of pkgFiles) {\n try {\n const raw = fs.readFileSync(pkgPath, \"utf-8\");\n const pkg = JSON.parse(raw) as {\n dependencies?: Record<string, string>;\n devDependencies?: Record<string, string>;\n };\n for (const name of Object.keys(pkg.dependencies ?? {})) deps.add(name);\n for (const name of Object.keys(pkg.devDependencies ?? {})) deps.add(name);\n } catch {\n // Skip malformed package.json\n }\n }\n\n return deps;\n}\n\n/**\n * Collect environment variable names from both process.env\n * and all .env* files found recursively in the project tree.\n */\nfunction collectEnvVars(projectRoot: string): Set<string> {\n const envVars = new Set(Object.keys(process.env));\n\n // Find all .env* files in the project tree\n const envFiles = findEnvFiles(projectRoot, 3);\n\n for (const envFile of envFiles) {\n try {\n const content = fs.readFileSync(envFile, \"utf-8\");\n const keys = content\n .split(\"\\n\")\n .filter((line) => line.includes(\"=\") && !line.startsWith(\"#\"))\n .map((line) => line.split(\"=\")[0]!.trim())\n .filter(Boolean);\n\n for (const key of keys) {\n envVars.add(key);\n }\n } catch {\n // Skip unreadable files\n }\n }\n\n return envVars;\n}\n\n/**\n * Find all .env* files recursively (but not in node_modules, .git, dist, etc.)\n */\nfunction findEnvFiles(dir: string, maxDepth: number): string[] {\n const results: string[] = [];\n if (maxDepth <= 0) return results;\n\n try {\n const entries = fs.readdirSync(dir, { withFileTypes: true });\n for (const entry of entries) {\n if (entry.name === \"node_modules\" || entry.name === \".git\" || entry.name === \"dist\") continue;\n const fullPath = path.join(dir, entry.name);\n if (entry.isDirectory()) {\n results.push(...findEnvFiles(fullPath, maxDepth - 1));\n } else if (entry.name.startsWith(\".env\")) {\n results.push(fullPath);\n }\n }\n } catch {\n // Skip unreadable directories\n }\n\n return results;\n}\n\n/**\n * Find files with a specific name recursively.\n * Used to find package.json files across monorepo subdirectories.\n */\nfunction findFiles(dir: string, fileName: string, maxDepth: number): string[] {\n const results: string[] = [];\n if (maxDepth <= 0) return results;\n\n try {\n const entries = fs.readdirSync(dir, { withFileTypes: true });\n for (const entry of entries) {\n if (entry.name === \"node_modules\" || entry.name === \".git\" || entry.name === \"dist\") continue;\n const fullPath = path.join(dir, entry.name);\n if (entry.isDirectory()) {\n results.push(...findFiles(fullPath, fileName, maxDepth - 1));\n } else if (entry.name === fileName) {\n results.push(fullPath);\n }\n }\n } catch {\n // Skip unreadable directories\n }\n\n return results;\n}\n\n/**\n * Lightweight import scanning.\n * Recursively scans the project for import/require statements.\n * Looks in src/, app/, lib/, pages/, and any other code directories.\n * Does NOT do a full AST parse — just string matching.\n */\nfunction scanImports(projectRoot: string): Set<string> {\n const imports = new Set<string>();\n\n // Scan common code directories + the root itself for source files\n const codeDirs = [\"src\", \"app\", \"lib\", \"pages\", \"components\", \"utils\", \"services\", \"hooks\"];\n const dirsToScan: string[] = [];\n\n for (const dir of codeDirs) {\n const fullPath = path.join(projectRoot, dir);\n if (fs.existsSync(fullPath)) {\n dirsToScan.push(fullPath);\n }\n }\n\n // Also check subdirectories (monorepo support)\n try {\n const entries = fs.readdirSync(projectRoot, { withFileTypes: true });\n for (const entry of entries) {\n if (!entry.isDirectory()) continue;\n if (entry.name === \"node_modules\" || entry.name === \".git\" || entry.name === \"dist\" || entry.name.startsWith(\".\")) continue;\n\n // Check if this subdirectory has its own package.json (monorepo package)\n const subPkgPath = path.join(projectRoot, entry.name, \"package.json\");\n if (fs.existsSync(subPkgPath)) {\n // Scan this subpackage's code directories\n for (const dir of codeDirs) {\n const fullPath = path.join(projectRoot, entry.name, dir);\n if (fs.existsSync(fullPath)) {\n dirsToScan.push(fullPath);\n }\n }\n }\n }\n } catch {\n // Skip if root is unreadable\n }\n\n for (const dir of dirsToScan) {\n const files = walkDir(dir, /\\.(ts|tsx|js|jsx|mjs|cjs)$/);\n for (const file of files) {\n try {\n const content = fs.readFileSync(file, \"utf-8\");\n // Match: import ... from \"package\" or require(\"package\")\n const importRegex =\n /(?:from\\s+[\"']|require\\s*\\(\\s*[\"'])([^./][^\"']*?)(?:[\"'])/g;\n let match: RegExpExecArray | null;\n while ((match = importRegex.exec(content)) !== null) {\n const pkg = match[1];\n if (pkg) {\n // Normalize scoped packages: @scope/pkg/subpath -> @scope/pkg\n const parts = pkg.split(\"/\");\n if (parts[0]?.startsWith(\"@\") && parts.length >= 2) {\n imports.add(`${parts[0]}/${parts[1]}`);\n } else if (parts[0]) {\n imports.add(parts[0]);\n }\n }\n }\n } catch {\n // Skip unreadable files\n }\n }\n }\n\n return imports;\n}\n\n/** Recursively walk a directory, returning files matching the pattern. */\nfunction walkDir(dir: string, pattern: RegExp, maxDepth = 5): string[] {\n const results: string[] = [];\n if (maxDepth <= 0) return results;\n\n try {\n const entries = fs.readdirSync(dir, { withFileTypes: true });\n for (const entry of entries) {\n if (entry.name.startsWith(\".\") || entry.name === \"node_modules\") continue;\n const fullPath = path.join(dir, entry.name);\n if (entry.isDirectory()) {\n results.push(...walkDir(fullPath, pattern, maxDepth - 1));\n } else if (pattern.test(entry.name)) {\n results.push(fullPath);\n }\n }\n } catch {\n // Skip unreadable directories\n }\n\n return results;\n}\n","import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport * as url from \"node:url\";\nimport type { ServiceDefinition } from \"./types.js\";\n\nconst __dirname = path.dirname(url.fileURLToPath(import.meta.url));\n\ninterface RegistryFile {\n version: string;\n lastUpdated: string;\n services: Record<string, ServiceDefinition>;\n}\n\nlet cachedRegistry: Map<string, ServiceDefinition> | null = null;\n\n/**\n * Load the service registry.\n * Checks project-local override first, then falls back to bundled registry.\n */\nexport function loadRegistry(projectRoot?: string): Map<string, ServiceDefinition> {\n if (cachedRegistry) return cachedRegistry;\n\n const registry = new Map<string, ServiceDefinition>();\n\n // Load bundled registry (shipped with package)\n // Try multiple possible locations — depends on whether running from src/ or dist/\n const candidates = [\n path.resolve(__dirname, \"../../registry.json\"), // from src/core/\n path.resolve(__dirname, \"../registry.json\"), // from dist/\n ];\n for (const candidate of candidates) {\n if (fs.existsSync(candidate)) {\n loadRegistryFile(candidate, registry);\n break;\n }\n }\n\n // Load project-local override (if exists)\n if (projectRoot) {\n const localPath = path.join(projectRoot, \".burnwatch\", \"registry.json\");\n if (fs.existsSync(localPath)) {\n loadRegistryFile(localPath, registry);\n }\n }\n\n cachedRegistry = registry;\n return registry;\n}\n\nfunction loadRegistryFile(\n filePath: string,\n registry: Map<string, ServiceDefinition>,\n): void {\n try {\n const raw = fs.readFileSync(filePath, \"utf-8\");\n const data = JSON.parse(raw) as RegistryFile;\n for (const [id, service] of Object.entries(data.services)) {\n registry.set(id, { ...service, id });\n }\n } catch {\n // Silently skip missing or malformed registry files\n }\n}\n\n/** Clear the cached registry (for testing). */\nexport function clearRegistryCache(): void {\n cachedRegistry = null;\n}\n\n/** Get a single service definition by ID. */\nexport function getService(\n id: string,\n projectRoot?: string,\n): ServiceDefinition | undefined {\n return loadRegistry(projectRoot).get(id);\n}\n\n/** Get all service definitions. */\nexport function getAllServices(\n projectRoot?: string,\n): ServiceDefinition[] {\n return Array.from(loadRegistry(projectRoot).values());\n}\n","import type { ConfidenceTier } from \"../core/types.js\";\n\n/** Result from polling a billing API. */\nexport interface BillingResult {\n serviceId: string;\n spend: number;\n isEstimate: boolean;\n tier: ConfidenceTier;\n raw?: Record<string, unknown>;\n error?: string;\n}\n\n/**\n * Base interface for service billing connectors.\n * Each LIVE service implements this to fetch real spend data.\n */\nexport interface BillingConnector {\n serviceId: string;\n /** Fetch current period spend. */\n fetchSpend(apiKey: string, options?: Record<string, string>): Promise<BillingResult>;\n}\n\n/**\n * Make an HTTP request and return JSON.\n * Uses native fetch (Node 18+). No external dependencies.\n */\nexport async function fetchJson<T>(\n url: string,\n options: {\n headers?: Record<string, string>;\n method?: string;\n body?: string;\n timeout?: number;\n } = {},\n): Promise<{ ok: boolean; status: number; data?: T; error?: string }> {\n try {\n const controller = new AbortController();\n const timeoutId = setTimeout(\n () => controller.abort(),\n options.timeout ?? 10_000,\n );\n\n const response = await fetch(url, {\n method: options.method ?? \"GET\",\n headers: options.headers,\n body: options.body,\n signal: controller.signal,\n });\n\n clearTimeout(timeoutId);\n\n if (!response.ok) {\n return {\n ok: false,\n status: response.status,\n error: `HTTP ${response.status}: ${response.statusText}`,\n };\n }\n\n const data = (await response.json()) as T;\n return { ok: true, status: response.status, data };\n } catch (err) {\n return {\n ok: false,\n status: 0,\n error: err instanceof Error ? err.message : \"Unknown error\",\n };\n }\n}\n","import type { BillingConnector, BillingResult } from \"./base.js\";\nimport { fetchJson } from \"./base.js\";\n\n/**\n * Anthropic billing connector.\n * Uses the /v1/organizations/usage endpoint.\n * Requires an admin API key.\n */\nexport const anthropicConnector: BillingConnector = {\n serviceId: \"anthropic\",\n\n async fetchSpend(apiKey: string): Promise<BillingResult> {\n // Get current month date range\n const now = new Date();\n const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);\n const startDate = startOfMonth.toISOString().split(\"T\")[0]!;\n const endDate = now.toISOString().split(\"T\")[0]!;\n\n const url = `https://api.anthropic.com/v1/organizations/usage?start_date=${startDate}&end_date=${endDate}`;\n\n const result = await fetchJson<{\n data?: Array<{ total_cost_usd?: number; spend?: number }>;\n total_cost_usd?: number;\n }>(url, {\n headers: {\n \"x-api-key\": apiKey,\n \"anthropic-version\": \"2023-06-01\",\n },\n });\n\n if (!result.ok || !result.data) {\n return {\n serviceId: \"anthropic\",\n spend: 0,\n isEstimate: true,\n tier: \"est\",\n error: result.error ?? \"Failed to fetch Anthropic usage\",\n };\n }\n\n // Sum up usage across the period\n let totalSpend = 0;\n if (result.data.total_cost_usd !== undefined) {\n totalSpend = result.data.total_cost_usd;\n } else if (result.data.data) {\n totalSpend = result.data.data.reduce(\n (sum, entry) => sum + (entry.total_cost_usd ?? entry.spend ?? 0),\n 0,\n );\n }\n\n return {\n serviceId: \"anthropic\",\n spend: totalSpend,\n isEstimate: false,\n tier: \"live\",\n raw: result.data as unknown as Record<string, unknown>,\n };\n },\n};\n","import type { BillingConnector, BillingResult } from \"./base.js\";\nimport { fetchJson } from \"./base.js\";\n\n/**\n * OpenAI billing connector.\n * Uses the /v1/organization/costs endpoint.\n * Requires an organization-level API key.\n */\nexport const openaiConnector: BillingConnector = {\n serviceId: \"openai\",\n\n async fetchSpend(apiKey: string): Promise<BillingResult> {\n const now = new Date();\n const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);\n // OpenAI uses Unix timestamps\n const startTime = Math.floor(startOfMonth.getTime() / 1000);\n\n const url = `https://api.openai.com/v1/organization/costs?start_time=${startTime}`;\n\n const result = await fetchJson<{\n data?: Array<{\n results?: Array<{\n amount?: { value?: number };\n }>;\n }>;\n object?: string;\n }>(url, {\n headers: {\n Authorization: `Bearer ${apiKey}`,\n },\n });\n\n if (!result.ok || !result.data) {\n return {\n serviceId: \"openai\",\n spend: 0,\n isEstimate: true,\n tier: \"est\",\n error: result.error ?? \"Failed to fetch OpenAI usage\",\n };\n }\n\n // Sum all cost buckets\n let totalSpend = 0;\n if (result.data.data) {\n for (const bucket of result.data.data) {\n if (bucket.results) {\n for (const r of bucket.results) {\n totalSpend += r.amount?.value ?? 0;\n }\n }\n }\n }\n\n // OpenAI returns costs in cents, convert to dollars\n totalSpend = totalSpend / 100;\n\n return {\n serviceId: \"openai\",\n spend: totalSpend,\n isEstimate: false,\n tier: \"live\",\n raw: result.data as unknown as Record<string, unknown>,\n };\n },\n};\n","import type { BillingConnector, BillingResult } from \"./base.js\";\nimport { fetchJson } from \"./base.js\";\n\n/**\n * Vercel billing connector.\n * Uses the Vercel billing API.\n * Requires a Vercel token (personal or team-scoped).\n */\nexport const vercelConnector: BillingConnector = {\n serviceId: \"vercel\",\n\n async fetchSpend(\n token: string,\n options?: Record<string, string>,\n ): Promise<BillingResult> {\n const teamId = options?.[\"teamId\"] ?? \"\";\n const teamParam = teamId ? `?teamId=${teamId}` : \"\";\n\n // Fetch current billing period usage\n const url = `https://api.vercel.com/v2/usage${teamParam}`;\n\n const result = await fetchJson<{\n usage?: {\n total?: number;\n bandwidth?: { total?: number };\n serverlessFunctionExecution?: { total?: number };\n edgeFunctionExecution?: { total?: number };\n imageOptimization?: { total?: number };\n };\n billing?: {\n plan?: string;\n period?: { start?: string; end?: string };\n invoiceItems?: Array<{ amount?: number }>;\n };\n }>(url, {\n headers: {\n Authorization: `Bearer ${token}`,\n },\n });\n\n if (!result.ok || !result.data) {\n return {\n serviceId: \"vercel\",\n spend: 0,\n isEstimate: true,\n tier: \"est\",\n error: result.error ?? \"Failed to fetch Vercel usage\",\n };\n }\n\n // Sum up usage costs\n let totalSpend = 0;\n if (result.data.usage?.total !== undefined) {\n totalSpend = result.data.usage.total;\n } else if (result.data.billing?.invoiceItems) {\n totalSpend = result.data.billing.invoiceItems.reduce(\n (sum, item) => sum + (item.amount ?? 0),\n 0,\n );\n }\n\n return {\n serviceId: \"vercel\",\n spend: totalSpend,\n isEstimate: false,\n tier: \"live\",\n raw: result.data as unknown as Record<string, unknown>,\n };\n },\n};\n","import type { BillingConnector, BillingResult } from \"./base.js\";\nimport { fetchJson } from \"./base.js\";\n\n/**\n * Scrapfly billing connector.\n * Uses the /account endpoint which returns credits used/remaining.\n * Works with the standard API key — no special billing key needed.\n */\nexport const scrapflyConnector: BillingConnector = {\n serviceId: \"scrapfly\",\n\n async fetchSpend(apiKey: string): Promise<BillingResult> {\n const url = `https://api.scrapfly.io/account?key=${apiKey}`;\n\n const result = await fetchJson<{\n subscription?: {\n usage?: {\n scrape?: { used?: number; allowed?: number };\n };\n };\n account?: {\n credits_used?: number;\n credits_total?: number;\n };\n }>(url);\n\n if (!result.ok || !result.data) {\n return {\n serviceId: \"scrapfly\",\n spend: 0,\n isEstimate: true,\n tier: \"est\",\n error: result.error ?? \"Failed to fetch Scrapfly account\",\n };\n }\n\n // Extract credits used from the response\n let creditsUsed = 0;\n let creditsTotal = 0;\n\n if (result.data.subscription?.usage?.scrape) {\n creditsUsed = result.data.subscription.usage.scrape.used ?? 0;\n creditsTotal = result.data.subscription.usage.scrape.allowed ?? 0;\n } else if (result.data.account) {\n creditsUsed = result.data.account.credits_used ?? 0;\n creditsTotal = result.data.account.credits_total ?? 0;\n }\n\n // Convert credits to USD at registry rate\n const creditRate = 0.00015; // $0.00015 per credit\n const spend = creditsUsed * creditRate;\n\n return {\n serviceId: \"scrapfly\",\n spend,\n isEstimate: false,\n tier: \"live\",\n raw: {\n credits_used: creditsUsed,\n credits_total: creditsTotal,\n credit_rate: creditRate,\n ...(result.data as unknown as Record<string, unknown>),\n },\n };\n },\n};\n","import type { BillingConnector, BillingResult } from \"./base.js\";\nimport { anthropicConnector } from \"./anthropic.js\";\nimport { openaiConnector } from \"./openai.js\";\nimport { vercelConnector } from \"./vercel.js\";\nimport { scrapflyConnector } from \"./scrapfly.js\";\nimport type { TrackedService, ConfidenceTier } from \"../core/types.js\";\nimport { readGlobalConfig } from \"../core/config.js\";\nimport { getService } from \"../core/registry.js\";\n\n/** All available billing connectors, keyed by service ID. */\nconst connectors: Map<string, BillingConnector> = new Map([\n [\"anthropic\", anthropicConnector],\n [\"openai\", openaiConnector],\n [\"vercel\", vercelConnector],\n [\"scrapfly\", scrapflyConnector],\n]);\n\n/**\n * Poll spend for a single tracked service.\n * Returns the best available data based on connector availability and API keys.\n */\nexport async function pollService(\n tracked: TrackedService,\n): Promise<BillingResult> {\n const globalConfig = readGlobalConfig();\n const serviceConfig = globalConfig.services[tracked.serviceId];\n const connector = connectors.get(tracked.serviceId);\n const definition = getService(tracked.serviceId);\n\n // If we have a connector and an API key, try LIVE\n if (connector && serviceConfig?.apiKey) {\n try {\n const result = await connector.fetchSpend(\n serviceConfig.apiKey,\n serviceConfig as unknown as Record<string, string>,\n );\n if (!result.error) return result;\n // Fall through to lower tiers on error\n } catch {\n // Fall through\n }\n }\n\n // If user provided a plan cost, use CALC\n if (tracked.planCost !== undefined) {\n const now = new Date();\n const daysInMonth = new Date(\n now.getFullYear(),\n now.getMonth() + 1,\n 0,\n ).getDate();\n const dayOfMonth = now.getDate();\n const projectedSpend = (tracked.planCost / daysInMonth) * dayOfMonth;\n\n return {\n serviceId: tracked.serviceId,\n spend: projectedSpend,\n isEstimate: true,\n tier: \"calc\",\n };\n }\n\n // If service is in registry but we have no key and no plan cost\n if (definition) {\n let tier: ConfidenceTier;\n if (tracked.tierOverride) {\n tier = tracked.tierOverride;\n } else if (definition.apiTier === \"live\") {\n // Has a LIVE API but we don't have the key — mark as BLIND\n tier = \"blind\";\n } else {\n // EST, CALC, or BLIND — use the registry's declared tier\n tier = definition.apiTier;\n }\n\n return {\n serviceId: tracked.serviceId,\n spend: 0,\n isEstimate: tier !== \"live\",\n tier,\n error: tier === \"blind\" ? \"No API key configured\" : undefined,\n };\n }\n\n // Completely unknown service\n return {\n serviceId: tracked.serviceId,\n spend: 0,\n isEstimate: true,\n tier: \"blind\",\n error: \"Unknown service — not in registry\",\n };\n}\n\n/**\n * Poll all tracked services concurrently.\n * Returns results in the same order as input.\n */\nexport async function pollAllServices(\n services: TrackedService[],\n): Promise<BillingResult[]> {\n return Promise.all(services.map(pollService));\n}\n\nexport { type BillingConnector, type BillingResult } from \"./base.js\";\n","/**\n * Confidence tiers for spend tracking.\n *\n * LIVE — Real billing API data\n * CALC — Fixed monthly cost, user-entered\n * EST — Estimated from usage signals + pricing formula\n * BLIND — Detected in project, no tracking configured\n */\nexport type ConfidenceTier = \"live\" | \"calc\" | \"est\" | \"blind\" | \"excluded\";\n\nexport const CONFIDENCE_BADGES: Record<ConfidenceTier, string> = {\n live: \"✅ LIVE\",\n calc: \"🟡 CALC\",\n est: \"🟠 EST\",\n blind: \"🔴 BLIND\",\n excluded: \"⬚ SKIP\",\n};\n\n/** How a service charges — determines tracking strategy. */\nexport type BillingModel =\n | \"token_usage\" // Per-token (Anthropic, OpenAI, Gemini)\n | \"credit_pool\" // Fixed credit bucket (Scrapfly)\n | \"per_unit\" // Per-email, per-session, per-command (Resend, Browserbase, Upstash)\n | \"percentage\" // Percentage of transaction (Stripe)\n | \"flat_monthly\" // Fixed monthly subscription (PostHog, Inngest free tier)\n | \"tiered\" // Free up to X, then jumps (PostHog, Supabase)\n | \"compute\" // Compute-time based (Vercel, AWS)\n | \"unknown\";\n\n/** How cost scales — helps the agent reason about future spend. */\nexport type ScalingShape =\n | \"linear\" // Each unit costs the same\n | \"linear_burndown\" // Fixed pool, each use depletes it\n | \"tiered_jump\" // Free until threshold, then expensive\n | \"percentage\" // Proportional to revenue/volume\n | \"fixed\" // Flat monthly, no scaling\n | \"unknown\";\n\n/** A plan tier option for a service in the registry. */\nexport interface PlanTier {\n /** Human-readable plan name */\n name: string;\n /** Plan type: usage (pay-as-you-go), flat (fixed monthly), exclude (don't track) */\n type: \"usage\" | \"flat\" | \"exclude\";\n /** Monthly base cost for flat plans */\n monthlyBase?: number;\n /** Whether this plan requires an API key for tracking */\n requiresKey?: boolean;\n /** Whether this is the default/most common plan */\n default?: boolean;\n}\n\n/** Risk category for service grouping in interactive init. */\nexport type ServiceRiskCategory = \"llm\" | \"usage\" | \"infra\" | \"flat\";\n\n/** A service definition from the registry. */\nexport interface ServiceDefinition {\n /** Unique service identifier */\n id: string;\n /** Human-readable name */\n name: string;\n /** Package names in npm/pip that indicate this service */\n packageNames: string[];\n /** Env var patterns that indicate this service */\n envPatterns: string[];\n /** Import patterns to scan for (regex strings) */\n importPatterns: string[];\n /** Keywords that indicate mentions in prompts */\n mentionKeywords: string[];\n /** Billing model */\n billingModel: BillingModel;\n /** How cost scales */\n scalingShape: ScalingShape;\n /** What tier of tracking is available */\n apiTier: ConfidenceTier;\n /** Billing API endpoint, if available */\n apiEndpoint?: string;\n /** Pricing details */\n pricing?: {\n /** Human-readable formula */\n formula?: string;\n /** Rate per unit, if applicable */\n unitRate?: number;\n /** Unit name (token, credit, email, session, etc.) */\n unitName?: string;\n /** Monthly base cost, if flat */\n monthlyBase?: number;\n };\n /** Known gotchas that affect cost */\n gotchas?: string[];\n /** Alternative services (free or cheaper) */\n alternatives?: string[];\n /** Documentation URL */\n docsUrl?: string;\n /** Last time pricing was verified */\n lastVerified?: string;\n /** Notes about recent pricing changes */\n pricingNotes?: string;\n /** Available plan tiers for interactive init */\n plans?: PlanTier[];\n /** Whether the plan can be auto-detected from an API key */\n autoDetectPlan?: boolean;\n}\n\n/** A tracked service instance — a service definition + user config. */\nexport interface TrackedService {\n /** Service definition ID */\n serviceId: string;\n /** How this service was detected */\n detectedVia: DetectionSource[];\n /** User-configured monthly budget */\n budget?: number;\n /** Whether the user has provided an API/billing key */\n hasApiKey: boolean;\n /** Override confidence tier (e.g., user provided billing key upgrades to LIVE) */\n tierOverride?: ConfidenceTier;\n /** User-entered monthly plan cost (for CALC tier) */\n planCost?: number;\n /** When this service was first detected */\n firstDetected: string;\n /** Explicitly excluded from tracking by user */\n excluded?: boolean;\n /** Plan name selected during interactive init */\n planName?: string;\n}\n\nexport type DetectionSource =\n | \"package_json\"\n | \"env_var\"\n | \"import_scan\"\n | \"prompt_mention\"\n | \"git_diff\"\n | \"manual\";\n\n/** A spend snapshot for a single service at a point in time. */\nexport interface SpendSnapshot {\n serviceId: string;\n /** Current period spend (or estimate) */\n spend: number;\n /** Is the spend figure exact or estimated? */\n isEstimate: boolean;\n /** Confidence tier for this reading */\n tier: ConfidenceTier;\n /** Budget allocated */\n budget?: number;\n /** Percentage of budget consumed */\n budgetPercent?: number;\n /** Budget status */\n status: \"healthy\" | \"caution\" | \"over\" | \"unknown\";\n /** Human-readable status label */\n statusLabel: string;\n /** Raw data from billing API, if available */\n raw?: Record<string, unknown>;\n /** Timestamp of this snapshot */\n timestamp: string;\n}\n\n/** The full spend brief, injected at session start. */\nexport interface SpendBrief {\n projectName: string;\n generatedAt: string;\n period: string;\n services: SpendSnapshot[];\n totalSpend: number;\n totalIsEstimate: boolean;\n estimateMargin: number;\n untrackedCount: number;\n alerts: SpendAlert[];\n}\n\nexport interface SpendAlert {\n serviceId: string;\n type: \"over_budget\" | \"near_budget\" | \"new_service\" | \"stale_data\" | \"blind_service\";\n message: string;\n severity: \"warning\" | \"critical\" | \"info\";\n}\n\n/** Ledger entry — one row in spend-ledger.md */\nexport interface LedgerEntry {\n serviceId: string;\n serviceName: string;\n spend: number;\n isEstimate: boolean;\n tier: ConfidenceTier;\n budget?: number;\n statusLabel: string;\n}\n\n/** Event logged to events.jsonl */\nexport interface SpendEvent {\n timestamp: string;\n sessionId: string;\n type:\n | \"session_start\"\n | \"session_end\"\n | \"service_detected\"\n | \"service_mentioned\"\n | \"spend_polled\"\n | \"budget_alert\"\n | \"ledger_written\"\n | \"cost_impact\";\n data: Record<string, unknown>;\n}\n\n/** A cost impact estimate for a file change. */\nexport interface CostImpact {\n serviceId: string;\n serviceName: string;\n filePath: string;\n /** Number of SDK call sites found */\n callCount: number;\n /** Detected multipliers (loops, .map(), etc.) */\n multipliers: string[];\n /** Effective multiplier applied to call count */\n multiplierFactor: number;\n /** Estimated monthly invocations */\n monthlyInvocations: number;\n /** Low estimate monthly cost */\n costLow: number;\n /** High estimate monthly cost */\n costHigh: number;\n /** Gotcha-based cost range explanation */\n rangeExplanation?: string;\n}\n\n/**\n * Hook input — the JSON received via stdin from Claude Code.\n * Subset of fields we care about.\n */\nexport interface HookInput {\n session_id: string;\n transcript_path?: string;\n cwd: string;\n hook_event_name: string;\n // SessionStart\n source?: string;\n // UserPromptSubmit\n prompt?: string;\n // PostToolUse\n tool_name?: string;\n tool_input?: {\n file_path?: string;\n command?: string;\n content?: string;\n old_string?: string;\n new_string?: string;\n };\n}\n\n/**\n * Hook output — the JSON we write to stdout for Claude Code.\n */\nexport interface HookOutput {\n hookSpecificOutput?: {\n hookEventName: string;\n additionalContext?: string;\n };\n}\n","import type {\n SpendBrief,\n SpendSnapshot,\n SpendAlert,\n ConfidenceTier,\n} from \"./types.js\";\nimport { CONFIDENCE_BADGES } from \"./types.js\";\n\n/**\n * Format a spend brief as a text block for injection into Claude's context.\n */\nexport function formatBrief(brief: SpendBrief): string {\n const lines: string[] = [];\n const width = 62;\n const hrDouble = \"═\".repeat(width);\n const hrSingle = \"─\".repeat(width - 4);\n\n lines.push(`╔${hrDouble}╗`);\n lines.push(\n `║ BURNWATCH — ${brief.projectName} — ${brief.period}`.padEnd(\n width + 1,\n ) + \"║\",\n );\n lines.push(`╠${hrDouble}╣`);\n\n // Header\n lines.push(\n formatRow(\"Service\", \"Spend\", \"Conf\", \"Budget\", \"Left\", width),\n );\n lines.push(`║ ${hrSingle} ║`);\n\n // Service rows\n for (const svc of brief.services) {\n const spendStr = svc.isEstimate\n ? `~$${svc.spend.toFixed(2)}`\n : `$${svc.spend.toFixed(2)}`;\n const badge = CONFIDENCE_BADGES[svc.tier];\n const budgetStr = svc.budget ? `$${svc.budget}` : \"—\";\n const leftStr = formatLeft(svc);\n\n lines.push(formatRow(svc.serviceId, spendStr, badge, budgetStr, leftStr, width));\n }\n\n // Footer\n lines.push(`╠${hrDouble}╣`);\n const totalStr = brief.totalIsEstimate\n ? `~$${brief.totalSpend.toFixed(2)}`\n : `$${brief.totalSpend.toFixed(2)}`;\n const marginStr = brief.estimateMargin > 0\n ? ` Est margin: ±$${brief.estimateMargin.toFixed(0)}`\n : \"\";\n const untrackedStr =\n brief.untrackedCount > 0\n ? `Untracked: ${brief.untrackedCount} ⚠️`\n : `Untracked: 0 ✅`;\n\n lines.push(\n `║ TOTAL: ${totalStr} ${untrackedStr}${marginStr}`.padEnd(\n width + 1,\n ) + \"║\",\n );\n\n // Alerts\n for (const alert of brief.alerts) {\n const icon = alert.severity === \"critical\" ? \"🚨\" : \"⚠️\";\n lines.push(\n `║ ${icon} ${alert.message}`.padEnd(width + 1) + \"║\",\n );\n }\n\n lines.push(`╚${hrDouble}╝`);\n\n return lines.join(\"\\n\");\n}\n\n/**\n * Format a single-service spend card for injection on mention.\n */\nexport function formatSpendCard(snapshot: SpendSnapshot): string {\n const badge = CONFIDENCE_BADGES[snapshot.tier];\n const spendStr = snapshot.isEstimate\n ? `~$${snapshot.spend.toFixed(2)}`\n : `$${snapshot.spend.toFixed(2)}`;\n const budgetStr = snapshot.budget\n ? `Budget: $${snapshot.budget}`\n : \"No budget set\";\n const statusStr = snapshot.statusLabel;\n\n const lines = [\n `[BURNWATCH] ${snapshot.serviceId} — current period`,\n ` Spend: ${spendStr} | ${budgetStr} | ${statusStr}`,\n ` Confidence: ${badge}`,\n ];\n\n if (snapshot.status === \"over\" && snapshot.budgetPercent) {\n lines.push(\n ` ⚠️ ${snapshot.budgetPercent.toFixed(0)}% of budget consumed`,\n );\n }\n\n return lines.join(\"\\n\");\n}\n\n/**\n * Build a SpendBrief from snapshots and project config.\n */\nexport function buildBrief(\n projectName: string,\n snapshots: SpendSnapshot[],\n blindCount: number,\n): SpendBrief {\n const now = new Date();\n const period = now.toLocaleDateString(\"en-US\", {\n month: \"long\",\n year: \"numeric\",\n });\n\n let totalSpend = 0;\n let hasEstimates = false;\n let estimateMargin = 0;\n const alerts: SpendAlert[] = [];\n\n for (const snap of snapshots) {\n totalSpend += snap.spend;\n if (snap.isEstimate) {\n hasEstimates = true;\n estimateMargin += snap.spend * 0.15; // ±15% margin on estimates\n }\n\n if (snap.status === \"over\") {\n alerts.push({\n serviceId: snap.serviceId,\n type: \"over_budget\",\n message: `${snap.serviceId.toUpperCase()} ${snap.budgetPercent?.toFixed(0) ?? \"?\"}% OVER BUDGET — review before use`,\n severity: \"critical\",\n });\n } else if (snap.status === \"caution\" && snap.budgetPercent && snap.budgetPercent >= 80) {\n alerts.push({\n serviceId: snap.serviceId,\n type: \"near_budget\",\n message: `${snap.serviceId} at ${snap.budgetPercent.toFixed(0)}% of budget`,\n severity: \"warning\",\n });\n }\n }\n\n if (blindCount > 0) {\n alerts.push({\n serviceId: \"_blind\",\n type: \"blind_service\",\n message: `${blindCount} service${blindCount > 1 ? \"s\" : \"\"} detected but untracked - run 'burnwatch init' to configure`,\n severity: \"warning\",\n });\n }\n\n return {\n projectName,\n generatedAt: now.toISOString(),\n period,\n services: snapshots,\n totalSpend,\n totalIsEstimate: hasEstimates,\n estimateMargin,\n untrackedCount: blindCount,\n alerts,\n };\n}\n\n// --- Helpers ---\n\nfunction formatRow(\n service: string,\n spend: string,\n conf: string,\n budget: string,\n left: string,\n width: number,\n): string {\n const row = ` ${service.padEnd(14)} ${spend.padEnd(11)} ${conf.padEnd(7)} ${budget.padEnd(7)} ${left}`;\n return `║${row}`.padEnd(width + 1) + \"║\";\n}\n\nfunction formatLeft(snap: SpendSnapshot): string {\n if (!snap.budget) return \"—\";\n if (snap.status === \"over\") return \"⚠️ OVR\";\n if (snap.budgetPercent !== undefined) {\n const remaining = 100 - snap.budgetPercent;\n return `${remaining.toFixed(0)}%`;\n }\n return \"—\";\n}\n\n/**\n * Build a SpendSnapshot from tracked service data.\n */\nexport function buildSnapshot(\n serviceId: string,\n tier: ConfidenceTier,\n spend: number,\n budget?: number,\n): SpendSnapshot {\n const isEstimate = tier === \"est\" || tier === \"calc\";\n const budgetPercent = budget ? (spend / budget) * 100 : undefined;\n\n let status: SpendSnapshot[\"status\"] = \"unknown\";\n let statusLabel = \"no budget\";\n\n if (budget) {\n if (budgetPercent! > 100) {\n status = \"over\";\n statusLabel = `⚠️ ${budgetPercent!.toFixed(0)}% over`;\n } else if (budgetPercent! >= 75) {\n status = \"caution\";\n statusLabel = `${(100 - budgetPercent!).toFixed(0)}% — caution`;\n } else {\n status = \"healthy\";\n statusLabel = `${(100 - budgetPercent!).toFixed(0)}% — healthy`;\n }\n }\n\n if (tier === \"calc\" && budget) {\n statusLabel = `flat — on plan`;\n status = \"healthy\";\n }\n\n return {\n serviceId,\n spend,\n isEstimate,\n tier,\n budget,\n budgetPercent,\n status,\n statusLabel,\n timestamp: new Date().toISOString(),\n };\n}\n","import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport type { SpendBrief, SpendEvent } from \"./types.js\";\nimport { CONFIDENCE_BADGES } from \"./types.js\";\nimport { projectConfigDir, projectDataDir } from \"./config.js\";\n\n/**\n * Write the spend ledger as a human-readable markdown file.\n * Designed to be git-committable and readable in 10 seconds.\n */\nexport function writeLedger(brief: SpendBrief, projectRoot?: string): void {\n const now = new Date();\n const lines: string[] = [];\n\n lines.push(`# Burnwatch Ledger — ${brief.projectName}`);\n lines.push(`Last updated: ${now.toISOString()}`);\n lines.push(\"\");\n lines.push(`## This Month (${brief.period})`);\n lines.push(\"\");\n lines.push(\"| Service | Spend | Conf | Budget | Status |\");\n lines.push(\"|---------|-------|------|--------|--------|\");\n\n for (const svc of brief.services) {\n const spendStr = svc.isEstimate\n ? `~$${svc.spend.toFixed(2)}`\n : `$${svc.spend.toFixed(2)}`;\n const badge = CONFIDENCE_BADGES[svc.tier];\n const budgetStr = svc.budget ? `$${svc.budget}` : \"—\";\n\n lines.push(\n `| ${svc.serviceId} | ${spendStr} | ${badge} | ${budgetStr} | ${svc.statusLabel} |`,\n );\n }\n\n // Add projected impact row if session impacts exist in alerts\n const impactAlert = brief.alerts.find(\n (a) => a.serviceId === \"_session_impact\",\n );\n if (impactAlert) {\n lines.push(\n `| _projected impact_ | — | 📈 EST | — | ${impactAlert.message} |`,\n );\n }\n\n lines.push(\"\");\n const totalStr = brief.totalIsEstimate\n ? `~$${brief.totalSpend.toFixed(2)}`\n : `$${brief.totalSpend.toFixed(2)}`;\n const marginStr =\n brief.estimateMargin > 0\n ? ` (±$${brief.estimateMargin.toFixed(0)} estimated margin)`\n : \"\";\n lines.push(`## TOTAL: ${totalStr}${marginStr}`);\n lines.push(`## Untracked services: ${brief.untrackedCount}`);\n lines.push(\"\");\n\n if (brief.alerts.length > 0) {\n lines.push(\"## Alerts\");\n for (const alert of brief.alerts) {\n const icon = alert.severity === \"critical\" ? \"🚨\" : \"⚠️\";\n lines.push(`- ${icon} ${alert.message}`);\n }\n lines.push(\"\");\n }\n\n const ledgerPath = path.join(\n projectConfigDir(projectRoot),\n \"spend-ledger.md\",\n );\n fs.mkdirSync(path.dirname(ledgerPath), { recursive: true });\n fs.writeFileSync(ledgerPath, lines.join(\"\\n\") + \"\\n\", \"utf-8\");\n}\n\n/**\n * Append an event to the append-only event log.\n */\nexport function logEvent(event: SpendEvent, projectRoot?: string): void {\n const logPath = path.join(projectDataDir(projectRoot), \"events.jsonl\");\n fs.mkdirSync(path.dirname(logPath), { recursive: true });\n fs.appendFileSync(logPath, JSON.stringify(event) + \"\\n\", \"utf-8\");\n}\n\n/**\n * Read recent events from the event log.\n */\nexport function readRecentEvents(\n count: number,\n projectRoot?: string,\n): SpendEvent[] {\n const logPath = path.join(projectDataDir(projectRoot), \"events.jsonl\");\n try {\n const raw = fs.readFileSync(logPath, \"utf-8\");\n const lines = raw.trim().split(\"\\n\").filter(Boolean);\n return lines\n .slice(-count)\n .map((line) => JSON.parse(line) as SpendEvent);\n } catch {\n return [];\n }\n}\n\n/**\n * Save a spend snapshot to the snapshots directory.\n * Used for delta computation across sessions.\n */\nexport function saveSnapshot(brief: SpendBrief, projectRoot?: string): void {\n const snapshotDir = path.join(projectDataDir(projectRoot), \"snapshots\");\n fs.mkdirSync(snapshotDir, { recursive: true });\n const filename = `snapshot-${new Date().toISOString().replace(/[:.]/g, \"-\")}.json`;\n fs.writeFileSync(\n path.join(snapshotDir, filename),\n JSON.stringify(brief, null, 2) + \"\\n\",\n \"utf-8\",\n );\n}\n\n/**\n * Read the most recent snapshot, if any.\n */\nexport function readLatestSnapshot(\n projectRoot?: string,\n): SpendBrief | null {\n const snapshotDir = path.join(projectDataDir(projectRoot), \"snapshots\");\n try {\n const files = fs\n .readdirSync(snapshotDir)\n .filter((f) => f.startsWith(\"snapshot-\") && f.endsWith(\".json\"))\n .sort()\n .reverse();\n\n if (files.length === 0) return null;\n\n const raw = fs.readFileSync(\n path.join(snapshotDir, files[0]!),\n \"utf-8\",\n );\n return JSON.parse(raw) as SpendBrief;\n } catch {\n return null;\n }\n}\n","/**\n * Interactive init flow for burnwatch.\n *\n * Groups detected services by risk category, presents plan tiers,\n * and collects user choices via Node readline.\n */\n\nimport * as readline from \"node:readline\";\nimport type {\n ServiceDefinition,\n PlanTier,\n TrackedService,\n ServiceRiskCategory,\n} from \"./core/types.js\";\nimport type { DetectionResult } from \"./detection/detector.js\";\nimport { readGlobalConfig, writeGlobalConfig } from \"./core/config.js\";\nimport { fetchJson } from \"./services/base.js\";\n\n/** Risk categories in display order: LLMs first, then usage-based, infra, flat-rate */\nconst RISK_ORDER: ServiceRiskCategory[] = [\"llm\", \"usage\", \"infra\", \"flat\"];\n\nconst RISK_LABELS: Record<ServiceRiskCategory, string> = {\n llm: \"🤖 LLM / AI Services (highest variable cost)\",\n usage: \"📊 Usage-Based Services\",\n infra: \"🏗️ Infrastructure & Compute\",\n flat: \"📦 Flat-Rate / Free Tier Services\",\n};\n\n/** Map service IDs to risk categories */\nfunction classifyRisk(service: ServiceDefinition): ServiceRiskCategory {\n if (service.billingModel === \"token_usage\") return \"llm\";\n if (\n service.billingModel === \"credit_pool\" ||\n service.billingModel === \"percentage\" ||\n service.billingModel === \"per_unit\"\n )\n return \"usage\";\n if (service.billingModel === \"compute\") return \"infra\";\n return \"flat\";\n}\n\n/** Group detection results by risk category */\nfunction groupByRisk(\n detected: DetectionResult[],\n): Map<ServiceRiskCategory, DetectionResult[]> {\n const groups = new Map<ServiceRiskCategory, DetectionResult[]>();\n for (const cat of RISK_ORDER) {\n groups.set(cat, []);\n }\n\n for (const det of detected) {\n const cat = classifyRisk(det.service);\n groups.get(cat)!.push(det);\n }\n\n return groups;\n}\n\n/** Prompt the user with a question and return their answer */\nfunction ask(rl: readline.Interface, question: string): Promise<string> {\n return new Promise((resolve) => {\n rl.question(question, (answer) => {\n resolve(answer.trim());\n });\n });\n}\n\n/** Try to auto-detect plan from Scrapfly API */\nasync function autoDetectScrapflyPlan(\n apiKey: string,\n): Promise<string | null> {\n try {\n const result = await fetchJson<{\n subscription?: { plan?: { name?: string } };\n }>(`https://api.scrapfly.io/account?key=${apiKey}`);\n\n if (result.ok && result.data?.subscription?.plan?.name) {\n return result.data.subscription.plan.name;\n }\n } catch {\n // Ignore errors\n }\n return null;\n}\n\nexport interface InteractiveInitResult {\n services: Record<string, TrackedService>;\n}\n\n/**\n * Run the interactive init flow.\n * Shows detected services grouped by risk, lets user pick plans.\n */\nexport async function runInteractiveInit(\n detected: DetectionResult[],\n): Promise<InteractiveInitResult> {\n const rl = readline.createInterface({\n input: process.stdin,\n output: process.stdout,\n });\n\n const services: Record<string, TrackedService> = {};\n const groups = groupByRisk(detected);\n const globalConfig = readGlobalConfig();\n\n console.log(\n \"\\n📋 Let's configure each detected service. Services are grouped by cost risk.\\n\",\n );\n\n for (const category of RISK_ORDER) {\n const group = groups.get(category)!;\n if (group.length === 0) continue;\n\n console.log(`\\n${RISK_LABELS[category]}`);\n console.log(\"─\".repeat(50));\n\n for (const det of group) {\n const service = det.service;\n const plans = service.plans;\n\n console.log(`\\n ${service.name}`);\n console.log(` Detected via: ${det.details.join(\", \")}`);\n\n if (!plans || plans.length === 0) {\n // No plans defined — fall back to basic tracking\n services[service.id] = {\n serviceId: service.id,\n detectedVia: det.sources,\n hasApiKey: false,\n firstDetected: new Date().toISOString(),\n };\n console.log(\" → Auto-configured (no plan tiers available)\");\n continue;\n }\n\n // Show plan options\n const defaultIndex = plans.findIndex((p) => p.default);\n console.log(\"\");\n for (let i = 0; i < plans.length; i++) {\n const plan = plans[i]!;\n const marker = i === defaultIndex ? \" (recommended)\" : \"\";\n const costStr =\n plan.type === \"exclude\"\n ? \"\"\n : plan.monthlyBase !== undefined\n ? ` — $${plan.monthlyBase}/mo`\n : \" — variable\";\n console.log(` ${i + 1}) ${plan.name}${costStr}${marker}`);\n }\n\n const defaultChoice =\n defaultIndex >= 0 ? String(defaultIndex + 1) : \"1\";\n const answer = await ask(\n rl,\n ` Choose [${defaultChoice}]: `,\n );\n\n const choiceIndex = (answer === \"\" ? parseInt(defaultChoice) : parseInt(answer)) - 1;\n const chosen =\n plans[choiceIndex] ?? plans[defaultIndex >= 0 ? defaultIndex : 0]!;\n\n if (chosen.type === \"exclude\") {\n // Explicitly excluded\n services[service.id] = {\n serviceId: service.id,\n detectedVia: det.sources,\n hasApiKey: false,\n firstDetected: new Date().toISOString(),\n excluded: true,\n planName: chosen.name,\n };\n console.log(` → ${service.name}: excluded from tracking`);\n continue;\n }\n\n const tracked: TrackedService = {\n serviceId: service.id,\n detectedVia: det.sources,\n hasApiKey: false,\n firstDetected: new Date().toISOString(),\n planName: chosen.name,\n };\n\n if (chosen.type === \"flat\" && chosen.monthlyBase !== undefined) {\n tracked.planCost = chosen.monthlyBase;\n // Auto-set budget to plan cost for paid flat plans\n if (chosen.monthlyBase > 0) {\n tracked.budget = chosen.monthlyBase;\n }\n }\n\n // If the service has a billing API, offer to provide a key\n if (service.apiTier === \"live\" || chosen.requiresKey) {\n // Check if we already have a key in global config\n const existingKey = globalConfig.services[service.id]?.apiKey;\n if (existingKey) {\n console.log(` 🔐 Using existing API key from global config`);\n tracked.hasApiKey = true;\n\n // Auto-detect plan for Scrapfly\n if (service.autoDetectPlan && service.id === \"scrapfly\") {\n console.log(\" 🔍 Auto-detecting plan from API...\");\n const planName = await autoDetectScrapflyPlan(existingKey);\n if (planName) {\n console.log(` → Detected plan: ${planName}`);\n tracked.planName = planName;\n }\n }\n } else if (chosen.requiresKey) {\n const keyAnswer = await ask(\n rl,\n ` Enter API key (or press Enter to skip): `,\n );\n if (keyAnswer) {\n tracked.hasApiKey = true;\n if (!globalConfig.services[service.id]) {\n globalConfig.services[service.id] = {};\n }\n globalConfig.services[service.id]!.apiKey = keyAnswer;\n\n // Auto-detect plan for Scrapfly\n if (service.autoDetectPlan && service.id === \"scrapfly\") {\n console.log(\" 🔍 Auto-detecting plan from API...\");\n const planName = await autoDetectScrapflyPlan(keyAnswer);\n if (planName) {\n console.log(` → Detected plan: ${planName}`);\n tracked.planName = planName;\n }\n }\n }\n }\n }\n\n // Always ask for budget if not already set to a meaningful value\n if (tracked.budget === undefined || tracked.budget === 0) {\n const suggestion = chosen.monthlyBase && chosen.monthlyBase > 0\n ? ` [${chosen.monthlyBase}]`\n : \"\";\n const budgetAnswer = await ask(\n rl,\n ` Monthly budget in USD${suggestion} (or press Enter to skip): $`,\n );\n if (budgetAnswer) {\n const budget = parseFloat(budgetAnswer);\n if (!isNaN(budget)) {\n tracked.budget = budget;\n }\n }\n }\n\n services[service.id] = tracked;\n\n const tierLabel = tracked.hasApiKey\n ? \"✅ LIVE\"\n : tracked.planCost !== undefined\n ? \"🟡 CALC\"\n : \"🔴 BLIND\";\n const budgetStr = tracked.budget !== undefined ? ` | Budget: $${tracked.budget}/mo` : \"\";\n console.log(\n ` → ${service.name}: ${chosen.name} (${tierLabel}${budgetStr})`,\n );\n }\n }\n\n // Save any collected API keys\n writeGlobalConfig(globalConfig);\n\n rl.close();\n\n return { services };\n}\n"],"mappings":";;;AAYA,YAAYA,SAAQ;AACpB,YAAYC,WAAU;;;ACbtB,YAAY,QAAQ;AACpB,YAAY,UAAU;AACtB,YAAY,QAAQ;AAab,SAAS,kBAA0B;AACxC,QAAM,YAAY,QAAQ,IAAI,iBAAiB;AAC/C,MAAI,UAAW,QAAY,UAAK,WAAW,WAAW;AACtD,SAAY,UAAQ,WAAQ,GAAG,WAAW,WAAW;AACvD;AAGO,SAAS,iBAAiB,aAA8B;AAC7D,QAAM,OAAO,eAAe,QAAQ,IAAI;AACxC,SAAY,UAAK,MAAM,YAAY;AACrC;AAGO,SAAS,eAAe,aAA8B;AAC3D,SAAY,UAAK,iBAAiB,WAAW,GAAG,MAAM;AACxD;AAeO,SAAS,mBAAiC;AAC/C,QAAM,aAAkB,UAAK,gBAA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,eAAe;AAAA,IACjE,SAAS;AAAA,IACT,OAAO;AAAA,MACL;AAAA,QACE,MAAM;AAAA,QACN,SAAS,SAAc,WAAK,UAAU,mBAAmB,CAAC;AAAA,QAC1D,SAAS;AAAA,MACX;AAAA,IACF;AAAA,EACF,CAAC;AAGD,MAAI,CAAC,MAAM,MAAM,EAAG,OAAM,MAAM,IAAI,CAAC;AACrC,mBAAiB,MAAM,MAAM,GAAgB,QAAQ;AAAA,IACnD,OAAO;AAAA,MACL;AAAA,QACE,MAAM;AAAA,QACN,SAAS,SAAc,WAAK,UAAU,YAAY,CAAC;AAAA,QACnD,SAAS;AAAA,QACT,OAAO;AAAA,MACT;AAAA,IACF;AAAA,EACF,CAAC;AAED,WAAS,OAAO,IAAI;AACpB,EAAG,kBAAc,cAAc,KAAK,UAAU,UAAU,MAAM,CAAC,IAAI,MAAM,OAAO;AAChF,UAAQ,IAAI,0BAA0B,YAAY,EAAE;AACtD;AAEA,SAAS,iBACP,WACA,YACA,YACM;AAEN,QAAM,WAAW,UAAU,KAAK,CAAC,MAAM;AACrC,UAAM,OAAO;AACb,WAAO,KAAK,OAAO,KAAK,CAAC,UAAU,MAAM,SAAS,SAAS,WAAW,CAAC;AAAA,EACzE,CAAC;AAED,MAAI,CAAC,UAAU;AACb,cAAU,KAAK,UAAU;AAAA,EAC3B;AACF;AAIA,KAAK,EAAE,MAAM,CAAC,QAAQ;AACpB,UAAQ,MAAM,UAAU,eAAe,QAAQ,IAAI,UAAU,GAAG;AAChE,UAAQ,KAAK,CAAC;AAChB,CAAC;","names":["fs","path","fs","path","fs","path","url","url","url","url","url","fs","path","resolve","tracked"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "burnwatch",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "Passive cost memory for vibe coding — detects paid services, tracks spend, injects budget context into your AI coding sessions.",
5
5
  "type": "module",
6
6
  "bin": {