import { timestamp, pgTable, text, primaryKey, json } from "drizzle-orm/pg-core"; import { nanoid } from "nanoid"; // Message role enum type export enum MessageRole { USER = "user", ASSISTANT = "assistant", TOOL = "tool" } export const chats = pgTable('chats', { id: text('id').primaryKey().notNull().$defaultFn(() => nanoid()), userId: text('user_id').notNull(), title: text('title').notNull().default('New Chat'), createdAt: timestamp('created_at').defaultNow().notNull(), updatedAt: timestamp('updated_at').defaultNow().notNull(), }); export const messages = pgTable('messages', { id: text('id').primaryKey().notNull().$defaultFn(() => nanoid()), chatId: text('chat_id').notNull().references(() => chats.id, { onDelete: 'cascade' }), role: text('role').notNull(), // user, assistant, or tool parts: json('parts').notNull(), // Store parts as JSON in the database createdAt: timestamp('created_at').defaultNow().notNull(), }); // Types for structured message content export type MessagePart = { type: string; text?: string; toolCallId?: string; toolName?: string; args?: any; result?: any; [key: string]: any; }; export type Attachment = { type: string; [key: string]: any; }; export type Chat = typeof chats.$inferSelect; export type Message = typeof messages.$inferSelect; export type DBMessage = { id: string; chatId: string; role: string; parts: MessagePart[]; createdAt: Date; };