chore: dry up credit system checks

This commit is contained in:
Charlie Lamb
2026-06-01 17:28:40 +01:00
parent d4cea174c6
commit 9e4909a9fa
9 changed files with 23 additions and 25 deletions

View File

@@ -4,6 +4,7 @@ import {
type Feature,
FeatureType,
InternalError,
isAnyCreditSystem,
RecaseError,
} from "@autumn/shared";
import { Decimal } from "decimal.js";
@@ -170,7 +171,7 @@ export const getCreditCost = async ({
modelName?: string;
tokens?: TokenInput;
}) => {
if (creditSystem.type !== FeatureType.CreditSystem && creditSystem.type !== FeatureType.AiCreditSystem) {
if (!isAnyCreditSystem(creditSystem.type)) {
return amount;
}
if (creditSystem.type === FeatureType.AiCreditSystem) {

View File

@@ -2,6 +2,7 @@ import {
CreateFeatureSchema,
type Feature,
FeatureType,
isAnyCreditSystem,
type ModelMarkups,
} from "@autumn/shared";
import type { AutumnContext } from "@/honoUtils/HonoEnv.js";
@@ -20,10 +21,7 @@ const validateFeature = (data: any, allFeatures: Feature[]) => {
let config = data.config;
if (featureType === FeatureType.Metered) {
config = validateMeteredConfig(config);
} else if (
featureType === FeatureType.CreditSystem ||
featureType === FeatureType.AiCreditSystem
) {
} else if (isAnyCreditSystem(featureType)) {
config = validateCreditSystem(config, featureType);
if (featureType === FeatureType.CreditSystem) {
validateCreditSystemSchemaReferences({

View File

@@ -3,6 +3,7 @@ import {
ErrCode,
type Feature,
FeatureType,
isAnyCreditSystem,
type ModelMarkups,
notNullish,
} from "@autumn/shared";
@@ -230,9 +231,7 @@ export const updateFeature = async ({
}
// Queue cache clear for credit system if schema or model markups changed
const isCreditSystem =
feature.type === FeatureType.CreditSystem ||
feature.type === FeatureType.AiCreditSystem;
const isCreditSystem = isAnyCreditSystem(feature.type);
if (isCreditSystem && updatedFeature) {
const schemaChanged =
updates.config != null &&

View File

@@ -8,6 +8,7 @@ import {
FeatureUsageType,
} from "@models/featureModels/featureEnums.js";
import type { Feature } from "@models/featureModels/featureModels.js";
import { isAnyCreditSystem } from "./classifyFeature/isAnyCreditSystem.js";
import { AppEnv } from "@models/genModels/genEnums.js";
import type { ApiFeatureV1 } from "../../api/features/apiFeatureV1.js";
import type {
@@ -208,8 +209,7 @@ export const dbToApiFeatureV1 = ({
name: dbFeature.name,
type: dbFeature.type,
consumable:
dbFeature.type === FeatureType.CreditSystem ||
dbFeature.type === FeatureType.AiCreditSystem ||
isAnyCreditSystem(dbFeature.type) ||
dbFeature.config?.usage_type === FeatureUsageType.Single,
credit_schema: Array.isArray(dbFeature.config?.schema)

View File

@@ -0,0 +1,4 @@
import { FeatureType } from "@models/featureModels/featureEnums";
export const isAnyCreditSystem = (type: FeatureType): boolean =>
type === FeatureType.CreditSystem || type === FeatureType.AiCreditSystem;

View File

@@ -1,4 +1,5 @@
import { isAllocatedFeature } from "@utils/featureUtils/classifyFeature/isAllocatedFeature";
import { isAnyCreditSystem } from "@utils/featureUtils/classifyFeature/isAnyCreditSystem";
import { isConsumableFeature } from "@utils/featureUtils/classifyFeature/isConsumableFeature";
import { findFeatureById } from "@utils/featureUtils/findFeatureUtils";
@@ -8,9 +9,12 @@ export * from "./convertFeatureUtils";
export * from "./creditSystemUtils";
export * from "./findFeatureUtils";
export { isAnyCreditSystem } from "@utils/featureUtils/classifyFeature/isAnyCreditSystem";
export const featureUtils = {
isConsumable: isConsumableFeature,
isAllocated: isAllocatedFeature,
isAnyCreditSystem,
find: {
byId: findFeatureById,

View File

@@ -3,6 +3,7 @@ import {
type CreditSchemaItem,
FeatureType,
FeatureUsageType,
isAnyCreditSystem,
} from "@autumn/shared";
import type { AxiosError } from "axios";
import { useEffect, useState } from "react";
@@ -52,10 +53,7 @@ function CreateFeatureSheet({
const handleCreateFeature = async () => {
// Validate credit system specific fields first
if (
feature.type === FeatureType.CreditSystem ||
feature.type === FeatureType.AiCreditSystem
) {
if (isAnyCreditSystem(feature.type)) {
const validationError = validateCreditSystem(feature);
if (validationError) {
toast.error(validationError);

View File

@@ -1,4 +1,4 @@
import { AppEnv, type Feature, FeatureType } from "@autumn/shared";
import { AppEnv, type Feature, isAnyCreditSystem } from "@autumn/shared";
import { ArrowSquareOutIcon, CoinsIcon, LegoIcon } from "@phosphor-icons/react";
import { useMemo, useState } from "react";
import { Table } from "@/components/general/table";
@@ -29,19 +29,15 @@ export function FeatureListTable() {
// Filter features and credit systems based on archived state
const { regularFeatures, creditSystems, hasEventNames } = useMemo(() => {
const isCreditType = (type: string) =>
type === FeatureType.CreditSystem ||
type === FeatureType.AiCreditSystem;
const regularFeatures = features?.filter((feature) => {
if (isCreditType(feature.type)) return false;
if (isAnyCreditSystem(feature.type)) return false;
return queryStates.showArchivedFeatures
? feature.archived
: !feature.archived;
});
const creditSystems = features?.filter((feature) => {
if (!isCreditType(feature.type)) return false;
if (!isAnyCreditSystem(feature.type)) return false;
return queryStates.showArchivedFeatures
? feature.archived
: !feature.archived;

View File

@@ -2,6 +2,7 @@ import {
type CreateFeature,
FeatureType,
FeatureUsageType,
isAnyCreditSystem,
} from "@autumn/shared";
import { AreaRadioGroupItem } from "@/components/v2/radio-groups/AreaRadioGroupItem";
import { RadioGroup } from "@/components/v2/radio-groups/RadioGroup";
@@ -62,10 +63,7 @@ export function NewFeatureBehaviour({
feature: CreateFeature;
setFeature: (feature: CreateFeature) => void;
}) {
if (
feature.type === FeatureType.CreditSystem ||
feature.type === FeatureType.AiCreditSystem
) {
if (isAnyCreditSystem(feature.type)) {
return <NewFeatureCreditSchema feature={feature} setFeature={setFeature} />;
}