Stateful Agent
This section describes how to build an Agent capable of maintaining its own state.
In this guide, we build a simple guessing game which can maintain its own state, and uses NPM libraries to enhance the game's experience.
We define a Stateful Agent as an Agent which can take maintain its own memory and event lifecycle.
The source code for this example is available on GitHub.
Video Tutorial
You can follow along this example by watching the video below:
Guide
Step 1: Setup usdk
Follow Setup the SDK to set up NodeJS and usdk
.
Make sure you are logged in.
Step 2: Initialize your agent
Create a new agent:
This will directly scaffold an agent for you in <your-agent-directory>
. Learn more
Your agent directory now contains the Node application and git
repository for your agent, should you choose to use git
.
The -y
flag means to skip the Agent
Interview process,
which we don't need here. You can also omit the agent directory. In that case,
a directory will be created for you.
Step 3: Start the agentic developer environment
Start a chat with your agent in the terminal:
To edit your agent, open agent.tsx
in any IDE or text editor.
Hot reloading is supported by default, so when you save your code the agent will automatically reload so you can test out your changes immediately in the chat terminal.
Step 4: Add the Guessing Game logic
Our guessing game will work like this:
The Agent will choose a random animal, and then give slight hints to the user until the user guesses the word.
We can use unique-names-generator
, an NPM library, to get the random animal name generation logic.
Install this library to your agent:
Note: You can also install any library from npm
.
Edit your agent.tsx
by adding the following code stub:
Next, let's write up our guessing game logic! To maintain state, we'll use useState
in React - for the sake of simplicity.
Step 4: Import the GuessingGame component in our agent.
Step 5: (optional) Test the Guessing Game Agent
Run usdk chat
to test the Agent in your CLI.
You can start the game by asking:
I want to play!
How long does it take you to guess your first animal? Join our Discord community and tell us; we'd love to know!
Further Challenges
- "20 questions": Put a limit on the number of questions, and have the AI answer only YES or NO. If the user makes a guess, the game is considered done!