File size: 11,752 Bytes
c16e1c9
 
d1e5882
c16e1c9
 
 
 
 
 
 
 
 
 
0452a50
345b8ff
 
c16e1c9
aa63765
c16e1c9
 
 
 
9d50a01
c16e1c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d50a01
 
 
 
 
 
 
 
 
 
c16e1c9
 
 
 
aa63765
 
 
 
e44e5dd
aa63765
d1e5882
 
 
 
 
 
 
484cae8
 
d1e5882
 
 
 
484cae8
 
 
 
 
 
 
d1e5882
aa63765
 
 
d1e5882
 
 
 
 
484cae8
 
 
 
d1e5882
 
 
 
 
 
aa63765
 
d1e5882
aa63765
 
 
484cae8
 
 
 
 
 
 
 
aa63765
 
e44e5dd
 
 
 
 
 
 
 
 
 
 
aa63765
 
484cae8
 
 
 
 
 
 
 
 
 
aa63765
484cae8
 
 
 
 
 
aa63765
 
 
 
e44e5dd
aa63765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e44e5dd
 
 
 
 
 
 
aa63765
 
 
 
 
345b8ff
30e3a7e
345b8ff
 
e44e5dd
30e3a7e
 
 
 
 
345b8ff
 
 
30e3a7e
 
 
 
 
 
 
 
 
 
 
345b8ff
 
 
 
 
 
 
 
 
 
 
 
 
 
e44e5dd
 
 
 
30e3a7e
 
 
 
 
 
 
e44e5dd
 
 
 
 
 
 
 
345b8ff
 
 
 
 
 
 
 
 
30e3a7e
345b8ff
 
e44e5dd
30e3a7e
 
 
 
345b8ff
 
 
30e3a7e
 
 
 
 
 
 
 
 
345b8ff
30e3a7e
345b8ff
 
 
 
 
 
 
 
 
 
 
 
e44e5dd
 
 
 
 
 
 
 
 
 
 
 
345b8ff
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import os
import httpx
from typing import Optional, Dict, Any
from dotenv import load_dotenv

load_dotenv()

