1.0.0 • Published 8 months ago

@earnkit/sdk-farcaster v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Create an Agent and Add a Tool Using the Agent SDK

This guide will help you create an AI agent and add a tool to it using the Agent SDK. Follow the steps below to set up your environment, create an agent, and integrate a new tool.

Pre-Requisites

Before you start implementing the SDK, ensure you have completed the following steps:

  1. Register on the Platform:

    • Go to the platform and register yourself.
  2. Create an API Key:

    • Create an API key that you will use with the Agent SDK.
  3. Create an AI Agent Wallet:

    • Create a wallet that will be used to perform transactions via the agent.
  4. Create a Farcaster Account:

    • Create a Farcaster account to interact with the SDK and manage your agent.
  5. Get API Key and Agent Name:

    • Get the API key and the agent name, which will be required when interacting with the agent SDK if you are adding a new tool.

SDK Implementation

Step-by-Step Flow for Implementation

Step 1: Set Up Your Project

  1. Create a new TypeScript project:

    • Initialize a new TypeScript project.
    • Install the necessary SDK packages.
    npm init -y
    npm install typescript @earnkit/sdk-farcaster
  2. Create index.ts file:

    • In the root of your project, create a file named index.ts.

Step 2: Import SDK in index.ts

In your index.ts file, import the required modules from the Agent SDK:

import { FarcasterAgentSDK } from "@earnkit/sdk-farcaster";
import { AgentConfig, ActionHandler } from "@earnkit/sdk-farcaster";
import { getEncodeFunctionData } from "@earnkit/sdk-farcaster";

Step 3: Initialize the SDK

Set up the SDK and initialize it with your API key:

async function main() {
  // Initialize SDK with API key
  const sdk = new FarcasterAgentSDK({
    apiKey: "YOUR_API_KEY", // Replace with your API key
  });

Step 4: Create Agent Configuration

Define the agent's configuration, including actions, schema, and characteristics:

  const agentConfig: AgentConfig = {
    name: "0xshinchain", // Replace with your agent name
    apiKey: "YOUR_API_KEY", // Replace with your API key
    schemaDescription: "This is an agent that gets the balance of a token",
    actions: [
      {
        name: "store_number",
        description: "Helps the user store a number in a smart contract.",
        schemaDescription: "Instructions for storing a number in a smart contract",
        inputs: [
          {
            name: "num",
            type: "number",
            description: "The number to store",
            required: true,
          },
        ],
        handler: async () => {
          return {};
        },
      },
    ],
    characteristics: {
      tone: "casual",
      style: "concise",
      personality: "friendly",
      customPrompt: "Focus on being helpful and clear.",
    },
  };

Step 5: Create Action Handler

Define the action handler for the tool (in this case, storing a number in a smart contract):

  const storeNumber: ActionHandler = async (
    walletService: any,
    params: any,
  ): Promise<`0x${string}`> => {
    try {
      const smartAccountClient = await walletService.getWalletClient();
      const args: [bigint] = [BigInt(params.num)];
      const txHash = await smartAccountClient.sendTransaction({
        to: "0xa826787bf17ab8f03228F48438feA484d54a16A6", // Replace with the correct address
        data: getEncodeFunctionData(
          "function store(uint256 num) public",
          "store",
          args,
        ),
        value: BigInt(0),
      });
      return txHash;
    } catch (error) {
      console.error("Error in store_number:", error);
      throw new Error(`Failed to store number: ${error}`);
    }
  };

  // Update the handler in the config
  agentConfig.actions[0].handler = storeNumber.toString();

Step 6: Deploy the Agent

Now, deploy the agent using the createAgent method:

  try {
    // Deploy the agent
    const agentStatus = await sdk.createAgent(agentConfig);
    console.log("Agent created:", agentStatus);

    // Monitor agent status
    const status = await sdk.getAgentStatus(agentStatus.agentId);
    console.log("Agent status:", status);

  } catch (error) {
    console.error("Error:", error);
  }
}

main().catch(console.error);

Step 7: Run the Application

Once everything is set up, you can run the project:

ts-node index.ts

This will create the agent, deploy it, and monitor its status.


Example Implementation

https://gist.github.com/Deepak973/205f5d9a12b7373e8218e9c79748966d

Conclusion

You have successfully created an AI agent and added a tool using the Agent SDK. You can now modify this setup to integrate additional tools or further customize the agent's behavior.


Notes:

  • Replace placeholders such as YOUR_API_KEY and agent name with your actual values.
  • Ensure that you have the necessary permissions for transaction execution, wallet access, and smart contract interactions.