quivr/frontend/lib/api/brain/brain.ts
Antoine Dewez 7ff9abee1a
feat(frontend): onboarding V2 (#2394)
# Description

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context.

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):

---------

Co-authored-by: Stan Girard <girard.stanislas@gmail.com>
2024-04-09 18:06:33 +02:00

150 lines
4.0 KiB
TypeScript

/* eslint-disable max-lines */
import { AxiosInstance } from "axios";
import { BrainRoleType } from "@/app/studio/[brainId]/BrainManagementTabs/components/PeopleTab/BrainUsers/types";
import {
BackendMinimalBrainForUser,
Brain,
MinimalBrainForUser,
PublicBrain,
} from "@/lib/context/BrainProvider/types";
import {
CreateBrainInput,
IntegrationBrains,
ListFilesProps,
SubscriptionUpdatableProperties,
UpdateBrainInput,
} from "./types";
import { mapBackendMinimalBrainToMinimalBrain } from "./utils/mapBackendMinimalBrainToMinimalBrain";
import {
BackendSubscription,
mapSubscriptionToBackendSubscription,
} from "./utils/mapSubscriptionToBackendSubscription";
import { mapSubscriptionUpdatablePropertiesToBackendSubscriptionUpdatableProperties } from "./utils/mapSubscriptionUpdatablePropertiesToBackendSubscriptionUpdatableProperties";
export const createBrain = async (
brain: CreateBrainInput,
axiosInstance: AxiosInstance
): Promise<MinimalBrainForUser> => {
return mapBackendMinimalBrainToMinimalBrain(
(await axiosInstance.post<BackendMinimalBrainForUser>(`/brains/`, brain))
.data
);
};
export const getBrain = async (
brainId: string,
axiosInstance: AxiosInstance
): Promise<Brain | undefined> => {
const brain = (
await axiosInstance.get<Brain | undefined>(`/brains/${brainId}/`)
).data;
return brain;
};
export const deleteBrain = async (
brainId: string,
axiosInstance: AxiosInstance
): Promise<void> => {
await axiosInstance.delete(`/brains/${brainId}/subscription`);
};
export const getBrains = async (
axiosInstance: AxiosInstance
): Promise<MinimalBrainForUser[]> => {
const { brains } = (
await axiosInstance.get<{ brains: BackendMinimalBrainForUser[] }>(
`/brains/`
)
).data;
return brains.map(mapBackendMinimalBrainToMinimalBrain);
};
export type Subscription = { email: string; role: BrainRoleType };
export const addBrainSubscriptions = async (
brainId: string,
subscriptions: Subscription[],
axiosInstance: AxiosInstance
): Promise<void> => {
await axiosInstance.post(
`/brains/${brainId}/subscription`,
subscriptions.map(mapSubscriptionToBackendSubscription)
);
};
export const getBrainUsers = async (
brainId: string,
axiosInstance: AxiosInstance
): Promise<Subscription[]> => {
const brainsUsers = (
await axiosInstance.get<BackendSubscription[]>(`/brains/${brainId}/users`)
).data;
return brainsUsers.map((brainUser) => ({
email: brainUser.email,
role: brainUser.rights,
}));
};
export const updateBrainAccess = async (
brainId: string,
userEmail: string,
subscription: SubscriptionUpdatableProperties,
axiosInstance: AxiosInstance
): Promise<void> => {
await axiosInstance.put(`/brains/${brainId}/subscription`, {
...mapSubscriptionUpdatablePropertiesToBackendSubscriptionUpdatableProperties(
subscription
),
email: userEmail,
});
};
export const updateBrain = async (
brainId: string,
brain: UpdateBrainInput,
axiosInstance: AxiosInstance
): Promise<void> => {
await axiosInstance.put(`/brains/${brainId}/`, brain);
};
export const getPublicBrains = async (
axiosInstance: AxiosInstance
): Promise<PublicBrain[]> => {
return (await axiosInstance.get<PublicBrain[]>(`/brains/public`)).data;
};
export const updateBrainSecrets = async (
brainId: string,
secrets: Record<string, string>,
axiosInstance: AxiosInstance
): Promise<void> => {
await axiosInstance.put(`/brains/${brainId}/secrets-values`, secrets);
};
export const getDocsFromQuestion = async (
brainId: string,
question: string,
axiosInstance: AxiosInstance
): Promise<ListFilesProps["files"]> => {
return (
await axiosInstance.post<Record<"docs", ListFilesProps["files"]>>(
`/brains/${brainId}/documents`,
{
question,
}
)
).data.docs;
};
export const getIntegrationBrains = async (
axiosInstance: AxiosInstance
): Promise<IntegrationBrains[]> => {
return (await axiosInstance.get<IntegrationBrains[]>(`/brains/integrations/`))
.data;
};