Files
Cyrene_v2/internal/persona/registry/registry.go
T
AskaEth 1a85de555d
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled
feat(emotion): A1 情绪系统接线 + refactor: 拆分 channelbridge.go
A1 情绪接线:
- loop 回合边界钩子(TurnEnd/OnTurnEnd/settleTurn, Result.Mood); 每轮一次非每步
- persona/emotion: React(valence五档) + Machine.Move(BFS最短合法路径一步) + Observer(observe/decay/好感度,仅变化才emit); 好感度步长读角色配置(cyrene delta=10, 0..700)
- 落库 Mood(web session_persist.res.Mood + IM channelbridge/persist; user消息不带mood); WS mood事件(BroadcastMood,仅变化); per-persona隔离; 群聊/陌生人只读不写不发
- cmd/cyrene/emotion.go 生产接线 + wire.go 注入 loop.Options.OnTurnEnd
- 22 测试(状态机被loop调用/Mood非空落库/mood帧shape/只变化才发/群聊只读) + 真二进制冒烟
- Docs: api-client v0.32 §3.7 情绪事件 + 开源对照清单(companion 应用对照)
- 诚实: 本机无LLM凭据→未活体验证'模型返回后完整轮次'一环

拆分 channelbridge.go 816→257(纯搬移零逻辑): connect/inbound/outbound/blacklist/helpers.go; A1 的 IM mood 调用点落在新 inbound.go, 故与本提交同批

回滚 6 个 gofmt 顺手格式化文件(capacity_loop_test/flow/source_test/findchannel_test/admin_channels_platform_test/history) 缩 diff, 含 history.go 注释被转弯引号
2026-09-30 08:51:38 +08:00

277 lines
8.0 KiB
Go

