fix timeout

This commit is contained in:
lpf
2026-02-14 11:50:21 +08:00
parent 64f84678cb
commit af6da309ec
5 changed files with 24 additions and 14 deletions

View File

@@ -50,7 +50,8 @@
"proxy": { "proxy": {
"api_key": "YOUR_CLIPROXYAPI_KEY", "api_key": "YOUR_CLIPROXYAPI_KEY",
"api_base": "http://localhost:8080/v1", "api_base": "http://localhost:8080/v1",
"auth": "bearer" "auth": "bearer",
"timeout_sec": 90
} }
}, },
"tools": { "tools": {

View File

@@ -31,7 +31,6 @@ import (
var errGatewayNotRunningSlash = errors.New("gateway not running") var errGatewayNotRunningSlash = errors.New("gateway not running")
const llmCallTimeout = 90 * time.Second
const perSessionQueueSize = 64 const perSessionQueueSize = 64
type sessionWorker struct { type sessionWorker struct {
@@ -53,6 +52,7 @@ type AgentLoop struct {
orchestrator *tools.Orchestrator orchestrator *tools.Orchestrator
running atomic.Bool running atomic.Bool
compactionCfg config.ContextCompactionConfig compactionCfg config.ContextCompactionConfig
llmCallTimeout time.Duration
workersMu sync.Mutex workersMu sync.Mutex
workers map[string]*sessionWorker workers map[string]*sessionWorker
} }
@@ -137,6 +137,7 @@ func NewAgentLoop(cfg *config.Config, msgBus *bus.MessageBus, provider providers
tools: toolsRegistry, tools: toolsRegistry,
orchestrator: orchestrator, orchestrator: orchestrator,
compactionCfg: cfg.Agents.Defaults.ContextCompaction, compactionCfg: cfg.Agents.Defaults.ContextCompaction,
llmCallTimeout: time.Duration(cfg.Providers.Proxy.TimeoutSec) * time.Second,
workers: make(map[string]*sessionWorker), workers: make(map[string]*sessionWorker),
} }
@@ -561,7 +562,7 @@ func (al *AgentLoop) runLLMToolLoop(
}) })
llmStart := time.Now() llmStart := time.Now()
llmCtx, cancelLLM := context.WithTimeout(ctx, llmCallTimeout) llmCtx, cancelLLM := context.WithTimeout(ctx, al.llmCallTimeout)
response, err := al.callLLMWithModelFallback(llmCtx, messages, providerToolDefs, map[string]interface{}{ response, err := al.callLLMWithModelFallback(llmCtx, messages, providerToolDefs, map[string]interface{}{
"max_tokens": 8192, "max_tokens": 8192,
"temperature": 0.7, "temperature": 0.7,
@@ -680,7 +681,7 @@ func (al *AgentLoop) runLLMToolLoop(
}) })
finalizeMessages = sanitizeMessagesForToolCalling(finalizeMessages) finalizeMessages = sanitizeMessagesForToolCalling(finalizeMessages)
llmCtx, cancelLLM := context.WithTimeout(ctx, llmCallTimeout) llmCtx, cancelLLM := context.WithTimeout(ctx, al.llmCallTimeout)
finalResp, err := al.callLLMWithModelFallback(llmCtx, finalizeMessages, nil, map[string]interface{}{ finalResp, err := al.callLLMWithModelFallback(llmCtx, finalizeMessages, nil, map[string]interface{}{
"max_tokens": 1024, "max_tokens": 1024,
"temperature": 0.3, "temperature": 0.3,

View File

@@ -107,9 +107,10 @@ type ProvidersConfig struct {
} }
type ProviderConfig struct { type ProviderConfig struct {
APIKey string `json:"api_key" env:"CLAWGO_PROVIDERS_{{.Name}}_API_KEY"` APIKey string `json:"api_key" env:"CLAWGO_PROVIDERS_{{.Name}}_API_KEY"`
APIBase string `json:"api_base" env:"CLAWGO_PROVIDERS_{{.Name}}_API_BASE"` APIBase string `json:"api_base" env:"CLAWGO_PROVIDERS_{{.Name}}_API_BASE"`
Auth string `json:"auth" env:"CLAWGO_PROVIDERS_{{.Name}}_AUTH"` Auth string `json:"auth" env:"CLAWGO_PROVIDERS_{{.Name}}_AUTH"`
TimeoutSec int `json:"timeout_sec" env:"CLAWGO_PROVIDERS_PROXY_TIMEOUT_SEC"`
} }
type GatewayConfig struct { type GatewayConfig struct {
@@ -276,7 +277,8 @@ func DefaultConfig() *Config {
}, },
Providers: ProvidersConfig{ Providers: ProvidersConfig{
Proxy: ProviderConfig{ Proxy: ProviderConfig{
APIBase: "http://localhost:8080/v1", APIBase: "http://localhost:8080/v1",
TimeoutSec: 90,
}, },
}, },
Gateway: GatewayConfig{ Gateway: GatewayConfig{

View File

@@ -39,6 +39,9 @@ func Validate(cfg *Config) []error {
if cfg.Providers.Proxy.APIBase == "" { if cfg.Providers.Proxy.APIBase == "" {
errs = append(errs, fmt.Errorf("providers.proxy.api_base is required")) errs = append(errs, fmt.Errorf("providers.proxy.api_base is required"))
} }
if cfg.Providers.Proxy.TimeoutSec <= 0 {
errs = append(errs, fmt.Errorf("providers.proxy.timeout_sec must be > 0"))
}
if cfg.Gateway.Port <= 0 || cfg.Gateway.Port > 65535 { if cfg.Gateway.Port <= 0 || cfg.Gateway.Port > 65535 {
errs = append(errs, fmt.Errorf("gateway.port must be in 1..65535")) errs = append(errs, fmt.Errorf("gateway.port must be in 1..65535"))

View File

@@ -24,18 +24,18 @@ type HTTPProvider struct {
apiKey string apiKey string
apiBase string apiBase string
authMode string authMode string
timeout time.Duration
httpClient *http.Client httpClient *http.Client
} }
const defaultChatTimeout = 90 * time.Second func NewHTTPProvider(apiKey, apiBase, authMode string, timeout time.Duration) *HTTPProvider {
func NewHTTPProvider(apiKey, apiBase, authMode string) *HTTPProvider {
return &HTTPProvider{ return &HTTPProvider{
apiKey: apiKey, apiKey: apiKey,
apiBase: apiBase, apiBase: apiBase,
authMode: authMode, authMode: authMode,
timeout: timeout,
httpClient: &http.Client{ httpClient: &http.Client{
Timeout: defaultChatTimeout, Timeout: timeout,
}, },
} }
} }
@@ -50,7 +50,7 @@ func (p *HTTPProvider) Chat(ctx context.Context, messages []Message, tools []Too
"model": model, "model": model,
"messages_count": len(messages), "messages_count": len(messages),
"tools_count": len(tools), "tools_count": len(tools),
"timeout": defaultChatTimeout.String(), "timeout": p.timeout.String(),
}) })
requestBody := map[string]interface{}{ requestBody := map[string]interface{}{
@@ -208,6 +208,9 @@ func CreateProvider(cfg *config.Config) (LLMProvider, error) {
if apiBase == "" { if apiBase == "" {
return nil, fmt.Errorf("no API base (CLIProxyAPI) configured") return nil, fmt.Errorf("no API base (CLIProxyAPI) configured")
} }
if cfg.Providers.Proxy.TimeoutSec <= 0 {
return nil, fmt.Errorf("invalid providers.proxy.timeout_sec: %d", cfg.Providers.Proxy.TimeoutSec)
}
return NewHTTPProvider(apiKey, apiBase, authMode), nil return NewHTTPProvider(apiKey, apiBase, authMode, time.Duration(cfg.Providers.Proxy.TimeoutSec)*time.Second), nil
} }