fix(配置): 模型相对路径按运行目录解析
模型相对路径以进程当前工作目录为基准,绝对路径继续直接使用。示例改为 models/guard_model,并补充路径迁移与容器挂载说明。 新增工作目录与配置目录分离、绝对路径和缺失目录校验测试。配置与 LLM 测试 20 项通过;全量测试 75 项通过,2 项现有插件示例测试因缺少初始化配置失败。
This commit is contained in:
1 parent
fd71fc4063
commit
5d71803b02
4 files changed
+49
-3
No files matched your search
+45
-1
@@ -6,12 +6,56 @@ from pydantic import ValidationError
|
||||
from middleware.config import NetworkRule, Settings, load_settings
|
||||
|
||||
|
||||
def test_configuration_and_paths():
|
||||
def test_configuration_and_paths(tmp_path, monkeypatch):
|
||||
monkeypatch.chdir(tmp_path)
|
||||
(tmp_path / "models" / "guard_model").mkdir(parents=True)
|
||||
settings = load_settings(Path(__file__).parents[1] / "config.yaml")
|
||||
assert settings.plugins[0].file.is_absolute()
|
||||
assert settings.limits.max_sessions == 4
|
||||
|
||||
|
||||
@pytest.mark.parametrize("absolute", [False, True])
|
||||
def test_model_path_uses_working_directory(tmp_path, monkeypatch, absolute):
|
||||
workdir = tmp_path / "service"
|
||||
model_dir = workdir / "models" / "guard_model"
|
||||
model_dir.mkdir(parents=True)
|
||||
config_dir = tmp_path / "config"
|
||||
config_dir.mkdir()
|
||||
plugin = config_dir / "guard.lua"
|
||||
plugin.write_text("return {}", encoding="utf-8")
|
||||
model_path = model_dir.as_posix() if absolute else "models/guard_model"
|
||||
config = config_dir / "config.yaml"
|
||||
config.write_text(
|
||||
f"upstream: http://agent:8000\nllm:\n - id: guard\n type: guard\n"
|
||||
f" file: {model_path}\nplugins:\n - id: guard\n file: guard.lua\n"
|
||||
" llm: {allow: [guard]}\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
monkeypatch.chdir(workdir)
|
||||
|
||||
settings = load_settings(config)
|
||||
|
||||
assert settings.llm[0].file == model_dir.resolve()
|
||||
assert settings.plugins[0].file == plugin.resolve()
|
||||
|
||||
|
||||
def test_missing_model_does_not_fall_back_to_config_directory(tmp_path, monkeypatch):
|
||||
workdir = tmp_path / "service"
|
||||
workdir.mkdir()
|
||||
config_dir = tmp_path / "config"
|
||||
(config_dir / "models" / "guard_model").mkdir(parents=True)
|
||||
config = config_dir / "config.yaml"
|
||||
config.write_text(
|
||||
"upstream: http://agent:8000\nllm:\n - id: guard\n type: guard\n"
|
||||
" file: models/guard_model\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
monkeypatch.chdir(workdir)
|
||||
|
||||
with pytest.raises(ValueError, match="llm model path not found: guard"):
|
||||
load_settings(config)
|
||||
|
||||
|
||||
def test_duplicate_ids_and_missing_file(tmp_path):
|
||||
path = tmp_path / "config.yaml"
|
||||
path.write_text(
|
||||
|
||||
Reference in new issue
Block a user