// Package registry implements the character library and persona registration
// for the persona system. A character comes from the character library, binds
// exactly one persona, and declares the behavior modes it is good at; the
// registered persona additionally owns a per-persona memory isolation namespace
// and the emotion/affection state that follows the persona.
package registry
import (
"fmt"
"sort"
"sync"
"git.yeij.top/AskaEth/Cyrene_v2/internal/persona/emotion"
"git.yeij.top/AskaEth/Cyrene_v2/internal/persona/loader"
)
// Character is an entity from the character library. It binds one persona and
// declares the behavior modes it excels at (as a suggestion, never an ability
// bound — see DevDocs/design/01).
type Character struct {
// ID is the unique character identifier.
ID string
// Name is the display name of the character.
Name string
// Default marks this character as the library's default role.
Default bool
// PersonaID is the persona bound to this character.
PersonaID string
// Modes lists the behavior modes the character is good at.
Modes []string
}
// Registry is the in-memory, concurrency-safe character library and persona
// registry. It also owns the emotion Manager so that emotion/affection always
// follow the registered persona.
type Registry struct {
mu sync.RWMutex
personas map[string]*loader.Persona
characters map[string]*Character
defaultID string
emotions *emotion.Manager
}
// New returns an empty Registry.
func New() *Registry {
return &Registry{
personas: make(map[string]*loader.Persona),
characters: make(map[string]*Character),
emotions: emotion.NewManager(),
}
}
// RegisterPersona registers p and builds its emotion machine and affection
// tracker. It fails when p is nil, has no id, is already registered, or its
// emotion/affection config is invalid.
func (r *Registry) RegisterPersona(p *loader.Persona) error {
if p == nil {
return fmt.Errorf("registry: nil persona")
}
if p.ID == "" {
return fmt.Errorf("registry: persona id required")
}
machine, err := p.NewMachine()
if err != nil {
return fmt.Errorf("registry: persona %q machine: %w", p.ID, err)
}
affection, err := p.NewAffection()
if err != nil {
return fmt.Errorf("registry: persona %q affection: %w", p.ID, err)
}
r.mu.Lock()
if _, ok := r.personas[p.ID]; ok {
r.mu.Unlock()
return fmt.Errorf("registry: persona %q already registered", p.ID)
}
r.personas[p.ID] = p
r.mu.Unlock()
r.emotions.Set(p.ID, machine, affection)
return nil
}
// Persona returns the persona registered under id.
func (r *Registry) Persona(id string) (*loader.Persona, bool) {
r.mu.RLock()
defer r.mu.RUnlock()
p, ok := r.personas[id]
return p, ok
}
// UpdatePersona replaces the persona registered under p.ID with p, rebuilding
// its emotion machine and affection tracker. It fails when p is nil, has no id,
// or is not currently registered. In-flight loops bound to the previous
// *loader.Persona keep their pointer; only future lookups observe the new value.
func (r *Registry) UpdatePersona(p *loader.Persona) error {
if p == nil {
return fmt.Errorf("registry: nil persona")
}
if p.ID == "" {
return fmt.Errorf("registry: persona id required")
}
machine, err := p.NewMachine()
if err != nil {
return fmt.Errorf("registry: persona %q machine: %w", p.ID, err)
}
affection, err := p.NewAffection()
if err != nil {
return fmt.Errorf("registry: persona %q affection: %w", p.ID, err)
}
r.mu.Lock()
if _, ok := r.personas[p.ID]; !ok {
r.mu.Unlock()
return fmt.Errorf("registry: persona %q not registered", p.ID)
}
r.personas[p.ID] = p
r.mu.Unlock()
r.emotions.Set(p.ID, machine, affection)
return nil
}
// UpdateCharacter replaces the fields of the character registered under c.ID,
// leaving its Default flag intact unless c re-declares it. It fails when c is
// nil, has no id, is not registered, or its persona does not exist.
func (r *Registry) UpdateCharacter(c *Character) error {
if c == nil {
return fmt.Errorf("registry: nil character")
}
if c.ID == "" {
return fmt.Errorf("registry: character id required")
}
r.mu.Lock()
defer r.mu.Unlock()
existing, ok := r.characters[c.ID]
if !ok {
return fmt.Errorf("registry: character %q not registered", c.ID)
}
if _, ok := r.personas[c.PersonaID]; !ok {
return fmt.Errorf("registry: character %q references unknown persona %q", c.ID, c.PersonaID)
}
existing.Name = c.Name
existing.PersonaID = c.PersonaID
existing.Modes = append(existing.Modes[:0], c.Modes...)
if c.Default {
existing.Default = true
r.defaultID = c.ID
}
return nil
}
// RegisterCharacter registers c. It requires c's persona to exist, so persona
// registration must happen first. A character marked Default becomes the
// library's default role.
func (r *Registry) RegisterCharacter(c *Character) error {
if c == nil {
return fmt.Errorf("registry: nil character")
}
if c.ID == "" {
return fmt.Errorf("registry: character id required")
}
if c.Name == "" {
return fmt.Errorf("registry: character %q name required", c.ID)
}
if c.PersonaID == "" {
return fmt.Errorf("registry: character %q personaID required", c.ID)
}
r.mu.Lock()
defer r.mu.Unlock()
if _, ok := r.characters[c.ID]; ok {
return fmt.Errorf("registry: character %q already registered", c.ID)
}
if _, ok := r.personas[c.PersonaID]; !ok {
return fmt.Errorf("registry: character %q references unknown persona %q", c.ID, c.PersonaID)
}
r.characters[c.ID] = c
if c.Default {
r.defaultID = c.ID
}
return nil
}
// Character returns the character registered under id.
func (r *Registry) Character(id string) (*Character, bool) {
r.mu.RLock()
defer r.mu.RUnlock()
c, ok := r.characters[id]
return c, ok
}
// SetDefaultCharacter makes the character with id the library default. It fails
// when id is not a registered character.
func (r *Registry) SetDefaultCharacter(id string) error {
r.mu.Lock()
defer r.mu.Unlock()
c, ok := r.characters[id]
if !ok {
return fmt.Errorf("registry: set default to unknown character %q", id)
}
c.Default = true
r.defaultID = id
return nil
}
// DefaultCharacter returns the library's default character, if one is set.
func (r *Registry) DefaultCharacter() (*Character, bool) {
r.mu.RLock()
defer r.mu.RUnlock()
if r.defaultID == "" {
return nil, false
}
c, ok := r.characters[r.defaultID]
return c, ok
}
// MemoryNamespace returns the per-persona memory isolation namespace for the
// persona under personaID, guarding memory isolation between personas.
func (r *Registry) MemoryNamespace(personaID string) (string, bool) {
r.mu.RLock()
defer r.mu.RUnlock()
p, ok := r.personas[personaID]
if !ok {
return "", false
}
return p.MemoryNamespace(), true
}
// Emotions returns the emotion Manager that owns every registered persona's
// emotion and affection state.
func (r *Registry) Emotions() *emotion.Manager {
return r.emotions
}
// AffectionStep returns the per-turn affection magnitude configured for the
// persona under personaID (its 好感度曲线 delta, design/01 §2). It returns 0 for
// an unknown persona, so a caller skips the adjustment instead of inventing one.
func (r *Registry) AffectionStep(personaID string) int {
if p, ok := r.Persona(personaID); ok {
return p.AffectionStep()
}
return 0
}
// CountPersonas returns the number of registered personas.
func (r *Registry) CountPersonas() int {
r.mu.RLock()
defer r.mu.RUnlock()
return len(r.personas)
}
// CountCharacters returns the number of registered characters.
func (r *Registry) CountCharacters() int {
r.mu.RLock()
defer r.mu.RUnlock()
return len(r.characters)
}
// List returns every registered character, sorted by ID. Each returned Character
// is a copy so callers cannot mutate the registry's internal state. It is safe
// for concurrent use.
func (r *Registry) List() []Character {
r.mu.RLock()
defer r.mu.RUnlock()
out := make([]Character, 0, len(r.characters))
for _, c := range r.characters {
ch := *c
ch.Modes = append([]string(nil), c.Modes...)
out = append(out, ch)
}
sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
return out
}