|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import uvicorn |
| 16 | +from fastapi import FastAPI |
| 17 | +from fastapi.responses import JSONResponse |
| 18 | + |
| 19 | +from veadk.a2a.hub.models import ( |
| 20 | + AgentInformation, |
| 21 | + GetAgentResponse, |
| 22 | + GetAgentsResponse, |
| 23 | + GetGroupsResponse, |
| 24 | + RegisterAgentRequest, |
| 25 | + RegisterAgentResponse, |
| 26 | + RegisterGroupResponse, |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +class A2AHubServer: |
| 31 | + def __init__(self): |
| 32 | + self.app = FastAPI() |
| 33 | + |
| 34 | + self.groups: list[str] = [] |
| 35 | + |
| 36 | + # group_id -> agent_id -> agent_card |
| 37 | + self.agent_cards: dict[str, dict[str, dict]] = {} |
| 38 | + |
| 39 | + @self.app.get("/ping") |
| 40 | + def ping() -> JSONResponse: |
| 41 | + return JSONResponse(content={"msg": "pong!"}) |
| 42 | + |
| 43 | + @self.app.post("/create_group") |
| 44 | + def create_group(group_id: str) -> RegisterGroupResponse: |
| 45 | + """Create a group.""" |
| 46 | + self.groups.append(group_id) |
| 47 | + self.agent_cards[group_id] = {} |
| 48 | + return RegisterGroupResponse(group_id=group_id) |
| 49 | + |
| 50 | + @self.app.post("/register_agent") |
| 51 | + def register_agent( |
| 52 | + request: RegisterAgentRequest, |
| 53 | + ) -> RegisterAgentResponse: |
| 54 | + """Register an agent to a specified group.""" |
| 55 | + if request.group_id not in self.groups: |
| 56 | + return RegisterAgentResponse( |
| 57 | + err_code=1, msg=f"group {request.group_id} not exist" |
| 58 | + ) |
| 59 | + if request.agent_id in self.agent_cards[request.group_id]: |
| 60 | + return RegisterAgentResponse( |
| 61 | + err_code=1, msg=f"agent {request.agent_id} already exist" |
| 62 | + ) |
| 63 | + |
| 64 | + self.agent_cards[request.group_id][request.agent_id] = request.agent_card |
| 65 | + return RegisterAgentResponse( |
| 66 | + group_id=request.group_id, |
| 67 | + agent_id=request.agent_id, |
| 68 | + agent_card=self.agent_cards[request.group_id][request.agent_id], |
| 69 | + ) |
| 70 | + |
| 71 | + @self.app.get("/group/{group_id}/agents") |
| 72 | + def agents(group_id: str) -> GetAgentsResponse: |
| 73 | + """Get all agents in a specified group.""" |
| 74 | + if group_id not in self.groups: |
| 75 | + return GetAgentsResponse(err_code=1, msg=f"group {group_id} not exist") |
| 76 | + |
| 77 | + agent_infos = [ |
| 78 | + AgentInformation(agent_id=agent_id, agent_card=agent_card) |
| 79 | + for agent_id, agent_card in self.agent_cards[group_id].items() |
| 80 | + ] |
| 81 | + return GetAgentsResponse(group_id=group_id, agent_infos=agent_infos) |
| 82 | + |
| 83 | + @self.app.get("/group/{group_id}/agent/{agent_id}") |
| 84 | + def agent(group_id: str, agent_id: str) -> GetAgentResponse: |
| 85 | + """Get the agent card of a specified agent in a specified group.""" |
| 86 | + if group_id not in self.groups: |
| 87 | + return GetAgentResponse(err_code=1, msg=f"group {group_id} not exist") |
| 88 | + if agent_id not in self.agent_cards[group_id]: |
| 89 | + return GetAgentResponse( |
| 90 | + err_code=1, |
| 91 | + msg=f"agent {agent_id} in group {group_id} not exist", |
| 92 | + ) |
| 93 | + return GetAgentResponse( |
| 94 | + agent_id=agent_id, |
| 95 | + agent_card=self.agent_cards[group_id][agent_id], |
| 96 | + ) |
| 97 | + |
| 98 | + @self.app.get("/groups") |
| 99 | + def groups() -> GetGroupsResponse: |
| 100 | + """Get all registered groups.""" |
| 101 | + return GetGroupsResponse(group_ids=self.groups) |
| 102 | + |
| 103 | + def serve(self, **kwargs): |
| 104 | + uvicorn.run(self.app, **kwargs) |
0 commit comments