All files index.js

100% Statements 33/33
90% Branches 9/10
100% Functions 15/15
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223              1x             1x         1x             15x       15x 15x     15x               15x     15x                   1x             7x             1x             9x             8x 8x 8x             6x 6x 1x   5x             2x 2x 2x   2x             2x             1x             1x             16x 16x             1x 1x                           1x                                                                                                       1x                        
/**
 * event-orchestrator - 主入口文件
 * 
 * 基于事件驱动架构 (EDA) 的技能编排器
 * 应用设计模式:EDA、Middleware 链、状态机
 */
 
const { EventBus } = require('./event-bus');
const {
  LoggingMiddleware,
  ValidationMiddleware,
  RetryMiddleware,
  RateLimitMiddleware,
  MiddlewareChainExecutor
} = require('./middleware-chain');
const {
  OrchestrationState,
  StateMachine,
  OrchestrationTask
} = require('./state-machine');
 
/**
 * 事件编排器主类
 */
class EventOrchestrator {
  constructor(options = {}) {
    this.eventBus = new EventBus({
      maxHistorySize: options.maxHistorySize || 1000
    });
    
    this.tasks = new Map();
    this.options = options;
    
    // 注册默认中间件
    this._registerDefaultMiddleware();
  }
 
  /**
   * 注册默认中间件
   */
  _registerDefaultMiddleware() {
    // 日志中间件
    this.eventBus.use(new LoggingMiddleware({ logLevel: 'info' }));
    
    // 速率限制中间件
    this.eventBus.use(new RateLimitMiddleware({
      maxEvents: 100,
      windowMs: 60000
    }));
  }
 
  /**
   * 注册事件 Schema
   */
  registerSchema(eventName, schema) {
    this.eventBus.registerSchema(eventName, schema);
  }
 
  /**
   * 订阅事件
   */
  subscribe(eventName, handler, options = {}) {
    return this.eventBus.subscribe(eventName, handler, options);
  }
 
  /**
   * 取消订阅
   */
  unsubscribe(eventName, subscriberId) {
    return this.eventBus.unsubscribe(eventName, subscriberId);
  }
 
  /**
   * 发布事件
   */
  async publish(eventName, payload = {}, metadata = {}) {
    return await this.eventBus.publish(eventName, payload, metadata);
  }
 
  /**
   * 创建编排任务
   */
  createTask(taskId, definition) {
    const task = new OrchestrationTask(taskId, definition);
    this.tasks.set(taskId, task);
    return task;
  }
 
  /**
   * 获取任务状态
   */
  getTaskStatus(taskId) {
    const task = this.tasks.get(taskId);
    if (!task) {
      return { error: 'Task not found', taskId };
    }
    return task.getStatus();
  }
 
  /**
   * 获取所有任务状态
   */
  getAllTasksStatus() {
    const status = {};
    for (const [taskId, task] of this.tasks.entries()) {
      status[taskId] = task.getStatus();
    }
    return status;
  }
 
  /**
   * 获取事件历史
   */
  getEventHistory(limit = 100, eventName = null) {
    return this.eventBus.getHistory(limit, eventName);
  }
 
  /**
   * 获取订阅者统计
   */
  getSubscriberStats() {
    return this.eventBus.getSubscriberStats();
  }
 
  /**
   * 添加自定义中间件
   */
  useMiddleware(middleware) {
    this.eventBus.use(middleware);
  }
 
  /**
   * 清空所有数据
   */
  clear() {
    this.eventBus.clearHistory();
    this.tasks.clear();
  }
 
  /**
   * 导出状态
   */
  exportState() {
    return {
      tasks: Array.from(this.tasks.entries()).map(([id, task]) => ({
        id,
        status: task.getStatus()
      })),
      eventHistory: this.eventBus.getHistory(1000),
      subscriberStats: this.eventBus.getSubscriberStats(),
      exportedAt: Date.now()
    };
  }
}
 
/**
 * 预定义事件 Schema
 */
const EventSchemas = {
  // 技能执行事件
  'skill.started': {
    skillId: 'string',
    taskId: 'string',
    parameters: 'object'
  },
  'skill.completed': {
    skillId: 'string',
    taskId: 'string',
    result: 'object',
    duration: 'number'
  },
  'skill.failed': {
    skillId: 'string',
    taskId: 'string',
    error: 'string',
    retryCount: 'number'
  },
  
  // 任务编排事件
  'task.created': {
    taskId: 'string',
    definition: 'object',
    priority: 'number'
  },
  'task.started': {
    taskId: 'string',
    startedAt: 'number'
  },
  'task.completed': {
    taskId: 'string',
    completedAt: 'number',
    result: 'object'
  },
  'task.failed': {
    taskId: 'string',
    failedAt: 'number',
    error: 'string'
  },
  
  // 系统事件
  'system.ready': {
    version: 'string',
    timestamp: 'number'
  },
  'system.shutdown': {
    reason: 'string',
    timestamp: 'number'
  }
};
 
module.exports = {
  EventOrchestrator,
  EventBus,
  EventSchemas,
  OrchestrationState,
  StateMachine,
  OrchestrationTask,
  LoggingMiddleware,
  ValidationMiddleware,
  RetryMiddleware,
  RateLimitMiddleware
};