Files
Agent-System/internal/server/chat_digest_api.go
T
weichenandCursor 4b8d8bf6d6 fix(chat): unify history read authorization and stats filtering
Apply session ownership plus scope_kind visibility across window, day,
search, dates, categories, unread, and digest endpoints; add PG refresh
regression test for pre-activation and task-scoped messages.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-13 03:06:11 +00:00

112 lines
2.9 KiB
Go

package server
import (
"encoding/json"
"net/http"
"strings"
"chat-agent-system/internal/ctxmgr"
)
// handleListDailyDigests 获取会话的历史每日摘要列表 (时间轴侧栏数据源)
// GET /api/v1/chat/digests?session_id=&from=&to=
func (s *Server) handleListDailyDigests(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
if s.svc == nil || s.svc.ContextManager() == nil {
http.Error(w, "context manager not initialized", http.StatusServiceUnavailable)
return
}
user, ok := s.requireAuthenticatedUser(w, r)
if !ok {
return
}
sessionID := strings.TrimSpace(r.URL.Query().Get("session_id"))
if sessionID == "" {
http.Error(w, "missing required query param 'session_id'", http.StatusBadRequest)
return
}
if _, ok := s.authorizeSessionRead(w, r, sessionID, user); !ok {
return
}
from := strings.TrimSpace(r.URL.Query().Get("from"))
to := strings.TrimSpace(r.URL.Query().Get("to"))
digests, err := s.svc.ContextManager().ListDailyDigests(r.Context(), sessionID, from, to)
if err != nil {
http.Error(w, "ListDailyDigests error: "+err.Error(), http.StatusInternalServerError)
return
}
if digests == nil {
digests = []ctxmgr.DailyDigest{}
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(digests)
}
// GenerateDigestRequest 手动触发单日摘要生成请求体
type GenerateDigestRequest struct {
SessionID string `json:"session_id"`
DayKey string `json:"day_key"`
}
// handleGenerateDailyDigest 手动触发或重试生成单日摘要
// POST /api/v1/chat/digests/generate
func (s *Server) handleGenerateDailyDigest(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
if s.svc == nil || s.svc.ContextManager() == nil {
http.Error(w, "context manager not initialized", http.StatusServiceUnavailable)
return
}
user, ok := s.requireAuthenticatedUser(w, r)
if !ok {
return
}
var req GenerateDigestRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "invalid request json body: "+err.Error(), http.StatusBadRequest)
return
}
req.SessionID = strings.TrimSpace(req.SessionID)
req.DayKey = strings.TrimSpace(req.DayKey)
if req.SessionID == "" || req.DayKey == "" {
http.Error(w, "missing required fields 'session_id' or 'day_key'", http.StatusBadRequest)
return
}
if _, ok := s.authorizeSessionRead(w, r, req.SessionID, user); !ok {
return
}
digest, err := s.svc.ContextManager().GenerateDailyDigest(r.Context(), req.SessionID, req.DayKey)
if err != nil {
http.Error(w, "GenerateDailyDigest error: "+err.Error(), http.StatusInternalServerError)
return
}
resp := map[string]any{
"queued": true,
"digest": digest,
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(resp)
}