feat: AI 대화 DB 영속 + 다중 대화/기록 목록
- AiConversation·AiMessage 엔티티(user/conversation CASCADE), 제목 자동 생성 - 대화 스코프 API: GET/POST /ai/conversations, GET/:id, POST/:id/messages, DELETE/:id (전부 본인 소유 스코프 → IDOR 차단) - 클라는 새 메시지 1건만 전송, 서버가 DB 이력으로 Claude 호출(무상태 /ai/chat 폐기) - Claude 실패 시 사용자 메시지·빈 대화 롤백(고아 데이터 방지) - 프론트: AgentChatPanel 채팅↔지난 대화 목록 뷰(열기/삭제), ai.store 재작성(목록·현재 대화) - 다른 기기·재로그인·새로고침에도 기록 유지 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,17 +1,15 @@
|
||||
<script setup lang="ts">
|
||||
// AI 채팅 패널 — Claude API 와 대화. body 로 Teleport 되는 우측 슬라이드 패널.
|
||||
// 대화 이력은 ai.store(싱글톤)에 보관해 화면 이동에도 유지되고, '새 대화' 로만 초기화된다.
|
||||
// AI 채팅 패널 — Claude API 와 대화. 대화/이력은 DB(ai.store)에 보관.
|
||||
// 우측 슬라이드 패널. 채팅 뷰 ↔ 지난 대화(기록) 목록 뷰 전환.
|
||||
import { nextTick, ref, watch } from 'vue'
|
||||
import { storeToRefs } from 'pinia'
|
||||
import { useAiStore } from '@/stores/ai.store'
|
||||
import { relativeTimeKo } from '@/shared/utils/format'
|
||||
|
||||
interface Props {
|
||||
open: boolean
|
||||
taskTitle?: string
|
||||
}
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
taskTitle: '',
|
||||
})
|
||||
const props = defineProps<Props>()
|
||||
|
||||
interface Emits {
|
||||
(e: 'close'): void
|
||||
@@ -19,24 +17,26 @@ interface Emits {
|
||||
const emit = defineEmits<Emits>()
|
||||
|
||||
const aiStore = useAiStore()
|
||||
const { messages, loading, error } = storeToRefs(aiStore)
|
||||
const { conversations, listLoading, messages, loading, error } =
|
||||
storeToRefs(aiStore)
|
||||
|
||||
// 입력값만 컴포넌트 로컬(전송 전 임시 텍스트)
|
||||
// 입력값과 뷰 전환만 컴포넌트 로컬
|
||||
const input = ref('')
|
||||
const showHistory = ref(false)
|
||||
const bodyEl = ref<HTMLElement | null>(null)
|
||||
|
||||
// 새 메시지/로딩 시 하단으로 스크롤
|
||||
function scrollToBottom() {
|
||||
void nextTick(() => {
|
||||
if (bodyEl.value) bodyEl.value.scrollTop = bodyEl.value.scrollHeight
|
||||
if (bodyEl.value && !showHistory.value) {
|
||||
bodyEl.value.scrollTop = bodyEl.value.scrollHeight
|
||||
}
|
||||
})
|
||||
}
|
||||
watch(() => [messages.value.length, loading.value], scrollToBottom)
|
||||
// 패널을 열 때 하단으로 스크롤
|
||||
watch(
|
||||
() => props.open,
|
||||
(open) => {
|
||||
if (open) scrollToBottom()
|
||||
if (open && !showHistory.value) scrollToBottom()
|
||||
},
|
||||
)
|
||||
|
||||
@@ -44,13 +44,30 @@ async function send() {
|
||||
const text = input.value
|
||||
if (!text.trim() || loading.value) return
|
||||
input.value = ''
|
||||
await aiStore.send(text)
|
||||
const ok = await aiStore.send(text)
|
||||
if (!ok) input.value = text // 실패 시 입력 복원
|
||||
}
|
||||
|
||||
function resetChat() {
|
||||
aiStore.reset()
|
||||
function newChat() {
|
||||
aiStore.newConversation()
|
||||
showHistory.value = false
|
||||
input.value = ''
|
||||
}
|
||||
|
||||
async function toggleHistory() {
|
||||
showHistory.value = !showHistory.value
|
||||
if (showHistory.value) await aiStore.loadConversations()
|
||||
}
|
||||
|
||||
async function openConv(id: string) {
|
||||
await aiStore.openConversation(id)
|
||||
showHistory.value = false
|
||||
}
|
||||
|
||||
async function removeConv(id: string) {
|
||||
if (!window.confirm('이 대화를 삭제할까요?')) return
|
||||
await aiStore.removeConversation(id)
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -69,26 +86,18 @@ function resetChat() {
|
||||
<header class="ac-head">
|
||||
<div class="ac-title">
|
||||
<span class="ac-logo">AI</span>
|
||||
<div class="ac-titletext">
|
||||
<div class="ac-name">
|
||||
Relay 어시스턴트
|
||||
</div>
|
||||
<div
|
||||
v-if="taskTitle"
|
||||
class="ac-ctx"
|
||||
>
|
||||
{{ taskTitle }}
|
||||
</div>
|
||||
<div class="ac-name">
|
||||
{{ showHistory ? '지난 대화' : 'Relay 어시스턴트' }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="ac-headbtns">
|
||||
<button
|
||||
class="ac-iconbtn"
|
||||
type="button"
|
||||
title="새 대화"
|
||||
aria-label="새 대화"
|
||||
:disabled="loading"
|
||||
@click="resetChat"
|
||||
:title="showHistory ? '대화로 돌아가기' : '지난 대화'"
|
||||
:aria-label="showHistory ? '대화로 돌아가기' : '지난 대화'"
|
||||
:class="{ on: showHistory }"
|
||||
@click="toggleHistory"
|
||||
>
|
||||
<svg
|
||||
viewBox="0 0 24 24"
|
||||
@@ -98,7 +107,26 @@ function resetChat() {
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<path d="M3 12a9 9 0 0 1 9-9 9.75 9.75 0 0 1 6.74 2.74L21 8" /><path d="M21 3v5h-5" />
|
||||
<path d="M3 12a9 9 0 1 0 9-9 9.74 9.74 0 0 0-6.74 2.74L3 8" /><path d="M3 3v5h5" /><path d="M12 7v5l3 2" />
|
||||
</svg>
|
||||
</button>
|
||||
<button
|
||||
class="ac-iconbtn"
|
||||
type="button"
|
||||
title="새 대화"
|
||||
aria-label="새 대화"
|
||||
:disabled="loading"
|
||||
@click="newChat"
|
||||
>
|
||||
<svg
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<path d="M12 5v14M5 12h14" />
|
||||
</svg>
|
||||
</button>
|
||||
<button
|
||||
@@ -122,12 +150,66 @@ function resetChat() {
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<!-- 대화 영역 -->
|
||||
<!-- 기록(지난 대화) 뷰 -->
|
||||
<div
|
||||
v-if="showHistory"
|
||||
class="ac-body ac-history"
|
||||
>
|
||||
<div
|
||||
v-if="listLoading && conversations.length === 0"
|
||||
class="ac-hist-empty"
|
||||
>
|
||||
불러오는 중…
|
||||
</div>
|
||||
<div
|
||||
v-else-if="conversations.length === 0"
|
||||
class="ac-hist-empty"
|
||||
>
|
||||
지난 대화가 없습니다.
|
||||
</div>
|
||||
<template v-else>
|
||||
<div
|
||||
v-for="c in conversations"
|
||||
:key="c.id"
|
||||
class="ac-hist-item"
|
||||
@click="openConv(c.id)"
|
||||
>
|
||||
<div class="ac-hist-main">
|
||||
<div class="ac-hist-title">
|
||||
{{ c.title }}
|
||||
</div>
|
||||
<div class="ac-hist-time">
|
||||
{{ relativeTimeKo(c.updatedAt) }}
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
class="ac-hist-del"
|
||||
type="button"
|
||||
title="삭제"
|
||||
aria-label="대화 삭제"
|
||||
@click.stop="removeConv(c.id)"
|
||||
>
|
||||
<svg
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<path d="M3 6h18M8 6V4a1 1 0 0 1 1-1h6a1 1 0 0 1 1 1v2m2 0v14a1 1 0 0 1-1 1H7a1 1 0 0 1-1-1V6" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
|
||||
<!-- 채팅 뷰 -->
|
||||
<div
|
||||
v-else
|
||||
ref="bodyEl"
|
||||
class="ac-body"
|
||||
>
|
||||
<!-- 환영 문구 -->
|
||||
<div
|
||||
v-if="messages.length === 0"
|
||||
class="ac-welcome"
|
||||
@@ -158,7 +240,6 @@ function resetChat() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 로딩(응답 대기) -->
|
||||
<div
|
||||
v-if="loading"
|
||||
class="ac-msg assistant"
|
||||
@@ -177,8 +258,11 @@ function resetChat() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 입력 -->
|
||||
<footer class="ac-foot">
|
||||
<!-- 입력 (채팅 뷰에서만) -->
|
||||
<footer
|
||||
v-if="!showHistory"
|
||||
class="ac-foot"
|
||||
>
|
||||
<div class="ac-inputrow">
|
||||
<textarea
|
||||
v-model="input"
|
||||
@@ -271,22 +355,11 @@ function resetChat() {
|
||||
font-weight: 800;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.ac-titletext {
|
||||
min-width: 0;
|
||||
}
|
||||
.ac-name {
|
||||
font-size: 0.9375rem;
|
||||
font-weight: 700;
|
||||
color: var(--text);
|
||||
}
|
||||
.ac-ctx {
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-3);
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
max-width: 16rem;
|
||||
}
|
||||
.ac-headbtns {
|
||||
display: flex;
|
||||
gap: 0.25rem;
|
||||
@@ -307,6 +380,10 @@ function resetChat() {
|
||||
background: #f1f2f4;
|
||||
color: var(--text);
|
||||
}
|
||||
.ac-iconbtn.on {
|
||||
background: var(--accent-weak);
|
||||
color: var(--accent);
|
||||
}
|
||||
.ac-iconbtn:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: default;
|
||||
@@ -433,6 +510,70 @@ function resetChat() {
|
||||
padding: 0.5rem 0.75rem;
|
||||
}
|
||||
|
||||
/* 기록(지난 대화) 목록 */
|
||||
.ac-history {
|
||||
gap: 0;
|
||||
padding: 0.5rem 0.5rem;
|
||||
}
|
||||
.ac-hist-empty {
|
||||
margin: auto;
|
||||
text-align: center;
|
||||
font-size: 0.8125rem;
|
||||
color: var(--text-3);
|
||||
padding: 2rem 0;
|
||||
}
|
||||
.ac-hist-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
padding: 0.625rem 0.625rem;
|
||||
border-radius: 0.5rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
.ac-hist-item:hover {
|
||||
background: #f4f5f7;
|
||||
}
|
||||
.ac-hist-main {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
.ac-hist-title {
|
||||
font-size: 0.875rem;
|
||||
color: var(--text);
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
.ac-hist-time {
|
||||
font-size: 0.6875rem;
|
||||
color: var(--text-3);
|
||||
margin-top: 0.1875rem;
|
||||
}
|
||||
.ac-hist-del {
|
||||
width: 1.75rem;
|
||||
height: 1.75rem;
|
||||
border: none;
|
||||
background: transparent;
|
||||
border-radius: 0.4375rem;
|
||||
color: var(--text-3);
|
||||
display: grid;
|
||||
place-items: center;
|
||||
cursor: pointer;
|
||||
flex-shrink: 0;
|
||||
opacity: 0;
|
||||
}
|
||||
.ac-hist-item:hover .ac-hist-del {
|
||||
opacity: 1;
|
||||
}
|
||||
.ac-hist-del:hover {
|
||||
background: var(--red-weak);
|
||||
color: var(--red);
|
||||
}
|
||||
.ac-hist-del svg {
|
||||
width: 0.9375rem;
|
||||
height: 0.9375rem;
|
||||
}
|
||||
|
||||
/* 입력 */
|
||||
.ac-foot {
|
||||
border-top: 1px solid var(--border);
|
||||
|
||||
@@ -1,13 +1,53 @@
|
||||
import { useApi } from './useApi'
|
||||
import type { ChatMessage, ChatResult } from '@/types/ai'
|
||||
import type {
|
||||
ConversationDetail,
|
||||
ConversationSummary,
|
||||
SendResult,
|
||||
} from '@/types/ai'
|
||||
import type { Paginated } from '@/types/pagination'
|
||||
|
||||
// AI 도메인 API Composable — 대화 이력 전체를 전달(서버 무상태)
|
||||
// AI 도메인 API Composable — 대화/메시지는 서버(DB)에서 관리(본인 소유 스코프)
|
||||
export function useAi() {
|
||||
const api = useApi()
|
||||
|
||||
async function chat(messages: ChatMessage[]): Promise<ChatResult> {
|
||||
return (await api.post('/ai/chat', { messages })) as unknown as ChatResult
|
||||
async function listConversations(
|
||||
page = 1,
|
||||
size = 30,
|
||||
): Promise<Paginated<ConversationSummary>> {
|
||||
return (await api.get('/ai/conversations', {
|
||||
params: { page, size },
|
||||
})) as unknown as Paginated<ConversationSummary>
|
||||
}
|
||||
|
||||
return { chat }
|
||||
async function getConversation(id: string): Promise<ConversationDetail> {
|
||||
return (await api.get(
|
||||
`/ai/conversations/${id}`,
|
||||
)) as unknown as ConversationDetail
|
||||
}
|
||||
|
||||
// 새 대화 시작(첫 메시지)
|
||||
async function startConversation(content: string): Promise<SendResult> {
|
||||
return (await api.post('/ai/conversations', {
|
||||
content,
|
||||
})) as unknown as SendResult
|
||||
}
|
||||
|
||||
// 기존 대화에 메시지 추가
|
||||
async function sendMessage(id: string, content: string): Promise<SendResult> {
|
||||
return (await api.post(`/ai/conversations/${id}/messages`, {
|
||||
content,
|
||||
})) as unknown as SendResult
|
||||
}
|
||||
|
||||
async function deleteConversation(id: string): Promise<void> {
|
||||
await api.delete(`/ai/conversations/${id}`)
|
||||
}
|
||||
|
||||
return {
|
||||
listConversations,
|
||||
getConversation,
|
||||
startConversation,
|
||||
sendMessage,
|
||||
deleteConversation,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +1,119 @@
|
||||
import { ref } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { useAi } from '@/composables/useAi'
|
||||
import type { ChatMessage } from '@/types/ai'
|
||||
import type { ChatMessage, ConversationSummary } from '@/types/ai'
|
||||
|
||||
// AI 채팅 상태 — 싱글톤 스토어라 화면(AppShell) 재마운트에도 대화가 유지된다.
|
||||
// 초기화는 사용자가 '새 대화' 를 누를 때만 수행한다.
|
||||
// AI 채팅 상태 — 대화/메시지는 서버(DB)에 보관. 스토어는 현재 열린 대화 + 목록 캐시를 관리.
|
||||
export const useAiStore = defineStore('ai', () => {
|
||||
const { chat } = useAi()
|
||||
const api = useAi()
|
||||
|
||||
// 대화 목록(최근순)
|
||||
const conversations = ref<ConversationSummary[]>([])
|
||||
const listLoading = ref(false)
|
||||
// 현재 열린 대화
|
||||
const currentId = ref<string | null>(null)
|
||||
const messages = ref<ChatMessage[]>([])
|
||||
const loading = ref(false)
|
||||
const error = ref<string | null>(null)
|
||||
|
||||
// 메시지 전송 — 사용자 발화 추가 후 대화 이력 전체로 응답 요청
|
||||
async function send(text: string): Promise<void> {
|
||||
// 대화 목록 로드(패널/기록 뷰 진입 시)
|
||||
async function loadConversations(): Promise<void> {
|
||||
listLoading.value = true
|
||||
try {
|
||||
const { items } = await api.listConversations(1, 30)
|
||||
conversations.value = items
|
||||
} catch {
|
||||
// 목록 로드 실패는 조용히(빈 목록 유지)
|
||||
} finally {
|
||||
listLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 새 대화 — DB 호출 없이 화면만 비운다(첫 메시지 전송 시 생성)
|
||||
function newConversation(): void {
|
||||
currentId.value = null
|
||||
messages.value = []
|
||||
error.value = null
|
||||
}
|
||||
|
||||
// 기존 대화 열기 — 메시지 로드
|
||||
async function openConversation(id: string): Promise<void> {
|
||||
if (loading.value) return
|
||||
error.value = null
|
||||
try {
|
||||
const detail = await api.getConversation(id)
|
||||
currentId.value = detail.id
|
||||
messages.value = detail.messages
|
||||
} catch {
|
||||
error.value = '대화를 불러오지 못했습니다.'
|
||||
}
|
||||
}
|
||||
|
||||
// 메시지 전송 — 성공 여부 반환(실패 시 호출측이 입력 복원)
|
||||
async function send(text: string): Promise<boolean> {
|
||||
const content = text.trim()
|
||||
if (!content || loading.value) return
|
||||
if (!content || loading.value) return false
|
||||
error.value = null
|
||||
messages.value.push({ role: 'user', content })
|
||||
loading.value = true
|
||||
try {
|
||||
const { reply } = await chat(messages.value)
|
||||
messages.value.push({ role: 'assistant', content: reply })
|
||||
if (currentId.value === null) {
|
||||
const res = await api.startConversation(content)
|
||||
currentId.value = res.conversationId
|
||||
messages.value.push({ role: 'assistant', content: res.reply })
|
||||
conversations.value.unshift({
|
||||
id: res.conversationId,
|
||||
title: res.title,
|
||||
updatedAt: new Date().toISOString(),
|
||||
})
|
||||
} else {
|
||||
const res = await api.sendMessage(currentId.value, content)
|
||||
messages.value.push({ role: 'assistant', content: res.reply })
|
||||
bumpConversation(currentId.value)
|
||||
}
|
||||
return true
|
||||
} catch {
|
||||
// 실패 — 낙관적으로 추가한 사용자 메시지 롤백(백엔드도 롤백) + 에러 표시
|
||||
messages.value.pop()
|
||||
error.value = '응답을 가져오지 못했습니다. 잠시 후 다시 시도해 주세요.'
|
||||
return false
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 새 대화 — 사용자가 명시적으로 누를 때만 초기화
|
||||
function reset(): void {
|
||||
messages.value = []
|
||||
error.value = null
|
||||
loading.value = false
|
||||
// 대화 삭제 — 목록에서 제거, 현재 대화였다면 새 대화로
|
||||
async function removeConversation(id: string): Promise<void> {
|
||||
try {
|
||||
await api.deleteConversation(id)
|
||||
conversations.value = conversations.value.filter((c) => c.id !== id)
|
||||
if (currentId.value === id) newConversation()
|
||||
} catch {
|
||||
error.value = '대화를 삭제하지 못했습니다.'
|
||||
}
|
||||
}
|
||||
|
||||
return { messages, loading, error, send, reset }
|
||||
// 대화를 목록 맨 위로 이동 + 시각 갱신
|
||||
function bumpConversation(id: string): void {
|
||||
const idx = conversations.value.findIndex((c) => c.id === id)
|
||||
if (idx < 0) return
|
||||
const [c] = conversations.value.splice(idx, 1)
|
||||
if (!c) return
|
||||
c.updatedAt = new Date().toISOString()
|
||||
conversations.value.unshift(c)
|
||||
}
|
||||
|
||||
return {
|
||||
conversations,
|
||||
listLoading,
|
||||
currentId,
|
||||
messages,
|
||||
loading,
|
||||
error,
|
||||
loadConversations,
|
||||
newConversation,
|
||||
openConversation,
|
||||
send,
|
||||
removeConversation,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,10 +1,26 @@
|
||||
// AI 채팅 메시지 — 백엔드 ChatMessageDto 와 1:1
|
||||
// AI 채팅 메시지 — 백엔드 메시지와 1:1
|
||||
export interface ChatMessage {
|
||||
role: 'user' | 'assistant'
|
||||
content: string
|
||||
}
|
||||
|
||||
// AI 채팅 응답
|
||||
export interface ChatResult {
|
||||
// 대화 목록 항목
|
||||
export interface ConversationSummary {
|
||||
id: string
|
||||
title: string
|
||||
updatedAt: string
|
||||
}
|
||||
|
||||
// 대화 상세(메시지 포함)
|
||||
export interface ConversationDetail {
|
||||
id: string
|
||||
title: string
|
||||
messages: ChatMessage[]
|
||||
}
|
||||
|
||||
// 메시지 전송 결과(대화 생성/이어가기 공통)
|
||||
export interface SendResult {
|
||||
conversationId: string
|
||||
title: string
|
||||
reply: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user