Skip to content

Commit

Permalink
fix: fix some bugs (#1201)
Browse files Browse the repository at this point in the history
* feat(brainSettings): add feed process instrcution to knowledge tab

* feat: prevent default brain auto fetch

* feat: prevent chat submision on submit button click

* feat: remove unrelevant toast

* feat: remove duplicated GA initialization

* feat: add brain name in notifications

* fix(test): update analytics import path

* refactor: move ChatsList utils to ChatsList directory

* fix(test): update chatlist tests
  • Loading branch information
mamadoudicko authored Sep 18, 2023
1 parent 8914c7c commit 1ec736b
Show file tree
Hide file tree
Showing 39 changed files with 222 additions and 255 deletions.
3 changes: 3 additions & 0 deletions backend/celery_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
@celery.task(name="process_file_and_notify")
def process_file_and_notify(
file_name: str,
file_original_name: str,
enable_summarization,
brain_id,
openai_api_key,
Expand Down Expand Up @@ -77,6 +78,7 @@ def process_file_and_notify(
enable_summarization=enable_summarization,
brain_id=brain_id,
openai_api_key=openai_api_key,
original_file_name=file_original_name,
)
)

Expand Down Expand Up @@ -129,6 +131,7 @@ def process_crawl_and_notify(
enable_summarization=enable_summarization,
brain_id=brain_id,
openai_api_key=openai_api_key,
original_file_name=crawl_website_url,
)
)
else:
Expand Down
9 changes: 4 additions & 5 deletions backend/routes/upload_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,21 @@
from auth import AuthBearer, get_current_user
from celery_worker import process_file_and_notify
from fastapi import APIRouter, Depends, Query, Request, UploadFile
from models import Brain, File, UserIdentity, UserUsage
from models import Brain, UserIdentity, UserUsage
from models.databases.supabase.notifications import (
CreateNotificationProperties,
NotificationUpdatableProperties,
)
from models.notifications import NotificationsStatusEnum
from repository.brain import get_brain_details
from repository.files.upload_file import upload_file_storage
from repository.notification.add_notification import add_notification
from repository.notification.update_notification import update_notification_by_id
from repository.user_identity import get_user_identity
from utils.file import convert_bytes, get_file_size

from routes.authorizations.brain_authorization import (
RoleEnum,
validate_brain_authorization,
)
from utils.file import convert_bytes, get_file_size
from utils.processors import filter_file

upload_router = APIRouter()

