mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-21 09:28:14 +00:00
feat: ConfigRegistry + SettingsAPI for plugin config access
Core exposes a unified settings interface through Plugin SDK: - internal/config/registry.go: thread-safe namespaced KV store - Register/Get/Set/List/Delete with JSON persistence - Flush() for explicit save to disk - SettingsAPI in SDK: plugins call plugin.Settings().Get/Set/List - Full access: read/write any key (core.*, plugin.<name>.*) - plugin.Registry.SetConfigRegistry() wires it into SDK plugins - main.go registers core.* keys at startup, flushes on shutdown - 7 tests for ConfigRegistry
This commit is contained in:
133
internal/config/registry.go
Normal file
133
internal/config/registry.go
Normal file
@ -0,0 +1,133 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type ConfigRegistry struct {
|
||||
mu sync.RWMutex
|
||||
values map[string]interface{}
|
||||
persistPath string
|
||||
dirty bool
|
||||
}
|
||||
|
||||
func NewConfigRegistry(persistPath string) *ConfigRegistry {
|
||||
r := &ConfigRegistry{
|
||||
values: make(map[string]interface{}),
|
||||
persistPath: persistPath,
|
||||
}
|
||||
r.load()
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *ConfigRegistry) Register(key string, value interface{}) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if _, exists := r.values[key]; !exists {
|
||||
r.values[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ConfigRegistry) RegisterDefault(key string, value interface{}) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if _, exists := r.values[key]; !exists {
|
||||
r.values[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ConfigRegistry) Get(key string) (interface{}, error) {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
v, ok := r.values[key]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("config key %q not found", key)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (r *ConfigRegistry) Set(key string, value interface{}) error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
r.values[key] = value
|
||||
r.dirty = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ConfigRegistry) List(prefix string) []string {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
var keys []string
|
||||
for k := range r.values {
|
||||
if prefix == "" || strings.HasPrefix(k, prefix) {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
}
|
||||
|
||||
func (r *ConfigRegistry) Delete(key string) error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
delete(r.values, key)
|
||||
r.dirty = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ConfigRegistry) Dump() map[string]interface{} {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
cp := make(map[string]interface{})
|
||||
for k, v := range r.values {
|
||||
cp[k] = v
|
||||
}
|
||||
return cp
|
||||
}
|
||||
|
||||
func (r *ConfigRegistry) Flush() error {
|
||||
r.mu.RLock()
|
||||
if !r.dirty {
|
||||
r.mu.RUnlock()
|
||||
return nil
|
||||
}
|
||||
r.mu.RUnlock()
|
||||
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
if r.persistPath == "" {
|
||||
return nil
|
||||
}
|
||||
os.MkdirAll(filepath.Dir(r.persistPath), 0755)
|
||||
data, err := json.MarshalIndent(r.values, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal config: %w", err)
|
||||
}
|
||||
if err := os.WriteFile(r.persistPath, data, 0644); err != nil {
|
||||
return fmt.Errorf("write config: %w", err)
|
||||
}
|
||||
r.dirty = false
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ConfigRegistry) load() {
|
||||
if r.persistPath == "" {
|
||||
return
|
||||
}
|
||||
data, err := os.ReadFile(r.persistPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var vals map[string]interface{}
|
||||
if err := json.Unmarshal(data, &vals); err != nil {
|
||||
return
|
||||
}
|
||||
r.values = vals
|
||||
}
|
||||
105
internal/config/registry_test.go
Normal file
105
internal/config/registry_test.go
Normal file
@ -0,0 +1,105 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRegistryBasic(t *testing.T) {
|
||||
r := NewConfigRegistry("")
|
||||
r.Register("core.llm.model", "deepseek-v4-flash")
|
||||
r.Register("plugin.qq.access_token", "abc123")
|
||||
|
||||
val, err := r.Get("core.llm.model")
|
||||
if err != nil {
|
||||
t.Fatalf("Get error: %v", err)
|
||||
}
|
||||
if v, ok := val.(string); !ok || v != "deepseek-v4-flash" {
|
||||
t.Fatalf("expected deepseek-v4-flash, got %v", val)
|
||||
}
|
||||
|
||||
keys := r.List("core")
|
||||
if len(keys) != 1 || keys[0] != "core.llm.model" {
|
||||
t.Fatalf("expected [core.llm.model], got %v", keys)
|
||||
}
|
||||
|
||||
r.Set("core.llm.model", "gpt-4")
|
||||
val, _ = r.Get("core.llm.model")
|
||||
if v, _ := val.(string); v != "gpt-4" {
|
||||
t.Fatalf("expected gpt-4, got %v", val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryPersist(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "settings.json")
|
||||
|
||||
r := NewConfigRegistry(path)
|
||||
r.Register("core.log_level", "debug")
|
||||
r.Set("plugin.test.key", 42)
|
||||
if err := r.Flush(); err != nil {
|
||||
t.Fatalf("Flush: %v", err)
|
||||
}
|
||||
|
||||
r2 := NewConfigRegistry(path)
|
||||
val, err := r2.Get("plugin.test.key")
|
||||
if err != nil {
|
||||
t.Fatalf("Get after reload: %v", err)
|
||||
}
|
||||
if v, _ := val.(float64); v != 42 {
|
||||
t.Fatalf("expected 42, got %v", val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryDelete(t *testing.T) {
|
||||
r := NewConfigRegistry("")
|
||||
r.Register("a.b", "1")
|
||||
r.Register("a.c", "2")
|
||||
r.Delete("a.b")
|
||||
keys := r.List("a")
|
||||
if len(keys) != 1 || keys[0] != "a.c" {
|
||||
t.Fatalf("expected [a.c], got %v", keys)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryDump(t *testing.T) {
|
||||
r := NewConfigRegistry("")
|
||||
r.Register("x", 1)
|
||||
r.Register("y", "two")
|
||||
dump := r.Dump()
|
||||
if len(dump) != 2 {
|
||||
t.Fatalf("expected 2 keys, got %d", len(dump))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryUnknownKey(t *testing.T) {
|
||||
r := NewConfigRegistry("")
|
||||
_, err := r.Get("nonexistent")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for unknown key")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryFlush(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "settings.json")
|
||||
r := NewConfigRegistry(path)
|
||||
r.Set("k", "v")
|
||||
if err := r.Flush(); err != nil {
|
||||
t.Fatalf("Flush: %v", err)
|
||||
}
|
||||
data, _ := os.ReadFile(path)
|
||||
if len(data) == 0 {
|
||||
t.Fatal("expected persisted data")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryFlushIdempotent(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "settings.json")
|
||||
r := NewConfigRegistry(path)
|
||||
r.Set("k", "v")
|
||||
r.Flush()
|
||||
r.Flush() // second flush should not error
|
||||
}
|
||||
Reference in New Issue
Block a user