fix(ui): emit utf-8 backend protocol on windows
This commit is contained in:
@@ -279,7 +279,13 @@ class ReactBackendHost:
|
||||
|
||||
async def _emit(self, event: BackendEvent) -> None:
|
||||
async with self._write_lock:
|
||||
sys.stdout.write(_PROTOCOL_PREFIX + event.model_dump_json() + "\n")
|
||||
payload = _PROTOCOL_PREFIX + event.model_dump_json() + "\n"
|
||||
buffer = getattr(sys.stdout, "buffer", None)
|
||||
if buffer is not None:
|
||||
buffer.write(payload.encode("utf-8"))
|
||||
buffer.flush()
|
||||
return
|
||||
sys.stdout.write(payload)
|
||||
sys.stdout.flush()
|
||||
|
||||
|
||||
|
||||
@@ -2,12 +2,16 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from openharness.api.client import ApiMessageCompleteEvent
|
||||
from openharness.api.usage import UsageSnapshot
|
||||
from openharness.engine.messages import ConversationMessage, TextBlock
|
||||
from openharness.ui.backend_host import BackendHostConfig, ReactBackendHost
|
||||
from openharness.ui.protocol import BackendEvent
|
||||
from openharness.ui.runtime import build_runtime, close_runtime, start_runtime
|
||||
|
||||
|
||||
@@ -26,6 +30,16 @@ class StaticApiClient:
|
||||
)
|
||||
|
||||
|
||||
class FakeBinaryStdout:
|
||||
"""Capture protocol writes through a binary stdout buffer."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.buffer = io.BytesIO()
|
||||
|
||||
def flush(self) -> None:
|
||||
return None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_backend_host_processes_command(tmp_path, monkeypatch):
|
||||
monkeypatch.chdir(tmp_path)
|
||||
@@ -131,3 +145,19 @@ async def test_backend_host_command_does_not_reset_cli_overrides(tmp_path, monke
|
||||
assert host._bundle.app_state.get().provider == "openai-compatible"
|
||||
finally:
|
||||
await close_runtime(host._bundle)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_backend_host_emits_utf8_protocol_bytes(monkeypatch):
|
||||
host = ReactBackendHost(BackendHostConfig())
|
||||
fake_stdout = FakeBinaryStdout()
|
||||
monkeypatch.setattr("openharness.ui.backend_host.sys.stdout", fake_stdout)
|
||||
|
||||
await host._emit(BackendEvent(type="assistant_delta", message="你好😊"))
|
||||
|
||||
raw = fake_stdout.buffer.getvalue()
|
||||
assert raw.startswith(b"OHJSON:")
|
||||
decoded = raw.decode("utf-8").strip()
|
||||
payload = json.loads(decoded.removeprefix("OHJSON:"))
|
||||
assert payload["type"] == "assistant_delta"
|
||||
assert payload["message"] == "你好😊"
|
||||
|
||||
Reference in New Issue
Block a user