quivr/frontend/lib/api/knowledge/knowledge.ts
Zineb El Bachiri d2b4ef4aff
feat: knowledge tab list (#1222)
*  get all knowledge utils

*  add remove knowledge and update router

*  new knowledge provider

* 🚨 remove eslint -disable

*  new useKnowledgeApi

*  set up KnowledgeItem

*  add KnowledgeTable component in knowledge tab

* 🔥 remove DocumentData replaced by KnowledgeItem

* 🐛 fix weird characters instead of '/'

* 💄 truncate knowledge name

*  add DownloadUploadedKnowledge component

* ⚰️ unused code

* 🏷️ introduce UploadedKnowledge and CrawledKnowledge types

* 💄 remove thread

* 💄 bin for delete knowledge

* 🌐 update wording for knowledge tab

* 🔇 remove logs and comments
2023-09-22 16:06:04 +02:00

69 lines
1.7 KiB
TypeScript

import { AxiosInstance } from "axios";
import { UUID } from "crypto";
import { Knowledge } from "@/lib/types/Knowledge";
export type GetAllKnowledgeInputProps = {
brainId: UUID;
};
interface BEKnowledge {
id: UUID;
brain_id: UUID;
file_name: string | null;
url: string | null;
extension: string;
}
export const getAllKnowledge = async (
{ brainId }: GetAllKnowledgeInputProps,
axiosInstance: AxiosInstance
): Promise<Knowledge[]> => {
const response = await axiosInstance.get<{
knowledges: BEKnowledge[];
}>(`/knowledge?brain_id=${brainId}`);
return response.data.knowledges.map((knowledge) => {
if (knowledge.file_name !== null) {
return {
id: knowledge.id,
brainId: knowledge.brain_id,
fileName: knowledge.file_name,
extension: knowledge.extension,
};
} else if (knowledge.url !== null) {
return {
id: knowledge.id,
brainId: knowledge.brain_id,
url: knowledge.url,
extension: "URL",
};
} else {
throw new Error(`Invalid knowledge ${knowledge.id}`);
}
});
};
export type DeleteKnowledgeInputProps = {
brainId: UUID;
knowledgeId: UUID;
};
export const deleteKnowledge = async (
{ knowledgeId, brainId }: DeleteKnowledgeInputProps,
axiosInstance: AxiosInstance
): Promise<void> => {
await axiosInstance.delete(`/knowledge/${knowledgeId}?brain_id=${brainId}`);
};
export const generateSignedUrlKnowledge = async (
{ knowledgeId }: { knowledgeId: UUID },
axiosInstance: AxiosInstance
): Promise<string> => {
const response = await axiosInstance.get<{
signedURL: string;
}>(`/knowledge/${knowledgeId}/signed_download_url`);
return response.data.signedURL;
};