google-adk 0.0.3__py3-none-any.whl → 0.0.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. google/adk/agents/run_config.py +4 -0
  2. google/adk/auth/auth_preprocessor.py +19 -16
  3. google/adk/cli/browser/index.html +1 -1
  4. google/adk/cli/browser/{main-SY2WYYGV.js → main-SLIAU2JL.js} +46 -30
  5. google/adk/cli/cli.py +8 -8
  6. google/adk/cli/cli_deploy.py +2 -4
  7. google/adk/cli/cli_tools_click.py +6 -6
  8. google/adk/flows/llm_flows/base_llm_flow.py +1 -1
  9. google/adk/flows/llm_flows/contents.py +21 -1
  10. google/adk/flows/llm_flows/functions.py +3 -1
  11. google/adk/models/google_llm.py +0 -1
  12. google/adk/runners.py +13 -2
  13. google/adk/version.py +1 -1
  14. {google_adk-0.0.3.dist-info → google_adk-0.0.5.dist-info}/METADATA +4 -2
  15. google_adk-0.0.5.dist-info/RECORD +175 -0
  16. {google_adk-0.0.3.dist-info → google_adk-0.0.5.dist-info}/WHEEL +1 -1
  17. google_adk-0.0.5.dist-info/licenses/LICENSE +202 -0
  18. google/adk/._version.py +0 -0
  19. google/adk/docs/Makefile +0 -20
  20. google/adk/docs/build/doctrees/google-adk.doctree +0 -0
  21. google/adk/docs/build/html/_sources/google-adk.rst.txt +0 -98
  22. google/adk/docs/build/html/_sources/index.rst.txt +0 -7
  23. google/adk/docs/build/html/_static/autodoc_pydantic.css +0 -27
  24. google/adk/docs/build/html/_static/basic.css +0 -925
  25. google/adk/docs/build/html/_static/debug.css +0 -85
  26. google/adk/docs/build/html/_static/doctools.js +0 -156
  27. google/adk/docs/build/html/_static/documentation_options.js +0 -29
  28. google/adk/docs/build/html/_static/file.png +0 -0
  29. google/adk/docs/build/html/_static/language_data.js +0 -199
  30. google/adk/docs/build/html/_static/minus.png +0 -0
  31. google/adk/docs/build/html/_static/plus.png +0 -0
  32. google/adk/docs/build/html/_static/pygments.css +0 -274
  33. google/adk/docs/build/html/_static/scripts/furo-extensions.js +0 -16
  34. google/adk/docs/build/html/_static/scripts/furo.js +0 -19
  35. google/adk/docs/build/html/_static/scripts/furo.js.LICENSE.txt +0 -7
  36. google/adk/docs/build/html/_static/scripts/furo.js.map +0 -1
  37. google/adk/docs/build/html/_static/searchtools.js +0 -620
  38. google/adk/docs/build/html/_static/skeleton.css +0 -312
  39. google/adk/docs/build/html/_static/sphinx_highlight.js +0 -170
  40. google/adk/docs/build/html/_static/styles/furo-extensions.css +0 -18
  41. google/adk/docs/build/html/_static/styles/furo-extensions.css.map +0 -1
  42. google/adk/docs/build/html/_static/styles/furo.css +0 -18
  43. google/adk/docs/build/html/_static/styles/furo.css.map +0 -1
  44. google/adk/docs/build/html/genindex.html +0 -861
  45. google/adk/docs/build/html/google-adk.html +0 -5461
  46. google/adk/docs/build/html/index.html +0 -567
  47. google/adk/docs/build/html/objects.inv +0 -0
  48. google/adk/docs/build/html/py-modindex.html +0 -373
  49. google/adk/docs/build/html/search.html +0 -333
  50. google/adk/docs/build/html/searchindex.js +0 -17
  51. google/adk/docs/source/conf.py +0 -133
  52. google/adk/docs/source/google-adk.rst +0 -98
  53. google/adk/docs/source/index.rst +0 -7
  54. google/adk/tests/__init__.py +0 -14
  55. google/adk/tests/integration/.env.example +0 -10
  56. google/adk/tests/integration/__init__.py +0 -18
  57. google/adk/tests/integration/conftest.py +0 -119
  58. google/adk/tests/integration/fixture/__init__.py +0 -14
  59. google/adk/tests/integration/fixture/agent_with_config/__init__.py +0 -15
  60. google/adk/tests/integration/fixture/agent_with_config/agent.py +0 -88
  61. google/adk/tests/integration/fixture/callback_agent/__init__.py +0 -15
  62. google/adk/tests/integration/fixture/callback_agent/agent.py +0 -105
  63. google/adk/tests/integration/fixture/context_update_test/OWNERS +0 -1
  64. google/adk/tests/integration/fixture/context_update_test/__init__.py +0 -15
  65. google/adk/tests/integration/fixture/context_update_test/agent.py +0 -43
  66. google/adk/tests/integration/fixture/context_update_test/successful_test.session.json +0 -582
  67. google/adk/tests/integration/fixture/context_variable_agent/__init__.py +0 -15
  68. google/adk/tests/integration/fixture/context_variable_agent/agent.py +0 -115
  69. google/adk/tests/integration/fixture/customer_support_ma/__init__.py +0 -15
  70. google/adk/tests/integration/fixture/customer_support_ma/agent.py +0 -172
  71. google/adk/tests/integration/fixture/ecommerce_customer_service_agent/__init__.py +0 -15
  72. google/adk/tests/integration/fixture/ecommerce_customer_service_agent/agent.py +0 -338
  73. google/adk/tests/integration/fixture/ecommerce_customer_service_agent/order_query.test.json +0 -69
  74. google/adk/tests/integration/fixture/ecommerce_customer_service_agent/test_config.json +0 -6
  75. google/adk/tests/integration/fixture/flow_complex_spark/__init__.py +0 -15
  76. google/adk/tests/integration/fixture/flow_complex_spark/agent.py +0 -182
  77. google/adk/tests/integration/fixture/flow_complex_spark/sample.session.json +0 -190
  78. google/adk/tests/integration/fixture/hello_world_agent/__init__.py +0 -15
  79. google/adk/tests/integration/fixture/hello_world_agent/agent.py +0 -95
  80. google/adk/tests/integration/fixture/hello_world_agent/roll_die.test.json +0 -24
  81. google/adk/tests/integration/fixture/hello_world_agent/test_config.json +0 -6
  82. google/adk/tests/integration/fixture/home_automation_agent/__init__.py +0 -15
  83. google/adk/tests/integration/fixture/home_automation_agent/agent.py +0 -304
  84. google/adk/tests/integration/fixture/home_automation_agent/simple_test.test.json +0 -5
  85. google/adk/tests/integration/fixture/home_automation_agent/simple_test2.test.json +0 -5
  86. google/adk/tests/integration/fixture/home_automation_agent/test_config.json +0 -5
  87. google/adk/tests/integration/fixture/home_automation_agent/test_files/dependent_tool_calls.test.json +0 -18
  88. google/adk/tests/integration/fixture/home_automation_agent/test_files/memorizing_past_events/eval_data.test.json +0 -17
  89. google/adk/tests/integration/fixture/home_automation_agent/test_files/memorizing_past_events/test_config.json +0 -6
  90. google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_multi_turn_conversation.test.json +0 -18
  91. google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_test.test.json +0 -17
  92. google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_test2.test.json +0 -5
  93. google/adk/tests/integration/fixture/home_automation_agent/test_files/test_config.json +0 -5
  94. google/adk/tests/integration/fixture/tool_agent/__init__.py +0 -15
  95. google/adk/tests/integration/fixture/tool_agent/agent.py +0 -218
  96. google/adk/tests/integration/fixture/tool_agent/files/Agent_test_plan.pdf +0 -0
  97. google/adk/tests/integration/fixture/trip_planner_agent/__init__.py +0 -15
  98. google/adk/tests/integration/fixture/trip_planner_agent/agent.py +0 -110
  99. google/adk/tests/integration/fixture/trip_planner_agent/initial.session.json +0 -13
  100. google/adk/tests/integration/fixture/trip_planner_agent/test_config.json +0 -5
  101. google/adk/tests/integration/fixture/trip_planner_agent/test_files/initial.session.json +0 -13
  102. google/adk/tests/integration/fixture/trip_planner_agent/test_files/test_config.json +0 -5
  103. google/adk/tests/integration/fixture/trip_planner_agent/test_files/trip_inquiry_sub_agent.test.json +0 -7
  104. google/adk/tests/integration/fixture/trip_planner_agent/trip_inquiry.test.json +0 -19
  105. google/adk/tests/integration/models/__init__.py +0 -14
  106. google/adk/tests/integration/models/test_google_llm.py +0 -65
  107. google/adk/tests/integration/test_callback.py +0 -70
  108. google/adk/tests/integration/test_context_variable.py +0 -67
  109. google/adk/tests/integration/test_evalute_agent_in_fixture.py +0 -76
  110. google/adk/tests/integration/test_multi_agent.py +0 -28
  111. google/adk/tests/integration/test_multi_turn.py +0 -42
  112. google/adk/tests/integration/test_single_agent.py +0 -23
  113. google/adk/tests/integration/test_sub_agent.py +0 -26
  114. google/adk/tests/integration/test_system_instruction.py +0 -177
  115. google/adk/tests/integration/test_tools.py +0 -287
  116. google/adk/tests/integration/test_with_test_file.py +0 -34
  117. google/adk/tests/integration/tools/__init__.py +0 -14
  118. google/adk/tests/integration/utils/__init__.py +0 -16
  119. google/adk/tests/integration/utils/asserts.py +0 -75
  120. google/adk/tests/integration/utils/test_runner.py +0 -97
  121. google/adk/tests/unittests/__init__.py +0 -14
  122. google/adk/tests/unittests/agents/__init__.py +0 -14
  123. google/adk/tests/unittests/agents/test_base_agent.py +0 -407
  124. google/adk/tests/unittests/agents/test_langgraph_agent.py +0 -191
  125. google/adk/tests/unittests/agents/test_llm_agent_callbacks.py +0 -138
  126. google/adk/tests/unittests/agents/test_llm_agent_fields.py +0 -231
  127. google/adk/tests/unittests/agents/test_loop_agent.py +0 -136
  128. google/adk/tests/unittests/agents/test_parallel_agent.py +0 -92
  129. google/adk/tests/unittests/agents/test_sequential_agent.py +0 -114
  130. google/adk/tests/unittests/artifacts/__init__.py +0 -14
  131. google/adk/tests/unittests/artifacts/test_artifact_service.py +0 -276
  132. google/adk/tests/unittests/auth/test_auth_handler.py +0 -575
  133. google/adk/tests/unittests/conftest.py +0 -73
  134. google/adk/tests/unittests/fast_api/__init__.py +0 -14
  135. google/adk/tests/unittests/fast_api/test_fast_api.py +0 -269
  136. google/adk/tests/unittests/flows/__init__.py +0 -14
  137. google/adk/tests/unittests/flows/llm_flows/__init__.py +0 -14
  138. google/adk/tests/unittests/flows/llm_flows/_test_examples.py +0 -142
  139. google/adk/tests/unittests/flows/llm_flows/test_agent_transfer.py +0 -311
  140. google/adk/tests/unittests/flows/llm_flows/test_functions_long_running.py +0 -244
  141. google/adk/tests/unittests/flows/llm_flows/test_functions_request_euc.py +0 -346
  142. google/adk/tests/unittests/flows/llm_flows/test_functions_sequential.py +0 -93
  143. google/adk/tests/unittests/flows/llm_flows/test_functions_simple.py +0 -258
  144. google/adk/tests/unittests/flows/llm_flows/test_identity.py +0 -66
  145. google/adk/tests/unittests/flows/llm_flows/test_instructions.py +0 -164
  146. google/adk/tests/unittests/flows/llm_flows/test_model_callbacks.py +0 -142
  147. google/adk/tests/unittests/flows/llm_flows/test_other_configs.py +0 -46
  148. google/adk/tests/unittests/flows/llm_flows/test_tool_callbacks.py +0 -269
  149. google/adk/tests/unittests/models/__init__.py +0 -14
  150. google/adk/tests/unittests/models/test_google_llm.py +0 -224
  151. google/adk/tests/unittests/models/test_litellm.py +0 -804
  152. google/adk/tests/unittests/models/test_models.py +0 -60
  153. google/adk/tests/unittests/sessions/__init__.py +0 -14
  154. google/adk/tests/unittests/sessions/test_session_service.py +0 -227
  155. google/adk/tests/unittests/sessions/test_vertex_ai_session_service.py +0 -246
  156. google/adk/tests/unittests/streaming/__init__.py +0 -14
  157. google/adk/tests/unittests/streaming/test_streaming.py +0 -50
  158. google/adk/tests/unittests/tools/__init__.py +0 -14
  159. google/adk/tests/unittests/tools/apihub_tool/clients/test_apihub_client.py +0 -499
  160. google/adk/tests/unittests/tools/apihub_tool/test_apihub_toolset.py +0 -204
  161. google/adk/tests/unittests/tools/application_integration_tool/clients/test_connections_client.py +0 -600
  162. google/adk/tests/unittests/tools/application_integration_tool/clients/test_integration_client.py +0 -630
  163. google/adk/tests/unittests/tools/application_integration_tool/test_application_integration_toolset.py +0 -345
  164. google/adk/tests/unittests/tools/google_api_tool/__init__.py +0 -13
  165. google/adk/tests/unittests/tools/google_api_tool/test_googleapi_to_openapi_converter.py +0 -657
  166. google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_auto_auth_credential_exchanger.py +0 -145
  167. google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_base_auth_credential_exchanger.py +0 -68
  168. google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_oauth2_exchanger.py +0 -153
  169. google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_service_account_exchanger.py +0 -196
  170. google/adk/tests/unittests/tools/openapi_tool/auth/test_auth_helper.py +0 -573
  171. google/adk/tests/unittests/tools/openapi_tool/common/test_common.py +0 -436
  172. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test.yaml +0 -1367
  173. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py +0 -628
  174. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_toolset.py +0 -139
  175. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_operation_parser.py +0 -406
  176. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_rest_api_tool.py +0 -966
  177. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_tool_auth_handler.py +0 -201
  178. google/adk/tests/unittests/tools/retrieval/__init__.py +0 -14
  179. google/adk/tests/unittests/tools/retrieval/test_vertex_ai_rag_retrieval.py +0 -147
  180. google/adk/tests/unittests/tools/test_agent_tool.py +0 -167
  181. google/adk/tests/unittests/tools/test_base_tool.py +0 -141
  182. google/adk/tests/unittests/tools/test_build_function_declaration.py +0 -277
  183. google/adk/tests/unittests/utils.py +0 -304
  184. google_adk-0.0.3.dist-info/RECORD +0 -340
  185. {google_adk-0.0.3.dist-info → google_adk-0.0.5.dist-info}/entry_points.txt +0 -0
