from deerflow.runtime.display_payload import ( DISPLAY_TOOL_TEXT_MAX_CHARS, slim_display_messages, ) from deerflow.runtime.serialization import serialize_channel_values def test_slim_display_truncates_write_file_body_and_keeps_visible_answer() -> None: answer = "这是用户要看的完整回答。" file_body = "x" * (DISPLAY_TOOL_TEXT_MAX_CHARS + 800) messages = slim_display_messages( [ {"type": "human", "id": "h1", "content": "写一份长报告"}, { "type": "ai", "id": "a1", "content": answer, "tool_calls": [ { "id": "w1", "name": "write_file", "args": {"path": "/mnt/user-data/outputs/report.html", "content": file_body}, } ], "additional_kwargs": { "tool_calls": [ { "id": "w1", "function": {"name": "write_file", "arguments": file_body}, } ] }, }, { "type": "tool", "id": "t1", "tool_call_id": "w1", "content": "y" * (DISPLAY_TOOL_TEXT_MAX_CHARS + 50), }, ] ) write = messages[1]["tool_calls"][0] assert write["args"]["path"] == "/mnt/user-data/outputs/report.html" assert len(write["args"]["content"]) < len(file_body) assert "display truncated" in write["args"]["content"] assert messages[1]["content"] == answer assert "display truncated" in messages[2]["content"] assert "display truncated" in messages[1]["additional_kwargs"]["tool_calls"][0]["function"]["arguments"] def test_serialize_channel_values_slim_strips_viewed_image_bodies() -> None: values = { "viewed_images": { "/mnt/user-data/uploads/a.png": { "base64": "iVBORw0KGgo=" * 200, "mime_type": "image/png", } } } full = serialize_channel_values(values)["viewed_images"]["/mnt/user-data/uploads/a.png"] slim = serialize_channel_values(values, slim_display=True)["viewed_images"]["/mnt/user-data/uploads/a.png"] assert full["base64"].startswith("iVBORw0KGgo") assert slim == {"mime_type": "image/png"} def test_serialize_channel_values_slim_flag_does_not_change_default() -> None: body = "z" * (DISPLAY_TOOL_TEXT_MAX_CHARS + 20) values = { "messages": [ { "type": "ai", "content": "ok", "tool_calls": [{"id": "w1", "name": "write_file", "args": {"content": body}}], } ] } full = serialize_channel_values(values)["messages"][0]["tool_calls"][0]["args"]["content"] slim = serialize_channel_values(values, slim_display=True)["messages"][0]["tool_calls"][0]["args"]["content"] assert full == body assert "display truncated" in slim