"""Канал преподавателя: только передача. **Ни одной команды, меняющей карточку курсанта.** Преподаватель управляет ситуацией, а не работой обучаемого, иначе оценка перестаёт быть оценкой курсанта (docs/arch/CONTRACT.md). Ответы сюда не идут — канал односторонний. Всё, что сервер хочет сказать преподавателю, уходит на его же сокет `observe`. """ import logging import re from uuid import UUID, uuid4 from fastapi import APIRouter, WebSocket, WebSocketDisconnect from pydantic import TypeAdapter, ValidationError from app.domain.events import ( CallEndReason, CallEnded, ErrorEvent, ErrorKind, ScoreReady, CallIncoming, CardBriefing, Exercise, StationState, InstructorToServer, InstructorNoteShown, ModeSet, ReferenceStarted, SessionEnded, ) import asyncio from app.dialog.factory import build_caller from app.dialog.director import apply as apply_directive from app.dialog.director import mood_of from app.dialog.persona import PersonaState from app.dialog.runtime import get_embedder from app.dialog.slots import SlotMachine from app.scenarios import store from app.api.auth import audit, principal_of from app.domain.roles import Role from app.session.hub import hub from app.session.state import SessionState, now_utc from app.domain.kio import KIO, ResponseStatus from app.voice.models import get_voice_models from app.voice.pipeline import FILLERS, prefetch log = logging.getLogger(__name__) router = APIRouter() _adapter = TypeAdapter(InstructorToServer) def card_briefing(state: SessionState) -> CardBriefing: """Учебная текстовая вводная — исходные реплики, а не эталон карточки. В отсутствие диалога факты раскрываются сразу. Если факт уточняется, показываем и уточнение: иначе правильно заполнить карточку невозможно. """ scenario = state.scenario lines = ["Учебная текстовая вводная: сведения заявителя приведены ниже.", scenario.first_line] for fact in scenario.facts: lines.append(f"• {fact.value}") if fact.refined: lines.append(f" Уточнено: {fact.refined}") return CardBriefing( scenario_id=scenario.id, mode=state.mode, text="\n".join(lines), required_fields=([field for field in state.required_fields if field != "dds"] if scenario.ground_truth.incident_code else list(state.required_fields)), card=state.kio, ) async def _start(session_id: UUID, event, who=None) -> None: scenario = store.get(event.scenario_id) if scenario is None: hub.to_observers( session_id, ErrorEvent(code=ErrorKind.SCENARIO_INVALID, message=f"Нет сценария {event.scenario_id}"), ) return attempt = 1 recorded_trainee_id = event.trainee_id if hub.journal: attempt, recorded_trainee_id = await hub.journal.start_lesson( session_id, scenario.id, event.mode.value, event.trainee, event.trainee_id ) # Занятие собирается целиком и только потом регистрируется: иначе # наблюдатель, подключившийся в эту щель, увидит полусобранное состояние # без слот-автомата и звонящего. state = SessionState( session_id=session_id, scenario_id=scenario.id, scenario_title=scenario.title, level=scenario.level.value, mode=event.mode, exercise=event.exercise, scenario=scenario.model_copy(deep=True), required_fields=scenario.required_fields, trainee_name=event.trainee, trainee_id=recorded_trainee_id, attempt=attempt, ) if event.exercise is Exercise.CALL: embedder = get_embedder() if embedder is not None: state.slots = SlotMachine(state.scenario, embedder) state.persona = PersonaState(state.scenario.persona) state.caller = build_caller(scenario.id) elif event.exercise is Exercise.DDS: # В ДДС поступает уже оформленная учебная карточка. Содержимое берётся # из утверждённого сценария, а не из действий несуществующего оператора. truth = scenario.ground_truth address_fact = next((fact.value for fact in scenario.facts if "address" in fact.id), "") floor = re.search(r"(\d+)[-‑–]?й?\s*этаж", address_fact, re.IGNORECASE) service = truth.dds.value if truth.dds else None fallback = {"01": "Служба 101", "02": "МВД", "03": "Скорая помощь", "04": "Аварийная служба"} state.kio = KIO( registered_at=now_utc(), caller_number="+7 (495) 000-00-00", address=truth.address or address_fact or None, floor=floor.group(1) if floor else None, incident_type=truth.incident_type, incident_code=truth.incident_code, dds=truth.dds, signs=list(scenario.signs), notify=list(truth.notify) or ([fallback[service]] if service in fallback else []), victims_count=truth.victims, description="; ".join(fact.value for fact in scenario.facts[:3]) or scenario.first_line, ) if service: state.dispatch(service) else: state.kio.response_status = ResponseStatus.TRANSFERRED state.dispatched_card = state.kio.model_copy(deep=True) state.dispatched_at = now_utc() state.started_at = state.dispatched_at else: state.started_at = now_utc() hub.register(state) if event.exercise is not Exercise.CALL and hub.journal and state.started_at is not None: await hub.journal.session_started(session_id, state.started_at) # Первая реплика и филлеры синтезируются, пока курсант не снял трубку: # «Алло! Помогите!» должно прозвучать мгновенно (docs/arch/BACKEND.md). if event.exercise is Exercise.CALL: models = get_voice_models() if models is not None: asyncio.create_task(prefetch(models, [scenario.first_line, *FILLERS.values()])) state.on_event("call.incoming") elif event.exercise is Exercise.DDS: state.on_event("dds.dispatch") hub.start_ticker(session_id) if who is not None: # Запуск занятия меняет чужой результат — значит попадает в журнал # аудита (ТЗ, хранение не менее шести месяцев). asyncio.create_task( audit(who.login, who.role.value, "lesson.start", str(session_id), f"{scenario.id}, режим {event.mode.value}, курсант {event.trainee}") ) if event.exercise is Exercise.CALL: hub.to_trainee( session_id, CallIncoming( scenario_id=scenario.id, caller_number="+7 (495) 000-00-00", level=scenario.level, mode=event.mode, required_fields=[field for field in scenario.required_fields if field != "dds" or not scenario.ground_truth.incident_code], ), ) elif event.exercise is Exercise.DDS: hub.to_station(session_id, state.card_received_event()) hub.to_station(session_id, StationState(snapshot=state.station_snapshot())) else: hub.to_trainee(session_id, card_briefing(state)) hub.to_observers(session_id, ModeSet(mode=event.mode)) hub.to_observers(session_id, state.snapshot()) async def _stop(session_id: UUID) -> None: state = hub.get(session_id) if state is None or state.ended: return state.ended_at = now_utc() state.end_reason = CallEndReason.INSTRUCTOR hub.stop_ticker(session_id) hub.to_observers(session_id, SessionEnded(reason=CallEndReason.INSTRUCTOR)) if state.exercise in {Exercise.DDS, Exercise.CARD}: from app.session.finish import finish if state.exercise is Exercise.DDS: hub.to_station(session_id, SessionEnded(reason=CallEndReason.INSTRUCTOR)) else: hub.to_trainee(session_id, CallEnded(reason=CallEndReason.INSTRUCTOR)) await finish(session_id, state) if state.exercise is Exercise.DDS: hub.to_station(session_id, ScoreReady(session_id=session_id)) if hub.journal: await hub.journal.session_ended(session_id, state.ended_at, CallEndReason.INSTRUCTOR.value) async def _reject(ws: WebSocket, message: str) -> None: """Отказ до входа в цикл: сокет закрывается с объяснением, а не молча.""" await ws.send_text( ErrorEvent(code=ErrorKind.FORBIDDEN, message=message).model_dump_json() ) await ws.close() @router.websocket("/ws/control/{session_id}") async def control(ws: WebSocket, session_id: UUID) -> None: await ws.accept() # Пульт преподавателя: управление занятием доступно только ему. who = principal_of(ws) if who is None or who.role not in (Role.INSTRUCTOR,): await _reject(ws, "Недостаточно прав для этого экрана") return try: while True: payload = await ws.receive_json() try: event = _adapter.validate_python(payload) except ValidationError: hub.to_observers( session_id, ErrorEvent(code=ErrorKind.UNSUPPORTED_EVENT, message=str(payload)[:200]), ) continue match event.type: case "scenario.start": await _start(session_id, event, who) case "session.stop": await _stop(session_id) case "instructor_note.add": state = hub.get(session_id) if state is not None: state.notes.append({ "type": "instructor_note.shown", "transcript_ref": event.transcript_ref, "text": event.text, "author": "преподаватель", }) hub.to_observers( session_id, InstructorNoteShown( transcript_ref=event.transcript_ref, text=event.text, author="преподаватель", ), ) if hub.journal: await hub.journal.note( session_id, event.transcript_ref, event.text, "преподаватель" ) case "reference.play": state = hub.get(session_id) if state is not None: hub.to_observers(session_id, ReferenceStarted(scenario_id=state.scenario_id)) case "score.override": state = hub.get(session_id) if state is not None and state.score is not None: # Автооценка остаётся рядом: видно, что скорректировано и кем. state.score = { **state.score, "score_final": float(event.verdict) if event.verdict.replace(".", "", 1).isdigit() else state.score["score_auto"], "overridden_by": "преподаватель", "override_comment": event.comment, } hub.to_observers(session_id, ScoreReady(session_id=session_id)) case "director.inject": state = hub.get(session_id) if state is None: continue if state.exercise is not Exercise.CALL: hub.to_observers(session_id, ErrorEvent( code=ErrorKind.UNSUPPORTED_EVENT, message="Директивы звонящему доступны только в голосовом упражнении", )) continue result = apply_directive(state, event.directive) if result.needs_network: hub.to_observers(session_id, ErrorEvent( code=ErrorKind.DIRECTIVE_NEEDS_NETWORK, message="Свободный текст требует LLM: офлайн доступны только кнопки", )) continue state.directives.append(event.directive) voice = state.voice if result.drop_line and voice is not None: # Обрыв рвёт звук на полуслове тем же механизмом, что # перебивание, и запускает норматив обратного дозвона. voice.barge_in() if result.drop_line: state.on_event("call.dropped") hub.to_trainee(session_id, CallEnded(reason=CallEndReason.DROPPED)) hub.to_observers(session_id, SessionEnded(reason=CallEndReason.DROPPED)) elif result.say and voice is not None: voice.speak(result.say, mood_of(state)) case _: hub.to_observers( session_id, ErrorEvent( code=ErrorKind.UNSUPPORTED_EVENT, message=f"{event.type} ещё не реализовано", ), ) except WebSocketDisconnect: return