1- from crewai_tools import tool
2- from dotenv import load_dotenv
31import os
4-
5- from agent_connect .simple_node import SimpleNode
62import json
3+ from agent_connect .simple_node import SimpleNode
74
8- load_dotenv ()
95
106# An HTTP and WS service will be started in agent-connect
117# It can be an IP address or a domain name
12- host_domain = os .getenv ("HOST_DOMAIN " )
8+ host_domain = os .getenv ("AGENT_CONNECT_HOST_DOMAIN " )
139# Host port, default is 80
14- host_port = os .getenv ("HOST_PORT " )
10+ host_port = os .getenv ("AGENT_CONNECT_HOST_PORT " )
1511# WS path, default is /ws
16- host_ws_path = os .getenv ("HOST_WS_PATH " )
12+ host_ws_path = os .getenv ("AGENT_CONNECT_HOST_WS_PATH " )
1713# Path to store DID document
18- did_document_path = os .getenv ("DID_DOCUMENT_PATH " )
14+ did_document_path = os .getenv ("AGENT_CONNECT_DID_DOCUMENT_PATH " )
1915# 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" )
16+ ssl_cert_path = os .getenv ("AGENT_CONNECT_SSL_CERT_PATH" )
17+ ssl_key_path = os .getenv ("AGENT_CONNECT_SSL_KEY_PATH" )
18+
19+ if not host_domain :
20+ raise Exception (
21+ "Host domain has not been provided.\n "
22+ "Did you set the AGENT_CONNECT_HOST_DOMAIN in you project's .env file?"
23+ )
24+
25+ if not did_document_path :
26+ raise Exception (
27+ "DID document path has not been provided.\n "
28+ "Did you set the AGENT_CONNECT_DID_DOCUMENT_PATH in you project's .env file?"
29+ )
2230
2331
2432def generate_did_info (node : SimpleNode , did_document_path : str ) -> None :
2533 """
2634 Generate or load DID information for a node.
27-
35+
2836 Args:
2937 node: SimpleNode instance
3038 did_document_path: Path to store/load DID document
@@ -33,35 +41,33 @@ def generate_did_info(node: SimpleNode, did_document_path: str) -> None:
3341 print (f"Loading existing DID information from { did_document_path } " )
3442 with open (did_document_path , "r" ) as f :
3543 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- )
44+ node .set_did_info (did_info ["private_key_pem" ], did_info ["did" ], did_info ["did_document_json" ])
4145 else :
4246 print ("Generating new DID information" )
4347 private_key_pem , did , did_document_json = node .generate_did_document ()
4448 node .set_did_info (private_key_pem , did , did_document_json )
45-
49+
4650 # Save DID information
47- os .makedirs (os .path .dirname (did_document_path ), exist_ok = True )
51+ if os .path .dirname (did_document_path ): # allow saving to current directory
52+ os .makedirs (os .path .dirname (did_document_path ), exist_ok = True )
4853 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+ json .dump (
55+ { "private_key_pem" : private_key_pem , "did" : did , "did_document_json" : did_document_json } ,
56+ f ,
57+ indent = 2 ,
58+ )
5459 print (f"DID information saved to { did_document_path } " )
5560
61+
5662agent_connect_simple_node = SimpleNode (host_domain , host_port , host_ws_path )
5763generate_did_info (agent_connect_simple_node , did_document_path )
5864agent_connect_simple_node .run ()
5965
60- @ tool ( "Send Message to Agent by DID" )
66+
6167async def send_message (message : str , destination_did : str ) -> bool :
6268 """
6369 Send a message through agent-connect node.
64-
70+
6571 Args:
6672 message: Message content to be sent
6773 destination_did: DID of the recipient agent
@@ -76,11 +82,11 @@ async def send_message(message: str, destination_did: str) -> bool:
7682 print (f"Failed to send message: { e } " )
7783 return False
7884
79- @ tool ( "Receive Message from Agent" )
85+
8086async def receive_message () -> tuple [str , str ]:
8187 """
8288 Receive message from agent-connect node.
83-
89+
8490 Returns:
8591 tuple[str, str]: Sender DID and received message content, empty string if no message or error occurred
8692 """
0 commit comments