All files state-persistence.js

86.31% Statements 82/95
78.12% Branches 25/32
95.23% Functions 20/21
88.17% Lines 82/93

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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300                    1x 1x 1x                       20x 20x 20x 20x     20x               20x 19x                   46x                   7x                   25x 25x     25x 7x       25x             25x     24x 24x     24x   1x 1x                   12x 12x   12x 3x     9x 9x   9x                                   2x   2x 1x       1x 1x     1x 1x     1x                 7x 7x 7x   7x 7x                       24x 24x 24x 43x         24x                                 2x 2x   2x 2x       2x 2x 2x   2x 2x 2x     2x                       2x 2x 7x   2x 7x 7x 7x   7x                                   1x   1x 4x 4x   4x   4x                   1x 1x   1x                   2x 2x 1x   1x 1x         1x      
/**
 * State Persistence - 状态持久化层
 * 
 * 实现状态持久化,支持任务中断恢复
 * 将任务状态保存到 JSON 文件,支持从断点恢复
 * 
 * 灵感来源:DeerFlow 的 JSON 状态机追踪任务状态,支持中断恢复
 * 应用设计模式:状态机模式 + 仓库模式(Repository Pattern)
 */
 
const fs = require('fs');
const path = require('path');
const { StateMachine, TaskState } = require('./state-machine');
 
/**
 * 状态持久化类
 */
class StatePersistence {
  /**
   * 构造函数
   * @param {string} storagePath - 存储路径
   * @param {Object} options - 配置选项
   */
  constructor(storagePath = './state-storage', options = {}) {
    this.storagePath = storagePath;
    this.autoSave = options.autoSave !== false; // 默认自动保存
    this.backupEnabled = options.backupEnabled !== false; // 默认启用备份
    this.maxBackups = options.maxBackups || 5; // 最大备份数
    
    // 确保存储目录存在
    this._ensureStorageDir();
  }
 
  /**
   * 确保存储目录存在
   * @private
   */
  _ensureStorageDir() {
    if (!fs.existsSync(this.storagePath)) {
      fs.mkdirSync(this.storagePath, { recursive: true });
    }
  }
 
  /**
   * 获取任务状态文件路径
   * @param {string} taskId - 任务 ID
   * @returns {string}
   */
  _getStateFilePath(taskId) {
    return path.join(this.storagePath, `${taskId}-state.json`);
  }
 
  /**
   * 获取备份文件路径
   * @param {string} taskId - 任务 ID
   * @param {number} timestamp - 时间戳
   * @returns {string}
   */
  _getBackupFilePath(taskId, timestamp) {
    return path.join(this.storagePath, `${taskId}-state-${timestamp}.json.bak`);
  }
 
  /**
   * 保存任务状态
   * @param {string} taskId - 任务 ID
   * @param {Object} stateData - 状态数据
   * @returns {boolean}
   */
  save(taskId, stateData) {
    try {
      const filePath = this._getStateFilePath(taskId);
      
      // 如果启用备份且文件已存在,创建备份
      if (this.backupEnabled && fs.existsSync(filePath)) {
        this._createBackup(taskId);
      }
      
      // 写入状态数据
      const data = {
        taskId,
        timestamp: Date.now(),
        state: stateData,
        version: '0.4.0',
      };
      
      fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
      
      // 清理旧备份
      Eif (this.backupEnabled) {
        this._cleanupOldBackups(taskId);
      }
      
      return true;
    } catch (error) {
      console.error(`[StatePersistence] 保存状态失败 [${taskId}]:`, error.message);
      return false;
    }
  }
 
  /**
   * 加载任务状态
   * @param {string} taskId - 任务 ID
   * @returns {Object|null}
   */
  load(taskId) {
    try {
      const filePath = this._getStateFilePath(taskId);
      
      if (!fs.existsSync(filePath)) {
        return null;
      }
      
      const data = fs.readFileSync(filePath, 'utf8');
      const parsed = JSON.parse(data);
      
      return {
        taskId: parsed.taskId,
        savedAt: parsed.timestamp,
        state: parsed.state,
        version: parsed.version,
      };
    } catch (error) {
      console.error(`[StatePersistence] 加载状态失败 [${taskId}]:`, error.message);
      return null;
    }
  }
 
