AI Code Generation
Generate smart contracts and AO processes using AI-powered code generation.
Overview
ao-forge includes AI-powered code generation to help you create smart contracts and AO processes quickly and efficiently.
Basic Usage
# Generate a new AO process
ao-forge ai generate process counter
# Generate with specific description
ao-forge ai generate process token --description "A simple token contract with transfer and balance functions"
# Generate multiple processes
ao-forge ai generate process dao --description "A DAO contract with voting and proposal functionality"
Process Generation
Simple Counter
ao-forge ai generate process counter
This generates a basic counter process with increment, decrement, and get functions.
Token Contract
ao-forge ai generate process token --description "ERC-20 style token with transfer, approve, and balance functions"
DAO Contract
ao-forge ai generate process dao --description "Decentralized Autonomous Organization with voting, proposals, and member management"
Configuration Options
# Specify output directory
ao-forge ai generate process my-process --output ./ao/
# Use specific AI model
ao-forge ai generate process my-process --model gpt-4
# Include comments and documentation
ao-forge ai generate process my-process --include-docs
# Generate TypeScript types
ao-forge ai generate process my-process --generate-types
AI Models
ao-forge supports multiple AI models:
- gpt-4 (default) - Most capable model
- gpt-3.5-turbo - Faster and more cost-effective
- claude-3 - Alternative model with different strengths
# Use specific model
ao-forge ai generate process my-process --model claude-3
Custom Prompts
You can provide custom prompts for more specific generation:
# Custom prompt
ao-forge ai generate process my-process --prompt "Create a voting system with quadratic voting and delegation"
# From file
ao-forge ai generate process my-process --prompt-file ./prompts/voting.txt
Generated Code Structure
The AI generates well-structured Lua code with:
- Proper error handling
- Input validation
- Clear function definitions
- Comprehensive comments
- Type annotations (when requested)
Best Practices
- Be specific in your descriptions
- Use clear naming for processes
- Review generated code before using
- Test thoroughly in development
- Customize as needed for your use case
Examples
Counter Process
-- Counter process with basic operations
Handlers.add("increment", function(msg)
local count = tonumber(AO.load("count") or "0")
AO.store("count", tostring(count + 1))
return { count = count + 1 }
end)
Handlers.add("decrement", function(msg)
local count = tonumber(AO.load("count") or "0")
AO.store("count", tostring(count - 1))
return { count = count - 1 }
end)
Handlers.add("get", function(msg)
local count = tonumber(AO.load("count") or "0")
return { count = count }
end)
Token Process
-- Token process with transfer functionality
Handlers.add("transfer", function(msg)
local from = msg.From
local to = msg.To
local amount = tonumber(msg.Amount)
-- Validate inputs
if not to or not amount or amount <= 0 then
return { error = "Invalid transfer parameters" }
end
-- Check balance
local balance = tonumber(AO.load("balance:" .. from) or "0")
if balance < amount then
return { error = "Insufficient balance" }
end
-- Update balances
AO.store("balance:" .. from, tostring(balance - amount))
AO.store("balance:" .. to, tostring(tonumber(AO.load("balance:" .. to) or "0") + amount))
return { success = true, from = from, to = to, amount = amount }
end)
Troubleshooting
Common Issues
- AI service unavailable - Check your internet connection
- Invalid prompt - Ensure your description is clear and specific
- Generation fails - Try with a simpler description first
Getting Help
# Get help with AI commands
ao-forge ai --help
# List available models
ao-forge ai models
# Check AI service status
ao-forge ai status