-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(cli): handle agent engine deploy imports #4243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,17 @@ | |
| ) | ||
|
|
||
|
|
||
| def _sanitize_temp_folder_name(folder_name: str) -> str: | ||
| if not folder_name: | ||
| return folder_name | ||
| sanitized = ''.join( | ||
| char if char.isalnum() or char == '_' else '_' for char in folder_name | ||
| ) | ||
| if sanitized and sanitized[0].isdigit(): | ||
| return '_' + sanitized | ||
| return sanitized | ||
|
|
||
|
|
||
| def _ensure_agent_engine_dependency(requirements_txt_path: str) -> None: | ||
| """Ensures staged requirements include Agent Engine dependencies.""" | ||
| if not os.path.exists(requirements_txt_path): | ||
|
|
@@ -108,7 +119,7 @@ def _ensure_agent_engine_dependency(requirements_txt_path: str) -> None: | |
| config_path = os.path.join(os.path.dirname(__file__), "root_agent.yaml") | ||
| root_agent = config_agent_utils.from_config(config_path) | ||
| else: | ||
| from .agent import {adk_app_object} | ||
| from {adk_app_import_module} import {adk_app_object} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| if {express_mode}: # Whether or not to use Express Mode | ||
| vertexai.init(api_key=os.environ.get("GOOGLE_API_KEY")) | ||
|
|
@@ -790,7 +801,8 @@ def to_agent_engine( | |
| os.chdir(parent_folder) | ||
| did_change_cwd = True | ||
| tmp_app_name = app_name + '_tmp' + datetime.now().strftime('%Y%m%d_%H%M%S') | ||
| temp_folder = temp_folder or tmp_app_name | ||
| raw_temp_folder = temp_folder or tmp_app_name | ||
| temp_folder = _sanitize_temp_folder_name(raw_temp_folder) | ||
|
Comment on lines
+804
to
+805
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| agent_src_path = os.path.join(parent_folder, temp_folder) | ||
| click.echo(f'Staging all files in: {agent_src_path}') | ||
| # remove agent_src_path if it exists | ||
|
|
@@ -954,6 +966,17 @@ def to_agent_engine( | |
| is_config_agent = True | ||
|
|
||
| adk_app_file = os.path.join(temp_folder, f'{adk_app}.py') | ||
| app_subdir_agent = os.path.join(agent_src_path, 'app', 'agent.py') | ||
| app_subdir_init = os.path.join(agent_src_path, 'app', '__init__.py') | ||
| root_agent_file = os.path.join(agent_src_path, 'agent.py') | ||
| if ( | ||
| os.path.exists(app_subdir_agent) | ||
| and os.path.exists(app_subdir_init) | ||
| and not os.path.exists(root_agent_file) | ||
| ): | ||
| adk_app_import_module = '.app.agent' | ||
| else: | ||
| adk_app_import_module = '.agent' | ||
|
Comment on lines
+969
to
+979
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if adk_app_object == 'root_agent': | ||
| adk_app_type = 'agent' | ||
| elif adk_app_object == 'app': | ||
|
|
@@ -973,6 +996,7 @@ def to_agent_engine( | |
| agent_folder=f'./{temp_folder}', | ||
| adk_app_object=adk_app_object, | ||
| adk_app_type=adk_app_type, | ||
| adk_app_import_module=adk_app_import_module, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| express_mode=api_key is not None, | ||
| ) | ||
| ) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
_sanitize_temp_folder_namefunction is a good addition to ensure that generated temporary folder names are valid Python module identifiers. This prevents import errors that could arise from special characters or leading digits in agent names.