mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-04-27 18:14:25 +08:00
Fix provider config hot reload
This commit is contained in:
@@ -48,7 +48,7 @@ type Server struct {
|
||||
logFilePath string
|
||||
onChat func(ctx context.Context, sessionKey, content string) (string, error)
|
||||
onChatHistory func(sessionKey string) []map[string]interface{}
|
||||
onConfigAfter func() error
|
||||
onConfigAfter func(forceRuntimeReload bool) error
|
||||
onCron func(action string, args map[string]interface{}) (interface{}, error)
|
||||
onToolsCatalog func() interface{}
|
||||
whatsAppBridge *channels.WhatsAppBridgeService
|
||||
@@ -85,7 +85,7 @@ func (s *Server) SetChatHandler(fn func(ctx context.Context, sessionKey, content
|
||||
func (s *Server) SetChatHistoryHandler(fn func(sessionKey string) []map[string]interface{}) {
|
||||
s.onChatHistory = fn
|
||||
}
|
||||
func (s *Server) SetConfigAfterHook(fn func() error) { s.onConfigAfter = fn }
|
||||
func (s *Server) SetConfigAfterHook(fn func(forceRuntimeReload bool) error) { s.onConfigAfter = fn }
|
||||
func (s *Server) SetCronHandler(fn func(action string, args map[string]interface{}) (interface{}, error)) {
|
||||
s.onCron = fn
|
||||
}
|
||||
@@ -414,7 +414,7 @@ func (s *Server) persistWebUIConfig(cfg *cfgpkg.Config) error {
|
||||
return err
|
||||
}
|
||||
if s.onConfigAfter != nil {
|
||||
return s.onConfigAfter()
|
||||
return s.onConfigAfter(false)
|
||||
}
|
||||
return requestSelfReloadSignal()
|
||||
}
|
||||
@@ -978,7 +978,9 @@ func (s *Server) saveProviderConfig(cfg *cfgpkg.Config, name string, pc cfgpkg.P
|
||||
return err
|
||||
}
|
||||
if s.onConfigAfter != nil {
|
||||
if err := s.onConfigAfter(); err != nil {
|
||||
// Provider updates may only change external credential file contents,
|
||||
// so force a runtime rebuild even when config JSON remains identical.
|
||||
if err := s.onConfigAfter(true); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -100,7 +100,10 @@ func TestHandleWebUIConfigPostSavesRawConfig(t *testing.T) {
|
||||
srv := NewServer("127.0.0.1", 0, "")
|
||||
srv.SetConfigPath(cfgPath)
|
||||
hookCalled := 0
|
||||
srv.SetConfigAfterHook(func() error {
|
||||
srv.SetConfigAfterHook(func(forceRuntimeReload bool) error {
|
||||
if forceRuntimeReload {
|
||||
t.Fatalf("expected raw config save to use non-forced reload")
|
||||
}
|
||||
hookCalled++
|
||||
return nil
|
||||
})
|
||||
@@ -150,7 +153,12 @@ func TestHandleWebUIConfigPostSavesNormalizedConfig(t *testing.T) {
|
||||
|
||||
srv := NewServer("127.0.0.1", 0, "")
|
||||
srv.SetConfigPath(cfgPath)
|
||||
srv.SetConfigAfterHook(func() error { return nil })
|
||||
srv.SetConfigAfterHook(func(forceRuntimeReload bool) error {
|
||||
if forceRuntimeReload {
|
||||
t.Fatalf("expected normalized config save to use non-forced reload")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/config?mode=normalized", strings.NewReader(`{"core":{"gateway":{"host":"127.0.0.1","port":18790},"tools":{"shell_enabled":false,"mcp_enabled":true}},"runtime":{"router":{"enabled":true,"strategy":"rules_first","max_hops":2,"default_timeout_sec":90},"providers":{"openai":{"api_base":"https://api.openai.com/v1","auth":"bearer","timeout_sec":150}}}}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
@@ -172,6 +180,45 @@ func TestHandleWebUIConfigPostSavesNormalizedConfig(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaveProviderConfigForcesRuntimeReload(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
cfgPath := filepath.Join(tmp, "config.json")
|
||||
cfg := cfgpkg.DefaultConfig()
|
||||
cfg.Logging.Enabled = false
|
||||
cfg.Models.Providers["openai"] = cfgpkg.ProviderConfig{
|
||||
APIBase: "https://api.openai.com/v1",
|
||||
Auth: "oauth",
|
||||
Models: []string{"gpt-5"},
|
||||
TimeoutSec: 120,
|
||||
OAuth: cfgpkg.ProviderOAuthConfig{
|
||||
Provider: "codex",
|
||||
CredentialFile: filepath.Join(tmp, "auth.json"),
|
||||
},
|
||||
}
|
||||
if err := cfgpkg.SaveConfig(cfgPath, cfg); err != nil {
|
||||
t.Fatalf("save config: %v", err)
|
||||
}
|
||||
|
||||
srv := NewServer("127.0.0.1", 0, "")
|
||||
srv.SetConfigPath(cfgPath)
|
||||
|
||||
forced := false
|
||||
srv.SetConfigAfterHook(func(forceRuntimeReload bool) error {
|
||||
forced = forceRuntimeReload
|
||||
return nil
|
||||
})
|
||||
|
||||
pc := cfg.Models.Providers["openai"]
|
||||
if err := srv.saveProviderConfig(cfg, "openai", pc); err != nil {
|
||||
t.Fatalf("save provider config: %v", err)
|
||||
}
|
||||
if !forced {
|
||||
t.Fatalf("expected provider config save to force runtime reload")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithCORSEchoesPreflightHeaders(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user