From dee5671311f82f366d2bcd3af45dc54f17f53b97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daphn=C3=A9=20Popin?= Date: Wed, 25 Oct 2023 09:40:46 +0200 Subject: [PATCH] Take plan out of WorkspaceType and access it from auth.plan() (#2244) --- .../assistant_builder/AssistantBuilder.tsx | 14 ++++---- front/lib/api/assistant/agent.ts | 5 ++- front/lib/api/assistant/configuration.ts | 18 +++++----- front/lib/api/assistant/global_agents.ts | 12 +++++-- front/lib/api/workspace.ts | 3 +- front/lib/auth.ts | 33 ++++++++++++++----- .../20230919_workspace_upgraded_at.ts | 2 +- .../api/poke/workspaces/[wId]/downgrade.ts | 9 +---- .../api/poke/workspaces/[wId]/upgrade.ts | 8 +---- front/pages/api/poke/workspaces/index.ts | 11 ++----- .../[name]/documents/[documentId]/index.ts | 13 ++++---- .../[name]/documents/[documentId]/index.ts | 13 ++++---- front/pages/api/w/[wId]/data_sources/index.ts | 7 ++-- .../pages/api/w/[wId]/data_sources/managed.ts | 5 +-- front/pages/poke/[wId]/index.tsx | 10 ++++-- .../[wId]/builder/assistants/[aId]/index.tsx | 9 +++-- .../pages/w/[wId]/builder/assistants/new.tsx | 10 ++++-- .../builder/data-sources/[name]/index.tsx | 16 ++++++--- .../builder/data-sources/[name]/upsert.tsx | 18 ++++++---- .../w/[wId]/builder/data-sources/managed.tsx | 5 +-- .../w/[wId]/builder/data-sources/static.tsx | 12 ++++--- front/types/user.ts | 1 - 22 files changed, 138 insertions(+), 96 deletions(-) diff --git a/front/components/assistant_builder/AssistantBuilder.tsx b/front/components/assistant_builder/AssistantBuilder.tsx index d8c83c3a646e..73963f842c73 100644 --- a/front/components/assistant_builder/AssistantBuilder.tsx +++ b/front/components/assistant_builder/AssistantBuilder.tsx @@ -56,7 +56,7 @@ import { PostOrPatchAgentConfigurationRequestBodySchema } from "@app/pages/api/w import { AppType } from "@app/types/app"; import { TimeframeUnit } from "@app/types/assistant/actions/retrieval"; import { DataSourceType } from "@app/types/data_source"; -import { UserType, WorkspaceType } from "@app/types/user"; +import { PlanType, UserType, WorkspaceType } from "@app/types/user"; import DataSourceResourceSelectorTree from "../DataSourceResourceSelectorTree"; import AssistantBuilderDustAppModal from "./AssistantBuilderDustAppModal"; @@ -159,6 +159,7 @@ export type AssistantBuilderInitialState = { type AssistantBuilderProps = { user: UserType; owner: WorkspaceType; + plan: PlanType; gaTrackingId: string; dataSources: DataSourceType[]; dustApps: AppType[]; @@ -203,6 +204,7 @@ const getCreativityLevelFromTemperature = (temperature: number) => { export default function AssistantBuilder({ user, owner, + plan, gaTrackingId, dataSources, dustApps, @@ -219,7 +221,7 @@ export default function AssistantBuilder({ ...DEFAULT_ASSISTANT_STATE, generationSettings: { ...DEFAULT_ASSISTANT_STATE.generationSettings, - modelSettings: owner.plan.limits.largeModels + modelSettings: plan.limits.largeModels ? GPT_4_32K_MODEL_CONFIG : GPT_3_5_TURBO_16K_MODEL_CONFIG, }, @@ -844,7 +846,7 @@ export default function AssistantBuilder({ /> { setEdited(true); @@ -1338,11 +1340,11 @@ function AssistantBuilderTextArea({ } function AdvancedSettings({ - owner, + plan, generationSettings, setGenerationSettings, }: { - owner: WorkspaceType; + plan: PlanType; generationSettings: AssistantBuilderState["generationSettings"]; setGenerationSettings: ( generationSettingsSettings: AssistantBuilderState["generationSettings"] @@ -1382,7 +1384,7 @@ function AdvancedSettings({ {usedModelConfigs .filter( (modelConfig) => - !modelConfig.largeModel || owner.plan.limits.largeModels + !modelConfig.largeModel || plan.limits.largeModels ) .map((modelConfig) => ( = owner.plan.limits.dataSources.documents.count + plan.limits.dataSources.documents.count != -1 && + documents.value.total >= plan.limits.dataSources.documents.count ) { return apiError(req, res, { status_code: 401, @@ -261,9 +262,9 @@ async function handler( // Enforce plan limits: DataSource document size. if ( - owner.plan.limits.dataSources.documents.sizeMb != -1 && + plan.limits.dataSources.documents.sizeMb != -1 && req.body.text.length > - 1024 * 1024 * owner.plan.limits.dataSources.documents.sizeMb + 1024 * 1024 * plan.limits.dataSources.documents.sizeMb ) { return apiError(req, res, { status_code: 401, diff --git a/front/pages/api/w/[wId]/data_sources/[name]/documents/[documentId]/index.ts b/front/pages/api/w/[wId]/data_sources/[name]/documents/[documentId]/index.ts index 6933f5791e9f..281947a7ae63 100644 --- a/front/pages/api/w/[wId]/data_sources/[name]/documents/[documentId]/index.ts +++ b/front/pages/api/w/[wId]/data_sources/[name]/documents/[documentId]/index.ts @@ -24,7 +24,8 @@ async function handler( ); const owner = auth.workspace(); - if (!owner) { + const plan = auth.plan(); + if (!owner || !plan) { return apiError(req, res, { status_code: 404, api_error: { @@ -143,7 +144,7 @@ async function handler( // We only load the number of documents if the limit is not -1 (unlimited). // the `getDataSourceDocuments` query involves a SELECT COUNT(*) in the DB that is not // optimized, so we avoid it for large workspaces if we know we're unlimited anyway - if (owner.plan.limits.dataSources.documents.count != -1) { + if (plan.limits.dataSources.documents.count != -1) { const documents = await CoreAPI.getDataSourceDocuments({ projectId: dataSource.dustAPIProjectId, dataSourceName: dataSource.name, @@ -162,8 +163,8 @@ async function handler( } if ( - owner.plan.limits.dataSources.documents.count != -1 && - documents.value.total >= owner.plan.limits.dataSources.documents.count + plan.limits.dataSources.documents.count != -1 && + documents.value.total >= plan.limits.dataSources.documents.count ) { return apiError(req, res, { status_code: 401, @@ -178,9 +179,9 @@ async function handler( // Enforce plan limits: DataSource document size. if ( - owner.plan.limits.dataSources.documents.sizeMb != -1 && + plan.limits.dataSources.documents.sizeMb != -1 && req.body.text.length > - 1024 * 1024 * owner.plan.limits.dataSources.documents.sizeMb + 1024 * 1024 * plan.limits.dataSources.documents.sizeMb ) { return apiError(req, res, { status_code: 401, diff --git a/front/pages/api/w/[wId]/data_sources/index.ts b/front/pages/api/w/[wId]/data_sources/index.ts index dae60c3d933b..6176b39869c5 100644 --- a/front/pages/api/w/[wId]/data_sources/index.ts +++ b/front/pages/api/w/[wId]/data_sources/index.ts @@ -32,7 +32,8 @@ async function handler( ); const owner = auth.workspace(); - if (!owner) { + const plan = auth.plan(); + if (!owner || !plan) { return apiError(req, res, { status_code: 404, api_error: { @@ -90,8 +91,8 @@ async function handler( // Enforce plan limits: DataSources count. if ( - owner.plan.limits.dataSources.count != -1 && - dataSources.length >= owner.plan.limits.dataSources.count + plan.limits.dataSources.count != -1 && + dataSources.length >= plan.limits.dataSources.count ) { return apiError(req, res, { status_code: 401, diff --git a/front/pages/api/w/[wId]/data_sources/managed.ts b/front/pages/api/w/[wId]/data_sources/managed.ts index e62a7eb90df9..11cb71ecaa64 100644 --- a/front/pages/api/w/[wId]/data_sources/managed.ts +++ b/front/pages/api/w/[wId]/data_sources/managed.ts @@ -31,7 +31,8 @@ async function handler( ); const owner = auth.workspace(); - if (!owner) { + const plan = auth.plan(); + if (!owner || !plan) { return apiError(req, res, { status_code: 404, api_error: { @@ -118,7 +119,7 @@ async function handler( const dataSourceMaxChunkSize = 256; // Enforce plan limits: managed DataSources. - if (!owner.plan.limits.dataSources.managed) { + if (!plan.limits.dataSources.managed) { return apiError(req, res, { status_code: 401, api_error: { diff --git a/front/pages/poke/[wId]/index.tsx b/front/pages/poke/[wId]/index.tsx index 2d20250a2165..72df97425397 100644 --- a/front/pages/poke/[wId]/index.tsx +++ b/front/pages/poke/[wId]/index.tsx @@ -12,11 +12,12 @@ import { ConnectorsAPI } from "@app/lib/connectors_api"; import { CoreAPI } from "@app/lib/core_api"; import { timeAgoFrom } from "@app/lib/utils"; import { DataSourceType } from "@app/types/data_source"; -import { UserType, WorkspaceType } from "@app/types/user"; +import { PlanType, UserType, WorkspaceType } from "@app/types/user"; export const getServerSideProps: GetServerSideProps<{ user: UserType; workspace: WorkspaceType; + plan: PlanType; dataSources: DataSourceType[]; slackbotEnabled?: boolean; documentCounts: Record; @@ -50,8 +51,9 @@ export const getServerSideProps: GetServerSideProps<{ const auth = await Authenticator.fromSuperUserSession(session, wId); const workspace = auth.workspace(); + const plan = auth.plan(); - if (!workspace) { + if (!workspace || !plan) { return { notFound: true, }; @@ -128,6 +130,7 @@ export const getServerSideProps: GetServerSideProps<{ props: { user, workspace, + plan, dataSources, slackbotEnabled, documentCounts: docCountByDsName, @@ -138,6 +141,7 @@ export const getServerSideProps: GetServerSideProps<{ const WorkspacePage = ({ workspace, + plan, dataSources, slackbotEnabled, documentCounts, @@ -279,7 +283,7 @@ const WorkspacePage = ({ This workspace is not upgraded.

)} - +