Shared Types Implementation in llm-mcp
Overviewโ
This document describes the implementation of shared types from the shared-utils
directory in the llm-mcp project. The implementation follows a backward-compatible approach that allows existing code to continue working while providing a path to migrate to the shared types.
Implementation Strategyโ
The implementation strategy for llm-mcp follows these principles:
- Extend Shared Types: Legacy types extend shared types where applicable
- Adapter Classes: Adapter classes are provided to convert between project-specific and shared interfaces
- Deprecation Notices: Legacy types are marked as deprecated to encourage migration to shared types
- Enhanced Interfaces: New interfaces are created that extend shared interfaces with llm-mcp-specific functionality
Implemented Changesโ
1. Model Types (types/models.ts
)โ
- Kept the original
ModelConfig
for backward compatibility but marked it as deprecated - Created a new
ExtendedModelConfig
interface that extends sharedModelConfig
with llm-mcp-specific fields - Added conversion functions between legacy and shared types
- Updated
ModelVersion
andModelRegistry
to use the newExtendedModelConfig
// New extended type that incorporates shared type
export interface ExtendedModelConfig extends SharedModelConfig {
type: 'llm' | 'embedding' | 'classifier' | 'generator';
// ...additional llm-mcp-specific fields
}
// Conversion function
export function toSharedModelConfig(config: ModelConfig): SharedModelConfig {
return {
id: config.id,
name: config.name,
// ...mapping fields
};
}
2. AI Provider Interface (interfaces/ai-provider.ts
)โ
- Kept the original
AiProvider
interface for backward compatibility but marked it as deprecated - Imported the shared
AiProvider
interface asSharedAiProvider
- Created a new
EnhancedAiProvider
interface that extendsSharedAiProvider
with llm-mcp-specific methods - Added adapter functions to convert between legacy and shared provider interfaces
// Enhanced AI provider that extends the shared interface
export interface EnhancedAiProvider extends SharedAiProvider {
// MCP-specific methods
processGenerationRequest(request: GenerationRequest): Promise<GenerationResponse>;
processChatRequest(request: ChatRequest): Promise<ChatResponse>;
getProviderMetrics(): Record<string, any>;
getStatus(): 'online' | 'offline' | 'degraded';
getUsageStats(): {
// Usage statistics
};
}
// Adapter functions
export function adaptToSharedProvider(provider: AiProvider): SharedAiProvider {
return provider as SharedAiProvider;
}
export function adaptToLegacyProvider(provider: SharedAiProvider): AiProvider {
return provider as AiProvider;
}
3. AI Streaming Types (interfaces/ai-streaming.ts
)โ
- Kept the original
AiOperationState
andStreamingMetrics
interfaces for backward compatibility - Imported the shared interfaces from shared-utils
- Created adapter classes that implement the shared interfaces using the legacy implementations
- Added comprehensive mapping between legacy and shared interface properties
// Adapter class for operation state
export class AiOperationStateAdapter implements SharedAiOperationState {
private legacyState: AiOperationState;
constructor(legacyState: AiOperationState) {
this.legacyState = legacyState;
}
// Implement shared interface using legacy state
get status(): 'pending' | 'in_progress' | 'completed' | 'failed' | 'cancelled' {
const status = this.legacyState.getStatus();
// Map legacy status to shared status
// ...
}
// ...other properties
}
Migration Pathโ
The implementation provides a clear path for migrating to shared types:
-
Immediate Changes:
- New code should use
ExtendedModelConfig
instead ofModelConfig
- New AI providers should implement
EnhancedAiProvider
- Use adapter classes to integrate with existing code
- New code should use
-
Near-term Changes (1-3 months):
- Update service interfaces to use shared types
- Implement conversion functions where needed for compatibility
- Gradually convert legacy implementations to use the shared types directly
-
Long-term Changes (3-6 months):
- Remove deprecated types and adapter classes
- Use shared types throughout the codebase
- Maintain only llm-mcp-specific extensions of shared types
Key Benefitsโ
This implementation of shared types in llm-mcp provides several benefits:
- Improved Interoperability: Standardized types across services
- Reduced Duplication: Eliminates duplicate type definitions
- Type Safety: Strong typing for cross-service communication
- Extensibility: Domain-specific types can extend shared types
- Backward Compatibility: Existing code continues to work
Next Stepsโ
- Update Core Services: Update service implementations to use shared types
- Update Models Registry: Update the model registry to use
ExtendedModelConfig
- Update AI Providers: Implement
EnhancedAiProvider
in all AI providers - Update Vector Bridge: Update vector-bridge components to use shared vector types
- Documentation: Update project documentation to reference shared types