feat: 重构为 agent / channels / flows / db 分层架构

- agent: 新增 base.py 执行基类、execution.py 执行器、web.py 网络工具,
  middlewares/steering.py 中间件;移除旧的 agent.py
- channels: 新增 ChannelManager、registry、models 与 IM 渠道(企业微信),
  扩展 base.py
- flows: 新增 agent_task / bus / cron / scheduler / service / stream / rpc /
  processor / tasks / model_settings / builtin;移除旧的 task.py
- db: 新增 sqlite schema 与 repo 层
- docs: 新增架构说明、Channel × EventBus 设计、任务编写教程
- 依赖新增 aiosqlite / httpx / langgraph / primp / pydantic / ripgrep /
  wecom-aibot-sdk;新增 bot 命令入口
- 测试:232 passed
This commit is contained in:
openbot committed 2026-11-09 20:32:25 +08:00
1 parent 94421e5c2b
commit fbb01c4c83
65 files changed
+15322 -173

No files matched your search

+193
View File
@@ -0,0 +1,193 @@
"""事件总线测试:纯异步 handler 的调度、once 与 error 路由。"""
from __future__ import annotations
import asyncio
import pytest
from bot042.flows.bus import BusError, EventBus
def test_emit_schedules_async_handlers() -> None:
async def main() -> None:
bus = EventBus()
got: list[tuple[str, int]] = []
async def handler(name: str, n: int) -> None:
await asyncio.sleep(0)
got.append((name, n))
bus.on("evt", handler)
assert bus.emit("evt", "a", 1) is True
assert bus.complete is False
await bus.wait_for_complete()
assert got == [("a", 1)]
assert bus.complete is True
asyncio.run(main())
def test_once_handler_runs_single_time() -> None:
async def main() -> None:
bus = EventBus()
calls: list[int] = []
async def handler(n: int) -> None:
calls.append(n)
bus.once("evt", handler)
bus.emit("evt", 1)
bus.emit("evt", 2)
await bus.wait_for_complete()
assert calls == [1]
asyncio.run(main())
def test_handler_error_routed_to_error_event() -> None:
async def main() -> None:
bus = EventBus()
errors: list[BaseException] = []
async def on_error(exc: BaseException) -> None:
errors.append(exc)
async def boom() -> None:
raise ValueError("boom")
bus.on("error", on_error)
bus.on("go", boom)
bus.emit("go")
await bus.wait_for_complete()
assert len(errors) == 1
assert isinstance(errors[0], ValueError)
asyncio.run(main())
def test_sync_handler_rejected_at_registration() -> None:
bus = EventBus()
with pytest.raises(BusError):
bus.on("ping", lambda: None)
def test_unhandled_error_event_raises() -> None:
bus = EventBus()
with pytest.raises(ValueError):
bus.emit("error", ValueError("x"))
with pytest.raises(BusError):
bus.emit("error", "not an exception")
def test_handler_sync_failure_never_escapes_emit() -> None:
"""handler 调用时元数不匹配(同步 TypeError)不能从 emit 冒出来。"""
async def main() -> None:
bus = EventBus()
async def no_args() -> None:
pass
bus.on("evt", no_args)
assert bus.emit("evt", 1) is True # 没有 error 监听者也不抛
await bus.wait_for_complete()
errors: list[BaseException] = []
async def on_error(exc: BaseException) -> None:
errors.append(exc)
bus.on("error", on_error)
assert bus.emit("evt", 2) is True
await bus.wait_for_complete()
assert len(errors) == 1
assert isinstance(errors[0], TypeError)
asyncio.run(main())
def test_publish_subscribe_aliases() -> None:
async def main() -> None:
bus = EventBus()
got: list[int] = []
async def handler(n: int) -> None:
got.append(n)
bus.subscribe("evt", handler)
assert bus.publish("evt", 1) is True
await bus.wait_for_complete()
assert got == [1]
async def once_handler(n: int) -> None:
got.append(n * 10)
bus.subscribe_once("evt2", once_handler)
bus.publish("evt2", 2)
bus.publish("evt2", 3)
await bus.wait_for_complete()
assert got == [1, 20]
asyncio.run(main())
def test_publish_threadsafe_same_thread() -> None:
async def main() -> None:
bus = EventBus()
bus.bind_loop()
got: list[int] = []
async def handler(n: int) -> None:
got.append(n)
bus.on("evt", handler)
bus.publish_threadsafe("evt", 7)
await bus.wait_for_complete()
assert got == [7]
asyncio.run(main())
def test_publish_threadsafe_cross_thread() -> None:
import threading
async def main() -> None:
bus = EventBus()
bus.bind_loop()
got: list[int] = []
async def handler(n: int) -> None:
got.append(n)
bus.on("evt", handler)
thread = threading.Thread(target=lambda: bus.publish_threadsafe("evt", 42))
thread.start()
await asyncio.sleep(0.05)
thread.join()
await bus.wait_for_complete()
assert got == [42]
asyncio.run(main())
def test_publish_threadsafe_without_loop_raises() -> None:
bus = EventBus()
with pytest.raises(BusError, match="bind_loop"):
bus.publish_threadsafe("evt", 1)
def test_duplicate_handler_registration_rejected() -> None:
"""同一事件重复注册同一 handler 会静默覆盖并让 once 失效,注册时应报错。"""
async def main() -> None:
bus = EventBus()
async def handler() -> None:
pass
bus.once("evt", handler)
with pytest.raises(BusError):
bus.on("evt", handler)
asyncio.run(main())