Files
cburn/internal/config/config.go
teernisse 8984d5062d feat: add configuration system with pricing tables and plan detection
Implement the configuration layer that supports the entire cost
estimation pipeline:

- config/config.go: TOML-based config at ~/.config/cburn/config.toml
  (XDG-compliant) with sections for general preferences, Admin API
  credentials, budget tracking, appearance, and per-model pricing
  overrides. Supports Load/Save with sensible defaults (30-day
  window, subagents included, Flexoki Dark theme). Admin API key
  resolution checks ANTHROPIC_ADMIN_KEY env var first, falling back
  to the config file.

- config/pricing.go: Hardcoded pricing table for 8 Claude model
  variants (Opus 4/4.1/4.5/4.6, Sonnet 4/4.5/4.6, Haiku 3.5/4.5)
  with per-million-token rates across 5 billing dimensions: input,
  output, cache_write_5m, cache_write_1h, cache_read, plus long-
  context overrides (>200K tokens). NormalizeModelName strips date
  suffixes (e.g., "claude-opus-4-5-20251101" -> "claude-opus-4-5").
  CalculateCost and CalculateCacheSavings compute per-call USD costs
  by multiplying token counts against the pricing table.

- config/plan.go: DetectPlan reads ~/.claude/.claude.json to
  determine the billing plan type. Maps "stripe_subscription" to
  the Max plan ($200/mo ceiling), everything else to Pro ($100/mo).
  Used by the budget tab for plan-relative spend visualization.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 13:01:11 -05:00

133 lines
3.3 KiB
Go

package config
import (
"fmt"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
)
// Config holds all cburn configuration.
type Config struct {
General GeneralConfig `toml:"general"`
AdminAPI AdminAPIConfig `toml:"admin_api"`
Budget BudgetConfig `toml:"budget"`
Appearance AppearanceConfig `toml:"appearance"`
Pricing PricingOverrides `toml:"pricing"`
}
// GeneralConfig holds general preferences.
type GeneralConfig struct {
DefaultDays int `toml:"default_days"`
IncludeSubagents bool `toml:"include_subagents"`
ClaudeDir string `toml:"claude_dir,omitempty"`
}
// AdminAPIConfig holds Anthropic Admin API settings.
type AdminAPIConfig struct {
APIKey string `toml:"api_key,omitempty"`
BaseURL string `toml:"base_url,omitempty"`
}
// BudgetConfig holds budget tracking settings.
type BudgetConfig struct {
MonthlyUSD *float64 `toml:"monthly_usd,omitempty"`
}
// AppearanceConfig holds theme settings.
type AppearanceConfig struct {
Theme string `toml:"theme"`
}
// PricingOverrides allows user-defined pricing for specific models.
type PricingOverrides struct {
Overrides map[string]ModelPricingOverride `toml:"overrides,omitempty"`
}
// ModelPricingOverride holds per-model pricing overrides.
type ModelPricingOverride struct {
InputPerMTok *float64 `toml:"input_per_mtok,omitempty"`
OutputPerMTok *float64 `toml:"output_per_mtok,omitempty"`
CacheWrite5mPerMTok *float64 `toml:"cache_write_5m_per_mtok,omitempty"`
CacheWrite1hPerMTok *float64 `toml:"cache_write_1h_per_mtok,omitempty"`
CacheReadPerMTok *float64 `toml:"cache_read_per_mtok,omitempty"`
}
// DefaultConfig returns the default configuration.
func DefaultConfig() Config {
return Config{
General: GeneralConfig{
DefaultDays: 30,
IncludeSubagents: true,
},
Appearance: AppearanceConfig{
Theme: "flexoki-dark",
},
}
}
// ConfigDir returns the XDG-compliant config directory.
func ConfigDir() string {
if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
return filepath.Join(xdg, "cburn")
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".config", "cburn")
}
// ConfigPath returns the full path to the config file.
func ConfigPath() string {
return filepath.Join(ConfigDir(), "config.toml")
}
// Load reads the config file, returning defaults if it doesn't exist.
func Load() (Config, error) {
cfg := DefaultConfig()
data, err := os.ReadFile(ConfigPath())
if err != nil {
if os.IsNotExist(err) {
return cfg, nil
}
return cfg, fmt.Errorf("reading config: %w", err)
}
if err := toml.Unmarshal(data, &cfg); err != nil {
return cfg, fmt.Errorf("parsing config: %w", err)
}
return cfg, nil
}
// Save writes the config to disk.
func Save(cfg Config) error {
dir := ConfigDir()
if err := os.MkdirAll(dir, 0o755); err != nil {
return fmt.Errorf("creating config dir: %w", err)
}
f, err := os.Create(ConfigPath())
if err != nil {
return fmt.Errorf("creating config file: %w", err)
}
defer f.Close()
enc := toml.NewEncoder(f)
return enc.Encode(cfg)
}
// GetAdminAPIKey returns the API key from env var or config, in that order.
func GetAdminAPIKey(cfg Config) string {
if key := os.Getenv("ANTHROPIC_ADMIN_KEY"); key != "" {
return key
}
return cfg.AdminAPI.APIKey
}
// Exists returns true if a config file exists on disk.
func Exists() bool {
_, err := os.Stat(ConfigPath())
return err == nil
}