-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_manager.py
More file actions
311 lines (250 loc) · 11.5 KB
/
config_manager.py
File metadata and controls
311 lines (250 loc) · 11.5 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python3
"""
Configuration management script for custom agents
"""
import os
import asyncio
from dotenv import load_dotenv, set_key
from custom_agent_client import CustomAgentConfigLoader, test_all_custom_agents
class CustomAgentConfigManager:
"""Manager for custom agent configuration"""
def __init__(self):
self.config_file = "custom_agents.env"
def enable_custom_agents(self):
"""Enable custom agents in the workflow"""
set_key(self.config_file, "USE_CUSTOM_AGENTS", "true")
print("✅ Custom agents enabled")
def disable_custom_agents(self):
"""Disable custom agents in the workflow"""
set_key(self.config_file, "USE_CUSTOM_AGENTS", "false")
print("✅ Custom agents disabled")
def add_builtin_agent(self, platform: str, name: str, color: str):
"""Add a new built-in SK agent configuration"""
# Find the next available slot for this platform
agent_num = 1
while True:
prefix = self._get_platform_prefix(platform)
if not prefix:
print(f"❌ Invalid platform: {platform}")
return
name_key = f"{prefix}{agent_num}_NAME"
if not os.getenv(name_key):
break
agent_num += 1
# Add the built-in agent configuration
type_key = f"{prefix}{agent_num}_TYPE"
color_key = f"{prefix}{agent_num}_COLOR"
set_key(self.config_file, name_key, name)
set_key(self.config_file, type_key, "builtin")
set_key(self.config_file, color_key, color)
print(f"✅ Added built-in SK agent: {name} ({platform}) - {color}")
print(" This agent will use your Azure OpenAI configuration")
def add_custom_agent(self, platform: str, name: str, endpoint: str, api_key: str, color: str, authenticated: bool = True):
"""Add a new custom agent configuration"""
# Find the next available slot for this platform
platform_prefix = f"{platform.upper()}_AGENT_"
agent_num = 1
while True:
name_key = f"{platform_prefix}{agent_num}_NAME"
if not os.getenv(name_key):
break
agent_num += 1
# Handle unauthenticated agents
if not authenticated or not api_key or api_key.upper() in ["NONE", "NULL", ""]:
api_key = "NONE"
# Set the configuration
set_key(self.config_file, f"{platform_prefix}{agent_num}_NAME", name)
set_key(self.config_file, f"{platform_prefix}{agent_num}_ENDPOINT", endpoint)
set_key(self.config_file, f"{platform_prefix}{agent_num}_API_KEY", api_key)
set_key(self.config_file, f"{platform_prefix}{agent_num}_COLOR", color)
auth_status = "Authenticated" if authenticated and api_key != "NONE" else "Unauthenticated"
print(f"✅ Added custom agent: {name} ({platform}) - {color} ({auth_status})")
def list_agents(self):
"""List all configured agents"""
load_dotenv(self.config_file)
use_custom = CustomAgentConfigLoader.should_use_custom_agents()
print("🔧 Agent Configuration Status:")
print(f" Configuration Loading Enabled: {'✅ Yes' if use_custom else '❌ No'}")
if use_custom:
agents = CustomAgentConfigLoader.load_custom_agents()
print(f"\n📋 Configured Agents ({len(agents)}):")
custom_count = 0
builtin_count = 0
for i, agent in enumerate(agents, 1):
if agent.agent_type == "custom":
custom_count += 1
endpoint_display = agent.endpoint[:50] + "..." if len(agent.endpoint) > 50 else agent.endpoint
auth_status = "🔐 Authenticated" if agent.is_authenticated else "🔓 Unauthenticated"
api_key_display = "Configured" if agent.is_authenticated else "Not Required"
print(f" {i}. {agent.name} (Custom HTTP Agent)")
print(f" Platform: {agent.platform}")
print(f" Color: {agent.favorite_color}")
print(f" Endpoint: {endpoint_display}")
print(f" Authentication: {auth_status}")
print(f" API Key: {api_key_display}")
print()
elif agent.agent_type == "builtin":
builtin_count += 1
print(f" {i}. {agent.name} (Built-in SK Agent)")
print(f" Platform: {agent.platform}")
print(f" Color: {agent.favorite_color}")
print(" Uses: Your Azure OpenAI configuration")
print()
print(f"📊 Summary: {custom_count} Custom Agents, {builtin_count} Built-in Agents")
else:
print("\n⚠️ No agents will be loaded. Set USE_CUSTOM_AGENTS=true to enable agents.")
async def test_agents(self):
"""Test connectivity to all custom agents"""
print("🧪 Testing Custom Agent Connectivity...")
print("-" * 40)
agents = CustomAgentConfigLoader.load_custom_agents()
if not agents:
print("⚠️ No custom agents configured to test")
return
results = await test_all_custom_agents()
print("\n📊 Test Results Summary:")
success_count = sum(1 for success in results.values() if success)
total_count = len(results)
print(f" ✅ Successful: {success_count}/{total_count}")
print(f" ❌ Failed: {total_count - success_count}/{total_count}")
if success_count < total_count:
print("\n⚠️ Some agents failed connectivity tests.")
print(" Please check your endpoints and API keys in custom_agents.env")
def show_menu():
"""Show the configuration menu"""
print("\n🔧 Agent Configuration Manager")
print("=" * 40)
print("1. List all agents")
print("2. Enable configuration loading")
print("3. Disable configuration loading")
print("4. Add custom HTTP agent (authenticated)")
print("5. Add custom HTTP agent (unauthenticated)")
print("6. Add built-in SK agent")
print("7. Test agent connectivity")
print("8. Show sample configuration")
print("9. Show authentication help")
print("10. Exit")
print("-" * 40)
def show_sample_config():
"""Show sample configuration for custom agents"""
print("\n📋 Sample Custom Agent Configuration:")
print("=" * 50)
print("""
# Example configuration for authenticated agents:
AWS_AGENT_1_NAME=MyCustomAWS-DataProcessor
AWS_AGENT_1_ENDPOINT=https://my-aws-agent.amazonaws.com/api/chat
AWS_AGENT_1_API_KEY=my-secret-api-key-123
AWS_AGENT_1_COLOR=Red
# Example configuration for unauthenticated agents:
AWS_AGENT_2_NAME=MyPublicAWS-Service
AWS_AGENT_2_ENDPOINT=https://my-public-agent.amazonaws.com/api/chat
AWS_AGENT_2_API_KEY=NONE
AWS_AGENT_2_COLOR=Green
# Alternative ways to specify unauthenticated agents:
# AWS_AGENT_3_API_KEY= (empty)
# AWS_AGENT_3_API_KEY=NULL (explicit null)
# Your agent should accept POST requests with JSON payload:
{
"message": "task description",
"context": "additional context",
"agent_info": {
"name": "agent name",
"platform": "cloud platform",
"favorite_color": "color"
}
}
# Authenticated agents will receive:
# Headers: Authorization: Bearer <api-key>
# Unauthenticated agents will NOT receive authorization headers
# And return JSON response with:
{
"message": "agent response text"
}
# OR
{
"response": "agent response text"
}
# OR OpenAI-compatible format with choices array
""")
def show_auth_help():
"""Show help about authentication options"""
print("\n🔐 Authentication Options:")
print("=" * 30)
print("""
AUTHENTICATED AGENTS:
- Require an API key for access
- API key sent in Authorization header as Bearer token
- More secure for production deployments
- Use when your agent requires authentication
UNAUTHENTICATED AGENTS:
- No API key required
- No authorization headers sent
- Good for public demos or internal testing
- Set API_KEY to "NONE", "NULL", or leave empty
SECURITY CONSIDERATIONS:
- Unauthenticated agents should have appropriate rate limiting
- Consider network-level security (VPC, firewall rules)
- Use HTTPS endpoints for all agents
- Monitor usage and implement abuse protection
""")
async def main():
"""Main configuration management function"""
manager = CustomAgentConfigManager()
while True:
show_menu()
try:
choice = input("Enter your choice (1-10): ").strip()
if choice == "1":
manager.list_agents()
elif choice == "2":
manager.enable_custom_agents()
elif choice == "3":
manager.disable_custom_agents()
elif choice == "4":
print("\nAdd Authenticated Custom HTTP Agent:")
platform = input("Platform (AWS/Azure/GCP/Multi-Cloud): ").strip()
name = input("Agent Name: ").strip()
endpoint = input("API Endpoint URL: ").strip()
api_key = input("API Key: ").strip()
color = input("Favorite Color: ").strip()
if all([platform, name, endpoint, api_key, color]):
manager.add_custom_agent(platform, name, endpoint, api_key, color, authenticated=True)
else:
print("❌ All fields are required for authenticated agents")
elif choice == "5":
print("\nAdd Unauthenticated Custom HTTP Agent:")
platform = input("Platform (AWS/Azure/GCP/Multi-Cloud): ").strip()
name = input("Agent Name: ").strip()
endpoint = input("API Endpoint URL: ").strip()
color = input("Favorite Color: ").strip()
if all([platform, name, endpoint, color]):
manager.add_custom_agent(platform, name, endpoint, "NONE", color, authenticated=False)
else:
print("❌ All fields are required")
elif choice == "6":
print("\nAdd Built-in Semantic Kernel Agent:")
platform = input("Platform (AWS/Azure/GCP/Multi-Cloud): ").strip()
name = input("Agent Name: ").strip()
color = input("Favorite Color: ").strip()
if all([platform, name, color]):
manager.add_builtin_agent(platform, name, color)
else:
print("❌ All fields are required")
elif choice == "7":
await manager.test_agents()
elif choice == "8":
show_sample_config()
elif choice == "9":
show_auth_help()
elif choice == "10":
print("👋 Goodbye!")
break
else:
print("❌ Invalid choice. Please enter 1-10.")
except KeyboardInterrupt:
print("\n👋 Goodbye!")
break
except Exception as e:
print(f"❌ Error: {str(e)}")
if __name__ == "__main__":
asyncio.run(main())