ab-replay: SSE 이벤트를 data.type으로 파싱

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
kimandClaude Sonnet 5.5 committed 2026-10-17 18:04:36 +09:00
1 parent 2385331622
commit f25dbde2fe
1 file changed
+16 -17
+16 -17
View File
@@ -58,24 +58,23 @@ def chat_once(token, model, sid, message, timeout=240):
tools, reply, tokens, err = [], "", "", None
try:
with urllib.request.urlopen(req, timeout=timeout) as r:
ev = None
for raw in r:
line = raw.decode("utf-8", "replace").rstrip("\n")
if line.startswith("event:"):
ev = line[6:].strip()
elif line.startswith("data:"):
try:
d = json.loads(line[5:].strip())
except Exception:
continue
if ev == "tool_result" and d.get("action") and not d.get("synthetic"):
tools.append(d["action"])
elif ev == "token":
tokens += d.get("text", "")
elif ev == "done":
reply = d.get("reply") or ""
elif ev == "error":
err = str(d.get("message") or d)[:150]
if not line.startswith("data:"):
continue
try:
d = json.loads(line[5:].strip())
except Exception:
continue
ev = d.get("type")
if ev == "tool_result" and d.get("action") and not d.get("synthetic"):
tools.append(d["action"])
elif ev == "token":
tokens += d.get("text", "")
elif ev == "done":
reply = d.get("reply") or ""
elif ev == "error":
err = str(d.get("message") or d)[:150]
except Exception as e:
err = f"{type(e).__name__}: {str(e)[:100]}"
return {"tools": tools, "reply": reply or tokens, "sec": round(time.time() - t0, 1), "error": err}
@@ -100,7 +99,7 @@ def main():
a = ap.parse_args()
models = [m for m in a.models.split(",") if m]
today = datetime.datetime.utcnow().strftime("%Y-%m-%d")
today = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d")
daily = os.path.join(USER_DIR, "workspace", "memory", today + ".md")
before = open(daily, "rb").read() if os.path.exists(daily) else None