Skip to content

Latest commit

 

History

History
333 lines (261 loc) · 11.6 KB

File metadata and controls

333 lines (261 loc) · 11.6 KB

OpenAgent Project - Code Summary

📋 Project Overview

OpenAgent (formerly OpenClaude) is an advanced open-source AI development assistant built with TypeScript and Node.js. It provides an intelligent CLI interface for AI-powered development tasks with persistent memory, token optimization, and advanced context management.

Project Stats:

  • Files: 29 total files (22,350 lines of code)
  • Languages: TypeScript, Text/Config files
  • Dependencies: 38 packages including Anthropic SDK, LangChain, SQLite3
  • Architecture: Modular, event-driven system with clear separation of concerns

🏗️ Project Architecture

High-Level Structure

OpenAgent/
├── 🚀 Entry Points        # Application startup
├── 💬 CLI Layer           # User interface & commands  
├── 🤖 Agent System        # Core AI processing
├── 🧠 Memory System       # Persistent learning & context
├── ⚙️  Core Systems       # Context, validation, optimization
├── 🔗 MCP Integration     # Model Context Protocol
├── 🛠️  Tools             # Terminal execution
└── 📝 Types & Prompts     # Type definitions & AI prompts

🚀 Application Entry Points

Main Entry Flow

src/index.ts → src/cli/index.ts → main() → startInteractiveChat()

Key Files:

  • src/index.ts - Primary entry point, imports CLI
  • src/cli/index.ts - CLI bootstrap with main() and startInteractiveChat() functions
  • package.json - Defines dist/index.js as main entry and openagent binary

Startup Process:

  1. main() function initializes the system
  2. Creates OpenAgentManager instance
  3. Sets up SlashCommandHandler for commands
  4. Starts interactive chat loop with readline interface

💬 CLI Layer - User Interface

Core Components

src/cli/index.ts - Main CLI Controller

  • startInteractiveChat() - Interactive readline loop
  • getApiKey() - Secure API key management
  • main() - Application initialization

src/cli/interface/ - UI Components

  • StreamingHandler.ts - Real-time streaming with spinner animations
  • logo.ts - Branding, logos, and status messages
  • components.ts - UI utilities (spinners, progress bars, menus)

src/cli/commands/ - Command System

  • SlashCommandHandler.ts - Processes slash commands (reset, help, status, etc.)
  • BaseCommand.ts - Abstract base for command implementations

Slash Commands Available

  • /help, /h, /? - Show available commands
  • /reset, /clear, /restart - Reset agent and clear history
  • /status, /st - Display agent status and metrics
  • /history, /hist - Show conversation history
  • /new, /thread - Create new conversation thread

🤖 Agent System - AI Processing Core

Main Components

src/agents/OpenAgentManager.ts - Agent Manager

  • Coordinates between CLI and agent instances
  • Handles message processing and response formatting
  • Manages agent lifecycle and configuration
  • Key Method: processMessage(message: string) - Main message handler

src/agents/OpenAgent.ts - Core AI Agent (881 lines)

  • Built on LangChain/LangGraph framework
  • Implements ReAct (Reasoning + Acting) pattern
  • Integrates with Anthropic Claude models
  • Key Features:
    • Streaming response handling
    • Memory integration for context persistence
    • Tool usage coordination
    • Error handling and recovery
    • Token usage tracking

Agent Processing Flow

User Message → OpenAgentManager → OpenAgent → LangGraph ReAct Agent → Claude API → Response Stream → CLI

🧠 Memory System - Persistent Learning

Architecture

src/memory/ - Complete memory subsystem

Core Classes:

  • MemoryManager.ts (1000 lines) - SQLite-based persistent storage
  • MemoryIntegration.ts - Agent-memory interface layer
  • types.ts - Memory-related type definitions
  • index.ts - Memory system exports

Memory Features

  • Persistent Storage: SQLite database with optimized indexes
  • Smart Retrieval: Vector-based semantic search
  • Session Management: Per-session context isolation
  • Performance Optimization: Automatic cleanup and indexing
  • Relationship Mapping: Memory interconnection tracking

Database Schema

-- Core tables with performance indexes
memories (id, content, type, importance, session_id, project_id, created_at, last_accessed)
memory_relationships (source_id, target_id, relationship_type)

-- Performance indexes
idx_memories_type, idx_memories_session, idx_memories_importance, etc.

⚙️ Core Systems

Context Management

src/core/context/ContextManager.ts (1335 lines)

  • Project State Tracking: File changes, dependencies, structure
  • Token-Aware Context: Intelligent context sizing for API limits
  • Thread Management: Multi-conversation support
  • Background Processes: Autosave and health monitoring
  • Performance Metrics: Context hit rates and optimization stats

Token Optimization

src/core/optimization/TokenOptimizer.ts (1259 lines)

  • Smart Compression: Pattern-based content compression
  • Cache Management: Intelligent caching of frequently used content
  • Usage Tracking: Detailed token consumption analytics
  • Background Optimization: Continuous optimization processes
  • Cost Management: Budget tracking and optimization recommendations

Code Validation

src/core/validation/ValidationEngine.ts (1634 lines)

  • Multi-Language Support: JavaScript, Python, Java, TypeScript validation
  • Quality Metrics: Syntax, logic, security, performance analysis
  • Pattern Recognition: Code smells and anti-pattern detection
  • Security Scanning: Vulnerability detection
  • Maintainability Analysis: Complexity and quality scoring

