from pathlib import Path
p=Path('/opt/struktur/social-media-agent/codex_image_qa.py')
s=p.read_text()
old='''def parse_qa_response(text: str) -> dict[str, Any]:
raw = _extract_json(text)
result: dict[str, Any] = {}'''
new='''def parse_qa_response(text: str) -> dict[str, Any]:
# Codex --json emits JSONL event envelopes; parse the actual agent_message first.
candidates = []
for line in text.splitlines():
try:
event = json.loads(line)
item = event.get("item", {}) if isinstance(event, dict) else {}
if isinstance(item, dict) and item.get("type") == "agent_message":
candidates.append(str(item.get("text", "")))
except json.JSONDecodeError:
continue
raw = None
for candidate in reversed(candidates):
try:
raw = _extract_json(candidate)
break
except CodexImageQAError:
pass
if raw is None:
raw = _extract_json(text)
result: dict[str, Any] = {}'''
assert s.count(old)==1
p.write_text(s.replace(old,new))
print('patched JSONL parser')