Files
Cyrene/backend/platform-bridge/internal/config/store.go
T
AskaEth 0717928496 fix: Phase 6联调 — 插件管理器端口修正 + 多模型配置系统整合 + 历史消息刷新修复
## 调试日志

### 1. 插件管理器启动失败
- **症状**: DevTools 显示插件管理器一直"已停止",手动启动正常
- **排查**: 对比 process-manager.js 传入的环境变量 vs plugin-manager config.go 读取的变量
- **根因**: config.js 传入 PLUGIN_MANAGER_PORT=8094,但 config.go 读取 os.Getenv("PORT"),env 名不匹配。且 process.env 中 PORT 泄露时被误读为 9090,与 DevTools 端口冲突
- **修复**: config.js 将 PLUGIN_MANAGER_PORT → PORT,使 env 名与代码一致 (c3055f4)

### 2. 历史消息刷新后消失
- **症状**: 浏览器刷新后聊天历史清空
- **排查**: WebSocket history_response handler 中 if (msg.messages) 对空数组 [] 为 truthy
- **根因**: 后端返回空的 history_response (缓存为空) 时,空数组覆盖了 HTTP 已加载的消息
- **修复**: useWebSocket.ts 改为 if (msg.messages && msg.messages.length > 0),空数组走 else-if 分支仅打日志,不覆盖已有消息

### 3. Phase 6 多模型配置系统
- Gateway: ModelsConfigStore (JSON文件持久化) + Admin CRUD API (providers/models/routing)
- ai-core: ModelSelector 支持按 purpose 选择 + fallback_chain,无配置时回退 .env
- DevTools: 模型配置管理面板 (Providers/Models/Routing 三Tab)、在线模型查询代理、路由表单 checkbox 多选、关键词搜索过滤
- .gitignore: models.json + platform_configs.json

### 4. 多端客户端追踪
- Hub 新增 knownClients 映射 (clientID → KnownClient),在线/离线状态追踪
- 客户端备注持久化到 PostgreSQL
- DevTools 客户端管理面板

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-23 21:23:10 +08:00

121 lines
2.7 KiB
Go

package config
import (
"encoding/json"
"fmt"
"os"
"sync"
"time"
)
// PlatformConfig holds persistent configuration for one platform adapter.
type PlatformConfig struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
Label string `json:"label"`
Fields map[string]string `json:"fields"`
UpdatedAt time.Time `json:"updated_at"`
}
// Store manages persistence of platform configs to a JSON file.
type Store struct {
mu sync.RWMutex
path string
configs map[string]*PlatformConfig
}
// NewStore creates a Store, creating the config file if it doesn't exist.
func NewStore(path string) (*Store, error) {
s := &Store{
path: path,
configs: make(map[string]*PlatformConfig),
}
if err := s.load(); err != nil {
return nil, err
}
return s, nil
}
func (s *Store) load() error {
data, err := os.ReadFile(s.path)
if err != nil {
if os.IsNotExist(err) {
// Initialize empty file.
return s.save()
}
return fmt.Errorf("read config file: %w", err)
}
if len(data) == 0 {
return nil
}
if err := json.Unmarshal(data, &s.configs); err != nil {
return fmt.Errorf("parse config file: %w", err)
}
return nil
}
func (s *Store) save() error {
data, err := json.MarshalIndent(s.configs, "", " ")
if err != nil {
return fmt.Errorf("marshal configs: %w", err)
}
tmpPath := s.path + ".tmp"
if err := os.WriteFile(tmpPath, data, 0640); err != nil {
return fmt.Errorf("write config file: %w", err)
}
return os.Rename(tmpPath, s.path)
}
// List returns all platform configs.
func (s *Store) List() []PlatformConfig {
s.mu.RLock()
defer s.mu.RUnlock()
result := make([]PlatformConfig, 0, len(s.configs))
for _, c := range s.configs {
result = append(result, *c)
}
return result
}
// Get returns a single platform config.
func (s *Store) Get(name string) (*PlatformConfig, error) {
s.mu.RLock()
defer s.mu.RUnlock()
c, ok := s.configs[name]
if !ok {
return nil, fmt.Errorf("config not found: %s", name)
}
return c, nil
}
// Set upserts a platform config and persists.
func (s *Store) Set(cfg PlatformConfig) error {
s.mu.Lock()
defer s.mu.Unlock()
if cfg.Fields == nil {
cfg.Fields = make(map[string]string)
}
cfg.UpdatedAt = time.Now()
s.configs[cfg.Name] = &cfg
return s.save()
}
// Delete removes a platform config and persists.
func (s *Store) Delete(name string) error {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.configs[name]; !ok {
return fmt.Errorf("config not found: %s", name)
}
delete(s.configs, name)
return s.save()
}
// HasConfig checks if a config exists for the given platform.
func (s *Store) HasConfig(name string) bool {
s.mu.RLock()
defer s.mu.RUnlock()
_, ok := s.configs[name]
return ok
}