Xstate 标志

Xstate

社区
citypaul
xstate

Model front-end and workflow logic as XState v5 statecharts and actors — when finite states earn their place (and when @xstate/store or a discriminated union is honest), event-first machine design, guards/actions/delays, invoke vs spawn, app-level actor systems, persistence, @xstate/react integration, behavior-driven machine testing including model-based path generation via xstate/graph, and a Mermaid stateDiagram-v2 render regenerated whenever a machine is designed or changed. Fires on ordinary React code that is already a hand-rolled statechart, not only on files that import xstate — a submitting or isLoading useState, a then/catch chain that sets state in sequence, a double-submit guard such as an early return while submitting, an error cleared just before a retry, a useEffect needing an ignore flag in its cleanup, a timer something must clear, or a drag or gesture handler binding document listeners it must unbind. Two tests decide ownership, never how the value renders: an external answer changed it, or the interaction has an interruptible middle. A disabled button or spinner is the presentation of temporal state, not presentation state. State stays in React only when both tests come back empty — one event sets it completely, with no phase to interrupt and nothing to dispose. Use when building or reviewing wizards, checkout/auth/upload flows, async orchestration with retries or timeouts, canvas and editor interactions, anything sent to a BFF or server, boolean-flag state bugs, or any XState or state-machine code. For general test patterns see testing; for component tests see react-testing; for store-only state see the when-to-model reference first.

概览

发布者citypaul
代码仓库.dotfiles
技能名称xstate
星标数
740
复刻数
96
附带文件
7
链接
  • Markdown 格式的指令

    模型按需加载 SKILL.md 文件,因此只有在请求确实与该技能匹配时才会消耗 Token。

  • 兼容任何大语言模型

    AI 技能采用纯 Markdown,而非特定提供商的代码,因此可搭配 GPT、Claude、Gemini、Grok 或本地模型使用。

  • 7 个附带文件

    模型执行任务时可读取的脚本、模板和参考资料。这些文件为只读文件,绝不会被执行。

  • 开源

    由 citypaul 发布于 GitHub。请在安装前查看技能的源文件。

安装

在 TypingMind 中安装 Xstate AI 技能,即可搭配任意大语言模型使用;也可以将其放入其他支持读取 SKILL.md 的智能体中。

1

安装到 TypingMind

TypingMind 可直接从 GitHub 文件夹安装技能:读取 SKILL.md,打包资源文件,并将结果保存在本地。

  1. 打开应用,前往“插件 → 技能”。
  2. 选择“从 GitHub 安装”。
  3. 粘贴下方的技能文件夹 URL 并确认。
    https://github.com/citypaul/.dotfiles/tree/main/claude/.claude/skills/xstate
  4. 请在每个需要使用该技能的聊天中启用它。
依次选择“插件 → 技能 → 添加技能 → 从 GitHub URL 导入”,然后粘贴文件夹 URL 并点击“继续”。
2

安装到其他智能体

任何支持 Agent Skills 格式的智能体都可以使用此技能,只需将文件夹复制到该智能体的技能目录中。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/citypaul/.dotfiles.git /tmp/.dotfiles
mkdir -p .claude/skills
cp -r /tmp/.dotfiles/claude/.claude/skills/xstate .claude/skills/xstate
复制后,请重启 Claude Code,以加载新技能。

在 TypingMind 中使用此技能

在任意 TypingMind 聊天中启用 Xstate,后续即可交由模型处理。技能的名称和描述会放在系统提示词中,一旦请求匹配,模型就会自动加载完整指令,无需您手动调用,且在实际使用前不会消耗 Token。

一旦请求与技能匹配,模型就会自动加载 Xstate。

兼容任何 AI 模型

AI 技能是纯 Markdown 指令,而非特定提供商的代码,因此 Xstate 不局限于最初适配的模型。只需在 TypingMind 中安装一次,即可搭配 GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 或您自行运行的本地模型使用,全程使用您自己的 API 密钥。

  • 仅在需要时加载

    系统提示词中仅包含技能名称和描述。首次遇到匹配的请求时才会加载完整指令,因此技能未使用时,这些指令不会消耗 Token。

  • 在聊天中切换模型

    技能是指令而非代码,因此切换模型不会导致技能失效,新模型会读取同一个 SKILL.md。

技能指令

这是模型会加载的 SKILL.md 内容。请在安装前阅读,因为技能就是模型将遵循的指令。

XState

