Advertisement
Guest User

Untitled

a guest
Jul 14th, 2025
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.88 KB | None | 0 0
  1. # CLAUDE.md
  2.  
  3. This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
  4.  
  5. # NewsDatabase - AI-Powered News Vector Database
  6.  
  7. ## 1. Project Overview
  8.  
  9. - **Vision:** AI-Powered Personal News Management with Vector Database
  10. - **Current Phase:** Production-ready with advanced content management features
  11. - **Key Architecture:** FastAPI + ChromaDB + Sentence Transformers + MCP Server with enterprise security
  12. - **Status:** Complete implementation with 955+ AI news documents, featuring duplicate detection, automatic categorization, and semantic search
  13. - **Core Features:** Duplicate prevention, AI-powered categorization (10 categories), sub-50ms semantic search, real-time analytics
  14. - **Security Features:** API key authentication, token bucket rate limiting, CORS configuration, comprehensive error boundaries
  15. - **MCP Integration:** Fully operational with Claude Code knowledge base integration via 3 specialized tools
  16. - **Content Management:** Automatic organization with duplicate detection and intelligent categorization
  17.  
  18. ## 2. Project Structure
  19.  
  20. **⚠️ CRITICAL: AI agents MUST read the [Project Structure documentation](/docs/ai-context/project-structure.md) before attempting any task to understand the complete technology stack, file tree and project organization.**
  21.  
  22. NewsDatabase is a production-ready personal news management system with advanced content intelligence features. Built on FastAPI + ChromaDB + Sentence Transformers, it provides automatic duplicate detection, AI-powered categorization into 10 content types, and sub-50ms semantic search. The system includes enterprise-grade security (API authentication, rate limiting, CORS), comprehensive error handling, and seamless Claude Code integration. Currently operational with 955+ AI news documents, featuring intelligent content organization and real-time analytics. For the complete tech stack, architecture, and new features, see [docs/ai-context/project-structure.md](/docs/ai-context/project-structure.md).
  23.  
  24. ## 3. Coding Standards & AI Instructions
  25.  
  26. ### General Instructions
  27.  
  28. - Your most important job is to manage your own context. Always read any relevant files BEFORE planning changes.
  29. - When updating documentation, keep updates concise and on point to prevent bloat.
  30. - Write code following KISS, YAGNI, and DRY principles.
  31. - When in doubt follow proven best practices for implementation.
  32. - Do not commit to git without user approval.
  33. - Do not run any servers, rather tell the user to run servers for testing.
  34. - Always consider industry standard libraries/frameworks first over custom implementations.
  35. - Never mock anything. Never use placeholders. Never omit code.
  36. - Apply SOLID principles where relevant. Use modern framework features rather than reinventing solutions.
  37. - Be brutally honest about whether an idea is good or bad.
  38. - Make side effects explicit and minimal.
  39. - Design database schema to be evolution-friendly (avoid breaking changes).
  40.  
  41.  
  42. ### File Organization & Modularity
  43.  
  44. - Default to creating multiple small, focused files rather than large monolithic ones
  45. - Each file should have a single responsibility and clear purpose
  46. - Keep files under 350 lines when possible - split larger files by extracting utilities, constants, types, or logical components into separate modules
  47. - Separate concerns: utilities, constants, types, components, and business logic into different files
  48. - Prefer composition over inheritance - use inheritance only for true 'is-a' relationships, favor composition for 'has-a' or behavior mixing
  49.  
  50. - Follow existing project structure and conventions - place files in appropriate directories. Create new directories and move files if deemed appropriate.
  51. - Use well defined sub-directories to keep things organized and scalable
  52. - Structure projects with clear folder hierarchies and consistent naming conventions
  53. - Import/export properly - design for reusability and maintainability
  54.  
  55. ### Type Hints (REQUIRED)
  56.  
  57. - **Always** use type hints for function parameters and return values
  58. - Use `from typing import` for complex types
  59. - Prefer `Optional[T]` over `Union[T, None]`
  60. - Use Pydantic models for data structures
  61.  
  62. ```python
  63. # Good
  64. from typing import Optional, List, Dict, Tuple
  65.  
  66. async def ingest_news_file(
  67. file_path: str,
  68. metadata: Dict[str, Any],
  69. embedding_model: Optional[str] = None
  70. ) -> Tuple[str, Dict[str, Any]]:
  71. """Ingest a news markdown file into the vector database."""
  72. pass
  73. ```
  74.  
  75. ### Naming Conventions
  76.  
  77. - **Classes**: PascalCase (e.g., `NewsDatabase`, `EmbeddingService`)
  78. - **Functions/Methods**: snake_case (e.g., `ingest_news_file`, `search_similar`)
  79. - **Constants**: UPPER_SNAKE_CASE (e.g., `MAX_FILE_SIZE`, `DEFAULT_MODEL`)
  80. - **Private methods**: Leading underscore (e.g., `_validate_markdown`, `_generate_embedding`)
  81. - **Pydantic Models**: PascalCase with `Schema` suffix (e.g., `NewsFileSchema`, `SearchRequestSchema`)
  82.  
  83.  
  84. ### Documentation Requirements
  85.  
  86. - Every module needs a docstring
  87. - Every public function needs a docstring
  88. - Use Google-style docstrings
  89. - Include type information in docstrings
  90.  
  91. ```python
  92. def calculate_similarity(query: str, document: str) -> float:
  93. """Calculate semantic similarity between query and document.
  94.  
  95. Args:
  96. query: Search query text
  97. document: Document content to compare against
  98.  
  99. Returns:
  100. Similarity score between 0 and 1
  101.  
  102. Raises:
  103. ValueError: If either text is empty
  104. """
  105. pass
  106. ```
  107.  
  108. ### Security First (Production Implemented)
  109.  
  110. - **API Key Authentication**: Bearer token authentication implemented for all protected endpoints
  111. - **Rate Limiting**: Token bucket algorithm with per-client tracking and burst protection
  112. - **Input Validation**: Multi-layer validation at API boundaries and service layers
  113. - **CORS Configuration**: Configurable cross-origin policies for production security
  114. - **Error Boundaries**: Comprehensive error handling with security-aware logging
  115. - **Secrets Management**: All secrets in environment variables, never in code
  116. - **Security Logging**: Log security events without exposing sensitive data
  117. - **Request Sanitization**: Comprehensive input sanitization before processing
  118. - **Graceful Degradation**: Security failures handled with appropriate fallbacks
  119.  
  120. ### Error Handling (Production Implemented)
  121.  
  122. - **Custom Exception Types**: VectorDBError, EmbeddingError, ProcessingError with categorization
  123. - **Error Boundaries**: Decorators for comprehensive error catching and handling
  124. - **Structured Logging**: JSON-formatted logs with correlation IDs and context
  125. - **Security-Aware Messages**: Error responses that don't reveal system internals
  126. - **Retry Mechanisms**: Exponential backoff for transient failures
  127. - **Graceful Fallbacks**: Fallback values and degraded service modes
  128.  
  129. ### Observable Systems & Logging Standards (Production Implemented)
  130.  
  131. - **Structured Logging**: JSON format with timestamp, level, correlation_id, event, context
  132. - **Security Event Logging**: Authentication attempts, rate limit violations, error boundaries
  133. - **Performance Monitoring**: Request timing, embedding generation metrics, vector search performance
  134. - **Health Monitoring**: Real-time system health checks with component status
  135. - **Production Debugging**: Correlation IDs for tracing requests across service boundaries
  136.  
  137. ### State Management
  138.  
  139. - Have one source of truth for each piece of state
  140. - Make state changes explicit and traceable
  141. - Design for distributed vector operations - use request IDs for tracking, avoid storing large embeddings in memory
  142. - Keep file metadata lightweight and cache-friendly
  143.  
  144. ### API Design Principles (Production Implemented)
  145.  
  146. - **RESTful design** with consistent URL patterns and HTTP status codes
  147. - **Authentication required** for all data-modifying and sensitive endpoints
  148. - **Rate limiting** with different tiers for heavy vs. light operations
  149. - **CORS support** with configurable origin policies
  150. - **Consistent JSON response format**:
  151. - Success: `{ "data": {...}, "error": null }`
  152. - Error: `{ "data": null, "error": {"message": "...", "code": "..."} }`
  153. - **Security headers** including rate limit status in responses
  154. - **Input validation** at multiple layers with detailed error messages
  155.  
  156.  
  157. ## 4. Claude Code Development Kit Commands
  158.  
  159. This project uses the Claude Code Development Kit, which provides sophisticated command workflows for development tasks. These commands auto-load documentation and coordinate multi-agent workflows.
  160.  
  161. ### Available Commands
  162.  
  163. #### `/full-context` - Comprehensive Analysis
  164.  
  165. **Usage**: `@commands/full-context.md "analyze authentication system"`
  166. **Purpose**: Deep understanding before implementing changes
  167. **When to use**:
  168.  
  169. - Starting work on new features
  170. - Understanding system interconnections
  171. - Planning architectural changes
  172.  
  173. #### `/code-review` - Multi-Agent Review
  174.  
  175. **Usage**: `@commands/code-review.md "review news ingestion module"`
  176. **Purpose**: Get security, performance, and architecture insights
  177. **When to use**:
  178.  
  179. - After implementing features
  180. - Before merging changes
  181. - Need confidence in code quality
  182.  
  183. #### `/update-docs` - Documentation Sync
  184.  
  185. **Usage**: `@commands/update-docs.md "document new search API"`
  186. **Purpose**: Keep documentation current with code changes
  187. **When to use**:
  188.  
  189. - After modifying code
  190. - After adding features
  191. - When project structure changes
  192.  
  193. #### `/refactor` - Intelligent Restructuring
  194.  
  195. **Usage**: `@commands/refactor.md "break up large database module"`
  196. **Purpose**: Restructure code while maintaining functionality
  197. **When to use**:
  198.  
  199. - Breaking up large files
  200. - Improving code organization
  201. - Extracting reusable components
  202.  
  203. #### `/handoff` - Session Continuity
  204.  
  205. **Usage**: `@commands/handoff.md "completed vector search implementation"`
  206. **Purpose**: Preserve context between sessions
  207. **When to use**:
  208.  
  209. - Ending work sessions
  210. - Context limit approaching
  211. - Switching between major tasks
  212.  
  213. ### Typical Development Workflow
  214.  
  215. ```bash
  216. /full-context "implement news ingestion API" # Understand requirements
  217. # ... implement the feature ...
  218. /code-review "review ingestion system" # Validate quality
  219. /update-docs "document ingestion API" # Keep docs current
  220. /handoff "completed news ingestion feature" # Preserve context
  221. ```
  222.  
  223. ## 5. Multi-Agent Workflows & Context Injection
  224.  
  225. ### Automatic Context Injection for Sub-Agents
  226.  
  227. When using the Task tool to spawn sub-agents, the core project context (CLAUDE.md, project-structure.md, docs-overview.md) is automatically injected into their prompts via the subagent-context-injector hook. This ensures all sub-agents have immediate access to essential project documentation without the need of manual specification in each Task prompt.
  228.  
  229.  
  230. ## 6. MCP Server Integrations
  231.  
  232. ### 🌟 NewsDatabase Knowledge Base Server (Production Ready)
  233.  
  234. **Repository**: This project - `/mcp_newsdb_server.py`
  235. **Status**: **🟢 FULLY OPERATIONAL** with 955 AI news documents
  236.  
  237. **Current Integration Status:**
  238.  
  239. - **✅ MCP Server**: Successfully integrated with Claude Code
  240. - **✅ Knowledge Base**: 955 AI news content chunks indexed and searchable
  241. - **✅ Performance**: Sub-50ms semantic search with similarity ranking
  242. - **✅ Health Monitoring**: Real-time system status and performance tracking
  243. - **✅ Production Stability**: Tested, documented, and operationally verified
  244.  
  245. **When to use:**
  246.  
  247. - Searching for AI and technology news information
  248. - Getting context on AI developments, companies, and trends
  249. - Finding relevant background information for AI-related tasks
  250. - Accessing curated AI news content with semantic search
  251. - Research on machine learning, neural networks, and AI breakthroughs
  252.  
  253. **Available MCP Tools:**
  254.  
  255. ```python
  256. # Search 955+ AI news documents with enhanced semantic similarity
  257. mcp__newsdb-knowledge__search_news_knowledge(
  258. query="OpenAI GPT-4 developments",
  259. limit=5,
  260. similarity_threshold=0.3 # Automatic duplicate exclusion and category awareness
  261. )
  262.  
  263. # Get comprehensive knowledge base and feature statistics
  264. mcp__newsdb-knowledge__get_knowledge_stats() # Includes category distribution and duplicate stats
  265.  
  266. # Check system health including new content management features
  267. mcp__newsdb-knowledge__check_knowledge_health()
  268. ```
  269.  
  270. **Key capabilities:**
  271.  
  272. - **955+ AI News Documents**: Comprehensive curated knowledge base with intelligent organization
  273. - **Sub-50ms Search**: High-performance semantic search with category filtering and duplicate exclusion
  274. - **Duplicate Prevention**: Automatic hash-based and semantic duplicate detection
  275. - **Smart Categorization**: AI-powered classification into 10 content categories with topic extraction
  276. - **Enhanced Analytics**: Category distribution, duplicate statistics, and content insights
  277. - **Real-time Health Monitoring**: System status, performance tracking, and feature health
  278. - **Similarity Scoring**: Results ranked 0.0-1.0 for relevance with metadata enrichment
  279. - **Production Stability**: Comprehensive testing including advanced features, security, and integration
  280.  
  281. **Example Search Queries:**
  282.  
  283. ```python
  284. # AI company developments
  285. search_news_knowledge("OpenAI ChatGPT GPT-4")
  286.  
  287. # Technology trends
  288. search_news_knowledge("machine learning breakthroughs")
  289.  
  290. # Industry analysis
  291. search_news_knowledge("AI startup funding venture capital")
  292.  
  293. # Research developments
  294. search_news_knowledge("neural networks deep learning research")
  295.  
  296. # Specific technologies
  297. search_news_knowledge("transformer architecture attention mechanisms")
  298.  
  299. # Market analysis
  300. search_news_knowledge("AI regulation policy government")
  301. ```
  302.  
  303. **Integration Architecture:**
  304.  
  305. ```
  306. NewsDatabase API ↔ MCP Server ↔ Claude Code
  307. ↓ ↓ ↓
  308. FastAPI HTTP/JSON Tool Calls
  309. ChromaDB Async search_news_knowledge
  310. 955 Docs Bridge get_knowledge_stats
  311. Embeddings Real-time check_knowledge_health
  312. ```
  313.  
  314. ### Gemini Consultation Server
  315.  
  316. **When to use:**
  317.  
  318. - Complex coding problems requiring deep analysis or multiple approaches
  319. - Code reviews and architecture discussions
  320. - Debugging complex issues across multiple files
  321. - Performance optimization and refactoring guidance
  322. - Detailed explanations of complex implementations
  323. - Highly security relevant tasks
  324.  
  325. **Automatic Context Injection:**
  326.  
  327. - The kit's `gemini-context-injector.sh` hook automatically includes two key files for new sessions:
  328. - `/docs/ai-context/project-structure.md` - Complete project structure and tech stack
  329. - `/MCP-ASSISTANT-RULES.md` - Your project-specific coding standards and guidelines
  330. - This ensures Gemini always has comprehensive understanding of your technology stack, architecture, and project standards
  331.  
  332. **Usage patterns:**
  333.  
  334. ```python
  335. # New consultation session (project structure auto-attached by hooks)
  336. mcp__gemini__consult_gemini(
  337. specific_question="How should I optimize the vector search performance?",
  338. problem_description="Need to reduce latency in similarity search operations",
  339. code_context="Current system processes embeddings sequentially...",
  340. attached_files=[
  341. "src/database/vector_search.py" # Your specific files
  342. ],
  343. preferred_approach="optimize"
  344. )
  345.  
  346. # Follow-up in existing session
  347. mcp__gemini__consult_gemini(
  348. specific_question="What about memory usage with large collections?",
  349. session_id="session_123",
  350. additional_context="Implemented your suggestions, now seeing high memory usage with ChromaDB"
  351. )
  352. ```
  353.  
  354. **Key capabilities:**
  355.  
  356. - Persistent conversation sessions with context retention
  357. - File attachment and caching for multi-file analysis
  358. - Specialized assistance modes (solution, review, debug, optimize, explain)
  359. - Session management for complex, multi-step problems
  360.  
  361. **Important:** Treat Gemini's responses as advisory feedback. Evaluate the suggestions critically, incorporate valuable insights into your solution, then proceed with your implementation.
  362.  
  363. ### Context7 Documentation Server
  364.  
  365. **Repository**: [Context7 MCP Server](https://github.com/upstash/context7)
  366.  
  367. **When to use:**
  368.  
  369. - Working with external libraries/frameworks (React, FastAPI, Next.js, etc.)
  370. - Need current documentation beyond training cutoff
  371. - Implementing new integrations or features with third-party tools
  372. - Troubleshooting library-specific issues
  373.  
  374. **Usage patterns:**
  375.  
  376. ```python
  377. # Resolve library name to Context7 ID for relevant libraries
  378. mcp__context7__resolve_library_id(libraryName="fastapi")
  379. mcp__context7__resolve_library_id(libraryName="chromadb")
  380.  
  381. # Fetch focused documentation
  382. mcp__context7__get_library_docs(
  383. context7CompatibleLibraryID="/tiangolo/fastapi",
  384. topic="async-endpoints",
  385. tokens=8000
  386. )
  387.  
  388. mcp__context7__get_library_docs(
  389. context7CompatibleLibraryID="/chroma-core/chroma",
  390. topic="collections",
  391. tokens=8000
  392. )
  393. ```
  394.  
  395. **Key capabilities:**
  396.  
  397. - Up-to-date library documentation access
  398. - Topic-focused documentation retrieval
  399. - Support for specific library versions
  400. - Integration with current development practices
  401.  
  402.  
  403.  
  404. ## 7. Development Commands & Verification
  405.  
  406. ### Essential Development Commands
  407.  
  408. ```bash
  409. # Start the main application
  410. python main.py
  411.  
  412. # Run security feature tests
  413. python test_security.py
  414.  
  415. # Run comprehensive functionality tests
  416. python test_newsdb.py
  417.  
  418. # Test MCP integration
  419. python test_mcp_integration.py
  420.  
  421. # Verify MCP setup
  422. ./verify_mcp_setup.sh
  423.  
  424. # Type checking
  425. mypy src/
  426.  
  427. # Health check (when server running)
  428. curl http://localhost:8000/health
  429.  
  430. # Test authenticated endpoint (requires API_KEY_VALUE set)
  431. curl -H "Authorization: Bearer your-api-key" http://localhost:8000/stats
  432. ```
  433.  
  434. ### Security Configuration Commands
  435.  
  436. ```bash
  437. # Copy environment template
  438. cp .env.template .env
  439.  
  440. # Edit environment variables for security
  441. # Set API_KEY_VALUE, CORS_ORIGINS, rate limits, etc.
  442. vim .env
  443.  
  444. # Test security features
  445. python test_security.py
  446.  
  447. # Monitor security logs (if running)
  448. tail -f logs/app.log | grep -E "auth|rate|error"
  449. ```
  450.  
  451. ### Post-Task Completion Protocol
  452.  
  453. After completing any coding task, follow this checklist:
  454.  
  455. ### 1. Security & Quality Assurance
  456.  
  457. - **Security testing**: Run `python test_security.py` to verify all security features
  458. - **Type checking**: Run `mypy src/` to ensure all type hints are valid
  459. - **Core functionality**: Execute `python test_newsdb.py` to verify core functionality
  460. - **MCP integration**: Run `python test_mcp_integration.py` to test Claude Code integration
  461. - **API health**: Verify `curl http://localhost:8000/health` returns healthy status
  462.  
  463. ### 2. Security Feature Verification
  464.  
  465. - **Authentication**: Test API key authentication on protected endpoints
  466. - **Rate limiting**: Verify rate limits are enforced correctly
  467. - **CORS policy**: Test cross-origin request handling
  468. - **Error boundaries**: Confirm proper error handling and logging
  469. - **Input validation**: Test with malformed inputs to verify validation
  470.  
  471. ### 3. Functional Verification
  472.  
  473. - Ensure all API endpoints respond correctly with authentication
  474. - Test vector operations with real data samples
  475. - Validate MCP tool functionality in Claude Code
  476. - Confirm search performance meets sub-50ms targets
  477. - Test error scenarios and recovery mechanisms
  478. - If errors are found, fix them before marking the task complete
  479.  
  480. ### 4. Production Readiness
  481.  
  482. - Verify security configuration is production-appropriate
  483. - Test with realistic load patterns and rate limits
  484. - Confirm monitoring and logging are working correctly
  485. - Validate error handling doesn't expose sensitive information
  486. - Ensure all secrets are properly externalized to environment variables
  487.  
  488. ## 8. Implemented System Architecture
  489.  
  490. ### Core Modules (Production Ready with Advanced Features)
  491.  
  492. ```
  493. src/
  494. ├── api/ # FastAPI endpoints with security and content management ✅ PRODUCTION READY
  495. │ ├── main.py # 13 API endpoints including duplicate detection and categorization
  496. │ ├── auth.py # API key authentication and security middleware
  497. │ └── rate_limiter.py # Token bucket rate limiting with burst protection
  498. ├── database/ # ChromaDB integration and vector operations ✅ PRODUCTION READY
  499. ├── ingestion/ # Markdown processing, embeddings, and content analysis ✅ PRODUCTION READY
  500. ├── models/ # Comprehensive Pydantic schemas with new feature models ✅ PRODUCTION READY
  501. ├── services/ # Business logic with intelligent content management ✅ PRODUCTION READY
  502. │ ├── news_service.py # Core orchestration and business logic
  503. │ ├── duplicate_detector.py # Hash and semantic duplicate detection
  504. │ └── categorizer.py # AI-powered content categorization (10 categories)
  505. └── utils/ # Configuration and comprehensive error handling ✅ PRODUCTION READY
  506. ├── config.py # Secure configuration management
  507. └── error_handler.py # Comprehensive error boundaries and logging
  508. ```
  509.  
  510. ### Operational Components
  511.  
  512. 1. **Intelligent Ingestion Pipeline**: ✅ Process MD files → Generate embeddings → Detect duplicates → Categorize content → Store in ChromaDB
  513. 2. **Advanced Search API**: ✅ Semantic search with category filtering, duplicate exclusion, and metadata inclusion
  514. 3. **Duplicate Detection System**: ✅ Hash-based exact matching + semantic similarity analysis with configurable thresholds
  515. 4. **Content Categorization**: ✅ AI-powered categorization into 10 content types with topic extraction
  516. 5. **File Management**: ✅ Upload, validation, metadata extraction, and intelligent content analysis
  517. 6. **Enhanced API Layer**: ✅ 13 RESTful endpoints with authentication, rate limiting, CORS, and content management
  518. 7. **Security Layer**: ✅ API key authentication, rate limiting, comprehensive error boundaries
  519. 8. **MCP Integration**: ✅ Real-time knowledge base for Claude Code with enhanced search capabilities
  520. 9. **Analytics & Monitoring**: ✅ Content statistics, category distribution, duplicate tracking, and system health
  521. 10. **Error Handling**: ✅ Comprehensive error boundaries with structured security-aware logging
  522.  
  523. ### Production Technology Stack
  524.  
  525. - **FastAPI**: ✅ Async API framework with 13 endpoints, security middleware, sub-50ms responses
  526. - **ChromaDB**: ✅ Vector database with 955+ documents, metadata, and intelligent organization
  527. - **Sentence Transformers**: ✅ all-MiniLM-L6-v2 model for embeddings and similarity analysis
  528. - **Content Intelligence**: ✅ Duplicate detection (hash + semantic) and AI categorization (10 categories)
  529. - **Pydantic**: ✅ Complete data validation with advanced feature models and security-aware schemas
  530. - **Security Features**: ✅ API authentication, rate limiting, CORS, comprehensive error boundaries
  531. - **Testing Suite**: ✅ Security, functionality, feature integration, and MCP integration coverage
  532. - **MCP Protocol**: ✅ Claude Code integration with 3 operational tools and enhanced search
  533. - **Analytics**: ✅ Real-time content statistics, category distribution, and duplicate tracking
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement