Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- 手机截图写到专家自己的工作区。只改定时任务文案时,间隔任务不再重新计时
- 浏览器工作台恢复中文输入。主动关怀等对话安静后再发
- 向导密码随数据目录迁移。Linux 桌面无边框窗口可以拖边缘调整大小
- 工具输出 JSON 里块 `type` 为对象或数组时,线程历史接口返回 500(`TypeError: unhashable type: 'dict'`);现按非媒体块跳过,且历史页媒体预览改写失败时保留原始输出并记日志,不再打挂整个线程历史(Fixes #1869)

### 变更
- 云端协同默认打码对端地址和账号。共享来的资源在头像上做标记
Expand Down
12 changes: 11 additions & 1 deletion src/octop/api/routers/chat/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,17 @@ def _enrich_history_tool_media(
if not isinstance(output, str) or not output.strip():
blocks.append(block)
continue
new_output = enrich_tool_output_string_sync(output, agent_id=media_agent)
try:
new_output = enrich_tool_output_string_sync(output, agent_id=media_agent)
except Exception:
# Rewriting preview URLs is cosmetic: keep the raw tool output
# rather than failing the whole history page.
logger.warning(
"history tool media enrichment failed agent=%s",
media_agent,
exc_info=True,
)
new_output = output
if new_output != output:
blocks.append({**block, "output": new_output})
changed = True
Expand Down
28 changes: 21 additions & 7 deletions src/octop/infra/gateway/media/tool_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@
)


def _media_block_type(block: Any) -> str:
"""Return ``block["type"]`` when it is a known media type, else ``""``.

Tool results occasionally carry a JSON object or list in ``type``; testing
such a value for membership in ``_MEDIA_BLOCK_TYPES`` raises ``TypeError``.
"""
if not isinstance(block, dict):
return ""
btype = block.get("type")
if isinstance(btype, str) and btype in _MEDIA_BLOCK_TYPES:
return btype
return ""


def tool_name_base(name: str) -> str:
trimmed = (name or "").strip()
slash = trimmed.rfind("/")
Expand Down Expand Up @@ -196,7 +210,7 @@ def iter_media_blocks(content: Any) -> list[dict[str, Any]]:
blocks: list[dict[str, Any]] = []
for candidate in candidates:
blocks.extend(_generated_media_blocks(candidate))
if isinstance(candidate, dict) and candidate.get("type") in _MEDIA_BLOCK_TYPES:
if _media_block_type(candidate):
blocks.append(candidate)
return blocks

Expand Down Expand Up @@ -343,9 +357,9 @@ async def _amap_content_blocks(

def enrich_media_block_preview(block: Any, *, agent_id: str) -> Any:
"""Rewrite media blocks for dashboard history (no workspace import)."""
if not isinstance(block, dict) or block.get("type") not in _MEDIA_BLOCK_TYPES:
if not _media_block_type(block):
return block
btype = str(block.get("type") or "")
btype = _media_block_type(block)
if btype == "file":
raw_url, mime, _ = _block_file_refs(block)
path = _file_block_path(block, raw_url)
Expand Down Expand Up @@ -492,9 +506,9 @@ def _file_block_path(block: dict[str, Any], raw_url: str) -> str | None:
async def _enrich_block_with_backend(
block: Any, *, agent_id: str, workspace: BackendWorkspace
) -> Any:
if not isinstance(block, dict) or block.get("type") not in _MEDIA_BLOCK_TYPES:
if not _media_block_type(block):
return block
btype = str(block.get("type") or "")
btype = _media_block_type(block)
# File blocks may already be path-only (no preview_url).
if block.get("preview_url") and btype != "file":
return block
Expand Down Expand Up @@ -831,8 +845,8 @@ async def block_to_content_part(
*,
workspace: BackendWorkspace,
) -> ContentPart | None:
btype = block.get("type")
if btype not in _MEDIA_BLOCK_TYPES:
btype = _media_block_type(block)
if not btype:
return None

source = block.get("source") or {}
Expand Down
56 changes: 56 additions & 0 deletions tests/unit/api/test_chat_polish.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,62 @@ def test_load_projected_falls_back_to_created_at() -> None:
assert messages[0]["timestamp"] == 1_700_000_000_000


def test_load_projected_tolerates_non_string_tool_block_type() -> None:
"""History must still load when a tool output block has an object ``type``."""
import json

from langchain_core.messages import message_to_dict

from octop.api.routers.chat.serialize import _load_projected_thread_messages

output = json.dumps([{"type": {"kind": "image"}}, {"text": "plain"}])
row = SimpleNamespace(
seq=1,
message_json=json.dumps(
message_to_dict(ToolMessage(content=output, tool_call_id="c1", id="m1"))
),
created_at=1_700_000_000,
)
server = SimpleNamespace(
services=SimpleNamespace(
thread_message_repo=SimpleNamespace(page=lambda *_a, **_k: ([row], False))
)
)
messages, has_more = _load_projected_thread_messages(server, "agent", "thread", 25, user=None)

assert has_more is False
assert [block["output"] for entry in messages for block in entry["content"]] == [output]


def test_load_projected_survives_unencodable_media_url() -> None:
"""Preview rewriting must degrade to the raw output, not 500 the page."""
import json

from langchain_core.messages import message_to_dict

from octop.api.routers.chat.serialize import _load_projected_thread_messages

# json.loads turns "\ud800" into a lone surrogate that cannot be quoted
# into a preview URL.
output = json.dumps({"type": "image", "source": {"url": "file:///tmp/\ud800.png"}})
row = SimpleNamespace(
seq=1,
message_json=json.dumps(
message_to_dict(ToolMessage(content=output, tool_call_id="c1", id="m1"))
),
created_at=1_700_000_000,
)
server = SimpleNamespace(
services=SimpleNamespace(
thread_message_repo=SimpleNamespace(page=lambda *_a, **_k: ([row], False))
)
)
messages, has_more = _load_projected_thread_messages(server, "agent", "thread", 25, user=None)

assert has_more is False
assert [block["output"] for entry in messages for block in entry["content"]] == [output]


def test_ts_to_ms_converts_seconds() -> None:
assert _ts_to_ms(1_700_000_000.5) == 1_700_000_000_500

Expand Down
47 changes: 47 additions & 0 deletions tests/unit/gateway/test_tool_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,53 @@ def test_iter_media_blocks_dict_content() -> None:
assert len(iter_media_blocks(block)) == 1


@pytest.mark.parametrize(
"dirty_type",
[{"kind": "image"}, ["image"], 3, None],
)
def test_media_blocks_ignore_non_string_type(dirty_type: object) -> None:
"""A JSON block whose ``type`` is not a string must not raise TypeError."""
block = {"type": dirty_type, "text": "plain"}
payload = json.dumps([block])

assert iter_media_blocks(payload) == []
assert enrich_media_block_preview(block, agent_id="A1") == block


def test_enrich_sync_tolerates_non_string_block_type() -> None:
from octop.infra.gateway.media.tool_media import enrich_tool_output_string_sync

payload = json.dumps([{"type": {"kind": "image"}}, {"text": "plain"}])
assert enrich_tool_output_string_sync(payload, agent_id="A1") == payload


@pytest.mark.asyncio
async def test_enrich_tool_result_tolerates_non_string_block_type() -> None:
"""Live tool-result enrichment must skip the dirty block, not crash."""
from octop.infra.gateway.media.tool_media import enrich_tool_result_with_backend

payload = json.dumps([{"type": {"kind": "image"}}, {"text": "plain"}])
chunk = {
"type": "tool_result",
"name": "desktop_screenshot",
"messages": [{"name": "desktop_screenshot", "content": payload}],
}
with tempfile.TemporaryDirectory() as ws:
enriched = await enrich_tool_result_with_backend(
chunk, agent_id="A1", workspace=_workspace(ws)
)
assert enriched["messages"][0]["content"] == payload


@pytest.mark.asyncio
async def test_block_to_content_part_ignores_non_string_type() -> None:
from octop.infra.gateway.media.tool_media import block_to_content_part

with tempfile.TemporaryDirectory() as ws:
part = await block_to_content_part({"type": {"kind": "image"}}, workspace=_workspace(ws))
assert part is None


@pytest.mark.asyncio
async def test_enrich_send_file_dict_content() -> None:
with tempfile.TemporaryDirectory() as ws, tempfile.TemporaryDirectory() as ext_dir:
Expand Down