State machines and statecharts (XState v5) turn front-end flow logic from scattered event handlers into one explicit, verifiable model. This skill exists because that trade is unusually good in AI-assisted development:

  • Headless testability — a machine is events in, snapshots out. Flow logic is proven with fast unit tests before any UI exists, and xstate/graph can generate covering paths through every transition.
  • Impossible states are unrepresentable — finite states plus guarded transitions delete the boolean-flag bug class (isLoading && isError); the model forbids drift instead of relying on discipline.
  • One reviewable artifact — every state × event combination lives in a single declarative structure, so agents and reviewers reason about the whole behavior surface at once.
  • Humans get the diagram for free — Stately Studio and the inspector render exactly the machine that ships; review becomes "look at the chart".

Default stance: model front-end flow logic — wizards, checkout, auth, uploads, anything with modes, sequencing, cancellation, timeouts, retries — as statecharts. Keep trivial state honest: data-shaped state with no modes goes to @xstate/store or a plain discriminated union, never a ceremonial machine.

The failure mode this skill exists to catch is under-modeling, not over-modeling. Model training data is overwhelmingly ordinary React — useState and useEffect for everything — so the realistic risk is quietly hand-rolling a statechart, not building a ceremonial one. Read Before You Write State before reaching for useState, not after deciding to use XState.

Version guardrail: XState v5 only (setup(), createActor, actors). Most tutorials, old blog posts, and model training data are v4 — if you see Machine(), interpret(), cond:, services:, or typegen, you are looking at v4; translate via the migration table before any of it ships.

Load alongside: testing (behavior-driven patterns and factories), react-testing/front-end-testing (component-level tests), typescript-strict (schema-first at trust boundaries — external events get parsed before send), functional (context is immutable data). For event-sourced domain state, event-sourcing owns the Decider; an XState machine may drive a UI flow in front of it, but the event log is not machine context.

Read the relevant reference before deciding:

ReferenceRead when...
references/when-to-model.mdDeciding machine vs store vs union — the complexity ladder, flag-smell signals, finite-state vs context split, store→machine migration, where machines live
references/machine-design.mdDesigning a machine — event-first workflow, naming, hierarchy/parallel/history/final, transition and re-entry semantics, actions, guards, delays, anti-pattern catalog
references/actors-and-systems.mdEffects and composition — actor logic types, invoke vs spawn, spawn hygiene, communication, systemId receptionist, persistence, inspection
references/react-integration.mdWiring to React — hook selection, createActorContext, StrictMode, @xstate/store, production pitfalls
references/testing.mdTesting — the layered split, provide() as the seam, SimulatedClock, pure transition, model-based testing, persistence round-trips, mutation-score guidance
references/typescript-and-migration.mdTypes and versions — setup() typing model, assertEvent, type helpers, no-typestates caveat, full v4→v5 table

Before You Write State

This section fires on ordinary React code. You do not have to be in a machine file — or have decided to use XState at all — for it to apply.

Step 0: Find out who owns machines in this repository

Before creating a machine, moving state into one, or deciding where a new actor lives, check what this repository already says: architecture or boundary tests, import-boundary rules such as eslint-plugin-boundaries or dependency-cruiser, architecture decision records, the directory already holding existing machines, CLAUDE.md, or the layout guidance in structure-codebase. Repository convention outranks this skill's default colocation advice.

A correct machine in a forbidden layer still fails the build. Establish the allowed home before writing, not after CI rejects it.

If you just wrote one of these, stop — you are already at step 1

Each of these is a hand-rolled statechart: the states exist, they are just implicit, scattered, and unverified. Treat any single hit as the trigger to work through this skill.

What you wroteThe statechart hiding in it
useState for submitting, isLoading, saving, pending, inFlightA submitting state, with SUBMIT illegal while it holds
.then(...)/.catch(...) or await followed by two or three setX calls in sequenceEntry actions of the success and failure states
A double-submit guard — an early return, a disabled flag checked before dispatchingA transition the model would simply not define
Clearing an error immediately before retryingA retrying transition that re-enters the invoking state
useEffect watching a flag to trigger the next stepA transition trying to escape from an effect
Two booleans that can contradict (isLoading && isError)n booleans = 2ⁿ combinations, of which a handful are legal
A status string that handlers keep re-deriving from other fieldsThe finite state, denormalized
A ref or timer coordinating cancellation, debounce, or staleness by handActor lifetime, after, and cancellation semantics
A useEffect that starts async work and needs an ignore flag in its cleanupActor lifetime — the machine cancels instead of ignoring
A setTimeout/setInterval for retry, debounce, or polling that something must clearafter, with cancellation tied to the state that owns it
A pointerdown, dragstart, or gesture handler binding document listeners it must unbindAn interaction with an interruptible middle — an invoked actor owns the listeners and disposes them
Handling for a response arriving after cancel, replacement, or unmountStale-result fencing by identity, which the machine makes structural

