This commit is contained in:
2026-07-10 18:42:30 +08:00
parent 2df5e871dc
commit a3b48f56c9
7 changed files with 55 additions and 27 deletions

View File

@@ -2,7 +2,6 @@ package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
@@ -13,7 +12,7 @@ type Config struct {
APIKey string `json:"api_key"`
Model string `json:"model"`
BaseURL string `json:"base_url"`
Provider string `json:"provider"` // "agnes" or "zhipu" or "custom"
Provider string `json:"provider"` // "agnes"
}
// Provider presets
@@ -25,18 +24,18 @@ var providerPresets = map[string]struct {
Model: "agnes-2.0-flash",
BaseURL: "https://apihub.agnes-ai.com/v1/chat/completions",
},
"zhipu": {
Model: "glm-4.6v-flash",
BaseURL: "https://open.bigmodel.cn/api/paas/v4/chat/completions",
},
}
// Hardcoded API key
const hardcodedAPIKey = "sk-68FL9xXRrJhxcY5TCGxoaFlQ94oCBioIJhyGfeDMCkCvA0SV"
// DefaultConfig returns a Config with sensible defaults (Agnes AI).
func DefaultConfig() *Config {
return &Config{
Provider: "agnes",
Model: "agnes-2.0-flash",
BaseURL: "https://apihub.agnes-ai.com/v1/chat/completions",
APIKey: hardcodedAPIKey,
}
}
@@ -44,26 +43,24 @@ func DefaultConfig() *Config {
func (c *Config) ApplyProvider(provider string) {
if preset, ok := providerPresets[provider]; ok {
c.Provider = provider
// Only override model/baseURL if user hasn't set custom ones
if c.Model == "" || c.Model == "agnes-2.0-flash" || c.Model == "glm-4.6v-flash" {
if c.Model == "" || c.Model == "agnes-2.0-flash" {
c.Model = preset.Model
}
if c.BaseURL == "" || c.BaseURL == "https://apihub.agnes-ai.com/v1/chat/completions" ||
c.BaseURL == "https://open.bigmodel.cn/api/paas/v4/chat/completions" {
if c.BaseURL == "" || c.BaseURL == "https://apihub.agnes-ai.com/v1/chat/completions" {
c.BaseURL = preset.BaseURL
}
}
}
// LoadConfig loads configuration from multiple sources in priority order:
// 1. Command-line --apikey flag (highest)
// 2. ZHIPU_API_KEY environment variable
// 3. config.json in the same directory as the executable
// 4. Default built-in key (lowest)
// 1. Command-line --apikey flag (highest)
// 2. AGNES_API_KEY environment variable
// 3. config.json in the same directory as the executable
// 4. Hardcoded default key (lowest)
func LoadConfig(flagKey, configPath string) (*Config, error) {
cfg := DefaultConfig()
// Load from config file if it exists (lowest priority)
// Load from config file if it exists
if configPath == "" {
execDir := getExecDir()
configPath = filepath.Join(execDir, "config.json")
@@ -88,22 +85,13 @@ func LoadConfig(flagKey, configPath string) (*Config, error) {
if envKey := os.Getenv("AGNES_API_KEY"); envKey != "" {
cfg.APIKey = envKey
}
if envKey := os.Getenv("ZHIPU_API_KEY"); envKey != "" {
cfg.APIKey = envKey
}
// Highest priority: command-line flag
if flagKey != "" {
cfg.APIKey = flagKey
}
if cfg.APIKey == "" {
return cfg, fmt.Errorf("API key not configured. Set it via:\n" +
" 1. --apikey flag\n" +
" 2. AGNES_API_KEY environment variable\n" +
" 3. config.json file in the tools directory")
}
// API key is always available thanks to hardcoded default
return cfg, nil
}