Skip to content

Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)

Python

added more tests for parser and made it more robust

Alishahryar16887de9cab0d
diff --git a/messaging/event_parser.py b/messaging/event_parser.py
index 6e748ec7..3198d720 100644
--- a/messaging/event_parser.py
+++ b/messaging/event_parser.py
@@ -26,6 +26,11 @@ def parse_cli_event(event: Any) -> List[Dict]:
etype = event.get("type")
results: List[Dict[str, Any]] = []
+ # Some CLI/proxy layers emit "system" events that are not user-visible and
+ # carry no transcript content. Ignore them explicitly to avoid noisy logs.
+ if etype == "system":
+ return []
+
# 1. Handle full messages (assistant/user or result)
msg_obj = None
if etype == "assistant":
@@ -36,8 +41,14 @@ def parse_cli_event(event: Any) -> List[Dict]:
res = event.get("result")
if isinstance(res, dict):
msg_obj = res.get("message")
+ # Some variants put content directly on the result.
+ if not msg_obj and isinstance(res.get("content"), list):
+ msg_obj = {"content": res.get("content")}
if not msg_obj:
msg_obj = event.get("message")
+ # Some variants put content directly on the event.
+ if not msg_obj and isinstance(event.get("content"), list):
+ msg_obj = {"content": event.get("content")}
if msg_obj and isinstance(msg_obj, dict):
content = msg_obj.get("content", [])
diff --git a/messaging/transcript.py b/messaging/transcript.py
index 4f70c690..fda13009 100644
--- a/messaging/transcript.py
+++ b/messaging/transcript.py
@@ -222,6 +222,10 @@ class TranscriptBuffer:
if et == "thinking_start":
idx = int(ev.get("index", -1))
+ if idx >= 0:
+ # Defensive: if a provider reuses indices without emitting a stop,
+ # close the previous open segment first.
+ self.apply({"type": "block_stop", "index": idx})
seg = self._ensure_thinking()
if idx >= 0:
self._open_thinking_by_index[idx] = seg
@@ -243,6 +247,8 @@ class TranscriptBuffer:
if et == "text_start":
idx = int(ev.get("index", -1))
+ if idx >= 0:
+ self.apply({"type": "block_stop", "index": idx})
seg = self._ensure_text()
if idx >= 0:
self._open_text_by_index[idx] = seg
@@ -264,6 +270,8 @@ class TranscriptBuffer:
if et == "tool_use_start":
idx = int(ev.get("index", -1))
+ if idx >= 0:
+ self.apply({"type": "block_stop", "index": idx})
tool_id = str(ev.get("id", "") or "")
name = str(ev.get("name", "") or "tool")
indent = 0
diff --git a/tests/test_event_parser.py b/tests/test_event_parser.py
index aa7d76d2..b6545876 100644
--- a/tests/test_event_parser.py
+++ b/tests/test_event_parser.py
@@ -144,3 +144,17 @@ def test_parse_cli_event_invalid_input():
assert parse_cli_event(None) == []
assert parse_cli_event("not a dict") == []
assert parse_cli_event({"type": "unknown"}) == []
+
+
+def test_parse_cli_event_system_ignored():
+ assert parse_cli_event({"type": "system", "foo": "bar"}) == []
+
+
+def test_parse_cli_event_result_with_content_directly():
+ event = {"type": "result", "content": [{"type": "text", "text": "hi"}]}
+ assert parse_cli_event(event) == [{"type": "text_chunk", "text": "hi"}]
+
+
+def test_parse_cli_event_result_with_result_content_directly():
+ event = {"type": "result", "result": {"content": [{"type": "text", "text": "hi"}]}}
+ assert parse_cli_event(event) == [{"type": "text_chunk", "text": "hi"}]
diff --git a/tests/test_transcript.py b/tests/test_transcript.py
index 9af3bd10..1fdc6fbc 100644
--- a/tests/test_transcript.py
+++ b/tests/test_transcript.py
@@ -85,3 +85,17 @@ def test_transcript_truncates_by_dropping_oldest_segments():
# We keep the tail and drop the oldest segments when truncating.
assert escape_md_v2("segment_59") in out
assert escape_md_v2("segment_0") not in out
+
+
+def test_transcript_reused_index_closes_previous_open_block():
+ t = TranscriptBuffer()
+ # Open a text block at index 0, but never close it.
+ t.apply({"type": "text_start", "index": 0})
+ t.apply({"type": "text_delta", "index": 0, "text": "a"})
+ # Provider reuses index 0 for a new tool block without a stop.
+ t.apply(
+ {"type": "tool_use_start", "index": 0, "id": "t1", "name": "ls", "input": {}}
+ )
+ # Old open text should have been closed.
+ assert 0 not in t._open_text_by_index
+ assert 0 in t._open_tools_by_index