Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tdraier committed Aug 9, 2024
1 parent 9ede7b4 commit 63fd1ec
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 264 deletions.
103 changes: 99 additions & 4 deletions front/lib/api/vaults.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,111 @@
import type {
ConnectorsAPIError,
ContentNodesViewType,
ContentNodeType,
CoreAPIError,
ResourceCategory,
Result,
WithAPIErrorResponse,
} from "@dust-tt/types";
import { ConnectorsAPI, CoreAPI, Ok } from "@dust-tt/types";
import type { NextApiRequest, NextApiResponse } from "next";

import config from "@app/lib/api/config";
import type { DataSourceResource } from "@app/lib/resources/data_source_resource";
import logger from "@app/logger/logger";
import { apiError } from "@app/logger/withlogging";

export type LightContentNode = {
internalId: string;
parentInternalId: string | null;
type: ContentNodeType;
title: string;
expandable: boolean;
preventSelection?: boolean;
dustDocumentId: string | null;
lastUpdatedAt: number | null;
};

export type GetDataSourceContentResponseBody = {
nodes: LightContentNode[];
};

export const getDataSourceCategory = (
dataSource: DataSourceResource
): ResourceCategory => {
if (!dataSource.connectorProvider) {
return "files";
}

if (dataSource.connectorProvider === "webcrawler") {
return "webfolder";
}

return "managed";
};

export const getContentHandler = async (
req: NextApiRequest,
res: NextApiResponse<WithAPIErrorResponse<GetDataSourceContentResponseBody>>,
dataSource: DataSourceResource,
rootIds: string[] | null
): Promise<void> => {
const viewType = req.query.viewType;
if (
!viewType ||
typeof viewType !== "string" ||
(viewType !== "tables" && viewType !== "documents")
) {
return apiError(req, res, {
status_code: 400,
api_error: {
type: "invalid_request_error",
message: "Invalid viewType. Required: tables | documents",
},
});
}

let parentId: string | null = null;
if (req.query.parentId && typeof req.query.parentId === "string") {
parentId = req.query.parentId;
}

const limit = req.query.limit ? parseInt(req.query.limit as string) : 10;
const offset = req.query.offset ? parseInt(req.query.offset as string) : 0;

const content = dataSource.connectorId
? await getManagedDataSourceContent(
dataSource.connectorId,
"read",
rootIds,
parentId,
viewType
)
: await getUnmanagedDataSourceContent(dataSource, viewType, limit, offset);

if (content.isErr()) {
return apiError(req, res, {
status_code: 500,
api_error: {
type: "internal_server_error",
message: `An error occurred while retrieving the data source permissions.`,
},
});
}

res.status(200).json({
nodes: content.value,
});
return;
};

