feat: edit file content support

This commit is contained in:
Timothy J. Baek
2024-10-04 00:46:32 -07:00
parent 630a78cead
commit 8013c152d0
5 changed files with 147 additions and 6 deletions

View File

@@ -92,6 +92,40 @@ export const getFileById = async (token: string, id: string) => {
return res;
};
export const updateFileDataContentById = async (token: string, id: string, content: string) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/files/${id}/data/content/update`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
},
body: JSON.stringify({
content: content
})
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.then((json) => {
return json;
})
.catch((err) => {
error = err.detail;
console.log(err);
return null;
});
if (error) {
throw error;
}
return res;
};
export const getFileContentById = async (id: string) => {
let error = null;

View File

@@ -173,6 +173,41 @@ export const addFileToKnowledgeById = async (token: string, id: string, fileId:
return res;
};
export const updateFileFromKnowledgeById = async (token: string, id: string, fileId: string) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/${id}/file/update`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
},
body: JSON.stringify({
file_id: fileId
})
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.then((json) => {
return json;
})
.catch((err) => {
error = err.detail;
console.log(err);
return null;
});
if (error) {
throw error;
}
return res;
};
export const removeFileFromKnowledgeById = async (token: string, id: string, fileId: string) => {
let error = null;

View File

@@ -8,11 +8,12 @@
import { page } from '$app/stores';
import { mobile, showSidebar } from '$lib/stores';
import { uploadFile } from '$lib/apis/files';
import { updateFileDataContentById, uploadFile } from '$lib/apis/files';
import {
addFileToKnowledgeById,
getKnowledgeById,
removeFileFromKnowledgeById,
updateFileFromKnowledgeById,
updateKnowledgeById
} from '$lib/apis/knowledge';
@@ -135,6 +136,28 @@
}
};
const updateFileContentHandler = async () => {
const fileId = selectedFile.id;
const content = selectedFile.data.content;
const res = updateFileDataContentById(localStorage.token, fileId, content).catch((e) => {
toast.error(e);
});
const updatedKnowledge = await updateFileFromKnowledgeById(
localStorage.token,
id,
fileId
).catch((e) => {
toast.error(e);
});
if (res && updatedKnowledge) {
knowledge = updatedKnowledge;
toast.success($i18n.t('File content updated successfully.'));
}
};
const changeDebounceHandler = () => {
console.log('debounce');
if (debounceTimeout) {
@@ -420,6 +443,9 @@
<div>
<button
class="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"
on:click={() => {
updateFileContentHandler();
}}
>
{$i18n.t('Save')}
</button>