Files
bot042/tests/test_builtin.py
T
openbot 2dde0d1160 feat(flows): 媒体入站进出站链路接线(issue #1 P0)
上一轮提交 9ce0ba0 把 media.py / outbox.py 的骨架铺好了,但没有任何调用方:
入站附件进不了上下文,出站 MediaOutbox 从不被创建,send_image / send_file
两个工具因此恒定返回「当前不在渠道会话中」。本次把链路接上。

入站:
- AgentReply 改用 prepare_inbound 整理分片:小图内联为 image_url 块,
  大图与音视频退化为「路径 + 元信息」,文本类文件抽取正文进上下文
- 附件全部失败时回「附件未能接收:<原因>」,不再误报成「(无输出)」
- 正文用 prepared.summary(正文 + 附件清单),历史只存清单不存字节
- 只有正文发纯文本,带附件才发 content blocks(is_multimodal)

出站:
- 每轮创建一个 MediaOutbox 传给 run_task / stream_task,工具往里登记分片,
  跑完 drain() 出来与正文合成一条 OutboundContent
- 只有媒体没有正文时不再补「(无输出)」;流式通道文本定稿后单独补发媒体
- ChannelServices.send_content 统一走「先文本、再逐个媒体」

解密钩子的抽象层:
- BaseChannel.media_transform(part, raw) 默认原样返回
- BaseChannel.make_media_transform(metadata) 按单条消息构造钩子。密钥随消息
  而来,挂在渠道实例上会让并发处理的两条消息串用彼此的密钥
- ChannelServices.media_transform 从渠道取钩子,渠道未注册时返回 None
  (不解密,但也不跳过后续的类型与大小校验)
- media.py 新增 MediaTransform 类型与 store_media(transform=)。钩子在类型嗅探
  与大小判定之前执行——对密文嗅魔数只会得到 octet-stream——并在转换后对明文
  再量一次上限,因为密文没超不等于明文没超

测试:test_builtin 增加 6 条媒体用例(文本+图、纯图、附件全失败、出站下发、
仅媒体不补空文案、流式补发),test_service / test_stream 的 fake_run_task 补
outbox 形参。全量 250 passed,ruff 全绿。

P0 边界说明:本提交只做接线与抽象,不含企业微信的具体解密实现、下载限长、
CLI 媒体命令与 docs 更新,这些在下一个提交(P1)。
2026-11-10 09:46:16 +08:00

495 lines
17 KiB
Python

"""内置任务 AgentReply 测试:会话分区、落库、RPC 回复与失败提示。"""
from __future__ import annotations
import asyncio
import base64
from datetime import datetime
from pathlib import Path
from typing import Any
import pytest
from bot042.agent.middlewares.budget import StepBudgetExceeded
from bot042.channels.base import BaseChannel
from bot042.channels.manager import ChannelManager
from bot042.channels.models import (
ChannelMessage,
ChannelSubject,
ImageContent,
InboundMessage,
TextContent,
)
from bot042.config import Settings
from bot042.db import repo
from bot042.flows import builtin
from bot042.flows.builtin import (
BUDGET_REPLY,
ERROR_REPLY,
RPC_SEND,
AgentReply,
ChannelServices,
)
from bot042.flows.bus import EventBus
from bot042.flows.media_input import PreparedInbound
from bot042.flows.rpc import RpcHub
from bot042.flows.scheduler import CN_TZ
from bot042.flows.tasks import EventTrigger, RunStatus, TaskContext, TaskRunner
class EchoChannel(BaseChannel):
"""测试渠道:任何 subject 都视为已登记,发送记录在内存。"""
channel_type = "echo"
def __init__(self) -> None:
super().__init__(channel_id="c1")
self.sent: list[str] = []
self.contents: list[list[Any]] = []
"""每次富内容发送的分片组合(文本 + 媒体)。"""
async def start(self) -> None:
pass
async def stop(self) -> None:
pass
def parse_inbound(self, raw_payload: Any) -> InboundMessage:
assert isinstance(raw_payload, InboundMessage)
return raw_payload
def get_subject(self, subject_id: str) -> ChannelSubject | None:
return ChannelSubject(subject_id=subject_id)
async def _send_text(self, subject: ChannelSubject, text: str) -> None:
self.sent.append(text)
async def _send_content(self, subject: ChannelSubject, parts: list) -> None:
self.contents.append(list(parts))
async def _send_media(self, subject: ChannelSubject, media: Any) -> None:
pass
def _envelope(
text: str, *, session: str | None = "s1", subject: str = "u1"
) -> ChannelMessage:
message = InboundMessage(
channel_id="c1",
channel_type="echo",
channel_session_id=session,
channel_subject=ChannelSubject(subject_id=subject),
content=[TextContent(text=text)],
)
return ChannelMessage.of(message, session_key=session or "")
def test_partition_key_matches_db_session_id() -> None:
"""分区键必须与会话历史使用的 session_id 一致。"""
ctx = TaskContext(
run_id="r1",
task=AgentReply.name,
event="channel.inbound",
fired_at=datetime.now(CN_TZ),
payload=_envelope("hi"),
)
assert AgentReply.partition_key(ctx) == "s1"
# 群聊:渠道锁键是发送者(user-a),历史会话键是群(chat-9),分区跟历史走
group = _envelope("hi", session="chat-9").model_copy(
update={"session_key": "user-a", "subject_id": "user-a"}
)
ctx2 = ctx.model_copy(update={"payload": group})
assert AgentReply.partition_key(ctx2) == "chat-9"
# 没有平台会话 ID 时回退「渠道:主体」,与 run() 的落库表达式一致
ctx3 = ctx.model_copy(update={"payload": _envelope("hi", session=None)})
assert AgentReply.partition_key(ctx3) == "c1:u1"
def test_db_session_id_property() -> None:
"""db_session_id 是落库与分区共用的唯一来源(消息本体与总线载荷一致)。"""
group = _envelope("hi", session="chat-9")
assert group.db_session_id == "chat-9"
assert group.message.db_session_id == "chat-9"
direct = _envelope("hi", session=None)
assert direct.db_session_id == "c1:u1"
assert direct.message.db_session_id == "c1:u1"
def test_agent_reply_persists_and_replies_via_rpc(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
calls: list[tuple[str, list]] = []
async def fake_run_task(
task, settings=None, history=None, *, cache=None, steering=None, outbox=None
):
calls.append((task, list(history or [])))
return {"messages": [{"role": "assistant", "content": "答:" + task}]}
monkeypatch.setattr(builtin, "run_task", fake_run_task)
async def _run() -> tuple[list[str], list[str], list[str]]:
bus = EventBus()
bus.bind_loop()
hub = RpcHub(bus)
manager = ChannelManager()
channel = EchoChannel()
await manager.add_channel(channel)
await manager.start()
_ = hub.serve(RPC_SEND, manager.rpc_send)
services = ChannelServices(
channels=manager,
settings=Settings(),
db=tmp_path / "bot.db",
hub=hub,
)
runner = TaskRunner(bus, deps=services)
runner.register(AgentReply)
_ = runner.add_task(AgentReply.name, EventTrigger(event="channel.inbound"))
await runner.start()
bus.emit("channel.inbound", _envelope("第一句"))
bus.emit("channel.inbound", _envelope("第二句"))
await runner.wait_idle(timeout=2)
await bus.wait_for_complete()
runs = runner.runs(AgentReply.name)
keys = [run.key for run in runs]
statuses = [run.status.value for run in runs]
await runner.stop()
hub.close()
await manager.stop()
return channel.sent, keys, statuses
sent, keys, statuses = asyncio.run(_run())
assert sent == ["答:第一句", "答:第二句"]
assert keys == ["s1", "s1"]
assert statuses == ["succeeded", "succeeded"]
# 第二轮上下文里有第一轮问答
assert calls[1][1] == [
{"role": "user", "content": "第一句"},
{"role": "assistant", "content": "答:第一句"},
]
rows = asyncio.run(repo.list_messages(tmp_path / "bot.db", session_id="s1"))
assert [(r["role"], r["content"]) for r in rows] == [
("user", "第一句"),
("assistant", "答:第一句"),
("user", "第二句"),
("assistant", "答:第二句"),
]
def test_agent_reply_sends_error_hint_on_failure(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
async def boom(task, settings=None, history=None, *, cache=None):
raise RuntimeError("agent down")
monkeypatch.setattr(builtin, "run_task", boom)
async def _run() -> tuple[list[str], list[RunStatus]]:
bus = EventBus()
bus.bind_loop()
hub = RpcHub(bus)
manager = ChannelManager()
channel = EchoChannel()
await manager.add_channel(channel)
await manager.start()
_ = hub.serve(RPC_SEND, manager.rpc_send)
services = ChannelServices(
channels=manager, settings=Settings(), db=tmp_path / "bot.db", hub=hub
)
runner = TaskRunner(bus, deps=services)
runner.register(AgentReply)
_ = runner.add_task(AgentReply.name, EventTrigger(event="channel.inbound"))
await runner.start()
bus.emit("channel.inbound", _envelope("会失败"))
await runner.wait_idle(timeout=2)
await bus.wait_for_complete()
statuses = [run.status for run in runner.runs(AgentReply.name)]
await runner.stop()
hub.close()
await manager.stop()
return channel.sent, statuses
sent, statuses = asyncio.run(_run())
assert sent == [ERROR_REPLY]
assert statuses == [RunStatus.FAILED]
def test_agent_reply_explains_step_budget_without_generic_error(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""步数用尽:给出可操作说明并落库,且不再补发通用错误文案。"""
async def exhausted(
task, settings=None, history=None, *, cache=None, steering=None, outbox=None
):
raise StepBudgetExceeded(200)
monkeypatch.setattr(builtin, "run_task", exhausted)
async def _run() -> tuple[list[str], list[RunStatus]]:
bus = EventBus()
bus.bind_loop()
hub = RpcHub(bus)
manager = ChannelManager()
channel = EchoChannel()
await manager.add_channel(channel)
await manager.start()
_ = hub.serve(RPC_SEND, manager.rpc_send)
services = ChannelServices(
channels=manager, settings=Settings(), db=tmp_path / "bot.db", hub=hub
)
runner = TaskRunner(bus, deps=services)
runner.register(AgentReply)
_ = runner.add_task(AgentReply.name, EventTrigger(event="channel.inbound"))
await runner.start()
bus.emit("channel.inbound", _envelope("把整站重构一遍"))
await runner.wait_idle(timeout=2)
await bus.wait_for_complete()
statuses = [run.status for run in runner.runs(AgentReply.name)]
await runner.stop()
hub.close()
await manager.stop()
return channel.sent, statuses
sent, statuses = asyncio.run(_run())
notice = BUDGET_REPLY.format(steps=200)
assert sent == [notice] # 只有说明,没有多余的「处理消息时出错」
assert statuses == [RunStatus.FAILED]
# 失败轮也要落库:用户已经看到这段内容,历史缺了它会前后不接
rows = asyncio.run(repo.list_messages(tmp_path / "bot.db", session_id="s1"))
assert [(r["role"], r["content"]) for r in rows] == [
("user", "把整站重构一遍"),
("assistant", notice),
]
# ---------------------------------------------------------------------------
# 媒体链路:入站进上下文、出站经信箱下发
# ---------------------------------------------------------------------------
PNG_HEAD = b"\x89PNG\r\n\x1a\n" + b"\x00\x00\x00\rIHDR" + b"\x00" * 16
"""最小 PNG 头:magic + IHDR 片段,足够 check_mime 认定成 image/png。"""
def _image_part() -> ImageContent:
"""入站图片分片:base64 直接带字节,不依赖网络与临时文件。"""
return ImageContent(
data=base64.b64encode(PNG_HEAD).decode("ascii"),
mime_type="image/png",
size=len(PNG_HEAD),
)
def _media_envelope(*parts: Any, text: str = "") -> ChannelMessage:
"""构造带任意内容分片的入站消息。"""
content = ([TextContent(text=text)] if text else []) + list(parts)
message = InboundMessage(
channel_id="c1",
channel_type="echo",
channel_session_id="s1",
channel_subject=ChannelSubject(subject_id="u1"),
content=content,
)
return ChannelMessage.of(message, session_key="s1")
def _drive(
db: Path,
payloads: list[ChannelMessage],
channel: EchoChannel,
*,
home: Path,
) -> list[RunStatus]:
"""跑一遍 AgentReply(调用方先 monkeypatch run_task / prepare_inbound)。
``home`` 指向临时目录,避免把测试媒体写进真实的 ``~/.bot042/data``。
"""
async def _run() -> list[RunStatus]:
bus = EventBus()
bus.bind_loop()
hub = RpcHub(bus)
manager = ChannelManager()
await manager.add_channel(channel)
await manager.start()
_ = hub.serve(RPC_SEND, manager.rpc_send)
services = ChannelServices(
channels=manager,
settings=Settings(home=home),
db=db,
hub=hub,
)
runner = TaskRunner(bus, deps=services)
runner.register(AgentReply)
_ = runner.add_task(AgentReply.name, EventTrigger(event="channel.inbound"))
await runner.start()
for payload in payloads:
bus.emit("channel.inbound", payload)
await runner.wait_idle(timeout=2)
await bus.wait_for_complete()
statuses = [run.status for run in runner.runs(AgentReply.name)]
await runner.stop()
hub.close()
await manager.stop()
return statuses
return asyncio.run(_run())
def test_agent_reply_feeds_inbound_image_to_agent(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""文本 + 图片:两者都进上下文,图片走 image_url 且不入库字节。"""
seen: list[Any] = []
async def fake_run_task(
task, settings=None, history=None, *, cache=None, steering=None, outbox=None
):
seen.append(task)
return {"messages": [{"role": "assistant", "content": "图里是一只猫"}]}
monkeypatch.setattr(builtin, "run_task", fake_run_task)
channel = EchoChannel()
statuses = _drive(
tmp_path / "bot.db",
[_media_envelope(_image_part(), text="这是什么")],
channel,
home=tmp_path,
)
assert statuses == [RunStatus.SUCCEEDED]
blocks = seen[0]
assert isinstance(blocks, list)
assert [b["type"] for b in blocks] == ["text", "image_url"]
assert blocks[0]["text"] == "这是什么"
assert blocks[1]["image_url"]["url"].startswith("data:image/png;base64,")
rows = asyncio.run(repo.list_messages(tmp_path / "bot.db", session_id="s1"))
assert rows[0]["role"] == "user"
assert "[附件]" in rows[0]["content"]
assert "base64" not in rows[0]["content"] # 历史只存清单,不存字节
# 落盘在 home 下的会话目录里
assert list((tmp_path / "data" / "inbound").rglob("*.png"))
# 回复没有媒体 → 仍走纯文本
assert channel.sent == ["图里是一只猫"]
assert channel.contents == []
def test_agent_reply_media_only_inbound_is_multimodal(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""只有图、没有正文:不能被当成空消息,也不能退化成纯文本把图丢掉。"""
seen: list[Any] = []
async def fake_run_task(
task, settings=None, history=None, *, cache=None, steering=None, outbox=None
):
seen.append(task)
return {"messages": [{"role": "assistant", "content": "收到"}]}
monkeypatch.setattr(builtin, "run_task", fake_run_task)
channel = EchoChannel()
statuses = _drive(
tmp_path / "bot.db", [_media_envelope(_image_part())], channel, home=tmp_path
)
assert statuses == [RunStatus.SUCCEEDED]
assert [b["type"] for b in seen[0]] == ["image_url"]
def test_agent_reply_reports_rejected_attachments(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""附件全部没接住:说清原因,不再笼统地当成「空消息」。"""
async def fake_prepare(
message, *, root, limits=None, policy=None, headers=None, transform=None
):
return PreparedInbound(text="", notes=["图片未接收:超过大小上限"])
async def boom(*args, **kwargs): # pragma: no cover - 不应被调用
raise AssertionError("附件全部失败时不应执行任务")
monkeypatch.setattr(builtin, "prepare_inbound", fake_prepare)
monkeypatch.setattr(builtin, "run_task", boom)
channel = EchoChannel()
statuses = _drive(
tmp_path / "bot.db", [_media_envelope(_image_part())], channel, home=tmp_path
)
assert statuses == [RunStatus.SUCCEEDED]
assert channel.sent == ["附件未能接收:图片未接收:超过大小上限"]
def test_agent_reply_delivers_agent_media_in_one_content_message(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""智能体用工具发的图:与正文合成一条富内容,不额外再发一条纯文本。"""
picture = tmp_path / "cat.png"
picture.write_bytes(PNG_HEAD)
async def fake_run_task(
task, settings=None, history=None, *, cache=None, steering=None, outbox=None
):
assert outbox is not None
outbox.push(ImageContent(local_path=str(picture), mime_type="image/png"))
return {"messages": [{"role": "assistant", "content": "图来了"}]}
monkeypatch.setattr(builtin, "run_task", fake_run_task)
channel = EchoChannel()
statuses = _drive(tmp_path / "bot.db", [_envelope("画只猫")], channel, home=tmp_path)
assert statuses == [RunStatus.SUCCEEDED]
assert channel.sent == [] # 没有再发一条纯文本
assert len(channel.contents) == 1
parts = channel.contents[0]
assert isinstance(parts[0], TextContent) and parts[0].text == "图来了"
assert isinstance(parts[1], ImageContent)
assert parts[1].local_path == str(picture)
def test_agent_reply_media_only_reply_skips_empty_placeholder(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""只发了媒体、没写正文:不要补一句「(无输出)」。"""
picture = tmp_path / "cat.png"
picture.write_bytes(PNG_HEAD)
async def fake_run_task(
task, settings=None, history=None, *, cache=None, steering=None, outbox=None
):
assert outbox is not None
outbox.push(ImageContent(local_path=str(picture), mime_type="image/png"))
return {"messages": [{"role": "assistant", "content": ""}]}
monkeypatch.setattr(builtin, "run_task", fake_run_task)
channel = EchoChannel()
statuses = _drive(tmp_path / "bot.db", [_envelope("发张图")], channel, home=tmp_path)
assert statuses == [RunStatus.SUCCEEDED]
assert channel.sent == []
assert [type(part) for part in channel.contents[0]] == [ImageContent]
def test_media_policy_falls_back_to_defaults(tmp_path: Path) -> None:
"""拿不到渠道实例时按默认上限跑,不能因为查不到渠道就跳过校验。"""
services = ChannelServices(
channels=ChannelManager(), settings=Settings(home=tmp_path), db=tmp_path / "bot.db"
)
limits, headers = services.media_policy("no-such-channel")
assert headers == {}
assert limits.image > 0 and limits.file > 0