[fix] solution/backend: 카카오 콜백 대기 문구를 늦어질 때만 — 3.5초 안에 끝나면 바로 답
콜백이 켜져 있으면 무조건 "확인하고 있어요" 부터 나가서, 1초면 끝나는 답에도 대기 문구가 먼저 붙었다 — 사장님에게는 두 번 말하는 봇이다(2026-10-02 체감 지적). - kakao_bot.py: FAST_REPLY_SEC(3.5초) 안에 끝나면 일반 응답, 넘으면 useCallback 즉답 후 같은 작업을 콜백으로 이어 보냄. shield 로 취소하지 않는다 — 다시 돌리면 도구가 두 번 실행될 수 있다 - docs/AGENT.md: 콜백 흐름 갱신, 콜백 블록의 일반 응답은 개발 채널 실측 전이라는 주의 test_kakao_webhook 44 passed. 전체 1045 passed · 48 failed(gemini·search_console·weather_notes — 변경 전에도 같은 48건 실패, 이번 변경과 무관) Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
c7749662fd
commit
42803de966
3 files changed
+89
-23
No files matched your search
@@ -12,8 +12,10 @@
|
||||
★ 5초 벽: 오픈빌더의 스킬 타임아웃은 **5초**다. 넘기면 카카오가 끊어 사장님에게는
|
||||
**말없이 실패하는 봇**이 된다.
|
||||
→ 오픈빌더 스킬 설정에서 **콜백 사용**을 켜면 요청에 `userRequest.callbackUrl` 이 실려 온다.
|
||||
그때는 `{"useCallback": true}` 로 **즉답**하고, 답을 다 만든 뒤 그 주소로 따로 보낸다.
|
||||
콜백 주소는 **1분 · 1회**만 유효하다.
|
||||
그때도 `FAST_REPLY_SEC` 안에 끝나면 **답을 바로** 준다. 넘을 때만 `{"useCallback": true}` 로
|
||||
대기 문구를 즉답하고, 답을 다 만든 뒤 그 주소로 따로 보낸다. 콜백 주소는 **1분 · 1회**만 유효하다.
|
||||
예전엔 콜백이 켜져 있으면 무조건 대기 문구부터 나가서, 1초면 끝나는 답에도 "확인하고 있어요"
|
||||
가 먼저 붙었다(2026-10-02, 사장님 체감 지적).
|
||||
→ 콜백이 꺼져 있으면 예전처럼 동기로 답하되 `DEADLINE_SEC` 로 끊는다. 실측(2026-09-22):
|
||||
필드 43개 + fact 수십 개가 실린 실제 프롬프트는 4초를 넘겼다 — 개발 중 재본
|
||||
1.3~2.4초는 항목 두 개짜리 장난감 프롬프트였다.
|
||||
@@ -36,6 +38,8 @@ router = APIRouter(prefix="/v1/agent/kakao", tags=["Agent"])
|
||||
DEADLINE_SEC = 4.5
|
||||
# 콜백이 켜져 있을 때의 상한. 콜백 주소가 1분간 유효하므로 그 안에서 넉넉히 잡는다.
|
||||
CALLBACK_DEADLINE_SEC = 45.0
|
||||
# 콜백이 켜져 있어도 이 안에 끝나면 대기 문구 없이 바로 답한다. 5초 벽에서 왕복 몫을 남긴다.
|
||||
FAST_REPLY_SEC = 3.5
|
||||
|
||||
_TIMEOUT_TEXT = "확인하는 데 시간이 조금 걸리네요. 잠시 뒤 다시 말씀해 주세요."
|
||||
_ERROR_TEXT = "지금은 처리할 수 없어요. 잠시 뒤 다시 말씀해 주세요."
|
||||
@@ -102,10 +106,13 @@ def _authorize(secret_in_path: str | None, header_secret: str | None, body: dict
|
||||
|
||||
|
||||
async def _answer(utterance: str, speaker: str, deadline: float, app_user_id: str = '') -> dict:
|
||||
return await _settle(asyncio.ensure_future(channel.handle(utterance, speaker, app_user_id)), deadline)
|
||||
|
||||
|
||||
async def _settle(task: asyncio.Future, deadline: float) -> dict:
|
||||
"""대화 한 턴을 SkillResponse 로. 어떤 실패도 문구로 바꾼다."""
|
||||
try:
|
||||
answer = await asyncio.wait_for(
|
||||
channel.handle(utterance, speaker, app_user_id), timeout=deadline)
|
||||
answer = await asyncio.wait_for(task, timeout=deadline)
|
||||
except asyncio.TimeoutError:
|
||||
LOG.w("[agent/kakao] 응답 시간 초과 — 안내로 끊음")
|
||||
return _reply(_TIMEOUT_TEXT)
|
||||
@@ -159,12 +166,12 @@ async def _approve_click(extra: dict, speaker: str) -> dict:
|
||||
return _reply_from(answer)
|
||||
|
||||
|
||||
async def _push(callback_url: str, utterance: str, speaker: str, app_user_id: str = '') -> None:
|
||||
"""답을 다 만든 뒤 콜백 주소로 보낸다.
|
||||
async def _push(callback_url: str, task: asyncio.Future, deadline: float) -> None:
|
||||
"""이미 돌고 있는 대화 한 턴이 끝나기를 기다렸다가 콜백 주소로 보낸다.
|
||||
|
||||
★ 주소는 1분 · 1회만 유효하다. 실패해도 재시도하지 않는다 — 두 번째 POST 는 어차피
|
||||
거절되고, 사장님에게는 이미 "확인하고 있어요" 가 가 있다."""
|
||||
payload = await _answer(utterance, speaker, CALLBACK_DEADLINE_SEC, app_user_id)
|
||||
payload = await _settle(task, deadline)
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=10.0) as client:
|
||||
res = await client.post(callback_url, json=payload)
|
||||
@@ -212,8 +219,18 @@ async def _handle(body: dict, tasks: BackgroundTasks) -> dict:
|
||||
LOG.i(f"[agent/kakao] 요청 — callbackUrl={'있음' if callback_url else '없음'} "
|
||||
f"block={(request.get('block') or {}).get('name')!r}")
|
||||
if callback_url:
|
||||
tasks.add_task(_push, callback_url, utterance, speaker, app_user_id)
|
||||
return {"version": "2.0", "useCallback": True, "data": {"text": _WAIT_TEXT}}
|
||||
# ★ 기다리는 동안 작업을 취소하지 않는다(shield) — 늦어지면 같은 작업을 콜백으로 이어 보낸다.
|
||||
# 취소하고 다시 돌리면 도구가 두 번 실행될 수 있다.
|
||||
task = asyncio.ensure_future(channel.handle(utterance, speaker, app_user_id))
|
||||
try:
|
||||
answer = await asyncio.wait_for(asyncio.shield(task), timeout=FAST_REPLY_SEC)
|
||||
except asyncio.TimeoutError:
|
||||
tasks.add_task(_push, callback_url, task, CALLBACK_DEADLINE_SEC - FAST_REPLY_SEC)
|
||||
return {"version": "2.0", "useCallback": True, "data": {"text": _WAIT_TEXT}}
|
||||
except Exception as ex: # noqa: BLE001 — 메신저에서는 500 도 침묵으로 보인다
|
||||
LOG.w(f"[agent/kakao] 처리 실패: {type(ex).__name__}: {ex}")
|
||||
return _reply(_ERROR_TEXT)
|
||||
return _reply_from(answer)
|
||||
|
||||
return await _answer(utterance, speaker, DEADLINE_SEC, app_user_id)
|
||||
|
||||
|
||||
Reference in new issue
Block a user