Files
vision-tool/config.go
2026-07-10 18:42:30 +08:00

130 lines
3.0 KiB
Go

package main
import (
"encoding/json"
"os"
"path/filepath"
"strings"
)
// Config holds the application configuration.
type Config struct {
APIKey string `json:"api_key"`
Model string `json:"model"`
BaseURL string `json:"base_url"`
Provider string `json:"provider"` // "agnes"
}
// Provider presets
var providerPresets = map[string]struct {
Model string
BaseURL string
}{
"agnes": {
Model: "agnes-2.0-flash",
BaseURL: "https://apihub.agnes-ai.com/v1/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,
}
}
// ApplyProvider applies a named provider preset, respecting user overrides.
func (c *Config) ApplyProvider(provider string) {
if preset, ok := providerPresets[provider]; ok {
c.Provider = provider
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 = preset.BaseURL
}
}
}
// LoadConfig loads configuration from multiple sources in priority order:
// 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
if configPath == "" {
execDir := getExecDir()
configPath = filepath.Join(execDir, "config.json")
}
if data, err := os.ReadFile(configPath); err == nil {
var fileCfg Config
if err := json.Unmarshal(data, &fileCfg); err == nil {
if fileCfg.APIKey != "" {
cfg.APIKey = fileCfg.APIKey
}
if fileCfg.Model != "" {
cfg.Model = fileCfg.Model
}
if fileCfg.BaseURL != "" {
cfg.BaseURL = fileCfg.BaseURL
}
}
}
// Override with environment variable
if envKey := os.Getenv("AGNES_API_KEY"); envKey != "" {
cfg.APIKey = envKey
}
// Highest priority: command-line flag
if flagKey != "" {
cfg.APIKey = flagKey
}
// API key is always available thanks to hardcoded default
return cfg, nil
}
// SaveConfigFile saves config to a JSON file.
func SaveConfigFile(path, apiKey string) error {
cfg := DefaultConfig()
cfg.APIKey = apiKey
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}
return os.WriteFile(path, data, 0600)
}
func getExecDir() string {
execPath, err := os.Executable()
if err != nil {
return "."
}
return filepath.Dir(execPath)
}
// MaskKey masks the API key for safe display, showing only first 8 and last 4 chars.
func MaskKey(key string) string {
if len(key) <= 12 {
return strings.Repeat("*", len(key))
}
return key[:8] + "..." + key[len(key)-4:]
}