🏭Factory Pattern Architecture
Understanding NeurosLink AI's unified architecture with BaseProvider inheritance and automatic tool support.
📋 Overview
NeurosLink AI uses a Factory Pattern architecture with BaseProvider inheritance to provide consistent functionality across all AI providers. This design eliminates code duplication and ensures every provider has the same core capabilities, including built-in tool support.
Key Benefits
✅ Zero Code Duplication: Shared logic in BaseProvider
✅ Automatic Tool Support: All providers inherit 6 built-in tools
✅ Consistent Interface: Same methods across all providers
✅ Easy Provider Addition: Minimal code for new providers
✅ Centralized Updates: Fix once, apply everywhere
🏗️ Architecture Components
1. BaseProvider (Core Foundation)
The BaseProvider class is the foundation of all AI providers:
// src/lib/core/baseProvider.ts
export abstract class BaseProvider implements LanguageModelV1 {
// Core properties
readonly specVersion = "v1";
readonly defaultObjectGenerationMode = "tool";
// Abstract methods that providers must implement
abstract readonly provider: string;
abstract doGenerate(request: LanguageModelV1CallRequest): PromiseOrValue<...>;
abstract doStream(request: LanguageModelV1CallRequest): PromiseOrValue<...>;
// Shared tool management
protected tools: Map<string, SimpleTool> = new Map();
// Built-in tools available to all providers
constructor() {
this.registerBuiltInTools();
}
// Tool registration shared by all providers
registerTool(name: string, tool: SimpleTool): void {
this.tools.set(name, tool);
}
// Generate with tool support
async generate(options: GenerateOptions): Promise<GenerateResult> {
// Common logic for all providers
// Including tool execution, analytics, evaluation
}
}2. Provider-Specific Implementation
Each provider extends BaseProvider with minimal code:
3. Factory Pattern Implementation
The factory creates providers with consistent configuration:
🔧 Built-in Tool System
Tool Registration in BaseProvider
All providers automatically get these tools:
Tool Conversion for AI Models
BaseProvider converts tools to provider-specific format:
🌟 Factory Pattern Benefits
1. Consistent Provider Creation
2. Easy Provider Addition
Adding a new provider requires minimal code:
3. Centralized Feature Addition
Add features once in BaseProvider, all providers get them:
📊 Architecture Diagram
🎯 Design Principles
1. Single Responsibility
Each component has one clear purpose:
BaseProvider: Core functionality and tool management
Provider Classes: Provider-specific API integration
Factory: Provider instantiation
Registry: Provider registration and lookup
2. Open/Closed Principle
Open for extension: Easy to add new providers
Closed for modification: Core logic doesn't change
3. Dependency Inversion
Providers depend on BaseProvider abstraction
High-level modules don't depend on low-level details
4. Interface Segregation
Clean, minimal interface for each provider
Only implement what's needed
🔄 Request Flow
Here's how a request flows through the architecture:
💡 Real-World Benefits
Before Factory Pattern (Old Architecture)
After Factory Pattern (Current Architecture)
🚀 Future Extensibility
The factory pattern makes it easy to add new features:
1. New Tool Categories
2. Provider Capabilities
3. Middleware System
📚 Code Examples
Creating Providers
Using Built-in Tools
Extending with Custom Tools
🏆 Summary
The Factory Pattern architecture provides:
Unified Experience: All providers work the same way
Automatic Tools: 6 built-in tools for every provider
Easy Extension: Add providers with minimal code
Clean Code: No duplication, clear separation
Future-Proof: Easy to add new features
This architecture ensures NeurosLink AI remains maintainable, extensible, and consistent as new AI providers and features are added.
Understanding the architecture helps you build better AI applications! 🚀
Last updated
Was this helpful?

