def validate_robo_session(): settings = ToolSettings() current_environment = settings.get_current_environment() if not current_environment: raise click.UsageError( "No environment is currently activated.\nRun 'roboai environment activate <env-name>' to activate" "an environment.") api_key = current_environment.api_key robo_client = get_robo_client(environment=current_environment) # an API key must be set if not api_key: raise click.UsageError( "You are not authenticated, please login using the login command.") # if a session isn't started yet, starts a new one token = current_environment.api_auth_token if not token: try: robo_client.oauth.authenticate(api_key) except InvalidCredentialsError: raise click.UsageError( "Your current credentials or endpoint configuration are not valid." ) else: # checks if the current token is valid try: robo_client.oauth.get_token_info(token) except InvalidTokenError: raise click.UsageError( "Your current credentials or endpoint configuration are not valid." )
def does_the_runtime_exist(bot_uuid: str) -> bool: settings = ToolSettings() current_environment = settings.get_current_environment() robo = get_robo_client(current_environment) try: robo.assistants.runtimes.get(bot_uuid) return True except NotFoundError: return False
def wait_for_runtime_status(bot_uuid: str, status: AssistantRuntimeStatus): settings = ToolSettings() current_environment = settings.get_current_environment() robo = get_robo_client(current_environment) polling.poll( lambda: robo.assistants.runtimes.get(bot_uuid).content.status == status, step=5, poll_forever=True, )
def get_runtime_logs(bot_uuid: str) -> str: settings = ToolSettings() current_environment = settings.get_current_environment() robo = get_robo_client(current_environment) if not does_the_runtime_exist(bot_uuid): raise click.UsageError("The bot runtime does not exist.") logs = robo.assistants.runtimes.get_logs(bot_uuid) lines = logs.content.lines return "\n".join(lines)
def validate_bot(bot_uuid: str): settings = ToolSettings() current_environment = settings.get_current_environment() robo = get_robo_client(current_environment) try: with loading_indicator("Validating bot..."): bot = robo.assistants.get_assistant(bot_uuid) bot_type = bot.content.assistantService if bot_type != SUPPORTED_BOT_TYPE: raise click.UsageError( "The selected bot engine is invalid: {0}".format(bot_type)) except NotFoundError: raise click.UsageError("The bot UUID is not valid.")
def get_bots(page: int) -> AssistantListResponse: """ Retrieves the list of bots from the ROBO.AI platform. Args: page (int): current page. Returns: AssistantListResponse: list of bots available in the current page. """ settings = ToolSettings() current_environment = settings.get_current_environment() robo = get_robo_client(current_environment) return robo.assistants.get_list(page)
def stop_runtime(bot_uuid: str): settings = ToolSettings() current_environment = settings.get_current_environment() robo = get_robo_client(current_environment) runtime = robo.assistants.runtimes.get(bot_uuid) assert_status_transition(runtime.content.status, AssistantRuntimeStatus.STOPPED, "stopped") with loading_indicator("Requesting bot runtime stop..."): robo.assistants.runtimes.stop(bot_uuid) with loading_indicator("Waiting for the bot runtime to stop..."): wait_for_runtime_status(bot_uuid, AssistantRuntimeStatus.STOPPED)
def command(action: tuple, env_name: str, base_url: str, username: str, password: str): """ Define the ROBO.AI platform API endpoint to use. Args: action: which action to be applied. Possible actions: - create: create an environment - remove: remove an environment - activate: activate an environment - which: indicate which environment is activated env_name: name of the environment to create, remove, activate base-url (str): Endpoint url of the environment to be created username: basic authentication username to use password: basic authentication password to use """ settings = ToolSettings() if action == 'create': create_environment(settings, env_name, base_url, username, password) elif action == 'activate': activate_environment(settings, env_name) elif action == 'remove': remove_environment(settings, env_name) elif action == 'which': which_environment(settings) elif action == 'list': list_environments(settings)
def create_environment(settings, new_env_name, base_url, username, password): new_env_name = new_env_name[0] verify_endpoint_validity(base_url, username, password) settings = ToolSettings() if username and password: env_auth = EnvironmentAuth(username, password) else: env_auth = None environment = Environment(environment_name=new_env_name, base_url=base_url, api_auth_setting=env_auth) settings.update_environment(environment) print_success( f"The environment was successfully created.\nYou can now activate it by running " f"'roboai environment activate {new_env_name}'.\n")
def start_runtime(bot_uuid: str): settings = ToolSettings() current_environment = settings.get_current_environment() robo = get_robo_client(current_environment) if not does_the_runtime_exist(bot_uuid): raise click.UsageError("The bot runtime does not exist.") runtime = robo.assistants.runtimes.get(bot_uuid) assert_status_transition(runtime.content.status, AssistantRuntimeStatus.RUNNING, "started") with loading_indicator("Requesting bot runtime start..."): robo.assistants.runtimes.start(bot_uuid) with loading_indicator("Waiting for the bot runtime to start..."): wait_for_runtime_status(bot_uuid, AssistantRuntimeStatus.RUNNING)
def remove_runtime(bot_uuid: str): settings = ToolSettings() current_environment = settings.get_current_environment() robo = get_robo_client(current_environment) if not does_the_runtime_exist(bot_uuid): print_warning("The bot runtime does not exist.") return runtime = robo.assistants.runtimes.get(bot_uuid) assert_status_transition(runtime.content.status, AssistantRuntimeStatus.REMOVED, "removed") with loading_indicator("Requesting bot runtime deletion..."): robo.assistants.runtimes.remove(bot_uuid) with loading_indicator("Waiting for the bot runtime to be removed..."): wait_for_runtime_non_existence(bot_uuid)
def command(): """ Close the current session in the ROBO.AI platform. """ settings = ToolSettings() current_environment = settings.get_current_environment() current_environment.api_auth_token = None current_environment.api_key = None settings.update_environment(current_environment) settings.set_current_environment(None) print_success('Your session was successfully terminated.\n')
def create_runtime(bot_uuid: str, package_file: str, base_version: str): settings = ToolSettings() current_environment = settings.get_current_environment() robo = get_robo_client(current_environment) total_file_size = os.path.getsize(package_file) with click.progressbar(length=total_file_size, label="Uploading package", show_eta=False) as bar: robo.assistants.runtimes.create(bot_uuid, package_file, base_version, progress_callback=lambda bytes_read: bar.update(bytes_read - bar.pos)) with loading_indicator( "Waiting for the bot runtime to be ready (it may take several minutes)..." ): wait_for_runtime_status(bot_uuid, AssistantRuntimeStatus.CREATED) with loading_indicator("Waiting for the bot runtime to start..."): robo.assistants.runtimes.start(bot_uuid) wait_for_runtime_status(bot_uuid, AssistantRuntimeStatus.RUNNING)
def update_runtime(bot_uuid: str, package_file: str, base_version: str): settings = ToolSettings() current_environment = settings.get_current_environment() robo = get_robo_client(current_environment) runtime = robo.assistants.runtimes.get(bot_uuid) if runtime.content.status == AssistantRuntimeStatus.RUNNING: with loading_indicator("The bot runtime is running, stopping..."): robo.assistants.runtimes.stop(bot_uuid) wait_for_runtime_status(bot_uuid, AssistantRuntimeStatus.STOPPED) total_file_size = os.path.getsize(package_file) with click.progressbar(length=total_file_size, label="Uploading package", show_eta=False) as bar: robo.assistants.runtimes.update(bot_uuid, package_file, base_version, progress_callback=lambda bytes_read: bar.update(bytes_read - bar.pos)) with loading_indicator("Waiting for the bot runtime to start..."): wait_for_runtime_status(bot_uuid, AssistantRuntimeStatus.RUNNING)
def command(api_key: str): """ Initialize a new session using a ROBO.AI API key, Args: api_key (str): API key """ settings = ToolSettings() current_environment = settings.get_current_environment( ) # this should be an object now robo = get_robo_client(current_environment) try: with loading_indicator('Authenticating...'): tokens = robo.oauth.authenticate(api_key) current_environment.api_key = api_key current_environment.api_auth_token = tokens.access_token settings.update_environment(current_environment) print_success('Successfully authenticated!\n') except InvalidCredentialsError: print_error('The API key is invalid.\n')
def get_bot_runtime(bot_uuid: str): settings = ToolSettings() current_environment = settings.get_current_environment() robo = get_robo_client(current_environment) return robo.assistants.runtimes.get(bot_uuid).content