- 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
63 lines
2.4 KiB
Go
63 lines
2.4 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).
|
|
|
|
// 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.
|
|
func (o *Orchestrator) persistUserMessages(ctx context.Context, sessionID string, messages []model.Message) {
|
|
if o.history == nil {
|
|
return
|
|
}
|
|
for _, m := range messages {
|
|
if m.Role != model.RoleUser {
|
|
continue
|
|
}
|
|
text, image := messageTextImage(m)
|
|
if text == "" && image == "" {
|
|
continue
|
|
}
|
|
if err := o.history.Append(ctx, history.Message{SessionID: sessionID, Role: history.RoleUser, Content: text, Image: image}); err != nil {
|
|
o.log.Error("orchestrator persist user", "err", err, "session", sessionID)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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.
|
|
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 {
|
|
o.log.Error("orchestrator persist assistant", "err", err, "session", sessionID)
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|