Spaces:
Sleeping
Sleeping
Commit
·
30e3a7e
1
Parent(s):
c78b8c1
fix the issues in the deleting kb
Browse files
backend/api/mcp_clients/rag_client.py
CHANGED
|
@@ -177,16 +177,29 @@ class RAGClient:
|
|
| 177 |
print("RAG List Error:", e)
|
| 178 |
return {"documents": [], "total": 0, "limit": limit, "offset": offset}
|
| 179 |
|
| 180 |
-
async def delete_document(self, tenant_id: str, document_id: int):
|
| 181 |
"""
|
| 182 |
Delete a specific document by ID for a tenant.
|
| 183 |
Returns the unwrapped data from the MCP server response.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
"""
|
| 185 |
try:
|
| 186 |
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
)
|
| 191 |
|
| 192 |
if response.status_code == 404:
|
|
@@ -205,6 +218,13 @@ class RAGClient:
|
|
| 205 |
# Check if MCP server returned an error response
|
| 206 |
if isinstance(data, dict) and data.get("status") == "error":
|
| 207 |
error_msg = data.get("message", "Unknown error")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
return {"error": error_msg}
|
| 209 |
|
| 210 |
# MCP server wraps response in a 'data' field
|
|
@@ -222,16 +242,28 @@ class RAGClient:
|
|
| 222 |
print(f"RAG Delete Error: {e}")
|
| 223 |
return {"error": str(e)}
|
| 224 |
|
| 225 |
-
async def delete_all_documents(self, tenant_id: str):
|
| 226 |
"""
|
| 227 |
Delete all documents for a tenant.
|
| 228 |
Returns the unwrapped data from the MCP server response.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
"""
|
| 230 |
try:
|
| 231 |
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
f"{self.base_url}/delete-all",
|
| 234 |
-
|
| 235 |
)
|
| 236 |
|
| 237 |
if response.status_code != 200:
|
|
|
|
| 177 |
print("RAG List Error:", e)
|
| 178 |
return {"documents": [], "total": 0, "limit": limit, "offset": offset}
|
| 179 |
|
| 180 |
+
async def delete_document(self, tenant_id: str, document_id: int, user_role: Optional[str] = None):
|
| 181 |
"""
|
| 182 |
Delete a specific document by ID for a tenant.
|
| 183 |
Returns the unwrapped data from the MCP server response.
|
| 184 |
+
|
| 185 |
+
Args:
|
| 186 |
+
tenant_id: Tenant identifier
|
| 187 |
+
document_id: Document ID to delete
|
| 188 |
+
user_role: User role (viewer, editor, admin, owner) - required for permission checks
|
| 189 |
"""
|
| 190 |
try:
|
| 191 |
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 192 |
+
# Use POST with JSON payload to include role for MCP protocol
|
| 193 |
+
payload = {
|
| 194 |
+
"tenant_id": tenant_id,
|
| 195 |
+
"document_id": document_id
|
| 196 |
+
}
|
| 197 |
+
if user_role:
|
| 198 |
+
payload["user_role"] = user_role
|
| 199 |
+
|
| 200 |
+
response = await client.post(
|
| 201 |
+
f"{self.base_url}/delete",
|
| 202 |
+
json=payload
|
| 203 |
)
|
| 204 |
|
| 205 |
if response.status_code == 404:
|
|
|
|
| 218 |
# Check if MCP server returned an error response
|
| 219 |
if isinstance(data, dict) and data.get("status") == "error":
|
| 220 |
error_msg = data.get("message", "Unknown error")
|
| 221 |
+
error_type = data.get("error_type", "unknown_error")
|
| 222 |
+
# For validation errors (like "not found"), return clear error message
|
| 223 |
+
if error_type == "validation_error":
|
| 224 |
+
if "not found" in error_msg.lower():
|
| 225 |
+
return {"error": f"Document {document_id} not found for tenant '{tenant_id}'"}
|
| 226 |
+
elif "access denied" in error_msg.lower() or "permission" in error_msg.lower():
|
| 227 |
+
return {"error": f"Access denied: {error_msg}"}
|
| 228 |
return {"error": error_msg}
|
| 229 |
|
| 230 |
# MCP server wraps response in a 'data' field
|
|
|
|
| 242 |
print(f"RAG Delete Error: {e}")
|
| 243 |
return {"error": str(e)}
|
| 244 |
|
| 245 |
+
async def delete_all_documents(self, tenant_id: str, user_role: Optional[str] = None):
|
| 246 |
"""
|
| 247 |
Delete all documents for a tenant.
|
| 248 |
Returns the unwrapped data from the MCP server response.
|
| 249 |
+
|
| 250 |
+
Args:
|
| 251 |
+
tenant_id: Tenant identifier
|
| 252 |
+
user_role: User role (viewer, editor, admin, owner) - required for permission checks
|
| 253 |
"""
|
| 254 |
try:
|
| 255 |
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 256 |
+
# Use POST with JSON payload to include role for MCP protocol
|
| 257 |
+
payload = {
|
| 258 |
+
"tenant_id": tenant_id,
|
| 259 |
+
"delete_all": True
|
| 260 |
+
}
|
| 261 |
+
if user_role:
|
| 262 |
+
payload["user_role"] = user_role
|
| 263 |
+
|
| 264 |
+
response = await client.post(
|
| 265 |
f"{self.base_url}/delete-all",
|
| 266 |
+
json=payload
|
| 267 |
)
|
| 268 |
|
| 269 |
if response.status_code != 200:
|
backend/api/routes/rag.py
CHANGED
|
@@ -389,7 +389,7 @@ async def rag_delete(
|
|
| 389 |
require_api_permission(x_user_role, "delete_documents")
|
| 390 |
|
| 391 |
try:
|
| 392 |
-
result = await rag_client.delete_document(x_tenant_id, document_id)
|
| 393 |
if "error" in result:
|
| 394 |
# Check if it's a connection error (500) or not found (404)
|
| 395 |
error_msg = result["error"]
|
|
@@ -419,7 +419,7 @@ async def rag_delete_all(
|
|
| 419 |
require_api_permission(x_user_role, "delete_documents")
|
| 420 |
|
| 421 |
try:
|
| 422 |
-
result = await rag_client.delete_all_documents(x_tenant_id)
|
| 423 |
if "error" in result:
|
| 424 |
error_msg = result["error"]
|
| 425 |
# Check if it's a connection error (503) or other error
|
|
|
|
| 389 |
require_api_permission(x_user_role, "delete_documents")
|
| 390 |
|
| 391 |
try:
|
| 392 |
+
result = await rag_client.delete_document(x_tenant_id, document_id, user_role=x_user_role)
|
| 393 |
if "error" in result:
|
| 394 |
# Check if it's a connection error (500) or not found (404)
|
| 395 |
error_msg = result["error"]
|
|
|
|
| 419 |
require_api_permission(x_user_role, "delete_documents")
|
| 420 |
|
| 421 |
try:
|
| 422 |
+
result = await rag_client.delete_all_documents(x_tenant_id, user_role=x_user_role)
|
| 423 |
if "error" in result:
|
| 424 |
error_msg = result["error"]
|
| 425 |
# Check if it's a connection error (503) or other error
|