🪢 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(models): define the pricing tier contract once (#16040)
Nikita Kabardinb3ee5b4b4f2e
diff --git a/packages/shared/src/features/model-pricing/validation.ts b/packages/shared/src/features/model-pricing/validation.tsindex 4a3723bb5f..f41c01e6ce 100644--- a/packages/shared/src/features/model-pricing/validation.ts+++ b/packages/shared/src/features/model-pricing/validation.ts@@ -72,8 +72,11 @@ export type PricingTierCondition = z.infer<typeof PricingTierConditionSchema>;* Used when creating new tiers via API or tRPC*/export const PricingTierInputSchema = z.object({+ // Trimmed before every later check, so a name differing only by whitespace+ // cannot pass uniqueness and then render as an identical label.name: z.string()+ .trim().min(1, "Name cannot be empty").max(100, "Name exceeds maximum length of 100 characters"),isDefault: z.boolean().default(false),@@ -91,6 +94,35 @@ export const PricingTierInputSchema = z.object({export type PricingTierInput = z.infer<typeof PricingTierInputSchema>;+/**+ * Indexes of entries whose name repeats an earlier one, with that name. The+ * first occurrence is not reported — it is the later one that is the duplicate.+ *+ * Shared with the model form so a duplicate means the same thing in the UI and+ * at the API boundary; the form needs the index to anchor a field error, the+ * API only needs to know that one exists.+ */+export function duplicateNameIndexes(+ entries: { name: string }[],+): [number, string][] {+ const seen = new Set<string>();+ const duplicates: [number, string][] = [];+ entries.forEach((entry, index) => {+ if (seen.has(entry.name)) duplicates.push([index, entry.name]);+ seen.add(entry.name);+ });+ return duplicates;+}++/** Indexes of non-default tiers with no condition, which can never match. */+export function tiersMissingConditions(+ tiers: { isDefault?: boolean; conditions: unknown[] }[],+): number[] {+ return tiers.flatMap((tier, index) =>+ !tier.isDefault && tier.conditions.length === 0 ? [index] : [],+ );+}+/*** Validates an array of pricing tiers* Ensures:@@ -139,13 +171,12 @@ export function validatePricingTiers(}// Validate non-default tiers have at least 1 condition- for (const tier of tiers) {- if (!tier.isDefault && tier.conditions.length === 0) {- return {- valid: false,- error: `Non-default pricing tier "${tier.name}" must have at least one condition`,- };- }+ const missingConditions = tiersMissingConditions(tiers);+ if (missingConditions.length > 0) {+ return {+ valid: false,+ error: `Non-default pricing tier "${tiers[missingConditions[0]].name}" must have at least one condition`,+ };}// Check for unique priorities@@ -159,9 +190,7 @@ export function validatePricingTiers(}// Check for unique names- const names = tiers.map((t) => t.name);- const uniqueNames = new Set(names);- if (names.length !== uniqueNames.size) {+ if (duplicateNameIndexes(tiers).length > 0) {return { valid: false, error: "Pricing tier names must be unique" };}diff --git a/web/src/__tests__/server/model-pricing-tiers.servertest.ts b/web/src/__tests__/server/model-pricing-tiers.servertest.tsindex 92407183ff..da96075ae8 100644--- a/web/src/__tests__/server/model-pricing-tiers.servertest.ts+++ b/web/src/__tests__/server/model-pricing-tiers.servertest.ts@@ -1,4 +1,5 @@import { randomUUID } from "crypto";+import { z } from "zod";import { prisma } from "@langfuse/shared/src/db";import {makeAPICall,@@ -14,6 +15,7 @@ import {validateRegexPattern,validatePricingTiers,validatePricingMethod,+ PricingTierInputSchema,type PricingTierInput,} from "@langfuse/shared";import { createOrgProjectAndApiKey } from "@langfuse/shared/src/server";@@ -271,6 +273,40 @@ describe("validation methods", () => {expect(getValidationError(result)).toContain("names must be unique");});+ it("should reject names that differ only by whitespace, as the UI does", () => {+ // The schema is what normalises: parsing trims, so the validator that+ // runs after it cannot see "Standard " as a distinct name. Without that,+ // a trailing space saved a second tier rendering an identical label.+ const tiers = z.array(PricingTierInputSchema).parse([+ {+ name: "Standard",+ isDefault: true,+ priority: 0,+ conditions: [],+ prices: { input: 3.0 },+ },+ {+ name: "Standard ",+ isDefault: false,+ priority: 1,+ conditions: [+ {+ usageDetailPattern: "^input",+ operator: "gt",+ value: 100,+ caseSensitive: false,+ },+ ],+ prices: { input: 5.0 },+ },+ ]);++ expect(tiers[1].name).toBe("Standard");+ const result = validatePricingTiers(tiers);+ expect(result.valid).toBe(false);+ expect(getValidationError(result)).toContain("names must be unique");+ });+it("should reject tier with invalid regex pattern", () => {const tiers: PricingTierInput[] = [{diff --git a/web/src/features/models/components/PricingSection/PricingSection.tsx b/web/src/features/models/components/PricingSection/PricingSection.tsxindex dbdf9c4acb..b1e3806341 100644--- a/web/src/features/models/components/PricingSection/PricingSection.tsx+++ b/web/src/features/models/components/PricingSection/PricingSection.tsx@@ -60,6 +60,8 @@ export function PricingSection({ form }: PricingSectionProps) {});};+ // These compare LIVE form values, which the schema has not parsed (and so not+ // trimmed) yet — unlike the submitted payload, where the schema owns it.const prefillUsageTypes = (names: string[]) => {const existing = new Set(form.getValues("usageTypes").map((row) => row.name.trim()),diff --git a/web/src/features/models/fns/toPricingTierInputs.ts b/web/src/features/models/fns/toPricingTierInputs.tsindex 08f360e716..1e1a63d2c7 100644--- a/web/src/features/models/fns/toPricingTierInputs.ts+++ b/web/src/features/models/fns/toPricingTierInputs.ts@@ -11,7 +11,7 @@ export const toPricingTierInputs = (const priorities = derivePriorities(values.pricingTiers);return values.pricingTiers.map((tier, tierIndex) => ({- name: tier.name.trim(),+ name: tier.name,isDefault: tier.isDefault,priority: priorities[tierIndex],conditions: tier.conditions.map((condition) => ({@@ -21,7 +21,7 @@ export const toPricingTierInputs = (prices: Object.fromEntries(values.usageTypes.flatMap((row) => {const price = parsePriceInput(tier.prices[row.key]);- return price === null ? [] : [[row.name.trim(), price] as const];+ return price === null ? [] : [[row.name, price] as const];}),),}));diff --git a/web/src/features/models/validation.ts b/web/src/features/models/validation.tsindex 808075e86f..ca5af9e0e1 100644--- a/web/src/features/models/validation.ts+++ b/web/src/features/models/validation.ts@@ -2,8 +2,10 @@ import { z } from "zod";import { parsePriceInput } from "@/src/features/models/fns/parsePriceInput";import {+ duplicateNameIndexes,PricingTierConditionSchema,PricingTierInputSchema,+ tiersMissingConditions,validatePricingTiers,} from "@langfuse/shared";@@ -40,7 +42,16 @@ export type PricingTier = z.infer<typeof PricingTierSchema>;*/const FormUsageTypeSchema = z.object({key: z.string(),- name: z.string(),+ // Trimmed here, so every later check and the submitted payload see one+ // normalised name. The value in the input is untouched while typing.+ name: z+ .string()+ .trim()+ .min(1, "Usage type required")+ .regex(+ USAGE_TYPE_PATTERN,+ "Only letters, numbers, hyphens and underscores",+ ),});export type FormUsageType = z.infer<typeof FormUsageTypeSchema>;@@ -48,7 +59,7 @@ export type FormUsageType = z.infer<typeof FormUsageTypeSchema>;// Form-level tier schema. Prices are keyed by usage type row key and held as// strings, so a half-typed "0." or "0.0000025" survives every keystroke.export const FormPricingTierSchema = z.object({- name: z.string().min(1, "Tier name is required"),+ name: z.string().trim().min(1, "Tier name is required"),isDefault: z.boolean(),conditions: z.array(PricingTierConditionSchema),prices: z.record(z.string(), z.string()),@@ -129,60 +140,35 @@ export const FormUpsertModelSchema = zusageTypes: z.array(FormUsageTypeSchema).min(1),pricingTiers: z.array(FormPricingTierSchema).min(1),})+ // Only cross-field rules live here; anything about a single value is on the+ // field itself, so there is one definition per rule..superRefine(({ usageTypes, pricingTiers }, ctx) => {- const seenUsageTypes = new Set<string>();- usageTypes.forEach((row, index) => {- const path = ["usageTypes", index, "name"];- const name = row.name.trim();-- if (!name) {- ctx.addIssue({ code: "custom", path, message: "Usage type required" });- } else if (!USAGE_TYPE_PATTERN.test(name)) {- ctx.addIssue({- code: "custom",- path,- message: "Only letters, numbers, hyphens and underscores",- });- } else if (seenUsageTypes.has(name)) {- ctx.addIssue({- code: "custom",- path,- message: `Duplicate usage type "${name}"`,- });- }- seenUsageTypes.add(name);- });+ duplicateNameIndexes(usageTypes).forEach(([index, name]) =>+ ctx.addIssue({+ code: "custom",+ path: ["usageTypes", index, "name"],+ message: `Duplicate usage type "${name}"`,+ }),+ );++ // The API's rule, reused rather than restated — the form's job is only to+ // point at the offending field.+ duplicateNameIndexes(pricingTiers).forEach(([index]) =>+ ctx.addIssue({+ code: "custom",+ path: ["pricingTiers", index, "name"],+ message: "Tier names must be unique",+ }),+ );+ tiersMissingConditions(pricingTiers).forEach((index) =>+ ctx.addIssue({+ code: "custom",+ path: ["pricingTiers", index, "conditions"],+ message: "Non-default tiers need at least one condition",+ }),+ );- const seenTierNames = new Set<string>();pricingTiers.forEach((tier, tierIndex) => {- // Compared trimmed, like usage types: whitespace must not sneak a second- // tier past this check and render as an identical label.- const tierName = tier.name.trim();- const tierNamePath = ["pricingTiers", tierIndex, "name"];-- if (!tierName) {- ctx.addIssue({- code: "custom",- path: tierNamePath,- message: "Tier name is required",- });- } else if (seenTierNames.has(tierName)) {- ctx.addIssue({- code: "custom",- path: tierNamePath,- message: "Tier names must be unique",- });- }- seenTierNames.add(tierName);-- if (!tier.isDefault && tier.conditions.length === 0) {- ctx.addIssue({- code: "custom",- path: ["pricingTiers", tierIndex, "conditions"],- message: "Non-default tiers need at least one condition",- });- }-usageTypes.forEach((row) => {if (parsePriceInput(tier.prices[row.key]) === null) {ctx.addIssue({