Files
veridian/db/schema.ts
T

61 lines
2.6 KiB
TypeScript

import { integer, pgTable, text, boolean, timestamp, varchar } from "drizzle-orm/pg-core";
import { nanoid } from "nanoid";
import { user } from "./auth/auth.schema";
import { relations } from "drizzle-orm";
export * from "./auth/auth.schema";
export const agents = pgTable("agents", {
id: text("id").primaryKey().$defaultFn(() => 'agents_' + nanoid()),
userId: text("user_id").references(() => user.id).notNull(),
name: text("name").notNull(),
systemPrompt: text("system_prompt"),
imageUrl: text("image_url")
});
export const topics = pgTable("topics", {
id: text("id").primaryKey().$defaultFn(() => 'topics_' + nanoid()),
userId: text("user_id").references(() => user.id).notNull(),
agentId: text("agent_id").references(() => agents.id).notNull(),
name: text("name").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow()
});
export const messages = pgTable("messages", {
id: text("id").primaryKey().$defaultFn(() => 'messages_' + nanoid()),
userId: text("user_id").references(() => user.id).notNull(),
topicId: text("topic_id").references(() => topics.id).notNull(),
content: text("content").notNull(),
isUser: boolean("is_user").notNull(),
regeneratedFromId: text("regenerated_from_id").references((): any => messages.id),
isRegenerated: boolean("is_regenerated").default(false),
editedAt: timestamp("edited_at"),
createdAt: timestamp("created_at").notNull().defaultNow()
});
export const generations = pgTable("generations", {
id: text("id").primaryKey().$defaultFn(() => 'generations_' + nanoid()),
userId: text("user_id").references(() => user.id).notNull(),
topicId: text("topic_id").references(() => topics.id).notNull(),
status: varchar("status", { length: 20 }).notNull().default("pending"),
messageId: text("message_id").references(() => messages.id),
regeneratesFrom: text("regenerates_from").references(() => messages.id),
model: text("model"),
tokensGenerated: integer("tokens_generated"),
tokensUsedThinking: integer("tokens_used_thinking"),
error: text("error"),
createdAt: timestamp("created_at").notNull().defaultNow(),
startedAt: timestamp("started_at"),
completedAt: timestamp("completed_at")
});
export const messagesRelations = relations(messages, ({ one, many }) => ({
generations: many(generations),
regeneratedFrom: one(messages, {
fields: [messages.regeneratedFromId],
references: [messages.id],
relationName: 'regeneratedFrom'
}),
regenerations: many(messages, {
relationName: 'regeneratedFrom'
})
}));