refactor(assistant): resolve availability through the existing endpoint
Drops the feature-flags module, the /api/features endpoint and its test in favour of calling api.assistant.getAvailability() from the root layout, the same way the layout already calls api.user.getById(). The gate only needs one server-resolved boolean, and getAvailability already computes exactly that. The separate helper duplicated the enabled / modelId / api-key-required rule, so the two copies could drift. A general feature-flag surface is still worth having, but it belongs at boot time across every optional feature rather than as a per-request database read introduced alongside the assistant.
This commit is contained in:
5 files changed
+9
-128
No files matched your search
@@ -14,7 +14,6 @@ import type { DayOfWeek } from "@mantine/dates";
|
||||
import { NextIntlClientProvider } from "next-intl";
|
||||
|
||||
import { api } from "@homarr/api/server";
|
||||
import { getFeatureFlagsAsync } from "@homarr/api/features";
|
||||
import { env } from "@homarr/auth/env";
|
||||
import { auth } from "@homarr/auth/next";
|
||||
import { db } from "@homarr/db";
|
||||
@@ -87,7 +86,14 @@ export default async function Layout(props: {
|
||||
const session = await auth();
|
||||
const user = session ? await api.user.getById({ userId: session.user.id }).catch(() => null) : null;
|
||||
const serverSettings = await getServerSettingsAsync(db);
|
||||
const features = await getFeatureFlagsAsync(db);
|
||||
// Resolved on the server so the assistant runtime chunk is never referenced by a page when the
|
||||
// feature is off. Reuses the same availability check the assistant management page invalidates.
|
||||
const assistantEnabled = session
|
||||
? await api.assistant
|
||||
.getAvailability()
|
||||
.then((availability) => availability.enabled)
|
||||
.catch(() => false)
|
||||
: false;
|
||||
const colorScheme = await getCurrentColorSchemeAsync();
|
||||
const direction = isLocaleRTL((await props.params).locale) ? "rtl" : "ltr";
|
||||
|
||||
@@ -126,7 +132,7 @@ export default async function Layout(props: {
|
||||
(innerProps) => <CustomMantineProvider {...innerProps} defaultColorScheme={colorScheme} />,
|
||||
(innerProps) => <ModalProvider {...innerProps} />,
|
||||
(innerProps) => <SpotlightProvider {...innerProps} />,
|
||||
(innerProps) => <AssistantGate enabled={features.assistant} {...innerProps} />,
|
||||
(innerProps) => <AssistantGate enabled={assistantEnabled} {...innerProps} />,
|
||||
]);
|
||||
|
||||
const { locale } = await props.params;
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
import { getFeatureFlagsAsync } from "@homarr/api/features";
|
||||
import { auth } from "@homarr/auth/next";
|
||||
import { db } from "@homarr/db";
|
||||
|
||||
/**
|
||||
* Reports which optional features are switched on for this instance.
|
||||
*
|
||||
* Kept deliberately small: booleans only, no configuration detail, so it stays cheap to call and
|
||||
* safe to hand to the client. It is the single place to look when deciding whether a feature's
|
||||
* client code needs to be loaded at all.
|
||||
*
|
||||
* Requires a session because whether an instance runs an AI assistant is not something an
|
||||
* anonymous visitor needs to know.
|
||||
*/
|
||||
export async function GET() {
|
||||
const session = await auth();
|
||||
if (!session) {
|
||||
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const features = await getFeatureFlagsAsync(db);
|
||||
|
||||
return Response.json({ features }, { headers: { "Cache-Control": "private, max-age=60" } });
|
||||
}
|
||||
Reference in new issue
Block a user