- Analytics: <Analytics /> in root layout loads umami when NEXT_PUBLIC_UMAMI_* env are set - Subscription: free/pro tiers in Postgres, daily quotas (10msg/5tool free, 1000/500 pro) - Chat route: checkAndConsume(chat_message) gate before streamText, 429 when exhausted - Tool metering: getWeather wrapped with withLimit decorator that deducts tool_call quota - Pricing page at /pricing with upgrade button - /api/billing/checkout posts to EGBE payment gateway, /api/billing/webhook verifies HMAC and upgrades user on checkout.session.completed - UsageBadge in chat header polls /api/usage every 15s - Dropped now-unused entitlements.ts
117 lines
3.4 KiB
TypeScript
117 lines
3.4 KiB
TypeScript
import type { InferSelectModel } from "drizzle-orm";
|
|
import {
|
|
boolean,
|
|
integer,
|
|
json,
|
|
pgTable,
|
|
primaryKey,
|
|
text,
|
|
timestamp,
|
|
uuid,
|
|
varchar,
|
|
} from "drizzle-orm/pg-core";
|
|
|
|
export const user = pgTable("User", {
|
|
id: uuid("id").primaryKey().notNull().defaultRandom(),
|
|
email: varchar("email", { length: 64 }).notNull(),
|
|
password: varchar("password", { length: 64 }),
|
|
name: text("name"),
|
|
emailVerified: boolean("emailVerified").notNull().default(false),
|
|
image: text("image"),
|
|
isAnonymous: boolean("isAnonymous").notNull().default(false),
|
|
createdAt: timestamp("createdAt").notNull().defaultNow(),
|
|
updatedAt: timestamp("updatedAt").notNull().defaultNow(),
|
|
});
|
|
|
|
export type User = InferSelectModel<typeof user>;
|
|
|
|
export const chat = pgTable("Chat", {
|
|
id: uuid("id").primaryKey().notNull().defaultRandom(),
|
|
createdAt: timestamp("createdAt").notNull(),
|
|
title: text("title").notNull(),
|
|
userId: uuid("userId")
|
|
.notNull()
|
|
.references(() => user.id),
|
|
visibility: varchar("visibility", { enum: ["public", "private"] })
|
|
.notNull()
|
|
.default("private"),
|
|
});
|
|
|
|
export type Chat = InferSelectModel<typeof chat>;
|
|
|
|
export const message = pgTable("Message_v2", {
|
|
id: uuid("id").primaryKey().notNull().defaultRandom(),
|
|
chatId: uuid("chatId")
|
|
.notNull()
|
|
.references(() => chat.id),
|
|
role: varchar("role").notNull(),
|
|
parts: json("parts").notNull(),
|
|
attachments: json("attachments").notNull(),
|
|
createdAt: timestamp("createdAt").notNull(),
|
|
});
|
|
|
|
export type DBMessage = InferSelectModel<typeof message>;
|
|
|
|
export const vote = pgTable(
|
|
"Vote_v2",
|
|
{
|
|
chatId: uuid("chatId")
|
|
.notNull()
|
|
.references(() => chat.id),
|
|
messageId: uuid("messageId")
|
|
.notNull()
|
|
.references(() => message.id),
|
|
isUpvoted: boolean("isUpvoted").notNull(),
|
|
},
|
|
(table) => ({
|
|
pk: primaryKey({ columns: [table.chatId, table.messageId] }),
|
|
})
|
|
);
|
|
|
|
export type Vote = InferSelectModel<typeof vote>;
|
|
|
|
export const subscription = pgTable("Subscription", {
|
|
userId: uuid("userId")
|
|
.primaryKey()
|
|
.notNull()
|
|
.references(() => user.id),
|
|
tier: varchar("tier", { enum: ["free", "pro"] })
|
|
.notNull()
|
|
.default("free"),
|
|
status: varchar("status", { enum: ["active", "canceled", "past_due"] })
|
|
.notNull()
|
|
.default("active"),
|
|
stripeCustomerId: text("stripeCustomerId"),
|
|
stripeSubscriptionId: text("stripeSubscriptionId"),
|
|
currentPeriodEnd: timestamp("currentPeriodEnd"),
|
|
createdAt: timestamp("createdAt").notNull().defaultNow(),
|
|
updatedAt: timestamp("updatedAt").notNull().defaultNow(),
|
|
});
|
|
|
|
export type Subscription = InferSelectModel<typeof subscription>;
|
|
|
|
export const usage = pgTable(
|
|
"Usage",
|
|
{
|
|
userId: uuid("userId")
|
|
.notNull()
|
|
.references(() => user.id),
|
|
periodKey: varchar("periodKey", { length: 10 }).notNull(),
|
|
messageCount: integer("messageCount").notNull().default(0),
|
|
toolCallCount: integer("toolCallCount").notNull().default(0),
|
|
updatedAt: timestamp("updatedAt").notNull().defaultNow(),
|
|
},
|
|
(table) => ({
|
|
pk: primaryKey({ columns: [table.userId, table.periodKey] }),
|
|
})
|
|
);
|
|
|
|
export type Usage = InferSelectModel<typeof usage>;
|
|
|
|
export const webhookEvent = pgTable("WebhookEvent", {
|
|
id: text("id").primaryKey().notNull(),
|
|
type: text("type").notNull(),
|
|
receivedAt: timestamp("receivedAt").notNull().defaultNow(),
|
|
});
|
|
|
|
export type WebhookEvent = InferSelectModel<typeof webhookEvent>;
|