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]}")