Documentation
Developer Reference/Architecture Reference

Architecture Reference

This document provides an in-depth look at ThinkCode's internal architecture, helping developers understand how the platform is structured and how different components interact with each other. Understanding this architecture is valuable for developing extensions, integrating with ThinkCode, or contributing to the core codebase.

System Overview

ThinkCode is built on a modular, extensible architecture that combines the power of Visual Studio Code with advanced AI capabilities. The system is designed to be:

  • Extensible: Supporting custom extensions and integrations
  • Performant: Minimizing overhead for AI processing
  • Secure: Protecting user code and data
  • Adaptive: Learning from user interactions
  • Cross-platform: Working consistently across operating systems

Component Architecture

ThinkCode's components are organized into several layers:

Core System Layer

The foundation of ThinkCode, providing essential services and interfaces:

ComponentDescription
Editor EngineVSCode-based text editing capabilities
Extension HostIsolated process for running extensions
UI FrameworkComponent system for building interface elements
File SystemAccess to local and remote files
Settings ServiceConfiguration management
TelemetryOptional usage data collection

AI Integration Layer

Components that enable integration with AI models and services:

ComponentDescription
Think EngineCore AI processing and orchestration
Context RegistryCentral store for contextual information
Model ManagerHandles loading and versioning of AI models
Prompt EngineConstructs effective prompts based on context
Response ParserProcesses and structures AI responses
Caching ServiceOptimizes performance through caching

Feature Layer

High-level features built on top of the core and AI layers:

ComponentDescription
AI Coding AssistantIntelligent code completion and generation
Context ExplorerVisualization of available context
Test GeneratorAI-assisted test creation
Documentation GeneratorAutomated documentation tools
Code AnalyzerDeep code analysis capabilities
Role ManagerManages AI personas and capabilities

Extension Layer

User-installable components that extend ThinkCode's capabilities:

ComponentDescription
Language ExtensionsSupport for additional programming languages
Custom Context ProvidersSources of specialized context
UI ExtensionsAdditional interface components
Workflow ExtensionsCustom development workflows
Integration ExtensionsConnections to external services

Component Diagram

┌─────────────────────────────────────────────────────────┐
│                      ThinkCode IDE                       │
├─────────────┬─────────────────────────┬─────────────────┤
│ VSCode Core │    ThinkCode Features   │    Extensions   │
├─────────────┘                         │                 │
│ ┌───────────────────┐ ┌─────────────┐ │ ┌─────────────┐ │
│ │   Think Engine    │ │    Roles    │ │ │  Language   │ │
│ └───────────────────┘ └─────────────┘ │ │  Support    │ │
│ ┌───────────────────┐ ┌─────────────┐ │ └─────────────┘ │
│ │  Context System   │ │  Knowledge  │ │ ┌─────────────┐ │
│ └───────────────────┘ │    Base     │ │ │   Custom    │ │
│ ┌───────────────────┐ └─────────────┘ │ │  Providers  │ │
│ │ Extension Manager │ ┌─────────────┐ │ └─────────────┘ │
│ └───────────────────┘ │  Workflow   │ │ ┌─────────────┐ │
│                       │   Engine    │ │ │ UI Elements │ │
│                       └─────────────┘ │ └─────────────┘ │
└───────────────────────────────────────┴─────────────────┘

Data Flow Architecture

ThinkCode's data flow architecture describes how information moves through the system, from user input to AI processing and back to the user interface.

Primary Data Flows

  1. Context Collection Flow

    • User actions trigger context collection
    • Context providers gather relevant information
    • Context is processed, filtered, and prioritized
    • Structured context is stored in the Context Registry
  2. AI Processing Flow

    • User request or automatic trigger initiates AI processing
    • Context is retrieved from Registry and formatted as a prompt
    • Prompt is sent to the appropriate AI model
    • Response is parsed and structured
    • Result is returned to the requesting component
  3. Extension Execution Flow

    • Extension activates based on trigger (command, event, etc.)
    • Extension accesses ThinkCode APIs
    • API calls are processed by respective services
    • Results are returned to the extension
    • UI updates reflect changes

Sequence Diagram Example

Below is a simplified sequence diagram for a code generation request:

