A Next.js application that demonstrates dynamic AI-generated software flows using XState state machines. This project showcases how AI can dynamically assemble software from task lists and execute them as state machines, applying dynamic programming principles to LLM reasoning tasks.
- Overview
- Video Demo
- Features
- Getting Started
- Architecture
- How It Works
- Recent Enhancements
- API Documentation
- Contributing
- Development Status
- License
X-Reason is an innovative prototype that demonstrates how AI can dynamically assemble and execute software workflows. By converting AI-generated task lists into executable XState machines, it enables non-technical users to compose software through natural language descriptions.
Recent architectural changes (as of v0.3.0):
- Unified Access: Now uses Vercel AI Gateway for the approved model allowlist
- OIDC First:
VERCEL_OIDC_TOKENworks for Vercel deployments and linked local projects - No Gateway API Key:
AI_GATEWAY_API_KEYis intentionally unsupported - Cost Control: Built-in rate limiting and usage monitoring
- Deprecated Keys:
OPENAI_API_KEY,GOOGLE_GENERATIVE_AI_API_KEY,XAI_API_KEYno longer supported
- Unified AI SDK: AI calls use plain Vercel AI SDK Gateway model IDs
- Tiny Allowlist: Only
openai/gpt-5.4-nanoandgoogle/gemini-3.1-flash-liteare exposed - Removed Routes: Legacy
/api/openai/and/api/gemini/directories have been deleted - New Endpoint: Use unified
/api/ai/chatfor all AI interactions - Centralized Config: All provider setup in
apps/x-reason-web/src/app/api/ai/providers.ts
- Remove old keys from
.env.local:OPENAI_API_KEY,GOOGLE_GENERATIVE_AI_API_KEY - Use Gateway auth: run
pnpm dlx vercel@latest link && pnpm dlx vercel@latest env pull apps/x-reason-web/.env.local - Clear browser localStorage to remove old client-side credentials
- Update any custom integrations to use
/api/ai/chatinstead of legacy provider-specific routes - Run
pnpm installto ensure Vercel AI SDK dependencies are installed
See ENV_CONFIG.md for detailed migration instructions.
For detailed technical documentation, see:
- apps/x-reason-web/src/app/api/reasoning/README.md - Reasoning engine documentation
- Dynamic State Machine Generation: Convert AI-generated task lists into executable XState machines
- Small Model Switcher: Switch between one OpenAI default and one Gemini comparison model
- Real-time Streaming: Server-sent events for live AI response streaming
- Domain-Specific Workflows: Pre-built demos for chemical engineering (Chemli) and user registration (Regie)
- Modern UI: Tailwind CSS with shadcn/ui components for a clean, responsive interface
- Human-in-the-Loop: Support for pause/resume execution with user interaction
- Persistent Context: Maintain state across execution steps
- Visual Debugging: State machine visualizer for understanding workflow execution
- Node.js 20.9+
- pnpm (recommended) or npm
- Vercel AI Gateway auth: OIDC via
pnpm dlx vercel@latest env pull
- Clone the repository:
git clone https://github.com/yourusername/x-reason.git
cd x-reason- Install dependencies:
pnpm install
# or
npm installCreate a .env.local file in the apps/x-reason-web/ directory:
# Pulled from a linked Vercel project
VERCEL_OIDC_TOKEN=your_oidc_token_hereGateway Benefits:
- Single auth path for the approved Gateway models
- Built-in rate limiting and cost monitoring
- Simplified credential management
- Server-side only (no client-side exposure)
See LOCAL_SETUP.md for verification steps and troubleshooting.
Start the development server:
pnpm run dev
# or
npm run devOpen http://localhost:3000 in your browser.
Other available commands:
# Build for production
pnpm run build
# Run production server
pnpm start
# Run tests
pnpm test
# Lint code
pnpm run lint-
State Machine Macro System (
src/app/actions/statesMacros.ts)- Converts task maps to XState configurations
- Supports pause/resume execution
- Maintains context across execution steps
- Enables real-time streaming support
-
AI Gateway System (
src/app/api/ai/)- Tiny model allowlist
- Plain Gateway model IDs
- Streaming response support
-
Domain-Specific Components
- Chemli (
src/app/components/chemli/): Chemical product engineering workflows - Regie (
src/app/components/regie/): Dynamic user registration flows
- Chemli (
The application uses the Vercel AI SDK with a deliberately tiny Gateway model allowlist:
- Architecture: Centralized provider configuration in
src/app/api/ai/providers.ts - Supported Models:
- Default:
openai/gpt-5.4-nano - Comparison:
google/gemini-3.1-flash-lite
- Default:
- Features:
- Gateway-only authentication (
VERCEL_OIDC_TOKEN) - Streaming responses via
streamText() - Server-side credential management
- Runtime model switching
- Cost control through Gateway
- Gateway-only authentication (
Approved models are accessed through Vercel AI Gateway OIDC auth.
Built on XState v5, the system provides:
- Dynamic state machine generation from task lists
- Error handling with retry mechanisms
- Context preservation across states
- Support for long-running async operations
- Human-in-the-loop interactions
X-Reason transforms natural language descriptions into executable software flows:
- Task Generation: AI analyzes user requirements and generates an optimized task list
- State Machine Creation: The macro system converts tasks into XState configurations
- Execution: The state machine executes tasks sequentially with support for:
- User interactions
- External system callbacks
- Async operations (data persistence, notifications)
- Adaptation: AI can modify flows based on context and user behavior
The system can generate customized registration flows based on user context:
// AI analyzes user context (location, visit frequency, etc.)
// and generates an optimized task list:
[
"Collect User Details",
"Age Confirmation", // Added for US/Canada users
"Present Special Offers", // Added for frequent visitors
"Select Plan",
"TOS Acceptance",
"Persist User Details",
"Send Registration Event"
]
// The macro converts this to an executable state machine
const registrationMachine = machineMacro(taskMap);- Vercel AI Gateway: Uses plain Gateway model IDs with OIDC/API-key auth
- Tiny Model Allowlist: Exposes only
openai/gpt-5.4-nanoandgoogle/gemini-3.1-flash-lite - Server-Side Credentials: Removed client credential prompts, all secrets stay server-side
- Next.js 16: Uses the App Router with Turbopack builds
- XState v5: Migrated from v4 with improved APIs
- Modern UI: Replaced Blueprint.js with Tailwind CSS + shadcn/ui
- Streaming: Real-time AI responses via Server-Sent Events
POST /api/ai/chat- Unified streaming chat endpoint for the approved Gateway modelsPOST /api/reasoning/stream- Reasoning engine streaming endpoint
POST /api/state-machine/create- Generate state machine from task listPOST /api/state-machine/execute- Execute state machine with contextGET /api/state-machine/status/:id- Get execution status
|
|
We welcome contributions! Please see our Contributing Guide for details. |
Expect bugs and breaking changes. The focus is on showcasing innovative AI-driven software composition patterns rather than production stability.
- Some TODO items remain in the codebase
- Limited error recovery in certain edge cases
This project is licensed under the MIT License - see the LICENSE file for details.