forked from Josh-XT/AGiXT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.py
More file actions
111 lines (103 loc) · 4.48 KB
/
Commands.py
File metadata and controls
111 lines (103 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import importlib
import os
from inspect import signature, Parameter
from Config.Agent import Agent
class Commands:
def __init__(self, agent_name: str = "Agent-LLM", load_commands_flag: bool = True):
if agent_name == "undefined":
self.agent_name = "Agent-LLM"
else:
self.agent_name = agent_name
self.CFG = Agent(self.agent_name)
self.agent_folder = self.CFG.create_agent_folder(self.agent_name)
# self.agent_config_file = self.CFG.create_agent_config_file(self.agent_folder)
self.agent_config = self.CFG.load_agent_config(self.agent_name)
if load_commands_flag:
self.commands = self.load_commands()
else:
self.commands = []
self.available_commands = self.get_available_commands()
def get_available_commands(self):
available_commands = []
for command in self.commands:
friendly_name, command_module, command_name, command_args = command
if (
"commands" in self.agent_config
and friendly_name in self.agent_config["commands"]
):
if (
self.agent_config["commands"][friendly_name] == "true"
or self.agent_config["commands"][friendly_name] == True
):
# Add command to list of commands to return
available_commands.append(
{
"friendly_name": friendly_name,
"name": command_name,
"args": command_args,
"enabled": True,
}
)
else:
available_commands.append(
{
"friendly_name": friendly_name,
"name": command_name,
"args": command_args,
"enabled": False,
}
)
return available_commands
def load_commands(self):
commands = []
command_files = self.CFG.load_command_files()
for command_file in command_files:
module_name = os.path.splitext(os.path.basename(command_file))[0]
module = importlib.import_module(f"commands.{module_name}")
if issubclass(getattr(module, module_name), Commands):
command_class = getattr(module, module_name)()
if hasattr(command_class, "commands"):
for (
command_name,
command_function,
) in command_class.commands.items():
params = self.get_command_params(command_function)
# Store the module along with the function name
commands.append(
(
command_name,
getattr(module, module_name),
command_function.__name__,
params,
)
)
# Return the commands list
return commands
def get_command_params(self, func):
params = {}
sig = signature(func)
for name, param in sig.parameters.items():
if param.default == Parameter.empty:
params[name] = None
else:
params[name] = param.default
return params
def find_command(self, command_name: str):
for name, module, function_name, params in self.commands:
if function_name == command_name:
command_function = getattr(module, function_name)
return command_function, module, params # Updated return statement
return None, None, None # Updated return statement
def get_commands_list(self):
self.commands = self.load_commands(agent_name=self.agent_name)
commands_list = [command_name for command_name, _, _ in self.commands]
return commands_list
def execute_command(self, command_name: str, command_args: dict):
command_function, module, params = self.find_command(command_name)
if command_function is None:
return False
for name, value in command_args.items():
if name in params:
params[name] = value
output = command_function(module, **params)
return output