def _run_interaction_channel(): loader = ConfigLoader.from_configuration_type(PackageType.AGENT) agent_configuration = loader.load(Path(DEFAULT_AEA_CONFIG_FILE).open()) agent_name = agent_configuration.name identity_stub = Identity(agent_name + "_interact", "interact") _load_packages(identity_stub) # load agent configuration file from packages.fetchai.connections.stub.connection import ( # noqa: F811 # pylint: disable=import-outside-toplevel DEFAULT_INPUT_FILE_NAME, DEFAULT_OUTPUT_FILE_NAME, StubConnection, ) from packages.fetchai.protocols.default.dialogues import ( # noqa: F811 # pylint: disable=import-outside-toplevel DefaultDialogue, DefaultDialogues, ) from packages.fetchai.protocols.default.message import ( # noqa: F811 # pylint: disable=import-outside-toplevel DefaultMessage, ) # load stub connection configuration = ConnectionConfig( input_file=DEFAULT_OUTPUT_FILE_NAME, output_file=DEFAULT_INPUT_FILE_NAME, connection_id=StubConnection.connection_id, ) stub_connection = StubConnection( configuration=configuration, identity=identity_stub ) multiplexer = Multiplexer([stub_connection]) inbox = InBox(multiplexer) outbox = OutBox(multiplexer) def role_from_first_message( # pylint: disable=unused-argument message: Message, receiver_address: Address ) -> BaseDialogue.Role: """Infer the role of the agent from an incoming/outgoing first message :param message: an incoming/outgoing first message :param receiver_address: the address of the receiving agent :return: The role of the agent """ return DefaultDialogue.Role.AGENT dialogues = DefaultDialogues(identity_stub.name, role_from_first_message) try: multiplexer.connect() while True: # pragma: no cover _process_envelopes(agent_name, inbox, outbox, dialogues, DefaultMessage) except KeyboardInterrupt: click.echo("Interaction interrupted!") except BaseException as e: # pylint: disable=broad-except # pragma: no cover click.echo(e) finally: multiplexer.disconnect()
def __init__(self, **kwargs: Any) -> None: """ Initialize dialogues. :return: None """ Model.__init__(self, **kwargs) def role_from_first_message( # pylint: disable=unused-argument message: Message, receiver_address: Address) -> Dialogue.Role: """Infer the role of the agent from an incoming/outgoing first message :param message: an incoming/outgoing first message :param receiver_address: the address of the receiving agent :return: The role of the agent """ return DefaultDialogue.Role.AGENT BaseDefaultDialogues.__init__( self, self_address=self.context.agent_address, role_from_first_message=role_from_first_message, )
def setup(self): """Set up the test case.""" self.add_item("skill", "fetchai/echo:latest", local=True) pkey_file = os.path.join(self._get_cwd(), "privkey") self.generate_private_key("fetchai", pkey_file) self.add_private_key("fetchai", pkey_file, False) self.add_private_key("fetchai", pkey_file, True) def role_from_first_message( # pylint: disable=unused-argument message: Message, receiver_address: Address ) -> Dialogue.Role: """Infer the role of the agent from an incoming/outgoing first message :param message: an incoming/outgoing first message :param receiver_address: the address of the receiving agent :return: The role of the agent """ return DefaultDialogue.Role.AGENT self.dialogues = DefaultDialogues( self_address="another_agent", role_from_first_message=role_from_first_message, )
def test_send_receive_envelope(self): """Run the echo skill sequence.""" self.add_item("connection", str(STUB_CONNECTION_ID)) self.add_item("skill", str(ECHO_SKILL_PUBLIC_ID)) process = self.run_agent() is_running = self.is_running(process) assert is_running, "AEA not running within timeout!" # add sending and receiving envelope from input/output files sender = "sender" def role_from_first_message( # pylint: disable=unused-argument message: Message, receiver_address: str) -> Dialogue.Role: return DefaultDialogue.Role.AGENT default_dialogues = DefaultDialogues(sender, role_from_first_message) message_content = b"hello" message = DefaultMessage( performative=DefaultMessage.Performative.BYTES, dialogue_reference=default_dialogues. new_self_initiated_dialogue_reference(), content=message_content, ) sent_envelope = Envelope( to=self.agent_name, sender=sender, protocol_specification_id=message.protocol_specification_id, message=message, ) self.send_envelope_to_agent(sent_envelope, self.agent_name) time.sleep(2.0) received_envelope = self.read_envelope_from_agent(self.agent_name) received_message = DefaultMessage.serializer.decode( received_envelope.message) assert sent_envelope.message.content == received_message.content
class TestDialogueModelSaveLoad(AEATestCaseEmpty): """Test dialogues sved and loaded on agent restart.""" def setup(self): """Set up the test case.""" self.add_item("skill", "fetchai/echo:latest", local=True) pkey_file = os.path.join(self._get_cwd(), "privkey") self.generate_private_key("fetchai", pkey_file) self.add_private_key("fetchai", pkey_file, False) self.add_private_key("fetchai", pkey_file, True) def role_from_first_message( # pylint: disable=unused-argument message: Message, receiver_address: Address ) -> Dialogue.Role: """Infer the role of the agent from an incoming/outgoing first message :param message: an incoming/outgoing first message :param receiver_address: the address of the receiving agent :return: The role of the agent """ return DefaultDialogue.Role.AGENT self.dialogues = DefaultDialogues( self_address="another_agent", role_from_first_message=role_from_first_message, ) def _build_aea(self) -> AEA: """Build an AEA.""" builder = AEABuilder.from_aea_project(self._get_cwd()) builder.set_storage_uri("sqlite://some_file.db") aea = builder.build() aea.runtime._threaded = True return aea def test_dialogues_dumped_and_restored_properly(self): """Test dialogues restored during restart of agent.""" aea = self._build_aea() aea.runtime.start() try: wait_for_condition(lambda: aea.is_running, timeout=10) echo_skill = aea.resources.get_skill(PUBLIC_ID) assert ( not echo_skill.skill_context.default_dialogues._dialogues_storage._dialogues_by_dialogue_label ) msg, dialogue = self.dialogues.create( aea.name, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) envelope = Envelope(to=msg.to, sender=msg.sender, message=msg,) aea.runtime.multiplexer.in_queue.put(envelope) dialogue_storage: PersistDialoguesStorage = echo_skill.skill_context.default_dialogues._dialogues_storage wait_for_condition( lambda: _storage_all_dialogues_labels(dialogue_storage), timeout=3, ) dialogues_for_check = _storage_all_dialogues_labels(dialogue_storage) finally: aea.runtime.stop() aea.runtime.wait_completed(sync=True, timeout=10) aea = self._build_aea() aea.runtime.start() try: wait_for_condition(lambda: aea.is_running, timeout=10) echo_skill = aea.resources.get_skill(PUBLIC_ID) dialogue_storage: PersistDialoguesStorage = echo_skill.skill_context.default_dialogues._dialogues_storage wait_for_condition( lambda: _storage_all_dialogues_labels(dialogue_storage), timeout=3, ) assert ( _storage_all_dialogues_labels(dialogue_storage) == dialogues_for_check ) finally: aea.runtime.stop() aea.runtime.wait_completed(sync=True, timeout=10)