@@ -50,16 +50,22 @@ def load_agent_module(agent_id: str, name: str, filename: str):
50
50
agent_module = importlib .util .module_from_spec (spec )
51
51
spec .loader .exec_module (agent_module )
52
52
53
- # # Check if the module has a run function
53
+ # Check if the module has a run function
54
54
if not hasattr (agent_module , "run" ):
55
55
raise AttributeError (f"Module { filename } does not have a run function" )
56
56
57
+ # Check if the module has an welcome function - which is optional
58
+ welcome = None
59
+ if hasattr (agent_module , "welcome" ):
60
+ welcome = agent_module .welcome
61
+
57
62
logger .debug (f"Loaded agent: { agent_id } " )
58
63
59
64
return {
60
65
"id" : agent_id ,
61
66
"name" : name ,
62
67
"run" : agent_module .run ,
68
+ "welcome" : welcome ,
63
69
}
64
70
65
71
@@ -77,12 +83,16 @@ async def run_agent(tracer, agentId, agent, payload, agents_by_id):
77
83
agent_context = AgentContext (
78
84
services = {
79
85
"kv" : KeyValueStore (
80
- base_url = os .environ .get ("AGENTUITY_TRANSPORT_URL" , "https://agentuity.ai" ),
86
+ base_url = os .environ .get (
87
+ "AGENTUITY_TRANSPORT_URL" , "https://agentuity.ai"
88
+ ),
81
89
api_key = os .environ .get ("AGENTUITY_API_KEY" ),
82
90
tracer = tracer ,
83
91
),
84
92
"vector" : VectorStore (
85
- base_url = os .environ .get ("AGENTUITY_TRANSPORT_URL" , "https://agentuity.ai" ),
93
+ base_url = os .environ .get (
94
+ "AGENTUITY_TRANSPORT_URL" , "https://agentuity.ai"
95
+ ),
86
96
api_key = os .environ .get ("AGENTUITY_API_KEY" ),
87
97
tracer = tracer ,
88
98
),
@@ -215,6 +225,40 @@ async def handle_run_request(request):
215
225
return resp
216
226
217
227
228
+ async def handle_welcome_request (request : web .Request ):
229
+ res = {}
230
+ for agent in request .app ["agents_by_id" ].values ():
231
+ if "welcome" in agent and agent ["welcome" ] is not None :
232
+ fn = agent ["welcome" ]()
233
+ if isinstance (fn , dict ):
234
+ res [agent ["id" ]] = fn
235
+ else :
236
+ res [agent ["id" ]] = await fn
237
+ return web .json_response (res )
238
+
239
+
240
+ async def handle_agent_welcome_request (request : web .Request ):
241
+ agents_by_id = request .app ["agents_by_id" ]
242
+ if request .match_info ["agent_id" ] in agents_by_id :
243
+ agent = agents_by_id [request .match_info ["agent_id" ]]
244
+ if "welcome" in agent and agent ["welcome" ] is not None :
245
+ fn = agent ["welcome" ]()
246
+ if not isinstance (fn , dict ):
247
+ fn = await fn
248
+ return web .json_response (fn )
249
+ else :
250
+ return web .Response (
251
+ status = 404 ,
252
+ content_type = "text/plain" ,
253
+ )
254
+ else :
255
+ return web .Response (
256
+ text = f"Agent { request .match_info ['agent_id' ]} not found" ,
257
+ status = 404 ,
258
+ content_type = "text/plain" ,
259
+ )
260
+
261
+
218
262
async def handle_agent_request (request : web .Request ):
219
263
# Access the agents_by_id from the app state
220
264
agents_by_id = request .app ["agents_by_id" ]
@@ -398,13 +442,18 @@ def load_config() -> Any:
398
442
from yaml import safe_load
399
443
400
444
agent_config = safe_load (config_file )
401
- config_data = {}
445
+ config_data = {"agents" : [] }
402
446
config_data ["environment" ] = "development"
403
447
config_data ["cli_version" ] = "unknown"
404
448
config_data ["app" ] = {"name" : agent_config ["name" ], "version" : "dev" }
405
- config_data ["filename" ] = os .path .join (
406
- os .getcwd (), "agents" , agent_config ["name" ], "agent.py"
407
- )
449
+ for agent in agent_config ["agents" ]:
450
+ config = {}
451
+ config ["id" ] = agent ["id" ]
452
+ config ["name" ] = agent ["name" ]
453
+ config ["filename" ] = os .path .join (
454
+ os .getcwd (), "agents" , agent ["name" ], "agent.py"
455
+ )
456
+ config_data ["agents" ].append (config )
408
457
return config_data
409
458
410
459
@@ -426,6 +475,11 @@ def load_agents(config_data):
426
475
"name" : agent ["name" ],
427
476
"filename" : agent ["filename" ],
428
477
"run" : agent_module ["run" ],
478
+ "welcome" : (
479
+ agent_module ["welcome" ]
480
+ if "welcome" in agent_module and agent_module ["welcome" ] is not None
481
+ else None
482
+ ),
429
483
}
430
484
logger .info (f"Loaded { len (agents_by_id )} agents" )
431
485
for agent in agents_by_id .values ():
@@ -481,6 +535,8 @@ def autostart(callback: Callable[[], None] = None):
481
535
app .router .add_get ("/_health" , handle_health_check )
482
536
app .router .add_post ("/run/{agent_id}" , handle_run_request )
483
537
app .router .add_post ("/{agent_id}" , handle_agent_request )
538
+ app .router .add_get ("/welcome" , handle_welcome_request )
539
+ app .router .add_get ("/welcome/{agent_id}" , handle_agent_welcome_request )
484
540
485
541
# Start the server
486
542
logger .info (f"Starting server on port { port } " )
0 commit comments