JSPM

@farm-framework/code-intelligence

0.2.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q17989F
  • License MIT

AI-powered code intelligence with semantic search and natural language querying

Package Exports

  • @farm-framework/code-intelligence
  • @farm-framework/code-intelligence/client

Readme

@farm-framework/code-intelligence

AI-powered code intelligence with semantic search and natural language querying for the FARM framework.

Features

  • 🧠 Natural Language Queries - Ask questions about your code in plain English
  • 🔍 Semantic Search - Find code by meaning, not just keywords
  • 📖 Code Explanations - Get detailed explanations of complex code
  • 🏗️ Architecture Analysis - Understand your codebase structure
  • 👀 Real-time Updates - Incremental indexing with file watching
  • 🔒 Privacy First - All processing happens locally by default
  • Fast Performance - Optimized vector search with intelligent caching

Quick Start

Installation

The code intelligence package is included in FARM projects by default. To enable it, add configuration to your farm.config.ts:

export default defineConfig({
  codeIntelligence: {
    enabled: true,
    indexing: {
      include: ["src/**/*", "packages/**/*"],
      exclude: ["**/test/**", "**/*.test.*"],
      watch: true,
    },
    ai: {
      provider: "local",
      model: "codellama",
    },
  },
});

First Run

Code Intelligence initializes automatically when you start your FARM project:

farm dev
# ✓ Creating vector database
# ✓ Indexing codebase (found 127 files)
# ✓ Building semantic graph
# ✓ Code Intelligence ready!

Usage

CLI Commands

Search Your Codebase

# Natural language search
farm intel search "user authentication flow"
farm intel search "database connections"
farm intel search "React components with hooks"

# With filters
farm intel search "error handling" --filter-type function --filter-lang typescript

Explain Code

# Get detailed explanations
farm intel explain UserService
farm intel explain "login function"

# With examples and tests
farm intel explain AuthMiddleware --examples --tests

Interactive Assistant

# Start interactive mode
farm intel ask

# Then ask questions:
❯ How does the authentication system work?
❯ What components use the useAuth hook?
❯ Find all database queries
❯ exit

Index Management

# Check index status
farm intel index --status

# Rebuild index
farm intel index --rebuild

# Watch for changes
farm intel index --watch

Find Usages

# Find where code is used
farm intel usages UserService
farm intel usages "login function"

Architecture Overview

# Visualize your codebase
farm intel arch
farm intel arch --format graph

Programmatic API

Client SDK

import { CodeIntelligenceClient } from "@farm-framework/code-intelligence/client";

const client = new CodeIntelligenceClient({
  baseUrl: "http://localhost:8001/api/code-intelligence",
});

// Search
const results = await client.query({
  query: "user authentication",
  maxResults: 10,
  includeContext: true,
});

// Explain
const explanation = await client.explain({
  entityName: "UserService",
  includeExamples: true,
});

// Get status
const status = await client.getStatus();

WebSocket Streaming

import { WebSocketClient } from "@farm-framework/code-intelligence/client";

const wsClient = new WebSocketClient({
  url: "ws://localhost:8001/api/code-intelligence/stream",
});

await wsClient.connect();

// Listen for real-time updates
wsClient.onIndexProgress((progress) => {
  console.log(`Indexing: ${progress.completed}/${progress.total}`);
});

wsClient.onEntityUpdated((entity) => {
  console.log(`Updated: ${entity.name}`);
});

IDE Integration

VS Code Extension

The FARM VS Code extension provides seamless integration:

  • Hover Explanations - Hover over code to see AI explanations
  • Code Lenses - Quick actions for explain and find usages
  • Command Palette - Access all intelligence features
  • Chat Panel - Interactive assistant in the sidebar

Features

  • Ctrl+Shift+P → "FARM: Search Code" - Quick search
  • Ctrl+Shift+P → "FARM: Explain Symbol" - Explain current symbol
  • Ctrl+Shift+P → "FARM: Open AI Chat" - Open chat panel
  • Hover over functions/classes for instant explanations
  • Code lenses show "Explain" and "Find Usages" actions

