Coverage for tinytroupe / tools / tiny_tool.py: 0%

29 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-02-28 17:48 +0000

1from tinytroupe.tools import logger 

2from tinytroupe.utils import JsonSerializableRegistry 

3 

4 

5class TinyTool(JsonSerializableRegistry): 

6 

7 # Define what attributes should be serialized 

8 serializable_attributes = ["name", "description", "real_world_side_effects"] 

9 

10 def __init__(self, name, description, owner=None, real_world_side_effects=False, exporter=None, enricher=None): 

11 """ 

12 Initialize a new tool. 

13 

14 Args: 

15 name (str): The name of the tool. 

16 description (str): A brief description of the tool. 

17 owner (str): The agent that owns the tool. If None, the tool can be used by anyone. 

18 real_world_side_effects (bool): Whether the tool has real-world side effects. That is to say, if it has the potential to change the  

19 state of the world outside of the simulation. If it does, it should be used with caution. 

20 exporter (ArtifactExporter): An exporter that can be used to export the results of the tool's actions. If None, the tool will not be able to export results. 

21 enricher (Enricher): An enricher that can be used to enrich the results of the tool's actions. If None, the tool will not be able to enrich results. 

22  

23 """ 

24 self.name = name 

25 self.description = description 

26 self.owner = owner 

27 self.real_world_side_effects = real_world_side_effects 

28 self.exporter = exporter 

29 self.enricher = enricher 

30 

31 def _process_action(self, agent, action: dict) -> bool: 

32 raise NotImplementedError("Subclasses must implement this method.") 

33 

34 def _protect_real_world(self): 

35 if self.real_world_side_effects: 

36 logger.warning(f" !!!!!!!!!! Tool {self.name} has REAL-WORLD SIDE EFFECTS. This is NOT just a simulation. Use with caution. !!!!!!!!!!") 

37 

38 def _enforce_ownership(self, agent): 

39 if self.owner is not None and agent.name != self.owner.name: 

40 raise ValueError(f"Agent {agent.name} does not own tool {self.name}, which is owned by {self.owner.name}.") 

41 

42 def set_owner(self, owner): 

43 self.owner = owner 

44 

45 def actions_definitions_prompt(self) -> str: 

46 raise NotImplementedError("Subclasses must implement this method.") 

47 

48 def actions_constraints_prompt(self) -> str: 

49 raise NotImplementedError("Subclasses must implement this method.") 

50 

51 def process_action(self, agent, action: dict) -> bool: 

52 self._protect_real_world() 

53 self._enforce_ownership(agent) 

54 return self._process_action(agent, action)