diff --git a/go.mod b/go.mod index 7892cade6..18d762b25 100644 --- a/go.mod +++ b/go.mod @@ -8,13 +8,16 @@ require ( github.com/bwmarrin/discordgo v0.29.0 github.com/caarlos0/env/v11 v11.3.1 github.com/chzyer/readline v1.5.1 + github.com/gdamore/tcell/v2 v2.13.8 github.com/google/uuid v1.6.0 github.com/gorilla/websocket v1.5.3 + github.com/joho/godotenv v1.5.1 github.com/larksuite/oapi-sdk-go/v3 v3.5.3 github.com/mdp/qrterminal/v3 v3.2.1 github.com/mymmrac/telego v1.6.0 github.com/open-dingtalk/dingtalk-stream-sdk-go v0.9.1 github.com/openai/openai-go/v3 v3.22.0 + github.com/rivo/tview v0.42.0 github.com/slack-go/slack v0.17.3 github.com/spf13/cobra v1.10.2 github.com/stretchr/testify v1.11.1 @@ -34,7 +37,6 @@ require ( github.com/dustin/go-humanize v1.0.1 // indirect github.com/elliotchance/orderedmap/v3 v3.1.0 // indirect github.com/gdamore/encoding v1.0.1 // indirect - github.com/gdamore/tcell/v2 v2.13.8 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/lucasb-eyer/go-colorful v1.3.0 // indirect github.com/mattn/go-colorable v0.1.14 // indirect @@ -43,7 +45,6 @@ require ( github.com/petermattis/goid v0.0.0-20260113132338-7c7de50cc741 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect - github.com/rivo/tview v0.42.0 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/rs/zerolog v1.34.0 // indirect github.com/spf13/pflag v1.0.10 // indirect diff --git a/go.sum b/go.sum index d1ee1d629..00f2b1600 100644 --- a/go.sum +++ b/go.sum @@ -101,6 +101,8 @@ github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyf github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.18.4 h1:RPhnKRAQ4Fh8zU2FY/6ZFDwTVTxgJ/EMydqSTzE9a2c= diff --git a/pkg/config/config.go b/pkg/config/config.go index c4c175495..a2151ccc2 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -3,10 +3,13 @@ package config import ( "encoding/json" "fmt" + "log" "os" + "path/filepath" "sync/atomic" "github.com/caarlos0/env/v11" + "github.com/joho/godotenv" "github.com/sipeed/picoclaw/pkg/fileutil" ) @@ -597,9 +600,28 @@ type ClawHubRegistryConfig struct { func LoadConfig(path string) (*Config, error) { cfg := DefaultConfig() + // Load .env file from config directory (secrets, API keys, etc.) + // This runs before reading config.json so .env works even on fresh installs. + envFile := filepath.Join(filepath.Dir(path), ".env") + if err := godotenv.Load(envFile); err != nil { + if os.IsNotExist(err) { + log.Printf("[INFO] No .env file found at %s; skipping .env loading", envFile) + } else { + log.Printf("[WARN] Failed to load .env file from %s: %v", envFile, err) + } + } + data, err := os.ReadFile(path) if err != nil { if os.IsNotExist(err) { + // No config file — still apply env vars + overrides to default config + if err := env.Parse(cfg); err != nil { + return nil, err + } + loadProviderEnvOverrides(cfg) + if cfg.HasProvidersConfig() { + cfg.ModelList = ConvertProvidersToModelList(cfg) + } return cfg, nil } return nil, err @@ -627,6 +649,9 @@ func LoadConfig(path string) (*Config, error) { return nil, err } + // Load provider-specific env overrides (PICOCLAW_PROVIDERS__API_KEY, etc.) + loadProviderEnvOverrides(cfg) + // Migrate legacy channel config fields to new unified structures cfg.migrateChannelConfigs() @@ -775,3 +800,41 @@ func (c *Config) ValidateModelList() error { } return nil } + +// loadProviderEnvOverrides reads PICOCLAW_PROVIDERS__API_KEY and _API_BASE +// environment variables and sets them on the corresponding provider config fields. +// This enables storing provider secrets in .env files without using struct tags. +func loadProviderEnvOverrides(cfg *Config) { + providers := []struct { + name string + apiKey *string + base *string + }{ + {"ANTHROPIC", &cfg.Providers.Anthropic.APIKey, &cfg.Providers.Anthropic.APIBase}, + {"OPENAI", &cfg.Providers.OpenAI.APIKey, &cfg.Providers.OpenAI.APIBase}, + {"OPENROUTER", &cfg.Providers.OpenRouter.APIKey, &cfg.Providers.OpenRouter.APIBase}, + {"GROQ", &cfg.Providers.Groq.APIKey, &cfg.Providers.Groq.APIBase}, + {"ZHIPU", &cfg.Providers.Zhipu.APIKey, &cfg.Providers.Zhipu.APIBase}, + {"GEMINI", &cfg.Providers.Gemini.APIKey, &cfg.Providers.Gemini.APIBase}, + {"NVIDIA", &cfg.Providers.Nvidia.APIKey, &cfg.Providers.Nvidia.APIBase}, + {"OLLAMA", &cfg.Providers.Ollama.APIKey, &cfg.Providers.Ollama.APIBase}, + {"MOONSHOT", &cfg.Providers.Moonshot.APIKey, &cfg.Providers.Moonshot.APIBase}, + {"SHENGSUANYUN", &cfg.Providers.ShengSuanYun.APIKey, &cfg.Providers.ShengSuanYun.APIBase}, + {"DEEPSEEK", &cfg.Providers.DeepSeek.APIKey, &cfg.Providers.DeepSeek.APIBase}, + {"MISTRAL", &cfg.Providers.Mistral.APIKey, &cfg.Providers.Mistral.APIBase}, + {"VLLM", &cfg.Providers.VLLM.APIKey, &cfg.Providers.VLLM.APIBase}, + {"CEREBRAS", &cfg.Providers.Cerebras.APIKey, &cfg.Providers.Cerebras.APIBase}, + {"VOLCENGINE", &cfg.Providers.VolcEngine.APIKey, &cfg.Providers.VolcEngine.APIBase}, + {"QWEN", &cfg.Providers.Qwen.APIKey, &cfg.Providers.Qwen.APIBase}, + // Note: GitHubCopilot and Antigravity use different auth patterns (ConnectMode/AuthMethod), + // not standard APIKey/APIBase, so they are not included here. + } + for _, p := range providers { + if v := os.Getenv("PICOCLAW_PROVIDERS_" + p.name + "_API_KEY"); v != "" { + *p.apiKey = v + } + if v := os.Getenv("PICOCLAW_PROVIDERS_" + p.name + "_API_BASE"); v != "" { + *p.base = v + } + } +}