Skip to content

Commit ccd3f3e

Browse files
committed
refactor: 유저노트/퓨처노트 적용 시 지연 적용으로 변경
1 parent cec69ae commit ccd3f3e

1 file changed

Lines changed: 10 additions & 85 deletions

File tree

src/main/java/shinhan/click/domain/chat/service/ChatService.java

Lines changed: 10 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -527,61 +527,6 @@ private void applyPendingUpdates(com.google.genai.Chat session, Chat chat, Long
527527
// 업데이트 명령 전송
528528
session.sendMessage(updateCommand.toString());
529529
}
530-
private void createNewSession(Chat chat) {
531-
Client client = new Client();
532-
533-
GenerateContentConfig cfg = GenerateContentConfig.builder()
534-
.systemInstruction(Content.fromParts(Part.fromText(buildSystemPrompt(chat))))
535-
.build();
536-
537-
com.google.genai.Chat newSession = client.chats.create("gemini-2.5-flash", cfg);
538-
chatSessionRegistry.put(chat.getId(), newSession);
539-
540-
// 최근 히스토리 시딩
541-
List<Message> messages = messageRepository.findByChatId(chat.getId());
542-
seedRecentHistoryFast(newSession, messages);
543-
}
544-
545-
/**
546-
* 유저노트 교체 명령어 생성
547-
*/
548-
private String buildUserNoteReplaceCommand(String oldPrompt, String newPrompt, Chat chat) {
549-
String personaName = safe(chat.getPersonaName());
550-
String characterName = safe(chat.getCharacter().getName());
551-
552-
StringBuilder command = new StringBuilder();
553-
command.append("(시스템 명령) 유저노트 교체:\n");
554-
555-
if (oldPrompt != null && !oldPrompt.isEmpty()) {
556-
command.append("- 기존 규칙 제거 완료\n");
557-
}
558-
559-
command.append("- 새 최우선 규칙 적용: ").append(newPrompt
560-
.replace("{{user}}", personaName)
561-
.replace("{{char}}", characterName)).append("\n");
562-
563-
command.append("- 모든 응답은 \"표준 따옴표\" 대사 위주로 작성\n");
564-
command.append("- 이제부터 이 규칙만 적용됩니다.");
565-
566-
return command.toString();
567-
}
568-
569-
/**
570-
* 퓨처노트 교체 명령어 생성
571-
*/
572-
private String buildFutureNoteReplaceCommand(FutureNote fn, Chat chat) {
573-
StringBuilder command = new StringBuilder();
574-
command.append("(시스템 명령) 퓨처노트 시나리오 적용:\n");
575-
command.append("- 제목: ").append(safe(fn.getTitle())).append("\n");
576-
command.append("- 총 턴: ").append(fn.getPlayTurn()).append("\n");
577-
578-
if (fn.getMileStones() != null && !fn.getMileStones().isEmpty()) {
579-
command.append("- 현재 턴에 맞는 마일스톤을 따라 진행\n");
580-
}
581-
582-
command.append("- 모든 응답은 \"표준 따옴표\" 대사 위주로 작성");
583-
return command.toString();
584-
}
585530

586531
/**
587532
* 최적화된 유저노트 적용 (세션 재생성 없이 교체 명령만 전달)
@@ -602,20 +547,12 @@ public void applyUserNote(Long chatId, Long userNoteId) {
602547
UserNote userNote = userNoteRepository.findById(userNoteId)
603548
.orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOTE_NOT_FOUND));
604549

605-
// 1) 기존 유저노트 교체
606-
String oldUserNote = chat.getUserNotePrompt();
550+
// 1) DB만 업데이트
607551
chat.setUserNotePrompt(userNote.getPrompt());
608552
chatRepository.save(chat);
609553

610-
// 2) 기존 세션에 교체 명령만 전달 (세션 재생성 없음)
611-
com.google.genai.Chat session = chatSessionRegistry.get(chatId);
612-
if (session != null) {
613-
String replaceCommand = buildUserNoteReplaceCommand(oldUserNote, userNote.getPrompt(), chat);
614-
session.sendMessage(replaceCommand);
615-
} else {
616-
// 세션이 없을 때만 새로 생성
617-
createNewSession(chat);
618-
}
554+
// 2) 지연 적용 플래그만 설정
555+
chatSessionRegistry.markUserNoteForUpdate(chatId);
619556
}
620557

621558
/**
@@ -637,19 +574,13 @@ public void applyFutureNote(Long chatId, Long futureNoteId) {
637574
FutureNote fn = futureNoteRepository.findById(futureNoteId)
638575
.orElseThrow(() -> new GeneralException(ErrorStatus.FUTURE_NOTE_NOT_FOUND));
639576

640-
// 1) 기존 퓨처노트 교체
577+
// 1) DB만 업데이트
641578
chat.setFutureNote(fn);
642579
chat.initProgressIfNull();
580+
chatRepository.save(chat);
643581

644-
// 2) 기존 세션에 교체 명령만 전달
645-
com.google.genai.Chat session = chatSessionRegistry.get(chatId);
646-
if (session != null) {
647-
String replaceCommand = buildFutureNoteReplaceCommand(fn, chat);
648-
session.sendMessage(replaceCommand);
649-
} else {
650-
// 세션이 없을 때만 새로 생성
651-
createNewSession(chat);
652-
}
582+
// 2) 지연 적용 플래그만 설정
583+
chatSessionRegistry.markFutureNoteForUpdate(chatId);
653584
}
654585

655586
/**
@@ -709,17 +640,11 @@ public void removeUserNote(Long chatId) {
709640
throw new GeneralException(ErrorStatus.CHAT_NOT_FOUND);
710641
}
711642

712-
// 1) UserNote 제거
643+
// 1) UserNote 제거 (기존 코드)
713644
chat.setUserNotePrompt(null);
714645
chatRepository.save(chat);
715646

716-
// 2) 기존 세션에 제거 명령만 전달
717-
com.google.genai.Chat session = chatSessionRegistry.get(chatId);
718-
if (session != null) {
719-
session.sendMessage("(시스템 명령) 유저노트 규칙이 제거되었습니다. 기본 캐릭터 설정으로 돌아갑니다. 모든 응답은 \"표준 따옴표\" 대사 위주로 작성하세요.");
720-
} else {
721-
// 세션이 없을 때만 새로 생성
722-
createNewSession(chat);
723-
}
647+
// 2) 지연 적용 플래그만 설정 (AI 세션 호출 제거)
648+
chatSessionRegistry.markUserNoteForUpdate(chatId);
724649
}
725650
}

0 commit comments

Comments
 (0)