feat: add clear session messages functionality
- Implemented a new API endpoint to clear all messages in a session, enhancing session management capabilities. - Updated the frontend to include a clear messages option in the session menu, allowing users to easily remove chat history. - Added localization support for the new feature in English, Korean, Russian, and Chinese, improving accessibility for a broader audience. - Enhanced the chat view to handle session message clearing events, ensuring a responsive user experience. These changes significantly improve the user experience by providing a straightforward method to manage session messages effectively.
This commit is contained in:
@@ -318,6 +318,48 @@ func (h *Handler) DeleteSession(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// ClearSessionMessages godoc
|
||||
// @Summary 清空会话消息
|
||||
// @Description 删除会话中的所有消息,同时清除 LLM 上下文和聊天历史知识库条目。会话本身保留。
|
||||
// @Tags 会话
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "会话ID"
|
||||
// @Success 200 {object} map[string]interface{} "清空成功"
|
||||
// @Failure 400 {object} errors.AppError "请求参数错误"
|
||||
// @Failure 404 {object} errors.AppError "会话不存在"
|
||||
// @Security Bearer
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /sessions/{id}/messages [delete]
|
||||
func (h *Handler) ClearSessionMessages(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
id := secutils.SanitizeForLog(c.Param("id"))
|
||||
if id == "" {
|
||||
logger.Error(ctx, "Session ID is empty")
|
||||
c.Error(errors.NewBadRequestError(errors.ErrInvalidSessionID.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Clearing all messages for session: %s", id)
|
||||
|
||||
if err := h.messageService.ClearSessionMessages(ctx, id); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{"session_id": id})
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.sessionService.ClearContext(ctx, id); err != nil {
|
||||
logger.Warnf(ctx, "Failed to clear LLM context for session %s: %v", id, err)
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Session messages cleared successfully, ID: %s", id)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": "Session messages cleared successfully",
|
||||
})
|
||||
}
|
||||
|
||||
// batchDeleteRequest represents the request body for batch deleting sessions
|
||||
type batchDeleteRequest struct {
|
||||
IDs []string `json:"ids"`
|
||||
|
||||
Reference in New Issue
Block a user