@@ -1,346 +0,0 @@
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
- from typing import Any
16
- from typing import Optional
17
-
18
- from fastapi.openapi.models import OAuth2
19
- from fastapi.openapi.models import OAuthFlowAuthorizationCode
20
- from fastapi.openapi.models import OAuthFlows
21
- from google.adk.agents import Agent
22
- from google.adk.auth import AuthConfig
23
- from google.adk.auth import AuthCredential
24
- from google.adk.auth import AuthCredentialTypes
25
- from google.adk.auth import OAuth2Auth
26
- from google.adk.flows.llm_flows import functions
27
- from google.adk.tools import AuthToolArguments
28
- from google.adk.tools import ToolContext
29
- from google.genai import types
30
-
31
- from ... import utils
32
-
33
-
34
- def function_call(function_call_id, name, args: dict[str, Any]) -> types.Part:
35
- part = types.Part.from_function_call(name=name, args=args)
36
- part.function_call.id = function_call_id
37
- return part
38
-
39
-
40
- def test_function_request_euc():
41
- responses = [
42
- [
43
- types.Part.from_function_call(name='call_external_api1', args={}),
44
- types.Part.from_function_call(name='call_external_api2', args={}),
45
- ],
46
- [
47
- types.Part.from_text(text='response1'),
48
- ],
49
- ]
50
-
51
- auth_config1 = AuthConfig(
52
- auth_scheme=OAuth2(
53
- flows=OAuthFlows(
54
- authorizationCode=OAuthFlowAuthorizationCode(
55
- authorizationUrl='https://accounts.google.com/o/oauth2/auth',
56
- tokenUrl='https://oauth2.googleapis.com/token',
57
- scopes={
58
- 'https://www.googleapis.com/auth/calendar': (
59
- 'See, edit, share, and permanently delete all the'
60
- ' calendars you can access using Google Calendar'
61
- )
62
- },
63
- )
64
- )
65
- ),
66
- raw_auth_credential=AuthCredential(
67
- auth_type=AuthCredentialTypes.OAUTH2,
68
- oauth2=OAuth2Auth(
69
- client_id='oauth_client_id_1',
70
- client_secret='oauth_client_secret1',
71
- ),
72
- ),
73
- )
74
- auth_config2 = AuthConfig(
75
- auth_scheme=OAuth2(
76
- flows=OAuthFlows(
77
- authorizationCode=OAuthFlowAuthorizationCode(
78
- authorizationUrl='https://accounts.google.com/o/oauth2/auth',
79
- tokenUrl='https://oauth2.googleapis.com/token',
80
- scopes={
81
- 'https://www.googleapis.com/auth/calendar': (
82
- 'See, edit, share, and permanently delete all the'
83
- ' calendars you can access using Google Calendar'
84
- )
85
- },
86
- )
87
- )
88
- ),
89
- raw_auth_credential=AuthCredential(
90
- auth_type=AuthCredentialTypes.OAUTH2,
91
- oauth2=OAuth2Auth(
92
- client_id='oauth_client_id_2',
93
- client_secret='oauth_client_secret2',
94
- ),
95
- ),
96
- )
97
-
98
- mock_model = utils.MockModel.create(responses=responses)
99
-
100
- def call_external_api1(tool_context: ToolContext) -> Optional[int]:
101
- tool_context.request_credential(auth_config1)
102
-
103
- def call_external_api2(tool_context: ToolContext) -> Optional[int]:
104
- tool_context.request_credential(auth_config2)
105
-
106
- agent = Agent(
107
- name='root_agent',
108
- model=mock_model,
109
- tools=[call_external_api1, call_external_api2],
110
- )
111
- runner = utils.InMemoryRunner(agent)
112
- events = runner.run('test')
113
- assert events[0].content.parts[0].function_call is not None
114
- assert events[0].content.parts[1].function_call is not None
115
- auth_configs = list(events[2].actions.requested_auth_configs.values())
116
- exchanged_auth_config1 = auth_configs[0]
117
- exchanged_auth_config2 = auth_configs[1]
118
- assert exchanged_auth_config1.auth_scheme == auth_config1.auth_scheme
119
- assert (
120
- exchanged_auth_config1.raw_auth_credential
121
- == auth_config1.raw_auth_credential
122
- )
123
- assert (
124
- exchanged_auth_config1.exchanged_auth_credential.oauth2.auth_uri
125
- is not None
126
- )
127
- assert exchanged_auth_config2.auth_scheme == auth_config2.auth_scheme
128
- assert (
129
- exchanged_auth_config2.raw_auth_credential
130
- == auth_config2.raw_auth_credential
131
- )
132
- assert (
133
- exchanged_auth_config2.exchanged_auth_credential.oauth2.auth_uri
134
- is not None
135
- )
136
- function_call_ids = list(events[2].actions.requested_auth_configs.keys())
137
-
138
- for idx, part in enumerate(events[1].content.parts):
139
- reqeust_euc_function_call = part.function_call
140
- assert reqeust_euc_function_call is not None
141
- assert (
142
- reqeust_euc_function_call.name
143
- == functions.REQUEST_EUC_FUNCTION_CALL_NAME
144
- )
145
- args = AuthToolArguments.model_validate(reqeust_euc_function_call.args)
146
-
147
- assert args.function_call_id == function_call_ids[idx]
148
- args.auth_config.auth_scheme.model_extra.clear()
149
- assert args.auth_config.auth_scheme == auth_configs[idx].auth_scheme
150
- assert (
151
- args.auth_config.raw_auth_credential
152
- == auth_configs[idx].raw_auth_credential
153
- )
154
-
155
-
156
- def test_function_get_auth_response():
157
- id_1 = 'id_1'
158
- id_2 = 'id_2'
159
- responses = [
160
- [
161
- function_call(id_1, 'call_external_api1', {}),
162
- function_call(id_2, 'call_external_api2', {}),
163
- ],
164
- [
165
- types.Part.from_text(text='response1'),
166
- ],
167
- [
168
- types.Part.from_text(text='response2'),
169
- ],
170
- ]
171
-
172
- mock_model = utils.MockModel.create(responses=responses)
173
- function_invoked = 0
174
-
175
- auth_config1 = AuthConfig(
176
- auth_scheme=OAuth2(
177
- flows=OAuthFlows(
178
- authorizationCode=OAuthFlowAuthorizationCode(
179
- authorizationUrl='https://accounts.google.com/o/oauth2/auth',
180
- tokenUrl='https://oauth2.googleapis.com/token',
181
- scopes={
182
- 'https://www.googleapis.com/auth/calendar': (
183
- 'See, edit, share, and permanently delete all the'
184
- ' calendars you can access using Google Calendar'
185
- )
186
- },
187
- )
188
- )
189
- ),
190
- raw_auth_credential=AuthCredential(
191
- auth_type=AuthCredentialTypes.OAUTH2,
192
- oauth2=OAuth2Auth(
193
- client_id='oauth_client_id_1',
194
- client_secret='oauth_client_secret1',
195
- ),
196
- ),
197
- )
198
- auth_config2 = AuthConfig(
199
- auth_scheme=OAuth2(
200
- flows=OAuthFlows(
201
- authorizationCode=OAuthFlowAuthorizationCode(
202
- authorizationUrl='https://accounts.google.com/o/oauth2/auth',
203
- tokenUrl='https://oauth2.googleapis.com/token',
204
- scopes={
205
- 'https://www.googleapis.com/auth/calendar': (
206
- 'See, edit, share, and permanently delete all the'
207
- ' calendars you can access using Google Calendar'
208
- )
209
- },
210
- )
211
- )
212
- ),
213
- raw_auth_credential=AuthCredential(
214
- auth_type=AuthCredentialTypes.OAUTH2,
215
- oauth2=OAuth2Auth(
216
- client_id='oauth_client_id_2',
217
- client_secret='oauth_client_secret2',
218
- ),
219
- ),
220
- )
221
-
222
- auth_response1 = AuthConfig(
223
- auth_scheme=OAuth2(
224
- flows=OAuthFlows(
225
- authorizationCode=OAuthFlowAuthorizationCode(
226
- authorizationUrl='https://accounts.google.com/o/oauth2/auth',
227
- tokenUrl='https://oauth2.googleapis.com/token',
228
- scopes={
229
- 'https://www.googleapis.com/auth/calendar': (
230
- 'See, edit, share, and permanently delete all the'
231
- ' calendars you can access using Google Calendar'
232
- )
233
- },
234
- )
235
- )
236
- ),
237
- raw_auth_credential=AuthCredential(
238
- auth_type=AuthCredentialTypes.OAUTH2,
239
- oauth2=OAuth2Auth(
240
- client_id='oauth_client_id_1',
241
- client_secret='oauth_client_secret1',
242
- ),
243
- ),
244
- exchanged_auth_credential=AuthCredential(
245
- auth_type=AuthCredentialTypes.OAUTH2,
246
- oauth2=OAuth2Auth(
247
- client_id='oauth_client_id_1',
248
- client_secret='oauth_client_secret1',
249
- token={'access_token': 'token1'},
250
- ),
251
- ),
252
- )
253
- auth_response2 = AuthConfig(
254
- auth_scheme=OAuth2(
255
- flows=OAuthFlows(
256
- authorizationCode=OAuthFlowAuthorizationCode(
257
- authorizationUrl='https://accounts.google.com/o/oauth2/auth',
258
- tokenUrl='https://oauth2.googleapis.com/token',
259
- scopes={
260
- 'https://www.googleapis.com/auth/calendar': (
261
- 'See, edit, share, and permanently delete all the'
262
- ' calendars you can access using Google Calendar'
263
- )
264
- },
265
- )
266
- )
267
- ),
268
- raw_auth_credential=AuthCredential(
269
- auth_type=AuthCredentialTypes.OAUTH2,
270
- oauth2=OAuth2Auth(
271
- client_id='oauth_client_id_2',
272
- client_secret='oauth_client_secret2',
273
- ),
274
- ),
275
- exchanged_auth_credential=AuthCredential(
276
- auth_type=AuthCredentialTypes.OAUTH2,
277
- oauth2=OAuth2Auth(
278
- client_id='oauth_client_id_2',
279
- client_secret='oauth_client_secret2',
280
- token={'access_token': 'token2'},
281
- ),
282
- ),
283
- )
284
-
285
- def call_external_api1(tool_context: ToolContext) -> int:
286
- nonlocal function_invoked
287
- function_invoked += 1
288
- auth_response = tool_context.get_auth_response(auth_config1)
289
- if not auth_response:
290
- tool_context.request_credential(auth_config1)
291
- return
292
- assert auth_response == auth_response1.exchanged_auth_credential
293
- return 1
294
-
295
- def call_external_api2(tool_context: ToolContext) -> int:
296
- nonlocal function_invoked
297
- function_invoked += 1
298
- auth_response = tool_context.get_auth_response(auth_config2)
299
- if not auth_response:
300
- tool_context.request_credential(auth_config2)
301
- return
302
- assert auth_response == auth_response2.exchanged_auth_credential
303
- return 2
304
-
305
- agent = Agent(
306
- name='root_agent',
307
- model=mock_model,
308
- tools=[call_external_api1, call_external_api2],
309
- )
310
- runner = utils.InMemoryRunner(agent)
311
- runner.run('test')
312
- request_euc_function_call_event = runner.session.events[-3]
313
- function_response1 = types.FunctionResponse(
314
- name=request_euc_function_call_event.content.parts[0].function_call.name,
315
- response=auth_response1.model_dump(),
316
- )
317
- function_response1.id = request_euc_function_call_event.content.parts[
318
- 0
319
- ].function_call.id
320
-
321
- function_response2 = types.FunctionResponse(
322
- name=request_euc_function_call_event.content.parts[1].function_call.name,
323
- response=auth_response2.model_dump(),
324
- )
325
- function_response2.id = request_euc_function_call_event.content.parts[
326
- 1
327
- ].function_call.id
328
- runner.run(
329
- new_message=types.Content(
330
- role='user',
331
- parts=[
332
- types.Part(function_response=function_response1),
333
- types.Part(function_response=function_response2),
334
- ],
335
- ),
336
- )
337
-
338
- assert function_invoked == 4
339
- reqeust = mock_model.requests[-1]
340
- content = reqeust.contents[-1]
341
- parts = content.parts
342
- assert len(parts) == 2
343
- assert parts[0].function_response.name == 'call_external_api1'
344
- assert parts[0].function_response.response == {'result': 1}
345
- assert parts[1].function_response.name == 'call_external_api2'
346
- assert parts[1].function_response.response == {'result': 2}
@@ -1,93 +0,0 @@
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
- from typing import Any
16
-
17
- from google.adk.agents import Agent
18
- from google.genai import types
19
-
20
- from ... import utils
21
-
22
-
23
- def function_call(args: dict[str, Any]) -> types.Part:
24
- return types.Part.from_function_call(name='increase_by_one', args=args)
25
-
26
-
27
- def function_response(response: dict[str, Any]) -> types.Part:
28
- return types.Part.from_function_response(
29
- name='increase_by_one', response=response
30
- )
31
-
32
-
33
- def test_sequential_calls():
34
- responses = [
35
- function_call({'x': 1}),
36
- function_call({'x': 2}),
37
- function_call({'x': 3}),
38
- 'response1',
39
- ]
40
- mockModel = utils.MockModel.create(responses=responses)
41
- function_called = 0
42
-
43
- def increase_by_one(x: int) -> int:
44
- nonlocal function_called
45
- function_called += 1
46
- return x + 1
47
-
48
- agent = Agent(name='root_agent', model=mockModel, tools=[increase_by_one])
49
- runner = utils.InMemoryRunner(agent)
50
- result = utils.simplify_events(runner.run('test'))
51
- assert result == [
52
- ('root_agent', function_call({'x': 1})),
53
- ('root_agent', function_response({'result': 2})),
54
- ('root_agent', function_call({'x': 2})),
55
- ('root_agent', function_response({'result': 3})),
56
- ('root_agent', function_call({'x': 3})),
57
- ('root_agent', function_response({'result': 4})),
58
- ('root_agent', 'response1'),
59
- ]
60
-
61
- # Asserts the requests.
62
- assert len(mockModel.requests) == 4
63
- # 1 item: user content
64
- assert utils.simplify_contents(mockModel.requests[0].contents) == [
65
- ('user', 'test')
66
- ]
67
- # 3 items: user content, functaion call / response for the 1st call
68
- assert utils.simplify_contents(mockModel.requests[1].contents) == [
69
- ('user', 'test'),
70
- ('model', function_call({'x': 1})),
71
- ('user', function_response({'result': 2})),
72
- ]
73
- # 5 items: user content, functaion call / response for two calls
74
- assert utils.simplify_contents(mockModel.requests[2].contents) == [
75
- ('user', 'test'),
76
- ('model', function_call({'x': 1})),
77
- ('user', function_response({'result': 2})),
78
- ('model', function_call({'x': 2})),
79
- ('user', function_response({'result': 3})),
80
- ]
81
- # 7 items: user content, functaion call / response for three calls
82
- assert utils.simplify_contents(mockModel.requests[3].contents) == [
83
- ('user', 'test'),
84
- ('model', function_call({'x': 1})),
85
- ('user', function_response({'result': 2})),
86
- ('model', function_call({'x': 2})),
87
- ('user', function_response({'result': 3})),
88
- ('model', function_call({'x': 3})),
89
- ('user', function_response({'result': 4})),
90
- ]
91
-
92
- # Asserts the function calls.
93
- assert function_called == 3
@@ -1,258 +0,0 @@
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
- from typing import Any
16
- from typing import AsyncGenerator
17
- from typing import Callable
18
-
19
- from google.adk.agents import Agent
20
- from google.adk.tools import ToolContext
21
- from google.adk.tools.function_tool import FunctionTool
22
- from google.genai import types
23
- import pytest
24
-
25
- from ... import utils
26
-
27
-
28
- def test_simple_function():
29
- function_call_1 = types.Part.from_function_call(
30
- name='increase_by_one', args={'x': 1}
31
- )
32
- function_respones_2 = types.Part.from_function_response(
33
- name='increase_by_one', response={'result': 2}
34
- )
35
- responses: list[types.Content] = [
36
- function_call_1,
37
- 'response1',
38
- 'response2',
39
- 'response3',
40
- 'response4',
41
- ]
42
- function_called = 0
43
- mock_model = utils.MockModel.create(responses=responses)
44
-
45
- def increase_by_one(x: int) -> int:
46
- nonlocal function_called
47
- function_called += 1
48
- return x + 1
49
-
50
- agent = Agent(name='root_agent', model=mock_model, tools=[increase_by_one])
51
- runner = utils.InMemoryRunner(agent)
52
- assert utils.simplify_events(runner.run('test')) == [
53
- ('root_agent', function_call_1),
54
- ('root_agent', function_respones_2),
55
- ('root_agent', 'response1'),
56
- ]
57
-
58
- # Asserts the requests.
59
- assert utils.simplify_contents(mock_model.requests[0].contents) == [
60
- ('user', 'test')
61
- ]
62
- assert utils.simplify_contents(mock_model.requests[1].contents) == [
63
- ('user', 'test'),
64
- ('model', function_call_1),
65
- ('user', function_respones_2),
66
- ]
67
-
68
- # Asserts the function calls.
69
- assert function_called == 1
70
-
71
-
72
- @pytest.mark.asyncio
73
- async def test_async_function():
74
- function_calls = [
75
- types.Part.from_function_call(name='increase_by_one', args={'x': 1}),
76
- types.Part.from_function_call(name='multiple_by_two', args={'x': 2}),
77
- types.Part.from_function_call(name='multiple_by_two_sync', args={'x': 3}),
78
- ]
79
- function_responses = [
80
- types.Part.from_function_response(
81
- name='increase_by_one', response={'result': 2}
82
- ),
83
- types.Part.from_function_response(
84
- name='multiple_by_two', response={'result': 4}
85
- ),
86
- types.Part.from_function_response(
87
- name='multiple_by_two_sync', response={'result': 6}
88
- ),
89
- ]
90
-
91
- responses: list[types.Content] = [
92
- function_calls,
93
- 'response1',
94
- 'response2',
95
- 'response3',
96
- 'response4',
97
- ]
98
- function_called = 0
99
- mock_model = utils.MockModel.create(responses=responses)
100
-
101
- async def increase_by_one(x: int) -> int:
102
- nonlocal function_called
103
- function_called += 1
104
- return x + 1
105
-
106
- async def multiple_by_two(x: int) -> int:
107
- nonlocal function_called
108
- function_called += 1
109
- return x * 2
110
-
111
- def multiple_by_two_sync(x: int) -> int:
112
- nonlocal function_called
113
- function_called += 1
114
- return x * 2
115
-
116
- agent = Agent(
117
- name='root_agent',
118
- model=mock_model,
119
- tools=[increase_by_one, multiple_by_two, multiple_by_two_sync],
120
- )
121
- runner = utils.TestInMemoryRunner(agent)
122
- events = await runner.run_async_with_new_session('test')
123
- assert utils.simplify_events(events) == [
124
- ('root_agent', function_calls),
125
- ('root_agent', function_responses),
126
- ('root_agent', 'response1'),
127
- ]
128
-
129
- # Asserts the requests.
130
- assert utils.simplify_contents(mock_model.requests[0].contents) == [
131
- ('user', 'test')
132
- ]
133
- assert utils.simplify_contents(mock_model.requests[1].contents) == [
134
- ('user', 'test'),
135
- ('model', function_calls),
136
- ('user', function_responses),
137
- ]
138
-
139
- # Asserts the function calls.
140
- assert function_called == 3
141
-
142
-
143
- @pytest.mark.asyncio
144
- async def test_function_tool():
145
- function_calls = [
146
- types.Part.from_function_call(name='increase_by_one', args={'x': 1}),
147
- types.Part.from_function_call(name='multiple_by_two', args={'x': 2}),
148
- types.Part.from_function_call(name='multiple_by_two_sync', args={'x': 3}),
149
- ]
150
- function_responses = [
151
- types.Part.from_function_response(
152
- name='increase_by_one', response={'result': 2}
153
- ),
154
- types.Part.from_function_response(
155
- name='multiple_by_two', response={'result': 4}
156
- ),
157
- types.Part.from_function_response(
158
- name='multiple_by_two_sync', response={'result': 6}
159
- ),
160
- ]
161
-
162
- responses: list[types.Content] = [
163
- function_calls,
164
- 'response1',
165
- 'response2',
166
- 'response3',
167
- 'response4',
168
- ]
169
- function_called = 0
170
- mock_model = utils.MockModel.create(responses=responses)
171
-
172
- async def increase_by_one(x: int) -> int:
173
- nonlocal function_called
174
- function_called += 1
175
- return x + 1
176
-
177
- async def multiple_by_two(x: int) -> int:
178
- nonlocal function_called
179
- function_called += 1
180
- return x * 2
181
-
182
- def multiple_by_two_sync(x: int) -> int:
183
- nonlocal function_called
184
- function_called += 1
185
- return x * 2
186
-
187
- class TestTool(FunctionTool):
188
-
189
- def __init__(self, func: Callable[..., Any]):
190
- super().__init__(func=func)
191
-
192
- wrapped_increase_by_one = TestTool(func=increase_by_one)
193
- agent = Agent(
194
- name='root_agent',
195
- model=mock_model,
196
- tools=[wrapped_increase_by_one, multiple_by_two, multiple_by_two_sync],
197
- )
198
- runner = utils.TestInMemoryRunner(agent)
199
- events = await runner.run_async_with_new_session('test')
200
- assert utils.simplify_events(events) == [
201
- ('root_agent', function_calls),
202
- ('root_agent', function_responses),
203
- ('root_agent', 'response1'),
204
- ]
205
-
206
- # Asserts the requests.
207
- assert utils.simplify_contents(mock_model.requests[0].contents) == [
208
- ('user', 'test')
209
- ]
210
- assert utils.simplify_contents(mock_model.requests[1].contents) == [
211
- ('user', 'test'),
212
- ('model', function_calls),
213
- ('user', function_responses),
214
- ]
215
-
216
- # Asserts the function calls.
217
- assert function_called == 3
218
-
219
-
220
- def test_update_state():
221
- mock_model = utils.MockModel.create(
222
- responses=[
223
- types.Part.from_function_call(name='update_state', args={}),
224
- 'response1',
225
- ]
226
- )
227
-
228
- def update_state(tool_context: ToolContext):
229
- tool_context.state['x'] = 1
230
-
231
- agent = Agent(name='root_agent', model=mock_model, tools=[update_state])
232
- runner = utils.InMemoryRunner(agent)
233
- runner.run('test')
234
- assert runner.session.state['x'] == 1
235
-
236
-
237
- def test_function_call_id():
238
- responses = [
239
- types.Part.from_function_call(name='increase_by_one', args={'x': 1}),
240
- 'response1',
241
- ]
242
- mock_model = utils.MockModel.create(responses=responses)
243
-
244
- def increase_by_one(x: int) -> int:
245
- return x + 1
246
-
247
- agent = Agent(name='root_agent', model=mock_model, tools=[increase_by_one])
248
- runner = utils.InMemoryRunner(agent)
249
- events = runner.run('test')
250
- for reqeust in mock_model.requests:
251
- for content in reqeust.contents:
252
- for part in content.parts:
253
- if part.function_call:
254
- assert part.function_call.id is None
255
- if part.function_response:
256
- assert part.function_response.id is None
257
- assert events[0].content.parts[0].function_call.id.startswith('adk-')
258
- assert events[1].content.parts[0].function_response.id.startswith('adk-')