Files
Cyrene/backend/platform-bridge/internal/config/store.go
T
AskaEth 47dce276a4 fix: platform_silent记忆提取 + 群聊上下文整合 + 多QQ实例支持
- platform_silent模式接入Orchestrator记忆提取:被动观察群聊时提取值得记住的信息到对应命名空间
- post_chat后台思考注入平台观察:对话后思考也能看到群聊摘要
- QQ适配器:OneBot v11 self_id动态捕获、CQ图片URL提取、视觉+OCR并行处理
- Router解耦:ConfigName/PlatformName分离,支持多QQ实例独立连接
- 黑白名单功能:后端API + Ethend代理 + UI面板
- \n\n双换行断句:AI回复按双换行分割为多条消息按间隔发送
- @提及修复:bot自感知UID进行@检测
- 群聊上下文共享:channel-based userID避免记忆碎片化
- 消息日志显示处理后内容而非原始SSE数据
- platform-bridge Dockerfile + docker-compose.yml更新

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-31 09:37:18 +08:00

128 lines
2.9 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"`
Platform string `json:"platform"` // base platform type: "qq", "telegram", etc.
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)
}
// Backward compat: old configs without platform field default to Name.
for _, c := range s.configs {
if c.Platform == "" {
c.Platform = c.Name
}
}
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
}