diff --git a/client/knowledge.go b/client/knowledge.go index 99ed14dc..10119abc 100644 --- a/client/knowledge.go +++ b/client/knowledge.go @@ -353,7 +353,9 @@ func (c *Client) ListKnowledgeWithFilter(ctx context.Context, return response.Data, response.Total, nil } -// DeleteKnowledge deletes a knowledge entry by its ID +// DeleteKnowledge enqueues an asynchronous delete for the given knowledge entry. +// The server returns 200 once the task has been submitted; the actual deletion is +// performed by a background worker (same pipeline as BatchDeleteKnowledge). func (c *Client) DeleteKnowledge(ctx context.Context, knowledgeID string) error { path := fmt.Sprintf("/api/v1/knowledge/%s", knowledgeID) resp, err := c.doRequest(ctx, http.MethodDelete, path, nil, nil) diff --git a/docs/docs.go b/docs/docs.go index 4abaf5f8..c6a0fed5 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -5926,7 +5926,7 @@ const docTemplate = `{ "ApiKeyAuth": [] } ], - "description": "根据ID删除知识条目", + "description": "根据ID异步删除知识条目。请求会被入队到与批量删除相同的异步管道(asynq);\n接口返回 200 仅表示任务已提交(响应 data.task_id 为任务 ID),实际删除由后台 worker 完成。", "consumes": [ "application/json" ], @@ -5948,7 +5948,7 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "删除成功", + "description": "任务已提交,返回 task_id", "schema": { "type": "object", "additionalProperties": true diff --git a/docs/swagger.json b/docs/swagger.json index d1ee5297..732e88ed 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -5919,7 +5919,7 @@ "ApiKeyAuth": [] } ], - "description": "根据ID删除知识条目", + "description": "根据ID异步删除知识条目。请求会被入队到与批量删除相同的异步管道(asynq);\n接口返回 200 仅表示任务已提交(响应 data.task_id 为任务 ID),实际删除由后台 worker 完成。", "consumes": [ "application/json" ], @@ -5941,7 +5941,7 @@ ], "responses": { "200": { - "description": "删除成功", + "description": "任务已提交,返回 task_id", "schema": { "type": "object", "additionalProperties": true diff --git a/docs/swagger.yaml b/docs/swagger.yaml index a7c6cb08..a81d9b7e 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -7901,7 +7901,9 @@ paths: delete: consumes: - application/json - description: 根据ID删除知识条目 + description: |- + 根据ID异步删除知识条目。请求会被入队到与批量删除相同的异步管道(asynq); + 接口返回 200 仅表示任务已提交(响应 data.task_id 为任务 ID),实际删除由后台 worker 完成。 parameters: - description: 知识ID in: path @@ -7912,7 +7914,7 @@ paths: - application/json responses: "200": - description: 删除成功 + description: 任务已提交,返回 task_id schema: additionalProperties: true type: object diff --git a/internal/handler/knowledge.go b/internal/handler/knowledge.go index a6821029..3e27a19f 100644 --- a/internal/handler/knowledge.go +++ b/internal/handler/knowledge.go @@ -651,12 +651,13 @@ func (h *KnowledgeHandler) ListKnowledge(c *gin.Context) { // DeleteKnowledge godoc // @Summary 删除知识 -// @Description 根据ID删除知识条目 +// @Description 根据ID异步删除知识条目。请求会被入队到与批量删除相同的异步管道(asynq); +// @Description 接口返回 200 仅表示任务已提交(响应 data.task_id 为任务 ID),实际删除由后台 worker 完成。 // @Tags 知识管理 // @Accept json // @Produce json // @Param id path string true "知识ID" -// @Success 200 {object} map[string]interface{} "删除成功" +// @Success 200 {object} map[string]interface{} "任务已提交,返回 task_id" // @Failure 400 {object} errors.AppError "请求参数错误" // @Security Bearer // @Security ApiKeyAuth @@ -678,18 +679,32 @@ func (h *KnowledgeHandler) DeleteKnowledge(c *gin.Context) { c.Error(err) return } - logger.Infof(ctx, "Deleting knowledge, ID: %s", secutils.SanitizeForLog(id)) - err = h.kgService.DeleteKnowledge(effCtx, id) - if err != nil { - logger.ErrorWithFields(ctx, err, nil) - c.Error(errors.NewInternalServerError(err.Error())) + + // Reuse the batch async pipeline so single-item delete shares the same + // hardening (asynq retries, business-aware queue routing, marking-as-deleting + // inside the worker) as BatchDeleteKnowledge / ClearKnowledgeBaseContents. + effectiveTenantID, _ := effCtx.Value(types.TenantIDContextKey).(uint64) + if effectiveTenantID == 0 { + logger.Error(ctx, "Effective tenant ID missing after access validation") + c.Error(errors.NewInternalServerError("tenant context unavailable")) return } - logger.Infof(ctx, "Knowledge deleted successfully, ID: %s", secutils.SanitizeForLog(id)) + logger.Infof(ctx, "Enqueuing knowledge delete, ID: %s", secutils.SanitizeForLog(id)) + taskID, err := h.enqueueKnowledgeListDelete(effCtx, effectiveTenantID, []string{id}) + if err != nil { + logger.Errorf(ctx, "Failed to enqueue knowledge delete task: %v", err) + c.Error(errors.NewInternalServerError("Failed to enqueue delete task")) + return + } + + logger.Infof(ctx, "Knowledge delete task enqueued: %s, knowledge_id: %s", taskID, secutils.SanitizeForLog(id)) c.JSON(http.StatusOK, gin.H{ "success": true, - "message": "Deleted successfully", + "message": "Delete task submitted", + "data": gin.H{ + "task_id": taskID, + }, }) }