Restore Ultracite + fix sidebar (#1233)
This commit is contained in:
parent
8fbfc253fa
commit
947ed094a6
177 changed files with 6908 additions and 8306 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import 'server-only';
|
||||
import "server-only";
|
||||
|
||||
import {
|
||||
and,
|
||||
|
|
@ -11,29 +11,28 @@ import {
|
|||
inArray,
|
||||
lt,
|
||||
type SQL,
|
||||
} from 'drizzle-orm';
|
||||
import { drizzle } from 'drizzle-orm/postgres-js';
|
||||
import postgres from 'postgres';
|
||||
|
||||
} from "drizzle-orm";
|
||||
import { drizzle } from "drizzle-orm/postgres-js";
|
||||
import postgres from "postgres";
|
||||
import type { ArtifactKind } from "@/components/artifact";
|
||||
import type { VisibilityType } from "@/components/visibility-selector";
|
||||
import { ChatSDKError } from "../errors";
|
||||
import type { AppUsage } from "../usage";
|
||||
import { generateUUID } from "../utils";
|
||||
import {
|
||||
user,
|
||||
chat,
|
||||
type User,
|
||||
document,
|
||||
type Suggestion,
|
||||
suggestion,
|
||||
message,
|
||||
vote,
|
||||
type DBMessage,
|
||||
type Chat,
|
||||
chat,
|
||||
type DBMessage,
|
||||
document,
|
||||
message,
|
||||
type Suggestion,
|
||||
stream,
|
||||
} from './schema';
|
||||
import type { ArtifactKind } from '@/components/artifact';
|
||||
import { generateUUID } from '../utils';
|
||||
import { generateHashedPassword } from './utils';
|
||||
import type { VisibilityType } from '@/components/visibility-selector';
|
||||
import { ChatSDKError } from '../errors';
|
||||
import type { AppUsage } from '../usage';
|
||||
suggestion,
|
||||
type User,
|
||||
user,
|
||||
vote,
|
||||
} from "./schema";
|
||||
import { generateHashedPassword } from "./utils";
|
||||
|
||||
// Optionally, if not using email/pass login, you can
|
||||
// use the Drizzle adapter for Auth.js / NextAuth
|
||||
|
|
@ -43,13 +42,13 @@ import type { AppUsage } from '../usage';
|
|||
const client = postgres(process.env.POSTGRES_URL!);
|
||||
const db = drizzle(client);
|
||||
|
||||
export async function getUser(email: string): Promise<Array<User>> {
|
||||
export async function getUser(email: string): Promise<User[]> {
|
||||
try {
|
||||
return await db.select().from(user).where(eq(user.email, email));
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get user by email',
|
||||
"bad_request:database",
|
||||
"Failed to get user by email"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -59,8 +58,8 @@ export async function createUser(email: string, password: string) {
|
|||
|
||||
try {
|
||||
return await db.insert(user).values({ email, password: hashedPassword });
|
||||
} catch (error) {
|
||||
throw new ChatSDKError('bad_request:database', 'Failed to create user');
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError("bad_request:database", "Failed to create user");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -73,10 +72,10 @@ export async function createGuestUser() {
|
|||
id: user.id,
|
||||
email: user.email,
|
||||
});
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to create guest user',
|
||||
"bad_request:database",
|
||||
"Failed to create guest user"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -100,8 +99,8 @@ export async function saveChat({
|
|||
title,
|
||||
visibility,
|
||||
});
|
||||
} catch (error) {
|
||||
throw new ChatSDKError('bad_request:database', 'Failed to save chat');
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError("bad_request:database", "Failed to save chat");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -116,10 +115,10 @@ export async function deleteChatById({ id }: { id: string }) {
|
|||
.where(eq(chat.id, id))
|
||||
.returning();
|
||||
return chatsDeleted;
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to delete chat by id',
|
||||
"bad_request:database",
|
||||
"Failed to delete chat by id"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -145,12 +144,12 @@ export async function getChatsByUserId({
|
|||
.where(
|
||||
whereCondition
|
||||
? and(whereCondition, eq(chat.userId, id))
|
||||
: eq(chat.userId, id),
|
||||
: eq(chat.userId, id)
|
||||
)
|
||||
.orderBy(desc(chat.createdAt))
|
||||
.limit(extendedLimit);
|
||||
|
||||
let filteredChats: Array<Chat> = [];
|
||||
let filteredChats: Chat[] = [];
|
||||
|
||||
if (startingAfter) {
|
||||
const [selectedChat] = await db
|
||||
|
|
@ -161,8 +160,8 @@ export async function getChatsByUserId({
|
|||
|
||||
if (!selectedChat) {
|
||||
throw new ChatSDKError(
|
||||
'not_found:database',
|
||||
`Chat with id ${startingAfter} not found`,
|
||||
"not_found:database",
|
||||
`Chat with id ${startingAfter} not found`
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -176,8 +175,8 @@ export async function getChatsByUserId({
|
|||
|
||||
if (!selectedChat) {
|
||||
throw new ChatSDKError(
|
||||
'not_found:database',
|
||||
`Chat with id ${endingBefore} not found`,
|
||||
"not_found:database",
|
||||
`Chat with id ${endingBefore} not found`
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -192,10 +191,10 @@ export async function getChatsByUserId({
|
|||
chats: hasMore ? filteredChats.slice(0, limit) : filteredChats,
|
||||
hasMore,
|
||||
};
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get chats by user id',
|
||||
"bad_request:database",
|
||||
"Failed to get chats by user id"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -208,20 +207,16 @@ export async function getChatById({ id }: { id: string }) {
|
|||
}
|
||||
|
||||
return selectedChat;
|
||||
} catch (error) {
|
||||
throw new ChatSDKError('bad_request:database', 'Failed to get chat by id');
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError("bad_request:database", "Failed to get chat by id");
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveMessages({
|
||||
messages,
|
||||
}: {
|
||||
messages: Array<DBMessage>;
|
||||
}) {
|
||||
export async function saveMessages({ messages }: { messages: DBMessage[] }) {
|
||||
try {
|
||||
return await db.insert(message).values(messages);
|
||||
} catch (error) {
|
||||
throw new ChatSDKError('bad_request:database', 'Failed to save messages');
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError("bad_request:database", "Failed to save messages");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -232,10 +227,10 @@ export async function getMessagesByChatId({ id }: { id: string }) {
|
|||
.from(message)
|
||||
.where(eq(message.chatId, id))
|
||||
.orderBy(asc(message.createdAt));
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get messages by chat id',
|
||||
"bad_request:database",
|
||||
"Failed to get messages by chat id"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -247,7 +242,7 @@ export async function voteMessage({
|
|||
}: {
|
||||
chatId: string;
|
||||
messageId: string;
|
||||
type: 'up' | 'down';
|
||||
type: "up" | "down";
|
||||
}) {
|
||||
try {
|
||||
const [existingVote] = await db
|
||||
|
|
@ -258,26 +253,26 @@ export async function voteMessage({
|
|||
if (existingVote) {
|
||||
return await db
|
||||
.update(vote)
|
||||
.set({ isUpvoted: type === 'up' })
|
||||
.set({ isUpvoted: type === "up" })
|
||||
.where(and(eq(vote.messageId, messageId), eq(vote.chatId, chatId)));
|
||||
}
|
||||
return await db.insert(vote).values({
|
||||
chatId,
|
||||
messageId,
|
||||
isUpvoted: type === 'up',
|
||||
isUpvoted: type === "up",
|
||||
});
|
||||
} catch (error) {
|
||||
throw new ChatSDKError('bad_request:database', 'Failed to vote message');
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError("bad_request:database", "Failed to vote message");
|
||||
}
|
||||
}
|
||||
|
||||
export async function getVotesByChatId({ id }: { id: string }) {
|
||||
try {
|
||||
return await db.select().from(vote).where(eq(vote.chatId, id));
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get votes by chat id',
|
||||
"bad_request:database",
|
||||
"Failed to get votes by chat id"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -307,8 +302,8 @@ export async function saveDocument({
|
|||
createdAt: new Date(),
|
||||
})
|
||||
.returning();
|
||||
} catch (error) {
|
||||
throw new ChatSDKError('bad_request:database', 'Failed to save document');
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError("bad_request:database", "Failed to save document");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -321,10 +316,10 @@ export async function getDocumentsById({ id }: { id: string }) {
|
|||
.orderBy(asc(document.createdAt));
|
||||
|
||||
return documents;
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get documents by id',
|
||||
"bad_request:database",
|
||||
"Failed to get documents by id"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -338,10 +333,10 @@ export async function getDocumentById({ id }: { id: string }) {
|
|||
.orderBy(desc(document.createdAt));
|
||||
|
||||
return selectedDocument;
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get document by id',
|
||||
"bad_request:database",
|
||||
"Failed to get document by id"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -359,18 +354,18 @@ export async function deleteDocumentsByIdAfterTimestamp({
|
|||
.where(
|
||||
and(
|
||||
eq(suggestion.documentId, id),
|
||||
gt(suggestion.documentCreatedAt, timestamp),
|
||||
),
|
||||
gt(suggestion.documentCreatedAt, timestamp)
|
||||
)
|
||||
);
|
||||
|
||||
return await db
|
||||
.delete(document)
|
||||
.where(and(eq(document.id, id), gt(document.createdAt, timestamp)))
|
||||
.returning();
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to delete documents by id after timestamp',
|
||||
"bad_request:database",
|
||||
"Failed to delete documents by id after timestamp"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -378,14 +373,14 @@ export async function deleteDocumentsByIdAfterTimestamp({
|
|||
export async function saveSuggestions({
|
||||
suggestions,
|
||||
}: {
|
||||
suggestions: Array<Suggestion>;
|
||||
suggestions: Suggestion[];
|
||||
}) {
|
||||
try {
|
||||
return await db.insert(suggestion).values(suggestions);
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to save suggestions',
|
||||
"bad_request:database",
|
||||
"Failed to save suggestions"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -400,10 +395,10 @@ export async function getSuggestionsByDocumentId({
|
|||
.select()
|
||||
.from(suggestion)
|
||||
.where(and(eq(suggestion.documentId, documentId)));
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get suggestions by document id',
|
||||
"bad_request:database",
|
||||
"Failed to get suggestions by document id"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -411,10 +406,10 @@ export async function getSuggestionsByDocumentId({
|
|||
export async function getMessageById({ id }: { id: string }) {
|
||||
try {
|
||||
return await db.select().from(message).where(eq(message.id, id));
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get message by id',
|
||||
"bad_request:database",
|
||||
"Failed to get message by id"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -431,28 +426,30 @@ export async function deleteMessagesByChatIdAfterTimestamp({
|
|||
.select({ id: message.id })
|
||||
.from(message)
|
||||
.where(
|
||||
and(eq(message.chatId, chatId), gte(message.createdAt, timestamp)),
|
||||
and(eq(message.chatId, chatId), gte(message.createdAt, timestamp))
|
||||
);
|
||||
|
||||
const messageIds = messagesToDelete.map((message) => message.id);
|
||||
const messageIds = messagesToDelete.map(
|
||||
(currentMessage) => currentMessage.id
|
||||
);
|
||||
|
||||
if (messageIds.length > 0) {
|
||||
await db
|
||||
.delete(vote)
|
||||
.where(
|
||||
and(eq(vote.chatId, chatId), inArray(vote.messageId, messageIds)),
|
||||
and(eq(vote.chatId, chatId), inArray(vote.messageId, messageIds))
|
||||
);
|
||||
|
||||
return await db
|
||||
.delete(message)
|
||||
.where(
|
||||
and(eq(message.chatId, chatId), inArray(message.id, messageIds)),
|
||||
and(eq(message.chatId, chatId), inArray(message.id, messageIds))
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to delete messages by chat id after timestamp',
|
||||
"bad_request:database",
|
||||
"Failed to delete messages by chat id after timestamp"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -462,14 +459,14 @@ export async function updateChatVisiblityById({
|
|||
visibility,
|
||||
}: {
|
||||
chatId: string;
|
||||
visibility: 'private' | 'public';
|
||||
visibility: "private" | "public";
|
||||
}) {
|
||||
try {
|
||||
return await db.update(chat).set({ visibility }).where(eq(chat.id, chatId));
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to update chat visibility by id',
|
||||
"bad_request:database",
|
||||
"Failed to update chat visibility by id"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -488,7 +485,7 @@ export async function updateChatLastContextById({
|
|||
.set({ lastContext: context })
|
||||
.where(eq(chat.id, chatId));
|
||||
} catch (error) {
|
||||
console.warn('Failed to update lastContext for chat', chatId, error);
|
||||
console.warn("Failed to update lastContext for chat", chatId, error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
@ -502,7 +499,7 @@ export async function getMessageCountByUserId({
|
|||
}) {
|
||||
try {
|
||||
const twentyFourHoursAgo = new Date(
|
||||
Date.now() - differenceInHours * 60 * 60 * 1000,
|
||||
Date.now() - differenceInHours * 60 * 60 * 1000
|
||||
);
|
||||
|
||||
const [stats] = await db
|
||||
|
|
@ -513,16 +510,16 @@ export async function getMessageCountByUserId({
|
|||
and(
|
||||
eq(chat.userId, id),
|
||||
gte(message.createdAt, twentyFourHoursAgo),
|
||||
eq(message.role, 'user'),
|
||||
),
|
||||
eq(message.role, "user")
|
||||
)
|
||||
)
|
||||
.execute();
|
||||
|
||||
return stats?.count ?? 0;
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get message count by user id',
|
||||
"bad_request:database",
|
||||
"Failed to get message count by user id"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -538,10 +535,10 @@ export async function createStreamId({
|
|||
await db
|
||||
.insert(stream)
|
||||
.values({ id: streamId, chatId, createdAt: new Date() });
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to create stream id',
|
||||
"bad_request:database",
|
||||
"Failed to create stream id"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -556,10 +553,10 @@ export async function getStreamIdsByChatId({ chatId }: { chatId: string }) {
|
|||
.execute();
|
||||
|
||||
return streamIds.map(({ id }) => id);
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get stream ids by chat id',
|
||||
"bad_request:database",
|
||||
"Failed to get stream ids by chat id"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue