def test_should_return_the_list_with_the_new_selected_values_if_exists_and_is_installed( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["demo_agent"]), autospec=True) mocker.patch.object(ConfigStorage, 'store_config', return_value=None, autospec=True) mocker.patch.object(AgentDatasource, 'all_plugins', return_value=ALL_PLUGINS_WITH_TAR_INSTALLED, autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) select_agent = clai_select_state('nlc2cmd') action = message_handler.process_message(select_agent) assert action.suggested_command == NOOP_COMMAND assert action.origin_command == select_agent.command assert action.execute assert message_handler.agent_datasource.get_current_plugin_name( select_agent.user_name) == ['nlc2cmd']
def test_should_return_an_error_when_selected_is_empty(mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mocker.patch.object(AgentDatasource, 'all_plugins', return_value=ALL_PLUGINS, autospec=True) mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["nlc2cmd"]), autospec=True) mocker.patch.object(ConfigStorage, 'store_config', return_value=None, autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) select_agent = clai_select_state('') action = message_handler.process_message(select_agent) assert action.suggested_command == NOOP_COMMAND assert action.origin_command == select_agent.command assert action.execute assert action.description == create_error_select('').description assert message_handler.agent_datasource.get_current_plugin_name( select_agent.user_name) == ['nlc2cmd']
def test_should_return_the_list_without_any_selected_plugin_when_default_doesnt_exist( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mocker.patch.object(AgentDatasource, 'all_plugins', return_value=ALL_PLUGINS, autospec=True) mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig( default="", default_orchestrator="max_orchestrator"), autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) action = message_handler.process_message(clai_plugins_state()) assert action.suggested_command == NOOP_COMMAND assert action.origin_command == 'clai skills' assert action.execute assert action.description == expected_description(ALL_PLUGINS, '')
def test_should_return_the_install_command_when_the_new_plugin_is_not_installed_yet( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["nlc2cmd"]), autospec=True) mocker.patch.object(ConfigStorage, 'store_config', return_value=None, autospec=True) mocker.patch.object(AgentDatasource, 'all_plugins', return_value=ALL_PLUGINS, autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) select_agent = clai_select_state('nlc2cmd') action = message_handler.process_message(select_agent) assert action.suggested_command == "$CLAI_PATH/fileExist.sh nlc2cmd" assert action.origin_command == select_agent.command assert message_handler.agent_datasource.get_current_plugin_name( select_agent.user_name) == ['nlc2cmd']
def install_orchestration(bin_path): agent_datasource = AgentDatasource(config_storage=ConfigStorage( alternate_path=f'{bin_path}/configPlugins.json')) orchestrator_provider = OrchestratorProvider(agent_datasource) all_orchestrators = orchestrator_provider.all_orchestrator() for orchestrator in all_orchestrators: install_orchestration_dependencies(bin_path, orchestrator.name)
def test_should_return_the_list_of_plugins_with_default_selected_when_the_server_received_plugins_no_selected( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mocker.patch.object(ConfigStorage, 'read_config', return_value=NO_SELECTED, autospec=True) mocker.patch.object(AgentDatasource, 'all_plugins', return_value=ALL_PLUGINS, autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) action = message_handler.process_message(clai_plugins_state()) assert action.suggested_command == NOOP_COMMAND assert action.origin_command == 'clai skills' assert action.execute assert action.description == expected_description(ALL_PLUGINS, NO_SELECTED.default)
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")
def test_should_return_the_command_to_get_the_plugin_from_url_and_move_it_into_plugin_folder_server( ): agent_datasource = AgentDatasource() clai_install_command_runner = ClaiInstallCommandRunner(agent_datasource) command_to_execute = clai_install_command_runner.execute( CLAI_INSTALL_STATE_URL) dir_to_install = CLAI_INSTALL_STATE_URL.command.replace( f'{"clai install"}', '').strip() assert command_to_execute.suggested_command == f'cd $CLAI_PATH/clai/server/plugins && curl -O {dir_to_install}'
def __init__(self, on_skills_ready, on_server_running, on_server_stopped): self.command_id = 0 self.server_running = False self.server_process = None self.current_active_skill = '' self.agent_datasource = AgentDatasource() self.on_skills_ready = on_skills_ready self.on_server_running = on_server_running self.on_server_stopped = on_server_stopped
def __init__(self, on_skills_ready, on_server_running, on_server_stopped): self.server_running = False self.server_process = None self.current_active_skill = '' self.agent_datasource = AgentDatasource() self.on_skills_ready = on_skills_ready self.on_server_running = on_server_running self.on_server_stopped = on_server_stopped self.my_clai: Optional[Container] = None
def __init__(self, server_status_datasource: ServerStatusDatasource = current_status_datasource, connector: ServerConnector = SocketServerConnector(current_status_datasource), remote_storage: ActionRemoteStorage = action_remote_storage, agent_datasource=AgentDatasource(), stats=stats_tracker ): self.connector = connector self.agent_datasource = agent_datasource self.server_status_datasource = server_status_datasource self.message_handler = MessageHandler(server_status_datasource, agent_datasource=agent_datasource) self.remote_storage = remote_storage self.stats_tracker = stats
def test_should_return_the_command_to_get_the_plugin_from_route_and_move_it_into_plugin_folder_server( mocker): mock_exisit = mocker.patch('os.path.exists') mock_exisit.return_value = True mock_exisit = mocker.patch('os.path.isdir') mock_exisit.return_value = True agent_datasource = AgentDatasource() clai_install_command_runner = ClaiInstallCommandRunner(agent_datasource) command_to_execute = clai_install_command_runner.execute( CLAI_INSTALL_STATE_FOLDER) dir_to_install = CLAI_INSTALL_STATE_FOLDER.command.replace( f'{"clai install"}', '').strip() assert command_to_execute.suggested_command == f'cp -R {dir_to_install} $CLAI_PATH/clai/server/plugins'
def test_should_return_the_message_error_when_the_folder_doesnt_exist(mocker): mock_exisit = mocker.patch('os.path.exists') mock_exisit.return_value = False mock_exisit = mocker.patch('os.path.isdir') mock_exisit.return_value = False agent_datasource = AgentDatasource() clai_install_command_runner = ClaiInstallCommandRunner(agent_datasource) command_to_execute = clai_install_command_runner.execute( CLAI_INSTALL_STATE_FOLDER) dir_to_install = CLAI_INSTALL_STATE_FOLDER.command.replace( f'{"clai install"}', '').strip() assert command_to_execute.suggested_command == ':' assert command_to_execute.description == create_error_install( dir_to_install).description
def install_plugins(install_path, user_install): agent_datasource = AgentDatasource(config_storage=ConfigStorage( alternate_path=f'{install_path}/configPlugins.json')) plugins = agent_datasource.all_plugins() for plugin in plugins: default = z_default = False if PLATFORM in ('zos', 'os390'): z_default = plugin.z_default else: default = plugin.default if default or z_default: installed = install_plugins_dependencies(install_path, plugin.pkg_name, user_install) if installed: agent_datasource.mark_plugins_as_installed(plugin.name, None) return agent_datasource
def test_should_return_valid_action_if_the_select_agent_return_none(mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mock_agent.execute.return_value = None mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["demo_agent"]), autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) action = message_handler.process_message(command_state()) assert action.suggested_command is action.origin_command assert action.origin_command == command_state().command assert not action.execute assert not action.description
def test_should_return_empty_action_from_selected_agent_when_the_command_goes_to_the_agent_and_not_confidence( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) action_to_execute = Action(suggested_command="command", confidence=0.1) mock_agent.execute.return_value = action_to_execute mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["demo_agent"]), autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) action = message_handler.process_message(command_state()) assert action.suggested_command is action.origin_command assert action.origin_command == command_state().command assert not action.execute assert not action.description
def test_should_return_the_suggestion_from_agent_ignoring_confidence_if_is_name_agent_command( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) action_to_execute = Action(suggested_command="command", confidence=0.0) mock_agent.execute.return_value = action_to_execute mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["demo_agent"]), autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) action = message_handler.process_message(COMMAND_NAME_AGENT_STATE) assert action.suggested_command == action_to_execute.suggested_command assert action.origin_command == command_state().command assert not action.execute assert not action.description
def execute(args): unassisted = args.unassisted no_skills = args.no_skills demo_mode = args.demo_mode bin_path = os.path.join(args.destdir, 'bin') code_path = os.path.join(bin_path, 'clai') cli_path = os.path.join(bin_path, 'bin') temp_path = '~/tmp' mkdir(f"{temp_path}/") create_rc_file_if_not_exist(args.system) if clai_installed(get_setup_file()): print_error('CLAI is already in you system. You should execute uninstall first') sys.exit(1) if not binary_installed(bin_path): mkdir(bin_path) mkdir(code_path) cp_tree('./clai', code_path) cp_tree('./bin', cli_path) copy('./scripts/clai.sh', bin_path) copy('./scripts/saveFilesChanges.sh', bin_path) copy('./configPlugins.json', bin_path) copy('./usersInstalled.json', bin_path) copy('./anonymize.json', bin_path) copy('./scripts/fileExist.sh', bin_path) os.system(f'chmod 775 {bin_path}/saveFilesChanges.sh') os.system(f'chmod 775 {bin_path}/fileExist.sh') os.system(f'chmod -R 777 {code_path}/server/plugins') os.system(f'chmod 777 {bin_path}/clai.sh') os.system(f'chmod 666 {bin_path}/configPlugins.json') os.system(f'chmod 666 {bin_path}/anonymize.json') os.system(f'chmod -R 777 {bin_path}') cli_executable(cli_path) download_file(URL_BASH_PREEXEC, filename='%s/%s' % (temp_path, BASH_PREEXEC)) copy('%s/%s' % (temp_path, BASH_PREEXEC), bin_path) register_the_user(bin_path, args.system) append_setup_to_file(get_setup_file(), bin_path) register_file(args.system) if not no_skills: agent_datasource = AgentDatasource( config_storage=ConfigStorage(alternate_path=f'{bin_path}/configPlugins.json')) plugins = agent_datasource.all_plugins() for plugin in plugins: if plugin.default: installed = install_plugins_dependencies(bin_path, plugin.pkg_name) if installed: agent_datasource.mark_plugins_as_installed(plugin.name, None) save_report_info(unassisted, agent_datasource, bin_path, demo_mode) remove(f"{temp_path}/") os.system(f'chmod -R 777 /var/tmp') print_complete("CLAI has been installed correctly, you need restart your shell.")