Configuration

Basic Configuration

// farm.config.ts
export default defineConfig({
  codeIntelligence: {
    enabled: true,

    // What to index
    indexing: {
      include: ["src/**/*", "packages/**/*"],
      exclude: ["**/node_modules/**", "**/dist/**"],
      watch: true,
      incremental: true,
    },

    // AI Configuration
    ai: {
      provider: "local", // 'local' | 'openai' | 'anthropic'
      model: "codellama",
      temperature: 0.1,
    },

    // Privacy settings
    privacy: {
      localOnly: true,
      sanitizeSecrets: true,
      excludeSensitive: true,
    },
  },
});

Advanced Configuration

export default defineConfig({
  codeIntelligence: {
    // Performance tuning
    performance: {
      maxMemory: "4GB",
      parallelism: 8,
      cacheSize: "1GB",
      embedBatchSize: 64,
    },

    // Custom vector storage
    vectorPath: ".farm/custom-intel",

    // API settings
    api: {
      port: 8001,
      host: "localhost",
      cors: true,
      rateLimit: {
        requests: 100,
        window: 60,
      },
    },

    // Advanced privacy
    privacy: {
      secretPatterns: ["api_key", "secret_key", "password", "private_key"],
      allowedFileTypes: [".ts", ".tsx", ".js", ".jsx", ".py", ".md", ".json"],
    },
  },
});

Environment Variables

# Disable watching in CI
FARM_INTEL_DISABLE_WATCH=1

# Custom vector storage path
FARM_INTEL_PATH=/custom/path/.farm/intel

# API configuration
FARM_INTEL_API_PORT=8002
FARM_INTEL_API_KEY=your-secret-key

Examples

Natural Language Queries

# Architecture questions
farm intel search "How does the authentication system work?"
farm intel search "What happens when a user logs in?"
farm intel search "Database connection management"

# Find specific patterns
farm intel search "React components that use useState"
farm intel search "Functions that throw errors"
farm intel search "API routes that require authentication"

# Code quality
farm intel search "Functions with high complexity"
farm intel search "Duplicate code patterns"
farm intel search "Unused imports"

Code Explanation Examples

# Explain complex functions
farm intel explain "processPayment"
# Output: Detailed explanation of payment processing logic,
# including error handling, validation, and integration points

# Explain React components
farm intel explain "UserDashboard"
# Output: Component purpose, props, state management,
# child components, and usage patterns

# Explain with full context
farm intel explain "AuthMiddleware" --context --examples --tests
# Output: Complete explanation with usage examples,
# related tests, and integration points

Integration Examples

Custom Search Integration

// Custom search component
import { CodeIntelligenceClient } from '@farm-framework/code-intelligence/client';

export function CodeSearch() {
  const [query, setQuery] = useState('');
  const [results, setResults] = useState([]);
  const client = new CodeIntelligenceClient();

  const handleSearch = async () => {
    const response = await client.query({
      query,
      maxResults: 20,
      includeContext: true
    });
    setResults(response.results);
  };

  return (
    <div>
      <input
        value={query}
        onChange={(e) => setQuery(e.target.value)}
        placeholder="Search your codebase..."
      />
      <button onClick={handleSearch}>Search</button>

      {results.map(result => (
        <SearchResult key={result.id} result={result} />
      ))}
    </div>
  );
}

Real-time Updates

// Real-time indexing status
import { WebSocketClient } from '@farm-framework/code-intelligence/client';

export function IndexingStatus() {
  const [progress, setProgress] = useState({ completed: 0, total: 0 });
  const wsClient = new WebSocketClient({
    url: 'ws://localhost:8001/api/code-intelligence/stream'
  });

  useEffect(() => {
    wsClient.connect();

    wsClient.onIndexProgress(setProgress);

    return () => wsClient.disconnect();
  }, []);

  const percentage = Math.round((progress.completed / progress.total) * 100);

  return (
    <div>
      <div>Indexing Progress: {percentage}%</div>
      <progress value={progress.completed} max={progress.total} />
      <div>{progress.completed} / {progress.total} files</div>
    </div>
  );
}