Any one of them is the entry point, however small the request, and whether or not the file imports xstate.

The two lifetime tests

Classify state by what these two tests show, never by what the value renders. Run both over every useState you write or keep.

Test 1 — did an external answer change it? A network response, a timer, another actor, or the platform makes state temporal whatever it looks like on screen. A disabled button or a spinner is the presentation of temporal state, not presentation state. Anything sent to a BFF or server, and everything the answer implies, is caught here.

Test 2 — does the interaction have an interruptible middle? Direct user input still creates a lifecycle when there is a phase between start and end that something can interrupt, cancel, or must dispose. A drag holds pointer capture, cancels on Escape, and must release on unmount. A panel that animates shut has a closing phase. A gesture that binds document listeners must unbind them.

State belongs to React only when both tests come back empty — one event sets the value completely, leaving no phase to interrupt and nothing to dispose. A toolbar toggle, a controlled input's current text, a hover, a focus flag, a plain open/closed disclosure: one event, no middle, nothing to clean up.

ExampleTest 1Test 2Owner
submitting around a commandYes — the response clears it—Machine
Spinner visibilityYes — presentation of temporal state—Machine
Canvas node dragNoYes — capture, Escape, unmountMachine
Panel with a closing animationNoYes — a closing phaseMachine
Toolbar open/closedNoNoReact
Controlled input textNoNoReact

Test 2 is the one most often missed, because a drag is user-driven and test 1 alone would wrongly file it in React.

State the verdict, and ask on genuine ties

Having run both tests, act on the answer and report the call in one line so the user can overrule it: "isSubmitting is set by the request, so it moved into the machine as submitting."

Where it is genuinely close — a real but small lifecycle, or a machine the repository's layer rules would push far from the component that uses it — put the choice to the user before building: name the state, name the phase that makes it a lifecycle, and ask whether it belongs in the machine. A tie is worth one question; it is not worth a silent guess in either direction.

Ask whether an existing actor already owns this flow

The complexity ladder in when-to-model.md answers a greenfield question. It is the wrong tool when a machine for this flow already exists.

If an actor already owns the lifecycle this state belongs to, the state goes in that actor — the ladder does not reopen the decision. A submission flag next to a machine that already sends the command, handles its errors, and owns the retry policy is not a fresh rung-1 decision; it is one state that escaped its machine. Splitting a single lifecycle across a machine and a component's useState gives you two sources of truth that can disagree, which is the exact bug class the machine was adopted to delete.

Before adding flow state to a component, search the feature for an existing machine, actor, or createActorContext provider and check whether the lifecycle you are about to hand-roll is one it already models.

When a new machine is justified, the boundary is a distinct unit of functionality with its own inputs and outputs — typically its own connection, resource, or authority. In a real actor-based front end that produced one machine per SSE stream, one for canvas interaction, one for the workshop's node-adding rules, and one for media-connection management: each owns a different external edge, so each fails and reconnects independently.

The question to answer explicitly, and out loud, is not "does this deserve a machine?" but "why does this not belong to the machine that already owns this edge?" A small new machine sitting beside a larger one that shares its inputs and outputs is usually a state that should have been added to the larger one. Where the answer is genuinely close, ask rather than guess.

Sweeping for the same violation elsewhere

Once one hand-rolled statechart is found, the same pattern is usually repeated across the feature. To sweep: grep the feature for the smell list above — useState initialized to false near an await, setError(null) before a call, if (<flag>) return at the top of a handler — then, for each hit, apply the cause-not-appearance table. Report each as keep in React or belongs in machine X, with the reason stated as what changes the value. Fix them as separate increments, each with its own failing machine test, rather than one sweeping commit.

