πŸ“AgentManager

Contract for managing agent tasks and lifecycle on the Somnia blockchain.

Overview

The AgentManager contract handles task creation, assignment, and completion for AI agents. It manages the full lifecycle of tasks from creation through completion or cancellation.

Contract Address

Testnet: 0x77F6dC5924652e32DBa0B4329De0a44a2C95691E

πŸ“Š Contract Architecture

contract AgentManager {
    enum TaskStatus {
        Pending,      // 0 - Task created, waiting to be accepted
        InProgress,   // 1 - Task accepted and being worked on
        Completed,    // 2 - Task completed successfully
        Cancelled     // 3 - Task cancelled
    }

    struct Task {
        uint256 agentId;        // ID of the agent assigned to the task
        address requester;      // Address that created the task
        string taskData;        // JSON string with task details
        uint256 payment;        // Payment amount for the task
        TaskStatus status;      // Current status
        uint256 createdAt;      // Creation timestamp
        uint256 completedAt;    // Completion timestamp (0 if not completed)
        string result;          // Task result (empty until completed)
    }
    
    mapping(uint256 => Task) private tasks;
    uint256 private taskCounter;
}
circle-info

Task Management:

  • The struct uses reward for payment amount

  • Status enum: Pending (0), InProgress (1), Completed (2), Failed (3), Cancelled (4)

  • Task includes createdAt and completedAt timestamps

  • Use startTask() to begin execution

  • Use failTask() to mark as failed (with refund)

  • Use cancelTask() to cancel pending tasks (with refund)

Main Functions

Create Task

Create a new task for an agent:

Function Signature:

Parameters:

  • _agentId - ID of the agent to assign the task

  • _taskData - JSON string containing task details

  • Payment sent via msg.value (payable function)

Returns:

  • uint256 - Task ID

Events Emitted:

circle-info

Note: The payment parameter is a regular function parameter, NOT sent as { value: payment } in the transaction options.

Get Task

Get task details:

Function Signature:

Start Task

Start working on a task (agent owner only):

Function Signature:

Requirements:

  • Task must be in Pending status

Events Emitted:

Complete Task

Complete a task with result:

Function Signature:

Requirements:

  • Task must be in InProgress or Pending status

Events Emitted:

Fail Task

Mark a task as failed and refund the requester:

Function Signature:

Requirements:

  • Task must be in InProgress status

Events Emitted:

Cancel Task

Cancel a pending task and refund the requester:

Function Signature:

Requirements:

  • Caller must be the task requester

  • Task must be in Pending status

Events Emitted:

Events

TaskCreated

TaskStarted

TaskCompleted

TaskFailed

TaskCancelled

Task Status

Complete Example

Best Practices

1. Structure Task Data

2. Handle Task Results

3. Monitor Task Progress

⚠️ Important Notes

  1. Payment via msg.value - Payment is sent as transaction value using { value }, not as a function parameter

  2. Task Status - Tasks can be: Pending (0), InProgress (1), Completed (2), Failed (3), or Cancelled (4)

  3. Start Before Complete - Must call startTask() before completeTask()

  4. Failure Handling - Use failTask() to mark as failed with refund, or cancelTask() to cancel pending tasks


Next: Learn about AgentVault for managing agent funds.

Last updated