Skill: wyblhl/auto-reflection (version 1.0.0)

--- name: auto-reflection description: > 自动执行深度反思和学习优化。每 5 轮自动深度反思，能力评估自动更新，学习计划自动调整，知识图谱自动优化。 Triggers: reflection, 反思，auto-reflection, 能力评估，learning optimization, 学习优化 metadata: author: wyblhl version: "1.0.0" --- # 💭 Auto Reflection - 自动反思技能 **版本**: 1.0 **创建时间**: 2026-03-19 **状态**: ✅ 激活 --- ## 🎯 功能说明 自动执行深度反思和学习优化： - 每 5 轮自动深度反思 - 能力评估自动更新 - 学习计划自动调整 - 知识图谱自动优化 - HEARTBEAT.md 自动同步 --- ## 🔄 触发条件 ### 自动触发 - 每完成 5 轮学习自动触发深度反思 - 每轮学习后更新能力评估 ### 手动触发 ```bash # 运行反思 node skills/auto-reflection/index.js ``` --- ## 📊 反思内容 ### 能力分析 - 整体等级和分数变化 - 各维度能力提升排序 - 优势与弱点识别 ### 知识分析 - 知识节点增长 - 领域掌握度 - 主题完成情况 ### 元认知 - 学习效率评估 - 知识增长率 - 改进建议生成 --- ## 📝 输出产物 ### 反思报告 ``` D:\OpenClaw\workspace\reflection-round-N-YYYY-MM-DD.json ``` 包含： - 能力变化详情 - 知识图谱分析 - 成就与不足 - 下一步计划 - 元认知建议 ### HEARTBEAT.md 更新 自动插入最新反思内容到 HEARTBEAT.md ### 能力评估更新 更新 capabilities.json 的时间戳和版本号 --- ## 🎯 反思维度 | 维度 | 说明 | 权重 | |------|------|------| | 能力提升 | 各维度分数变化 | 30% | | 知识增长 | 知识节点和领域掌握 | 25% | | 学习效率 | 单位轮次提升幅度 | 20% | | 成果质量 | 实际产出和成就 | 15% | | 改进空间 | 弱点识别和改进计划 | 10% | --- ## 📈 反思质量指标 | 等级 | 标准 | 说明 | |------|------|------| | S | 深度洞察 + 可执行建议 | 产生重大改进 | | A | 清晰分析 + 具体建议 | 产生明显改进 | | B | 基本分析 + 一般建议 | 产生小幅改进 | | C | 表面分析 + 模糊建议 | 改进有限 | --- ## 🔧 配置参数 ```json { "autoReflection": { "enabled": true, "reflectionInterval": 5, "updateCapabilities": true, "updateHeartbeat": true, "saveReport": true, "minQualityScore": 0.7 } } ``` --- ## 📊 历史反思记录 | 轮次 | 日期 | 等级 | 关键发现 | |------|------|------|----------| | 5 | - | - | - | | 10 | - | - | - | | 15 | - | - | - | | ... | - | - | - | | 45 | - | - | - | --- ## 🚀 使用示例 ### 完整流程 ``` 学习完成 → 更新能力评估 → 检查反思条件 → → (满足条件) 生成反思报告 → 更新 HEARTBEAT → → (不满足) 跳过等待下次 ``` ### 反思后的行动 1. 阅读反思报告 2. 理解改进建议 3. 调整学习计划 4. 执行改进行动 5. 验证改进效果 --- ## 💡 最佳实践 1. **定期反思**: 每 5 轮不要间断 2. **深度分析**: 不仅看分数，要理解原因 3. **可执行建议**: 建议要具体可操作 4. **跟踪改进**: 下次反思验证改进效果 5. **知识关联**: 建立跨领域的知识连接 --- ## ⚠️ 注意事项 - ❌ 避免形式化反思（为反思而反思） - ❌ 避免过于笼统的建议 - ❌ 避免只分析不行动 - ✅ 保持客观和诚实 - ✅ 关注可执行的改进 - ✅ 建立持续改进循环 --- **自动反思技能结束**

