Skip to content

Commit c56bd97

Browse files
authored
feat: support both agent and app in AgentkitAgentServerApp (#52)
2 parents 7ad7afe + 13375d1 commit c56bd97

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

agentkit/apps/agent_server_app/agent_server_app.py

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from google.adk.a2a.utils.agent_to_a2a import to_a2a
2525
from google.adk.agents.base_agent import BaseAgent
2626
from google.adk.agents.run_config import RunConfig, StreamingMode
27+
from google.adk.apps.app import App
2728
from google.adk.artifacts.in_memory_artifact_service import (
2829
InMemoryArtifactService,
2930
)
@@ -42,8 +43,8 @@
4243
from google.genai import types
4344
from opentelemetry import trace
4445
from veadk import Agent
45-
from veadk.memory.short_term_memory import ShortTermMemory
4646
from veadk.runner import Runner
47+
from veadk.memory.short_term_memory import ShortTermMemory
4748

4849
from agentkit.apps.agent_server_app.middleware import (
4950
AgentkitTelemetryHTTPMiddleware,
@@ -55,27 +56,37 @@
5556

5657

5758
class AgentKitAgentLoader(BaseAgentLoader):
58-
def __init__(self, agent: BaseAgent) -> None:
59+
def __init__(self, agent_or_app: BaseAgent | App) -> None:
5960
super().__init__()
6061

61-
self.agent = agent
62+
self.agent_or_app = agent_or_app
63+
if isinstance(agent_or_app, App):
64+
self.root_agent = agent_or_app.root_agent
65+
self.app_name = agent_or_app.name or self.root_agent.name
66+
else:
67+
self.root_agent = agent_or_app
68+
self.app_name = agent_or_app.name
6269

6370
@override
64-
def load_agent(self, agent_name: str) -> BaseAgent:
65-
return self.agent
71+
def load_agent(self, agent_name: str) -> BaseAgent | App:
72+
if agent_name != self.app_name:
73+
raise ValueError(
74+
f"Unknown agent '{agent_name}'. Expected '{self.app_name}'."
75+
)
76+
return self.agent_or_app
6677

6778
@override
6879
def list_agents(self) -> list[str]:
69-
return [self.agent.name]
80+
return [self.app_name]
7081

7182
@override
7283
def list_agents_detailed(self) -> list[dict[str, Any]]:
73-
name = self.agent.name
74-
description = getattr(self.agent, "description", "") or ""
84+
name = self.app_name
85+
description = getattr(self.root_agent, "description", "") or ""
7586
return [
7687
{
7788
"name": name,
78-
"root_agent_name": name,
89+
"root_agent_name": self.root_agent.name,
7990
"description": description,
8091
"language": "python",
8192
}
@@ -85,24 +96,38 @@ def list_agents_detailed(self) -> list[dict[str, Any]]:
8596
class AgentkitAgentServerApp(BaseAgentkitApp):
8697
def __init__(
8798
self,
88-
agent: BaseAgent,
89-
short_term_memory: BaseSessionService | ShortTermMemory,
99+
agent: BaseAgent | App | None = None,
100+
short_term_memory: BaseSessionService | ShortTermMemory | None = None,
101+
*,
102+
app: App | None = None,
90103
) -> None:
91104
super().__init__()
92105

106+
if short_term_memory is None:
107+
raise TypeError("short_term_memory is required.")
108+
109+
if app is not None and agent is not None:
110+
raise TypeError("Only one of 'agent' or 'app' can be provided.")
111+
112+
entry = app if app is not None else agent
113+
if entry is None:
114+
raise TypeError("Either 'agent' or 'app' must be provided.")
115+
116+
root_agent = entry.root_agent if isinstance(entry, App) else entry
117+
93118
_artifact_service = InMemoryArtifactService()
94119
_credential_service = InMemoryCredentialService()
95120

96121
_eval_sets_manager = LocalEvalSetsManager(agents_dir=".")
97122
_eval_set_results_manager = LocalEvalSetResultsManager(agents_dir=".")
98123

99124
self.server = AdkWebServer(
100-
agent_loader=AgentKitAgentLoader(agent),
125+
agent_loader=AgentKitAgentLoader(entry),
101126
session_service=short_term_memory
102127
if isinstance(short_term_memory, BaseSessionService)
103128
else short_term_memory.session_service,
104-
memory_service=agent.long_term_memory
105-
if isinstance(agent, Agent) and agent.long_term_memory
129+
memory_service=root_agent.long_term_memory
130+
if isinstance(root_agent, Agent) and root_agent.long_term_memory
106131
else InMemoryMemoryService(),
107132
artifact_service=_artifact_service,
108133
credential_service=_credential_service,
@@ -111,8 +136,8 @@ def __init__(
111136
agents_dir=".",
112137
)
113138

114-
runner = Runner(agent=agent)
115-
_a2a_server_app = to_a2a(agent=agent, runner=runner)
139+
runner = Runner(agent=root_agent)
140+
_a2a_server_app = to_a2a(agent=root_agent, runner=runner)
116141

117142
@asynccontextmanager
118143
async def lifespan(app: FastAPI):

agentkit/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
VERSION = "0.4.3"
15+
VERSION = "0.4.4"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "agentkit-sdk-python"
3-
version = "0.4.3"
3+
version = "0.4.4"
44
description = "Python SDK for transforming any AI agent into a production-ready application. Framework-agnostic primitives for runtime, memory, authentication, and tools with volcengine-managed infrastructure."
55
readme = "README.md"
66
requires-python = ">=3.10"

0 commit comments

Comments
 (0)