feat(sprint5): state machine + bouncing — thread_state и служебные теги
Таблица thread_state (intent, step, slots) ведётся per-thread. В системный
промпт ветки дописывается текущее состояние, LLM возвращает служебный тег
[STATE: step=N; slots={...}] после основного ответа — парсер в chat_service
вырезает его и обновляет состояние. Если ветка решила, что тема ушла в другую,
она выдаёт [INTENT_CHANGE: code] — делаем один повторный вызов LLM с новой
веткой и сброшенным state (bouncing, MAX_BOUNCES=1). Если роутер сам выбрал
другую ветку, чем в thread_state, — state тоже сбрасывается. Промпт new_booking
переписан под 6-шаговый сценарий (имя → повод → специалист → время → подтверждение
→ запись), в «Песочнице» появился блок «Состояние треда» с intent/step/slots
и списком переходов.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
+4
-1
@@ -5,7 +5,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from db.session import get_session
|
||||
from models.requests import ChatRequest
|
||||
from models.responses import ChatResponse, SourceInfo
|
||||
from models.responses import BounceInfo, ChatResponse, SourceInfo, ThreadStateInfo
|
||||
from services import chat_service
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -44,10 +44,13 @@ async def chat(req: ChatRequest, session: AsyncSession = Depends(get_session)):
|
||||
message_id=result["message_id"],
|
||||
intent_code=result["intent_code"],
|
||||
intent_name=result["intent_name"],
|
||||
router_intent_code=result.get("router_intent_code", ""),
|
||||
config_version=result["config_version"],
|
||||
router_version=result.get("router_version"),
|
||||
answer=result["answer"],
|
||||
sources=[SourceInfo(**s) for s in result["sources"]],
|
||||
model_used=result["model_used"],
|
||||
assembled_prompt=result["assembled_prompt"],
|
||||
thread_state=ThreadStateInfo(**result["thread_state"]),
|
||||
bounces=[BounceInfo(**b) for b in result.get("bounces", [])],
|
||||
)
|
||||
|
||||
@@ -12,6 +12,7 @@ from models.responses import (
|
||||
ThreadDetailResponse,
|
||||
ThreadInfo,
|
||||
ThreadListResponse,
|
||||
ThreadStateInfo,
|
||||
)
|
||||
from services import chat_service
|
||||
|
||||
@@ -34,6 +35,7 @@ async def get_thread(thread_id: int, session: AsyncSession = Depends(get_session
|
||||
data = await chat_service.get_thread_detail(session, thread_id)
|
||||
if data is None:
|
||||
raise HTTPException(status_code=404, detail="Thread not found")
|
||||
state = data.get("thread_state") or {}
|
||||
return ThreadDetailResponse(
|
||||
id=data["id"],
|
||||
name=data["name"],
|
||||
@@ -47,9 +49,16 @@ async def get_thread(thread_id: int, session: AsyncSession = Depends(get_session
|
||||
created_at=m["created_at"],
|
||||
sources=[SourceInfo(**s) for s in m["sources"]],
|
||||
assembled_prompt=m["assembled_prompt"],
|
||||
intent_code=m.get("intent_code", ""),
|
||||
intent_name=m.get("intent_name", ""),
|
||||
)
|
||||
for m in data["messages"]
|
||||
],
|
||||
thread_state=ThreadStateInfo(
|
||||
current_intent_code=state.get("current_intent_code"),
|
||||
current_step=state.get("current_step", 0),
|
||||
slots=state.get("slots", {}),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user