Xstate v5

From AkaraChen/angel-engine · View interactive version

Xstate v5 is an AI agent skill from the GitHub repository AkaraChen/angel-engine. Design, implement, review, and migrate XState v5 state machines and statecharts in TypeScript using modern v5 patterns. Use this whenever the user mentions XState, actors, state machines, statecharts, guards, transitions, workflows, or Stately, or is modeling non-trivial UI/app/process logic in a co

How to use this skill

Skill file

XState v5

Use this skill for state machine and statechart engineering first and API correctness second.

This skill is v5-only. When examples, blog posts, answers, or local code smell v4-ish, translate them rather than mixing versions. Prefer local repo code and official v5 docs over generic memory.

Your job:

First pass

In an existing codebase:

  1. Inspect local package.json files and imports.
  2. Read nearby machines, stores, actors, and adapter usage.
  3. Distinguish between new code, local edits, and migration work before choosing how opinionated to be.
  4. Preserve surrounding style by default.

Task mode

Choose a mode before writing code:

For migration and local edits, do not introduce setup(...), named implementations, object-form guards/actions, tags, or actor decomposition unless they are required for correctness, significantly reduce local complexity, or were explicitly requested.

Choose the tool

Prefer @xstate/store when the domain is simple event-based state management:

Simple fetching or mutation logic can still fit @xstate/store if it does not need machine-level orchestration.

Prefer XState when the domain has one or more of these:

If the problem is too simple for a machine, say so plainly and recommend @xstate/store.

Workflow

When requirements are fuzzy:

  1. Sketch the machine shape first.
  2. Name the important states, events, context, actors, and tags.
  3. Call out uncertainty or tradeoffs briefly.
  4. Then write the code.

When requirements are already clear, keep the sketch short or implicit and move to code.

Do not over-refactor existing code for conceptual purity. Improve the model where it matters, but preserve working structure when a larger rewrite is not justified.

Modeling questions

Use these questions before writing or revising code:

Preferred v5 patterns

For new code, prefer these patterns unless the local codebase has a strong reason not to:

Keep event naming consistent. The . in event names is useful and meaningful, including for partial event descriptors and clearer domain grouping. See the official docs on events and transitions and TypeScript narrowing with CODE1.

See references/examples.md for canonical code shapes, references/advanced-patterns.md when the task involves typed actions beyond assign(...), callback actors, emitted events, or persistence/hydration, and references/observables-and-inspection.md for fromObservable(...), inspection, and browser inspector patterns.

UI integration

Prefer letting the machine drive UI behavior:

If a component owns a small local actor, useMachine(...) is often enough.

If an actor is shared, long-lived, or performance-sensitive:

See references/adapters.md for concise adapter guidance.

Review and repair guidance

When reviewing or fixing XState code, look for:

Avoid pushing decomposition too far. Actor boundaries are useful when they improve clarity, ownership, or concurrency. Do not split for its own sake.

Migration

When legacy code is present, translate toward v5 gradually and locally unless the user asked for a broader migration.

Common migration targets include:

For migration tasks:

Use references/v4-to-v5-quick-ref.md for quick translation patterns.

Persistence

When persistence or hydration matters:

See references/advanced-patterns.md for concrete persistence and hydration patterns.

Testing

Do not introduce testing guidance unless the user asks about testing or requests tests.

If they do ask, keep it brief:

If a link would help, point them to the XState testing and graph/path generation docs.

Optional tools

Stately Studio and inspection tools can help with design, debugging, and communication, but they are optional. Mention them when the user is designing a machine, wants visualization, or needs better debugging visibility.

Output format

Prefer this output shape:

  1. Short machine sketch if requirements are fuzzy.
  2. Code.
  3. Brief rationale explaining states, events, context, actors, and tags.
  4. Migration notes only if relevant.

Prefer fewer complete files over a larger but partially wired example. Do not sketch extra shell files unless they are fully wired.

See references/examples.md for more canonical examples that can grow over time.

Final self-check

Before you answer, do a quick compile-minded pass:

If a complete multi-file answer is getting bulky, remove the shell file and keep the smaller set of files that still demonstrates the pattern correctly.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.