reasoning-deployment-service 0.6.0__tar.gz → 0.7.0__tar.gz

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.

Potentially problematic release.


This version of reasoning-deployment-service might be problematic. Click here for more details.

Files changed (34) hide show
  1. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/PKG-INFO +1 -1
  2. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/pyproject.toml +1 -1
  3. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/cli_editor/api_client.py +0 -14
  4. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/gui_editor/src/ui/reasoning_engine_view.py +0 -34
  5. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/reasoning_deployment_service.py +78 -84
  6. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service.egg-info/PKG-INFO +1 -1
  7. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service.egg-info/SOURCES.txt +0 -1
  8. reasoning_deployment_service-0.6.0/reasoning_deployment_service/gui_editor/src/core/reasoning_engine_creator.py +0 -448
  9. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/README.md +0 -0
  10. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/__init__.py +0 -0
  11. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/cli_editor/__init__.py +0 -0
  12. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/cli_editor/cli_runner.py +0 -0
  13. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/cli_editor/config.py +0 -0
  14. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/cli_editor/google_deps.py +0 -0
  15. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/gui_editor/__init__.py +0 -0
  16. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/gui_editor/agent_checkbox_list.py +0 -0
  17. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/gui_editor/main.py +0 -0
  18. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/gui_editor/src/__init__.py +0 -0
  19. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/gui_editor/src/core/__init__.py +0 -0
  20. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/gui_editor/src/core/api_client.py +0 -0
  21. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/gui_editor/src/core/config.py +0 -0
  22. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/gui_editor/src/core/google_deps.py +0 -0
  23. {reasoning_deployment_service-0.6.0/reasoning_deployment_service/cli_editor → reasoning_deployment_service-0.7.0/reasoning_deployment_service/gui_editor/src/core}/reasoning_engine_creator.py +0 -0
  24. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/gui_editor/src/ui/__init__.py +0 -0
  25. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/gui_editor/src/ui/agent_space_view.py +0 -0
  26. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/gui_editor/src/ui/authorization_view.py +0 -0
  27. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/gui_editor/src/ui/reasoning_engines_view.py +0 -0
  28. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/gui_editor/src/ui/ui_components.py +0 -0
  29. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service/runner.py +0 -0
  30. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service.egg-info/dependency_links.txt +0 -0
  31. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service.egg-info/entry_points.txt +0 -0
  32. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service.egg-info/requires.txt +0 -0
  33. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/reasoning_deployment_service.egg-info/top_level.txt +0 -0
  34. {reasoning_deployment_service-0.6.0 → reasoning_deployment_service-0.7.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: reasoning-deployment-service
3
- Version: 0.6.0
3
+ Version: 0.7.0
4
4
  Summary: Deployment helper for Vertex AI Reasoning Engines & Agent Spaces
5
5
  Author-email: Sergio Estrada <sergio.estrada@accenture.com>
6
6
  License: Apache-2.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "reasoning-deployment-service"
7
- version = "0.6.0"
7
+ version = "0.7.0"
8
8
  description = "Deployment helper for Vertex AI Reasoning Engines & Agent Spaces"
9
9
  readme = "README.md"
10
10
  license = { text = "Apache-2.0" }
@@ -14,14 +14,11 @@ try:
14
14
  HAS_GOOGLE, google, GoogleAuthRequest,
15
15
  vertexai, agent_engines
16
16
  )
17
- from reasoning_engine_creator import ReasoningEngineCreator
18
17
  except ImportError as e:
19
18
  from .google_deps import (HAS_GOOGLE, google, GoogleAuthRequest, vertexai, agent_engines)
20
- from .reasoning_engine_creator import ReasoningEngineCreator
21
19
 
22
20
  BASE_URL = "https://discoveryengine.googleapis.com/v1alpha"
23
21
 
24
-
25
22
  # --- helpers for clean packaging ---
