Skip to content

Commit

Permalink
changed shared state and caller agent to private attributes in BaseTool
Browse files Browse the repository at this point in the history
  • Loading branch information
VRSEN committed Aug 15, 2024
1 parent e9043bc commit 0708be9
Show file tree
Hide file tree
Showing 21 changed files with 93 additions and 120 deletions.
8 changes: 4 additions & 4 deletions agency_swarm/agency/agency.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ def check_recipient(cls, value):
return value

def run(self):
thread = outer_self.agents_and_threads[self.caller_agent.name][self.recipient.value]
thread = outer_self.agents_and_threads[self._caller_agent.name][self.recipient.value]

if not outer_self.async_mode == 'threading':
message = thread.get_completion(message=self.message,
Expand All @@ -1083,7 +1083,7 @@ def run(self):

return message or ""

SendMessage.caller_agent = agent
SendMessage._caller_agent = agent
if self.async_mode == 'threading':
SendMessage.__doc__ = self.send_message_tool_description_async
SendMessage.ToolConfig.one_call_at_a_time = False
Expand Down Expand Up @@ -1114,11 +1114,11 @@ def check_recipient(cls, value):
return value

def run(self):
thread = outer_self.agents_and_threads[self.caller_agent.name][self.recipient.value]
thread = outer_self.agents_and_threads[self._caller_agent.name][self.recipient.value]

return thread.check_status()

GetResponse.caller_agent = agent
GetResponse._caller_agent = agent

return GetResponse

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ class CreateAgentTemplate(BaseTool):
)

def run(self):
if not self.shared_state.get("manifesto_read"):
if not self._shared_state.get("manifesto_read"):
raise ValueError("Please read the manifesto first with the ReadManifesto tool.")

self.shared_state.set("agent_name", self.agent_name)
self._shared_state.set("agent_name", self.agent_name)

os.chdir(self.shared_state.get("agency_path"))
os.chdir(self._shared_state.get("agency_path"))

# remove folder if it already exists
if os.path.exists(self.agent_name):
Expand All @@ -70,7 +70,7 @@ def run(self):
include_example_tool=False)

# # create or append to init file
path = self.shared_state.get("agency_path")
path = self._shared_state.get("agency_path")
class_name = self.agent_name.replace(" ", "").strip()
if not os.path.isfile("__init__.py"):
with open("__init__.py", "w") as f:
Expand All @@ -87,7 +87,7 @@ def run(self):
with open("agency.py", "w") as f:
f.writelines(lines)

os.chdir(self.shared_state.get("default_folder"))
os.chdir(self._shared_state.get("default_folder"))

if "ceo" in self.agent_name.lower():
return f"You can tell the user that the process of creating {self.agent_name} has been completed, because CEO agent does not need to utilizie any tools or APIs."
Expand Down
14 changes: 7 additions & 7 deletions agency_swarm/agency/genesis/AgentCreator/tools/ImportAgent.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class ImportAgent(BaseTool):
None, description="Path to the agency where the agent will be imported. Default is the current agency.")

def run(self):
if not self.shared_state.get("default_folder"):
self.shared_state.set("default_folder", os.getcwd())
if not self._shared_state.get("default_folder"):
self._shared_state.set("default_folder", os.getcwd())

if not self.shared_state.get("agency_path") and not self.agency_path:
if not self._shared_state.get("agency_path") and not self.agency_path:
return "Error: You must set the agency_path."

if self.shared_state.get("agency_path"):
os.chdir(self.shared_state.get("agency_path"))
if self._shared_state.get("agency_path"):
os.chdir(self._shared_state.get("agency_path"))
else:
os.chdir(self.agency_path)

Expand All @@ -38,7 +38,7 @@ def run(self):
with open("agency.py", "w") as f:
f.writelines(lines)

os.chdir(self.shared_state.get("default_folder"))
os.chdir(self._shared_state.get("default_folder"))

return (f"Success. {self.agent_name} has been imported. "
f"You can now tell the user to user proceed with next agents.")
Expand All @@ -54,5 +54,5 @@ def agent_name_exists(cls, v):

