feat(sprint6b): блок E — причина передачи оператору + саммари

- Роутер возвращает escalate_human|reason (acute_pain/surgery/angry/explicit_request/routing_loop)
- RouterClient парсит reason; дефолт explicit_request при неразобранном
- _format_state_context получает escalation_reason → подставляется в промпт escalate_human
- Промпт escalate_human переписан: разное поведение по reason
- _build_operator_summary: reason + 8 реплик истории + слоты, логируется при передаче
- Message.escalation_reason (String 50, nullable) + миграция h4b52e9dc0f83
- ChatResponse и MessageInfo получили escalation_reason и operator_summary
- Sandbox: красный блок «передача оператору · причина» в состоянии треда
- Sandbox: блок саммари для оператора (предпросмотр) в панели отладки

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
AR 15 M4
2026-04-26 20:45:09 +05:00
parent d7ded5c9f1
commit 231e1f2d01
10 changed files with 189 additions and 24 deletions
+44 -9
View File
@@ -573,6 +573,7 @@
<h3>Решение роутера</h3>
<div id="debug-router"><div class="mini">— пока пусто —</div></div>
</div>
<div class="debug-section" id="debug-operator-summary" style="display:none;background:#fff1f2;border-radius:6px;padding:10px 14px;font-size:12px;"></div>
<details class="debug-section collapsible" id="debug-chunks-section">
<summary>
<span>Найденные фрагменты</span>
@@ -688,9 +689,10 @@ async function openThread(id) {
$("chat-title").textContent = d.name;
renderMessages(d.messages);
const lastAssistant = [...d.messages].reverse().find(m => m.role === "assistant");
const lastEscalation = [...d.messages].reverse().find(m => m.role === "assistant" && m.escalation_reason);
if (lastAssistant) {
renderDebug(lastAssistant.sources, lastAssistant.assembled_prompt, lastAssistant.intent_code, lastAssistant.intent_name, null, null, null, [], d.thread_state && d.thread_state.current_step_code);
renderState(d.thread_state, [], [], null, false, false);
renderDebug(lastAssistant.sources, lastAssistant.assembled_prompt, lastAssistant.intent_code, lastAssistant.intent_name, null, null, null, [], d.thread_state && d.thread_state.current_step_code, null);
renderState(d.thread_state, [], [], null, false, false, lastEscalation ? lastEscalation.escalation_reason : null);
} else {
clearDebug();
}
@@ -793,7 +795,7 @@ function appendMessage(role, text, iso, intentCode, intentName, meta) {
}
/* ---------- отладка ---------- */
function renderState(state, bounces, validationEvents, parseError, routingLoopTriggered, resumedFromSuspended) {
function renderState(state, bounces, validationEvents, parseError, routingLoopTriggered, resumedFromSuspended, escalationReason) {
const box = $("debug-state");
if (!state || !state.current_intent_code) {
box.innerHTML = '<div class="mini">сценарий ещё не запущен</div>';
@@ -818,9 +820,23 @@ function renderState(state, bounces, validationEvents, parseError, routingLoopTr
<span style="opacity:.75;">${esc(pendingGuard.description || "")}</span>
</div>`
: "";
const REASON_LABELS = {
acute_pain: "острая боль / срочное состояние",
surgery: "операция / хирургия / стационар",
angry: "пациент раздражён",
explicit_request: "запросил оператора",
routing_loop: "автоматически (петля роутера)",
};
const loopHtml = routingLoopTriggered
? `<div style="margin-top:8px;padding:6px 8px;border-radius:4px;background:#fee2e2;color:#7f1d1d;font-size:11px;">
🛑 защита от петли сработала: диалог уведён в <code>escalate_human</code>.
🛑 защита от петли сработала: диалог уведён к оператору.
</div>`
: "";
const effectiveReason = escalationReason || (state.current_intent_code === "escalate_human" ? "explicit_request" : null);
const escalationHtml = effectiveReason
? `<div style="margin-top:8px;padding:6px 8px;border-radius:4px;background:#fee2e2;color:#7f1d1d;font-size:11px;">
🔴 <b>передача оператору</b> · причина: <code>${esc(effectiveReason)}</code>
<span style="opacity:.75;"> — ${esc(REASON_LABELS[effectiveReason] || effectiveReason)}</span>
</div>`
: "";
const suspendedSlotsCount = Object.keys(state.resumable_slots || {}).length;
@@ -866,7 +882,7 @@ function renderState(state, bounces, validationEvents, parseError, routingLoopTr
<b>${esc(state.current_intent_code)}</b>
<span style="color:var(--muted);font-size:11px;margin-left:4px;">— без пошагового сценария</span>
</div>
${handoffHtml}${pendingGuardHtml}${softNudgeHtml}${loopHtml}${suspendedHtml}${resumedHtml}${bounceHtml}${validationHtml}${parseErrorHtml}
${handoffHtml}${escalationHtml}${pendingGuardHtml}${softNudgeHtml}${loopHtml}${suspendedHtml}${resumedHtml}${bounceHtml}${validationHtml}${parseErrorHtml}
</div>
`;
return;
@@ -877,12 +893,12 @@ function renderState(state, bounces, validationEvents, parseError, routingLoopTr
<div style="font-size:12px;">
<div><b>${esc(state.current_intent_code)}</b> · шаг <code>${esc(state.current_step_code)}</code></div>
<div class="prompt-box" style="margin-top:6px;max-height:200px;">${esc(slotsJson)}</div>
${handoffHtml}${pendingGuardHtml}${softNudgeHtml}${loopHtml}${suspendedHtml}${resumedHtml}${bounceHtml}${validationHtml}${parseErrorHtml}
${handoffHtml}${escalationHtml}${pendingGuardHtml}${softNudgeHtml}${loopHtml}${suspendedHtml}${resumedHtml}${bounceHtml}${validationHtml}${parseErrorHtml}
</div>
`;
}
function renderDebug(sources, prompt, intentCode, intentName, configVersion, routerVersion, routerIntentCode, bounces, stepCode) {
function renderDebug(sources, prompt, intentCode, intentName, configVersion, routerVersion, routerIntentCode, bounces, stepCode, operatorSummary) {
const routerVer = routerVersion != null ? `роутер v${routerVersion}` : "роутер";
const hasBounces = bounces && bounces.length > 0;
const routerDiffers = routerIntentCode && intentCode && routerIntentCode !== intentCode;
@@ -941,6 +957,24 @@ function renderDebug(sources, prompt, intentCode, intentName, configVersion, rou
$("debug-prompt").innerHTML = prompt
? `<div class="prompt-box">${esc(prompt)}</div>`
: '<div class="mini">промпт пуст</div>';
const summaryBox = $("debug-operator-summary");
if (summaryBox) {
if (operatorSummary) {
summaryBox.style.display = "";
summaryBox.innerHTML = `
<div style="font-size:11px;color:var(--muted);margin-bottom:4px;">саммари для оператора (предпросмотр)</div>
<div style="margin-bottom:4px;"><b>причина:</b> <code>${esc(operatorSummary.reason || "")}</code></div>
<div style="margin-bottom:4px;"><b>слоты:</b> <code>${esc(JSON.stringify(operatorSummary.slots || {}))}</code></div>
<div><b>история:</b>
${(operatorSummary.history_tail || []).map(h =>
`<div style="margin-top:3px;"><span style="color:var(--muted);">${esc(h.role === "user" ? "пациент" : "ассистент")}:</span> ${esc(h.text)}</div>`
).join("")}
</div>`;
} else {
summaryBox.style.display = "none";
}
}
}
function clearDebug() {
@@ -948,6 +982,7 @@ function clearDebug() {
$("debug-router").innerHTML = '<div class="mini">— пока пусто —</div>';
$("debug-chunks").innerHTML = '<div class="mini">— пока пусто —</div>';
$("debug-prompt").innerHTML = '<div class="mini">— пока пусто —</div>';
const s = $("debug-operator-summary"); if (s) s.style.display = "none";
}
/* ---------- send message ---------- */
@@ -987,8 +1022,8 @@ async function sendMessage() {
appendMessage("assistant", r.answer, null, r.intent_code, r.intent_name, r.message_meta);
$("chat-title").className = "chat-title";
$("chat-title").textContent = r.thread_name;
renderDebug(r.sources, r.assembled_prompt, r.intent_code, r.intent_name, r.config_version, r.router_version, r.router_intent_code, r.bounces, r.thread_state && r.thread_state.current_step_code);
renderState(r.thread_state, r.bounces, r.validation_events, r.parse_error, r.routing_loop_triggered, r.resumed_from_suspended);
renderDebug(r.sources, r.assembled_prompt, r.intent_code, r.intent_name, r.config_version, r.router_version, r.router_intent_code, r.bounces, r.thread_state && r.thread_state.current_step_code, r.operator_summary);
renderState(r.thread_state, r.bounces, r.validation_events, r.parse_error, r.routing_loop_triggered, r.resumed_from_suspended, r.escalation_reason);
refreshThreads();
} catch (e) {
// Откатываем визуально: убираем пузырь-заглушку ассистента и только что