feat(session,web): 会话角色不可变 — PATCH 拒 personaId + 移除前端换角色入口
design/22 §8-1(主人 2026-10-03 拍板):会话角色在创建时确定、之后不可变,
要换角色必须新开会话。
后端:
- PATCH /api/v1/sessions/{id} 的 personaId 改为 json.RawMessage 判「键出现」,
出现即 400(null/""/未知 id 一律拒),且先于会话存在性检查;错误消息点名 personaId
- 删除 Server.setSessionPersona 与 PATCH 的 501 分支;title/pinned 行为逐字不变
- sessionPersonaWriter 接口与 history.Store.SetSessionPersona 保留(存储能力)
前端:
- 移除会话行「切换角色」入口与面板「挂角色」下拉(含 setSessionPersona action、
lib/sessionPersona.ts);保留「新建会话选角色」;面板「角色」列改为只读展示
契约:Docs/api-client.md -> v0.46(破坏性:依赖 PATCH 换角色的客户端须改为新建会话)
验证:go build/vet ./... 绿;gateway+history 包测试绿(-count=1);前端 npm run build
(tsc --noEmit + vite build)绿;9 个前端逻辑测试全绿。
变异验证:改回「静默忽略」/「写绑定」两种反向实现,新断言均 FAIL。
This commit is contained in:
1 parent
f9430dee81
commit
c7ecc3a249
10 files changed
+193
-448
No files matched your search
@@ -19,15 +19,20 @@ import (
|
||||
// broadcast their outcome over the WS hub (message_edited / message_deleted, see
|
||||
// ws_hub.go) so the account's other browsers stay in sync without a manual re-read.
|
||||
|
||||
// patchSessionRequest is the PATCH /api/v1/sessions/{id} body. All fields are
|
||||
// patchSessionRequest is the PATCH /api/v1/sessions/{id} body. Both fields are
|
||||
// pointers so an absent field is distinguishable from an explicit zero value:
|
||||
// title renames the session (A4), pinned sets/unets the pin (D5), personaId
|
||||
// rebinds the session's role (design/22 §4.5, an explicit "" clears the binding
|
||||
// back to "inherit the global default"), and at least one must be present.
|
||||
// title renames the session (A4), pinned sets/unets the pin (D5), and at least one
|
||||
// must be present.
|
||||
//
|
||||
// personaId is deliberately NOT a settable field: a session's role is fixed when the
|
||||
// session is created (design/22 §8-1), so the key is rejected outright instead of
|
||||
// being ignored. It stays in the struct as the raw JSON so the handler can tell
|
||||
// "the key is absent" from "the key is present with any value" — `"yuki"`, `""` and
|
||||
// `null` are all refused, and only a body without the key is accepted.
|
||||
type patchSessionRequest struct {
|
||||
Title *string `json:"title"`
|
||||
Pinned *bool `json:"pinned"`
|
||||
PersonaID *string `json:"personaId"`
|
||||
Title *string `json:"title"`
|
||||
Pinned *bool `json:"pinned"`
|
||||
PersonaID json.RawMessage `json:"personaId"`
|
||||
}
|
||||
|
||||
// maxSessionTitleRunes bounds a client-supplied session title so a display row never
|
||||
@@ -36,14 +41,17 @@ type patchSessionRequest struct {
|
||||
const maxSessionTitleRunes = 80
|
||||
|
||||
// handlePatchSession updates a persistent session's display metadata — its title
|
||||
// (A4), its pinned flag (D5) — and/or its role binding (design/22 §4.5). All three
|
||||
// are metadata: unlike DELETE they are allowed on the fixed main session. A request
|
||||
// carrying none of the fields answers 400, as do an empty/whitespace or over-long
|
||||
// title and an unknown personaId; an unknown session id answers 404. Rebinding a
|
||||
// role does not touch the stored conversation — the messages keep the role that
|
||||
// spoke them (design/22 §7-5) — it only changes which role answers from the next
|
||||
// turn on. It is owner/admin-gated like the other history writes. The response
|
||||
// echoes only the fields that were changed, so a title-only PATCH keeps its pre-D5
|
||||
// (A4) and its pinned flag (D5). Both are metadata: unlike DELETE they are allowed
|
||||
// on the fixed main session. A request carrying neither field answers 400, as do an
|
||||
// empty/whitespace or over-long title and an unknown session id (404/500 as usual).
|
||||
//
|
||||
// A body carrying the personaId key answers 400 for EVERY value, and that check runs
|
||||
// before the session is looked up: a session's role is fixed at creation (design/22
|
||||
// §8-1), so switching the role of an existing conversation — including clearing it —
|
||||
// is not a supported operation; create a new session instead. Refusing explicitly
|
||||
// (rather than ignoring the key) is what keeps a client from believing a rebind
|
||||
// landed. It is owner/admin-gated like the other history writes. The response echoes
|
||||
// only the fields that were changed, so a title-only PATCH keeps its pre-D5
|
||||
// {id,title} shape.
|
||||
func (s *Server) handlePatchSession(w http.ResponseWriter, r *http.Request) {
|
||||
if s.history == nil {
|
||||
@@ -60,8 +68,13 @@ func (s *Server) handlePatchSession(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, apperr.Wrap(apperr.ErrBadRequest, "bad_request", "invalid request body"))
|
||||
return
|
||||
}
|
||||
if req.Title == nil && req.Pinned == nil && req.PersonaID == nil {
|
||||
writeError(w, apperr.Wrap(apperr.ErrBadRequest, "bad_request", "title, pinned or personaId is required"))
|
||||
if req.PersonaID != nil {
|
||||
writeError(w, apperr.Wrap(apperr.ErrBadRequest, "bad_request",
|
||||
"personaId cannot be changed: 会话角色在创建时确定,之后不可变,要换角色请新开会话 (design/22 §8-1)"))
|
||||
return
|
||||
}
|
||||
if req.Title == nil && req.Pinned == nil {
|
||||
writeError(w, apperr.Wrap(apperr.ErrBadRequest, "bad_request", "title or pinned is required"))
|
||||
return
|
||||
}
|
||||
resp := map[string]any{"id": id}
|
||||
@@ -88,43 +101,16 @@ func (s *Server) handlePatchSession(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
resp["pinned"] = *req.Pinned
|
||||
}
|
||||
if req.PersonaID != nil {
|
||||
personaID := strings.TrimSpace(*req.PersonaID)
|
||||
if !s.requireKnownPersona(w, personaID) {
|
||||
return
|
||||
}
|
||||
if err := s.setSessionPersona(r.Context(), id, personaID); err != nil {
|
||||
s.writeSessionPatchError(w, err, id, "set session persona")
|
||||
return
|
||||
}
|
||||
resp["personaId"] = personaID
|
||||
}
|
||||
writeJSON(w, http.StatusOK, resp)
|
||||
}
|
||||
|
||||
// setSessionPersona rebinds a session's role through the store's optional
|
||||
// role-writer. A store without it answers 501 rather than reporting a binding that
|
||||
// was never written.
|
||||
func (s *Server) setSessionPersona(ctx context.Context, sessionID, personaID string) error {
|
||||
writer, ok := s.history.(sessionPersonaWriter)
|
||||
if !ok {
|
||||
return errPersonaBindingUnsupported
|
||||
}
|
||||
return writer.SetSessionPersona(ctx, sessionID, personaID)
|
||||
}
|
||||
|
||||
// writeSessionPatchError maps a RenameSession/SetPinned/SetSessionPersona failure to
|
||||
// its response: ErrSessionNotFound → 404, a store that cannot bind roles → 501,
|
||||
// anything else → 500 (logged under op).
|
||||
// writeSessionPatchError maps a RenameSession/SetPinned failure to its response:
|
||||
// ErrSessionNotFound → 404, anything else → 500 (logged under op).
|
||||
func (s *Server) writeSessionPatchError(w http.ResponseWriter, err error, id, op string) {
|
||||
if errors.Is(err, history.ErrSessionNotFound) {
|
||||
writeError(w, apperr.Wrap(apperr.ErrNotFound, "not_found", "session %q not found", id))
|
||||
return
|
||||
}
|
||||
if errors.Is(err, errPersonaBindingUnsupported) {
|
||||
writeError(w, &apperr.AppError{Code: "not_implemented", Message: "会话角色绑定需要支持 personaId 的 HistoryStore", HTTPCode: http.StatusNotImplemented})
|
||||
return
|
||||
}
|
||||
s.log.Error(op, "err", err, "session", id)
|
||||
writeError(w, apperr.Wrap(apperr.ErrInternal, "session_failed", "failed to update session"))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user