From 62bc257be6dc635227749620cf200ea2cef92b2e Mon Sep 17 00:00:00 2001 From: Variet Agent Date: Wed, 18 Mar 2026 18:15:57 +0900 Subject: [PATCH] =?UTF-8?q?fix(bot):=20JSON=20=ED=8C=8C=EC=84=9C=20?= =?UTF-8?q?=EC=A4=91=EC=B2=A9=20=EA=B0=9D=EC=B2=B4=20=EB=A7=A4=EC=B9=AD=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20(=EC=A4=91=EA=B4=84=ED=98=B8=20=EA=B7=A0?= =?UTF-8?q?=ED=98=95=20=EB=B0=A9=EC=8B=9D)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/discord_bot.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/api/discord_bot.py b/api/discord_bot.py index d1502e6..670b882 100644 --- a/api/discord_bot.py +++ b/api/discord_bot.py @@ -167,13 +167,20 @@ def _parse_unified_response(raw: str) -> dict: except json.JSONDecodeError: pass - # 2) { ... } 직접 - m = _re.search(r"\{[\s\S]*\"mode\"[\s\S]*?\}", raw) - if m: - try: - return json.loads(m.group(0)) - except json.JSONDecodeError: - pass + # 2) 중괄호 균형 매칭으로 JSON 추출 + start = raw.find("{") + if start != -1: + depth = 0 + for i in range(start, len(raw)): + if raw[i] == "{": + depth += 1 + elif raw[i] == "}": + depth -= 1 + if depth == 0: + try: + return json.loads(raw[start:i + 1]) + except json.JSONDecodeError: + break # 3) 파싱 실패 → chat 모드 폴백 logger.warning(f"unified 응답 JSON 파싱 실패: {raw[:200]}")