xonecas.io
5414
Sa

sat / files

sat / src / db / schema.ts

1.7 KB
1import { relations } from "drizzle-orm";
2import { jsonb, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
3
4// conversations table
5export const conversations = pgTable("conversations", {
6  id: uuid("id").primaryKey().defaultRandom(),
7  title: text("title"),
8  summary: text("summary"), // Compressed history of older messages
9  createdAt: timestamp("created_at").defaultNow().notNull(),
10  updatedAt: timestamp("updated_at").defaultNow().notNull(),
11});
12
13// turns table (named to avoid collision with LangChain message types)
14export const turns = pgTable("turns", {
15  id: uuid("id").primaryKey().defaultRandom(),
16  conversationId: uuid("conversation_id")
17    .references(() => conversations.id, { onDelete: "cascade" })
18    .notNull(),
19  role: text("role", { enum: ["user", "assistant"] }).notNull(),
20  content: text("content").notNull(),
21  reasoning: text("reasoning"),
22  toolCalls: jsonb("tool_calls"),
23  createdAt: timestamp("created_at").defaultNow().notNull(),
24});
25
26// Relations for query builder
27export const conversationsRelations = relations(conversations, ({ many }) => ({
28  turns: many(turns),
29}));
30
31export const turnsRelations = relations(turns, ({ one }) => ({
32  conversation: one(conversations, {
33    fields: [turns.conversationId],
34    references: [conversations.id],
35  }),
36}));
37
38// Type exports for use in server actions
39export type Conversation = typeof conversations.$inferSelect;
40export type NewConversation = typeof conversations.$inferInsert;
41export type Turn = typeof turns.$inferSelect;
42export type NewTurn = typeof turns.$inferInsert;
43
44// Types for chat history
45interface ToolCallData {
46  id: string;
47  name: string;
48  args: Record<string, unknown>;
49  result?: string;
50}
51
52export interface TurnWithToolCalls extends Turn {
53  toolCalls: ToolCallData[];
54}