feat(flows): 助手出站媒体写入会话历史(issue #1 验收标准 8)
智能体发的附件此前只下发不落库:下一轮模型不知道上一轮发过什么,用户说 「再发大点」时没有可回指的对象。新增 describe_outbound() 把出站分片整理成 一行引用说明(类型/大小/MIME/时长/本地路径),与入站 prepare_inbound 的 summary 格式对齐,只存引用不存字节。 两条处理链都改走 _persist_reply(): - 同步链:原先只在 reply 非空时落库,纯媒体回复会让这一轮完全消失 - 流式链:原先同样只写正文 历史写入保持在发送之前——发送失败不该让下一轮失去上下文,因此不回滚, 且刻意不写「已发送」。 tests: test_builtin 3 条(附件入库、纯媒体入库、发送失败仍入库), test_stream 补断流式分支的落库断言。变异验证:把两处调用点改回旧写法, 对应断言分别变红。
This commit is contained in:
1 parent
13b8d7a466
commit
d0e187f74d
4 files changed
+186
-21
No files matched your search
@@ -484,6 +484,90 @@ def test_agent_reply_media_only_reply_skips_empty_placeholder(
|
||||
assert [type(part) for part in channel.contents[0]] == [ImageContent]
|
||||
|
||||
|
||||
def test_agent_reply_persists_agent_media_to_history(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
||||
) -> None:
|
||||
"""智能体发的附件要进历史:下一轮模型才知道自己发过什么。"""
|
||||
|
||||
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", size=len(PNG_HEAD))
|
||||
)
|
||||
return {"messages": [{"role": "assistant", "content": "图来了"}]}
|
||||
|
||||
picture = tmp_path / "cat.png"
|
||||
picture.write_bytes(PNG_HEAD)
|
||||
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]
|
||||
rows = asyncio.run(repo.list_messages(tmp_path / "bot.db", session_id="s1"))
|
||||
assert rows[-1]["role"] == "assistant"
|
||||
content = rows[-1]["content"]
|
||||
assert content.startswith("图来了")
|
||||
assert "[附件] 图片" in content
|
||||
assert str(picture) in content # 存路径引用,便于下一轮回指
|
||||
assert "base64" not in content # 不存字节
|
||||
|
||||
|
||||
def test_agent_reply_media_only_reply_still_lands_in_history(
|
||||
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]
|
||||
rows = asyncio.run(repo.list_messages(tmp_path / "bot.db", session_id="s1"))
|
||||
assert [r["role"] for r in rows] == ["user", "assistant"]
|
||||
assert rows[-1]["content"].startswith("[附件] 图片")
|
||||
assert "无输出" not in rows[-1]["content"] # 不把兜底文案当成助手说的话
|
||||
|
||||
|
||||
def test_agent_reply_persists_reply_before_delivery(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
||||
) -> None:
|
||||
"""下发失败也要留住上下文:历史写入先于发送,且不回滚。"""
|
||||
|
||||
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": "给你"}]}
|
||||
|
||||
picture = tmp_path / "cat.png"
|
||||
picture.write_bytes(PNG_HEAD)
|
||||
monkeypatch.setattr(builtin, "run_task", fake_run_task)
|
||||
|
||||
class BrokenChannel(EchoChannel):
|
||||
async def _send_content(self, subject, parts): # type: ignore[override]
|
||||
raise RuntimeError("平台发送失败")
|
||||
|
||||
channel = BrokenChannel()
|
||||
statuses = _drive(tmp_path / "bot.db", [_envelope("发图")], channel, home=tmp_path)
|
||||
|
||||
assert statuses == [RunStatus.FAILED]
|
||||
rows = asyncio.run(repo.list_messages(tmp_path / "bot.db", session_id="s1"))
|
||||
assert [r["role"] for r in rows] == ["user", "assistant"]
|
||||
assert "给你" in rows[-1]["content"]
|
||||
|
||||
|
||||
def test_media_policy_falls_back_to_defaults(tmp_path: Path) -> None:
|
||||
"""拿不到渠道实例时按默认上限跑,不能因为查不到渠道就跳过校验。"""
|
||||
services = ChannelServices(
|
||||
|
||||
+11
-3
@@ -315,7 +315,9 @@ def test_agent_reply_sends_media_after_stream_finishes(
|
||||
|
||||
monkeypatch.setattr(builtin, "stream_task", fake_stream)
|
||||
|
||||
async def _run() -> tuple[list[tuple[str, bool]], list[list], list[RunStatus]]:
|
||||
async def _run() -> tuple[
|
||||
list[tuple[str, bool]], list[list], list[RunStatus], list
|
||||
]:
|
||||
bus = EventBus()
|
||||
bus.bind_loop()
|
||||
hub = RpcHub(bus)
|
||||
@@ -354,13 +356,19 @@ def test_agent_reply_sends_media_after_stream_finishes(
|
||||
await runner.stop()
|
||||
hub.close()
|
||||
await manager.stop()
|
||||
return channel.frames, channel.contents, statuses
|
||||
rows = await repo.list_messages(tmp_path / "bot.db", session_id="s1:u1")
|
||||
return channel.frames, channel.contents, statuses, rows
|
||||
|
||||
frames, contents, statuses = asyncio.run(_run())
|
||||
frames, contents, statuses, rows = asyncio.run(_run())
|
||||
assert frames[-1] == ("图来了", True) # 文本仍然走流式收尾
|
||||
assert statuses == [RunStatus.SUCCEEDED]
|
||||
assert len(contents) == 1 # 媒体单独一条,且只含媒体分片
|
||||
assert [type(part) for part in contents[0]] == [ImageContent]
|
||||
# 流式分支同样要落库附件:它和同步分支共用 _persist_reply,走的是另一处调用点
|
||||
assert rows[-1]["role"] == "assistant"
|
||||
assert rows[-1]["content"].startswith("图来了")
|
||||
assert "[附件] 图片" in rows[-1]["content"]
|
||||
assert str(picture) in rows[-1]["content"]
|
||||
|
||||
|
||||
def test_outbound_stream_payload_defaults() -> None:
|
||||
|
||||
Reference in new issue
Block a user