  /**
   * 恢复任务状态机
   * @param {string} taskId - 任务 ID
   * @returns {StateMachine|null}
   */
  restoreStateMachine(taskId) {
    const data = this.load(taskId);
    
    if (!data || !data.state) {
      return null;
    }
    
    // 从保存的状态重建状态机
    const stateData = data.state;
    const machine = new StateMachine(stateData.currentState);
    
    // 恢复历史记录
    Eif (stateData.history) {
      machine.history = stateData.history;
    }
    
    return machine;
  }
 
  /**
   * 创建备份
   * @param {string} taskId - 任务 ID
   * @private
   */
  _createBackup(taskId) {
    const filePath = this._getStateFilePath(taskId);
    const timestamp = Date.now();
    const backupPath = this._getBackupFilePath(taskId, timestamp);
    
    try {
      fs.copyFileSync(filePath, backupPath);
    } catch (error) {
      console.error(`[StatePersistence] 创建备份失败 [${taskId}]:`, error.message);
    }
  }
 
  /**
   * 清理旧备份
   * @param {string} taskId - 任务 ID
   * @private
   */
  _cleanupOldBackups(taskId) {
    try {
      const pattern = new RegExp(`^${taskId}-state-\\d+\\.json\\.bak$`);
      const files = fs.readdirSync(this.storagePath)
        .filter(f => pattern.test(f))
        .sort()
        .reverse(); // 最新的在前
      
      // 删除超出数量的备份
      Iif (files.length > this.maxBackups) {
        files.slice(this.maxBackups).forEach(file => {
          const filePath = path.join(this.storagePath, file);
          fs.unlinkSync(filePath);
        });
      }
    } catch (error) {
      console.error(`[StatePersistence] 清理备份失败 [${taskId}]:`, error.message);
    }
  }
 
  /**
   * 删除任务状态
   * @param {string} taskId - 任务 ID
   * @returns {boolean}
   */
  delete(taskId) {
    try {
      const filePath = this._getStateFilePath(taskId);
      
      Eif (fs.existsSync(filePath)) {
        fs.unlinkSync(filePath);
      }
      
      // 删除所有备份
      const pattern = new RegExp(`^${taskId}-state-\\d+\\.json\\.bak$`);
      const files = fs.readdirSync(this.storagePath)
        .filter(f => pattern.test(f));
      
      files.forEach(file => {
        const filePath = path.join(this.storagePath, file);
        fs.unlinkSync(filePath);
      });
      
      return true;
    } catch (error) {
      console.error(`[StatePersistence] 删除状态失败 [${taskId}]:`, error.message);
      return false;
    }
  }
 
  /**
   * 列出所有保存的任务状态
   * @returns {Array}
   */
  listAll() {
    try {
      const files = fs.readdirSync(this.storagePath)
        .filter(f => f.endsWith('-state.json'));
      
      return files.map(file => {
        const taskId = file.replace('-state.json', '');
        const filePath = path.join(this.storagePath, file);
        const stats = fs.statSync(filePath);
        
        return {
          taskId,
          createdAt: stats.birthtime,
          modifiedAt: stats.mtime,
          size: stats.size,
        };
      });
    } catch (error) {
      console.error('[StatePersistence] 列出状态失败:', error.message);
      return [];
    }
  }
 
  /**
   * 获取可恢复的任务列表
   * @returns {Array}
   */
  listRecoverable() {
    const all = this.listAll();
    
    return all.filter(item => {
      const data = this.load(item.taskId);
      Iif (!data || !data.state) return false;
      
      const state = data.state.currentState;
      // 只有运行中或暂停的任务可以恢复
      return state === TaskState.RUNNING || state === TaskState.PAUSED;
    });
  }
 
  /**
   * 导出状态为 JSON 字符串
   * @param {string} taskId - 任务 ID
   * @returns {string|null}
   */
  exportJSON(taskId) {
    const data = this.load(taskId);
    Iif (!data) return null;
    
    return JSON.stringify(data, null, 2);
  }
 
  /**
   * 从 JSON 字符串导入状态
   * @param {string} taskId - 任务 ID
   * @param {string} jsonString - JSON 字符串
   * @returns {boolean}
   */
  importJSON(taskId, jsonString) {
    try {
      const data = JSON.parse(jsonString);
      return this.save(taskId, data.state);
    } catch (error) {
      console.error(`[StatePersistence] 导入状态失败 [${taskId}]:`, error.message);
      return false;
    }
  }
}
 
module.exports = {
  StatePersistence,
};