From the course: LangGraph.js: Building Agents with Javascript
Setting up our agent: Agent State
From the course: LangGraph.js: Building Agents with Javascript
Setting up our agent: Agent State
The first step to building an AI agent in LandGraph is to define the agent's working memory. This is referred to as the agent's state and it contains all the transient data the agent would need to perform its operations at runtime. It can also help keep a record of the conversation history with the LLM the agent is using. So let's define that. First we create a new file inside our source agent folder and this is going to be called state.js. Inside here, the first thing we'll do is to import our annotation library that will be used to define our state and the messagesStateReducer that will help us in updating our messages state variable. So let's say import, we get annotation and messagesStateReducer. We can pull this to the side and there we'll say from langchain langgraphs. Just as I've said earlier, This will help us define our state schema and also manage our message history with the LLM respectively. Next, we'll create a NoteFlowState variable with the annotation's root command to define our state. So we'll do that by coming down here, we'll say export const NoteFlowState then call the root command of our annotation class. We have now scaffolded our state object and now we can start defining the state variables that we'll be needing to implement our AI features. The first one is going to be note content, and this is going to represent a single note that the user is currently viewing. So whenever the user clicks on a note, this state variable is going to hold the content of that note. Let's define some of its properties. We'll say annotation, and we'll give it a default, which is going to take a callback, and we'll just set this to an empty string. Now I'm going to be using this default expression a couple of times, so I'm just going to copy it. The next variable is going to be for our note title. So we'll call that noteTitle. This is also going to be empty by default and it will hold the title for any selected note. Next, we're going to define a messages variable. This is going to hold our conversation history with the LLM. By default, it's going to be an empty array because our messages are going to be stored in an array in chronological order and we're also going to be adding a reducer. The reducer is going to manage how this message history is going to be updated. And this is where we're going to be using our messages state reducer. So let's grab that and paste it here. Good. The next state variable we're going to be defining is summary. Now we're beginning to get to some of the state variables that are going to help us with our AI implementations. So we'll set this to annotation. It is going to be null by default. And what this simply does is that it holds a summary to any note that we want to summarize. We have a Summarize feature that we want to implement in NoteFlow. And this is the variable that is going to be holding the summary of any note that we want to summarize. Next, we're going to be creating another variable for our suggested tags. For suggested tags, by default, this is going to be an empty array, because we're going to be having more than one tag. We're going to be having an error state variable, where we can store any errors that are generated by our AI process in. I'm going to set that to a default of null. So we have defined the initial state of our NoteFlowAgentState and finally, let us make sure that we are exporting this NoteFlowState variable from this file. Let's say export, default, NoteFlowState. Good. Good. And we are done. For now. We will be coming back to this file from time to time during the course of all our AI implementations to add more state variables to this file if needed. But next, we'll start building our very first agent by writing one of the most important parts of a land graph agent, nodes.