diff --git a/packages/db/src/migrations/0047_nebulous_klaw.sql b/packages/db/src/migrations/0047_nebulous_klaw.sql new file mode 100644 index 00000000..f7fb2f6e --- /dev/null +++ b/packages/db/src/migrations/0047_nebulous_klaw.sql @@ -0,0 +1,27 @@ +CREATE TABLE "chat_conversations" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "company_id" uuid NOT NULL, + "title" text, + "agent_id" uuid, + "pinned_at" timestamp with time zone, + "archived_at" timestamp with time zone, + "deleted_at" timestamp with time zone, + "created_at" timestamp with time zone DEFAULT now() NOT NULL, + "updated_at" timestamp with time zone DEFAULT now() NOT NULL +); +--> statement-breakpoint +CREATE TABLE "chat_messages" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "conversation_id" uuid NOT NULL, + "role" text NOT NULL, + "content" text NOT NULL, + "agent_id" uuid, + "created_at" timestamp with time zone DEFAULT now() NOT NULL +); +--> statement-breakpoint +ALTER TABLE "chat_conversations" ADD CONSTRAINT "chat_conversations_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint +ALTER TABLE "chat_conversations" ADD CONSTRAINT "chat_conversations_agent_id_agents_id_fk" FOREIGN KEY ("agent_id") REFERENCES "public"."agents"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint +ALTER TABLE "chat_messages" ADD CONSTRAINT "chat_messages_conversation_id_chat_conversations_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."chat_conversations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint +CREATE INDEX "chat_conversations_company_updated_idx" ON "chat_conversations" USING btree ("company_id","updated_at");--> statement-breakpoint +CREATE INDEX "chat_conversations_company_deleted_idx" ON "chat_conversations" USING btree ("company_id","deleted_at");--> statement-breakpoint +CREATE INDEX "chat_messages_conversation_created_idx" ON "chat_messages" USING btree ("conversation_id","created_at"); \ No newline at end of file diff --git a/packages/db/src/schema/chat_conversations.ts b/packages/db/src/schema/chat_conversations.ts new file mode 100644 index 00000000..f1d781a9 --- /dev/null +++ b/packages/db/src/schema/chat_conversations.ts @@ -0,0 +1,22 @@ +import { pgTable, uuid, text, timestamp, index } from "drizzle-orm/pg-core"; +import { companies } from "./companies.js"; +import { agents } from "./agents.js"; + +export const chatConversations = pgTable( + "chat_conversations", + { + id: uuid("id").primaryKey().defaultRandom(), + companyId: uuid("company_id").notNull().references(() => companies.id), + title: text("title"), + agentId: uuid("agent_id").references(() => agents.id, { onDelete: "set null" }), + pinnedAt: timestamp("pinned_at", { withTimezone: true }), + archivedAt: timestamp("archived_at", { withTimezone: true }), + deletedAt: timestamp("deleted_at", { withTimezone: true }), + createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), + updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(), + }, + (table) => ({ + companyUpdatedIdx: index("chat_conversations_company_updated_idx").on(table.companyId, table.updatedAt), + companyDeletedIdx: index("chat_conversations_company_deleted_idx").on(table.companyId, table.deletedAt), + }), +); diff --git a/packages/db/src/schema/chat_messages.ts b/packages/db/src/schema/chat_messages.ts new file mode 100644 index 00000000..d68a0846 --- /dev/null +++ b/packages/db/src/schema/chat_messages.ts @@ -0,0 +1,18 @@ +import { pgTable, uuid, text, timestamp, index } from "drizzle-orm/pg-core"; +import { chatConversations } from "./chat_conversations.js"; + +export const chatMessages = pgTable( + "chat_messages", + { + id: uuid("id").primaryKey().defaultRandom(), + conversationId: uuid("conversation_id").notNull() + .references(() => chatConversations.id, { onDelete: "cascade" }), + role: text("role").notNull(), + content: text("content").notNull(), + agentId: uuid("agent_id"), + createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), + }, + (table) => ({ + conversationCreatedIdx: index("chat_messages_conversation_created_idx").on(table.conversationId, table.createdAt), + }), +); diff --git a/packages/db/src/schema/index.ts b/packages/db/src/schema/index.ts index 1b6fe01f..0c198b46 100644 --- a/packages/db/src/schema/index.ts +++ b/packages/db/src/schema/index.ts @@ -58,3 +58,5 @@ export { pluginEntities } from "./plugin_entities.js"; export { pluginJobs, pluginJobRuns } from "./plugin_jobs.js"; export { pluginWebhookDeliveries } from "./plugin_webhooks.js"; export { pluginLogs } from "./plugin_logs.js"; +export { chatConversations } from "./chat_conversations.js"; +export { chatMessages } from "./chat_messages.js";