if __name__ == "__main__":
tool = ImportAgent(agent_name="Devid")
tool.shared_state.set("agency_path", "./")
tool._shared_state.set("agency_path", "./")
tool.run()
12 changes: 6 additions & 6 deletions agency_swarm/agency/genesis/AgentCreator/tools/ReadManifesto.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ class ReadManifesto(BaseTool):
)

def run(self):
if not self.shared_state.get("default_folder"):
self.shared_state.set('default_folder', os.getcwd())
if not self._shared_state.get("default_folder"):
self._shared_state.set('default_folder', os.getcwd())

if not self.shared_state.get("agency_path") and not self.agency_name:
if not self._shared_state.get("agency_path") and not self.agency_name:
raise ValueError("Please specify the agency name. Ask user for clarification if needed.")

if self.agency_name:
os.chdir("./" + self.agency_name)
else:
os.chdir(self.shared_state.get("agency_path"))
os.chdir(self._shared_state.get("agency_path"))

with open("agency_manifesto.md", "r") as f:
manifesto = f.read()

os.chdir(self.shared_state.get("default_folder"))
os.chdir(self._shared_state.get("default_folder"))

self.shared_state.set("manifesto_read", True)
self._shared_state.set("manifesto_read", True)

return manifesto
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ class CreateAgencyFolder(BaseTool):
)

def run(self):
if not self.shared_state.get("default_folder"):
self.shared_state.set('default_folder', Path.cwd())
if not self._shared_state.get("default_folder"):
self._shared_state.set('default_folder', Path.cwd())

if self.shared_state.get("agency_name") is None:
if self._shared_state.get("agency_name") is None:
os.mkdir(self.agency_name)
os.chdir("./" + self.agency_name)
self.shared_state.set("agency_name", self.agency_name)
self.shared_state.set("agency_path", Path("./").resolve())
elif self.shared_state.get("agency_name") == self.agency_name and os.path.exists(self.shared_state.get("agency_path")):
os.chdir(self.shared_state.get("agency_path"))
self._shared_state.set("agency_name", self.agency_name)
self._shared_state.set("agency_path", Path("./").resolve())
elif self._shared_state.get("agency_name") == self.agency_name and os.path.exists(self._shared_state.get("agency_path")):
os.chdir(self._shared_state.get("agency_path"))
for file in os.listdir():
if file != "__init__.py" and os.path.isfile(file):
os.remove(file)
else:
os.mkdir(self.shared_state.get("agency_path"))
os.mkdir(self._shared_state.get("agency_path"))
os.chdir("./" + self.agency_name)

# check that agency chart is valid
Expand All @@ -67,7 +67,7 @@ def run(self):
with open(path, "w") as f:
f.write(self.manifesto)

os.chdir(self.shared_state.get('default_folder'))
os.chdir(self._shared_state.get('default_folder'))

return f"Agency folder has been created. You can now tell AgentCreator to create agents for {self.agency_name}.\n"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class FinalizeAgency(BaseTool):

def run(self):
agency_path = None
if self.shared_state.get("agency_path"):
os.chdir(self.shared_state.get("agency_path"))
agency_path = self.shared_state.get("agency_path")
if self._shared_state.get("agency_path"):
os.chdir(self._shared_state.get("agency_path"))
agency_path = self._shared_state.get("agency_path")
else:
os.chdir(self.agency_path)
agency_path = self.agency_path
Expand Down Expand Up @@ -50,7 +50,7 @@ def run(self):

@model_validator(mode="after")
def validate_agency_path(self):
if not self.shared_state.get("agency_path") and not self.agency_path:
if not self._shared_state.get("agency_path") and not self.agency_path:
raise ValueError("Agency path not found. Please specify the agency_path. Ask user for clarification if needed.")


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CreateToolsFromOpenAPISpec(BaseTool):
)

def run(self):
os.chdir(self.shared_state.get("agency_path"))
os.chdir(self._shared_state.get("agency_path"))

os.chdir(self.agent_name)

Expand Down Expand Up @@ -58,9 +58,9 @@ def run(self):
with open("schemas/" + api_name + ".json", "w") as f:
f.write(self.openapi_spec)

