File size: 1,501 Bytes
e44e5dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from __future__ import annotations

from typing import Mapping

from backend.mcp_server.common.database import delete_all_documents, delete_document
from backend.mcp_server.common.tenant import TenantContext
from backend.mcp_server.common.utils import ToolValidationError, tool_handler


@tool_handler("rag.delete")
async def rag_delete(context: TenantContext, payload: Mapping[str, object]) -> dict[str, object]:
    """
    Delete one document by ID or purge all documents for the tenant.
    """

    document_id = payload.get("document_id")
    delete_all = bool(payload.get("delete_all", False))

    if delete_all:
        deleted = delete_all_documents(context.tenant_id)
        return {
            "tenant_id": context.tenant_id,
            "deleted_count": deleted,
            "mode": "all",
        }

    if document_id is None:
        raise ToolValidationError("document_id is required unless delete_all=true")

    try:
        doc_id_value = int(document_id)
    except (TypeError, ValueError):
        raise ToolValidationError("document_id must be an integer")

    if doc_id_value <= 0:
        raise ToolValidationError("document_id must be positive")

    deleted = delete_document(context.tenant_id, doc_id_value)
    if not deleted:
        raise ToolValidationError(f"Document {doc_id_value} not found or access denied")
    
    return {
        "tenant_id": context.tenant_id,
        "document_id": doc_id_value,
        "deleted": True,
        "mode": "single",
    }