This commit is contained in:
Timothy Jaeryang Baek committed 2026-06-29 01:41:43 -05:00
1 parent 3730a9eaac
commit 46c1d6591b
2 files changed
+63 -17

No files matched your search

+22 -8
View File
@@ -31,6 +31,7 @@ from sqlalchemy import (
update, update,
) )
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import defer
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@@ -416,6 +417,7 @@ class KnowledgeTable:
if limit: if limit:
stmt = stmt.limit(limit) stmt = stmt.limit(limit)
stmt = stmt.options(defer(File.data))
result = await db.execute(stmt) result = await db.execute(stmt)
rows = result.all() rows = result.all()
@@ -423,7 +425,13 @@ class KnowledgeTable:
for file, user, knowledge in rows: for file, user, knowledge in rows:
items.append( items.append(
FileUserResponse( FileUserResponse(
**FileModel.model_validate(file).model_dump(), id=file.id,
user_id=file.user_id,
hash=file.hash,
filename=file.filename,
meta=file.meta,
created_at=file.created_at,
updated_at=file.updated_at,
user=(UserResponse(**UserModel.model_validate(user).model_dump()) if user else None), user=(UserResponse(**UserModel.model_validate(user).model_dump()) if user else None),
collection=(await self._to_knowledge_model(knowledge, db=db)).model_dump(), collection=(await self._to_knowledge_model(knowledge, db=db)).model_dump(),
) )
@@ -603,17 +611,23 @@ class KnowledgeTable:
if limit: if limit:
stmt = stmt.limit(limit) stmt = stmt.limit(limit)
stmt = stmt.options(defer(File.data))
result = await db.execute(stmt) result = await db.execute(stmt)
items = result.all() items = result.all()
files = [] files = [
for file, user in items: FileUserResponse(
files.append( id=file.id,
FileUserResponse( user_id=file.user_id,
**FileModel.model_validate(file).model_dump(), hash=file.hash,
user=(UserResponse(**UserModel.model_validate(user).model_dump()) if user else None), filename=file.filename,
) meta=file.meta,
created_at=file.created_at,
updated_at=file.updated_at,
user=(UserResponse(**UserModel.model_validate(user).model_dump()) if user else None),
) )
for file, user in items
]
return KnowledgeFileListResponse( return KnowledgeFileListResponse(
items=files, items=files,
@@ -113,6 +113,7 @@
let selectedFileId = null; let selectedFileId = null;
let selectedFile = null; let selectedFile = null;
let selectedFileContent = ''; let selectedFileContent = '';
let loadingFileContent = false;
let inputFiles = null; let inputFiles = null;
@@ -249,11 +250,29 @@
}; };
const fileSelectHandler = async (file) => { const fileSelectHandler = async (file) => {
selectedFile = file;
selectedFileContent = file?.data?.content ?? '';
loadingFileContent = false;
if (!file?.id || file?.data?.content !== undefined) {
return;
}
loadingFileContent = true;
try { try {
selectedFile = file; const fileWithContent = await getFileById(localStorage.token, file.id);
selectedFileContent = selectedFile?.data?.content || ''; if (selectedFileId === file.id) {
selectedFile = fileWithContent ?? file;
selectedFileContent = fileWithContent?.data?.content ?? '';
}
} catch (e) { } catch (e) {
toast.error($i18n.t('Failed to load file content.')); if (selectedFileId === file.id) {
toast.error($i18n.t('Failed to load file content.'));
}
} finally {
if (selectedFileId === file.id) {
loadingFileContent = false;
}
} }
}; };
@@ -825,6 +844,8 @@
currentPage = 1; currentPage = 1;
selectedFileId = null; selectedFileId = null;
selectedFile = null; selectedFile = null;
selectedFileContent = '';
loadingFileContent = false;
getItemsPage(); getItemsPage();
}; };
@@ -953,8 +974,7 @@
let isSaving = false; let isSaving = false;
const updateFileContentHandler = async () => { const updateFileContentHandler = async () => {
if (isSaving) { if (isSaving || loadingFileContent || !selectedFile?.id) {
console.log('Save operation already in progress, skipping...');
return; return;
} }
@@ -1414,6 +1434,9 @@
placeholder={$i18n.t('Search Collection')} placeholder={$i18n.t('Search Collection')}
on:focus={() => { on:focus={() => {
selectedFileId = null; selectedFileId = null;
selectedFile = null;
selectedFileContent = '';
loadingFileContent = false;
}} }}
/> />
@@ -1578,13 +1601,18 @@
if (file) { if (file) {
fileSelectHandler(file); fileSelectHandler(file);
} else { } else {
selectedFileId = null;
selectedFile = null; selectedFile = null;
selectedFileContent = '';
loadingFileContent = false;
} }
} }
}} }}
onDelete={(fileId) => { onDelete={(fileId) => {
selectedFileId = null; selectedFileId = null;
selectedFile = null; selectedFile = null;
selectedFileContent = '';
loadingFileContent = false;
deleteFileHandler(fileId); deleteFileHandler(fileId);
}} }}
@@ -1622,6 +1650,8 @@
onClose={() => { onClose={() => {
selectedFileId = null; selectedFileId = null;
selectedFile = null; selectedFile = null;
selectedFileContent = '';
loadingFileContent = false;
}} }}
> >
<div class="flex flex-col justify-start h-full max-h-full"> <div class="flex flex-col justify-start h-full max-h-full">
@@ -1634,6 +1664,8 @@
on:click={() => { on:click={() => {
selectedFileId = null; selectedFileId = null;
selectedFile = null; selectedFile = null;
selectedFileContent = '';
loadingFileContent = false;
}} }}
> >
<ChevronLeft strokeWidth="2.5" /> <ChevronLeft strokeWidth="2.5" />
@@ -1647,7 +1679,7 @@
<div> <div>
<button <button
class="flex self-center w-fit text-sm py-1 px-2.5 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-lg disabled:opacity-50 disabled:cursor-not-allowed" class="flex self-center w-fit text-sm py-1 px-2.5 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-lg disabled:opacity-50 disabled:cursor-not-allowed"
disabled={isSaving} disabled={isSaving || loadingFileContent}
on:click={() => { on:click={() => {
updateFileContentHandler(); updateFileContentHandler();
}} }}
@@ -1663,14 +1695,14 @@
{/if} {/if}
</div> </div>
{#key selectedFile.id} {#key selectedFile?.id}
<textarea <textarea
class="w-full h-full text-sm outline-none resize-none px-3 py-2" class="w-full h-full text-sm outline-none resize-none px-3 py-2"
bind:value={selectedFileContent} bind:value={selectedFileContent}
disabled={!knowledge?.write_access} disabled={!knowledge?.write_access || loadingFileContent}
aria-label={$i18n.t('File content')} aria-label={$i18n.t('File content')}
placeholder={$i18n.t('Add content here')} placeholder={$i18n.t('Add content here')}
/> ></textarea>
{/key} {/key}
</div> </div>
</div> </div>