mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-05-18 01:17:30 +08:00
Add multi-service provider mode
This commit is contained in:
@@ -29,8 +29,8 @@ type AgentsConfig struct {
|
||||
|
||||
type AgentDefaults struct {
|
||||
Workspace string `json:"workspace" env:"CLAWGO_AGENTS_DEFAULTS_WORKSPACE"`
|
||||
Model string `json:"model" env:"CLAWGO_AGENTS_DEFAULTS_MODEL"`
|
||||
ModelFallbacks []string `json:"model_fallbacks" env:"CLAWGO_AGENTS_DEFAULTS_MODEL_FALLBACKS"`
|
||||
Proxy string `json:"proxy" env:"CLAWGO_AGENTS_DEFAULTS_PROXY"`
|
||||
ProxyFallbacks []string `json:"proxy_fallbacks" env:"CLAWGO_AGENTS_DEFAULTS_PROXY_FALLBACKS"`
|
||||
MaxTokens int `json:"max_tokens" env:"CLAWGO_AGENTS_DEFAULTS_MAX_TOKENS"`
|
||||
Temperature float64 `json:"temperature" env:"CLAWGO_AGENTS_DEFAULTS_TEMPERATURE"`
|
||||
MaxToolIterations int `json:"max_tool_iterations" env:"CLAWGO_AGENTS_DEFAULTS_MAX_TOOL_ITERATIONS"`
|
||||
@@ -104,14 +104,17 @@ type DingTalkConfig struct {
|
||||
}
|
||||
|
||||
type ProvidersConfig struct {
|
||||
Proxy ProviderConfig `json:"proxy"`
|
||||
Proxy ProviderConfig `json:"proxy"`
|
||||
Proxies map[string]ProviderConfig `json:"proxies"`
|
||||
}
|
||||
|
||||
type ProviderConfig struct {
|
||||
APIKey string `json:"api_key" env:"CLAWGO_PROVIDERS_{{.Name}}_API_KEY"`
|
||||
APIBase string `json:"api_base" env:"CLAWGO_PROVIDERS_{{.Name}}_API_BASE"`
|
||||
Auth string `json:"auth" env:"CLAWGO_PROVIDERS_{{.Name}}_AUTH"`
|
||||
TimeoutSec int `json:"timeout_sec" env:"CLAWGO_PROVIDERS_PROXY_TIMEOUT_SEC"`
|
||||
APIKey string `json:"api_key" env:"CLAWGO_PROVIDERS_{{.Name}}_API_KEY"`
|
||||
APIBase string `json:"api_base" env:"CLAWGO_PROVIDERS_{{.Name}}_API_BASE"`
|
||||
Protocol string `json:"protocol" env:"CLAWGO_PROVIDERS_{{.Name}}_PROTOCOL"`
|
||||
Models []string `json:"models" env:"CLAWGO_PROVIDERS_{{.Name}}_MODELS"`
|
||||
Auth string `json:"auth" env:"CLAWGO_PROVIDERS_{{.Name}}_AUTH"`
|
||||
TimeoutSec int `json:"timeout_sec" env:"CLAWGO_PROVIDERS_PROXY_TIMEOUT_SEC"`
|
||||
}
|
||||
|
||||
type GatewayConfig struct {
|
||||
@@ -228,8 +231,8 @@ func DefaultConfig() *Config {
|
||||
Agents: AgentsConfig{
|
||||
Defaults: AgentDefaults{
|
||||
Workspace: filepath.Join(configDir, "workspace"),
|
||||
Model: "glm-4.7",
|
||||
ModelFallbacks: []string{},
|
||||
Proxy: "proxy",
|
||||
ProxyFallbacks: []string{},
|
||||
MaxTokens: 8192,
|
||||
Temperature: 0.7,
|
||||
MaxToolIterations: 20,
|
||||
@@ -288,8 +291,11 @@ func DefaultConfig() *Config {
|
||||
Providers: ProvidersConfig{
|
||||
Proxy: ProviderConfig{
|
||||
APIBase: "http://localhost:8080/v1",
|
||||
Protocol: "chat_completions",
|
||||
Models: []string{"glm-4.7"},
|
||||
TimeoutSec: 90,
|
||||
},
|
||||
Proxies: map[string]ProviderConfig{},
|
||||
},
|
||||
Gateway: GatewayConfig{
|
||||
Host: "0.0.0.0",
|
||||
|
||||
@@ -11,9 +11,6 @@ func Validate(cfg *Config) []error {
|
||||
|
||||
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"))
|
||||
}
|
||||
@@ -36,11 +33,22 @@ func Validate(cfg *Config) []error {
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.Providers.Proxy.APIBase == "" {
|
||||
errs = append(errs, fmt.Errorf("providers.proxy.api_base is required"))
|
||||
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.Providers.Proxy.TimeoutSec <= 0 {
|
||||
errs = append(errs, fmt.Errorf("providers.proxy.timeout_sec must be > 0"))
|
||||
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.Gateway.Port <= 0 || cfg.Gateway.Port > 65535 {
|
||||
@@ -129,3 +137,35 @@ func Validate(cfg *Config) []error {
|
||||
|
||||
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.Protocol != "" {
|
||||
switch p.Protocol {
|
||||
case "chat_completions", "responses":
|
||||
default:
|
||||
errs = append(errs, fmt.Errorf("%s.protocol must be one of: chat_completions, responses", 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))
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user