Files
Agent-System/internal/server/chatflow_http.go
T
weichenandCursor 65bdda8aae feat(api): route chat history reads through ChatLog
Ignore request user_id as identity, authorize from the session Principal, and hydrate restore/memory from the same visible set so HTTP, SSE, and prompt assembly cannot amplify hidden messages.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-15 04:11:27 +00:00

38 lines
1.2 KiB
Go

package server
import (
"errors"
"net/http"
"chat-agent-system/internal/app/chatflow"
"chat-agent-system/internal/auth"
)
func (s *Server) chatPrincipal(user *auth.UserAccount, action chatflow.Action) chatflow.Principal {
if user == nil {
return chatflow.Principal{Action: action}
}
return chatflow.Principal{UserID: user.ID, Action: action}
}
func (s *Server) writeChatLogError(w http.ResponseWriter, err error) {
switch {
case errors.Is(err, chatflow.ErrUnauthenticated):
http.Error(w, `{"error":"ERR_UNAUTHENTICATED","message":"请先登录"}`, http.StatusUnauthorized)
case errors.Is(err, chatflow.ErrForbidden):
writeJSONResponse(w, http.StatusForbidden, map[string]string{
"error_code": "SESSION_FORBIDDEN",
"message": "无权访问该会话",
})
case errors.Is(err, chatflow.ErrNotFound):
writeJSONResponse(w, http.StatusNotFound, map[string]string{
"error_code": "SESSION_NOT_FOUND",
"message": "指定会话不存在或已被删除",
})
case errors.Is(err, chatflow.ErrClosed):
http.Error(w, "context manager not initialized", http.StatusServiceUnavailable)
default:
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}