AI Q&A: Repo-Aware Chat with 3-Tier Fallback
By (author)Maheen Iftikhar
read more
Read Me
# AI Q&A Endpoint — CodeMesh-AI
## What it does
POST /api/ai/chat lets a developer ask natural-language questions
about a repository. It doesn't just forward the question to an LLM —
it first builds real context from the indexed codebase.
## How context is built
1. Keyword-searches indexed files by path, module, and function names
2. Pulls each matched file's pre-generated summary (if any)
3. Lists that file's classes and functions
4. Appends a repo-level summary as closing context
## 3-tier answer fallback
1. OpenAI-compatible API, if AI_API_KEY is set
2. Local Ollama (qwen2.5-coder), if reachable
3. Deterministic heuristic: lists the most relevant files found,
so the endpoint never returns nothing — even with zero AI
providers configured
## Response
Returns the answer plus which files were used as context and
which provider answered (openai / ollama / heuristic) — full
transparency, no black box.
## Files
backend/app/api/ai.py
backend/app/schemas/ai.py
## Verified in repo
Commit a1662c6 — Add code-index, AI Q&A, and repo services