Skip to content

Commit 8fc43ee

Browse files
authored
Merge pull request #25 from chgaowei/main
Add tool:agent-connect, A Tool for Intelligent Agent Communication and Collaboration
2 parents bd103af + f0ac9bd commit 8fc43ee

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 "", ""
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "agent-connect",
3+
"package": "poetry add agent-connect",
4+
"env": "HOST_DOMAIN=...\nHOST_PORT=\"80\"\nHOST_WS_PATH=\"/ws\"\nDID_DOCUMENT_PATH=...\nSSL_CERT_PATH=...\nSSL_KEY_PATH=...",
5+
"tools": ["send_message", "receive_message"]
6+
}

agentstack/tools/tools.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,9 @@
2828
},{
2929
"name": "ftp",
3030
"url": "AgentStack custom tool"
31+
}],
32+
"network-protocols": [{
33+
"name": "agent-connect",
34+
"url": "https://github.com/chgaowei/AgentConnect"
3135
}]
32-
}
36+
}

0 commit comments

Comments
 (0)