import { sql } from "drizzle-orm"; import { index, int, sqliteTable, text, type AnySQLiteColumn, } from "drizzle-orm/sqlite-core"; /** * Represents chat conversations in the system using string IDs for better * compatibility with AI SDK and modern chat applications. * * Chats can be organized hierarchically using parentId references and * support status tracking for real-time conversation management. */ export const chats = sqliteTable( "chats", { /** Unique identifier for the chat - compatible with UIMessage chatId */ id: text().primaryKey(), /** The title/name of the chat conversation */ title: text().notNull(), /** Timestamp (in milliseconds) when the chat was created */ createdAt: int("created_at") .notNull() .default(sql`CURRENT_TIMESTAMP`), /** Timestamp when the chat was last updated */ updatedAt: int("updated_at") .notNull() .default(sql`CURRENT_TIMESTAMP`), /** Optional reference to a parent chat, allowing for hierarchical chat organization */ parentId: text().references((): AnySQLiteColumn => chats.id, { onDelete: "cascade", }), /** Current status of the chat conversation */ status: text() .notNull() .default("idle") .$type<"idle" | "in_progress" | "completed" | "error">(), }, (table) => ({ parentIdx: index("idx_chats_parent_id").on(table.parentId), statusIdx: index("idx_chats_status").on(table.status), updatedAtIdx: index("idx_chats_updated_at").on(table.updatedAt), }) );