mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-04-13 04:27:28 +08:00
369 lines
16 KiB
Go
369 lines
16 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// 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.MaxToolIterations <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.max_tool_iterations must be > 0"))
|
|
}
|
|
rc := cfg.Agents.Defaults.RuntimeControl
|
|
if rc.IntentMaxInputChars < 200 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.intent_max_input_chars must be >= 200"))
|
|
}
|
|
if rc.AutonomyTickIntervalSec < 5 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.autonomy_tick_interval_sec must be >= 5"))
|
|
}
|
|
if rc.AutonomyMinRunIntervalSec < 5 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.autonomy_min_run_interval_sec must be >= 5"))
|
|
}
|
|
if rc.AutonomyIdleThresholdSec < 5 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.autonomy_idle_threshold_sec must be >= 5"))
|
|
}
|
|
if rc.AutonomyMaxRoundsWithoutUser <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.autonomy_max_rounds_without_user must be > 0"))
|
|
}
|
|
if rc.AutonomyMaxPendingDurationSec < 10 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.autonomy_max_pending_duration_sec must be >= 10"))
|
|
}
|
|
if rc.AutonomyMaxConsecutiveStalls <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.autonomy_max_consecutive_stalls must be > 0"))
|
|
}
|
|
if rc.AutoLearnMaxRoundsWithoutUser <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.autolearn_max_rounds_without_user must be > 0"))
|
|
}
|
|
if rc.RunStateTTLSeconds < 60 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.run_state_ttl_seconds must be >= 60"))
|
|
}
|
|
if rc.RunStateMax <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.run_state_max must be > 0"))
|
|
}
|
|
errs = append(errs, validateNonEmptyStringList("agents.defaults.runtime_control.tool_parallel_safe_names", rc.ToolParallelSafeNames)...)
|
|
if rc.ToolMaxParallelCalls <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.tool_max_parallel_calls must be > 0"))
|
|
}
|
|
if strings.TrimSpace(rc.SystemSummary.Marker) == "" {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.system_summary.marker must be non-empty"))
|
|
}
|
|
if strings.TrimSpace(rc.SystemSummary.CompletedPrefix) == "" {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.system_summary.completed_prefix must be non-empty"))
|
|
}
|
|
if strings.TrimSpace(rc.SystemSummary.ChangesPrefix) == "" {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.system_summary.changes_prefix must be non-empty"))
|
|
}
|
|
if strings.TrimSpace(rc.SystemSummary.OutcomePrefix) == "" {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.system_summary.outcome_prefix must be non-empty"))
|
|
}
|
|
if strings.TrimSpace(rc.SystemSummary.CompletedTitle) == "" {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.system_summary.completed_title must be non-empty"))
|
|
}
|
|
if strings.TrimSpace(rc.SystemSummary.ChangesTitle) == "" {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.system_summary.changes_title must be non-empty"))
|
|
}
|
|
if strings.TrimSpace(rc.SystemSummary.OutcomesTitle) == "" {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.runtime_control.system_summary.outcomes_title must be non-empty"))
|
|
}
|
|
hb := cfg.Agents.Defaults.Heartbeat
|
|
if hb.Enabled {
|
|
if hb.EverySec <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.heartbeat.every_sec must be > 0 when enabled=true"))
|
|
}
|
|
if hb.AckMaxChars <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.heartbeat.ack_max_chars must be > 0 when enabled=true"))
|
|
}
|
|
}
|
|
aut := cfg.Agents.Defaults.Autonomy
|
|
if aut.Enabled {
|
|
if aut.TickIntervalSec <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.autonomy.tick_interval_sec must be > 0 when enabled=true"))
|
|
}
|
|
if aut.MinRunIntervalSec <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.autonomy.min_run_interval_sec must be > 0 when enabled=true"))
|
|
}
|
|
if aut.MaxPendingDurationSec <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.autonomy.max_pending_duration_sec must be > 0 when enabled=true"))
|
|
}
|
|
if aut.MaxConsecutiveStalls <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.autonomy.max_consecutive_stalls must be > 0 when enabled=true"))
|
|
}
|
|
if aut.MaxDispatchPerTick <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.autonomy.max_dispatch_per_tick must be > 0 when enabled=true"))
|
|
}
|
|
if aut.NotifyCooldownSec <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.autonomy.notify_cooldown_sec must be > 0 when enabled=true"))
|
|
}
|
|
if aut.NotifySameReasonCooldownSec <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.autonomy.notify_same_reason_cooldown_sec must be > 0 when enabled=true"))
|
|
}
|
|
if qh := strings.TrimSpace(aut.QuietHours); qh != "" {
|
|
parts := strings.Split(qh, "-")
|
|
if len(parts) != 2 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.autonomy.quiet_hours must be HH:MM-HH:MM"))
|
|
}
|
|
}
|
|
if aut.UserIdleResumeSec <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.autonomy.user_idle_resume_sec must be > 0 when enabled=true"))
|
|
}
|
|
if aut.WaitingResumeDebounceSec <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.autonomy.waiting_resume_debounce_sec must be > 0 when enabled=true"))
|
|
}
|
|
if aut.IdleRoundBudgetReleaseSec < 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.autonomy.idle_round_budget_release_sec must be >= 0 when enabled=true"))
|
|
}
|
|
if aut.TaskHistoryRetentionDays <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.autonomy.task_history_retention_days must be > 0 when enabled=true"))
|
|
}
|
|
if aut.EKGConsecutiveErrorThreshold <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.autonomy.ekg_consecutive_error_threshold must be > 0 when enabled=true"))
|
|
}
|
|
}
|
|
texts := cfg.Agents.Defaults.Texts
|
|
if strings.TrimSpace(texts.NoResponseFallback) == "" {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.texts.no_response_fallback must be non-empty"))
|
|
}
|
|
if strings.TrimSpace(texts.ThinkOnlyFallback) == "" {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.texts.think_only_fallback must be non-empty"))
|
|
}
|
|
if len(texts.MemoryRecallKeywords) == 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.texts.memory_recall_keywords must contain at least one keyword"))
|
|
}
|
|
if strings.TrimSpace(texts.LangUpdatedTemplate) != "" && !strings.Contains(texts.LangUpdatedTemplate, "%s") {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.texts.lang_updated_template must contain %%s placeholder"))
|
|
}
|
|
if strings.TrimSpace(texts.RuntimeCompactionNote) != "" {
|
|
if strings.Count(texts.RuntimeCompactionNote, "%d") < 2 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.texts.runtime_compaction_note must contain two %%d placeholders"))
|
|
}
|
|
}
|
|
if strings.TrimSpace(texts.StartupCompactionNote) != "" {
|
|
if strings.Count(texts.StartupCompactionNote, "%d") < 2 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.texts.startup_compaction_note must contain two %%d placeholders"))
|
|
}
|
|
}
|
|
if len(texts.AutonomyImportantKeywords) == 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.texts.autonomy_important_keywords must contain at least one keyword"))
|
|
}
|
|
if strings.Count(strings.TrimSpace(texts.AutonomyCompletionTemplate), "%s") < 2 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.texts.autonomy_completion_template must contain two %%s placeholders"))
|
|
}
|
|
if strings.Count(strings.TrimSpace(texts.AutonomyBlockedTemplate), "%s") < 3 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.texts.autonomy_blocked_template must contain three %%s placeholders"))
|
|
}
|
|
|
|
if cfg.Agents.Defaults.ContextCompaction.Enabled {
|
|
cc := cfg.Agents.Defaults.ContextCompaction
|
|
if cc.Mode != "" {
|
|
switch cc.Mode {
|
|
case "summary", "responses_compact", "hybrid":
|
|
default:
|
|
errs = append(errs, fmt.Errorf("agents.defaults.context_compaction.mode must be one of: summary, responses_compact, hybrid"))
|
|
}
|
|
}
|
|
if cc.TriggerMessages <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.context_compaction.trigger_messages must be > 0 when enabled=true"))
|
|
}
|
|
if cc.KeepRecentMessages <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.context_compaction.keep_recent_messages must be > 0 when enabled=true"))
|
|
}
|
|
if cc.TriggerMessages > 0 && cc.KeepRecentMessages >= cc.TriggerMessages {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.context_compaction.keep_recent_messages must be < trigger_messages"))
|
|
}
|
|
if cc.MaxSummaryChars <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.context_compaction.max_summary_chars must be > 0 when enabled=true"))
|
|
}
|
|
if cc.MaxTranscriptChars <= 0 {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.context_compaction.max_transcript_chars must be > 0 when enabled=true"))
|
|
}
|
|
}
|
|
|
|
if len(cfg.Providers.Proxies) == 0 {
|
|
errs = append(errs, validateProviderConfig("providers.proxy", cfg.Providers.Proxy)...)
|
|
} else {
|
|
for name, p := range cfg.Providers.Proxies {
|
|
errs = append(errs, validateProviderConfig("providers.proxies."+name, p)...)
|
|
}
|
|
}
|
|
if cfg.Agents.Defaults.Proxy != "" {
|
|
if !providerExists(cfg, cfg.Agents.Defaults.Proxy) {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.proxy %q not found in providers", cfg.Agents.Defaults.Proxy))
|
|
}
|
|
}
|
|
for _, name := range cfg.Agents.Defaults.ProxyFallbacks {
|
|
if !providerExists(cfg, name) {
|
|
errs = append(errs, fmt.Errorf("agents.defaults.proxy_fallbacks contains unknown proxy %q", name))
|
|
}
|
|
}
|
|
if cfg.Agents.Defaults.ContextCompaction.Enabled && cfg.Agents.Defaults.ContextCompaction.Mode == "responses_compact" {
|
|
active := cfg.Agents.Defaults.Proxy
|
|
if active == "" {
|
|
active = "proxy"
|
|
}
|
|
if pc, ok := providerConfigByName(cfg, active); !ok || !pc.SupportsResponsesCompact {
|
|
errs = append(errs, fmt.Errorf("context_compaction.mode=responses_compact requires active proxy %q with supports_responses_compact=true", active))
|
|
}
|
|
}
|
|
|
|
if cfg.Gateway.Port <= 0 || cfg.Gateway.Port > 65535 {
|
|
errs = append(errs, fmt.Errorf("gateway.port must be in 1..65535"))
|
|
}
|
|
if cfg.Cron.MinSleepSec <= 0 {
|
|
errs = append(errs, fmt.Errorf("cron.min_sleep_sec must be > 0"))
|
|
}
|
|
if cfg.Cron.MaxSleepSec <= 0 {
|
|
errs = append(errs, fmt.Errorf("cron.max_sleep_sec must be > 0"))
|
|
}
|
|
if cfg.Cron.MinSleepSec > cfg.Cron.MaxSleepSec {
|
|
errs = append(errs, fmt.Errorf("cron.min_sleep_sec must be <= cron.max_sleep_sec"))
|
|
}
|
|
if cfg.Cron.RetryBackoffBaseSec <= 0 {
|
|
errs = append(errs, fmt.Errorf("cron.retry_backoff_base_sec must be > 0"))
|
|
}
|
|
if cfg.Cron.RetryBackoffMaxSec <= 0 {
|
|
errs = append(errs, fmt.Errorf("cron.retry_backoff_max_sec must be > 0"))
|
|
}
|
|
if cfg.Cron.RetryBackoffBaseSec > cfg.Cron.RetryBackoffMaxSec {
|
|
errs = append(errs, fmt.Errorf("cron.retry_backoff_base_sec must be <= cron.retry_backoff_max_sec"))
|
|
}
|
|
if cfg.Cron.MaxConsecutiveFailureRetries < 0 {
|
|
errs = append(errs, fmt.Errorf("cron.max_consecutive_failure_retries must be >= 0"))
|
|
}
|
|
if cfg.Cron.MaxWorkers <= 0 {
|
|
errs = append(errs, fmt.Errorf("cron.max_workers must be > 0"))
|
|
}
|
|
|
|
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.Sentinel.Enabled && cfg.Sentinel.IntervalSec <= 0 {
|
|
errs = append(errs, fmt.Errorf("sentinel.interval_sec must be > 0 when sentinel.enabled=true"))
|
|
}
|
|
if cfg.Memory.RecentDays <= 0 {
|
|
errs = append(errs, fmt.Errorf("memory.recent_days must be > 0"))
|
|
}
|
|
|
|
if cfg.Channels.InboundMessageIDDedupeTTLSeconds <= 0 {
|
|
errs = append(errs, fmt.Errorf("channels.inbound_message_id_dedupe_ttl_seconds must be > 0"))
|
|
}
|
|
if cfg.Channels.InboundContentDedupeWindowSeconds <= 0 {
|
|
errs = append(errs, fmt.Errorf("channels.inbound_content_dedupe_window_seconds must be > 0"))
|
|
}
|
|
if cfg.Channels.OutboundDedupeWindowSeconds <= 0 {
|
|
errs = append(errs, fmt.Errorf("channels.outbound_dedupe_window_seconds 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
|
|
}
|
|
|
|
func validateProviderConfig(path string, p ProviderConfig) []error {
|
|
var errs []error
|
|
if p.APIBase == "" {
|
|
errs = append(errs, fmt.Errorf("%s.api_base is required", path))
|
|
}
|
|
if p.TimeoutSec <= 0 {
|
|
errs = append(errs, fmt.Errorf("%s.timeout_sec must be > 0", path))
|
|
}
|
|
if len(p.Models) == 0 {
|
|
errs = append(errs, fmt.Errorf("%s.models must contain at least one model", path))
|
|
}
|
|
if p.Responses.WebSearchContextSize != "" {
|
|
switch p.Responses.WebSearchContextSize {
|
|
case "low", "medium", "high":
|
|
default:
|
|
errs = append(errs, fmt.Errorf("%s.responses.web_search_context_size must be one of: low, medium, high", path))
|
|
}
|
|
}
|
|
if p.Responses.FileSearchMaxNumResults < 0 {
|
|
errs = append(errs, fmt.Errorf("%s.responses.file_search_max_num_results must be >= 0", path))
|
|
}
|
|
errs = append(errs, validateNonEmptyStringList(path+".responses.file_search_vector_store_ids", p.Responses.FileSearchVectorStoreIDs)...)
|
|
errs = append(errs, validateNonEmptyStringList(path+".responses.include", p.Responses.Include)...)
|
|
return errs
|
|
}
|
|
|
|
func providerExists(cfg *Config, name string) bool {
|
|
if name == "proxy" && cfg.Providers.Proxy.APIBase != "" {
|
|
return true
|
|
}
|
|
if cfg.Providers.Proxies == nil {
|
|
return false
|
|
}
|
|
_, ok := cfg.Providers.Proxies[name]
|
|
return ok
|
|
}
|
|
|
|
func providerConfigByName(cfg *Config, name string) (ProviderConfig, bool) {
|
|
if strings.TrimSpace(name) == "proxy" {
|
|
return cfg.Providers.Proxy, true
|
|
}
|
|
pc, ok := cfg.Providers.Proxies[name]
|
|
return pc, ok
|
|
}
|
|
|
|
func validateNonEmptyStringList(path string, values []string) []error {
|
|
if len(values) == 0 {
|
|
return nil
|
|
}
|
|
var errs []error
|
|
for i, value := range values {
|
|
if strings.TrimSpace(value) == "" {
|
|
errs = append(errs, fmt.Errorf("%s[%d] must not be empty", path, i))
|
|
}
|
|
}
|
|
return errs
|
|
}
|