refactor(knowledge): route single delete through async pipeline

Reuse enqueueKnowledgeListDelete inside DeleteKnowledge so that single-item
delete shares the same hardening as BatchDeleteKnowledge / ClearKnowledgeBase:
asynq retries, business-aware queue routing, and marking-as-deleting inside
the worker.

The endpoint now returns 200 once the delete task has been enqueued; the
response body carries the asynq task_id and the message is updated to
"Delete task submitted". Swagger annotations, generated docs and the Go
client SDK comment are updated to reflect the new asynchronous semantics.

Note: this is a behavior change. Callers that previously assumed the
knowledge was already gone on a 200 response should poll the task status
or accept eventual consistency, matching the existing BatchDeleteKnowledge
contract.
This commit is contained in:
wizardchen
2026-05-25 16:54:26 +08:00
committed by lyingbug
parent af1f2b469a
commit d12fb42e60
5 changed files with 35 additions and 16 deletions
+3 -1
View File
@@ -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)
+2 -2
View File
@@ -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
+2 -2
View File
@@ -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
+4 -2
View File
@@ -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
+24 -9
View File
@@ -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,
},
})
}