All files state-machine.js

100% Statements 38/38
88.88% Branches 24/27
100% Functions 22/22
100% Lines 38/38

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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252                          2x                         2x                                           38x 38x 38x 38x 38x     38x               94x                           58x 58x               8x                       58x     58x 2x             56x     56x 56x     56x     56x   56x                 32x               6x               5x               7x               2x               3x               2x               18x               6x               2x               2x               8x               3x                           1x 1x 1x       2x          
/**
 * State Machine - 任务状态机
 * 
 * 实现状态机模式,管理任务生命周期
 * 状态转换:pending → running → (completed | failed | cancelled)
 * 
 * 灵感来源:DeerFlow 的 JSON 状态机追踪任务状态
 * 应用设计模式:状态机模式(State Machine Pattern)
 */
 
/**
 * 任务状态枚举
 */
const TaskState = {
  PENDING: 'pending',       // 等待执行
  RUNNING: 'running',       // 执行中
  COMPLETED: 'completed',   // 已完成
  FAILED: 'failed',         // 失败
  CANCELLED: 'cancelled',   // 已取消
  PAUSED: 'paused',         // 已暂停(支持恢复)
};
 
/**
 * 状态转换规则
 * 定义哪些状态转换是允许的
 */
const STATE_TRANSITIONS = {
  [TaskState.PENDING]: [TaskState.RUNNING, TaskState.CANCELLED],
  [TaskState.RUNNING]: [TaskState.COMPLETED, TaskState.FAILED, TaskState.PAUSED, TaskState.CANCELLED],
  [TaskState.PAUSED]: [TaskState.RUNNING, TaskState.CANCELLED],
  [TaskState.COMPLETED]: [], // 终态,不可转换
  [TaskState.FAILED]: [TaskState.PENDING], // 允许重试
  [TaskState.CANCELLED]: [], // 终态,不可转换
};
 
/**
 * 状态机类
 */
class StateMachine {
  /**
   * 构造函数
   * @param {string} initialState - 初始状态
   * @param {Object} options - 配置选项
   * @param {Function} options.onStateChange - 状态变更回调
   * @param {Function} options.onEnter - 进入状态回调
   * @param {Function} options.onExit - 退出状态回调
   */
  constructor(initialState = TaskState.PENDING, options = {}) {
    this.currentState = initialState;
    this.history = [];
    this.onStateChange = options.onStateChange || (() => {});
    this.onEnter = options.onEnter || (() => {});
    this.onExit = options.onExit || (() => {});
    
    // 记录初始状态
    this._recordState(initialState, '初始化');
  }
 
  /**
   * 记录状态历史
   * @private
   */
  _recordState(state, reason = '') {
    this.history.push({
      state,
      timestamp: Date.now(),
      reason,
    });
  }
 
  /**
   * 检查状态转换是否允许
   * @param {string} fromState - 当前状态
   * @param {string} toState - 目标状态
   * @returns {boolean}
   */
  canTransition(fromState, toState) {
    const allowedTransitions = STATE_TRANSITIONS[fromState];
    return allowedTransitions && allowedTransitions.includes(toState);
  }
 
  /**
   * 获取所有允许的状态转换
   * @returns {string[]}
   */
  getAllowedTransitions() {
    return STATE_TRANSITIONS[this.currentState] || [];
  }
 
  /**
   * 转换状态
   * @param {string} newState - 新状态
   * @param {string} reason - 转换原因
   * @param {Object} context - 上下文数据
   * @returns {boolean} 是否成功
   * @throws {Error} 如果状态转换不允许
   */
  transition(newState, reason = '', context = {}) {
    const oldState = this.currentState;
 
    // 检查转换是否允许
    if (!this.canTransition(oldState, newState)) {
      throw new Error(
        `Invalid state transition: ${oldState} → ${newState}. ` +
        `Allowed transitions: ${this.getAllowedTransitions().join(', ')}`
      );
    }
 
    // 调用退出回调
    this.onExit(oldState, newState, context);
 
    // 更新状态
    this.currentState = newState;
    this._recordState(newState, reason);
 
    // 调用进入回调
    this.onEnter(newState, oldState, context);
 
    // 触发状态变更事件
    this.onStateChange(newState, oldState, context);
 
    return true;
  }
 
  /**
   * 开始执行(PENDING → RUNNING)
   * @param {string} reason - 原因
   * @param {Object} context - 上下文
   */
  start(reason = '任务开始执行', context = {}) {
    return this.transition(TaskState.RUNNING, reason, context);
  }
 
  /**
   * 完成任务(RUNNING → COMPLETED)
   * @param {Object} context - 上下文
   */
  complete(context = {}) {
    return this.transition(TaskState.COMPLETED, '任务完成', context);
  }
 
  /**
   * 任务失败(RUNNING → FAILED)
   * @param {Object} context - 上下文
   */
  fail(context = {}) {
    return this.transition(TaskState.FAILED, '任务失败', context);
  }
 
  /**
   * 暂停任务(RUNNING → PAUSED)
   * @param {Object} context - 上下文
   */
  pause(context = {}) {
    return this.transition(TaskState.PAUSED, '任务暂停', context);
  }
 
  /**
   * 恢复任务(PAUSED → RUNNING)
   * @param {Object} context - 上下文
   */
  resume(context = {}) {
    return this.transition(TaskState.RUNNING, '任务恢复', context);
  }
 
  /**
   * 取消任务
   * @param {Object} context - 上下文
   */
  cancel(context = {}) {
    return this.transition(TaskState.CANCELLED, '任务取消', context);
  }
 
  /**
   * 重试任务(FAILED → PENDING)
   * @param {Object} context - 上下文
   */
  retry(context = {}) {
    return this.transition(TaskState.PENDING, '任务重试', context);
  }
 
  /**
   * 获取当前状态
   * @returns {string}
   */
  getState() {
    return this.currentState;
  }
 
  /**
   * 检查是否为终态
   * @returns {boolean}
   */
  isTerminal() {
    return [TaskState.COMPLETED, TaskState.CANCELLED].includes(this.currentState);
  }
 
  /**
   * 检查是否可以恢复
   * @returns {boolean}
   */
  canResume() {
    return this.currentState === TaskState.PAUSED;
  }
 
  /**
   * 检查是否可以重试
   * @returns {boolean}
   */
  canRetry() {
    return this.currentState === TaskState.FAILED;
  }
 
  /**
   * 获取状态历史
   * @returns {Array}
   */
  getHistory() {
    return [...this.history];
  }
 
  /**
   * 导出状态为 JSON
   * @returns {Object}
   */
  toJSON() {
    return {
      currentState: this.currentState,
      history: this.history,
      allowedTransitions: this.getAllowedTransitions(),
      isTerminal: this.isTerminal(),
    };
  }
 
  /**
   * 从 JSON 导入状态
   * @param {Object} json - 状态数据
   * @returns {StateMachine}
   */
  static fromJSON(json) {
    const machine = new StateMachine(json.currentState);
    machine.history = json.history || [];
    return machine;
  }
}
 
module.exports = {
  StateMachine,
  TaskState,
  STATE_TRANSITIONS,
};