Skip to content

#601 AI 조교 호출 횟수 제한 및 히스토리 영구 저장 기능 구현#611

Merged
wlsgur11 merged 9 commits into
developfrom
ai-usage-limit
Apr 23, 2026
Merged

#601 AI 조교 호출 횟수 제한 및 히스토리 영구 저장 기능 구현#611
wlsgur11 merged 9 commits into
developfrom
ai-usage-limit

Conversation

@wlsgur11
Copy link
Copy Markdown
Contributor

@wlsgur11 wlsgur11 commented Apr 14, 2026

Changelog

  • ProblemAIHintLog 모델 추가: 기존 휘발되던 AI 조교의 답변을 DB에 영구적으로 저장하여 복습할 수 있도록 했습니다.
  • 호출 횟수 제한 로직 적용: 무분별한 LLM API 호출을 방지하기 위해 문제당 5회의 호출 횟수 제한을 ProblemLLMHintAPI에 적용했습니다. 또한 user, created_at 복합 인덱스를 사용하여 최적화 하였습니다.
  • 관리자 권한 우회 적용: admin, super admin 계정은 횟수 제한 없이 무제한으로 호출할 수 있습니다.
  • AIHintHistoryAPI: 문제에 접속했을 때 이전 대화 내역을 프론트엔드에 전달합니다.
  • 프론트엔드 UI 연동 (BottomDrag.vue):
    • 기존의 임시 로직을 수정했습니다.
    • 과거 내역을 채팅창에 렌더링합니다.
    • 사용자의 남은 횟수를 실시간으로 드래그 우측 상단에 표시하며, 횟수 소진 시 버튼을 비활성화합니다.
  • 로컬 개발 환경 테스트: 실제 LLM 서버가 없는 로컬 환경에서도 UI/UX 및 DB 로직을 테스트할 수 있도록 환경변수(IS_LOCAL_TEST=True) 기반의 Mocking 스트리밍 응답을 추가했습니다.
  • 브라우저의 다중 탭을 사용한 호출 횟수 제한을 우회하는 버그를 수정했습니다.

Testing

2026-04-14-155642-1_k9GKwpUi.mp4
  • 백엔드 단위 테스트 작성 및 통과 완료:
    • problem.tests.ProblemLLMHintAPITest: 정상 저장, 일일 제한 초과 에러(400), 문제당 제한 초과 에러(400), 관리자 제한 우회 동작 등 13개 케이스 통과 (OK)
    • problem.tests.AIHintHistoryAPITest: 비로그인 차단, 로그 데이터 및 카운트 반환 구조(Schema) 정상 동작 확인 (OK)
  • 로컬 E2E 테스트 완료: IS_LOCAL_TEST=True 모드를 켜고 프론트엔드에서 직접 버튼을 클릭하여, 응답 스트리밍이 끝나면 즉시 DB에 저장되고 남은 횟수가 즉각적으로 UI에서 차감되는 것을 확인했습니다. 창을 닫았다 열어도 이전 대화 내용이 정상적으로 유지됩니다.
image image image
  • DB 추가 확인

Ops Impact

  • DB 마이그레이션 필수 (중요): problem_ai_hint_log라는 새로운 테이블이 추가되었습니다. 배포 시 백엔드 컨테이너에서 반드시 DB 마이그레이션(python manage.py makemigrations problem, python manage.py migrate)을 실행해야 합니다.
  • DB 용량 고려: 유저가 힌트를 받을 때마다 텍스트가 DB에 Insert 됩니다. 단일 힌트의 길이를 제한하도록 시스템 프롬프트가 설정되어 있어 당장 큰 부하는 없겠으나, 장기적인 관점에서 DB 용량 모니터링이 권장됩니다.
  • 속도 제한을 위한 Redis 대신 PostgreSQL를 활용하였으므로(복습 히스토리 유지 목적), 추가적인 Redis 캐시 설정 변경은 필요 없습니다.

Version Compatibility

N/A

- 백엔드: ProblemAIHintLog 모델 추가 및 영구 저장 로직 구현
- 백엔드: 일일 30회, 문제당 5회 횟수 제한 및 관리자(Admin) 우회 로직 적용
- 백엔드: 과거 AI 힌트 내역 및 사용 횟수 조회 API(AIHintHistoryAPI) 추가
- 프론트엔드: BottomDrag.vue에 백엔드 API 연동 및 남은 횟수 UI 렌더링 적용
- 프론트엔드: 기존 localStorage 기반의 임시 저장 로직 제거
- 테스트: 횟수 제한 및 API 관련 백엔드 단위 테스트 추가 및 100% 통과
@wlsgur11 wlsgur11 requested review from Neibce and taekoong April 14, 2026 07:03
@wlsgur11 wlsgur11 self-assigned this Apr 14, 2026
@wlsgur11 wlsgur11 added frontend-client 프론트엔드(클라이언트) 페이지와 관련된 일에 지정합니다. backend 일반적인 백엔드에서 처리해야 하는 일에 지정합니다. labels Apr 14, 2026
@Neibce Neibce requested a review from Copilot April 14, 2026 07:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

