-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrun_three_agents.sh
More file actions
executable file
·273 lines (231 loc) · 9.55 KB
/
run_three_agents.sh
File metadata and controls
executable file
·273 lines (231 loc) · 9.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/bin/bash
# Script to run three bidirectional agents with directed connections
# Agent 1 (port 4200) connects to Agent 2 (port 4201)
# Agent 2 (port 4201) connects to both Agent 1 (port 4200) and Agent 3 (port 4202)
# Agent 3 (port 4202) connects to Agent 2 (port 4201)
# Function to handle cleanup on exit
cleanup() {
echo "Cleaning up and stopping all agents..."
# Kill any background processes we started
if [ -n "$AGENT1_PID" ]; then
kill $AGENT1_PID 2>/dev/null || true
fi
if [ -n "$AGENT2_PID" ]; then
kill $AGENT2_PID 2>/dev/null || true
fi
if [ -n "$AGENT3_PID" ]; then
kill $AGENT3_PID 2>/dev/null || true
fi
# Exit
exit 0
}
# Function to find PID for a given config file using pgrep
# Retries for a few seconds to allow the process to start
find_agent_pid() {
local config_file="$1"
local pid=""
local attempts=0
local max_attempts=10 # Try for 5 seconds (10 * 0.5s)
echo "Searching for PID using config: ${config_file}..." >&2 # Debug output to stderr
while [ -z "$pid" ] && [ $attempts -lt $max_attempts ]; do
# Use pgrep -f to match the full command line
pid=$(pgrep -f "bidirectional-agent ${config_file}" | head -n 1)
if [ -z "$pid" ]; then
sleep 0.5
attempts=$((attempts + 1))
else
: # PID found, break loop
fi
done
if [ -z "$pid" ]; then
echo "Error: Could not find PID for agent with config ${config_file} after ${max_attempts} attempts." >&2
echo "" # Return empty string on failure
else
echo "$pid"
fi
}
# Set up trap to catch SIGINT (Ctrl+C) and SIGTERM
trap cleanup SIGINT SIGTERM
# Check if API key is provided
if [ -z "$CLAUDE_API_KEY" ] && [ -z "$GEMINI_API_KEY" ]; then
echo "Neither CLAUDE_API_KEY nor GEMINI_API_KEY environment variable is set."
echo "Please set one of them before running this script."
exit 1
fi
# Check if a terminal emulator is available
TERMINAL=""
if command -v gnome-terminal &> /dev/null; then
TERMINAL="gnome-terminal"
elif command -v xterm &> /dev/null; then
TERMINAL="xterm"
elif command -v konsole &> /dev/null; then
TERMINAL="konsole"
elif command -v terminal &> /dev/null; then
TERMINAL="terminal" # For macOS
else
echo "Error: No supported terminal emulator found."
echo "Please install gnome-terminal, xterm, or konsole."
exit 1
fi
# Build the agent
echo "Building bidirectional agent..."
RUSTFLAGS="-A warnings" cargo build --quiet --bin bidirectional-agent || { echo "Build failed, exiting."; exit 1; }
# Directory where the a2a-test-suite is located
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$PROJECT_DIR"
# Ensure data directory for agent directories exists
mkdir -p "${PROJECT_DIR}/data"
echo "Starting bidirectional agents..."
# Create config files for all agents
cat > "${PROJECT_DIR}/agent1_config.toml" << EOF
[server]
port = 4200
bind_address = "0.0.0.0"
agent_id = "bidirectional-agent-1"
agent_name = "Agent One"
# [client]
# target_url = "http://localhost:4201"
[llm]
# API key set via environment variable
system_prompt = "You are a multi-purpose agent that discovers and connects with other agents. Your primary role is to identify when you need assistance from specialized agents and establish connections with them to solve complex tasks collaboratively."
[mode]
repl = true
get_agent_card = false
repl_log_file = "shared_agent_interactions.log"
[tools]
enabled = ["echo", "llm", "list_agents", "execute_command", "remember_agent"]
# REMOVED agent_directory_path
EOF
cat > "${PROJECT_DIR}/agent2_config.toml" << EOF
[server]
port = 4201
bind_address = "0.0.0.0"
agent_id = "bidirectional-agent-2"
agent_name = "Agent Two"
# [client]
# target_url = "http://localhost:4200"
[llm]
# API key set via environment variable
system_prompt = "You are a network coordination agent that maintains awareness of available agent services. Your primary function is to connect requestors with the right specialized agents based on the task requirements."
[mode]
repl = true
get_agent_card = false
repl_log_file = "shared_agent_interactions.log"
[tools]
enabled = ["echo", "llm", "list_agents", "execute_command", "remember_agent"]
# REMOVED agent_directory_path
EOF
cat > "${PROJECT_DIR}/agent3_config.toml" << EOF
[server]
port = 4202
bind_address = "0.0.0.0"
agent_id = "bidirectional-agent-3"
agent_name = "Agent Three -- Memory Specialist"
# [client]
# target_url = "http://localhost:4201"
[llm]
# API key set via environment variable
system_prompt = "You are a specialized agent with exceptional memory capabilities. Your primary function is to record, store, and recall information about other agents and past interactions, serving as a long-term memory resource for the agent network."
[mode]
repl = true
get_agent_card = false
repl_log_file = "shared_agent_interactions.log"
[tools]
enabled = ["echo", "summarize", "list_agents", "remember_agent", "execute_command", "llm"]
# REMOVED agent_directory_path
EOF
# Start Agent 1
AGENT1_CMD="cd \"$PROJECT_DIR\" && echo -e \"Starting Agent 1 (listening on port 4200, connecting to 4201)...\n\" && RUST_LOG=info CLAUDE_API_KEY=$CLAUDE_API_KEY GEMINI_API_KEY=$GEMINI_API_KEY AUTO_LISTEN=true ./target/debug/bidirectional-agent agent1_config.toml" # Changed to info
if [ "$TERMINAL" = "gnome-terminal" ]; then
gnome-terminal --title="Agent 1 (Port 4200)" -- bash -c "$AGENT1_CMD" &
elif [ "$TERMINAL" = "xterm" ]; then
xterm -title "Agent 1 (Port 4200)" -e "$AGENT1_CMD" &
elif [ "$TERMINAL" = "konsole" ]; then
konsole --new-tab -p tabtitle="Agent 1 (Port 4200)" -e bash -c "$AGENT1_CMD" &
elif [ "$TERMINAL" = "terminal" ]; then
# For macOS
terminal -e "$AGENT1_CMD" &
fi
# Find and save the PID of Agent 1
sleep 1
AGENT1_PID=$(find_agent_pid "agent1_config.toml")
if [ -z "$AGENT1_PID" ]; then
echo "Warning: Failed to get PID for Agent 1. Cleanup might be incomplete."
else
echo "Agent 1 PID: $AGENT1_PID"
fi
# Wait a bit for the first agent to start
sleep 1
# Start Agent 2
AGENT2_CMD="cd \"$PROJECT_DIR\" && echo -e \"Starting Agent 2 (listening on port 4201, connecting to 4200 and 4202)...\n\" && RUST_LOG=info CLAUDE_API_KEY=$CLAUDE_API_KEY GEMINI_API_KEY=$GEMINI_API_KEY AUTO_LISTEN=true ./target/debug/bidirectional-agent agent2_config.toml" # Changed to info
if [ "$TERMINAL" = "gnome-terminal" ]; then
gnome-terminal --title="Agent 2 (Port 4201)" -- bash -c "$AGENT2_CMD" &
elif [ "$TERMINAL" = "xterm" ]; then
xterm -title "Agent 2 (Port 4201)" -e "$AGENT2_CMD" &
elif [ "$TERMINAL" = "konsole" ]; then
konsole --new-tab -p tabtitle="Agent 2 (Port 4201)" -e bash -c "$AGENT2_CMD" &
elif [ "$TERMINAL" = "terminal" ]; then
terminal -e "$AGENT2_CMD" &
fi
# Find and save the PID of Agent 2
sleep 1
AGENT2_PID=$(find_agent_pid "agent2_config.toml")
if [ -z "$AGENT2_PID" ]; then
echo "Warning: Failed to get PID for Agent 2. Cleanup might be incomplete."
else
echo "Agent 2 PID: $AGENT2_PID"
fi
# Wait a bit for the second agent to start
sleep 1
# Start Agent 3
AGENT3_CMD="cd \"$PROJECT_DIR\" && echo -e \"Starting Agent 3 (listening on port 4202, connecting to 4201)...\n\" && RUST_LOG=info CLAUDE_API_KEY=$CLAUDE_API_KEY GEMINI_API_KEY=$GEMINI_API_KEY AUTO_LISTEN=true ./target/debug/bidirectional-agent agent3_config.toml" # Changed to info
if [ "$TERMINAL" = "gnome-terminal" ]; then
gnome-terminal --title="Agent 3 -- Memory Specialist (Port 4202)" -- bash -c "$AGENT3_CMD" &
elif [ "$TERMINAL" = "xterm" ]; then
xterm -title "Agent 3 -- Memory Specialist (Port 4202)" -e "$AGENT3_CMD" &
elif [ "$TERMINAL" = "konsole" ]; then
konsole --new-tab -p tabtitle="Agent 3 -- Memory Specialist (Port 4202)" -e bash -c "$AGENT3_CMD" &
elif [ "$TERMINAL" = "terminal" ]; then
terminal -e "$AGENT3_CMD" &
fi
# Find and save the PID of Agent 3
sleep 1
AGENT3_PID=$(find_agent_pid "agent3_config.toml")
if [ -z "$AGENT3_PID" ]; then
echo "Warning: Failed to get PID for Agent 3. Cleanup might be incomplete."
else
echo "Agent 3 PID: $AGENT3_PID"
fi
echo "All agents started and listening on their respective ports:"
echo "- Agent 1: Port 4200 (connecting to Agent 2 on port 4201)"
echo "- Agent 2: Port 4201 (will connect to both Agent 1 on port 4200 and Agent 3 on port 4202)"
echo "- Agent 3: Port 4202 (connecting to Agent 2 on port 4201)"
echo
echo "All agents are logging at INFO level for detailed output"
echo
echo "Setup for Agent Discovery:"
echo "1. First connect the agents to each other:"
echo " - In Agent 1 terminal: :connect http://localhost:4201"
echo " - In Agent 2 terminal: :connect http://localhost:4200"
echo " - In Agent 2 terminal: :connect http://localhost:4202"
echo " - In Agent 3 terminal: :connect http://localhost:4201"
echo
echo "2. To discover other agents, use the list_agents tool:"
echo " - In any agent terminal: :tool list_agents {\"format\":\"detailed\"}"
echo " - This will display all agents this agent knows about"
echo
echo "3. To check if Agent 2 knows about Agent 3:"
echo " - In Agent 1 terminal: :remote :tool list_agents"
echo " - This will ask Agent 2 to list all agents it knows about"
echo
echo "4. To demonstrate agent discovery works:"
echo " - Verify Agent 1 doesn't initially know about Agent 3 by running :tool list_agents"
echo " - Have Agent 1 ask Agent 2 about other agents using :remote"
echo " - After discovering Agent 3, connect directly: :connect http://localhost:4202"
echo " - Verify Agent 1 now knows about Agent 3 by running :tool list_agents again"
echo
echo "Press Ctrl+C to stop all agents."
# Keep the script running to catch Ctrl+C for cleanup
while true; do
sleep 1
done