diff --git a/src/bot042/flows/builtin.py b/src/bot042/flows/builtin.py index 037c394..94d7dd8 100644 --- a/src/bot042/flows/builtin.py +++ b/src/bot042/flows/builtin.py @@ -16,6 +16,7 @@ from __future__ import annotations import logging +from collections.abc import Sequence from dataclasses import dataclass, field from pathlib import Path from typing import Any @@ -43,7 +44,7 @@ from bot042.flows.agent_task import ( run_task, stream_task, ) -from bot042.flows.media_input import prepare_inbound +from bot042.flows.media_input import describe_outbound, prepare_inbound from bot042.flows.processor import ( DEFAULT_HISTORY_CHARS, DEFAULT_HISTORY_LIMIT, @@ -286,6 +287,37 @@ async def _reply_with( await services.send_text(payload.channel_id, payload.subject_id, text, metadata=meta) +async def _persist_reply( + services: ChannelServices, + session_id: str, + channel: str, + text: str, + parts: Sequence[ContentPart], + *, + empty_ok: bool = False, +) -> None: + """把一轮助手回复写入会话历史:正文 + 附件清单(不存字节)。 + + ``parts`` 非空时即使正文为空也落一行——「只发了张图」是真实发生过的 + 一轮对话,丢掉它会让下一轮模型不知道自己发过什么,用户说「再发大点」 + 时就没有可回指的对象。 + + ``empty_ok=False`` 且两者都空时不写(纯占位回复 ``EMPTY_REPLY`` 不入库, + 与既有行为一致:那是给用户看的兜底文案,不是助手说的话)。 + """ + summary = describe_outbound(parts) + content = "\n".join(piece for piece in (text.strip(), summary) if piece) + if not content and not empty_ok: + return + await repo.add_message( + services.db, + session_id=session_id, + role="assistant", + content=content, + channel=channel, + ) + + class AgentReply(Task): """渠道消息任务:读历史 → 调智能体 → 落库 → 回复。 @@ -431,14 +463,11 @@ class AgentReply(Task): reply = _with_notice(answer, aborted) # 智能体可能只发了媒体、没写正文:这时不必再补一句「(无输出)」 pending = outbox.drain() - if reply: - await repo.add_message( - services.db, - session_id=session_id, - role="assistant", - content=reply, - channel=channel, - ) + # 媒体也要进历史:只发图不写字时正文为空,但这一轮确实产出过附件。 + # 先落库再下发——发送失败不该让下一轮失去「我发过什么」的上下文。 + await _persist_reply( + services, session_id, channel, reply, pending, empty_ok=bool(pending) + ) await _reply_with( services, payload, @@ -480,14 +509,9 @@ class AgentReply(Task): # 只有媒体没正文时传空串:ReplyStream.finish("") 不会发任何文本 await stream.finish(final or ("" if pending else EMPTY_REPLY)) await self._persist_steered(services, session_id, channel) - if final: - await repo.add_message( - services.db, - session_id=session_id, - role="assistant", - content=final, - channel=channel, - ) + await _persist_reply( + services, session_id, channel, final, pending, empty_ok=bool(pending) + ) if pending: # 流式通道只送文本,媒体只能补一条(文本此时已经定稿) await services.send_content( diff --git a/src/bot042/flows/media_input.py b/src/bot042/flows/media_input.py index 6430ca7..aa8a2e5 100644 --- a/src/bot042/flows/media_input.py +++ b/src/bot042/flows/media_input.py @@ -24,6 +24,7 @@ from __future__ import annotations import base64 +from collections.abc import Sequence from dataclasses import dataclass, field from pathlib import Path from typing import Any @@ -48,7 +49,7 @@ from bot042.channels.models import ( VideoContent, ) -__all__ = ["InlinePolicy", "PreparedInbound", "prepare_inbound"] +__all__ = ["InlinePolicy", "PreparedInbound", "describe_outbound", "prepare_inbound"] _TEXT_MIME_PREFIXES = ("text/",) _TEXT_EXTENSIONS = frozenset( @@ -151,6 +152,54 @@ def _describe(part: ContentPart, size: int, mime: str, path: Path) -> str: return f"{' / '.join(bits)},已保存到 {path}" +def _outbound_size(part: ContentPart) -> int: + """出站分片的字节数:优先模型声明的 ``size``,缺省按本地文件探测。 + + 工具构造分片时总会填 ``size``;这里兜底是为了让手工构造的分片 + (测试、插件)也能拿到合理描述,而不是在历史里写「0 B」。 + """ + declared = getattr(part, "size", None) + if declared: + return int(declared) + path = getattr(part, "local_path", None) + if path: + try: + return Path(path).stat().st_size + except OSError: + return 0 + return 0 + + +def describe_outbound(parts: Sequence[ContentPart]) -> str: + """把智能体发出的媒体整理成一行可入库的说明(只存引用,不存字节)。 + + 格式与入站 :func:`prepare_inbound` 的 summary 对齐(``[附件] a;b``): + 历史里「用户发的图」和「机器人发的图」长得一样,下一轮模型才能对照。 + 回指路径是本地文件而非平台地址——平台 `media_id` 有过期, + 而本地落盘路径在会话目录内可复现。 + + **刻意不写「已发送」**:记录在调用下发之前写入(发送失败时也要留住这轮 + 上下文),此时还没有证据说它发出去了。历史只陈述「这轮产出过这个附件」。 + """ + items: list[str] = [] + for part in parts: + if isinstance(part, TextContent): + continue + bits = [_media_label(part), format_size(_outbound_size(part))] + mime = getattr(part, "mime_type", None) + if mime: + bits.append(mime) + duration = getattr(part, "duration", None) + if isinstance(duration, int) and duration > 0: + bits.append(f"{duration / 1000:.1f}s") + head = " / ".join(bits) + local = getattr(part, "local_path", None) + items.append(f"{head}(本地 {local})" if local else head) + if not items: + return "" + return "[附件] " + ";".join(items) + + async def prepare_inbound( message: InboundMessage, *, diff --git a/tests/test_builtin.py b/tests/test_builtin.py index 75fa226..84fb634 100644 --- a/tests/test_builtin.py +++ b/tests/test_builtin.py @@ -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( diff --git a/tests/test_stream.py b/tests/test_stream.py index dc72357..e667357 100644 --- a/tests/test_stream.py +++ b/tests/test_stream.py @@ -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: