Logo Refactor

Refactor

Cộng đồng
hamr0
refactor

Refactor and optimize [code]

Tổng quan

Nhà phát hànhhamr0
Kho mãliteagents
Tên kỹ năngrefactor
Sao
27
Bản fork
4
Tệp đi kèm
Chỉ có hướng dẫn
Giấy phépApache-2.0
Liên kết
  • Hướng dẫn bằng Markdown

    Tệp SKILL.md được mô hình tải khi cần, nên chỉ tốn token khi có yêu cầu thực sự phù hợp.

  • Dùng được với mọi LLM

    Kỹ năng AI là Markdown thuần túy, không phải mã dành riêng cho một nhà cung cấp, nên dùng được với GPT, Claude, Gemini, Grok hoặc mô hình cục bộ.

  • Đầy đủ trong hướng dẫn

    Mọi thứ mô hình cần đều nằm trong hướng dẫn — không có tệp bổ sung cần đồng bộ.

  • Mã nguồn mở

    Được hamr0 công bố trên GitHub. Hãy đọc nội dung nguồn trước khi cài đặt.

Cài đặt

Cài đặt kỹ năng AI Refactor trong TypingMind để dùng với bất kỳ LLM nào, hoặc thêm vào một tác nhân AI khác có thể đọc SKILL.md.

1

Cài đặt trong TypingMind

TypingMind cài đặt kỹ năng trực tiếp từ thư mục GitHub — đọc SKILL.md, đóng gói các tệp tài nguyên và lưu kết quả trên máy.

  1. Mở ứng dụng và vào Plugin → Kỹ năng.
  2. Chọn "Cài đặt từ GitHub".
  3. Dán URL thư mục kỹ năng bên dưới và xác nhận.
    https://github.com/hamr0/liteagents/tree/main/packages/ampcode/skills/refactor
  4. Bật kỹ năng trong từng cuộc trò chuyện mà bạn muốn sử dụng kỹ năng đó.
Plugin → Kỹ năng → Thêm kỹ năng → Từ URL GitHub, sau đó dán URL thư mục và nhấn Tiếp tục.
2

Cài đặt trong tác nhân AI khác

Bất kỳ tác nhân AI nào đọc được định dạng Agent Skills đều có thể dùng kỹ năng này — hãy sao chép thư mục vào thư mục kỹ năng của tác nhân AI đó.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/hamr0/liteagents.git /tmp/liteagents
mkdir -p .claude/skills
cp -r /tmp/liteagents/packages/ampcode/skills/refactor .claude/skills/refactor
Khởi động lại Claude Code sau khi sao chép để ứng dụng nhận kỹ năng mới.

Sử dụng trong TypingMind

Bật Refactor trong bất kỳ cuộc trò chuyện nào trên TypingMind, mô hình sẽ tự xử lý phần còn lại. Tên và mô tả của kỹ năng nằm trong prompt hệ thống. Ngay khi có yêu cầu phù hợp, mô hình tự tải toàn bộ hướng dẫn — bạn không cần gọi thủ công và kỹ năng không tốn token cho đến khi thực sự được dùng.

Mô hình tự tải Refactor ngay khi có yêu cầu phù hợp.

Dùng được với mọi mô hình AI

Kỹ năng AI là các hướng dẫn Markdown thuần túy, không phải mã dành riêng cho một nhà cung cấp, nên Refactor không bị giới hạn ở mô hình mà kỹ năng này được viết cho. Chỉ cần cài đặt một lần trong TypingMind rồi dùng với GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama hoặc mô hình cục bộ do bạn tự chạy — tất cả đều dùng khóa API của riêng bạn.

  • Chỉ tải khi cần

    Prompt hệ thống chỉ chứa tên và mô tả. Hướng dẫn được tải khi có yêu cầu phù hợp đầu tiên, nên kỹ năng chưa dùng không tốn chi phí.

  • Đổi mô hình ngay trong cuộc trò chuyện

    Vì kỹ năng là hướng dẫn chứ không phải mã, việc đổi mô hình không làm kỹ năng ngừng hoạt động — mô hình tiếp theo vẫn đọc cùng tệp SKILL.md.

Hướng dẫn của kỹ năng

Đây là nội dung SKILL.md mà mô hình tải. Hãy đọc trước khi cài đặt — kỹ năng là những hướng dẫn mà mô hình của bạn sẽ làm theo.

Refactor $ARGUMENTS. A targeted refactor includes the performance pass below — it is on by default, not a separate command.

