Files
Cyrene_v2/internal/services/gateway/ws_history.go
T
Aska 48421923f6 feat(history): 会话消息持久化 + 主/次级会话(design/16)
- runtime/history: Store + HistoryStore 接口(Append/List 游标分页/Recent/DeleteSession/NewSubSession/ListSessions),主会话固定 web、次级 web:<uuid>
- db 迁移 0003: chat_sessions + chat_messages + idx_chat_msgs_session_created(事务幂等)
- gateway: GET/POST /sessions、GET /sessions/{id}/messages 热分页、DELETE /sessions/{id}(主不可删 403);Deps.History 可空注入
- 编排层落库: session_persist.go(submitInternal 写用户消息、completeTurn 写助手消息,拒绝的消息不落库)
- WS: wsEvent 加 sessionId 定向 + history 回放帧(握手上行后回放主会话最新 50 条,thinking 剥离)
- wire.go 装配 History 到 gateway.Deps.History
- 端到端实测: POST /sessions 建次级 201 / GET /sessions 列表 / POST /chat 后 /messages 读到 user+assistant / DELETE 次级成功 / DELETE 主会话 403
2026-09-07 03:52:43 +08:00

35 lines
1.2 KiB
Go

package gateway
import (
"context"
)
// wsReplayLimit is the number of most-recent messages replayed to a connecting
// client. It is intentionally small so a reconnect is cheap; the frontend pulls
// the full active-session history via REST (design/16 §4.1) and the replay is
// only there to give a brand-new connection immediate context.
const wsReplayLimit = 50
// replayHistory sends the most recent messages for sessionID to a single,
// freshly-registered connection as a ws "history" event. It is best-effort: a
// nil store or a read error skips the replay silently. Thinking is NOT included
// (design/16 §6) so the frame never leaks chain-of-thought to a connection whose
// role the gateway did not restrict on upgrade.
func (h *hub) replayHistory(c *wsConn, sessionID string) {
if h.history == nil {
return
}
msgs, err := h.history.Recent(context.Background(), sessionID, wsReplayLimit)
if err != nil {
h.log.Warn("history replay", "err", err, "session", sessionID)
return
}
out := make([]historyMessage, 0, len(msgs))
for _, m := range msgs {
wire := toHistoryMessage(m)
wire.Thinking = ""
out = append(out, wire)
}
c.enqueueJSON(wsEvent{Type: "history", SessionID: sessionID, Data: map[string]any{"messages": out}})
}