Skip to content

Commit

Permalink
Upg: memoize feature flags for 3 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraggle committed Nov 26, 2024
1 parent 7888a24 commit bda5a38
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
15 changes: 7 additions & 8 deletions front/lib/api/assistant/jit_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import {
} from "@app/lib/api/assistant/utils";
import type { Authenticator } from "@app/lib/auth";
import { getFeatureFlags } from "@app/lib/auth";
import { canEnableJIT } from "@app/lib/development";
import { renderLightContentFragmentForModel } from "@app/lib/resources/content_fragment_resource";
import { DataSourceViewResource } from "@app/lib/resources/data_source_view_resource";
import { generateRandomModelSId } from "@app/lib/resources/string_ids";
Expand All @@ -59,14 +58,14 @@ export async function isJITActionsEnabled(
auth: Authenticator
): Promise<boolean> {
let use = false;
if (canEnableJIT(auth.getNonNullableWorkspace())) {
// For now we limit the feature flag to development and dust workspace only to not introduce an extraneous DB call
// on the critical path of conversations.
const flags = await getFeatureFlags(auth.getNonNullableWorkspace());
if (flags.includes("conversations_jit_actions")) {
use = true;
}

// For now we limit the feature flag to development and dust workspace only to not introduce an extraneous DB call
// on the critical path of conversations.
const flags = await getFeatureFlags(auth.getNonNullableWorkspace());
if (flags.includes("conversations_jit_actions")) {
use = true;
}

return use;
}

Expand Down
31 changes: 19 additions & 12 deletions front/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
Ok,
WHITELISTABLE_FEATURES,
} from "@dust-tt/types";
import memoizer from "lru-memoizer";
import type {
GetServerSidePropsContext,
NextApiRequest,
Expand Down Expand Up @@ -1006,15 +1007,21 @@ export async function prodAPICredentialsForOwner(
};
}

export const getFeatureFlags = async (
workspace: WorkspaceType
): Promise<WhitelistableFeature[]> => {
if (ACTIVATE_ALL_FEATURES_DEV && isDevelopment()) {
return [...WHITELISTABLE_FEATURES];
} else {
const res = await FeatureFlag.findAll({
where: { workspaceId: workspace.id },
});
return res.map((flag) => flag.name);
}
};
export const getFeatureFlags = memoizer.sync({
load: async (workspace: WorkspaceType): Promise<WhitelistableFeature[]> => {
if (ACTIVATE_ALL_FEATURES_DEV && isDevelopment()) {
return [...WHITELISTABLE_FEATURES];
} else {
const res = await FeatureFlag.findAll({
where: { workspaceId: workspace.id },
});
return res.map((flag) => flag.name);
}
},

hash: function (workspace: WorkspaceType) {
return `feature_flags_${workspace.id}`;
},

itemMaxAge: () => 3000,
});
4 changes: 0 additions & 4 deletions front/lib/development.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ export function canForceUserRole(owner: LightWorkspaceType) {
return isDevelopment() || isADustProdWorkspace(owner);
}

export function canEnableJIT(owner: LightWorkspaceType) {
return isDevelopment() || isADustProdWorkspace(owner);
}

export async function forceUserRole(
user: UserType,
owner: LightWorkspaceType,
Expand Down
3 changes: 2 additions & 1 deletion front/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions front/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"lodash": "^4.17.21",
"lucide-react": "^0.363.0",
"luxon": "^3.4.4",
"lru-memoizer": "^2.2.0",
"marked": "^14.1.3",
"mermaid": "^10.9.0",
"minimist": "^1.2.8",
Expand Down

0 comments on commit bda5a38

Please sign in to comment.