|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -from octop.infra.gateway.process.stream_project import enrich_tool_stream_chunk |
| 5 | +import json |
| 6 | +from pathlib import Path |
| 7 | +from unittest.mock import MagicMock |
| 8 | + |
| 9 | +import pytest |
| 10 | +from langchain_core.messages import ToolMessage |
| 11 | +from octop_gateway.models import ImageContent, MessageEventType |
| 12 | +from tests.support.fakes import FakeHarnessAgent |
| 13 | + |
| 14 | +from octop.i18n import tr |
| 15 | +from octop.infra.gateway.process.stream_project import ( |
| 16 | + enrich_tool_stream_chunk, |
| 17 | + project_resume_stream, |
| 18 | + project_stream, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.parametrize("resume", [False, True]) |
| 23 | +@pytest.mark.parametrize("available,locale", [(True, "zh"), (False, "zh"), (False, "en")]) |
| 24 | +@pytest.mark.asyncio |
| 25 | +async def test_generated_media_projection_uses_workspace_and_user_locale( |
| 26 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, available: bool, locale: str, resume: bool |
| 27 | +) -> None: |
| 28 | + monkeypatch.setenv("OCTOP_HOME", str(tmp_path)) |
| 29 | + agent = FakeHarnessAgent() |
| 30 | + agent.use_workspace_dir(tmp_path / "workspace") |
| 31 | + path = "generated/images/result.png" |
| 32 | + if available: |
| 33 | + await agent.workspace.aupload_bytes(path, b"PNG") |
| 34 | + result = { |
| 35 | + "type": "tool_result", |
| 36 | + "messages": [ |
| 37 | + ToolMessage( |
| 38 | + name="generate_image", |
| 39 | + tool_call_id="generate-1", |
| 40 | + content=json.dumps( |
| 41 | + { |
| 42 | + "type": "image_gen_tool_result", |
| 43 | + "images": [{"path": path, "mediaType": "image/png"}], |
| 44 | + } |
| 45 | + ), |
| 46 | + ) |
| 47 | + ], |
| 48 | + } |
| 49 | + |
| 50 | + async def stream(*_args, **_kwargs): |
| 51 | + yield { |
| 52 | + "type": "tool_call_chunk", |
| 53 | + "name": "generate_image", |
| 54 | + "args": "{}", |
| 55 | + "id": "generate-1", |
| 56 | + } |
| 57 | + yield result |
| 58 | + yield result # Harness history replay must not resend media or errors. |
| 59 | + |
| 60 | + manager = MagicMock() |
| 61 | + manager.get_agent.return_value = agent |
| 62 | + manager.stream = stream |
| 63 | + manager.resume_hitl = stream |
| 64 | + events = [ |
| 65 | + event |
| 66 | + async for event in ( |
| 67 | + project_resume_stream(manager, "agent-1", "thread-1", [], locale=locale) |
| 68 | + if resume |
| 69 | + else project_stream(manager, "agent-1", {}, locale=locale) |
| 70 | + ) |
| 71 | + ] |
| 72 | + deliveries = [ |
| 73 | + event |
| 74 | + for event in events |
| 75 | + if event.type in {MessageEventType.MESSAGE, MessageEventType.ERROR} |
| 76 | + ] |
| 77 | + |
| 78 | + assert len(deliveries) == 1 |
| 79 | + if available: |
| 80 | + assert deliveries[0].type == MessageEventType.MESSAGE |
| 81 | + part = deliveries[0].content[0] |
| 82 | + assert isinstance(part, ImageContent) |
| 83 | + assert part.data == "UE5H" |
| 84 | + assert not part.url |
| 85 | + else: |
| 86 | + assert deliveries[0].type == MessageEventType.ERROR |
| 87 | + assert deliveries[0].error == tr("channel.media_delivery_failed", locale) |
| 88 | + assert path not in deliveries[0].model_dump_json() |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize("same_content", [False, True]) |
| 92 | +@pytest.mark.parametrize("resume", [False, True]) |
| 93 | +@pytest.mark.asyncio |
| 94 | +async def test_generated_media_is_not_resent_by_send_file( |
| 95 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, same_content: bool, resume: bool |
| 96 | +) -> None: |
| 97 | + monkeypatch.setenv("OCTOP_HOME", str(tmp_path)) |
| 98 | + agent = FakeHarnessAgent() |
| 99 | + agent.use_workspace_dir(tmp_path / "workspace") |
| 100 | + path = "generated/images/result.png" |
| 101 | + await agent.workspace.aupload_bytes(path, b"PNG") |
| 102 | + |
| 103 | + generated = { |
| 104 | + "type": "tool_result", |
| 105 | + "messages": [ |
| 106 | + ToolMessage( |
| 107 | + name="generate_image", |
| 108 | + tool_call_id="generate-1", |
| 109 | + content=json.dumps( |
| 110 | + { |
| 111 | + "type": "image_gen_tool_result", |
| 112 | + "images": [{"path": path, "mediaType": "image/png"}], |
| 113 | + } |
| 114 | + ), |
| 115 | + ) |
| 116 | + ], |
| 117 | + } |
| 118 | + sent = { |
| 119 | + "type": "tool_result", |
| 120 | + "messages": [ |
| 121 | + ToolMessage( |
| 122 | + name="send_file_to_user", |
| 123 | + tool_call_id="send-2", |
| 124 | + content=json.dumps( |
| 125 | + { |
| 126 | + "type": "image", |
| 127 | + "path": path, |
| 128 | + "filename": "result.png", |
| 129 | + "source": { |
| 130 | + "type": "url", |
| 131 | + "url": path, |
| 132 | + "media_type": "image/png", |
| 133 | + }, |
| 134 | + } |
| 135 | + ), |
| 136 | + ) |
| 137 | + ], |
| 138 | + } |
| 139 | + |
| 140 | + async def stream(*_args, **_kwargs): |
| 141 | + await agent.workspace.aupload_bytes(path, b"PNG") |
| 142 | + yield { |
| 143 | + "type": "tool_call_chunk", |
| 144 | + "name": "generate_image", |
| 145 | + "args": "{}", |
| 146 | + "index": 0, |
| 147 | + "id": "generate-1", |
| 148 | + } |
| 149 | + yield generated |
| 150 | + if not same_content: |
| 151 | + await agent.workspace.aupload_bytes(path, b"NEW") |
| 152 | + yield { |
| 153 | + "type": "tool_call_chunk", |
| 154 | + "name": "send_file_to_user", |
| 155 | + "args": "{}", |
| 156 | + "index": 1, |
| 157 | + "id": "send-2", |
| 158 | + } |
| 159 | + yield sent |
| 160 | + |
| 161 | + manager = MagicMock() |
| 162 | + manager.get_agent.return_value = agent |
| 163 | + manager.stream = stream |
| 164 | + manager.resume_hitl = stream |
| 165 | + for _turn in range(2): |
| 166 | + events = [ |
| 167 | + event |
| 168 | + async for event in ( |
| 169 | + project_resume_stream(manager, "agent-1", "thread-1", [], locale="zh") |
| 170 | + if resume |
| 171 | + else project_stream(manager, "agent-1", {}, locale="zh") |
| 172 | + ) |
| 173 | + ] |
| 174 | + media = [ |
| 175 | + part |
| 176 | + for event in events |
| 177 | + if event.type == MessageEventType.MESSAGE |
| 178 | + for part in event.content |
| 179 | + if isinstance(part, ImageContent) |
| 180 | + ] |
| 181 | + |
| 182 | + assert [part.data for part in media] == (["UE5H"] if same_content else ["UE5H", "TkVX"]) |
6 | 183 |
|
7 | 184 |
|
8 | 185 | def test_enrich_tool_stream_chunk_adds_display_name(): |
|
0 commit comments