return "Successfully added OpenAPI Schema to " + self.shared_state.get("agent_name")
return "Successfully added OpenAPI Schema to " + self._shared_state.get("agent_name")
finally:
os.chdir(self.shared_state.get("default_folder"))
os.chdir(self._shared_state.get("default_folder"))

@field_validator("openapi_spec", mode='before')
@classmethod
Expand Down
18 changes: 9 additions & 9 deletions agency_swarm/agency/genesis/ToolCreator/tools/CreateTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ def run(self):
class MyCustomTool(BaseTool):
def run(self):
# Access the shared state
value = self.shared_state.get("key")
value = self._shared_state.get("key")
# Update the shared state
self.shared_state.set("key", "value")
self._shared_state.set("key", "value")
return "Result of MyCustomTool operation"
# Access shared state in another tool
class AnotherTool(BaseTool):
def run(self):
# Access the shared state
value = self.shared_state.get("key")
value = self._shared_state.get("key")
return "Result of AnotherTool operation"
```
Expand Down Expand Up @@ -132,7 +132,7 @@ def run(self):
if self.agency_name:
os.chdir("./" + self.agency_name)
else:
os.chdir(self.shared_state.get("agency_path"))
os.chdir(self._shared_state.get("agency_path"))
os.chdir(self.agent_name)

client = get_openai_client()
Expand All @@ -153,7 +153,7 @@ def run(self):
prev_content = file.read()
message += f"\n\n```{prev_content}```"
except Exception as e:
os.chdir(self.shared_state.get("default_folder"))
os.chdir(self._shared_state.get("default_folder"))
return f'Error reading {self.tool_name}: {e}'

history.append({
Expand Down Expand Up @@ -212,16 +212,16 @@ def run(self):
if n == 3 or not code:
# remove last message from history
history.pop()
os.chdir(self.shared_state.get("default_folder"))
os.chdir(self._shared_state.get("default_folder"))
return "Error: Could not generate a valid file."
try:
with open("./tools/" + self.tool_name + ".py", "w") as file:
file.write(code)

os.chdir(self.shared_state.get("default_folder"))
os.chdir(self._shared_state.get("default_folder"))
return f'{content}\n\nPlease make sure to now test this tool if possible.'
except Exception as e:
os.chdir(self.shared_state.get("default_folder"))
os.chdir(self._shared_state.get("default_folder"))
return f'Error writing to file: {e}'

@field_validator("requirements", mode="after")
Expand Down Expand Up @@ -250,7 +250,7 @@ def validate_details(cls, v):

@model_validator(mode="after")
def validate_agency_name(self):
if not self.agent_name and not self.shared_state.get("agent_name"):
if not self.agent_name and not self._shared_state.get("agent_name"):
raise ValueError("Please provide agent name.")

check_agency_path(self)
Expand Down
22 changes: 11 additions & 11 deletions agency_swarm/agency/genesis/ToolCreator/tools/TestTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def run(self):
if self.agency_name:
os.chdir("./" + self.agency_name)
else:
os.chdir(self.shared_state.get("agency_path"))
os.chdir(self._shared_state.get("agency_path"))
os.chdir(self.agent_name)

# import tool by self.tool_name from local tools.py file
Expand All @@ -38,7 +38,7 @@ def run(self):
except Exception as e:
raise ValueError(f"Error importing tool {self.tool_name}: {e}")
finally:
os.chdir(self.shared_state.get("default_folder"))
os.chdir(self._shared_state.get("default_folder"))

try:
if not self.arguments:
Expand All @@ -48,7 +48,7 @@ def run(self):
except Exception as e:
raise ValueError(f"Error running tool {self.tool_name}: {e}")
finally:
os.chdir(self.shared_state.get("default_folder"))
os.chdir(self._shared_state.get("default_folder"))

if not output:
raise ValueError(f"Tool {self.tool_name} did not return any output.")
Expand All @@ -59,38 +59,38 @@ def run(self):
def validate_tool_name(self):
check_agency_path(self)

if not self.agent_name and not self.shared_state.get("agent_name"):
if not self.agent_name and not self._shared_state.get("agent_name"):
raise ValueError("Please provide agent name.")

agent_name = self.agent_name or self.shared_state.get("agent_name")
agent_name = self.agent_name or self._shared_state.get("agent_name")

tool_path = os.path.join(self.shared_state.get("agency_path"), agent_name)
tool_path = os.path.join(self._shared_state.get("agency_path"), agent_name)
tool_path = os.path.join(str(tool_path), "tools")
tool_path = os.path.join(tool_path, self.tool_name + ".py")


# check if tools.py file exists
if not os.path.isfile(tool_path):
available_tools = os.listdir(os.path.join(self.shared_state.get("agency_path"), agent_name))
available_tools = os.listdir(os.path.join(self._shared_state.get("agency_path"), agent_name))
available_tools = [tool for tool in available_tools if tool.endswith(".py")]
available_tools = [tool for tool in available_tools if
not tool.startswith("__") and not tool.startswith(".")]
available_tools = [tool.replace(".py", "") for tool in available_tools]
available_tools = ", ".join(available_tools)
raise ValueError(f"Tool {self.tool_name} not found. Available tools are: {available_tools}")

agent_path = os.path.join(self.shared_state.get("agency_path"), self.agent_name)
agent_path = os.path.join(self._shared_state.get("agency_path"), self.agent_name)
if not os.path.exists(agent_path):
available_agents = os.listdir(self.shared_state.get("agency_path"))
available_agents = os.listdir(self._shared_state.get("agency_path"))
available_agents = [agent for agent in available_agents if
os.path.isdir(os.path.join(self.shared_state.get("agency_path"), agent))]
os.path.isdir(os.path.join(self._shared_state.get("agency_path"), agent))]
raise ValueError(f"Agent {self.agent_name} not found. Available agents are: {available_agents}")

return True


if __name__ == "__main__":
TestTool.shared_state.data = {"agency_path": "/Users/vrsen/Projects/agency-swarm/agency-swarm/TestAgency",
TestTool._shared_state.data = {"agency_path": "/Users/vrsen/Projects/agency-swarm/agency-swarm/TestAgency",
"default_folder": "/Users/vrsen/Projects/agency-swarm/agency-swarm/TestAgency"}
test_tool = TestTool(agent_name="TestAgent", tool_name="PrintTestTool", arguments="{}", chain_of_thought="")
print(test_tool.run())
16 changes: 8 additions & 8 deletions agency_swarm/agency/genesis/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@


def check_agency_path(self):
if not self.shared_state.get("default_folder"):
self.shared_state.set('default_folder', Path.cwd())
if not self._shared_state.get("default_folder"):
self._shared_state.set('default_folder', Path.cwd())

if not self.shared_state.get("agency_path") and not self.agency_name:
if not self._shared_state.get("agency_path") and not self.agency_name:
available_agencies = os.listdir("./")
available_agencies = [agency for agency in available_agencies if os.path.isdir(agency)]
raise ValueError(f"Please specify an agency. Available agencies are: {available_agencies}")
elif not self.shared_state.get("agency_path") and self.agency_name:
elif not self._shared_state.get("agency_path") and self.agency_name:
if not os.path.exists(os.path.join("./", self.agency_name)):
available_agencies = os.listdir("./")
available_agencies = [agency for agency in available_agencies if os.path.isdir(agency)]
raise ValueError(f"Agency {self.agency_name} not found. Available agencies are: {available_agencies}")
self.shared_state.set("agency_path", os.path.join("./", self.agency_name))
self._shared_state.set("agency_path", os.path.join("./", self.agency_name))


def check_agent_path(self):
agent_path = os.path.join(self.shared_state.get("agency_path"), self.agent_name)
agent_path = os.path.join(self._shared_state.get("agency_path"), self.agent_name)
if not os.path.exists(agent_path):
available_agents = os.listdir(self.shared_state.get("agency_path"))
available_agents = os.listdir(self._shared_state.get("agency_path"))
available_agents = [agent for agent in available_agents if
os.path.isdir(os.path.join(self.shared_state.get("agency_path"), agent))]
os.path.isdir(os.path.join(self._shared_state.get("agency_path"), agent))]
raise ValueError(f"Agent {self.agent_name} not found. Available agents are: {available_agents}")
Loading

0 comments on commit 0708be9

Please sign in to comment.