"""Agent package for the Farient brand-compliance review system. This package lives at ``apps/api/agents/`` and coexists with the ``openai-agents`` SDK which is *also* installed under the name ``agents`` in site-packages. Because Python resolves ``import agents`` to whichever ``agents/`` appears first on ``sys.path`` (our local copy wins when CWD is ``apps/api``), we need to explicitly bootstrap the SDK symbols here so that the rest of the codebase can do:: from agents import Agent, Runner, RunContextWrapper, function_tool from agents.tool import ToolOutputImage from agents.context import AuditContext # our local submodule from agents.tools import … # our local submodule Strategy -------- 1. Temporarily remove ``apps/api`` (and ``""``) from ``sys.path``. 2. Clear any partially-loaded ``agents.*`` modules from ``sys.modules``. 3. Import the SDK's ``agents`` package — it now resolves to site-packages. 4. Add our local ``agents/`` directory to the SDK module's ``__path__`` so that ``from agents.context import …`` and ``from agents.tools import …`` find our local submodules. 5. Re-inject every SDK public name into *this* module's namespace so that ``from agents import RunContextWrapper`` also works when Python has loaded our ``__init__.py`` first. 6. Replace ``sys.modules["agents"]`` with the SDK module (which now has the extended ``__path__``). """ from __future__ import annotations import sys from pathlib import Path # --------------------------------------------------------------------------- # Bootstrap SDK symbols + extend SDK __path__ with our local dir # --------------------------------------------------------------------------- _OUR_AGENTS_DIR = str(Path(__file__).parent.resolve()) # apps/api/agents _OUR_API_DIR = str(Path(__file__).parent.parent.resolve()) # apps/api def _bootstrap_sdk() -> None: """Load SDK agents, extend its __path__ with our dir, inject names here.""" exclude = {_OUR_API_DIR, ""} sdk_path = [p for p in sys.path if p not in exclude] # Stash any of *our* already-loaded submodules (shouldn't exist yet). our_prefix = "agents." saved_ours: dict = {} for key in list(sys.modules.keys()): if key == "agents" or key.startswith(our_prefix): saved_ours[key] = sys.modules.pop(key) original_path = sys.path[:] sys.path = sdk_path try: import agents as _sdk # resolves to site-packages/agents finally: sys.path = original_path # Make SDK submodule imports also search our local agents/ directory. if _OUR_AGENTS_DIR not in _sdk.__path__: _sdk.__path__.insert(0, _OUR_AGENTS_DIR) # Replace sys.modules["agents"] with the SDK module (extended __path__). sys.modules["agents"] = _sdk # Inject SDK public names into this module's globals so that code that # already imported *this* __init__.py (before the replacement) can still # do ``from agents import RunContextWrapper`` etc. import importlib _this = sys.modules.get(__name__) if _this is not None and _this is not _sdk: for _name in dir(_sdk): if not _name.startswith("__"): setattr(_this, _name, getattr(_sdk, _name)) # Restore our saved submodules. for key, mod in saved_ours.items(): if key != "agents": sys.modules.setdefault(key, mod) _bootstrap_sdk() del _bootstrap_sdk