Skip to content

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

12.5 KB
Raw Blame
import type { z } from "zod";
import { DashboardWidgetChartType } from "@langfuse/shared/src/db";
import {
  DashboardService,
  dashboardWidgetViewToQueryView,
  queryViewToDashboardWidgetView,
  resolveDashboardWidgetMinVersion,
  type WidgetDomain,
  type ApiAccessScope,
} from "@langfuse/shared/src/server";
import {
  getValidAggregationsForMeasureType,
  getViewDeclaration,
  type ViewVersion,
} from "@langfuse/shared/query";
import { auditLog } from "@/src/features/audit-logs/auditLog";
import { createStructuredPublicApiError } from "@/src/features/public-api/types/structuredPublicApiError";
import {
  PostUnstableDashboardWidgetResponse,
  type DashboardWidgetViewOutputType,
  type PostUnstableDashboardWidgetBodyType,
} from "@/src/features/public-api/types/unstable-dashboard-widgets";

import { ChartConfigSchema, LangfuseNotFoundError } from "@langfuse/shared";
import {
  getWidgetImportFilterConfig,
  partitionStoredUiTableFiltersToView,
} from "@/src/features/dashboard/lib/dashboardUiTableToViewMapping";
import {
  MAX_PIVOT_TABLE_DIMENSIONS,
  MAX_PIVOT_TABLE_METRICS,
} from "@/src/features/widgets/utils/pivot-table-utils";

// The widget shape used internally after input normalization: the public
// body with chartConfig and filters fully resolved.
type NormalizedWidgetInput = Omit<
  z.infer<typeof PostUnstableDashboardWidgetResponse>,
  "id" | "createdAt" | "updatedAt"
>;

const throwInvalidWidget = (params: {
  message: string;
  field?: string;
  allowedValues?: string[];
}): never => {
  throw createStructuredPublicApiError({
    httpCode: 400,
    code: "invalid_request",
    message: params.message,
    details:
      params.field || params.allowedValues
        ? {
            field: params.field,
            allowedValues: params.allowedValues,
          }
        : undefined,
  });
};

function resolvePublicWidgetPersistedViewVersion(
  widget: NormalizedWidgetInput,
  persistedMinVersion?: number,
): ViewVersion {
  return resolveDashboardWidgetMinVersion(
    {
      view: widget.view,
      dimensions: widget.dimensions,
      measures: widget.metrics,
      filters: widget.filters,
    },
    persistedMinVersion,
  ) >= 2
    ? "v2"
    : "v1";
}

function getPublicDashboardWidgetViewDeclaration(
  widget: NormalizedWidgetInput,
  viewVersion: ViewVersion,
): ReturnType<typeof getViewDeclaration> {
  try {
    return getViewDeclaration(widget.view, viewVersion);
  } catch (error) {
    return throwInvalidWidget({
      message: error instanceof Error ? error.message : "Invalid widget view",
      field: "view",
    });
  }
}

type PublicDashboardWidgetInput = Omit<
  PostUnstableDashboardWidgetBodyType,
  "view"
> & { view: DashboardWidgetViewOutputType };

/**
 * Normalize a public widget against the query tables available in this
 * deployment. V2 widgets read the `events_*` tables, which the worker only
 * writes in `dual` and `events_only` mode; preview opt-in controls whether the
 * UI offers v4, not whether those tables contain data.
 *
 * The shape-required version is checked against what the deployment can
 * actually query, so the API never accepts a widget that can never render
 * (LFE-14581). The DashboardService derives the persisted version when it
 * writes the normalized shape.
 */