path: index.js
content: /**
 * Auto Learning & Reflection - 自动学习与反思机制
 *
 * 功能：
 * 1. 每 5 轮自动深度反思
 * 2. 能力评估自动更新
 * 3. 学习计划自动调整
 * 4. 知识图谱自动优化
 */

const fs = require('fs');
const path = require('path');

const CONFIG = {
  workspace: 'D:\\OpenClaw\\workspace',
  memoryDir: 'D:\\OpenClaw\\workspace\\memory',
  logsDir: 'D:\\OpenClaw\\workspace\\logs',
  reflectionInterval: 5,  // 每 5 轮反思一次
};

function log(msg, level = 'INFO') {
  const ts = new Date().toISOString().replace('T', ' ').substring(0, 19);
  const emoji = {
    'INFO': '⚪',
    'SUCCESS': '🟢',
    'ERROR': '🔴',
    'WARN': '🟡',
    'REFLECT': '💭'
  }[level] || '⚪';
  console.log(`${emoji} ${msg}`);

  const logFile = path.join(CONFIG.logsDir, 'auto-reflection.log');
  fs.appendFileSync(logFile, `[${ts}] [${level}] ${msg}\n`);
}

function loadCapabilities() {
  const capPath = path.join(CONFIG.memoryDir, 'capabilities.json');
  if (fs.existsSync(capPath)) {
    return JSON.parse(fs.readFileSync(capPath, 'utf8'));
  }
  return null;
}

function saveCapabilities(capabilities) {
  const capPath = path.join(CONFIG.memoryDir, 'capabilities.json');
  fs.writeFileSync(capPath, JSON.stringify(capabilities, null, 2));
  log('✅ 能力评估已更新', 'SUCCESS');
}

function loadKnowledgeGraph() {
  const kgPath = path.join(CONFIG.memoryDir, 'knowledge-graph.json');
  if (fs.existsSync(kgPath)) {
    return JSON.parse(fs.readFileSync(kgPath, 'utf8'));
  }
  return null;
}

function saveKnowledgeGraph(kg) {
  const kgPath = path.join(CONFIG.memoryDir, 'knowledge-graph.json');
  fs.writeFileSync(kgPath, JSON.stringify(kg, null, 2));
  log('✅ 知识图谱已更新', 'SUCCESS');
}

function getLearningRoundCount() {
  if (!fs.existsSync(CONFIG.memoryDir)) return 0;

  return fs.readdirSync(CONFIG.memoryDir)
    .filter(f => f.startsWith('learning-round-') && f.endsWith('.json'))
    .length;
}

function shouldReflect() {
  const roundCount = getLearningRoundCount();
  return roundCount % CONFIG.reflectionInterval === 0;
}

