Fix OpenCode server HTTP response parsing for parts array

This commit is contained in:
Adolfo Reyna
2026-08-07 19:43:03 -04:00
parent a8ad741a4d
commit 0aa790a8b0
+13 -8
View File
@@ -240,12 +240,16 @@ class OpenCodeLLM(FrameProcessor):
}
async with session.post(msg_url, json=payload) as resp:
async for line in resp.content:
line_str = line.decode("utf-8").strip()
if not line_str:
continue
try:
data = json.loads(line_str)
if resp.status == 200:
data = await resp.json()
parts = data.get("parts", []) if isinstance(data, dict) else []
for p in parts:
if isinstance(p, dict) and p.get("type") == "text" and "text" in p:
text_chunk = _clean_spoken_text(p["text"])
if text_chunk:
chunks.append(text_chunk)
await self.push_frame(LLMTextFrame(text_chunk))
if not chunks and isinstance(data, dict):
if "delta" in data:
text_chunk = _clean_spoken_text(data["delta"])
if text_chunk:
@@ -256,8 +260,9 @@ class OpenCodeLLM(FrameProcessor):
if text_chunk:
chunks.append(text_chunk)
await self.push_frame(LLMTextFrame(text_chunk))
except json.JSONDecodeError:
pass
else:
err_text = await resp.text()
logger.error(f"OpenCode server HTTP {resp.status}: {err_text}")
except asyncio.CancelledError:
logger.info("OpenCode server turn cancelled mid-response.")