Skip to content

Commit 3b8ea78

Browse files
authored
Merge pull request #64 from Serverless-Devs/feat-default-health-api
feat(server): add health check endpoint with tests
2 parents 7b9a8f3 + b69c5f8 commit 3b8ea78

File tree

6 files changed

+49
-23
lines changed

6 files changed

+49
-23
lines changed

agentrun/server/agui_protocol.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,6 @@ async def run_agent(request: Request):
206206
headers=sse_headers,
207207
)
208208

209-
@router.get("/health")
210-
async def health_check():
211-
"""健康检查端点"""
212-
return {"status": "ok", "protocol": "ag-ui", "version": "1.0"}
213-
214209
return router
215210

216211
async def parse_request(

agentrun/server/server.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ def __init__(
132132

133133
self.agent_invoker = AgentInvoker(invoke_agent)
134134

135+
# 注册 health check 路由
136+
self._register_health_check()
137+
135138
# 配置 CORS
136139
self._setup_cors(config.cors_origins if config else None)
137140

@@ -145,6 +148,13 @@ def __init__(
145148
# 挂载所有协议的 Router
146149
self._mount_protocols(protocols)
147150

151+
def _register_health_check(self):
152+
"""注册 /health 健康检查路由 / Register /health health check route"""
153+
154+
@self.app.get("/health")
155+
async def health_check():
156+
return {"status": "ok"}
157+
148158
def _wrap_with_memory(
149159
self,
150160
invoke_agent: InvokeAgentHandler,

tests/unittests/integration/langchain/test_agent_invoke_methods.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def _start_server(app: FastAPI) -> tuple:
100100
base_url = f"http://127.0.0.1:{port}"
101101
for i in range(50):
102102
try:
103-
httpx.get(f"{base_url}/ag-ui/agent/health", timeout=0.2)
103+
httpx.get(f"{base_url}/health", timeout=0.2)
104104
break
105105
except Exception:
106106
if i == 49:

tests/unittests/integration/test_langchain_agui_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ async def invoke_agent(request: AgentRequest):
671671
base_url = f"http://127.0.0.1:{port}"
672672
for i in range(50):
673673
try:
674-
httpx.get(f"{base_url}/ag-ui/agent/health", timeout=0.2)
674+
httpx.get(f"{base_url}/health", timeout=0.2)
675675
break
676676
except Exception:
677677
if i == 49:

tests/unittests/server/test_agui_protocol.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,6 @@ def get_client(self, invoke_agent):
4343
server = AgentRunServer(invoke_agent=invoke_agent)
4444
return TestClient(server.as_fastapi_app())
4545

46-
@pytest.mark.asyncio
47-
async def test_health_check(self):
48-
"""测试健康检查端点"""
49-
50-
def invoke_agent(request: AgentRequest):
51-
return "Hello"
52-
53-
client = self.get_client(invoke_agent)
54-
response = client.get("/ag-ui/agent/health")
55-
56-
assert response.status_code == 200
57-
data = response.json()
58-
assert data["status"] == "ok"
59-
assert data["protocol"] == "ag-ui"
60-
assert data["version"] == "1.0"
61-
6246
@pytest.mark.asyncio
6347
async def test_value_error_handling(self):
6448
"""测试 ValueError 处理"""

tests/unittests/server/test_server.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,43 @@ def get_client(self, invoke_agent):
124124

125125
return TestClient(app)
126126

127+
async def test_health_check(self):
128+
"""测试 /health 健康检查路由"""
129+
130+
client = self.get_client(self.get_invoke_agent_non_streaming())
131+
132+
response = client.get("/health")
133+
134+
assert response.status_code == 200
135+
assert response.json() == {"status": "ok"}
136+
137+
async def test_health_check_post_not_allowed(self):
138+
"""测试 POST /health 不被允许"""
139+
140+
client = self.get_client(self.get_invoke_agent_non_streaming())
141+
142+
response = client.post("/health")
143+
144+
# FastAPI 对不匹配的方法返回 405
145+
assert response.status_code == 405
146+
147+
async def test_health_check_with_custom_protocols(self):
148+
"""测试自定义协议列表时 /health 仍可用"""
149+
from agentrun.server.openai_protocol import OpenAIProtocolHandler
150+
151+
server = AgentRunServer(
152+
invoke_agent=self.get_invoke_agent_non_streaming(),
153+
protocols=[OpenAIProtocolHandler()],
154+
)
155+
from fastapi.testclient import TestClient
156+
157+
client = TestClient(server.as_fastapi_app())
158+
159+
response = client.get("/health")
160+
161+
assert response.status_code == 200
162+
assert response.json() == {"status": "ok"}
163+
127164
async def test_server_non_streaming_protocols(self):
128165
"""测试非流式的 OpenAI 和 AGUI 服务器响应功能"""
129166

0 commit comments

Comments
 (0)