fix: mine_resource 채굴 로직 개선 및 JSON 파싱 안정성 향상

- 광석 위치로 이동 후 실제 자원 존재 여부를 재확인하여 실패한 타일을 제외하는 로직 추가
- JSON 파싱 시 중괄호 및 대괄호 균형을 추적하고, 잘린 응답 복구 로직을 개선하여 안정성 향상
- README.md에 변경 사항 및 기능 설명 추가
This commit is contained in:
21in7
2026-03-25 21:37:15 +09:00
parent 9a2185ca4b
commit 7abdf8713a
5 changed files with 63 additions and 15 deletions

View File

@@ -238,7 +238,8 @@ class AIPlanner:
start = text.find("{")
if start == -1:
raise ValueError("JSON 파싱 실패 ('{' 없음):\n" + raw[:300])
depth = 0
brace_depth = 0
bracket_depth = 0
in_string = False
escape = False
end = start
@@ -256,23 +257,36 @@ class AIPlanner:
if in_string:
continue
if c == '{':
depth += 1
brace_depth += 1
elif c == '}':
depth -= 1
if depth == 0:
brace_depth -= 1
elif c == '[':
bracket_depth += 1
elif c == ']':
bracket_depth -= 1
if brace_depth == 0 and bracket_depth == 0:
# 최상위 JSON 객체가 종료된 지점으로 추정
if i > start:
end = i + 1
break
if depth != 0:
if brace_depth != 0 or bracket_depth != 0:
partial = text[start:]
partial = self._repair_truncated_json(partial)
try:
return json.loads(partial)
except json.JSONDecodeError:
raise ValueError(f"JSON 파싱 실패 (잘린 응답 복구 불가):\n{raw[:400]}")
candidate = text[start:end]
try:
return json.loads(text[start:end])
return json.loads(candidate)
except json.JSONDecodeError:
raise ValueError(f"JSON 파싱 실패:\n{raw[:400]}")
# 중괄호는 맞지만 배열/후행 속성이 잘려 파싱 실패하는 케이스 복구
repaired = self._repair_truncated_json(candidate)
try:
return json.loads(repaired)
except json.JSONDecodeError:
raise ValueError(f"JSON 파싱 실패:\n{raw[:400]}")
def _repair_truncated_json(self, text: str) -> str:
if '"actions"' not in text:
@@ -284,9 +298,13 @@ class AIPlanner:
result = text[:last_complete]
open_brackets = result.count('[') - result.count(']')
open_braces = result.count('{') - result.count('}')
result += ']' * open_brackets
result += ',"after_this":"계속 진행"'
result += '}' * open_braces
# JSON이 '...,' 로 끝나는 경우를 방지
if result.rstrip().endswith(","):
result = result.rstrip()[:-1]
result += ']' * max(0, open_brackets)
if '"after_this"' not in result and open_braces > 0:
result += ',"after_this":"계속 진행"'
result += '}' * max(0, open_braces)
return result
return '{"thinking":"응답 잘림","current_goal":"탐색","actions":[],"after_this":"재시도"}'