class RAGClient:
    """
    Communicates with the RAG MCP server.
    """

    def __init__(self):
        self.base_url = os.getenv("RAG_MCP_URL", "http://localhost:8900/rag")
        if not self.base_url:
            raise ValueError("RAG_MCP_URL environment variable is not set")
        self.search_endpoint = f"{self.base_url}/search"
        self.ingest_endpoint = f"{self.base_url}/ingest"

    async def search(self, query: str, tenant_id: str):
        """
        Sends the query to the RAG server and returns document chunks.
        Unwraps MCP server responses automatically.
        """

        try:
            async with httpx.AsyncClient() as client:
                response = await client.post(
                    self.search_endpoint,
                    json={
                        "query": query,
                        "tenant_id": tenant_id
                    }
                )

            if response.status_code != 200:
                return []

            data = response.json()

            if isinstance(data, dict) and data.get("status") == "error":
                print("RAG Client Error:", data.get("message"))
                return []

            if isinstance(data, dict) and "data" in data:
                payload = data["data"]
                return payload.get("results", []) if isinstance(payload, dict) else payload

            return data.get("results", []) if isinstance(data, dict) else data

        except Exception as e:
            print("RAG Client Error:", e)
            return []

    async def ingest(self, content: str, tenant_id: str):
        """
        Sends content to the RAG server for ingestion.
        Returns the unwrapped data from the MCP server response.
        """
        return await self.ingest_with_metadata(content, tenant_id, metadata=None, doc_id=None)

    async def ingest_with_metadata(
        self,
        content: str,
        tenant_id: str,
        metadata: Optional[Dict[str, Any]] = None,
        doc_id: Optional[str] = None,
        user_role: Optional[str] = None
    ):
        """
        Sends content to the RAG server for ingestion with metadata.
        Returns the unwrapped data from the MCP server response.
        
        Args:
            content: Text content to ingest
            tenant_id: Tenant identifier
            metadata: Optional metadata dictionary
            doc_id: Optional document ID
            user_role: User role (viewer, editor, admin, owner) - required for permission checks
        """

        try:
            async with httpx.AsyncClient() as client:
                payload = {
                    "tenant_id": tenant_id,
                    "content": content
                }
                
                # Add role to payload (MCP server expects it for permission checks)
                if user_role:
                    payload["user_role"] = user_role
                
                # Add metadata if provided
                if metadata:
                    payload["metadata"] = metadata
                if doc_id:
                    payload["doc_id"] = doc_id
                
                response = await client.post(
                    self.ingest_endpoint,
                    json=payload
                )

            if response.status_code != 200:
                error_text = response.text[:500] if hasattr(response, 'text') else f"HTTP {response.status_code}"
                raise RuntimeError(
                    f"RAG server returned error {response.status_code}: {error_text}\n\n"
                    f"Please check:\n"
                    f"1. RAG MCP server is running at {self.base_url}\n"
                    f"2. Database connection (POSTGRESQL_URL) is configured\n"
                    f"3. The 'documents' table exists in the database"
                )

            data = response.json()
            
            # MCP server wraps response in a 'data' field
            # Extract the actual result data
            if isinstance(data, dict) and "data" in data:
                result = data["data"]
                # Map chunks_ingested to chunks_stored for consistency
                if "chunks_ingested" in result:
                    result["chunks_stored"] = result.pop("chunks_ingested")
                return result
            
            # If not wrapped, return as-is (backward compatibility)
            return data

        except httpx.RequestError as e:
            error_msg = f"Failed to connect to RAG server at {self.base_url}: {str(e)}"
            print(f"❌ RAG Ingest Connection Error: {error_msg}")
            raise RuntimeError(
                f"{error_msg}\n\n"
                f"Please check:\n"
                f"1. RAG_MCP_URL is set correctly (current: {self.base_url})\n"
                f"2. RAG MCP server is running\n"
                f"3. Network connectivity to the server"
            ) from e
        except Exception as e:
            error_msg = f"RAG ingestion error: {str(e)}"
            print(f"❌ {error_msg}")
            raise RuntimeError(
                f"{error_msg}\n\n"
                f"Please check the RAG server logs for more details."
            ) from e

    async def list_documents(self, tenant_id: str, limit: int = 1000, offset: int = 0):
        """
        List all documents for a tenant.
        Returns the unwrapped data from the MCP server response.
        """

        try:
            async with httpx.AsyncClient() as client:
                response = await client.get(
                    f"{self.base_url}/list",
                    params={
                        "tenant_id": tenant_id,
                        "limit": limit,
                        "offset": offset
                    }
                )

            if response.status_code != 200:
                return {"documents": [], "total": 0, "limit": limit, "offset": offset}

            data = response.json()
            
            # MCP server wraps response in a 'data' field
            # Extract the actual result data
            if isinstance(data, dict) and "data" in data:
                return data["data"]
            
            # If not wrapped, return as-is (backward compatibility)
            return data

        except Exception as e:
            print("RAG List Error:", e)
            return {"documents": [], "total": 0, "limit": limit, "offset": offset}

    async def delete_document(self, tenant_id: str, document_id: int, user_role: Optional[str] = None):
        """
        Delete a specific document by ID for a tenant.
        Returns the unwrapped data from the MCP server response.
        
        Args:
            tenant_id: Tenant identifier
            document_id: Document ID to delete
            user_role: User role (viewer, editor, admin, owner) - required for permission checks
        """
        try:
            async with httpx.AsyncClient(timeout=30.0) as client:
                # Use POST with JSON payload to include role for MCP protocol
                payload = {
                    "tenant_id": tenant_id,
                    "document_id": document_id
                }
                if user_role:
                    payload["user_role"] = user_role
                
                response = await client.post(
                    f"{self.base_url}/delete",
                    json=payload
                )

            if response.status_code == 404:
                return {"error": f"Document {document_id} not found or access denied"}
            if response.status_code != 200:
                error_text = response.text
                try:
                    error_json = response.json()
                    error_text = error_json.get("detail", error_text)
                except:
                    pass
                return {"error": f"HTTP {response.status_code}: {error_text}"}

            data = response.json()
            
            # Check if MCP server returned an error response
            if isinstance(data, dict) and data.get("status") == "error":
                error_msg = data.get("message", "Unknown error")
                error_type = data.get("error_type", "unknown_error")
                # For validation errors (like "not found"), return clear error message
                if error_type == "validation_error":
                    if "not found" in error_msg.lower():
                        return {"error": f"Document {document_id} not found for tenant '{tenant_id}'"}
                    elif "access denied" in error_msg.lower() or "permission" in error_msg.lower():
                        return {"error": f"Access denied: {error_msg}"}
                return {"error": error_msg}
            
            # MCP server wraps response in a 'data' field
            # Extract the actual result data
            if isinstance(data, dict) and "data" in data:
                return data["data"]
            
            # If not wrapped, return as-is (backward compatibility)
            return data

        except httpx.ConnectError as e:
            print(f"RAG Delete Error: Cannot connect to RAG MCP server at {self.base_url}")
            return {"error": f"Cannot connect to RAG MCP server. Is it running at {self.base_url}?"}
        except Exception as e:
            print(f"RAG Delete Error: {e}")
            return {"error": str(e)}

    async def delete_all_documents(self, tenant_id: str, user_role: Optional[str] = None):
        """
        Delete all documents for a tenant.
        Returns the unwrapped data from the MCP server response.
        
        Args:
            tenant_id: Tenant identifier
            user_role: User role (viewer, editor, admin, owner) - required for permission checks
        """
        try:
            async with httpx.AsyncClient(timeout=30.0) as client:
                # Use POST with JSON payload to include role for MCP protocol
                payload = {
                    "tenant_id": tenant_id,
                    "delete_all": True
                }
                if user_role:
                    payload["user_role"] = user_role
                
                response = await client.post(
                    f"{self.base_url}/delete-all",
                    json=payload
                )

            if response.status_code != 200:
                error_text = response.text
                try:
                    error_json = response.json()
                    error_text = error_json.get("detail", error_text)
                except:
                    pass
                return {"error": f"HTTP {response.status_code}: {error_text}"}

            data = response.json()
            
            # Check if MCP server returned an error response
            if isinstance(data, dict) and data.get("status") == "error":
                error_msg = data.get("message", "Unknown error")
                return {"error": error_msg}
            
            # MCP server wraps response in a 'data' field
            # Extract the actual result data
            if isinstance(data, dict) and "data" in data:
                return data["data"]
            
            # If not wrapped, return as-is (backward compatibility)
            return data

        except httpx.ConnectError as e:
            print(f"RAG Delete All Error: Cannot connect to RAG MCP server at {self.base_url}")
            return {"error": f"Cannot connect to RAG MCP server. Is it running at {self.base_url}?"}
        except Exception as e:
            print(f"RAG Delete All Error: {e}")
            return {"error": str(e)}