SSOT 대시보드:
- 한신대 기술분석 PDF(19쪽) 정합성 분석 + 이번 세션 발견 섹션 추가
- 섹션 폴드아웃(접기)·상단 목차(드릴다운)·모두 펼치기/접기 — 내용 보존, 레이아웃만 정리
페르소나 반응 강화('저항·반응 조절' 핵심 차별):
- PersonaCard.triggers(역린) 필드 + CCD 핵심상처 파생 역린 블록
- L0에 무례·모욕·조롱 시 현실적 동맹 균열 반응 지침
버그·성능 수정(라이브/E2E로 포착):
- 게이트웨이 페르소나 격리: --append-system-prompt를 --system-prompt(교체)로 + --exclude-dynamic-system-prompt-sections (내담자 캐릭터 붕괴·개발맥락 누출 차단)
- RAG: 임베더 동기 로드(약 7-13초)를 _warm_rag_caches 백그라운드 warm으로(세션 생성 블로킹 회귀 수정)
- voice TTS RMS 데드힌트 제거, init_state OpennessParams 파라미터객체화
- 한국어 PII(날짜·금액·주소) 마스킹 보강
- 레이아웃 시각 게이트: 폼 컨트롤 값 스크롤 오탐 제외(7/7)
검증: 백엔드 84/84, E2E 42(데스크톱 27·모바일 11·아바타 4), 시각 게이트 7/7
149 lines
5 KiB
Python
149 lines
5 KiB
Python
"""Docker 없이도 도는 in-memory 세션/턴 스토어 (DB degraded 폴백).
|
|
|
|
DB(NAS Postgres)가 단일 SoR 이지만(db.py), Docker off 개발/시연에서도 엔진만 떠 있으면
|
|
상담 1턴이 돌아야 한다. 이 모듈은 app.sessions / app.session_state / app.turns 의
|
|
*최소 in-proc 미러*를 제공한다. DB 가 붙으면 라우트가 DB 경로로 전환한다(교체 대상).
|
|
|
|
스레드/동시성: uvicorn 단일 프로세스 가정의 단순 dict. 멀티워커 시엔 DB 가 SoR 이므로 무방.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
from dataclasses import asdict, dataclass, field
|
|
from decimal import Decimal
|
|
from typing import Optional
|
|
from uuid import uuid4
|
|
|
|
from .services.persona import PersonaCard
|
|
from .services.state_machine import SessionState
|
|
|
|
|
|
DEFAULT_TURN_VISIBLE_TO: tuple[str, ...] = ("client", "counselor", "evaluator")
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class TurnRecord:
|
|
"""발화 1건(② episodic 미러). append-only."""
|
|
|
|
turn_seq: int
|
|
speaker: str # 'counselor' | 'client'
|
|
stage: str
|
|
text: str # 원문(개발용; 실제 저장은 마스킹본)
|
|
text_masked: str
|
|
created_at: float = field(default_factory=time.time)
|
|
llm_provider: str | None = None
|
|
model: str | None = None
|
|
tokens_in: int | None = None
|
|
tokens_out: int | None = None
|
|
cost_usd: float | Decimal | None = None
|
|
audio_ref: str | None = None
|
|
silence_ms: int | None = None
|
|
speech_rate: float | None = None
|
|
barge_in: bool | None = None
|
|
# fast-loop 턴 평가(TurnEvaluation.to_hook_dict). 학습자(상담자) 발화에 부착.
|
|
evaluation: Optional[dict] = None
|
|
visible_to: tuple[str, ...] = DEFAULT_TURN_VISIBLE_TO
|
|
|
|
def is_visible_to(self, role: str) -> bool:
|
|
return role in (self.visible_to or ())
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class InProcSession:
|
|
"""① working + 메타 + 페르소나 핀(in-proc)."""
|
|
|
|
session_id: str
|
|
case_id: str
|
|
learner_id: str
|
|
persona_code: str
|
|
theory_mode: str
|
|
persona: PersonaCard
|
|
state: SessionState
|
|
session_no: int = 1
|
|
created_at: float = field(default_factory=time.time)
|
|
ended_at: Optional[float] = None
|
|
turns: list[TurnRecord] = field(default_factory=list)
|
|
ended: bool = False
|
|
prev_rapport_credit: float = 0.0 # carry-over delta 계산용
|
|
|
|
def recent_turns(self, k: int = 6, visible_to: str | None = None) -> list[dict[str, str]]:
|
|
"""최근 K턴 버퍼(L6 직전 맥락). 마스킹본 사용."""
|
|
turns = self.turns if visible_to is None else self.turns_visible_to(visible_to)
|
|
return [{"speaker": t.speaker, "text": t.text_masked} for t in turns[-k:]]
|
|
|
|
def turns_visible_to(self, role: str) -> list[TurnRecord]:
|
|
return [turn for turn in self.turns if turn.is_visible_to(role)]
|
|
|
|
def masked_turns(self, visible_to: str | None = None) -> list[dict[str, str]]:
|
|
turns = self.turns if visible_to is None else self.turns_visible_to(visible_to)
|
|
return [{"speaker": t.speaker, "text": t.text_masked} for t in turns]
|
|
|
|
|
|
class SessionStore:
|
|
"""in-memory 세션 저장소. DB degraded 시 SoR 대용."""
|
|
|
|
def __init__(self) -> None:
|
|
self._sessions: dict[str, InProcSession] = {}
|
|
|
|
def create(
|
|
self,
|
|
*,
|
|
learner_id: str,
|
|
persona: PersonaCard,
|
|
theory_mode: str,
|
|
state: SessionState,
|
|
session_no: int = 1,
|
|
carry_rapport: float = 0.0,
|
|
) -> InProcSession:
|
|
session_id = uuid4().hex
|
|
case_id = uuid4().hex
|
|
s = InProcSession(
|
|
session_id=session_id,
|
|
case_id=case_id,
|
|
learner_id=learner_id,
|
|
persona_code=persona.code,
|
|
theory_mode=theory_mode,
|
|
persona=persona,
|
|
state=state,
|
|
session_no=session_no,
|
|
prev_rapport_credit=carry_rapport,
|
|
)
|
|
self._sessions[session_id] = s
|
|
return s
|
|
|
|
def get(self, session_id: str) -> Optional[InProcSession]:
|
|
return self._sessions.get(session_id)
|
|
|
|
def put(self, session: InProcSession) -> None:
|
|
self._sessions[session.session_id] = session
|
|
|
|
def list(self) -> list[InProcSession]:
|
|
return list(self._sessions.values())
|
|
|
|
def append_turn(self, session_id: str, turn: TurnRecord) -> None:
|
|
s = self._sessions.get(session_id)
|
|
if s is not None:
|
|
s.turns.append(turn)
|
|
|
|
def update_state(self, session_id: str, state: SessionState) -> None:
|
|
s = self._sessions.get(session_id)
|
|
if s is not None:
|
|
s.state = state
|
|
|
|
def end(self, session_id: str) -> Optional[InProcSession]:
|
|
s = self._sessions.get(session_id)
|
|
if s is not None:
|
|
s.ended = True
|
|
s.ended_at = time.time()
|
|
return s
|
|
|
|
def remove(self, session_id: str) -> None:
|
|
self._sessions.pop(session_id, None)
|
|
|
|
|
|
# 앱 전역 싱글톤 (DB 없이도 라우트가 바로 쓸 수 있게)
|
|
store = SessionStore()
|
|
|
|
|
|
__all__ = ["DEFAULT_TURN_VISIBLE_TO", "TurnRecord", "InProcSession", "SessionStore", "store"]
|