def _crete_agent_config(ctx: Context, agent_name: str, set_author: str) -> AgentConfig: """ Create agent config. :param ctx: context object. :param agent_name: agent name. :param set_author: author name to set. :return: AgentConfig object. """ agent_config = AgentConfig( agent_name=agent_name, aea_version=aea.__version__, author=set_author, version=DEFAULT_VERSION, license_=DEFAULT_LICENSE, registry_path=os.path.join("..", DEFAULT_REGISTRY_PATH), description="", ) agent_config.default_connection = str(DEFAULT_CONNECTION) agent_config.default_ledger = DEFAULT_LEDGER with open(os.path.join(agent_name, DEFAULT_AEA_CONFIG_FILE), "w") as config_file: ctx.agent_loader.dump(agent_config, config_file) return agent_config
def _create_aea(click_context, agent_name: str, set_author: str, local: bool) -> None: ctx = cast(Context, click_context.obj) path = Path(agent_name) ctx.clean_paths.append(str(path)) # we have already checked that the directory does not exist. path.mkdir(exist_ok=False) try: # set up packages directories. _setup_package_folder(Path(agent_name, "protocols")) _setup_package_folder(Path(agent_name, "contracts")) _setup_package_folder(Path(agent_name, "connections")) _setup_package_folder(Path(agent_name, "skills")) # set up a vendor directory Path(agent_name, "vendor").mkdir(exist_ok=False) Path(agent_name, "vendor", "__init__.py").touch(exist_ok=False) # create a config file inside it click.echo("Creating config file {}".format(DEFAULT_AEA_CONFIG_FILE)) config_file = open(os.path.join(agent_name, DEFAULT_AEA_CONFIG_FILE), "w") agent_config = AgentConfig( agent_name=agent_name, aea_version=aea.__version__, author=set_author, version=DEFAULT_VERSION, license=DEFAULT_LICENSE, registry_path=os.path.join("..", DEFAULT_REGISTRY_PATH), description="", ) agent_config.default_connection = DEFAULT_CONNECTION # type: ignore agent_config.default_ledger = DEFAULT_LEDGER ctx.agent_loader.dump(agent_config, config_file) # next commands must be done from the agent's directory -> overwrite ctx.cwd ctx.agent_config = agent_config ctx.cwd = agent_config.agent_name click.echo("Adding default packages ...") if local: ctx.set_config("is_local", True) _add_item(click_context, "connection", DEFAULT_CONNECTION) _add_item(click_context, "skill", DEFAULT_SKILL) except Exception as e: raise click.ClickException(str(e))
def create(click_context, agent_name): """Create an agent.""" ctx = cast(Context, click_context.obj) path = Path(agent_name) logger.info("Initializing AEA project '{}'".format(agent_name)) logger.info("Creating project directory '/{}'".format(agent_name)) # create the agent's directory try: path.mkdir(exist_ok=False) # create a config file inside it logger.info("Creating config file {}".format(DEFAULT_AEA_CONFIG_FILE)) config_file = open(os.path.join(agent_name, DEFAULT_AEA_CONFIG_FILE), "w") agent_config = AgentConfig(agent_name=agent_name, aea_version=aea.__version__, authors="", version="v1", license="", url="", registry_path=DEFAULT_REGISTRY_PATH, description="") agent_config.default_connection = DEFAULT_CONNECTION ctx.agent_loader.dump(agent_config, config_file) # next commands must be done from the agent's directory -> overwrite ctx.cwd ctx.agent_config = agent_config ctx.cwd = agent_config.agent_name logger.info("Default connections:") click_context.invoke(connection, connection_name=DEFAULT_CONNECTION) logger.info("Default skills:") click_context.invoke(skill, skill_name=DEFAULT_SKILL) except OSError: logger.error("Directory already exist. Aborting...") sys.exit(1) except ValidationError as e: logger.error(str(e)) shutil.rmtree(agent_name, ignore_errors=True) sys.exit(1) except Exception as e: logger.exception(e) shutil.rmtree(agent_name, ignore_errors=True) sys.exit(1)
def test_agent_config_to_json_with_optional_configurations(): """Test agent config to json with optional configurations.""" agent_config = AgentConfig( "name", "author", timeout=0.1, execution_timeout=1.0, max_reactions=100, decision_maker_handler=dict(dotted_path="", file_path=""), skill_exception_policy="propagate", default_routing={"author/name:0.1.0": "author/name:0.1.0"}, loop_mode="sync", runtime_mode="async", ) agent_config.default_connection = "author/name:0.1.0" agent_config.default_ledger = DEFAULT_LEDGER agent_config.json
def create(click_context, agent_name): """Create an agent.""" ctx = cast(Context, click_context.obj) path = Path(agent_name) logger.info("Creating agent's directory in '{}'".format(path)) # create the agent's directory try: path.mkdir(exist_ok=False) # create a config file inside it config_file = open(os.path.join(agent_name, DEFAULT_AEA_CONFIG_FILE), "w") agent_config = AgentConfig(agent_name=agent_name, aea_version=aea.__version__, authors="", version="v1", license="", url="", registry_path="../packages", private_key_pem_path="") agent_config.default_connection = DEFAULT_CONNECTION ctx.agent_loader.dump(agent_config, config_file) logger.info("Created config file {}".format(DEFAULT_AEA_CONFIG_FILE)) # next commands must be done from the agent's directory -> overwrite ctx.cwd ctx.agent_config = agent_config ctx.cwd = agent_config.agent_name logger.info("Adding default connection '{}' to the agent...".format(DEFAULT_CONNECTION)) click_context.invoke(connection, connection_name=DEFAULT_CONNECTION) logger.info("Adding default skill '{}' to the agent...".format(DEFAULT_SKILL)) click_context.invoke(skill, skill_name=DEFAULT_SKILL) except OSError: logger.error("Directory already exist. Aborting...") exit(-1) except ValidationError as e: logger.error(str(e)) shutil.rmtree(agent_name, ignore_errors=True) exit(-1) except Exception as e: logger.exception(e) shutil.rmtree(agent_name, ignore_errors=True) exit(-1)
def test_agent_config_to_json_with_optional_configurations(): """Test agent config to json with optional configurations.""" agent_config = AgentConfig( "name", "author", period=0.1, execution_timeout=1.0, max_reactions=100, decision_maker_handler=dict(dotted_path="", file_path=""), error_handler=dict(dotted_path="", file_path=""), skill_exception_policy="propagate", connection_exception_policy="propagate", default_routing={"author/name:0.1.0": "author/name:0.1.0"}, currency_denominations={"fetchai": "fet"}, loop_mode="sync", runtime_mode="async", storage_uri="some_uri_to_storage", ) agent_config.default_connection = "author/name:0.1.0" agent_config.default_ledger = DEFAULT_LEDGER agent_config.json assert agent_config.package_id == PackageId.from_uri_path( "agent/author/name/0.1.0")
def test_default_connection(self): """Test case for default_connection setter positive result.""" agent_config = AgentConfig(agent_name="my_agent", author="fetchai") agent_config.default_connection = None agent_config.default_connection = 1 agent_config.public_id
def create(click_context, agent_name, author, local): """Create an agent.""" try: _check_is_parent_folders_are_aea_projects_recursively() except Exception: logger.error( "The current folder is already an AEA project. Please move to the parent folder." ) sys.exit(1) if author is not None: if local: do_init(author, False, False) else: raise click.ClickException( "Author is not set up. Please use 'aea init' to initialize.") config = _get_or_create_cli_config() set_author = config.get(AUTHOR, None) if set_author is None: click.echo( "The AEA configurations are not initialized. Uses `aea init` before continuing or provide optional argument `--author`." ) sys.exit(1) ctx = cast(Context, click_context.obj) path = Path(agent_name) click.echo("Initializing AEA project '{}'".format(agent_name)) click.echo("Creating project directory './{}'".format(agent_name)) # create the agent's directory try: path.mkdir(exist_ok=False) # set up packages directories. _setup_package_folder(Path(agent_name, "protocols")) _setup_package_folder(Path(agent_name, "contracts")) _setup_package_folder(Path(agent_name, "connections")) _setup_package_folder(Path(agent_name, "skills")) # set up a vendor directory Path(agent_name, "vendor").mkdir(exist_ok=False) Path(agent_name, "vendor", "__init__.py").touch(exist_ok=False) # create a config file inside it click.echo("Creating config file {}".format(DEFAULT_AEA_CONFIG_FILE)) config_file = open(os.path.join(agent_name, DEFAULT_AEA_CONFIG_FILE), "w") agent_config = AgentConfig( agent_name=agent_name, aea_version=aea.__version__, author=set_author, version=DEFAULT_VERSION, license=DEFAULT_LICENSE, registry_path=os.path.join("..", DEFAULT_REGISTRY_PATH), description="", ) agent_config.default_connection = DEFAULT_CONNECTION agent_config.default_ledger = DEFAULT_LEDGER ctx.agent_loader.dump(agent_config, config_file) # next commands must be done from the agent's directory -> overwrite ctx.cwd ctx.agent_config = agent_config ctx.cwd = agent_config.agent_name click.echo("Adding default packages ...") if local: ctx.set_config("is_local", True) _add_item(click_context, "connection", DEFAULT_CONNECTION) _add_item(click_context, "skill", DEFAULT_SKILL) except OSError: logger.error("Directory already exist. Aborting...") sys.exit(1) except ValidationError as e: logger.error(str(e)) shutil.rmtree(agent_name, ignore_errors=True) sys.exit(1) except Exception as e: logger.exception(e) shutil.rmtree(agent_name, ignore_errors=True) sys.exit(1)
def _( arg: AgentConfig, replacements: Dict[ComponentType, Dict[PublicId, PublicId]], ) -> None: """ Replace references in agent configuration. It breaks down in: 1) replace public ids in 'protocols', 'connections', 'contracts' and 'skills'; 2) replace public ids in default routing; 3) replace public id of default connection; 4) replace custom component configurations. :param arg: the agent configuration. :param replacements: the replacement mapping. :return: None """ _replace_component_id( arg, { ComponentType.PROTOCOL, ComponentType.CONNECTION, ComponentType.CONTRACT, ComponentType.SKILL, }, replacements, ) # update default routing protocol_replacements = replacements.get(ComponentType.PROTOCOL, {}) connection_replacements = replacements.get(ComponentType.CONNECTION, {}) for protocol_id, connection_id in list(arg.default_routing.items()): # update protocol (if replacements provides it) new_protocol_id = protocol_replacements.get(protocol_id, protocol_id) old_value = arg.default_routing.pop(protocol_id) arg.default_routing[new_protocol_id] = old_value # in case needs to be used below protocol_id = new_protocol_id # update connection (if replacements provides it) new_connection_id = connection_replacements.get( connection_id, connection_id) arg.default_routing[protocol_id] = new_connection_id # update default connection if arg.default_connection is not None: default_connection_public_id = arg.default_connection new_default_connection_public_id = replacements.get( ComponentType.CONNECTION, {}).get(default_connection_public_id, default_connection_public_id) arg.default_connection = new_default_connection_public_id for component_id in set(arg.component_configurations.keys()): replacements_by_type = replacements.get(component_id.component_type, {}) if component_id.public_id in replacements_by_type: new_component_id = ComponentId( component_id.component_type, replacements_by_type[component_id.public_id], ) old_value = arg.component_configurations.pop(component_id) arg.component_configurations[new_component_id] = old_value
def test_default_connection(self): """Test case for default_connection setter positive result.""" agent_config = AgentConfig() agent_config.default_connection = None agent_config.default_connection = 1 agent_config.public_id