The Rules That Prevent Most XState Bugs

  1. Events first, then modes. List what the world can do; states fall out of "when does the same event mean something different?" Never design states off the UI tree.
  2. Finite state = mode, context = data. If a value gates which events are legal, it is a state; if it is unbounded, it is context. A context flag checked by guards on every transition is a state in hiding — promote it. Classify by what changes a value, never by what it renders.
  3. Context is JSON data. No class instances, promises, or DOM nodes — persistence, inspection, and the editor all depend on it. Spawned actor refs are the sanctioned exception.
  4. Effects are actors, not actions. Anything with a result, a lifetime, or a failure mode is invoked or spawned; actions are fire-and-forget. invoke when the lifetime belongs to a state, spawn for dynamic counts — and every spawn has an owner that stops it.
  5. Name everything in setup(). Named actions, guards, actors, and delays make string references type-checked, the chart readable, and machine.provide() the universal test seam.
  6. All v5 transitions are internal by default. Targetless self-transitions for "update and stay"; reenter: true only when you truly want exit/entry re-run and invocations restarted.
  7. Machines complete like functions. Flows that end declare final states and output; parents consume onDone/onError. Error paths are modeled states, not console noise.
  8. One machine per flow — and one owner per lifecycle. Cross-flow coordination is an actor system (parent machines, systemId), never a god machine. Equally, a lifecycle already owned by an actor does not get a second copy of itself in a component's useState.
  9. Test the machine headlessly, the component through the DOM. The snapshot is the machine's contract; it is implementation detail one level up. Never assert machine state from a component test.
  10. Validate at the trust boundary. Events from outside the process (sockets, storage, URLs) are schema-parsed before send; persisted snapshots are restored only with a versioning story.

Rendering the Machine as a Diagram

A statechart's main advantage over scattered handlers is that a human can look at it — which is worth nothing if the picture and the machine have drifted apart.

Render or update the diagram whenever a machine is designed or changed, even when nobody asked for one, and render on request at any time. The render is a file in the repository, not a message. Write the fenced mermaid block either into a markdown file beside the machine or into a comment at the top of the machine file itself, then name that path in your reply. A chart that exists only in the reply is not a render — it leaves with the conversation, and the next reviewer opens the machine with no diagram at all. The cost of a diagram is small; the cost of a stale one is a reviewer trusting a picture that no longer describes the code. Regenerating on every change is what keeps that from happening, so it is not optional work at the end — it is part of changing the machine.

Derive it from the machine source, never from memory of it, and regenerate from the final definition once the change is complete. Report the validation result honestly: say whether the diagram was checked to render, and never claim a render that was not performed.

Render as Mermaid stateDiagram-v2:

  • One node per finite state; nested states become state parent { ... }; parallel regions are separated by --.

  • Label every edge with its event, and append [guard] where a guard gates it.

  • Show onDone/onError for invoked actors — the error path is the half reviewers most need to see.

  • Keep it to one flow. A diagram spanning an actor system is a system diagram, and belongs in the diagrams skill's care.

  • Keep composite and concurrent boundaries legal, and keep parser-sensitive labels safe — an event name containing a colon, a guard containing a bracket, or a state name Mermaid reads as a keyword all need escaping or renaming in the label.

diagrams owns format choice, validation, and the accessible text explanation that must accompany any committed diagram. Stately Studio and @statelyai/inspect render the live machine and remain the better loop for interactive exploration — Mermaid loses that interactivity, which is the one real thing it costs — but a checked-in Mermaid render is what makes the machine reviewable in a diff.

Anti-Patterns

Under-modeling — the common failure, listed first because it is the one that actually happens:

  • A submitting/saving/isLoading flag living in component useState while a machine next to it already owns the command, its errors, and its retries — one lifecycle, two sources of truth.
  • Classifying flow state as presentational because of how it renders — "it is just a disabled button", "it is just a spinner".
  • Reading rung 1 or rung 3 of the ladder as permission to hand-roll, when the flow already has retry, cancellation, staleness, or an owning actor.
  • A promise chain in an event handler doing the machine's job: set flag, await, clear flag, set error, clear error before the next attempt.
  • useEffect chains sequencing steps by watching flags — transitions trying to escape from effects.
  • Canvas, editor, permission, or command-dispatch interactions left in ad-hoc handlers because each individual handler looked small.

Over-modeling — real, but rarer:

  • An 80-line machine wrapping a modal boolean — over-modeling trivial state (rung 1–2 of the ladder).
  • Machine-as-reducer: one or two states, every event an unconditional assign — demote to @xstate/store.

Modeling done wrong:

  • Boolean flags accumulating in context, gating behavior through guards — hidden finite states.
  • Form field values mirrored into machine context on every keystroke — the machine owns the submission lifecycle, the form layer owns the current text. (Note the split: values stay local, submitting does not.)
  • sendParent coupling children to parent shapes — pass the parent ref via input and use sendTo.
  • Spawned actors never stopped; stopChild without clearing the context ref.
  • Asserting transient always states, or component tests reading actorRef.getSnapshot().
  • v4 idioms in new code (cond, services, interpret, typegen) — translate before shipping.
  • Inspector or @statelyai/inspect wired into production builds.
  • A committed diagram left behind after the machine changed — a picture a reviewer will trust and the code no longer matches.
  • Claiming a diagram renders without having checked it.

