google-cloud-agentplatform 1.165.1.dev0__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 (62) hide show
  1. agentplatform/__init__.py +72 -0
  2. agentplatform/_genai/__init__.py +43 -0
  3. agentplatform/_genai/_agent_engines_utils.py +2341 -0
  4. agentplatform/_genai/_bigquery_utils.py +49 -0
  5. agentplatform/_genai/_datasets_utils.py +344 -0
  6. agentplatform/_genai/_evals_builtin_tools.py +209 -0
  7. agentplatform/_genai/_evals_common.py +4268 -0
  8. agentplatform/_genai/_evals_constant.py +122 -0
  9. agentplatform/_genai/_evals_data_converters.py +926 -0
  10. agentplatform/_genai/_evals_metric_handlers.py +1783 -0
  11. agentplatform/_genai/_evals_metric_loaders.py +401 -0
  12. agentplatform/_genai/_evals_utils.py +1043 -0
  13. agentplatform/_genai/_evals_visualization.py +2070 -0
  14. agentplatform/_genai/_gcs_utils.py +262 -0
  15. agentplatform/_genai/_logging_utils.py +47 -0
  16. agentplatform/_genai/_memory_bank_utils.py +206 -0
  17. agentplatform/_genai/_observability_data_converter.py +186 -0
  18. agentplatform/_genai/_operations_utils.py +94 -0
  19. agentplatform/_genai/_prompt_management_utils.py +147 -0
  20. agentplatform/_genai/_prompt_optimizer_utils.py +215 -0
  21. agentplatform/_genai/_skills_utils.py +69 -0
  22. agentplatform/_genai/_transformers.py +628 -0
  23. agentplatform/_genai/a2a_task_events.py +509 -0
  24. agentplatform/_genai/a2a_tasks.py +861 -0
  25. agentplatform/_genai/agent_engines.py +3931 -0
  26. agentplatform/_genai/client.py +519 -0
  27. agentplatform/_genai/datasets.py +3045 -0
  28. agentplatform/_genai/endpoints.py +1149 -0
  29. agentplatform/_genai/evals.py +6883 -0
  30. agentplatform/_genai/example_stores.py +1445 -0
  31. agentplatform/_genai/feedback_contexts.py +700 -0
  32. agentplatform/_genai/feedback_entries.py +1644 -0
  33. agentplatform/_genai/live.py +64 -0
  34. agentplatform/_genai/live_agent_engines.py +179 -0
  35. agentplatform/_genai/memories.py +2962 -0
  36. agentplatform/_genai/memory_banks.py +1927 -0
  37. agentplatform/_genai/memory_revisions.py +465 -0
  38. agentplatform/_genai/model_garden.py +2638 -0
  39. agentplatform/_genai/prompt_optimizer.py +995 -0
  40. agentplatform/_genai/prompts.py +4515 -0
  41. agentplatform/_genai/rag.py +4961 -0
  42. agentplatform/_genai/runtime_revisions.py +1257 -0
  43. agentplatform/_genai/runtimes.py +78 -0
  44. agentplatform/_genai/sandbox_snapshots.py +1015 -0
  45. agentplatform/_genai/sandbox_templates.py +1088 -0
  46. agentplatform/_genai/sandboxes.py +1604 -0
  47. agentplatform/_genai/session_events.py +543 -0
  48. agentplatform/_genai/sessions.py +1449 -0
  49. agentplatform/_genai/skill_revisions.py +377 -0
  50. agentplatform/_genai/skills.py +1708 -0
  51. agentplatform/_genai/types/__init__.py +4695 -0
  52. agentplatform/_genai/types/agent_engines.py +16 -0
  53. agentplatform/_genai/types/common.py +32784 -0
  54. agentplatform/_genai/types/evals.py +1031 -0
  55. agentplatform/_genai/types/prompt_optimizer.py +107 -0
  56. agentplatform/_genai/types/prompts.py +107 -0
  57. agentplatform/version.py +17 -0
  58. google_cloud_agentplatform-1.165.1.dev0.dist-info/METADATA +79 -0
  59. google_cloud_agentplatform-1.165.1.dev0.dist-info/RECORD +62 -0
  60. google_cloud_agentplatform-1.165.1.dev0.dist-info/WHEEL +5 -0
  61. google_cloud_agentplatform-1.165.1.dev0.dist-info/licenses/LICENSE +202 -0
  62. google_cloud_agentplatform-1.165.1.dev0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1449 @@
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
+
16
+ # Code generated by the Google Gen AI SDK generator DO NOT EDIT.
17
+
18
+ import functools
19
+ import importlib
20
+ import json
21
+ import logging
22
+ import typing
23
+ from typing import Any, Iterator, Optional, Union
24
+ from urllib.parse import urlencode
25
+
26
+ from google.genai import _api_module
27
+ from google.genai import _common
28
+ from google.genai._common import get_value_by_path as getv
29
+ from google.genai._common import set_value_by_path as setv
30
+ from google.genai.pagers import AsyncPager, Pager
31
+
32
+ from . import _agent_engines_utils
33
+ from . import types
34
+
35
+ if typing.TYPE_CHECKING:
36
+ from . import session_events as session_events_module
37
+
38
+ _ = session_events_module
39
+
40
+
41
+ logger = logging.getLogger("agentplatform_genai.sessions")
42
+
43
+ logger.setLevel(logging.INFO)
44
+
45
+
46
+ def _CreateAgentEngineSessionConfig_to_vertex(
47
+ from_object: Union[dict[str, Any], object],
48
+ parent_object: Optional[dict[str, Any]] = None,
49
+ ) -> dict[str, Any]:
50
+ to_object: dict[str, Any] = {}
51
+
52
+ if getv(from_object, ["display_name"]) is not None:
53
+ setv(parent_object, ["displayName"], getv(from_object, ["display_name"]))
54
+
55
+ if getv(from_object, ["session_state"]) is not None:
56
+ setv(parent_object, ["sessionState"], getv(from_object, ["session_state"]))
57
+
58
+ if getv(from_object, ["ttl"]) is not None:
59
+ setv(parent_object, ["ttl"], getv(from_object, ["ttl"]))
60
+
61
+ if getv(from_object, ["expire_time"]) is not None:
62
+ setv(parent_object, ["expireTime"], getv(from_object, ["expire_time"]))
63
+
64
+ if getv(from_object, ["labels"]) is not None:
65
+ setv(parent_object, ["labels"], getv(from_object, ["labels"]))
66
+
67
+ if getv(from_object, ["session_id"]) is not None:
68
+ setv(parent_object, ["_query", "sessionId"], getv(from_object, ["session_id"]))
69
+
70
+ return to_object
71
+
72
+
73
+ def _CreateAgentEngineSessionRequestParameters_to_vertex(
74
+ from_object: Union[dict[str, Any], object],
75
+ parent_object: Optional[dict[str, Any]] = None,
76
+ ) -> dict[str, Any]:
77
+ to_object: dict[str, Any] = {}
78
+ if getv(from_object, ["name"]) is not None:
79
+ setv(to_object, ["_url", "name"], getv(from_object, ["name"]))
80
+
81
+ if getv(from_object, ["user_id"]) is not None:
82
+ setv(to_object, ["userId"], getv(from_object, ["user_id"]))
83
+
84
+ if getv(from_object, ["config"]) is not None:
85
+ _CreateAgentEngineSessionConfig_to_vertex(
86
+ getv(from_object, ["config"]), to_object
87
+ )
88
+
89
+ return to_object
90
+
91
+
92
+ def _DeleteAgentEngineSessionRequestParameters_to_vertex(
93
+ from_object: Union[dict[str, Any], object],
94
+ parent_object: Optional[dict[str, Any]] = None,
95
+ ) -> dict[str, Any]:
96
+ to_object: dict[str, Any] = {}
97
+ if getv(from_object, ["name"]) is not None:
98
+ setv(to_object, ["_url", "name"], getv(from_object, ["name"]))
99
+
100
+ return to_object
101
+
102
+
103
+ def _GetAgentEngineSessionOperationParameters_to_vertex(
104
+ from_object: Union[dict[str, Any], object],
105
+ parent_object: Optional[dict[str, Any]] = None,
106
+ ) -> dict[str, Any]:
107
+ to_object: dict[str, Any] = {}
108
+ if getv(from_object, ["operation_name"]) is not None:
109
+ setv(
110
+ to_object, ["_url", "operationName"], getv(from_object, ["operation_name"])
111
+ )
112
+
113
+ return to_object
114
+
115
+
116
+ def _GetAgentEngineSessionRequestParameters_to_vertex(
117
+ from_object: Union[dict[str, Any], object],
118
+ parent_object: Optional[dict[str, Any]] = None,
119
+ ) -> dict[str, Any]:
120
+ to_object: dict[str, Any] = {}
121
+ if getv(from_object, ["name"]) is not None:
122
+ setv(to_object, ["_url", "name"], getv(from_object, ["name"]))
123
+
124
+ return to_object
125
+
126
+
127
+ def _ListAgentEngineSessionsConfig_to_vertex(
128
+ from_object: Union[dict[str, Any], object],
129
+ parent_object: Optional[dict[str, Any]] = None,
130
+ ) -> dict[str, Any]:
131
+ to_object: dict[str, Any] = {}
132
+
133
+ if getv(from_object, ["page_size"]) is not None:
134
+ setv(parent_object, ["_query", "pageSize"], getv(from_object, ["page_size"]))
135
+
136
+ if getv(from_object, ["page_token"]) is not None:
137
+ setv(parent_object, ["_query", "pageToken"], getv(from_object, ["page_token"]))
138
+
139
+ if getv(from_object, ["filter"]) is not None:
140
+ setv(parent_object, ["_query", "filter"], getv(from_object, ["filter"]))
141
+
142
+ return to_object
143
+
144
+
145
+ def _ListAgentEngineSessionsRequestParameters_to_vertex(
146
+ from_object: Union[dict[str, Any], object],
147
+ parent_object: Optional[dict[str, Any]] = None,
148
+ ) -> dict[str, Any]:
149
+ to_object: dict[str, Any] = {}
150
+ if getv(from_object, ["name"]) is not None:
151
+ setv(to_object, ["_url", "name"], getv(from_object, ["name"]))
152
+
153
+ if getv(from_object, ["config"]) is not None:
154
+ _ListAgentEngineSessionsConfig_to_vertex(
155
+ getv(from_object, ["config"]), to_object
156
+ )
157
+
158
+ return to_object
159
+
160
+
161
+ def _UpdateAgentEngineSessionConfig_to_vertex(
162
+ from_object: Union[dict[str, Any], object],
163
+ parent_object: Optional[dict[str, Any]] = None,
164
+ ) -> dict[str, Any]:
165
+ to_object: dict[str, Any] = {}
166
+
167
+ if getv(from_object, ["display_name"]) is not None:
168
+ setv(parent_object, ["displayName"], getv(from_object, ["display_name"]))
169
+
170
+ if getv(from_object, ["session_state"]) is not None:
171
+ setv(parent_object, ["sessionState"], getv(from_object, ["session_state"]))
172
+
173
+ if getv(from_object, ["ttl"]) is not None:
174
+ setv(parent_object, ["ttl"], getv(from_object, ["ttl"]))
175
+
176
+ if getv(from_object, ["expire_time"]) is not None:
177
+ setv(parent_object, ["expireTime"], getv(from_object, ["expire_time"]))
178
+
179
+ if getv(from_object, ["labels"]) is not None:
180
+ setv(parent_object, ["labels"], getv(from_object, ["labels"]))
181
+
182
+ if getv(from_object, ["session_id"]) is not None:
183
+ setv(parent_object, ["_query", "sessionId"], getv(from_object, ["session_id"]))
184
+
185
+ if getv(from_object, ["update_mask"]) is not None:
186
+ setv(
187
+ parent_object, ["_query", "updateMask"], getv(from_object, ["update_mask"])
188
+ )
189
+
190
+ if getv(from_object, ["user_id"]) is not None:
191
+ setv(parent_object, ["userId"], getv(from_object, ["user_id"]))
192
+
193
+ return to_object
194
+
195
+
196
+ def _UpdateAgentEngineSessionRequestParameters_to_vertex(
197
+ from_object: Union[dict[str, Any], object],
198
+ parent_object: Optional[dict[str, Any]] = None,
199
+ ) -> dict[str, Any]:
200
+ to_object: dict[str, Any] = {}
201
+ if getv(from_object, ["name"]) is not None:
202
+ setv(to_object, ["_url", "name"], getv(from_object, ["name"]))
203
+
204
+ if getv(from_object, ["config"]) is not None:
205
+ _UpdateAgentEngineSessionConfig_to_vertex(
206
+ getv(from_object, ["config"]), to_object
207
+ )
208
+
209
+ return to_object
210
+
211
+
212
+ class Sessions(_api_module.BaseModule):
213
+
214
+ def _create(
215
+ self,
216
+ *,
217
+ name: str,
218
+ user_id: str,
219
+ config: Optional[types.CreateAgentEngineSessionConfigOrDict] = None,
220
+ ) -> types.AgentEngineSessionOperation:
221
+ """
222
+ Creates a new session in the Agent Engine.
223
+
224
+ Args:
225
+ name (str): Required. The name of the Agent Engine to create the session under. Format:
226
+ `projects/{project}/locations/{location}/reasoningEngines/{resource_id}`.
227
+ user_id (str): Required. The user ID of the session.
228
+ config (CreateAgentEngineSessionConfig):
229
+ Optional. Additional configurations for creating the Agent Engine session.
230
+
231
+ Returns:
232
+ AgentEngineSessionOperation: The operation for creating the Agent Engine session.
233
+
234
+ """
235
+
236
+ parameter_model = types._CreateAgentEngineSessionRequestParameters(
237
+ name=name,
238
+ user_id=user_id,
239
+ config=config,
240
+ )
241
+
242
+ request_url_dict: Optional[dict[str, str]]
243
+ if not self._api_client.vertexai:
244
+ raise ValueError(
245
+ "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini Developer API mode."
246
+ )
247
+ else:
248
+ request_dict = _CreateAgentEngineSessionRequestParameters_to_vertex(
249
+ parameter_model
250
+ )
251
+ request_url_dict = request_dict.get("_url")
252
+ if request_url_dict:
253
+ path = "{name}/sessions".format_map(request_url_dict)
254
+ else:
255
+ path = "{name}/sessions"
256
+
257
+ query_params = request_dict.get("_query")
258
+ if query_params:
259
+ path = f"{path}?{urlencode(query_params)}"
260
+ # TODO: remove the hack that pops config.
261
+ request_dict.pop("config", None)
262
+
263
+ http_options: Optional[types.HttpOptions] = None
264
+ if (
265
+ parameter_model.config is not None
266
+ and parameter_model.config.http_options is not None
267
+ ):
268
+ http_options = parameter_model.config.http_options
269
+
270
+ request_dict = _common.convert_to_dict(request_dict)
271
+ request_dict = _common.encode_unserializable_types(request_dict)
272
+
273
+ response = self._api_client.request("post", path, request_dict, http_options)
274
+
275
+ response_dict = {} if not response.body else json.loads(response.body)
276
+
277
+ return_value = types.AgentEngineSessionOperation._from_response(
278
+ response=response_dict,
279
+ kwargs=(
280
+ {
281
+ "config": {
282
+ "response_schema": getattr(
283
+ parameter_model.config, "response_schema", None
284
+ ),
285
+ "response_json_schema": getattr(
286
+ parameter_model.config, "response_json_schema", None
287
+ ),
288
+ "include_all_fields": getattr(
289
+ parameter_model.config, "include_all_fields", None
290
+ ),
291
+ }
292
+ }
293
+ if getattr(parameter_model, "config", None)
294
+ else {}
295
+ ),
296
+ )
297
+
298
+ self._api_client._verify_response(return_value)
299
+ return return_value
300
+
301
+ def delete(
302
+ self,
303
+ *,
304
+ name: str,
305
+ config: Optional[types.DeleteAgentEngineSessionConfigOrDict] = None,
306
+ ) -> types.DeleteAgentEngineSessionOperation:
307
+ """
308
+ Delete an Agent Engine session.
309
+
310
+ Args:
311
+ name (str): Required. The name of the Agent Engine session to be deleted. Format:
312
+ `projects/{project}/locations/{location}/reasoningEngines/{resource_id}/sessions/{session_id}`.
313
+ config (DeleteAgentEngineSessionConfig):
314
+ Optional. Additional configurations for deleting the Agent Engine session.
315
+
316
+ Returns:
317
+ DeleteAgentEngineSessionOperation: The operation for deleting the Agent Engine session.
318
+
319
+ """
320
+
321
+ parameter_model = types._DeleteAgentEngineSessionRequestParameters(
322
+ name=name,
323
+ config=config,
324
+ )
325
+
326
+ request_url_dict: Optional[dict[str, str]]
327
+ if not self._api_client.vertexai:
328
+ raise ValueError(
329
+ "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini Developer API mode."
330
+ )
331
+ else:
332
+ request_dict = _DeleteAgentEngineSessionRequestParameters_to_vertex(
333
+ parameter_model
334
+ )
335
+ request_url_dict = request_dict.get("_url")
336
+ if request_url_dict:
337
+ path = "{name}".format_map(request_url_dict)
338
+ else:
339
+ path = "{name}"
340
+
341
+ query_params = request_dict.get("_query")
342
+ if query_params:
343
+ path = f"{path}?{urlencode(query_params)}"
344
+ # TODO: remove the hack that pops config.
345
+ request_dict.pop("config", None)
346
+
347
+ http_options: Optional[types.HttpOptions] = None
348
+ if (
349
+ parameter_model.config is not None
350
+ and parameter_model.config.http_options is not None
351
+ ):
352
+ http_options = parameter_model.config.http_options
353
+
354
+ request_dict = _common.convert_to_dict(request_dict)
355
+ request_dict = _common.encode_unserializable_types(request_dict)
356
+
357
+ response = self._api_client.request("delete", path, request_dict, http_options)
358
+
359
+ response_dict = {} if not response.body else json.loads(response.body)
360
+
361
+ return_value = types.DeleteAgentEngineSessionOperation._from_response(
362
+ response=response_dict,
363
+ kwargs=(
364
+ {
365
+ "config": {
366
+ "response_schema": getattr(
367
+ parameter_model.config, "response_schema", None
368
+ ),
369
+ "response_json_schema": getattr(
370
+ parameter_model.config, "response_json_schema", None
371
+ ),
372
+ "include_all_fields": getattr(
373
+ parameter_model.config, "include_all_fields", None
374
+ ),
375
+ }
376
+ }
377
+ if getattr(parameter_model, "config", None)
378
+ else {}
379
+ ),
380
+ )
381
+
382
+ self._api_client._verify_response(return_value)
383
+ return return_value
384
+
385
+ def get(
386
+ self,
387
+ *,
388
+ name: str,
389
+ config: Optional[types.GetAgentEngineSessionConfigOrDict] = None,
390
+ ) -> types.Session:
391
+ """
392
+ Gets an agent engine session.
393
+
394
+ Args:
395
+ name (str): Required. The name of the Agent Engine session to get. Format:
396
+ `projects/{project}/locations/{location}/reasoningEngines/{resource_id}/sessions/{session_id}`.
397
+ config (GetAgentEngineSessionConfig):
398
+ Optional. Additional configurations for getting the Agent Engine session.
399
+
400
+ Returns:
401
+ AgentEngineSession: The requested Agent Engine session.
402
+
403
+ """
404
+
405
+ parameter_model = types._GetAgentEngineSessionRequestParameters(
406
+ name=name,
407
+ config=config,
408
+ )
409
+
410
+ request_url_dict: Optional[dict[str, str]]
411
+ if not self._api_client.vertexai:
412
+ raise ValueError(
413
+ "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini Developer API mode."
414
+ )
415
+ else:
416
+ request_dict = _GetAgentEngineSessionRequestParameters_to_vertex(
417
+ parameter_model
418
+ )
419
+ request_url_dict = request_dict.get("_url")
420
+ if request_url_dict:
421
+ path = "{name}".format_map(request_url_dict)
422
+ else:
423
+ path = "{name}"
424
+
425
+ query_params = request_dict.get("_query")
426
+ if query_params:
427
+ path = f"{path}?{urlencode(query_params)}"
428
+ # TODO: remove the hack that pops config.
429
+ request_dict.pop("config", None)
430
+
431
+ http_options: Optional[types.HttpOptions] = None
432
+ if (
433
+ parameter_model.config is not None
434
+ and parameter_model.config.http_options is not None
435
+ ):
436
+ http_options = parameter_model.config.http_options
437
+
438
+ request_dict = _common.convert_to_dict(request_dict)
439
+ request_dict = _common.encode_unserializable_types(request_dict)
440
+
441
+ response = self._api_client.request("get", path, request_dict, http_options)
442
+
443
+ response_dict = {} if not response.body else json.loads(response.body)
444
+
445
+ return_value = types.Session._from_response(
446
+ response=response_dict,
447
+ kwargs=(
448
+ {
449
+ "config": {
450
+ "response_schema": getattr(
451
+ parameter_model.config, "response_schema", None
452
+ ),
453
+ "response_json_schema": getattr(
454
+ parameter_model.config, "response_json_schema", None
455
+ ),
456
+ "include_all_fields": getattr(
457
+ parameter_model.config, "include_all_fields", None
458
+ ),
459
+ }
460
+ }
461
+ if getattr(parameter_model, "config", None)
462
+ else {}
463
+ ),
464
+ )
465
+
466
+ self._api_client._verify_response(return_value)
467
+ return return_value
468
+
469
+ def _list(
470
+ self,
471
+ *,
472
+ name: str,
473
+ config: Optional[types.ListAgentEngineSessionsConfigOrDict] = None,
474
+ ) -> types.ListReasoningEnginesSessionsResponse:
475
+ """
476
+ Lists Agent Engine sessions.
477
+
478
+ Args:
479
+ name (str): Required. The name of the Agent Engine to list sessions for. Format:
480
+ `projects/{project}/locations/{location}/reasoningEngines/{resource_id}`.
481
+ config (ListAgentEngineSessionsConfig):
482
+ Optional. Additional configurations for listing the Agent Engine sessions.
483
+
484
+ Returns:
485
+ ListReasoningEnginesSessionsResponse: The requested Agent Engine sessions.
486
+
487
+ """
488
+
489
+ parameter_model = types._ListAgentEngineSessionsRequestParameters(
490
+ name=name,
491
+ config=config,
492
+ )
493
+
494
+ request_url_dict: Optional[dict[str, str]]
495
+ if not self._api_client.vertexai:
496
+ raise ValueError(
497
+ "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini Developer API mode."
498
+ )
499
+ else:
500
+ request_dict = _ListAgentEngineSessionsRequestParameters_to_vertex(
501
+ parameter_model
502
+ )
503
+ request_url_dict = request_dict.get("_url")
504
+ if request_url_dict:
505
+ path = "{name}/sessions".format_map(request_url_dict)
506
+ else:
507
+ path = "{name}/sessions"
508
+
509
+ query_params = request_dict.get("_query")
510
+ if query_params:
511
+ path = f"{path}?{urlencode(query_params)}"
512
+ # TODO: remove the hack that pops config.
513
+ request_dict.pop("config", None)
514
+
515
+ http_options: Optional[types.HttpOptions] = None
516
+ if (
517
+ parameter_model.config is not None
518
+ and parameter_model.config.http_options is not None
519
+ ):
520
+ http_options = parameter_model.config.http_options
521
+
522
+ request_dict = _common.convert_to_dict(request_dict)
523
+ request_dict = _common.encode_unserializable_types(request_dict)
524
+
525
+ response = self._api_client.request("get", path, request_dict, http_options)
526
+
527
+ response_dict = {} if not response.body else json.loads(response.body)
528
+
529
+ return_value = types.ListReasoningEnginesSessionsResponse._from_response(
530
+ response=response_dict,
531
+ kwargs=(
532
+ {
533
+ "config": {
534
+ "response_schema": getattr(
535
+ parameter_model.config, "response_schema", None
536
+ ),
537
+ "response_json_schema": getattr(
538
+ parameter_model.config, "response_json_schema", None
539
+ ),
540
+ "include_all_fields": getattr(
541
+ parameter_model.config, "include_all_fields", None
542
+ ),
543
+ }
544
+ }
545
+ if getattr(parameter_model, "config", None)
546
+ else {}
547
+ ),
548
+ )
549
+
550
+ self._api_client._verify_response(return_value)
551
+ return return_value
552
+
553
+ def _get_session_operation(
554
+ self,
555
+ *,
556
+ operation_name: str,
557
+ config: Optional[types.GetAgentEngineOperationConfigOrDict] = None,
558
+ ) -> types.AgentEngineSessionOperation:
559
+ parameter_model = types._GetAgentEngineSessionOperationParameters(
560
+ operation_name=operation_name,
561
+ config=config,
562
+ )
563
+
564
+ request_url_dict: Optional[dict[str, str]]
565
+ if not self._api_client.vertexai:
566
+ raise ValueError(
567
+ "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini Developer API mode."
568
+ )
569
+ else:
570
+ request_dict = _GetAgentEngineSessionOperationParameters_to_vertex(
571
+ parameter_model
572
+ )
573
+ request_url_dict = request_dict.get("_url")
574
+ if request_url_dict:
575
+ path = "{operationName}".format_map(request_url_dict)
576
+ else:
577
+ path = "{operationName}"
578
+
579
+ query_params = request_dict.get("_query")
580
+ if query_params:
581
+ path = f"{path}?{urlencode(query_params)}"
582
+ # TODO: remove the hack that pops config.
583
+ request_dict.pop("config", None)
584
+
585
+ http_options: Optional[types.HttpOptions] = None
586
+ if (
587
+ parameter_model.config is not None
588
+ and parameter_model.config.http_options is not None
589
+ ):
590
+ http_options = parameter_model.config.http_options
591
+
592
+ request_dict = _common.convert_to_dict(request_dict)
593
+ request_dict = _common.encode_unserializable_types(request_dict)
594
+
595
+ response = self._api_client.request("get", path, request_dict, http_options)
596
+
597
+ response_dict = {} if not response.body else json.loads(response.body)
598
+
599
+ return_value = types.AgentEngineSessionOperation._from_response(
600
+ response=response_dict,
601
+ kwargs=(
602
+ {
603
+ "config": {
604
+ "response_schema": getattr(
605
+ parameter_model.config, "response_schema", None
606
+ ),
607
+ "response_json_schema": getattr(
608
+ parameter_model.config, "response_json_schema", None
609
+ ),
610
+ "include_all_fields": getattr(
611
+ parameter_model.config, "include_all_fields", None
612
+ ),
613
+ }
614
+ }
615
+ if getattr(parameter_model, "config", None)
616
+ else {}
617
+ ),
618
+ )
619
+
620
+ self._api_client._verify_response(return_value)
621
+ return return_value
622
+
623
+ def _update(
624
+ self,
625
+ *,
626
+ name: str,
627
+ config: Optional[types.UpdateAgentEngineSessionConfigOrDict] = None,
628
+ ) -> types.Session:
629
+ """
630
+ Updates an Agent Engine session.
631
+
632
+ Args:
633
+ name (str): Required. The name of the Agent Engine session to be updated. Format:
634
+ `projects/{project}/locations/{location}/reasoningEngines/{resource_id}/sessions/{session_id}`.
635
+ config (UpdateAgentEngineSessionConfig):
636
+ Optional. Additional configurations for updating the Agent Engine session.
637
+
638
+ Returns:
639
+ types.Session: The updated Agent Engine session.
640
+
641
+ """
642
+
643
+ parameter_model = types._UpdateAgentEngineSessionRequestParameters(
644
+ name=name,
645
+ config=config,
646
+ )
647
+
648
+ request_url_dict: Optional[dict[str, str]]
649
+ if not self._api_client.vertexai:
650
+ raise ValueError(
651
+ "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini Developer API mode."
652
+ )
653
+ else:
654
+ request_dict = _UpdateAgentEngineSessionRequestParameters_to_vertex(
655
+ parameter_model
656
+ )
657
+ request_url_dict = request_dict.get("_url")
658
+ if request_url_dict:
659
+ path = "{name}".format_map(request_url_dict)
660
+ else:
661
+ path = "{name}"
662
+
663
+ query_params = request_dict.get("_query")
664
+ if query_params:
665
+ path = f"{path}?{urlencode(query_params)}"
666
+ # TODO: remove the hack that pops config.
667
+ request_dict.pop("config", None)
668
+
669
+ http_options: Optional[types.HttpOptions] = None
670
+ if (
671
+ parameter_model.config is not None
672
+ and parameter_model.config.http_options is not None
673
+ ):
674
+ http_options = parameter_model.config.http_options
675
+
676
+ request_dict = _common.convert_to_dict(request_dict)
677
+ request_dict = _common.encode_unserializable_types(request_dict)
678
+
679
+ response = self._api_client.request("patch", path, request_dict, http_options)
680
+
681
+ response_dict = {} if not response.body else json.loads(response.body)
682
+
683
+ return_value = types.Session._from_response(
684
+ response=response_dict,
685
+ kwargs=(
686
+ {
687
+ "config": {
688
+ "response_schema": getattr(
689
+ parameter_model.config, "response_schema", None
690
+ ),
691
+ "response_json_schema": getattr(
692
+ parameter_model.config, "response_json_schema", None
693
+ ),
694
+ "include_all_fields": getattr(
695
+ parameter_model.config, "include_all_fields", None
696
+ ),
697
+ }
698
+ }
699
+ if getattr(parameter_model, "config", None)
700
+ else {}
701
+ ),
702
+ )
703
+
704
+ self._api_client._verify_response(return_value)
705
+ return return_value
706
+
707
+ _events = None
708
+
709
+ @property
710
+ def events(self) -> "session_events_module.SessionEvents":
711
+ if self._events is None:
712
+ try:
713
+ # We need to lazy load the sessions.events module to handle the
714
+ # possibility of ImportError when dependencies are not installed.
715
+ self._events = importlib.import_module(".session_events", __package__)
716
+ except ImportError as e:
717
+ raise ImportError(
718
+ "The 'agent_engines.sessions.events' module requires"
719
+ "additional packages. Please install them using pip install "
720
+ "google-cloud-aiplatform[agent_engines]"
721
+ ) from e
722
+ return self._events.SessionEvents(self._api_client) # type: ignore[no-any-return]
723
+
724
+ def create(
725
+ self,
726
+ *,
727
+ name: str,
728
+ user_id: str,
729
+ config: Optional[types.CreateAgentEngineSessionConfigOrDict] = None,
730
+ ) -> types.AgentEngineSessionOperation:
731
+ """Creates a new session in the Agent Engine.
732
+
733
+ Args:
734
+ name (str):
735
+ Required. The name of the agent engine to create the session for.
736
+ user_id (str):
737
+ Required. The user ID of the session.
738
+ config (CreateAgentEngineSessionConfig):
739
+ Optional. The configuration for the session to create.
740
+
741
+ Returns:
742
+ AgentEngineSessionOperation: The operation for creating the session.
743
+ """
744
+ if config is None:
745
+ config = types.CreateAgentEngineSessionConfig()
746
+ elif isinstance(config, dict):
747
+ config = types.CreateAgentEngineSessionConfig.model_validate(config)
748
+ operation = self._create(
749
+ name=name,
750
+ user_id=user_id,
751
+ config=config,
752
+ )
753
+ if config.wait_for_completion:
754
+ if not operation.done:
755
+ operation = _agent_engines_utils._await_operation(
756
+ operation_name=operation.name,
757
+ get_operation_fn=self._get_session_operation,
758
+ poll_interval_seconds=0.5,
759
+ )
760
+ # We need to make a call to get the session because the operation
761
+ # response might not contain the relevant fields.
762
+ if operation.response:
763
+ operation.response = self.get(name=operation.response.name)
764
+ elif operation.error:
765
+ raise RuntimeError(f"Failed to create session: {operation.error}")
766
+ else:
767
+ raise RuntimeError(
768
+ "Error retrieving session from the operation response. "
769
+ f"Operation name: {operation.name}"
770
+ )
771
+ return operation
772
+
773
+ def update(
774
+ self,
775
+ *,
776
+ name: str,
777
+ config: Optional[types.UpdateAgentEngineSessionConfigOrDict] = None,
778
+ ) -> types.Session:
779
+ """Updates an Agent Engine session.
780
+
781
+ Args:
782
+ name (str):
783
+ Required. The name of the Agent Engine session to be updated. Format:
784
+ `projects/{project}/locations/{location}/reasoningEngines/{resource_id}/sessions/{session_id}`.
785
+ config (UpdateAgentEngineSessionConfig):
786
+ Optional. The configuration for the session to update.
787
+
788
+ Returns:
789
+ Session: The updated Agent Engine session.
790
+ """
791
+ if config is None:
792
+ config = types.UpdateAgentEngineSessionConfig()
793
+ elif isinstance(config, dict):
794
+ config = types.UpdateAgentEngineSessionConfig.model_validate(config)
795
+ return self._update(
796
+ name=name,
797
+ config=config,
798
+ )
799
+
800
+ def list(
801
+ self,
802
+ *,
803
+ name: str,
804
+ config: Optional[types.ListAgentEngineSessionsConfigOrDict] = None,
805
+ ) -> Iterator[types.Session]:
806
+ """Lists Agent Engine sessions.
807
+
808
+ Args:
809
+ name (str): Required. The name of the agent engine to list sessions
810
+ for.
811
+ config (ListAgentEngineSessionConfig): Optional. The configuration
812
+ for the sessions to list.
813
+
814
+ Returns:
815
+ Iterable[Session]: An iterable of sessions.
816
+ """
817
+
818
+ return Pager(
819
+ "sessions",
820
+ functools.partial(self._list, name=name),
821
+ self._list(name=name, config=config),
822
+ config,
823
+ )
824
+
825
+
826
+ class AsyncSessions(_api_module.BaseModule):
827
+
828
+ async def _create(
829
+ self,
830
+ *,
831
+ name: str,
832
+ user_id: str,
833
+ config: Optional[types.CreateAgentEngineSessionConfigOrDict] = None,
834
+ ) -> types.AgentEngineSessionOperation:
835
+ """
836
+ Creates a new session in the Agent Engine.
837
+
838
+ Args:
839
+ name (str): Required. The name of the Agent Engine to create the session under. Format:
840
+ `projects/{project}/locations/{location}/reasoningEngines/{resource_id}`.
841
+ user_id (str): Required. The user ID of the session.
842
+ config (CreateAgentEngineSessionConfig):
843
+ Optional. Additional configurations for creating the Agent Engine session.
844
+
845
+ Returns:
846
+ AgentEngineSessionOperation: The operation for creating the Agent Engine session.
847
+
848
+ """
849
+
850
+ parameter_model = types._CreateAgentEngineSessionRequestParameters(
851
+ name=name,
852
+ user_id=user_id,
853
+ config=config,
854
+ )
855
+
856
+ request_url_dict: Optional[dict[str, str]]
857
+ if not self._api_client.vertexai:
858
+ raise ValueError(
859
+ "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini Developer API mode."
860
+ )
861
+ else:
862
+ request_dict = _CreateAgentEngineSessionRequestParameters_to_vertex(
863
+ parameter_model
864
+ )
865
+ request_url_dict = request_dict.get("_url")
866
+ if request_url_dict:
867
+ path = "{name}/sessions".format_map(request_url_dict)
868
+ else:
869
+ path = "{name}/sessions"
870
+
871
+ query_params = request_dict.get("_query")
872
+ if query_params:
873
+ path = f"{path}?{urlencode(query_params)}"
874
+ # TODO: remove the hack that pops config.
875
+ request_dict.pop("config", None)
876
+
877
+ http_options: Optional[types.HttpOptions] = None
878
+ if (
879
+ parameter_model.config is not None
880
+ and parameter_model.config.http_options is not None
881
+ ):
882
+ http_options = parameter_model.config.http_options
883
+
884
+ request_dict = _common.convert_to_dict(request_dict)
885
+ request_dict = _common.encode_unserializable_types(request_dict)
886
+
887
+ response = await self._api_client.async_request(
888
+ "post", path, request_dict, http_options
889
+ )
890
+
891
+ response_dict = {} if not response.body else json.loads(response.body)
892
+
893
+ return_value = types.AgentEngineSessionOperation._from_response(
894
+ response=response_dict,
895
+ kwargs=(
896
+ {
897
+ "config": {
898
+ "response_schema": getattr(
899
+ parameter_model.config, "response_schema", None
900
+ ),
901
+ "response_json_schema": getattr(
902
+ parameter_model.config, "response_json_schema", None
903
+ ),
904
+ "include_all_fields": getattr(
905
+ parameter_model.config, "include_all_fields", None
906
+ ),
907
+ }
908
+ }
909
+ if getattr(parameter_model, "config", None)
910
+ else {}
911
+ ),
912
+ )
913
+
914
+ self._api_client._verify_response(return_value)
915
+ return return_value
916
+
917
+ async def delete(
918
+ self,
919
+ *,
920
+ name: str,
921
+ config: Optional[types.DeleteAgentEngineSessionConfigOrDict] = None,
922
+ ) -> types.DeleteAgentEngineSessionOperation:
923
+ """
924
+ Delete an Agent Engine session.
925
+
926
+ Args:
927
+ name (str): Required. The name of the Agent Engine session to be deleted. Format:
928
+ `projects/{project}/locations/{location}/reasoningEngines/{resource_id}/sessions/{session_id}`.
929
+ config (DeleteAgentEngineSessionConfig):
930
+ Optional. Additional configurations for deleting the Agent Engine session.
931
+
932
+ Returns:
933
+ DeleteAgentEngineSessionOperation: The operation for deleting the Agent Engine session.
934
+
935
+ """
936
+
937
+ parameter_model = types._DeleteAgentEngineSessionRequestParameters(
938
+ name=name,
939
+ config=config,
940
+ )
941
+
942
+ request_url_dict: Optional[dict[str, str]]
943
+ if not self._api_client.vertexai:
944
+ raise ValueError(
945
+ "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini Developer API mode."
946
+ )
947
+ else:
948
+ request_dict = _DeleteAgentEngineSessionRequestParameters_to_vertex(
949
+ parameter_model
950
+ )
951
+ request_url_dict = request_dict.get("_url")
952
+ if request_url_dict:
953
+ path = "{name}".format_map(request_url_dict)
954
+ else:
955
+ path = "{name}"
956
+
957
+ query_params = request_dict.get("_query")
958
+ if query_params:
959
+ path = f"{path}?{urlencode(query_params)}"
960
+ # TODO: remove the hack that pops config.
961
+ request_dict.pop("config", None)
962
+
963
+ http_options: Optional[types.HttpOptions] = None
964
+ if (
965
+ parameter_model.config is not None
966
+ and parameter_model.config.http_options is not None
967
+ ):
968
+ http_options = parameter_model.config.http_options
969
+
970
+ request_dict = _common.convert_to_dict(request_dict)
971
+ request_dict = _common.encode_unserializable_types(request_dict)
972
+
973
+ response = await self._api_client.async_request(
974
+ "delete", path, request_dict, http_options
975
+ )
976
+
977
+ response_dict = {} if not response.body else json.loads(response.body)
978
+
979
+ return_value = types.DeleteAgentEngineSessionOperation._from_response(
980
+ response=response_dict,
981
+ kwargs=(
982
+ {
983
+ "config": {
984
+ "response_schema": getattr(
985
+ parameter_model.config, "response_schema", None
986
+ ),
987
+ "response_json_schema": getattr(
988
+ parameter_model.config, "response_json_schema", None
989
+ ),
990
+ "include_all_fields": getattr(
991
+ parameter_model.config, "include_all_fields", None
992
+ ),
993
+ }
994
+ }
995
+ if getattr(parameter_model, "config", None)
996
+ else {}
997
+ ),
998
+ )
999
+
1000
+ self._api_client._verify_response(return_value)
1001
+ return return_value
1002
+
1003
+ async def get(
1004
+ self,
1005
+ *,
1006
+ name: str,
1007
+ config: Optional[types.GetAgentEngineSessionConfigOrDict] = None,
1008
+ ) -> types.Session:
1009
+ """
1010
+ Gets an agent engine session.
1011
+
1012
+ Args:
1013
+ name (str): Required. The name of the Agent Engine session to get. Format:
1014
+ `projects/{project}/locations/{location}/reasoningEngines/{resource_id}/sessions/{session_id}`.
1015
+ config (GetAgentEngineSessionConfig):
1016
+ Optional. Additional configurations for getting the Agent Engine session.
1017
+
1018
+ Returns:
1019
+ AgentEngineSession: The requested Agent Engine session.
1020
+
1021
+ """
1022
+
1023
+ parameter_model = types._GetAgentEngineSessionRequestParameters(
1024
+ name=name,
1025
+ config=config,
1026
+ )
1027
+
1028
+ request_url_dict: Optional[dict[str, str]]
1029
+ if not self._api_client.vertexai:
1030
+ raise ValueError(
1031
+ "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini Developer API mode."
1032
+ )
1033
+ else:
1034
+ request_dict = _GetAgentEngineSessionRequestParameters_to_vertex(
1035
+ parameter_model
1036
+ )
1037
+ request_url_dict = request_dict.get("_url")
1038
+ if request_url_dict:
1039
+ path = "{name}".format_map(request_url_dict)
1040
+ else:
1041
+ path = "{name}"
1042
+
1043
+ query_params = request_dict.get("_query")
1044
+ if query_params:
1045
+ path = f"{path}?{urlencode(query_params)}"
1046
+ # TODO: remove the hack that pops config.
1047
+ request_dict.pop("config", None)
1048
+
1049
+ http_options: Optional[types.HttpOptions] = None
1050
+ if (
1051
+ parameter_model.config is not None
1052
+ and parameter_model.config.http_options is not None
1053
+ ):
1054
+ http_options = parameter_model.config.http_options
1055
+
1056
+ request_dict = _common.convert_to_dict(request_dict)
1057
+ request_dict = _common.encode_unserializable_types(request_dict)
1058
+
1059
+ response = await self._api_client.async_request(
1060
+ "get", path, request_dict, http_options
1061
+ )
1062
+
1063
+ response_dict = {} if not response.body else json.loads(response.body)
1064
+
1065
+ return_value = types.Session._from_response(
1066
+ response=response_dict,
1067
+ kwargs=(
1068
+ {
1069
+ "config": {
1070
+ "response_schema": getattr(
1071
+ parameter_model.config, "response_schema", None
1072
+ ),
1073
+ "response_json_schema": getattr(
1074
+ parameter_model.config, "response_json_schema", None
1075
+ ),
1076
+ "include_all_fields": getattr(
1077
+ parameter_model.config, "include_all_fields", None
1078
+ ),
1079
+ }
1080
+ }
1081
+ if getattr(parameter_model, "config", None)
1082
+ else {}
1083
+ ),
1084
+ )
1085
+
1086
+ self._api_client._verify_response(return_value)
1087
+ return return_value
1088
+
1089
+ async def _list(
1090
+ self,
1091
+ *,
1092
+ name: str,
1093
+ config: Optional[types.ListAgentEngineSessionsConfigOrDict] = None,
1094
+ ) -> types.ListReasoningEnginesSessionsResponse:
1095
+ """
1096
+ Lists Agent Engine sessions.
1097
+
1098
+ Args:
1099
+ name (str): Required. The name of the Agent Engine to list sessions for. Format:
1100
+ `projects/{project}/locations/{location}/reasoningEngines/{resource_id}`.
1101
+ config (ListAgentEngineSessionsConfig):
1102
+ Optional. Additional configurations for listing the Agent Engine sessions.
1103
+
1104
+ Returns:
1105
+ ListReasoningEnginesSessionsResponse: The requested Agent Engine sessions.
1106
+
1107
+ """
1108
+
1109
+ parameter_model = types._ListAgentEngineSessionsRequestParameters(
1110
+ name=name,
1111
+ config=config,
1112
+ )
1113
+
1114
+ request_url_dict: Optional[dict[str, str]]
1115
+ if not self._api_client.vertexai:
1116
+ raise ValueError(
1117
+ "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini Developer API mode."
1118
+ )
1119
+ else:
1120
+ request_dict = _ListAgentEngineSessionsRequestParameters_to_vertex(
1121
+ parameter_model
1122
+ )
1123
+ request_url_dict = request_dict.get("_url")
1124
+ if request_url_dict:
1125
+ path = "{name}/sessions".format_map(request_url_dict)
1126
+ else:
1127
+ path = "{name}/sessions"
1128
+
1129
+ query_params = request_dict.get("_query")
1130
+ if query_params:
1131
+ path = f"{path}?{urlencode(query_params)}"
1132
+ # TODO: remove the hack that pops config.
1133
+ request_dict.pop("config", None)
1134
+
1135
+ http_options: Optional[types.HttpOptions] = None
1136
+ if (
1137
+ parameter_model.config is not None
1138
+ and parameter_model.config.http_options is not None
1139
+ ):
1140
+ http_options = parameter_model.config.http_options
1141
+
1142
+ request_dict = _common.convert_to_dict(request_dict)
1143
+ request_dict = _common.encode_unserializable_types(request_dict)
1144
+
1145
+ response = await self._api_client.async_request(
1146
+ "get", path, request_dict, http_options
1147
+ )
1148
+
1149
+ response_dict = {} if not response.body else json.loads(response.body)
1150
+
1151
+ return_value = types.ListReasoningEnginesSessionsResponse._from_response(
1152
+ response=response_dict,
1153
+ kwargs=(
1154
+ {
1155
+ "config": {
1156
+ "response_schema": getattr(
1157
+ parameter_model.config, "response_schema", None
1158
+ ),
1159
+ "response_json_schema": getattr(
1160
+ parameter_model.config, "response_json_schema", None
1161
+ ),
1162
+ "include_all_fields": getattr(
1163
+ parameter_model.config, "include_all_fields", None
1164
+ ),
1165
+ }
1166
+ }
1167
+ if getattr(parameter_model, "config", None)
1168
+ else {}
1169
+ ),
1170
+ )
1171
+
1172
+ self._api_client._verify_response(return_value)
1173
+ return return_value
1174
+
1175
+ async def _get_session_operation(
1176
+ self,
1177
+ *,
1178
+ operation_name: str,
1179
+ config: Optional[types.GetAgentEngineOperationConfigOrDict] = None,
1180
+ ) -> types.AgentEngineSessionOperation:
1181
+ parameter_model = types._GetAgentEngineSessionOperationParameters(
1182
+ operation_name=operation_name,
1183
+ config=config,
1184
+ )
1185
+
1186
+ request_url_dict: Optional[dict[str, str]]
1187
+ if not self._api_client.vertexai:
1188
+ raise ValueError(
1189
+ "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini Developer API mode."
1190
+ )
1191
+ else:
1192
+ request_dict = _GetAgentEngineSessionOperationParameters_to_vertex(
1193
+ parameter_model
1194
+ )
1195
+ request_url_dict = request_dict.get("_url")
1196
+ if request_url_dict:
1197
+ path = "{operationName}".format_map(request_url_dict)
1198
+ else:
1199
+ path = "{operationName}"
1200
+
1201
+ query_params = request_dict.get("_query")
1202
+ if query_params:
1203
+ path = f"{path}?{urlencode(query_params)}"
1204
+ # TODO: remove the hack that pops config.
1205
+ request_dict.pop("config", None)
1206
+
1207
+ http_options: Optional[types.HttpOptions] = None
1208
+ if (
1209
+ parameter_model.config is not None
1210
+ and parameter_model.config.http_options is not None
1211
+ ):
1212
+ http_options = parameter_model.config.http_options
1213
+
1214
+ request_dict = _common.convert_to_dict(request_dict)
1215
+ request_dict = _common.encode_unserializable_types(request_dict)
1216
+
1217
+ response = await self._api_client.async_request(
1218
+ "get", path, request_dict, http_options
1219
+ )
1220
+
1221
+ response_dict = {} if not response.body else json.loads(response.body)
1222
+
1223
+ return_value = types.AgentEngineSessionOperation._from_response(
1224
+ response=response_dict,
1225
+ kwargs=(
1226
+ {
1227
+ "config": {
1228
+ "response_schema": getattr(
1229
+ parameter_model.config, "response_schema", None
1230
+ ),
1231
+ "response_json_schema": getattr(
1232
+ parameter_model.config, "response_json_schema", None
1233
+ ),
1234
+ "include_all_fields": getattr(
1235
+ parameter_model.config, "include_all_fields", None
1236
+ ),
1237
+ }
1238
+ }
1239
+ if getattr(parameter_model, "config", None)
1240
+ else {}
1241
+ ),
1242
+ )
1243
+
1244
+ self._api_client._verify_response(return_value)
1245
+ return return_value
1246
+
1247
+ async def _update(
1248
+ self,
1249
+ *,
1250
+ name: str,
1251
+ config: Optional[types.UpdateAgentEngineSessionConfigOrDict] = None,
1252
+ ) -> types.Session:
1253
+ """
1254
+ Updates an Agent Engine session.
1255
+
1256
+ Args:
1257
+ name (str): Required. The name of the Agent Engine session to be updated. Format:
1258
+ `projects/{project}/locations/{location}/reasoningEngines/{resource_id}/sessions/{session_id}`.
1259
+ config (UpdateAgentEngineSessionConfig):
1260
+ Optional. Additional configurations for updating the Agent Engine session.
1261
+
1262
+ Returns:
1263
+ types.Session: The updated Agent Engine session.
1264
+
1265
+ """
1266
+
1267
+ parameter_model = types._UpdateAgentEngineSessionRequestParameters(
1268
+ name=name,
1269
+ config=config,
1270
+ )
1271
+
1272
+ request_url_dict: Optional[dict[str, str]]
1273
+ if not self._api_client.vertexai:
1274
+ raise ValueError(
1275
+ "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini Developer API mode."
1276
+ )
1277
+ else:
1278
+ request_dict = _UpdateAgentEngineSessionRequestParameters_to_vertex(
1279
+ parameter_model
1280
+ )
1281
+ request_url_dict = request_dict.get("_url")
1282
+ if request_url_dict:
1283
+ path = "{name}".format_map(request_url_dict)
1284
+ else:
1285
+ path = "{name}"
1286
+
1287
+ query_params = request_dict.get("_query")
1288
+ if query_params:
1289
+ path = f"{path}?{urlencode(query_params)}"
1290
+ # TODO: remove the hack that pops config.
1291
+ request_dict.pop("config", None)
1292
+
1293
+ http_options: Optional[types.HttpOptions] = None
1294
+ if (
1295
+ parameter_model.config is not None
1296
+ and parameter_model.config.http_options is not None
1297
+ ):
1298
+ http_options = parameter_model.config.http_options
1299
+
1300
+ request_dict = _common.convert_to_dict(request_dict)
1301
+ request_dict = _common.encode_unserializable_types(request_dict)
1302
+
1303
+ response = await self._api_client.async_request(
1304
+ "patch", path, request_dict, http_options
1305
+ )
1306
+
1307
+ response_dict = {} if not response.body else json.loads(response.body)
1308
+
1309
+ return_value = types.Session._from_response(
1310
+ response=response_dict,
1311
+ kwargs=(
1312
+ {
1313
+ "config": {
1314
+ "response_schema": getattr(
1315
+ parameter_model.config, "response_schema", None
1316
+ ),
1317
+ "response_json_schema": getattr(
1318
+ parameter_model.config, "response_json_schema", None
1319
+ ),
1320
+ "include_all_fields": getattr(
1321
+ parameter_model.config, "include_all_fields", None
1322
+ ),
1323
+ }
1324
+ }
1325
+ if getattr(parameter_model, "config", None)
1326
+ else {}
1327
+ ),
1328
+ )
1329
+
1330
+ self._api_client._verify_response(return_value)
1331
+ return return_value
1332
+
1333
+ _events = None
1334
+
1335
+ @property
1336
+ def events(self) -> "session_events_module.AsyncSessionEvents":
1337
+ if self._events is None:
1338
+ try:
1339
+ # We need to lazy load the sessions.events module to handle the
1340
+ # possibility of ImportError when dependencies are not installed.
1341
+ self._events = importlib.import_module(".session_events", __package__)
1342
+ except ImportError as e:
1343
+ raise ImportError(
1344
+ "The 'agent_engines.sessions.events' module requires"
1345
+ "additional packages. Please install them using pip install "
1346
+ "google-cloud-aiplatform[agent_engines]"
1347
+ ) from e
1348
+ return self._events.AsyncSessionEvents(self._api_client) # type: ignore[no-any-return]
1349
+
1350
+ async def create(
1351
+ self,
1352
+ *,
1353
+ name: str,
1354
+ user_id: str,
1355
+ config: Optional[types.CreateAgentEngineSessionConfigOrDict] = None,
1356
+ ) -> types.AgentEngineSessionOperation:
1357
+ """Creates a new session in the Agent Engine.
1358
+
1359
+ Args:
1360
+ name (str):
1361
+ Required. The name of the agent engine to create the session for.
1362
+ user_id (str):
1363
+ Required. The user ID of the session.
1364
+ config (CreateAgentEngineSessionConfig):
1365
+ Optional. The configuration for the session to create.
1366
+
1367
+ Returns:
1368
+ AgentEngineSessionOperation: The operation for creating the session.
1369
+ """
1370
+ if config is None:
1371
+ config = types.CreateAgentEngineSessionConfig()
1372
+ elif isinstance(config, dict):
1373
+ config = types.CreateAgentEngineSessionConfig.model_validate(config)
1374
+ operation = await self._create(
1375
+ name=name,
1376
+ user_id=user_id,
1377
+ config=config,
1378
+ )
1379
+ if config.wait_for_completion:
1380
+ if not operation.done:
1381
+ operation = await _agent_engines_utils._await_async_operation(
1382
+ operation_name=operation.name,
1383
+ get_operation_fn=self._get_session_operation,
1384
+ poll_interval_seconds=0.5,
1385
+ )
1386
+ # We need to make a call to get the session because the operation
1387
+ # response might not contain the relevant fields.
1388
+ if operation.response:
1389
+ operation.response = await self.get(name=operation.response.name)
1390
+ elif operation.error:
1391
+ raise RuntimeError(f"Failed to create session: {operation.error}")
1392
+ else:
1393
+ raise RuntimeError(
1394
+ "Error retrieving session from the operation response. "
1395
+ f"Operation name: {operation.name}"
1396
+ )
1397
+ return operation
1398
+
1399
+ async def update(
1400
+ self,
1401
+ *,
1402
+ name: str,
1403
+ config: Optional[types.UpdateAgentEngineSessionConfigOrDict] = None,
1404
+ ) -> types.Session:
1405
+ """Updates an Agent Engine session.
1406
+
1407
+ Args:
1408
+ name (str):
1409
+ Required. The name of the Agent Engine session to be updated. Format:
1410
+ `projects/{project}/locations/{location}/reasoningEngines/{resource_id}/sessions/{session_id}`.
1411
+ config (UpdateAgentEngineSessionConfig):
1412
+ Optional. The configuration for the session to update.
1413
+
1414
+ Returns:
1415
+ Session: The updated Agent Engine session.
1416
+ """
1417
+ if config is None:
1418
+ config = types.UpdateAgentEngineSessionConfig()
1419
+ elif isinstance(config, dict):
1420
+ config = types.UpdateAgentEngineSessionConfig.model_validate(config)
1421
+ return await self._update(
1422
+ name=name,
1423
+ config=config,
1424
+ )
1425
+
1426
+ async def list(
1427
+ self,
1428
+ *,
1429
+ name: str,
1430
+ config: Optional[types.ListAgentEngineSessionsConfigOrDict] = None,
1431
+ ) -> AsyncPager[types.Session]:
1432
+ """Lists Agent Engine sessions.
1433
+
1434
+ Args:
1435
+ name (str): Required. The name of the agent engine to list sessions
1436
+ for.
1437
+ config (ListAgentEngineSessionConfig): Optional. The configuration
1438
+ for the sessions to list.
1439
+
1440
+ Returns:
1441
+ AsyncPager[Session]: An async pager of sessions.
1442
+ """
1443
+
1444
+ return AsyncPager(
1445
+ "sessions",
1446
+ functools.partial(self._list, name=name),
1447
+ await self._list(name=name, config=config),
1448
+ config,
1449
+ )