export function normalizePublicDashboardWidgetInput(
  input: PublicDashboardWidgetInput,
): NormalizedWidgetInput {
  const { mappedFilters, unsupportedFilters } =
    partitionStoredUiTableFiltersToView(input.view, input.filters);
  const { allowedColumns, columnAliases } = getWidgetImportFilterConfig(
    input.view,
  );

  if (unsupportedFilters.length > 0) {
    throwInvalidWidget({
      message: `Unsupported filter column for view "${input.view}": ${unsupportedFilters.map((filter) => filter.column).join(", ")}`,
      field: "filters",
    });
  }

  const invalidFilterColumns = mappedFilters.flatMap((filter) => {
    const normalizedColumn = columnAliases[filter.column] ?? filter.column;
    return allowedColumns.has(normalizedColumn) ? [] : [filter.column];
  });

  if (invalidFilterColumns.length > 0) {
    throwInvalidWidget({
      message: `Unsupported filter column for view "${input.view}": ${invalidFilterColumns.join(", ")}`,
      field: "filters",
    });
  }

  // chartConfig.type defaults to chartType; when given explicitly it must
  // match. Per-type option validation happens after the type is resolved.
  const chartConfigType = input.chartConfig?.type ?? input.chartType;
  if (chartConfigType !== input.chartType) {
    throwInvalidWidget({
      message: "chartConfig.type must match chartType",
      field: "chartConfig.type",
    });
  }
  const chartConfig = ChartConfigSchema.safeParse({
    ...input.chartConfig,
    type: chartConfigType,
  });
  if (!chartConfig.success) {
    return throwInvalidWidget({
      message: `Invalid chartConfig: ${chartConfig.error.issues
        .map((issue) => `${issue.path.join(".")}: ${issue.message}`)
        .join(", ")}`,
      field: "chartConfig",
    });
  }

  const normalizedFilters = mappedFilters.map((filter) => ({
    ...filter,
    column: columnAliases[filter.column] ?? filter.column,
  }));
  return {
    ...input,
    filters: normalizedFilters,
    chartConfig: chartConfig.data,
  };
}

export function validatePublicDashboardWidgetInput(
  widget: NormalizedWidgetInput,
  persistedMinVersion?: number,
): void {
  const viewVersion = resolvePublicWidgetPersistedViewVersion(
    widget,
    persistedMinVersion,
  );
  const viewDeclaration = getPublicDashboardWidgetViewDeclaration(
    widget,
    viewVersion,
  );
  const widgetCompatibleDimensions = Object.entries(
    viewDeclaration.dimensions,
  ).flatMap(([field, definition]) => (definition.uiHidden ? [] : [field]));

  for (const [index, dimension] of widget.dimensions.entries()) {
    const dimensionDefinition = viewDeclaration.dimensions[dimension.field];

    if (!dimensionDefinition) {
      throwInvalidWidget({
        message: `Dimension "${dimension.field}" is not available for view "${widget.view}" in version "${viewVersion}"`,
        field: `dimensions[${index}].field`,
        allowedValues: widgetCompatibleDimensions,
      });
    }

    if (dimensionDefinition.uiHidden) {
      throwInvalidWidget({
        message: `Dimension "${dimension.field}" is not available for widgets`,
        field: `dimensions[${index}].field`,
        allowedValues: widgetCompatibleDimensions,
      });
    }
  }

  for (const [index, metric] of widget.metrics.entries()) {
    const measureDefinition = viewDeclaration.measures[metric.measure];

    if (!measureDefinition) {
      throwInvalidWidget({
        message: `Measure "${metric.measure}" is not available for view "${widget.view}" in version "${viewVersion}"`,
        field: `metrics[${index}].measure`,
        allowedValues: Object.keys(viewDeclaration.measures),
      });
    }

    const validAggregations = getValidAggregationsForMeasureType(
      measureDefinition.type,
    );

    if (!validAggregations.includes(metric.agg)) {
      throwInvalidWidget({
        message: `Aggregation "${metric.agg}" is not valid for measure "${metric.measure}"`,
        field: `metrics[${index}].agg`,
        allowedValues: validAggregations,
      });
    }
  }

  if (
    widget.chartType !== DashboardWidgetChartType.PIVOT_TABLE &&
    widget.dimensions.length > 1
  ) {
    throwInvalidWidget({
      message: "Only pivot table widgets can have multiple dimensions",
      field: "dimensions",
    });
  }

  if (
    widget.chartType === DashboardWidgetChartType.PIVOT_TABLE &&
    widget.dimensions.length > MAX_PIVOT_TABLE_DIMENSIONS
  ) {
    throwInvalidWidget({
      message: `Pivot table widgets can have at most ${MAX_PIVOT_TABLE_DIMENSIONS} dimensions`,
      field: "dimensions",
    });
  }

  if (
    widget.chartType === DashboardWidgetChartType.PIVOT_TABLE &&
    widget.metrics.length > MAX_PIVOT_TABLE_METRICS
  ) {
    throwInvalidWidget({
      message: `Pivot table widgets can have at most ${MAX_PIVOT_TABLE_METRICS} metrics`,
      field: "metrics",
    });
  }
}

function toApiDashboardWidget(widget: WidgetDomain) {
  return PostUnstableDashboardWidgetResponse.parse({
    id: widget.id,
    createdAt: widget.createdAt,
    updatedAt: widget.updatedAt,
    name: widget.name,
    description: widget.description,
    view: dashboardWidgetViewToQueryView[widget.view],
    dimensions: widget.dimensions,
    metrics: widget.metrics,
    filters: widget.filters,
    chartType: widget.chartType,
    chartConfig: widget.chartConfig,
  });
}

