Files
Cyrene_v2/internal/runtime/loop/turn.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

63 lines
2.6 KiB
Go

package loop
import (
"context"
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
)
// This file owns the loop's turn boundary: the point where one user turn has
// settled (a Run produced a Result) and the state that follows the persona —
// emotion and affection, design/01 §2 — is advanced outside the loop. The loop
// itself stays persona- and emotion-agnostic (design/16 §3.3: the engine
// generates text; state and persistence belong to the layers around it), so it
// only reports the turn and records the mood label the hook hands back.
// TurnEnd describes one settled turn to the loop's OnTurnEnd hook. It carries
// everything an observer needs without the loop knowing what the observer does.
type TurnEnd struct {
// SessionID is the conversation the turn belonged to.
SessionID string
// PersonaID is the persona the loop is bound to (Options.PersonaID), so the
// observer stays persona-scoped (design/01 §2).
PersonaID string
// UserText is the latest user message of the turn ("" when the turn carried
// no user text, e.g. a resumed tool round).
UserText string
// AssistantText is the turn's final assistant text.
AssistantText string
// StopReason classifies how the turn ended.
StopReason string
// Steps is the number of model steps the turn took.
Steps int
// Env is the conversation's resolved isolation environment (owner / group /
// stranger), so a hook can keep a restricted conversation read-only.
Env SessionEnv
}
// TurnEndHook observes a settled turn and returns the persona's mood label for
// it ("" when the caller tracks no emotion). It runs once per settled turn — not
// once per model step — so a multi-step tool turn applies its effect exactly
// once. It must not block: it runs on the Run path after the answer is complete,
// and its return value is carried on Result.Mood to the persistence layer.
type TurnEndHook func(TurnEnd) string
// settleTurn reports a settled turn to the hook (when wired) and records the
// returned mood label on the result, so the persisted assistant message carries
// the turn's mood (design/16 §3.3). The conversation environment travels on ctx,
// so a group/stranger turn reaches the hook as restricted.
func (l *Loop) settleTurn(ctx context.Context, sessionID string, msgs []model.Message, res *Result) {
if l.onTurnEnd == nil {
return
}
res.Mood = l.onTurnEnd(TurnEnd{
SessionID: sessionID,
PersonaID: l.personaID,
UserText: lastUserText(msgs),
AssistantText: res.Text,
StopReason: res.StopReason,
Steps: res.Steps,
Env: sessionEnvFrom(ctx),
})
}