This commit is contained in:
lpf
2026-02-13 13:50:09 +08:00
parent f88a78ef8b
commit 085c265319
15 changed files with 1485 additions and 179 deletions

View File

@@ -16,6 +16,7 @@ type Config struct {
Providers ProvidersConfig `json:"providers"`
Gateway GatewayConfig `json:"gateway"`
Tools ToolsConfig `json:"tools"`
Logging LoggingConfig `json:"logging"`
mu sync.RWMutex
}
@@ -140,6 +141,14 @@ type ToolsConfig struct {
Filesystem FilesystemConfig `json:"filesystem"`
}
type LoggingConfig struct {
Enabled bool `json:"enabled" env:"CLAWGO_LOGGING_ENABLED"`
Dir string `json:"dir" env:"CLAWGO_LOGGING_DIR"`
Filename string `json:"filename" env:"CLAWGO_LOGGING_FILENAME"`
MaxSizeMB int `json:"max_size_mb" env:"CLAWGO_LOGGING_MAX_SIZE_MB"`
RetentionDays int `json:"retention_days" env:"CLAWGO_LOGGING_RETENTION_DAYS"`
}
var (
isDebug bool
muDebug sync.RWMutex
@@ -253,6 +262,13 @@ func DefaultConfig() *Config {
DeniedPaths: []string{"/etc/shadow", "/etc/passwd"},
},
},
Logging: LoggingConfig{
Enabled: true,
Dir: filepath.Join(configDir, "logs"),
Filename: "clawgo.log",
MaxSizeMB: 20,
RetentionDays: 3,
},
}
}
@@ -313,6 +329,18 @@ func (c *Config) GetAPIBase() string {
return c.Providers.Proxy.APIBase
}
func (c *Config) LogFilePath() string {
c.mu.RLock()
defer c.mu.RUnlock()
dir := expandHome(c.Logging.Dir)
filename := c.Logging.Filename
if filename == "" {
filename = "clawgo.log"
}
return filepath.Join(dir, filename)
}
func expandHome(path string) string {
if path == "" {
return path

79
pkg/config/validate.go Normal file
View File

@@ -0,0 +1,79 @@
package config
import "fmt"
// Validate returns configuration problems found in cfg.
// It does not mutate cfg.
func Validate(cfg *Config) []error {
if cfg == nil {
return []error{fmt.Errorf("config is nil")}
}
var errs []error
if cfg.Agents.Defaults.Model == "" {
errs = append(errs, fmt.Errorf("agents.defaults.model is required"))
}
if cfg.Agents.Defaults.MaxToolIterations <= 0 {
errs = append(errs, fmt.Errorf("agents.defaults.max_tool_iterations must be > 0"))
}
if cfg.Providers.Proxy.APIBase == "" {
errs = append(errs, fmt.Errorf("providers.proxy.api_base is required"))
}
if cfg.Gateway.Port <= 0 || cfg.Gateway.Port > 65535 {
errs = append(errs, fmt.Errorf("gateway.port must be in 1..65535"))
}
if cfg.Logging.Enabled {
if cfg.Logging.Dir == "" {
errs = append(errs, fmt.Errorf("logging.dir is required when logging.enabled=true"))
}
if cfg.Logging.Filename == "" {
errs = append(errs, fmt.Errorf("logging.filename is required when logging.enabled=true"))
}
if cfg.Logging.MaxSizeMB <= 0 {
errs = append(errs, fmt.Errorf("logging.max_size_mb must be > 0"))
}
if cfg.Logging.RetentionDays <= 0 {
errs = append(errs, fmt.Errorf("logging.retention_days must be > 0"))
}
}
if cfg.Channels.Telegram.Enabled && cfg.Channels.Telegram.Token == "" {
errs = append(errs, fmt.Errorf("channels.telegram.token is required when channels.telegram.enabled=true"))
}
if cfg.Channels.Discord.Enabled && cfg.Channels.Discord.Token == "" {
errs = append(errs, fmt.Errorf("channels.discord.token is required when channels.discord.enabled=true"))
}
if cfg.Channels.WhatsApp.Enabled && cfg.Channels.WhatsApp.BridgeURL == "" {
errs = append(errs, fmt.Errorf("channels.whatsapp.bridge_url is required when channels.whatsapp.enabled=true"))
}
if cfg.Channels.DingTalk.Enabled {
if cfg.Channels.DingTalk.ClientID == "" {
errs = append(errs, fmt.Errorf("channels.dingtalk.client_id is required when channels.dingtalk.enabled=true"))
}
if cfg.Channels.DingTalk.ClientSecret == "" {
errs = append(errs, fmt.Errorf("channels.dingtalk.client_secret is required when channels.dingtalk.enabled=true"))
}
}
if cfg.Channels.Feishu.Enabled {
if cfg.Channels.Feishu.AppID == "" {
errs = append(errs, fmt.Errorf("channels.feishu.app_id is required when channels.feishu.enabled=true"))
}
if cfg.Channels.Feishu.AppSecret == "" {
errs = append(errs, fmt.Errorf("channels.feishu.app_secret is required when channels.feishu.enabled=true"))
}
}
if cfg.Channels.QQ.Enabled {
if cfg.Channels.QQ.AppID == "" {
errs = append(errs, fmt.Errorf("channels.qq.app_id is required when channels.qq.enabled=true"))
}
if cfg.Channels.QQ.AppSecret == "" {
errs = append(errs, fmt.Errorf("channels.qq.app_secret is required when channels.qq.enabled=true"))
}
}
return errs
}