agenthub/backend/tests/channel_config_probe.py

56 lines
1.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""Print the GatewayConfig our nanobot channel builds, one process per run.
渠道代码在 deploy/ 下(compose 只读挂进容器),不在 site-packages 里,所以这里按
nanobot 自己的发现口径把它注册成 nanobot.channels.agenthub 再导入 —— 用的是真
BaseChannel,不是测试替身。
.nanobot-venv/bin/python backend/tests/channel_config_probe.py '<json>'
入参是一组用例 [{"config": {...}, "env": {"AGENTHUB_X": ...}}, ...],输出等长的结果
数组;每项是解析出的 config,或 {"error": "ValueError: ..."}。
"""
from __future__ import annotations
import dataclasses
import importlib
import json
import os
import pathlib
import sys
import types
ROOT = pathlib.Path(__file__).resolve().parents[2]
CHANNEL_DIR = ROOT / "deploy" / "nanobot-gateway" / "channel" / "agenthub"
def load_runtime():
import nanobot.channels # 真包;渠道要挂到它的 __path__ 名下
name = "nanobot.channels.agenthub"
package = types.ModuleType(name)
package.__path__ = [str(CHANNEL_DIR)]
sys.modules[name] = package
setattr(sys.modules["nanobot.channels"], "agenthub", package)
return importlib.import_module(name + ".runtime")
def main() -> None:
cases = json.loads(sys.argv[1])
runtime = load_runtime()
base_env = {key: value for key, value in os.environ.items() if not key.startswith("AGENTHUB_")}
results = []
for case in cases:
os.environ.clear()
os.environ.update(base_env)
os.environ.update(case.get("env") or {})
try:
channel = runtime.AgentHubChannel(case.get("config") or {}, None)
results.append(dataclasses.asdict(channel._gateway_config()))
except Exception as exc: # 用例故意喂坏配置时,把异常类型原样带回去
results.append({"error": type(exc).__name__ + ": " + str(exc)})
print(json.dumps(results, ensure_ascii=False))
main()