@mordn/chat-widget 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +338 -0
- package/dist/api/index.d.mts +6 -0
- package/dist/api/index.d.ts +6 -0
- package/dist/api/index.js +242 -0
- package/dist/api/index.js.map +1 -0
- package/dist/api/index.mjs +197 -0
- package/dist/api/index.mjs.map +1 -0
- package/dist/db/index.d.mts +323 -0
- package/dist/db/index.d.ts +323 -0
- package/dist/db/index.js +223 -0
- package/dist/db/index.js.map +1 -0
- package/dist/db/index.mjs +186 -0
- package/dist/db/index.mjs.map +1 -0
- package/dist/index.d.mts +187 -0
- package/dist/index.d.ts +187 -0
- package/dist/index.js +2663 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2647 -0
- package/dist/index.mjs.map +1 -0
- package/dist/styles.css +2 -0
- package/package.json +100 -0
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// src/db/chat-store.ts
|
|
8
|
+
import { generateId } from "ai";
|
|
9
|
+
|
|
10
|
+
// src/db/index.ts
|
|
11
|
+
import "server-only";
|
|
12
|
+
import { drizzle } from "drizzle-orm/postgres-js";
|
|
13
|
+
import postgres from "postgres";
|
|
14
|
+
|
|
15
|
+
// src/db/schema.ts
|
|
16
|
+
var schema_exports = {};
|
|
17
|
+
__export(schema_exports, {
|
|
18
|
+
conversations: () => conversations,
|
|
19
|
+
messages: () => messages
|
|
20
|
+
});
|
|
21
|
+
import { pgTable, text, timestamp, jsonb, index } from "drizzle-orm/pg-core";
|
|
22
|
+
var conversations = pgTable("conversations", {
|
|
23
|
+
id: text("id").primaryKey(),
|
|
24
|
+
userId: text("user_id").notNull(),
|
|
25
|
+
title: text("title").notNull().default("New Chat"),
|
|
26
|
+
metadata: jsonb("metadata"),
|
|
27
|
+
createdAt: timestamp("created_at").defaultNow().notNull(),
|
|
28
|
+
updatedAt: timestamp("updated_at").defaultNow().notNull()
|
|
29
|
+
}, (table) => [
|
|
30
|
+
index("conversations_user_id_idx").on(table.userId),
|
|
31
|
+
index("conversations_updated_at_idx").on(table.updatedAt)
|
|
32
|
+
]);
|
|
33
|
+
var messages = pgTable("messages", {
|
|
34
|
+
id: text("id").primaryKey(),
|
|
35
|
+
conversationId: text("conversation_id").notNull().references(() => conversations.id, { onDelete: "cascade" }),
|
|
36
|
+
role: text("role").notNull(),
|
|
37
|
+
// 'user' | 'assistant' | 'system'
|
|
38
|
+
content: text("content").notNull(),
|
|
39
|
+
files: jsonb("files"),
|
|
40
|
+
// Array of file attachments
|
|
41
|
+
model: text("model"),
|
|
42
|
+
// AI model used
|
|
43
|
+
metadata: jsonb("metadata"),
|
|
44
|
+
// Additional data (parts, reasoning, etc.)
|
|
45
|
+
createdAt: timestamp("created_at").defaultNow().notNull()
|
|
46
|
+
}, (table) => [
|
|
47
|
+
index("messages_conversation_id_idx").on(table.conversationId),
|
|
48
|
+
index("messages_created_at_idx").on(table.createdAt)
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
// src/db/index.ts
|
|
52
|
+
var connectionString = process.env.DATABASE_URL;
|
|
53
|
+
var client = postgres(connectionString, { prepare: false });
|
|
54
|
+
var db = drizzle(client, { schema: schema_exports });
|
|
55
|
+
|
|
56
|
+
// src/db/chat-store.ts
|
|
57
|
+
import { eq, desc, asc, sql } from "drizzle-orm";
|
|
58
|
+
async function createChat(userId) {
|
|
59
|
+
const id = generateId();
|
|
60
|
+
await db.insert(conversations).values({
|
|
61
|
+
id,
|
|
62
|
+
userId,
|
|
63
|
+
title: "New Chat",
|
|
64
|
+
metadata: {}
|
|
65
|
+
});
|
|
66
|
+
return id;
|
|
67
|
+
}
|
|
68
|
+
async function loadChat(conversationId) {
|
|
69
|
+
try {
|
|
70
|
+
const dbMessages = await db.select().from(messages).where(eq(messages.conversationId, conversationId)).orderBy(asc(messages.createdAt));
|
|
71
|
+
if (!dbMessages.length) return [];
|
|
72
|
+
return dbMessages.map((msg) => {
|
|
73
|
+
const metadata = msg.metadata;
|
|
74
|
+
if (metadata?.parts && Array.isArray(metadata.parts)) {
|
|
75
|
+
return {
|
|
76
|
+
id: msg.id,
|
|
77
|
+
role: msg.role,
|
|
78
|
+
parts: metadata.parts,
|
|
79
|
+
createdAt: msg.createdAt
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
id: msg.id,
|
|
84
|
+
role: msg.role,
|
|
85
|
+
parts: [{ type: "text", text: msg.content }],
|
|
86
|
+
createdAt: msg.createdAt
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
} catch (error) {
|
|
90
|
+
console.error("Error loading chat:", error);
|
|
91
|
+
return [];
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async function updateConversationTitle(chatId, title) {
|
|
95
|
+
try {
|
|
96
|
+
await db.update(conversations).set({ title, updatedAt: /* @__PURE__ */ new Date() }).where(eq(conversations.id, chatId));
|
|
97
|
+
} catch (error) {
|
|
98
|
+
console.error("Error updating conversation title:", error);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async function saveChat({
|
|
102
|
+
chatId,
|
|
103
|
+
messages: chatMessages,
|
|
104
|
+
model,
|
|
105
|
+
userId
|
|
106
|
+
}) {
|
|
107
|
+
if (!userId) {
|
|
108
|
+
console.error("userId is required for saveChat");
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
const existingConv = await db.select({ id: conversations.id, title: conversations.title }).from(conversations).where(eq(conversations.id, chatId)).limit(1);
|
|
113
|
+
if (!existingConv.length) {
|
|
114
|
+
console.error("Conversation not found:", chatId);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const conv = existingConv[0];
|
|
118
|
+
if (conv.title === "New Chat") {
|
|
119
|
+
const firstUserMessage = chatMessages.find((m) => m.role === "user");
|
|
120
|
+
if (firstUserMessage) {
|
|
121
|
+
const textPart = firstUserMessage.parts?.find((p) => p.type === "text");
|
|
122
|
+
if (textPart?.text) {
|
|
123
|
+
const newTitle = textPart.text.slice(0, 100);
|
|
124
|
+
await updateConversationTitle(chatId, newTitle);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const existingMessages = await db.select({ id: messages.id }).from(messages).where(eq(messages.conversationId, chatId));
|
|
129
|
+
const existingIds = new Set(existingMessages.map((m) => m.id));
|
|
130
|
+
const newMessages = chatMessages.filter((msg) => !existingIds.has(msg.id));
|
|
131
|
+
if (newMessages.length > 0) {
|
|
132
|
+
for (const msg of newMessages) {
|
|
133
|
+
const textPart = msg.parts?.find((p) => p.type === "text");
|
|
134
|
+
const fileParts = msg.parts?.filter((p) => p.type === "file") || [];
|
|
135
|
+
await db.insert(messages).values({
|
|
136
|
+
id: msg.id,
|
|
137
|
+
conversationId: chatId,
|
|
138
|
+
role: msg.role,
|
|
139
|
+
content: textPart?.text || "",
|
|
140
|
+
files: fileParts,
|
|
141
|
+
model: model || "openai/gpt-4o-mini",
|
|
142
|
+
metadata: { parts: msg.parts || [] }
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
await db.update(conversations).set({ updatedAt: /* @__PURE__ */ new Date() }).where(eq(conversations.id, chatId));
|
|
146
|
+
}
|
|
147
|
+
} catch (error) {
|
|
148
|
+
console.error("Error saving chat:", error);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
async function getConversations(userId) {
|
|
152
|
+
try {
|
|
153
|
+
const result = await db.select({
|
|
154
|
+
id: conversations.id,
|
|
155
|
+
title: conversations.title,
|
|
156
|
+
createdAt: conversations.createdAt,
|
|
157
|
+
updatedAt: conversations.updatedAt,
|
|
158
|
+
metadata: conversations.metadata,
|
|
159
|
+
messageCount: sql`(
|
|
160
|
+
SELECT COUNT(*) FROM ${messages}
|
|
161
|
+
WHERE ${messages.conversationId} = ${conversations.id}
|
|
162
|
+
)`
|
|
163
|
+
}).from(conversations).where(eq(conversations.userId, userId)).orderBy(desc(conversations.updatedAt));
|
|
164
|
+
return result;
|
|
165
|
+
} catch (error) {
|
|
166
|
+
console.error("Error getting conversations:", error);
|
|
167
|
+
return [];
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
async function deleteConversation(chatId) {
|
|
171
|
+
try {
|
|
172
|
+
await db.delete(conversations).where(eq(conversations.id, chatId));
|
|
173
|
+
} catch (error) {
|
|
174
|
+
console.error("Error deleting conversation:", error);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// src/api/index.ts
|
|
179
|
+
import { eq as eq2, and, or, desc as desc2, asc as asc2, sql as sql2 } from "drizzle-orm";
|
|
180
|
+
export {
|
|
181
|
+
and,
|
|
182
|
+
asc2 as asc,
|
|
183
|
+
conversations,
|
|
184
|
+
createChat,
|
|
185
|
+
db,
|
|
186
|
+
deleteConversation,
|
|
187
|
+
desc2 as desc,
|
|
188
|
+
eq2 as eq,
|
|
189
|
+
getConversations,
|
|
190
|
+
loadChat,
|
|
191
|
+
messages,
|
|
192
|
+
or,
|
|
193
|
+
saveChat,
|
|
194
|
+
sql2 as sql,
|
|
195
|
+
updateConversationTitle
|
|
196
|
+
};
|
|
197
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/db/chat-store.ts","../../src/db/index.ts","../../src/db/schema.ts","../../src/api/index.ts"],"sourcesContent":["import { generateId, UIMessage } from 'ai';\nimport { db, conversations, messages } from './index';\nimport { eq, desc, asc, sql } from 'drizzle-orm';\n\n/**\n * Create a new conversation\n */\nexport async function createChat(userId: string): Promise<string> {\n const id = generateId();\n\n await db.insert(conversations).values({\n id,\n userId,\n title: 'New Chat',\n metadata: {},\n });\n\n return id;\n}\n\n/**\n * Load messages for a conversation\n */\nexport async function loadChat(conversationId: string): Promise<UIMessage[]> {\n try {\n const dbMessages = await db\n .select()\n .from(messages)\n .where(eq(messages.conversationId, conversationId))\n .orderBy(asc(messages.createdAt));\n\n if (!dbMessages.length) return [];\n\n // Convert database messages to UIMessage format\n return dbMessages.map((msg) => {\n // If we have metadata with parts, use those (includes reasoning)\n const metadata = msg.metadata as { parts?: any[] } | null;\n if (metadata?.parts && Array.isArray(metadata.parts)) {\n return {\n id: msg.id,\n role: msg.role as 'user' | 'assistant' | 'system',\n parts: metadata.parts,\n createdAt: msg.createdAt,\n };\n }\n\n // Fallback to simple text message\n return {\n id: msg.id,\n role: msg.role as 'user' | 'assistant' | 'system',\n parts: [{ type: 'text', text: msg.content }],\n createdAt: msg.createdAt,\n };\n });\n } catch (error) {\n console.error('Error loading chat:', error);\n return [];\n }\n}\n\n/**\n * Update conversation title\n */\nexport async function updateConversationTitle(\n chatId: string,\n title: string\n): Promise<void> {\n try {\n await db\n .update(conversations)\n .set({ title, updatedAt: new Date() })\n .where(eq(conversations.id, chatId));\n } catch (error) {\n console.error('Error updating conversation title:', error);\n }\n}\n\n/**\n * Save messages to a conversation\n */\nexport async function saveChat({\n chatId,\n messages: chatMessages,\n model,\n userId,\n}: {\n chatId: string;\n messages: UIMessage[];\n model?: string;\n userId: string;\n}): Promise<void> {\n if (!userId) {\n console.error('userId is required for saveChat');\n return;\n }\n\n try {\n // Verify conversation exists\n const existingConv = await db\n .select({ id: conversations.id, title: conversations.title })\n .from(conversations)\n .where(eq(conversations.id, chatId))\n .limit(1);\n\n if (!existingConv.length) {\n console.error('Conversation not found:', chatId);\n return;\n }\n\n const conv = existingConv[0];\n\n // Update title if this is the first user message and title is still \"New Chat\"\n if (conv.title === 'New Chat') {\n const firstUserMessage = chatMessages.find((m) => m.role === 'user');\n if (firstUserMessage) {\n const textPart = firstUserMessage.parts?.find((p) => p.type === 'text') as { text: string } | undefined;\n if (textPart?.text) {\n const newTitle = textPart.text.slice(0, 100);\n await updateConversationTitle(chatId, newTitle);\n }\n }\n }\n\n // Get existing message IDs from database\n const existingMessages = await db\n .select({ id: messages.id })\n .from(messages)\n .where(eq(messages.conversationId, chatId));\n\n const existingIds = new Set(existingMessages.map((m) => m.id));\n\n // Insert only new messages\n const newMessages = chatMessages.filter((msg) => !existingIds.has(msg.id));\n\n if (newMessages.length > 0) {\n for (const msg of newMessages) {\n const textPart = msg.parts?.find((p) => p.type === 'text') as { text: string } | undefined;\n const fileParts = msg.parts?.filter((p) => p.type === 'file') || [];\n\n await db.insert(messages).values({\n id: msg.id,\n conversationId: chatId,\n role: msg.role,\n content: textPart?.text || '',\n files: fileParts,\n model: model || 'openai/gpt-4o-mini',\n metadata: { parts: msg.parts || [] },\n });\n }\n\n // Update conversation's updatedAt\n await db\n .update(conversations)\n .set({ updatedAt: new Date() })\n .where(eq(conversations.id, chatId));\n }\n } catch (error) {\n console.error('Error saving chat:', error);\n }\n}\n\n/**\n * Get all conversations for a user\n */\nexport async function getConversations(userId: string) {\n try {\n const result = await db\n .select({\n id: conversations.id,\n title: conversations.title,\n createdAt: conversations.createdAt,\n updatedAt: conversations.updatedAt,\n metadata: conversations.metadata,\n messageCount: sql<number>`(\n SELECT COUNT(*) FROM ${messages}\n WHERE ${messages.conversationId} = ${conversations.id}\n )`,\n })\n .from(conversations)\n .where(eq(conversations.userId, userId))\n .orderBy(desc(conversations.updatedAt));\n\n return result;\n } catch (error) {\n console.error('Error getting conversations:', error);\n return [];\n }\n}\n\n/**\n * Delete a conversation and all its messages\n */\nexport async function deleteConversation(chatId: string): Promise<void> {\n try {\n // Messages are deleted automatically due to cascade\n await db.delete(conversations).where(eq(conversations.id, chatId));\n } catch (error) {\n console.error('Error deleting conversation:', error);\n }\n}\n","import 'server-only';\nimport { drizzle } from 'drizzle-orm/postgres-js';\nimport postgres from 'postgres';\nimport * as schema from './schema';\n\n// Create postgres connection\nconst connectionString = process.env.DATABASE_URL!;\n\n// Disable prefetch as it is not supported for \"Transaction\" pool mode\nconst client = postgres(connectionString, { prepare: false });\n\n// Create drizzle database instance\nexport const db = drizzle(client, { schema });\n\n// Export schema for convenience\nexport * from './schema';\n\n// Export chat store functions\nexport * from './chat-store';\n","import { pgTable, text, timestamp, jsonb, index } from 'drizzle-orm/pg-core';\n\n/**\n * Conversations table\n * Stores chat conversation metadata\n */\nexport const conversations = pgTable('conversations', {\n id: text('id').primaryKey(),\n userId: text('user_id').notNull(),\n title: text('title').notNull().default('New Chat'),\n metadata: jsonb('metadata'),\n createdAt: timestamp('created_at').defaultNow().notNull(),\n updatedAt: timestamp('updated_at').defaultNow().notNull(),\n}, (table) => [\n index('conversations_user_id_idx').on(table.userId),\n index('conversations_updated_at_idx').on(table.updatedAt),\n]);\n\n/**\n * Messages table\n * Stores individual chat messages\n */\nexport const messages = pgTable('messages', {\n id: text('id').primaryKey(),\n conversationId: text('conversation_id').notNull().references(() => conversations.id, { onDelete: 'cascade' }),\n role: text('role').notNull(), // 'user' | 'assistant' | 'system'\n content: text('content').notNull(),\n files: jsonb('files'), // Array of file attachments\n model: text('model'), // AI model used\n metadata: jsonb('metadata'), // Additional data (parts, reasoning, etc.)\n createdAt: timestamp('created_at').defaultNow().notNull(),\n}, (table) => [\n index('messages_conversation_id_idx').on(table.conversationId),\n index('messages_created_at_idx').on(table.createdAt),\n]);\n\n// Type exports for use in application code\nexport type Conversation = typeof conversations.$inferSelect;\nexport type NewConversation = typeof conversations.$inferInsert;\nexport type Message = typeof messages.$inferSelect;\nexport type NewMessage = typeof messages.$inferInsert;\n","/**\n * API Helpers for Chat Widget\n *\n * These utilities help you create the required API routes for the chat widget.\n * Copy the route examples to your Next.js app/api folder.\n */\n\nexport {\n createChat,\n loadChat,\n saveChat,\n getConversations,\n updateConversationTitle,\n deleteConversation\n} from '../db/chat-store';\n\nexport { db, conversations, messages } from '../db';\nexport type { Conversation, Message, NewConversation, NewMessage } from '../db/schema';\n\n// Re-export drizzle utilities for convenience\nexport { eq, and, or, desc, asc, sql }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AAAA,EACnE,SAAS,OAAO;AACd,YAAQ,MAAM,gCAAgC,KAAK;AAAA,EACrD;AACF;;;AGnLA,SAAS,MAAAA,KAAI,KAAK,IAAI,QAAAC,OAAM,OAAAC,MAAK,OAAAC,YAAW;","names":["eq","desc","asc","sql"]}
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import { UIMessage } from 'ai';
|
|
2
|
+
import * as drizzle_orm_postgres_js from 'drizzle-orm/postgres-js';
|
|
3
|
+
import postgres from 'postgres';
|
|
4
|
+
import * as drizzle_orm_pg_core from 'drizzle-orm/pg-core';
|
|
5
|
+
import 'drizzle-orm';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Conversations table
|
|
9
|
+
* Stores chat conversation metadata
|
|
10
|
+
*/
|
|
11
|
+
declare const conversations: drizzle_orm_pg_core.PgTableWithColumns<{
|
|
12
|
+
name: "conversations";
|
|
13
|
+
schema: undefined;
|
|
14
|
+
columns: {
|
|
15
|
+
id: drizzle_orm_pg_core.PgColumn<{
|
|
16
|
+
name: "id";
|
|
17
|
+
tableName: "conversations";
|
|
18
|
+
dataType: "string";
|
|
19
|
+
columnType: "PgText";
|
|
20
|
+
data: string;
|
|
21
|
+
driverParam: string;
|
|
22
|
+
notNull: true;
|
|
23
|
+
hasDefault: false;
|
|
24
|
+
isPrimaryKey: true;
|
|
25
|
+
isAutoincrement: false;
|
|
26
|
+
hasRuntimeDefault: false;
|
|
27
|
+
enumValues: [string, ...string[]];
|
|
28
|
+
baseColumn: never;
|
|
29
|
+
identity: undefined;
|
|
30
|
+
generated: undefined;
|
|
31
|
+
}, {}, {}>;
|
|
32
|
+
userId: drizzle_orm_pg_core.PgColumn<{
|
|
33
|
+
name: "user_id";
|
|
34
|
+
tableName: "conversations";
|
|
35
|
+
dataType: "string";
|
|
36
|
+
columnType: "PgText";
|
|
37
|
+
data: string;
|
|
38
|
+
driverParam: string;
|
|
39
|
+
notNull: true;
|
|
40
|
+
hasDefault: false;
|
|
41
|
+
isPrimaryKey: false;
|
|
42
|
+
isAutoincrement: false;
|
|
43
|
+
hasRuntimeDefault: false;
|
|
44
|
+
enumValues: [string, ...string[]];
|
|
45
|
+
baseColumn: never;
|
|
46
|
+
identity: undefined;
|
|
47
|
+
generated: undefined;
|
|
48
|
+
}, {}, {}>;
|
|
49
|
+
title: drizzle_orm_pg_core.PgColumn<{
|
|
50
|
+
name: "title";
|
|
51
|
+
tableName: "conversations";
|
|
52
|
+
dataType: "string";
|
|
53
|
+
columnType: "PgText";
|
|
54
|
+
data: string;
|
|
55
|
+
driverParam: string;
|
|
56
|
+
notNull: true;
|
|
57
|
+
hasDefault: true;
|
|
58
|
+
isPrimaryKey: false;
|
|
59
|
+
isAutoincrement: false;
|
|
60
|
+
hasRuntimeDefault: false;
|
|
61
|
+
enumValues: [string, ...string[]];
|
|
62
|
+
baseColumn: never;
|
|
63
|
+
identity: undefined;
|
|
64
|
+
generated: undefined;
|
|
65
|
+
}, {}, {}>;
|
|
66
|
+
metadata: drizzle_orm_pg_core.PgColumn<{
|
|
67
|
+
name: "metadata";
|
|
68
|
+
tableName: "conversations";
|
|
69
|
+
dataType: "json";
|
|
70
|
+
columnType: "PgJsonb";
|
|
71
|
+
data: unknown;
|
|
72
|
+
driverParam: unknown;
|
|
73
|
+
notNull: false;
|
|
74
|
+
hasDefault: false;
|
|
75
|
+
isPrimaryKey: false;
|
|
76
|
+
isAutoincrement: false;
|
|
77
|
+
hasRuntimeDefault: false;
|
|
78
|
+
enumValues: undefined;
|
|
79
|
+
baseColumn: never;
|
|
80
|
+
identity: undefined;
|
|
81
|
+
generated: undefined;
|
|
82
|
+
}, {}, {}>;
|
|
83
|
+
createdAt: drizzle_orm_pg_core.PgColumn<{
|
|
84
|
+
name: "created_at";
|
|
85
|
+
tableName: "conversations";
|
|
86
|
+
dataType: "date";
|
|
87
|
+
columnType: "PgTimestamp";
|
|
88
|
+
data: Date;
|
|
89
|
+
driverParam: string;
|
|
90
|
+
notNull: true;
|
|
91
|
+
hasDefault: true;
|
|
92
|
+
isPrimaryKey: false;
|
|
93
|
+
isAutoincrement: false;
|
|
94
|
+
hasRuntimeDefault: false;
|
|
95
|
+
enumValues: undefined;
|
|
96
|
+
baseColumn: never;
|
|
97
|
+
identity: undefined;
|
|
98
|
+
generated: undefined;
|
|
99
|
+
}, {}, {}>;
|
|
100
|
+
updatedAt: drizzle_orm_pg_core.PgColumn<{
|
|
101
|
+
name: "updated_at";
|
|
102
|
+
tableName: "conversations";
|
|
103
|
+
dataType: "date";
|
|
104
|
+
columnType: "PgTimestamp";
|
|
105
|
+
data: Date;
|
|
106
|
+
driverParam: string;
|
|
107
|
+
notNull: true;
|
|
108
|
+
hasDefault: true;
|
|
109
|
+
isPrimaryKey: false;
|
|
110
|
+
isAutoincrement: false;
|
|
111
|
+
hasRuntimeDefault: false;
|
|
112
|
+
enumValues: undefined;
|
|
113
|
+
baseColumn: never;
|
|
114
|
+
identity: undefined;
|
|
115
|
+
generated: undefined;
|
|
116
|
+
}, {}, {}>;
|
|
117
|
+
};
|
|
118
|
+
dialect: "pg";
|
|
119
|
+
}>;
|
|
120
|
+
/**
|
|
121
|
+
* Messages table
|
|
122
|
+
* Stores individual chat messages
|
|
123
|
+
*/
|
|
124
|
+
declare const messages: drizzle_orm_pg_core.PgTableWithColumns<{
|
|
125
|
+
name: "messages";
|
|
126
|
+
schema: undefined;
|
|
127
|
+
columns: {
|
|
128
|
+
id: drizzle_orm_pg_core.PgColumn<{
|
|
129
|
+
name: "id";
|
|
130
|
+
tableName: "messages";
|
|
131
|
+
dataType: "string";
|
|
132
|
+
columnType: "PgText";
|
|
133
|
+
data: string;
|
|
134
|
+
driverParam: string;
|
|
135
|
+
notNull: true;
|
|
136
|
+
hasDefault: false;
|
|
137
|
+
isPrimaryKey: true;
|
|
138
|
+
isAutoincrement: false;
|
|
139
|
+
hasRuntimeDefault: false;
|
|
140
|
+
enumValues: [string, ...string[]];
|
|
141
|
+
baseColumn: never;
|
|
142
|
+
identity: undefined;
|
|
143
|
+
generated: undefined;
|
|
144
|
+
}, {}, {}>;
|
|
145
|
+
conversationId: drizzle_orm_pg_core.PgColumn<{
|
|
146
|
+
name: "conversation_id";
|
|
147
|
+
tableName: "messages";
|
|
148
|
+
dataType: "string";
|
|
149
|
+
columnType: "PgText";
|
|
150
|
+
data: string;
|
|
151
|
+
driverParam: string;
|
|
152
|
+
notNull: true;
|
|
153
|
+
hasDefault: false;
|
|
154
|
+
isPrimaryKey: false;
|
|
155
|
+
isAutoincrement: false;
|
|
156
|
+
hasRuntimeDefault: false;
|
|
157
|
+
enumValues: [string, ...string[]];
|
|
158
|
+
baseColumn: never;
|
|
159
|
+
identity: undefined;
|
|
160
|
+
generated: undefined;
|
|
161
|
+
}, {}, {}>;
|
|
162
|
+
role: drizzle_orm_pg_core.PgColumn<{
|
|
163
|
+
name: "role";
|
|
164
|
+
tableName: "messages";
|
|
165
|
+
dataType: "string";
|
|
166
|
+
columnType: "PgText";
|
|
167
|
+
data: string;
|
|
168
|
+
driverParam: string;
|
|
169
|
+
notNull: true;
|
|
170
|
+
hasDefault: false;
|
|
171
|
+
isPrimaryKey: false;
|
|
172
|
+
isAutoincrement: false;
|
|
173
|
+
hasRuntimeDefault: false;
|
|
174
|
+
enumValues: [string, ...string[]];
|
|
175
|
+
baseColumn: never;
|
|
176
|
+
identity: undefined;
|
|
177
|
+
generated: undefined;
|
|
178
|
+
}, {}, {}>;
|
|
179
|
+
content: drizzle_orm_pg_core.PgColumn<{
|
|
180
|
+
name: "content";
|
|
181
|
+
tableName: "messages";
|
|
182
|
+
dataType: "string";
|
|
183
|
+
columnType: "PgText";
|
|
184
|
+
data: string;
|
|
185
|
+
driverParam: string;
|
|
186
|
+
notNull: true;
|
|
187
|
+
hasDefault: false;
|
|
188
|
+
isPrimaryKey: false;
|
|
189
|
+
isAutoincrement: false;
|
|
190
|
+
hasRuntimeDefault: false;
|
|
191
|
+
enumValues: [string, ...string[]];
|
|
192
|
+
baseColumn: never;
|
|
193
|
+
identity: undefined;
|
|
194
|
+
generated: undefined;
|
|
195
|
+
}, {}, {}>;
|
|
196
|
+
files: drizzle_orm_pg_core.PgColumn<{
|
|
197
|
+
name: "files";
|
|
198
|
+
tableName: "messages";
|
|
199
|
+
dataType: "json";
|
|
200
|
+
columnType: "PgJsonb";
|
|
201
|
+
data: unknown;
|
|
202
|
+
driverParam: unknown;
|
|
203
|
+
notNull: false;
|
|
204
|
+
hasDefault: false;
|
|
205
|
+
isPrimaryKey: false;
|
|
206
|
+
isAutoincrement: false;
|
|
207
|
+
hasRuntimeDefault: false;
|
|
208
|
+
enumValues: undefined;
|
|
209
|
+
baseColumn: never;
|
|
210
|
+
identity: undefined;
|
|
211
|
+
generated: undefined;
|
|
212
|
+
}, {}, {}>;
|
|
213
|
+
model: drizzle_orm_pg_core.PgColumn<{
|
|
214
|
+
name: "model";
|
|
215
|
+
tableName: "messages";
|
|
216
|
+
dataType: "string";
|
|
217
|
+
columnType: "PgText";
|
|
218
|
+
data: string;
|
|
219
|
+
driverParam: string;
|
|
220
|
+
notNull: false;
|
|
221
|
+
hasDefault: false;
|
|
222
|
+
isPrimaryKey: false;
|
|
223
|
+
isAutoincrement: false;
|
|
224
|
+
hasRuntimeDefault: false;
|
|
225
|
+
enumValues: [string, ...string[]];
|
|
226
|
+
baseColumn: never;
|
|
227
|
+
identity: undefined;
|
|
228
|
+
generated: undefined;
|
|
229
|
+
}, {}, {}>;
|
|
230
|
+
metadata: drizzle_orm_pg_core.PgColumn<{
|
|
231
|
+
name: "metadata";
|
|
232
|
+
tableName: "messages";
|
|
233
|
+
dataType: "json";
|
|
234
|
+
columnType: "PgJsonb";
|
|
235
|
+
data: unknown;
|
|
236
|
+
driverParam: unknown;
|
|
237
|
+
notNull: false;
|
|
238
|
+
hasDefault: false;
|
|
239
|
+
isPrimaryKey: false;
|
|
240
|
+
isAutoincrement: false;
|
|
241
|
+
hasRuntimeDefault: false;
|
|
242
|
+
enumValues: undefined;
|
|
243
|
+
baseColumn: never;
|
|
244
|
+
identity: undefined;
|
|
245
|
+
generated: undefined;
|
|
246
|
+
}, {}, {}>;
|
|
247
|
+
createdAt: drizzle_orm_pg_core.PgColumn<{
|
|
248
|
+
name: "created_at";
|
|
249
|
+
tableName: "messages";
|
|
250
|
+
dataType: "date";
|
|
251
|
+
columnType: "PgTimestamp";
|
|
252
|
+
data: Date;
|
|
253
|
+
driverParam: string;
|
|
254
|
+
notNull: true;
|
|
255
|
+
hasDefault: true;
|
|
256
|
+
isPrimaryKey: false;
|
|
257
|
+
isAutoincrement: false;
|
|
258
|
+
hasRuntimeDefault: false;
|
|
259
|
+
enumValues: undefined;
|
|
260
|
+
baseColumn: never;
|
|
261
|
+
identity: undefined;
|
|
262
|
+
generated: undefined;
|
|
263
|
+
}, {}, {}>;
|
|
264
|
+
};
|
|
265
|
+
dialect: "pg";
|
|
266
|
+
}>;
|
|
267
|
+
type Conversation = typeof conversations.$inferSelect;
|
|
268
|
+
type NewConversation = typeof conversations.$inferInsert;
|
|
269
|
+
type Message = typeof messages.$inferSelect;
|
|
270
|
+
type NewMessage = typeof messages.$inferInsert;
|
|
271
|
+
|
|
272
|
+
type schema_Conversation = Conversation;
|
|
273
|
+
type schema_Message = Message;
|
|
274
|
+
type schema_NewConversation = NewConversation;
|
|
275
|
+
type schema_NewMessage = NewMessage;
|
|
276
|
+
declare const schema_conversations: typeof conversations;
|
|
277
|
+
declare const schema_messages: typeof messages;
|
|
278
|
+
declare namespace schema {
|
|
279
|
+
export { type schema_Conversation as Conversation, type schema_Message as Message, type schema_NewConversation as NewConversation, type schema_NewMessage as NewMessage, schema_conversations as conversations, schema_messages as messages };
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Create a new conversation
|
|
284
|
+
*/
|
|
285
|
+
declare function createChat(userId: string): Promise<string>;
|
|
286
|
+
/**
|
|
287
|
+
* Load messages for a conversation
|
|
288
|
+
*/
|
|
289
|
+
declare function loadChat(conversationId: string): Promise<UIMessage[]>;
|
|
290
|
+
/**
|
|
291
|
+
* Update conversation title
|
|
292
|
+
*/
|
|
293
|
+
declare function updateConversationTitle(chatId: string, title: string): Promise<void>;
|
|
294
|
+
/**
|
|
295
|
+
* Save messages to a conversation
|
|
296
|
+
*/
|
|
297
|
+
declare function saveChat({ chatId, messages: chatMessages, model, userId, }: {
|
|
298
|
+
chatId: string;
|
|
299
|
+
messages: UIMessage[];
|
|
300
|
+
model?: string;
|
|
301
|
+
userId: string;
|
|
302
|
+
}): Promise<void>;
|
|
303
|
+
/**
|
|
304
|
+
* Get all conversations for a user
|
|
305
|
+
*/
|
|
306
|
+
declare function getConversations(userId: string): Promise<{
|
|
307
|
+
id: string;
|
|
308
|
+
title: string;
|
|
309
|
+
createdAt: Date;
|
|
310
|
+
updatedAt: Date;
|
|
311
|
+
metadata: unknown;
|
|
312
|
+
messageCount: number;
|
|
313
|
+
}[]>;
|
|
314
|
+
/**
|
|
315
|
+
* Delete a conversation and all its messages
|
|
316
|
+
*/
|
|
317
|
+
declare function deleteConversation(chatId: string): Promise<void>;
|
|
318
|
+
|
|
319
|
+
declare const db: drizzle_orm_postgres_js.PostgresJsDatabase<typeof schema> & {
|
|
320
|
+
$client: postgres.Sql<{}>;
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
export { type Conversation, type Message, type NewConversation, type NewMessage, conversations, createChat, db, deleteConversation, getConversations, loadChat, messages, saveChat, updateConversationTitle };
|