Architecture

Overview

┌─────────────────────────────────────────────────────┐
│                   FARM CLI                          │
│  farm intel search | explain | ask | index         │
└─────────────────────┬───────────────────────────────┘
                      │
┌─────────────────────▼───────────────────────────────┐
│              Code Intelligence API                  │
│  Query Engine │ Vector Store │ File Watcher        │
└─────────────────────┬───────────────────────────────┘
                      │
┌─────────────────────▼───────────────────────────────┐
│                Python Backend                       │
│  Parser │ Embeddings │ Semantic Analysis │ AI       │
└─────────────────────────────────────────────────────┘

Components

  • CLI Interface - TypeScript commands integrated with FARM CLI
  • API Layer - FastAPI endpoints for query execution
  • Query Engine - Natural language query planning and execution
  • Vector Store - ChromaDB for semantic search and entity storage
  • File Watcher - Real-time incremental indexing
  • Python Backend - AST parsing, embeddings, and AI integration

Data Flow

  1. Indexing - File watcher detects changes → Python parser extracts entities → Embeddings generated → Vector store updated
  2. Querying - Natural language query → Query planner → Vector search → AI synthesis → Formatted response
  3. Real-time - WebSocket streams for live updates and streaming results

Performance

Benchmarks

  • Initial Indexing - ~1000 files/minute on average hardware
  • Incremental Updates - <100ms per file change
  • Query Response - <500ms for semantic search
  • Memory Usage - ~500MB for medium projects (10K+ files)

Optimization Tips

// Optimize for large codebases
export default defineConfig({
  codeIntelligence: {
    performance: {
      parallelism: 8, // Use more CPU cores
      embedBatchSize: 64, // Larger batches for embeddings
      cacheSize: "2GB", // More cache for better performance
    },
    indexing: {
      batchSize: 200, // Process more files at once
      maxFileSize: 2097152, // 2MB file limit
    },
  },
});

Troubleshooting

Common Issues

Index Not Building

# Check if watcher is disabled
echo $FARM_INTEL_DISABLE_WATCH

# Check index status
farm intel index --status

# Force rebuild
farm intel index --rebuild

Slow Performance

# Check memory usage
farm intel index --stats

# Clear cache
rm -rf .farm/intel

# Optimize configuration

Python Dependencies

# Install required Python packages
pip install chromadb sentence-transformers torch networkx numpy

# Check Python environment
python --version

Debug Mode

# Enable verbose logging
FARM_INTEL_DEBUG=1 farm dev

# Check API logs
curl http://localhost:8001/api/code-intelligence/status

Contributing

See CONTRIBUTING.md for general contribution guidelines.

Development Setup

# Install dependencies
pnpm install

# Build the package
pnpm build

# Run tests
pnpm test

# Start development server
pnpm dev

Testing

# Unit tests
pnpm test

# Integration tests
pnpm test:integration

# End-to-end tests
pnpm test:e2e

License

MIT - see LICENSE for details.

🎉 NEW: AI Code Explanation Feature - WORKING!

We've successfully implemented a fully functional AI-powered code explanation engine!

Try It Now ✅

# Quick demo of the AI explanation feature
cd packages/code-intelligence
npm install
npm run demo

# Explain specific code entities with real AI
npm run test:explanation MockProvider --ollama
npm run test:explanation generateExplanation

# Test file discovery
npm run test:explanation --test-files

What Works Right Now

  • Real AI Integration - Uses Ollama with CodeGemma 7B model
  • AST Code Parsing - Extracts functions, classes, interfaces, types
  • Intelligent Explanations - Comprehensive code analysis with AI
  • Performance Optimized - 8-15 second response times
  • Production Ready - Full error handling and fallback systems
  • CLI Integrated - Working npm scripts and commands

Demo Results: Successfully analyzed 31+ code entities and generated detailed AI explanations!