mirror of
https://github.com/open-webui/open-webui.git
synced 2025-12-16 11:57:51 +01:00
enh/refac: temp chat file upload behaviour
client-side content extraction
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
<script lang="ts">
|
||||
import * as pdfjs from 'pdfjs-dist';
|
||||
import * as pdfWorker from 'pdfjs-dist/build/pdf.worker.mjs';
|
||||
pdfjs.GlobalWorkerOptions.workerSrc = import.meta.url + 'pdfjs-dist/build/pdf.worker.mjs';
|
||||
|
||||
import DOMPurify from 'dompurify';
|
||||
import { marked } from 'marked';
|
||||
import heic2any from 'heic2any';
|
||||
@@ -23,13 +27,15 @@
|
||||
tools,
|
||||
user as _user,
|
||||
showControls,
|
||||
TTSWorker
|
||||
TTSWorker,
|
||||
temporaryChatEnabled
|
||||
} from '$lib/stores';
|
||||
|
||||
import {
|
||||
blobToFile,
|
||||
compressImage,
|
||||
createMessagesList,
|
||||
extractContentFromFile,
|
||||
extractCurlyBraceWords,
|
||||
extractInputVariables,
|
||||
getCurrentDateTime,
|
||||
@@ -529,47 +535,77 @@
|
||||
|
||||
files = [...files, fileItem];
|
||||
|
||||
try {
|
||||
// If the file is an audio file, provide the language for STT.
|
||||
let metadata = null;
|
||||
if (
|
||||
(file.type.startsWith('audio/') || file.type.startsWith('video/')) &&
|
||||
$settings?.audio?.stt?.language
|
||||
) {
|
||||
metadata = {
|
||||
language: $settings?.audio?.stt?.language
|
||||
};
|
||||
}
|
||||
|
||||
// During the file upload, file content is automatically extracted.
|
||||
const uploadedFile = await uploadFile(localStorage.token, file, metadata);
|
||||
|
||||
if (uploadedFile) {
|
||||
console.log('File upload completed:', {
|
||||
id: uploadedFile.id,
|
||||
name: fileItem.name,
|
||||
collection: uploadedFile?.meta?.collection_name
|
||||
});
|
||||
|
||||
if (uploadedFile.error) {
|
||||
console.warn('File upload warning:', uploadedFile.error);
|
||||
toast.warning(uploadedFile.error);
|
||||
if (!$temporaryChatEnabled) {
|
||||
try {
|
||||
// If the file is an audio file, provide the language for STT.
|
||||
let metadata = null;
|
||||
if (
|
||||
(file.type.startsWith('audio/') || file.type.startsWith('video/')) &&
|
||||
$settings?.audio?.stt?.language
|
||||
) {
|
||||
metadata = {
|
||||
language: $settings?.audio?.stt?.language
|
||||
};
|
||||
}
|
||||
|
||||
fileItem.status = 'uploaded';
|
||||
fileItem.file = uploadedFile;
|
||||
fileItem.id = uploadedFile.id;
|
||||
fileItem.collection_name =
|
||||
uploadedFile?.meta?.collection_name || uploadedFile?.collection_name;
|
||||
fileItem.url = `${WEBUI_API_BASE_URL}/files/${uploadedFile.id}`;
|
||||
// During the file upload, file content is automatically extracted.
|
||||
const uploadedFile = await uploadFile(localStorage.token, file, metadata);
|
||||
|
||||
files = files;
|
||||
} else {
|
||||
if (uploadedFile) {
|
||||
console.log('File upload completed:', {
|
||||
id: uploadedFile.id,
|
||||
name: fileItem.name,
|
||||
collection: uploadedFile?.meta?.collection_name
|
||||
});
|
||||
|
||||
if (uploadedFile.error) {
|
||||
console.warn('File upload warning:', uploadedFile.error);
|
||||
toast.warning(uploadedFile.error);
|
||||
}
|
||||
|
||||
fileItem.status = 'uploaded';
|
||||
fileItem.file = uploadedFile;
|
||||
fileItem.id = uploadedFile.id;
|
||||
fileItem.collection_name =
|
||||
uploadedFile?.meta?.collection_name || uploadedFile?.collection_name;
|
||||
fileItem.url = `${WEBUI_API_BASE_URL}/files/${uploadedFile.id}`;
|
||||
|
||||
files = files;
|
||||
} else {
|
||||
files = files.filter((item) => item?.itemId !== tempItemId);
|
||||
}
|
||||
} catch (e) {
|
||||
toast.error(`${e}`);
|
||||
files = files.filter((item) => item?.itemId !== tempItemId);
|
||||
}
|
||||
} catch (e) {
|
||||
toast.error(`${e}`);
|
||||
files = files.filter((item) => item?.itemId !== tempItemId);
|
||||
} else {
|
||||
// If temporary chat is enabled, we just add the file to the list without uploading it.
|
||||
|
||||
const content = await extractContentFromFile(file, pdfjsLib).catch((error) => {
|
||||
toast.error(
|
||||
$i18n.t('Failed to extract content from the file: {{error}}', { error: error })
|
||||
);
|
||||
return null;
|
||||
});
|
||||
|
||||
if (content === null) {
|
||||
toast.error($i18n.t('Failed to extract content from the file.'));
|
||||
files = files.filter((item) => item?.itemId !== tempItemId);
|
||||
return null;
|
||||
} else {
|
||||
console.log('Extracted content from file:', {
|
||||
name: file.name,
|
||||
size: file.size,
|
||||
content: content
|
||||
});
|
||||
|
||||
fileItem.status = 'uploaded';
|
||||
fileItem.type = 'text';
|
||||
fileItem.content = content;
|
||||
fileItem.id = uuidv4(); // Temporary ID for the file
|
||||
|
||||
files = files;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user