From aaa1accebdf673b9603aaa555c5610a8fd7727d9 Mon Sep 17 00:00:00 2001 From: lyingbug Date: Thu, 15 Oct 2026 15:57:58 +0800 Subject: [PATCH] feat(agent): let the assistant delete and move documents in batches (#4319) * feat(agent): let the assistant delete and move documents in batches A batch target can now resolve each item: x-agent "items" names the list parameter and a read run once per item, and the approval lists every title under the knowledge base name. The list must be capped at MaxApprovalItems (20) by x-agent-maximum, which now also bounds string lists, so nothing approved goes unnamed. One item the user cannot read blocks the whole batch before any prompt. New operations: knowledge.batch_delete, knowledge.move (to another knowledge base, destination named on the card), knowledge.get_move_progress and knowledge_base.list_move_targets. The move and move-progress Swagger responses now show their data envelope. The approval card keeps line breaks in its description and renders trailing "- " lines as a scrollable list. * fix(agent): reconcile batch approvals with workspace operation updates * fix(agent): keep every batch approval item visible --- .../prompt_templates/agent_system_prompt.yaml | 2 +- docs/docs.go | 131 +++++++++++++++--- docs/swagger.json | 131 +++++++++++++++--- docs/swagger.yaml | 78 ++++++++++- .../ToolApprovalCard.description.test.mjs | 30 ++++ .../chat/components/ToolApprovalCard.vue | 19 ++- .../components/agent-interaction-card.less | 18 ++- internal/agent/operations/catalog.go | 64 +++++++-- internal/agent/operations/catalog_test.go | 48 +++++++ internal/agent/operations/spec.go | 50 ++++++- internal/agent/operations/spec_test.go | 34 +++++ internal/agent/tools/operations.go | 65 ++++++++- internal/agent/tools/operations_test.go | 67 ++++++++- internal/handler/knowledge.go | 33 +++-- internal/handler/knowledgebase.go | 4 +- internal/handler/swagger_bodies.go | 2 +- internal/modelcontext/tool_policy.go | 7 +- internal/types/task.go | 18 +-- 18 files changed, 704 insertions(+), 97 deletions(-) create mode 100644 frontend/src/views/chat/components/ToolApprovalCard.description.test.mjs diff --git a/config/prompt_templates/agent_system_prompt.yaml b/config/prompt_templates/agent_system_prompt.yaml index c51e437c..0a608523 100644 --- a/config/prompt_templates/agent_system_prompt.yaml +++ b/config/prompt_templates/agent_system_prompt.yaml @@ -155,7 +155,7 @@ templates: 2. Look before you change. Diagnose with read operations first — for a failed document, knowledge.get then knowledge.get_parse_progress — and tell the user what you found before proposing a fix. 3. Change only what was asked. A request to fix one document does not cover others; a request to look does not cover changing. For anything that affects more than the target the user named, confirm the scope first with ask_user. 4. Every write or destructive operation shows the user a confirmation card. Run one at a time so each card is clear. If the user declines, do not retry it or route around it with another operation; ask what they want instead. - 5. Asynchronous work (reparse, URL import, delete) is accepted, not finished. Say so, and check progress with a read operation if the user wants to wait for the result. + 5. Asynchronous work (reparse, URL import, move, delete) is accepted, not finished. Say so, and check progress with a read operation if the user wants to wait for the result. Questions about WeKnora itself: - For how a feature works, how to configure or deploy it, environment variables, the API or what changed in a release, search the documentation with search_weknora_docs and read the relevant section with read_weknora_doc before answering. It is the documentation of the version the user is running. - The documentation explains how WeKnora works; operations show how this workspace actually is. When the two seem to disagree, trust the operation result and say so. diff --git a/docs/docs.go b/docs/docs.go index d5b83bd5..541b8583 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -6705,6 +6705,7 @@ const docTemplate = `{ "parameters": [ { "type": "string", + "x-agent": "knowledge_base_id", "description": "源知识库 ID", "name": "id", "in": "path", @@ -6757,7 +6758,12 @@ const docTemplate = `{ "$ref": "#/definitions/github_com_Tencent_WeKnora_internal_errors.AppError" } } - } + }, + "x-agent": { + "id": "knowledge_base.list_move_targets", + "risk": "read" + }, + "x-agent-hint": "Knowledge bases the documents of this one can be moved to: same type and embedding model." } }, "/knowledge-bases/{id}/pin": { @@ -7668,7 +7674,20 @@ const docTemplate = `{ "$ref": "#/definitions/github_com_Tencent_WeKnora_internal_errors.AppError" } } - } + }, + "x-agent": { + "id": "knowledge.batch_delete", + "items": { + "as": "knowledge_id", + "label": "title", + "param": "knowledge_ids", + "target": "knowledge.get" + }, + "label": "name", + "risk": "destructive", + "target": "knowledge_base.get" + }, + "x-agent-hint": "Removes up to 20 documents of one knowledge base with their chunks, embeddings and graph data. Cannot be undone; the confirmation lists every document." } }, "/knowledge/batch-reparse": { @@ -7975,7 +7994,19 @@ const docTemplate = `{ "200": { "description": "任务信息", "schema": { - "$ref": "#/definitions/internal_handler.MoveKnowledgeResponse" + "allOf": [ + { + "$ref": "#/definitions/internal_handler.DataResponse" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/internal_handler.MoveKnowledgeResponse" + } + } + } + ] } }, "400": { @@ -7984,7 +8015,20 @@ const docTemplate = `{ "$ref": "#/definitions/github_com_Tencent_WeKnora_internal_errors.AppError" } } - } + }, + "x-agent": { + "id": "knowledge.move", + "items": { + "as": "knowledge_id", + "label": "title", + "param": "knowledge_ids", + "target": "knowledge.get" + }, + "label": "name", + "risk": "write", + "target": "knowledge_base.get" + }, + "x-agent-hint": "Moves up to 20 documents to another knowledge base of the same workspace; knowledge_base_id is the destination, from knowledge_base.list_move_targets. Runs in the background; follow with knowledge.get_move_progress." } }, "/knowledge/move/progress/{task_id}": { @@ -8008,6 +8052,7 @@ const docTemplate = `{ "parameters": [ { "type": "string", + "x-agent": true, "description": "移动任务 ID", "name": "task_id", "in": "path", @@ -8018,7 +8063,19 @@ const docTemplate = `{ "200": { "description": "进度信息", "schema": { - "$ref": "#/definitions/github_com_Tencent_WeKnora_internal_types.KnowledgeMoveProgress" + "allOf": [ + { + "$ref": "#/definitions/internal_handler.DataResponse" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/github_com_Tencent_WeKnora_internal_types.KnowledgeMoveProgress" + } + } + } + ] } }, "404": { @@ -8027,6 +8084,10 @@ const docTemplate = `{ "$ref": "#/definitions/github_com_Tencent_WeKnora_internal_errors.AppError" } } + }, + "x-agent": { + "id": "knowledge.get_move_progress", + "risk": "read" } } }, @@ -26303,29 +26364,39 @@ const docTemplate = `{ }, "error": { "description": "错误信息", - "type": "string" + "type": "string", + "x-agent": true }, "failed": { "description": "失败数", - "type": "integer" + "type": "integer", + "x-agent": true }, "message": { "description": "状态消息", - "type": "string" + "type": "string", + "x-agent": true }, "processed": { "description": "已处理数", - "type": "integer" + "type": "integer", + "x-agent": true }, "progress": { "description": "0-100", - "type": "integer" + "type": "integer", + "x-agent": true }, "source_kb_id": { "type": "string" }, "status": { - "$ref": "#/definitions/github_com_Tencent_WeKnora_internal_types.KBCloneTaskStatus" + "allOf": [ + { + "$ref": "#/definitions/github_com_Tencent_WeKnora_internal_types.KBCloneTaskStatus" + } + ], + "x-agent": true }, "target_kb_id": { "type": "string" @@ -26335,7 +26406,8 @@ const docTemplate = `{ }, "total": { "description": "总知识数", - "type": "integer" + "type": "integer", + "x-agent": true }, "updated_at": { "description": "最后更新时间", @@ -32612,13 +32684,18 @@ const docTemplate = `{ ], "properties": { "ids": { + "description": "Documents to delete.", "type": "array", "items": { "type": "string" - } + }, + "x-agent": "knowledge_ids", + "x-agent-maximum": "20" }, "kb_id": { - "type": "string" + "description": "Knowledge base the documents belong to.", + "type": "string", + "x-agent": "knowledge_base_id" } } }, @@ -34343,24 +34420,33 @@ const docTemplate = `{ ], "properties": { "knowledge_ids": { + "description": "Documents to move.", "type": "array", "minItems": 1, "items": { "type": "string" - } + }, + "x-agent": true, + "x-agent-maximum": "20" }, "mode": { + "description": "reuse_vectors keeps the embeddings; reparse processes the documents again.", "type": "string", "enum": [ "reuse_vectors", "reparse" - ] + ], + "x-agent": true }, "source_kb_id": { - "type": "string" + "description": "Knowledge base the documents are in now.", + "type": "string", + "x-agent": "source_knowledge_base_id" }, "target_kb_id": { - "type": "string" + "description": "Knowledge base to move them to.", + "type": "string", + "x-agent": "knowledge_base_id" } } }, @@ -34368,7 +34454,8 @@ const docTemplate = `{ "type": "object", "properties": { "knowledge_count": { - "type": "integer" + "type": "integer", + "x-agent": true }, "message": { "type": "string" @@ -34380,7 +34467,8 @@ const docTemplate = `{ "type": "string" }, "task_id": { - "type": "string" + "type": "string", + "x-agent": true } } }, @@ -35168,7 +35256,8 @@ const docTemplate = `{ "type": "object", "properties": { "deleted_count": { - "type": "integer" + "type": "integer", + "x-agent": true }, "reparse_count": { "type": "integer", diff --git a/docs/swagger.json b/docs/swagger.json index 5aacbea5..b7ff0aa8 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -6698,6 +6698,7 @@ "parameters": [ { "type": "string", + "x-agent": "knowledge_base_id", "description": "源知识库 ID", "name": "id", "in": "path", @@ -6750,7 +6751,12 @@ "$ref": "#/definitions/github_com_Tencent_WeKnora_internal_errors.AppError" } } - } + }, + "x-agent": { + "id": "knowledge_base.list_move_targets", + "risk": "read" + }, + "x-agent-hint": "Knowledge bases the documents of this one can be moved to: same type and embedding model." } }, "/knowledge-bases/{id}/pin": { @@ -7661,7 +7667,20 @@ "$ref": "#/definitions/github_com_Tencent_WeKnora_internal_errors.AppError" } } - } + }, + "x-agent": { + "id": "knowledge.batch_delete", + "items": { + "as": "knowledge_id", + "label": "title", + "param": "knowledge_ids", + "target": "knowledge.get" + }, + "label": "name", + "risk": "destructive", + "target": "knowledge_base.get" + }, + "x-agent-hint": "Removes up to 20 documents of one knowledge base with their chunks, embeddings and graph data. Cannot be undone; the confirmation lists every document." } }, "/knowledge/batch-reparse": { @@ -7968,7 +7987,19 @@ "200": { "description": "任务信息", "schema": { - "$ref": "#/definitions/internal_handler.MoveKnowledgeResponse" + "allOf": [ + { + "$ref": "#/definitions/internal_handler.DataResponse" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/internal_handler.MoveKnowledgeResponse" + } + } + } + ] } }, "400": { @@ -7977,7 +8008,20 @@ "$ref": "#/definitions/github_com_Tencent_WeKnora_internal_errors.AppError" } } - } + }, + "x-agent": { + "id": "knowledge.move", + "items": { + "as": "knowledge_id", + "label": "title", + "param": "knowledge_ids", + "target": "knowledge.get" + }, + "label": "name", + "risk": "write", + "target": "knowledge_base.get" + }, + "x-agent-hint": "Moves up to 20 documents to another knowledge base of the same workspace; knowledge_base_id is the destination, from knowledge_base.list_move_targets. Runs in the background; follow with knowledge.get_move_progress." } }, "/knowledge/move/progress/{task_id}": { @@ -8001,6 +8045,7 @@ "parameters": [ { "type": "string", + "x-agent": true, "description": "移动任务 ID", "name": "task_id", "in": "path", @@ -8011,7 +8056,19 @@ "200": { "description": "进度信息", "schema": { - "$ref": "#/definitions/github_com_Tencent_WeKnora_internal_types.KnowledgeMoveProgress" + "allOf": [ + { + "$ref": "#/definitions/internal_handler.DataResponse" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/github_com_Tencent_WeKnora_internal_types.KnowledgeMoveProgress" + } + } + } + ] } }, "404": { @@ -8020,6 +8077,10 @@ "$ref": "#/definitions/github_com_Tencent_WeKnora_internal_errors.AppError" } } + }, + "x-agent": { + "id": "knowledge.get_move_progress", + "risk": "read" } } }, @@ -26296,29 +26357,39 @@ }, "error": { "description": "错误信息", - "type": "string" + "type": "string", + "x-agent": true }, "failed": { "description": "失败数", - "type": "integer" + "type": "integer", + "x-agent": true }, "message": { "description": "状态消息", - "type": "string" + "type": "string", + "x-agent": true }, "processed": { "description": "已处理数", - "type": "integer" + "type": "integer", + "x-agent": true }, "progress": { "description": "0-100", - "type": "integer" + "type": "integer", + "x-agent": true }, "source_kb_id": { "type": "string" }, "status": { - "$ref": "#/definitions/github_com_Tencent_WeKnora_internal_types.KBCloneTaskStatus" + "allOf": [ + { + "$ref": "#/definitions/github_com_Tencent_WeKnora_internal_types.KBCloneTaskStatus" + } + ], + "x-agent": true }, "target_kb_id": { "type": "string" @@ -26328,7 +26399,8 @@ }, "total": { "description": "总知识数", - "type": "integer" + "type": "integer", + "x-agent": true }, "updated_at": { "description": "最后更新时间", @@ -32605,13 +32677,18 @@ ], "properties": { "ids": { + "description": "Documents to delete.", "type": "array", "items": { "type": "string" - } + }, + "x-agent": "knowledge_ids", + "x-agent-maximum": "20" }, "kb_id": { - "type": "string" + "description": "Knowledge base the documents belong to.", + "type": "string", + "x-agent": "knowledge_base_id" } } }, @@ -34336,24 +34413,33 @@ ], "properties": { "knowledge_ids": { + "description": "Documents to move.", "type": "array", "minItems": 1, "items": { "type": "string" - } + }, + "x-agent": true, + "x-agent-maximum": "20" }, "mode": { + "description": "reuse_vectors keeps the embeddings; reparse processes the documents again.", "type": "string", "enum": [ "reuse_vectors", "reparse" - ] + ], + "x-agent": true }, "source_kb_id": { - "type": "string" + "description": "Knowledge base the documents are in now.", + "type": "string", + "x-agent": "source_knowledge_base_id" }, "target_kb_id": { - "type": "string" + "description": "Knowledge base to move them to.", + "type": "string", + "x-agent": "knowledge_base_id" } } }, @@ -34361,7 +34447,8 @@ "type": "object", "properties": { "knowledge_count": { - "type": "integer" + "type": "integer", + "x-agent": true }, "message": { "type": "string" @@ -34373,7 +34460,8 @@ "type": "string" }, "task_id": { - "type": "string" + "type": "string", + "x-agent": true } } }, @@ -35161,7 +35249,8 @@ "type": "object", "properties": { "deleted_count": { - "type": "integer" + "type": "integer", + "x-agent": true }, "reparse_count": { "type": "integer", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 15b67b54..23e54d53 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -4084,22 +4084,29 @@ definitions: error: description: 错误信息 type: string + x-agent: true failed: description: 失败数 type: integer + x-agent: true message: description: 状态消息 type: string + x-agent: true processed: description: 已处理数 type: integer + x-agent: true progress: description: 0-100 type: integer + x-agent: true source_kb_id: type: string status: - $ref: '#/definitions/github_com_Tencent_WeKnora_internal_types.KBCloneTaskStatus' + allOf: + - $ref: '#/definitions/github_com_Tencent_WeKnora_internal_types.KBCloneTaskStatus' + x-agent: true target_kb_id: type: string task_id: @@ -4107,6 +4114,7 @@ definitions: total: description: 总知识数 type: integer + x-agent: true updated_at: description: 最后更新时间 type: integer @@ -9054,11 +9062,16 @@ definitions: internal_handler.BatchDeleteKnowledgeRequest: properties: ids: + description: Documents to delete. items: type: string type: array + x-agent: knowledge_ids + x-agent-maximum: "20" kb_id: + description: Knowledge base the documents belong to. type: string + x-agent: knowledge_base_id required: - ids - kb_id @@ -10271,19 +10284,29 @@ definitions: internal_handler.MoveKnowledgeRequest: properties: knowledge_ids: + description: Documents to move. items: type: string minItems: 1 type: array + x-agent: true + x-agent-maximum: "20" mode: + description: reuse_vectors keeps the embeddings; reparse processes the documents + again. enum: - reuse_vectors - reparse type: string + x-agent: true source_kb_id: + description: Knowledge base the documents are in now. type: string + x-agent: source_knowledge_base_id target_kb_id: + description: Knowledge base to move them to. type: string + x-agent: knowledge_base_id required: - knowledge_ids - mode @@ -10294,6 +10317,7 @@ definitions: properties: knowledge_count: type: integer + x-agent: true message: type: string source_kb_id: @@ -10302,6 +10326,7 @@ definitions: type: string task_id: type: string + x-agent: true type: object internal_handler.MoveKnowledgeToFolderRequest: properties: @@ -10849,6 +10874,7 @@ definitions: properties: deleted_count: type: integer + x-agent: true reparse_count: type: integer x-agent: true @@ -16041,6 +16067,7 @@ paths: name: id required: true type: string + x-agent: knowledge_base_id produces: - application/json responses: @@ -16077,6 +16104,11 @@ paths: summary: 获取可移动目标知识库列表 tags: - 知识库 + x-agent: + id: knowledge_base.list_move_targets + risk: read + x-agent-hint: 'Knowledge bases the documents of this one can be moved to: same + type and embedding model.' /knowledge-bases/{id}/pin: put: consumes: @@ -17067,6 +17099,19 @@ paths: summary: 批量删除知识 tags: - 知识管理 + x-agent: + id: knowledge.batch_delete + items: + as: knowledge_id + label: title + param: knowledge_ids + target: knowledge.get + label: name + risk: destructive + target: knowledge_base.get + x-agent-hint: Removes up to 20 documents of one knowledge base with their chunks, + embeddings and graph data. Cannot be undone; the confirmation lists every + document. /knowledge/batch-reparse: post: consumes: @@ -17253,7 +17298,12 @@ paths: "200": description: 任务信息 schema: - $ref: '#/definitions/internal_handler.MoveKnowledgeResponse' + allOf: + - $ref: '#/definitions/internal_handler.DataResponse' + - properties: + data: + $ref: '#/definitions/internal_handler.MoveKnowledgeResponse' + type: object "400": description: 请求参数错误 schema: @@ -17264,6 +17314,19 @@ paths: summary: 移动知识到其他知识库 tags: - 知识 + x-agent: + id: knowledge.move + items: + as: knowledge_id + label: title + param: knowledge_ids + target: knowledge.get + label: name + risk: write + target: knowledge_base.get + x-agent-hint: Moves up to 20 documents to another knowledge base of the same + workspace; knowledge_base_id is the destination, from knowledge_base.list_move_targets. + Runs in the background; follow with knowledge.get_move_progress. /knowledge/move/progress/{task_id}: get: description: 按任务 ID 查询移动进度 @@ -17273,13 +17336,19 @@ paths: name: task_id required: true type: string + x-agent: true produces: - application/json responses: "200": description: 进度信息 schema: - $ref: '#/definitions/github_com_Tencent_WeKnora_internal_types.KnowledgeMoveProgress' + allOf: + - $ref: '#/definitions/internal_handler.DataResponse' + - properties: + data: + $ref: '#/definitions/github_com_Tencent_WeKnora_internal_types.KnowledgeMoveProgress' + type: object "404": description: 任务不存在 schema: @@ -17290,6 +17359,9 @@ paths: summary: 获取知识移动进度 tags: - 知识 + x-agent: + id: knowledge.get_move_progress + risk: read /knowledge/search: get: consumes: diff --git a/frontend/src/views/chat/components/ToolApprovalCard.description.test.mjs b/frontend/src/views/chat/components/ToolApprovalCard.description.test.mjs new file mode 100644 index 00000000..0fd1d6bc --- /dev/null +++ b/frontend/src/views/chat/components/ToolApprovalCard.description.test.mjs @@ -0,0 +1,30 @@ +import assert from 'node:assert/strict' +import { readFileSync } from 'node:fs' +import { runInNewContext } from 'node:vm' +import test from 'node:test' + +const source = readFileSync(new URL('./ToolApprovalCard.vue', import.meta.url), 'utf8') +const styles = readFileSync(new URL('./agent-interaction-card.less', import.meta.url), 'utf8') +const descriptionCode = source.slice( + source.indexOf('const descriptionParts ='), source.indexOf('const isJsonValid ='), +).replace(' as string[]', '') +const parse = description => JSON.parse(JSON.stringify(runInNewContext( + descriptionCode + '\ndescriptionParts', { props: { description }, computed: fn => fn() }, +))) + +test('batch approval retains every title in the separate item list', () => { + const titles = Array.from({ length: 20 }, (_, i) => `Document ${i + 1}`) + const description = ['Delete documents', 'Finance', ...titles.map(title => '- ' + title)].join('\n') + assert.deepEqual(parse(description), { head: 'Delete documents\nFinance', items: titles }) +}) + +test('legacy multiline titles remain readable through the scrollable fallback', () => { + const description = 'Delete documents\nFinance\n- First\ncontinued title\n- Last document' + assert.deepEqual(parse(description), { head: description, items: [] }) + assert.match(source, /class="prompt-desc">\{\{ descriptionParts\.head \}\}/) + const fallback = styles.match(/\.prompt-desc \{([\s\S]*?)\n\}/)?.[1] + assert.ok(fallback) + assert.match(fallback, /white-space:\s*pre-line/) + assert.match(fallback, /overflow-y:\s*auto/) + assert.doesNotMatch(fallback, /line-clamp|overflow:\s*hidden/) +}) diff --git a/frontend/src/views/chat/components/ToolApprovalCard.vue b/frontend/src/views/chat/components/ToolApprovalCard.vue index 7218e5db..3e433f74 100644 --- a/frontend/src/views/chat/components/ToolApprovalCard.vue +++ b/frontend/src/views/chat/components/ToolApprovalCard.vue @@ -1,7 +1,10 @@