Skip to content

Commit f5ba3d7

Browse files
authored
Add agent inspect endpoint support (#29)
* Add agent inspect endpoint support * fix test error * fix test * use welcome
1 parent 48f96ac commit f5ba3d7

File tree

2 files changed

+175
-124
lines changed

2 files changed

+175
-124
lines changed

agentuity/server/__init__.py

+63-7
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,22 @@ def load_agent_module(agent_id: str, name: str, filename: str):
5050
agent_module = importlib.util.module_from_spec(spec)
5151
spec.loader.exec_module(agent_module)
5252

53-
# # Check if the module has a run function
53+
# Check if the module has a run function
5454
if not hasattr(agent_module, "run"):
5555
raise AttributeError(f"Module {filename} does not have a run function")
5656

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+
5762
logger.debug(f"Loaded agent: {agent_id}")
5863

5964
return {
6065
"id": agent_id,
6166
"name": name,
6267
"run": agent_module.run,
68+
"welcome": welcome,
6369
}
6470

6571

@@ -77,12 +83,16 @@ async def run_agent(tracer, agentId, agent, payload, agents_by_id):
7783
agent_context = AgentContext(
7884
services={
7985
"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+
),
8189
api_key=os.environ.get("AGENTUITY_API_KEY"),
8290
tracer=tracer,
8391
),
8492
"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+
),
8696
api_key=os.environ.get("AGENTUITY_API_KEY"),
8797
tracer=tracer,
8898
),
@@ -215,6 +225,40 @@ async def handle_run_request(request):
215225
return resp
216226

217227

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+
218262
async def handle_agent_request(request: web.Request):
219263
# Access the agents_by_id from the app state
220264
agents_by_id = request.app["agents_by_id"]
@@ -398,13 +442,18 @@ def load_config() -> Any:
398442
from yaml import safe_load
399443

400444
agent_config = safe_load(config_file)
401-
config_data = {}
445+
config_data = {"agents": []}
402446
config_data["environment"] = "development"
403447
config_data["cli_version"] = "unknown"
404448
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)
408457
return config_data
409458

410459

@@ -426,6 +475,11 @@ def load_agents(config_data):
426475
"name": agent["name"],
427476
"filename": agent["filename"],
428477
"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+
),
429483
}
430484
logger.info(f"Loaded {len(agents_by_id)} agents")
431485
for agent in agents_by_id.values():
@@ -481,6 +535,8 @@ def autostart(callback: Callable[[], None] = None):
481535
app.router.add_get("/_health", handle_health_check)
482536
app.router.add_post("/run/{agent_id}", handle_run_request)
483537
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)
484540

485541
# Start the server
486542
logger.info(f"Starting server on port {port}")

0 commit comments

Comments
 (0)