export async function createPublicDashboardWidget(params: {
  projectId: string;
  input: PostUnstableDashboardWidgetBodyType;
  auditScope: Pick<ApiAccessScope, "orgId" | "apiKeyId">;
}) {
  const input = normalizePublicDashboardWidgetInput(params.input);
  validatePublicDashboardWidgetInput(input);

  const widget = await DashboardService.createWidget(params.projectId, {
    ...input,
    view: queryViewToDashboardWidgetView[input.view],
  });

  await auditLog({
    action: "create",
    resourceType: "dashboardWidget",
    resourceId: widget.id,
    projectId: params.projectId,
    orgId: params.auditScope.orgId,
    apiKeyId: params.auditScope.apiKeyId,
    after: toApiDashboardWidget(widget),
  });

  return toApiDashboardWidget(widget);
}

function toPublicWidgetInput(widget: WidgetDomain) {
  const {
    id: _id,
    createdAt: _createdAt,
    updatedAt: _updatedAt,
    ...input
  } = toApiDashboardWidget(widget);
  return input;
}

async function getProjectWidgetOrThrow(projectId: string, widgetId: string) {
  const widget = await DashboardService.getWidget(widgetId, projectId);
  if (!widget || widget.projectId !== projectId) {
    throw new LangfuseNotFoundError(`Dashboard widget ${widgetId} not found`);
  }
  return widget;
}

export async function listPublicDashboardWidgets(params: {
  projectId: string;
  page: number;
  limit: number;
}) {
  const result = await DashboardService.listWidgets(params);
  return {
    data: result.widgets.map(toApiDashboardWidget),
    meta: {
      page: params.page,
      limit: params.limit,
      totalItems: result.totalCount,
      totalPages: Math.ceil(result.totalCount / params.limit),
    },
  };
}

export async function getPublicDashboardWidget(params: {
  projectId: string;
  widgetId: string;
}) {
  return toApiDashboardWidget(
    await getProjectWidgetOrThrow(params.projectId, params.widgetId),
  );
}

export async function updatePublicDashboardWidget(params: {
  projectId: string;
  widgetId: string;
  input: Partial<PostUnstableDashboardWidgetBodyType>;
  auditScope: Pick<ApiAccessScope, "orgId" | "apiKeyId">;
}) {
  const current = await getProjectWidgetOrThrow(
    params.projectId,
    params.widgetId,
  );
  const currentPublic = toPublicWidgetInput(current);
  const chartTypeChanged =
    params.input.chartType !== undefined &&
    params.input.chartType !== currentPublic.chartType;
  const input = normalizePublicDashboardWidgetInput({
    ...currentPublic,
    ...params.input,
    // A chartType change without an explicit chartConfig resets the config
    // to the new type; carrying the stale config type over would always
    // fail validation.
    chartConfig:
      params.input.chartConfig ??
      (chartTypeChanged
        ? { type: params.input.chartType }
        : currentPublic.chartConfig),
  });
  const persistedMinVersion =
    current.view === queryViewToDashboardWidgetView[input.view]
      ? current.minVersion
      : undefined;
  validatePublicDashboardWidgetInput(input, persistedMinVersion);
  const updated = await DashboardService.updateWidget(
    params.projectId,
    params.widgetId,
    {
      ...input,
      view: queryViewToDashboardWidgetView[input.view],
    },
  );
  const result = toApiDashboardWidget(updated);
  await auditLog({
    action: "update",
    resourceType: "dashboardWidget",
    resourceId: updated.id,
    projectId: params.projectId,
    orgId: params.auditScope.orgId,
    apiKeyId: params.auditScope.apiKeyId,
    before: toApiDashboardWidget(current),
    after: result,
  });
  return result;
}

export async function deletePublicDashboardWidget(params: {
  projectId: string;
  widgetId: string;
  auditScope: Pick<ApiAccessScope, "orgId" | "apiKeyId">;
}) {
  const current = await getProjectWidgetOrThrow(
    params.projectId,
    params.widgetId,
  );
  await DashboardService.deleteWidget(params.widgetId, params.projectId);
  await auditLog({
    action: "delete",
    resourceType: "dashboardWidget",
    resourceId: current.id,
    projectId: params.projectId,
    orgId: params.auditScope.orgId,
    apiKeyId: params.auditScope.apiKeyId,
    before: toApiDashboardWidget(current),
  });
}