Expand Down Expand Up @@ -86,6 +84,7 @@ async def upload_file(
upload_file_storage(file_content, filename_with_brain_id)
process_file_and_notify.delay(
file_name=filename_with_brain_id,
file_original_name=uploadFile.filename,
enable_summarization=enable_summarization,
brain_id=brain_id,
openai_api_key=openai_api_key,
Expand Down
22 changes: 14 additions & 8 deletions backend/utils/processors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from models.brains import Brain
from models.files import File
from parsers.audio import process_audio
from parsers.code_python import process_python
from parsers.csv import process_csv
from parsers.docx import process_docx
from parsers.epub import process_epub
Expand All @@ -12,7 +13,7 @@
from parsers.powerpoint import process_powerpoint
from parsers.txt import process_txt
from parsers.xlsx import process_xlsx
from parsers.code_python import process_python
from repository.brain.get_brain_by_id import get_brain_by_id

file_processors = {
".txt": process_txt,
Expand Down Expand Up @@ -48,27 +49,32 @@ async def filter_file(
enable_summarization: bool,
brain_id,
openai_api_key,
original_file_name=None,
):
await file.compute_file_sha1()

print("file sha1", file.file_sha1)
file_exists = file.file_already_exists()
file_exists_in_brain = file.file_already_exists_in_brain(brain_id)
using_file_name = original_file_name or file.file.filename if file.file else ""

brain = get_brain_by_id(brain_id)
if brain is None:
raise Exception("It seems like you're uploading knowledge to an unknown brain.")

if file_exists_in_brain:
return create_response(
f"🤔 {file.file.filename} already exists in brain {brain_id}.", # pyright: ignore reportPrivateUsage=none
f"🤔 {using_file_name} already exists in brain {brain.name}.", # pyright: ignore reportPrivateUsage=none
"warning",
)
elif file.file_is_empty():
return create_response(
f"❌ {file.file.filename} is empty.", # pyright: ignore reportPrivateUsage=none
f"❌ {original_file_name} is empty.", # pyright: ignore reportPrivateUsage=none
"error", # pyright: ignore reportPrivateUsage=none
)
elif file_exists:
file.link_file_to_brain(brain=Brain(id=brain_id))
return create_response(
f"✅ {file.file.filename} has been uploaded to brain {brain_id}.", # pyright: ignore reportPrivateUsage=none
f"✅ {using_file_name} has been uploaded to brain {brain.name}.", # pyright: ignore reportPrivateUsage=none
"success",
)

Expand All @@ -81,18 +87,18 @@ async def filter_file(
user_openai_api_key=openai_api_key,
)
return create_response(
f"✅ {file.file.filename} has been uploaded to brain {brain_id}.", # pyright: ignore reportPrivateUsage=none
f"✅ {using_file_name} has been uploaded to brain {brain.name}.", # pyright: ignore reportPrivateUsage=none
"success",
)
except Exception as e:
# Add more specific exceptions as needed.
print(f"Error processing file: {e}")
return create_response(
f"⚠️ An error occurred while processing {file.file.filename}.", # pyright: ignore reportPrivateUsage=none
f"⚠️ An error occurred while processing {using_file_name}.", # pyright: ignore reportPrivateUsage=none
"error",
)

return create_response(
f"❌ {file.file.filename} is not supported.", # pyright: ignore reportPrivateUsage=none
f"❌ {using_file_name} is not supported.", # pyright: ignore reportPrivateUsage=none
"error",
)
2 changes: 1 addition & 1 deletion frontend/app/(auth)/login/__tests__/page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ vi.mock("@/lib/context/SupabaseProvider", () => ({
useSupabase: () => mockUseSupabase(),
}));

vi.mock("@/services/analytics/useEventTracking", () => ({
vi.mock("@/services/analytics/june/useEventTracking", () => ({
useEventTracking: () => ({ track: vi.fn() }),
}));

Expand Down
6 changes: 3 additions & 3 deletions frontend/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ const queryClient = new QueryClient();

// This wrapper is used to make effect calls at a high level in app rendering.
export const App = ({ children }: PropsWithChildren): JSX.Element => {
const { fetchAllBrains, fetchAndSetActiveBrain, fetchPublicPrompts } =
const { fetchAllBrains, fetchDefaultBrain, fetchPublicPrompts } =
useBrainContext();
const { session } = useSupabase();

usePageTracking();

useEffect(() => {
void fetchAllBrains();
void fetchAndSetActiveBrain();
void fetchDefaultBrain();
void fetchPublicPrompts();
}, [session?.user]);
}, [session?.user.id]);

return (
<QueryClientProvider client={queryClient}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use client";
import { UUID } from "crypto";
import { AnimatePresence, motion } from "framer-motion";
import Link from "next/link";
import { useTranslation } from "react-i18next";

import Button from "@/lib/components/ui/Button";
import Spinner from "@/lib/components/ui/Spinner";

import DocumentItem from "./DocumentItem";
Expand Down Expand Up @@ -45,9 +43,9 @@ export const KnowledgeTab = ({ brainId }: KnowledgeTabProps): JSX.Element => {
) : (
<div className="flex flex-col items-center justify-center mt-10 gap-1">
<p className="text-center">{t("empty", { ns: "explore" })}</p>
<Link href="/upload">
<Button>{t("uploadButton")}</Button>
</Link>
<p className="text-center">
{t("feed_brain_instructions", { ns: "explore" })}
</p>
</div>
)}
</motion.div>
Expand Down
19 changes: 12 additions & 7 deletions frontend/app/chat/[chatId]/components/ActionsBar/ActionsBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ export const ActionsBar = (): JSX.Element => {
hasPendingRequests,
setHasPendingRequests,
} = useActionBar();

const { addContent, contents, feedBrain, removeContent } = useKnowledgeUploader({
setHasPendingRequests,
setShouldDisplayUploadCard,
});

const { addContent, contents, feedBrain, removeContent } =
useKnowledgeUploader({
setHasPendingRequests,
setShouldDisplayUploadCard,
});

const { t } = useTranslation(["chat"]);

Expand All @@ -25,13 +26,17 @@ export const ActionsBar = (): JSX.Element => {
{hasPendingRequests && (
<div className="flex mt-1 flex-col md:flex-row w-full shadow-md dark:shadow-primary/25 hover:shadow-xl transition-shadow rounded-xl bg-white dark:bg-black border border-black/10 dark:border-white/25 p-2 md:p-6 pl-6">
<div className="flex flex-1 items-center mb-2 md:mb-0">
<span className="text-sm md:text-1xl">{t("filesUploading")}</span>
<span className="text-sm md:text-1xl">{t("feedingBrain")}</span>
</div>
<AiOutlineLoading3Quarters className="animate-spin text-2xl md:text-3xl self-center" />
</div>
)}

<div className={shouldDisplayUploadCard ? "h-full flex flex-col flex-auto" : ""}>
<div
className={
shouldDisplayUploadCard ? "h-full flex flex-col flex-auto" : ""
}
>
{shouldDisplayUploadCard && (
<div className="flex flex-1 overflow-y-scroll shadow-md dark:shadow-primary/25 hover:shadow-xl transition-shadow rounded-xl bg-white dark:bg-black border border-black/10 dark:border-white/25 p-4 md:p-6">
<KnowledgeToFeed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,7 @@ export const ConfigModal = ({ chatId }: { chatId?: string }): JSX.Element => {
setOpen={setIsConfigModalOpen}
CloseTrigger={<div />}
>
<form
onSubmit={(e) => {
void handleSubmit(e);
setIsConfigModalOpen(false);
}}
className="mt-10 flex flex-col items-center gap-2"
>
<form className="mt-10 flex flex-col items-center gap-2">
<fieldset className="w-full flex flex-col">
<label className="flex-1 text-sm" htmlFor="model">
Model
Expand Down Expand Up @@ -91,7 +85,14 @@ export const ConfigModal = ({ chatId }: { chatId?: string }): JSX.Element => {
/>
</fieldset>

<Button className="mt-12 self-end" type="submit">
<Button
className="mt-12 self-end"
type="button"
onClick={() => {
handleSubmit();
setIsConfigModalOpen(false);
}}
>
Save
<MdCheck className="text-xl" />
</Button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable max-lines */
import { useQuery } from "@tanstack/react-query";
import { FormEvent, useEffect, useState } from "react";
import { useEffect, useState } from "react";
import { useForm } from "react-hook-form";

import { useBrainApi } from "@/lib/api/brain/useBrainApi";
Expand Down Expand Up @@ -91,8 +91,7 @@ export const useConfigModal = (chatId?: string) => {
setValue("maxTokens", Math.min(maxTokens, defineMaxTokens(model)));
}, [maxTokens, model, setValue]);

const handleSubmit = (e: FormEvent) => {
e.preventDefault();
const handleSubmit = () => {
if (chatId === undefined) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,6 @@ export const useKnowledgeUploader = ({
} else {
await fetchNotifications(currentChatId);
}

publish({
variant: "success",
text: t("knowledgeUploaded"),
});
} catch (e) {
publish({
variant: "danger",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,18 @@ vi.mock("next/navigation", async () => {
vi.mock("@/lib/context/ChatsProvider/hooks/useChatsContext", () => ({
useChatsContext: () => ({
allChats: [
{ chat_id: 1, name: "Chat 1" },
{ chat_id: 2, name: "Chat 2" },
{
chat_id: 1,
name: "Chat 1",
creation_time: new Date().toISOString(),
},
{
chat_id: 2,
name: "Chat 2",
creation_time: new Date().toISOString(),
},
],

deleteChat: vi.fn(),
setAllChats: vi.fn(),
}),
Expand Down
Loading

0 comments on commit 1ec736b

Please sign in to comment.