⚑Task Management

Guide for creating and managing tasks with the Somnia Agent Kit SDK.

Overview

Tasks allow you to assign work to AI agents and track their execution on-chain.

Initialize SDK

import { SomniaAgentKit, SOMNIA_NETWORKS } from 'somnia-agent-kit';
import { ethers } from 'ethers';

const kit = new SomniaAgentKit({
  network: SOMNIA_NETWORKS.testnet,
  contracts: {
    agentRegistry: '0xC9f3452090EEB519467DEa4a390976D38C008347',
    agentManager: '0x77F6dC5924652e32DBa0B4329De0a44a2C95691E',
    agentExecutor: '0x157C56dEdbAB6caD541109daabA4663Fc016026e',
    agentVault: '0x7cEe3142A9c6d15529C322035041af697B2B5129',
  },
  privateKey: process.env.PRIVATE_KEY,
});

await kit.initialize();

Create Task

Create a new task for an agent:

circle-info

Note: The createTask function takes 2 parameters: agentId and taskData. Payment is sent via { value } option, not as a third parameter.

Get Task ID from Event

Query Task

Get Task Details

Task Status

circle-info

Note: Task status: Pending (0), Active (1), Completed (2), Failed (3).

Execute Task (Agent Owner)

Start Task

Complete Task

Cancel Task

circle-info

Note: Use startTask() to start a task (changes status to InProgress), completeTask() to finish it, failTask() to mark as failed, and cancelTask() to cancel it.

Listen to Events

Task Created

Task Started

Task Completed

Task Failed

Task Cancelled

circle-info

Note: Events are TaskCreated, TaskStarted, TaskCompleted, TaskFailed, and TaskCancelled.

Complete Example

Best Practices

1. Structure Task Data

2. Handle Task Results

3. Set Appropriate Payments

4. Monitor Task Progress

See Also

Last updated