feat: add deleteAllSessions functionality and update batch delete logic
- Introduced a new API function `deleteAllSessions` to allow deletion of all sessions for a tenant. - Updated the `BatchManageDialog` component to support a confirmation dialog for deleting all sessions. - Enhanced the menu component to reflect the new delete all functionality with appropriate confirmation messages. - Added internationalization support for the new delete all confirmation message in multiple languages. These changes improve session management by providing users with the ability to delete all sessions at once, enhancing the overall user experience.
This commit is contained in:
@@ -95,3 +95,8 @@ func (r *sessionRepository) BatchDelete(ctx context.Context, tenantID uint64, id
|
||||
}
|
||||
return r.db.WithContext(ctx).Where("tenant_id = ? AND id IN ?", tenantID, ids).Delete(&types.Session{}).Error
|
||||
}
|
||||
|
||||
// DeleteAllByTenantID deletes all sessions for a tenant
|
||||
func (r *sessionRepository) DeleteAllByTenantID(ctx context.Context, tenantID uint64) error {
|
||||
return r.db.WithContext(ctx).Where("tenant_id = ?", tenantID).Delete(&types.Session{}).Error
|
||||
}
|
||||
|
||||
@@ -260,6 +260,36 @@ func (s *sessionService) BatchDeleteSessions(ctx context.Context, ids []string)
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteAllSessions deletes all sessions for the current tenant
|
||||
func (s *sessionService) DeleteAllSessions(ctx context.Context) error {
|
||||
tenantID := types.MustTenantIDFromContext(ctx)
|
||||
logger.Infof(ctx, "Deleting all sessions for tenant %d", tenantID)
|
||||
|
||||
sessions, err := s.sessionRepo.GetByTenantID(ctx, tenantID)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "Failed to list sessions for cleanup: %v", err)
|
||||
} else {
|
||||
for _, session := range sessions {
|
||||
if err := s.webSearchStateRepo.DeleteWebSearchTempKBState(ctx, session.ID); err != nil {
|
||||
logger.Warnf(ctx, "Failed to cleanup temporary KB for session %s: %v", session.ID, err)
|
||||
}
|
||||
if err := s.sessionStorage.Delete(ctx, session.ID); err != nil {
|
||||
logger.Warnf(ctx, "Failed to cleanup conversation context for session %s: %v", session.ID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.sessionRepo.DeleteAllByTenantID(ctx, tenantID); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
"tenant_id": tenantID,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "All sessions deleted for tenant %d", tenantID)
|
||||
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,
|
||||
|
||||
@@ -306,12 +306,13 @@ func (h *Handler) DeleteSession(c *gin.Context) {
|
||||
|
||||
// batchDeleteRequest represents the request body for batch deleting sessions
|
||||
type batchDeleteRequest struct {
|
||||
IDs []string `json:"ids" binding:"required,min=1"`
|
||||
IDs []string `json:"ids"`
|
||||
DeleteAll bool `json:"delete_all"`
|
||||
}
|
||||
|
||||
// BatchDeleteSessions godoc
|
||||
// @Summary 批量删除会话
|
||||
// @Description 根据ID列表批量删除对话会话
|
||||
// @Description 根据ID列表批量删除对话会话,或设置 delete_all=true 删除当前租户的所有会话
|
||||
// @Tags 会话
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -327,7 +328,25 @@ func (h *Handler) BatchDeleteSessions(c *gin.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"))
|
||||
c.Error(errors.NewBadRequestError("invalid request"))
|
||||
return
|
||||
}
|
||||
|
||||
if req.DeleteAll {
|
||||
if err := h.sessionService.DeleteAllSessions(ctx); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": "All sessions deleted successfully",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if len(req.IDs) == 0 {
|
||||
c.Error(errors.NewBadRequestError("ids are required when delete_all is false"))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,8 @@ type SessionService interface {
|
||||
DeleteSession(ctx context.Context, id string) error
|
||||
// BatchDeleteSessions deletes multiple sessions by IDs
|
||||
BatchDeleteSessions(ctx context.Context, ids []string) error
|
||||
// DeleteAllSessions deletes all sessions for the current tenant
|
||||
DeleteAllSessions(ctx context.Context) 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)
|
||||
@@ -84,4 +86,6 @@ type SessionRepository interface {
|
||||
Delete(ctx context.Context, tenantID uint64, id string) error
|
||||
// BatchDelete deletes multiple sessions by IDs
|
||||
BatchDelete(ctx context.Context, tenantID uint64, ids []string) error
|
||||
// DeleteAllByTenantID deletes all sessions for a tenant
|
||||
DeleteAllByTenantID(ctx context.Context, tenantID uint64) error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user