feat: IM式跨端实时同步——message_persisted 广播 + 前端按id去重追加
- 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:
1 parent
bcc53d68f3
commit
1bf60c1ae4
9 files changed
+260
-38
No files matched your search
@@ -14,9 +14,11 @@ import (
|
||||
// orchestrator runs unchanged without a store (tests / an unwired gateway).
|
||||
|
||||
// persistUserMessages writes each RoleUser message of a submitted turn to the
|
||||
// history store so the raw user stream is durable before the turn resolves. It is
|
||||
// a no-op when no history store is wired. Rejected turns (empty, pending-full)
|
||||
// never reach it, so nothing vacuous is persisted.
|
||||
// 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). 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) {
|
||||
if o.history == nil {
|
||||
return
|
||||
@@ -29,23 +31,30 @@ func (o *Orchestrator) persistUserMessages(ctx context.Context, sessionID string
|
||||
if text == "" && image == "" {
|
||||
continue
|
||||
}
|
||||
if err := o.history.Append(ctx, history.Message{SessionID: sessionID, Role: history.RoleUser, Content: text, Image: image}); err != nil {
|
||||
full, err := o.history.AppendFull(ctx, history.Message{SessionID: sessionID, Role: history.RoleUser, Content: text, Image: image})
|
||||
if err != nil {
|
||||
o.log.Error("orchestrator persist user", "err", err, "session", sessionID)
|
||||
continue
|
||||
}
|
||||
o.notifyPersisted(full)
|
||||
}
|
||||
}
|
||||
|
||||
// persistAssistantReply writes the final assistant text of a settled turn to the
|
||||
// history store. 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.
|
||||
// history store, then notifies the transport (design/16 §3.3, §4.2). 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) {
|
||||
if o.history == nil || res == nil || res.Text == "" {
|
||||
return
|
||||
}
|
||||
if err := o.history.Append(context.Background(), history.Message{SessionID: sessionID, Role: history.RoleAssistant, Content: res.Text}); err != nil {
|
||||
full, err := o.history.AppendFull(context.Background(), history.Message{SessionID: sessionID, Role: history.RoleAssistant, Content: res.Text})
|
||||
if err != nil {
|
||||
o.log.Error("orchestrator persist assistant", "err", err, "session", sessionID)
|
||||
return
|
||||
}
|
||||
o.notifyPersisted(full)
|
||||
}
|
||||
|
||||
// messageTextImage returns the concatenated text and the first image URL of a
|
||||
|
||||
Reference in new issue
Block a user