🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
refactor(web): Migrate model definitions settings table to design system (#17943)
Ben Bacheme0a2735e24e3
diff --git a/web/src/components/design-system/DialogController/DialogController.stories.tsx b/web/src/components/design-system/DialogController/DialogController.stories.tsxindex ae959699be..95acb49ece 100644--- a/web/src/components/design-system/DialogController/DialogController.stories.tsx+++ b/web/src/components/design-system/DialogController/DialogController.stories.tsx@@ -1,4 +1,5 @@import { expect, fn, userEvent, waitFor, within } from "storybook/test";+import { useState } from "react";import preview from "../../../../.storybook/preview";import { Dialog } from "@/src/components/design-system/Dialog/Dialog";@@ -12,6 +13,86 @@ const meta = preview.meta({},});+function StatefulContent({+ closeDialog,+ initialValue,+}: {+ closeDialog: () => void;+ initialValue: string;+}) {+ const [value, setValue] = useState(initialValue);++ return (+ <Dialog title="Stateful content">+ <Dialog.Body>+ <input+ aria-label="Draft"+ value={value}+ onChange={(event) => setValue(event.target.value)}+ />+ <button type="button" onClick={closeDialog}>+ Close from content+ </button>+ </Dialog.Body>+ </Dialog>+ );+}++export const FreshContentOnOpen = meta.story({+ name: "(Test) Fresh content on open",+ render: () => (+ <DialogController<string>+ renderDialog={({ state, closeDialog }) => (+ <StatefulContent closeDialog={closeDialog} initialValue={state} />+ )}+ >+ {({ openDialog }) => (+ <>+ <button type="button" onClick={() => openDialog("")}>+ Open+ </button>+ <button type="button" onClick={() => openDialog("updated")}>+ Open updated+ </button>+ </>+ )}+ </DialogController>+ ),+ play: async ({ canvasElement }) => {+ const canvas = within(canvasElement);+ const body = within(canvasElement.ownerDocument.body);++ await userEvent.click(canvas.getByRole("button", { name: "Open" }));+ await userEvent.type(body.getByRole("textbox", { name: "Draft" }), "old");+ await userEvent.click(+ body.getByRole("button", { name: "Close from content" }),+ );+ await waitFor(() =>+ expect(canvas.getByRole("button", { name: "Open" })).toBeVisible(),+ );+ await userEvent.click(canvas.getByRole("button", { name: "Open" }));++ expect(body.getByRole("textbox", { name: "Draft" })).toHaveValue("");++ await userEvent.click(+ body.getByRole("button", { name: "Close from content" }),+ );+ await waitFor(() =>+ expect(+ canvas.getByRole("button", { name: "Open updated" }),+ ).toBeVisible(),+ );+ await userEvent.click(canvas.getByRole("button", { name: "Open updated" }));+ expect(body.getByRole("textbox", { name: "Draft" })).toHaveValue("updated");+ await userEvent.click(+ body.getByRole("button", { name: "Close from content" }),+ );+ await waitFor(() =>+ expect(canvas.getByRole("button", { name: "Open" })).toBeVisible(),+ );+ },+});+export const StateValues = meta.story({name: "(Test) State values",render: () => (@@ -59,6 +140,11 @@ export const StateValues = meta.story({expect(canvas.getByTestId("open-state")).toHaveTextContent("closed");expect(body.queryByText("Stateful dialog")).not.toBeInTheDocument();});+ await waitFor(() =>+ expect(+ canvas.getByRole("button", { name: "Open with undefined" }),+ ).toBeVisible(),+ );await userEvent.click(canvas.getByRole("button", { name: "Open with undefined" }),@@ -66,6 +152,14 @@ export const StateValues = meta.story({await waitFor(() =>expect(body.getByTestId("dialog-state")).toHaveTextContent("undefined"),);+ await userEvent.click(+ body.getByRole("button", { name: "Close from content" }),+ );+ await waitFor(() =>+ expect(+ canvas.getByRole("button", { name: "Open with null" }),+ ).toBeVisible(),+ );},});@@ -102,6 +196,14 @@ export const CloseVeto = meta.story({expect(onDismiss).not.toHaveBeenCalled();expect(body.getByText("Vetoed dialog")).toBeVisible();});+ onBeforeClose.mockImplementationOnce(() => true);+ await userEvent.keyboard("{Escape}");+ await waitFor(() =>+ expect(body.queryByText("Vetoed dialog")).not.toBeInTheDocument(),+ );+ await waitFor(() =>+ expect(canvas.getByRole("button", { name: "Open" })).toBeVisible(),+ );},});@@ -119,5 +221,9 @@ export const InitialState = meta.story({const body = within(canvasElement.ownerDocument.body);await waitFor(() => expect(body.getByText("Initial state")).toBeVisible());+ await userEvent.keyboard("{Escape}");+ await waitFor(() =>+ expect(body.queryByText("Initial state")).not.toBeInTheDocument(),+ );},});diff --git a/web/src/components/design-system/DialogController/DialogController.tsx b/web/src/components/design-system/DialogController/DialogController.tsxindex ef7a320176..69240be491 100644--- a/web/src/components/design-system/DialogController/DialogController.tsx+++ b/web/src/components/design-system/DialogController/DialogController.tsx@@ -26,12 +26,17 @@ function DialogController<State = void>({}: DialogControllerProps<State>) {const [controllerState, setControllerState] = React.useState<| { status: "uninitialized" }- | { status: "initialized"; isOpen: boolean; state: State }+ | {+ status: "initialized";+ isOpen: boolean;+ state: State;+ openCount: number;+ }>(() => {const state = initialState?.();return state === undefined? { status: "uninitialized" }- : { status: "initialized", isOpen: true, state };+ : { status: "initialized", isOpen: true, state, openCount: 0 };});const closeDialog = () => {@@ -57,15 +62,23 @@ function DialogController<State = void>({isOpen:controllerState.status === "initialized" && controllerState.isOpen,openDialog: (...args) =>- setControllerState({+ setControllerState((currentState) => ({status: "initialized",isOpen: true,state: args[0] as State,- }),+ openCount:+ currentState.status === "initialized"+ ? currentState.openCount + 1+ : 0,+ })),})}- {controllerState.status === "initialized"- ? renderDialog({ state: controllerState.state, closeDialog })- : null}+ {controllerState.status === "initialized" ? (+ // Keep the closed content mounted for Radix's exit animation, but+ // start a fresh content instance whenever the dialog opens again.+ <React.Fragment key={controllerState.openCount}>+ {renderDialog({ state: controllerState.state, closeDialog })}+ </React.Fragment>+ ) : null}</DialogPrimitive.Root>);}diff --git a/web/src/features/models/ModelDefinitionsTable/ConnectedModelDefinitionsTable.tsx b/web/src/features/models/ModelDefinitionsTable/ConnectedModelDefinitionsTable.tsxnew file mode 100644index 0000000000..9b544dde30--- /dev/null+++ b/web/src/features/models/ModelDefinitionsTable/ConnectedModelDefinitionsTable.tsx@@ -0,0 +1,235 @@+import { useRouter } from "next/router";+import { FlaskConical, PlusIcon } from "lucide-react";+import { useRef } from "react";+import { StringParam, useQueryParams, withDefault } from "use-query-params";++import { type AsyncTableData } from "@/src/components/design-system/table/Table";+import { DialogController } from "@/src/components/design-system/DialogController/DialogController";+import { ConfirmationDialogController } from "@/src/components/design-system/ConfirmationDialogController/ConfirmationDialogController";+import { useRowHeightLocalStorage } from "@/src/components/table/data-table-row-height-switch";+import { PriceUnitSelector } from "@/src/features/models/components/PriceUnitSelector";+import {+ UpsertModelFormDialogController,+ UpsertModelFormDialogContent,+} from "@/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialogController";+import { TestModelMatchDialogContent } from "@/src/features/models/components/test-match/TestModelMatchDialog";+import { usePriceUnitMultiplier } from "@/src/features/models/hooks/usePriceUnitMultiplier";+import { type GetModelResult } from "@/src/features/models/validation";+import { useHasProjectAccess } from "@/src/features/rbac";+import { usePostHogClientCapture } from "@/src/features/posthog-analytics";+import { usePaginationState } from "@/src/hooks/usePaginationState";+import { api, reportTrpcErrorWithoutToast } from "@/src/utils/api";+import {+ ModelDefinitionsTable,+ type ModelTableRow,+} from "./ModelDefinitionsTable";++export function ConnectedModelDefinitionsTable({+ projectId,+}: {+ projectId: string;+}) {+ const router = useRouter();+ const capture = usePostHogClientCapture();+ const utils = api.useUtils();+ const deleteModel = api.models.delete.useMutation({+ onSuccess: () => utils.models.invalidate(),+ });+ const editCloseGuard = useRef<() => boolean>(() => true);+ const [paginationState, setPaginationState] = usePaginationState(0, 50, {+ page: "pageIndex",+ limit: "pageSize",+ });+ const [queryParams, setQueryParams] = useQueryParams({+ search: withDefault(StringParam, ""),+ });+ const models = api.models.getAll.useQuery(+ {+ page: paginationState.pageIndex,+ limit: paginationState.pageSize,+ projectId,+ searchString: queryParams.search,+ },+ {+ refetchOnWindowFocus: false,+ refetchOnMount: true,+ refetchOnReconnect: false,+ staleTime: 1000 * 60 * 10,+ },+ );+ const modelIds = models.data?.models.map((model) => model.id) ?? [];+ const lastUsed = api.models.lastUsedByModelIds.useQuery(+ { projectId, modelIds },+ {+ enabled: models.isSuccess && modelIds.length > 0,+ refetchOnWindowFocus: false,+ refetchOnMount: true,+ refetchOnReconnect: false,+ staleTime: 1000 * 60 * 10,+ },+ );+ const { priceUnit, priceUnitMultiplier } = usePriceUnitMultiplier();+ const [rowHeight, setRowHeight] = useRowHeightLocalStorage("models", "m");+ const hasWriteAccess = useHasProjectAccess({+ projectId,+ scope: "models:CUD",+ });++ let data: AsyncTableData<ModelTableRow[]>;+ if (models.isPending) {+ data = { status: "loading" };+ } else if (models.isError) {+ data = { status: "error", error: models.error.message };+ } else {+ data = {+ status: "success",+ data: (models.data?.models ?? []).map(convertToTableRow),+ };+ }++ return (+ <DialogController+ renderDialog={({ closeDialog }) => (+ <TestModelMatchDialogContent+ projectId={projectId}+ open+ onClose={closeDialog}+ />+ )}+ >+ {({ openDialog: openTestMatchDialog }) => (+ <>+ <DialogController<{+ action: "edit" | "clone";+ model: GetModelResult;+ }>+ onBeforeClose={() => editCloseGuard.current()}+ renderDialog={({ state, closeDialog }) => (+ <UpsertModelFormDialogContent+ projectId={projectId}+ action={state.action}+ modelData={state.model}+ closeDialog={closeDialog}+ closeGuardRef={editCloseGuard}+ />+ )}+ >+ {({ openDialog }) => {+ const openCloneDialog = (model: ModelTableRow) => {+ openDialog({+ action: "clone",+ model: model.serverResponse,+ });+ };+ const openEditDialog = (model: ModelTableRow) => {+ openDialog({+ action: "edit",+ model: model.serverResponse,+ });+ };++ return (+ <UpsertModelFormDialogController+ projectId={projectId}+ action="create"+ >+ {({ openDialog: openCreateDialog }) => (+ <ConfirmationDialogController<GetModelResult>+ title="Delete model?"+ text="This action permanently deletes this model definition."+ confirmLabel="Delete model"+ variant="destructive"+ loading={deleteModel.isPending}+ error={deleteModel.error?.message}+ onAfterDismiss={deleteModel.reset}+ onConfirm={async (model) => {+ capture("models:delete_button_click");+ try {+ await deleteModel.mutateAsync({+ projectId,+ modelId: model.id,+ });+ } catch (error) {+ reportTrpcErrorWithoutToast(error, "models");+ throw error;+ }+ }}+ >+ {({ openDialog: openDeleteDialog }) => (+ <ModelDefinitionsTable+ data={data}+ pagination={{+ mode: "offset",+ totalCount: models.data?.totalCount ?? null,+ onChange: setPaginationState,+ state: paginationState,+ }}+ search={{+ value: queryParams.search,+ onChange: (search) => setQueryParams({ search }),+ }}+ rowHeight={rowHeight}+ onRowHeightChange={setRowHeight}+ priceUnit={priceUnit}+ priceUnitMultiplier={priceUnitMultiplier}+ priceUnitSelector={<PriceUnitSelector />}+ lastUsed={lastUsed.data}+ toolbarActions={[+ {+ id: "test-model-match",+ label: "Test Model Match",+ variant: "secondary",+ icon: <FlaskConical className="h-4 w-4" />,+ onClick: openTestMatchDialog,+ },+ {+ id: "add-model-definition",+ label: "Add Model Definition",+ variant: "secondary",+ icon: <PlusIcon className="h-4 w-4" />,+ hasAccess: hasWriteAccess,+ trackingEventName: "models:new_form_open",+ onClick: openCreateDialog,+ },+ ]}+ modelActions={{+ canEdit: hasWriteAccess,+ onClone: openCloneDialog,+ onEdit: openEditDialog,+ onDelete: (model) => {+ deleteModel.reset();+ openDeleteDialog(model.serverResponse);+ },+ }}+ onRowClick={(model) => {+ router.push(+ `/project/${projectId}/settings/models/${model.modelId}`,+ );+ }}+ />+ )}+ </ConfirmationDialogController>+ )}+ </UpsertModelFormDialogController>+ );+ }}+ </DialogController>+ </>+ )}+ </DialogController>+ );+}++function convertToTableRow(model: GetModelResult): ModelTableRow {+ const prices = model.pricingTiers.find((tier) => tier.isDefault)?.prices;+ return {+ modelId: model.id,+ maintainer: model.projectId ? "User" : "Langfuse",+ modelName: model.modelName,+ matchPattern: model.matchPattern,+ prices,+ tokenizerId: model.tokenizerId ?? undefined,+ config: model.tokenizerConfig,+ serverResponse: model,+ };+}diff --git a/web/src/features/models/ModelDefinitionsTable/ModelDefinitionsTable.stories.tsx b/web/src/features/models/ModelDefinitionsTable/ModelDefinitionsTable.stories.tsxnew file mode 100644index 0000000000..9a1c0f8813--- /dev/null+++ b/web/src/features/models/ModelDefinitionsTable/ModelDefinitionsTable.stories.tsx@@ -0,0 +1,77 @@+import { fn } from "storybook/test";++import preview from "../../../../.storybook/preview";+import { PriceUnit } from "@/src/features/models/validation";+import {+ ModelDefinitionsTable,+ type ModelTableRow,+} from "./ModelDefinitionsTable";++const meta = preview.meta({ component: ModelDefinitionsTable });++const model: ModelTableRow = {+ modelId: "model-1",+ maintainer: "Langfuse",+ modelName: "example-model",+ matchPattern: "^example-model$",+ prices: { input: 0.000001, output: 0.000002 },+ tokenizerId: "openai",+ config: null,+ serverResponse: {+ id: "model-1",+ projectId: null,+ modelName: "example-model",+ matchPattern: "^example-model$",+ tokenizerId: "openai",+ tokenizerConfig: null,+ pricingTiers: [],+ },+};++export const Default = meta.story({+ args: {+ data: {+ status: "success",+ data: [+ model,+ {+ ...model,+ modelId: "model-2",+ modelName: "custom-model",+ maintainer: "User",+ serverResponse: {+ ...model.serverResponse,+ id: "model-2",+ projectId: "project-1",+ },+ },+ ],+ },+ pagination: {+ mode: "offset",+ totalCount: 2,+ state: { pageIndex: 0, pageSize: 50 },+ onChange: fn(),+ },+ search: { value: "", onChange: fn() },+ rowHeight: "m",+ onRowHeightChange: fn(),+ priceUnit: PriceUnit.PerUnit,+ priceUnitMultiplier: 1,+ lastUsed: { "model-1": new Date("2026-09-01") },+ toolbarActions: [+ { id: "add-model", label: "Add Model Definition", onClick: fn() },+ ],+ modelActions: {+ canEdit: true,+ onClone: fn(),+ onEdit: fn(),+ onDelete: fn(),+ },+ onRowClick: fn(),+ },+});++export const Loading = meta.story({+ args: { ...Default.input.args, data: { status: "loading" } },+});diff --git a/web/src/features/models/ModelDefinitionsTable/ModelDefinitionsTable.tsx b/web/src/features/models/ModelDefinitionsTable/ModelDefinitionsTable.tsxnew file mode 100644index 0000000000..359a8b1bb6--- /dev/null+++ b/web/src/features/models/ModelDefinitionsTable/ModelDefinitionsTable.tsx@@ -0,0 +1,237 @@+import { type ReactNode } from "react";+import { type Prisma } from "@langfuse/shared/src/db";+import { Copy, Pencil, Trash } from "lucide-react";+import { type LangfuseColumnDef } from "@/src/components/table/types";+import { type PaginationBarProps } from "@/src/components/design-system/PaginationBar/PaginationBar";+import { type TableProps } from "@/src/components/design-system/table/Table";+import {+ SettingsTable,+ type SettingsTableProps,+} from "@/src/components/SettingsTable/SettingsTable";+import { type RowHeight } from "@/src/components/table/data-table-row-height-switch";+import { createIOTableColumn } from "@/src/components/design-system/table/columns/createIOTableColumn";+import { createBadgeTableColumn } from "@/src/components/design-system/table/columns/createBadgeTableColumn";+import { createDateTableColumn } from "@/src/components/design-system/table/columns/createDateTableColumn";+import { createTextTableColumn } from "@/src/components/design-system/table/columns/createTextTableColumn";+import {+ type GetModelResult,+ type PriceUnit,+} from "@/src/features/models/validation";+import { PriceBreakdownTooltip } from "@/src/features/models/components/PriceBreakdownTooltip";++export type ModelTableRow = {+ modelId: string;+ maintainer: "User" | "Langfuse";+ modelName: string;+ matchPattern: string;+ prices?: Record<string, number>;+ tokenizerId?: string;+ config?: Prisma.JsonValue;+ serverResponse: GetModelResult;+};++const modelConfigDescriptions = {+ modelName:+ "Standardized model name. Generations are assigned to this model name if they match the `matchPattern` upon ingestion.",+ matchPattern:+ "Regex pattern to match `model` parameter of generations to model pricing",+ startDate:+ "Date to start pricing model. If not set, model is active unless a more recent version exists.",+ prices: "Prices per usage type",+ tokenizerId:+ "Tokenizer used for this model to calculate token counts if none are ingested. Pick from list of supported tokenizers.",+ config:+ "Some tokenizers require additional configuration (e.g. openai tiktoken). See docs for details.",+ maintainer:+ "Maintainer of the model. Langfuse managed models can be cloned, user managed models can be edited and deleted. To supersede a Langfuse managed model, set the custom model name to the Langfuse model name.",+ lastUsed: "Start time of the latest generation using this model",+} as const;++export function ModelDefinitionsTable({+ data,+ pagination,+ search,+ rowHeight,+ onRowHeightChange,+ priceUnit,+ priceUnitMultiplier,+ priceUnitSelector,+ lastUsed,+ toolbarActions,+ modelActions,+ onRowClick,+}: {+ data: TableProps<ModelTableRow>["data"];+ pagination: PaginationBarProps;+ search: { value: string; onChange: (value: string) => void };+ rowHeight: RowHeight;+ onRowHeightChange: (height: RowHeight) => void;+ priceUnit: PriceUnit;+ priceUnitMultiplier: number;+ priceUnitSelector?: ReactNode;+ lastUsed?: Record<string, Date>;+ toolbarActions?: SettingsTableProps<ModelTableRow>["toolbarActions"];+ modelActions: {+ canEdit: boolean;+ onClone: (model: ModelTableRow) => void;+ onEdit: (model: ModelTableRow) => void;+ onDelete: (model: ModelTableRow) => void;+ };+ onRowClick: (model: ModelTableRow) => void;+}) {+ const columns: LangfuseColumnDef<ModelTableRow>[] = [+ createTextTableColumn<ModelTableRow>({+ accessorKey: "modelName",+ header: "Model Name",+ headerTooltip: {+ description: modelConfigDescriptions.modelName,+ },+ size: 120,+ }),+ createBadgeTableColumn<ModelTableRow>({+ accessorKey: "maintainer",+ header: "Maintainer",+ headerTooltip: {+ description: modelConfigDescriptions.maintainer,+ },+ size: 60,+ range: "decorative",+ getBadge: (maintainer) => ({+ value: maintainer,+ variant: maintainer === "Langfuse" ? "teal" : "blue",+ }),+ }),+ createTextTableColumn<ModelTableRow>({+ accessorKey: "matchPattern",+ headerTooltip: {+ description: modelConfigDescriptions.matchPattern,+ },+ header: "Match Pattern",+ size: 200,+ }),+ {+ accessorKey: "prices",+ id: "prices",+ header: () => {+ return (+ <div className="flex items-center gap-2">+ <span>Prices {priceUnit}</span>+ {priceUnitSelector}+ </div>+ );+ },+ size: 120,+ cell: ({ row }) => {+ const prices: Record<string, number> | undefined =+ row.getValue("prices");++ if (!prices) return;++ return (+ <PriceBreakdownTooltip+ modelName={row.original.modelName}+ prices={prices}+ priceUnit={priceUnit}+ priceUnitMultiplier={priceUnitMultiplier}+ rowHeight={rowHeight}+ />+ );+ },+ enableHiding: true,+ },+ createTextTableColumn<ModelTableRow>({+ accessorKey: "tokenizerId",+ header: "Tokenizer",+ headerTooltip: {+ description: modelConfigDescriptions.tokenizerId,+ },+ enableHiding: true,+ size: 120,+ }),+ createIOTableColumn<ModelTableRow>({+ accessorKey: "config",+ header: "Tokenizer Configuration",+ headerTooltip: {+ description: modelConfigDescriptions.config,+ },+ enableHiding: true,+ size: 120,+ getCell: (value) => value || undefined,+ singleLine: rowHeight === "s",+ }),+ createDateTableColumn<ModelTableRow>({+ accessorFn: () => undefined,+ id: "lastUsed",+ header: "Last used",+ emptyValue: "-",+ headerTooltip: {+ description: modelConfigDescriptions.lastUsed,+ },+ enableHiding: true,+ size: 120,+ getValue: (_, { row }) => {+ if (!lastUsed) return { type: "loading" };+ return lastUsed[row.original.modelId];+ },+ }),+ ];++ return (+ <SettingsTable+ tableName="models"+ columns={columns}+ actions={(model) =>+ model.maintainer === "Langfuse"+ ? [+ {+ id: "clone",+ type: "item",+ title: "Clone model",+ icon: Copy,+ disabled: modelActions.canEdit+ ? undefined+ : {+ reason: "You do not have permission to clone this model",+ },+ onClick: () => modelActions.onClone(model),+ },+ ]+ : [+ {+ id: "edit",+ type: "item",+ title: "Edit model",+ icon: Pencil,+ disabled: modelActions.canEdit+ ? undefined+ : { reason: "You do not have permission to edit this model" },+ onClick: () => modelActions.onEdit(model),+ },+ {+ id: "delete",+ type: "item",+ title: "Delete model",+ icon: Trash,+ variant: "destructive",+ disabled: modelActions.canEdit+ ? undefined+ : {+ reason: "You do not have permission to delete this model",+ },+ onClick: () => modelActions.onDelete(model),+ },+ ]+ }+ columnVisibilityKey="modelsColumnVisibility"+ columnOrderKey="modelsColumnOrder"+ data={data}+ pagination={pagination}+ loadingRowCount={Math.min(pagination.state.pageSize, 8)}+ search={{ ...search, placeholder: "Search models" }}+ rowHeight={rowHeight}+ rowHeightControl={{ rowHeight, onRowHeightChange }}+ toolbarActions={toolbarActions}+ onRowClick={onRowClick}+ />+ );+}diff --git a/web/src/features/models/ModelsTable.tsx b/web/src/features/models/ModelsTable.tsxdeleted file mode 100644index 950056b0f5..0000000000--- a/web/src/features/models/ModelsTable.tsx+++ /dev/null@@ -1,370 +0,0 @@-/* eslint-disable no-nested-ternary */-import { DataTable } from "@/src/components/table/data-table";-import { type LangfuseColumnDef } from "@/src/components/table/types";-import { createIOTableColumn } from "@/src/components/design-system/table/columns/createIOTableColumn";-import {- useColumnVisibility,- useColumnOrder,-} from "@/src/features/column-visibility";-import { api } from "@/src/utils/api";-import { safeExtract } from "@/src/utils/map-utils";-import { type Prisma } from "@langfuse/shared/src/db";-import { useQueryParams, withDefault, StringParam } from "use-query-params";-import { usePaginationState } from "@/src/hooks/usePaginationState";-import { useRowHeightLocalStorage } from "@/src/components/table/data-table-row-height-switch";-import { DataTableToolbar } from "@/src/components/table/data-table-toolbar";-import { type GetModelResult } from "@/src/features/models/validation";-import { DeleteModelButton } from "@/src/features/models/components/DeleteModelButton";-import { EditModelButton } from "@/src/features/models/components/EditModelButton";-import { CloneModelButton } from "@/src/features/models/components/CloneModelButton";-import { PriceBreakdownTooltip } from "@/src/features/models/components/PriceBreakdownTooltip";-import { UserCircle2Icon, PlusIcon } from "lucide-react";-import {- Tooltip,- TooltipContent,- TooltipTrigger,-} from "@/src/components/ui/tooltip";-import { createTextTableColumn } from "@/src/components/design-system/table/columns/createTextTableColumn";-import { LangfuseIcon } from "@/src/components/design-system/LangfuseIcon/LangfuseIcon";-import { useRouter } from "next/router";-import { PriceUnitSelector } from "@/src/features/models/components/PriceUnitSelector";-import { usePriceUnitMultiplier } from "@/src/features/models/hooks/usePriceUnitMultiplier";-import { UpsertModelFormDialog } from "@/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialog";-import { TestModelMatchButton } from "@/src/features/models/components/test-match/TestModelMatchButton";-import { ActionButton } from "@/src/components/ActionButton";-import { useHasProjectAccess } from "@/src/features/rbac";-import { SettingsTableCard } from "@/src/components/layouts/settings-table-card";--export type ModelTableRow = {- modelId: string;- maintainer: string;- modelName: string;- matchPattern: string;- prices?: Record<string, number>;- tokenizerId?: string;- config?: Prisma.JsonValue;- serverResponse: GetModelResult;-};--const modelConfigDescriptions = {- modelName:- "Standardized model name. Generations are assigned to this model name if they match the `matchPattern` upon ingestion.",- matchPattern:- "Regex pattern to match `model` parameter of generations to model pricing",- startDate:- "Date to start pricing model. If not set, model is active unless a more recent version exists.",- prices: "Prices per usage type",- tokenizerId:- "Tokenizer used for this model to calculate token counts if none are ingested. Pick from list of supported tokenizers.",- config:- "Some tokenizers require additional configuration (e.g. openai tiktoken). See docs for details.",- maintainer:- "Maintainer of the model. Langfuse managed models can be cloned, user managed models can be edited and deleted. To supersede a Langfuse managed model, set the custom model name to the Langfuse model name.",- lastUsed: "Start time of the latest generation using this model",-} as const;--export default function ModelsTable({ projectId }: { projectId: string }) {- const router = useRouter();- const [paginationState, setPaginationState] = usePaginationState(0, 50, {- page: "pageIndex",- limit: "pageSize",- });- const [queryParams, setQueryParams] = useQueryParams({- search: withDefault(StringParam, ""),- });- const searchString = queryParams.search;- const models = api.models.getAll.useQuery(- {- page: paginationState.pageIndex,- limit: paginationState.pageSize,- projectId,- searchString,- },- {- refetchOnWindowFocus: false,- refetchOnMount: true,- refetchOnReconnect: false,- staleTime: 1000 * 60 * 10,- },- );- const totalCount = models.data?.totalCount ?? null;-- const modelIds = models.data?.models.map((m) => m.id) ?? [];- const lastUsed = api.models.lastUsedByModelIds.useQuery(- { projectId, modelIds },- {- enabled: models.isSuccess && modelIds.length > 0,- refetchOnWindowFocus: false,- refetchOnMount: true,- refetchOnReconnect: false,- staleTime: 1000 * 60 * 10,- },- );- const { priceUnit } = usePriceUnitMultiplier();- const [rowHeight, setRowHeight] = useRowHeightLocalStorage("models", "m");-- const hasWriteAccess = useHasProjectAccess({- projectId,- scope: "models:CUD",- });-- const columns: LangfuseColumnDef<ModelTableRow>[] = [- {- accessorKey: "modelName",- id: "modelName",- header: "Model Name",- headerTooltip: {- description: modelConfigDescriptions.modelName,- },- cell: ({ row }) => {- return (- <span- className="truncate font-mono text-xs font-bold"- title={row.original.modelName}- >- {row.original.modelName}- </span>- );- },- size: 120,- },- {- accessorKey: "maintainer",- id: "maintainer",- header: "Maintainer",- headerTooltip: {- description: modelConfigDescriptions.maintainer,- },- size: 60,- cell: ({ row }) => {- const isLangfuse = row.original.maintainer === "Langfuse";- return (- <div className="flex justify-center">- <Tooltip>- <TooltipTrigger>- {isLangfuse ? (- <LangfuseIcon size={16} />- ) : (- <UserCircle2Icon className="h-4 w-4" />- )}- </TooltipTrigger>- <TooltipContent>- {isLangfuse ? "Langfuse maintained" : "User maintained"}- </TooltipContent>- </Tooltip>- </div>- );- },- },- {- accessorKey: "matchPattern",- id: "matchPattern",- headerTooltip: {- description: modelConfigDescriptions.matchPattern,- },- header: "Match Pattern",- size: 200,- cell: ({ row }) => {- const value: string = row.getValue("matchPattern");-- return value ? (- <span className="truncate font-mono text-xs" title={value}>- {value}- </span>- ) : null;- },- },- {- accessorKey: "prices",- id: "prices",- header: () => {- return (- <div className="flex items-center gap-2">- <span>Prices {priceUnit}</span>- <PriceUnitSelector />- </div>- );- },- size: 120,- cell: ({ row }) => {- const prices: Record<string, number> | undefined =- row.getValue("prices");-- if (!prices) return;-- return (- <PriceBreakdownTooltip- modelName={row.original.modelName}- prices={prices}- priceUnit={priceUnit}- rowHeight={rowHeight}- />- );- },- enableHiding: true,- },- createTextTableColumn<ModelTableRow>({- accessorKey: "tokenizerId",- header: "Tokenizer",- headerTooltip: {- description: modelConfigDescriptions.tokenizerId,- },- enableHiding: true,- size: 120,- }),- createIOTableColumn<ModelTableRow>({- accessorKey: "config",- header: "Tokenizer Configuration",- headerTooltip: {- description: modelConfigDescriptions.config,- },- enableHiding: true,- size: 120,- getCell: (value) => value || undefined,- singleLine: rowHeight === "s",- }),- createTextTableColumn<ModelTableRow>({- accessorFn: () => undefined,- id: "lastUsed",- header: "Last used",- headerTooltip: {- description: modelConfigDescriptions.lastUsed,- },- enableHiding: true,- size: 120,- mapValue: (_, { row }) => {- if (!lastUsed.data) return { type: "loading" };- return lastUsed.data[row.original.modelId]?.toLocaleString() ?? "";- },- }),- {- accessorKey: "actions",- header: "Actions",- size: 120,- cell: ({ row }) => {- return row.original.maintainer !== "Langfuse" ? (- <div- className="flex items-center gap-2"- onClick={(e) => e.stopPropagation()}- >- <EditModelButton- projectId={projectId}- modelData={row.original.serverResponse}- />- <DeleteModelButton- projectId={projectId}- modelData={row.original.serverResponse}- />- </div>- ) : (- <div onClick={(e) => e.stopPropagation()}>- <CloneModelButton- projectId={projectId}- modelData={row.original.serverResponse}- />- </div>- );- },- },- ];-- const [columnVisibility, setColumnVisibility] =- useColumnVisibility<ModelTableRow>("modelsColumnVisibility", columns);-- const [columnOrder, setColumnOrder] = useColumnOrder<ModelTableRow>(- "modelsColumnOrder",- columns,- );-- const convertToTableRow = (model: GetModelResult): ModelTableRow => {- // Get default tier prices for backward compatibility- const defaultTier = model.pricingTiers.find((t) => t.isDefault);- const prices = defaultTier?.prices;-- return {- modelId: model.id,- maintainer: model.projectId ? "User" : "Langfuse",- modelName: model.modelName,- matchPattern: model.matchPattern,- prices,- tokenizerId: model.tokenizerId ?? undefined,- config: model.tokenizerConfig,- serverResponse: model,- };- };-- return (- <>- <DataTableToolbar- tableName="models"- columns={columns}- columnVisibility={columnVisibility}- setColumnVisibility={setColumnVisibility}- columnOrder={columnOrder}- setColumnOrder={setColumnOrder}- rowHeight={rowHeight}- setRowHeight={setRowHeight}- searchConfig={{- updateQuery: (event: string) => {- setQueryParams({ search: event });- },- tableAllowsFullTextSearch: true,- currentQuery: searchString,- }}- actionButtons={- <>- <TestModelMatchButton projectId={projectId} />- <UpsertModelFormDialog {...{ projectId, action: "create" }}>- <ActionButton- variant="secondary"- icon={<PlusIcon className="h-4 w-4" />}- hasAccess={hasWriteAccess}- trackingEventName="models:new_form_open"- >- Add Model Definition- </ActionButton>- </UpsertModelFormDialog>- </>- }- className="px-0"- />- <SettingsTableCard className="max-h-[75dvh]">- <DataTable- tableName="models"- columns={columns}- data={- models.isPending- ? { isLoading: true, isError: false }- : models.isError- ? {- isLoading: false,- isError: true,- error: models.error.message,- }- : {- isLoading: false,- isError: false,- data: safeExtract(models.data, "models", []).map((t) =>- convertToTableRow(t),- ),- }- }- pagination={{- totalCount,- onChange: setPaginationState,- state: paginationState,- }}- columnVisibility={columnVisibility}- onColumnVisibilityChange={setColumnVisibility}- columnOrder={columnOrder}- onColumnOrderChange={setColumnOrder}- rowHeight={rowHeight}- cellPadding="comfortable"- onRowClick={(row) => {- router.push(`/project/${projectId}/settings/models/${row.modelId}`);- }}- />- </SettingsTableCard>- </>- );-}diff --git a/web/src/features/models/components/CloneModelButton.tsx b/web/src/features/models/components/CloneModelButton.tsxindex 3f1d41b9c6..bce5fa5751 100644--- a/web/src/features/models/components/CloneModelButton.tsx+++ b/web/src/features/models/components/CloneModelButton.tsx@@ -1,6 +1,6 @@import { Button } from "@/src/components/ui/button";import { useHasProjectAccess } from "@/src/features/rbac";-import { UpsertModelFormDialog } from "@/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialog";+import { UpsertModelFormDialogController } from "@/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialogController";import { type GetModelResult } from "@/src/features/models/validation";export const CloneModelButton = ({@@ -16,15 +16,20 @@ export const CloneModelButton = ({});return (- <UpsertModelFormDialog {...{ modelData, projectId, action: "clone" }}>- <Button- variant="outline"- disabled={!hasAccess}- title="Clone model"- className="flex items-center"- >- <span>Clone</span>- </Button>- </UpsertModelFormDialog>+ <UpsertModelFormDialogController+ {...{ modelData, projectId, action: "clone" }}+ >+ {({ openDialog }) => (+ <Button+ variant="outline"+ disabled={!hasAccess}+ title="Clone model"+ className="flex items-center"+ onClick={openDialog}+ >+ <span>Clone</span>+ </Button>+ )}+ </UpsertModelFormDialogController>);};diff --git a/web/src/features/models/components/EditModelButton.tsx b/web/src/features/models/components/EditModelButton.tsxindex 450cd9b010..9f9af0424f 100644--- a/web/src/features/models/components/EditModelButton.tsx+++ b/web/src/features/models/components/EditModelButton.tsx@@ -1,6 +1,6 @@import { Button } from "@/src/components/ui/button";import { useHasProjectAccess } from "@/src/features/rbac";-import { UpsertModelFormDialog } from "@/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialog";+import { UpsertModelFormDialogController } from "@/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialogController";import { type GetModelResult } from "@/src/features/models/validation";export const EditModelButton = ({@@ -16,15 +16,20 @@ export const EditModelButton = ({});return (- <UpsertModelFormDialog {...{ modelData, projectId, action: "edit" }}>- <Button- variant="outline"- disabled={!hasAccess}- title="Edit model"- className="flex items-center"- >- <span>Edit</span>- </Button>- </UpsertModelFormDialog>+ <UpsertModelFormDialogController+ {...{ modelData, projectId, action: "edit" }}+ >+ {({ openDialog }) => (+ <Button+ variant="outline"+ disabled={!hasAccess}+ title="Edit model"+ className="flex items-center"+ onClick={openDialog}+ >+ <span>Edit</span>+ </Button>+ )}+ </UpsertModelFormDialogController>);};diff --git a/web/src/features/models/components/ModelSettings.tsx b/web/src/features/models/components/ModelSettings.tsxindex 7bc828c307..f9bb3ba66d 100644--- a/web/src/features/models/components/ModelSettings.tsx+++ b/web/src/features/models/components/ModelSettings.tsx@@ -1,5 +1,5 @@import Header from "@/src/components/layouts/header";-import ModelsTable from "@/src/features/models/ModelsTable";+import { ConnectedModelDefinitionsTable } from "@/src/features/models/ModelDefinitionsTable/ConnectedModelDefinitionsTable";export function ModelsSettings(props: { projectId: string }) {return (@@ -11,7 +11,7 @@ export function ModelsSettings(props: { projectId: string }) {Langfuse to automatically calculate the price of generations based ontoken usage.</p>- <ModelsTable projectId={props.projectId} />+ <ConnectedModelDefinitionsTable projectId={props.projectId} /></>);}diff --git a/web/src/features/models/components/PriceBreakdownTooltip.tsx b/web/src/features/models/components/PriceBreakdownTooltip.tsxindex e79ccbc44d..4c84e722b5 100644--- a/web/src/features/models/components/PriceBreakdownTooltip.tsx+++ b/web/src/features/models/components/PriceBreakdownTooltip.tsx@@ -10,7 +10,6 @@ import {TooltipProvider,TooltipTrigger,} from "@/src/components/ui/tooltip";-import { usePriceUnitMultiplier } from "@/src/features/models/hooks/usePriceUnitMultiplier";import { getMaxDecimals } from "@/src/features/models/fns/getMaxDecimals";import { type PriceUnit } from "@/src/features/models/validation";@@ -18,15 +17,16 @@ export const PriceBreakdownTooltip = ({modelName,prices,priceUnit,+ priceUnitMultiplier,rowHeight,}: {modelName: string;prices: Record<string, number>;priceUnit: PriceUnit;+ priceUnitMultiplier: number;rowHeight: RowHeight;}) => {const [isOpen, setIsOpen] = useState(false);- const { priceUnitMultiplier } = usePriceUnitMultiplier();const maxDecimals = useMemo(() =>diff --git a/web/src/features/models/components/ProvidedModelNameCell.tsx b/web/src/features/models/components/ProvidedModelNameCell.tsxindex 283c4becf3..9500a47e8a 100644--- a/web/src/features/models/components/ProvidedModelNameCell.tsx+++ b/web/src/features/models/components/ProvidedModelNameCell.tsx@@ -1,6 +1,6 @@import { PlusCircle } from "lucide-react";import { IdTableCell } from "@/src/components/design-system/table/components/IdTableCell/IdTableCell";-import { UpsertModelFormDialog } from "@/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialog";+import { UpsertModelFormDialogController } from "@/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialogController";/*** Renders the "Provided Model Name" cell shared by the generations and events@@ -48,27 +48,25 @@ export function ProvidedModelNameCell({: undefined;return (- <UpsertModelFormDialog+ <UpsertModelFormDialogControlleraction="create"projectId={projectId}prefilledModelData={{ modelName, prices }}- className="cursor-pointer">- {/*- Native <button> (not a styled span): DialogTrigger's Slot only forwards- onClick, so Enter/Space activation must come from the element itself.- A real button also matches the table row's interactive-skip selector, so- activating it never opens the row's peek view.- */}- <button- type="button"- title={`Add a model definition for "${modelName}"`}- onClick={(e) => e.stopPropagation()}- className="inline-flex max-w-full min-w-0 cursor-pointer items-center gap-1 text-left"- >- <IdTableCell value={modelName} />- <PlusCircle className="h-3.5 w-3.5 shrink-0" />- </button>- </UpsertModelFormDialog>+ {({ openDialog }) => (+ <button+ type="button"+ title={`Add a model definition for "${modelName}"`}+ onClick={(e) => {+ e.stopPropagation();+ openDialog();+ }}+ className="inline-flex max-w-full min-w-0 cursor-pointer items-center gap-1 text-left"+ >+ <IdTableCell value={modelName} />+ <PlusCircle className="h-3.5 w-3.5 shrink-0" />+ </button>+ )}+ </UpsertModelFormDialogController>);}diff --git a/web/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialog.tsx b/web/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialog.tsxdeleted file mode 100644index 93f92f7494..0000000000--- a/web/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialog.tsx+++ /dev/null@@ -1,363 +0,0 @@-/* eslint-disable no-nested-ternary */-/* eslint-disable @repo/no-abstracted-overlay-trigger */-/* eslint @repo/no-style-props: "off" */-import cloneDeep from "lodash/cloneDeep";-import isEqual from "lodash/isEqual";-import Link from "next/link";-import { useState } from "react";-import { useForm } from "react-hook-form";--import { CodeMirrorEditor } from "@/src/components/editor";-import { Button } from "@/src/components/ui/button";-import {- Dialog,- DialogContent,- DialogDescription,- DialogFooter,- DialogHeader,- DialogTitle,- DialogTrigger,- DialogBody,-} from "@/src/components/ui/dialog";-import {- Form,- FormControl,- FormDescription,- FormField,- FormItem,- FormLabel,- FormMessage,-} from "@/src/components/ui/form";-import { Input } from "@/src/components/ui/input";-import {- Select,- SelectContent,- SelectItem,- SelectTrigger,- SelectValue,-} from "@/src/components/ui/select";-import { buildFormValues } from "@/src/features/models/fns/buildFormValues";-import { matchPatternFor } from "@/src/features/models/fns/matchPatternFor";-import { toPricingTierInputs } from "@/src/features/models/fns/toPricingTierInputs";-import {- type FormUpsertModel,- FormUpsertModelSchema,- type GetModelResult,-} from "@/src/features/models/validation";-import { usePostHogClientCapture } from "@/src/features/posthog-analytics";-import { api } from "@/src/utils/api";-import { zodResolver } from "@hookform/resolvers/zod";-import { useRouter } from "next/router";--import { showSuccessToast } from "@/src/features/notifications";-import { PricingSection } from "@/src/features/models/components/PricingSection/PricingSection";--type UpsertModelDialogProps =- | {- action: "create";- children: React.ReactNode;- projectId: string;- prefilledModelData?: {- modelName?: string;- prices?: Record<string, number>;- };- className?: string;- }- | {- action: "edit" | "clone";- children: React.ReactNode;- projectId: string;- modelData: GetModelResult;- className?: string;- };--const DISCARD_CHANGES_MESSAGE =- "Discard your unsaved changes to this model definition?";--export const UpsertModelFormDialog = (({- children,- ...props-}: UpsertModelDialogProps) => {- const capture = usePostHogClientCapture();- const router = useRouter();- const [formError, setFormError] = useState<string | null>(null);- const utils = api.useUtils();- const [open, setOpen] = useState(false);-- const form = useForm({- resolver: zodResolver(FormUpsertModelSchema),- defaultValues: buildFormValues(props),- });-- const tokenizerId = form.watch("tokenizerId");-- const upsertModelMutation = api.models.upsert.useMutation();-- const onSubmit = async (values: FormUpsertModel) => {- capture("models:new_form_submit");- setFormError(null);- // Snapshot before the await: nothing typed while the save is in flight may- // become part of the saved baseline, or the dirty guard would let it go.- // getValues() copies only the top level, so the clone is load-bearing.- const submitted = cloneDeep(form.getValues());-- try {- const upsertedModel = await upsertModelMutation.mutateAsync({- modelId: props.action === "edit" ? props.modelData.id : null,- projectId: props.projectId,- modelName:- props.action === "edit"- ? props.modelData.modelName- : values.modelName,- matchPattern: values.matchPattern,- pricingTiers: toPricingTierInputs(values),- tokenizerId: values.tokenizerId,- tokenizerConfig:- values.tokenizerConfig &&- typeof JSON.parse(values.tokenizerConfig) === "object"- ? (JSON.parse(values.tokenizerConfig) as Record<string, number>)- : undefined,- });-- utils.models.invalidate();- showSuccessToast({- title: `Model ${props.action === "edit" ? "updated" : "created"}`,- description: `The model '${upsertedModel.modelName}' has been successfully ${props.action === "edit" ? "updated" : "created"}. New generations will use these model prices.`,- });-- // Editing is a checkpoint, not an exit: keep the dialog and its context.- if (props.action === "edit") {- const live = form.getValues();- form.reset(submitted); // what we saved is the new clean baseline- if (!isEqual(live, submitted)) {- // Typed while the save was in flight: still the user's unsaved work.- form.reset(live, { keepDefaultValues: true });- }- return;- }-- setOpen(false);- router.push(- `/project/${props.projectId}/settings/models/${upsertedModel.id}`,- );- } catch (error) {- setFormError(error instanceof Error ? error.message : String(error));- }- };-- const requestClose = () => {- if (form.formState.isDirty && !window.confirm(DISCARD_CHANGES_MESSAGE)) {- return;- }- setOpen(false);- };-- return (- <Dialog- open={open}- onOpenChange={(newOpen) => {- if (!newOpen) {- requestClose();- return;- }- // Reopening starts from the model's current data, not the last edit.- form.reset(buildFormValues(props));- setFormError(null);- setOpen(true);- }}- >- <DialogTrigger- asChild- onClick={() => setOpen(true)}- className={props.className}- title={- props.action === "create"- ? "Create model definition"- : "Edit model definition"- }- >- {children}- </DialogTrigger>- <DialogContent size="lg">- <DialogHeader>- <DialogTitle>- {props.action === "create"- ? "Create Model"- : props.action === "clone"- ? "Clone Model"- : "Edit Model"}- </DialogTitle>- {props.action === "edit" && (- <DialogDescription>{props.modelData.modelName}</DialogDescription>- )}- {props.action === "create" && (- <DialogDescription>- Create a new model configuration to track generation costs.- </DialogDescription>- )}- </DialogHeader>- <Form {...form}>- <form- onSubmit={form.handleSubmit(onSubmit)}- className="flex flex-1 flex-col overflow-hidden"- >- <DialogBody className="space-y-6">- <FormField- control={form.control}- name="modelName"- disabled={props.action === "edit"}- render={({ field }) => (- <FormItem>- <FormLabel>Model Name</FormLabel>- <FormDescription>- The name of the model. This will be used to reference the- model in the API. You can track price changes of models by- using the same name and match pattern.- </FormDescription>- <FormControl>- <Input- {...field}- onChange={(e) => {- const previousName = form.getValues("modelName");- const nextName = e.target.value;- field.onChange(e);-- // Follow the name only while the pattern is still the- // one we generated — a hand-written or cloned pattern- // is the user's, and typing a name must not touch it.- const pattern = form.getValues("matchPattern");- if (- !pattern ||- pattern === matchPatternFor(previousName)- ) {- form.setValue(- "matchPattern",- nextName ? matchPatternFor(nextName) : "",- );- }- }}- />- </FormControl>- <FormMessage />- </FormItem>- )}- />- <FormField- control={form.control}- name="matchPattern"- render={({ field }) => (- <FormItem>- <FormLabel>Match pattern</FormLabel>- <FormDescription>- Regular expression (Postgres syntax) to match ingested- generations (model attribute) to this model definition.- For an exact, case-insensitive match to a model name, use- the expression: (?i)^(modelname)$- </FormDescription>- <FormControl>- <Input {...field} />- </FormControl>- <FormMessage />- </FormItem>- )}- />-- <PricingSection form={form} />-- <FormField- control={form.control}- name="tokenizerId"- render={({ field }) => (- <FormItem>- <FormLabel>Tokenizer</FormLabel>- <Select- onValueChange={(tokenizerId) => {- field.onChange(tokenizerId);- if (tokenizerId === "None") {- form.setValue("tokenizerConfig", "{}");- }- }}- defaultValue={field.value ?? "None"}- >- <FormControl>- <SelectTrigger>- <SelectValue placeholder="Select a unit" />- </SelectTrigger>- </FormControl>- <SelectContent>- {["openai", "claude", "None"].map((unit) => (- <SelectItem value={unit} key={unit}>- {unit}- </SelectItem>- ))}- </SelectContent>- </Select>- <FormDescription>- Optionally, Langfuse can tokenize the input and output of- a generation if no unit counts are ingested. This is- useful for e.g. streamed OpenAI completions. For details- on the supported tokenizers, see the{" "}- <Link- href="https://langfuse.com/docs/model-usage-and-cost"- className="underline"- target="_blank"- >- docs- </Link>- .- </FormDescription>- <FormMessage />- </FormItem>- )}- />- {tokenizerId && tokenizerId !== "None" && (- <FormField- control={form.control}- name="tokenizerConfig"- render={({ field }) => (- <FormItem>- <FormLabel>Tokenizer Config</FormLabel>- <CodeMirrorEditor- mode="json"- value={field.value ?? "{}"}- onChange={field.onChange}- />- <FormDescription>- The config for the tokenizer. Required for openai. See- the{" "}- <Link- href="https://langfuse.com/docs/model-usage-and-cost"- className="underline"- target="_blank"- >- docs- </Link>{" "}- for details.- </FormDescription>- <FormMessage />- </FormItem>- )}- />- )}- </DialogBody>-- <DialogFooter>- <Button type="button" variant="outline" onClick={requestClose}>- Cancel- </Button>-- <Button type="submit" loading={upsertModelMutation.isPending}>- {props.action === "edit" ? "Save" : "Submit"}- </Button>- </DialogFooter>- </form>- {formError ? (- <p className="text-destructive my-2 text-center text-sm font-bold">- <span className="font-bold">Error:</span> {formError}- </p>- ) : null}- </Form>- </DialogContent>- </Dialog>- );-}) as React.FC<UpsertModelDialogProps>;diff --git a/web/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialog.clienttest.tsx b/web/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialogController.clienttest.tsxsimilarity index 90%rename from web/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialog.clienttest.tsxrename to web/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialogController.clienttest.tsxindex 6194a9c273..caf73959e9 100644--- a/web/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialog.clienttest.tsx+++ b/web/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialogController.clienttest.tsx@@ -10,7 +10,7 @@ import {import { type GetModelResult } from "@/src/features/models/validation";import { ModelBadge } from "@/src/features/traces/components/ObservationDetailView/components/ModelBadge";-import { UpsertModelFormDialog } from "./UpsertModelFormDialog";+import { UpsertModelFormDialogController } from "./UpsertModelFormDialogController";const upsertMutateAsync = vi.fn().mockResolvedValue({id: "model-1",@@ -122,26 +122,26 @@ const priceInputs = () =>const openEditDialog = (pricingTiers: GetModelResult["pricingTiers"]) => {render(- <UpsertModelFormDialog+ <UpsertModelFormDialogControlleraction="edit"projectId="p1"modelData={modelData(pricingTiers)}>- <button>Open editor</button>- </UpsertModelFormDialog>,+ {({ openDialog }) => <button onClick={openDialog}>Open editor</button>}+ </UpsertModelFormDialogController>,);fireEvent.click(screen.getByRole("button", { name: "Open editor" }));};const openCloneDialog = (matchPattern: string) => {render(- <UpsertModelFormDialog+ <UpsertModelFormDialogControlleraction="clone"projectId="p1"modelData={{ ...modelData([defaultTier]), matchPattern }}>- <button>Open editor</button>- </UpsertModelFormDialog>,+ {({ openDialog }) => <button onClick={openDialog}>Open editor</button>}+ </UpsertModelFormDialogController>,);fireEvent.click(screen.getByRole("button", { name: "Open editor" }));};@@ -149,7 +149,7 @@ const openCloneDialog = (matchPattern: string) => {const submit = () =>fireEvent.click(screen.getByRole("button", { name: /^(Save|Submit)$/ }));-describe("UpsertModelFormDialog price editor", () => {+describe("UpsertModelFormDialogController price editor", () => {beforeAll(() => {vi.stubGlobal("ResizeObserver",@@ -311,6 +311,17 @@ describe("UpsertModelFormDialog price editor", () => {confirm.mockRestore();});+ it("reopens with the original values after discarding an edit", () => {+ const confirm = vi.spyOn(window, "confirm").mockReturnValue(true);+ openEditDialog([defaultTier]);+ retype(priceInputs()[0], "0.000009");+ fireEvent.click(screen.getByRole("button", { name: "Cancel" }));++ fireEvent.click(screen.getByRole("button", { name: "Open editor" }));+ expect(priceInputs()[0].value).toBe("0.000004");+ confirm.mockRestore();+ });+it("shows inline errors instead of silently refusing to submit", async () => {openEditDialog([defaultTier]);diff --git a/web/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialogController.tsx b/web/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialogController.tsxnew file mode 100644index 0000000000..e98fd8f7f2--- /dev/null+++ b/web/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialogController.tsx@@ -0,0 +1,361 @@+/* eslint-disable no-nested-ternary */+/* eslint @repo/no-style-props: "off" */+import cloneDeep from "lodash/cloneDeep";+import isEqual from "lodash/isEqual";+import Link from "next/link";+import { useImperativeHandle, useRef, useState, type Ref } from "react";+import { useForm } from "react-hook-form";++import { CodeMirrorEditor } from "@/src/components/editor";+import { DialogController } from "@/src/components/design-system/DialogController/DialogController";+import { Button } from "@/src/components/ui/button";+import {+ DialogContent,+ DialogDescription,+ DialogFooter,+ DialogHeader,+ DialogTitle,+ DialogBody,+} from "@/src/components/ui/dialog";+import {+ Form,+ FormControl,+ FormDescription,+ FormField,+ FormItem,+ FormLabel,+ FormMessage,+} from "@/src/components/ui/form";+import { Input } from "@/src/components/ui/input";+import {+ Select,+ SelectContent,+ SelectItem,+ SelectTrigger,+ SelectValue,+} from "@/src/components/ui/select";+import { buildFormValues } from "@/src/features/models/fns/buildFormValues";+import { matchPatternFor } from "@/src/features/models/fns/matchPatternFor";+import { toPricingTierInputs } from "@/src/features/models/fns/toPricingTierInputs";+import {+ type FormUpsertModel,+ FormUpsertModelSchema,+ type GetModelResult,+} from "@/src/features/models/validation";+import { usePostHogClientCapture } from "@/src/features/posthog-analytics";+import { api } from "@/src/utils/api";+import { zodResolver } from "@hookform/resolvers/zod";+import { useRouter } from "next/router";++import { showSuccessToast } from "@/src/features/notifications";+import { PricingSection } from "@/src/features/models/components/PricingSection/PricingSection";++type DialogChild = (control: { openDialog: () => void }) => React.ReactNode;+type ModelFormSource =+ | {+ action: "create";+ projectId: string;+ prefilledModelData?: {+ modelName?: string;+ prices?: Record<string, number>;+ };+ }+ | {+ action: "edit" | "clone";+ projectId: string;+ modelData: GetModelResult;+ };+type UpsertModelDialogProps = ModelFormSource & { children: DialogChild };++const DISCARD_CHANGES_MESSAGE =+ "Discard your unsaved changes to this model definition?";++export const UpsertModelFormDialogController = (({+ children,+ ...props+}: UpsertModelDialogProps) => {+ const guard = useRef<() => boolean>(() => true);++ return (+ <DialogController<true>+ onBeforeClose={() => guard.current()}+ renderDialog={({ closeDialog }) => (+ <UpsertModelFormDialogContent+ {...props}+ closeDialog={closeDialog}+ closeGuardRef={guard}+ />+ )}+ >+ {({ openDialog }) =>+ children({+ openDialog: () => {+ openDialog(true);+ },+ })+ }+ </DialogController>+ );+}) as React.FC<UpsertModelDialogProps>;++export function UpsertModelFormDialogContent({+ closeDialog,+ closeGuardRef,+ ...props+}: ModelFormSource & {+ closeDialog: () => void;+ closeGuardRef?: Ref<() => boolean>;+}) {+ const capture = usePostHogClientCapture();+ const router = useRouter();+ const [formError, setFormError] = useState<string | null>(null);+ const utils = api.useUtils();+ const form = useForm({+ resolver: zodResolver(FormUpsertModelSchema),+ defaultValues: buildFormValues(props),+ });+ useImperativeHandle(+ closeGuardRef,+ () => () =>+ !form.formState.isDirty || window.confirm(DISCARD_CHANGES_MESSAGE),+ );++ const tokenizerId = form.watch("tokenizerId");++ const upsertModelMutation = api.models.upsert.useMutation();++ const onSubmit = async (values: FormUpsertModel) => {+ capture("models:new_form_submit");+ setFormError(null);+ // Snapshot before the await: nothing typed while the save is in flight may+ // become part of the saved baseline, or the dirty guard would let it go.+ // getValues() copies only the top level, so the clone is load-bearing.+ const submitted = cloneDeep(form.getValues());++ try {+ const upsertedModel = await upsertModelMutation.mutateAsync({+ modelId: props.action === "edit" ? props.modelData.id : null,+ projectId: props.projectId,+ modelName:+ props.action === "edit"+ ? props.modelData.modelName+ : values.modelName,+ matchPattern: values.matchPattern,+ pricingTiers: toPricingTierInputs(values),+ tokenizerId: values.tokenizerId,+ tokenizerConfig:+ values.tokenizerConfig &&+ typeof JSON.parse(values.tokenizerConfig) === "object"+ ? (JSON.parse(values.tokenizerConfig) as Record<string, number>)+ : undefined,+ });++ utils.models.invalidate();+ showSuccessToast({+ title: `Model ${props.action === "edit" ? "updated" : "created"}`,+ description: `The model '${upsertedModel.modelName}' has been successfully ${props.action === "edit" ? "updated" : "created"}. New generations will use these model prices.`,+ });++ // Editing is a checkpoint, not an exit: keep the dialog and its context.+ if (props.action === "edit") {+ const live = form.getValues();+ form.reset(submitted); // what we saved is the new clean baseline+ if (!isEqual(live, submitted)) {+ // Typed while the save was in flight: still the user's unsaved work.+ form.reset(live, { keepDefaultValues: true });+ }+ return;+ }++ form.reset(submitted);+ closeDialog();+ router.push(+ `/project/${props.projectId}/settings/models/${upsertedModel.id}`,+ );+ } catch (error) {+ setFormError(error instanceof Error ? error.message : String(error));+ }+ };++ return (+ <DialogContent size="lg">+ <DialogHeader>+ <DialogTitle>+ {props.action === "create"+ ? "Create Model"+ : props.action === "clone"+ ? "Clone Model"+ : "Edit Model"}+ </DialogTitle>+ {props.action === "edit" && (+ <DialogDescription>{props.modelData.modelName}</DialogDescription>+ )}+ {props.action === "create" && (+ <DialogDescription>+ Create a new model configuration to track generation costs.+ </DialogDescription>+ )}+ </DialogHeader>+ <Form {...form}>+ <form+ onSubmit={form.handleSubmit(onSubmit)}+ className="flex flex-1 flex-col overflow-hidden"+ >+ <DialogBody className="space-y-6">+ <FormField+ control={form.control}+ name="modelName"+ disabled={props.action === "edit"}+ render={({ field }) => (+ <FormItem>+ <FormLabel>Model Name</FormLabel>+ <FormDescription>+ The name of the model. This will be used to reference the+ model in the API. You can track price changes of models by+ using the same name and match pattern.+ </FormDescription>+ <FormControl>+ <Input+ {...field}+ onChange={(e) => {+ const previousName = form.getValues("modelName");+ const nextName = e.target.value;+ field.onChange(e);++ // Follow the name only while the pattern is still the+ // one we generated — a hand-written or cloned pattern+ // is the user's, and typing a name must not touch it.+ const pattern = form.getValues("matchPattern");+ if (+ !pattern ||+ pattern === matchPatternFor(previousName)+ ) {+ form.setValue(+ "matchPattern",+ nextName ? matchPatternFor(nextName) : "",+ );+ }+ }}+ />+ </FormControl>+ <FormMessage />+ </FormItem>+ )}+ />+ <FormField+ control={form.control}+ name="matchPattern"+ render={({ field }) => (+ <FormItem>+ <FormLabel>Match pattern</FormLabel>+ <FormDescription>+ Regular expression (Postgres syntax) to match ingested+ generations (model attribute) to this model definition. For+ an exact, case-insensitive match to a model name, use the+ expression: (?i)^(modelname)$+ </FormDescription>+ <FormControl>+ <Input {...field} />+ </FormControl>+ <FormMessage />+ </FormItem>+ )}+ />++ <PricingSection form={form} />++ <FormField+ control={form.control}+ name="tokenizerId"+ render={({ field }) => (+ <FormItem>+ <FormLabel>Tokenizer</FormLabel>+ <Select+ onValueChange={(tokenizerId) => {+ field.onChange(tokenizerId);+ if (tokenizerId === "None") {+ form.setValue("tokenizerConfig", "{}");+ }+ }}+ defaultValue={field.value ?? "None"}+ >+ <FormControl>+ <SelectTrigger>+ <SelectValue placeholder="Select a unit" />+ </SelectTrigger>+ </FormControl>+ <SelectContent>+ {["openai", "claude", "None"].map((unit) => (+ <SelectItem value={unit} key={unit}>+ {unit}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ <FormDescription>+ Optionally, Langfuse can tokenize the input and output of a+ generation if no unit counts are ingested. This is useful+ for e.g. streamed OpenAI completions. For details on the+ supported tokenizers, see the{" "}+ <Link+ href="https://langfuse.com/docs/model-usage-and-cost"+ className="underline"+ target="_blank"+ >+ docs+ </Link>+ .+ </FormDescription>+ <FormMessage />+ </FormItem>+ )}+ />+ {tokenizerId && tokenizerId !== "None" && (+ <FormField+ control={form.control}+ name="tokenizerConfig"+ render={({ field }) => (+ <FormItem>+ <FormLabel>Tokenizer Config</FormLabel>+ <CodeMirrorEditor+ mode="json"+ value={field.value ?? "{}"}+ onChange={field.onChange}+ />+ <FormDescription>+ The config for the tokenizer. Required for openai. See the{" "}+ <Link+ href="https://langfuse.com/docs/model-usage-and-cost"+ className="underline"+ target="_blank"+ >+ docs+ </Link>{" "}+ for details.+ </FormDescription>+ <FormMessage />+ </FormItem>+ )}+ />+ )}+ </DialogBody>++ <DialogFooter>+ <Button type="button" variant="outline" onClick={closeDialog}>+ Cancel+ </Button>++ <Button type="submit" loading={upsertModelMutation.isPending}>+ {props.action === "edit" ? "Save" : "Submit"}+ </Button>+ </DialogFooter>+ </form>+ {formError ? (+ <p className="text-destructive my-2 text-center text-sm font-bold">+ <span className="font-bold">Error:</span> {formError}+ </p>+ ) : null}+ </Form>+ </DialogContent>+ );+}diff --git a/web/src/features/models/components/test-match/TestModelMatchDialog.tsx b/web/src/features/models/components/test-match/TestModelMatchDialog.tsxindex 851d5ed201..fbd4ae120b 100644--- a/web/src/features/models/components/test-match/TestModelMatchDialog.tsx+++ b/web/src/features/models/components/test-match/TestModelMatchDialog.tsx@@ -35,6 +35,26 @@ export function TestModelMatchDialog({open,onOpenChange,}: TestModelMatchDialogProps) {+ return (+ <Dialog open={open} onOpenChange={onOpenChange}>+ <TestModelMatchDialogContent+ projectId={projectId}+ open={open}+ onClose={() => onOpenChange(false)}+ />+ </Dialog>+ );+}++export function TestModelMatchDialogContent({+ projectId,+ open,+ onClose,+}: {+ projectId: string;+ open: boolean;+ onClose: () => void;+}) {const [modelName, setModelName] = useState("");const [usageDetails, setUsageDetails] = useState<Record<string, number>>({});const [modelParameterEntries, setModelParameterEntries] = useState<@@ -82,7 +102,7 @@ export function TestModelMatchDialog({}, [open]);return (- <Dialog open={open} onOpenChange={onOpenChange}>+ <><DialogContent size="lg" className="min-h-[62vh] overflow-y-auto"><form onSubmit={handleSubmit} className="flex flex-1 flex-col"><DialogHeader>@@ -138,7 +158,7 @@ export function TestModelMatchDialog({<Buttontype="button"variant="outline"- onClick={() => onOpenChange(false)}+ onClick={onClose}className="flex-1">Close@@ -214,6 +234,6 @@ export function TestModelMatchDialog({</DialogBody></form></DialogContent>- </Dialog>+ </>);}diff --git a/web/src/features/models/index.ts b/web/src/features/models/index.tsindex 031e9d7fa3..d12b214c51 100644--- a/web/src/features/models/index.ts+++ b/web/src/features/models/index.ts@@ -2,4 +2,4 @@// re-exports only — exactly what other features already imported.export { ModelsSettings } from "@/src/features/models/components/ModelSettings";export { ProvidedModelNameCell } from "@/src/features/models/components/ProvidedModelNameCell";-export { UpsertModelFormDialog } from "@/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialog";+export { UpsertModelFormDialogController } from "@/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialogController";diff --git a/web/src/features/traces/components/ObservationDetailView/components/ModelBadge.tsx b/web/src/features/traces/components/ObservationDetailView/components/ModelBadge.tsxindex 29efa578b9..f7107c7c65 100644--- a/web/src/features/traces/components/ObservationDetailView/components/ModelBadge.tsx+++ b/web/src/features/traces/components/ObservationDetailView/components/ModelBadge.tsx@@ -6,7 +6,7 @@import { Badge } from "@/src/components/design-system/Badge/Badge";import { ExternalLinkIcon } from "lucide-react";import Link from "next/link";-import { UpsertModelFormDialog } from "@/src/features/models";+import { UpsertModelFormDialogController } from "@/src/features/models";export function ModelBadge({model,@@ -34,7 +34,7 @@ export function ModelBadge({// Unlinked model - show create form dialogreturn (- <UpsertModelFormDialog+ <UpsertModelFormDialogControlleraction="create"projectId={projectId}prefilledModelData={{@@ -53,9 +53,16 @@ export function ModelBadge({: undefined,}}>- <button type="button" className="inline-flex cursor-pointer">- <Badge color="ghost" interactive text={model} />- </button>- </UpsertModelFormDialog>+ {({ openDialog }) => (+ <button+ type="button"+ title="Create model definition"+ className="inline-flex cursor-pointer"+ onClick={openDialog}+ >+ <Badge color="ghost" interactive text={model} />+ </button>+ )}+ </UpsertModelFormDialogController>);}