Spaces:
Running
Running
File size: 7,972 Bytes
5012205 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
import { db } from "./db";
import { chats, messages, type Chat, type Message, MessageRole, type MessagePart, type DBMessage } from "./db/schema";
import { eq, desc, and } from "drizzle-orm";
import { nanoid } from "nanoid";
import { generateTitle } from "@/app/actions";
type AIMessage = {
role: string;
content: string | any[];
id?: string;
parts?: MessagePart[];
};
type UIMessage = {
id: string;
role: string;
content: string;
parts: MessagePart[];
createdAt?: Date;
};
type SaveChatParams = {
id?: string;
userId: string;
messages?: any[];
title?: string;
};
type ChatWithMessages = Chat & {
messages: Message[];
};
export async function saveMessages({
messages: dbMessages,
}: {
messages: Array<DBMessage>;
}) {
try {
if (dbMessages.length > 0) {
const chatId = dbMessages[0].chatId;
// First delete any existing messages for this chat
await db
.delete(messages)
.where(eq(messages.chatId, chatId));
// Then insert the new messages
return await db.insert(messages).values(dbMessages);
}
return null;
} catch (error) {
console.error('Failed to save messages in database', error);
throw error;
}
}
// Function to convert AI messages to DB format
export function convertToDBMessages(aiMessages: AIMessage[], chatId: string): DBMessage[] {
return aiMessages.map(msg => {
// Use existing id or generate a new one
const messageId = msg.id || nanoid();
// If msg has parts, use them directly
if (msg.parts) {
return {
id: messageId,
chatId,
role: msg.role,
parts: msg.parts,
createdAt: new Date()
};
}
// Otherwise, convert content to parts
let parts: MessagePart[];
if (typeof msg.content === 'string') {
parts = [{ type: 'text', text: msg.content }];
} else if (Array.isArray(msg.content)) {
if (msg.content.every(item => typeof item === 'object' && item !== null)) {
// Content is already in parts-like format
parts = msg.content as MessagePart[];
} else {
// Content is an array but not in parts format
parts = [{ type: 'text', text: JSON.stringify(msg.content) }];
}
} else {
// Default case
parts = [{ type: 'text', text: String(msg.content) }];
}
return {
id: messageId,
chatId,
role: msg.role,
parts,
createdAt: new Date()
};
});
}
// Convert DB messages to UI format
export function convertToUIMessages(dbMessages: Array<Message>): Array<UIMessage> {
return dbMessages.map((message) => ({
id: message.id,
parts: message.parts as MessagePart[],
role: message.role as string,
content: getTextContent(message), // For backward compatibility
createdAt: message.createdAt,
}));
}
export async function saveChat({ id, userId, messages: aiMessages, title }: SaveChatParams) {
// Generate a new ID if one wasn't provided
const chatId = id || nanoid();
// Check if title is provided, if not generate one
let chatTitle = title;
// Generate title if messages are provided and no title is specified
if (aiMessages && aiMessages.length > 0) {
const hasEnoughMessages = aiMessages.length >= 2 &&
aiMessages.some(m => m.role === 'user') &&
aiMessages.some(m => m.role === 'assistant');
if (!chatTitle || chatTitle === 'New Chat' || chatTitle === undefined) {
if (hasEnoughMessages) {
try {
// Use AI to generate a meaningful title based on conversation
chatTitle = await generateTitle(aiMessages);
} catch (error) {
console.error('Error generating title:', error);
// Fallback to basic title extraction if AI title generation fails
const firstUserMessage = aiMessages.find(m => m.role === 'user');
if (firstUserMessage) {
// Check for parts first (new format)
if (firstUserMessage.parts && Array.isArray(firstUserMessage.parts)) {
const textParts = firstUserMessage.parts.filter((p: MessagePart) => p.type === 'text' && p.text);
if (textParts.length > 0) {
chatTitle = textParts[0].text?.slice(0, 50) || 'New Chat';
if ((textParts[0].text?.length || 0) > 50) {
chatTitle += '...';
}
} else {
chatTitle = 'New Chat';
}
}
// Fallback to content (old format)
else if (typeof firstUserMessage.content === 'string') {
chatTitle = firstUserMessage.content.slice(0, 50);
if (firstUserMessage.content.length > 50) {
chatTitle += '...';
}
} else {
chatTitle = 'New Chat';
}
} else {
chatTitle = 'New Chat';
}
}
} else {
// Not enough messages for AI title, use first message
const firstUserMessage = aiMessages.find(m => m.role === 'user');
if (firstUserMessage) {
// Check for parts first (new format)
if (firstUserMessage.parts && Array.isArray(firstUserMessage.parts)) {
const textParts = firstUserMessage.parts.filter((p: MessagePart) => p.type === 'text' && p.text);
if (textParts.length > 0) {
chatTitle = textParts[0].text?.slice(0, 50) || 'New Chat';
if ((textParts[0].text?.length || 0) > 50) {
chatTitle += '...';
}
} else {
chatTitle = 'New Chat';
}
}
// Fallback to content (old format)
else if (typeof firstUserMessage.content === 'string') {
chatTitle = firstUserMessage.content.slice(0, 50);
if (firstUserMessage.content.length > 50) {
chatTitle += '...';
}
} else {
chatTitle = 'New Chat';
}
} else {
chatTitle = 'New Chat';
}
}
}
} else {
chatTitle = chatTitle || 'New Chat';
}
// Check if chat already exists
const existingChat = await db.query.chats.findFirst({
where: and(
eq(chats.id, chatId),
eq(chats.userId, userId)
),
});
if (existingChat) {
// Update existing chat
await db
.update(chats)
.set({
title: chatTitle,
updatedAt: new Date()
})
.where(and(
eq(chats.id, chatId),
eq(chats.userId, userId)
));
} else {
// Create new chat
await db.insert(chats).values({
id: chatId,
userId,
title: chatTitle,
createdAt: new Date(),
updatedAt: new Date()
});
}
return { id: chatId };
}
// Helper to get just the text content for display
export function getTextContent(message: Message): string {
try {
const parts = message.parts as MessagePart[];
return parts
.filter(part => part.type === 'text' && part.text)
.map(part => part.text)
.join('\n');
} catch (e) {
// If parsing fails, return empty string
return '';
}
}
export async function getChats(userId: string) {
return await db.query.chats.findMany({
where: eq(chats.userId, userId),
orderBy: [desc(chats.updatedAt)]
});
}
export async function getChatById(id: string, userId: string): Promise<ChatWithMessages | null> {
const chat = await db.query.chats.findFirst({
where: and(
eq(chats.id, id),
eq(chats.userId, userId)
),
});
if (!chat) return null;
const chatMessages = await db.query.messages.findMany({
where: eq(messages.chatId, id),
orderBy: [messages.createdAt]
});
return {
...chat,
messages: chatMessages
};
}
export async function deleteChat(id: string, userId: string) {
await db.delete(chats).where(
and(
eq(chats.id, id),
eq(chats.userId, userId)
)
);
} |