上一轮提交 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)。
382 lines
12 KiB
Python
382 lines
12 KiB
Python
"""流式回复测试:节流、去重、超时续流,以及 AgentReply 的流式链路。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Any, ClassVar
|
|
|
|
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 (
|
|
ChannelConfig,
|
|
ChannelMessage,
|
|
ChannelSubject,
|
|
ImageContent,
|
|
InboundMessage,
|
|
OutboundStream,
|
|
TextContent,
|
|
)
|
|
from bot042.channels.registry import register_channel
|
|
from bot042.config import Settings
|
|
from bot042.db import repo
|
|
from bot042.flows import builtin
|
|
from bot042.flows.builtin import (
|
|
BUDGET_REPLY,
|
|
RPC_SEND,
|
|
RPC_STREAM,
|
|
AgentReply,
|
|
ChannelServices,
|
|
)
|
|
from bot042.flows.bus import EventBus
|
|
from bot042.flows.rpc import RpcHub
|
|
from bot042.flows.stream import ReplyStream
|
|
from bot042.flows.tasks import EventTrigger, RunStatus, TaskRunner
|
|
|
|
|
|
def test_stream_sends_ack_and_throttles() -> None:
|
|
sent: list[tuple[str, str, bool]] = []
|
|
now = [0.0]
|
|
|
|
async def _send(text: str, *, stream_id: str, finish: bool) -> None:
|
|
sent.append((stream_id, text, finish))
|
|
|
|
stream = ReplyStream(
|
|
_send, min_interval=1.0, segment_seconds=540, clock=lambda: now[0]
|
|
)
|
|
|
|
async def _run() -> None:
|
|
await stream.update("收到", force=True) # 首帧立即发
|
|
now[0] = 0.5
|
|
await stream.update("收到,正在") # 节流窗口内被跳过
|
|
now[0] = 1.2
|
|
await stream.update("收到,正在处理") # 发出
|
|
await stream.update("收到,正在处理") # 内容未变,跳过
|
|
await stream.finish()
|
|
|
|
asyncio.run(_run())
|
|
assert [t[1] for t in sent] == ["收到", "收到,正在处理", "收到,正在处理"]
|
|
assert [t[2] for t in sent] == [False, False, True]
|
|
assert len({t[0] for t in sent}) == 1
|
|
|
|
|
|
def test_stream_rollover_after_segment_limit() -> None:
|
|
"""超过平台存活上限时,结束旧流并另起一段。"""
|
|
sent: list[tuple[str, str, bool]] = []
|
|
now = [0.0]
|
|
|
|
async def _send(text: str, *, stream_id: str, finish: bool) -> None:
|
|
sent.append((stream_id, text, finish))
|
|
|
|
stream = ReplyStream(
|
|
_send, min_interval=0.0, segment_seconds=10, clock=lambda: now[0]
|
|
)
|
|
|
|
async def _run() -> None:
|
|
await stream.update("第一段", force=True)
|
|
now[0] = 11.0
|
|
await stream.update("第一段第二段") # 触发续流
|
|
await stream.finish()
|
|
|
|
asyncio.run(_run())
|
|
assert stream.segments == 2
|
|
stream_ids = {sid for sid, _, _ in sent}
|
|
assert len(stream_ids) == 2
|
|
# 旧段以 finish 收尾,新段首帧立刻铺上已有内容
|
|
first_id = sent[0][0]
|
|
assert (first_id, "第一段第二段", True) in sent
|
|
assert sent[-1][2] is True
|
|
|
|
|
|
def test_stream_ignores_send_failure() -> None:
|
|
"""单次推送失败不能打断生成。"""
|
|
sent: list[str] = []
|
|
|
|
async def _send(text: str, *, stream_id: str, finish: bool) -> None:
|
|
if not finish:
|
|
raise RuntimeError("boom")
|
|
sent.append(text)
|
|
|
|
stream = ReplyStream(_send, min_interval=0.0)
|
|
|
|
async def _run() -> None:
|
|
await stream.update("一半", force=True)
|
|
await stream.finish("完整答案")
|
|
|
|
asyncio.run(_run())
|
|
assert sent == ["完整答案"]
|
|
|
|
|
|
class StreamConfig(ChannelConfig):
|
|
pass
|
|
|
|
|
|
class StreamChannel(BaseChannel):
|
|
"""支持流式的测试渠道:记录每次 reply_stream 调用。"""
|
|
|
|
channel_type = "stream-test"
|
|
supports_stream = True
|
|
instances: ClassVar[list[StreamChannel]] = []
|
|
|
|
def __init__(
|
|
self,
|
|
processor: Any = None,
|
|
*,
|
|
config: StreamConfig | None = None,
|
|
channel_id: str | None = None,
|
|
**kwargs: Any,
|
|
) -> None:
|
|
super().__init__(processor, config=config, channel_id=channel_id, **kwargs)
|
|
self.frames: list[tuple[str, bool]] = []
|
|
self.contents: list[list] = []
|
|
"""每次富内容发送的分片组合(流式通道只送文本,媒体走这里)。"""
|
|
StreamChannel.instances.append(self)
|
|
|
|
async def start(self) -> None:
|
|
pass
|
|
|
|
async def stop(self) -> None:
|
|
pass
|
|
|
|
def parse_inbound(self, raw_payload: Any) -> InboundMessage:
|
|
if isinstance(raw_payload, InboundMessage):
|
|
return raw_payload
|
|
return InboundMessage(
|
|
channel_id=self.channel_id,
|
|
channel_type=self.channel_type,
|
|
channel_subject=ChannelSubject(subject_id="u1"),
|
|
content=[TextContent(text=str(raw_payload))],
|
|
)
|
|
|
|
def get_subject(self, subject_id: str) -> ChannelSubject | None:
|
|
return ChannelSubject(subject_id=subject_id)
|
|
|
|
async def reply_stream(
|
|
self, subject: ChannelSubject, text: str, *, stream_id: str, finish: bool = False
|
|
) -> None:
|
|
self.frames.append((text, finish))
|
|
|
|
async def _send_text(self, subject: ChannelSubject, text: str) -> None:
|
|
self.frames.append((text, True))
|
|
|
|
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
|
|
|
|
|
|
register_channel("stream-test", StreamChannel, StreamConfig)
|
|
|
|
|
|
def test_agent_reply_acks_then_streams(
|
|
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
|
) -> None:
|
|
"""收到消息先 ack,随后逐步覆盖,最后以 finish 收尾。"""
|
|
StreamChannel.instances.clear()
|
|
|
|
async def fake_stream(
|
|
task, settings=None, history=None, *, cache=None, steering=None, outbox=None
|
|
):
|
|
for text in ("你", "你好"):
|
|
yield text
|
|
|
|
monkeypatch.setattr(builtin, "stream_task", fake_stream)
|
|
|
|
async def _run() -> list[tuple[str, bool]]:
|
|
bus = EventBus()
|
|
bus.bind_loop()
|
|
hub = RpcHub(bus)
|
|
manager = ChannelManager()
|
|
channel = StreamChannel(channel_id="s1")
|
|
await manager.add_channel(channel)
|
|
await manager.start()
|
|
_ = hub.serve(RPC_STREAM, manager.rpc_stream)
|
|
|
|
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",
|
|
ChannelMessage.of(
|
|
InboundMessage(
|
|
channel_id="s1",
|
|
channel_type="stream-test",
|
|
channel_subject=ChannelSubject(subject_id="u1"),
|
|
content=[TextContent(text="你好")],
|
|
)
|
|
),
|
|
)
|
|
await asyncio.sleep(0.05) # 等 handler 真正开始派发
|
|
await runner.wait_idle()
|
|
await runner.stop()
|
|
hub.close()
|
|
await manager.stop()
|
|
return channel.frames
|
|
|
|
frames = asyncio.run(_run())
|
|
# 首帧 ack,首段内容立即覆盖(跳过节流),最后以 finish 收尾
|
|
assert frames[0] == (builtin.ACK_TEXT, False)
|
|
assert frames[1] == ("你", False)
|
|
assert frames[-1] == ("你好", True)
|
|
|
|
|
|
def test_agent_reply_streams_budget_notice_after_partial(
|
|
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
|
) -> None:
|
|
"""步数用尽:说明补在已流出的内容后面,落库,且不再补发通用错误文案。"""
|
|
StreamChannel.instances.clear()
|
|
|
|
async def exhausted(
|
|
task, settings=None, history=None, *, cache=None, steering=None, outbox=None
|
|
):
|
|
yield "做到一半"
|
|
raise StepBudgetExceeded(200)
|
|
|
|
monkeypatch.setattr(builtin, "stream_task", exhausted)
|
|
|
|
async def _run() -> tuple[list[tuple[str, bool]], list[RunStatus]]:
|
|
bus = EventBus()
|
|
bus.bind_loop()
|
|
hub = RpcHub(bus)
|
|
manager = ChannelManager()
|
|
channel = StreamChannel(channel_id="s1")
|
|
await manager.add_channel(channel)
|
|
await manager.start()
|
|
_ = hub.serve(RPC_STREAM, manager.rpc_stream)
|
|
|
|
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",
|
|
ChannelMessage.of(
|
|
InboundMessage(
|
|
channel_id="s1",
|
|
channel_type="stream-test",
|
|
channel_subject=ChannelSubject(subject_id="u1"),
|
|
content=[TextContent(text="把整站重构一遍")],
|
|
)
|
|
),
|
|
)
|
|
await asyncio.sleep(0.05)
|
|
await runner.wait_idle()
|
|
statuses = [run.status for run in runner.runs(AgentReply.name)]
|
|
await runner.stop()
|
|
hub.close()
|
|
await manager.stop()
|
|
return channel.frames, statuses
|
|
|
|
frames, statuses = asyncio.run(_run())
|
|
final = "做到一半\n\n" + BUDGET_REPLY.format(steps=200)
|
|
assert frames[0] == (builtin.ACK_TEXT, False)
|
|
assert frames[-1] == (final, True) # 收尾帧保留了部分内容 + 说明
|
|
assert statuses == [RunStatus.FAILED]
|
|
rows = asyncio.run(repo.list_messages(tmp_path / "bot.db", session_id="s1:u1"))
|
|
assert rows[-1]["role"] == "assistant"
|
|
assert rows[-1]["content"] == final
|
|
|
|
|
|
def test_agent_reply_sends_media_after_stream_finishes(
|
|
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
|
) -> None:
|
|
"""流式路径:文本走流式消息,媒体在文本定稿后单独补一条。"""
|
|
StreamChannel.instances.clear()
|
|
picture = tmp_path / "cat.png"
|
|
picture.write_bytes(b"\x89PNG\r\n\x1a\n" + b"\x00" * 32)
|
|
|
|
async def fake_stream(
|
|
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"))
|
|
yield "图"
|
|
yield "图来了"
|
|
|
|
monkeypatch.setattr(builtin, "stream_task", fake_stream)
|
|
|
|
async def _run() -> tuple[list[tuple[str, bool]], list[list], list[RunStatus]]:
|
|
bus = EventBus()
|
|
bus.bind_loop()
|
|
hub = RpcHub(bus)
|
|
manager = ChannelManager()
|
|
channel = StreamChannel(channel_id="s1")
|
|
await manager.add_channel(channel)
|
|
await manager.start()
|
|
_ = hub.serve(RPC_STREAM, manager.rpc_stream)
|
|
_ = hub.serve(RPC_SEND, manager.rpc_send)
|
|
|
|
services = ChannelServices(
|
|
channels=manager,
|
|
settings=Settings(home=tmp_path),
|
|
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",
|
|
ChannelMessage.of(
|
|
InboundMessage(
|
|
channel_id="s1",
|
|
channel_type="stream-test",
|
|
channel_subject=ChannelSubject(subject_id="u1"),
|
|
content=[TextContent(text="画只猫")],
|
|
)
|
|
),
|
|
)
|
|
await asyncio.sleep(0.05)
|
|
await runner.wait_idle()
|
|
statuses = [run.status for run in runner.runs(AgentReply.name)]
|
|
await runner.stop()
|
|
hub.close()
|
|
await manager.stop()
|
|
return channel.frames, channel.contents, statuses
|
|
|
|
frames, contents, statuses = asyncio.run(_run())
|
|
assert frames[-1] == ("图来了", True) # 文本仍然走流式收尾
|
|
assert statuses == [RunStatus.SUCCEEDED]
|
|
assert len(contents) == 1 # 媒体单独一条,且只含媒体分片
|
|
assert [type(part) for part in contents[0]] == [ImageContent]
|
|
|
|
|
|
def test_outbound_stream_payload_defaults() -> None:
|
|
payload = OutboundStream(
|
|
channel_id="c", subject_id="u", stream_id="s", text="x"
|
|
)
|
|
assert payload.finish is False
|
|
assert payload.metadata == {}
|
|
|
|
|
|
def test_manager_rejects_unknown_stream_payload() -> None:
|
|
manager = ChannelManager()
|
|
|
|
async def _run() -> None:
|
|
with pytest.raises(TypeError):
|
|
await manager.rpc_stream(json.dumps({"x": 1}))
|
|
|
|
asyncio.run(_run())
|