Files
adk-python/src/google/adk/tools/agent_tool.py
T

203 lines
6.6 KiB
Python
Raw Normal View History

2025-04-08 17:22:09 +00:00
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from typing import Any
from typing import TYPE_CHECKING
from google.genai import types
from pydantic import model_validator
from typing_extensions import override
2025-05-21 23:39:57 -07:00
from . import _automatic_function_calling_util
from ..agents.common_configs import AgentRefConfig
2025-04-08 17:22:09 +00:00
from ..memory.in_memory_memory_service import InMemoryMemoryService
from ..utils.context_utils import Aclosing
from ._forwarding_artifact_service import ForwardingArtifactService
2025-04-08 17:22:09 +00:00
from .base_tool import BaseTool
from .tool_configs import BaseToolConfig
from .tool_configs import ToolArgsConfig
2025-04-08 17:22:09 +00:00
from .tool_context import ToolContext
if TYPE_CHECKING:
from ..agents.base_agent import BaseAgent
class AgentTool(BaseTool):
"""A tool that wraps an agent.
This tool allows an agent to be called as a tool within a larger application.
The agent's input schema is used to define the tool's input parameters, and
the agent's output is returned as the tool's result.
Attributes:
agent: The agent to wrap.
skip_summarization: Whether to skip summarization of the agent output.
"""
2025-04-21 23:23:57 -07:00
def __init__(self, agent: BaseAgent, skip_summarization: bool = False):
2025-04-08 17:22:09 +00:00
self.agent = agent
2025-04-21 23:23:57 -07:00
self.skip_summarization: bool = skip_summarization
2025-04-08 17:22:09 +00:00
super().__init__(name=agent.name, description=agent.description)
@model_validator(mode='before')
@classmethod
def populate_name(cls, data: Any) -> Any:
data['name'] = data['agent'].name
return data
@override
2025-08-12 22:28:43 -07:00
def _get_declaration(self) -> types.FunctionDeclaration:
2025-04-08 17:22:09 +00:00
from ..agents.llm_agent import LlmAgent
from ..utils.variant_utils import GoogleLLMVariant
2025-04-08 17:22:09 +00:00
if isinstance(self.agent, LlmAgent) and self.agent.input_schema:
result = _automatic_function_calling_util.build_function_declaration(
func=self.agent.input_schema, variant=self._api_variant
)
else:
result = types.FunctionDeclaration(
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
'request': types.Schema(
type=types.Type.STRING,
),
},
required=['request'],
),
description=self.agent.description,
name=self.name,
)
# Set response schema for non-GEMINI_API variants
if self._api_variant != GoogleLLMVariant.GEMINI_API:
# Determine response type based on agent's output schema
if isinstance(self.agent, LlmAgent) and self.agent.output_schema:
# Agent has structured output schema - response is an object
result.response = types.Schema(type=types.Type.OBJECT)
else:
# Agent returns text - response is a string
result.response = types.Schema(type=types.Type.STRING)
2025-04-08 17:22:09 +00:00
result.name = self.name
return result
@override
async def run_async(
self,
*,
args: dict[str, Any],
tool_context: ToolContext,
) -> Any:
from ..agents.llm_agent import LlmAgent
from ..runners import Runner
from ..sessions.in_memory_session_service import InMemorySessionService
2025-04-08 17:22:09 +00:00
if self.skip_summarization:
tool_context.actions.skip_summarization = True
if isinstance(self.agent, LlmAgent) and self.agent.input_schema:
input_value = self.agent.input_schema.model_validate(args)
content = types.Content(
role='user',
parts=[
types.Part.from_text(
text=input_value.model_dump_json(exclude_none=True)
)
],
)
else:
content = types.Content(
role='user',
2025-06-13 10:09:53 -07:00
parts=[types.Part.from_text(text=args['request'])],
2025-04-08 17:22:09 +00:00
)
2025-10-21 11:29:43 -07:00
invocation_context = tool_context._invocation_context
parent_app_name = (
invocation_context.app_name if invocation_context else None
)
child_app_name = parent_app_name or self.agent.name
2025-04-08 17:22:09 +00:00
runner = Runner(
2025-10-21 11:29:43 -07:00
app_name=child_app_name,
2025-04-08 17:22:09 +00:00
agent=self.agent,
artifact_service=ForwardingArtifactService(tool_context),
2025-04-08 17:22:09 +00:00
session_service=InMemorySessionService(),
memory_service=InMemoryMemoryService(),
2025-07-11 15:34:07 -07:00
credential_service=tool_context._invocation_context.credential_service,
plugins=list(tool_context._invocation_context.plugin_manager.plugins),
2025-04-08 17:22:09 +00:00
)
state_dict = {
k: v
for k, v in tool_context.state.to_dict().items()
if not k.startswith('_adk') # Filter out adk internal states
}
2025-05-15 12:46:12 -07:00
session = await runner.session_service.create_session(
2025-10-21 11:29:43 -07:00
app_name=child_app_name,
user_id=tool_context._invocation_context.user_id,
state=state_dict,
2025-04-08 17:22:09 +00:00
)
last_content = None
async with Aclosing(
runner.run_async(
user_id=session.user_id, session_id=session.id, new_message=content
)
) as agen:
async for event in agen:
# Forward state delta to parent session.
if event.actions.state_delta:
tool_context.state.update(event.actions.state_delta)
if event.content:
last_content = event.content
2025-04-08 17:22:09 +00:00
if not last_content:
2025-04-08 17:22:09 +00:00
return ''
merged_text = '\n'.join(p.text for p in last_content.parts if p.text)
2025-04-08 17:22:09 +00:00
if isinstance(self.agent, LlmAgent) and self.agent.output_schema:
tool_result = self.agent.output_schema.model_validate_json(
merged_text
2025-04-08 17:22:09 +00:00
).model_dump(exclude_none=True)
else:
2025-06-13 10:09:53 -07:00
tool_result = merged_text
2025-04-08 17:22:09 +00:00
return tool_result
@override
2025-08-21 15:48:51 -07:00
@classmethod
def from_config(
cls, config: ToolArgsConfig, config_abs_path: str
) -> AgentTool:
from ..agents import config_agent_utils
agent_tool_config = AgentToolConfig.model_validate(config.model_dump())
agent = config_agent_utils.resolve_agent_reference(
agent_tool_config.agent, config_abs_path
)
return cls(
agent=agent, skip_summarization=agent_tool_config.skip_summarization
)
class AgentToolConfig(BaseToolConfig):
"""The config for the AgentTool."""
agent: AgentRefConfig
"""The reference to the agent instance."""
skip_summarization: bool = False
"""Whether to skip summarization of the agent output."""