Guardrails

  • Spawn a worker, mid tier stated explicitly (omitted inherits the parent's, not the balanced one; not cheapest/fastest either — judgment degrades there; never a vendor model name). Fall back to running inline if your tool has no subagent mechanism.
  • Escalate, never assume. Anything you cannot decide, cannot verify, or that this spec does not cover → stop and report it to the orchestrator (the main session). Never improvise, never widen scope, never fix a side issue you noticed along the way.
  • The worker does the work itself — no delegation. The fixer must not spawn subagents of its own. Every edit it reports, and every test run it cites, has to be one it made or ran with its own tool calls: a relayed "I fixed it and the suite is green" from a sub-worker is hearsay, and this command's whole output is the claim that a change landed and the tests still pass. A fix that delegates its work is a report about a report.
  • The HITL gates below belong to the orchestrator, not the worker. A subagent cannot hold a conversation with the user, so it cannot run a gate that ends in stop and ask. When one trips — a failing test, a crossed public API boundary, a change bigger than the bullet asked for — the worker stops there and hands the situation back, with the options and its reasoning but no choice made. The orchestrator asks. A worker that picks revert / patch / update-test on the user's behalf has answered a question it was never allowed to ask.
  • Edit only what a surviving bullet names. Ledger mode's scope is the bullets that survive revalidation, one change per bullet — not the neighbouring code, not the formatting, not a second finding noticed on the way past. Anything else goes back to the orchestrator to become a new bullet.
  • Prove the blast radius with two checks, because neither sees what the other does. git status --porcelain at exit must list only files a surviving bullet named — that is this command's scope guarantee, and unlike /branch-review it is not expected to be empty. It cannot police the memory directory: .amp/ is normally gitignored, so porcelain stays empty whether you deleted a fixed bullet, wrote nothing, or overwrote MEMORY.md. So also hash the files there before you start and again before you report:
    find .amp/remember -maxdepth 1 -type f -exec md5sum {} + | sort -k2
    and show the comparison: only fix-ledger.md may differ. last-review.md in particular is /branch-review's to write — a fixer that touches it forges the gate that judges its own work.

Ledger mode — $ARGUMENTS empty

Work through .amp/remember/fix-ledger.md, the non-blocking findings /self-review and /branch-review have accumulated. Everything below (goals, constraints, verification, HITL gates) still applies; this section only says what to refactor and how to close each item.

  1. Tree must be clean and not on main. The orchestrator runs this check before spawning the worker, so a dirty tree costs no worker; the worker then re-runs it as its own first act. git status --porcelain non-empty → stop, say what is uncommitted. On main → git switch -c chore/fix-ledger.
  2. Ledger missing or has zero bullets → say so and stop. Nothing to do.
  3. Revalidate every bullet first, fix nothing yet. For each: git grep -F "<snippet>" -- <path>. No hit → delete the bullet and list it as "cleaned by other work". Hit → re-read the surrounding code; if the finding no longer holds, delete the bullet with a one-line reason. What survives is the work list.
  4. Fix only surviving nit bullets (untagged bullets count as nit), one bullet per change, under the constraints below. Delete each bullet as its fix lands. Surviving change and idea bullets: the worker lists them in its report ("left: change" / "left: idea") and changes nothing about them — they need a behaviour change, a redesign or a build, not a refactor, and the worker cannot ask. A nit that turns out to need one is retagged change in place, not fixed and not left silently. The orchestrator then asks the user, per change/idea item: keep (stays in the ledger), drop (the orchestrator deletes the bullet) or spec it (the user describes it; it becomes its own task on its own branch after this run — never built in ledger mode, whose diff must have NO behavior changes).
  5. Run the tests as described below. Then report: fixed / dropped / left with the reason per left item, ending with N nits, K changes, I ideas remaining — counted the same mechanical way /branch-review does:
    grep -c '^- ' .amp/remember/fix-ledger.md
    grep -cE '@ [0-9a-f]{7,40} · change$' .amp/remember/fix-ledger.md
    grep -cE '@ [0-9a-f]{7,40} · idea$' .amp/remember/fix-ledger.md
    First is the total bullet count, second is K, third is I; N = total − K − I.
  6. Hand it back; do not chain it. Say plainly: commit, then run /branch-review on this branch — ledger mode is a fixer, not a review, and its diff gets the ordinary gate. That is a sentence you say, not a sequence you run. They are two separate calls and both are the user's: an answer of "commit", "yes" or "go" authorizes the commit and nothing after it. Never start /branch-review off the back of it. Observed in the field: a run chained the review onto the owner's "commit" and the owner objected.

Where to look — broad targets only

Empty $ARGUMENTS is ledger mode; the surviving bullets are the scope, so skip this. When $ARGUMENTS names a specific file or function, that is the scope; skip this too. When it names a whole area ("the auth module", "clean this up"), let recent change decide where inside it to start: git log --oneline -- <path> and weight the files that keep coming back. A refactor is an investment in the next change to that code, so code nobody edits pays the worst return — say which files you picked and what churn you saw.

Do not edit yet. After weighting by churn, list candidates — file(s) · what is wrong · the change proposed · strength (strong / worth exploring / speculative) — then stop and hand the list back. The worker cannot ask; the orchestrator asks the user which to do, and only picked candidates are edited.

Goals

  • Reduce complexity
  • Improve readability
  • Apply DRY
  • Better naming
  • Shrink the interface, not the pieces. A caller should have to learn less after the refactor than before — fewer entry points, fewer parameters, fewer ordering rules and error modes to keep in mind. Splitting one messy function into five that the caller must now sequence itself makes the code worse: the same complexity, spread thinner, behind a bigger surface. Push complexity inward; private helpers inside the thing are fine, they are not part of what a caller must learn. This targets the surface callers inside the change see — narrowing something outside callers depend on is an API change, which is the public-API gate below, not a free win.
  • Apply the deletion test before you create anything. Imagine the new function, class, file, or wrapper already deleted. If the complexity reappears, spread across its callers, it earns its place — create it. If the complexity simply vanishes, it was a pass-through: do not create it. Run the same test on what is already there; a pass-through you find is a deletion, not a refactor target. Deleting one that callers outside this change can see is the same public-API gate.
  • Remove needless work — the performance pass below

In ledger mode these two are a filter on the bullet's own fix, never a licence to hunt: shape the change a surviving bullet asked for so it shrinks the interface, and do not create something the deletion test rejects. A pass-through or fat interface you spot elsewhere goes back to the orchestrator as a new bullet, exactly like a side perf finding.

Performance — part of every targeted refactor

When $ARGUMENTS names a target, look for wasted work as well as messy work: time and space complexity, N+1 queries, I/O inside a loop, needless allocations, the same value recomputed repeatedly.

Ground every finding before you touch it. Performance claims are easy to invent. A finding counts as confirmed only with at least one of:

  • a profile, benchmark or log line showing call frequency or duration,
  • the path sits on an obvious hot loop or per-request handler with real volume,
  • the user supplied evidence in the request.

Without one of those it is uncertain — report it, do not optimise it. Speculative optimisation is scope creep with a stopwatch.

Fix confirmed findings under the same constraints as any other refactor: minimal change, one obvious shape, no behaviour change, no API change. After each such edit, re-read the changed region and confirm it still computes the same answer — a perf change that quietly alters semantics is the worst kind. Report per finding: location (file:line), cost (concrete — "N+1 over ~1k rows on every page load", not "could be faster"), change, expected improvement, trade-off (readability / memory / consistency).

In ledger mode the surviving bullets are the whole scope — do not add perf findings of your own. One you notice goes back to the orchestrator as a new bullet, like any other side finding.

Constraints

  • NO behavior changes
  • Keep public API intact
  • Existing tests must pass

Explain each change.

After the refactor — verify it didn't break anything

"Existing tests must pass" is the load-bearing constraint, and the only honest way to know is to run them.

  1. Detect the project's test command (look for package.json scripts, pytest.ini / pyproject.toml, go.mod, Cargo.toml, Makefile). If none is found, stop and ask before claiming the refactor is done — silent green isn't acceptable.
  2. Run the tests. Scope to the affected area when possible (-t, --testPathPattern, pytest path/, go test ./pkg); otherwise run the suite.
  3. Report pass / fail counts and any failure's name + file:line.

Stop and ask when (HITL gates — not all the time, only here):

  • $ARGUMENTS names a whole area — the candidate list above is handed back before any edit.
  • a test fails after the refactor. Don't auto-revert (destroys work-in-progress) and don't push forward (the no-behavior-change constraint is broken). Present the failure and the options: revert, patch the refactor, or update the test (with reasoning).
  • the refactor crossed a public API boundary that callers depend on — even if tests pass, downstream consumers may break.
  • the change is bigger than the user asked for (scope creep — unrelated cleanups, formatting, comment edits). Confirm before applying.
  • a perf fix has multiple reasonable shapes (cache vs precompute vs batch vs paginate vs index) — present the options with trade-offs, not a chosen path.
  • a perf fix trades correctness for speed (lossy approximation, weaker or eventual consistency) — even when it is "obviously" faster.
  • a perf fix touches concurrency primitives (locks, atomics, ordering) — easy to introduce a race.
  • a perf fix changes a DB schema, response shape or caller contract.

Final report:

  • refactor done — ready, OR
  • refactor done, but K tests fail — awaiting direction (revert / patch / update test).

Either way the report carries this line, filled from the run:

tests: <cmd> exit <code> <totals> (scoped | full) | NOT RUN: <reason>

Plus the performance pass: confirmed-and-fixed · confirmed-but-asking (why + options) · uncertain (what profiling or data would settle it) · none found.

Câu hỏi thường gặp

Kỹ năng AI Refactor làm được gì?

Refactor and optimize [code]

Vì sao nên dùng Refactor trên TypingMind?

Vì bạn chỉ cần cài một lần rồi dùng với bất kỳ mô hình nào. Refactor là nội dung Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên cùng một kỹ năng có thể chạy trên GPT-5, Claude, Gemini, Grok hoặc mô hình trên thiết bị — và bạn có thể đổi mô hình ngay trong cuộc trò chuyện mà kỹ năng vẫn hoạt động. TypingMind dùng khóa API riêng của bạn, nên bạn thanh toán trực tiếp cho nhà cung cấp thay vì trả phí thuê bao theo suất người dùng, đồng thời kỹ năng và các cuộc trò chuyện vẫn nằm trong bộ lưu trữ của bạn.

Làm thế nào để cài Refactor trong TypingMind?

Mở Plugin → Kỹ năng → Cài đặt từ GitHub trong TypingMind rồi dán https://github.com/hamr0/liteagents/tree/main/packages/ampcode/skills/refactor. TypingMind đọc tệp SKILL.md và cài đặt thành một kỹ năng mà bạn có thể bật cho từng cuộc trò chuyện.

Những mô hình AI nào có thể dùng Refactor?

Bất kỳ mô hình nào bạn kết nối trong TypingMind. Kỹ năng AI là các hướng dẫn bằng Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên GPT, Claude, Gemini, Grok và các mô hình chạy trên thiết bị đều có thể tải kỹ năng này khi có yêu cầu phù hợp.

Bạn có thể dùng Refactor với bao nhiêu mô hình AI?

Bao nhiêu cũng được. Miễn là mô hình hỗ trợ kỹ năng, bạn có thể dùng Refactor với mô hình đó — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama và nhiều mô hình khác — tất cả đều trên TypingMind với khóa API riêng của bạn.

Kỹ năng AI Refactor có miễn phí không?

Có. Kỹ năng này được hamr0 phát hành trên GitHub theo giấy phép Apache-2.0. Bạn chỉ trả tiền cho nhà cung cấp AI của mình theo số token đã dùng.

Kỹ năng AI là gì?

Kỹ năng AI là một gói hướng dẫn có thể tái sử dụng, chỉ cho mô hình AI cách thực hiện một tác vụ cụ thể. Kỹ năng tuân theo định dạng mở Agent Skills: một tệp SKILL.md chứa tên và mô tả, cùng các tập lệnh, mẫu hoặc tệp tham khảo mà mô hình có thể cần. Mô hình chỉ đọc hướng dẫn khi yêu cầu của bạn phù hợp với kỹ năng, nên kỹ năng đã cài đặt không phát sinh chi phí cho đến khi được sử dụng.

Kỹ năng AI khác gì với plugin hoặc máy chủ MCP?

Plugin hoặc máy chủ MCP cung cấp cho mô hình các công cụ mới để gọi — mã được chạy ở đâu đó và trả về kết quả. Còn kỹ năng AI cung cấp kiến thức và quy trình: cách tiếp cận tác vụ, các bước cần thực hiện và tiêu chí của đầu ra tốt. Kỹ năng chỉ là nội dung Markdown, nên không cần máy chủ, khóa API hay môi trường thực thi và hoạt động với mọi mô hình.

Xem tất cả

Tạo ngay không gian làm việc AI riêng

Đăng ký bản tin của chúng tôi để nhận thông báo về tính năng mới và các đợt tặng quà sắp tới 👇