ai command

Generate smart contracts and AO processes using AI-powered code generation.

Syntax

ao-forge ai <subcommand> [options]

Subcommands

  • generate - Generate new code
  • models - List available AI models
  • status - Check AI service status
  • help - Show help information

generate subcommand

Syntax

ao-forge ai generate <type> <name> [options]

Types

  • process - Generate AO process
  • contract - Generate smart contract
  • component - Generate frontend component
  • test - Generate test files

Options

OptionDescriptionDefault
--description <desc>Description of what to generateRequired
--model <model>AI model to usegpt-4
--output <path>Output directory./ao/
--include-docsInclude documentationfalse
--generate-typesGenerate TypeScript typesfalse
--prompt <prompt>Custom prompt-
--prompt-file <file>Prompt from file-
--helpShow help information-

Examples

Generate AO Process

# Basic process generation
ao-forge ai generate process counter --description "A simple counter with increment and decrement functions"

# Advanced process generation
ao-forge ai generate process token --description "ERC-20 style token with transfer, approve, and balance functions" --model gpt-4 --include-docs --generate-types

# Custom output directory
ao-forge ai generate process dao --description "DAO contract with voting and proposal functionality" --output ./contracts/

Generate Smart Contract

# Generate smart contract
ao-forge ai generate contract voting --description "Voting system with quadratic voting and delegation"

# Generate with custom model
ao-forge ai generate contract nft --description "NFT contract with minting and trading" --model claude-3

Generate Frontend Component

# Generate React component
ao-forge ai generate component Counter --description "Counter component with increment/decrement buttons"

# Generate Vue component
ao-forge ai generate component TokenBalance --description "Token balance display component"

Generate Test Files

# Generate tests for process
ao-forge ai generate test counter --description "Tests for counter process functionality"

# Generate tests for contract
ao-forge ai generate test voting --description "Tests for voting contract functionality"

AI Models

Available Models

# List available models
ao-forge ai models

# Use specific model
ao-forge ai generate process counter --model gpt-4
ao-forge ai generate process counter --model gpt-3.5-turbo
ao-forge ai generate process counter --model claude-3

Model Comparison

ModelSpeedQualityCostBest For
gpt-4SlowHighHighComplex logic
gpt-3.5-turboFastMediumLowSimple processes
claude-3MediumHighMediumCode generation

Custom Prompts

Using Custom Prompts

# Custom prompt
ao-forge ai generate process my-process --prompt "Create a voting system with quadratic voting and delegation"

# Prompt from file
ao-forge ai generate process my-process --prompt-file ./prompts/voting.txt

Prompt File Format

# voting.txt
Create a voting system with the following features:
- Quadratic voting mechanism
- Delegation support
- Proposal management
- Vote tallying
- Result reporting

Generated Code Structure

AO Process Structure

-- Generated process with proper structure
Handlers.add("function_name", function(msg)
    -- Input validation
    if not msg.Input then
        return { error = "Missing input" }
    end
    
    -- Process logic
    local result = processInput(msg.Input)
    
    -- Return result
    return { success = true, result = result }
end)

Smart Contract Structure

-- Generated contract with proper structure
-- Contract: Token
-- Description: ERC-20 style token with transfer, approve, and balance functions

-- State management
local function getBalance(address)
    return tonumber(AO.load("balance:" .. address) or "0")
end

local function setBalance(address, amount)
    AO.store("balance:" .. address, tostring(amount))
end

-- Transfer function
Handlers.add("transfer", function(msg)
    local from = msg.From
    local to = msg.To
    local amount = tonumber(msg.Amount)
    
    -- Validation
    if not to or not amount or amount <= 0 then
        return { error = "Invalid transfer parameters" }
    end
    
    local balance = getBalance(from)
    if balance < amount then
        return { error = "Insufficient balance" }
    end
    
    -- Execute transfer
    setBalance(from, balance - amount)
    setBalance(to, getBalance(to) + amount)
    
    return { success = true, from = from, to = to, amount = amount }
end)

Configuration

AI Configuration

# ao.config.yml
ai:
  defaultModel: 'gpt-4'
  apiKey: 'your-api-key'
  maxTokens: 4000
  temperature: 0.7
  timeout: 30000

Environment Variables

# AI service configuration
OPENAI_API_KEY=your-openai-api-key
ANTHROPIC_API_KEY=your-anthropic-api-key
AI_SERVICE_URL=https://api.openai.com/v1

Error Handling

Common Errors

  1. AI service unavailable
    Error: AI service is not available
    Solution: Check your internet connection and API key
    
  2. Invalid model
    Error: Model 'invalid-model' not found
    Solution: Use 'ao-forge ai models' to list available models
    
  3. Generation failed
    Error: Code generation failed
    Solution: Try with a simpler description or different model
    

Best Practices

Code Generation

  1. Be specific in your descriptions
  2. Use clear naming for processes and contracts
  3. Review generated code before using
  4. Test thoroughly in development
  5. Customize as needed for your use case

AI Usage

  1. Use appropriate models for your needs
  2. Provide clear descriptions for better results
  3. Use custom prompts for specific requirements
  4. Monitor API usage and costs
  5. Keep API keys secure