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 注释被转弯引号
109 lines
5.2 KiB
Go
109 lines
5.2 KiB
Go
package channelbridge
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/session"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/history"
|
|
)
|
|
|
|
// This file holds the channelbridge's chat-history persistence hooks (design/16
|
|
// §4.4): an owner private chat is routed INTO the single main web session and
|
|
// tagged with the channel platform, then broadcast so the Web client mirrors it
|
|
// without a refresh. Kept separate from channelbridge.go so the bridge's inbound
|
|
// pipeline file stays under the module size bound.
|
|
|
|
// modeChat is the behavior mode written for every IM-persisted message: IM carries
|
|
// chat text alone (design/02 §1 单向回流), so no other mode ever applies there.
|
|
const modeChat = "chat"
|
|
|
|
// SetOnPersisted wires (or replaces) the transport hook called after an
|
|
// owner-private message is persisted into the main web session. The gateway is
|
|
// built after the bridge in the composition root, so the hook is attached post-New
|
|
// through this setter; it is a no-op-safe call and is safe for concurrent use with
|
|
// in-flight processing.
|
|
func (b *Bridge) SetOnPersisted(fn func(history.Message)) {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
b.onPersisted = fn
|
|
}
|
|
|
|
// SetOnRejected wires (or replaces) the hook called after a message is dropped by
|
|
// the allow/deny black-or-white lists (design/16 §4.6 §6). The composition root
|
|
// routes it into the gateway audit trail. It is a no-op-safe call and is safe for
|
|
// concurrent use with in-flight processing.
|
|
func (b *Bridge) SetOnRejected(fn func(adapter.UnifiedMessage, string)) {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
b.onRejected = fn
|
|
}
|
|
|
|
// notifyPersisted hands a just-persisted message to the transport hook when one is
|
|
// wired, so the gateway can broadcast a `message_persisted` (web chat) or
|
|
// `channel_persisted` (IM adapter log) event for cross-end realtime sync.
|
|
func (b *Bridge) notifyPersisted(msg history.Message) {
|
|
b.mu.RLock()
|
|
fn := b.onPersisted
|
|
b.mu.RUnlock()
|
|
if fn != nil {
|
|
fn(msg)
|
|
}
|
|
}
|
|
|
|
// persistToMain writes one message into the main web session with an explicit source
|
|
// platform and client deviceID, then notifies the transport hook so Web clients receive
|
|
// a `message_persisted` event without a refresh (design/16 §4.4). deviceID is the
|
|
// concrete client that sent the message (design/16 §4.5) — e.g. "weixin-ilink:conf0:wx_uid"
|
|
// for IM. adapterID is the account adapter registration name that received the message
|
|
// (e.g. "kook:3bef..."), so the admin log can query a per-adapter send/receive stream;
|
|
// it is empty for Web-originated messages. mood is the persona's mood for that turn
|
|
// (design/01 §2 情绪状态机), carried on the assistant reply only (design/16 §3.3: the
|
|
// assistant message carries mode/mood); a user message and an untracked mood pass "".
|
|
// It is a no-op when no history store is wired, so the bridge runs unchanged without
|
|
// storage.
|
|
func (b *Bridge) persistToMain(ctx context.Context, role, content, mood, platform, deviceID, adapterID string) {
|
|
if b.history == nil {
|
|
return
|
|
}
|
|
full, err := b.history.AppendFull(ctx, history.Message{
|
|
SessionID: history.MainSessionID, Role: role, Content: content, Mood: mood, Platform: platform, DeviceID: deviceID, AdapterID: adapterID, Mode: modeChat,
|
|
})
|
|
if err != nil {
|
|
b.log.Warn("persist main session", "err", err, "role", role, "platform", platform, "device_id", deviceID, "adapter", adapterID)
|
|
return
|
|
}
|
|
b.notifyPersisted(full)
|
|
}
|
|
|
|
// persistIsolated writes one message into a restricted (group / stranger) session's
|
|
// own platform log (design/16 §4.6 §4): the message lands in the isolated sess-xxx
|
|
// session (NOT the main web session) and carries the channel platform, the client
|
|
// deviceID, the sender's display name and the account adapter registration name, so
|
|
// the IM adapter log page can render the non-owner message stream per adapter config.
|
|
// The message is still NOT mirrored into the Web main session (only owner-private
|
|
// messages mirror to the Web); it is merely handed to the transport hook so the
|
|
// gateway broadcasts a `channel_persisted` event and the IM adapter log page appends
|
|
// it without a refresh. It is a no-op when no history store is wired, so the bridge
|
|
// runs unchanged without storage.
|
|
func (b *Bridge) persistIsolated(ctx context.Context, role, content, deviceID string, sess session.Session, platform, senderName, adapterID string) {
|
|
if b.history == nil {
|
|
return
|
|
}
|
|
full, err := b.history.AppendFull(ctx, history.Message{
|
|
SessionID: sess.SessionID, Role: role, Content: content, Platform: platform,
|
|
DeviceID: deviceID, SenderName: senderName, AdapterID: adapterID, Mode: modeChat,
|
|
})
|
|
if err != nil {
|
|
b.log.Warn("persist isolated session", "err", err, "role", role,
|
|
"session", sess.SessionID, "platform", platform, "sender_name", senderName, "adapter", adapterID)
|
|
return
|
|
}
|
|
// Broadcast the isolated write as a `channel_persisted` event so the IM adapter
|
|
// log page appends it in realtime. The gateway splits the event type by session:
|
|
// this is NOT the main web session, so the web chat has no handler for it and the
|
|
// message itself still lands only in the isolated sess-xxx session (never the
|
|
// main/`web` session, design/16 §4.6 §3).
|
|
b.notifyPersisted(full)
|
|
}
|