┌────────┐       ┌────────────┐       ┌────────────┐       ┌──────────┐       ┌──────────┐
│  User  │       │    IDE     │       │  Context   │       │   Think  │       │  Models  │
│        │       │            │       │  System    │       │  Engine  │       │          │
└────┬───┘       └─────┬──────┘       └─────┬──────┘       └────┬─────┘       └────┬─────┘
     │                  │                    │                   │                  │
     │ Request Code     │                    │                   │                  │
     │─────────────────>│                    │                   │                  │
     │                  │                    │                   │                  │
     │                  │ Gather Context     │                   │                  │
     │                  │───────────────────>│                   │                  │
     │                  │                    │                   │                  │
     │                  │                    │ Return Context    │                  │
     │                  │<───────────────────│                   │                  │
     │                  │                    │                   │                  │
     │                  │ Generate Code      │                   │                  │
     │                  │───────────────────────────────────────>│                  │
     │                  │                    │                   │                  │
     │                  │                    │                   │ Process Request  │
     │                  │                    │                   │─────────────────>│
     │                  │                    │                   │                  │
     │                  │                    │                   │ Return Response  │
     │                  │                    │                   │<─────────────────│
     │                  │                    │                   │                  │
     │                  │ Display Result     │                   │                  │
     │                  │<───────────────────────────────────────│                  │
     │                  │                    │                   │                  │
     │ View Result      │                    │                   │                  │
     │<─────────────────│                    │                   │                  │
     │                  │                    │                   │                  │

Context System Architecture

The Context System is a central component of ThinkCode, responsible for gathering, structuring, and providing contextual information to AI models.

Context Layers

The context system is organized into four main layers:

  1. Source Layer: Raw data collectors that pull information from various sources
  2. Processing Layer: Filters, transforms, and structures the raw context data
  3. Storage Layer: Efficiently stores and indexes context for quick retrieval
  4. Delivery Layer: Formats and delivers context based on specific AI requirements

Context Types

ThinkCode works with several types of context:

Context TypeDescriptionExamples
Code ContextInformation about codeCurrent file, symbols, AST
Project ContextProject-level informationDependencies, structure, config
Environment ContextDevelopment environmentRuntime, OS, tools
User ContextUser-specific informationPreferences, history, expertise
Domain ContextKnowledge about domainsPatterns, best practices

Context Provider Interface

Context providers must implement this interface:

export interface ContextProvider {
  /**
   * Unique identifier for this provider
   */
  readonly id: string;
  
  /**
   * Display name of the provider
   */
  readonly displayName: string;
  
  /**
   * Type of context this provider supplies
   */
  readonly contextType: ContextType;
  
  /**
   * Priority level (higher values = higher priority)
   */
  readonly priority: number;
  
  /**
   * Provide context data based on the request
   */
  provideContext(request: ContextRequest): Promise<ContextData>;
  
  /**
   * Optional method to determine if this provider is relevant
   * for the current request
   */
  isRelevant?(request: ContextRequest): Promise<boolean>;
}

Extension System Architecture

ThinkCode's extension system builds upon VSCode's extension model while adding AI-specific capabilities.

Extension Types

ThinkCode supports several types of extensions:

  1. Standard VSCode Extensions: Compatible with both VSCode and ThinkCode
  2. ThinkCode Feature Extensions: Add new AI-powered features
  3. Context Provider Extensions: Supply additional context to the AI system
  4. AI Model Extensions: Integrate custom AI models and capabilities
  5. UI Extensions: Add new interface elements and visualizations

Extension Host

Extensions run in an isolated process called the Extension Host, which:

  • Provides security through isolation
  • Prevents extensions from affecting editor performance
  • Enables controlled access to ThinkCode APIs
  • Manages extension lifecycle and resources

Extension Activation

Extensions can be activated through various triggers:

  • On startup (specified in the manifest)
  • On command execution
  • When a specific file type is opened
  • In response to events
  • On-demand by other extensions

Extension Points

Extension points are specific areas where ThinkCode's behavior can be customized or extended. These serve as the primary interfaces for extension development.

Core Extension Points

Extension PointDescriptionAPI Namespace
commandsRegister commands that can be triggered by usersthinkcode.commands
contextProvidersAdd new sources of contextthinkcode.exmcp.contextProviders
contextTransformersModify context before AI processingthinkcode.exmcp.contextTransformers
aiModelsIntegrate custom AI modelsthinkcode.ai.models
viewsAdd UI elements to the workbenchthinkcode.window.registerView
fileDecorationsAdd visual indicators to filesthinkcode.window.registerFileDecorationProvider

Example: Registering an Extension Point

// Register a context provider
const myContextProvider: ContextProvider = {
  id: 'my-provider',
  displayName: 'My Custom Provider',
  contextType: ContextType.ProjectMetadata,
  priority: 100,
  
  async provideContext(request) {
    // Implementation
    return {
      type: 'project-metadata',
      data: {
        // Context data
      }
    };
  }
};
 
