def execute(self, state: State) -> Action: command_to_check = state.command.replace(CLAI_COMMAND_NAME, "", 1).strip() state.command = command_to_check if not command_to_check.strip(): return ClaiHelpCommandRunner().execute(state) if command_to_check.startswith('"'): possible_agents = command_to_check.split('"')[1::2] if possible_agents: agent_name = possible_agents[0] self.agent.force_agent = agent_name state.command = command_to_check.replace( f'"{agent_name}"', "", 1).strip() action = self.agent.execute(state) if not action: return ClaiHelpCommandRunner().execute(state) if isinstance(action, Action): if action.is_same_command() and not action.description: return ClaiHelpCommandRunner().execute(state) if isinstance(action, List): diffent_actions = list( filter(lambda value: not value.is_same_action(), action)) if not diffent_actions: return ClaiHelpCommandRunner().execute(state) return action
def process_message(self, message: State) -> Action: try: message = self.server_status_datasource.store_info(message) if message.is_post_process(): message = self.server_status_datasource.find_message_stored(message.command_id, message.user_name) return self.process_post_command(message) if message.is_command(): return self.__process_command(message) # pylint: disable=broad-except except Exception as ex: logger.info(f"error processing message {ex}") logger.info(traceback.format_exc()) return Action(origin_command=message.command)
def setUpClass(cls): cls.state = State(user_name='tester', command_id='0', command="show me the list of cloud tags", result_code='0') cls.agent = NLC2CMD()
def complete_history(self, message: State): lines = read_history() index = self.find_value(lines, message) if index: last_values = lines[index::] message.values_executed = last_values if message.action_suggested.suggested_command \ and message.action_suggested.suggested_command in last_values[0]: message.suggested_executed = True else: message.values_executed = [] message.suggested_executed = False return self.server_status_datasource.store_info(message)
def setUpClass(cls): cls.state = State(user_name='tester', command_id='0', command="./tmp_file.sh", result_code='1', stderr="Permission denied") cls.agent = HelpMeAgent()
def __process_command(self, message: State) -> Action: if not message.is_already_processed(): message.previous_execution = self.server_status_datasource.get_last_message(message.user_name) actions = self.__process_command_ai(message) message.mark_as_processed() logger.info(f"after setting info: {message.is_already_processed()}") self.server_status_datasource.store_info(message) action = self.server_pending_actions_datasource.store_pending_actions( message.command_id, actions, message.user_name) else: logger.info(f"we have pending action") action = self.server_pending_actions_datasource.get_next_action(message.command_id, message.user_name) if action is None: action = Action( suggested_command=message.command, origin_command=message.command, execute=False ) action.origin_command = message.command if message.is_post_process(): message.action_post_suggested = action else: message.action_suggested = action self.server_status_datasource.store_info(message) action.execute = action.execute or self.server_status_datasource.is_power() return action
def print_and_verify(self, question, answer): state = State(user_name='tester', command_id='0', command=question) action = self.agent.get_next_action(state=state) print(f"Input: {state.command}") print("===========================") print(f"Response: {action.suggested_command}") print("===========================") print(f"Explanation: {action.description}") self.assertEqual(answer, action.suggested_command)
def test_get_next_action_disk(self): self.agent.init_agent() state = State(user_name='tester', command_id='0', command="find out disk usage per user?") action = self.agent.get_next_action(state=state) print("Input: {}".format(state.command)) print("===========================") print("Response: {}".format(action.suggested_command)) print("===========================") print("Explanation: {}".format(action.description)) self.assertEqual('man df', action.suggested_command)
def test_get_next_action_pwd_without_question(self): self.agent.init_agent() state = State(user_name='tester', command_id='0', command="pwd") action = self.agent.get_next_action(state=state) print("Input: {}".format(state.command)) print("===========================") print("Response: {}".format(action.suggested_command)) print("===========================") print("Explanation: {}".format(action.description)) self.assertEqual('pwd', action.suggested_command)
def test_get_next_action_zip(self): self.agent.init_agent() state = State(user_name='tester', command_id='0', command="How to process gz files?") action = self.agent.get_next_action(state=state) print("Input: {}".format(state.command)) print("===========================") print("Response: {}".format(action.suggested_command)) print("===========================") print("Explanation: {}".format(action.description)) self.assertEqual('man gzip', action.suggested_command)
def test_get_next_action_sudo(self): self.agent.init_agent() state = State(user_name='tester', command_id='0', command="when to use sudo vs su?") action = self.agent.get_next_action(state=state) print("Input: {}".format(state.command)) print("===========================") print("Response: {}".format(action.suggested_command)) print("===========================") print("Explanation: {}".format(action.description)) self.assertEqual('man su', action.suggested_command)
def serialize_message(data) -> State: StateDTO.update_forward_refs() dto = StateDTO(**json.loads(data)) return State(command_id=dto.command_id, user_name=dto.user_name, command=dto.command, root=dto.root, processes=dto.processes, file_changes=dto.file_changes, network=dto.network, result_code=dto.result_code, stderr=dto.stderr)
def scaffold_command_response(cls, **kwargs) -> Action: state = State(command_id='0', user_name='tester', command=kwargs['command'], result_code=kwargs['result_code'], stderr=kwargs['stderr']) print(f"Command: {kwargs['command']}") print(f"RetCode: {kwargs['result_code']}") print(f"stdout: '{kwargs['stdout']}'") print(f"stderr: '{kwargs['stderr']}'") print("===========================") #action = cls.agent.get_next_action(state=state) action = cls.agent.post_execute(state=state) print("Input: {}".format(state.command)) print("===========================") print("Response: {}".format(action.suggested_command)) print("===========================") print("Explanation: {}".format(action.description)) return action
def clai_plugins_state(): return State(command_id=ANY_ID, user_name=ANY_NAME, command="clai skills") def clai_power_state(): return State(command_id=ANY_ID, user_name=ANY_NAME, command="clai auto") def clai_power_disabled_state(): return State(command_id=ANY_ID, user_name=ANY_NAME, command="clai manual") ANY_COMMAND_MESSAGE = State(command_id=ANY_ID, user_name=ANY_NAME, command=ANY_INPUT_COMMAND) def command_state(): return State(command_id=ANY_ID, user_name=ANY_NAME, command="command") COMMAND_AGENT_STATE = State(command_id=ANY_ID, user_name=ANY_NAME, command="clai command") COMMAND_NAME_AGENT_STATE = State(command_id=ANY_ID, user_name=ANY_NAME, command='clai "demo agent" command')
def clai_plugins_state(): return State(command_id=ANY_ID, user_name=ANY_NAME, command="clai skills")
def clai_power_state(): return State(command_id=ANY_ID, user_name=ANY_NAME, command="clai auto")
def clai_power_disabled_state(): return State(command_id=ANY_ID, user_name=ANY_NAME, command="clai manual")
def clai_unselect_state(plugin_to_select): return State(command_id=ANY_ID, user_name=ANY_NAME, command=f"clai unselect {plugin_to_select}")
def command_state(): return State(command_id=ANY_ID, user_name=ANY_NAME, command="command")