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 注释被转弯引号
111 lines
4.8 KiB
Go
111 lines
4.8 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/history"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/loop"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
|
|
)
|
|
|
|
// This file owns the orchestrator's chat-history persistence hooks (design/16
|
|
// §3.3): the user stream is written at submit time and the assistant reply when a
|
|
// turn settles. Both are silent no-ops when no history store is wired, so the
|
|
// orchestrator runs unchanged without a store (tests / an unwired gateway).
|
|
|
|
// modeChat is the behavior mode persisted when a turn carries none (design/01 §3,
|
|
// design/16 §5.1). It is also the only mode the IM paths ever write, because IM
|
|
// carries chat text alone.
|
|
const modeChat = "chat"
|
|
|
|
// modeOrDefault returns mode, or modeChat when the caller supplied none, so a
|
|
// persisted message never carries an empty behavior mode (design/16 §5.1).
|
|
func modeOrDefault(mode string) string {
|
|
if mode == "" {
|
|
return modeChat
|
|
}
|
|
return mode
|
|
}
|
|
|
|
// messageModeContextKey is the context key carrying a turn's behavior mode
|
|
// (design/01 §3, design/16 §5.1) from the HTTP/WS boundary into the orchestrator's
|
|
// persistence, so each persisted message is tagged with the mode it was sent in.
|
|
type messageModeContextKey struct{}
|
|
|
|
// withMode returns a context carrying mode so the orchestrator can tag the
|
|
// persisted user message and assistant reply. An empty mode is a no-op (the
|
|
// persistence layer falls back to modeChat).
|
|
func withMode(ctx context.Context, mode string) context.Context {
|
|
return context.WithValue(ctx, messageModeContextKey{}, mode)
|
|
}
|
|
|
|
// modeFrom returns the behavior mode carried by ctx, or "" when none was set.
|
|
func modeFrom(ctx context.Context) string {
|
|
v, _ := ctx.Value(messageModeContextKey{}).(string)
|
|
return v
|
|
}
|
|
|
|
// persistUserMessages writes each RoleUser message of a submitted turn to the
|
|
// history store so the raw user stream is durable before the turn resolves, then
|
|
// notifies the transport so every browser of the same account can append it
|
|
// without a manual refresh (design/16 §3.3, §4.2). deviceID tags the message with
|
|
// the sending client (design/16 §4.5) and mode is the behavior mode the turn was
|
|
// sent in (design/16 §5.1), falling back to modeChat when empty. It is a no-op
|
|
// when no history store is wired. Rejected turns (empty, pending-full) never
|
|
// reach it, so nothing vacuous is persisted.
|
|
func (o *Orchestrator) persistUserMessages(ctx context.Context, sessionID string, messages []model.Message, deviceID, mode string) {
|
|
if o.history == nil {
|
|
return
|
|
}
|
|
for _, m := range messages {
|
|
if m.Role != model.RoleUser {
|
|
continue
|
|
}
|
|
text, image := messageTextImage(m)
|
|
if text == "" && image == "" {
|
|
continue
|
|
}
|
|
full, err := o.history.AppendFull(ctx, history.Message{SessionID: sessionID, Role: history.RoleUser, Content: text, Image: image, DeviceID: deviceID, Mode: modeOrDefault(mode)})
|
|
if err != nil {
|
|
o.log.Error("orchestrator persist user", "err", err, "session", sessionID, "device_id", deviceID)
|
|
continue
|
|
}
|
|
o.notifyPersisted(full)
|
|
}
|
|
}
|
|
|
|
// persistAssistantReply writes the final assistant text of a settled turn to the
|
|
// history store, then notifies the transport (design/16 §3.3, §4.2). deviceID tags
|
|
// the reply with the client that sent the turn (design/16 §4.5) and mode is the
|
|
// behavior mode of that turn (design/16 §5.1), falling back to modeChat when
|
|
// empty. Mood is the persona's mood for that turn (design/01 §2 情绪状态机), taken
|
|
// off the loop result, so the stored/DTO/`message_persisted` mood is real data
|
|
// rather than a permanently empty column; a turn with no mood tracking stores an
|
|
// empty mood. It is a no-op when no history store is wired or the turn produced no
|
|
// text (interrupted/errored turns leave res nil). The write runs under a fresh
|
|
// background context so a canceled turn context never aborts it.
|
|
func (o *Orchestrator) persistAssistantReply(sessionID string, res *loop.Result, deviceID, mode string) {
|
|
if o.history == nil || res == nil || res.Text == "" {
|
|
return
|
|
}
|
|
full, err := o.history.AppendFull(context.Background(), history.Message{SessionID: sessionID, Role: history.RoleAssistant, Content: res.Text, Mood: res.Mood, DeviceID: deviceID, Mode: modeOrDefault(mode)})
|
|
if err != nil {
|
|
o.log.Error("orchestrator persist assistant", "err", err, "session", sessionID, "device_id", deviceID)
|
|
return
|
|
}
|
|
o.notifyPersisted(full)
|
|
}
|
|
|
|
// messageTextImage returns the concatenated text and the first image URL of a
|
|
// message's content parts, for persistence into the chat_messages columns.
|
|
func messageTextImage(m model.Message) (text, image string) {
|
|
for _, c := range m.Content {
|
|
if c.Type == model.ContentTypeText {
|
|
text += c.Text
|
|
} else if c.Type == model.ContentTypeImage && image == "" {
|
|
image = c.ImageURL
|
|
}
|
|
}
|
|
return text, image
|
|
}
|