| 44 | |
| 45 | |
| 46 | class Agent: |
| 47 | def __init__(self, |
| 48 | api_key: str, |
| 49 | name: str, |
| 50 | agent_goal: str, |
| 51 | agent_description: str, |
| 52 | get_agent_state_fn: Callable, |
| 53 | workers: Optional[List[WorkerConfig]] = None, |
| 54 | ): |
| 55 | |
| 56 | self._base_url: str = "https://game.virtuals.io" |
| 57 | self._api_key: str = api_key |
| 58 | |
| 59 | # checks |
| 60 | if not self._api_key: |
| 61 | raise ValueError("API key not set") |
| 62 | |
| 63 | # initialize session |
| 64 | self._session = Session() |
| 65 | |
| 66 | self.name = name |
| 67 | self.agent_goal = agent_goal |
| 68 | self.agent_description = agent_description |
| 69 | |
| 70 | # set up workers |
| 71 | if workers is not None: |
| 72 | self.workers = {w.id: w for w in workers} |
| 73 | else: |
| 74 | self.workers = {} |
| 75 | self.current_worker_id = None |
| 76 | |
| 77 | # get agent/task generator state function |
| 78 | self.get_agent_state_fn = get_agent_state_fn |
| 79 | |
| 80 | # initialize and set up agent states |
| 81 | self.agent_state = self.get_agent_state_fn(None, None) |
| 82 | |
| 83 | # create agent |
| 84 | self.agent_id = create_agent( |
| 85 | self._base_url, self._api_key, self.name, self.agent_description, self.agent_goal |
| 86 | ) |
| 87 | |
| 88 | def compile(self): |
| 89 | """ Compile the workers for the agent - i.e. set up task generator""" |
| 90 | if not self.workers: |
| 91 | raise ValueError("No workers added to the agent") |
| 92 | |
| 93 | workers_list = list(self.workers.values()) |
| 94 | |
| 95 | self._map_id = create_workers( |
| 96 | self._base_url, self._api_key, workers_list) |
| 97 | self.current_worker_id = next(iter(self.workers.values())).id |
| 98 | |
| 99 | # initialize and set up worker states |
| 100 | worker_states = {} |
| 101 | for worker in workers_list: |
| 102 | dummy_function_result = FunctionResult( |
| 103 | action_id="", |