fix(bot): chat 응답 JSON 파싱 strict=False + Embed 렌더링 통일
This commit is contained in:
@@ -163,7 +163,7 @@ def _parse_unified_response(raw: str) -> dict:
|
|||||||
m = _re.search(r"```json\s*\n(.+?)```", raw, _re.DOTALL)
|
m = _re.search(r"```json\s*\n(.+?)```", raw, _re.DOTALL)
|
||||||
if m:
|
if m:
|
||||||
try:
|
try:
|
||||||
return json.loads(m.group(1))
|
return json.loads(m.group(1), strict=False)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -178,7 +178,7 @@ def _parse_unified_response(raw: str) -> dict:
|
|||||||
depth -= 1
|
depth -= 1
|
||||||
if depth == 0:
|
if depth == 0:
|
||||||
try:
|
try:
|
||||||
return json.loads(raw[start:i + 1])
|
return json.loads(raw[start:i + 1], strict=False)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
break
|
break
|
||||||
|
|
||||||
@@ -411,16 +411,27 @@ async def on_message(message: discord.Message):
|
|||||||
logger.info("[NC] handle 완료")
|
logger.info("[NC] handle 완료")
|
||||||
|
|
||||||
elif mode == "chat":
|
elif mode == "chat":
|
||||||
# 즉시 응답
|
# 즉시 응답 — 항상 Embed 사용 (Discord markdown 렌더링)
|
||||||
response = parsed.get("response", "")
|
response = parsed.get("response", "")
|
||||||
logger.info(f"[chat] 응답 길이: {len(response)}")
|
logger.info(f"[chat] 응답 길이: {len(response)}")
|
||||||
if response:
|
if response:
|
||||||
if len(response) <= 2000:
|
# 응답이 JSON 구조를 포함하면 제거 (fallback 시 raw 전체가 들어옴)
|
||||||
await message.reply(response)
|
if response.strip().startswith('"mode"') or response.strip().startswith('{"mode"'):
|
||||||
else:
|
import re as _re
|
||||||
|
m = _re.search(r'"response"\s*:\s*"(.*)', response, _re.DOTALL)
|
||||||
|
if m:
|
||||||
|
cleaned = m.group(1)
|
||||||
|
# 끝의 따옴표+중괄호 제거
|
||||||
|
if cleaned.endswith('"}'):
|
||||||
|
cleaned = cleaned[:-2]
|
||||||
|
elif cleaned.endswith('"'):
|
||||||
|
cleaned = cleaned[:-1]
|
||||||
|
response = cleaned.replace('\\n', '\n')
|
||||||
|
from handlers.renderer import safe_send_embed
|
||||||
for i in range(0, len(response), 4000):
|
for i in range(0, len(response), 4000):
|
||||||
embed = discord.Embed(description=response[i:i + 4000], color=0x3498DB)
|
chunk = response[i:i + 4000]
|
||||||
await message.channel.send(embed=embed)
|
embed = discord.Embed(description=chunk, color=0x3498DB)
|
||||||
|
await safe_send_embed(message.channel, embed)
|
||||||
else:
|
else:
|
||||||
await message.reply("응답을 생성하지 못했습니다.")
|
await message.reply("응답을 생성하지 못했습니다.")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user