|
| 1 | +from crewai_tools import tool |
| 2 | +from dotenv import load_dotenv |
| 3 | +import os |
| 4 | + |
| 5 | +from agent_connect.simple_node import SimpleNode |
| 6 | +import json |
| 7 | + |
| 8 | +load_dotenv() |
| 9 | + |
| 10 | +# An HTTP and WS service will be started in agent-connect |
| 11 | +# It can be an IP address or a domain name |
| 12 | +host_domain = os.getenv("HOST_DOMAIN") |
| 13 | +# Host port, default is 80 |
| 14 | +host_port = os.getenv("HOST_PORT") |
| 15 | +# WS path, default is /ws |
| 16 | +host_ws_path = os.getenv("HOST_WS_PATH") |
| 17 | +# Path to store DID document |
| 18 | +did_document_path = os.getenv("DID_DOCUMENT_PATH") |
| 19 | +# SSL certificate path, if using HTTPS, certificate and key need to be provided |
| 20 | +ssl_cert_path = os.getenv("SSL_CERT_PATH") |
| 21 | +ssl_key_path = os.getenv("SSL_KEY_PATH") |
| 22 | + |
| 23 | + |
| 24 | +def generate_did_info(node: SimpleNode, did_document_path: str) -> None: |
| 25 | + """ |
| 26 | + Generate or load DID information for a node. |
| 27 | + |
| 28 | + Args: |
| 29 | + node: SimpleNode instance |
| 30 | + did_document_path: Path to store/load DID document |
| 31 | + """ |
| 32 | + if os.path.exists(did_document_path): |
| 33 | + print(f"Loading existing DID information from {did_document_path}") |
| 34 | + with open(did_document_path, "r") as f: |
| 35 | + did_info = json.load(f) |
| 36 | + node.set_did_info( |
| 37 | + did_info["private_key_pem"], |
| 38 | + did_info["did"], |
| 39 | + did_info["did_document_json"] |
| 40 | + ) |
| 41 | + else: |
| 42 | + print("Generating new DID information") |
| 43 | + private_key_pem, did, did_document_json = node.generate_did_document() |
| 44 | + node.set_did_info(private_key_pem, did, did_document_json) |
| 45 | + |
| 46 | + # Save DID information |
| 47 | + os.makedirs(os.path.dirname(did_document_path), exist_ok=True) |
| 48 | + with open(did_document_path, "w") as f: |
| 49 | + json.dump({ |
| 50 | + "private_key_pem": private_key_pem, |
| 51 | + "did": did, |
| 52 | + "did_document_json": did_document_json |
| 53 | + }, f, indent=2) |
| 54 | + print(f"DID information saved to {did_document_path}") |
| 55 | + |
| 56 | +agent_connect_simple_node = SimpleNode(host_domain, host_port, host_ws_path) |
| 57 | +generate_did_info(agent_connect_simple_node, did_document_path) |
| 58 | +agent_connect_simple_node.run() |
| 59 | + |
| 60 | +@tool("Send Message to Agent by DID") |
| 61 | +async def send_message(message: str, destination_did: str) -> bool: |
| 62 | + """ |
| 63 | + Send a message through agent-connect node. |
| 64 | + |
| 65 | + Args: |
| 66 | + message: Message content to be sent |
| 67 | + destination_did: DID of the recipient agent |
| 68 | + Returns: |
| 69 | + bool: True if message was sent successfully, False otherwise |
| 70 | + """ |
| 71 | + try: |
| 72 | + await agent_connect_simple_node.send_message(message, destination_did) |
| 73 | + print(f"Successfully sent message: {message}") |
| 74 | + return True |
| 75 | + except Exception as e: |
| 76 | + print(f"Failed to send message: {e}") |
| 77 | + return False |
| 78 | + |
| 79 | +@tool("Receive Message from Agent") |
| 80 | +async def receive_message() -> tuple[str, str]: |
| 81 | + """ |
| 82 | + Receive message from agent-connect node. |
| 83 | + |
| 84 | + Returns: |
| 85 | + tuple[str, str]: Sender DID and received message content, empty string if no message or error occurred |
| 86 | + """ |
| 87 | + try: |
| 88 | + sender_did, message = await agent_connect_simple_node.receive_message() |
| 89 | + if message: |
| 90 | + print(f"Received message from {sender_did}: {message}") |
| 91 | + return sender_did, message |
| 92 | + return "", "" |
| 93 | + except Exception as e: |
| 94 | + print(f"Failed to receive message: {e}") |
| 95 | + return "", "" |
0 commit comments