Skip to content

Latest commit

ย 

History

History
290 lines (216 loc) ยท 8.49 KB

File metadata and controls

290 lines (216 loc) ยท 8.49 KB

๐Ÿ”— Blockchain Integration Documentation

Overview

This integration connects the AI agent orchestrator system with smart contracts for decentralized agent discovery, payments, and reputation tracking. The system gracefully falls back to traditional operation when blockchain services are unavailable.

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚     Frontend        โ”‚    โ”‚    Orchestrator     โ”‚    โ”‚   Smart Contracts   โ”‚
โ”‚                     โ”‚    โ”‚                     โ”‚    โ”‚                     โ”‚
โ”‚ โ€ข Wallet Connect    โ”‚โ—„โ”€โ”€โ–บโ”‚ โ€ข Agent Discovery   โ”‚โ—„โ”€โ”€โ–บโ”‚ โ€ข AgentRegistry     โ”‚
โ”‚ โ€ข Rating UI         โ”‚    โ”‚ โ€ข Payment Handling  โ”‚    โ”‚ โ€ข ReputationLayer   โ”‚
โ”‚ โ€ข Blockchain Status โ”‚    โ”‚ โ€ข Reputation Updatesโ”‚    โ”‚ โ€ข OrchestrationContractโ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿš€ Features Implemented

โœ… Smart Contract Integration

  • Agent Registry: Discover agents from blockchain
  • Reputation Layer: Track and rate agent performance
  • Orchestration Contract: Handle payments and task lifecycle

โœ… Backend Orchestrator Enhancements

  • Enhanced agent discovery (blockchain + traditional fallback)
  • Automatic task completion recording on blockchain
  • Rating submission API endpoints
  • Blockchain status monitoring

โœ… Frontend Integration

  • Wallet connection interface
  • Real-time blockchain status display
  • Rating submission to blockchain
  • Graceful error handling and fallbacks

โœ… Graceful Fallbacks

  • Traditional agent discovery when blockchain unavailable
  • Local rating storage when blockchain fails
  • UI continues to work without wallet connection
  • Backend operates normally without blockchain

๐Ÿ”ง Technical Implementation

Smart Contracts (protocol/)

  • Location: protocol/src/
  • Contracts: AgentRegistry, ReputationLayer, OrchestrationContract
  • Tests: Comprehensive test suite with 51 passing tests
  • Deployment: Local Anvil testnet ready

Backend Services

  • BlockchainService (orchestrator/src/BlockchainService.ts)

    • Contract interaction layer
    • Agent discovery from blockchain
    • Task completion recording
    • Reputation data retrieval
  • Enhanced JobRunner (orchestrator/src/JobRunner.ts)

    • Blockchain-enhanced agent discovery
    • Automatic reputation tracking
    • Payment integration hooks
  • API Endpoints (orchestrator/src/server.ts)

    • POST /agents/rate - Submit agent ratings
    • GET /agents/:agentUrl/reputation - Get reputation data
    • GET /blockchain/status - Check blockchain availability
    • POST /feedback/submit - Submit comprehensive feedback

Frontend Integration

  • BlockchainService (frontend/src/services/blockchain.ts)

    • Wallet connection management
    • Contract interaction from frontend
    • User address retrieval
  • Enhanced ResultPanel (frontend/src/components/ResultPanel.tsx)

    • Blockchain rating submission
    • Transaction hash display
    • Fallback to local storage
  • Workflow Page (frontend/src/pages/workflow/index.tsx)

    • Real-time blockchain status
    • Wallet connection UI
    • Feature availability indicators

๐Ÿ“‹ API Endpoints

Rating & Reputation

// Submit agent rating
POST /agents/rate
{
  "agentUrl": "http://localhost:3001",
  "rating": 5,
  "userAddress": "0x...",
  "feedback": "Great agent!"
}

// Get agent reputation
GET /agents/{agentUrl}/reputation
Returns: {
  "success": true,
  "reputation": {
    "totalTasks": 10,
    "successfulTasks": 9,
    "successRate": 90,
    "averageRating": 4.5,
    "reputationScore": 8500
  }
}

// Submit comprehensive feedback
POST /feedback/submit
{
  "executionId": "exec_123",
  "modelRatings": {
    "DALL-E 3": 5,
    "NFT Deployer": 4
  },
  "overallFeedback": "Excellent workflow!",
  "userAddress": "0x..."
}

