Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(ask-ai): Support Ask AI on self-hosted (#16298)

Lysander Kieself7e3c26a3896
diff --git a/web/src/__tests__/server/ai-filter-access.servertest.ts b/web/src/__tests__/server/ai-filter-access.servertest.ts
index 38efce264e..a6f6815366 100644
--- a/web/src/__tests__/server/ai-filter-access.servertest.ts
+++ b/web/src/__tests__/server/ai-filter-access.servertest.ts
@@ -1,9 +1,29 @@
import { appRouter } from "@/src/server/api/root";
import { createInnerTRPCContext } from "@/src/server/api/trpc";
import { prisma, type Role } from "@langfuse/shared/src/db";
+import { env as sharedEnv } from "@langfuse/shared/src/env";
import { createOrgProjectAndApiKey } from "@langfuse/shared/src/server";
+import { env } from "@/src/env.mjs";
import type { Session } from "next-auth";
+const llmMocks = vi.hoisted(() => ({
+ generateLLMText: vi.fn(async () => ({ text: "[]" })),
+}));
+
+// Stub the published LLM helper rather than the shared server barrel: the
+// barrel is also imported by `@langfuse/shared/src/db`, and mocking it breaks
+// Prisma setup. `langfuseAiCompletion` is not a package export, so tsc cannot
+// resolve a mock of that path.
+vi.mock("@langfuse/shared/src/server/llm/llmText", async () => {
+ const actual = await vi.importActual(
+ "@langfuse/shared/src/server/llm/llmText",
+ );
+ return {
+ ...actual,
+ generateLLMText: llmMocks.generateLLMText,
+ };
+});
+
// Both Ask AI endpoints only generate a filter over data the caller can
// already read, so they are gated on `project:read` — a VIEWER must get in.
const NO_ACCESS = "User does not have access to this resource or action";
@@ -61,7 +81,7 @@ async function prepare(projectRole: Role) {
};
const ctx = createInnerTRPCContext({ session, headers: {} });
- return { project, caller: appRouter.createCaller({ ...ctx, prisma }) };
+ return { project, org, caller: appRouter.createCaller({ ...ctx, prisma }) };
}
/** Message of a rejection, or "" when the call resolved. */
@@ -77,9 +97,9 @@ describe("Ask AI filter generation access", () => {
await prisma.organization.deleteMany({ where: { id: { in: __orgIds } } });
});
- // The org's `aiFeaturesEnabled` is off, so both calls still fail on the
- // org-level AI gate (or the self-hosted precondition) right after the RBAC
- // check — no LLM is reached. Only the RBAC verdict is asserted here.
+ // The org's `aiFeaturesEnabled` is off, so the v4 call still fails on the
+ // org-level AI gate right after RBAC. The leftover wand is Cloud-only and
+ // may fail that gate first. Only the RBAC verdict is asserted here.
it("lets a VIEWER through the RBAC gate on both endpoints", async () => {
const { project, caller } = await prepare("VIEWER");
@@ -119,4 +139,65 @@ describe("Ask AI filter generation access", () => {
}),
).rejects.toThrow(NO_ACCESS);
});
+
+ it("generates Ask AI on self-hosted and keeps Filter with AI Cloud-only", async () => {
+ const originalCloudRegion = env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION;
+ const originalBedrockModel = env.LANGFUSE_AWS_BEDROCK_MODEL;
+ const originalBedrockSmallModel = env.LANGFUSE_AWS_BEDROCK_SMALL_MODEL;
+ const originalAiFeaturesProjectId =
+ sharedEnv.LANGFUSE_AI_FEATURES_PROJECT_ID;
+
+ (
+ env as { NEXT_PUBLIC_LANGFUSE_CLOUD_REGION?: string }
+ ).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = undefined;
+ (
+ env as { LANGFUSE_AWS_BEDROCK_MODEL?: string }
+ ).LANGFUSE_AWS_BEDROCK_MODEL = "test-model";
+ (
+ env as { LANGFUSE_AWS_BEDROCK_SMALL_MODEL?: string }
+ ).LANGFUSE_AWS_BEDROCK_SMALL_MODEL = undefined;
+ (
+ sharedEnv as { LANGFUSE_AI_FEATURES_PROJECT_ID?: string }
+ ).LANGFUSE_AI_FEATURES_PROJECT_ID = undefined;
+ llmMocks.generateLLMText.mockClear();
+
+ try {
+ const { project, org, caller } = await prepare("VIEWER");
+ await prisma.organization.update({
+ where: { id: org.id },
+ data: { aiFeaturesEnabled: true, aiTelemetryEnabled: true },
+ });
+
+ await expect(
+ caller.naturalLanguageFilters.createCompletion({
+ projectId: project.id,
+ prompt: "traces from today",
+ }),
+ ).rejects.toThrow(
+ "Natural language filtering is not available in self-hosted deployments.",
+ );
+ expect(llmMocks.generateLLMText).not.toHaveBeenCalled();
+
+ await expect(
+ caller.searchBar.generateFilter({
+ projectId: project.id,
+ prompt: "traces from today",
+ }),
+ ).resolves.toMatchObject({ filters: [] });
+ expect(llmMocks.generateLLMText).toHaveBeenCalledOnce();
+ } finally {
+ (
+ env as { NEXT_PUBLIC_LANGFUSE_CLOUD_REGION?: string }
+ ).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = originalCloudRegion;
+ (
+ env as { LANGFUSE_AWS_BEDROCK_MODEL?: string }
+ ).LANGFUSE_AWS_BEDROCK_MODEL = originalBedrockModel;
+ (
+ env as { LANGFUSE_AWS_BEDROCK_SMALL_MODEL?: string }
+ ).LANGFUSE_AWS_BEDROCK_SMALL_MODEL = originalBedrockSmallModel;
+ (
+ sharedEnv as { LANGFUSE_AI_FEATURES_PROJECT_ID?: string }
+ ).LANGFUSE_AI_FEATURES_PROJECT_ID = originalAiFeaturesProjectId;
+ }
+ });
});
diff --git a/web/src/components/table/data-table-controls.tsx b/web/src/components/table/data-table-controls.tsx
index 8e80e86072..a79e8fab5a 100644
--- a/web/src/components/table/data-table-controls.tsx
+++ b/web/src/components/table/data-table-controls.tsx
@@ -95,8 +95,8 @@ import {
PopoverTrigger,
} from "@/src/components/ui/popover";
import { DataTableAIFilters } from "@/src/components/table/data-table-ai-filters";
-import { type FilterState } from "@langfuse/shared";
import { useLangfuseCloudRegion } from "@/src/features/organizations/hooks";
+import { type FilterState } from "@langfuse/shared";
interface ControlsContextType {
open: boolean;
@@ -209,8 +209,8 @@ export function DataTableControls({
blockedColumnReason,
layout = "panel",
}: DataTableControlsProps) {
- const { isLangfuseCloud } = useLangfuseCloudRegion();
const { setOpen, tableName, isMobile } = useDataTableControls();
+ const { isLangfuseCloud } = useLangfuseCloudRegion();
const capture = usePostHogClientCapture();
const [aiPopoverOpen, setAiPopoverOpen] = useState(false);
const activeFilterCount = queryFilter.filters.filter(
diff --git a/web/src/features/filters/components/filter-builder.tsx b/web/src/features/filters/components/filter-builder.tsx
index 27a1e00f85..953bb0330b 100644
--- a/web/src/features/filters/components/filter-builder.tsx
+++ b/web/src/features/filters/components/filter-builder.tsx
@@ -119,9 +119,9 @@ export function PopoverFilterBuilder({
isV4?: boolean;
}) {
const capture = usePostHogClientCapture();
- const { isLangfuseCloud } = useLangfuseCloudRegion();
const projectId = useProjectIdFromURL();
const { organization } = useQueryProject();
+ const { isLangfuseCloud } = useLangfuseCloudRegion();
const createFilterMutation =
api.naturalLanguageFilters.createCompletion.useMutation();
const [wipFilterState, _setWipFilterState] =
diff --git a/web/src/features/natural-language-filters/server/router.ts b/web/src/features/natural-language-filters/server/router.ts
index 29dfc1e3fd..06e93e1c76 100644
--- a/web/src/features/natural-language-filters/server/router.ts
+++ b/web/src/features/natural-language-filters/server/router.ts
@@ -32,6 +32,9 @@ export const naturalLanguageFilterRouter = createTRPCRouter({
scope: "project:read",
});
+ // Leftover table-wand path: still Cloud-only. It needs the managed
+ // `get-filter-conditions-from-query` prompt and has no bundled fallback.
+ // v4 Ask AI (`searchBar.generateFilter`) is the self-hosted path.
if (!env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION) {
throw new TRPCError({
code: "PRECONDITION_FAILED",
@@ -102,14 +105,12 @@ export const naturalLanguageFilterRouter = createTRPCRouter({
{ type: "chat" },
);
- const aiTelemetryEnabled = project.organization.aiTelemetryEnabled;
-
- if (aiTelemetryEnabled && !isLangfuseAITracingConfigured()) {
- throw new TRPCError({
- code: "INTERNAL_SERVER_ERROR",
- message: "Langfuse AI Features not configured.",
- });
- }
+ // Tracing is optional: skip it when the AI-features project is not
+ // configured (the default self-hosted case) rather than failing the
+ // generation. Self-hosted cannot toggle `aiTelemetryEnabled` off.
+ const aiTelemetryEnabled =
+ project.organization.aiTelemetryEnabled &&
+ isLangfuseAITracingConfigured();
// Get current datetime in ISO format with day of week for AI context
const now = new Date();
diff --git a/web/src/features/search-bar/README.md b/web/src/features/search-bar/README.md
index 2828452aa0..bdc758d9a3 100644
--- a/web/src/features/search-bar/README.md
+++ b/web/src/features/search-bar/README.md
@@ -270,15 +270,16 @@ encode/decode round-trip. The flat URL contract (`FilterState` + `searchQuery`
The bar is also the home of AI-assisted filtering on v4 (it replaces the legacy
sidebar "✨ wand" — `EventsTable` now passes `filterWithAI={!searchBarMode}`, so
-the wand only survives on non-bar/embedded surfaces and the v3 traces table).
+the wand only survives on Cloud, on non-bar/embedded surfaces and the leftover
+traces table). Self-hosted uses Ask AI on this bar only.
- **Entry.** The **"Ask AI"** affordance opens AI mode — a plain button placed
AFTER the field in DOM order, always available (build from scratch OR refine
existing filters). Tab is deliberately NOT a shortcut: while typing it belongs
to autocomplete navigation, so forward-tab just moves focus from the field
onto the button. `EventsSearchBarRow` owns the `'grammar' | 'ai'` mode and
- gates availability on `isLangfuseCloud && organization.aiFeaturesEnabled` (the
- server enforces it too). `SearchComposer` only takes an `onActivateAi` callback
+ gates availability on `organization.aiFeaturesEnabled` (the server enforces it
+ too). `SearchComposer` only takes an `onActivateAi` callback
- renders the affordance; it stays grammar-only.
- **The component.** `components/SearchBarAiPrompt.tsx` — a plain NL input (not
the contenteditable). Enter generates; Esc or the back arrow exits (no
diff --git a/web/src/features/search-bar/components/EventsSearchBarRow.tsx b/web/src/features/search-bar/components/EventsSearchBarRow.tsx
index ba2b58ab1d..4e09790509 100644
--- a/web/src/features/search-bar/components/EventsSearchBarRow.tsx
+++ b/web/src/features/search-bar/components/EventsSearchBarRow.tsx
@@ -17,7 +17,6 @@
import * as React from "react";
import { type FilterState } from "@langfuse/shared";
-import { useLangfuseCloudRegion } from "@/src/features/organizations/hooks";
import { useQueryProject } from "@/src/features/projects/hooks";
import { cn } from "@/src/utils/tailwind";
import type {
@@ -86,12 +85,10 @@ export function EventsSearchBarRow({
className?: string;
}) {
const [aiOpen, setAiOpen] = React.useState(false);
- const { isLangfuseCloud } = useLangfuseCloudRegion();
const { organization } = useQueryProject();
- // Mirror the legacy wand gate: Cloud + org-level AI features. The server
+ // Mirror the legacy wand gate: org-level AI features. The server
// enforces it too, so this only governs whether the affordance is offered.
- const aiAvailable =
- isLangfuseCloud && Boolean(organization?.aiFeaturesEnabled);
+ const aiAvailable = Boolean(organization?.aiFeaturesEnabled);
const activateAi = React.useCallback(() => {
// Ground the model on real project values: lazily request the AI columns so
diff --git a/web/src/features/search-bar/server/router.ts b/web/src/features/search-bar/server/router.ts
index 2401bb4578..290a523723 100644
--- a/web/src/features/search-bar/server/router.ts
+++ b/web/src/features/search-bar/server/router.ts
@@ -89,14 +89,6 @@ export const searchBarRouter = createTRPCRouter({
scope: "project:read",
});
- if (!env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION) {
- throw new TRPCError({
- code: "PRECONDITION_FAILED",
- message:
- "AI filter generation is not available in self-hosted deployments.",
- });
- }
-
const project = await ctx.prisma.project.findUnique({
where: { id: input.projectId },
select: {
@@ -137,14 +129,12 @@ export const searchBarRouter = createTRPCRouter({
const dayOfWeek = now.toLocaleDateString("en-US", { weekday: "long" });
const currentDatetime = `${dayOfWeek}, ${now.toISOString()}`;
- const aiTelemetryEnabled = project.organization.aiTelemetryEnabled;
-
- if (aiTelemetryEnabled && !isLangfuseAITracingConfigured()) {
- throw new TRPCError({
- code: "INTERNAL_SERVER_ERROR",
- message: "Langfuse AI Features not configured.",
- });
- }
+ // Tracing is optional: skip it when the AI-features project is not
+ // configured (the default self-hosted case) rather than failing the
+ // generation. Self-hosted cannot toggle `aiTelemetryEnabled` off.
+ const aiTelemetryEnabled =
+ project.organization.aiTelemetryEnabled &&
+ isLangfuseAITracingConfigured();
// Pre-generated (rather than left to `getLangfuseAITraceSinkParams`'s
// own default) so this handler OWNS the id: the parse-outcome scores