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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
"""
RAG Chatbot Service for OpenTriage.
AI assistant trained on repo docs and closed issues to answer contributor questions.
"""
import logging
from typing import List, Dict, Any, Optional
from datetime import datetime, timezone
from pydantic import BaseModel
import time

from config.settings import settings

logger = logging.getLogger(__name__)


class RAGAnswer(BaseModel):
    """Response from the RAG chatbot."""
    question: str
    answer: str
    sources: List[Dict[str, Any]] = []  # List of source documents used
    confidence: float = 0.0
    related_issues: List[Dict[str, Any]] = []
    repo_name: Optional[str] = None
    generated_at: datetime = None


class RAGChatbotService:
    """
    RAG-powered Q&A chatbot service.
    
    Uses vector similarity search on indexed documents
    (issues, PRs, docs) to provide context-aware answers.
    
    Note: For production, integrate with ChromaDB or Pinecone.
    This implementation uses in-memory search as fallback.
    """
    
    # Cache settings
    README_CACHE_TTL = 600  # 10 minutes in seconds
    
    def __init__(self):
        self.use_vector_db = False  # Set True when ChromaDB is available
        self._embeddings_cache = {}
        self._readme_cache = {}  # {repo_key: {"content": str, "timestamp": float}}
    
    def _get_cached_readme(self, repo_name: str) -> Optional[str]:
        """
        Get cached README if it exists and hasn't expired.
        
        Args:
            repo_name: Repository name (owner/repo format)
            
        Returns:
            README content if cached and valid, None otherwise
        """
        if repo_name not in self._readme_cache:
            return None
        
        cache_entry = self._readme_cache[repo_name]
        age = time.time() - cache_entry["timestamp"]
        
        if age > self.README_CACHE_TTL:
            # Cache expired, remove it
            del self._readme_cache[repo_name]
            logger.info(f"README cache expired for {repo_name} (age: {age:.1f}s)")
            return None
        
        logger.info(f"✅ Serving README from cache for {repo_name} (age: {age:.1f}s)")
        return cache_entry["content"]
    
    def _cache_readme(self, repo_name: str, content: str) -> None:
        """
        Cache README content with timestamp.
        
        Args:
            repo_name: Repository name (owner/repo format)
            content: README content to cache
        """
        self._readme_cache[repo_name] = {
            "content": content,
            "timestamp": time.time()
        }
        logger.info(f"📝 Cached README for {repo_name} ({len(content)} chars)")

    
    async def answer_question(
        self,
        question: str,
        repo_name: Optional[str] = None,
        top_k: int = 5,
        github_access_token: Optional[str] = None
    ) -> RAGAnswer:
        """
        Answer a question using RAG.
        
        Args:
            question: The question to answer
            repo_name: Optional repo context
            top_k: Number of documents to retrieve
            github_access_token: Optional GitHub token for README fetching
            
        Returns:
            RAGAnswer with the response and sources
        """
        from config.database import db
        import httpx
        
        # Check if we have any indexed content for this repo
        has_indexed_content = False
        readme_content = None
        
        if repo_name:
            # Check for existing RAG chunks
            try:
                existing_chunks = await db.rag_chunks.count_documents({"sourceRepo": repo_name})
                has_indexed_content = existing_chunks > 0
            except:
                has_indexed_content = False
            
            # If no indexed content, try to fetch README directly from GitHub
            if not has_indexed_content:
                logger.info(f"No indexed content for {repo_name}, checking cache and fetching README if needed...")
                
                # Stage 1: Check cache first
                cached_readme = self._get_cached_readme(repo_name)
                if cached_readme:
                    readme_content = cached_readme
                else:
                    # Cache miss - fetch from GitHub
                    try:
                        owner, repo = repo_name.split('/')
                        url = f"https://raw.githubusercontent.com/{owner}/{repo}/main/README.md"
                        
                        async with httpx.AsyncClient(timeout=10) as client:
                            response = await client.get(url)
                            if response.status_code == 200:
                                readme_content = response.text
                                # Cache the fetched content
                                self._cache_readme(repo_name, readme_content)
                                logger.info(f"Successfully fetched README for {repo_name} ({len(readme_content)} chars)")
                            else:
                                # Try master branch instead
                                url = f"https://raw.githubusercontent.com/{owner}/{repo}/master/README.md"
                                response = await client.get(url)
                                if response.status_code == 200:
                                    readme_content = response.text
                                    # Cache the fetched content
                                    self._cache_readme(repo_name, readme_content)
                                    logger.info(f"Successfully fetched README (master) for {repo_name} ({len(readme_content)} chars)")
                                else:
                                    logger.warning(f"README not found at {url}")
                    except Exception as e:
                        logger.error(f"Error fetching README for {repo_name}: {e}")
        
        # Search for relevant documents (from indexed chunks)
        relevant_docs = await self.search_documents(question, repo_name, top_k)
        
        # Build context from documents
        context = self._build_context(relevant_docs)
        
        # If we have a fresh README but no indexed content, prepend it to context
        if readme_content and not has_indexed_content:
            # Truncate README if too long (keep first 5000 chars for better context)
            truncated_readme = readme_content[:5000] if len(readme_content) > 5000 else readme_content
            context = f"[PROJECT README]\n{truncated_readme}\n\n---\n\n{context}"
            # Add README to sources
            relevant_docs.insert(0, {
                "id": f"{repo_name}_readme_live",
                "title": "Project README (Live from GitHub)",
                "type": "readme",
                "relevance": 1.0
            })
            logger.info(f"Added README to context for {repo_name}")
        
        # Generate answer using AI
        answer, confidence = await self._generate_answer(question, context, repo_name)
        
        # Find related issues
        related_issues = await self._find_related_issues(question, repo_name)
        
        return RAGAnswer(
            question=question,
            answer=answer,
            sources=[{
                "id": doc.get("id", ""),
                "title": doc.get("title", ""),
                "type": doc.get("type", ""),
                "relevance": doc.get("relevance", 0)
            } for doc in relevant_docs],
            confidence=confidence,
            related_issues=related_issues,
            repo_name=repo_name,
            generated_at=datetime.now(timezone.utc)
        )
    
    async def search_documents(
        self,
        query: str,
        repo_name: Optional[str] = None,
        top_k: int = 5
    ) -> List[Dict[str, Any]]:
        """
        Search for relevant documents.
        
        Uses vector search if available, falls back to keyword search.
        """
        from config.database import db
        
        results = []
        
        # First, try to find RAG chunks
        chunk_query = {}
        if repo_name:
            chunk_query["sourceRepo"] = repo_name
            
        # First, search specifically for README content if repo_name is provided
        if repo_name:
            readme_query = {
                "sourceRepo": repo_name,
                "documentType": "readme"
            }
            cursor = db.rag_chunks.find(readme_query, {"_id": 0}).sort("chunkIndex", 1).limit(3)
            readme_chunks = await cursor.to_list(length=3)
            
            for chunk in readme_chunks:
                results.append({
                    "id": chunk.get("chunkId", ""),
                    "title": "Project README",
                    "content": chunk.get("content", ""),
                    "type": "readme",
                    "relevance": 1.0  # High relevance for README context
                })
        
        # Simple text search as fallback
        if query:
            chunk_query["$or"] = [
                {"content": {"$regex": query, "$options": "i"}},
                {"metadata.title": {"$regex": query, "$options": "i"}}
            ]
        
        cursor = db.rag_chunks.find(chunk_query, {"_id": 0}).limit(top_k * 2)
        chunks = await cursor.to_list(length=top_k * 2)
        
        for chunk in chunks:
            results.append({
                "id": chunk.get("chunkId", ""),
                "title": chunk.get("metadata", {}).get("title", "Document"),
                "content": chunk.get("content", ""),
                "type": chunk.get("documentType", ""),
                "relevance": self._calculate_relevance(query, chunk.get("content", ""))
            })
        
        # Also search closed issues for answers
        issue_query = {"state": "closed"}
        if repo_name:
            issue_query["repoName"] = repo_name
        if query:
            issue_query["$or"] = [
                {"title": {"$regex": query, "$options": "i"}},
                {"body": {"$regex": query, "$options": "i"}}
            ]
        
        cursor = db.issues.find(issue_query, {"_id": 0}).limit(top_k)
        issues = await cursor.to_list(length=top_k)
        
        for issue in issues:
            results.append({
                "id": issue.get("id", ""),
                "title": issue.get("title", ""),
                "content": issue.get("body", ""),
                "type": "closed_issue",
                "relevance": self._calculate_relevance(query, f"{issue.get('title', '')} {issue.get('body', '')}")
            })
        
        # Sort by relevance and return top_k
        results.sort(key=lambda x: x.get("relevance", 0), reverse=True)
        return results[:top_k]
    
    def _calculate_relevance(self, query: str, content: str) -> float:
        """Calculate simple relevance score based on keyword matching."""
        if not query or not content:
            return 0.0
        
        query_terms = set(query.lower().split())
        content_lower = content.lower()
        
        matches = sum(1 for term in query_terms if term in content_lower)
        
        if len(query_terms) == 0:
            return 0.0
        
        return matches / len(query_terms)
    
    def _build_context(self, documents: List[Dict[str, Any]]) -> str:
        """Build context string from documents."""
        if not documents:
            return ""
        
        context_parts = []
        
        for i, doc in enumerate(documents[:5]):
            title = doc.get("title", "Document")
            content = doc.get("content", "")[:500]  # Limit content
            doc_type = doc.get("type", "document")
            
            context_parts.append(f"[{doc_type.upper()}] {title}:\n{content}\n")
        
        return "\n---\n".join(context_parts)
    
    async def _generate_answer(
        self,
        question: str,
        context: str,
        repo_name: Optional[str]
    ) -> tuple[str, float]:
        """Generate answer using AI with context and model fallbacks."""
        from openai import OpenAI
        
        # Use the same fallback models as chat service
        models = [
            "google/gemini-2.0-flash-001",
            "cohere/rerank-4-pro",
            "arcee-ai/trinity-large-preview:free",
            "liquid/lfm-2.5-1.2b-thinking:free",
        ]
        
        client = OpenAI(
            base_url="https://openrouter.ai/api/v1",
            api_key=settings.OPENROUTER_API_KEY
        )
        
        system_prompt = f"""You are a knowledgeable guide for contributors to{f' the {repo_name} project' if repo_name else ' this open source project'}, acting much like a senior developer who has worked on the codebase for years.

When answering questions, draw from the provided documentation and issue history naturally. Present information as helpful conversation rather than recitation.

If the context provides information, use it to give a solid answer. If context is limited, acknowledge what you don't know while offering general guidance.

Provide clear, practical answers. Reference specific issues or documentation sections naturally."""

        user_prompt = f"""Context from project:
{context}

---
Question: {question}

Provide a helpful answer based on the context above. If context is limited, be honest about it."""

        # Try each model
        for model in models:
            try:
                logger.info(f"RAG: Attempting answer generation with {model}")
                response = client.chat.completions.create(
                    model=model,
                    messages=[
                        {"role": "system", "content": system_prompt},
                        {"role": "user", "content": user_prompt}
                    ],
                    max_tokens=800,
                    temperature=0.3
                )
                
                answer = response.choices[0].message.content.strip()
                confidence = 0.9 if context and len(context) > 500 else 0.5
                
                logger.info(f"RAG: Successfully generated answer with {model}")
                return answer, confidence
                
            except Exception as e:
                logger.warning(f"RAG: Model {model} failed: {e}")
                continue
        
        # All models failed
        logger.error("RAG: All models failed for answer generation")
        return (
            "I apologize, I'm currently unable to generate an answer. Please try again in a moment.",
            0.0
        )
    
    async def _find_related_issues(
        self,
        question: str,
        repo_name: Optional[str],
        limit: int = 3
    ) -> List[Dict[str, Any]]:
        """Find issues related to the question."""
        from config.database import db
        
        query = {}
        if repo_name:
            query["repoName"] = repo_name
        if question:
            query["$or"] = [
                {"title": {"$regex": question.split()[0] if question.split() else "", "$options": "i"}},
            ]
        
        cursor = db.issues.find(query, {"_id": 0}).limit(limit)
        issues = await cursor.to_list(length=limit)
        
        return [{
            "id": issue.get("id"),
            "number": issue.get("number"),
            "title": issue.get("title"),
            "state": issue.get("state"),
            "url": issue.get("htmlUrl", "")
        } for issue in issues]
    
    async def index_repository(self, repo_name: str, github_access_token: Optional[str] = None) -> Dict[str, Any]:
        """
        Index a repository's content for RAG.
        Uses the existing rag_data_prep service.
        """
        try:
            from services.rag_data_prep import rag_data_prep
            
            result = await rag_data_prep.prepare_and_store(
                doc_types=["issue", "pr", "comment", "readme"],
                repo_names=[repo_name],
                collection_name="rag_chunks",
                github_access_token=github_access_token
            )
            
            return {
                "status": "success",
                "repo_name": repo_name,
                "documents_indexed": result.get("documents_processed", 0),
                "chunks_created": result.get("chunks_created", 0)
            }
            
        except Exception as e:
            logger.error(f"Indexing failed: {e}")
            return {
                "status": "error",
                "error": str(e)
            }
    
    async def get_suggested_questions(
        self,
        repo_name: Optional[str] = None
    ) -> List[str]:
        """Get suggested questions based on common topics."""
        from config.database import db
        
        suggestions = [
            "How do I get started contributing?",
            "What is the development setup?",
            "How do I run the tests?",
            "What coding style should I follow?",
            "How do I submit a pull request?"
        ]
        
        # Add repo-specific suggestions based on common issues
        if repo_name:
            cursor = db.issues.find(
                {"repoName": repo_name, "state": "closed"},
                {"_id": 0, "title": 1}
            ).limit(5)
            issues = await cursor.to_list(length=5)
            
            for issue in issues:
                title = issue.get("title", "")
                if "?" in title:
                    suggestions.append(title)
        
        return suggestions[:8]


# Singleton instance
rag_chatbot_service = RAGChatbotService()