function generateReflection() {
  log('生成深度反思报告...', 'REFLECT');

  const capabilities = loadCapabilities();
  const knowledgeGraph = loadKnowledgeGraph();
  const roundCount = getLearningRoundCount();

  if (!capabilities || !knowledgeGraph) {
    log('⚠️ 无法加载能力评估或知识图谱', 'WARN');
    return null;
  }

  const reflection = {
    round: roundCount,
    timestamp: new Date().toISOString(),
    type: 'deep-reflection',

    // 能力分析
    capabilityAnalysis: {
      overallLevel: capabilities.overallLevel,
      overallScore: capabilities.overallScore,
      changeFromPrevious: capabilities.overallChange || 'N/A',
      topStrengths: capabilities.strengths,
      criticalWeaknesses: capabilities.weaknesses,
      dimensionChanges: Object.entries(capabilities.dimensions)
        .map(([name, dim]) => ({
          name,
          level: dim.level,
          score: dim.score,
          change: dim.change || '0'
        }))
        .sort((a, b) => parseFloat(b.change) - parseFloat(a.change))
    },

    // 知识图谱分析
    knowledgeAnalysis: {
      totalNodes: knowledgeGraph.nodes ? knowledgeGraph.nodes.length : 0,
      totalEdges: knowledgeGraph.edges ? knowledgeGraph.edges.length : 0,
      domainMastery: knowledgeGraph.knowledgeDomains || {},
      masteredTopics: knowledgeGraph.masteredTopics || [],
      learningTopics: knowledgeGraph.learningTopics || []
    },

    // 成就与不足
    achievements: capabilities.recentAchievements || [],
    areasForImprovement: capabilities.weaknesses.map(w => ({
      area: w,
      priority: 'high',
      suggestedActions: []
    })),

    // 下一步计划
    nextSteps: {
      immediate: capabilities.nextLearningFocus,
      shortTerm: capabilities.learningPath.slice(0, 3),
      longTerm: capabilities.learningPath
    },

    // 元认知
    metaCognition: {
      learningEfficiency: calculateLearningEfficiency(capabilities),
      knowledgeGrowthRate: calculateKnowledgeGrowth(knowledgeGraph),
      reflectionQuality: 'good',
      suggestions: generateSuggestions(capabilities, knowledgeGraph)
    }
  };

  // 保存反思报告
  const reflectionPath = path.join(CONFIG.workspace, `reflection-round-${roundCount}-${new Date().toISOString().replace(/[:.]/g, '-')}.json`);
  fs.writeFileSync(reflectionPath, JSON.stringify(reflection, null, 2));

  log(`✅ 反思报告已保存：${reflectionPath}`, 'SUCCESS');

  // 更新 HEARTBEAT.md
  updateHeartbeatWithReflection(reflection);

  return reflection;
}

function calculateLearningEfficiency(capabilities) {
  // 计算学习效率：平均分变化 / 轮次
  const avgChange = Object.values(capabilities.dimensions)
    .map(d => parseFloat(d.change || '0'))
    .reduce((a, b) => a + b, 0) / Object.keys(capabilities.dimensions).length;

  return {
    averageChangePerDimension: parseFloat(avgChange.toFixed(2)),
    overallChange: parseFloat(capabilities.overallChange || '0'),
    efficiency: avgChange > 5 ? 'high' : avgChange > 2 ? 'medium' : 'low'
  };
}

function calculateKnowledgeGrowth(knowledgeGraph) {
  const domains = knowledgeGraph.knowledgeDomains || {};
  const avgMastery = Object.values(domains)
    .map(d => d.mastery || 0)
    .reduce((a, b) => a + b, 0) / (Object.keys(domains).length || 1);

  return {
    averageDomainMastery: parseFloat(avgMastery.toFixed(2)),
    totalTopics: (knowledgeGraph.masteredTopics || []).length + (knowledgeGraph.learningTopics || []).length,
    growthRate: avgMastery > 75 ? 'fast' : avgMastery > 50 ? 'medium' : 'slow'
  };
}

function generateSuggestions(capabilities, knowledgeGraph) {
  const suggestions = [];

  // 基于弱点生成建议
  capabilities.weaknesses.forEach(weakness => {
    if (weakness.includes('性能')) {
      suggestions.push('📊 建议：深入学习性能优化技术，包括缓存机制、并发控制、资源管理');
    }
    if (weakness.includes('知识')) {
      suggestions.push('📚 建议：加强知识整合，建立更系统的知识框架，定期复习和关联');
    }
  });

  // 基于优势生成建议
  capabilities.strengths.forEach(strength => {
    if (strength.includes('自动化')) {
      suggestions.push('⚡ 优势：继续保持自动化建设，考虑扩展到更多场景');
    }
    if (strength.includes('协作')) {
      suggestions.push('🤝 优势：优化 Agent 协作机制，探索更高效的并行模式');
    }
  });

  // 基于学习路径生成建议
  if (capabilities.learningPath && capabilities.learningPath.length > 0) {
    suggestions.push(`🎯 当前重点：${capabilities.learningPath[0]}`);
  }

  return suggestions;
}