Project Setup

src/core/setup/ProjectSetup.ts (214 lines)

  • Project Initialization: .openagent/ directory structure
  • Configuration Management: Project-specific settings
  • Git Integration: Automatic .gitignore updates
  • Dependency Detection: Framework and library recognition

Token Counting

src/core/tokens/TokenCounter.ts (135 lines)

  • Accurate Estimation: Token count prediction
  • Model-Specific Counting: Different tokenization strategies
  • Performance Optimization: Efficient counting algorithms

🔗 MCP Integration - Model Context Protocol

src/mcp/client/MCPClient.ts (175 lines)

  • Server Management: MCP server lifecycle
  • Tool Integration: External tool access
  • Protocol Handling: MCP message formatting
  • Error Recovery: Robust connection management

MCP Capabilities

  • Connect to external MCP servers
  • Access additional tools and resources
  • Extend agent capabilities dynamically
  • Protocol-compliant communication

🛠️ Tools System

src/tools/terminal.ts (267 lines)

  • Command Execution: Interactive and non-interactive commands
  • System Integration: Cross-platform terminal operations
  • Output Management: Structured command results
  • Timeout Handling: Configurable execution limits
  • Environment Support: Custom environment variables

Terminal Features

  • Interactive Commands: Real-time command execution
  • System Information: OS, platform, and environment detection
  • Execution Metrics: Timing and performance tracking
  • Error Handling: Robust error capture and reporting

📝 Types & Configuration

Type Definitions

src/types/agent.ts (262 lines) - Comprehensive agent type system

  • Agent interfaces and configurations
  • Session management types
  • Response and message structures
  • Event and callback definitions

src/types/mcp.ts (30 lines) - MCP protocol types

  • MCP client and server interfaces
  • Protocol message structures

AI Prompts

src/prompts/openagent_prompt.ts (314 lines) - Core AI behavior

  • System Prompt: Defines agent personality and behavior
  • Engineering Principles: Code quality, security, maintainability standards
  • Workflow Guidelines: Development process and best practices
  • Quality Standards: Professional coding requirements

🔄 Data Flow & Interactions

Message Processing Flow

1. User Input (CLI) → 2. SlashCommandHandler (if slash command) → 3. OpenAgentManager
4. OpenAgentManager → 5. OpenAgent → 6. Memory Integration (context retrieval)
7. Context + Token Optimization → 8. LangGraph ReAct Agent → 9. Claude API
10. Streaming Response → 11. StreamingHandler → 12. CLI Output

Key Integrations

  • Memory ↔ Agent: Persistent context and learning
  • Context ↔ TokenOptimizer: Efficient token usage
  • Validation ↔ Agent: Code quality assurance
  • MCP ↔ Agent: External tool integration
  • Tools ↔ Agent: System command execution

🎯 Key Features & Capabilities

Advanced Features

  1. Persistent Memory: Remembers context across sessions
  2. Token Optimization: Intelligent cost management (40%+ savings)
  3. Real-time Streaming: Professional UI with live updates
  4. Multi-thread Conversations: Separate conversation contexts
  5. Code Validation: Multi-layer quality checking
  6. MCP Integration: Extensible tool ecosystem
  7. Background Processes: Automatic optimization and cleanup

Developer Experience

  • Zero Configuration: Works out of the box
  • Slash Commands: Quick access to functionality
  • Streaming Interface: Real-time feedback
  • Error Recovery: Robust error handling
  • Performance Monitoring: Built-in metrics and optimization

🔧 Configuration & Setup

Project Structure

.openagent/               # Local configuration directory
├── config.json         # Agent configuration  
├── memory/             # Memory storage
├── context/            # Context cache
├── checkpoints/        # Save points
└── logs/              # Operation logs

Environment Variables

  • ANTHROPIC_API_KEY - Required for Claude API access
  • Node.js Environment: ESM modules, TypeScript compilation

Build & Run Commands

npm run build        # TypeScript compilation
npm start           # Run compiled version
npm run dev         # Development mode with ts-node
npm test            # Jest testing
npm run lint        # ESLint code checking

🚀 Getting Started for New Developers

Quick Understanding Checklist

  • Start with src/cli/index.ts - understand the entry point
  • Review src/agents/OpenAgent.ts - core AI processing logic
  • Check src/memory/MemoryManager.ts - persistent storage system
  • Explore src/core/ - understand optimization and validation
  • Look at src/prompts/openagent_prompt.ts - AI behavior definition

Making Changes

  1. CLI Changes: Modify src/cli/ for interface updates
  2. Agent Behavior: Update src/agents/ for AI logic changes
  3. Memory System: Adjust src/memory/ for storage modifications
  4. Core Features: Modify src/core/ for system functionality
  5. New Commands: Add to src/cli/commands/SlashCommandHandler.ts

Testing Your Changes

npm run build          # Compile TypeScript
npm run dev           # Test in development
./dist/cli/index.js   # Test compiled version

📊 Performance & Metrics

Current Project Health

  • Overall Health Score: 100.0/100
  • Files: 29 files, ~22K lines of code
  • Memory Usage: ~12KB during analysis
  • Cache Hit Rate: 80%
  • Token Optimization: Significant savings through smart compression
  • Zero Circular Dependencies: Clean architecture ✅
  • Modular Design: Clear separation of concerns