refactor
它能做什么
这个工具可以帮你整理和改进代码。它能理解你想要做的修改,分析代码之间的关联,并安全地规划修改方案,避免出错。
适用场景
- ✓当你想简化一段复杂的代码时。
- ✓当你需要把一部分代码提取到一个新模块中时。
- ✓当你想更新或重构一个老项目时。
技能链接
https://github.com/code-yeongyu/oh-my-openagent/blob/dev/packages/shared-skills/skills/refactor/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“把登录函数重构为使用新的身份验证模式。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 会先理解你的需求,然后分析你的全部代码,看看各部分之间如何关联。接着,它会制定详细的计划,逐步进行修改,并检查一切是否仍然正常工作。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
export const REFACTOR_TEMPLATE = `# Intelligent Refactor Command
Usage
``` /refactor <refactoring-target> [--scope=<file|module|project>] [--strategy=<safe|aggressive>]
Arguments: refactoring-target: What to refactor. Can be: - File path: src/auth/handler.ts - Symbol name: "AuthService class" - Pattern: "all functions using deprecated API" - Description: "extract validation logic into separate module"
Options: --scope: Refactoring scope (default: module) - file: Single file only - module: Module/directory scope - project: Entire codebase
--strategy: Risk tolerance (default: safe) - safe: Conservative, maximum test coverage required - aggressive: Allow broader changes with adequate coverage ```
What This Command Does
Performs intelligent, deterministic refactoring with full codebase awareness. Unlike blind search-and-replace, this command:
- Understands your intent - Analyzes what you actually want to achieve
- Maps the codebase - Builds a definitive codemap before touching anything
- Assesses risk - Evaluates test coverage and determines verification strategy
- Plans meticulously - Creates a detailed plan with Plan agent
- Executes precisely - Step-by-step refactoring with LSP and AST-grep
- Verifies constantly - Runs tests after each change to ensure zero regression
PHASE 0: INTENT GATE (MANDATORY FIRST STEP)
BEFORE ANY ACTION, classify and validate the request.
Step 0.1: Parse Request Type
| Signal | Classification | Action |
|---|---|---|
| Specific file/symbol | Explicit | Proceed to codebase analysis |
| "Refactor X to Y" | Clear transformation | Proceed to codebase analysis |
| "Improve", "Clean up" | Open-ended | MUST ask: "What specific improvement?" |
| Ambiguous scope | Uncertain | MUST ask: "Which modules/files?" |
| Missing context | Incomplete | MUST ask: "What's the desired outcome?" |
Step 0.2: Validate Understanding
Before proceeding, confirm:
- Target is clearly identified
- Desired outcome is understood
- Scope is defined (file/module/project)
- Success criteria can be articulated
If ANY of above is unclear, ASK CLARIFYING QUESTION:
``` I want to make sure I understand the refactoring goal correctly.
What I understood: [interpretation] What I'm unsure about: [specific ambiguity]
Options I see:
- [Option A] - [implications]
- [Option B] - [implications]
My recommendation: [suggestion with reasoning]
Should I proceed with [recommendation], or would you prefer differently? ```
Step 0.3: Create Initial Todos
IMMEDIATELY after understanding the request, create todos:
``` TodoWrite([ {"id": "phase-1", "content": "PHASE 1: Codebase Analysis - launch parallel explore agents", "status": "pending", "priority": "high"}, {"id": "phase-2", "content": "PHASE 2: Build Codemap - map dependencies and impact zones", "status": "pending", "priority": "high"}, {"id": "phase-3", "content": "PHASE 3: Test Assessment - analyze test coverage and verification strategy", "status": "pending", "priority": "high"}, {"id": "phase-4", "content": "PHASE 4: Plan Generation - invoke Plan agent for detailed refactoring plan", "status": "pending", "priority": "high"}, {"id": "phase-5", "content": "PHASE 5: Execute Refactoring - step-by-step with continuous verification", "status": "pending", "priority": "high"}, {"id": "phase-6", "content": "PHASE 6: Final Verification - full test suite and regression check", "status": "pending", "priority": "high"} ]) ```
PHASE 1: CODEBASE ANALYSIS (PARALLEL EXPLORATION)
Mark phase-1 as in_progress.
1.1: Launch Parallel Explore Agents (BACKGROUND)
Fire ALL of these simultaneously using `call_omo_agent`:
``` // Agent 1: Find the refactoring target call_omo_agent( subagent_type="explore", run_in_background=true, prompt="Find all occurrences and definitions of [TARGET]. Report: file paths, line numbers, usage patterns." )
// Agent 2: Find related code call_omo_agent( subagent_type="explore", run_in_background=true, prompt="Find all code that imports, uses, or depends on [TARGET]. Report: dependency chains, import graphs." )
// Agent 3: Find similar patterns call_omo_agent( subagent_type="explore", run_in_background=true, prompt="Find similar code patterns to [TARGET] in the codebase.
…
常见问题
“重构”是什么意思?
重构是指在不改变代码功能的前提下重新组织现有代码,使其更简洁、更高效或更易于理解。
AI 如何确保不会把代码改坏?
它会分析现有的测试覆盖情况,并在每次小改动后运行测试,确保没有破坏已有功能(即回归测试)。
我可以只让它重构一个文件的某一部分,还是必须整个文件一起改?
可以。你可以指定重构范围是单个文件、一个模块(目录),还是整个项目。
详情
作者的原始描述
Guides a refactor, cleanup, or restructure with the right decomposition. Use when the user asks to refactor, simplify, extract, or modernize code.
热度
- GitHub 星标
- 70k
- 排名
- #3137
- 许可证
- NOASSERTION
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
comment-checker
70k帮你的智能体理解并回应自动代码检查工具的反馈。适用于处理自动化代码或内容审查的结果。
lcx-contribute-bug-fix
70k帮你的智能体为 LazyCodex 相关项目贡献错误修复。它会创建 issue 或 pull request,保障代码质量。
lcx-doctor
70k让你的智能体检查 LazyCodex 和 Codex CLI 安装的健康状况。当系统在更新或改动后表现异常时很有用。
lcx-report-bug
70k帮你的智能体为 LazyCodex 项目详细报告 bug。它会创建包含有用信息的 issue,方便开发者定位并修复问题。