Completion Check

  • For every piece of state in the touched code, can you say what changes it — and does anything changed by a server, socket, timer, or another actor still sit in useState?
  • Did you check where this repository allows machines to live before creating one, rather than after CI rejected it?
  • If a machine already existed for this flow, does it now own the whole lifecycle, with no flag mirroring part of it in a component?
  • Can you name the flow this machine owns, and would a diagram of it make sense to a non-developer?
  • Is every impossible state actually unrepresentable — no contradictory booleans, no guard re-deriving a status?
  • Does every effect with a result, lifetime, cancellation, or failure mode live in a named actor (with onDone/onError modeled where the actor's protocol has them), and is every fire-and-forget effect a named action?
  • Does the machine suite cover every guard boundary, ignored event, error path, and timeout headlessly — and do component tests touch only the DOM?
  • Are external events schema-validated before send, and is persisted-snapshot compatibility across releases either tested or explicitly not needed?
  • Is everything v5 idiom — setup(), named implementations, createActor — with no v4 vocabulary?
  • Does every designed or changed machine have a diagram regenerated from its final definition and written to a file in the repository — with that path named in the reply, not just a chart pasted into the conversation — with composite and concurrent boundaries legal, parser-sensitive labels safe, and the validation result reported accurately?
  • Would the next reviewer learn the flow faster from the chart than from the diff? If not, the model is not carrying its weight yet.

附带文件

技能加载后,模型会按需读取这些文件。它们仅作为可读文件提供,绝不会被执行。

常见问题

Xstate AI 技能有什么作用?

Model front-end and workflow logic as XState v5 statecharts and actors — when finite states earn their place (and when @xstate/store or a discriminated union is honest), event-first machine design, guards/actions/delays, invoke vs spawn, app-level actor systems, persistence, @xstate/react integration, behavior-driven machine testing including model-based path generation via xstate/graph, and a Mermaid stateDiagram-v2 render regenerated whenever a machine is designed or changed. Fires on ordinary React code that is already a hand-rolled statechart, not only on files that import xstate — a su...

为什么在 TypingMind 中使用 Xstate?

因为您只需安装一次,就能搭配任何模型使用。Xstate 采用纯 Markdown 格式,而非特定提供商专用的代码,因此同一技能可用于 GPT-5、Claude、Gemini、Grok 或本地模型。您甚至可以在聊天中途切换模型,而不会影响技能的正常使用。TypingMind 使用您自己的 API 密钥,因此您直接向提供商付费,而不是按席位订阅;您的技能和聊天记录也都保留在您自己的存储空间中。

如何在 TypingMind 中安装 Xstate?

在 TypingMind 中打开“插件 → 技能 → 从 GitHub 安装”,然后粘贴 https://github.com/citypaul/.dotfiles/tree/main/claude/.claude/skills/xstate。TypingMind 会读取其中的 SKILL.md,打包相关文件,并将其安装为技能,供您在各个聊天中按需启用。

哪些 AI 模型可以使用 Xstate?

您在 TypingMind 中连接的任何模型都可以。AI 技能采用纯 Markdown 指令,而非特定提供商专用的代码,因此 GPT、Claude、Gemini、Grok 和本地模型都能在请求匹配时加载此技能。

Xstate 可以搭配多少个 AI 模型使用?

数量不限。只要模型支持技能,您就可以将它与 Xstate 搭配使用。GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 等模型都可以通过您自己的 API 密钥接入 TypingMind。

Xstate AI 技能免费吗?

citypaul 已在 GitHub 上发布了该技能。请查看代码仓库了解许可条款。您只需向自己的 AI 提供商支付所用 Token 的费用。

什么是 AI 技能?

AI 技能是一组可复用的指令,用于教会 AI 模型完成某项特定任务。它遵循开放的 Agent Skills 格式:包含名称和描述的 SKILL.md 文件,以及模型可能需要的脚本、模板或参考文件。模型仅在您的请求与技能匹配时才会读取这些指令,因此已安装的技能在使用前不会产生任何费用。

AI 技能与插件或 MCP 服务器有何不同?

插件或 MCP 服务器为模型提供可调用的新工具,也就是在某处运行并返回结果的代码。AI 技能则为模型提供知识和流程:如何处理任务、遵循哪些步骤,以及怎样的输出才算优质。技能采用纯 Markdown 格式,因此不需要服务器、API 密钥或运行时环境,并且适用于任何模型。

查看全部

立即创建您自己的 AI 工作区

订阅我们的电子简报,获取新功能和后续赠奖活动的通知 👇