This package has been deprecated

Author message:

Moved to https://www.npmjs.com/package/@virtuals-protocol/game

virtuals-game
TypeScript icon, indicating that this package has built-in type declarations

0.1.5 • Public • Published

Installation

To install the package, run:

npm install virtuals-game

Example Usage (Twitter)

Setting Up the Agent

  1. Create the Twitter Functions

    Define the functions for posting tweets, searching tweets, and replying to tweets:

    import {
      ExecutableGameFunctionResponse,
      ExecutableGameFunctionStatus,
      GameFunction,
    } from "virtuals-game";
    
    const postTweetFunction = new GameFunction({
      name: "post_tweet",
      description: "Post a tweet",
      args: [
        { name: "tweet", description: "The tweet content" },
        {
          name: "tweet_reasoning",
          description: "The reasoning behind the tweet",
        },
      ] as const,
      executable: async (args) => {
        try {
          // TODO: Implement posting
          console.log(`Posting tweet`, args.tweet);
          console.log(`Reasoning`, args.tweet_reasoning);
    
          return new ExecutableGameFunctionResponse(
            ExecutableGameFunctionStatus.Done,
            "Tweet posted"
          );
        } catch (e) {
          return new ExecutableGameFunctionResponse(
            ExecutableGameFunctionStatus.Failed,
            "Failed to post tweet"
          );
        }
      },
    });
    
    const searchTweetsFunction = new GameFunction({
      name: "search_tweets",
      description: "Search tweets and return results",
      args: [
        { name: "query", description: "The query to search for" },
        { name: "reasoning", description: "The reasoning behind the search" },
      ] as const,
      executable: async (args) => {
        try {
          const query = args.query;
          // TODO: Implement searching of tweets based on query string
          console.log(`Searching tweets for query`, query);
    
          return new ExecutableGameFunctionResponse(
            ExecutableGameFunctionStatus.Done,
            "Tweets searched here are the results: [{tweetId: 1, content: 'Hello World'}, {tweetId: 2, content: 'Goodbye World'}]"
          );
        } catch (e) {
          return new ExecutableGameFunctionResponse(
            ExecutableGameFunctionStatus.Failed,
            "Failed to search tweets"
          );
        }
      },
    });
    
    const replyToTweetFunction = new GameFunction({
      name: "reply_to_tweet",
      description: "Reply to a tweet",
      args: [
        { name: "tweet_id", description: "The tweet id to reply to" },
        { name: "reply", description: "The reply content" },
      ] as const,
      executable: async (args) => {
        try {
          const tweetId = args.tweet_id;
          const reply = args.reply;
    
          // TODO: Implement reply tweet
          console.log(`Replying to tweet`, tweetId);
          console.log(`Replying with`, reply);
    
          return new ExecutableGameFunctionResponse(
            ExecutableGameFunctionStatus.Done,
            `Replied to tweet ${tweetId} with ${reply}`
          );
        } catch (e) {
          return new ExecutableGameFunctionResponse(
            ExecutableGameFunctionStatus.Failed,
            "Failed to reply to tweet"
          );
        }
      },
    });
  2. Create the Worker

    Define a worker that uses the functions in twitter.ts:

    import { GameWorker } from "virtuals-game";
    
    const postTweetWorker = new GameWorker({
      id: "twitter_main_worker",
      name: "Twitter main worker",
      description: "Worker that posts tweets",
      functions: [searchTweetsFunction, replyToTweetFunction, postTweetFunction],
      // Optional: Provide environment to LLP
      getEnvironment: async () => {
        return {
          tweet_limit: 15,
        };
      },
    });
  3. Create the Agent

    Define an agent that uses the worker in agent.ts:

    import { GameAgent } from "virtuals-game";
    
    const agent = new GameAgent("YOUR_API_KEY", {
      name: "Twitter Bot",
      goal: "Search and reply to tweets",
      description: "A bot that searches for tweets and replies to them",
      workers: [postTweetWorker],
      // Optional: Provide state to HLP
      getAgentState: async () => {
        return {
          username: "twitter_bot",
          follower_count: 1000,
          tweet_count: 10,
        };
      },
    });

Running the Agent

The agent will initialize and start running, performing actions such as posting tweets, searching for tweets, and replying to tweets at regular intervals.

await agent.init();
// running at a fix interval of 60 seconds
await agent.run(60);

Running Agent (without fix interval)

With the step function app has more control over in interval

await agent.step();

License

This project is licensed under the MIT License.

Readme

Keywords

none

Package Sidebar

Install

npm i virtuals-game

Weekly Downloads

3

Version

0.1.5

License

ISC

Unpacked Size

35.6 kB

Total Files

6

Last publish

Collaborators

  • zuhwa