oclaw/runtime/hooks/_selftest.py
oliver 4a23b715a2 重构仓库目录为统一的 runtime 分层并清理历史 openclaw 残留。
本次迁移将网关/通道/工具/技能/脚本与协议资源集中到新结构,统一路径常量与脚本转发机制,减少顶层噪音并保证运行与测试行为一致。

Made-with: Cursor
2026-04-25 01:24:23 +08:00

95 lines
2.9 KiB
Python

from __future__ import annotations
import asyncio
from pathlib import Path
import sys
HOOKS_PY_DIR = Path(__file__).resolve().parent
OCLAW_DIR = HOOKS_PY_DIR.parent
if str(OCLAW_DIR) not in sys.path:
sys.path.insert(0, str(OCLAW_DIR))
from oclaw.runtime.hooks.internal_hooks import create_hook_event, trigger_hook # noqa: E402
from oclaw.runtime.hooks.loader import load_internal_hooks # noqa: E402
def main() -> int:
repo_root = Path(__file__).resolve().parents[1]
bundled = repo_root / "runtime" / "hooks" / "bundled"
ws_dir = repo_root / "_hooks_selftest_workspace"
ws_dir.mkdir(parents=True, exist_ok=True)
(ws_dir / "memory").mkdir(parents=True, exist_ok=True)
cfg = {
"hooks": {
"internal": {
"enabled": True,
"entries": {
"bootstrap-extra-files": {
"enabled": True,
"paths": ["**/AGENTS.md"],
},
"session-memory": {
"enabled": True,
"messages": 15,
},
},
}
}
}
# Use repo root as a fake "workspace dir" for this self-test.
loaded = load_internal_hooks(cfg, workspace_dir=str(ws_dir), bundled_hooks_dir=str(bundled))
print(f"loaded_hooks={loaded}")
async def run() -> None:
# gateway:startup
ev0 = create_hook_event(
"gateway",
"startup",
"agent:main:main",
context={"cfg": cfg, "workspaceDir": str(ws_dir)},
)
await trigger_hook(ev0)
# agent:bootstrap (inject extra files)
# prepare a fake AGENTS.md to match **/AGENTS.md
(ws_dir / "AGENTS.md").write_text("# AGENTS\n", encoding="utf-8")
ctx = {"cfg": cfg, "workspaceDir": str(ws_dir), "bootstrapFiles": []}
ev1 = create_hook_event("agent", "bootstrap", "agent:main:main", context=ctx)
await trigger_hook(ev1)
print(f"bootstrapFiles_after={len(ctx.get('bootstrapFiles') or [])}")
# command:new and command:reset (writes memory markdown; also command-logger runs on command)
ev = create_hook_event(
"command",
"new",
"agent:main:main",
context={
"senderId": "selftest",
"commandSource": "local",
"cfg": cfg,
"workspaceDir": str(ws_dir),
},
)
await trigger_hook(ev)
ev2 = create_hook_event(
"command",
"reset",
"agent:main:main",
context={
"senderId": "selftest",
"commandSource": "local",
"cfg": cfg,
"workspaceDir": str(ws_dir),
},
)
await trigger_hook(ev2)
asyncio.run(run())
return 0
if __name__ == "__main__":
raise SystemExit(main())