From b96b0f8915233c99d0c40c535bbd6521ebdc46a1 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 23 Apr 2024 18:31:41 -0700 Subject: [PATCH] refactor --- src/matrix.py | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/matrix.py b/src/matrix.py index 5b4ba25..e9a7278 100644 --- a/src/matrix.py +++ b/src/matrix.py @@ -238,6 +238,24 @@ def add_agent_to_simulation(self, agent): self.agents.append(agent) + def inject_thoughts(): + if self.redis_connection: + control_cmd = self.redis_connection.lpop(f"{self.id}:communications") + if control_cmd: + control_cmd_str = control_cmd.decode('utf-8') + try: + control_cmd_dict = json.loads(control_cmd_str) + + name = control_cmd_dict.get('name', None) + msg = control_cmd_dict.get('msg', None) + control_type = control_cmd_dict.get('type', "thought") + + if name and msg: + for agent in self.agents: + if name == agent.name: + agent.addMemory(kind=control_type, content=msg, timestamp=unix_to_strftime(self.unix_time), score=10) + except json.JSONDecodeError as e: + print(f"Error decoding control_cmd: {e}") def run_singlethread(self): @@ -254,23 +272,7 @@ def run_singlethread(self): start_time = datetime.now() pd(f"Step {step + 1}:") - if self.redis_connection: - control_cmd = self.redis_connection.lpop(f"{self.id}:communications") - if control_cmd: - control_cmd_str = control_cmd.decode('utf-8') - try: - control_cmd_dict = json.loads(control_cmd_str) - - name = control_cmd_dict.get('name', None) - msg = control_cmd_dict.get('msg', None) - control_type = control_cmd_dict.get('type', None) - - if name: - for agent in self.agents: - if name == agent.name: - agent.addMemory(kind=control_type, content=msg, timestamp=unix_to_strftime(self.unix_time), score=10) - except json.JSONDecodeError as e: - print(f"Error decoding control_cmd: {e}") + self.inject_thoughts() # Submit agent actions concurrently for i in range(len(self.agents)):