mirror of
https://github.com/StanGirard/quivr.git
synced 2024-12-14 05:05:09 +03:00
51de056e5f
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
93 lines
2.4 KiB
TypeScript
93 lines
2.4 KiB
TypeScript
import { UUID } from "crypto";
|
|
import { useCallback } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { useCrawlApi } from "@/lib/api/crawl/useCrawlApi";
|
|
import { useUploadApi } from "@/lib/api/upload/useUploadApi";
|
|
import { getAxiosErrorParams } from "@/lib/helpers/getAxiosErrorParams";
|
|
import { useToast } from "@/lib/hooks";
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
export const useKnowledgeToFeedInput = () => {
|
|
const { publish } = useToast();
|
|
const { uploadFile } = useUploadApi();
|
|
const { t } = useTranslation(["upload"]);
|
|
const { crawlWebsiteUrl } = useCrawlApi();
|
|
|
|
const crawlWebsiteHandler = useCallback(
|
|
async (url: string, brainId: UUID, chat_id?: UUID) => {
|
|
// Configure parameters
|
|
const config = {
|
|
url: url,
|
|
js: false,
|
|
depth: 1,
|
|
max_pages: 100,
|
|
max_time: 60,
|
|
};
|
|
|
|
try {
|
|
await crawlWebsiteUrl({
|
|
brainId,
|
|
config,
|
|
chat_id,
|
|
});
|
|
} catch (error: unknown) {
|
|
const errorParams = getAxiosErrorParams(error);
|
|
if (errorParams !== undefined) {
|
|
publish({
|
|
variant: "danger",
|
|
text: t("crawlFailed", {
|
|
message: JSON.stringify(errorParams.message),
|
|
}),
|
|
});
|
|
} else {
|
|
publish({
|
|
variant: "danger",
|
|
text: t("crawlFailed", {
|
|
message: JSON.stringify(error),
|
|
}),
|
|
});
|
|
}
|
|
}
|
|
},
|
|
[crawlWebsiteUrl, publish, t]
|
|
);
|
|
|
|
const uploadFileHandler = useCallback(
|
|
async (file: File, brainId: UUID, chat_id?: UUID) => {
|
|
const formData = new FormData();
|
|
formData.append("uploadFile", file);
|
|
try {
|
|
await uploadFile({
|
|
brainId,
|
|
formData,
|
|
chat_id,
|
|
});
|
|
} catch (e: unknown) {
|
|
const errorParams = getAxiosErrorParams(e);
|
|
if (errorParams !== undefined) {
|
|
publish({
|
|
variant: "danger",
|
|
text: t("uploadFailed", {
|
|
message: JSON.stringify(errorParams.message),
|
|
}),
|
|
});
|
|
} else {
|
|
publish({
|
|
variant: "danger",
|
|
text: t("uploadFailed", {
|
|
message: JSON.stringify(e),
|
|
}),
|
|
});
|
|
}
|
|
}
|
|
},
|
|
[publish, t, uploadFile]
|
|
);
|
|
|
|
return {
|
|
crawlWebsiteHandler,
|
|
uploadFileHandler,
|
|
};
|
|
};
|