Spaces:
Sleeping
Sleeping
/** | |
* RAG ๊ฒ์ ์ฑ๋ด ๋ฌธ์ ๊ด๋ฆฌ JavaScript | |
*/ | |
// DOM ์์ ๋ฏธ๋ฆฌ ์ ์ธ | |
let uploadForm, documentFile, fileName, uploadButton, uploadStatus; | |
let refreshDocsButton, docsList, docsLoading, noDocsMessage; | |
/** | |
* ๋ฌธ์ ๊ด๋ฆฌ DOM ์์ ์ด๊ธฐํ | |
*/ | |
function initDocsElements() { | |
console.log('๋ฌธ์ ๊ด๋ฆฌ DOM ์์ ์ด๊ธฐํ ์ค...'); | |
uploadForm = document.getElementById('uploadForm'); | |
documentFile = document.getElementById('documentFile'); | |
fileName = document.getElementById('fileName'); | |
uploadButton = document.getElementById('uploadButton'); | |
uploadStatus = document.getElementById('uploadStatus'); | |
refreshDocsButton = document.getElementById('refreshDocsButton'); | |
docsList = document.getElementById('docsList'); | |
docsLoading = document.getElementById('docsLoading'); | |
noDocsMessage = document.getElementById('noDocsMessage'); | |
console.log('๋ฌธ์ ๊ด๋ฆฌ DOM ์์ ์ด๊ธฐํ ์๋ฃ'); | |
// ๋ฌธ์ ๊ด๋ฆฌ ์ด๋ฒคํธ ๋ฆฌ์ค๋ ์ด๊ธฐํ | |
initDocsEventListeners(); | |
} | |
/** | |
* ๋ฌธ์ ๊ด๋ฆฌ ์ด๋ฒคํธ ๋ฆฌ์ค๋ ์ด๊ธฐํ | |
*/ | |
function initDocsEventListeners() { | |
console.log('๋ฌธ์ ๊ด๋ฆฌ ์ด๋ฒคํธ ๋ฆฌ์ค๋ ์ด๊ธฐํ ์ค...'); | |
// ๋ฌธ์ ์ ๋ก๋ ์ด๋ฒคํธ ๋ฆฌ์ค๋ | |
documentFile.addEventListener('change', (event) => { | |
if (event.target.files.length > 0) { | |
fileName.textContent = event.target.files[0].name; | |
console.log(`ํ์ผ ์ ํ๋จ: ${event.target.files[0].name}`); | |
} else { | |
fileName.textContent = '์ ํ๋ ํ์ผ ์์'; | |
console.log('ํ์ผ ์ ํ ์ทจ์๋จ'); | |
} | |
}); | |
uploadForm.addEventListener('submit', (event) => { | |
event.preventDefault(); | |
console.log('์ ๋ก๋ ํผ ์ ์ถ๋จ'); | |
uploadDocument(); | |
}); | |
// ๋ฌธ์ ๋ชฉ๋ก ์๋ก๊ณ ์นจ ์ด๋ฒคํธ ๋ฆฌ์ค๋ | |
refreshDocsButton.addEventListener('click', () => { | |
console.log('๋ฌธ์ ๋ชฉ๋ก ์๋ก๊ณ ์นจ ์์ฒญ'); | |
loadDocuments(); | |
}); | |
console.log('๋ฌธ์ ๊ด๋ฆฌ ์ด๋ฒคํธ ๋ฆฌ์ค๋ ์ด๊ธฐํ ์๋ฃ'); | |
} | |
/** | |
* ๋ฌธ์ ์ ๋ก๋ ํจ์ | |
*/ | |
async function uploadDocument() { | |
if (documentFile.files.length === 0) { | |
console.log('์ ๋ก๋ํ ํ์ผ์ด ์ ํ๋์ง ์์'); | |
alert('ํ์ผ์ ์ ํํด ์ฃผ์ธ์.'); | |
return; | |
} | |
console.log(`ํ์ผ ์ ๋ก๋ ์์: ${documentFile.files[0].name}`); | |
// UI ์ ๋ฐ์ดํธ | |
uploadStatus.classList.remove('hidden'); | |
uploadStatus.className = 'upload-status'; | |
uploadStatus.innerHTML = '<div class="spinner"></div><p>์ ๋ก๋ ์ค...</p>'; | |
uploadButton.disabled = true; | |
try { | |
const formData = new FormData(); | |
formData.append('document', documentFile.files[0]); | |
console.log('๋ฌธ์ ์ ๋ก๋ API ์์ฒญ ์ ์ก'); | |
// API ์์ฒญ | |
const response = await fetch('/api/upload', { | |
method: 'POST', | |
body: formData | |
}); | |
const data = await response.json(); | |
console.log('๋ฌธ์ ์ ๋ก๋ API ์๋ต ์์ '); | |
// ์๋ต ์ฒ๋ฆฌ | |
if (data.error) { | |
console.error('์ ๋ก๋ ์ค๋ฅ:', data.error); | |
uploadStatus.className = 'upload-status error'; | |
uploadStatus.textContent = `์ค๋ฅ: ${data.error}`; | |
} else if (data.warning) { | |
console.warn('์ ๋ก๋ ๊ฒฝ๊ณ :', data.message); | |
uploadStatus.className = 'upload-status warning'; | |
uploadStatus.textContent = data.message; | |
} else { | |
console.log('์ ๋ก๋ ์ฑ๊ณต:', data.message); | |
uploadStatus.className = 'upload-status success'; | |
uploadStatus.textContent = data.message; | |
// ๋ฌธ์ ๋ชฉ๋ก ์๋ก๊ณ ์นจ | |
loadDocuments(); | |
// ์ ๋ ฅ ํ๋ ์ด๊ธฐํ | |
documentFile.value = ''; | |
fileName.textContent = '์ ํ๋ ํ์ผ ์์'; | |
} | |
} catch (error) { | |
console.error('์ ๋ก๋ ์ฒ๋ฆฌ ์ค ์ค๋ฅ:', error); | |
uploadStatus.className = 'upload-status error'; | |
uploadStatus.textContent = '์ ๋ก๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. ๋ค์ ์๋ํด ์ฃผ์ธ์.'; | |
} finally { | |
uploadButton.disabled = false; | |
} | |
} | |
/** | |
* ๋ฌธ์ ๋ชฉ๋ก ๋ก๋ ํจ์ | |
*/ | |
async function loadDocuments() { | |
console.log('๋ฌธ์ ๋ชฉ๋ก ๋ก๋ ์์'); | |
// UI ์ ๋ฐ์ดํธ | |
docsList.querySelector('tbody').innerHTML = ''; | |
docsLoading.classList.remove('hidden'); | |
noDocsMessage.classList.add('hidden'); | |
try { | |
console.log('๋ฌธ์ ๋ชฉ๋ก API ์์ฒญ ์ ์ก'); | |
// API ์์ฒญ | |
const response = await fetch('/api/documents'); | |
if (!response.ok) { | |
throw new Error(`HTTP error! status: ${response.status}`); | |
} | |
const data = await response.json(); | |
console.log(`๋ฌธ์ ๋ชฉ๋ก API ์๋ต ์์ : ${data.documents ? data.documents.length : 0}๊ฐ ๋ฌธ์`); | |
// ์๋ต ์ฒ๋ฆฌ | |
docsLoading.classList.add('hidden'); | |
if (!data.documents || data.documents.length === 0) { | |
console.log('๋ก๋๋ ๋ฌธ์๊ฐ ์์'); | |
noDocsMessage.classList.remove('hidden'); | |
return; | |
} | |
// ๋ฌธ์ ๋ชฉ๋ก ์ ๋ฐ์ดํธ | |
const tbody = docsList.querySelector('tbody'); | |
data.documents.forEach(doc => { | |
console.log(`๋ฌธ์ ํ์: ${doc.filename || doc.source}`); | |
const row = document.createElement('tr'); | |
const fileNameCell = document.createElement('td'); | |
fileNameCell.textContent = doc.filename || doc.source; | |
row.appendChild(fileNameCell); | |
const chunksCell = document.createElement('td'); | |
chunksCell.textContent = doc.chunks; | |
row.appendChild(chunksCell); | |
const typeCell = document.createElement('td'); | |
typeCell.textContent = doc.filetype || '-'; | |
row.appendChild(typeCell); | |
tbody.appendChild(row); | |
}); | |
console.log('๋ฌธ์ ๋ชฉ๋ก ์ ๋ฐ์ดํธ ์๋ฃ'); | |
} catch (error) { | |
console.error('๋ฌธ์ ๋ชฉ๋ก ๋ก๋ ์ค๋ฅ:', error); | |
docsLoading.classList.add('hidden'); | |
noDocsMessage.classList.remove('hidden'); | |
noDocsMessage.querySelector('p').textContent = '๋ฌธ์ ๋ชฉ๋ก์ ๋ถ๋ฌ์ค๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.'; | |
} | |
} | |
// ํ์ด์ง ๋ก๋ ์ ๋ชจ๋ ์ด๊ธฐํ | |
document.addEventListener('DOMContentLoaded', function() { | |
console.log('๋ฌธ์ ๊ด๋ฆฌ ๋ชจ๋ ์ด๊ธฐํ'); | |
// ๋น๋๊ธฐ์ ์ผ๋ก ์ด๊ธฐํ (DOM ์์๊ฐ ์ค๋น๋ ํ) | |
setTimeout(() => { | |
initDocsElements(); | |
}, 100); | |
}); | |