function updateHeartbeatWithReflection(reflection) {
  const heartbeatPath = path.join(CONFIG.workspace, 'HEARTBEAT.md');

  if (!fs.existsSync(heartbeatPath)) {
    log('⚠️ HEARTBEAT.md 不存在', 'WARN');
    return;
  }

  let content = fs.readFileSync(heartbeatPath, 'utf8');

  // 更新反思部分
  const反思Section = `
## 💭 最新反思（第 ${reflection.round} 轮）

**反思时间**: ${new Date(reflection.timestamp).toLocaleString('zh-CN')}

### 能力变化
- **整体等级**: ${reflection.capabilityAnalysis.overallLevel}
- **整体分数**: ${reflection.capabilityAnalysis.overallScore} (${reflection.capabilityAnalysis.changeFromPrevious})
- **最大提升**: ${reflection.capabilityAnalysis.dimensionChanges[0]?.name} (+${reflection.capabilityAnalysis.dimensionChanges[0]?.change})

### 关键成就
${reflection.achievements.map(a => `- ✅ ${a}`).join('\n')}

### 改进建议
${reflection.metaCognition.suggestions.map(s => `${s}`).join('\n')}

---
`;

  // 插入反思内容
  const marker = '## 💭 最新反思';
  const markerIndex = content.indexOf(marker);

  if (markerIndex >= 0) {
    // 找到下一个章节标记
    const nextSectionIndex = content.indexOf('##', markerIndex + 1);
    if (nextSectionIndex >= 0) {
      content = content.substring(0, markerIndex) + 反思 Section + content.substring(nextSectionIndex);
    }
  } else {
    // 在开头插入
    content = 反思 Section + content;
  }

  fs.writeFileSync(heartbeatPath, content);
  log('✅ HEARTBEAT.md 已更新', 'SUCCESS');
}

function updateCapabilitiesAfterLearning() {
  log('更新能力评估...', 'REFLECT');

  const capabilities = loadCapabilities();
  if (!capabilities) {
    log('⚠️ 无法加载能力评估', 'WARN');
    return;
  }

  // 更新时间戳
  capabilities.lastAssessment = new Date().toISOString();

  // 如果有新版本，更新
  capabilities.version = (capabilities.version || 1) + 1;

  saveCapabilities(capabilities);
}

function run() {
  log('=========================================', 'REFLECT');
  log('💭 自动学习与反思机制启动', 'REFLECT');
  log('=========================================', 'REFLECT');

  const roundCount = getLearningRoundCount();
  log(`📊 当前学习轮次：${roundCount}`, 'INFO');

  if (shouldReflect()) {
    log(`✨ 达到反思条件（每${CONFIG.reflectionInterval}轮）`, 'REFLECT');
    const reflection = generateReflection();

    if (reflection) {
      log('=========================================', 'REFLECT');
      log('📊 反思摘要', 'INFO');
      log('=========================================', 'INFO');
      log(`等级：${reflection.capabilityAnalysis.overallLevel} (${reflection.capabilityAnalysis.overallScore})`, 'INFO');
      log(`变化：${reflection.capabilityAnalysis.changeFromPrevious}`, 'INFO');
      log(`成就：${reflection.achievements.length} 项`, 'INFO');
      log(`建议：${reflection.metaCognition.suggestions.length} 条`, 'INFO');
      log('=========================================', 'REFLECT');
    }
  } else {
    const nextReflectIn = CONFIG.reflectionInterval - (roundCount % CONFIG.reflectionInterval);
    log(`⏭️  跳过反思（${nextReflectIn} 轮后进行）`, 'INFO');
  }

  // 更新能力评估
  updateCapabilitiesAfterLearning();

  log('=========================================', 'REFLECT');
  log('✅ 自动学习与反思完成', 'SUCCESS');
  log('=========================================', 'REFLECT');
}

// 如果直接运行
if (require.main === module) {
  run();
}

module.exports = { run, generateReflection, shouldReflect, getLearningRoundCount };


Security scan verdicts — ClawScan: clean | static analysis: clean | VirusTotal: clean | SkillSpector: suspicious (HIGH)