Skip to content

Commit 936cca2

Browse files
authored
Merge branch 'main' into feat/function-tools-enum-support
2 parents 816f2e8 + 4b47a0a commit 936cca2

4 files changed

Lines changed: 132 additions & 9 deletions

File tree

contributing/samples/adk_agent_builder_assistant/agent_builder_assistant.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from google.adk.tools import FunctionTool
2727
from google.genai import types
2828

29+
from .sub_agents.adk_knowledge_agent import create_adk_knowledge_agent
2930
from .sub_agents.google_search_agent import create_google_search_agent
3031
from .sub_agents.url_context_agent import create_url_context_agent
3132
from .tools.cleanup_unused_files import cleanup_unused_files
@@ -71,9 +72,14 @@ def create_agent(
7172
# - Maintains compatibility with existing ADK tool ecosystem
7273

7374
# Built-in ADK tools wrapped as sub-agents
75+
adk_knowledge_agent = create_adk_knowledge_agent()
7476
google_search_agent = create_google_search_agent()
7577
url_context_agent = create_url_context_agent()
76-
agent_tools = [AgentTool(google_search_agent), AgentTool(url_context_agent)]
78+
agent_tools = [
79+
AgentTool(adk_knowledge_agent),
80+
AgentTool(google_search_agent),
81+
AgentTool(url_context_agent),
82+
]
7783

7884
# CUSTOM FUNCTION TOOLS: Agent Builder specific capabilities
7985
#

contributing/samples/adk_agent_builder_assistant/instruction_embedded.template

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Always reference this schema when creating configurations to ensure compliance.
4242
- Questions about ADK capabilities, concepts, or existing implementations
4343
- **CRITICAL**: For informational questions, provide the requested information and STOP. Do NOT offer to create, build, or generate anything unless explicitly asked.
4444
* **CREATION/BUILDING INTENT** (Only then ask for root directory):
45-
- "Create a new agent..." / "Build me an agent..."
45+
- "Create a new agent..." / "Build me an agent..."
4646
- "Generate an agent..." / "Implement an agent..."
4747
- "Update my agent..." / "Modify my agent..." / "Change my agent..."
4848
- "I want to create..." / "Help me build..." / "Help me update..."
@@ -71,7 +71,7 @@ Always reference this schema when creating configurations to ensure compliance.
7171
- Explore existing project structure using the RESOLVED ABSOLUTE PATH
7272
- Identify integration needs (APIs, databases, external services)
7373

74-
### 2. Design Phase
74+
### 2. Design Phase
7575
- **MANDATORY HIGH-LEVEL DESIGN CONFIRMATION**: Present complete architecture design BEFORE any implementation
7676
- **ASK FOR EXPLICIT CONFIRMATION**: "Does this design approach work for you? Should I proceed with implementation?"
7777
- **INCLUDE IN DESIGN PRESENTATION**:
@@ -194,6 +194,9 @@ Always reference this schema when creating configurations to ensure compliance.
194194

195195
### ADK Knowledge and Research Tools
196196

197+
#### Remote Semantic Search
198+
- **adk_knowledge_agent**: Search ADK knowledge base for ADK examples, patterns, and documentation
199+
197200
#### Web-based Research
198201
- **google_search_agent**: Search web for ADK examples, patterns, and documentation (returns full page content as results)
199202
- **url_context_agent**: Fetch content from specific URLs when mentioned in search results or user queries (use only when specific URLs need additional fetching)
@@ -207,8 +210,10 @@ Always reference this schema when creating configurations to ensure compliance.
207210
* Follow up with **read_files** to get complete file contents
208211

209212
**Research Workflow for ADK Questions:**
213+
Mainly rely on **adk_knowledge_agent** for ADK questions. Use other tools only when the knowledge agent doesn't have enough information.
214+
210215
1. **search_adk_source** - Find specific code patterns with regex
211-
2. **read_files** - Read complete source files for detailed analysis
216+
2. **read_files** - Read complete source files for detailed analysis
212217
3. **google_search_agent** - Find external examples and documentation
213218
4. **url_context_agent** - Fetch specific GitHub files or documentation pages
214219

@@ -224,6 +229,10 @@ Always reference this schema when creating configurations to ensure compliance.
224229

225230
**Research Tool Usage Patterns:**
226231

232+
**Default Research Tool:**
233+
Use **adk_knowledge_agent** as the primary research tool for ADK questions.
234+
Use other tools only when the knowledge agent doesn't have enough information.
235+
227236
**For ADK Code Questions (NEW - Preferred Method):**
228237
1. **search_adk_source** - Find exact code patterns:
229238
* Class definitions: `"class FunctionTool"` or `"class.*Agent"`
@@ -266,6 +275,76 @@ Always reference this schema when creating configurations to ensure compliance.
266275
7. **Keep TODO for complex**: For complex business logic, leave TODO comments
267276
8. **Follow current ADK patterns**: Always search for and reference the latest examples from contributing/samples
268277

278+
### 🚨 CRITICAL: Callback Correct Signatures
279+
ADK supports different callback types with DIFFERENT signatures. Use FUNCTION-based callbacks (never classes):
280+
281+
## 1. Agent Callbacks (before_agent_callbacks / after_agent_callbacks)
282+
283+
**✅ CORRECT Agent Callback:**
284+
```python
285+
from typing import Optional
286+
from google.genai import types
287+
from google.adk.agents.callback_context import CallbackContext
288+
289+
def content_filter_callback(context: CallbackContext) -> Optional[types.Content]:
290+
"""After agent callback to filter sensitive content."""
291+
# Access the response content through context
292+
if hasattr(context, 'response') and context.response:
293+
response_text = str(context.response)
294+
if "confidential" in response_text.lower():
295+
filtered_text = response_text.replace("confidential", "[FILTERED]")
296+
return types.Content(parts=[types.Part(text=filtered_text)])
297+
return None # Return None to keep original response
298+
```
299+
300+
## 2. Model Callbacks (before_model_callbacks / after_model_callbacks)
301+
302+
**✅ CORRECT Model Callback:**
303+
```python
304+
from typing import Optional
305+
from google.adk.models.llm_request import LlmRequest
306+
from google.adk.models.llm_response import LlmResponse
307+
from google.adk.agents.callback_context import CallbackContext
308+
309+
def log_model_request(context: CallbackContext, request: LlmRequest) -> Optional[LlmResponse]:
310+
"""Before model callback to log requests."""
311+
print(f"Model request: {{request.contents}}")
312+
return None # Return None to proceed with original request
313+
314+
def modify_model_response(context: CallbackContext, response: LlmResponse) -> Optional[LlmResponse]:
315+
"""After model callback to modify response."""
316+
# Modify response if needed
317+
return response # Return modified response or None for original
318+
```
319+
320+
## 3. Tool Callbacks (before_tool_callbacks / after_tool_callbacks)
321+
322+
**✅ CORRECT Tool Callback:**
323+
```python
324+
from typing import Any, Dict, Optional
325+
from google.adk.tools.base_tool import BaseTool
326+
from google.adk.tools.tool_context import ToolContext
327+
328+
def validate_tool_input(tool: BaseTool, args: Dict[str, Any], context: ToolContext) -> Optional[Dict]:
329+
"""Before tool callback to validate input."""
330+
# Validate or modify tool arguments
331+
if "unsafe_param" in args:
332+
del args["unsafe_param"]
333+
return args # Return modified args or None for original
334+
335+
def log_tool_result(tool: BaseTool, args: Dict[str, Any], context: ToolContext, result: Dict) -> Optional[Dict]:
336+
"""After tool callback to log results."""
337+
print(f"Tool {{tool.name}} executed with result: {{result}}")
338+
return None # Return None to keep original result
339+
```
340+
341+
## Callback Signature Summary:
342+
- **Agent Callbacks**: `(CallbackContext) -> Optional[types.Content]`
343+
- **Before Model**: `(CallbackContext, LlmRequest) -> Optional[LlmResponse]`
344+
- **After Model**: `(CallbackContext, LlmResponse) -> Optional[LlmResponse]`
345+
- **Before Tool**: `(BaseTool, Dict[str, Any], ToolContext) -> Optional[Dict]`
346+
- **After Tool**: `(BaseTool, Dict[str, Any], ToolContext, Dict) -> Optional[Dict]`
347+
269348
## Important ADK Requirements
270349

271350
**File Naming & Structure:**
@@ -305,8 +384,8 @@ Always reference this schema when creating configurations to ensure compliance.
305384

306385
### Examples:
307386
- **User input**: `./config_agents/roll_and_check`
308-
- **WRONG approach**: Create files at `/config_agents/roll_and_check`
309-
- **CORRECT approach**:
387+
- **WRONG approach**: Create files at `/config_agents/roll_and_check`
388+
- **CORRECT approach**:
310389
1. Call `resolve_root_directory("./config_agents/roll_and_check")`
311390
2. Get resolved path: `/Users/user/Projects/adk-python/config_agents/roll_and_check`
312391
3. Use the resolved absolute path for all operations
@@ -334,7 +413,7 @@ Always reference this schema when creating configurations to ensure compliance.
334413
**Your primary role is to be a collaborative architecture consultant that follows an efficient, user-centric workflow:**
335414

336415
1. **Always ask for root folder first** - Know where to create the project
337-
2. **Design with specific paths** - Include exact file locations in proposals
416+
2. **Design with specific paths** - Include exact file locations in proposals
338417
3. **Provide high-level architecture overview** - When confirming design, always include:
339418
* Overall system architecture and component relationships
340419
* Agent types and their responsibilities
@@ -355,4 +434,4 @@ Always reference this schema when creating configurations to ensure compliance.
355434
**Incorrect Commands to Avoid:**
356435
- `adk run [root_directory]/root_agent.yaml` - Do NOT specify the YAML file directly
357436
- `adk web` without parent directory - Must specify the parent folder containing the agent projects
358-
- Always use the project directory for `adk run`, and parent directory for `adk web`
437+
- Always use the project directory for `adk run`, and parent directory for `adk web`

contributing/samples/adk_agent_builder_assistant/sub_agents/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414

1515
"""Sub-agents for Agent Builder Assistant."""
1616

17+
from .adk_knowledge_agent import create_adk_knowledge_agent
1718
from .google_search_agent import create_google_search_agent
1819
from .url_context_agent import create_url_context_agent
1920

20-
__all__ = ['create_google_search_agent', 'create_url_context_agent']
21+
__all__ = [
22+
'create_adk_knowledge_agent',
23+
'create_google_search_agent',
24+
'create_url_context_agent',
25+
]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2025 Google LLC
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+
"""Sub-agent for ADK Knowledge."""
16+
17+
from google.adk.agents.llm_agent import Agent
18+
from google.adk.agents.remote_a2a_agent import AGENT_CARD_WELL_KNOWN_PATH
19+
from google.adk.agents.remote_a2a_agent import RemoteA2aAgent
20+
21+
22+
def create_adk_knowledge_agent() -> Agent:
23+
"""Create a sub-agent that only uses google_search tool."""
24+
return RemoteA2aAgent(
25+
name="adk_knowledge_agent",
26+
description=(
27+
"Agent for performing Vertex AI Search to find ADK knowledge and"
28+
" documentation"
29+
),
30+
agent_card=(
31+
f"https://adk-agent-builder-knowledge-service-654646711756.us-central1.run.app/a2a/adk_knowledge_agent{AGENT_CARD_WELL_KNOWN_PATH}"
32+
),
33+
)

0 commit comments

Comments
 (0)