AI 조교(LLM 힌트) 기능에 사용 횟수 제한을 도입하고, 기존 스트리밍으로 휘발되던 응답을 DB에 영구 저장/조회할 수 있도록 백엔드·프론트엔드 전반을 확장한 PR입니다.

Changes:

  • ProblemAIHintLog 모델/마이그레이션 추가 및 힌트 스트리밍 완료 시 DB 저장
  • 사용자별(일 30회) / 문제별(5회) 호출 제한 적용 + 관리자 무제한 우회
  • 히스토리 조회 API(AIHintHistoryAPI) 및 프론트 채팅 UI 연동/표시 개선

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
frontend/src/pages/oj/views/problem/problemSolving/problemSolvingComponent/BottomDrag.vue 히스토리 API 연동, 잔여 횟수 표시, 메시지 렌더링 변경
frontend/src/pages/oj/api.js AI 힌트 히스토리 조회 API 래퍼 추가
backend/problem/views/oj.py 호출 제한 로직, 스트리밍 응답 DB 저장, 히스토리 API 추가
backend/problem/urls/oj.py 히스토리 API 라우팅 추가
backend/problem/tests.py 호출 제한/저장/히스토리 API 관련 테스트 케이스 추가
backend/problem/serializers.py 힌트 로그 직렬화 Serializer 추가
backend/problem/models.py ProblemAIHintLog 모델 추가
backend/problem/migrations/0002_problemaihintlog.py ProblemAIHintLog 테이블 생성 마이그레이션 추가
backend/problem/llm_hint.py 로컬 테스트용 mock 스트리밍 모드 추가

Comment thread backend/problem/views/oj.py Outdated
Comment thread backend/problem/views/oj.py Outdated
Comment thread backend/problem/views/oj.py Outdated
- 남은 횟수 UI표기 변경
- 페이지가 열릴 때가 아닌 AI 조교 패널이 열릴 때 history 불러오기로 수정
- streaming chunk 누적 방식 string -> list로 변경
- 날짜 탐색 방식 범위 검색 방식으로 변경
- v-html="renderText" 보안 위협으로 인한 제거
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Comment thread backend/problem/views/oj.py
- 사용하지 않는 renderText를 삭제하였습니다.
- 브라우저 탭을 여러개 사용하여 횟수 제한을 우회할 수 있는 버그 수정
  - 스트리밍 시작 전 빈 로그를 먼저 생성하여 즉시 횟수 차감 처리 (Transaction 적용)
  - vLLM 통신 에러 및 스트림 중단 시 생성된 빈 로그를 삭제하여 횟수 롤백 로직 추가
- AI 조교 일일 사용 횟수(30회) 제한 제거, 문제당 5회 제한만 유지
- ProblemAIHintLog 모델 성능 최적화: user, created_at 인덱스 추가
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.

Comment thread backend/problem/views/oj.py
Comment thread backend/problem/views/oj.py Outdated
Comment thread backend/problem/views/oj.py Outdated
Comment thread backend/problem/views/oj.py Outdated
- select_for_update()를 활용하여 원자성 충족 -> 동시 요청 병렬적 처리 방지 (DB락)
- daily_count 하드 코딩 제거
- 불필요한 import 삭제
- 일일 제한 테스트 코드 삭제
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 7 comments.

Comment thread backend/problem/models.py
Comment thread backend/problem/views/oj.py
Comment thread backend/problem/views/oj.py Outdated
Comment thread backend/problem/views/oj.py
Comment thread backend/problem/views/oj.py
Comment thread backend/problem/tests.py
wlsgur11 and others added 3 commits April 21, 2026 13:54
…ingComponent/BottomDrag.vue

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- 내용이 조금이라도 있으면 저장, 1바이트도 없으면 횟수 환불
- 바깥에서 미리 만들어둔 hint_log 객체의 내용만 바꾼 뒤 저장
- user_id, problem_id, created_at 인덱스 생성
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.

Comment thread backend/problem/views/oj.py Outdated
Comment thread backend/problem/views/oj.py
Comment thread backend/problem/views/oj.py Outdated
- AI 힌트 스트리밍 빈 응답 시 횟수 차감 방지 로직 추가
- 히스토리 조회 API의 비공개/대회용 문제 접근 차단 및 조인 최적화
- 동시성 제어 중 발생하는 SimpleLazyObject 에러 해결 (get_user_model)
- 힌트 생성 및 조회 관련 엣지 케이스 테스트 코드 추가
@Neibce Neibce requested a review from Copilot April 23, 2026 05:54
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Comment thread backend/problem/views/oj.py
Comment thread backend/problem/tests.py
@wlsgur11 wlsgur11 merged commit f3ce789 into develop Apr 23, 2026
4 checks passed
@wlsgur11 wlsgur11 deleted the ai-usage-limit branch April 23, 2026 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend 일반적인 백엔드에서 처리해야 하는 일에 지정합니다. frontend-client 프론트엔드(클라이언트) 페이지와 관련된 일에 지정합니다.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants