| 16 | |
| 17 | |
| 18 | class AgentState: |
| 19 | def __init__(self): |
| 20 | config = Config() |
| 21 | sqlite_path = config.get_sqlite_db() |
| 22 | self.engine = create_engine(f"sqlite:///{sqlite_path}") |
| 23 | SQLModel.metadata.create_all(self.engine) |
| 24 | |
| 25 | def new_state(self): |
| 26 | timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 27 | |
| 28 | return { |
| 29 | "internal_monologue": '', |
| 30 | "browser_session": { |
| 31 | "url": None, |
| 32 | "screenshot": None |
| 33 | }, |
| 34 | "terminal_session": { |
| 35 | "command": None, |
| 36 | "output": None, |
| 37 | "title": None |
| 38 | }, |
| 39 | "step": int(), |
| 40 | "message": None, |
| 41 | "completed": False, |
| 42 | "agent_is_active": True, |
| 43 | "token_usage": 0, |
| 44 | "timestamp": timestamp |
| 45 | } |
| 46 | |
| 47 | def create_state(self, project: str): |
| 48 | with Session(self.engine) as session: |
| 49 | new_state = self.new_state() |
| 50 | new_state["step"] = 1 |
| 51 | new_state["internal_monologue"] = "I'm starting the work..." |
| 52 | agent_state = AgentStateModel(project=project, state_stack_json=json.dumps([new_state])) |
| 53 | session.add(agent_state) |
| 54 | session.commit() |
| 55 | emit_agent("agent-state", [new_state]) |
| 56 | |
| 57 | def delete_state(self, project: str): |
| 58 | with Session(self.engine) as session: |
| 59 | agent_state = session.query(AgentStateModel).filter(AgentStateModel.project == project).all() |
| 60 | if agent_state: |
| 61 | for state in agent_state: |
| 62 | session.delete(state) |
| 63 | session.commit() |
| 64 | |
| 65 | def add_to_current_state(self, project: str, state: dict): |
| 66 | with Session(self.engine) as session: |
| 67 | agent_state = session.query(AgentStateModel).filter(AgentStateModel.project == project).first() |
| 68 | if agent_state: |
| 69 | state_stack = json.loads(agent_state.state_stack_json) |
| 70 | state_stack.append(state) |
| 71 | agent_state.state_stack_json = json.dumps(state_stack) |
| 72 | session.commit() |
| 73 | else: |
| 74 | state_stack = [state] |
| 75 | agent_state = AgentStateModel(project=project, state_stack_json=json.dumps(state_stack)) |
no outgoing calls
no test coverage detected