-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: finalise steps based brain creation (#1825)
Issue: https://github.com/users/StanGirard/projects/5/views/2?pane=issue&itemId=46291978 - Finalise steps based brain creation - Remove feature flag - Demo: <img width="1068" alt="Screenshot 2023-12-05 at 18 05 52" src="https://github.com/StanGirard/quivr/assets/63923024/99e38cab-d510-4bb5-8153-b0db406d1650"> https://github.com/StanGirard/quivr/assets/63923024/1850e2bd-5df1-43fe-be9e-261a3b90af2b https://github.com/StanGirard/quivr/assets/63923024/c7335679-b090-40ac-aece-fbaae0303d51
- Loading branch information
1 parent
5bbc72e
commit 51de056
Showing
34 changed files
with
336 additions
and
541 deletions.
There are no files selected for viewing
5 changes: 2 additions & 3 deletions
5
...abs/components/KnowledgeOrSecretsTab/components/AddKnowledge/hooks/useFeedBrainHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
frontend/lib/components/AddBrainModal/AddBrainModalSelector.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...dBrainModal/components/AddBrainSteps/components/BrainKnowledgeStep/BrainKnowledgeStep.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Fragment } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { FaArrowLeft } from "react-icons/fa"; | ||
|
||
import { ApiRequestDefinition } from "@/lib/components/ApiRequestDefinition"; | ||
import Button from "@/lib/components/ui/Button"; | ||
import { BrainType } from "@/lib/types/brainConfig"; | ||
|
||
import { KnowledgeToFeedInput } from "./components/KnowledgeToFeedInput"; | ||
import { useBrainCreationHandler } from "./hooks/useBrainCreationHandler"; | ||
import { useBrainKnowledgeStep } from "./hooks/useBrainKnowledgeStep"; | ||
import { useBrainCreationSteps } from "../../hooks/useBrainCreationSteps"; | ||
|
||
type BrainKnowledgeStepProps = { | ||
onCancelBrainCreation: () => void; | ||
}; | ||
|
||
export const BrainKnowledgeStep = ({ | ||
onCancelBrainCreation, | ||
}: BrainKnowledgeStepProps): JSX.Element => { | ||
const { brainType, isSubmitButtonDisabled } = useBrainKnowledgeStep(); | ||
const { t } = useTranslation(["translation"]); | ||
const { goToPreviousStep, currentStep } = useBrainCreationSteps(); | ||
const { handleCreateBrain, isBrainCreationPending } = useBrainCreationHandler( | ||
{ | ||
closeBrainCreationModal: onCancelBrainCreation, | ||
} | ||
); | ||
|
||
const brainTypeToKnowledgeComponent: Record<BrainType, JSX.Element> = { | ||
doc: <KnowledgeToFeedInput />, | ||
api: <ApiRequestDefinition />, | ||
chatflow: <div>Coming soon</div>, | ||
}; | ||
|
||
if (currentStep !== "KNOWLEDGE" || brainType === undefined) { | ||
return <Fragment />; | ||
} | ||
|
||
return ( | ||
<> | ||
{brainTypeToKnowledgeComponent[brainType]} | ||
<div className="flex flex-row justify-between items-center flex-1 mt-10 w-full"> | ||
<Button | ||
type="button" | ||
variant="tertiary" | ||
onClick={onCancelBrainCreation} | ||
className="text-primary" | ||
disabled={isBrainCreationPending} | ||
> | ||
{t("cancel", { ns: "translation" })} | ||
</Button> | ||
<div className="flex gap-4"> | ||
<Button | ||
type="button" | ||
variant="secondary" | ||
onClick={goToPreviousStep} | ||
className="py-2 border-primary text-primary" | ||
disabled={isBrainCreationPending} | ||
> | ||
<FaArrowLeft className="text-xl" size={16} /> | ||
{t("previous", { ns: "translation" })} | ||
</Button> | ||
|
||
<Button | ||
className="bg-primary text-white py-2 border-none" | ||
type="button" | ||
onClick={() => void handleCreateBrain()} | ||
disabled={isSubmitButtonDisabled || isBrainCreationPending} | ||
isLoading={isBrainCreationPending} | ||
> | ||
{t("createButton", { ns: "translation" })} | ||
</Button> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
}; |
26 changes: 26 additions & 0 deletions
26
...omponents/AddBrainSteps/components/BrainKnowledgeStep/components/KnowledgeToFeedInput.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { useTranslation } from "react-i18next"; | ||
|
||
import { | ||
Crawler, | ||
FeedItems, | ||
FileUploader, | ||
} from "@/lib/components/KnowledgeToFeedInput/components"; | ||
|
||
export const KnowledgeToFeedInput = (): JSX.Element => { | ||
const { t } = useTranslation(["translation", "upload"]); | ||
|
||
return ( | ||
<div> | ||
<div className="flex flex-row gap-10 justify-between items-center mt-5"> | ||
<FileUploader /> | ||
<span className="whitespace-nowrap "> | ||
{`${t("and", { ns: "translation" })} / ${t("or", { | ||
ns: "translation", | ||
})}`} | ||
</span> | ||
<Crawler /> | ||
</div> | ||
<FeedItems /> | ||
</div> | ||
); | ||
}; |
101 changes: 101 additions & 0 deletions
101
...Modal/components/AddBrainSteps/components/BrainKnowledgeStep/hooks/useBrainCreationApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
import { UUID } from "crypto"; | ||
import { useFormContext } from "react-hook-form"; | ||
import { useTranslation } from "react-i18next"; | ||
|
||
import { PUBLIC_BRAINS_KEY } from "@/lib/api/brain/config"; | ||
import { useBrainApi } from "@/lib/api/brain/useBrainApi"; | ||
import { CreateBrainProps } from "@/lib/components/AddBrainModal/types"; | ||
import { useKnowledgeToFeedInput } from "@/lib/components/KnowledgeToFeedInput/hooks/useKnowledgeToFeedInput.ts"; | ||
import { useBrainContext } from "@/lib/context/BrainProvider/hooks/useBrainContext"; | ||
import { useKnowledgeToFeedContext } from "@/lib/context/KnowledgeToFeedProvider/hooks/useKnowledgeToFeedContext"; | ||
import { useToast } from "@/lib/hooks"; | ||
import { useKnowledgeToFeedFilesAndUrls } from "@/lib/hooks/useKnowledgeToFeed"; | ||
|
||
type UseBrainCreationHandler = { | ||
closeBrainCreationModal: () => void; | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
export const useBrainCreationApi = ({ | ||
closeBrainCreationModal, | ||
}: UseBrainCreationHandler) => { | ||
const queryClient = useQueryClient(); | ||
const { publish } = useToast(); | ||
const { t } = useTranslation(["brain", "config"]); | ||
const { files, urls } = useKnowledgeToFeedFilesAndUrls(); | ||
const { getValues, reset } = useFormContext<CreateBrainProps>(); | ||
const { setKnowledgeToFeed } = useKnowledgeToFeedContext(); | ||
const { createBrain: createBrainApi, setCurrentBrainId } = useBrainContext(); | ||
const { setAsDefaultBrain } = useBrainApi(); | ||
const { crawlWebsiteHandler, uploadFileHandler } = useKnowledgeToFeedInput(); | ||
|
||
const handleFeedBrain = async (brainId: UUID): Promise<void> => { | ||
const uploadPromises = files.map((file) => | ||
uploadFileHandler(file, brainId) | ||
); | ||
const crawlPromises = urls.map((url) => crawlWebsiteHandler(url, brainId)); | ||
|
||
await Promise.all([...uploadPromises, ...crawlPromises]); | ||
setKnowledgeToFeed([]); | ||
}; | ||
|
||
const createBrain = async (): Promise<void> => { | ||
const { | ||
name, | ||
description, | ||
setDefault, | ||
brain_definition, | ||
brain_secrets_values, | ||
status, | ||
brain_type, | ||
} = getValues(); | ||
|
||
const createdBrainId = await createBrainApi({ | ||
name, | ||
description, | ||
status, | ||
brain_type, | ||
brain_definition: brain_type === "api" ? brain_definition : undefined, | ||
brain_secrets_values: | ||
brain_type === "api" ? brain_secrets_values : undefined, | ||
}); | ||
|
||
if (createdBrainId === undefined) { | ||
publish({ | ||
variant: "danger", | ||
text: t("errorCreatingBrain", { ns: "brain" }), | ||
}); | ||
|
||
return; | ||
} | ||
if (brain_type === "doc") { | ||
void handleFeedBrain(createdBrainId); | ||
} | ||
|
||
if (setDefault) { | ||
await setAsDefaultBrain(createdBrainId); | ||
} | ||
setCurrentBrainId(createdBrainId); | ||
closeBrainCreationModal(); | ||
reset(); | ||
void queryClient.invalidateQueries({ | ||
queryKey: [PUBLIC_BRAINS_KEY], | ||
}); | ||
}; | ||
|
||
const { mutate, isPending: isBrainCreationPending } = useMutation({ | ||
mutationFn: createBrain, | ||
onSuccess: () => { | ||
publish({ | ||
variant: "success", | ||
text: t("brainCreated", { ns: "brain" }), | ||
}); | ||
}, | ||
}); | ||
|
||
return { | ||
createBrain: mutate, | ||
isBrainCreationPending, | ||
}; | ||
}; |
52 changes: 52 additions & 0 deletions
52
...l/components/AddBrainSteps/components/BrainKnowledgeStep/hooks/useBrainCreationHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useFormContext } from "react-hook-form"; | ||
import { useTranslation } from "react-i18next"; | ||
|
||
import { CreateBrainProps } from "@/lib/components/AddBrainModal/types"; | ||
import { useToast } from "@/lib/hooks"; | ||
|
||
import { useBrainCreationApi } from "./useBrainCreationApi"; | ||
|
||
type UseBrainCreationHandler = { | ||
closeBrainCreationModal: () => void; | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
export const useBrainCreationHandler = ({ | ||
closeBrainCreationModal, | ||
}: UseBrainCreationHandler) => { | ||
const { getValues } = useFormContext<CreateBrainProps>(); | ||
const { publish } = useToast(); | ||
const { t } = useTranslation(["brain", "config"]); | ||
|
||
const { isBrainCreationPending, createBrain } = useBrainCreationApi({ | ||
closeBrainCreationModal, | ||
}); | ||
|
||
const handleCreateBrain = () => { | ||
const { name, description } = getValues(); | ||
|
||
if (name.trim() === "" || isBrainCreationPending) { | ||
publish({ | ||
variant: "danger", | ||
text: t("nameRequired", { ns: "config" }), | ||
}); | ||
|
||
return; | ||
} | ||
|
||
if (description.trim() === "") { | ||
publish({ | ||
variant: "danger", | ||
text: t("descriptionRequired", { ns: "config" }), | ||
}); | ||
|
||
return; | ||
} | ||
createBrain(); | ||
}; | ||
|
||
return { | ||
handleCreateBrain, | ||
isBrainCreationPending, | ||
}; | ||
}; |
22 changes: 22 additions & 0 deletions
22
...dal/components/AddBrainSteps/components/BrainKnowledgeStep/hooks/useBrainKnowledgeStep.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useFormContext } from "react-hook-form"; | ||
|
||
import { CreateBrainProps } from "@/lib/components/AddBrainModal/types"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
export const useBrainKnowledgeStep = () => { | ||
const { watch } = useFormContext<CreateBrainProps>(); | ||
const brainType = watch("brain_type"); | ||
const url = watch("brain_definition.url"); | ||
const isApiBrain = brainType === "api"; | ||
const isChatflowBrain = brainType === "chatflow"; | ||
|
||
const isApiBrainDefinitionsFilled = url !== ""; | ||
|
||
const isSubmitButtonDisabled = | ||
isChatflowBrain || (isApiBrain && !isApiBrainDefinitionsFilled); | ||
|
||
return { | ||
brainType, | ||
isSubmitButtonDisabled, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.