Skip to main content

Agents Package Unified Services

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

Implementation Overview

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

  1. Removed duplicate files:

    • Removed authService.ts - Replaced with unified auth service
    • Removed baseService.ts - Replaced with unified API client
    • Removed logger.ts - Replaced with unified logger
    • Removed environment.ts - Replaced with unified config
  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 agentSystem.ts to use the unified auth service, logger, and config
    • Created activityLogger.ts to provide specialized logging for agent activities

Unified Services Export

The services/index.ts file exports all the unified services from the shared package for use throughout the agents 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 agents 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');
}

Agent System

The agentSystem.ts file has been updated to use the unified services directly. It now imports the auth service, logger, and config from the shared package and uses them for all operations.

Key changes:

  • Imports the unified auth service, logger, and config from the shared package
  • Uses the unified auth service for authentication
  • Uses the unified config for configuration
  • Uses the unified logger for logging

Activity Logger

The activityLogger.ts file provides specialized logging for agent activities. It uses the unified logger from the shared package and adds agent-specific context to log entries.

/**
* Agent Activity Logger
*
* Provides specialized logging for agent activities, including task execution,
* agent creation, and other agent-related events.
*/

import from '../services';

// Create a specialized logger for agent activities
const activityLogger = createLogger('AgentActivity');

/**
* Log agent activity
*
* @param agentId - ID of the agent
* @param activity - Activity details
*/
export function logAgentActivity(
agentId: string,
activity: {
action: 'agent_creation' | 'agent_deletion' | 'task_execution' | 'task_completion' | 'error';
status: 'start' | 'success' | 'error' | 'warning';
details?: Record<string, any>;
error?: Error;
}
): void {
const { action, status, details, error } = activity;

// Create a structured log entry
const logEntry = {
agentId,
action,
status,
timestamp: new Date().toISOString(),
...details
};

// Log at the appropriate level based on status
switch (status) {
case 'start':
activityLogger.info(`Agent ${agentId} $ started`, logEntry);
break;
case 'success':
activityLogger.info(`Agent $ $ succeeded`, logEntry);
break;
case 'warning':
activityLogger.warn(`Agent $ $ warning`, logEntry);
break;
case 'error':
activityLogger.error(`Agent $ $ failed`, error, logEntry);
break;
}
}

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. Update the ml package to use the unified services directly
  2. Add more storage providers (Google Cloud Storage, Azure Blob Storage, etc.)
  3. Add more authentication providers (SAML, etc.)
  4. Implement caching mechanisms for improved performance
  5. Add more comprehensive monitoring and telemetry