managarten/apps/questions/CLAUDE.md
Claude ec96d4e952
feat(questions): implement questions app NestJS backend
Complete backend implementation for the AI-powered research assistant app:

Database Schema (Drizzle ORM):
- collections: Organize questions into folders with colors and icons
- questions: User questions with status, priority, tags, and research depth
- research_results: Results from mana-search service with summaries and key points
- sources: Extracted content from web search results
- answers: AI-generated answers with ratings and citations

NestJS Modules:
- QuestionModule: CRUD operations with filtering, pagination, and status management
- CollectionModule: Collection management with reordering and question counts
- ResearchModule: Integration with mana-search microservice for web search
- AnswerModule: Answer management with ratings and acceptance tracking
- SourceModule: Source content retrieval and management
- HealthModule: Health checks for database and search service

Features:
- Full JWT authentication via @manacore/shared-nestjs-auth
- Research depths: quick (5 sources), standard (15), deep (30)
- Automatic content extraction and summarization
- Follow-up question generation

Also updated:
- Root package.json: Added questions:* development scripts
- setup-databases.sh: Added questions database setup

https://claude.ai/code/session_01Rk3YVJCU3nM8uvVPghRz6r
2026-01-28 23:52:22 +00:00

176 lines
6.2 KiB
Markdown

# Questions App
AI-powered research assistant that collects user questions and performs comprehensive research using the mana-search microservice.
## Overview
- **Backend Port**: 3011
- **Technology**: NestJS + Drizzle ORM + PostgreSQL
- **Search**: mana-search microservice (SearXNG)
## Architecture
```
┌─────────────────────────────────────────────────────────────┐
│ Questions App │
│ Collections │ Questions │ Research │ Answers │ Sources │
└─────────────────────────┬───────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ mana-search (Port 3021) │
│ Search API │ Extract API │ Redis Cache │
└─────────────────────────┬───────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ SearXNG (Port 8080) │
│ Google │ Bing │ arXiv │ Wikipedia │ GitHub │ ... │
└─────────────────────────────────────────────────────────────┘
```
## Quick Start
```bash
# 1. Start infrastructure (PostgreSQL, Redis, mana-search dependencies)
pnpm docker:up
# 2. Start mana-search service
pnpm dev:search:full
# 3. Start questions backend
pnpm dev:questions:backend
# Or use the combined command:
pnpm dev:questions:full
```
## API Endpoints
### Collections
```bash
POST /api/v1/collections # Create collection
GET /api/v1/collections # List collections
GET /api/v1/collections/:id # Get collection
PUT /api/v1/collections/:id # Update collection
DELETE /api/v1/collections/:id # Delete collection
POST /api/v1/collections/reorder # Reorder collections
```
### Questions
```bash
POST /api/v1/questions # Create question
GET /api/v1/questions # List questions (with filters)
GET /api/v1/questions/:id # Get question
PUT /api/v1/questions/:id # Update question
DELETE /api/v1/questions/:id # Delete question
PUT /api/v1/questions/:id/status # Update status
```
### Research
```bash
POST /api/v1/research/start # Start research
GET /api/v1/research/question/:id # Get results for question
GET /api/v1/research/:id # Get research result
GET /api/v1/research/health/search # Check search service
```
### Answers
```bash
POST /api/v1/answers # Create answer
GET /api/v1/answers/question/:id # List answers for question
GET /api/v1/answers/question/:id/accepted # Get accepted answer
GET /api/v1/answers/:id # Get answer
PUT /api/v1/answers/:id # Update answer
POST /api/v1/answers/:id/rate # Rate answer
POST /api/v1/answers/:id/accept # Accept answer
DELETE /api/v1/answers/:id # Delete answer
```
### Sources
```bash
GET /api/v1/sources/research/:id # Sources by research result
GET /api/v1/sources/question/:id # All sources for question
GET /api/v1/sources/:id # Get source
GET /api/v1/sources/:id/content # Get source content
```
## Research Depths
| Depth | Sources | Extraction | Categories |
|-------|---------|------------|------------|
| `quick` | 5 | No | general |
| `standard` | 15 | Yes | general, news |
| `deep` | 30 | Yes | general, news, science, it |
## Database Schema
```sql
-- Collections for organizing questions
collections (id, user_id, name, description, color, icon, sort_order, ...)
-- User questions
questions (id, user_id, collection_id, title, description, status, priority, tags, ...)
-- Research results from mana-search
research_results (id, question_id, summary, key_points, follow_up_questions, ...)
-- Extracted sources from search
sources (id, research_result_id, url, title, snippet, extracted_content, ...)
-- AI-generated answers
answers (id, question_id, research_result_id, content, rating, is_accepted, ...)
```
## Environment Variables
| Variable | Default | Description |
|----------|---------|-------------|
| `PORT` | 3011 | Backend port |
| `DATABASE_URL` | - | PostgreSQL connection |
| `MANA_CORE_AUTH_URL` | http://localhost:3001 | Auth service URL |
| `MANA_SEARCH_URL` | http://localhost:3021 | Search service URL |
| `MANA_SEARCH_TIMEOUT` | 30000 | Search timeout (ms) |
| `DEV_BYPASS_AUTH` | false | Skip auth in dev |
| `DEV_USER_ID` | - | User ID when auth bypassed |
## Development Commands
```bash
# Backend only
pnpm dev:questions:backend
# Type checking
cd apps/questions/apps/backend && pnpm type-check
# Database
cd apps/questions/apps/backend
pnpm drizzle-kit generate # Generate migrations
pnpm drizzle-kit push # Push schema to DB
pnpm drizzle-kit studio # Open Drizzle Studio
```
## Testing the API
```bash
# Create a collection
curl -X POST http://localhost:3011/api/v1/collections \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"name": "Tech Research", "color": "#6366f1"}'
# Create a question
curl -X POST http://localhost:3011/api/v1/questions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"title": "What are the best practices for TypeScript?", "researchDepth": "standard"}'
# Start research
curl -X POST http://localhost:3011/api/v1/research/start \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"questionId": "uuid-here", "depth": "standard"}'
```