feat(chat): add batch management for conversations

This commit is contained in:
AndyYang
2026-02-25 11:37:36 +08:00
committed by lyingbug
parent b536e86cd0
commit 172bf797ee
13 changed files with 610 additions and 1 deletions
@@ -87,3 +87,11 @@ func (r *sessionRepository) Update(ctx context.Context, session *types.Session)
func (r *sessionRepository) Delete(ctx context.Context, tenantID uint64, id string) error {
return r.db.WithContext(ctx).Where("tenant_id = ?", tenantID).Delete(&types.Session{}, "id = ?", id).Error
}
// BatchDelete deletes multiple sessions by IDs
func (r *sessionRepository) BatchDelete(ctx context.Context, tenantID uint64, ids []string) error {
if len(ids) == 0 {
return nil
}
return r.db.WithContext(ctx).Where("tenant_id = ? AND id IN ?", tenantID, ids).Delete(&types.Session{}).Error
}
+32
View File
@@ -225,6 +225,38 @@ func (s *sessionService) DeleteSession(ctx context.Context, id string) error {
return nil
}
// BatchDeleteSessions deletes multiple sessions by IDs
func (s *sessionService) BatchDeleteSessions(ctx context.Context, ids []string) error {
if len(ids) == 0 {
logger.Error(ctx, "Failed to batch delete sessions: IDs list is empty")
return errors.New("session ids are required")
}
// Get tenant ID from context
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
// Cleanup associated resources for each session
for _, id := range ids {
if err := s.webSearchStateRepo.DeleteWebSearchTempKBState(ctx, id); err != nil {
logger.Warnf(ctx, "Failed to cleanup temporary KB for session %s: %v", id, err)
}
if err := s.sessionStorage.Delete(ctx, id); err != nil {
logger.Warnf(ctx, "Failed to cleanup conversation context for session %s: %v", id, err)
}
}
// Batch delete sessions from repository
if err := s.sessionRepo.BatchDelete(ctx, tenantID, ids); err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
"session_ids": ids,
"tenant_id": tenantID,
})
return err
}
return nil
}
// GenerateTitle generates a title for the current conversation content
// modelID: optional model ID to use for title generation (if empty, uses first available KnowledgeQA model)
func (s *sessionService) GenerateTitle(ctx context.Context,
+53
View File
@@ -303,3 +303,56 @@ func (h *Handler) DeleteSession(c *gin.Context) {
"message": "Session deleted successfully",
})
}
// batchDeleteRequest represents the request body for batch deleting sessions
type batchDeleteRequest struct {
IDs []string `json:"ids" binding:"required,min=1"`
}
// BatchDeleteSessions godoc
// @Summary 批量删除会话
// @Description 根据ID列表批量删除对话会话
// @Tags 会话
// @Accept json
// @Produce json
// @Param request body batchDeleteRequest true "批量删除请求"
// @Success 200 {object} map[string]interface{} "删除结果"
// @Failure 400 {object} errors.AppError "请求参数错误"
// @Security Bearer
// @Security ApiKeyAuth
// @Router /sessions/batch [delete]
func (h *Handler) BatchDeleteSessions(c *gin.Context) {
ctx := c.Request.Context()
var req batchDeleteRequest
if err := c.ShouldBindJSON(&req); err != nil {
logger.Errorf(ctx, "Invalid batch delete request: %v", err)
c.Error(errors.NewBadRequestError("invalid request: ids are required"))
return
}
// Sanitize all IDs
sanitizedIDs := make([]string, 0, len(req.IDs))
for _, id := range req.IDs {
sanitized := secutils.SanitizeForLog(id)
if sanitized != "" {
sanitizedIDs = append(sanitizedIDs, sanitized)
}
}
if len(sanitizedIDs) == 0 {
c.Error(errors.NewBadRequestError("no valid session IDs provided"))
return
}
if err := h.sessionService.BatchDeleteSessions(ctx, sanitizedIDs); err != nil {
logger.ErrorWithFields(ctx, err, nil)
c.Error(errors.NewInternalServerError(err.Error()))
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "Sessions deleted successfully",
})
}
+1
View File
@@ -266,6 +266,7 @@ func RegisterSessionRoutes(r *gin.RouterGroup, handler *session.Handler) {
sessions := r.Group("/sessions")
{
sessions.POST("", handler.CreateSession)
sessions.DELETE("/batch", handler.BatchDeleteSessions)
sessions.GET("/:id", handler.GetSession)
sessions.GET("", handler.GetSessionsByTenant)
sessions.PUT("/:id", handler.UpdateSession)
+4
View File
@@ -21,6 +21,8 @@ type SessionService interface {
UpdateSession(ctx context.Context, session *types.Session) error
// DeleteSession deletes a session
DeleteSession(ctx context.Context, id string) error
// BatchDeleteSessions deletes multiple sessions by IDs
BatchDeleteSessions(ctx context.Context, ids []string) error
// GenerateTitle generates a title for the current conversation
// modelID: optional model ID to use for title generation (if empty, uses first available KnowledgeQA model)
GenerateTitle(ctx context.Context, session *types.Session, messages []types.Message, modelID string) (string, error)
@@ -79,4 +81,6 @@ type SessionRepository interface {
Update(ctx context.Context, session *types.Session) error
// Delete deletes a session
Delete(ctx context.Context, tenantID uint64, id string) error
// BatchDelete deletes multiple sessions by IDs
BatchDelete(ctx context.Context, tenantID uint64, ids []string) error
}