Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix:加载manager_config_receiver换成相对目录方式加载

wangxinc5b809fe1ff7
diff --git a/jiuwenswarm/infrastructure/module_importer.py b/jiuwenswarm/infrastructure/module_importer.py
index eb0249e42b..93692cf1cc 100644
--- a/jiuwenswarm/infrastructure/module_importer.py
+++ b/jiuwenswarm/infrastructure/module_importer.py
@@ -1,30 +1,94 @@
# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.
-"""动态加载 Gateway EE 扩展 ``manager_config_receiver`` 子模块。"""
+"""从 ``manager_config_receiver`` EE 扩展按命名空间加载子模块(不修改 ``sys.path``)。
+
+AgentServer 不一定会走扩展动态加载把 ``packages/jiuwenclaw-ee/gateway/extensions``
+注册进 ``jiuwenswarm.loaded_extension``,因此这里按磁盘路径解析扩展根并挂到
+``jiuwenswarm.loaded_extension.manager_config_receiver`` 命名空间后再 import。
+"""
from __future__ import annotations
import importlib
+import os
import sys
-from types import ModuleType
+import types
+from pathlib import Path
+from typing import Any
+
+
+LOADED_EXTENSION_PARENT_PKG = "jiuwenswarm.loaded_extension"
+MANAGER_CONFIG_RECEIVER_EXT_PKG = f"{LOADED_EXTENSION_PARENT_PKG}.manager_config_receiver"
+
+
+def is_manager_config_receiver_available() -> bool:
+ """``manager_config_receiver`` 扩展目录是否可解析(不执行子模块 import)。"""
+ return resolve_manager_config_receiver_root() is not None
+
+
+def resolve_manager_config_receiver_root() -> Path | None:
+ """解析 ``manager_config_receiver`` 扩展根目录(``EXTENSION_DIRS`` 或仓库内置路径)。"""
+ for entry in os.getenv("EXTENSION_DIRS", "").split(os.pathsep):
+ raw = entry.strip()
+ if not raw:
+ continue
+ base = Path(raw).expanduser()
+ for candidate in (base, base / "manager_config_receiver"):
+ if (candidate / "infrastructure" / "db.py").is_file():
+ return candidate.resolve()
+
+ # jiuwenswarm/infrastructure/module_importer.py → parents[2] = 仓库根
+ repo_root = Path(__file__).resolve().parents[2]
+ bundled = (
+ repo_root
+ / "packages"
+ / "jiuwenclaw-ee"
+ / "gateway"
+ / "extensions"
+ / "manager_config_receiver"
+ )
+ if (bundled / "infrastructure" / "db.py").is_file():
+ return bundled.resolve()
+ return None
+
+
+def ensure_manager_config_receiver_package(ext_root: Path | None = None) -> Path:
+ """注册 ``jiuwenswarm.loaded_extension.manager_config_receiver`` 包命名空间。"""
+ root = ext_root if ext_root is not None else resolve_manager_config_receiver_root()
+ if root is None:
+ raise ImportError("manager_config_receiver extension not found")
+
+ root_str = str(root.resolve())
+ if LOADED_EXTENSION_PARENT_PKG not in sys.modules:
+ parent = types.ModuleType(LOADED_EXTENSION_PARENT_PKG)
+ parent.__path__ = []
+ sys.modules[LOADED_EXTENSION_PARENT_PKG] = parent
+
+ existing = sys.modules.get(MANAGER_CONFIG_RECEIVER_EXT_PKG)
+ if existing is not None:
+ paths = list(getattr(existing, "__path__", []) or [])
+ if root_str not in paths:
+ paths.append(root_str)
+ existing.__path__ = paths
+ return root
-_EXTENSION_NAME = "manager_config_receiver"
+ ext_pkg = types.ModuleType(MANAGER_CONFIG_RECEIVER_EXT_PKG)
+ ext_pkg.__path__ = [root_str]
+ ext_pkg.__package__ = MANAGER_CONFIG_RECEIVER_EXT_PKG
+ sys.modules[MANAGER_CONFIG_RECEIVER_EXT_PKG] = ext_pkg
+ return root
-def _loaded_extension_module() -> ModuleType:
- full = f"jiuwenswarm.loaded_extension.{_EXTENSION_NAME}"
- if full in sys.modules:
- return sys.modules[full]
- return importlib.import_module(full)
+def import_manager_config_receiver_module(module_suffix: str) -> Any:
+ """导入扩展内子模块,``module_suffix`` 为点分路径(相对扩展根包)。
+ 示例::
-def import_manager_config_receiver_module(submodule: str) -> ModuleType:
- """导入 ``manager_config_receiver`` 扩展内子模块。"""
- full = f"jiuwenswarm.loaded_extension.{_EXTENSION_NAME}.{submodule}"
- if full in sys.modules:
- return sys.modules[full]
- parent = f"jiuwenswarm.loaded_extension.{_EXTENSION_NAME}"
- if parent in sys.modules:
- return importlib.import_module(f".{submodule}", parent)
- root = _loaded_extension_module()
- return importlib.import_module(f".{submodule}", root.__name__)
+ import_manager_config_receiver_module("infrastructure.db")
+ import_manager_config_receiver_module("core.enterprise_config.gateway_db")
+ """
+ suffix = str(module_suffix or "").strip().lstrip(".")
+ if not suffix:
+ raise ValueError("module_suffix is required")
+ ensure_manager_config_receiver_package()
+ return importlib.import_module(f"{MANAGER_CONFIG_RECEIVER_EXT_PKG}.{suffix}")
diff --git a/tests/unit_tests/infrastructure/test_module_importer.py b/tests/unit_tests/infrastructure/test_module_importer.py
index 5e5e2842bd..1e652351ff 100644
--- a/tests/unit_tests/infrastructure/test_module_importer.py
+++ b/tests/unit_tests/infrastructure/test_module_importer.py
@@ -1,38 +1,62 @@
# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.
-"""module_importer:按需加载 manager_config_receiver EE 扩展子模块。"""
+"""module_importer:按磁盘路径挂载 manager_config_receiver EE 扩展子模块。"""
from __future__ import annotations
import sys
import types
+from pathlib import Path
import pytest
from jiuwenswarm.infrastructure import module_importer as mi
-def test_import_manager_config_receiver_module_uses_loaded_extension(
+def test_resolve_manager_config_receiver_root_bundled() -> None:
+ root = mi.resolve_manager_config_receiver_root()
+ assert root is not None
+ assert (root / "infrastructure" / "db.py").is_file()
+ assert root.name == "manager_config_receiver"
+
+
+def test_import_manager_config_receiver_module_from_path(
+ monkeypatch: pytest.MonkeyPatch,
+ tmp_path: Path,
+) -> None:
+ ext = tmp_path / "manager_config_receiver"
+ (ext / "infrastructure").mkdir(parents=True)
+ (ext / "infrastructure" / "db.py").write_text("# marker\n", encoding="utf-8")
+ (ext / "stubmod.py").write_text("VALUE = 7\n", encoding="utf-8")
+
+ monkeypatch.delenv("EXTENSION_DIRS", raising=False)
+ monkeypatch.setattr(mi, "resolve_manager_config_receiver_root", lambda: ext.resolve())
+ # 清掉可能已存在的命名空间,强制按临时目录重新挂载。
+ sys.modules.pop(mi.MANAGER_CONFIG_RECEIVER_EXT_PKG, None)
+ sys.modules.pop(f"{mi.MANAGER_CONFIG_RECEIVER_EXT_PKG}.stubmod", None)
+
+ mod = mi.import_manager_config_receiver_module("stubmod")
+ assert mod.VALUE == 7
+
+
+def test_import_manager_config_receiver_module_reuses_sys_modules(
monkeypatch: pytest.MonkeyPatch,
) -> None:
- parent = types.ModuleType("jiuwenswarm.loaded_extension")
+ parent = types.ModuleType(mi.LOADED_EXTENSION_PARENT_PKG)
parent.__path__ = []
- root = types.ModuleType("jiuwenswarm.loaded_extension.manager_config_receiver")
+ root = types.ModuleType(mi.MANAGER_CONFIG_RECEIVER_EXT_PKG)
root.__path__ = []
- stub = types.ModuleType(
- "jiuwenswarm.loaded_extension.manager_config_receiver.stubmod"
- )
- stub.VALUE = 7
-
- monkeypatch.setitem(sys.modules, "jiuwenswarm.loaded_extension", parent)
- monkeypatch.setitem(
- sys.modules, "jiuwenswarm.loaded_extension.manager_config_receiver", root
- )
- monkeypatch.setitem(
- sys.modules,
- "jiuwenswarm.loaded_extension.manager_config_receiver.stubmod",
- stub,
+ stub = types.ModuleType(f"{mi.MANAGER_CONFIG_RECEIVER_EXT_PKG}.stubmod")
+ stub.VALUE = 9
+
+ monkeypatch.setitem(sys.modules, mi.LOADED_EXTENSION_PARENT_PKG, parent)
+ monkeypatch.setitem(sys.modules, mi.MANAGER_CONFIG_RECEIVER_EXT_PKG, root)
+ monkeypatch.setitem(sys.modules, f"{mi.MANAGER_CONFIG_RECEIVER_EXT_PKG}.stubmod", stub)
+ monkeypatch.setattr(
+ mi,
+ "ensure_manager_config_receiver_package",
+ lambda ext_root=None: Path("."),
)
mod = mi.import_manager_config_receiver_module("stubmod")
assert mod is stub
- assert mod.VALUE == 7
+ assert mod.VALUE == 9