🏭Factory Pattern Architecture
📋 Overview
Key Benefits
🏗️ Architecture Components
1. BaseProvider (Core Foundation)
// 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
3. Factory Pattern Implementation
🔧 Built-in Tool System
Tool Registration in BaseProvider
Tool Conversion for AI Models
🌟 Factory Pattern Benefits
1. Consistent Provider Creation
2. Easy Provider Addition
3. Centralized Feature Addition
📊 Architecture Diagram
🎯 Design Principles
1. Single Responsibility
2. Open/Closed Principle
3. Dependency Inversion
4. Interface Segregation
🔄 Request Flow
💡 Real-World Benefits
Before Factory Pattern (Old Architecture)
After Factory Pattern (Current Architecture)
🚀 Future Extensibility
1. New Tool Categories
2. Provider Capabilities
3. Middleware System
📚 Code Examples
Creating Providers
Using Built-in Tools
Extending with Custom Tools
🏆 Summary
Last updated
Was this helpful?

