Files
cfw-autumn/shared/models/featureModels/featureTable.ts
Charlie Lamb 8822664df9 feat: AI credit system with model-based token pricing
Adds a new FeatureType.AiCreditSystem that enables model-based token
pricing with per-model markup configuration. Includes the @useautumn/ai-sdk
package for Vercel AI SDK integration with automatic token tracking.

Co-authored-by: TheUntraceable <73362400+TheUntraceable@users.noreply.github.com>
2026-05-19 19:05:37 +01:00

52 lines
1.4 KiB
TypeScript

import { sql } from "drizzle-orm";
import {
boolean,
foreignKey,
jsonb,
numeric,
pgTable,
text,
unique,
} from "drizzle-orm/pg-core";
import { collatePgColumn } from "../../db/utils";
import { organizations } from "../orgModels/orgTable";
import type { CreditSystemConfig, ModelMarkups } from "./featureConfig/creditConfig";
import type { MeteredConfig } from "./featureConfig/meteredConfig";
type FeatureDisplay = {
singular: string;
plural: string;
};
export const features = pgTable(
"features",
{
internal_id: text("internal_id").primaryKey().notNull(),
org_id: text("org_id").notNull(),
created_at: numeric({ mode: "number" }),
env: text(),
id: text().notNull(),
name: text(),
type: text().notNull(),
config: jsonb().$type<MeteredConfig | CreditSystemConfig>(),
display: jsonb().default(sql`null`).$type<FeatureDisplay>(),
archived: boolean("archived").notNull().default(false),
event_names: text("event_names").array().default([]),
model_markups: jsonb().$type<ModelMarkups>().default(sql`null`),
},
(table) => [
foreignKey({
columns: [table.org_id],
foreignColumns: [organizations.id],
name: "features_org_id_fkey",
}).onDelete("cascade"),
unique("feature_id_constraint").on(table.org_id, table.id, table.env),
],
);
collatePgColumn(features.internal_id, "C");
export type DbFeature = typeof features.$inferSelect;
export type InsertDbFeature = typeof features.$inferInsert;