feat: add a last_run field to the agent state model (#1124)

This commit is contained in:
Charles Packer
2024-03-10 18:06:00 -07:00
committed by GitHub
parent 47a14b5917
commit d1a22d0b69
3 changed files with 20 additions and 3 deletions

View File

@@ -58,6 +58,7 @@ class AgentStateModel(BaseModel):
user_id: uuid.UUID = Field(..., description="The unique identifier of the user associated with the agent.")
# timestamps
# created_at: datetime = Field(default_factory=datetime.now, description="The unix timestamp of when the agent was created.")
created_at: int = Field(..., description="The unix timestamp of when the agent was created.")
# preset information

View File

@@ -51,7 +51,7 @@ def validate_agent_name(name: str) -> str:
def setup_agents_config_router(server: SyncServer, interface: QueuingInterface, password: str):
get_current_user_with_server = partial(partial(get_current_user, server), password)
@router.get("/agents", tags=["agents"], response_model=GetAgentResponse)
@router.get("/agents/config", tags=["agents"], response_model=GetAgentResponse)
def get_agent_config(
agent_id: str = Query(..., description="Unique identifier of the agent whose config is requested."),
user_id: uuid.UUID = Depends(get_current_user_with_server),

View File

@@ -684,16 +684,32 @@ class SyncServer(LockingServer):
}
return agent_config
def list_agents(self, user_id: uuid.UUID) -> dict:
def list_agents(self, user_id: uuid.UUID, add_last_run: bool = True) -> dict:
"""List all available agents to a user"""
if self.ms.get_user(user_id=user_id) is None:
raise ValueError(f"User user_id={user_id} does not exist")
agents_states = self.ms.list_agents(user_id=user_id)
agents_states_dicts = [self._agent_state_to_config(state) for state in agents_states]
if add_last_run:
# TODO add a get_message_obj_from_message_id(...) function
# this would allow grabbing Message.created_by without having to load the agent object
for agent_state, return_dict in zip(agents_states, agents_states_dicts):
# Get the agent object (loaded in memory)
memgpt_agent = self._get_or_load_agent(user_id=user_id, agent_id=agent_state.id)
# Retrive the Message object via the recall storage or by directly access _messages
last_msg_obj = memgpt_agent._messages[-1]
# Update the return to include the last_run info
# NOTE: 'last_run' is just the timestamp on the latest message in the buffer
return_dict["last_run"] = last_msg_obj.created_at
logger.info(f"Retrieved {len(agents_states)} agents for user {user_id}:\n{[vars(s) for s in agents_states]}")
return {
"num_agents": len(agents_states),
"agents": [self._agent_state_to_config(state) for state in agents_states],
"agents": agents_states_dicts,
}
def get_agent(self, user_id: uuid.UUID, agent_id: uuid.UUID):