refac
This commit is contained in:
@@ -330,6 +330,48 @@ export const processYoutubeVideo = async (token: string, url: string) => {
|
||||
return res;
|
||||
};
|
||||
|
||||
export const processUrl = async (
|
||||
token: string,
|
||||
url: string,
|
||||
collection_name: string | null = null,
|
||||
process: boolean = true
|
||||
) => {
|
||||
let error = null;
|
||||
|
||||
const searchParams = new URLSearchParams();
|
||||
if (!process) {
|
||||
searchParams.append('process', 'false');
|
||||
}
|
||||
|
||||
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/process/url?${searchParams.toString()}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
authorization: `Bearer ${token}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
url,
|
||||
collection_name
|
||||
})
|
||||
})
|
||||
.then(async (res) => {
|
||||
if (!res.ok) throw await res.json();
|
||||
return res.json();
|
||||
})
|
||||
.catch((err) => {
|
||||
error = err.detail;
|
||||
console.error(err);
|
||||
return null;
|
||||
});
|
||||
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
return res;
|
||||
};
|
||||
|
||||
export const processWeb = async (
|
||||
token: string,
|
||||
collection_name: string,
|
||||
|
||||
@@ -62,7 +62,6 @@
|
||||
processDetails,
|
||||
removeAllDetails,
|
||||
getCodeBlockContents,
|
||||
isYoutubeUrl,
|
||||
displayFileHandler
|
||||
} from '$lib/utils';
|
||||
import { AudioQueue } from '$lib/utils/audio';
|
||||
@@ -82,7 +81,7 @@
|
||||
updateChatFolderIdById
|
||||
} from '$lib/apis/chats';
|
||||
import { generateOpenAIChatCompletion } from '$lib/apis/openai';
|
||||
import { processWeb, processWebSearch, processYoutubeVideo } from '$lib/apis/retrieval';
|
||||
import { processUrl, processWebSearch } from '$lib/apis/retrieval';
|
||||
import { getAndUpdateUserLocation, getUserSettings } from '$lib/apis/users';
|
||||
import {
|
||||
generateQueries,
|
||||
@@ -1560,24 +1559,49 @@
|
||||
|
||||
for (const fileItem of fileItems) {
|
||||
try {
|
||||
const res = isYoutubeUrl(fileItem.url)
|
||||
? await processYoutubeVideo(localStorage.token, fileItem.url)
|
||||
: await processWeb(localStorage.token, '', fileItem.url);
|
||||
const res = await processUrl(localStorage.token, fileItem.url);
|
||||
|
||||
if (res) {
|
||||
const uploadedFile = res.file;
|
||||
fileItem.status = 'uploaded';
|
||||
fileItem.name = res.name ?? fileItem.name;
|
||||
fileItem.collection_name = res.collection_name;
|
||||
fileItem.file = {
|
||||
...res.file,
|
||||
...fileItem.file
|
||||
};
|
||||
|
||||
if (res.type === 'image' && uploadedFile) {
|
||||
fileItem.type = 'image';
|
||||
fileItem.file = uploadedFile;
|
||||
fileItem.id = uploadedFile.id;
|
||||
fileItem.url = `${uploadedFile.id}`;
|
||||
fileItem.content_type = uploadedFile.meta?.content_type;
|
||||
fileItem.size = uploadedFile.meta?.size;
|
||||
} else if (res.type === 'file' && uploadedFile) {
|
||||
fileItem.type = 'file';
|
||||
fileItem.file = uploadedFile;
|
||||
fileItem.id = uploadedFile.id;
|
||||
fileItem.url = `${uploadedFile.id}`;
|
||||
fileItem.content_type = uploadedFile.meta?.content_type;
|
||||
fileItem.size = uploadedFile.meta?.size;
|
||||
fileItem.collection_name =
|
||||
res.collection_name ?? uploadedFile.meta?.collection_name ?? uploadedFile.collection_name;
|
||||
} else {
|
||||
fileItem.type = 'text';
|
||||
fileItem.file = {
|
||||
data: {
|
||||
content: res.content
|
||||
},
|
||||
meta: {
|
||||
name: res.name ?? fileItem.name,
|
||||
source: res.url ?? fileItem.url
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
files = [...files];
|
||||
} catch (e) {
|
||||
fileItem.status = 'error';
|
||||
fileItem.error = `${e}`;
|
||||
files = files.filter((f) => f.name !== url);
|
||||
files = files.filter((f) => f.name !== fileItem.name);
|
||||
toast.error(`${e}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,9 +46,9 @@
|
||||
syncKnowledgeCleanup,
|
||||
testExternalKnowledgeRetrieval
|
||||
} from '$lib/apis/knowledge';
|
||||
import { processWeb, processYoutubeVideo } from '$lib/apis/retrieval';
|
||||
import { processUrl } from '$lib/apis/retrieval';
|
||||
|
||||
import { blobToFile, isYoutubeUrl, copyToClipboard } from '$lib/utils';
|
||||
import { blobToFile, copyToClipboard } from '$lib/utils';
|
||||
import { computeFileHash } from '$lib/utils/hash';
|
||||
|
||||
import Spinner from '$lib/components/common/Spinner.svelte';
|
||||
@@ -307,6 +307,11 @@
|
||||
};
|
||||
|
||||
const uploadWeb = async (urls) => {
|
||||
if (!knowledge) {
|
||||
toast.error($i18n.t('Knowledge base not found.'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Array.isArray(urls)) {
|
||||
urls = [urls];
|
||||
}
|
||||
@@ -329,29 +334,47 @@
|
||||
for (const fileItem of newFileItems) {
|
||||
try {
|
||||
console.log(fileItem);
|
||||
const res = await processWeb(localStorage.token, '', fileItem.url, false).catch((e) => {
|
||||
console.error('Error processing web URL:', e);
|
||||
const res = await processUrl(localStorage.token, fileItem.url).catch((e) => {
|
||||
console.error('Error processing URL:', e);
|
||||
return null;
|
||||
});
|
||||
|
||||
if (res) {
|
||||
console.log(res);
|
||||
const file = createFileFromText(
|
||||
// Use URL as filename, sanitized
|
||||
fileItem.url
|
||||
.replace(/[^a-z0-9]/gi, '_')
|
||||
.toLowerCase()
|
||||
.slice(0, 50),
|
||||
res.content
|
||||
);
|
||||
let uploadedFile = res.file;
|
||||
|
||||
const uploadedFile = await uploadFile(localStorage.token, file, {
|
||||
knowledge_id: knowledge.id,
|
||||
directory_id: currentDirectoryId
|
||||
}).catch((e) => {
|
||||
toast.error(`${e}`);
|
||||
return null;
|
||||
});
|
||||
if (res.type === 'web' || res.type === 'youtube') {
|
||||
const file = createFileFromText(
|
||||
// Use URL as filename, sanitized
|
||||
fileItem.url
|
||||
.replace(/[^a-z0-9]/gi, '_')
|
||||
.toLowerCase()
|
||||
.slice(0, 50),
|
||||
res.content ?? ''
|
||||
);
|
||||
|
||||
uploadedFile = await uploadFile(localStorage.token, file, {
|
||||
knowledge_id: knowledge.id,
|
||||
directory_id: currentDirectoryId,
|
||||
source_url: fileItem.url
|
||||
}).catch((e) => {
|
||||
toast.error(`${e}`);
|
||||
return null;
|
||||
});
|
||||
} else if (uploadedFile?.id) {
|
||||
const linkedKnowledge = await addFileToKnowledgeById(
|
||||
localStorage.token,
|
||||
knowledge.id,
|
||||
uploadedFile.id,
|
||||
currentDirectoryId
|
||||
).catch((e) => {
|
||||
toast.error(`${e}`);
|
||||
return null;
|
||||
});
|
||||
if (!linkedKnowledge) {
|
||||
uploadedFile = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (uploadedFile) {
|
||||
console.log(uploadedFile);
|
||||
|
||||
Reference in New Issue
Block a user