2020import subprocess
2121from typing import Final
2222from typing import Optional
23+ import warnings
2324
2425import click
2526from packaging .version import parse
2627
2728_IS_WINDOWS = os .name == 'nt'
2829_GCLOUD_CMD = 'gcloud.cmd' if _IS_WINDOWS else 'gcloud'
2930_LOCAL_STORAGE_FLAG_MIN_VERSION : Final [str ] = '1.21.0'
31+ _AGENT_ENGINE_REQUIREMENT : Final [str ] = (
32+ 'google-cloud-aiplatform[adk,agent_engines]'
33+ )
34+
35+
36+ def _ensure_agent_engine_dependency (requirements_txt_path : str ) -> None :
37+ """Ensures staged requirements include Agent Engine dependencies."""
38+ if not os .path .exists (requirements_txt_path ):
39+ raise FileNotFoundError (
40+ f'requirements.txt not found at: { requirements_txt_path } '
41+ )
42+
43+ requirements = ''
44+ with open (requirements_txt_path , 'r' , encoding = 'utf-8' ) as f :
45+ requirements = f .read ()
46+
47+ for line in requirements .splitlines ():
48+ stripped = line .strip ()
49+ if (
50+ stripped
51+ and not stripped .startswith ('#' )
52+ and stripped .startswith ('google-cloud-aiplatform' )
53+ ):
54+ return
55+
56+ with open (requirements_txt_path , 'a' , encoding = 'utf-8' ) as f :
57+ if requirements and not requirements .endswith ('\n ' ):
58+ f .write ('\n ' )
59+ f .write (_AGENT_ENGINE_REQUIREMENT + '\n ' )
60+
3061
3162_DOCKERFILE_TEMPLATE : Final [str ] = """
3263FROM python:3.11-slim
@@ -656,7 +687,7 @@ def to_agent_engine(
656687 agent_folder : str ,
657688 temp_folder : Optional [str ] = None ,
658689 adk_app : str ,
659- staging_bucket : str ,
690+ staging_bucket : Optional [ str ] = None ,
660691 trace_to_cloud : Optional [bool ] = None ,
661692 api_key : Optional [str ] = None ,
662693 adk_app_object : Optional [str ] = None ,
@@ -699,7 +730,8 @@ def to_agent_engine(
699730 files. It will be replaced with the generated files if it already exists.
700731 adk_app (str): The name of the file (without .py) containing the AdkApp
701732 instance.
702- staging_bucket (str): The GCS bucket for staging the deployment artifacts.
733+ staging_bucket (str): Deprecated. This argument is no longer required or
734+ used.
703735 trace_to_cloud (bool): Whether to enable Cloud Trace.
704736 api_key (str): Optional. The API key to use for Express Mode.
705737 If not provided, the API key from the GOOGLE_API_KEY environment variable
@@ -729,26 +761,41 @@ def to_agent_engine(
729761 app_name = os .path .basename (agent_folder )
730762 display_name = display_name or app_name
731763 parent_folder = os .path .dirname (agent_folder )
732- if parent_folder != os .getcwd ():
733- click .echo (f'Please deploy from the project dir: { parent_folder } ' )
734- return
735- tmp_app_name = app_name + '_tmp' + datetime .now ().strftime ('%Y%m%d_%H%M%S' )
736- temp_folder = temp_folder or tmp_app_name
737- agent_src_path = os .path .join (parent_folder , temp_folder )
738- click .echo (f'Staging all files in: { agent_src_path } ' )
739764 adk_app_object = adk_app_object or 'root_agent'
740765 if adk_app_object not in ['root_agent' , 'app' ]:
741766 click .echo (
742767 f'Invalid adk_app_object: { adk_app_object } . Please use "root_agent"'
743768 ' or "app".'
744769 )
745770 return
771+ if staging_bucket :
772+ warnings .warn (
773+ 'WARNING: `staging_bucket` is deprecated and will be removed in a'
774+ ' future release. Please drop it from the list of arguments.' ,
775+ DeprecationWarning ,
776+ stacklevel = 2 ,
777+ )
778+
779+ original_cwd = os .getcwd ()
780+ did_change_cwd = False
781+ if parent_folder != original_cwd :
782+ click .echo (
783+ 'Agent Engine deployment uses relative paths; temporarily switching '
784+ f'working directory to: { parent_folder } '
785+ )
786+ os .chdir (parent_folder )
787+ did_change_cwd = True
788+ tmp_app_name = app_name + '_tmp' + datetime .now ().strftime ('%Y%m%d_%H%M%S' )
789+ temp_folder = temp_folder or tmp_app_name
790+ agent_src_path = os .path .join (parent_folder , temp_folder )
791+ click .echo (f'Staging all files in: { agent_src_path } ' )
746792 # remove agent_src_path if it exists
747793 if os .path .exists (agent_src_path ):
748794 click .echo ('Removing existing files' )
749795 shutil .rmtree (agent_src_path )
750796
751797 try :
798+ click .echo (f'Staging all files in: { agent_src_path } ' )
752799 ignore_patterns = None
753800 ae_ignore_path = os .path .join (agent_folder , '.ae_ignore' )
754801 if os .path .exists (ae_ignore_path ):
@@ -757,15 +804,18 @@ def to_agent_engine(
757804 patterns = [pattern .strip () for pattern in f .readlines ()]
758805 ignore_patterns = shutil .ignore_patterns (* patterns )
759806 click .echo ('Copying agent source code...' )
760- shutil .copytree (agent_folder , agent_src_path , ignore = ignore_patterns )
807+ shutil .copytree (
808+ agent_folder ,
809+ agent_src_path ,
810+ ignore = ignore_patterns ,
811+ dirs_exist_ok = True ,
812+ )
761813 click .echo ('Copying agent source code complete.' )
762814
763815 project = _resolve_project (project )
764816
765817 click .echo ('Resolving files and dependencies...' )
766818 agent_config = {}
767- if staging_bucket :
768- agent_config ['staging_bucket' ] = staging_bucket
769819 if not agent_engine_config_file :
770820 # Attempt to read the agent engine config from .agent_engine_config.json in the dir (if any).
771821 agent_engine_config_file = os .path .join (
@@ -808,8 +858,9 @@ def to_agent_engine(
808858 if not os .path .exists (requirements_txt_path ):
809859 click .echo (f'Creating { requirements_txt_path } ...' )
810860 with open (requirements_txt_path , 'w' , encoding = 'utf-8' ) as f :
811- f .write ('google-cloud-aiplatform[adk,agent_engines] ' )
861+ f .write (_AGENT_ENGINE_REQUIREMENT + ' \n ' )
812862 click .echo (f'Created { requirements_txt_path } ' )
863+ _ensure_agent_engine_dependency (requirements_txt_path )
813864 agent_config ['requirements_file' ] = f'{ temp_folder } /requirements.txt'
814865
815866 env_vars = {}
@@ -940,7 +991,9 @@ def to_agent_engine(
940991 click .secho (f'✅ Updated agent engine: { agent_engine_id } ' , fg = 'green' )
941992 finally :
942993 click .echo (f'Cleaning up the temp folder: { temp_folder } ' )
943- shutil .rmtree (temp_folder )
994+ shutil .rmtree (agent_src_path )
995+ if did_change_cwd :
996+ os .chdir (original_cwd )
944997
945998
946999def to_gke (
0 commit comments