Revert "Upgrade linter and formatter to Ultracite" (#1226)

This commit is contained in:
josh 2025-09-21 12:03:29 +01:00 committed by GitHub
parent 0e320b391d
commit 1aff7d9868
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
177 changed files with 8334 additions and 6943 deletions

View file

@ -1,4 +1,4 @@
import "server-only";
import 'server-only';
import {
and,
@ -11,28 +11,29 @@ import {
inArray,
lt,
type SQL,
} 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";
} from 'drizzle-orm';
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import {
type Chat,
chat,
type DBMessage,
document,
message,
type Suggestion,
stream,
suggestion,
type User,
user,
chat,
type User,
document,
type Suggestion,
suggestion,
message,
vote,
} from "./schema";
import { generateHashedPassword } from "./utils";
type DBMessage,
type Chat,
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';
// Optionally, if not using email/pass login, you can
// use the Drizzle adapter for Auth.js / NextAuth
@ -42,13 +43,13 @@ import { generateHashedPassword } from "./utils";
const client = postgres(process.env.POSTGRES_URL!);
const db = drizzle(client);
export async function getUser(email: string): Promise<User[]> {
export async function getUser(email: string): Promise<Array<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',
);
}
}
@ -58,8 +59,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');
}
}
@ -72,10 +73,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',
);
}
}
@ -99,8 +100,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');
}
}
@ -115,10 +116,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',
);
}
}
@ -144,12 +145,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: Chat[] = [];
let filteredChats: Array<Chat> = [];
if (startingAfter) {
const [selectedChat] = await db
@ -160,8 +161,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`,
);
}
@ -175,8 +176,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`,
);
}
@ -191,10 +192,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',
);
}
}
@ -207,16 +208,20 @@ 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: DBMessage[] }) {
export async function saveMessages({
messages,
}: {
messages: Array<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');
}
}
@ -227,10 +232,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',
);
}
}
@ -242,7 +247,7 @@ export async function voteMessage({
}: {
chatId: string;
messageId: string;
type: "up" | "down";
type: 'up' | 'down';
}) {
try {
const [existingVote] = await db
@ -253,26 +258,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',
);
}
}
@ -302,8 +307,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');
}
}
@ -316,10 +321,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',
);
}
}
@ -333,10 +338,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',
);
}
}
@ -354,18 +359,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',
);
}
}
@ -373,14 +378,14 @@ export async function deleteDocumentsByIdAfterTimestamp({
export async function saveSuggestions({
suggestions,
}: {
suggestions: Suggestion[];
suggestions: Array<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',
);
}
}
@ -395,10 +400,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',
);
}
}
@ -406,10 +411,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',
);
}
}
@ -426,30 +431,28 @@ 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(
(currentMessage) => currentMessage.id
);
const messageIds = messagesToDelete.map((message) => message.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',
);
}
}
@ -459,14 +462,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',
);
}
}
@ -485,7 +488,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;
}
}
@ -499,7 +502,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
@ -510,16 +513,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',
);
}
}
@ -535,10 +538,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',
);
}
}
@ -553,10 +556,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',
);
}
}