DOCS/plans/250901_rb8001_chat_history_implementation_plan.md
happybell80 79758fa496 docs: Frontend 응답 형식 추가 - 형식 변환 필수
- Frontend 기대 형식: {text, sender, timestamp} 배열
- 현재 DB 형식: user_message, robeing_response 한 row
- 각 row를 2개 메시지로 분리 필요
2025-09-01 22:44:34 +09:00

3.8 KiB

rb8001 카톡 스타일 대화 히스토리 구현 계획

작성일: 2025-09-01

대상: rb8001 (프로덕션 로빙)

참조: rb10508_micro 구현 코드

수정: PostgreSQL 전용 히스토리 구현으로 변경


현재 상태 분석

  • rb10508_micro: /api/config 엔드포인트 구현됨 (endpoints.py:407)
  • rb8001:
    • database.py: ConversationLog 모델 있음 (line 52)
    • get_recent_conversations() 함수 있음 (line 81)
    • get_paginated_conversations() 함수 없음 (추가 필요)
    • main.py: @app.get() 직접 사용, router 분리 없음

UUID 및 인증 체계

  • 내부 처리: 모든 user_id는 UUID 형식으로 통일
  • JWT 토큰: X-User-Id 헤더로 UUID 전달
  • Slack 사용자: slack_user_mapping 테이블로 UUID 매핑
  • Frontend: JWT에서 user_id(UUID) 추출하여 API 호출

수정 필요 파일

백엔드 (rb8001)

  1. app/core/config.py - 환경변수 추가 완료
  2. app/state/database.py - get_paginated_conversations() 추가 완료
  3. main.py - 엔드포인트 수정 필요
    • /api/config 이미 추가됨
    • /api/messages 삭제 필요 (잘못 추가함)
    • ⚠️ /api/history 추가 필요 (이것만!)
  4. .env 파일 - 환경변수 설정 완료

프론트엔드 (frontend-customer)

  1. 이미 완료됨 (250818 rb10508_micro에서 구현)
  2. /api/history 호출 중 (수정 불필요)

API 스펙

// GET /api/config
{ message_batch_size: 30, max_messages_in_dom: 200 }

// GET /api/history?before={timestamp}&limit=30
// Frontend 기대 형식 (robeing-api.ts:154-161)
{
  messages: Array<{
    id: string;
    text: string;
    sender: 'user' | 'robeing';
    timestamp: string;
    metadata?: any;
  }>;
  has_more: boolean;
}

// 현재 rb8001 /api/messages 반환 형식 (변환 필요!)
{
  "user_message": "...",
  "robeing_response": "...", 
  "timestamp": "...",
  "user_id": "..."
}

PostgreSQL 쿼리 예시

-- user_id는 항상 UUID 형식
SELECT id, message, response, timestamp, user_id 
FROM conversation_logs 
WHERE user_id = $1::uuid  -- UUID 타입 캐스팅
  AND robeing_id = 'rb8001'
  AND timestamp < $2
ORDER BY timestamp DESC 
LIMIT 30;

API 호출 예시

// Frontend에서 JWT의 user_id 사용
const token = localStorage.getItem('token');
const decoded = jwt_decode(token);
const userId = decoded.user_id;  // UUID 형식

// API 호출 (Frontend가 이미 호출 중)
await fetch(`/api/history?before=${timestamp}&limit=30`);

데이터베이스 스키마

-- conversation_logs 테이블 (실제)
CREATE TABLE conversation_logs (
    id INTEGER PRIMARY KEY (auto-increment),
    robeing_id VARCHAR,
    channel_id VARCHAR,
    message VARCHAR,
    response VARCHAR,
    intent VARCHAR,
    confidence DOUBLE PRECISION,
    timestamp TIMESTAMP,
    user_id UUID (FK  users),
    slack_user_id VARCHAR(100),
    thread_ts VARCHAR(128),
    channel_type VARCHAR(32)
);

주의사항

API 경로

# main.py에 직접 추가 (rb8001은 router 분리 없음)
@app.get("/api/config")
@app.get("/api/messages")

rb10508_micro 참조 코드

# rb10508_micro/app/config.py:101-103
MESSAGE_BATCH_SIZE: int = int(os.getenv("MESSAGE_BATCH_SIZE", 30))
SCROLL_THRESHOLD: int = int(os.getenv("SCROLL_THRESHOLD", 100))
MAX_MESSAGES_IN_DOM: int = int(os.getenv("MAX_MESSAGES_IN_DOM", 200))

실제 필요 작업

  1. 백엔드 대부분 완료 (bbf9c50 커밋)
  2. ⚠️ rb8001 main.py 수정 필요:
    • /api/messages/api/history 이름 변경
    • 응답 형식 변환 필수:
      • DB 각 row를 2개 메시지로 분리 (user, robeing)
      • text, sender, timestamp 형식 맞추기
      • 배열로 반환
  3. Frontend 수정 불필요 (이미 /api/history 호출 중)
  4. 배포는 Gitea Actions 자동 처리