feat: IM式跨端实时同步——message_persisted 广播 + 前端按id去重追加
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled

- history.Store 新增 AppendFull(返回含 id/createdAt 的完整消息),Append 签名不变
- session_orchestrator 加可空 OnPersisted;persistUserMessages/persistAssistantReply 落库后回调广播
- server.go newOrchestrator 接 OnPersisted → hub.broadcast message_persisted(thinking 剥除)
- 前端 ws.ts 加 message_persisted case(仅 activeSessionId 匹配才处理);appendOrUpdateMessage 升级为 id 精确upsert+内容/时间归一
- 实测:双 WS,A stream 发消息,B 免刷新收到 user+assistant 各1条(count=2 PASS)
- 红线: 身份仍靠 actor 元数据,不靠提示词;用户/助手消息、本端/异端统一路径
This commit is contained in:
Aska committed 2026-09-07 04:16:29 +08:00
1 parent bcc53d68f3
commit 1bf60c1ae4
9 files changed
+260 -38

No files matched your search

@@ -7,6 +7,7 @@ import (
"sync"
"time"
"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"
)
@@ -108,6 +109,11 @@ type OrchestratorOptions struct {
// When nil every Append is skipped, so the orchestrator runs without storage
// (tests / a gateway built without a history store).
History HistoryStore
// OnPersisted, when non-nil, is called with the complete stored message right
// after a user or assistant message is written, so the transport can broadcast
// a `message_persisted` event for cross-end realtime sync (design/16 §4.2).
// Dropped when nil (tests / a gateway built without a transport hook).
OnPersisted func(history.Message)
}
// sessionState is the per-session message state (design/15 §2). Every mutation
@@ -115,7 +121,7 @@ type OrchestratorOptions struct {
// lock so a slow model call never blocks Submit.
type sessionState struct {
mode Mode
gen uint64 // generation: bumped on interrupt and on a fresh top-level turn
gen uint64 // generation: bumped on interrupt and on a fresh top-level turn
cancel context.CancelFunc
pending []pendingMsg
tail []model.Message // bounded conversation tail for context continuation
@@ -134,14 +140,15 @@ type pendingMsg struct {
// actual run off to a goroutine. It is the single owner of "is this session busy"
// for the chat path; the loop/task/subagent engines are reused, not re-created.
type Orchestrator struct {
mu sync.Mutex
states map[string]*sessionState
run TurnRunner
emit StateSink
log *slog.Logger
history HistoryStore
maxPending int
maxTail int
mu sync.Mutex
states map[string]*sessionState
run TurnRunner
emit StateSink
log *slog.Logger
history HistoryStore
onPersisted func(history.Message)
maxPending int
maxTail int
}
// NewOrchestrator builds an orchestrator from opts. Run is required: it is the
@@ -161,13 +168,14 @@ func NewOrchestrator(opts OrchestratorOptions) *Orchestrator {
opts.Log = slog.New(slog.NewTextHandler(io.Discard, nil))
}
return &Orchestrator{
states: make(map[string]*sessionState),
run: opts.Run,
emit: opts.Emit,
log: opts.Log,
history: opts.History,
maxPending: maxPending,
maxTail: maxTail,
states: make(map[string]*sessionState),
run: opts.Run,
emit: opts.Emit,
log: opts.Log,
history: opts.History,
onPersisted: opts.OnPersisted,
maxPending: maxPending,
maxTail: maxTail,
}
}
@@ -414,6 +422,14 @@ func (o *Orchestrator) emitState(sessionID, typ string, data map[string]any) {
}
}
// notifyPersisted hands a just-persisted message to the transport hook when one is
// wired, so the gateway can broadcast `message_persisted` for cross-end sync.
func (o *Orchestrator) notifyPersisted(msg history.Message) {
if o.onPersisted != nil {
o.onPersisted(msg)
}
}
// boundedTail returns a fresh copy of the last maxTail messages, so continuation
// stays incremental and bounded (design/15 §2.2, §4) and never aliases the input
// slice or another state's tail.