Blockchain Status

GET /blockchain/status
Returns: {
  "blockchain": {
    "isAvailable": true,
    "hasWallet": true,
    "features": {
      "agentDiscovery": true,
      "reputationTracking": true,
      "paymentProcessing": true
    }
  }
}

๐Ÿงช Testing the Integration

1. Start Local Blockchain

cd protocol
anvil --host 0.0.0.0 --port 8545

2. Deploy Contracts

cd protocol
PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 forge script script/Deploy.s.sol:DeployLocal --rpc-url http://localhost:8545 --broadcast

3. Start Orchestrator with Blockchain

cd orchestrator
export RPC_URL=http://localhost:8545
export ORCHESTRATOR_PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
npm start

4. Start Frontend

cd frontend
npm run dev

5. Test Workflow

  1. Visit: http://localhost:5173
  2. Check Status: Blockchain status should show "Connected"
  3. Connect Wallet: Click "Connect Wallet" button
  4. Create Workflow: Use example prompt
  5. Submit Rating: After completion, rate agents
  6. Verify: Check console for blockchain interactions

๐Ÿ” Verification Points

Blockchain Integration Working:

  • โœ… Anvil testnet running on port 8545
  • โœ… Contracts deployed successfully
  • โœ… Orchestrator shows "Blockchain: Connected"
  • โœ… Frontend shows blockchain status
  • โœ… Wallet connection works
  • โœ… Rating submission shows transaction hashes

Graceful Fallbacks Working:

  • โœ… Works without anvil running
  • โœ… Works without wallet connected
  • โœ… Rating stored locally when blockchain fails
  • โœ… Agent discovery falls back to HTTP endpoints

๐Ÿšจ Error Handling

Blockchain Unavailable

  • Agent Discovery: Falls back to configured HTTP endpoints
  • Task Recording: Skipped, logs warning
  • Rating Submission: Stored locally with warning message
  • UI Status: Shows "Blockchain: Offline"

Wallet Not Connected

  • Rating Submission: Uses orchestrator wallet as fallback
  • UI: Shows "Connect Wallet" button
  • Functionality: All features still work

Contract Interaction Failures

  • Network Issues: Retry logic with exponential backoff
  • Gas Failures: Graceful error messages
  • Invalid Data: Validation before submission

๐ŸŽฏ User Experience

With Blockchain Connected:

  1. User sees blockchain status in real-time
  2. Ratings are submitted to blockchain with transaction hashes
  3. Agent reputation is pulled from smart contracts
  4. Payments can be processed through escrow contracts

Without Blockchain:

  1. Traditional workflow execution continues
  2. Ratings stored locally for future blockchain submission
  3. Agent discovery uses configured HTTP endpoints
  4. All UI functionality remains available

๐Ÿ”ง Configuration

Environment Variables

# Orchestrator
RPC_URL=http://localhost:8545
ORCHESTRATOR_PRIVATE_KEY=0x...
AGENT_REGISTRY_ADDRESS=0x5FbDB2315678afecb367f032d93F642f64180aa3
REPUTATION_LAYER_ADDRESS=0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
ORCHESTRATION_CONTRACT_ADDRESS=0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0

# Frontend
VITE_ORCHESTRATOR_URL=http://localhost:8080

Contract Addresses (Local Development)

  • AgentRegistry: 0x5FbDB2315678afecb367f032d93F642f64180aa3
  • ReputationLayer: 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
  • OrchestrationContract: 0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0

๐ŸŽ‰ Success Metrics

  • โœ… 51 smart contract tests passing
  • โœ… Agent discovery enhanced with blockchain
  • โœ… Rating system integrated with reputation layer
  • โœ… Graceful fallbacks implemented
  • โœ… Real-time blockchain status monitoring
  • โœ… Wallet connection interface
  • โœ… Comprehensive error handling

The integration successfully bridges the AI agent orchestrator with blockchain infrastructure while maintaining backward compatibility and providing excellent user experience regardless of blockchain availability.

๐Ÿ”ฎ Future Enhancements

  • Agent Registration UI: Allow agents to register via frontend
  • Payment Processing: Full payment flow through OrchestrationContract
  • Multi-chain Support: Support for different blockchain networks
  • IPFS Integration: Store agent metadata on IPFS
  • Governance: DAO for protocol parameter updates