⌨️ Codingadvancedstate-machinearchitecturexstatebusiness-logic

Model Business Logic as a State Machine

Convert complex business logic with many states and transitions into a clean state machine implementation.

The Prompt

prompt.txt
Model the following business process as a state machine. Provide:
1. All states with descriptions
2. All transitions with trigger conditions and actions
3. A state diagram (text-based or Mermaid)
4. Implementation using XState v5 or a hand-rolled state machine
5. Guards — conditions that must be true for a transition to be allowed
6. Side effects — what happens on each transition (emails, database updates, etc.)

Business process to model:
[DESCRIBE THE PROCESS — e.g., An order in an e-commerce system: starts as PENDING, moves to CONFIRMED when payment succeeds, to PROCESSING when warehouse picks it, to SHIPPED when tracking number is added, to DELIVERED when signed for, or to CANCELLED from any state before shipping, or to REFUNDED from DELIVERED.]

Language: [TYPESCRIPT / JAVASCRIPT]

Example Output

Defined 7 states and 11 transitions in an XState machine, with guards preventing cancellation after shipping, side effects for sending confirmation emails on CONFIRMED and shipping notifications on SHIPPED, and a Mermaid diagram for documentation.

FAQ

Which AI model is best for Model Business Logic as a State Machine?

Claude Sonnet 4 — excellent at formal modeling and structured reasoning about state transitions.

How do I use the Model Business Logic as a State Machine prompt?

Copy the prompt, replace the [BRACKETED] placeholders with your specific information, and paste into your preferred AI assistant (ChatGPT, Claude, Gemini, etc.). Defined 7 states and 11 transitions in an XState machine, with guards preventing cancellation after shipping, side effects for sending confirmation emails on CONFIRMED and shipping notifications on SHIPPED, and a Mermaid diagram for documentation.