export const getManagedDataSourceContent = async (
connectorId: string,
permission: "read" | "write" | "read_write" | "none",
rootIds: string[] | null,
parentId: string | null,
viewType: "tables" | "documents"
) => {
viewType: ContentNodesViewType
): Promise<Result<LightContentNode[], ConnectorsAPIError>> => {
const connectorsAPI = new ConnectorsAPI(
config.getConnectorsAPIConfig(),
logger
Expand Down Expand Up @@ -58,10 +153,10 @@ export const getManagedDataSourceContent = async (

export const getUnmanagedDataSourceContent = async (
dataSource: DataSourceResource,
viewType: "tables" | "documents",
viewType: ContentNodesViewType,
limit: number,
offset: number
) => {
): Promise<Result<LightContentNode[], CoreAPIError>> => {
const coreAPI = new CoreAPI(config.getCoreAPIConfig(), logger);

if (viewType === "documents") {
Expand Down
10 changes: 3 additions & 7 deletions front/lib/resources/vault_resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ export class VaultResource extends BaseResource<VaultModel> {
return new this(VaultModel, vault.get());
}

static async fetchByName(
static async isNameAvailable(
auth: Authenticator,
name: string
): Promise<VaultResource | null> {
): Promise<boolean> {
const owner = auth.getNonNullableWorkspace();

const vault = await this.model.findOne({
Expand All @@ -186,11 +186,7 @@ export class VaultResource extends BaseResource<VaultModel> {
},
});

if (!vault) {
return null;
}

return new this(VaultModel, vault.get());
return !vault;
}

async delete(
Expand Down
82 changes: 82 additions & 0 deletions front/lib/swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ import type { GetLabsTranscriptsConfigurationResponseBody } from "@app/pages/api
import type { GetMembersResponseBody } from "@app/pages/api/w/[wId]/members";
import type { GetProvidersResponseBody } from "@app/pages/api/w/[wId]/providers";
import type { GetSubscriptionsResponseBody } from "@app/pages/api/w/[wId]/subscriptions";
import type { GetVaultsResponseBody } from "@app/pages/api/w/[wId]/vaults";
import type { GetVaultResponseBody } from "@app/pages/api/w/[wId]/vaults/[vId]";
import type { GetVaultDataSourceViewsResponseBody } from "@app/pages/api/w/[wId]/vaults/[vId]/data_source_views";
import type { GetVaultDataSourcesResponseBody } from "@app/pages/api/w/[wId]/vaults/[vId]/data_sources";
import type { GetWorkspaceAnalyticsResponse } from "@app/pages/api/w/[wId]/workspace-analytics";

const DEFAULT_SWR_CONFIG: SWRConfiguration = {
Expand Down Expand Up @@ -1261,3 +1265,81 @@ export function useLabsTranscriptsConfiguration({
mutateTranscriptsConfiguration: mutate,
};
}

export function useVaults({ workspaceId }: { workspaceId: string }) {
const vaultsFetcher: Fetcher<GetVaultsResponseBody> = fetcher;

const { data, error } = useSWRWithDefaults(
`/api/w/${workspaceId}/vaults`,
vaultsFetcher
);

return {
vaults: data ? data.vaults : null,
isVaultsLoading: !error && !data,
isVaultsError: error,
};
}

export function useVaultInfo({
workspaceId,
vaultId,
disabled,
}: {
workspaceId: string;
vaultId: string;
disabled?: boolean;
}) {
const vaultsCategoriesFetcher: Fetcher<GetVaultResponseBody> = fetcher;

const { data, error } = useSWRWithDefaults(
disabled ? null : `/api/w/${workspaceId}/vaults/${vaultId}`,
vaultsCategoriesFetcher
);

return {
vaultInfo: data ? data.vault : null,
isVaultInfoLoading: !error && !data,
isVaultInfoError: error,
};
}

export function useVaultDataSourceOrViews({
workspaceId,
vaultId,
category,
type,
disabled,
}: {
workspaceId: string;
vaultId: string;
category: string;
type: "data_sources" | "data_source_views";
disabled?: boolean;
}) {
const vaultsDataSourcesFetcher: Fetcher<
GetVaultDataSourcesResponseBody | GetVaultDataSourceViewsResponseBody
> = fetcher;

const { data, error } = useSWRWithDefaults(
disabled
? null
: `/api/w/${workspaceId}/vaults/${vaultId}/${type}?category=${category}`,
vaultsDataSourcesFetcher
);

console.log(data, error);
if (data && data.dataSources) {
return {
vaultDataSources: data ? data.dataSources : null,
isVaultDataSourcesLoading: !error && !data,
isVaultDataSourcesError: error,
};
} else {
return {
vaultDataSources: data ? data.dataSourceViews : null,
isVaultDataSourcesLoading: !error && !data,
isVaultDataSourcesError: error,
};
}
}
91 changes: 0 additions & 91 deletions front/pages/api/w/[wId]/vaults/[vId]/categories.ts

This file was deleted.

Loading

0 comments on commit 63fd1ec

Please sign in to comment.