def search(ctx: Context, registry): """Search for components in the registry. If called from an agent directory, it will check E.g. aea search connections aea search --registry skills """ if registry: ctx.set_config("is_registry", True) else: # if we are in an agent directory, try to load the configuration file. # otherwise, use the default path (i.e. 'packages/' in the current directory.) try: try_to_load_agent_config(ctx, is_exit_on_except=False) # path = Path(DEFAULT_AEA_CONFIG_FILE) # fp = open(str(path), mode="r", encoding="utf-8") # agent_config = ctx.agent_loader.load(fp) registry_directory = ctx.agent_config.registry_path except Exception: registry_directory = os.path.join(ctx.cwd, DEFAULT_REGISTRY_PATH) ctx.set_config("registry_directory", registry_directory) logger.debug("Using registry {}".format(registry_directory))
def search(ctx: Context, registry): """Search for components in the registry. E.g. aea search --registry packages/ skills """ if registry: ctx.set_config("is_registry", True) else: registry = os.path.join(ctx.cwd, DEFAULT_REGISTRY_PATH) ctx.set_config("registry", registry) logger.debug("Using registry {}".format(registry))
def delete(ctx: Context, agent_name): """Delete an agent.""" path = Path(agent_name) # check that the target folder is an AEA project. cwd = os.getcwd() try: os.chdir(agent_name) fp = open(DEFAULT_AEA_CONFIG_FILE, mode="r", encoding="utf-8") ctx.agent_config = ctx.agent_loader.load(fp) _try_to_load_agent_config(ctx) except Exception: logger.error("The name provided is not an AEA project.") sys.exit(1) finally: os.chdir(cwd) logger.info("Deleting agent project directory '/{}'...".format(path)) # delete the agent's directory try: shutil.rmtree(path, ignore_errors=False) except OSError: logger.error( "An error occurred while deleting the agent directory. Aborting..." ) sys.exit(1)
def freeze(ctx: Context): """Get the dependencies.""" try_to_load_agent_config(ctx) for dependency_name, dependency_data in sorted( ctx.get_dependencies().items(), key=lambda x: x[0] ): print(dependency_name + dependency_data.get("version", ""))
def _fetch_agent_locally(ctx: Context, public_id: PublicId, click_context) -> None: """ Fetch Agent from local packages. :param ctx: Context :param public_id: public ID of agent to be fetched. :return: None """ packages_path = os.path.basename(DEFAULT_REGISTRY_PATH) source_path = _try_get_item_source_path(packages_path, public_id.author, "agents", public_id.name) target_path = os.path.join(ctx.cwd, public_id.name) if os.path.exists(target_path): raise click.ClickException( 'Item "{}" already exists in target folder.'.format( public_id.name)) copy_tree(source_path, target_path) # add dependencies ctx.cwd = target_path try_to_load_agent_config(ctx) for item_type in ("skill", "connection", "contract", "protocol"): item_type_plural = "{}s".format(item_type) required_items = getattr(ctx.agent_config, item_type_plural) for item_id in required_items: try: _add_item(click_context, item_type, item_id) except SystemExit: continue click.echo("Agent {} successfully fetched.".format(public_id.name))
def install(ctx: Context, requirement: Optional[str]): """Install the dependencies.""" _try_to_load_agent_config(ctx) if requirement: logger.debug( "Installing the dependencies in '{}'...".format(requirement)) dependencies = list( map(lambda x: x.strip(), open(requirement).readlines())) else: logger.debug("Installing all the dependencies...") dependencies = ctx.get_dependencies() for d in dependencies: logger.info("Installing {}...".format(d)) try: subp = subprocess.Popen( [sys.executable, "-m", "pip", "install", d]) subp.wait(30.0) assert subp.returncode == 0 except Exception: logger.error( "An error occurred while installing {}. Stopping...".format(d)) sys.exit(1)
def install(ctx: Context, requirement: Optional[str]): """Install the dependencies.""" try_to_load_agent_config(ctx) if requirement: logger.debug( "Installing the dependencies in '{}'...".format(requirement)) _install_from_requirement(requirement) else: logger.debug("Installing all the dependencies...") dependencies = ctx.get_dependencies() for name, d in dependencies.items(): _install_dependency(name, d)
def run(ctx: Context, connection_name: str): """Run the agent.""" _try_to_load_agent_config(ctx) agent_name = cast(str, ctx.agent_config.agent_name) private_key_pem_path = cast(str, ctx.agent_config.private_key_pem_path) if private_key_pem_path == "": private_key_pem_path = _create_temporary_private_key_pem_path() else: _try_validate_private_key_pem_path(private_key_pem_path) crypto = Crypto(private_key_pem_path=private_key_pem_path) public_key = crypto.public_key connection_name = ctx.agent_config.default_connection if connection_name is None else connection_name _try_to_load_protocols(ctx) try: connection = _setup_connection(connection_name, public_key, ctx) except AEAConfigException as e: logger.error(str(e)) exit(-1) return logger.debug("Installing all the dependencies...") for d in ctx.get_dependencies(): logger.debug("Installing {}...".format(d)) try: subp = subprocess.Popen( [sys.executable, "-m", "pip", "install", d]) subp.wait(30.0) except Exception: logger.error( "An error occurred while installing {}. Stopping...".format(d)) exit(-1) mailbox = MailBox(connection) agent = AEA(agent_name, mailbox, private_key_pem_path=private_key_pem_path, directory=str(Path("."))) try: agent.start() except KeyboardInterrupt: logger.info("Interrupted.") except Exception as e: logger.exception(e) finally: agent.stop()
def fetch_agent(ctx: Context, public_id: PublicId, click_context) -> None: """ Fetch Agent from Registry. :param public_id: str public ID of desirable Agent. :return: None """ author, name, version = public_id.author, public_id.name, public_id.version api_path = "/agents/{}/{}/{}".format(author, name, version) resp = request_api("GET", api_path) file_url = resp["file"] filepath = download_file(file_url, ctx.cwd) extract(filepath, ctx.cwd) target_folder = os.path.join(ctx.cwd, name) ctx.cwd = target_folder try_to_load_agent_config(ctx) click.echo("Fetching dependencies...") for item_type in ("connection", "contract", "skill", "protocol"): item_type_plural = item_type + "s" # initialize fetched agent with empty folders for custom packages custom_items_folder = os.path.join(ctx.cwd, item_type_plural) os.makedirs(custom_items_folder) config = getattr(ctx.agent_config, item_type_plural) for item_public_id in config: try: _add_item(click_context, item_type, item_public_id) except Exception as e: rmtree(target_folder) raise click.ClickException( 'Unable to fetch dependency for agent "{}", aborting. {}'. format(name, e)) click.echo("Dependencies successfully fetched.") click.echo("Agent {} successfully fetched to {}.".format( name, target_folder))
def add(ctx: Context, registry): """Add a resource to the agent.""" if registry: ctx.set_config("is_registry", True) try_to_load_agent_config(ctx)
def cli(click_context, skip_consistency_check: bool) -> None: """Command-line tool for setting up an Autonomous Economic Agent.""" verbosity_option = click_context.meta.pop("verbosity") click_context.obj = Context(cwd=".", verbosity=verbosity_option) click_context.obj.set_config("skip_consistency_check", skip_consistency_check)
def cli(ctx) -> None: """Command-line tool for setting up an Autonomous Economic Agent.""" ctx.obj = Context(cwd=".")
def freeze(ctx: Context): """Get the dependencies.""" _try_to_load_agent_config(ctx) for d in ctx.get_dependencies(): print(d)
def _verify_or_create_private_keys(ctx: Context) -> None: """ Verify or create private keys. :param ctx: Context """ path = Path(DEFAULT_AEA_CONFIG_FILE) agent_loader = ConfigLoader("aea-config_schema.json", AgentConfig) fp = open(str(path), mode="r", encoding="utf-8") aea_conf = agent_loader.load(fp) for identifier, value in aea_conf.private_key_paths.read_all(): if identifier not in SUPPORTED_CRYPTOS: ValueError("Unsupported identifier in private key paths.") default_private_key_config = aea_conf.private_key_paths.read(DEFAULT) if default_private_key_config is None: _create_default_private_key() default_private_key_config = PrivateKeyPathConfig( DEFAULT, DEFAULT_PRIVATE_KEY_FILE) aea_conf.private_key_paths.create(default_private_key_config.ledger, default_private_key_config) else: default_private_key_config = cast(PrivateKeyPathConfig, default_private_key_config) try: _try_validate_private_key_pem_path(default_private_key_config.path) except FileNotFoundError: logger.error("File {} for private key {} not found.".format( repr(default_private_key_config.path), default_private_key_config.ledger)) sys.exit(1) fetchai_private_key_config = aea_conf.private_key_paths.read(FETCHAI) if fetchai_private_key_config is None: _create_fetchai_private_key() fetchai_private_key_config = PrivateKeyPathConfig( FETCHAI, FETCHAI_PRIVATE_KEY_FILE) aea_conf.private_key_paths.create(fetchai_private_key_config.ledger, fetchai_private_key_config) else: fetchai_private_key_config = cast(PrivateKeyPathConfig, fetchai_private_key_config) try: _try_validate_fet_private_key_path(fetchai_private_key_config.path) except FileNotFoundError: logger.error("File {} for private key {} not found.".format( repr(fetchai_private_key_config.path), fetchai_private_key_config.ledger)) sys.exit(1) ethereum_private_key_config = aea_conf.private_key_paths.read(ETHEREUM) if ethereum_private_key_config is None: _create_ethereum_private_key() ethereum_private_key_config = PrivateKeyPathConfig( ETHEREUM, ETHEREUM_PRIVATE_KEY_FILE) aea_conf.private_key_paths.create(ethereum_private_key_config.ledger, ethereum_private_key_config) else: ethereum_private_key_config = cast(PrivateKeyPathConfig, ethereum_private_key_config) try: _try_validate_ethereum_private_key_path( ethereum_private_key_config.path) except FileNotFoundError: logger.error("File {} for private key {} not found.".format( repr(ethereum_private_key_config.path), ethereum_private_key_config.ledger)) sys.exit(1) # update aea config path = Path(DEFAULT_AEA_CONFIG_FILE) fp = open(str(path), mode="w", encoding="utf-8") agent_loader.dump(aea_conf, fp) ctx.agent_config = aea_conf
def push(ctx: Context, registry): """Push item to Registry or save it in local packages.""" try_to_load_agent_config(ctx) ctx.set_config("registry", registry)