// Register the provider
const registration = thinkcode.exmcp.contextProviders.register(myContextProvider);
 
// The registration is a disposable that should be added to subscriptions
context.subscriptions.push(registration);

Communication Patterns

ThinkCode uses several communication patterns to enable component interaction:

Event System

Components communicate through a pub-sub event system:

// Publishing events
thinkcode.events.fire('contextChanged', { source: 'git', data: gitContext });
 
// Subscribing to events
const subscription = thinkcode.events.on('contextChanged', (e) => {
  // Handle event
  console.log(`Context changed: ${e.source}`);
});
 
// Subscription is disposable
context.subscriptions.push(subscription);

Message Passing

For cross-process communication, ThinkCode uses structured messages:

// Send message from extension to IDE
await thinkcode.runtime.sendMessage({
  target: 'thinkEngine',
  type: 'request',
  payload: {
    // Message-specific data
  }
});
 
// Listen for messages
thinkcode.runtime.onMessage(message => {
  if (message.type === 'response' && message.source === 'thinkEngine') {
    // Handle response
  }
});

Synchronous APIs

When appropriate, ThinkCode provides synchronous APIs for efficiency:

// Get current editor synchronously
const editor = thinkcode.window.activeTextEditor;
 
// Get configuration synchronously
const config = thinkcode.workspace.getConfiguration('thinkcode');
const aiEnabled = config.get('ai.enabled');

Storage and State Management

ThinkCode provides several mechanisms for storing and managing state:

Extension Storage

Persistent storage for extension data:

// Access extension storage
const storage = thinkcode.storage.getExtensionStorage();
 
// Store data
await storage.set('lastRun', { timestamp: Date.now() });
 
// Retrieve data
const lastRun = await storage.get('lastRun');

Memento Storage

For lighter state persistence:

// Use memento from extension context
const globalState = context.globalState;
await globalState.update('userStats', userStats);
const savedStats = globalState.get('userStats');
 
// Workspace state is scoped to the current workspace
const workspaceState = context.workspaceState;
await workspaceState.update('projectConfig', config);

In-Memory State

For transient state that doesn't need persistence:

// Module-level variables
let sessionContext = {};
 
// StateManager utility
const stateManager = new thinkcode.StateManager();
stateManager.setState('processingFiles', ['file1.ts', 'file2.ts']);
const files = stateManager.getState('processingFiles');

Security Architecture

ThinkCode's security architecture protects both user data and system integrity:

Security Boundaries

  1. Process Isolation: Extensions run in separate processes
  2. Permission System: Extensions must request specific capabilities
  3. Sandboxing: AI execution is contained in secure environments
  4. Data Privacy: Context filtering removes sensitive information

Permission Model

Extensions must declare and request permissions:

// In extension manifest
{
  "permissions": [
    "workspace.readFiles",
    "ai.generateCode",
    "network"
  ],
  "restrictedPermissions": [
    "workspace.writeFiles"
  ]
}

Restricted permissions require explicit user approval:

// Request permission at runtime
const granted = await thinkcode.security.requestPermission(
  'workspace.writeFiles',
  'This extension needs to modify files to generate tests'
);
 
if (granted) {
  // Proceed with file writing
}

Logging and Diagnostics

ThinkCode provides comprehensive logging facilities for debugging and diagnostics:

// Logger available to extensions
const logger = thinkcode.logging.getLogger('my-extension');
 
// Different log levels
logger.debug('Detailed information for debugging');
logger.info('General information about operation');
logger.warn('Warning that might need attention');
logger.error('Error that prevents normal operation', error);
 
// Structured logging
logger.info('Processing file', {
  fileName: document.fileName,
  language: document.languageId,
  size: document.getText().length
});
 
// Performance tracking
const tracker = logger.startPerformanceTracker('generateCode');
// ... perform operation
tracker.end({ additionalInfo: 'some context' });

Best Practices for Working with ThinkCode Architecture

  1. Follow Extension Patterns: Adhere to established patterns for extension development
  2. Respect Security Boundaries: Only request permissions that are necessary
  3. Handle Errors Gracefully: Account for failures in API calls and services
  4. Optimize Context Usage: Be selective about what context you provide or consume
  5. Clean Up Resources: Dispose of subscriptions and registrations appropriately
  6. Document Extension Points: Clearly document extension points you implement or consume

Further Resources