26
23
  EXCLUDES = [
27
24
  ".env", ".env.*", ".git", "__pycache__", ".pytest_cache", ".mypy_cache",
@@ -501,17 +498,6 @@ class ApiClient:
501
498
  if name in sys.modules:
502
499
  del sys.modules[name]
503
500
 
504
- def create_reasoning_engine_advanced(self, config: Dict[str, Any]) -> Tuple[str, str, Optional[str]]:
505
- """Create a reasoning engine with advanced configuration options."""
506
- creator = ReasoningEngineCreator(
507
- project_id=self.project_id,
508
- location=self.location,
509
- staging_bucket=self.staging_bucket,
510
- debug=self.debug,
511
- )
512
-
513
- return creator.create_advanced_engine(config)
514
-
515
501
  def delete_reasoning_engine(self) -> Tuple[str, str]:
516
502
  if not self._profile.get("name"):
517
503
  return ("not_found", "No engine")
@@ -290,40 +290,6 @@ class ReasoningEngineView(ttk.Frame):
290
290
  finally:
291
291
  self.engines_menu.grab_release()
292
292
 
293
- def _create_engine_advanced(self):
294
- """Create a new reasoning engine with advanced configuration."""
295
- if not self.api.is_authenticated:
296
- self.log("❌ Authentication required")
297
- return
298
-
299
- # Show advanced create engine dialog
300
- dialog = CreateReasoningEngineAdvancedDialog(self.winfo_toplevel(), self.api)
301
- self.wait_window(dialog)
302
-
303
- if not dialog.result:
304
- return # User cancelled
305
-
306
- config = dialog.result
307
- self.log(f"⚙️ Creating advanced reasoning engine '{config['display_name']}'...")
308
- # self.create_advanced_btn.set_enabled(False, "Creating...")
309
-
310
- def callback(res):
311
- # self.create_advanced_btn.set_enabled(True)
312
- if isinstance(res, Exception):
313
- self.log(f"❌ {res}")
314
- return
315
-
316
- status, msg, resource = res
317
- self.log(f"{status.upper()}: {msg}")
318
- if resource:
319
- self.log(f"resource: {resource}")
320
-
321
- # Refresh engines list to show the new engine
322
- self._refresh_engines()
323
- self._update_button_states()
324
-
325
- async_operation(lambda: self.api.create_reasoning_engine_advanced(config), callback=callback, ui_widget=self)
326
-
327
293
  def update_api(self, api: ApiClient):
328
294
  """Update the API client reference."""
329
295
  self.api = api
@@ -39,6 +39,7 @@ class ReasoningEngineDeploymentService:
39
39
 
40
40
  self._load_agent_definition()
41
41
  self._load_deployment_environment_variables(deployment_environment=deployment_environment)
42
+ self._load_runtime_variables()
42
43
  self._check_requirements_file_present()
43
44
 
44
45
  self._http = _requests.Session()
@@ -97,6 +98,25 @@ class ReasoningEngineDeploymentService:
97
98
  def info(self, message: str):
98
99
  self.logger.info(f"[DEPLOYMENT SERVICE: INFO]: {message}")
99
100
 
101
+ def _generate_authorization_id(self) -> str:
102
+ get_deployment_environment = os.getenv('AGENT_DEPLOYMENT_PIPELINE_ID', "LOCAL_RUN")
103
+
104
+ return f"{get_deployment_environment}-{self._reasoning_engine_name}-{self._agent_space_engine}-auth".lower()
105
+
106
+ def _load_runtime_variables(self):
107
+ load_dotenv(dotenv_path=".env", override=True)
108
+ runtime_vars = {}
109
+
110
+ for key in self._specific_dot_env_variables:
111
+ if key in os.environ:
112
+ runtime_vars[key] = os.environ[key]
113
+
114
+ runtime_vars.update(self._runtime_variable_definitions or {})
115
+ runtime_vars.update({'AUTHORIZATION_ID': self._generate_authorization_id()})
116
+
117
+ self._authorization_id = runtime_vars.get('AUTHORIZATION_ID')
118
+ self._environment_variables = runtime_vars
119
+
100
120
  def _check_required_files_exist(self):
101
121
  end_run = False
102
122
  if not os.path.exists(".env.agent"):
@@ -185,33 +205,12 @@ class ReasoningEngineDeploymentService:
185
205
  if path.exists() and not overwrite:
186
206
  raise FileExistsError(f"{path} already exists. Pass overwrite=True to replace it.")
187
207
 
188
- template = """#===================== **** DEPLOYMENT PROFILE **** =====================
189
- # Development Profile
190
- DEV_PROJECT_ID=
191
- DEV_PROJECT_NUMBER=
192
- DEV_PROJECT_LOCATION=
193
- DEV_STAGING_BUCKET=
194
- DEV_AGENT_SPACE_ENGINE=
195
- DEV_API_TOKEN=
196
- DEV_OAUTH_CLIENT_ID=
197
- DEV_OAUTH_CLIENT_SECRET=
198
-
199
- # Production Profile
200
- PROD_PROJECT_ID=
201
- PROD_PROJECT_NUMBER=
202
- PROD_PROJECT_LOCATION=
203
- PROD_STAGING_BUCKET=
204
- PROD_AGENT_SPACE_ENGINE=
205
- PROD_API_TOKEN=
206
- PROD_OAUTH_CLIENT_ID=
207
- PROD_OAUTH_CLIENT_SECRET=
208
- #===================== **** DEPLOYMENT PROFILE **** =====================
209
-
210
-
211
- #===================== **** YOUR APP ENV VARIABLES **** =====================
212
- DEVELOPER=dev
213
- #===================== **** YOUR APP ENV VARIABLES **** =====================
214
- """
208
+ template = """
209
+ DEV_PROJECT_ID=
210
+ DEV_PROJECT_NUMBER=
211
+ DEV_PROJECT_LOCATION=
212
+ DEV_OAUTH_CLIENT_ID=
213
+ DEV_OAUTH_CLIENT_SECRET="""
215
214
 
216
215
  path.write_text(template.strip() + "\n")
217
216
 
@@ -250,13 +249,6 @@ class ReasoningEngineDeploymentService:
250
249
  def _generate_example_yaml_config(self, path: str | Path = "agent.yaml", overwrite: bool = False) -> Path:
251
250
  """
252
251
  Create an example YAML config matching the requested schema.
253
-
254
- Structure:
255
- defaults:
256
- scopes: [ ... ]
257
- metadata: { ... }
258
- auth: { ... }
259
- environment_variables: [ ... ]
260
252
  """
261
253
  path = Path(path)
262
254
  if path.exists() and not overwrite:
@@ -264,23 +256,28 @@ class ReasoningEngineDeploymentService:
264
256
 
265
257
  config = {
266
258
  "defaults": {
267
- "scopes": [
268
- "https://www.googleapis.com/auth/cloud-platform",
269
- "https://www.googleapis.com/auth/userinfo.email",
270
- ],
271
- "metadata": {
272
- "reasoning_engine_name": "reasoning-engine-dev",
273
- "reasoning_engine_description": "A reasoning engine for development",
274
- "agent_space_name": "Agent Space Dev Numba Three!",
275
- "agent_space_description": "Agent spece description, lets go",
276
- "agent_space_tool_description": "Agent space tool description",
259
+ "deployment_service": "0.6.2",
260
+ "reasoning_engine": {
261
+ "name": "reasoning-engine-dev",
262
+ "description": "A reasoning engine for development"
277
263
  },
278
- "auth": {
279
- "oauth_authorization_id": "test_auth_three",
264
+ "gemini_enterprise": {
265
+ "target_deployment_engine_id": "",
266
+ "name": "Agent Name Here",
267
+ "description": "Agent description here",
268
+ "tool_description": "Tool description here",
280
269
  },
281
- "environment_variables": [
282
- "DEVELOPER",
283
- ],
270
+ "authorization": {
271
+ "enabled": True,
272
+ "scopes": [
273
+ "https://www.googleapis.com/auth/cloud-platform",
274
+ "https://www.googleapis.com/auth/userinfo.email"
275
+ ]
276
+ },
277
+ "import_from_dot_env_by_name": ["TEST_ENV_VAR"],
278
+ "runtime_variable_definitions":{
279
+ "EXAMPLE_VAR": "An example environment variable for the agent runtime"
280
+ }
284
281
  }
285
282
  }
286
283
 
@@ -298,26 +295,34 @@ class ReasoningEngineDeploymentService:
298
295
 
299
296
  try:
300
297
  config = config['defaults']
301
- scopes = config['scopes']
302
- metadata = config['metadata']
303
- auth = config['auth']
304
- environment_variables = config['environment_variables']
305
-
306
- reasoning_engine_name = metadata['reasoning_engine_name']
307
- reasoning_engine_description = metadata['reasoning_engine_description']
308
- agent_space_name = metadata['agent_space_name']
309
- agent_space_description = metadata['agent_space_description']
310
- agent_space_tool_description = metadata["agent_space_tool_description"]
311
-
312
- self._required_scopes = scopes
298
+ deployment_service_version = config.get('deployment_service')
299
+
300
+ if deployment_service_version >= 0.7:
301
+ raise RuntimeError(f"Unsupported deployment_service version: {deployment_service_version}. Expected minimum '0.7'")
302
+
303
+ authorization = config['authorization']
304
+ gemini_enterprise = config['gemini_enterprise']
305
+ reasoning_engine = config['reasoning_engine']
306
+ self._specific_dot_env_variables = config.get('import_from_dot_env_by_name', [])
307
+ self._runtime_variable_definitions = config.get('runtime_variable_definitions', {})
308
+
309
+ reasoning_engine_name = reasoning_engine.get('name')
310
+ reasoning_engine_description = reasoning_engine.get('description')
311
+
312
+ gemini_enterprise_name = gemini_enterprise.get('name')
313
+ gemini_enterprise_description = gemini_enterprise.get('description')
314
+ gemini_enterprise_tool_description = gemini_enterprise.get('tool_description')
315
+ gemini_enterprise_engine_id = gemini_enterprise.get('target_deployment_engine_id')
316
+
317
+ self._target_deployment_engine_id = gemini_enterprise_engine_id
318
+ self._required_scopes = authorization.get('scopes', [])
313
319
  self._agent_folder = "agent"
314
320
  self._reasoning_engine_name = reasoning_engine_name
315
321
  self._reasoning_engine_description = reasoning_engine_description
316
- self._agent_space_name = agent_space_name
317
- self._agent_space_description = agent_space_description
318
- self._agent_space_tool_description = agent_space_tool_description
319
- self._authorization_id = auth.get("oauth_authorization_id", None)
320
- self._environment_variables = environment_variables or []
322
+ self._agent_space_name = gemini_enterprise_name
323
+ self._agent_space_description = gemini_enterprise_description
324
+ self._agent_space_tool_description = gemini_enterprise_tool_description
325
+ self._use_authorization = authorization.get('enabled', False)
321
326
  except KeyError as e:
322
327
  raise RuntimeError(f"Missing required key in agent.yaml: {e}")
323
328
 
@@ -331,7 +336,7 @@ class ReasoningEngineDeploymentService:
331
336
 
332
337
  setattr(self, f"_{var.lower()}", os.getenv(env_var))
333
338
 
334
- if self._authorization_id:
339
+ if self._use_authorization:
335
340
  required_auth_vars = ['OAUTH_CLIENT_ID', 'OAUTH_CLIENT_SECRET']
336
341
 
337
342
  for var in required_auth_vars:
@@ -723,8 +728,12 @@ class ReasoningEngineDeploymentService:
723
728
 
724
729
  self.info(r.json())
725
730
  self.info(r.text)
726
-
727
- r.raise_for_status()
731
+
732
+ if r.status_code == 403:
733
+ self.warning("Access denied")
734
+ return None
735
+ else:
736
+ r.raise_for_status()
728
737
 
729
738
  return r.json().get("name", name)
730
739
 
@@ -851,8 +860,6 @@ class ReasoningEngineDeploymentService:
851
860
  - Agent Space: create if missing; patch if found (by displayName under engine).
852
861
  """
853
862
  self.info("Starting GitHub deployment...")
854
-
855
- # Config snapshot
856
863
  self.info(
857
864
  f"[CFG] project_id={self._project_id} project_number={self._project_number} "
858
865
  f"location={self._project_location} engine_name={self._reasoning_engine_name} "
@@ -860,7 +867,6 @@ class ReasoningEngineDeploymentService:
860
867
  f"scopes={self._required_scopes} staging_bucket={self._staging_bucket}"
861
868
  )
862
869
 
863
- # Ensure Vertex SDK calls have context for list/update
864
870
  self._cicd_deploy = True
865
871
  self.info(f"[INIT] vertexai.init(project={self._project_id}, location={self._project_location}, staging_bucket={self._staging_bucket})")
866
872
  vertexai.init(
@@ -869,9 +875,6 @@ class ReasoningEngineDeploymentService:
869
875
  staging_bucket=self._staging_bucket,
870
876
  )
871
877
 
872
- # -----------------------------
873
- # 1) Reasoning Engine (create or update)
874
- # -----------------------------
875
878
  self.info(f"[ENGINE] Resolving by display_name={self._reasoning_engine_name}")
876
879
  engine_rn = self.find_engine_by_name(self._reasoning_engine_name)
877
880
  self.info(f"[ENGINE] find_engine_by_name -> {engine_rn}")
@@ -893,9 +896,6 @@ class ReasoningEngineDeploymentService:
893
896
 
894
897
  self.info(f"[ENGINE] final engine_rn={engine_rn}")
895
898
 
896
- # -----------------------------
897
- # 2) Authorization (create if missing; update scopes if changed)
898
- # -----------------------------
899
899
  auth_full_name = None
900
900
  if self._authorization_id:
901
901
  want_scopes = set(self._required_scopes or [])
@@ -913,7 +913,6 @@ class ReasoningEngineDeploymentService:
913
913
  self.error("[AUTH] Creation failed or did not resolve.")
914
914
  raise RuntimeError("Authorization creation failed.")
915
915
  else:
916
- # Compare scopes; patch if different
917
916
  auth_url = f"{DISCOVERY_ENGINE_URL}/{auth_full_name}"
918
917
  hdrs = self._get_headers().copy()
919
918
  if "Authorization" in hdrs:
@@ -952,15 +951,11 @@ class ReasoningEngineDeploymentService:
952
951
  else:
953
952
  self.info("[AUTH] No authorization_id configured; skipping authorization step.")
954
953
 
955
- # -----------------------------
956
- # 3) Agent Space Agent (create or update)
957
- # -----------------------------
958
954
  self.info(f"[AGENT] Resolving by display_name={self._agent_space_name}")
959
955
  existing_agent = self.find_agent_space_agents_by_display(self._agent_space_name)
960
956
  self.info(f"[AGENT] find_agent_space_agents_by_display -> {json.dumps(existing_agent, indent=2)}")
961
957
 
962
958
  if not existing_agent:
963
- # Fresh create
964
959
  headers, payload = self._get_agent_space_payload(engine_rn)
965
960
  create_url = self._get_agent_space_agent_url_new()
966
961
  self.info(f"[AGENT] POST {create_url}")
@@ -976,7 +971,6 @@ class ReasoningEngineDeploymentService:
976
971
  else:
977
972
  self.warning("[AGENT] Created but response missing name. Verify in console.")
978
973
  else:
979
- # Safe patch using our new helper
980
974
  self.info(f"[AGENT] '{self._agent_space_name}' exists. Patching metadata and auth only...")
981
975
  patched = self.patch_agent_space_metadata_and_auth(
982
976
  agent_id=existing_agent["id"],
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: reasoning-deployment-service
3
- Version: 0.6.0
3
+ Version: 0.7.0
4
4
  Summary: Deployment helper for Vertex AI Reasoning Engines & Agent Spaces
5
5
  Author-email: Sergio Estrada <sergio.estrada@accenture.com>
6
6
  License: Apache-2.0
@@ -14,7 +14,6 @@ reasoning_deployment_service/cli_editor/api_client.py
14
14
  reasoning_deployment_service/cli_editor/cli_runner.py
15
15
  reasoning_deployment_service/cli_editor/config.py
16
16
  reasoning_deployment_service/cli_editor/google_deps.py
17
- reasoning_deployment_service/cli_editor/reasoning_engine_creator.py
18
17
  reasoning_deployment_service/gui_editor/__init__.py
19
18
  reasoning_deployment_service/gui_editor/agent_checkbox_list.py
20
19
  reasoning_deployment_service/gui_editor/main.py
@@ -1,448 +0,0 @@
1
- """Reasoning Engine creation with robust venv lifecycle and safe imports.
2
-
3
- This module provides a `ReasoningEngineCreator` that:
4
- - Creates an isolated virtual environment per engine build
5
- - Installs dependencies using the *venv's* interpreter (`python -m pip`)
6
- - Temporarily exposes the venv's site-packages to the current process for imports
7
- - Emulates activation for subprocesses via PATH/VIRTUAL_ENV
8
- - Stages a clean copy of the agent directory as an extra package
9
- - Cleans up the venv after completion
10
-
11
- Pass a config dict to `create_advanced_engine` with keys:
12
- - display_name (str)
13
- - description (str, optional)
14
- - enable_tracing (bool, optional)
15
- - requirements_source_type ("file" | "text")
16
- - requirements_file (str, optional when source_type == "file")
17
- - requirements_text (str, optional when source_type == "text")
18
- - agent_file_path (str, path to the python file exporting `root_agent`)
19
- - project_id, location, staging_bucket should be supplied to the constructor
20
- """
21
- from __future__ import annotations
22
-
23
- import importlib
24
- import importlib.util
25
- import json
26
- import os
27
- import platform
28
- import shutil
29
- import subprocess
30
- import sys
31
- import tempfile
32
- import venv
33
- from datetime import datetime
34
- from pathlib import Path
35
- from typing import Any, Dict, List, Optional, Tuple
36
-
37
- from vertexai import init as vertexai_init
38
- from vertexai.preview.reasoning_engines import AdkApp
39
- from vertexai import agent_engines
40
-
41
- # --- helpers for clean packaging ---
42
- EXCLUDES = [
43
- ".env",
44
- ".env.*",
45
- ".git",
46
- "__pycache__",
47
- ".pytest_cache",
48
- ".mypy_cache",
49
- ".DS_Store",
50
- "*.pyc",
51
- "*.pyo",
52
- "*.pyd",
53
- ".venv",
54
- "venv",
55
- "tests",
56
- "docs",
57
- ]
58
-
59
-
60
- class ReasoningEngineCreator:
61
- """
62
- Dedicated class for creating reasoning engines with advanced virtual environment management.
63
- Handles all the complex logic for venv creation, dependency installation, and deployment.
64
- """
65
-
66
- def __init__(self, project_id: str, location: str, staging_bucket: str, debug: bool = False):
67
- self.project_id = project_id
68
- self.location = location
69
- self.staging_bucket = staging_bucket
70
- self.debug = debug
71
-
72
- # Ensure staging bucket has gs:// prefix
73
- if not self.staging_bucket.startswith("gs://"):
74
- self.staging_bucket = f"gs://{self.staging_bucket}"
75
-
76
- # ---------------- Vertex init ----------------
77
- def _ensure_vertex_inited(self) -> None:
78
- """Initialize Vertex AI once and reuse."""
79
- if not getattr(self, "_vertex_inited", False):
80
- vertexai_init(project=self.project_id, location=self.location, staging_bucket=self.staging_bucket)
81
- self._vertex_inited = True
82
-
83
- # ---------------- Virtual Environment Management ----------------
84
- def _create_venv_name(self, engine_name: str) -> str:
85
- """Generate a unique virtual environment name with timestamp."""
86
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
87
- # Clean engine name for filesystem safety
88
- clean_name = "".join(c for c in engine_name if c.isalnum() or c in "_-").lower()
89
- return f"venv_{timestamp}_{clean_name}"
90
-
91
- def _deactivate_current_venv(self) -> bool:
92
- """Deactivate any currently active virtual environment."""
93
- if not os.environ.get("VIRTUAL_ENV"):
94
- if self.debug:
95
- print("📍 No virtual environment currently active.")
96
- return True
97
- if self.debug:
98
- print(f"📍 Deactivating current virtual environment: {os.environ.get('VIRTUAL_ENV')}")
99
- # No-op for current process; we'll spawn new processes with the target venv
100
- return True
101
-
102
- def _create_and_activate_venv(self, venv_name: str, project_dir: str) -> Tuple[bool, str, str]:
103
- """
104
- Create a new virtual environment.
105
- Returns: (success, venv_path, python_executable)
106
- """
107
- try:
108
- venv_base = os.path.join(os.path.expanduser("~"), ".agent_venvs")
109
- os.makedirs(venv_base, exist_ok=True)
110
- venv_path = os.path.join(venv_base, venv_name)
111
-
112
- print(f"🔧 Creating virtual environment: {venv_path}")
113
- venv.create(venv_path, with_pip=True, clear=True)
114
-
115
- if platform.system() == "Windows":
116
- python_exe = os.path.join(venv_path, "Scripts", "python.exe")
117
- else:
118
- python_exe = os.path.join(venv_path, "bin", "python")
119
-
120
- if not os.path.exists(python_exe):
121
- raise RuntimeError(f"Python executable not found at: {python_exe}")
122
-
123
- print("✅ Virtual environment created successfully")
124
- print(f"📍 Python executable: {python_exe}")
125
- return True, venv_path, python_exe
126
- except Exception as e:
127
- print(f"❌ Failed to create virtual environment: {e}")
128
- return False, "", ""
129
-
130
- def _install_requirements_in_venv(self, python_exe: str, requirements: List[str]) -> bool:
131
- """Install requirements in the specified virtual environment using interpreter-correct pip."""
132
- if not requirements:
133
- print("📍 No requirements to install.")
134
- return True
135
- try:
136
- print(f"📦 Installing {len(requirements)} requirements...")
137
- for req in requirements:
138
- print(f" 📦 Installing: {req}")
139
- cmd = [python_exe, "-m", "pip", "install", req]
140
- result = subprocess.run(cmd, capture_output=True, text=True, timeout=300)
141
- if result.returncode != 0:
142
- print(f"❌ Failed to install {req}\nSTDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}")
143
- return False
144
- print("✅ All requirements installed successfully!")
145
- return True
146
- except subprocess.TimeoutExpired:
147
- print("❌ Package installation timed out")
148
- return False
149
- except Exception as e:
150
- print(f"❌ Error installing requirements: {e}")
151
- return False
152
-
153
- def _cleanup_venv(self, venv_path: str) -> bool:
154
- """Remove the virtual environment directory."""
155
- if not venv_path or not os.path.exists(venv_path):
156
- if self.debug:
157
- print("📍 Virtual environment path doesn't exist, nothing to clean up.")
158
- return True
159
- try:
160
- print(f"🧹 Cleaning up virtual environment: {venv_path}")
161
- shutil.rmtree(venv_path)
162
- print("✅ Virtual environment cleaned up successfully!")
163
- return True
164
- except Exception as e:
165
- print(f"⚠️ Warning: Failed to clean up virtual environment: {e}")
166
- return False
167
-
168
- def _add_venv_to_sys_path(self, python_exe: str) -> Optional[str]:
169
- """Add the virtual environment's site-packages to sys.path for imports."""
170
- try:
171
- code = "import sysconfig, json; print(json.dumps(sysconfig.get_paths()))"
172
- result = subprocess.check_output([python_exe, "-c", code], text=True)
173
- paths = json.loads(result.strip())
174
- site_pkgs = paths.get("purelib") or paths.get("platlib")
175
- if site_pkgs and site_pkgs not in sys.path:
176
- print(f"📍 Adding venv site-packages to sys.path: {site_pkgs}")
177
- sys.path.insert(0, site_pkgs)
178
- return site_pkgs
179
- return site_pkgs
180
- except Exception as e:
181
- print(f"⚠️ Warning: Could not add venv to sys.path: {e}")
182
- return None
183
-
184
- def _remove_venv_from_sys_path(self, site_pkgs_path: Optional[str]) -> None:
185
- """Remove the virtual environment's site-packages from sys.path."""
186
- if site_pkgs_path and site_pkgs_path in sys.path:
187
- sys.path.remove(site_pkgs_path)
188
- print(f"📍 Removed venv site-packages from sys.path: {site_pkgs_path}")
189
-
190
- def _push_venv_envvars(self, venv_path: str) -> None:
191
- """Temporarily emulate activation for subprocesses/tools."""
192
- self._old_env = {
193
- "PATH": os.environ.get("PATH", ""),
194
- "VIRTUAL_ENV": os.environ.get("VIRTUAL_ENV"),
195
- }
196
- bin_dir = os.path.join(venv_path, "Scripts" if platform.system() == "Windows" else "bin")
197
- os.environ["VIRTUAL_ENV"] = venv_path
198
- os.environ["PATH"] = bin_dir + os.pathsep + self._old_env["PATH"]
199
-
200
- def _pop_venv_envvars(self) -> None:
201
- if hasattr(self, "_old_env"):
202
- os.environ["PATH"] = self._old_env["PATH"]
203
- if self._old_env["VIRTUAL_ENV"] is None:
204
- os.environ.pop("VIRTUAL_ENV", None)
205
- else:
206
- os.environ["VIRTUAL_ENV"] = self._old_env["VIRTUAL_ENV"]
207
- del self._old_env
208
-
209
- # ---------------- Validation ----------------
210
- def _assert_no_google_shadow(self, agent_dir: str) -> None:
211
- """Ensure no local package named 'google' shadows site-packages."""
212
- local_google = os.path.join(agent_dir, "google")
213
- if os.path.isdir(local_google) or os.path.isfile(local_google + ".py"):
214
- raise RuntimeError(
215
- f"Found local '{local_google}'. This will shadow 'google.adk'. "
216
- "Rename/remove it or move agent code under a different package."
217
- )
218
-
219
- # ---------------- Agent Loading and Staging ----------------
220
- def _stage_clean_copy(self, src_dir: str) -> str:
221
- """Copy agent directory to temp dir, excluding dev files and secrets."""
222
- src = Path(src_dir).resolve()
223
- dst_root = Path(tempfile.mkdtemp(prefix="agent_stage_"))
224
- dst = dst_root / src.name
225
-
226
- if self.debug:
227
- print("📦 Staging agent directory...")
228
- print(f"📁 Source: {src}")
229
- print(f"📁 Destination: {dst}")
230
- print(f"🚫 Excluding: {EXCLUDES}")
231
- if src.exists():
232
- print("📋 Source contents:")
233
- for item in sorted(src.iterdir()):
234
- print(f" {'📁' if item.is_dir() else '📄'} {item.name}{'/' if item.is_dir() else ''}")
235
-
236
- shutil.copytree(src, dst, ignore=shutil.ignore_patterns(*EXCLUDES), dirs_exist_ok=True)
237
-
238
- if self.debug:
239
- print("📋 Staged contents:")
240
- for item in sorted(dst.iterdir()):
241
- print(f" {'📁' if item.is_dir() else '📄'} {item.name}{'/' if item.is_dir() else ''}")
242
-
243
- # Clean up .env files
244
- for p in dst.rglob(".env*"):
245
- try:
246
- p.unlink()
247
- if self.debug:
248
- print(f"🗑️ Removed: {p}")
249
- except Exception:
250
- pass
251
-
252
- if self.debug:
253
- print(f"✅ Staging complete: {dst}")
254
- return str(dst)
255
-
256
- def _load_agent_from_file(self, agent_file_path: str):
257
- """Load root_agent from a Python file, handling relative imports properly."""
258
- agent_file = Path(agent_file_path).resolve()
259
- if not agent_file.exists():
260
- raise RuntimeError(f"Agent file not found: {agent_file}")
261
-
262
- agent_dir = agent_file.parent
263
- package_name = agent_dir.name
264
- module_name = f"{package_name}.{agent_file.stem}"
265
-
266
- parent_dir = str(agent_dir.parent)
267
- agent_dir_str = str(agent_dir)
268
-
269
- print(f"🤖 Loading {agent_file.stem} from: {agent_file}")
270
- print(f"📁 Agent directory: {agent_dir}")
271
- print(f"📦 Package name: {package_name}")
272
- print(f"🔧 Module name: {module_name}")
273
- if self.debug:
274
- print(f"🛤️ Adding to sys.path: {parent_dir} (for package imports)")
275
- print(f"🛤️ Adding to sys.path: {agent_dir_str} (for absolute imports like 'tools')")
276
-
277
- # Add both parent directory and agent directory
278
- paths_added: List[str] = []
279
- if parent_dir not in sys.path:
280
- sys.path.insert(0, parent_dir)
281
- paths_added.append(parent_dir)
282
- if agent_dir_str not in sys.path:
283
- sys.path.insert(0, agent_dir_str)
284
- paths_added.append(agent_dir_str)
285
-
286
- try:
287
- # Optionally create a package for proper relative import resolution
288
- init_py = agent_dir / "__init__.py"
289
- package_spec = importlib.util.spec_from_file_location(package_name, init_py if init_py.exists() else None)
290
- if package_spec:
291
- package_module = importlib.util.module_from_spec(package_spec)
292
- sys.modules[package_name] = package_module
293
- if package_spec.loader and init_py.exists():
294
- package_spec.loader.exec_module(package_module)
295
-
296
- spec = importlib.util.spec_from_file_location(module_name, agent_file)
297
- if spec is None or spec.loader is None:
298
- raise RuntimeError(f"Could not load module spec from {agent_file}")
299
-
300
- module = importlib.util.module_from_spec(spec)
301
- module.__package__ = package_name # help relative imports
302
- sys.modules[module_name] = module
303
- spec.loader.exec_module(module)
304
-
305
- if not hasattr(module, "root_agent"):
306
- raise RuntimeError(f"Module '{agent_file}' does not define `root_agent`.")
307
- print(f"✅ Successfully loaded root_agent from {agent_file}")
308
- return getattr(module, "root_agent")
309
- except Exception as e:
310
- print(f"❌ Failed to load agent: {e}")
311
- raise RuntimeError(f"Failed to execute agent module {agent_file}: {e}") from e
312
- finally:
313
- for path in reversed(paths_added):
314
- while path in sys.path:
315
- sys.path.remove(path)
316
- # clean any submodules from this package
317
- mods_to_remove = [name for name in list(sys.modules.keys()) if name.startswith(package_name)]
318
- for name in mods_to_remove:
319
- sys.modules.pop(name, None)
320
-
321
- # ---------------- Utilities ----------------
322
- @staticmethod
323
- def _merge_requirements(baseline: List[str], user: List[str]) -> List[str]:
324
- seen = set()
325
- out: List[str] = []
326
- for seq in (baseline, user):
327
- for item in seq:
328
- key = item.strip().lower()
329
- if not key or key in seen:
330
- continue
331
- seen.add(key)
332
- out.append(item.strip())
333
- return out
334
-
335
- # ---------------- Main Creation Method ----------------
336
- def create_advanced_engine(self, config: Dict[str, Any]) -> Tuple[str, str, Optional[str]]:
337
- """Create a reasoning engine with advanced configuration options."""
338
- print("🚀 Starting advanced reasoning engine creation...")
339
- if self.debug:
340
- print("📋 Configuration:")
341
- try:
342
- print(json.dumps(config, indent=2))
343
- except Exception:
344
- print(str(config))
345
-
346
- try:
347
- display_name = config["display_name"]
348
- description = config.get("description", "")
349
- enable_tracing = config.get("enable_tracing", True)
350
-
351
- # Requirements
352
- requirements: List[str] = []
353
- if config["requirements_source_type"] == "file":
354
- req_file = config.get("requirements_file")
355
- if req_file and os.path.exists(req_file):
356
- with open(req_file, "r", encoding="utf-8") as f:
357
- requirements = [line.strip() for line in f if line.strip() and not line.strip().startswith("#")]
358
- elif config["requirements_source_type"] == "text":
359
- requirements_text = config.get("requirements_text", "").strip()
360
- requirements = [line.strip() for line in requirements_text.splitlines() if line.strip() and not line.strip().startswith("#")]
361
-
362
- # Ensure baseline ADK deps exist in the build venv (idempotent)
363
- baseline = [
364
- "google-adk>=1.0.0",
365
- "google-cloud-aiplatform[agent_engines]>=1.93.0,<2.0.0",
366
- "google-genai>=1.16.1,<2.0.0",
367
- ]
368
- requirements = self._merge_requirements(baseline, requirements)
369
-
370
- # Paths
371
- agent_file_path = config["agent_file_path"]
372
- agent_dir = os.path.dirname(agent_file_path)
373
-
374
- # venv lifecycle
375
- print("🌐 Setting up isolated virtual environment...")
376
- self._deactivate_current_venv()
377
- venv_name = self._create_venv_name(display_name)
378
- v_success, venv_path, python_exe = self._create_and_activate_venv(venv_name, agent_dir)
379
- if not v_success:
380
- raise RuntimeError("Failed to create virtual environment")
381
-
382
- # installs
383
- if requirements:
384
- if not self._install_requirements_in_venv(python_exe, requirements):
385
- self._cleanup_venv(venv_path)
386
- raise RuntimeError("Failed to install requirements in virtual environment")
387
-
388
- # make imports & subprocesses behave like activated
389
- self._push_venv_envvars(venv_path)
390
- venv_site_pkgs = self._add_venv_to_sys_path(python_exe)
391
-
392
- try:
393
- # quick guard against local google/ package
394
- self._assert_no_google_shadow(agent_dir)
395
-
396
- print("🔍 Checking agent directory structure...")
397
- agent_path = Path(agent_dir)
398
- tools_path = agent_path / "tools"
399
- if tools_path.exists() and self.debug:
400
- tool_files = [p.name for p in tools_path.glob("*.py")]
401
- print(f"✅ Found tools directory with files: {tool_files}")
402
- elif not tools_path.exists():
403
- print(f"❌ WARNING: tools directory not found at {tools_path}")
404
-
405
- staged_dir = self._stage_clean_copy(agent_dir)
406
- staged_tools = Path(staged_dir) / "tools"
407
- if not staged_tools.exists():
408
- print("❌ ERROR: Tools directory missing from staged copy!")
409
- raise RuntimeError("Tools directory was not properly staged")
410
-
411
- # load agent
412
- print(f"🤖 Loading root_agent from: {agent_file_path}")
413
- root_agent = self._load_agent_from_file(agent_file_path)
414
-
415
- # vertex init + create
416
- self._ensure_vertex_inited()
417
- print("🚀 Creating reasoning engine with venv dependencies…")
418
- app = AdkApp(agent=root_agent, enable_tracing=enable_tracing)
419
- remote = agent_engines.create(
420
- app,
421
- display_name=display_name,
422
- description=description,
423
- requirements=requirements,
424
- extra_packages=[staged_dir],
425
- )
426
-
427
- print("✅ Engine creation successful!")
428
- return (
429
- "created",
430
- f"Advanced engine '{display_name}' created successfully",
431
- remote.resource_name,
432
- )
433
-
434
- except Exception as e:
435
- print(f"❌ Deployment failed: {e}")
436
- raise
437
- finally:
438
- # undo import/env tweaks, then remove the venv
439
- if venv_site_pkgs:
440
- self._remove_venv_from_sys_path(venv_site_pkgs)
441
- self._pop_venv_envvars()
442
- print("🧹 Cleaning up virtual environment...")
443
- self._cleanup_venv(venv_path)
444
-
445
- except Exception as e:
446
- import traceback
447
- traceback.print_exc()
448
- return ("failed", f"Creation failed: {str(e)}", None)