Skip to content

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

6.5 KB
Raw Blame
import type { NextApiRequest, NextApiResponse } from "next";
import * as z from "zod/v4";
import {
  isProgressRow,
  isRow,
  isException,
  ClickHouseResourceError,
  queryClickhouseWithProgress,
  logger,
} from "@langfuse/shared/src/server";
import { RESOURCE_LIMIT_ERROR_MESSAGE } from "@langfuse/shared";

import { getServerAuthSession } from "@/src/server/auth";
import { sendAdminAccessWebhook } from "@/src/server/adminAccessWebhook";
import { prisma } from "@langfuse/shared/src/db";
import {
  prepareExecuteQuery,
  toClickhouseQueryOpts,
} from "@langfuse/shared/query/server";
import {
  query as customQuery,
  validateQuery,
  viewVersions,
} from "@langfuse/shared/query";
export type SSEEvent =
  | { type: "progress"; progress: object }
  | { type: "row"; row: Record<string, unknown> }
  | { type: "done" }
  | { type: "error"; message: string };

function formatSSEEvent(event: SSEEvent): string {
  switch (event.type) {
    case "progress":
      return `event: progress\ndata: ${JSON.stringify(event.progress)}\n\n`;
    case "row":
      return `event: row\ndata: ${JSON.stringify(event.row)}\n\n`;
    case "done":
      return `event: done\ndata: {}\n\n`;
    case "error":
      return `event: error\ndata: ${JSON.stringify({ message: event.message })}\n\n`;
  }
}

// `version` is required on purpose — see dashboard.executeQuery: an implicit
// v1 default let unresolved-session clients silently pick the read path.
const inputSchema = z.object({
  projectId: z.string(),
  query: customQuery,
  version: viewVersions,
});

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse,
) {
  if (req.method !== "POST") {
    res.setHeader("Allow", "POST");
    res.status(405).end();
    return;
  }

  const session = await getServerAuthSession({ req, res });
  if (!session?.user) {
    res.status(401).json({ message: "Unauthorized" });
    return;
  }

  const parsed = inputSchema.safeParse(req.body);
  if (!parsed.success) {
    res.status(400).json({ message: "Invalid input", errors: parsed.error });
    return;
  }

  const { projectId, query, version } = parsed.data;

  // Verify user is a member of this project (mirrors enforceUserIsAuthedAndProjectMember)
  const sessionProject = session.user.organizations
    .flatMap((org) =>
      org.projects.map((project) => ({ ...project, organization: org })),
    )
    .find((project) => project.id === projectId);

  if (!sessionProject) {
    if (session.user.admin === true) {
      const dbProject = await prisma.project.findFirst({
        select: { orgId: true },
        where: { id: projectId, deletedAt: null },
      });
      if (!dbProject) {
        res.status(404).json({ message: "Project not found" });
        return;
      }
      await sendAdminAccessWebhook({
        email: session.user.email,
        projectId,
        orgId: dbProject.orgId,
      });
    } else {
      res.status(403).json({ message: "Not a member of this project" });
      return;
    }
  } else if (session.user.admin === true) {
    await sendAdminAccessWebhook({
      email: session.user.email,
      projectId,
      orgId: sessionProject.organization.id,
    });
  }

  if (session.user.v4BetaEnabled !== true) {
    res.status(400).json({
      message: "Streaming is only supported for v4-enabled dashboard queries",
    });
    return;
  }

  const validation = validateQuery(query, version);
  if (!validation.valid) {
    res.status(400).json({ message: "Invalid query", errors: validation });
    return;
  }

  // SSE headers
  res.writeHead(200, {
    "Content-Type": "text/event-stream",
    "Cache-Control": "no-cache, no-transform",
    Connection: "keep-alive",
    "X-Accel-Buffering": "no",
  });
  res.flushHeaders();

  // A closed client should also stop the ClickHouse query, not just this
  // response: aborting the signal tears down the CH request (and counts the
  // query as client_closed), and cancel_http_readonly_queries_on_client_close
  // makes ClickHouse kill the query on that close instead of running the
  // abandoned aggregation to completion. max_execution_time still bounds the
  // stragglers either way.
  const clientClosed = new AbortController();
  // `res`, not `req`: the request stream can end once the POST body is
  // consumed, while the response lives for the whole stream — its close
  // fires on a client disconnect mid-stream (and only after res.end() on
  // the happy path, where the loop has already finished).
  res.on("close", () => clientClosed.abort());

  try {
    const prepared = await prepareExecuteQuery({
      projectId,
      query,
      version,
      enableSingleLevelOptimization: version === "v2",
    });
    const chOpts = toClickhouseQueryOpts(prepared);

    for await (const event of queryClickhouseWithProgress<
      Record<string, unknown>
    >({
      ...chOpts,
      abortSignal: clientClosed.signal,
      tags: {
        ...chOpts.tags,
        surface: "internalapi",
        route: "POST /api/dashboard/execute-query-stream",
      },
      clickhouseSettings: {
        ...chOpts.clickhouseSettings,
        cancel_http_readonly_queries_on_client_close: 1,
      },
    })) {
      if (isProgressRow(event)) {
        res.write(
          formatSSEEvent({ type: "progress", progress: event.progress }),
        );
      } else if (isRow<Record<string, unknown>>(event)) {
        res.write(formatSSEEvent({ type: "row", row: event.row }));
      } else if (isException(event)) {
        const isResource =
          ClickHouseResourceError.wrapIfResourceError(
            new Error(event.exception),
          ) instanceof ClickHouseResourceError;
        logger.error(
          `[execute-query-stream] ClickHouse exception: ${event.exception}`,
          { projectId },
        );
        const userMessage = isResource
          ? RESOURCE_LIMIT_ERROR_MESSAGE
          : event.exception;
        res.write(formatSSEEvent({ type: "error", message: userMessage }));
        return;
      }
    }

    if (!clientClosed.signal.aborted) {
      res.write(formatSSEEvent({ type: "done" }));
    }
  } catch (error) {
    if (!clientClosed.signal.aborted) {
      logger.error("[execute-query-stream] Query failed", {
        error: error instanceof Error ? error.message : String(error),
        projectId,
      });
      const message = (() => {
        if (error instanceof ClickHouseResourceError) {
          return RESOURCE_LIMIT_ERROR_MESSAGE;
        }
        if (error instanceof Error) {
          return error.message;
        }
        return "Internal server error";
      })();
      res.write(formatSSEEvent({ type: "error", message }));
    }
  } finally {
    res.end();
  }
}