Skip to content

Commit d760037

Browse files
xuanyang15copybara-github
authored andcommitted
feat: Add support for excluding predefined functions in ComputerUseToolset
Co-authored-by: Xuan Yang <xygoogle@google.com> PiperOrigin-RevId: 904517817
1 parent 62d7ee0 commit d760037

2 files changed

Lines changed: 70 additions & 2 deletions

File tree

src/google/adk/tools/computer_use/computer_use_toolset.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ def __init__(
4848
self,
4949
*,
5050
computer: BaseComputer,
51+
excluded_predefined_functions: Optional[list[str]] = None,
5152
):
5253
super().__init__()
5354
self._computer = computer
55+
self._excluded_predefined_functions = excluded_predefined_functions
5456
self._initialized = False
5557
self._tools = None
5658

@@ -203,6 +205,13 @@ async def get_tools(
203205
if method_name == "session_state":
204206
continue
205207

208+
# Skip methods excluded by configuration
209+
if (
210+
self._excluded_predefined_functions
211+
and method_name in self._excluded_predefined_functions
212+
):
213+
continue
214+
206215
# Check if it's a method defined in Computer class
207216
attr = getattr(BaseComputer, method_name, None)
208217
if attr is not None and callable(attr):
@@ -259,11 +268,18 @@ async def process_llm_request(
259268
types.Environment.ENVIRONMENT_BROWSER,
260269
)
261270
llm_request.config.tools.append(
262-
types.Tool(computer_use=types.ComputerUse(environment=environment))
271+
types.Tool(
272+
computer_use=types.ComputerUse(
273+
environment=environment,
274+
excluded_predefined_functions=self._excluded_predefined_functions,
275+
)
276+
)
263277
)
264278
logger.debug(
265-
"Added computer use tool with environment: %s",
279+
"Added computer use tool with environment: %s,"
280+
" excluded_functions: %s",
266281
environment,
282+
self._excluded_predefined_functions,
267283
)
268284

269285
except Exception as e:

tests/unittests/tools/computer_use/test_computer_use_toolset.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,23 @@ async def test_get_tools_excludes_utility_methods(self, toolset):
202202
for method in expected_methods:
203203
assert method in tool_names
204204

205+
@pytest.mark.asyncio
206+
async def test_get_tools_filters_excluded_functions(self, mock_computer):
207+
"""Test that get_tools filters out excluded functions."""
208+
excluded_funcs = ["drag_and_drop", "key_combination"]
209+
toolset = ComputerUseToolset(
210+
computer=mock_computer,
211+
excluded_predefined_functions=excluded_funcs,
212+
)
213+
214+
tools = await toolset.get_tools()
215+
tool_names = [tool.func.__name__ for tool in tools]
216+
217+
for func in excluded_funcs:
218+
assert func not in tool_names
219+
220+
assert "click_at" in tool_names
221+
205222
@pytest.mark.asyncio
206223
async def test_get_tools_with_readonly_context(self, toolset):
207224
"""Test get_tools with readonly_context parameter."""
@@ -362,6 +379,41 @@ async def test_process_llm_request(self, toolset, mock_computer):
362379
== types.Environment.ENVIRONMENT_BROWSER
363380
)
364381

382+
@pytest.mark.asyncio
383+
async def test_process_llm_request_with_excluded_functions(
384+
self, mock_computer
385+
):
386+
"""Test that process_llm_request passes excluded_predefined_functions."""
387+
excluded_funcs = ["drag_and_drop", "key_combination"]
388+
toolset = ComputerUseToolset(
389+
computer=mock_computer,
390+
excluded_predefined_functions=excluded_funcs,
391+
)
392+
393+
llm_request = LlmRequest(
394+
model="gemini-1.5-flash",
395+
config=types.GenerateContentConfig(),
396+
)
397+
398+
await toolset.process_llm_request(
399+
tool_context=MagicMock(), llm_request=llm_request
400+
)
401+
402+
# Should have computer use tool
403+
computer_use_tools = [
404+
tool
405+
for tool in llm_request.config.tools
406+
if hasattr(tool, "computer_use") and tool.computer_use
407+
]
408+
assert len(computer_use_tools) == 1
409+
410+
# Should have correct excluded functions
411+
computer_use_tool = computer_use_tools[0]
412+
assert (
413+
computer_use_tool.computer_use.excluded_predefined_functions
414+
== excluded_funcs
415+
)
416+
365417
@pytest.mark.asyncio
366418
async def test_process_llm_request_with_existing_computer_use(
367419
self, toolset, mock_computer

0 commit comments

Comments
 (0)