Skip to content

Commit 14cf910

Browse files
seanzhougooglecopybara-github
authored andcommitted
refactor: refactor application integration toolset to hide non-public field
PiperOrigin-RevId: 758469938
1 parent 00e0035 commit 14cf910

File tree

4 files changed

+55
-53
lines changed

4 files changed

+55
-53
lines changed

contributing/samples/application_integration_agent/agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
location=connection_location,
3434
connection=connection_name,
3535
entity_operations={"Issues": [], "Projects": []},
36-
tool_name="jira_issue_manager",
36+
tool_name_prefix="jira_issue_manager",
3737
)
3838

3939
root_agent = LlmAgent(

src/google/adk/tools/application_integration_tool/application_integration_toolset.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def __init__(
8686
actions: Optional[str] = None,
8787
# Optional parameter for the toolset. This is prepended to the generated
8888
# tool/python function name.
89-
tool_name: Optional[str] = "",
89+
tool_name_prefix: Optional[str] = "",
9090
# Optional parameter for the toolset. This is appended to the generated
9191
# tool/python function description.
9292
tool_instructions: Optional[str] = "",
@@ -103,7 +103,7 @@ def __init__(
103103
connection: The connection name.
104104
entity_operations: The entity operations supported by the connection.
105105
actions: The actions supported by the connection.
106-
tool_name: The name of the tool.
106+
tool_name_prefix: The name prefix of the generated tools.
107107
tool_instructions: The instructions for the tool.
108108
service_account_json: The service account configuration as a dictionary.
109109
Required if not using default service credential. Used for fetching
@@ -122,15 +122,15 @@ def __init__(
122122
"""
123123
self.project = project
124124
self.location = location
125-
self.integration = integration
126-
self.triggers = triggers
127-
self.connection = connection
128-
self.entity_operations = entity_operations
129-
self.actions = actions
130-
self.tool_name = tool_name
131-
self.tool_instructions = tool_instructions
132-
self.service_account_json = service_account_json
133-
self._tool_filter = tool_filter
125+
self._integration = integration
126+
self._triggers = triggers
127+
self._connection = connection
128+
self._entity_operations = entity_operations
129+
self._actions = actions
130+
self._tool_name_prefix = tool_name_prefix
131+
self._tool_instructions = tool_instructions
132+
self._service_account_json = service_account_json
133+
self.tool_filter = tool_filter
134134

135135
integration_client = IntegrationClient(
136136
project,
@@ -151,23 +151,23 @@ def __init__(
151151
)
152152
connection_details = connections_client.get_connection_details()
153153
spec = integration_client.get_openapi_spec_for_connection(
154-
tool_name,
154+
tool_name_prefix,
155155
tool_instructions,
156156
)
157157
else:
158158
raise ValueError(
159159
"Invalid request, Either integration or (connection and"
160160
" (entity_operations or actions)) should be provided."
161161
)
162-
self.openapi_toolset = None
163-
self.tool = None
162+
self._openapi_toolset = None
163+
self._tool = None
164164
self._parse_spec_to_toolset(spec, connection_details)
165165

166166
def _parse_spec_to_toolset(self, spec_dict, connection_details):
167167
"""Parses the spec dict to OpenAPI toolset."""
168-
if self.service_account_json:
168+
if self._service_account_json:
169169
sa_credential = ServiceAccountCredential.model_validate_json(
170-
self.service_account_json
170+
self._service_account_json
171171
)
172172
service_account = ServiceAccount(
173173
service_account_credential=sa_credential,
@@ -186,12 +186,12 @@ def _parse_spec_to_toolset(self, spec_dict, connection_details):
186186
)
187187
auth_scheme = HTTPBearer(bearerFormat="JWT")
188188

189-
if self.integration:
190-
self.openapi_toolset = OpenAPIToolset(
189+
if self._integration:
190+
self._openapi_toolset = OpenAPIToolset(
191191
spec_dict=spec_dict,
192192
auth_credential=auth_credential,
193193
auth_scheme=auth_scheme,
194-
tool_filter=self._tool_filter,
194+
tool_filter=self.tool_filter,
195195
)
196196
return
197197

@@ -210,7 +210,7 @@ def _parse_spec_to_toolset(self, spec_dict, connection_details):
210210
rest_api_tool.configure_auth_scheme(auth_scheme)
211211
if auth_credential:
212212
rest_api_tool.configure_auth_credential(auth_credential)
213-
self.tool = IntegrationConnectorTool(
213+
self._tool = IntegrationConnectorTool(
214214
name=rest_api_tool.name,
215215
description=rest_api_tool.description,
216216
connection_name=connection_details["name"],
@@ -224,9 +224,11 @@ def _parse_spec_to_toolset(self, spec_dict, connection_details):
224224

225225
@override
226226
async def get_tools(self) -> List[RestApiTool]:
227-
return [self.tool] if self.tool else await self.openapi_toolset.get_tools()
227+
return (
228+
[self._tool] if self._tool else await self._openapi_toolset.get_tools()
229+
)
228230

229231
@override
230232
async def close(self) -> None:
231-
if self.openapi_toolset:
232-
await self.openapi_toolset.close()
233+
if self._openapi_toolset:
234+
await self._openapi_toolset.close()

src/google/adk/tools/application_integration_tool/integration_connector_tool.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,18 @@ def __init__(
101101
name=name,
102102
description=description,
103103
)
104-
self.connection_name = connection_name
105-
self.connection_host = connection_host
106-
self.connection_service_name = connection_service_name
107-
self.entity = entity
108-
self.operation = operation
109-
self.action = action
110-
self.rest_api_tool = rest_api_tool
104+
self._connection_name = connection_name
105+
self._connection_host = connection_host
106+
self._connection_service_name = connection_service_name
107+
self._entity = entity
108+
self._operation = operation
109+
self._action = action
110+
self._rest_api_tool = rest_api_tool
111111

112112
@override
113113
def _get_declaration(self) -> FunctionDeclaration:
114114
"""Returns the function declaration in the Gemini Schema format."""
115-
schema_dict = self.rest_api_tool._operation_parser.get_json_schema()
115+
schema_dict = self._rest_api_tool._operation_parser.get_json_schema()
116116
for field in self.EXCLUDE_FIELDS:
117117
if field in schema_dict['properties']:
118118
del schema_dict['properties'][field]
@@ -130,30 +130,30 @@ def _get_declaration(self) -> FunctionDeclaration:
130130
async def run_async(
131131
self, *, args: dict[str, Any], tool_context: Optional[ToolContext]
132132
) -> Dict[str, Any]:
133-
args['connection_name'] = self.connection_name
134-
args['service_name'] = self.connection_service_name
135-
args['host'] = self.connection_host
136-
args['entity'] = self.entity
137-
args['operation'] = self.operation
138-
args['action'] = self.action
133+
args['connection_name'] = self._connection_name
134+
args['service_name'] = self._connection_service_name
135+
args['host'] = self._connection_host
136+
args['entity'] = self._entity
137+
args['operation'] = self._operation
138+
args['action'] = self._action
139139
logger.info('Running tool: %s with args: %s', self.name, args)
140-
return self.rest_api_tool.call(args=args, tool_context=tool_context)
140+
return self._rest_api_tool.call(args=args, tool_context=tool_context)
141141

142142
def __str__(self):
143143
return (
144144
f'ApplicationIntegrationTool(name="{self.name}",'
145145
f' description="{self.description}",'
146-
f' connection_name="{self.connection_name}", entity="{self.entity}",'
147-
f' operation="{self.operation}", action="{self.action}")'
146+
f' connection_name="{self._connection_name}", entity="{self._entity}",'
147+
f' operation="{self._operation}", action="{self._action}")'
148148
)
149149

150150
def __repr__(self):
151151
return (
152152
f'ApplicationIntegrationTool(name="{self.name}",'
153153
f' description="{self.description}",'
154-
f' connection_name="{self.connection_name}",'
155-
f' connection_host="{self.connection_host}",'
156-
f' connection_service_name="{self.connection_service_name}",'
157-
f' entity="{self.entity}", operation="{self.operation}",'
158-
f' action="{self.action}", rest_api_tool={repr(self.rest_api_tool)})'
154+
f' connection_name="{self._connection_name}",'
155+
f' connection_host="{self._connection_host}",'
156+
f' connection_service_name="{self._connection_service_name}",'
157+
f' entity="{self._entity}", operation="{self._operation}",'
158+
f' action="{self._action}", rest_api_tool={repr(self._rest_api_tool)})'
159159
)

tests/unittests/tools/application_integration_tool/test_application_integration_toolset.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ async def test_initialization_with_connection_and_entity_operations(
262262
location,
263263
connection=connection_name,
264264
entity_operations=entity_operations_list,
265-
tool_name=tool_name,
265+
tool_name_prefix=tool_name,
266266
tool_instructions=tool_instructions,
267267
)
268268
mock_integration_client.assert_called_once_with(
@@ -289,8 +289,8 @@ async def test_initialization_with_connection_and_entity_operations(
289289
assert len(tools) == 1
290290
assert tools[0].name == "list_issues"
291291
assert isinstance(tools[0], IntegrationConnectorTool)
292-
assert tools[0].entity == "Issues"
293-
assert tools[0].operation == "LIST_ENTITIES"
292+
assert tools[0]._entity == "Issues"
293+
assert tools[0]._operation == "LIST_ENTITIES"
294294

295295

296296
@pytest.mark.asyncio
@@ -314,7 +314,7 @@ async def test_initialization_with_connection_and_actions(
314314
location,
315315
connection=connection_name,
316316
actions=actions_list,
317-
tool_name=tool_name,
317+
tool_name_prefix=tool_name,
318318
tool_instructions=tool_instructions,
319319
)
320320
mock_integration_client.assert_called_once_with(
@@ -332,8 +332,8 @@ async def test_initialization_with_connection_and_actions(
332332
assert len(tools) == 1
333333
assert tools[0].name == "list_issues_operation"
334334
assert isinstance(tools[0], IntegrationConnectorTool)
335-
assert tools[0].action == "CustomAction"
336-
assert tools[0].operation == "EXECUTE_ACTION"
335+
assert tools[0]._action == "CustomAction"
336+
assert tools[0]._operation == "EXECUTE_ACTION"
337337

338338

339339
def test_initialization_without_required_params(project, location):
@@ -467,7 +467,7 @@ def test_initialization_with_connection_details(
467467
location,
468468
connection=connection_name,
469469
entity_operations=entity_operations_list,
470-
tool_name=tool_name,
470+
tool_name_prefix=tool_name,
471471
tool_instructions=tool_instructions,
472472
)
473473
mock_integration_client.return_value.get_openapi_spec_for_connection.assert_called_once_with(

0 commit comments

Comments
 (0)