Exemplo n.º 1
0
class ClaiUnselectCommandRunner(CommandRunner):
    UNSELECT_DIRECTIVE = 'clai deactivate'

    def __init__(self, config_storage: ConfigStorage, agent_datasource: AgentDatasource):
        self.config_storage = config_storage
        self.agent_datasource = agent_datasource
        self.stats_tracker = StatsTracker()

    def execute(self, state: State) -> Action:
        plugin_to_select = state.command.replace(f'{self.UNSELECT_DIRECTIVE}', '').strip()

        plugin_to_select = extract_quoted_agent_name(plugin_to_select)

        selected = self.agent_datasource.unselect_plugin(plugin_to_select, state.user_name)
        if selected:
            self.stats_tracker.log_deactivate_skills(state.user_name, plugin_to_select)
            plugins_config = self.config_storage.read_config(state.user_name)
            if plugins_config.selected is not None and selected.name in plugins_config.selected:
                plugins_config.selected.remove(selected.name)
            self.config_storage.store_config(plugins_config, state.user_name)

        action_to_return = create_message_list(
            self.agent_datasource.get_current_plugin_name(state.user_name),
            self.agent_datasource.all_plugins()
        )
        action_to_return.origin_command = state.command
        return action_to_return
Exemplo n.º 2
0
def stat_uninstall(bin_path):
    agent_datasource = AgentDatasource(config_storage=ConfigStorage(alternate_path=f"{bin_path}/configPlugins.json"))
    report_enable = agent_datasource.get_report_enable()
    stats_tracker = StatsTracker(sync=True, anonymizer=Anonymizer(alternate_path=f"{bin_path}/anonymize.json"))
    stats_tracker.report_enable = report_enable
    login = getpass.getuser()
    stats_tracker.log_uninstall(login)
    print("record uninstall")
Exemplo n.º 3
0
 def __init__(self,
              server_status_datasource:
              ServerStatusDatasource = current_status_datasource,
              connector: ServerConnector = SocketServerConnector(
                  current_status_datasource),
              agent_datasource=AgentDatasource()):
     self.connector = connector
     self.agent_datasource = agent_datasource
     self.server_status_datasource = server_status_datasource
     self.remote_storage = ActionRemoteStorage()
     self.message_handler = MessageHandler(
         server_status_datasource, agent_datasource=agent_datasource)
     self.stats_tracker = StatsTracker()
Exemplo n.º 4
0
def save_report_info(unassisted, agent_datasource, bin_path, demo_mode):
    enable_report = True

    if demo_mode:
        enable_report = False
    elif not unassisted:
        enable_report = \
            ask_to_user(
                "Would you like anonymously send debugging and usage information"
                "to the CLAI team in order to help improve it? (y/n)")

    agent_datasource.mark_report_enable(enable_report)
    stats_tracker = StatsTracker(sync=True, anonymizer=Anonymizer(alternate_path=f'{bin_path}/anonymize.json'))
    stats_tracker.report_enable = enable_report
    stats_tracker.log_install(getpass.getuser())
Exemplo n.º 5
0
class ClaiServer:

    # pylint: disable=too-many-arguments
    def __init__(self,
                 server_status_datasource:
                 ServerStatusDatasource = current_status_datasource,
                 connector: ServerConnector = SocketServerConnector(
                     current_status_datasource),
                 agent_datasource=AgentDatasource()):
        self.connector = connector
        self.agent_datasource = agent_datasource
        self.server_status_datasource = server_status_datasource
        self.remote_storage = ActionRemoteStorage()
        self.message_handler = MessageHandler(
            server_status_datasource, agent_datasource=agent_datasource)
        self.stats_tracker = StatsTracker()

    def init_server(self):
        self.message_handler.init_server()
        self.server_status_datasource.running = True
        self.remote_storage.start(self.agent_datasource)
        self.stats_tracker.start(self.agent_datasource)

    @staticmethod
    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 create_socket(self, host, port):
        self.connector.create_socket(host, port)

    def listen_client_sockets(self):
        self.connector.loop(self.process_message)
        self.remote_storage.wait()
        self.stats_tracker.wait()

    def process_message(self, received_data: bytes) -> Action:
        message = self.serialize_message(received_data)
        return self.message_handler.process_message(message)
Exemplo n.º 6
0
 def __init__(self, config_storage: ConfigStorage, agent_datasource: AgentDatasource):
     self.config_storage = config_storage
     self.agent_datasource = agent_datasource
     self.stats_tracker = StatsTracker()
Exemplo n.º 7
0
class ClaiSelectCommandRunner(CommandRunner, PostCommandRunner):
    SELECT_DIRECTIVE = 'clai activate'

    def __init__(self, config_storage: ConfigStorage,
                 agent_datasource: AgentDatasource):
        self.agent_datasource = agent_datasource
        self.config_storage = config_storage
        self.stats_tracker = StatsTracker()

    def execute(self, state: State) -> Action:
        plugin_to_select = state.command.replace(f'{self.SELECT_DIRECTIVE}',
                                                 '').strip()
        plugin_to_select = extract_quoted_agent_name(plugin_to_select)

        agent_descriptor = self.agent_datasource.get_agent_descriptor(
            plugin_to_select)

        plugins_config = self.config_storage.read_config(None)

        if not agent_descriptor:
            return create_error_select(plugin_to_select)

        if agent_descriptor and not agent_descriptor.installed:
            logger.info(
                f'installing dependencies of plugin {agent_descriptor.name}')

            command = f'$CLAI_PATH/fileExist.sh {agent_descriptor.pkg_name} $CLAI_PATH' \
                    f'{" --user" if plugins_config.user_install else ""}'
            action_selected_to_return = Action(suggested_command=command,
                                               execute=True)
        else:
            self.select_plugin(plugin_to_select, state)
            action_selected_to_return = Action(suggested_command=":",
                                               execute=True)

        action_selected_to_return.origin_command = state.command
        return action_selected_to_return

    def select_plugin(self, plugin_to_select, state):
        selected_plugin = self.agent_datasource.select_plugin(
            plugin_to_select, state.user_name)
        if selected_plugin:
            self.stats_tracker.log_activate_skills(state.user_name,
                                                   plugin_to_select)
            plugins_config = self.config_storage.read_config(state.user_name)
            if plugins_config.selected is None:
                plugins_config.selected = [selected_plugin.name]
            else:
                if selected_plugin.name not in plugins_config.selected:
                    plugins_config.selected.append(selected_plugin.name)
            self.config_storage.store_config(plugins_config, state.user_name)

        return create_message_list(
            self.agent_datasource.get_current_plugin_name(state.user_name),
            self.agent_datasource.all_plugins())

    def execute_post(self, state: State) -> Action:
        plugin_to_select = state.command.replace(f'{self.SELECT_DIRECTIVE}',
                                                 '').strip()
        plugin_to_select = extract_quoted_agent_name(plugin_to_select)

        agent_descriptor = self.agent_datasource.get_agent_descriptor(
            plugin_to_select)

        if not agent_descriptor:
            return Action()

        if state.result_code == '0':
            self.agent_datasource.mark_plugins_as_installed(
                plugin_to_select, state.user_name)
            return self.select_plugin(plugin_to_select, state)

        return create_message_list(
            self.agent_datasource.get_current_plugin_name(state.user_name),
            self.agent_datasource.all_plugins())