diff --git a/agentplatform/_genai/runtimes.py b/agentplatform/_genai/runtimes.py index 0ba49c550e..694aca36b5 100644 --- a/agentplatform/_genai/runtimes.py +++ b/agentplatform/_genai/runtimes.py @@ -15,6 +15,7 @@ # Code generated by the Google Gen AI SDK generator DO NOT EDIT. +import asyncio import builtins import importlib import json @@ -22,13 +23,14 @@ import typing from typing import Any, AsyncIterator, Iterator, Optional, Sequence, Tuple, Union from urllib.parse import urlencode +import warnings from google.genai import _api_module from google.genai import _common from google.genai import types as genai_types from google.genai._common import get_value_by_path as getv from google.genai._common import set_value_by_path as setv -from google.genai.pagers import Pager +from google.genai.pagers import AsyncPager, Pager from . import _runtimes_utils from . import types @@ -1507,32 +1509,16 @@ def _list_pager( config, ) - def check_query_job( + def _build_check_query_job_result( self, *, name: str, - config: Optional[types.CheckQueryJobRuntimeConfigOrDict] = None, + raw_response: Any, + retrieve_result: Optional[bool], ) -> types.CheckQueryJobResult: - """Checks a query job on an agent runtime and optionally returns the results. - - Args: - name (str): - Required. A fully-qualified resource name or ID. - config (CheckQueryJobRuntimeConfigOrDict): - Optional. The configuration for the check_query_job. If not provided, - the default configuration will be used. This can be used to specify - the following fields: - - retrieve_result: Whether to retrieve the results of the query job. - """ from google.cloud import storage # type: ignore[attr-defined] import json - if config is None: - config = types.CheckQueryJobRuntimeConfig() - elif isinstance(config, dict): - config = types.CheckQueryJobRuntimeConfig(**config) - - raw_response = self._api_client.request("get", name, {}) if hasattr(raw_response, "body"): operation = ( json.loads(raw_response.body) @@ -1574,13 +1560,17 @@ def check_query_job( error = getattr(operation, "error", None) result_str = None - if status == "SUCCESS" and config.retrieve_result and output_gcs_uri: + if status == "SUCCESS" and retrieve_result and output_gcs_uri: storage_client = storage.Client( project=self._api_client.project, credentials=self._api_client._credentials, ) - bucket_name = output_gcs_uri.replace("gs://", "").split("/")[0] - blob_name = output_gcs_uri.replace(f"gs://{bucket_name}/", "") + bucket_and_object = ( + output_gcs_uri[len("gs://") :] + if output_gcs_uri.startswith("gs://") + else output_gcs_uri + ) + bucket_name, _, blob_name = bucket_and_object.partition("/") bucket = storage_client.bucket(bucket_name) blob = bucket.blob(blob_name) if blob.exists(): @@ -1600,6 +1590,35 @@ def check_query_job( result=result_str, ) + def check_query_job( + self, + *, + name: str, + config: Optional[types.CheckQueryJobRuntimeConfigOrDict] = None, + ) -> types.CheckQueryJobResult: + """Checks a query job on an agent runtime and optionally returns the results. + + Args: + name (str): + Required. A fully-qualified resource name or ID. + config (CheckQueryJobRuntimeConfigOrDict): + Optional. The configuration for the check_query_job. If not provided, + the default configuration will be used. This can be used to specify + the following fields: + - retrieve_result: Whether to retrieve the results of the query job. + """ + if config is None: + config = types.CheckQueryJobRuntimeConfig() + elif isinstance(config, dict): + config = types.CheckQueryJobRuntimeConfig(**config) + + raw_response = self._api_client.request("get", name, {}) + return self._build_check_query_job_result( + name=name, + raw_response=raw_response, + retrieve_result=config.retrieve_result, + ) + def _is_lightweight_creation( self, agent: Any, config: types.AgentRuntimeConfig ) -> bool: @@ -1614,24 +1633,11 @@ def _is_lightweight_creation( return False return True - def run_query_job( + def _prepare_run_query_job( self, *, - name: str, config: Optional[types.RunQueryJobRuntimeConfigOrDict] = None, - ) -> types.RunQueryJobResult: - """Launches a long-running query job on an Agent Runtime - - Args: - name (str): - Required. A fully-qualified resource name or ID. - config (RunQueryJobRuntimeConfigOrDict): - Optional. The configuration for the async query. If not provided, - the default configuration will be used. This can be used to specify - the following fields: - - query: The query to send to the agent runtime. - - output_gcs_uri: The GCS URI to use for the output. - """ + ) -> Tuple[str, str, types._RunQueryJobRuntimeConfig]: from google.cloud import storage # type: ignore[attr-defined] from google.api_core import exceptions import uuid @@ -1647,36 +1653,41 @@ def run_query_job( raise ValueError("`output_gcs_uri` is required in the config object.") output_gcs_uri = config.output_gcs_uri - is_file = False - last_part = "" - if not output_gcs_uri.endswith("/"): - last_part = output_gcs_uri.split("/")[-1] - if "." in last_part: - is_file = True + if not output_gcs_uri.startswith("gs://"): + raise ValueError("`output_gcs_uri` must start with 'gs://'.") + + bucket_and_object = output_gcs_uri[len("gs://") :] + bucket_name, _, object_path = bucket_and_object.partition("/") + if not bucket_name: + raise ValueError("`output_gcs_uri` must include a GCS bucket name.") + + is_file = ( + bool(object_path) + and not object_path.endswith("/") + and "." in object_path.split("/")[-1] + ) if is_file: - path_parts = output_gcs_uri.split("/") - file_name = path_parts[-1] - base_uri = "/".join(path_parts[:-1]) - name_parts = file_name.rsplit(".", 1) - if len(name_parts) == 2: - name_part, ext = name_parts[0], "." + name_parts[1] - else: - name_part = name_parts[0] - ext = "" - input_gcs_uri = f"{base_uri}/{name_part}_input{ext}" + dir_prefix, _, file_name = object_path.rpartition("/") + name_part, ext_part = file_name.rsplit(".", 1) + input_file_name = f"{name_part}_input.{ext_part}" + input_blob_name = ( + f"{dir_prefix}/{input_file_name}" if dir_prefix else input_file_name + ) + input_gcs_uri = f"gs://{bucket_name}/{input_blob_name}" else: job_uuid = uuid.uuid4().hex - gcs_path = output_gcs_uri.rstrip("/") - input_gcs_uri = f"{gcs_path}/{job_uuid}_input.json" - output_gcs_uri = f"{gcs_path}/{job_uuid}_output.json" + trimmed_object_path = object_path.rstrip("/") + prefix = f"{trimmed_object_path}/" if trimmed_object_path else "" + input_blob_name = f"{prefix}{job_uuid}_input.json" + output_blob_name = f"{prefix}{job_uuid}_output.json" + input_gcs_uri = f"gs://{bucket_name}/{input_blob_name}" + output_gcs_uri = f"gs://{bucket_name}/{output_blob_name}" storage_client = storage.Client( - project=self._api_client.project, credentials=self._api_client._credentials + project=self._api_client.project, + credentials=self._api_client._credentials, ) - - # Handle creating the bucket if it does not exist - bucket_name = config.output_gcs_uri.replace("gs://", "").split("/")[0] bucket = storage_client.bucket(bucket_name) try: @@ -1696,7 +1707,6 @@ def run_query_job( "The service account may lack 'storage.buckets.create' permission." ) from e - input_blob_name = input_gcs_uri.replace(f"gs://{bucket_name}/", "") blob = bucket.blob(input_blob_name) blob.upload_from_string(config.query) @@ -1704,6 +1714,29 @@ def run_query_job( input_gcs_uri=input_gcs_uri, output_gcs_uri=output_gcs_uri, ) + return input_gcs_uri, output_gcs_uri, new_config + + def run_query_job( + self, + *, + name: str, + config: Optional[types.RunQueryJobRuntimeConfigOrDict] = None, + ) -> types.RunQueryJobResult: + """Launches a long-running query job on an Agent Runtime + + Args: + name (str): + Required. A fully-qualified resource name or ID. + config (RunQueryJobRuntimeConfigOrDict): + Optional. The configuration for the async query. If not provided, + the default configuration will be used. This can be used to specify + the following fields: + - query: The query to send to the agent runtime. + - output_gcs_uri: The GCS URI to use for the output. + """ + input_gcs_uri, output_gcs_uri, new_config = self._prepare_run_query_job( + config=config + ) # Proceed with sending the async query via the auto-generated method operation = self._run_query_job(name=name, config=new_config) @@ -1766,6 +1799,125 @@ def delete( logger.info(f"Started Runtime delete operation: {operation.name}") return operation + def _prepare_create_config( + self, + *, + runtime: Any = None, + agent: Any = None, + config: Optional[types.AgentRuntimeConfigOrDict] = None, + ) -> Tuple[Any, types.AgentRuntimeConfig, types.UpdateRuntimeConfigDict]: + if config is None: + config = {} + if isinstance(config, dict): + config = types.AgentRuntimeConfig.model_validate(config) + elif not isinstance(config, types.AgentRuntimeConfig): + raise TypeError( + f"config must be a dict or AgentRuntimeConfig, but got {type(config)}." + ) + context_spec = config.context_spec + if context_spec is not None: + # Conversion to a dict for _create_config + context_spec = json.loads(context_spec.model_dump_json()) + developer_connect_source = config.developer_connect_source + if developer_connect_source is not None: + developer_connect_source = json.loads( + developer_connect_source.model_dump_json() + ) + git_source = config.git_source + if git_source is not None: + git_source = json.loads(git_source.model_dump_json(exclude_none=True)) + agent_config_source = config.agent_config_source + if agent_config_source is not None: + agent_config_source = json.loads(agent_config_source.model_dump_json()) + keep_alive_probe = config.keep_alive_probe + if keep_alive_probe is not None: + keep_alive_probe = json.loads( + keep_alive_probe.model_dump_json(exclude_none=True) + ) + image_spec = config.image_spec + if image_spec is not None: + image_spec = json.loads(image_spec.model_dump_json()) + container_spec = config.container_spec + if container_spec is not None: + container_spec = json.loads(container_spec.model_dump_json()) + if agent and runtime: + raise ValueError("Please specify only one of `agent` or `runtime`.") + agent = agent or runtime + api_config = self._create_config( + mode="create", + agent=agent, + identity_type=config.identity_type, + staging_bucket=config.staging_bucket, + requirements=config.requirements, + display_name=config.display_name, + description=config.description, + gcs_dir_name=config.gcs_dir_name, + extra_packages=config.extra_packages, + env_vars=config.env_vars, + service_account=config.service_account, + context_spec=context_spec, + psc_interface_config=config.psc_interface_config, + agent_gateway_config=config.agent_gateway_config, + min_instances=config.min_instances, + max_instances=config.max_instances, + resource_limits=config.resource_limits, + container_concurrency=config.container_concurrency, + encryption_spec=config.encryption_spec, + agent_server_mode=config.agent_server_mode, + labels=config.labels, + class_methods=config.class_methods, + source_packages=config.source_packages, + developer_connect_source=developer_connect_source, + git_source=git_source, + entrypoint_module=config.entrypoint_module, + entrypoint_object=config.entrypoint_object, + requirements_file=config.requirements_file, + agent_framework=config.agent_framework, + python_version=config.python_version, + build_options=config.build_options, + image_spec=image_spec, + agent_config_source=agent_config_source, + container_spec=container_spec, + keep_alive_probe=keep_alive_probe, + build_config=config.build_config, + agent_card=config.agent_card, + ) + return agent, config, api_config + + def _build_runtime_from_operation( + self, + *, + operation: Any, + action: str, + async_client: "AsyncRuntimes", + register_api_methods: bool, + get_call_prefix: str, + ) -> types.Runtime: + runtime = types.Runtime( + api_client=self, + api_async_client=async_client, + api_resource=operation.response, + ) + if runtime.api_resource: + verb = "created" if action == "create" else "updated" + logger.info(f"Agent Runtime {verb}. To use it in another session:") + logger.info( + f"runtime={get_call_prefix}(name='{runtime.api_resource.name}')" + ) + elif operation.error: + _runtimes_utils._raise_for_operation_error( + operation=operation, + message=( + f"Operation {operation.name} failed to {action} Agent Runtime:" + f" {operation.error}" + ), + ) + elif action == "create": + logger.warning("The operation returned an empty response.") + if register_api_methods: + runtime = self._register_api_methods(runtime=runtime) + return runtime + def create( self, *, @@ -1844,79 +1996,16 @@ def create( IOError: If ``config.requirements` is a string that corresponds to a nonexistent file. """ - if config is None: - config = {} - if isinstance(config, dict): - config = types.AgentRuntimeConfig.model_validate(config) - elif not isinstance(config, types.AgentRuntimeConfig): - raise TypeError( - f"config must be a dict or AgentRuntimeConfig, but got {type(config)}." - ) - context_spec = config.context_spec - if context_spec is not None: - # Conversion to a dict for _create_config - context_spec = json.loads(context_spec.model_dump_json()) - developer_connect_source = config.developer_connect_source - if developer_connect_source is not None: - developer_connect_source = json.loads( - developer_connect_source.model_dump_json() + if runtime is not None: + warnings.warn( + "The `runtime` argument is deprecated. Please use `agent` instead.", + DeprecationWarning, + stacklevel=2, ) - git_source = config.git_source - if git_source is not None: - git_source = json.loads(git_source.model_dump_json(exclude_none=True)) - agent_config_source = config.agent_config_source - if agent_config_source is not None: - agent_config_source = json.loads(agent_config_source.model_dump_json()) - keep_alive_probe = config.keep_alive_probe - if keep_alive_probe is not None: - keep_alive_probe = json.loads( - keep_alive_probe.model_dump_json(exclude_none=True) - ) - if agent and runtime: - raise ValueError("Please specify only one of `agent` or `runtime`.") - elif runtime: - raise DeprecationWarning( - "The `runtime` argument is deprecated. Please use `agent` instead." - ) - agent = agent or runtime - api_config = self._create_config( - mode="create", + agent, config, api_config = self._prepare_create_config( + runtime=runtime, agent=agent, - identity_type=config.identity_type, - staging_bucket=config.staging_bucket, - requirements=config.requirements, - display_name=config.display_name, - description=config.description, - gcs_dir_name=config.gcs_dir_name, - extra_packages=config.extra_packages, - env_vars=config.env_vars, - service_account=config.service_account, - context_spec=context_spec, - psc_interface_config=config.psc_interface_config, - agent_gateway_config=config.agent_gateway_config, - min_instances=config.min_instances, - max_instances=config.max_instances, - resource_limits=config.resource_limits, - container_concurrency=config.container_concurrency, - encryption_spec=config.encryption_spec, - agent_server_mode=config.agent_server_mode, - labels=config.labels, - class_methods=config.class_methods, - source_packages=config.source_packages, - developer_connect_source=developer_connect_source, - git_source=git_source, - entrypoint_module=config.entrypoint_module, - entrypoint_object=config.entrypoint_object, - requirements_file=config.requirements_file, - agent_framework=config.agent_framework, - python_version=config.python_version, - build_options=config.build_options, - image_spec=config.image_spec, - agent_config_source=agent_config_source, - container_spec=config.container_spec, - keep_alive_probe=keep_alive_probe, - build_config=config.build_config, - agent_card=config.agent_card, + config=config, ) operation = self._create(config=api_config) logger.info(f"Create Agent Runtime backing LRO: {operation.name}") @@ -1939,31 +2028,13 @@ def create( poll_interval_seconds=poll_interval_seconds, ) - runtime = types.Runtime( - api_client=self, - api_async_client=AsyncRuntimes(api_client_=self._api_client), - api_resource=operation.response, + return self._build_runtime_from_operation( # type: ignore[no-any-return] + operation=operation, + action="create", + async_client=AsyncRuntimes(api_client_=self._api_client), + register_api_methods=not self._is_lightweight_creation(agent, config), + get_call_prefix="client.runtimes.get", ) - if runtime.api_resource: - logger.info("Agent Runtime created. To use it in another session:") - logger.info( - f"runtime=client.runtimes.get(name='{runtime.api_resource.name}')" - ) - elif operation.error: - _runtimes_utils._raise_for_operation_error( - operation=operation, - message=( - f"Operation {operation.name} failed to create Agent Runtime:" - f" {operation.error}" - ), - ) - else: - logger.warning("The operation returned an empty response.") - if not self._is_lightweight_creation(agent, config): - # If the user did not provide an runtime (e.g. lightweight - # provisioning), it will not have any API methods registered. - runtime = self._register_api_methods(runtime=runtime) - return runtime # type: ignore[no-any-return] def _set_source_code_spec( self, @@ -2641,60 +2712,21 @@ def list( Returns: Iterable[Runtime]: An iterable of Agent Runtimes matching the filter. """ - - for reasoning_engine in self._list_pager(config=config): - yield types.Runtime( - api_client=self, - api_async_client=AsyncRuntimes(api_client_=self._api_client), - api_resource=reasoning_engine, - ) - - def update( - self, - *, - name: str, - agent: Any = None, - runtime: Any = None, - config: types.AgentRuntimeConfigOrDict, - ) -> types.Runtime: - """Updates an existing Agent Runtime. - - This method updates the configuration of an existing Agent Runtime running - remotely, which is identified by its name. - - Args: - name (str): Required. A fully-qualified resource name or ID such as - "projects/123/locations/us-central1/reasoningEngines/456" or a - shortened name such as "reasoningEngines/456". - agent (Any): - Optional. The instance to be used as the updated Agent Runtime. - If it is not specified, the existing instance will be used. - runtime (Any): - Optional. This is deprecated. Please use `agent` instead. - config (AgentRuntimeConfig): - Optional. The configurations to use for updating the Agent Runtime. - - Returns: - Runtime: The updated Agent Runtime. - - Raises: - ValueError: If the `project` was not set using `client.Client`. - ValueError: If the `location` was not set using `client.Client`. - ValueError: If `config.staging_bucket` was not set when `runtime` - is specified. - ValueError: If `config.staging_bucket` does not start with "gs://". - ValueError: If `config.extra_packages` is specified but `runtime` - is None. - ValueError: If `config.requirements` is specified but `runtime` is - None. - ValueError: If `config.env_vars` has a dictionary entry that does not - correspond to an environment variable value or a SecretRef. - TypeError: If `config.env_vars` is not a dictionary. - FileNotFoundError: If `config.extra_packages` includes a file or - directory that does not exist. - IOError: If `config.requirements` is a string that corresponds to a - nonexistent file. - """ + + for reasoning_engine in self._list_pager(config=config): + yield types.Runtime( + api_client=self, + api_async_client=AsyncRuntimes(api_client_=self._api_client), + api_resource=reasoning_engine, + ) + + def _prepare_update_config( + self, + *, + agent: Any = None, + runtime: Any = None, + config: types.AgentRuntimeConfigOrDict, + ) -> types.UpdateRuntimeConfigDict: if isinstance(config, dict): config = types.AgentRuntimeConfig.model_validate(config) elif not isinstance(config, types.AgentRuntimeConfig): @@ -2726,10 +2758,6 @@ def update( traffic_config = json.loads(traffic_config.model_dump_json()) if agent and runtime: raise ValueError("Please specify only one of `agent` or `runtime`.") - elif runtime: - raise DeprecationWarning( - "The `runtime` argument is deprecated. Please use `agent` instead." - ) image_spec = config.image_spec if image_spec is not None: # Conversion to a dict for _create_config @@ -2739,7 +2767,7 @@ def update( # Conversion to a dict for _create_config container_spec = json.loads(container_spec.model_dump_json()) agent = agent or runtime - api_config = self._create_config( + return self._create_config( mode="update", agent=agent, identity_type=config.identity_type, @@ -2777,6 +2805,64 @@ def update( build_config=config.build_config, agent_card=config.agent_card, ) + + def update( + self, + *, + name: str, + agent: Any = None, + runtime: Any = None, + config: types.AgentRuntimeConfigOrDict, + ) -> types.Runtime: + """Updates an existing Agent Runtime. + + This method updates the configuration of an existing Agent Runtime running + remotely, which is identified by its name. + + Args: + name (str): Required. A fully-qualified resource name or ID such as + "projects/123/locations/us-central1/reasoningEngines/456" or a + shortened name such as "reasoningEngines/456". + agent (Any): + Optional. The instance to be used as the updated Agent Runtime. + If it is not specified, the existing instance will be used. + runtime (Any): + Optional. This is deprecated. Please use `agent` instead. + config (AgentRuntimeConfig): + Optional. The configurations to use for updating the Agent Runtime. + + Returns: + Runtime: The updated Agent Runtime. + + Raises: + ValueError: If the `project` was not set using `client.Client`. + ValueError: If the `location` was not set using `client.Client`. + ValueError: If `config.staging_bucket` was not set when `runtime` + is specified. + ValueError: If `config.staging_bucket` does not start with "gs://". + ValueError: If `config.extra_packages` is specified but `runtime` + is None. + ValueError: If `config.requirements` is specified but `runtime` is + None. + ValueError: If `config.env_vars` has a dictionary entry that does not + correspond to an environment variable value or a SecretRef. + TypeError: If `config.env_vars` is not a dictionary. + FileNotFoundError: If `config.extra_packages` includes a file or + directory that does not exist. + IOError: If `config.requirements` is a string that corresponds to a + nonexistent file. + """ + if runtime is not None: + warnings.warn( + "The `runtime` argument is deprecated. Please use `agent` instead.", + DeprecationWarning, + stacklevel=2, + ) + api_config = self._prepare_update_config( + agent=agent, + runtime=runtime, + config=config, + ) operation = self._update(name=name, config=api_config) logger.info(f"Update Agent Runtime backing LRO: {operation.name}") reasoning_engine_id = _runtimes_utils._get_reasoning_engine_id( @@ -2792,27 +2878,13 @@ def update( operation_name=operation.name, get_operation_fn=self._get_agent_operation, ) - runtime = types.Runtime( - api_client=self, - api_async_client=AsyncRuntimes(api_client_=self._api_client), - api_resource=operation.response, + return self._build_runtime_from_operation( # type: ignore[no-any-return] + operation=operation, + action="update", + async_client=AsyncRuntimes(api_client_=self._api_client), + register_api_methods=bool(operation.response and operation.response.spec), + get_call_prefix="client.runtimes.get", ) - if runtime.api_resource: - logger.info("Agent Runtime updated. To use it in another session:") - logger.info( - f"runtime=client.runtimes.get(name='{runtime.api_resource.name}')" - ) - elif operation.error: - _runtimes_utils._raise_for_operation_error( - operation=operation, - message=( - f"Operation {operation.name} failed to update Agent Runtime:" - f" {operation.error}" - ), - ) - if runtime.api_resource.spec: - self._register_api_methods(runtime=runtime) - return runtime # type: ignore[no-any-return] def _stream_query( self, *, name: str, config: Optional[types.QueryRuntimeConfigOrDict] = None @@ -3656,6 +3728,106 @@ async def _update( _revisions = None + async def _list_pager( + self, *, config: Optional[types.ListRuntimeConfigOrDict] = None + ) -> AsyncPager[types.ReasoningEngine]: + return AsyncPager( + "reasoning_engines", + self._list, + await self._list(config=config), + config, + ) + + async def check_query_job( + self, + *, + name: str, + config: Optional[types.CheckQueryJobRuntimeConfigOrDict] = None, + ) -> types.CheckQueryJobResult: + """Checks a query job on an agent runtime and optionally returns the results. + + Args: + name (str): + Required. A fully-qualified resource name or ID. + config (CheckQueryJobRuntimeConfigOrDict): + Optional. The configuration for the check_query_job. If not provided, + the default configuration will be used. This can be used to specify + the following fields: + - retrieve_result: Whether to retrieve the results of the query job. + """ + if config is None: + config = types.CheckQueryJobRuntimeConfig() + elif isinstance(config, dict): + config = types.CheckQueryJobRuntimeConfig(**config) + + sync_runtimes = Runtimes(api_client_=self._api_client) + raw_response = await self._api_client.async_request("get", name, {}) + return await asyncio.to_thread( + sync_runtimes._build_check_query_job_result, + name=name, + raw_response=raw_response, + retrieve_result=config.retrieve_result, + ) + + async def run_query_job( + self, + *, + name: str, + config: Optional[types.RunQueryJobRuntimeConfigOrDict] = None, + ) -> types.RunQueryJobResult: + """Launches a long-running query job on an Agent Runtime. + + Args: + name (str): + Required. A fully-qualified resource name or ID. + config (RunQueryJobRuntimeConfigOrDict): + Optional. The configuration for the async query. If not provided, + the default configuration will be used. This can be used to specify + the following fields: + - query: The query to send to the agent runtime. + - output_gcs_uri: The GCS URI to use for the output. + """ + sync_runtimes = Runtimes(api_client_=self._api_client) + input_gcs_uri, output_gcs_uri, new_config = await asyncio.to_thread( + sync_runtimes._prepare_run_query_job, + config=config, + ) + + operation = await self._run_query_job(name=name, config=new_config) + + return types.RunQueryJobResult( + job_name=operation.name, + input_gcs_uri=input_gcs_uri, + output_gcs_uri=output_gcs_uri, + ) + + async def get( + self, + *, + name: str, + config: Optional[types.GetRuntimeConfigOrDict] = None, + ) -> types.Runtime: + """Gets an agent runtime. + + Args: + name (str): + Required. A fully-qualified resource name or ID such as + "projects/123/locations/us-central1/reasoningEngines/456" or + a shortened name such as "reasoningEngines/456". + config (GetRuntimeConfigOrDict): + Optional. Additional configurations for getting the Agent Runtime. + """ + sync_runtimes = Runtimes(api_client_=self._api_client) + api_resource = await self._get(name=name, config=config) + runtime = types.Runtime( + api_client=sync_runtimes, + api_async_client=self, + api_resource=api_resource, + ) + if api_resource.spec: + sync_runtimes._register_api_methods(runtime=runtime) + return runtime + async def delete( self, *, @@ -3684,6 +3856,155 @@ async def delete( logger.info(f"Started Runtime delete operation: {operation.name}") return operation + async def create( + self, + *, + runtime: Any = None, + agent: Any = None, + config: Optional[types.AgentRuntimeConfigOrDict] = None, + ) -> types.Runtime: + """Creates an agent runtime asynchronously. + + Args: + agent (Any): + Optional. The Agent to be created. If not specified, this will + correspond to a lightweight instance that cannot be queried + (but can be updated to future instances that can be queried). + runtime (Any): + Optional. This is deprecated. Please use `agent` instead. + config (AgentRuntimeConfig): + Optional. The configurations to use for creating the Agent Runtime. + + Returns: + Runtime: The created Agent Runtime instance. + """ + if runtime is not None: + warnings.warn( + "The `runtime` argument is deprecated. Please use `agent` instead.", + DeprecationWarning, + stacklevel=2, + ) + sync_runtimes = Runtimes(api_client_=self._api_client) + agent, config, api_config = await asyncio.to_thread( + sync_runtimes._prepare_create_config, + runtime=runtime, + agent=agent, + config=config, + ) + operation = await self._create(config=api_config) + logger.info(f"Create Agent Runtime backing LRO: {operation.name}") + reasoning_engine_id = _runtimes_utils._get_reasoning_engine_id( + operation_name=operation.name + ) + logger.info( + "View progress and logs at https://console.cloud.google.com/logs/query?" + f"project={self._api_client.project}" + "&query=resource.type%3D%22aiplatform.googleapis.com%2FReasoningEngine%22%0A" + f"resource.labels.reasoning_engine_id%3D%22{reasoning_engine_id}%22." + ) + if not sync_runtimes._is_lightweight_creation(agent, config): + poll_interval_seconds = 10 + else: + poll_interval_seconds = 1 + operation = await _runtimes_utils._await_async_operation( + operation_name=operation.name, + get_operation_fn=self._get_agent_operation, + poll_interval_seconds=poll_interval_seconds, + ) + + return sync_runtimes._build_runtime_from_operation( # type: ignore[no-any-return] + operation=operation, + action="create", + async_client=self, + register_api_methods=not sync_runtimes._is_lightweight_creation( + agent, config + ), + get_call_prefix="await client.aio.runtimes.get", + ) + + async def list( + self, *, config: Optional[types.ListRuntimeConfigOrDict] = None + ) -> AsyncIterator[types.Runtime]: + """List all instances of Agent Runtime matching the filter. + + Args: + config (ListRuntimeConfig): + Optional. The config (e.g. filter) for the agents to be listed. + + Returns: + AsyncIterator[Runtime]: An async iterator of Agent Runtimes matching the + filter. + """ + sync_runtimes = Runtimes(api_client_=self._api_client) + list_pager = await self._list_pager(config=config) + async for reasoning_engine in list_pager: + yield types.Runtime( + api_client=sync_runtimes, + api_async_client=self, + api_resource=reasoning_engine, + ) + + async def update( + self, + *, + name: str, + agent: Any = None, + runtime: Any = None, + config: types.AgentRuntimeConfigOrDict, + ) -> types.Runtime: + """Updates an existing Agent Runtime asynchronously. + + Args: + name (str): Required. A fully-qualified resource name or ID such as + "projects/123/locations/us-central1/reasoningEngines/456" or a + shortened name such as "reasoningEngines/456". + agent (Any): + Optional. The instance to be used as the updated Agent Runtime. + If it is not specified, the existing instance will be used. + runtime (Any): + Optional. This is deprecated. Please use `agent` instead. + config (AgentRuntimeConfig): + Optional. The configurations to use for updating the Agent Runtime. + + Returns: + Runtime: The updated Agent Runtime. + """ + if runtime is not None: + warnings.warn( + "The `runtime` argument is deprecated. Please use `agent` instead.", + DeprecationWarning, + stacklevel=2, + ) + sync_runtimes = Runtimes(api_client_=self._api_client) + api_config = await asyncio.to_thread( + sync_runtimes._prepare_update_config, + agent=agent, + runtime=runtime, + config=config, + ) + operation = await self._update(name=name, config=api_config) + logger.info(f"Update Agent Runtime backing LRO: {operation.name}") + reasoning_engine_id = _runtimes_utils._get_reasoning_engine_id( + resource_name=name + ) + logger.info( + "View progress and logs at https://console.cloud.google.com/logs/query?" + f"project={self._api_client.project}" + "&query=resource.type%3D%22aiplatform.googleapis.com%2FReasoningEngine%22%0A" + f"resource.labels.reasoning_engine_id%3D%22{reasoning_engine_id}%22." + ) + operation = await _runtimes_utils._await_async_operation( + operation_name=operation.name, + get_operation_fn=self._get_agent_operation, + ) + return sync_runtimes._build_runtime_from_operation( # type: ignore[no-any-return] + operation=operation, + action="update", + async_client=self, + register_api_methods=bool(operation.response and operation.response.spec), + get_call_prefix="await client.aio.runtimes.get", + ) + @property def revisions(self) -> "runtime_revisions_module.AsyncRuntimeRevisions": if self._revisions is None: diff --git a/tests/unit/agentplatform/genai/test_agent_engines.py b/tests/unit/agentplatform/genai/test_agent_engines.py index afb16e0e3a..e093a3faaa 100644 --- a/tests/unit/agentplatform/genai/test_agent_engines.py +++ b/tests/unit/agentplatform/genai/test_agent_engines.py @@ -28,19 +28,19 @@ from google import auth from google.auth import credentials as auth_credentials -from google.cloud import aiplatform import agentplatform -from google.cloud.aiplatform import initializer -from agentplatform.frameworks import adk +from google.cloud import aiplatform from agentplatform._genai import _runtimes_utils from agentplatform._genai import runtimes from agentplatform._genai import types as _genai_types +from agentplatform.frameworks import adk +from google.cloud.aiplatform import initializer from google.genai import client as genai_client from google.genai import errors as genai_errors from google.genai import types as genai_types -from google.api import httpbody_pb2 import pytest +from google.api import httpbody_pb2 _TEST_AGENT_FRAMEWORK = "google-adk" GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY = ( @@ -364,9 +364,7 @@ def register_operations(self) -> Dict[str, List[str]]: _TEST_DEFAULT_ASYNC_STREAM_METHOD_NAME = ( _runtimes_utils._DEFAULT_ASYNC_STREAM_METHOD_NAME ) -_TEST_DEFAULT_BIDI_STREAM_METHOD_NAME = ( - _runtimes_utils._DEFAULT_BIDI_STREAM_METHOD_NAME -) +_TEST_DEFAULT_BIDI_STREAM_METHOD_NAME = _runtimes_utils._DEFAULT_BIDI_STREAM_METHOD_NAME _TEST_CAPITALIZE_ENGINE_METHOD_DOCSTRING = "Runs the engine." _TEST_STREAM_METHOD_DOCSTRING = "Runs the stream engine." _TEST_ASYNC_STREAM_METHOD_DOCSTRING = "Runs the async stream engine." @@ -652,11 +650,9 @@ def register_operations(self) -> Dict[str, List[str]]: _TEST_AGENT_ENGINE_ASYNC_STREAM_QUERY_SCHEMA[_TEST_MODE_KEY_IN_SCHEMA] = ( _TEST_ASYNC_STREAM_API_MODE ) -_TEST_AGENT_ENGINE_CUSTOM_ASYNC_STREAM_QUERY_SCHEMA = ( - _runtimes_utils._generate_schema( - OperationRegistrableEngine().custom_async_stream_method, - schema_name=_TEST_CUSTOM_ASYNC_STREAM_METHOD_NAME, - ) +_TEST_AGENT_ENGINE_CUSTOM_ASYNC_STREAM_QUERY_SCHEMA = _runtimes_utils._generate_schema( + OperationRegistrableEngine().custom_async_stream_method, + schema_name=_TEST_CUSTOM_ASYNC_STREAM_METHOD_NAME, ) _TEST_AGENT_ENGINE_CUSTOM_ASYNC_STREAM_QUERY_SCHEMA[_TEST_MODE_KEY_IN_SCHEMA] = ( _TEST_ASYNC_STREAM_API_MODE @@ -668,11 +664,9 @@ def register_operations(self) -> Dict[str, List[str]]: _TEST_AGENT_ENGINE_BIDI_STREAM_QUERY_SCHEMA[_TEST_MODE_KEY_IN_SCHEMA] = ( _TEST_BIDI_STREAM_API_MODE ) -_TEST_AGENT_ENGINE_CUSTOM_BIDI_STREAM_QUERY_SCHEMA = ( - _runtimes_utils._generate_schema( - OperationRegistrableEngine().custom_bidi_stream_method, - schema_name=_TEST_CUSTOM_BIDI_STREAM_METHOD_NAME, - ) +_TEST_AGENT_ENGINE_CUSTOM_BIDI_STREAM_QUERY_SCHEMA = _runtimes_utils._generate_schema( + OperationRegistrableEngine().custom_bidi_stream_method, + schema_name=_TEST_CUSTOM_BIDI_STREAM_METHOD_NAME, ) _TEST_AGENT_ENGINE_CUSTOM_BIDI_STREAM_QUERY_SCHEMA[_TEST_MODE_KEY_IN_SCHEMA] = ( _TEST_BIDI_STREAM_API_MODE @@ -1771,9 +1765,7 @@ def test_update_runtime_config_with_agent_card(self, mock_prepare): ) @mock.patch.object(_runtimes_utils, "_prepare") - def test_update_runtime_config_without_agent_card_omits_mask( - self, mock_prepare - ): + def test_update_runtime_config_without_agent_card_omits_mask(self, mock_prepare): config = self.client.runtimes._create_config( mode="update", agent=self.test_agent, @@ -2130,9 +2122,7 @@ def test_yield_parsed_json_from_httpbody_event_stream(self): def test_yield_parsed_json_from_httpbody_non_json_content_type(self): body = httpbody_pb2.HttpBody(content_type="text/plain", data=b"hello") - assert list(_runtimes_utils._yield_parsed_json_from_httpbody(body)) == [ - body - ] + assert list(_runtimes_utils._yield_parsed_json_from_httpbody(body)) == [body] def test_create_base64_encoded_tarball(self): with tempfile.TemporaryDirectory() as tmpdir: @@ -3198,9 +3188,7 @@ def test_update_runtime_git_source(self, mock_await_operation): @mock.patch.object(_runtimes_utils, "_prepare") @mock.patch.object(_runtimes_utils, "_await_operation") - def test_update_runtime_extra_packages( - self, mock_await_operation, mock_prepare - ): + def test_update_runtime_extra_packages(self, mock_await_operation, mock_prepare): mock_await_operation.return_value = _genai_types.RuntimeOperation( response=_genai_types.ReasoningEngine( name=_TEST_AGENT_ENGINE_RESOURCE_NAME, @@ -3263,9 +3251,7 @@ def test_create_runtime_deployment_config_without_agent_raises( ) @mock.patch.object(_runtimes_utils, "_await_operation") - def test_update_runtime_deployment_config_without_agent( - self, mock_await_operation - ): + def test_update_runtime_deployment_config_without_agent(self, mock_await_operation): mock_await_operation.return_value = _genai_types.RuntimeOperation( response=_genai_types.ReasoningEngine( name=_TEST_AGENT_ENGINE_RESOURCE_NAME, @@ -3411,9 +3397,7 @@ def test_update_runtime_empty_deployment_field_without_mask_omits_spec( @mock.patch.object(_runtimes_utils, "_prepare") @mock.patch.object(_runtimes_utils, "_await_operation") - def test_update_runtime_env_vars( - self, mock_await_operation, mock_prepare, caplog - ): + def test_update_runtime_env_vars(self, mock_await_operation, mock_prepare, caplog): mock_await_operation.return_value = _genai_types.RuntimeOperation( response=_genai_types.ReasoningEngine( name=_TEST_AGENT_ENGINE_RESOURCE_NAME, @@ -4225,9 +4209,7 @@ def test_query_runtime_stream(self, http_options_arg, expected_http_options): ], ids=["default", "http_options_instance", "http_options_dict"], ) - def test_query_runtime_async_stream( - self, http_options_arg, expected_http_options - ): + def test_query_runtime_async_stream(self, http_options_arg, expected_http_options): """Async streaming query: forwards http_options to the HTTP layer (or None).""" kwargs = {"query": _TEST_QUERY_PROMPT} if http_options_arg is not _UNSET_HTTP_OPTIONS: @@ -4766,15 +4748,439 @@ def setup_method(self): def teardown_method(self): initializer.global_pool.shutdown(wait=True) - def test_delete_runtime(self): + def test_get_runtime(self): + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + body=json.dumps( + { + "name": _TEST_AGENT_ENGINE_RESOURCE_NAME, + "spec": { + "classMethods": [ + _TEST_AGENT_ENGINE_CLASS_METHOD_1, + _TEST_AGENT_ENGINE_CLASS_METHOD_ASYNC_QUERY, + ] + }, + } + ) + ) + runtime = asyncio.run( + self.client.aio.runtimes.get(name=_TEST_AGENT_ENGINE_RESOURCE_NAME) + ) + request_mock.assert_called_with( + "get", + _TEST_AGENT_ENGINE_RESOURCE_NAME, + {"_url": {"name": _TEST_AGENT_ENGINE_RESOURCE_NAME}}, + None, + ) + assert isinstance(runtime, _genai_types.Runtime) + assert runtime.api_resource.name == _TEST_AGENT_ENGINE_RESOURCE_NAME + assert callable(runtime.query) + assert callable(runtime.async_query) + + def test_get_runtime_and_async_query(self): + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.side_effect = [ + genai_types.HttpResponse( + body=json.dumps( + { + "name": _TEST_AGENT_ENGINE_RESOURCE_NAME, + "spec": { + "classMethods": [ + _TEST_AGENT_ENGINE_CLASS_METHOD_ASYNC_QUERY, + ] + }, + } + ) + ), + genai_types.HttpResponse(body=json.dumps({"output": "RESULT"})), + ] + + async def get_and_query(): + runtime = await self.client.aio.runtimes.get( + name=_TEST_AGENT_ENGINE_RESOURCE_NAME + ) + return await runtime.async_query(query=_TEST_QUERY_PROMPT) + + output = asyncio.run(get_and_query()) + assert output == "RESULT" + request_mock.assert_called_with( + "post", + f"{_TEST_AGENT_ENGINE_RESOURCE_NAME}:query", + { + "_url": {"name": _TEST_AGENT_ENGINE_RESOURCE_NAME}, + "classMethod": "async_query", + "input": {"query": _TEST_QUERY_PROMPT}, + }, + None, + ) + + def test_list_runtime(self): + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + body=json.dumps( + {"reasoningEngines": [{"name": _TEST_AGENT_ENGINE_RESOURCE_NAME}]} + ) + ) + expected_query_params = {"filter": _TEST_AGENT_ENGINE_LIST_FILTER} + + async def collect_runtimes(): + return [ + item + async for item in self.client.aio.runtimes.list( + config=expected_query_params + ) + ] + + items = asyncio.run(collect_runtimes()) + assert len(items) == 1 + assert isinstance(items[0], _genai_types.Runtime) + assert items[0].api_resource.name == _TEST_AGENT_ENGINE_RESOURCE_NAME + request_mock.assert_called_with( + "get", + f"reasoningEngines?{urlencode(expected_query_params)}", + {"_query": expected_query_params}, + None, + ) + + @mock.patch.object(_runtimes_utils, "_prepare") + @mock.patch.object(_runtimes_utils, "_await_async_operation") + @mock.patch.object( + _runtimes_utils, + "_get_reasoning_engine_id", + return_value=_TEST_RESOURCE_ID, + ) + def test_create_runtime( + self, + mock_get_reasoning_engine_id, + mock_await_async_operation, + mock_prepare, + ): + mock_await_async_operation.return_value = _genai_types.RuntimeOperation( + response=_genai_types.ReasoningEngine( + name=_TEST_AGENT_ENGINE_RESOURCE_NAME, + spec=_TEST_AGENT_ENGINE_SPEC, + ) + ) + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + body=json.dumps({"name": _TEST_AGENT_ENGINE_OPERATION_NAME}) + ) + created_runtime = asyncio.run( + self.client.aio.runtimes.create( + agent=self.test_agent, + config=_genai_types.AgentRuntimeConfig( + display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME, + staging_bucket=_TEST_STAGING_BUCKET, + ), + ) + ) + assert isinstance(created_runtime, _genai_types.Runtime) + assert created_runtime.api_resource.name == _TEST_AGENT_ENGINE_RESOURCE_NAME + assert callable(created_runtime.query) + mock_await_async_operation.assert_called_once() + request_mock.assert_called_once() + assert request_mock.call_args[0][0] == "post" + assert request_mock.call_args[0][1] == "reasoningEngines" + + @mock.patch.object(_runtimes_utils, "_prepare") + @mock.patch.object(_runtimes_utils, "_await_async_operation") + @mock.patch.object( + _runtimes_utils, + "_get_reasoning_engine_id", + return_value=_TEST_RESOURCE_ID, + ) + def test_create_runtime_error( + self, + mock_get_reasoning_engine_id, + mock_await_async_operation, + mock_prepare, + ): + mock_await_async_operation.return_value = _genai_types.RuntimeOperation( + error=_TEST_AGENT_ENGINE_ERROR, + ) + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse(body="") + with pytest.raises(genai_errors.APIError) as excinfo: + asyncio.run( + self.client.aio.runtimes.create( + agent=self.test_agent, + config=_genai_types.AgentRuntimeConfig( + display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME, + staging_bucket=_TEST_STAGING_BUCKET, + ), + ) + ) + assert excinfo.value.code == _TEST_AGENT_ENGINE_ERROR["code"] + assert excinfo.value.message == _TEST_AGENT_ENGINE_ERROR["message"] + + @mock.patch.object(_runtimes_utils, "_await_async_operation") + def test_update_runtime(self, mock_await_async_operation): + mock_await_async_operation.return_value = _genai_types.RuntimeOperation( + response=_genai_types.ReasoningEngine( + name=_TEST_AGENT_ENGINE_RESOURCE_NAME, + spec=_TEST_AGENT_ENGINE_SPEC, + ) + ) + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + body=json.dumps({"name": _TEST_AGENT_ENGINE_OPERATION_NAME}) + ) + updated_runtime = asyncio.run( + self.client.aio.runtimes.update( + name=_TEST_AGENT_ENGINE_RESOURCE_NAME, + config=_genai_types.AgentRuntimeConfig( + description=_TEST_AGENT_ENGINE_DESCRIPTION, + ), + ) + ) + assert isinstance(updated_runtime, _genai_types.Runtime) + assert updated_runtime.api_resource.name == _TEST_AGENT_ENGINE_RESOURCE_NAME + mock_await_async_operation.assert_called_once() + request_mock.assert_called_once_with( + "patch", + f"{_TEST_AGENT_ENGINE_RESOURCE_NAME}?updateMask=description", + { + "_url": {"name": _TEST_AGENT_ENGINE_RESOURCE_NAME}, + "_query": {"updateMask": "description"}, + "description": _TEST_AGENT_ENGINE_DESCRIPTION, + }, + None, + ) + + @mock.patch.object(_runtimes_utils, "_await_async_operation") + def test_update_runtime_error(self, mock_await_async_operation): + mock_await_async_operation.return_value = _genai_types.RuntimeOperation( + error=_TEST_AGENT_ENGINE_ERROR, + ) with mock.patch.object( self.client.aio.runtimes._api_client, "async_request" ) as request_mock: request_mock.return_value = genai_types.HttpResponse(body="") + with pytest.raises(genai_errors.APIError) as excinfo: + asyncio.run( + self.client.aio.runtimes.update( + name=_TEST_AGENT_ENGINE_RESOURCE_NAME, + config=_genai_types.AgentRuntimeConfig( + description=_TEST_AGENT_ENGINE_DESCRIPTION, + ), + ) + ) + assert excinfo.value.code == _TEST_AGENT_ENGINE_ERROR["code"] + assert excinfo.value.message == _TEST_AGENT_ENGINE_ERROR["message"] + + def test_check_query_job_runtime(self): + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + headers={}, + body=( + '{"done": true, "name": ' + '"projects/123/locations/us-central1/reasoningEngines/456/operations/789", ' + '"response": {"output_gcs_uri": "gs://my-output-bucket/output.json"}}' + ), + ) + with mock.patch("google.cloud.storage.Client") as mock_storage_client: + mock_bucket = mock.Mock() + mock_blob = mock.Mock() + mock_blob.exists.return_value = True + mock_blob.download_as_string.return_value = b'{"success": true}' + mock_bucket.blob.return_value = mock_blob + mock_storage_client.return_value.bucket.return_value = mock_bucket + + result = asyncio.run( + self.client.aio.runtimes.check_query_job( + name="projects/123/locations/us-central1/reasoningEngines/456/operations/789", + config={"retrieve_result": True}, + ) + ) + + assert result == _genai_types.CheckQueryJobResult( + operation_name="projects/123/locations/us-central1/reasoningEngines/456/operations/789", + status="SUCCESS", + output_gcs_uri="gs://my-output-bucket/output.json", + result='{"success": true}', + ) + request_mock.assert_called_once_with( + "get", + "projects/123/locations/us-central1/reasoningEngines/456/operations/789", + {}, + ) + + @mock.patch("google.cloud.storage.Client") + def test_run_query_job_runtime(self, mock_storage_client): + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + body='{"name": "projects/123/locations/us-central1/reasoningEngines/456/operations/789"}' + ) + mock_bucket = mock.Mock() + mock_bucket.exists.return_value = True + mock_blob = mock.Mock() + mock_bucket.blob.return_value = mock_blob + mock_storage_client.return_value.bucket.return_value = mock_bucket + + result = asyncio.run( + self.client.aio.runtimes.run_query_job( + name=_TEST_AGENT_ENGINE_RESOURCE_NAME, + config={ + "query": _TEST_QUERY_PROMPT, + "output_gcs_uri": "gs://my-input-bucket/path/output.json", + }, + ) + ) + mock_blob.upload_from_string.assert_called_once_with(_TEST_QUERY_PROMPT) + assert result == _genai_types.RunQueryJobResult( + job_name="projects/123/locations/us-central1/reasoningEngines/456/operations/789", + input_gcs_uri="gs://my-input-bucket/path/output_input.json", + output_gcs_uri="gs://my-input-bucket/path/output.json", + ) + + @mock.patch("google.cloud.storage.Client") + @mock.patch("uuid.uuid4") + def test_run_query_job_runtime_dotted_bucket_no_slash( + self, mock_uuid, mock_storage_client + ): + mock_uuid.return_value.hex = "b92b9b89-4585-4146-8ee5-22fe99802a8e" + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + body='{"name": "projects/123/locations/us-central1/reasoningEngines/456/operations/789"}' + ) + mock_bucket = mock.Mock() + mock_bucket.exists.return_value = True + mock_blob = mock.Mock() + mock_bucket.blob.return_value = mock_blob + mock_storage_client.return_value.bucket.return_value = mock_bucket + + result = asyncio.run( + self.client.aio.runtimes.run_query_job( + name=_TEST_AGENT_ENGINE_RESOURCE_NAME, + config={ + "query": _TEST_QUERY_PROMPT, + "output_gcs_uri": "gs://my.company.bucket", + }, + ) + ) + mock_storage_client.return_value.bucket.assert_called_once_with( + "my.company.bucket" + ) + mock_bucket.blob.assert_called_once_with( + "b92b9b89-4585-4146-8ee5-22fe99802a8e_input.json" + ) + assert result == _genai_types.RunQueryJobResult( + job_name="projects/123/locations/us-central1/reasoningEngines/456/operations/789", + input_gcs_uri="gs://my.company.bucket/b92b9b89-4585-4146-8ee5-22fe99802a8e_input.json", + output_gcs_uri="gs://my.company.bucket/b92b9b89-4585-4146-8ee5-22fe99802a8e_output.json", + ) + + @pytest.mark.parametrize( + "invalid_uri, expected_match", + [ + ("my-bucket/output.json", "`output_gcs_uri` must start with 'gs://'."), + ("gs://", "`output_gcs_uri` must include a GCS bucket name."), + ("gs:///output.json", "`output_gcs_uri` must include a GCS bucket name."), + ], + ) + def test_run_query_job_runtime_invalid_gcs_uri(self, invalid_uri, expected_match): + with pytest.raises(ValueError, match=expected_match): asyncio.run( - self.client.aio.runtimes.delete( - name=_TEST_AGENT_ENGINE_RESOURCE_NAME + self.client.aio.runtimes.run_query_job( + name=_TEST_AGENT_ENGINE_RESOURCE_NAME, + config={ + "query": _TEST_QUERY_PROMPT, + "output_gcs_uri": invalid_uri, + }, + ) + ) + + @mock.patch.object(_runtimes_utils, "_prepare") + @mock.patch.object(_runtimes_utils, "_await_async_operation") + @mock.patch.object( + _runtimes_utils, + "_get_reasoning_engine_id", + return_value=_TEST_RESOURCE_ID, + ) + def test_create_runtime_deprecated_runtime_arg( + self, + mock_get_reasoning_engine_id, + mock_await_async_operation, + mock_prepare, + ): + mock_await_async_operation.return_value = _genai_types.RuntimeOperation( + response=_genai_types.ReasoningEngine( + name=_TEST_AGENT_ENGINE_RESOURCE_NAME, + spec=_TEST_AGENT_ENGINE_SPEC, + ) + ) + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + body=json.dumps({"name": _TEST_AGENT_ENGINE_OPERATION_NAME}) + ) + with pytest.warns(DeprecationWarning, match="deprecated"): + created_runtime = asyncio.run( + self.client.aio.runtimes.create( + runtime=self.test_agent, + config=_genai_types.AgentRuntimeConfig( + display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME, + staging_bucket=_TEST_STAGING_BUCKET, + ), + ) + ) + assert isinstance(created_runtime, _genai_types.Runtime) + + @mock.patch.object(_runtimes_utils, "_prepare") + @mock.patch.object(_runtimes_utils, "_await_async_operation") + def test_update_runtime_deprecated_runtime_arg( + self, mock_await_async_operation, mock_prepare + ): + mock_await_async_operation.return_value = _genai_types.RuntimeOperation( + response=_genai_types.ReasoningEngine( + name=_TEST_AGENT_ENGINE_RESOURCE_NAME, + spec=_TEST_AGENT_ENGINE_SPEC, + ) + ) + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + body=json.dumps({"name": _TEST_AGENT_ENGINE_OPERATION_NAME}) + ) + with pytest.warns(DeprecationWarning, match="deprecated"): + updated_runtime = asyncio.run( + self.client.aio.runtimes.update( + name=_TEST_AGENT_ENGINE_RESOURCE_NAME, + runtime=self.test_agent, + config=_genai_types.AgentRuntimeConfig( + staging_bucket=_TEST_STAGING_BUCKET, + ), + ) ) + assert isinstance(updated_runtime, _genai_types.Runtime) + + def test_delete_runtime(self): + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse(body="") + asyncio.run( + self.client.aio.runtimes.delete(name=_TEST_AGENT_ENGINE_RESOURCE_NAME) ) request_mock.assert_called_with( "delete", @@ -4800,3 +5206,261 @@ def test_delete_runtime_force(self): {"_url": {"name": _TEST_AGENT_ENGINE_RESOURCE_NAME}, "force": True}, None, ) + + def test_get_runtime_and_async_stream_query(self): + async def mock_async_generator(): + yield genai_types.HttpResponse(body=b'data: {"output": "CHUNK"}\n\n') + + with ( + mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as get_request_mock, + mock.patch.object( + self.client.aio.runtimes._api_client, "async_request_streamed" + ) as stream_request_mock, + ): + get_request_mock.return_value = genai_types.HttpResponse( + body=json.dumps( + { + "name": _TEST_AGENT_ENGINE_RESOURCE_NAME, + "spec": { + "classMethods": [ + _TEST_AGENT_ENGINE_CLASS_METHOD_ASYNC_STREAM_QUERY, + ] + }, + } + ) + ) + stream_request_mock.return_value = mock_async_generator() + + async def get_and_stream(): + runtime = await self.client.aio.runtimes.get( + name=_TEST_AGENT_ENGINE_RESOURCE_NAME + ) + return [ + chunk + async for chunk in runtime.async_stream_query( + query=_TEST_QUERY_PROMPT + ) + ] + + chunks = asyncio.run(get_and_stream()) + assert chunks == [{"output": "CHUNK"}] + + @mock.patch.object(runtimes.Runtimes, "_register_api_methods") + @mock.patch.object(_runtimes_utils, "_await_async_operation") + @mock.patch.object( + _runtimes_utils, + "_get_reasoning_engine_id", + return_value=_TEST_RESOURCE_ID, + ) + def test_create_runtime_lightweight( + self, + mock_get_reasoning_engine_id, + mock_await_async_operation, + mock_register_api_methods, + ): + mock_await_async_operation.return_value = _genai_types.RuntimeOperation( + response=_genai_types.ReasoningEngine( + name=_TEST_AGENT_ENGINE_RESOURCE_NAME, + ) + ) + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + body=json.dumps({"name": _TEST_AGENT_ENGINE_OPERATION_NAME}) + ) + created_runtime = asyncio.run( + self.client.aio.runtimes.create( + config=_genai_types.AgentRuntimeConfig( + display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME, + ), + ) + ) + assert isinstance(created_runtime, _genai_types.Runtime) + assert created_runtime.api_resource.name == _TEST_AGENT_ENGINE_RESOURCE_NAME + assert ( + mock_await_async_operation.call_args.kwargs["poll_interval_seconds"] + == 1 + ) + mock_register_api_methods.assert_not_called() + + @mock.patch.object(_runtimes_utils, "_await_async_operation") + @mock.patch.object( + _runtimes_utils, + "_get_reasoning_engine_id", + return_value=_TEST_RESOURCE_ID, + ) + def test_create_runtime_converts_image_spec_and_container_spec( + self, + mock_get_reasoning_engine_id, + mock_await_async_operation, + ): + mock_await_async_operation.return_value = _genai_types.RuntimeOperation( + response=_genai_types.ReasoningEngine( + name=_TEST_AGENT_ENGINE_RESOURCE_NAME, + spec=_TEST_AGENT_ENGINE_SPEC, + ) + ) + with ( + mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock, + mock.patch.object( + runtimes.Runtimes, "_create_config", return_value={} + ) as create_config_mock, + ): + request_mock.return_value = genai_types.HttpResponse( + body=json.dumps({"name": _TEST_AGENT_ENGINE_OPERATION_NAME}) + ) + asyncio.run( + self.client.aio.runtimes.create( + config=_genai_types.AgentRuntimeConfig( + display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME, + container_spec={"image_uri": "gcr.io/my-project/my-image"}, + ), + ) + ) + assert isinstance( + create_config_mock.call_args.kwargs["container_spec"], dict + ) + + def test_check_query_job_runtime_running(self): + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + headers={}, + body=( + '{"done": false, "name": ' + '"projects/123/locations/us-central1/reasoningEngines/456/operations/789", ' + '"response": {"output_gcs_uri": "gs://my-output-bucket/output.json"}}' + ), + ) + result = asyncio.run( + self.client.aio.runtimes.check_query_job( + name="projects/123/locations/us-central1/reasoningEngines/456/operations/789", + config={"retrieve_result": True}, + ) + ) + assert result == _genai_types.CheckQueryJobResult( + operation_name="projects/123/locations/us-central1/reasoningEngines/456/operations/789", + status="RUNNING", + output_gcs_uri="gs://my-output-bucket/output.json", + result=None, + ) + + def test_check_query_job_runtime_failed(self): + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + headers={}, + body='{"done": true, "error": {"message": "Job failed with errors."}}', + ) + result = asyncio.run( + self.client.aio.runtimes.check_query_job( + name="projects/123/locations/us-central1/reasoningEngines/456/operations/789", + config={"retrieve_result": True}, + ) + ) + assert result == _genai_types.CheckQueryJobResult( + operation_name="projects/123/locations/us-central1/reasoningEngines/456/operations/789", + status="FAILED", + output_gcs_uri=None, + result="{'message': 'Job failed with errors.'}", + ) + + def test_check_query_job_runtime_no_retrieve(self): + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + headers={}, + body=( + '{"done": true, "name": ' + '"projects/123/locations/us-central1/reasoningEngines/456/operations/789", ' + '"response": {"output_gcs_uri": "gs://my-output-bucket/output.json"}}' + ), + ) + result = asyncio.run( + self.client.aio.runtimes.check_query_job( + name="projects/123/locations/us-central1/reasoningEngines/456/operations/789", + config={"retrieve_result": False}, + ) + ) + assert result == _genai_types.CheckQueryJobResult( + operation_name="projects/123/locations/us-central1/reasoningEngines/456/operations/789", + status="SUCCESS", + output_gcs_uri="gs://my-output-bucket/output.json", + result=None, + ) + + def test_check_query_job_runtime_blob_not_exists(self): + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + headers={}, + body=( + '{"done": true, "name": ' + '"projects/123/locations/us-central1/reasoningEngines/456/operations/789", ' + '"response": {"output_gcs_uri": "gs://my-output-bucket/output.json"}}' + ), + ) + with mock.patch("google.cloud.storage.Client") as mock_storage_client: + mock_bucket = mock.Mock() + mock_blob = mock.Mock() + mock_blob.exists.return_value = False + mock_bucket.blob.return_value = mock_blob + mock_storage_client.return_value.bucket.return_value = mock_bucket + + with pytest.raises( + ValueError, + match="Failed to retrieve blob results for gs://my-output-bucket/output.json", + ): + asyncio.run( + self.client.aio.runtimes.check_query_job( + name="projects/123/locations/us-central1/reasoningEngines/456/operations/789", + config={"retrieve_result": True}, + ) + ) + + @mock.patch("google.cloud.storage.Client") + @mock.patch("uuid.uuid4") + def test_run_query_job_runtime_directory_and_bucket_create( + self, mock_uuid, mock_storage_client + ): + mock_uuid.return_value.hex = "b92b9b89-4585-4146-8ee5-22fe99802a8e" + with mock.patch.object( + self.client.aio.runtimes._api_client, "async_request" + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + body='{"name": "projects/123/locations/us-central1/reasoningEngines/456/operations/789"}' + ) + mock_bucket = mock.Mock() + mock_bucket.exists.return_value = False + mock_blob = mock.Mock() + mock_bucket.blob.return_value = mock_blob + mock_storage_client.return_value.bucket.return_value = mock_bucket + + result = asyncio.run( + self.client.aio.runtimes.run_query_job( + name=_TEST_AGENT_ENGINE_RESOURCE_NAME, + config={ + "query": _TEST_QUERY_PROMPT, + "output_gcs_uri": "gs://my-input-bucket/path/", + }, + ) + ) + mock_bucket.create.assert_called_once() + mock_bucket.blob.assert_called_once_with( + "path/b92b9b89-4585-4146-8ee5-22fe99802a8e_input.json" + ) + mock_blob.upload_from_string.assert_called_once_with(_TEST_QUERY_PROMPT) + assert result == _genai_types.RunQueryJobResult( + job_name="projects/123/locations/us-central1/reasoningEngines/456/operations/789", + input_gcs_uri="gs://my-input-bucket/path/b92b9b89-4585-4146-8ee5-22fe99802a8e_input.json", + output_gcs_uri="gs://my-input-bucket/path/b92b9b89-4585-4146-8ee5-22fe99802a8e_output.json", + )