Mini Program Development#
AI-Powered Development Environment
An AI-assisted mini program development IDE that demonstrates AEnvironment’s capabilities for tool integration, deployment, and scaling. This IDE enables developers to create web applications through natural language conversations with an AI agent.
Features#
AI Agent: Multi-turn conversation powered by OpenAI API with automatic retry logic for rate limits
Virtual File System (VFS): Persistent file system for managing mini program files
MCP Tools: Model Context Protocol tools for file operations and code execution
Live Preview: Real-time preview of generated applications in an iframe
Responsive Design: Automatically ensures generated content fits the preview window
Version Tracking: Displays version numbers on generated pages for update verification
Architecture#
The project is organized into three main directories:
mini-program/
├── agent/ # Agent server (FastAPI + OpenAI integration)
│ └── agent_server.py
├── environment/ # AEnvironment tools and configuration
│ ├── src/ # MCP tools (read_file, write_file, etc.)
│ ├── config.json # Environment configuration
│ ├── Dockerfile # Container build configuration
│ └── requirements.txt
└── frontend/ # Web UI (HTML/CSS/JavaScript)
├── index.html
├── app.js
└── style.css
Component Overview#
Frontend: HTML/CSS/JS interface with split view (chat | live preview)
Agent Server: FastAPI backend that integrates OpenAI API with MCP tools
MCP Server: AEnvironment MCP server providing tools via Model Context Protocol
Tools:
read_file: Read files from virtual file systemwrite_file: Write files to virtual file systemlist_files: List all files in the VFSexecute_python_code: Execute Python code snippets for validationvalidate_html: Validate HTML structure and syntaxcheck_responsive_design: Analyze CSS for responsive design compliance
Prerequisites#
Python 3.8+ installed
AEnvironment SDK installed (
pip install aenvironment)OpenAI API Key (required for AI chat functionality)
Node.js and npm (for MCP Inspector, installed automatically by
aenv run)
Installation#
Install Python dependencies:
pip install -r environment/requirements.txt
Set environment variables:
export OPENAI_API_KEY='your-api-key-here'
export OPENAI_MODEL='gpt-4' # Optional, defaults to gpt-4
export OPENAI_BASE_URL='https://api.openai.com/v1' # Optional, for custom endpoints
export AENV_ENV_NAME='mini-program@1.0.0' # Optional, environment name
Quick Start#
Step 1: Start the MCP Server (Environment Side)#
Open a terminal and navigate to the environment directory:
cd environment
aenv run
This will:
Start the AEnvironment MCP server
Load tools from
environment/src/Start the MCP Inspector (available at
http://localhost:6274)
Keep this terminal running.
Step 2: Start the Agent Server (Agent Side)#
Open a new terminal and navigate to the agent directory:
cd agent
python agent_server.py
This will:
Start the FastAPI server on port 8080
Connect to the MCP server for tool access
Enable the web interface
Keep this terminal running.
Step 3: Open the Web Interface#
Open your browser and navigate to:
http://localhost:8080
Step 4: Start Building#
Type your request in the chat input (e.g., “Create a simple counter mini program”)
The AI agent will:
Analyze your request
Use tools to read existing files (if any)
Create or modify files as needed
Generate complete HTML/CSS/JavaScript applications
The Live Preview will automatically update to show your generated application
Click the “🔄 Refresh” button to manually refresh the preview
Usage Examples#
Example 1: Create a Counter Application#
User: “Create a simple counter mini program”
Agent Actions:
Uses
list_filesto check existing filesCreates
index.htmlwith HTML structure, CSS styling, and JavaScript logicUses
validate_htmlto check for errorsUses
check_responsive_designto ensure responsive layoutPreview automatically updates
Result: A fully functional counter application appears in the Live Preview.
Example 2: Modify Existing Application#
User: “Add a reset button to the counter”
Agent Actions:
Uses
read_fileto read the existingindex.htmlModifies the file to add a reset button
Increments the version number displayed on the page
Preview updates automatically
Result: The counter now includes a reset button.
Example 3: Create a Game#
User: “Create a Snake game”
Agent Actions:
Plans the game structure (HTML canvas, game logic, controls)
Creates
index.htmlwith complete game implementationEnsures responsive design (scales to fit preview window)
Adds version number tracking
Preview shows the playable game
Result: A fully playable Snake game in the Live Preview.
Workflow#
User Input: Type a natural language request in the chat
Agent Planning: AI agent analyzes the request and plans the implementation
Tool Execution: Agent uses MCP tools to:
Check existing files (
list_files)Read files if needed (
read_file)Create/modify files (
write_file)Validate code (
validate_html,check_responsive_design)Execute Python code for calculations (
execute_python_code)
Automatic Preview: System automatically renders the generated HTML in Live Preview
Iteration: User can provide feedback and continue refining the application
Features in Detail#
Rate Limit Handling#
The agent server includes automatic retry logic with exponential backoff for OpenAI API rate limit errors (429):
Max Retries: 3 attempts
Initial Delay: 1 second
Backoff Factor: 2x (delays: 1s, 2s, 4s)
Max Delay: 60 seconds
Virtual File System#
Persistent Storage: Files are stored in
/tmp/code/on diskIn-Memory Cache: Fast access to frequently used files
Automatic Loading: Existing files are loaded on server startup
Clear Function: Use
/api/clearendpoint to reset the VFS
Responsive Design Enforcement#
The agent is instructed to:
Use viewport-relative units (vw, vh, %, clamp)
Ensure content fits without scrolling
Scale canvas elements dynamically
Use flexible layouts (flexbox, grid)
Version Tracking#
Every generated index.html includes a visible version number that increments with each modification, helping users verify that updates are working correctly.
API Endpoints#
Agent Server Endpoints#
GET /: Serve the main HTML interfacePOST /api/chat: Send chat messages and receive AI responsesGET /api/files: List all files in VFSGET /api/files/{file_path}: Get file contentPOST /api/files/{file_path}: Save file contentPOST /api/clear: Clear all files from VFSGET /health: Health check endpointWebSocket /ws: Real-time communication (for future use)
Configuration#
Environment Variables#
OPENAI_API_KEY(required): Your OpenAI API keyOPENAI_MODEL(optional): Model to use (default: “gpt-4”)OPENAI_BASE_URL(optional): Custom API endpoint URLAENV_ENV_NAME(optional): Environment name (default: “mini-program@1.0.0”)
Project Configuration#
environment/config.json: AEnvironment project metadataenvironment/Dockerfile: Container build configurationenvironment/requirements.txt: Python dependencies
Troubleshooting#
MCP Server Not Starting#
Ensure you’re in the
environmentdirectory when runningaenv runCheck that
environment/src/contains the tool filesVerify AEnvironment SDK is installed:
pip list | grep aenv
Agent Server Connection Issues#
Ensure the MCP server is running first
Check that
AENV_ENV_NAMEmatches the environment nameVerify OpenAI API key is set correctly
Static Files Not Loading#
Ensure you’re running
agent_server.pyfrom theagent/directoryCheck that
frontend/directory exists at the project rootVerify FastAPI is serving static files (check server logs)
Rate Limit Errors#
The system automatically retries with exponential backoff
If errors persist, check your OpenAI API quota
Consider using a different model or reducing request frequency
Testing#
The project includes basic functionality tests. Run tests using pytest or your preferred testing framework.
Architecture Diagram#
┌─────────────────────────────────────────────────────────┐
│ Browser (User) │
│ http://localhost:8080 │
└────────────────────┬────────────────────────────────────┘
│ HTTP/WebSocket
┌────────────────────▼────────────────────────────────────┐
│ Agent Server (FastAPI) │
│ Port: 8080 │
│ │
│ ┌──────────────────────────────────────────────────┐ │
│ │ OpenAI API Client │ │
│ │ - Chat Completions │ │
│ │ - Tool Calling │ │
│ │ - Retry Logic (429 handling) │ │
│ └──────────────┬───────────────────────────────────┘ │
│ │ HTTP API │
│ ┌──────────────▼───────────────────────────────────┐ │
│ │ MCP Client │ │
│ │ - Tool Discovery │ │
│ │ - Tool Execution │ │
│ └──────────────┬───────────────────────────────────┘ │
└─────────────────┼───────────────────────────────────────┘
│ MCP Protocol (HTTP)
┌─────────────────▼───────────────────────────────────────┐
│ AEnvironment MCP Server │
│ Port: 8081 │
│ │
│ ┌──────────────────────────────────────────────────┐ │
│ │ MCP Tools (from environment/src/) │ │
│ │ - read_file │ │
│ │ - write_file │ │
│ │ - list_files │ │
│ │ - execute_python_code │ │
│ │ - validate_html │ │
│ │ - check_responsive_design │ │
│ └──────────────┬───────────────────────────────────┘ │
│ │ │
│ ┌──────────────▼───────────────────────────────────┐ │
│ │ Virtual File System (VFS) │ │
│ │ Storage: /tmp/code/ │ │
│ │ - Persistent on disk │ │
│ │ - In-memory cache │ │
│ └──────────────────────────────────────────────────┘ │
└──────────────────────────────────────────────────────────┘
Development#
Project Structure#
agent/: Agent server code (FastAPI application)environment/src/: MCP tools implementationfrontend/: Web UI filestest_*.py: Test scripts for verification
Adding New Tools#
Create a new tool function in
environment/src/mini_program_tools.pyUse the
@register_tooldecoratorRestart the MCP server (
aenv run)The tool will be automatically available to the agent
Modifying the Frontend#
Edit files in
frontend/directoryChanges take effect immediately (no rebuild needed)
Refresh the browser to see updates
License#
Copyright 2025. Licensed under the Apache License, Version 2.0.
Support#
For issues or questions:
Check the troubleshooting section above
Review server logs for error messages
Verify all prerequisites are installed
Ensure environment variables are set correctly