digital-brain
<p align="center">
<h1 align="center">π§ Digital Brain</h1>
<p align="center">
<strong>AI-powered personal browsing history assistant that lives locally on your machine.</strong>
</p>
<p align="center">
<a href="#-quick-start">Quick Start</a> β’
<a href="#-features">Features</a> β’
<a href="#-architecture">Architecture</a> β’
<a href="#-cli-usage">CLI</a> β’
<a href="#-mcp-server">MCP</a> β’
<a href="#-api-reference">API</a>
</p>
</p>
<br/>
> **What was I working on yesterday related to RAG?**
> **Which website did I spend the most time on last week?**
> **What was that command I copied to clear the terminal?**
Digital Brain captures your Chrome browsing activity β page visits, text selections, copy/paste events, page content, and time spent β stores it **100% locally**, and lets you search and ask questions using AI. It combines **keyword search** (SQLite FTS5) with **semantic search** (ChromaDB vectors) through a **RAG pipeline** powered by a local LLM.
<br/>
## β¨ Features
| Feature | Description |
|---------|-------------|
| π§© **Chrome Extension** | Captures page visits, selections, copy/paste, page content, and tab focus time |
| β‘ **FastAPI Backend** | Receives, stores, and indexes events locally on `localhost:8420` |
| π **Full-Text Search** | SQLite FTS5 keyword search with highlighted snippets and filters |
| 𧬠**Vector Search** | ChromaDB semantic indexing for meaning-based recall |
| π€ **RAG Q&A** | Ask questions in plain English β uses local LLM to generate answers |
| π **Activity Stats** | Top domains by time spent, daily breakdowns, and event timelines |
| π» **CLI Tool** | Rich terminal interface with `brain search`, `brain ask`, `brain stats` |
| π **MCP Server** | Query your history from Claude Desktop, VS Code, or any MCP client |
| π‘οΈ **Privacy-First** | All data stays on your machine β no cloud, no tracking |
<br/>
## π Architecture
```
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Chrome Browser β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Chrome Extension (Manifest V3) β β
β β βββββββββββββββ ββββββββββββββββββ βββββββββββββββββββββ β β
β β β Content.js β β Background.js β β Popup UI β β β
β β β β’ Selectionsβ β β’ Tab tracking β β β’ Toggle on/off β β β
β β β β’ Copy/Pasteβ β β’ Event queue β β β’ View stats β β β
β β β β’ Page text β β β’ Batch flush β β β’ Sync now β β β
β β ββββββββ¬βββββββ βββββββββ¬βββββββββ βββββββββββββββββββββ β β
β βββββββββββΌββββββββββββββββββΌββββββββββββββββββββββββββββββββββββ β
ββββββββββββββΌββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββ
β chrome.runtime β
ββββββββββ¬βββββββββ
β POST /api/events (batch every 30s)
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Local Backend (Python) β
β β
β ββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β FastAPI ββββΆβ Event ββββΆβ SQLite+FTS5 β β
β β Server β β Processor β β (structured) β β
β β :8420 β β dedup+domain β ββββββββββββββββ β
β ββββββββββββ ββββββββ¬ββββββββ β
β β β ββββββββββββββββ β
β β βββββββββββΆβ ChromaDB β β
β β β (semantic) β β
β βΌ ββββββββ¬ββββββββ β
β ββββββββββββ β β
β β RAG βββββββ vector search ββββββββ β
β β Pipeline βββββββ FTS5 search βββββββββ β
β ββββββ¬ββββββ β
β β β
β βΌ β
β ββββββββββββ β
β β LLM β Ollama (local, free) or OpenRouter (cloud) β
β ββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β² β² β²
β β β
ββββββ΄βββββ βββββββ΄ββββββ βββββββ΄ββββββ
β CLI β β MCP Serverβ β REST API β
β $ brain β β Claude/ β β curl/apps β
β search β β VS Code β β β
βββββββββββ βββββββββββββ βββββββββββββ
```
<br/>
## π Quick Start
### Prerequisites
- **Python 3.10+**
- **Google Chrome** (latest)
- **Ollama** (optional β for AI-powered answers)
### 1. Clone & Install
```bash
git clone https://github.com/ArpitaSethi-12/digital-brain.git
cd digital-brain
pip install -e .
```
### 2. Start the Backend
```bash
python -m backend.main
```
```
Digital Brain backend is ready π§
Server running at http://localhost:8420
```
### 3. Install the Chrome Extension
1. Open `chrome://extensions/` in Chrome
2. Enable **Developer mode** (top-right toggle)
3. Click **Load unpacked** β select the `extension/` directory
4. The π§ icon appears in your toolbar
### 4. (Optional) Install Ollama for AI Answers
```bash
# Install Ollama from https://ollama.com
ollama serve
ollama pull llama3.2
```
Without Ollama, Digital Brain still works β it returns retrieved results instead of generated answers.
### 5. Start Browsing!
Open any website. The extension captures your activity in the background. Then query it:
```bash
brain search "python tutorial"
brain ask "What was I reading about yesterday?"
brain stats
```
<br/>
## π» CLI Usage
Digital Brain ships with a CLI built on [Typer](https://typer.tiangolo.com/) + [Rich](https://rich.readthedocs.io/):
### Search
```bash
# Basic search
brain search "RAG pipeline"
# Filter by event type
brain search "terminal command" --type copy
# Filter by domain
brain search "react hooks" --domain github.com
# Filter by time window
brain search "python" --last 7d # last 7 days
brain search "docker" --last 24h # last 24 hours
brain search "AI paper" --last 2w # last 2 weeks
```
### Ask (RAG Q&A)
```bash
brain ask "What was I working on yesterday related to RAG?"
brain ask "What command did I copy to clear the terminal?" --type copy
brain ask "Which website did I spend the most time on?" --last 7d
brain ask "Summarize what I read about transformers" --no-llm # skip LLM
```
### Stats & Activity
```bash
brain stats # top domains + weekly activity
brain stats --period month # monthly breakdown
brain activity # today's timeline
brain activity --date yesterday
brain activity --date 2026-06-01
```
### System
```bash
brain status # backend health + LLM availability
brain reindex # rebuild vector index from SQLite
```
<br/>
## π MCP Server
Digital Brain includes an **MCP (Model Context Protocol) server** that lets AI assistants query your browsing history directly.
### How It Works
```
You: "What was I working on yesterday related to RAG?"
Claude: β calls search_history("RAG", last="1d")
β Digital Brain searches local SQLite + ChromaDB
Claude: "Yesterday you visited 3 pages about RAG pipelines..."
```
### Available Tools
| Tool | Description |
|------|-------------|
| `search_history` | Keyword search with type/domain/time filters |
| `ask_brain` | RAG-powered natural language Q&A |
| `get_stats` | Top domains and activity breakdown |
| `get_activity` | Day's chronological timeline |
| `get_timeline` | Detailed event feed with full content |
| `check_status` | Backend health + LLM status |
### Setup with Claude Desktop
1. Start the backend: `python -m backend.main`
2. Add to `~/Library/Application Support/Claude/claude_desktop_config.json`:
```json
{
"mcpServers": {
"digital-brain": {
"command": "python3",
"args": ["/absolute/path/to/digital-brain/mcp_server.py"]
}
}
}
```
3. Restart Claude Desktop β the π§ tools appear automatically.
### Setup with VS Code
Add to `.vscode/settings.json`:
```json
{
"mcp": {
"servers": {
"digital-brain": {
"command": "python3",
"args": ["/absolute/path/to/digital-brain/mcp_server.py"]
}
}
}
}
```
### Test with MCP Inspector
```bash
npx -y @modelcontextprotocol/inspector python3 mcp_server.py
```
<br/>
## π‘ API Reference
The backend exposes a REST API at `http://localhost:8420`:
| Method | Endpoint | Description |
|--------|----------|-------------|
| `GET` | `/api/health` | Backend health status |
| `POST` | `/api/events` | Ingest event batch from extension |
| `GET` | `/api/search?q=...` | Full-text search with filters |
| `POST` | `/api/ask` | RAG question answering |
| `GET` | `/api/ask?q=...` | RAG via query string |
| `GET` | `/api/stats/domains` | Top domains by time spent |
| `GET` | `/api/stats/activity?period=week` | Daily event breakdown |
| `GET` | `/api/stats/timeline?date=2026-06-01` | Event timeline for a date |
| `POST` | `/api/index/rebuild` | Rebuild vector index |
| `GET` | `/api/llm/status` | Check LLM availability |
### Example: Ask a Question
```bash
curl -X POST http://localhost:8420/api/ask \
-H "Content-Type: application/json" \
-d '{"question": "What did I copy about clearing the terminal?"}'
```
**Response:**
```json
{
"answer": "You copied the command 'clear && printf ...' from Stack Overflow.",
"sources": [
{
"event_type": "copy",
"domain": "stackoverflow.com",
"content": "clear && printf '\\e[3J'"
}
],
"llm_used": true,
"retrieval_count": 3
}
```
### Example: Search History
```bash
curl "http://localhost:8420/api/search?q=RAG&event_type=page_visit&limit=5"
```
<br/>
## π Project Structure
```
digital-brain/
βββ extension/ # Chrome Extension (Manifest V3)
β βββ manifest.json # Permissions & config
β βββ background.js # Service worker β event batching, tab tracking
β βββ content.js # Content script β selections, copy/paste, page text
β βββ popup.html/js/css # Extension popup UI
β βββ icons/ # Extension icons
β
βββ backend/ # Python FastAPI Backend
β βββ main.py # App entry point with CORS & lifespan
β βββ config.py # Environment-based configuration
β βββ models.py # 13 Pydantic models (API contract)
β βββ database.py # SQLite + FTS5 (schema, CRUD, search)
β βββ routers/
β β βββ events.py # POST /api/events
β β βββ search.py # GET /api/search
β β βββ stats.py # GET /api/stats/*
β β βββ ask.py # POST /api/ask + index rebuild
β βββ services/
β βββ event_processor.py # Validate, dedup, extract domains
β βββ vector_store.py # ChromaDB operations
β βββ rag.py # RAG pipeline (vector + FTS β LLM)
β βββ llm.py # Ollama / OpenRouter client
β βββ indexer.py # Vector index rebuild
β
βββ cli/
β βββ brain.py # Typer + Rich CLI (6 commands)
β
βββ mcp_server.py # MCP server (6 tools for AI assistants)
β
βββ data/ # Local storage (auto-created, gitignored)
β βββ brain.db # SQLite database + FTS5 index
β βββ chroma/ # ChromaDB vector store
β
βββ .env.example # Environment variable template
βββ pyproject.toml # Project config & dependencies
βββ README.md
```
<br/>
## βοΈ Configuration
Copy the example and customize:
```bash
cp .env.example .env
```
| Variable | Default | Description |
|----------|---------|-------------|
| `BRAIN_HOST` | `127.0.0.1` | Backend host |
| `BRAIN_PORT` | `8420` | Backend port |
| `BRAIN_DATA_DIR` | `./data` | Storage directory |
| `BRAIN_LLM_PROVIDER` | `ollama` | `ollama` or `openrouter` |
| `OLLAMA_BASE_URL` | `http://localhost:11434` | Ollama server URL |
| `OLLAMA_MODEL` | `llama3.2` | Ollama model name |
| `OPENROUTER_API_KEY` | β | OpenRouter API key (optional) |
| `BRAIN_RAG_TOP_K` | `8` | Number of results for RAG context |
<br/>
## 𧬠How RAG Works
```
User: "What command did I copy to clear the terminal?"
β
ββββΆ ChromaDB Semantic Search
β Finds events with similar meaning
β (e.g., "bash clear screen" matches "terminal command")
β
ββββΆ SQLite FTS5 Keyword Search
β Finds events containing exact words
β (e.g., "clear" AND "terminal")
β
ββββΆ Merge & Deduplicate
β Combines both result sets by event ID
β
ββββΆ Format Context Block
β Prepares retrieved events as LLM context
β
ββββΆ LLM Generation (Ollama)
Generates natural language answer
grounded in your actual browsing data
Result: "You copied 'clear && printf \\e[3J' from stackoverflow.com on June 8th."
```
<br/>
## π‘οΈ Privacy
Digital Brain is **privacy-first by design**:
- β
All data stored locally in `data/` directory
- β
Chrome extension only communicates with `localhost:8420`
- β
MCP server runs locally via stdio β no network requests
- β
LLM runs locally via Ollama β your data never leaves your machine
- β
No analytics, no telemetry, no cloud sync
- β οΈ OpenRouter is optional and only used if you explicitly set `OPENROUTER_API_KEY`
<br/>
## π οΈ Tech Stack
| Layer | Technology |
|-------|-----------|
| Data Capture | Chrome Extension (Manifest V3) |
| Backend | Python 3.10+ Β· FastAPI Β· Uvicorn |
| Structured DB | SQLite 3 + FTS5 |
| Vector DB | ChromaDB (all-MiniLM-L6-v2 embeddings) |
| Local LLM | Ollama (llama3.2) |
| Cloud LLM | OpenRouter (optional) |
| CLI | Typer + Rich |
| MCP | FastMCP (mcp SDK) |
| Validation | Pydantic v2 |
| HTTP Client | httpx (async) |
<br/>
## π Project Stats
| Metric | Value |
|--------|-------|
| Total Lines of Code | ~3,800 |
| Python Files | 18 |
| JavaScript Files | 3 |
| API Endpoints | 10 |
| CLI Commands | 6 |
| MCP Tools | 6 |
| Pydantic Models | 13 |
| Event Types Captured | 6 |
| Dependencies | 10 |
<br/>
## πΊοΈ Roadmap
- [ ] π Web chat UI with streaming responses
- [ ] π
Smarter date parsing ("yesterday", "last week") in RAG
- [ ] π Optional encryption at rest for `data/`
- [ ] π MCP Registry listing
- [ ] π± Firefox extension
- [ ] π§ Multi-turn conversation memory
- [ ] π¦ PyPI package (`pip install digital-brain`)
<br/>
## π€ Contributing
Contributions are welcome! Here's how:
1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
<br/>
## π License
This project is licensed under the MIT License β see the [LICENSE](LICENSE) file for details.
---
<p align="center">
Built with β€οΈ as a BTech Major Project
</p>
TDQS
Scored across 6 tools
Most tools are distinct, but get_activity and get_timeline have overlapping purposes (both show timeline, get_timeline just more detailed). Also, ask_brain and search_history both retrieve history but through different interfaces (Q&A vs keyword search), which could cause ambiguity. Overall, boundaries are clear for most tools.
All tool names follow a consistent verb_noun pattern using snake_case: ask_brain, check_status, get_activity, get_stats, get_timeline, search_history. The pattern is uniform and predictable.
With 6 tools, the set is well-scoped for a browsing history assistant. Each tool serves a distinct need (Q&A, health check, daily overview, statistics, detailed timeline, keyword search) without redundancy or bloat.
The tool surface covers the main browsing history interactions: query, search, timeline, stats, health. However, there is no dedicated tool to retrieve all events of a specific type without a date or search query, which is a minor gap. Otherwise, the set feels complete for a read-only assistant.