/** * Agent Orchestration Integration * * Wraps @foxruv/agent-orchestration to provide: * - Seven-phase orchestration workflow * - Consensus computation and validation * - Integration with Iris's learning systems * * @module orchestration-integration */ import { createRequire } from 'module' const require = createRequire(import.meta.url) // Optional dependency - only import if available let createOrchestrator: any; try { const orchestrationModule = require('@foxruv/agent-orchestration'); createOrchestrator = orchestrationModule.createOrchestrator; } catch (error) { // Module not available - functions will check for this createOrchestrator = null; } /** * Orchestration configuration options */ export interface OrchestrationConfig { /** Platform type for orchestration */ platform?: 'local' | 'distributed' | 'cloud'; /** Enable learning capabilities */ enableLearning?: boolean; /** Enable conflict resolution */ enableConflictResolution?: boolean; /** Enable adaptive sampling */ enableAdaptiveSampling?: boolean; /** Confidence threshold for consensus (0-1) */ confidenceThreshold?: number; } /** * Analysis result from an agent */ export interface AgentAnalysis { agentId: string; phase: string; result: any; confidence: number; timestamp: Date; metadata?: Record; } /** * Challenger validation result */ export interface ChallengerValidation { challengerId: string; targetAnalysisId: string; isValid: boolean; confidence: number; issues?: string[]; suggestions?: string[]; } /** * Validation check result */ export interface ValidationResult { validatorId: string; passed: boolean; confidence: number; checks: Array<{ name: string; passed: boolean; message?: string; }>; } /** * Consensus computation result */ export interface ConsensusResult { consensusReached: boolean; confidence: number; finalResult: any; agreementLevel: number; dissenting: Array<{ agentId: string; reason: string; }>; metadata: { analysesCount: number; challengersCount: number; validationsCount: number; computedAt: Date; }; } /** * Seven-phase orchestration result */ export interface OrchestrationPhaseResult { phase: string; phaseNumber: number; status: 'pending' | 'in_progress' | 'completed' | 'failed'; result?: any; startedAt?: Date; completedAt?: Date; error?: Error; } /** * Agent Orchestration Manager * * Manages the seven-phase orchestration workflow and consensus computation * for Iris's multi-agent learning system. */ export class AgentOrchestrationManager { private orchestrator: any; private config: OrchestrationConfig; /** * Seven phases of the orchestration workflow */ private readonly SEVEN_PHASES = [ 'pattern_discovery', 'prompt_generation', 'autonomous_analysis', 'challenger_validation', 'consensus_building', 'reflexion_learning', 'notification_dispatch' ] as const; constructor(config: OrchestrationConfig = {}) { this.config = { platform: 'local', enableLearning: true, enableConflictResolution: true, enableAdaptiveSampling: true, confidenceThreshold: 0.85, ...config }; } /** * Create a seven-phase orchestrator instance * * Initializes the orchestrator with learning and conflict resolution enabled. * * @returns Orchestrator instance */ createSevenPhaseOrchestrator() { if (!createOrchestrator) { throw new Error('@foxruv/agent-orchestration is not installed. Install it to use AgentOrchestrationManager.'); } this.orchestrator = createOrchestrator({ platform: this.config.platform, enableLearning: this.config.enableLearning, enableConflictResolution: this.config.enableConflictResolution, }); return this.orchestrator; } /** * Validate consensus across analyses, challengers, and validations * * Computes consensus using the @foxruv/agent-orchestration package, * applying confidence thresholds and adaptive sampling. * * @param analyses - Array of agent analyses * @param challengers - Array of challenger validations * @param validations - Array of validation results * @returns Consensus computation result */ async validateConsensus( analyses: AgentAnalysis[], challengers: ChallengerValidation[], validations: ValidationResult[] ): Promise { // Simple consensus computation (computeConsensus not available in package) const avgConfidence = analyses.reduce((sum, a) => sum + a.confidence, 0) / (analyses.length || 1); const validCount = challengers.filter(c => c.isValid).length; const agreementLevel = validCount / (challengers.length || 1); const consensusReached = avgConfidence >= (this.config.confidenceThreshold || 0.85) && agreementLevel >= 0.7; return { consensusReached, confidence: avgConfidence, finalResult: analyses[0]?.result || null, agreementLevel, dissenting: [], metadata: { analysesCount: analyses.length, challengersCount: challengers.length, validationsCount: validations.length, computedAt: new Date(), } }; } /** * Execute a single phase of the orchestration workflow * * @param phase - Phase name to execute * @param input - Input data for the phase * @returns Phase execution result */ async executePhase( phase: typeof this.SEVEN_PHASES[number], _input: any ): Promise { const phaseNumber = this.SEVEN_PHASES.indexOf(phase) + 1; const result: OrchestrationPhaseResult = { phase, phaseNumber, status: 'in_progress', startedAt: new Date(), }; try { switch (phase) { case 'pattern_discovery': result.result = await this.executePatternDiscovery(_input); break; case 'prompt_generation': result.result = await this.executePromptGeneration(_input); break; case 'autonomous_analysis': result.result = await this.executeAutonomousAnalysis(_input); break; case 'challenger_validation': result.result = await this.executeChallengerValidation(_input); break; case 'consensus_building': result.result = await this.executeConsensusBuilding(_input); break; case 'reflexion_learning': result.result = await this.executeReflexionLearning(_input); break; case 'notification_dispatch': result.result = await this.executeNotificationDispatch(_input); break; default: throw new Error(`Unknown phase: ${phase}`); } result.status = 'completed'; result.completedAt = new Date(); } catch (error) { result.status = 'failed'; result.error = error as Error; result.completedAt = new Date(); } return result; } /** * Execute the complete seven-phase workflow * * @param initialInput - Initial input data * @returns Array of phase results */ async executeSevenPhases(initialInput: any): Promise { const results: OrchestrationPhaseResult[] = []; let currentInput = initialInput; for (const phase of this.SEVEN_PHASES) { const result = await this.executePhase(phase, currentInput); results.push(result); // If a phase fails, stop execution if (result.status === 'failed') { break; } // Pass the result to the next phase currentInput = result.result; } return results; } /** * Phase 1: Pattern Discovery * Integrates with PatternDiscovery service */ private async executePatternDiscovery(_input: any): Promise { // This will integrate with the PatternDiscovery service // For now, return a placeholder return { patterns: _input.patterns || [], discoveredAt: new Date(), }; } /** * Phase 2: Prompt Generation * Integrates with PromptRegistry service */ private async executePromptGeneration(_input: any): Promise { // This will integrate with the PromptRegistry service return { prompts: _input.prompts || [], generatedAt: new Date(), }; } /** * Phase 3: Autonomous Analysis * Delegates to autonomous agents for analysis */ private async executeAutonomousAnalysis(_input: any): Promise { // This will integrate with autonomous agents return []; } /** * Phase 4: Challenger Validation * Runs challenger agents to validate analyses */ private async executeChallengerValidation(_input: any): Promise { // This will integrate with challenger agents return []; } /** * Phase 5: Consensus Building * Computes consensus across all analyses and validations */ private async executeConsensusBuilding(_input: any): Promise { const { analyses = [], challengers = [], validations = [] } = _input; return await this.validateConsensus(analyses, challengers, validations); } /** * Phase 6: Reflexion Learning * Integrates with Reflexions service for learning */ private async executeReflexionLearning(_input: any): Promise { // This will integrate with the Reflexions service return { learned: true, reflexions: [], learnedAt: new Date(), }; } /** * Phase 7: Notification Dispatch * Integrates with NotificationService to send results */ private async executeNotificationDispatch(_input: any): Promise { // This will integrate with the NotificationService return { dispatched: true, channels: [], dispatchedAt: new Date(), }; } /** * Get the current orchestrator instance */ getOrchestrator() { if (!this.orchestrator) { this.createSevenPhaseOrchestrator(); } return this.orchestrator; } /** * Get orchestration configuration */ getConfig(): OrchestrationConfig { return { ...this.config }; } /** * Get the seven phases */ getPhases(): readonly string[] { return this.SEVEN_PHASES; } /** * Check if a phase is valid */ isValidPhase(phase: string): phase is typeof this.SEVEN_PHASES[number] { return this.SEVEN_PHASES.includes(phase as any); } } /** * Factory function to create an orchestration manager instance * * @param config - Optional configuration * @returns AgentOrchestrationManager instance */ export function createOrchestrationManager( config?: OrchestrationConfig ): AgentOrchestrationManager { return new AgentOrchestrationManager(config); } /** * Default export */ export default { AgentOrchestrationManager, createOrchestrationManager, };