Skip to main content

ML Package Unified Services

This document describes the implementation of the unified services architecture in the ml package. The unified services architecture completely removes backward compatibility layers and uses the unified services directly.

Implementation Overview

The ml package has been updated to use the unified services from the shared package directly. The following changes were made:

  1. Removed duplicate files:

    • Removed logger.ts - Replaced with unified logger
    • Removed mcp-integration.ts - Replaced with unified MCP client
  2. Created a unified services export:

    • Created services/index.ts to export all unified services from the shared package
  3. Updated components to use unified services directly:

    • Updated property-specific-training.ts to use the unified logger
    • Updated material-specific-training.ts to use the unified logger
    • Updated material-specific-ocr.ts to use the unified logger

Unified Services Export

The services/index.ts file exports all the unified services from the shared package for use throughout the ml package. It also provides a function to initialize all services.

/**
* Unified Services Export
*
* This file exports all the unified services from the shared package
* for use throughout the ml package.
*/

// Export auth service
export {
auth,
initializeAuth,
User,
LoginCredentials,
RegisterCredentials,
AuthResult
} from '@kai/shared';

// Export API client
export {
apiClient,
createApiClient,
ApiClientConfig,
ApiError,
BaseService,
ServiceConfig
} from '@kai/shared';

// Export MCP client
export {
mcpClient,
createMCPClient
} from '@kai/shared';

// Export Supabase client
export {
supabase
} from '@kai/shared';

// Export storage service
export {
storage,
initializeStorage,
StorageProvider
} from '@kai/shared';

// Export logger
export {
createLogger,
LogLevel
} from '@kai/shared';

// Export config
export {
config
} from '@kai/shared';

/**
* Initialize all services
*
* This function initializes all the unified services.
* It should be called early in the application lifecycle.
*/
export function initializeServices(): void {
// Initialize auth service
initializeAuth();

// Initialize storage service
initializeStorage();

// Log that services have been initialized
const logger = createLogger('Services');
logger.info('Unified services initialized');
}

/**
* Check if MCP server is enabled
*
* @returns Whether MCP server is enabled
*/
export function isMCPEnabled(): boolean {
return config.get('ml.useMcpServer');
}

/**
* Check if MCP server is available
*
* @returns Whether MCP server is available
*/
export async function checkMCPServerAvailability(): Promise<boolean> {
try {
const client = createMCPClient();
await client.checkHealth();
return true;
} catch (error) {
const logger = createLogger('MCP');
logger.error('MCP server health check failed', error as Error);
return false;
}
}

ML Package Entry Point

The index.ts file has been updated to initialize the unified services before exporting any functionality. This ensures that all services are properly initialized before they are used.

/**
* Main entry point for the ML package
* This file exports the functions that will be used by the server package
* to interact with the ML components
*
* The ML package provides functionality for:
* - PDF processing and image extraction
* - Material recognition using feature-based and ML-based approaches
* - Vector embedding generation for similarity search
* - Model training and evaluation
* - Image segmentation for multiple tile detection
* - Feedback loop for improving recognition over time
* - Performance optimization for faster recognition
* - Crawler data integration for training
* - 3D reconstruction and visualization with Gaussian Splatting
* - Improved text-to-3D generation
*/

// Initialize services
import from './services';

// Initialize services on module load
initializeServices();

Updated Components

The following components have been updated to use the unified services:

Property-Specific Training

The property-specific-training.ts file has been updated to use the unified logger from the shared package.

import * as path from 'path';
import * as fs from 'fs';
import from 'child_process';
import from './services';

const logger = createLogger('PropertySpecificTraining');

Material-Specific Training

The material-specific-training.ts file has been updated to use the unified logger from the shared package.

import * as path from 'path';
import * as fs from 'fs';
import from 'child_process';
import from './services';

const logger = createLogger('MaterialSpecificTraining');

Material-Specific OCR

The material-specific-ocr.ts file has been updated to use the unified logger from the shared package.

import axios from 'axios';
import from '../services';

const logger = createLogger('MaterialSpecificOCR');

Benefits

The direct unified services architecture provides several benefits:

  1. Simplified codebase: No more compatibility layers or adapter files
  2. Reduced code duplication: Common functionality is implemented once in the shared package
  3. Improved maintainability: Changes to common functionality only need to be made in one place
  4. Consistent behavior: All parts of the application use the same implementation of common functionality
  5. Type safety: The unified services provide type-safe interfaces for common operations
  6. Extensibility: The provider pattern allows adding new implementations without changing client code

Next Steps

The following steps are recommended to further improve the unified services architecture:

  1. Add more storage providers (Google Cloud Storage, Azure Blob Storage, etc.)
  2. Add more authentication providers (SAML, etc.)
  3. Implement caching mechanisms for improved performance
  4. Add more comprehensive monitoring and telemetry