Skip to content

Commit

Permalink
Update api
Browse files Browse the repository at this point in the history
  • Loading branch information
tdraier committed Aug 11, 2024
1 parent 63fd1ec commit 67068e1
Show file tree
Hide file tree
Showing 12 changed files with 590 additions and 157 deletions.
48 changes: 47 additions & 1 deletion front/lib/api/vaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import type {
ContentNodeType,
CoreAPIError,
ResourceCategory,
ResourceInfo,
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 type { Authenticator } from "@app/lib/auth";
import { DataSourceResource } from "@app/lib/resources/data_source_resource";
import { DataSourceViewResource } from "@app/lib/resources/data_source_view_resource";
import type { VaultResource } from "@app/lib/resources/vault_resource";
import logger from "@app/logger/logger";
import { apiError } from "@app/logger/withlogging";

Expand All @@ -30,6 +34,48 @@ export type GetDataSourceContentResponseBody = {
nodes: LightContentNode[];
};

export const getDataSourceInfo = (
dataSource: DataSourceResource
): ResourceInfo => {
return {
...dataSource.toJSON(),
sId: dataSource.name,
usage: 0,
category: getDataSourceCategory(dataSource),
};
};

export const getDataSourceInfos = async (
auth: Authenticator,
vault: VaultResource
): Promise<ResourceInfo[]> => {
const dataSources = await DataSourceResource.listByVault(auth, vault);

return dataSources.map((dataSource) => getDataSourceInfo(dataSource));
};

export const getDataSourceViewInfo = (
dataSourceView: DataSourceViewResource
): ResourceInfo => {
return {
...(dataSourceView.dataSource as DataSourceResource).toJSON(),
...dataSourceView.toJSON(),
usage: 0,
category: getDataSourceCategory(
dataSourceView.dataSource as DataSourceResource
),
};
};

export const getDataSourceViewsInfo = async (
auth: Authenticator,
vault: VaultResource
): Promise<ResourceInfo[]> => {
const dataSourceViews = await DataSourceViewResource.listByVault(auth, vault);

return dataSourceViews.map((view) => getDataSourceViewInfo(view));
};

export const getDataSourceCategory = (
dataSource: DataSourceResource
): ResourceCategory => {
Expand Down
22 changes: 14 additions & 8 deletions front/lib/resources/data_source_view_resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,30 @@ export class DataSourceViewResource extends ResourceWithVault<DataSourceViewMode
constructor(
model: ModelStatic<DataSourceViewModel>,
blob: Attributes<DataSourceViewModel>,
vault: VaultResource
vault: VaultResource,
dataSource: DataSourceResource
) {
super(DataSourceViewModel, blob, vault);
this.ds = dataSource;
}

// Creation.

private static async makeNew(
blob: Omit<CreationAttributes<DataSourceViewModel>, "vaultId">,
vault: VaultResource
vault: VaultResource,
dataSource: DataSourceResource
) {
const key = await DataSourceViewResource.model.create({
...blob,
vaultId: vault.id,
});

return new this(DataSourceViewResource.model, key.get(), vault);
return new this(DataSourceViewResource.model, key.get(), vault, dataSource);
}

static async createViewInVaultFromDataSource(
vault: VaultResource,
dataSource: DataSourceType,
dataSource: DataSourceResource,
parentsIn: string[]
) {
return this.makeNew(
Expand All @@ -70,7 +72,8 @@ export class DataSourceViewResource extends ResourceWithVault<DataSourceViewMode
parentsIn,
workspaceId: vault.workspaceId,
},
vault
vault,
dataSource
);
}

Expand All @@ -86,7 +89,8 @@ export class DataSourceViewResource extends ResourceWithVault<DataSourceViewMode
parentsIn: null,
workspaceId: vault.workspaceId,
},
vault
vault,
dataSource
);
}

Expand Down Expand Up @@ -171,7 +175,7 @@ export class DataSourceViewResource extends ResourceWithVault<DataSourceViewMode
parentsIn: string[]
): Promise<Result<undefined, Error>> {
try {
await this.model.update(
const [, affectedRows] = await this.model.update(
{
parentsIn,
},
Expand All @@ -180,8 +184,10 @@ export class DataSourceViewResource extends ResourceWithVault<DataSourceViewMode
workspaceId: auth.getNonNullableWorkspace().id,
id: this.id,
},
returning: true,
}
);
Object.assign(this, affectedRows[0].get());

return new Ok(undefined);
} catch (err) {
Expand Down
Loading

0 comments on commit 67068e1

Please sign in to comment.