fix(bot): JSON 파서 중첩 객체 매칭 수정 (중괄호 균형 방식)

This commit is contained in:
2026-03-18 18:15:57 +09:00
parent 56787b1e4f
commit 62bc257be6

View File

@@ -167,13 +167,20 @@ def _parse_unified_response(raw: str) -> dict:
except json.JSONDecodeError: except json.JSONDecodeError:
pass pass
# 2) { ... } 직접 # 2) 중괄호 균형 매칭으로 JSON 추출
m = _re.search(r"\{[\s\S]*\"mode\"[\s\S]*?\}", raw) start = raw.find("{")
if m: 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: try:
return json.loads(m.group(0)) return json.loads(raw[start:i + 1])
except json.JSONDecodeError: except json.JSONDecodeError:
pass break
# 3) 파싱 실패 → chat 모드 폴백 # 3) 파싱 실패 → chat 모드 폴백
logger.warning(f"unified 응답 JSON 파싱 실패: {raw[:200]}") logger.warning(f"unified 응답 JSON 파싱 실패: {raw[:200]}")