diff --git a/server/src/internal/features/creditSystemUtils.ts b/server/src/internal/features/creditSystemUtils.ts index 3b47a3009..08b6340d6 100644 --- a/server/src/internal/features/creditSystemUtils.ts +++ b/server/src/internal/features/creditSystemUtils.ts @@ -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) { diff --git a/server/src/internal/features/featureActions/createFeature.ts b/server/src/internal/features/featureActions/createFeature.ts index 5e073b6a5..6fd71732e 100644 --- a/server/src/internal/features/featureActions/createFeature.ts +++ b/server/src/internal/features/featureActions/createFeature.ts @@ -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({ diff --git a/server/src/internal/features/featureActions/updateFeature.ts b/server/src/internal/features/featureActions/updateFeature.ts index c597bbf4b..38d4b9472 100644 --- a/server/src/internal/features/featureActions/updateFeature.ts +++ b/server/src/internal/features/featureActions/updateFeature.ts @@ -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 && diff --git a/shared/utils/featureUtils/apiFeatureToDbFeature.ts b/shared/utils/featureUtils/apiFeatureToDbFeature.ts index 4c1d0cb6e..c621bf054 100644 --- a/shared/utils/featureUtils/apiFeatureToDbFeature.ts +++ b/shared/utils/featureUtils/apiFeatureToDbFeature.ts @@ -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) diff --git a/shared/utils/featureUtils/classifyFeature/isAnyCreditSystem.ts b/shared/utils/featureUtils/classifyFeature/isAnyCreditSystem.ts new file mode 100644 index 000000000..6f4bac496 --- /dev/null +++ b/shared/utils/featureUtils/classifyFeature/isAnyCreditSystem.ts @@ -0,0 +1,4 @@ +import { FeatureType } from "@models/featureModels/featureEnums"; + +export const isAnyCreditSystem = (type: FeatureType): boolean => + type === FeatureType.CreditSystem || type === FeatureType.AiCreditSystem; diff --git a/shared/utils/featureUtils/index.ts b/shared/utils/featureUtils/index.ts index 4d47d8777..184129007 100644 --- a/shared/utils/featureUtils/index.ts +++ b/shared/utils/featureUtils/index.ts @@ -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, diff --git a/vite/src/views/products/features/components/CreateFeatureSheet.tsx b/vite/src/views/products/features/components/CreateFeatureSheet.tsx index ab61c4be4..e5003ff12 100644 --- a/vite/src/views/products/features/components/CreateFeatureSheet.tsx +++ b/vite/src/views/products/features/components/CreateFeatureSheet.tsx @@ -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); diff --git a/vite/src/views/products/features/feature-list/FeatureListTable.tsx b/vite/src/views/products/features/feature-list/FeatureListTable.tsx index a34d65026..8d65826a6 100644 --- a/vite/src/views/products/features/feature-list/FeatureListTable.tsx +++ b/vite/src/views/products/features/feature-list/FeatureListTable.tsx @@ -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; diff --git a/vite/src/views/products/plan/components/new-feature/NewFeatureBehaviour.tsx b/vite/src/views/products/plan/components/new-feature/NewFeatureBehaviour.tsx index 928f19176..1dcf82a4e 100644 --- a/vite/src/views/products/plan/components/new-feature/NewFeatureBehaviour.tsx +++ b/vite/src/views/products/plan/components/new-feature/NewFeatureBehaviour.tsx @@ -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 ; }